diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml new file mode 100644 index 0000000000..80949d9d35 --- /dev/null +++ b/.github/workflows/gradle.yml @@ -0,0 +1,27 @@ +name: Java CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +permissions: read-all +jobs: + build-linux: + name: build-linux + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: '17' + cache: 'gradle' + - name: Build with Gradle + run: chmod +x gradlew && ./gradlew build diff --git a/.gitignore b/.gitignore index a7a0c33a20..c6b4f54e91 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ out/ *.iml *.vscode/ src/main/java/com/fishercoder/solutions/_99999RandomQuestions.java +src/main/java/com/fishercoder/solutions/_Contest.java +.project +bin \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 2c7042662c..0858f7c492 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,10 @@ jdk: before_script: - chmod a+x gradlew -script: ./gradlew clean build +node_js: "v13.7.0" +install: "npm install && npm install standard --global" + +script: ./gradlew clean build && standard javascript/* notifications: email: @@ -14,3 +17,6 @@ notifications: - fishercoder@gmail.com on_failure: always +branches: + only: + - master \ No newline at end of file diff --git a/README.md b/README.md index dff845e191..e5a1ed6aae 100644 --- a/README.md +++ b/README.md @@ -1,1051 +1,35 @@ -# [LeetCode](https://leetcode.com/problemset/algorithms/) [![License](https://img.shields.io/badge/license-Apache_2.0-blue.svg)](LICENSE.md) [![Build Status](https://travis-ci.org/fishercoder1534/Leetcode.svg?branch=master)](https://travis-ci.org/fishercoder1534/Leetcode) ![Language](https://img.shields.io/badge/language-Java%20%2F%20MySQL%20%2F%20Bash-blue.svg) +# [LeetCode](https://leetcode.com/problemset/algorithms/) [![License](https://img.shields.io/badge/license-Apache_2.0-blue.svg)](LICENSE.md) [![Java CI](https://github.com/fishercoder1534/Leetcode/actions/workflows/gradle.yml/badge.svg)](https://github.com/fishercoder1534/Leetcode/actions/workflows/gradle.yml) ![Language](https://img.shields.io/badge/language-Java%20%2F%20MySQL%20%2F%20Bash-blue.svg) _If you like this project, please leave me a star._ ★ > ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews) ## Algorithms +[For problems 1 to 999](https://github.com/fishercoder1534/Leetcode/tree/master/paginated_contents/algorithms/1st_thousand) -| # | Title | Solutions | Video | Difficulty | Tag -|-----|----------------|---------------|--------|-------------|------------- -|1441|[Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1441.java) | |Easy|Stack| -|1437|[Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1437.java) | |Medium|Array| -|1436|[Destination City](https://leetcode.com/problems/destination-city/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1436.java) | |Easy|String| -|1432|[Max Difference You Can Get From Changing an Integer](https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1432.java) | |Medium|String| -|1431|[Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1431.java) | |Easy|Array| -|1428|[Leftmost Column with at Least a One](https://leetcode.com/problems/leftmost-column-with-at-least-a-one/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1428.java) | |Medium|Array| -|1427|[Perform String Shifts](https://leetcode.com/problems/perform-string-shifts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1427.java) | |Easy|Array, Math| -|1426|[Counting Elements](https://leetcode.com/problems/counting-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1426.java) | |Easy|Array| -|1423|[Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1423.java) | |Medium|Array, DP, Sliding Window| -|1422|[Maximum Score After Splitting a String](https://leetcode.com/problems/maximum-score-after-splitting-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1422.java) | |Easy|String| -|1418|[Display Table of Food Orders in a Restaurant](https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1418.java) | |Medium|HashTable| -|1417|[Reformat The String](https://leetcode.com/problems/reformat-the-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1417.java) | |Easy|String| -|1415|[The k-th Lexicographical String of All Happy Strings of Length n](https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1415.java) | |Medium|Backtracking| -|1413|[Minimum Value to Get Positive Step by Step Sum](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1413.java) | |Easy|Array| -|1410|[HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1410.java) | |Medium|String, Stack| -|1409|[Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1409.java) | |Medium|Array| -|1408|[String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1408.java) | |Easy|String| -|1403|[Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1403.java) | |Easy|Greedy, Sort| -|1401|[Circle and Rectangle Overlapping](https://leetcode.com/problems/circle-and-rectangle-overlapping/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1401.java) | |Medium|Geometry| -|1400|[Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1400.java) | |Medium|Greedy| -|1399|[Count Largest Group](https://leetcode.com/problems/count-largest-group/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1399.java) | |Easy|Array| -|1396|[Design Underground System](https://leetcode.com/problems/design-underground-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1396.java) | |Medium|Design| -|1395|[Count Number of Teams](https://leetcode.com/problems/count-number-of-teams/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1395.java) | |Medium|Array| -|1394|[Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1394.java) | |Easy|Array| -|1392|[Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1392.java) | |Hard|String, Rolling Hash| -|1390|[Four Divisors](https://leetcode.com/problems/four-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1390.java) | |Medium|Math| -|1389|[Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1389.java) | |Easy|Array| -|1388|[Pizza With 3n Slices](https://leetcode.com/problems/pizza-with-3n-slices/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1388.java) | |Hard|DP| -|1387|[Sort Integers by The Power Value](https://leetcode.com/problems/sort-integers-by-the-power-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1387.java) | |Medium|Sort, Graph| -|1386|[Cinema Seat Allocation](https://leetcode.com/problems/cinema-seat-allocation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1386.java) | |Medium|Array, Greedy| -|1385|[Find the Distance Value Between Two Arrays](https://leetcode.com/problems/find-the-distance-value-between-two-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1385.java) | |Easy|Array| -|1382|[Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1382.java) | |Medium|Binary Search Tree| -|1381|[Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1381.java) | |Medium|Stack, Design| -|1380|[Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1380.java) | |Easy|Array| -|1379|[Find a Corresponding Node of a Binary Tree in a Clone of That Tree](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1379.java) | |Medium|Tree| -|1377|[Frog Position After T Seconds](https://leetcode.com/problems/frog-position-after-t-seconds/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1377.java) | |Hard|DFS, BFS| -|1376|[Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1376.java) | |Medium|DFS| -|1375|[Bulb Switcher III](https://leetcode.com/problems/bulb-switcher-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1375.java) | |Medium|Array| -|1374|[Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1374.java) | |Easy|String| -|1373|[Maximum Sum BST in Binary Tree](https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1373.java) | |Hard|DP, BST| -|1372|[Longest ZigZag Path in a Binary Tree](https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1372.java) | |Hard|DP, Tree| -|1371|[Find the Longest Substring Containing Vowels in Even Counts](https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1371.java) | |Medium|String| -|1370|[Increasing Decreasing String](https://leetcode.com/problems/increasing-decreasing-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1370.java) | |Easy|String, Sort| -|1367|[Linked List in Binary Tree](https://leetcode.com/problems/linked-list-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1367.java) | |Medium|DP, Linked List, Tree| -|1366|[Rank Teams by Votes](https://leetcode.com/problems/rank-teams-by-votes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1366.java) | |Medium|Array, Sort| -|1365|[How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1365.java) | |Easy|Array, HashTable| -|1362|[Closest Divisors](https://leetcode.com/problems/closest-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1362.java) | |Medium|Math| -|1361|[Validate Binary Tree Nodes](https://leetcode.com/problems/validate-binary-tree-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1361.java) | |Medium|Graph -|1360|[Number of Days Between Two Dates](https://leetcode.com/problems/number-of-days-between-two-dates/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1360.java) | |Easy|| -|1358|[Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1358.java) | |Medium|String| -|1357|[Apply Discount Every n Orders](https://leetcode.com/problems/apply-discount-every-n-orders/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1357.java) | |Medium|Design| -|1356|[Sort Integers by The Number of 1 Bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1356.java) | |Easy|Sort, Bit Manipulation| -|1354|[Construct Target Array With Multiple Sums](https://leetcode.com/problems/construct-target-array-with-multiple-sums/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1354.java) | |Hard|Greedy| -|1353|[Maximum Number of Events That Can Be Attended](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1353.java) | |Medium|Greedy, Sort, Segment Tree| -|1352|[Product of the Last K Numbers](https://leetcode.com/problems/product-of-the-last-k-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1352.java) | |Medium|Array, Design| -|1351|[Count Negative Numbers in a Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1351.java) | |Easy|Array, Binary Search| -|1349|[Maximum Students Taking Exam](https://leetcode.com/problems/maximum-students-taking-exam/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1349.java) | |Hard|Dynamic Programming| -|1348|[Tweet Counts Per Frequency](https://leetcode.com/problems/tweet-counts-per-frequency/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1348.java) | |Medium|Design| -|1347|[Minimum Number of Steps to Make Two Strings Anagram](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1347.java) | |Easy|String| -|1346|[Check If N and Its Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1346.java) | |Easy|Array| -|1345|[Jump Game IV](https://leetcode.com/problems/jump-game-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1345.java) | |Hard|BFS| -|1344|[Angle Between Hands of a Clock](https://leetcode.com/problems/angle-between-hands-of-a-clock/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1344.java) | |Medium|Math| -|1343|[Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium|Array| -|1342|[Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1342.java) | |Easy|Bit Manipulation| -|1341|[The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1341.java) | |Easy|| -|1339|[Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1339.java) | |Medium|DP, Tree| -|1338|[Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1338.java) | |Medium|| -|1337|[Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1337.java) | |Easy|String| -|1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1333.java) | |Medium|| -|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy|| -|1329|[Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1329.java) | |Medium|| -|1325|[Delete Leaves With a Given Value](https://leetcode.com/problems/delete-leaves-with-a-given-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1325.java) | |Medium|Tree| -|1324|[Print Words Vertically](https://leetcode.com/problems/print-words-vertically/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1324.java) | |Medium|String| -|1323|[Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1323.java) | |Easy|Math| -|1317|[Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1317.java) | |Easy|| -|1315|[Sum of Nodes with Even-Valued Grandparent](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1315.java) | |Medium|Tree, DFS| -|1314|[Matrix Block Sum](https://leetcode.com/problems/matrix-block-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1314.java) | |Medium|Dynamic Programming| -|1313|[Decompress Run-Length Encoded List](https://leetcode.com/problems/decompress-run-length-encoded-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1313.java) | |Easy|Array| -|1305|[All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1305.java) | |Medium|| -|1304|[Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1304.java) | |Easy|| -|1302|[Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1302.java) | |Medium|| -|1300|[Sum of Mutated Array Closest to Target](https://leetcode.com/problems/sum-of-mutated-array-closest-to-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1300.java) | |Medium|| -|1299|[Replace Elements with Greatest Element on Right Side](https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1299.java) | |Easy|| -|1297|[Maximum Number of Occurrences of a Substring](https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1297.java) | |Medium|| -|1296|[Divide Array in Sets of K Consecutive Numbers](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1296.java) | |Medium|| -|1295|[Find Numbers with Even Number of Digits](https://leetcode.com/problems/find-numbers-with-even-number-of-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1295.java) | [:tv:](https://youtu.be/HRp8mNJvLZ0) |Easy|| -|1291|[Sequential Digits](https://leetcode.com/problems/sequential-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1291.java) | |Medium|| -|1290|[Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1290.java) | |Easy|| -|1289|[Minimum Falling Path Sum II](https://leetcode.com/problems/minimum-falling-path-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1289.java) | |Hard|Dynamic Programming| -|1287|[Element Appearing More Than 25% In Sorted Array](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1287.java) | [:tv:](https://youtu.be/G74W8v2yVjY) |Easy|| -|1286|[Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1286.java) | |Medium|Backtracking, Design| -|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1282.java) | [:tv:](https://www.youtube.com/watch?v=wGgcRCpSAa8)|Medium|| -|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | |Easy|| -|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1277.java) | |Medium|| -|1275|[Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1275.java) | |Easy|Array| -|1273|[Delete Tree Nodes](https://leetcode.com/problems/delete-tree-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1273.java) | |Medium|Dynamic Programming, DFS | -|1271|[Hexspeak](https://leetcode.com/problems/hexspeak/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1271.java) | |Easy|| -|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1267.java) | |Medium|| -|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | |Easy|| -|1265|[Print Immutable Linked List in Reverse](https://leetcode.com/problems/print-immutable-linked-list-in-reverse//)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1265.java) | |Medium|| -|1261|[Find Elements in a Contaminated Binary Tree](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1261.java) ||Medium|Tree, HashTable| -|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy|| -|1258|[Synonymous Sentences](https://leetcode.com/problems/synonymous-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1258.java) ||Medium|Backtracking| -|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | |Easy|| -|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | |Easy|| -|1243|[Array Transformation](https://leetcode.com/problems/array-transformation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1243.java) | [:tv:](https://www.youtube.com/watch?v=MQ2i4T1l-Gs)|Easy|| -|1232|[Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1232.java) | [:tv:](https://www.youtube.com/watch?v=_tfiTQNZCbs) |Easy|| -|1228|[Missing Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1228.java) | |Easy|| -|1221|[Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1221.java) | |Easy|Greedy| -|1219|[Path with Maximum Gold](https://leetcode.com/problems/path-with-maximum-gold/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1219.java) | |Medium|Backtracking| -|1217|[Play with Chips](https://leetcode.com/problems/play-with-chips/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1217.java) | |Easy|Array, Math, Greedy| -|1214|[Two Sum BSTs](https://leetcode.com/problems/two-sum-bsts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1214.java) | |Medium| Binary Search Tree| -|1213|[Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1213.java) | [:tv:](https://www.youtube.com/watch?v=zceoOrHSHNQ)|Easy|| -|1209|[Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) ||Medium|Stack| -|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | [:tv:](https://www.youtube.com/watch?v=_NYimlZY1PE)|Easy|| -|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1200.java) | [:tv:](https://www.youtube.com/watch?v=mH1aEjOEjcQ)|Easy|| -|1198|[Find Smallest Common Element in All Rows](https://leetcode.com/problems/find-smallest-common-element-in-all-rows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1198.java) | [:tv:](https://www.youtube.com/watch?v=RMiofZrTmWo)|Easy|| -|1196|[How Many Apples Can You Put into the Basket](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1196.java) | [:tv:](https://www.youtube.com/watch?v=UelshlMQNJM)|Easy|| -|1190|[Reverse Substrings Between Each Pair of Parentheses](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1190.java) | |Medium|Stack| -|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1189.java) |[:tv:](https://youtu.be/LGgMZC0vj5s) |Easy|| -|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1185.java) | |Easy|| -|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1184.java) | [:tv:](https://www.youtube.com/watch?v=RFq7yA5iyhI)|Easy|| -|1182|[Shortest Distance to Target Color](https://leetcode.com/problems/shortest-distance-to-target-color/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1182.java) ||Medium|Binary Search| -|1180|[Count Substrings with Only One Distinct Letter](https://leetcode.com/problems/count-substrings-with-only-one-distinct-letter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1180.java) | |Easy|Math, String| -|1176|[Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1176.java) | |Easy|Array, Sliding Window| -|1175|[Prime Arrangements](https://leetcode.com/problems/prime-arrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1175.java) | |Easy|| -|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java) | |Easy|| -|1161|[Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1161.java) | |Medium|Graph| -|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)| |Easy|| -|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | |Easy|| -|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java)|[:tv:](https://youtu.be/-t2cdVs9cKk) |Easy|| -|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | |Easy|| -|1134|[Armstrong Number](https://leetcode.com/problems/armstrong-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1134.java) | [:tv:](https://www.youtube.com/watch?v=HTL7fd4HPf4)|Easy|| -|1133|[Largest Unique Number](https://leetcode.com/problems/largest-unique-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1133.java) | [:tv:](https://youtu.be/Fecpt1YZlCs)|Easy|| -|1128|[Number of Equivalent Domino Pairs](https://leetcode.com/problems/number-of-equivalent-domino-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1128.java) | [:tv:](https://www.youtube.com/watch?v=7EpEEHAAxyw)|Easy|| -|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | |Easy|| -|1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1170.java) | |Easy|| -|1119|[Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1119.java) | [:tv:](https://www.youtube.com/watch?v=6KCBrIWEauw)|Easy|| -|1118|[Number of Days in a Month](https://leetcode.com/problems/number-of-days-in-a-month/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1118.java) | |Easy|| -|1108|[Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1108.java) | [:tv:](https://www.youtube.com/watch?v=FP0Na-pL0qk)|Easy|| -|1104|[Path In Zigzag Labelled Binary Tree](https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1104.java) | |Medium|Math, Tree| -|1103|[Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1103.java) | |Easy|Math| -|1100|[Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1100.java) | |Medium|String, Sliding Window| -|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java) | [:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI)|Easy|| -|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | |Easy|| -|1087|[Brace Expansion](https://leetcode.com/problems/brace-expansion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1087.java) | |Medium|Backtracking| -|1086|[High Five](https://leetcode.com/problems/high-five/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1086.java) | [:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc)|Easy|| -|1085|[Sum of Digits in the Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1085.java)| [:tv:](https://www.youtube.com/watch?v=GKYmPuHZpQg)|Easy|| -|1079|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1079.java)| |Medium|| -|1078|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1078.java) | |Easy|| -|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1071.java) | |Easy|| -|1066|[Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1066.java) | |Medium|Backtracking, DP| -|1065|[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java) | |Medium|| -|1062|[Longest Repeating Substring](https://leetcode.com/problems/longest-repeating-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1062.java) | |Medium|| -|1057|[Campus Bikes](https://leetcode.com/problems/campus-bikes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1057.java) | |Medium||Greedy, Sort -|1056|[Confusing Number](https://leetcode.com/problems/confusing-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1056.java) | |Easy|| -|1055|[Fixed Point](https://leetcode.com/problems/fixed-point/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1055.java) | |Easy|| -|1051|[Height Checker](https://leetcode.com/problems/height-checker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1051.java) | |Easy|| -|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1047.java) | |Easy|| -|1046|[Last Stone Weight](https://leetcode.com/problems/last-stone-weight/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1046.java) |[:tv:](https://youtu.be/IfElFyaEV8s) |Easy|| -|1043|[Partition Array for Maximum Sum](https://leetcode.com/problems/partition-array-for-maximum-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1043.java) | |Medium|DP| -|1038|[Binary Search Tree to Greater Sum Tree](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1038.java) | |Medium|DFS, tree| -|1037|[Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1037.java) | |Easy|Math| -|1033|[Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1033.java) | |Easy|Math| -|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1030.java) | |Easy| -|1029|[Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1029.java) | |Easy| -|1025|[Divisor Game](https://leetcode.com/problems/divisor-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1025.java) | |Easy| -|1022|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1022.java) | |Easy| -|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | |Easy| -|1020|[Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1020.java) | |Medium|Graph, DFS, BFS, recursion| -|1019|[Next Greater Node In Linked List](https://leetcode.com/problems/next-greater-node-in-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1019.java) | |Medium|Linked List, Stack| -|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java) | |Easy| -|1014|[Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1014.java) | |Medium| -|1013|[Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1013.java) | |Easy| -|1011|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1011.java) | |Medium|Binary Search| -|1010|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1010.java) | |Easy| -|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1009.java) | |Easy| -|1008|[Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1008.java) | |Medium| Recursion -|1005|[Maximize Sum Of Array After K Negations](https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1005.java) | [:tv:](https://youtu.be/spiwTAuz1_4) |Easy| -|1003|[Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1003.java) | |Medium| -|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1002.java) | |Easy| -|999|[Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_999.java) | |Easy| -|997|[Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_997.java) | |Easy| -|994|[Rotting Oranges](https://leetcode.com/problems/rotting-oranges/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_994.java) | |Easy| BFS -|993|[Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_993.java) | |Easy| Tree, BFS -|989|[Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_989.java) | |Easy| Array -|988|[Smallest String Starting From Leaf](https://leetcode.com/problems/smallest-string-starting-from-leaf/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_988.java) | |Medium| Tree, DFS -|987|[Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_987.java) | |Medium| Recursion -|986|[Interval List Intersections](https://leetcode.com/problems/interval-list-intersections/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_986.java) | |Medium| Two Pointers -|985|[Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_985.java) | |Easy| Array -|980|[Unique Paths III](https://leetcode.com/problems/unique-paths-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_980.java) | |Hard| Backtracking, DFS -|979|[Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_979.java) | |Medium| Recursion -|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_977.java) | |Easy| Array -|976|[Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_976.java) | |Easy| Math Array -|974|[Subarray Sums Divisible by K](https://leetcode.com/problems/subarray-sums-divisible-by-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_974.java) | |Medium| Array| -|973|[K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_973.java) | |Easy| Math Sort| -|970|[Powerful Integers](https://leetcode.com/problems/powerful-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_970.java) | |Easy| Math -|966|[Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_966.java) | |Medium| Hash Table, String -|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | |Easy| DFS, recursion| -|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | |Easy| -|958|[Check Completeness of a Binary Tree](https://leetcode.com/problems/check-completeness-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_958.java) | |Medium|Tree -|954|[Array of Doubled Pairs](https://leetcode.com/problems/array-of-doubled-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_954.java) | [:tv:](https://youtu.be/Q0WKzdpR74o)|Medium| -|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java)| |Easy| -|951|[Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_951.java) | |Medium| Tree, DFS, recursion| -|950|[Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_950.java) | |Medium| -|946|[Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_946.java) | |Medium|Stack -|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | |Easy| -|942|[DI String Match](https://leetcode.com/problems/di-string-match/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_942.java) | |Easy| -|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | |Easy| -|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_938.java) | |Medium| BST, recursion, DFS -|937|[Reorder Log Files](https://leetcode.com/problems/reorder-log-files/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_937.java) | |Easy| -|935|[Knight Dialer](https://leetcode.com/problems/knight-dialer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_935.java) | |Medium| -|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | |Easy| -|931|[Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_931.java) | |Medium|Dynamic Programming -|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_929.java) | |Easy| -|925|[Long Pressed Name](https://leetcode.com/problems/long-pressed-name/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_925.java) | |Easy| -|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | |Easy| -|921|[Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_921.java) | |Medium|Stack, Greedy -|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_917.java) | |Easy| -|914|[X of a Kind in a Deck of Cards](https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_914.java) | |Easy| -|912|[Sort an Array](https://leetcode.com/problems/sort-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_912.java) | |Easy| -|908|[Smallest Range I](https://leetcode.com/problems/smallest-range-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_908.java) | |Easy| -|900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | |Medium| -|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_897.java) | |Easy| DFS, recursion -|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | |Easy| -|890|[Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_890.java) | |Medium| -|893|[Groups of Special-Equivalent Strings](https://leetcode.com/problems/groups-of-special-equivalent-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_893.java) | [:tv:](https://youtu.be/tbtXPKkA2Zw) |Easy| -|888|[Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_888.java) | |Easy| -|885|[Spiral Matrix III](https://leetcode.com/problems/spiral-matrix-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_885.java) |[:tv:](https://www.youtube.com/watch?v=0qep3f9cqVs) |Medium| -|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | |Easy| -|883|[Projection Area of 3D Shapes](https://leetcode.com/problems/projection-area-of-3d-shapes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_883.java) | |Easy|Math -|880|[Decoded String at Index](https://leetcode.com/problems/decoded-string-at-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_880.java) | |Medium|Stack -|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | |Easy| -|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | |Easy| DFS, recursion -|868|[Binary Gap](https://leetcode.com/problems/binary-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_868.java) | |Easy| -|867|[Transpose Matrix](https://leetcode.com/problems/transpose-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_867.java) | |Easy| -|860|[Lemonade Change](https://leetcode.com/problems/lemonade-change/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_860.java) | |Easy| -|859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_859.java) | |Easy| -|856|[Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_856.java) | |Medium| Stack, String -|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | |Easy| -|849|[Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_849.java) | |Easy| -|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | |Easy| -|841|[Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_841.java) | |Easy|DFS, Graph -|840|[Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_840.java) | |Easy| -|836|[Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_836.java) | [:tv:](https://youtu.be/o6hHUk4DOW0) |Easy| -|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | |Easy| -|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java) | |Easy| -|824|[Goat Latin](https://leetcode.com/problems/goat-latin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_824.java) | |Easy| -|821|[Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_821.java) | |Easy| -|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_819.java) | |Easy| HashMap -|814|[Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_814.java) | |Medium| recursion, DFS -|811|[Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_811.java) | |Easy| HashMap -|807|[Max Increase to Keep City Skyline](https://leetcode.com/problems/max-increase-to-keep-city-skyline/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_807.java) | |Medium| -|806|[Number of Lines To Write String](https://leetcode.com/problems/number-of-lines-to-write-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_806.java) | |Easy| -|804|[Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_804.java) | |Easy| -|800|[Similar RGB Color](https://leetcode.com/problems/similar-rgb-color/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_800.java) | |Easy| -|799|[Champagne Tower](https://leetcode.com/problems/champagne-tower/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_799.java) | |Medium| -|796|[Rotate String](https://leetcode.com/problems/rotate-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_796.java) | |Easy| -|791|[Custom Sort String](https://leetcode.com/problems/custom-sort-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_791.java) | |Medium| -|789|[Escape The Ghosts](https://leetcode.com/problems/escape-the-ghosts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_789.java) | |Medium| Math| -|788|[Rotated Digits](https://leetcode.com/problems/rotated-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_788.java) | |Easy| -|784|[Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_784.java) | |Easy| -|783|[Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_783.java) | |Easy| -|779|[K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_779.java) | |Medium| -|776|[Split BST](https://leetcode.com/problems/split-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_776.java) | |Medium| Recursion -|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_771.java) | |Easy| -|769|[Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_769.java) | |Medium| Array -|767|[Reorganize String](https://leetcode.com/problems/reorganize-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_767.java) | |Medium| -|766|[Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_766.java) | |Easy| -|765|[Couples Holding Hands](https://leetcode.com/problems/couples-holding-hands/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_765.java) | |Hard| -|764|[Largest Plus Sign](https://leetcode.com/problems/largest-plus-sign/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_764.java) | |Medium| DP -|763|[Partition Labels](https://leetcode.com/problems/partition-labels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_763.java) | |Medium| -|762|[Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_762.java) | |Easy| -|760|[Find Anagram Mappings](https://leetcode.com/problems/find-anagram-mappings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_760.java) | |Easy| -|758|[Bold Words in String](https://leetcode.com/problems/bold-words-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_758.java) | |Easy| -|756|[Pyramid Transition Matrix](https://leetcode.com/problems/pyramid-transition-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_756.java) | |Medium| Backtracking -|755|[Pour Water](https://leetcode.com/problems/pour-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_755.java) ||Medium| Array -|754|[Reach a Number](https://leetcode.com/problems/reach-a-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_754.java) ||Medium| Math -|750|[Number Of Corner Rectangles](https://leetcode.com/problems/number-of-corner-rectangles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_750.java) | |Medium| -|748|[Shortest Completing Word](https://leetcode.com/problems/shortest-completing-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_748.java) | |Easy| -|747|[Largest Number Greater Than Twice of Others](https://leetcode.com/problems/largest-number-greater-than-twice-of-others/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_747.java) | |Easy| -|746|[Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_746.java) | |Easy| -|744|[Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_744.java) | | Easy| -|743|[Network Delay Time](https://leetcode.com/problems/network-delay-time/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_743.java) || Medium|Graph, Djikstra| -|742|[Closest Leaf in a Binary Tree](https://leetcode.com/problems/closest-leaf-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_742.java) | |Medium|Tree -|740|[Delete and Earn](https://leetcode.com/problems/delete-and-earn/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_740.java) | |Medium| -|739|[Daily Temperatures](https://leetcode.com/problems/daily-temperatures/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_739.java) | |Medium| -|738|[Monotone Increasing Digits](https://leetcode.com/problems/monotone-increasing-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_738.java) | |Medium| -|737|[Sentence Similarity II](https://leetcode.com/problems/sentence-similarity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_737.java) | |Medium| Union Find -|735|[Asteroid Collision](https://leetcode.com/problems/asteroid-collision/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_735.java) | |Medium | Stack -|734|[Sentence Similarity](https://leetcode.com/problems/sentence-similarity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_734.java) | |Easy | HashTable -|733|[Flood Fill](https://leetcode.com/problem**__**s/flood-fill/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_733.java) | | Easy | BFS, DFS -|729|[My Calendar I](https://leetcode.com/problems/my-calendar-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_729.java) ||Medium | -|728|[Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_728.java) | |Easy | -|727|[Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_727.java) | |Hard | DP -|725|[Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_725.java) | |Medium | LinkedList -|724|[Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_724.java) | |Easy | Array -|723|[Candy Crush](https://leetcode.com/problems/candy-crush/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_723.java) | |Medium | Array, Two Pointers -|721|[Accounts Merge](https://leetcode.com/problems/accounts-merge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_721.java) | |Medium | DFS, Union Find -|720|[Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_720.java) | |Easy | Trie -|719|[Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_719.java) | |Hard | Binary Search -|718|[Maximum Length of Repeated Subarray](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_718.java) | |Medium | DP -|717|[1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_717.java) | |Easy | -|716|[Max Stack](https://leetcode.com/problems/max-stack/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_716.java) | |Hard| Design -|714|[Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_714.java) | |Medium | DP -|713|[Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_713.java) ||Medium | -|712|[Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_712.java) | |Medium | DP -|709|[To Lower Case](https://leetcode.com/problems/to-lower-case/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_709.java) | |Easy| String -|706|[Design HashMap](https://leetcode.com/problems/design-hashmap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_706.java) | |Easy| Design -|705|[Design HashSet](https://leetcode.com/problems/design-hashset/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_705.java) | |Easy| Design -|704|[Binary Search](https://leetcode.com/problems/binary-search/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_704.java) | |Easy| Binary Search -|703|[Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_703.java) | |Easy| -|701|[Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_701.java) | |Medium | DFS, recursion -|700|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_700.java) | |Easy| recusion, dfs -|699|[Falling Squares](https://leetcode.com/problems/falling-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_699.java) ||Hard | Segment Tree -|698|[Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_698.java) | |Medium | Backtracking -|697|[Degree of an Array](https://leetcode.com/problems/degree-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_697.java) ||Easy | -|696|[Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_696.java) | |Easy | -|695|[Max Area of Island](https://leetcode.com/problems/max-area-of-island/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_695.java) | |Easy | DFS -|694|[Number of Distinct Islands](https://leetcode.com/problems/number-of-distinct-islands/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_694.java) | |Medium | DFS -|693|[Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_693.java) | |Easy | -|692|[Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_692.java) | | Medium | -|691|[Stickers to Spell Word](https://leetcode.com/problems/stickers-to-spell-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_691.java) | | Hard | DP -|690|[Employee Importance](https://leetcode.com/problems/employee-importance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_690.java) | |Easy | DFS -|689|[Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_689.java) | |Hard | DP -|688|[Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_688.java) | |Medium | DP -|687|[Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_687.java) | |Easy | DFS -|686|[Repeated String Match](https://leetcode.com/problems/repeated-string-match/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_686.java) | |Easy | -|685|[Redundant Connection II](https://leetcode.com/problems/redundant-connection-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_685.java) | | Hard | Union Find -|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_684.java) | |Medium | Union Find -|683|[K Empty Slots](https://leetcode.com/problems/k-empty-slots/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_683.java) | |Hard | -|682|[Baseball Game](https://leetcode.com/problems/baseball-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_682.java) | |Easy | -|681|[Next Closest Time](https://leetcode.com/problems/parents-closest-time/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_681.java) | |Medium | -|680|[Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_680.java) | |Easy | String -|679|[24 Game](https://leetcode.com/problems/24-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_679.java) | |Hard | Recursion -|678|[Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_678.java) | |Medium| Recursion, Greedy -|677|[Map Sum Pairs](https://leetcode.com/problems/map-sum-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_677.java) | |Medium | HashMap, Trie -|676|[Implement Magic Dictionary](https://leetcode.com/problems/implement-magic-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_676.java) | | Medium | -|675|[Cut Off Trees for Golf Event](https://leetcode.com/problems/cut-off-trees-for-golf-event/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_675.java) | |Hard | BFS -|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_674.java) | |Easy | -|673|[Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_673.java) | |Medium | DP -|672|[Bulb Switcher II](https://leetcode.com/problems/bulb-switcher-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_672.java) | |Medium | Math -|671|[Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_671.java) | |Easy | Tree, DFS -|670|[Maximum Swap](https://leetcode.com/problems/maximum-swap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_670.java) | |Medium | String -|669|[Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_669.java) | |Easy | Tree, DFS -|668|[Kth Smallest Number in Multiplication Table](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_668.java) | |Hard | Binary Search -|667|[Beautiful Arrangement II](https://leetcode.com/problems/beautiful-arrangement-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_667.java) | |Medium | Array -|666|[Path Sum IV](https://leetcode.com/problems/path-sum-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_666.java) | |Medium | Tree, DFS -|665|[Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_665.java) | |Easy | -|664|[Strange Printer](https://leetcode.com/problems/strange-printer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_664.java) | |Hard | DP -|663|[Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_663.java) | |Medium | Tree -|662|[Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_662.java) | |Medium | BFS, DFS -|661|[Image Smoother](https://leetcode.com/problems/image-smoother/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_661.java) | |Easy | Array -|660|[Remove 9](https://leetcode.com/problems/remove-9/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_660.java) | |Hard | Math -|659|[Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_659.java) | |Medium | HashMap -|658|[Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_658.java) | |Medium | -|657|[Judge Route Circle](https://leetcode.com/problems/judge-route-circle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_657.java) | |Easy | -|656|[Coin Path](https://leetcode.com/problems/coin-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_656.java) | |Hard | DP -|655|[Print Binary Tree](https://leetcode.com/problems/print-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_655.java) | |Medium | Recursion -|654|[Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_654.java) | |Medium | Tree -|653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_653.java) | |Easy | Tree -|652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_652.java) | |Medium | Tree -|651|[4 Keys Keyboard](https://leetcode.com/problems/4-keys-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_651.java) | |Medium | DP -|650|[2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_650.java) | |Medium | DP -|649|[Dota2 Senate](https://leetcode.com/problems/dota2-senate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_649.java) | |Medium | Greedy -|648|[Replace Words](https://leetcode.com/problems/replace-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_648.java) | |Medium | Trie -|647|[Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_647.java) | |Medium | DP -|646|[Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_646.java) | |Medium | DP, Greedy -|645|[Set Mismatch](https://leetcode.com/problems/set-mismatch/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_645.java) | |Easy | -|644|[Maximum Average Subarray II](https://leetcode.com/problems/maximum-average-subarray-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_644.java) | |Hard | Binary Search -|643|[Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_643.java) | | Easy | -|642|[Design Search Autocomplete System](https://leetcode.com/problems/design-search-autocomplete-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_642.java) | |Hard | Design -|640|[Solve the Equation](https://leetcode.com/problems/solve-the-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_640.java) | |Medium | -|639|[Decode Ways II](https://leetcode.com/problems/decode-ways-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_639.java) | |Hard| DP -|638|[Shopping Offers](https://leetcode.com/problems/shopping-offers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_638.java) | |Medium | DP, DFS -|637|[Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_637.java) | |Easy | -|636|[Exclusive Time of Functions](https://leetcode.com/problems/exclusive-time-of-functions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_636.java) | |Medium | Stack -|635|[Design Log Storage System](https://leetcode.com/problems/design-log-storage-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_635.java) | |Medium | Design -|634|[Find the Derangement of An Array](https://leetcode.com/problems/find-the-derangement-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_634.java) | |Medium | Math -|633|[Sum of Square Numbers](https://leetcode.com/problems/sum-of-square-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_633.java) | |Easy | Binary Search -|632|[Smallest Range](https://leetcode.com/problems/smallest-range/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_632.java) | |Hard| Heap -|631|[Design Excel Sum Formula](https://leetcode.com/problems/design-excel-sum-formula/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_631.java) | |Hard| Design, Topological Sort -|630|[Course Schedule III](https://leetcode.com/problems/course-schedule-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_630.java) | |Hard| Heap, Greedy -|629|[K Inverse Pairs Array](https://leetcode.com/problems/k-inverse-pairs-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_629.java) | |Hard| DP -|628|[Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_628.java) | |Easy | -|625|[Minimum Factorization](https://leetcode.com/problems/minimum-factorization/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_625.java) | |Medium | -|624|[Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_624.java) | |Easy | Sort, Array -|623|[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_623.java) | |Medium | Tree -|621|[Task Scheduler](https://leetcode.com/problems/task-scheduler/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_621.java) | |Medium | Greedy, Queue -|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_617.java) | |Easy | Tree, Recursion -|616|[Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_616.java) | | Medium | String -|611|[Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_611.java) | |Medium | Binary Search -|609|[Find Duplicate File in System](https://leetcode.com/problems/find-duplicate-file-in-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_609.java) | |Medium | HashMap -|606|[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_606.java) | |Easy | Tree, Recursion -|605|[Can Place Flowers](https://leetcode.com/problems/can-place-flowers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_605.java) | |Easy | Array -|604|[Design Compressed String Iterator](https://leetcode.com/problems/design-compressed-string-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_604.java) | |Easy |Design, String -|600|[Non-negative Integers without Consecutive Ones](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_600.java) | |Hard | Bit Manipulation, DP -|599|[Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_599.java) | |Easy | HashMap -|598|[Range Addition II](https://leetcode.com/problems/range-addition-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_598.java) | |Easy | -|594|[Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_594.java) | |Easy | Array, HashMap -|593|[Valid Square](https://leetcode.com/problems/valid-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_593.java) | |Medium | Math -|592|[Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_592.java) | |Medium | Math -|591|[Tag Validator](https://leetcode.com/problems/tag-validator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_591.java) | |Hard | Stack, String -|590|[N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_590.java) | |Easy| DFS, recursion -|589|[N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_589.java) | |Easy | DFS, recursion -|588|[Design In-Memory File System](https://leetcode.com/problems/design-in-memory-file-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_588.java) | |Hard | Trie, Design -|587|[Erect the Fence](https://leetcode.com/problems/erect-the-fence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_587.java) | |Hard | Geometry -|583|[Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_583.java) | |Medium | DP -|582|[Kill Process](https://leetcode.com/problems/kill-process/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_582.java) | |Medium | Stack -|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_581.java) | |Easy | Array, Sort -|576|[Out of Boundary Paths](https://leetcode.com/problems/out-of-boundary-paths/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_576.java) | |Hard | DP, DFS -|575|[Distribute Candies](https://leetcode.com/problems/distribute-candies/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_575.java) | |Easy | Array -|573|[Squirrel Simulation](https://leetcode.com/problems/squirrel-simulation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_573.java) | |Medium | Math -|572|[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_572.java) | |Easy | Tree -|568|[Maximum Vacation Days](https://leetcode.com/problems/maximum-vacation-days/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_568.java) | |Hard | DP -|567|[Permutation in String](https://leetcode.com/problems/permutation-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_567.java) | | Medium | Sliding Windows, Two Pointers -|566|[Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_566.java) | |Easy | -|565|[Array Nesting](https://leetcode.com/problems/array-nesting/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_565.java) | | Medium | -|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_563.java) | |Easy | Tree Recursion -|562|[Longest Line of Consecutive One in Matrix](https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_562.java) | |Medium | Matrix DP -|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_561.java) | |Easy | Array -|560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_560.java) || Medium | Array, HashMap -|559|[Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_559.java) | |Easy | DFS, recursion -|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_557.java) | |Easy | String -|556|[Next Greater Element III](https://leetcode.com/problems/parents-greater-element-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_556.java) | |Medium | String -|555|[Split Concatenated Strings](https://leetcode.com/problems/split-concatenated-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_555.java) | |Medium | String -|554|[Brick Wall](https://leetcode.com/problems/brick-wall/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_554.java) | |Medium | HashMap -|553|[Optimal Division](https://leetcode.com/problems/optimal-division/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_553.java) | |Medium | String, Math -|552|[Student Attendance Record II](https://leetcode.com/problems/student-attendance-record-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_552.java) | |Hard| DP -|551|[Student Attendance Record I](https://leetcode.com/problems/student-attendance-record-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_551.java) | |Easy| String -|549|[Binary Tree Longest Consecutive Sequence II](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_549.java) | |Medium | Tree -|548|[Split Array with Equal Sum](https://leetcode.com/problems/split-array-with-equal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_548.java) | |Medium | Array -|547|[Friend Circles](https://leetcode.com/problems/friend-circles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_547.java) | |Medium | Union Find -|546|[Remove Boxes](https://leetcode.com/problems/remove-boxes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_546.java) | |Hard| DFS, DP -|545|[Boundary of Binary Tree](https://leetcode.com/problems/boundary-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_545.java) | |Medium | Recursion -|544|[Output Contest Matches](https://leetcode.com/problems/output-a824-matches/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_544.java) | |Medium | Recursion -|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_543.java) | | Easy | Tree/DFS/Recursion -|542|[01 Matrix](https://leetcode.com/problems/01-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_542.java) | |Medium | BFS -|541|[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_541.java) | |Easy | String -|540|[Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_540.java) | |Medium | -|539|[Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_539.java) | | Medium | String -|538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_538.java) | |Easy | Tree -|537|[Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_537.java) | |Medium | Math, String -|536|[Construct Binary Tree from String](https://leetcode.com/problems/construct-binary-tree-from-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_536.java) | | Medium | Recursion, Stack -|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_535.java) | |Medium | Design -|533|[Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_533.java) | | Medium | HashMap -|532|[K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_532.java) | |Easy | HashMap -|531|[Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_531.java) | |Medium | -|530|[Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_530.java) | |Easy| DFS -|529|[Minesweeper](https://leetcode.com/problems/minesweeper/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_529.java) | |Medium | BFS -|527|[Word Abbreviation](https://leetcode.com/problems/word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_527.java) | |Hard | -|526|[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_526.java) | |Medium | Backtracking -|525|[Contiguous Array](https://leetcode.com/problems/contiguous-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_525.java) | |Medium | HashMap -|524|[Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_524.java) | |Medium | Sort -|523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_523.java) | |Medium| DP -|522|[Longest Uncommon Subsequence II](https://leetcode.com/problems/longest-uncommon-subsequence-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_522.java) | | Medium| -|521|[Longest Uncommon Subsequence I](https://leetcode.com/problems/longest-uncommon-subsequence-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_521.java) | | Easy| -|520|[Detect Capital](https://leetcode.com/problems/detect-capital/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_520.java) | |Easy| -|517|[Super Washing Machines](https://leetcode.com/problems/super-washing-machines/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_517.java) | |Hard| DP -|516|[Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_516.java) | |Medium| DP -|515|[Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_515.java) | |Medium| BFS -|514|[Freedom Trail](https://leetcode.com/problems/freedom-trail/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_514.java) | |Hard | DP -|513|[Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_513.java) ||Medium| BFS -|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_509.java) | [:tv:](https://www.youtube.com/watch?v=WPBTYmvcHXs)|Easy| Array -|508|[Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_508.java) ||Medium| DFS, Tree -|507|[Perfect Number](https://leetcode.com/problems/perfect-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_507.java) | |Easy| Math -|506|[Relative Ranks](https://leetcode.com/problems/relative-ranks/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_506.java) | |Easy| -|505|[The Maze II](https://leetcode.com/problems/the-maze-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_505.java) | |Medium| BFS -|504|[Base 7](https://leetcode.com/problems/base-7/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_504.java) | |Easy| -|503|[Next Greater Element II](https://leetcode.com/problems/parents-greater-element-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_503.java) | |Medium| Stack -|502|[IPO](https://leetcode.com/problems/ipo/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_502.java) | |Hard| Heap, Greedy -|501|[Find Mode in Binary Tree](https://leetcode.com/problems/find-mode-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_501.java) | |Easy| Binary Tree -|500|[Keyboard Row](https://leetcode.com/problems/keyboard-row/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_500.java) | |Easy| -|499|[The Maze III](https://leetcode.com/problems/the-maze-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_499.java) | |Hard| BFS -|496|[Next Greater Element I](https://leetcode.com/problems/parents-greater-element-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_496.java) | |Easy| -|498|[Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_498.java) | |Medium| -|495|[Teemo Attacking](https://leetcode.com/problems/teemo-attacking/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_495.java) | |Medium| Array -|494|[Target Sum](https://leetcode.com/problems/target-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_494.java) | |Medium| -|493|[Reverse Pairs](https://leetcode.com/problems/reverse-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_493.java) | |Hard| Recursion -|492|[Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_492.java) | |Easy| Array -|491|[Increasing Subsequences](https://leetcode.com/problems/increasing-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_491.java) | |Medium| Backtracking, DFS -|490|[The Maze](https://leetcode.com/problems/the-maze/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_490.java) | |Medium| BFS -|488|[Zuma Game](https://leetcode.com/problems/zuma-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_488.java) | |Hard | DFS, Backtracking -|487|[Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_487.java) | |Medium| Array -|486|[Predict the Winner](https://leetcode.com/problems/predict-the-winner/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_486.java) | | Medium | DP -|485|[Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_485.java) | |Easy| Array -|484|[Find Permutation](https://leetcode.com/problems/find-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_484.java) | |Medium | Array, String, Greedy -|483|[Smallest Good Base](https://leetcode.com/problems/smallest-good-base/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_483.java) | |Hard | Binary Search, Math -|482|[License Key Formatting](https://leetcode.com/problems/license-key-formatting/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_482.java) | |Medium| -|481|[Magical String](https://leetcode.com/problems/magical-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_481.java) ||Medium| -|480|[Sliding Window Median](https://leetcode.com/problems/sliding-window-median/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_480.java) | |Hard| Heap -|479|[Largest Palindrome Product](https://leetcode.com/problems/largest-palindrome-product/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_479.java) | |Easy| -|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_477.java) | |Medium| Bit Manipulation -|476|[Number Complement](https://leetcode.com/problems/number-complement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_476.java) | | Easy| Bit Manipulation -|475|[Heaters](https://leetcode.com/problems/heaters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_475.java) | |Easy | Array Binary Search -|474|[Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_474.java) | |Medium| DP -|473|[Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_473.java) | |Medium| Backtracking, DFS -|472|[Concatenated Words](https://leetcode.com/problems/concatenated-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_472.java) | |Hard| Trie, DP, DFS -|471|[Encode String with Shortest Length](https://leetcode.com/problems/encode-string-with-shortest-length/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_471.java) | |Hard| DP -|469|[Convex Polygon](https://leetcode.com/problems/convex-polygon/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_469.java) | |Medium| Math -|468|[Validate IP Address](https://leetcode.com/problems/validate-ip-address/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_468.java) | |Medium | String -|467|[Unique Substrings in Wraparound String](https://leetcode.com/problems/unique-substrings-in-wraparound-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_467.java) ||Medium| DP -|466|[Count The Repetitions](https://leetcode.com/problems/count-the-repetitions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_466.java)| |Hard| DP -|465|[Optimal Account Balancing](https://leetcode.com/problems/optimal-account-balancing/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_465.java)| |Hard| DP -|464|[Can I Win](https://leetcode.com/problems/can-i-win/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_464.java)| |Medium| DP -|463|[Island Perimeter](https://leetcode.com/problems/island-perimeter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_463.java)| |Easy| -|462|[Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_462.java) || Medium| -|461|[Hamming Distance](https://leetcode.com/problems/hamming-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_461.java) | |Easy| -|460|[LFU Cache](https://leetcode.com/problems/lfu-cache/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_460.java) | | Hard| Design, LinkedHashMap, HashMap -|459|[Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_459.java)| |Easy| String, KMP -|458|[Poor Pigs](https://leetcode.com/problems/poor-pigs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_458.java) | |Easy| Math -|457|[Circular Array Loop](https://leetcode.com/problems/circular-array-loop/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_457.java) | |Medium | -|456|[132 Pattern](https://leetcode.com/problems/132-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_456.java) | |Medium| Stack -|455|[Assign Cookies](https://leetcode.com/problems/assign-cookies/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_455.java)| |Easy| -|454|[4Sum II](https://leetcode.com/problems/4sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_454.java) | |Medium| HashMap -|453|[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_453.java)| |Easy| -|452|[Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_452.java) | |Medium| Array, Greedy -|451|[Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_451.java) | |Medium| HashMap -|450|[Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_450.java)| |Medium| Tree, Recursion -|449|[Serialize and Deserialize BST](https://leetcode.com/problems/serialize-and-deserialize-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_449.java)| |Medium| BFS -|448|[Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_448.java)| |Easy| Array, HashMap -|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_447.java)| |Easy| HashMap -|446|[Arithmetic Slices II - Subsequence](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_446.java)| |Hard| DP -|445|[Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_445.java)| |Medium| Stack, LinkedList -|444|[Sequence Reconstruction](https://leetcode.com/problems/sequence-reconstruction/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_444.java)| |Medium| Topological Sort, Graph -|443|[String Compression](https://leetcode.com/problems/string-compression/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_443.java)| |Easy | -|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_442.java)| |Medium| Array -|441|[Arranging Coins](https://leetcode.com/problems/arrange-coins/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_441.java)| |Easy| -|440|[K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_440.java)| |Hard| -|439|[Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_439.java)| |Medium| Stack -|438|[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_438.java)| |Easy| Sliding Window -|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_437.java) | |Easy| DFS, recursion -|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_436.java) | |Medium| Binary Search -|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_435.java) | |Medium| Greedy -|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_434.java)| |Easy| -|432|[All O`one Data Structure](https://leetcode.com/problems/all-oone-data-structure/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_432.java)| |Hard| Design -|429|[N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_429.java)| |Easy| BFS, Tree -|425|[Word Squares](https://leetcode.com/problems/word-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_425.java)| |Hard| Trie, Backtracking, Recursion -|424|[Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_424.java)| | Medium| Sliding Window -|423|[Reconstruct Original Digits from English](https://leetcode.com/problems/reconstruct-original-digits-from-english/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_423.java)| | Medium| Math -|422|[Valid Word Square](https://leetcode.com/problems/valid-word-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_422.java)| |Easy| -|421|[Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_421.java)| |Medium | Bit Manipulation, Trie -|420|[Strong Password Checker](https://leetcode.com/problems/strong-password-checker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_420.java)| | Hard| -|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_419.java) | |Medium| DFS -|418|[Sentence Screen Fitting](https://leetcode.com/problems/sentence-screen-fitting/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_418.java) | |Medium| -|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_417.java) | |Medium| DFS -|416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_416.java)| |Medium | DP -|415|[Add Strings](https://leetcode.com/problems/add-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_415.java)| |Easy| -|414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_414.java)| |Easy| -|413|[Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_413.java) | |Medium| DP -|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_412.java)| |Easy| -|411|[Minimum Unique Word Abbreviation](https://leetcode.com/problems/minimum-unique-word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_411.java)| |Hard| NP-Hard, Backtracking, Trie, Recursion -|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_410.java)| |Hard| Binary Search, DP -|409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_409.java)| |Easy| -|408|[Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_408.java)| |Easy| -|407|[Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_407.java)| |Hard| Heap -|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_406.java)| |Medium| LinkedList, PriorityQueue -|405|[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_405.java)| |Easy| -|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_404.java)| |Easy| -|403|[Frog Jump](https://leetcode.com/problems/frog-jump/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_403.java)| |Hard| DP -|402|[Remove K Digits](https://leetcode.com/problems/remove-k-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_402.java)| |Medium| Greedy, Stack -|401|[Binary Watch](https://leetcode.com/problems/binary-watch/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_401.java)| |Easy| -|400|[Nth Digit](https://leetcode.com/problems/nth-digit/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_400.java)| |Easy| -|399|[Evaluate Division](https://leetcode.com/problems/evaluate-division/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_399.java)| |Medium| Graph, DFS, Backtracking -|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_398.java) | |Medium| Reservoir Sampling -|397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_397.java)| |Easy| BFS -|396|[Rotate Function](https://leetcode.com/problems/rotate-function/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_396.java) | |Easy| -|395|[Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_395.java)| |Medium| Recursion -|394|[Decode String](https://leetcode.com/problems/decode-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_394.java)| |Medium| Stack Depth-first-search -|393|[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_393.java)| |Medium| Bit Manipulation -|392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_392.java)| |Medium| Array, String -|391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_391.java)| |Hard| -|390|[Elimination Game](https://leetcode.com/problems/elimination-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_390.java)| |Medium| -|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_389.java)| || Easy| -|388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_388.java)| |Medium| Stack -|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_387.java)| |Easy| HashMap -|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_386.java)| |Medium| -|385|[Mini Parser](https://leetcode.com/problems/mini-parser/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_385.java)| |Medium| Stack -|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_384.java)| |Medium| -|383|[Ransom Note](https://leetcode.com/problems/ransom-note/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_383.java)| |Easy | String -|382|[Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_382.java)| |Medium| Reservoir Sampling -|381|[Insert Delete GetRandom O(1) - Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_381.java) || Hard| -|380|[Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_380.java)| |Medium| Design, HashMap -|379|[Design Phone Directory](https://leetcode.com/problems/design-phone-directory/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_379.java)| |Medium| -|378|[Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_378.java)| |Medium| Binary Search -|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_377.java)| |Medium| DP -|376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_376.java)| |Medium| DP, Greedy -|375|[Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_375.java)| |Medium| DP -|374|[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_374.java)| |Easy| Binary Search -|373|[Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_373.java)| |Medium| Heap -|372|[Super Pow](https://leetcode.com/problems/super-pow/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_372.java)| |Medium| Math -|371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_371.java)| |Easy| -|370|[Range Addition](https://leetcode.com/problems/range-addition/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_370.java)| |Medium|Array -|369|[Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_369.java)| |Medium| Linked List -|368|[Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_368.java)| | Medium| DP -|367|[Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_367.java)| |Medium| -|366|[Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_366.java)| |Medium| DFS -|365|[Water and Jug Problem](https://leetcode.com/problems/water-and-jug-problem/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_365.java)| |Medium| Math -|364|[Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_364.java)| |Medium| DFS -|363|[Max Sum of Rectangle No Larger Than K](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_363.java)| |Hard| DP -|362|[Design Hit Counter](https://leetcode.com/problems/design-hit-counter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_362.java)| |Medium| Design -|361|[Bomb Enemy](https://leetcode.com/problems/bomb-enemy/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_361.java)| |Medium| -|360|[Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_360.java)| |Medium| Two Pointers, Math -|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_359.java)| |Easy| HashMap -|358|[Rearrange String k Distance Apart](https://leetcode.com/problems/rearrange-string-k-distance-apart/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_358.java)| |Hard| HashMap, Heap, Greedy -|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_357.java)| |Medium| DP, Math -|356|[Line Reflection](https://leetcode.com/problems/line-reflection/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_356.java)| |Medium| HashSet -|355|[Design Twitter](https://leetcode.com/problems/design-twitter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_355.java)| |Medium| Design, HashMap, Heap -|354|[Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_354.java)| |Hard| DP, Binary Search -|353|[Design Snake Game](https://leetcode.com/problems/design-snake-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_353.java)| |Medium| -|352|[Data Stream as Disjoint Intervals](https://leetcode.com/problems/data-stream-as-disjoint-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_352.java)| |Hard| TreeMap -|351|[Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_351.java)| |Medium| -|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_350.java)| [:tv:](https://youtu.be/lKuK69-hMcc) |Easy| HashMap, Binary Search -|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_349.java)| [:tv:](https://youtu.be/XxStWmfXJRs) |Easy| Two Pointers, Binary Search -|348|[Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_348.java)| |Medium| Design -|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_347.java)| |Medium| HashTable, Heap, Bucket Sort -|346|[Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_346.java)| |Easy| Queue -|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_345.java) | |Easy | String -|344|[Reverse String](https://leetcode.com/problems/reverse-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_344.java) | [:tv:](https://youtu.be/P68JPXtFyYg) |Easy | String -|343|[Integer Break](https://leetcode.com/problems/integer-break/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_343.java)| |Medium| Math -|342|[Power of Four](https://leetcode.com/problems/power-of-four/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_342.java)| |Easy| Math -|341|[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_341.java)| |Medium| Stack -|340|[Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_340.java)| |Hard| Sliding Window -|339|[Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_339.java)| |Easy| DFS -|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_338.java)| |Medium| -|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_337.java)| |Medium | DP -|336|[Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_336.java)| |Hard| -|335|[Self Crossing](https://leetcode.com/problems/self-crossing/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_335.java)| |Hard| Math -|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_334.java)| |Medium| -|333|[Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_333.java)| |Medium| Tree -|332|[Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_332.java)| |Medium| Graph, DFS -|331|[Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_331.java)| |Medium| Stack -|330|[Patching Array](https://leetcode.com/problems/patching-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_330.java)| |Hard| Greedy -|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_329.java)| |Hard| DFS, DP -|328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_328.java)| |Medium| Linked List -|327|[Count of Range Sum](https://leetcode.com/problems/count-of-range-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_327.java)| |Hard| BST, Divide and Conquer -|326|[Power of Three](https://leetcode.com/problems/power-of-three/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_326.java)| |Easy| Math -|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_325.java)| |Medium| HashTable -|324|[Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_324.java)| |Medium| Sort -|323|[Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_323.java)| | Medium| -|322|[Coin Change](https://leetcode.com/problems/coin-change/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_322.java)| |Medium| DP -|321|[Create Maximum Number](https://leetcode.com/problems/create-maximum-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_321.java)| |Hard -|320|[Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_320.java)| |Medium| Backtracking, Bit Manipulation -|319|[Bulb Switcher](https://leetcode.com/problems/bulb-switcher/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_319.java)| |Medium| Brainteaser -|318|[Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_318.java)| |Medium| -|317|[Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_317.java)| |Hard| -|316|[Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_316.java)| |Hard| Stack, Recursion, Greedy -|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_315.java)| |Hard| Tree -|314|[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_314.java)| |Medium| HashMap, BFS -|313|[Super Ugly Number](https://leetcode.com/problems/super-ugly-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_313.java)| |Medium| -|312|[Burst Balloons](https://leetcode.com/problems/burst-balloons/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_312.java)| |Hard| DP -|311|[Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_311.java)| |Medium| -|310|[Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_310.java)| |Medium| -|309|[Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-cooldown/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_309.java)| |Medium| DP -|308|[Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_308.java)| |Hard| Tree -|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_307.java)| |Medium| Tree -|306|[Additive Number](https://leetcode.com/problems/additive-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_306.java)| |Medium| -|305|[Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_305.java)| | Hard| Union Find -|304|[Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_304.java)| |Medium| -|303|[Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_303.java)| |Easy| -|302|[Smallest Rectangle Enclosing Black Pixels](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_302.java)| |Hard| DFS, BFS -|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_301.java)| |Hard| BFS -|300|[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_300.java)| |Medium| DP -|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_299.java)| |Easy| -|298|[Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_298.java)| |Medium | Tree -|297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_297.java)| |Hard| BFS -|296|[Best Meeting Point](https://leetcode.com/problems/best-meeting-point/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_296.java)| |Hard| -|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_295.java)| |Hard| Heap -|294|[Flip Game II](https://leetcode.com/problems/flip-game-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_294.java)| |Medium| Backtracking -|293|[Flip Game](https://leetcode.com/problems/flip-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_293.java)| |Easy| -|292|[Nim Game](https://leetcode.com/problems/nim-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_292.java)| | Easy| -|291|[Word Pattern II](https://leetcode.com/problems/word-pattern-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_291.java)| |Hard| Recursion, Backtracking -|290|[Word Pattern](https://leetcode.com/problems/word-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_290.java)| |Easy| HashMap -|289|[Game of Life](https://leetcode.com/problems/game-of-life/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_289.java)| |Medium| -|288|[Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_288.java)| |Easy| -|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_287.java)| |Medium| -|286|[Walls and Gates](https://leetcode.com/problems/walls-and-gates/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_286.java)| |Medium| BFS -|285|[Inorder Successor In BST](https://leetcode.com/problems/inorder-successor-in-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_285.java)| |Medium| Tree -|284|[Peeking Iterator](https://leetcode.com/problems/peeking-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_284.java)| |Medium| Design -|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_283.java)| [:tv:](https://youtu.be/39VJV4KVyi8)|Easy| -|282|[Expression Add Operators](https://leetcode.com/problems/expression-add-operators/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_282.java)| |Hard| -|281|[Zigzag Iterator](https://leetcode.com/problems/zigzag-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_281.java)| |Medium| -|280|[Wiggle Sort](https://leetcode.com/problems/wiggle-sort/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_280.java)| |Medium| -|279|[Perfect Squares](https://leetcode.com/problems/perfect-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_279.java)| |Medium| -|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_278.java)|[:tv:](https://youtu.be/E15djRphPj0)|Easy| Binary Search -|277|[Find the Celebrity](https://leetcode.com/problems/find-the-celebrity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_277.java)| |Medium| -|276|[Paint Fence](https://leetcode.com/problems/paint-fence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_276.java)| |Easy| DP -|275|[H-Index II](https://leetcode.com/problems/h-index-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_275.java)| |Medium| Binary Search -|274|[H-Index](https://leetcode.com/problems/h-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_274.java)| |Medium| -|273|[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_273.java)| |Hard| Math, String -|272|[Closest Binary Search Tree Value II](https://leetcode.com/problems/closest-binary-search-tree-value-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_272.java)| |Hard| Stack -|271|[Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_271.java)| | |Medium| -|270|[Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_270.java)| | |Easy| DFS -|269|[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_269.java)| | |Hard| Topological Sort -|268|[Missing Number](https://leetcode.com/problems/missing-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_268.java)| | |Easy| Bit Manipulation -|267|[Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_267.java)| |Medium| -|266|[Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_266.java)| |Easy| -|265|[Paint House II](https://leetcode.com/problems/paint-house-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_265.java)| |Hard| DP -|264|[Ugly Number II](https://leetcode.com/problems/ugly-number-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_264.java)| |Medium| DP -|263|[Ugly Number](https://leetcode.com/problems/ugly-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_263.java)| |Easy| -|261|[Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_261.java)| |Medium| -|260|[Single Number III](https://leetcode.com/problems/single-number-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_260.java)| |Medium| -|259|[3Sum Smaller](https://leetcode.com/problems/3sum-smaller/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_259.java)| |Medium| -|258|[Add Digits](https://leetcode.com/problems/add-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_258.java)| |Easy| -|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_257.java) | ||DFS/Recursion -|256|[Paint House](https://leetcode.com/problems/paint-house/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_256.java) | |Medium| DP -|255|[Verify Preorder Sequence in Binary Search Tree](https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_255.java) | |Medium| Tree -|254|[Factor Combinations](https://leetcode.com/problems/factor-combinations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_254.java) | |Medium| Backtracking -|253|[Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_253.java) | |Medium| Heap -|252|[Meeting Rooms](https://leetcode.com/problems/meeting-rooms/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_252.java) | | Easy -|251|[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_251.java)| |Medium| -|250|[Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_250.java)| |Medium| DFS -|249|[Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_249.java) | || -|248|[Strobogrammatic Number III](https://leetcode.com/problems/strobogrammatic-number-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_248.java) | |Hard | Recursion, DFS -|247|[Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_247.java) | |Medium | Recursion -|246|[Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_246.java) | |Easy -|245|[Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_245.java) | |Medium | -|244|[Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_244.java) | |Medium | HashMap -|243|[Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_243.java) | |Easy -|242|[Valid Anagram](https://leetcode.com/problems/valid-anagram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_242.java) | |Easy -|241|[Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_241.java) | |Medium | Divide and Conquer -|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_240.java)| |Medium| Binary Search -|239|[Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_239.java)| |Hard| Heap -|238|[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_238.java)| |Medium| Array -|237|[Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_237.java)|[:tv:](https://youtu.be/sW8ZaOTtvgI) |Easy| LinkedList -|236|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_236.java)| |Medium| DFS -|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_235.java)| |Easy| DFS -|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_234.java)| [:tv:](https://youtu.be/bOGh_3MTrdE) |Easy| Linked List -|233|[Number of Digit One](https://leetcode.com/problems/number-of-digit-one/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_233.java)| |Hard| Math -|232|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_232.java)| |Medium| Stack, Design -|231|[Power of Two](https://leetcode.com/problems/power-of-two/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_231.java)| |Easy| -|230|[Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_230.java)| |Medium| Tree -|229|[Majority Element II](https://leetcode.com/problems/majority-element-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_229.java)| |Medium| -|228|[Summary Ranges](https://leetcode.com/problems/summary-ranges/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_228.java)| |Medium| Array -|227|[Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_227.java)| |Medium| String -|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_226.java)| |Easy| DFS, recursion -|225|[Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_225.java)| |Easy| Stack, Queue -|224|[Basic Calculator](https://leetcode.com/problems/basic-calculator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_224.java)| |Hard| -|223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_223.java)| | Easy| -|222|[Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_222.java)| |Medium| Recursion -|221|[Maximal Square](https://leetcode.com/problems/maximal-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_221.java)| |Medium| Recursion -|220|[Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_220.java)| [:tv:](https://youtu.be/Cu7g9ovYHNI)|Medium| TreeSet -|219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_219.java)| [:tv:](https://youtu.be/SFMCxqSeM94)|Easy| HashMap -|218|[The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_218.java)| |Hard| TreeMap, Design -|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_217.java)| [:tv:](https://youtu.be/SFMCxqSeM94) |Easy| HashSet -|216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_216.java)| |Medium| Backtracking -|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_215.java)| |Medium| Heap -|214|[Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_214.java)| |Hard | KMP -|213|[House Robber II](https://leetcode.com/problems/house-robber-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_213.java)| |Medium | DP -|212|[Word Search II](https://leetcode.com/problems/word-search-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/WordSearchII.java)| |Hard | Trie -|211|[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_211.java)| |Medium| Trie -|210|[Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_210.java)| |Medium| -|209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_209.java)| |Medium| -|208|[Implement Trie](https://leetcode.com/problems/implement-trie-prefix-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_208.java)| |Medium| Trie -|207|[Course Schedule](https://leetcode.com/problems/course-schedule/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_207.java)| |Medium| -|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_206.java)| [:tv:](https://youtu.be/N_Y12-5oa-w) |Easy | Linked List -|205|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_205.java)| |Easy -|204|[Count Primes](https://leetcode.com/problems/count-primes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_204.java)| |Easy | The Sieve of Eratosthenes -|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_203.java)| |Easy -|202|[Happy Number](https://leetcode.com/problems/happy-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_202.java)| |Easy -|201|[Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_201.java)| |Medium | Bit Manipulation -|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_200.java)| |Medium| Union Find, DFS -|199|[Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_199.java)| |Medium | BFS -|198|[House Robber](https://leetcode.com/problems/house-robber/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_198.java)| |Easy | DP -|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_191.java)| |Easy | Bit Manipulation -|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_190.java)| |Easy | Bit Manipulation -|189|[Rotate Array](https://leetcode.com/problems/rotate-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_189.java)|[:tv:](https://youtu.be/lTHTR_jsqAQ) | Easy -|188|[Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_188.java)| |Hard | DP -|187|[Repeated DNA Sequences](https://leetcode.com/problems/repeated-dna-sequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_187.java)| | Medium -|186|[Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_186.java)| |Medium -|179|[Largest Number](https://leetcode.com/problems/largest-number/)|[Solution](../../master/src/main/java/com/fishercoder/solutions/_179.java)| |Medium| -|174|[Dungeon Game](https://leetcode.com/problems/dungeon-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_174.java)| |Hard| DP -|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)|[Solution](../../blmaster/MEDIUM/src/medium/_173.java)| |Medium| Stack, Design -|172|[Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_172.java)| |Easy -|171|[Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_171.java)| |Easy -|170|[Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_170.java)| |Easy -|169|[Majority Element](https://leetcode.com/problems/majority-element/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_169.java)| |Easy| -|168|[Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_168.java)| |Easy| -|167|[Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_167.java)| |Easy| Binary Search -|166|[Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_166.java) | |Medium| HashMap -|165|[Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_165.java)| |Easy| -|164|[Maximum Gap](https://leetcode.com/problems/maximum-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_164.java) | |Hard| -|163|[Missing Ranges](https://leetcode.com/problems/missing-ranges/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_163.java) | || -|162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_162.java) | |Binary Search| -|161|[One Edit Distance](https://leetcode.com/problems/one-edit-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_161.java) | || -|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_160.java)| |Easy| Linked List -|159|[Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_159.java)| | Hard| String, Sliding Window -|158|[Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_158.java)| |Hard| -|157|[Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_157.java)| |Easy| -|156|[Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_156.java)| |Medium| Tree, Recursion -|155|[Min Stack](https://leetcode.com/problems/min-stack/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_155.java)| |Easy| Stack -|154|[Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_154.java)| |Hard| Array, Binary Search -|153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_153.java)| |Medium| Array, Binary Search -|152|[Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_152.java)| |Medium| Array -|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_151.java)| | Medium| String -|150|[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_150.java)| |Medium -|149|[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_149.java)| |Hard| -|147|[Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_147.java) ||Medium| Linked List -|146|[LRU Cache](https://leetcode.com/problems/lru-cache/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_146.java)| |Hard| Doubly Linked List, LinkedHashMap -|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_145.java)|[:tv:](https://youtu.be/B6XTLPpsW7k) |Hard| Binary Tree -|144|[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_144.java)|[:tv:](https://youtu.be/367McfIeBDM) |Medium| Binary Tree -|143|[Reorder List](https://leetcode.com/problems/reorder-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_143.java)| |Medium| -|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_142.java)| |Medium| Linked List -|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_141.java)| [:tv:](https://youtu.be/agkyC-rbgKM) |Easy| Linked List -|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_140.java)| |Hard| Backtracking/DFS -|139|[Word Break](https://leetcode.com/problems/word-break/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_139.java)| |Medium| DP, Pruning -|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_138.java)| |Medium| LinkedList, HashMap -|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_137.java)| |Medium| Bit Manipulation -|136|[Single Number](https://leetcode.com/problems/single-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_136.java)| [:tv:](https://youtu.be/gJ8VcJ8f_Vk) |Easy | Bit Manipulation -|135|[Candy](https://leetcode.com/problems/candy/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_135.java)| |Hard| Greedy -|134|[Gas Station](https://leetcode.com/problems/gas-station/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_134.java)| |Medium| Greedy -|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_133.java)| |Medium| HashMap, BFS, Graph -|132|[Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_132.java)| |Hard| -|131|[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_131.java)| |Medium| -|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_130.java)| |Medium| -|129|[Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_129.java)| |Medium| DFS -|128|[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_128.java)| |Hard| Union Find -|127|[Word Ladder](https://leetcode.com/problems/word-ladder/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_127.java)| |Medium| BFS -|126|[Word Ladder II](https://leetcode.com/problems/word-ladder-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_126.java)| |Hard| BFS -|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_125.java)| |Easy| Two Pointers -|124|[Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_124.java)| |Hard | Tree, DFS -|123|[Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_123.java)| |Hard | DP -|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_122.java)| |Easy | Greedy -|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_121.java)| |Easy| -|120|[Triangle](https://leetcode.com/problems/triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_120.java)| |Medium| DP -|119|[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_119.java) |[:tv:](https://www.youtube.com/watch?v=iVhmR1bzKoo)|Easy| -|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_118.java)| [:tv:](https://www.youtube.com/watch?v=TXd5lfP3Gac) |Easy| -|117|[Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-parents-right-pointers-in-each-node-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_117.java)| |Medium| BFS -|116|[Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-parents-right-pointers-in-each-node/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_116.java)| |Medium| BFS -|115|[Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_115.java)| |Hard| DP -|114|[Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_114.java)| |Medium| Tree -|113|[Path Sum II](https://leetcode.com/problems/path-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_113.java)| |Medium| DFS, Backtracking -|112|[Path Sum](https://leetcode.com/problems/path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_112.java)| |Easy| DFS -|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_111.java)| |Easy| BFS, DFS -|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_110.java)| |Easy| DFS -|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_109.java)| |Medium | DFS, Recursion -|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_108.java)| [:tv:](https://youtu.be/VVSnM5DGvjg) |Easy | Tree -|107|[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_107.java)| |Easy| BFS -|106|[Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_106.java)| |Medium| Recursion, Tree -|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_105.java)| |Medium| Recursion, Tree -|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_104.java)| [:tv:](https://youtu.be/dvmoHr5cN80) |Easy| DFS -|103|[Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_103.java)| |Medium| BFS,DFS -|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_102.java)| [:tv:](https://youtu.be/sFDNL6r5aDM) |Medium| BFS -|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_101.java)| [:tv:](https://www.youtube.com/watch?v=F85boSPtfKg)|Easy| DFS -|100|[Same Tree](https://leetcode.com/problems/same-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_100.java)| [:tv:](https://www.youtube.com/watch?v=2Pe6e0KbgFI)|Easy| DFS -|99|[Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_99.java) | |Hard | -|98|[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_98.java) | [:tv:](https://youtu.be/kR5AxWHa9nc) |Medium | DFS/Recursion -|97|[Interleaving String](https://leetcode.com/problems/interleaving-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_97.java)| |Hard| DP -|96|[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_96.java) | |Medium | Recursion, DP -|95|[Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_95.java) | |Medium | Recursion -|94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_94.java)| [:tv:](https://youtu.be/o_T8MswDI_Y) |Medium| Binary Tree -|93|[Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_93.java)| |Medium | Backtracking -|92|[Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_92.java)| |Medium -|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_91.java)| |Medium| DP -|90|[Subsets II](https://leetcode.com/problems/subsets-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_90.java)||Medium|Backtracking -|89|[Gray Code](https://leetcode.com/problems/gray-code/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_89.java)||Medium|Bit Manipulation -|88|[Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_88.java)||Easy| -|87|[Scramble String](https://leetcode.com/problems/scramble-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_87.java)||Hard| Recursion -|86|[Partition List](https://leetcode.com/problems/partition-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_86.java)||Medium| Linked List -|85|[Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_85.java)||Hard|DP -|84|[Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_84.java)||Hard|Array, Stack -|83|[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_83.java)||Easy| Linked List -|82|[Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_82.java)||Medium| Linked List -|81|[Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_81.java)||Medium|Binary Search -|80|[Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_80.java)||Medium| -|79|[Word Search](https://leetcode.com/problems/word-search/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_79.java)| |Medium | Backtracking, DFS -|78|[Subsets](https://leetcode.com/problems/subsets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_78.java)||Medium|Backtracking -|77|[Combinations](https://leetcode.com/problems/combinations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_77.java)||Medium|Backtracking -|76|[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_76.java)||Hard|Two Pointers -|75|[Sort Colors](https://leetcode.com/problems/sort-colors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_75.java)||Medium| Two Pointers -|74|[Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_74.java)||Medium| Binary Search -|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_73.java)||Medium| -|72|[Edit Distance](https://leetcode.com/problems/edit-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_72.java)||Hard| -|71|[Simplify Path](https://leetcode.com/problems/simplify-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_71.java)||Medium| Stack -|70|[Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_70.java)|[:tv:](https://youtu.be/ZMNRb9TYiQM)|Easy| DP -|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_69.java)||Easy| -|68|[Text Justification](https://leetcode.com/problems/text-justification/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_68.java)||Hard| -|67|[Add Binary](https://leetcode.com/problems/add-binary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_67.java)||Easy| -|66|[Plus One](https://leetcode.com/problems/plus-one/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_66.java)|[:tv:](https://youtu.be/HKjt0f1N0GA)|Easy| -|65|[Valid Number](https://leetcode.com/problems/valid-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_65.java)||Hard| -|64|[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_64.java)||Medium| DP -|63|[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_63.java)||Medium| DP -|62|[Unique Paths](https://leetcode.com/problems/unique-paths/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_62.java)||Medium| DP -|61|[Rotate List](https://leetcode.com/problems/rotate-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_61.java)||Medium| Linked List -|60|[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_60.java)||Medium| Math, Backtracking -|59|[Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_59.java)|[:tv:](https://www.youtube.com/watch?v=Sv9DK2C4rtc)|Medium| -|58|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_58.java)||Easy| -|57|[Insert Intervals](https://leetcode.com/problems/insert-interval/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_57.java)||Hard| Array, Sort -|56|[Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_56.java)||Medium| Array, Sort -|55|[Jump Game](https://leetcode.com/problems/jump-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_55.java)||Medium| Greedy -|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_54.java)|[:tv:](https://www.youtube.com/watch?v=uYgoo8BdUAA)|Medium| Array -|53|[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_53.java)||Easy| Array -|52|[N-Queens II](https://leetcode.com/problems/n-queens-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_52.java)||Hard| Backtracking -|51|[N-Queens](https://leetcode.com/problems/n-queens/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_51.java)||Hard| -|50|[Pow(x, n)](https://leetcode.com/problems/powx-n/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_50.java)||Medium| -|49|[Group Anagrams](https://leetcode.com/problems/group-anagrams/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_49.java)||Medium| HashMap -|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_48.java)|[:tv:](https://youtu.be/gCciKhaK2v8) |Medium | Array -|47|[Permutations II](https://leetcode.com/problems/permutations-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_47.java)||Medium|Backtracking -|46|[Permutations](https://leetcode.com/problems/permutations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_46.java)| |Medium | Backtracking -|45|[Jump Game II](https://leetcode.com/problems/jump-game-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_45.java)||Hard| Array, Greedy -|44|[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_44.java)||Hard| Backtracking, DP, Greedy, String -|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_43.java)||Medium| Array, String -|42|[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_42.java)||Hard| -|41|[First Missing Positive](https://leetcode.com/problems/first-missing-positive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_41.java)||Hard| Array -|40|[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_40.java)||Medium|Backtracking -|39|[Combination Sum](https://leetcode.com/problems/combination-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_39.java)||Medium|Backtracking -|38|[Count and Say](https://leetcode.com/problems/count-and-say/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_38.java)||Easy| Recursion, LinkedList -|37|[Sudoku Solver](https://leetcode.com/problems/sudoku-solver/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_37.java)||Hard| -|36|[Valid Sudoku](https://leetcode.com/problems/valid-sudoku/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_36.java)||Medium| -|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_35.java)||Easy|Array -|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_34.java)||Medium|Array, Binary Search -|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_33.java)||Medium|Binary Search -|32|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_32.java)||Hard|Stack, DP -|31|[Next Permutation](https://leetcode.com/problems/parents-permutation)|[Solution](../master/src/main/java/com/fishercoder/solutions/_31.java)||Medium|Array -|30|[Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_30.java)||Hard| HashMap -|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_29.java)||Medium| -|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_28.java)||Easy| String -|27|[Remove Element](https://leetcode.com/problems/remove-element/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_27.java)| |Easy | -|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_26.java)|[:tv:](https://youtu.be/nRKZC2JF7LU)|Easy| Array -|25|[Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_25.java)| |Hard | Recursion, LinkedList -|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_24.java)||Medium| Recursion, LinkedList -|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_23.java)|[:tv:](https://www.youtube.com/watch?v=Llse1tImXQA)|Hard|Heap -|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_22.java)||Medium|Backtracking -|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_21.java)|[:tv:](https://youtu.be/N8WTaSSivEI)|Easy| Recursion -|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_20.java)|[:tv:](https://www.youtube.com/watch?v=eBbg5pnq5Zg)|Easy|Stack -|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_19.java)|[:tv:](https://youtu.be/Kka8VgyFZfc)|Medium| Linked List -|18|[4 Sum](https://leetcode.com/problems/4sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_18.java)||Medium|Two Pointers -|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_17.java)||Medium|Backtracking -|16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_16.java)||Medium|Two Pointers -|15|[3Sum](https://leetcode.com/problems/3sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_15.java)|[:tv:](https://www.youtube.com/watch?v=jeim_j8VdiM)|Medium|Two Pointers, Binary Search -|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_14.java)| [:tv:](https://www.youtube.com/watch?v=K1ps6d7YCy4)| Easy -|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer)|[Solution](../master/src/main/java/com/fishercoder/solutions/_13.java)| |Easy | Math, String -|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_12.java)||Medium| Math, String -|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_11.java)||Medium| -|10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_10.java)||Hard|DP -|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/)|[Java](../master/src/main/java/com/fishercoder/solutions/_9.java), [C++](../master/cpp/_9.cpp)| | Easy -|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_8.java)| |Medium -|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_7.java) | [:tv:](https://youtu.be/tm1Yrb_SfBM) |Easy | -|6|[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_6.java) | |Easy | -|5|[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5.java) | |Medium| -|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_4.java) | |Hard | Divide and Conquer -|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_3.java) | |Medium | HashMap, Sliding Window -|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_2.java) | |Medium | LinkedList -|1|[Two Sum](https://leetcode.com/problems/two-sum/)|[Java](../master/src/main/java/com/fishercoder/solutions/_1.java), [C++](../master/cpp/_1.cpp)|[:tv:](https://www.youtube.com/watch?v=kPXOr6pW8KM&t=)|Easy| HashMap +[For problems 1000 to 1999](https://github.com/fishercoder1534/Leetcode/tree/master/paginated_contents/algorithms/2nd_thousand) -## Database +[For problems 2000 to 2999](https://github.com/fishercoder1534/Leetcode/tree/master/paginated_contents/algorithms/3rd_thousand) + +[For problems 3000 to 3999](https://github.com/fishercoder1534/Leetcode/tree/master/paginated_contents/algorithms/4th_thousand) -| # | Title | Solutions | Video | Difficulty | Tag -|-----|----------------|---------------|---------------|---------------|------------- -|1407|[Top Travellers](https://leetcode.com/problems/top-travellers/)|[Solution](../master/database/_1407.sql) || Easy | -|1384|[Total Sales Amount by Year](https://leetcode.com/problems/total-sales-amount-by-year/)|[Solution](../master/database/_1384.sql) || Hard | -|1378|[Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier/)|[Solution](../master/database/_1378.sql) || Easy | -|1369|[Get the Second Most Recent Activity](https://leetcode.com/problems/get-the-second-most-recent-activity/)|[Solution](../master/database/_1369.sql) || Hard | -|1364|[Number of Trusted Contacts of a Customer](https://leetcode.com/problems/number-of-trusted-contacts-of-a-customer/)|[Solution](../master/database/_1364.sql) || Medium | -|1355|[Activity Participants](https://leetcode.com/problems/activity-participants/)|[Solution](../master/database/_1355.sql) || Medium | -|1350|[Students With Invalid Departments](https://leetcode.com/problems/students-with-invalid-departments/)|[Solution](../master/database/_1350.sql) || Easy | -|1341|[Movie Rating](https://leetcode.com/problems/movie-rating/)|[Solution](../master/database/_1341.sql) || Medium | -|1327|[List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period/)|[Solution](../master/database/_1327.sql) || Easy | -|1322|[Ads Performance](https://leetcode.com/problems/ads-performance/)|[Solution](../master/database/_1322.sql) || Easy | -|1308|[Running Total for Different Genders](https://leetcode.com/problems/running-total-for-different-genders/)|[Solution](../master/database/_1308.sql) || Medium | -|1303|[Find the Team Size](https://leetcode.com/problems/find-the-team-size/)|[Solution](../master/database/_1303.sql) || Easy | -|1294|[Weather Type in Each Country](https://leetcode.com/problems/weather-type-in-each-country/)|[Solution](../master/database/_1294.sql) | | Easy | -|1285|[Find the Start and End Number of Continuous Ranges](https://leetcode.com/problems/find-the-start-and-end-number-of-continuous-ranges/)|[Solution](../master/database/_1285.sql) || Medium | -|1280|[Students and Examinations](https://leetcode.com/problems/students-and-examinations/)|[Solution](../master/database/_1280.sql) | [:tv:](https://www.youtube.com/watch?v=ThbkV4Fs7iE)| Easy | -|1270|[All People Report to the Given Manager](https://leetcode.com/problems/all-people-report-to-the-given-manager/)|[Solution](../master/database/_1270.sql) || Medium | -|1251|[Average Selling Price](https://leetcode.com/problems/average-selling-price/)|[Solution](../master/database/_1251.sql) | | Easy | -|1241|[Number of Comments per Post](https://leetcode.com/problems/number-of-comments-per-post/)|[Solution](../master/database/_1241.sql) | | Easy | -|1179|[Reformat Department Table](https://leetcode.com/problems/reformat-department-table/)|[Solution](../master/database/_1179.sql) | | Easy | -|1173|[Immediate Food Delivery I](https://leetcode.com/problems/immediate-food-delivery-i/)|[Solution](../master/database/_1173.sql) | | Easy | -|1148|[Article Views I](https://leetcode.com/problems/article-views-i/)|[Solution](../master/database/_1148.sql) | | Easy | -|1141|[User Activity for the Past 30 Days I](https://leetcode.com/problems/user-activity-for-the-past-30-days-i/)|[Solution](../master/database/_1141.sql) | | Easy | -|1084|[Sales Analysis III](https://leetcode.com/problems/sales-analysis-iii/)|[Solution](../master/database/_1084.sql) | | Easy | -|1083|[Sales Analysis II](https://leetcode.com/problems/sales-analysis-ii/)|[Solution](../master/database/_1083.sql) | | Easy | -|1082|[Sales Analysis I](https://leetcode.com/problems/sales-analysis-i/)|[Solution](../master/database/_1082.sql) | | Easy | -|1076|[Project Employees II](https://leetcode.com/problems/project-employees-ii/)|[Solution](../master/database/_1076.sql) | | Easy | -|1075|[Project Employees I](https://leetcode.com/problems/project-employees-i/)|[Solution](../master/database/_1075.sql) | | Easy | -|1069|[Product Sales Analysis II](https://leetcode.com/problems/product-sales-analysis-ii/)|[Solution](../master/database/_1069.sql) | | Easy | -|1068|[Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i/)|[Solution](../master/database/_1068.sql) | | Easy | -|1050|[Actors and Directors Who Cooperated At Least Three Times](https://leetcode.com/problems/actors-and-directors-who-cooperated-at-least-three-times/)|[Solution](../master/database/_1050.sql) || Easy | -|627|[Swap Salary](https://leetcode.com/problems/swap-salary/)|[Solution](../master/database/_627.sql) | | Easy | -|626|[Exchange Seats](https://leetcode.com/problems/exchange-seats/)|[Solution](../master/database/_626.sql) | | Medium | -|620|[Not Boring Movies](https://leetcode.com/problems/not-boring-movies/)|[Solution](../master/database/_620.sql) | | Easy | -|619|[Biggest Single Number](https://leetcode.com/problems/biggest-single-number/)|[Solution](../master/database/_619.sql) | | Easy | -|618|[Students Report By Geography](https://leetcode.com/problems/students-report-by-geography/)|[Solution](../master/database/_618.sql) | | Hard | Session Variables -|615|[Average Salary: Departments VS Company](https://leetcode.com/problems/average-salary-departments-vs-company/)|[Solution](../master/database/_615.sql) | | Hard| -|614|[Second Degree Follower](https://leetcode.com/problems/second-degree-follower/)|[Solution](../master/database/_614.sql) | | Medium | Inner Join -|613|[Shortest Distance in a Line](https://leetcode.com/problems/shortest-distance-in-a-line/)|[Solution](../master/database/_613.sql) || Easy| -|612|[Shortest Distance in a Plane](https://leetcode.com/problems/shortest-distance-in-a-plane/)|[Solution](../master/database/_612.sql) || Medium| -|610|[Triangle Judgement](https://leetcode.com/problems/triangle-judgement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_610.java) | | Easy | -|608|[Tree Node](https://leetcode.com/problems/tree-node/)|[Solution](../master/database/_608.sql) | | Medium | Union -|607|[Sales Person](https://leetcode.com/problems/sales-person/)|[Solution](../master/database/_607.sql) | | Easy | -|603|[Consecutive Available Seats](https://leetcode.com/problems/sales-person/)|[Solution](../master/database/_603.sql) | | Easy | -|602|[Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/)|[Solution](../master/database/_602.sql) | | Medium | -|601|[Human Traffic of Stadium](https://leetcode.com/problems/human-traffic-of-stadium/)|[Solution](../master/database/_601.sql) | | Hard | -|597|[Friend Requests I: Overall Acceptance Rate](https://leetcode.com/problems/friend-requests-i-overall-acceptance-rate/)|[Solution](../master/database/_597.sql) | | Easy | -|596|[Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students/)|[Solution](../master/database/_596.sql) || Easy | -|595|[Big Countries](https://leetcode.com/problems/big-countries/)|[Solution](../master/database/_595.sql) | | Easy | -|586|[Customer Placing the Largest Number of Orders](https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/)|[Solution](../master/database/_586.sql) | | Easy| -|585|[Investments in 2016](https://leetcode.com/problems/investments-in-2016/)|[Solution](../master/database/_585.java) || Medium| -|584|[Find Customer Referee](https://leetcode.com/problems/find-customer-referee/)|[Solution](../master/database/_584.java) || Easy| -|580|[Count Student Number in Departments](https://leetcode.com/problems/count-student-number-in-departments/)|[Solution](../master/database/_580.sql) | |Medium | Left Join -|578|[Get Highest Answer Rate Question](https://leetcode.com/problems/get-highest-answer-rate-question/)|[Solution](../master/database/_578.sql) || Medium | -|577|[Employee Bonus](https://leetcode.com/problems/employee-bonus/)|[Solution](../master/database/_577.sql) || Easy | -|574|[Winning Candidate](https://leetcode.com/problems/winning-candidate/)|[Solution](../master/database/_574.sql) || Medium | -|571|[Find Median Given Frequency of Numbers](https://leetcode.com/problems/find-median-given-frequency-of-numbers/)|[Solution](../master/database/_571.sql) || Hard | -|570|[Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports/)|[Solution](../master/database/_570.sql) || Medium | -|569|[Median Employee Salary](https://leetcode.com/problems/median-employee-salary/)|[Solution](../master/database/_569.sql) || Hard | -|534|[Game Play Analysis III](https://leetcode.com/problems/game-play-analysis-iii/)|[Solution](../master/database/_534.sql)|| Easy| -|512|[Game Play Analysis II](https://leetcode.com/problems/game-play-analysis-ii/)|[Solution](../master/database/_512.sql)|| Easy| -|511|[Game Play Analysis I](https://leetcode.com/problems/game-play-analysis-i/)|[Solution](../master/database/_511.sql)|| Easy| -|262|[Trips and Users](https://leetcode.com/problems/trips-and-users/)|[Solution](../master/database/_262.sql)||Hard| Inner Join -|197|[Rising Temperature](https://leetcode.com/problems/rising-temperature/)|[Solution](../master/database/_197.sql)| | Easy| -|196|[Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/)|[Solution](../master/database/_196.sql)| |Easy| -|185|[Department Top Three Salaries](https://leetcode.com/problems/department-top-three-salaries)|[Solution](../master/database/_185.sql)| | Hard| -|184|[Department Highest Salary](https://leetcode.com/problems/department-highest-salary)|[Solution](../master/database/_184.sql)| | Medium| -|183|[Customers Who Never Order](https://leetcode.com/problems/customers-who-never-order/)|[Solution](../master/database/_183.sql)| | Easy| -|182|[Duplicate Emails](https://leetcode.com/problems/duplicate-emails/)|[Solution](../master/database/_182.sql)| | Easy| -|181|[Employees Earning More Than Their Managers](https://leetcode.com/problems/employees-earning-more-than-their-managers/)|[Solution](../master/database/_181.sql)| | Easy| -|180|[Consecutive Numbers](https://leetcode.com/problems/consecutive-numbers)|[Solution](../master/database/_180.sql)| | Medium| -|178|[Rank Scores](https://leetcode.com/problems/rank-scores/)|[Solution](../master/database/_178.sql)| | Medium| -|177|[Nth Highest Salary](https://leetcode.com/problems/nth-highest-salary/)|[Solution](../master/database/_177.sql)| | Medium| -|176|[Second Highest Salary](https://leetcode.com/problems/second-highest-salary/)|[Solution](../master/database/_176.sql)| | Easy| -|175|[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)|[Solution](../master/database/_175.sql)| | Easy| + + +## Database +[All database problems](https://github.com/fishercoder1534/Leetcode/tree/master/paginated_contents/database) ## Shell +[All shell problems](https://github.com/fishercoder1534/Leetcode/tree/master/paginated_contents/shell) -| # | Title | Solutions | Video | Difficulty | Tag -|-----|----------------|---------------|---------------|---------------|------------- -|195|[Tenth Line](https://leetcode.com/problems/tenth-line/)|[Solution](../master/shell/TenthLine.sh)| | Easy| -|194|[Transpose File](https://leetcode.com/problems/transpose-file/)|[Solution](../master/shell/TransposeFile.sh)| | Medium| -|193|[Valid Phone Numbers](https://leetcode.com/problems/valid-phone-numbers/)|[Solution](../master/shell/ValidPhoneNumbers.sh)| | Easy| -|192|[Word Frequency](https://leetcode.com/problems/word-frequency/)|[Solution](../master/shell/_192.sh)| | Medium| +## Javascript +[All Javascript problems](https://github.com/fishercoder1534/Leetcode/tree/master/paginated_contents/javascript) ## Contributing Your ideas/fixes/algorithms are more than welcome! +0. Please make sure your PR builds after submitting! Check out here: https://travis-ci.org/github/fishercoder1534/Leetcode/pull_requests and look for your PR build. 1. Fork this repo 2. Clone your forked repo (`git clone https://github.com/YOUR_GITHUB_USERNAME/Leetcode.git`) onto your local machine 3. `cd` into your cloned directory, create your feature branch (`git checkout -b my-awesome-fix`) @@ -1056,9 +40,9 @@ Your ideas/fixes/algorithms are more than welcome! ## Best way to open this project -1. Install Intellij on your machine, either CE or UE. +1. Install IntelliJ on your machine, either CE or UE. 2. git clone this repo to your local disk 3. import this project as a new project (does need to be imported as a gradle project) -4. If you run into "Could not determine Java version using executable ..." error, use local gradle distribution: "/usr/local/Cellar/gradle/4.8.1/libexec/" instead of the default one. More details, see [Stackoverflow](https://stackoverflow.com/questions/52195643/cannot-find-symbol-intellij-gradle/52196069#52196069). +4. If you run into "Could not determine Java version using executable ..." error, use local gradle distribution: "/usr/local/Cellar/gradle/4.8.1/libexec/" instead of the default one. More details, see [this question on Stackoverflow](https://stackoverflow.com/questions/52195643/cannot-find-symbol-intellij-gradle/52196069#52196069). diff --git a/build.gradle b/build.gradle index 994b084e07..eb7adc8423 100644 --- a/build.gradle +++ b/build.gradle @@ -1,27 +1,72 @@ -apply plugin: 'java' -apply plugin: 'checkstyle' +plugins { + id 'java' + id 'checkstyle' + id 'com.diffplug.spotless' version '6.25.0' +} group = 'com.fishercoder' version = '1.0-SNAPSHOT' -checkstyle { - //include ( '**/*.java') - configFile = file("${rootDir}/fishercoder_checkstyle.xml") +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } } -description = """""" +tasks.javadoc { + options.encoding = 'UTF-8' +} -sourceCompatibility = 1.8 -targetCompatibility = 1.8 +tasks.compileJava { + options.encoding = 'UTF-8' +} repositories { mavenCentral() - maven { url "http://repo.maven.apache.org/maven2" } } dependencies { - compile 'com.google.code.gson:gson:2.8.0' - compile 'junit:junit:4.12' - compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.0' - testCompile group: 'junit', name: 'junit', version:'4.12' + implementation 'com.google.code.gson:gson:2.10.1' + implementation 'org.apache.commons:commons-collections4:4.0' + + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.3' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.3' + + testImplementation 'org.assertj:assertj-core:3.11.1' + compileOnly 'org.projectlombok:lombok:1.18.32' + annotationProcessor 'org.projectlombok:lombok:1.18.32' + testCompileOnly 'org.projectlombok:lombok:1.18.32' + testAnnotationProcessor 'org.projectlombok:lombok:1.18.32' +} + +testing { + suites { + test { + useJUnitJupiter() + } + } +} + +tasks.withType(Test).configureEach { + maxParallelForks = Runtime.runtime.availableProcessors() +} + +checkstyle { + toolVersion = '6.17' + config = rootProject.resources.text.fromFile('fishercoder_checkstyle.xml') +} + +spotless { + java { + encoding 'UTF-8' + target fileTree(projectDir) { + include '**/src/**/*.java' + exclude '**/build/**' + } + importOrder '\\#', '', '*' + removeUnusedImports() + googleJavaFormat('1.22.0').aosp() + toggleOffOn() + endWithNewline() + } } diff --git a/cpp/_1.cpp b/cpp/_1.cpp index 63daa1f90e..0e4844eca3 100644 --- a/cpp/_1.cpp +++ b/cpp/_1.cpp @@ -1,19 +1,3 @@ -/** -1. Two Sum - -Given an array of integers, return indices of the two numbers such that they add up to a specific target. - -You may assume that each input would have exactly one solution, and you may not use the same element twice. - -Example: - -Given nums = [2, 7, 11, 15], target = 9, - -Because nums[0] + nums[1] = 2 + 7 = 9, -return [0, 1]. - -*/ - #include #include using namespace std; diff --git a/cpp/_11.cpp b/cpp/_11.cpp new file mode 100644 index 0000000000..0452f628fb --- /dev/null +++ b/cpp/_11.cpp @@ -0,0 +1,26 @@ +// container-with-most-water +// Problem Statement: https://leetcode.com/problems/container-with-most-water + +#include +using namespace std; + +class Solution { +public: + int maxArea(vector& height) { + if(height.size() < 1) + return 0; + + int left = 0; + int right = height.size() - 1; + int result = 0; + + while(left < right){ + int area = (height[left] < height[right]) ? (height[left] * (right - left)) : (height[right] * (right -left)); + result = (area > result) ? area : result; + (height[left] < height[right]) ? left++ : right--; + } + + return result; + } +}; + diff --git a/cpp/_1402.cpp b/cpp/_1402.cpp new file mode 100644 index 0000000000..53486f65a6 --- /dev/null +++ b/cpp/_1402.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int maxSatisfaction(vector& sat) { + // we'll greedily only consider a suffix of the sorted array + sort(sat.begin(), sat.end()); + int cmax = 0; + int sum = 0; + int csum = 0; + int i = sat.size(); // iterate from n - 1 to 0 + while(i--) { + // calulate current satisfaction + csum += sat[i]; + sum += csum; + // compare with cmax + cmax = max(cmax, sum); + } + return cmax; + } +}; diff --git a/cpp/_1431.cpp b/cpp/_1431.cpp new file mode 100644 index 0000000000..38e71d2da0 --- /dev/null +++ b/cpp/_1431.cpp @@ -0,0 +1,24 @@ +// Kids With the Greatest Number of Candies + +class Solution { +public: + vector kidsWithCandies(vector& candies, int extraCandies) { + + vector v; + int mx=0; + + for(int i=0;i shuffle(vector& nums, int n) { + + vector v; + + for(int i=0;i runningSum(vector& nums) { + + for(int i=1;i> threeSum(vector& nums) { + sort(nums.begin(),nums.end()); + + vector> v; + + if(nums.size()<3){ + //if vector nums has less than 3 elements, impossible to distribute into 3 element set -> return 0. + return v; + } + + for(int i=0;i0 and nums[i]==nums[i-1]){ + //same elements dont have to be taken. + continue; + } + + int l = i+1, r = nums.size()-1; + //Using 2-pointer concept + while(l0){ + r--; + }else if(sum<0){ + l++; + }else{ + v.push_back(vector {nums[i],nums[l],nums[r]}); + while(l0) i-=x; + x=0; + memset(visit,0,sizeof(visit)); + } + } + mx = max(x,mx); + return mx; + }else{ + // There's a only white-spaces. + if(s.size()>0) return 1; + else return 0; + } + + } +}; + +//Solutio 2: Using Map Method (High Memory and Time) + +class Solution { +public: + int lengthOfLongestSubstring(string s) { + + unordered_map ump; + + int mx=0,x=0; + for(int i=0;i0) i-=x; + x=0; + ump.clear(); + } + } + mx = max(x,mx); + return mx; + + } +}; diff --git a/cpp/_31.cpp b/cpp/_31.cpp new file mode 100644 index 0000000000..dcf1138f46 --- /dev/null +++ b/cpp/_31.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + void nextPermutation(vector& nums) { + + int i1; + int i2; + bool hasPermutation = false; + + for(int i=nums.size()-1; i>0; i--){ + if(nums[i-1]i1; i--){ + if(nums[i]>nums[i1]){ + j=i; + break; + } + } + swap(nums[i1], nums[j]); + reverse(nums.begin()+i1+1, nums.end()); + }else{ + sort(nums.begin(), nums.end()); + } + + } +}; diff --git a/cpp/_322.cpp b/cpp/_322.cpp new file mode 100644 index 0000000000..852a6b5a20 --- /dev/null +++ b/cpp/_322.cpp @@ -0,0 +1,20 @@ +// coin-change +// Problem Statement: https://leetcode.com/problems/coin-change/ + +class Solution{ +public: + int coinChange(vector& coins, int amount){ + + int MAX = amount + 1; + vector cache(amount + 1, MAX); + + cache[0] = 0; + for(auto coin : coins){ + for(int i = coin; i <= amount; i++) + cache[i] = std::min(cache[i], cache[i - coin] + 1); + } + + return cache[amount] == MAX ? -1 : cache[amount]; + } +}; + diff --git a/cpp/_4.cpp b/cpp/_4.cpp new file mode 100644 index 0000000000..803268c9fb --- /dev/null +++ b/cpp/_4.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + //for adding second vector to last of first vector + nums1.insert(nums1.end(),nums2.begin(),nums2.end()); + sort(nums1.begin(),nums1.end()); + + int size = nums1.size(); + float sum = nums1[size/2]; + + if(size%2==0){ + sum += nums1[(size/2)-1]; + sum/=2; + } + + return (float)(sum); + } +}; diff --git a/cpp/_41.cpp b/cpp/_41.cpp new file mode 100644 index 0000000000..892d77686a --- /dev/null +++ b/cpp/_41.cpp @@ -0,0 +1,12 @@ +//Problem Link : https://leetcode.com/problems/first-missing-positive/ + +class Solution { +public: + int firstMissingPositive(vector& nums) { + for(int i=1;i<=nums.size()+1;i++){ + int d=count(nums.begin(),nums.end(),i); + if(d==0) return i; + } + return 0; + } +}; \ No newline at end of file diff --git a/cpp/_416.cpp b/cpp/_416.cpp new file mode 100644 index 0000000000..3a8354b158 --- /dev/null +++ b/cpp/_416.cpp @@ -0,0 +1,42 @@ +//416. Partition Equal Subset Sum + +class Solution { +public: + //maximum size top-down dp table + bool dp[201][20001]; + + bool equalPart(vector& arr,int size,int sum){ + for(int i=0;i<=size;i++){ + dp[i][0]=true; + } + for(int i=1;i<=sum;i++){ + dp[0][i]=false; + } + + for(int i=1;i<=size;i++){ + for(int j=1;j<=sum;j++){ + + if(arr[i-1]<=j){ + dp[i][j] = dp[i-1][j] || dp[i-1][j-arr[i-1]]; + }else{ + dp[i][j] = dp[i-1][j]; + } + + } + } + + return dp[size][sum]; + } + + bool canPartition(vector& nums) { + int sum=0; + for(int i=0;i &nums, vector visited, int idx,int k,int currSum,int targetSum){ + if(k==1){ + //All subsets are found + return true; + } + if(currSum==targetSum){ + //one subset found + return dfs(nums,visited,0,k-1,0,targetSum); + } + for(int i=idx;i& nums, int k) { + vector visited(nums.size(),false); + int sum=0; + for(int i=0;i0){ + int last = x%10; + res = res*10 + last; + x/=10; + } + + //Adding minus sign in the result + if(isMinus){ + res *= -1; + } + //Range of int + int mn = -2147483648, mx = 2147483647; + + //Checking if is in range of int + if(res > mn and res < mx){ + res = res; + }else{ + res = 0; + } + + return res; + } +}; diff --git a/cpp/_72.cpp b/cpp/_72.cpp new file mode 100644 index 0000000000..18f4f8e4d7 --- /dev/null +++ b/cpp/_72.cpp @@ -0,0 +1,26 @@ + +//Problem Link : https://leetcode.com/problems/edit-distance/ +//Method : DP + +class Solution { +public: + int minDistance(string word1, string word2) { + int n=word1.length(); + int m=word2.length(); + int dp[n+1][m+1]; + for(int i=0;i<=n;i++){ + for(int j=0;j<=m;j++){ + if(i==0) + dp[i][j]=j; + else if(j==0) + dp[i][j]=i; + else if(word1[i-1]==word2[j-1]) + dp[i][j]=dp[i-1][j-1]; + else + dp[i][j]=1+min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1])); + + } + } + return dp[n][m]; + } +}; \ No newline at end of file diff --git a/cpp/_9.cpp b/cpp/_9.cpp index 910694b5bf..26cf0a4f5a 100644 --- a/cpp/_9.cpp +++ b/cpp/_9.cpp @@ -1,27 +1,3 @@ -/** - * 9. Palindrome Number - * - * Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. - -Example 1: - -Input: 121 -Output: true -Example 2: - -Input: -121 -Output: false -Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. -Example 3: - -Input: 10 -Output: false -Explanation: Reads 01 from right to left. Therefore it is not a palindrome. -Follow up: - -Coud you solve it without converting the integer to a string? -*/ - class Solution1 { /**This is a Java translation, while its Java counterpart could be accepted perfectly, this one failed due to * Char 21: runtime error: signed integer overflow: 746384741 * 10 cannot be represented in type 'int' (solution.cpp) diff --git a/cpp/_916.cpp b/cpp/_916.cpp new file mode 100644 index 0000000000..7ac4a644a1 --- /dev/null +++ b/cpp/_916.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + vector wordSubsets(vector& words1, vector& words2) { + int maxCharFreq[26] = {0}; + int tempCharFreq[26]; + for (const auto& word : words2) { + memset(tempCharFreq, 0, sizeof tempCharFreq); + for (char ch : word) { + tempCharFreq[ch - 'a']++; + } + for (int i = 0; i < 26; ++i) { + maxCharFreq[i] = max(maxCharFreq[i], tempCharFreq[i]); + } + } + vector universalWords; + for (const auto& word : words1) { + memset(tempCharFreq, 0, sizeof tempCharFreq); + for (char ch : word) { + tempCharFreq[ch - 'a']++; + } + bool isUniversal = true; + for (int i = 0; i < 26; ++i) { + if (maxCharFreq[i] > tempCharFreq[i]) { + isUniversal = false; + break; + } + } + if (isUniversal) { + universalWords.emplace_back(word); + } + } + return universalWords; + } +}; diff --git a/database/_1113.sql b/database/_1113.sql new file mode 100644 index 0000000000..d6986c49f3 --- /dev/null +++ b/database/_1113.sql @@ -0,0 +1 @@ +select extra as report_reason, count(distinct(post_id)) as report_count from Actions where action_date = '2019-07-04' and action = 'report' group by extra; \ No newline at end of file diff --git a/database/_1142.sql b/database/_1142.sql new file mode 100644 index 0000000000..6c6d9e4a05 --- /dev/null +++ b/database/_1142.sql @@ -0,0 +1,5 @@ +--# Write your MySQL query statement below +select ifnull(round(count(distinct session_id)/count(distinct user_id), 2), 0.00) +as average_sessions_per_user +from Activity +where activity_date between '2019-06-28' and '2019-07-27'; \ No newline at end of file diff --git a/database/_1211.sql b/database/_1211.sql new file mode 100644 index 0000000000..56e537ac57 --- /dev/null +++ b/database/_1211.sql @@ -0,0 +1 @@ +select query_name, round(avg(rating/position), 2) as quality, round(avg(rating < 3) * 100, 2) as poor_query_percentage from Queries group by query_name; \ No newline at end of file diff --git a/database/_1371.sql b/database/_1371.sql new file mode 100644 index 0000000000..12569bf18a --- /dev/null +++ b/database/_1371.sql @@ -0,0 +1,5 @@ +--# Write your MySQL query statement below +select b.employee_id, b.name, count(*) as reports_count, round(avg(a.age)) as average_age +from Employees a join Employees b on a.reports_to = b.employee_id +group by b.employee_id +order by b.employee_id \ No newline at end of file diff --git a/database/_1393.sql b/database/_1393.sql new file mode 100644 index 0000000000..4708d15351 --- /dev/null +++ b/database/_1393.sql @@ -0,0 +1,6 @@ +select stock_name, sum( + case + when operation = 'Buy' then -price + else price + end +) as capital_gain_loss from Stocks group by stock_name; \ No newline at end of file diff --git a/database/_1421.sql b/database/_1421.sql new file mode 100644 index 0000000000..f0cfa9f448 --- /dev/null +++ b/database/_1421.sql @@ -0,0 +1,2 @@ +-- # Write your MySQL query statement below +select q.id, q.year, ifnull(n.npv, 0) as npv from Queries as q left join NPV as n on q.id = n.id and q.year = n.year \ No newline at end of file diff --git a/database/_1435.sql b/database/_1435.sql new file mode 100644 index 0000000000..fffcc3a14e --- /dev/null +++ b/database/_1435.sql @@ -0,0 +1,14 @@ +-- # Write your MySQL query statement below +with groupedBy as ( + select "[0-5>" as bin, 0 as minDuration, 60*5 as maxDuration + union all + select "[5-10>" as bin, 60*5 as minDuration, 60*10 as maxDuration + union all + select "[10-15>" as bin, 60*10 as minDuration, 60*15 as maxDuration + union all + select "15 or more" as bin, 60*15 as minDuration, 2147483647 as maxDuration +) + +select bin, count(session_id) as total from groupedBy +left join Sessions on duration >= minDuration and duration < maxDuration +group by bin; \ No newline at end of file diff --git a/database/_1445.sql b/database/_1445.sql new file mode 100644 index 0000000000..43dbe93d22 --- /dev/null +++ b/database/_1445.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select sale_date as SALE_DATE, (o.sold_num - a.sold_num) as DIFF from Sales a join Sales o using(sale_date) group by sale_date order by sale_date \ No newline at end of file diff --git a/database/_1484.sql b/database/_1484.sql new file mode 100644 index 0000000000..87bda41d24 --- /dev/null +++ b/database/_1484.sql @@ -0,0 +1,7 @@ +--# Write your MySQL query statement below +select sell_date, + count(distinct(product)) as num_sold, + group_concat(distinct product order by product asc separator ',') as products +from Activities +group by sell_date +order by sell_date asc; \ No newline at end of file diff --git a/database/_1495.sql b/database/_1495.sql new file mode 100644 index 0000000000..86fa7717a2 --- /dev/null +++ b/database/_1495.sql @@ -0,0 +1,4 @@ +--# Write your MySQL query statement below +select title from Content where Kids_content = 'Y' and content_id in ( +select content_id from TVProgram where program_date between '2020-06-01 00:00' and '2020-06-30 23:59' +) and content_type = 'Movies' \ No newline at end of file diff --git a/database/_1511.sql b/database/_1511.sql new file mode 100644 index 0000000000..a595c1d40c --- /dev/null +++ b/database/_1511.sql @@ -0,0 +1,8 @@ +select customer_id, name from Customers +join Orders using(customer_id) +join Product using(product_id) +group by customer_id +having +sum(if(left(order_date, 7) = '2020-06', quantity, 0) * price) >= 100 +and +sum(if(left(order_date, 7) = '2020-07', quantity, 0) * price) >= 100; \ No newline at end of file diff --git a/database/_1517.sql b/database/_1517.sql new file mode 100644 index 0000000000..8a4bf14917 --- /dev/null +++ b/database/_1517.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select * from Users where regexp_like(mail, '^[A-Za-z]+[A-Za-z0-9\_\.\-]*@leetcode.com') \ No newline at end of file diff --git a/database/_1527.sql b/database/_1527.sql new file mode 100644 index 0000000000..00d2b6eff3 --- /dev/null +++ b/database/_1527.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select * from Patients where conditions like "DIAB1%" or conditions like "% DIAB1%"; \ No newline at end of file diff --git a/database/_1543.sql b/database/_1543.sql new file mode 100644 index 0000000000..37ff4e42bd --- /dev/null +++ b/database/_1543.sql @@ -0,0 +1,5 @@ +--# Write your MySQL query statement below +select lower(trim(product_name)) product_name, date_format(sale_date, "%Y-%m") sale_date, count(sale_id) total +from sales +group by 1, 2 +order by 1, 2 \ No newline at end of file diff --git a/database/_1565.sql b/database/_1565.sql new file mode 100644 index 0000000000..be676b0c49 --- /dev/null +++ b/database/_1565.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select LEFT(order_date, 7) as month, count(order_id) as order_count, count(distinct(customer_id)) as customer_count from Orders where invoice > 20 group by month; \ No newline at end of file diff --git a/database/_1571.sql b/database/_1571.sql new file mode 100644 index 0000000000..0cc7037f92 --- /dev/null +++ b/database/_1571.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select name as warehouse_name, sum(units * Width * Length * Height) as volume from Warehouse w left join Products p on w.product_id = p.product_id group by name; \ No newline at end of file diff --git a/database/_1581.sql b/database/_1581.sql new file mode 100644 index 0000000000..f9d5ce39c0 --- /dev/null +++ b/database/_1581.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select customer_id as customer_id, count(*) as count_no_trans from Visits where visit_id not in (select distinct visit_id from Transactions) group by customer_id; \ No newline at end of file diff --git a/database/_1587.sql b/database/_1587.sql new file mode 100644 index 0000000000..a921308868 --- /dev/null +++ b/database/_1587.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select u.name, sum(t.amount) as balance from Users u join Transactions t on u.account = t.account group by u.account having balance > 10000; \ No newline at end of file diff --git a/database/_1596.sql b/database/_1596.sql new file mode 100644 index 0000000000..50b10c1f01 --- /dev/null +++ b/database/_1596.sql @@ -0,0 +1,13 @@ +--credit: https://leetcode.com/problems/the-most-frequently-ordered-products-for-each-customer/discuss/861257/simple-and-easy-solution-using-window-function + +select customer_id, product_id, product_name from +( + select o.customer_id, o.product_id, p.product_name, + rank() over (partition by customer_id order by count(o.product_id) desc) as ranking + from Orders o + join Products p + on o.product_id = p.product_id + group by customer_id, product_id +) tmp +where ranking = 1 +order by customer_id, product_id \ No newline at end of file diff --git a/database/_1607.sql b/database/_1607.sql new file mode 100644 index 0000000000..5845c790f9 --- /dev/null +++ b/database/_1607.sql @@ -0,0 +1,4 @@ +--# Write your MySQL query statement below +select seller_name from Seller as SELLER_NAME where seller_id not in ( +select distinct(seller_id) from Orders where sale_date between '2020-01-01' and '2020-12-31' +) order by seller_name; \ No newline at end of file diff --git a/database/_1623.sql b/database/_1623.sql new file mode 100644 index 0000000000..125d8f6292 --- /dev/null +++ b/database/_1623.sql @@ -0,0 +1,10 @@ +--# Write your MySQL query statement below +select a.student_name as member_A, +b.student_name as member_B, +c.student_name as member_C +from SchoolA a cross join SchoolB b cross join SchoolC c where a.student_name != b.student_name and +a.student_id != b.student_id and +b.student_name != c.student_name and +b.student_id != c.student_id and +a.student_name != c.student_name and +a.student_id != c.student_id; \ No newline at end of file diff --git a/database/_1633.sql b/database/_1633.sql new file mode 100644 index 0000000000..a9524edf9c --- /dev/null +++ b/database/_1633.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select contest_id, round((count(distinct(user_id)) * 100 / (select count(*) from Users)), 2) as percentage from Register group by contest_id order by percentage desc, contest_id; \ No newline at end of file diff --git a/database/_1661.sql b/database/_1661.sql new file mode 100644 index 0000000000..106283e8de --- /dev/null +++ b/database/_1661.sql @@ -0,0 +1,3 @@ +select s.machine_id, round(avg(e.timestamp - s.timestamp), 3) as processing_time +from Activity s join Activity e +on s.machine_id = e.machine_id and s.process_id = e.process_id and s.activity_type = 'start' and e.activity_type = 'end' group by s.machine_id; \ No newline at end of file diff --git a/database/_1667.sql b/database/_1667.sql new file mode 100644 index 0000000000..eecf6109bc --- /dev/null +++ b/database/_1667.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select user_id, concat(UCASE(LEFT(name, 1)), LCASE(SUBSTRING(name, 2))) as name from Users order by user_id; \ No newline at end of file diff --git a/database/_1677.sql b/database/_1677.sql new file mode 100644 index 0000000000..74623f8b22 --- /dev/null +++ b/database/_1677.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select name, sum(rest) as rest, sum(paid) as paid, sum(canceled) as canceled, sum(refunded) as refunded from Product join Invoice using (product_id) group by name order by name; \ No newline at end of file diff --git a/database/_1683.sql b/database/_1683.sql new file mode 100644 index 0000000000..2b9ba9160c --- /dev/null +++ b/database/_1683.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select tweet_id from Tweets where length(content) > 15; \ No newline at end of file diff --git a/database/_1693.sql b/database/_1693.sql new file mode 100644 index 0000000000..0a3b7424b0 --- /dev/null +++ b/database/_1693.sql @@ -0,0 +1,3 @@ +--# Write your MySQL query statement below +select date_id, make_name, count(distinct(lead_id)) as unique_leads, +count(distinct(partner_id)) as unique_partners from DailySales group by date_id, make_name; \ No newline at end of file diff --git a/database/_1709.sql b/database/_1709.sql new file mode 100644 index 0000000000..13ea5688fa --- /dev/null +++ b/database/_1709.sql @@ -0,0 +1,6 @@ +--# Write your MySQL query statement below +with diff_table as ( +select user_id, datediff(lead(visit_date, 1, '2021-1-1') over (partition by user_id order by visit_date), visit_date) as diff from UserVisits +) + +select user_id, max(diff) as biggest_window from diff_table group by user_id order by user_id diff --git a/database/_1729.sql b/database/_1729.sql new file mode 100644 index 0000000000..8b719e29cd --- /dev/null +++ b/database/_1729.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select user_id, count(follower_id) as followers_count from followers group by user_id order by user_id; \ No newline at end of file diff --git a/database/_1741.sql b/database/_1741.sql new file mode 100644 index 0000000000..598310e088 --- /dev/null +++ b/database/_1741.sql @@ -0,0 +1,4 @@ +-- # Write your MySQL query statement below +select event_day as day, emp_id, sum(out_time - in_time) as total_time +from Employees +group by day, emp_id \ No newline at end of file diff --git a/database/_175.sql b/database/_175.sql index 900eb9c0c4..9d3943332d 100644 --- a/database/_175.sql +++ b/database/_175.sql @@ -1 +1,3 @@ -select Person.FirstName, Person.LastName, Address.City, Address.State from Person left join Address on Person.PersonId = Address.PersonId; \ No newline at end of file +# Write your MySQL query statement below +select p.firstName, p.lastName, a.city, a.state +from Person as p left join Address as a on p.personId = a.personId \ No newline at end of file diff --git a/database/_1757.sql b/database/_1757.sql new file mode 100644 index 0000000000..2290bc7447 --- /dev/null +++ b/database/_1757.sql @@ -0,0 +1,2 @@ +--# Write your MySQL query statement below +select product_id from Products where low_fats = 'Y' and recyclable = 'Y'; \ No newline at end of file diff --git a/database/_1777.sql b/database/_1777.sql new file mode 100644 index 0000000000..73d0d1b4e9 --- /dev/null +++ b/database/_1777.sql @@ -0,0 +1,10 @@ +-- # Write your MySQL query statement below +select + product_id, + max(case when store = 'store1' then price end) as store1, + max(case when store = 'store2' then price end) as store2, + max(case when store = 'store3' then price end) as store3 +from + Products +group by + product_id \ No newline at end of file diff --git a/database/_1789.sql b/database/_1789.sql new file mode 100644 index 0000000000..f9243620b8 --- /dev/null +++ b/database/_1789.sql @@ -0,0 +1,5 @@ +-- # Write your MySQL query statement below +select employee_id, department_id +from (select *, count(employee_id) over(partition by employee_id) as EmployeeCount from Employee) EP +where EmployeeCount = 1 + or primary_flag = 'Y'; \ No newline at end of file diff --git a/database/_1795.sql b/database/_1795.sql new file mode 100644 index 0000000000..ca0ec9e840 --- /dev/null +++ b/database/_1795.sql @@ -0,0 +1,12 @@ +-- Write your PostgreSQL query statement below +select product_id, 'store1' as store, store1 as price +from Products +where store1 is not null +union +select product_id, 'store2' as store, store2 as price +from Products +where store2 is not null +union +select product_id, 'store3' as store, store3 as price +from Products +where store3 is not null \ No newline at end of file diff --git a/database/_1809.sql b/database/_1809.sql new file mode 100644 index 0000000000..3edd97dd63 --- /dev/null +++ b/database/_1809.sql @@ -0,0 +1,13 @@ +-- Write your PostgreSQL query statement below +-- my completely original solution +with sessions_with_ads as (select p.customer_id, p.session_id, p.start_time, p.end_time, a.timestamp as ts, a.ad_id + from Playback p + inner join Ads a + on p.customer_id = a.customer_id and + a.timestamp between p.start_time and p.end_time) + +select distinct(session_id) +from (select distinct(session_id) from Playback) +where session_id not in (select distinct(session_id) from sessions_with_ads) + + diff --git a/database/_1821.sql b/database/_1821.sql new file mode 100644 index 0000000000..b54cbb1a80 --- /dev/null +++ b/database/_1821.sql @@ -0,0 +1,2 @@ +-- Write your PostgreSQL query statement below +select customer_id from Customers where revenue > 0 and year = 2021; \ No newline at end of file diff --git a/database/_1853.sql b/database/_1853.sql new file mode 100644 index 0000000000..ed66975b89 --- /dev/null +++ b/database/_1853.sql @@ -0,0 +1,7 @@ +-- Write your PostgreSQL query statement below +-- 1. use to_char function +-- 2. use FM to strip padding space, FM is a data type formatting function in PostgreSQL that removes leading zeros and trailing spaces that would otherwise be added to make a pattern's output fixed-width +-- 3. Day gives you the day in the week +-- 4. DD gives you the day in the month +-- 5. YYYY gives you the year +select to_char(day, 'FMDay, FMMonth FMDD, YYYY') as day from Days; \ No newline at end of file diff --git a/database/_1873.sql b/database/_1873.sql new file mode 100644 index 0000000000..d162a8a5ef --- /dev/null +++ b/database/_1873.sql @@ -0,0 +1,13 @@ +-- Write your PostgreSQL query statement below +with employee_bonus as ( + select employee_id, salary as bonus from Employees where name not like 'M%' and employee_id % 2 = 1 + ), + non_employee_bonus as ( + select employee_id, 0 as bonus from Employees where employee_id not in ( + select employee_id from employee_bonus + )) + +select * from employee_bonus +union +select * from non_employee_bonus +order by employee_id \ No newline at end of file diff --git a/database/_1978.sql b/database/_1978.sql new file mode 100644 index 0000000000..df249d6d20 --- /dev/null +++ b/database/_1978.sql @@ -0,0 +1,7 @@ +-- Write your PostgreSQL query statement below +select employee_id +from Employees +where manager_id not in + (select employee_id from Employees) + and salary < 30000 +order by 1 asc \ No newline at end of file diff --git a/database/_2026.sql b/database/_2026.sql new file mode 100644 index 0000000000..90c256aa68 --- /dev/null +++ b/database/_2026.sql @@ -0,0 +1,5 @@ +-- Write your PostgreSQL query statement below +select problem_id +from Problems +where (likes / ((likes + dislikes) * 1.0)) < 0.6 +order by 1; \ No newline at end of file diff --git a/database/_2072.sql b/database/_2072.sql new file mode 100644 index 0000000000..c8ea38b1e7 --- /dev/null +++ b/database/_2072.sql @@ -0,0 +1,13 @@ +-- Write your PostgreSQL query statement below + + +with excellent_std_cnts as (select (select count(student_id) from NewYork where score >= 90) as ny, + (select count(student_id) from California where score >= 90) as ca) + +select (case + when E.ny > E.ca then 'New York University' + when E.ny < E.ca then 'California University' + else 'No Winner' + end + ) as winner +from excellent_std_cnts as E \ No newline at end of file diff --git a/database/_2082.sql b/database/_2082.sql new file mode 100644 index 0000000000..aa4a8a0a34 --- /dev/null +++ b/database/_2082.sql @@ -0,0 +1,4 @@ +-- #Write your MySQL query statement below +select count(distinct customer_id) as rich_count +from Store +where amount > 500; \ No newline at end of file diff --git a/database/_2205.sql b/database/_2205.sql new file mode 100644 index 0000000000..61b43f8de0 --- /dev/null +++ b/database/_2205.sql @@ -0,0 +1,8 @@ +CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT +BEGIN +RETURN ( +-- # Write your MySQL query statement below. + select count(distinct(user_id)) from Purchases where + time_stamp between timestamp(startDate) and timestamp(endDate) and amount >= minAmount + ); +END \ No newline at end of file diff --git a/database/_2990.sql b/database/_2990.sql new file mode 100644 index 0000000000..e5d8b149fb --- /dev/null +++ b/database/_2990.sql @@ -0,0 +1,7 @@ +-- # Write your MySQL query statement below +select user_id +from Loans +group by user_id +having sum(loan_type = "Refinance") > 0 + and sum(loan_type = "Mortgage") > 0 +order by 1 asc; \ No newline at end of file diff --git a/database/_3051.sql b/database/_3051.sql new file mode 100644 index 0000000000..f842f8aec8 --- /dev/null +++ b/database/_3051.sql @@ -0,0 +1,4 @@ +-- Write your PostgreSQL query statement below +select candidate_id from Candidates +where skill in ('Python', 'Tableau', 'PostgreSQL') group by 1 +having count(candidate_id) = 3 order by 1 \ No newline at end of file diff --git a/database/_3059.sql b/database/_3059.sql new file mode 100644 index 0000000000..109275b472 --- /dev/null +++ b/database/_3059.sql @@ -0,0 +1,7 @@ +-- Write your MySQL query statement below +select substring(email, position('@' in email) + 1) as email_domain, + count(*) as count +from emails +where email like '%@%.com' +group by 1 +order by 1; \ No newline at end of file diff --git a/database/_3150.sql b/database/_3150.sql new file mode 100644 index 0000000000..72d0b92c40 --- /dev/null +++ b/database/_3150.sql @@ -0,0 +1,7 @@ +-- Write your PostgreSQL query statement below +select tweet_id +from tweets +where length(content) > 140 + or regexp_count(content, '@\w') > 3 + or regexp_count(content, '#\w') > 3 +order by 1 \ No newline at end of file diff --git a/database/_3172.sql b/database/_3172.sql new file mode 100644 index 0000000000..2476c1a0ad --- /dev/null +++ b/database/_3172.sql @@ -0,0 +1,7 @@ +-- Write your MySQL query statement below +-- my completely original solution +select user_id +from emails as e + join texts as t on e.email_id = t.email_id +where date_add(date(e.signup_date), Interval 1 Day) = date (t.action_date) and signup_action = 'Verified' +order by user_id diff --git a/database/_3198.sql b/database/_3198.sql new file mode 100644 index 0000000000..3a0cd98d46 --- /dev/null +++ b/database/_3198.sql @@ -0,0 +1,6 @@ +-- # Write your MySQL query statement below +select state, + group_concat(city order by city SEPARATOR ', ') as cities +from cities +group by state +order by state \ No newline at end of file diff --git a/fishercoder_checkstyle.xml b/fishercoder_checkstyle.xml index a6c27166f6..975ba8af4b 100755 --- a/fishercoder_checkstyle.xml +++ b/fishercoder_checkstyle.xml @@ -55,8 +55,6 @@ - - diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 01b8bf6b1f..e6441136f3 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index c3c3e4eb15..09523c0e54 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ -#Sat Aug 10 07:31:35 PDT 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip diff --git a/javascript/_1.js b/javascript/_1.js new file mode 100644 index 0000000000..1f7b8070ab --- /dev/null +++ b/javascript/_1.js @@ -0,0 +1,14 @@ +// Two sum + +var twoSum = function (nums, target) { + var sum = {} + for (var i = 0; i < nums.length; i++) { + if (!Object.prototype.hasOwnProperty.call(sum, (target - nums[i]))) { + sum[nums[i]] = i + } else { + return [i, sum[target - nums[i]]] + } + } +} + +console.log(twoSum([2, 7, 11, 15], 9)) diff --git a/javascript/_10.js b/javascript/_10.js new file mode 100644 index 0000000000..75c8f391ba --- /dev/null +++ b/javascript/_10.js @@ -0,0 +1,6 @@ + +var isMatch = function (s, p) { + return (new RegExp('^' + p + '$')).test(s) +} + +console.log(isMatch('aa', 'a*')) diff --git a/javascript/_1146.js b/javascript/_1146.js new file mode 100644 index 0000000000..d4c9dad7c3 --- /dev/null +++ b/javascript/_1146.js @@ -0,0 +1,48 @@ +/** + * Author: Phuong Lam + */ + +/** + * @param {number} length + */ +var SnapshotArray = function (length) { + this.changes = {} + this.snapShots = [] +} + +/** + * @param {number} index + * @param {number} val + * @return {void} + */ +SnapshotArray.prototype.set = function (index, val) { + this.changes[index] = val +} + +/** + * @return {number} + */ +SnapshotArray.prototype.snap = function () { + this.snapShots.push(this.changes) + this.changes = {} + return this.snapShots.length - 1 +} + +/** + * @param {number} index + * @param {number} snap_id + * @return {number} + */ +SnapshotArray.prototype.get = function (index, snap_id) { + while (snap_id >= 0 && this.snapShots[snap_id][index] === undefined) snap_id-- + if (snap_id >= 0) return this.snapShots[snap_id][index] + return 0 +} + +/** + * Your SnapshotArray object will be instantiated and called as such: + * var obj = new SnapshotArray(length) + * obj.set(index,val) + * var param_2 = obj.snap() + * var param_3 = obj.get(index,snap_id) + */ diff --git a/javascript/_1295.js b/javascript/_1295.js new file mode 100644 index 0000000000..d536276b29 --- /dev/null +++ b/javascript/_1295.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @return {number} + */ + +var findNumbers = function (nums) { + let result = 0 + for (let i = 0; i < nums.length; i++) { + const num = String(nums[i]) + if (num.length % 2 === 0) result++ + } + return result +} + +console.log(findNumbers([12, 345, 2, 6, 7896])) diff --git a/javascript/_1637.js b/javascript/_1637.js new file mode 100644 index 0000000000..98083789c0 --- /dev/null +++ b/javascript/_1637.js @@ -0,0 +1,23 @@ +// Author: Phuong Lam + +/** + * @param {number[][]} points + * @return {number} + */ +var maxWidthOfVerticalArea = function(points) { + const sortedX = points + .map((point) => { + return point[0] + }) + .sort((a, b) => { + if (a < b) return -1 + if (a > b) return 1 + return 0 + }) + var max = 0 + for (let i = 0; i < sortedX.length - 1; i++) { + const d = sortedX[i + 1] - sortedX[i] + if (d > max) max = d + } + return max +}; \ No newline at end of file diff --git a/javascript/_1658.js b/javascript/_1658.js new file mode 100644 index 0000000000..d308fb73a0 --- /dev/null +++ b/javascript/_1658.js @@ -0,0 +1,25 @@ +// Author: Phuong Lam + +/** + * @param {number[]} nums + * @param {number} x + * @return {number} + */ +var minOperations = function (nums, x) { + const total = nums.reduce((a, b) => a + b) + if (total === x) return nums.length + + var sum = 0 + var head = 0 + var max = -1 + for (var tail = 0; tail < nums.length; tail++) { + sum += nums[tail] + while (sum > total - x) { + sum -= nums[head] + head++ + } + if (sum === total - x) max = Math.max(max, tail - head + 1) + } + + return max === -1 ? -1 : nums.length - max +} diff --git a/javascript/_1664.js b/javascript/_1664.js new file mode 100644 index 0000000000..07c432dede --- /dev/null +++ b/javascript/_1664.js @@ -0,0 +1,37 @@ +// Author: Phuong Lam + +/** + * @param {number[]} nums + * @return {number} + */ +var waysToMakeFair = function (nums) { + var total = 0 + + var lo = 0 // left odd + var le = 0 // left even + var ro = 0 // right odd + var re = 0 // right even + + // when ignore 1 element => right odd mean i % 2 === 0 + for (i = 0; i < nums.length; i++) { + if (i % 2 === 0) ro += nums[i] + else re += nums[i] + } + + // remove position i, recalculate left - right even-odd + for (i = 0; i < nums.length; i++) { + // recalculate left + if (i > 0) { + if (i % 2 === 0) lo += nums[i - 1] + else le += nums[i - 1] + } + + // recalculate right + if (i % 2 === 0) ro -= nums[i] + else re -= nums[i] + + // calc total even & odd + if (lo + ro === le + re) total++ + } + return total +} diff --git a/javascript/_167.js b/javascript/_167.js new file mode 100644 index 0000000000..0c47bc9d0c --- /dev/null +++ b/javascript/_167.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} numbers + * @param {number} target + * @return {number[]} + */ +var twoSum = function (numbers, target) { + var p1 = 0 + var p2 = numbers.length - 1 + + while (p1 < p2) { + if (numbers[p1] + numbers[p2] < target) { + p1 += 1 + } else if (numbers[p1] + numbers[p2] > target) { + p2 -= 1 + } else { + return [p1 + 1, p2 + 1] + } + } + return [-1, -1] +} + +console.log(twoSum([2, 7, 11, 15], 9)) diff --git a/javascript/_17.js b/javascript/_17.js new file mode 100644 index 0000000000..f950e696de --- /dev/null +++ b/javascript/_17.js @@ -0,0 +1,43 @@ +function letterCombinations(digits) { + // If the input is an empty string, return an empty array. + if (digits.length === 0) { + return []; + } + + // Mapping of digits to letters as per the telephone keypad using a javascript dictionary. + const digitToChar = { + '2': ['a', 'b', 'c'], + '3': ['d', 'e', 'f'], + '4': ['g', 'h', 'i'], + '5': ['j', 'k', 'l'], + '6': ['m', 'n', 'o'], + '7': ['p', 'q', 'r', 's'], + '8': ['t', 'u', 'v'], + '9': ['w', 'x', 'y', 'z'] + }; + + // Resultant array to store all possible combinations + const result = []; + + // Backtracking function to generate combinations + function backtrack(index, currentCombination) { + // if the current combination has the same length as the input digits. + if (index === digits.length) { + result.push(currentCombination); + return; + } + + // Get the letters that the current digit maps to. + let letters = digitToChar[digits[index]]; + + // Loop through the letters and call backtrack recursively for the next digit. + for (let letter of letters) { + backtrack(index + 1, currentCombination + letter); + } + } + + // Start backtracking from the first digit (index 0) with an empty string as the initial combination. + backtrack(0, ''); + + return result; +}; diff --git a/javascript/_2619.js b/javascript/_2619.js new file mode 100644 index 0000000000..219bb7c1c8 --- /dev/null +++ b/javascript/_2619.js @@ -0,0 +1,14 @@ +/** + * @return {null|boolean|number|string|Array|Object} + */ +Array.prototype.last = function () { + if (this.length === 0) { + return -1; + } + return this[this.length - 1]; +}; + +/** + * const arr = [1, 2, 3]; + * arr.last(); // 3 + */ \ No newline at end of file diff --git a/javascript/_2620.js b/javascript/_2620.js new file mode 100644 index 0000000000..28764ab0f4 --- /dev/null +++ b/javascript/_2620.js @@ -0,0 +1,18 @@ +/** + * @param {number} n + * @return {Function} counter + */ +var createCounter = function (n) { + let counter = n; + + return function () { + return n++; + }; +}; + +/** + * const counter = createCounter(10) + * counter() // 10 + * counter() // 11 + * counter() // 12 + */ \ No newline at end of file diff --git a/javascript/_2703.js b/javascript/_2703.js new file mode 100644 index 0000000000..8ff8668336 --- /dev/null +++ b/javascript/_2703.js @@ -0,0 +1,7 @@ +/** + * @param {...(null|boolean|number|string|Array|Object)} args + * @return {number} + */ +var argumentsLength = function (...args) { + return args.length; +}; \ No newline at end of file diff --git a/javascript/_3.js b/javascript/_3.js new file mode 100644 index 0000000000..2218e25361 --- /dev/null +++ b/javascript/_3.js @@ -0,0 +1,23 @@ +function lengthOfLongestSubstring(s) { + // Using the "sliding window" data structure. + // Create a javascript set to store unique characters. + let charSet = new Set(); + let left = 0; // Left pointer of the sliding window. + let maxLength = 0; + + // This moves the right pointer of the sliding window. + for (let right = 0; right < s.length; right++) { + // If the character at the right pointer is already in the set, move the left pointer. + while (charSet.has(s[right])) { + charSet.delete(s[left]); + left++; + } + // Add the current character at the right pointer to the set. + charSet.add(s[right]); + + // Update the maximum length of substring without repeating characters. + maxLength = Math.max(maxLength, right - left + 1); + } + + return maxLength; +} \ No newline at end of file diff --git a/javascript/_36.js b/javascript/_36.js new file mode 100644 index 0000000000..f3bafd88f8 --- /dev/null +++ b/javascript/_36.js @@ -0,0 +1,30 @@ +// Author: Phuong Lam + +/** + * @param {character[][]} board + * @return {boolean} + */ +var isValidSudoku = function (board) { + var valid = [[], [], []] + for (var i = 0; i < 9; i++) { + valid[0][i] = [] + for (var j = 0; j < 9; j++) { + if (board[i][j] !== '.') { + const val = board[i][j] + const pos = Math.floor(i / 3) * 3 + Math.floor(j / 3) + + if (!valid[1][j]) valid['1'][j] = [] + if (!valid[2][pos]) valid['2'][pos] = [] + + if (valid[0][i][val]) return false + if (valid[1][j][val]) return false + if (valid[2][pos][val]) return false + + valid[0][i][val] = 1 + valid[1][j][val] = 1 + valid[2][pos][val] = 1 + } + } + } + return true +} diff --git a/javascript/_485.js b/javascript/_485.js new file mode 100644 index 0000000000..653faa56f1 --- /dev/null +++ b/javascript/_485.js @@ -0,0 +1,19 @@ +var findMaxConsecutiveOnes = function (nums) { + let slow = 0 + let fast = 0 + let max = 0 + while (fast < nums.length) { + if (nums[fast] === 0) { + if (fast - slow > max) { + max = fast - slow + } + fast++ + slow = fast + } else { + fast++ + } + } + return fast - slow > max ? fast - slow : max +} + +console.log(findMaxConsecutiveOnes([2, 7, 11, 15])) diff --git a/javascript/_593.js b/javascript/_593.js new file mode 100644 index 0000000000..b3dd4db272 --- /dev/null +++ b/javascript/_593.js @@ -0,0 +1,42 @@ +/** + * @param {number[]} p1 + * @param {number[]} p2 + * @param {number[]} p3 + * @param {number[]} p4 + * @return {boolean} + */ + +// const d12 = distanceSquare(p1, p2) +// const d13 = distanceSquare(p1, p3) +// const d14 = distanceSquare(p1, p4) +// const d23 = distanceSquare(p2, p3) +// const d24 = distanceSquare(p2, p4) +// const d34 = distanceSquare(p3, p4) + +// if (d12 === 0 || d13 === 0 || d14 === 0) return false + +// // 1 2 +// // 3 4 +// if (d12 === d13 && d12 + d13 === d14 && d23 === d24 + d34) return true + +// // 1 2 +// // 4 3 +// if (d12 === d14 && d12 + d14 === d13 && d24 === d34 + d23) return true + +// // 1 3 +// // 4 2 +// if (d13 === d14 && d13 + d14 === d12 && d34 === d23 + d24) return true + +var validSquare = function (p1, p2, p3, p4) { + function distanceSquare(a, b) { + return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) + } + + const ds = [distanceSquare(p1, p2), distanceSquare(p1, p3), distanceSquare(p1, p4), distanceSquare(p2, p3), distanceSquare(p2, p4), distanceSquare(p3, p4)].sort((a, b) => a - b) + + for (var i = 0; i < 3; i++) if (ds[i] !== ds[i + 1] || ds[i] === 0) return false + + if (ds[4] === ds[5] && ds[4] === ds[0] * 2) return true + + return false +} diff --git a/javascript/_994.js b/javascript/_994.js new file mode 100644 index 0000000000..67ab649ddd --- /dev/null +++ b/javascript/_994.js @@ -0,0 +1,57 @@ +function orangesRotting(grid) { + const rows = grid.length; + const cols = grid[0].length; + let queue = []; + let freshOranges = 0; + + // Initialize the queue with all rotten oranges and count fresh oranges + for (let r = 0; r < rows; r++) { + for (let c = 0; c < cols; c++) { + if (grid[r][c] === 2) { + queue.push([r, c]); + } else if (grid[r][c] === 1) { + freshOranges++; + } + } + } + + // If there are no fresh oranges, return 0 + if (freshOranges === 0) return 0; + + let minutes = 0; + const directions = [ + [0, 1], // right + [1, 0], // down + [0, -1], // left + [-1, 0] // up + ]; + + // Step 2: Perform BFS + while (queue.length > 0) { + let size = queue.length; + let newRotten = false; + + for (let i = 0; i < size; i++) { + let [x, y] = queue.shift(); + + for (let [dx, dy] of directions) { + let nx = x + dx; + let ny = y + dy; + + // Check if the neighboring cell is a fresh orange + if (nx >= 0 && ny >= 0 && nx < rows && ny < cols && grid[nx][ny] === 1) { + grid[nx][ny] = 2; // Make it rotten + freshOranges--; // Decrease count of fresh oranges + queue.push([nx, ny]); // Add it to the queue + newRotten = true; + } + } + } + + // If rotten oranges exist, increment minutes + if (newRotten) minutes++; + } + + // Check if there are any fresh oranges left + return freshOranges === 0 ? minutes : -1; +} diff --git a/paginated_contents/algorithms/1st_thousand/README.md b/paginated_contents/algorithms/1st_thousand/README.md new file mode 100644 index 0000000000..2979301b84 --- /dev/null +++ b/paginated_contents/algorithms/1st_thousand/README.md @@ -0,0 +1,815 @@ +| # | Title | Solutions | Video | Difficulty | Tag +|-----|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|----------------------------------|---------------------------------------------------------------------- +| 999 | [Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_999.java) | | Easy | +| 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_991.java) | | Medium | Math, Greedy +| 981 | [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_981.java) | [:tv:](https://youtu.be/eVi4gDimCoo) | Medium | +| 997 | [Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_997.java) | | Easy | +| 994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_994.java) [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_994.js) | | Medium | BFS +| 993 | [Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_993.java) | | Easy | Tree, BFS +| 989 | [Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_989.java) | | Easy | Array +| 988 | [Smallest String Starting From Leaf](https://leetcode.com/problems/smallest-string-starting-from-leaf/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_988.java) | | Medium | Tree, DFS +| 987 | [Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_987.java) | | Medium | Recursion +| 986 | [Interval List Intersections](https://leetcode.com/problems/interval-list-intersections/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_986.java) | | Medium | Two Pointers +| 985 | [Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_985.java) | | Easy | Array +| 980 | [Unique Paths III](https://leetcode.com/problems/unique-paths-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_980.java) | | Hard | Backtracking, DFS +| 979 | [Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_979.java) | | Medium | Recursion +| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_977.java) | | Easy | Array +| 976 | [Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_976.java) | | Easy | Math Array +| 974 | [Subarray Sums Divisible by K](https://leetcode.com/problems/subarray-sums-divisible-by-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_974.java) | | Medium | Array | +| 973 | [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_973.java) | | Easy | Math Sort | +| 970 | [Powerful Integers](https://leetcode.com/problems/powerful-integers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_970.java) | | Easy | Math +| 966 | [Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_966.java) | | Medium | Hash Table, String +| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_965.java) | | Easy | DFS, recursion | +| 961 | [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_961.java) | | Easy | +| 958 | [Check Completeness of a Binary Tree](https://leetcode.com/problems/check-completeness-of-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_958.java) | | Medium | Tree +| 957 | [Prison Cells After N Days](https://leetcode.com/problems/prison-cells-after-n-days/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_957.java) | [:tv:](https://youtu.be/mQQp6I985bw) | Medium | +| 954 | [Array of Doubled Pairs](https://leetcode.com/problems/array-of-doubled-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_954.java) | [:tv:](https://youtu.be/Q0WKzdpR74o) | Medium | +| 953 | [Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_953.java) | | Easy | +| 951 | [Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_951.java) | | Medium | Tree, DFS, recursion | +| 950 | [Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_950.java) | | Medium | +| 946 | [Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_946.java) | | Medium | Stack +| 945 | [Minimum Increment to Make Array Unique](https://leetcode.com/problems/minimum-increment-to-make-array-unique/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_945.java) | | Medium | Greedy +| 944 | [Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_944.java) | | Easy | +| 942 | [DI String Match](https://leetcode.com/problems/di-string-match/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_942.java) | | Easy | +| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_941.java) | | Easy | +| 938 | [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_938.java) | | Medium | BST, recursion, DFS +| 937 | [Reorder Log Files](https://leetcode.com/problems/reorder-log-files/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_937.java) | | Easy | +| 936 | [Stamping The Sequence](https://leetcode.com/problems/stamping-the-sequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_936.java) | | Hard | String, Greedy +| 935 | [Knight Dialer](https://leetcode.com/problems/knight-dialer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_935.java) | | Medium | +| 934 | [Shortest Bridge](https://leetcode.com/problems/shortest-bridge/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_934.java) | | Medium |BFS +| 933 | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_933.java) | | Easy | +| 931 | [Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_931.java) | | Medium | Dynamic Programming +| 929 | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_929.java) | | Easy | +| 925 | [Long Pressed Name](https://leetcode.com/problems/long-pressed-name/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_925.java) | | Easy | +| 923 | [3Sum With Multiplicity](https://leetcode.com/problems/3sum-with-multiplicity/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_923.java) | | Medium | Two Pointers +| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_922.java) | | Easy | +| 921 | [Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_921.java) | | Medium | Stack, Greedy +| 919 | [Complete Binary Tree Inserter](https://leetcode.com/problems/complete-binary-tree-inserter/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_919.java) | | Medium | Tree, BFS +| 918 | [Maximum Sum Circular Subarray](https://leetcode.com/problems/maximum-sum-circular-subarray/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_918.java) | | Medium | Array, DP, Monotonic Queue +| 917 | [Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_917.java) | | Easy | +| 914 | [X of a Kind in a Deck of Cards](https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_914.java) | | Easy | +| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_912.java) | | Medium | Sort, MergeSort +| 908 | [Smallest Range I](https://leetcode.com/problems/smallest-range-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_908.java) | | Easy | +| 901 | [Online Stock Span](https://leetcode.com/problems/online-stock-span/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_901.java) | | Medium | Stack +| 904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_904.java) | [:tv:](https://youtu.be/GVecnelW8mA) | Medium | Two Pointers +| 900 | [RLE Iterator](https://leetcode.com/problems/rle-iterator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_900.java) | | Medium | +| 897 | [Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_897.java) | | Easy | DFS, recursion +| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_896.java) | | Easy | +| 895 | [Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_895.java) | HashTable, Stack | Hard | +| 893 | [Groups of Special-Equivalent Strings](https://leetcode.com/problems/groups-of-special-equivalent-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_893.java) | [:tv:](https://youtu.be/tbtXPKkA2Zw) | Easy | +| 892 | [Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_892.java) | Array, Math, Geometry, Matrix | Easy | +| 890 | [Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_890.java) | | Medium | +| 888 | [Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_888.java) | | Easy | +| 885 | [Spiral Matrix III](https://leetcode.com/problems/spiral-matrix-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_885.java) | [:tv:](https://www.youtube.com/watch?v=0qep3f9cqVs) | Medium | +| 884 | [Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_884.java) | | Easy | +| 883 | [Projection Area of 3D Shapes](https://leetcode.com/problems/projection-area-of-3d-shapes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_883.java) | | Easy | Math +| 881 | [Boats to Save People](https://leetcode.com/problems/boats-to-save-people/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_881.java) | | Medium | Two Pointers, Greedy +| 880 | [Decoded String at Index](https://leetcode.com/problems/decoded-string-at-index/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_880.java) | | Medium | Stack +| 877 | [Stone Game](https://leetcode.com/problems/stone-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_877.java) | | Medium | Math, DP, Minimax +| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_876.java) | | Easy | +| 875 | [Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_875.java) | | Medium | Binary Search +| 872 | [Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_872.java) | | Easy | DFS, recursion +| 870 | [Advantage Shuffle](https://leetcode.com/problems/advantage-shuffle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_870.java) | | Medium | Array, Greedy +| 868 | [Binary Gap](https://leetcode.com/problems/binary-gap/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_868.java) | | Easy | +| 867 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_867.java) | | Easy | +| 863 | [All Nodes Distance K in Binary Tree](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_863.java) | | Medium | BFS +| 861 | [Score After Flipping Matrix](https://leetcode.com/problems/score-after-flipping-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_861.java) | | Medium | Greedy +| 860 | [Lemonade Change](https://leetcode.com/problems/lemonade-change/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_860.java) | | Easy | +| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_859.java) | | Easy | +| 856 | [Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_856.java) | | Medium | Stack, String +| 852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_852.java) | | Easy | +| 851 | [Loud and Rich](https://leetcode.com/problems/loud-and-rich/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_851.java) | | Medium |Topological Sort +| 849 | [Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_849.java) | | Easy | +| 848 | [Shifting Letters](https://leetcode.com/problems/shifting-letters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_848.java) | | Medium | Array, String +| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_844.java) | | Easy | +| 841 | [Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_841.java) | | Easy | DFS, Graph +| 840 | [Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_840.java) | | Easy | +| 838 | [Push Dominoes](https://leetcode.com/problems/push-dominoes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_838.java) | [:tv:](https://youtu.be/0_XmKkgHSdI) | Medium | Two Pointers, DP +| 836 | [Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_836.java) | [:tv:](https://youtu.be/o6hHUk4DOW0) | Easy | +| 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_832.java) | | Easy | +| 830 | [Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_830.java) | | Easy | +| 826 | [Most Profit Assigning Work](https://leetcode.com/problems/most-profit-assigning-work/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_826.java) | | Medium |Binary Search, Greedy, Sorting +| 824 | [Goat Latin](https://leetcode.com/problems/goat-latin/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_824.java) | | Easy | +| 823 | [Binary Trees With Factors](https://leetcode.com/problems/binary-trees-with-factors/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_823.java) | | Medium | +| 821 | [Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_821.java) | | Easy | +| 820 | [Short Encoding of Words](https://leetcode.com/problems/short-encoding-of-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_820.java) | | Medium | +| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_819.java) | | Easy | HashMap +| 816 | [Ambiguous Coordinates](https://leetcode.com/problems/ambiguous-coordinates/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_816.java) | | Medium | String +| 814 | [Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_814.java) | | Medium | recursion, DFS +| 812 | [Largest Triangle Area](https://leetcode.com/problems/largest-triangle-area/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_812.java) | | Easy | Array, Math, Geometry +| 811 | [Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_811.java) | | Easy | HashMap +| 809 | [Expressive Words](https://leetcode.com/problems/expressive-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_809.java) | | Medium | +| 807 | [Max Increase to Keep City Skyline](https://leetcode.com/problems/max-increase-to-keep-city-skyline/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_807.java) | | Medium | +| 806 | [Number of Lines To Write String](https://leetcode.com/problems/number-of-lines-to-write-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_806.java) | | Easy | +| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_804.java) | | Easy | +| 802 | [Find Eventual Safe States](https://leetcode.com/problems/find-eventual-safe-states/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_802.java) | | Medium | Topological Sort +| 800 | [Similar RGB Color](https://leetcode.com/problems/similar-rgb-color/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_800.java) | | Easy | +| 799 | [Champagne Tower](https://leetcode.com/problems/champagne-tower/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_799.java) | | Medium | +| 796 | [Rotate String](https://leetcode.com/problems/rotate-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_796.java) | | Easy | +| 792 | [Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_792.java) | | Medium | HashTable, String, Trie, Sorting +| 791 | [Custom Sort String](https://leetcode.com/problems/custom-sort-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_791.java) | | Medium | +| 789 | [Escape The Ghosts](https://leetcode.com/problems/escape-the-ghosts/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_789.java) | | Medium | Math | +| 788 | [Rotated Digits](https://leetcode.com/problems/rotated-digits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_788.java) | | Easy | +| 785 | [Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_785.java) | | Medium | +| 784 | [Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_784.java) | | Easy | +| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_783.java) | | Easy | +| 781 | [Rabbits in Forest](https://leetcode.com/problems/rabbits-in-forest/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_781.java) | [:tv:](https://youtu.be/leiSa1i-QrI) | Medium | HashTable, Math +| 779 | [K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_779.java) | | Medium | +| 777 | [Swap Adjacent in LR String](https://leetcode.com/problems/swap-adjacent-in-lr-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_777.java) | | Medium | Two Pointers, String +| 776 | [Split BST](https://leetcode.com/problems/split-bst/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_776.java) | | Medium | Recursion +| 775 | [Global and Local Inversions](https://leetcode.com/problems/global-and-local-inversions/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_775.java) | | Medium | Array, Math +| 773 | [Sliding Puzzle](https://leetcode.com/problems/sliding-puzzle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_773.java) | | Hard | BFS +| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_771.java) | | Easy | +| 769 | [Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_769.java) | | Medium | Array +| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_767.java) | | Medium | +| 766 | [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_766.java) | | Easy | +| 765 | [Couples Holding Hands](https://leetcode.com/problems/couples-holding-hands/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_765.java) | | Hard | +| 764 | [Largest Plus Sign](https://leetcode.com/problems/largest-plus-sign/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_764.java) | | Medium | DP +| 763 | [Partition Labels](https://leetcode.com/problems/partition-labels/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_763.java) | | Medium | +| 762 | [Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_762.java) | | Easy | +| 760 | [Find Anagram Mappings](https://leetcode.com/problems/find-anagram-mappings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_760.java) | | Easy | +| 758 | [Bold Words in String](https://leetcode.com/problems/bold-words-in-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_758.java) | | Easy | +| 757 | [Set Intersection Size At Least Two](https://leetcode.com/problems/set-intersection-size-at-least-two/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_757.java) | | Hard | +| 756 | [Pyramid Transition Matrix](https://leetcode.com/problems/pyramid-transition-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_756.java) | | Medium | Backtracking +| 755 | [Pour Water](https://leetcode.com/problems/pour-water/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_755.java) || Medium | Array +| 754 | [Reach a Number](https://leetcode.com/problems/reach-a-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_754.java) || Medium | Math +| 752 | [Open the Lock](https://leetcode.com/problems/open-the-lock/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_752.java) || Medium | BFS +| 750 | [Number Of Corner Rectangles](https://leetcode.com/problems/number-of-corner-rectangles/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_750.java) | | Medium | +| 748 | [Shortest Completing Word](https://leetcode.com/problems/shortest-completing-word/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_748.java) | | Easy | +| 747 | [Largest Number Greater Than Twice of Others](https://leetcode.com/problems/largest-number-greater-than-twice-of-others/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_747.java) | | Easy | +| 746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_746.java) | | Easy | +| 744 | [Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_744.java) | | Easy | +| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_743.java) || Medium | Graph, Djikstra | +| 742 | [Closest Leaf in a Binary Tree](https://leetcode.com/problems/closest-leaf-in-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_742.java) | | Medium | Tree +| 740 | [Delete and Earn](https://leetcode.com/problems/delete-and-earn/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_740.java) | | Medium | +| 739 | [Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_739.java) | | Medium | +| 738 | [Monotone Increasing Digits](https://leetcode.com/problems/monotone-increasing-digits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_738.java) | | Medium | +| 737 | [Sentence Similarity II](https://leetcode.com/problems/sentence-similarity-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_737.java) | | Medium | Union Find +| 735 | [Asteroid Collision](https://leetcode.com/problems/asteroid-collision/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_735.java) | | Medium | Stack +| 734 | [Sentence Similarity](https://leetcode.com/problems/sentence-similarity/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_734.java) | | Easy | HashTable +| 733 | [Flood Fill](https://leetcode.com/problem**__**s/flood-fill/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_733.java) | | Easy | BFS, DFS +| 729 | [My Calendar I](https://leetcode.com/problems/my-calendar-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_729.java) || Medium | +| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_728.java) | | Easy | +| 727 | [Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_727.java) | | Hard | DP +| 726 | [Number of Atoms](https://leetcode.com/problems/number-of-atoms/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_726.java) | | Hard | Stack +| 725 | [Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_725.java) | | Medium | LinkedList +| 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_724.java) | | Easy | Array +| 723 | [Candy Crush](https://leetcode.com/problems/candy-crush/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_723.java) | | Medium | Array, Two Pointers +| 722 | [Remove Comments](https://leetcode.com/problems/remove-comments/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_722.java) | | Medium | Array, String +| 721 | [Accounts Merge](https://leetcode.com/problems/accounts-merge/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_721.java) | | Medium | DFS, Union Find +| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_720.java) | | Easy | Trie +| 719 | [Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_719.java) | | Hard | Binary Search +| 718 | [Maximum Length of Repeated Subarray](https://leetcode.com/problems/maximum-length-of-repeated-subarray/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_718.java) | | Medium | DP +| 717 | [1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_717.java) | | Easy | +| 716 | [Max Stack](https://leetcode.com/problems/max-stack/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_716.java) | | Hard | Design +| 714 | [Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_714.java) | | Medium | DP +| 713 | [Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_713.java) || Medium | Sliding Window, Two Pointers +| 712 | [Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_712.java) | | Medium | DP +| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_709.java) | | Easy | String +| 708 | [Insert into a Sorted Circular Linked List](https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_708.java) | | Medium | Linked List +| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_706.java) | | Easy | Design +| 705 | [Design HashSet](https://leetcode.com/problems/design-hashset/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_705.java) | | Easy | Design +| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_704.java) | [:tv:](https://youtu.be/eHVe_uyXeWg) | Easy | Binary Search +| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_703.java) | | Easy | PriorityQueue, Design +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_701.java) | | Medium | DFS, recursion +| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_700.java) | | Easy | recusion, dfs +| 699 | [Falling Squares](https://leetcode.com/problems/falling-squares/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_699.java) || Hard | Segment Tree +| 698 | [Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_698.java), [C++](../master/cpp/_698.cpp) | | Medium | Backtracking + DP +| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_697.java) || Easy | +| 696 | [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_696.java) | | Easy | +| 695 | [Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_695.java) | | Medium | DFS, FBS, Union Find, Matrix +| 694 | [Number of Distinct Islands](https://leetcode.com/problems/number-of-distinct-islands/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_694.java) | | Medium | DFS +| 693 | [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_693.java) | | Easy | +| 692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_692.java) | | Medium | +| 691 | [Stickers to Spell Word](https://leetcode.com/problems/stickers-to-spell-word/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_691.java) | | Hard | DP +| 690 | [Employee Importance](https://leetcode.com/problems/employee-importance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_690.java) | | Easy | DFS +| 689 | [Maximum Sum of 3 Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_689.java) | | Hard | DP +| 688 | [Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_688.java) | | Medium | DP +| 687 | [Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_687.java) | | Easy | DFS +| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_686.java) | | Easy | +| 685 | [Redundant Connection II](https://leetcode.com/problems/redundant-connection-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_685.java) | | Hard | Union Find +| 684 | [Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_684.java) | | Medium | Union Find, DFS +| 683 | [K Empty Slots](https://leetcode.com/problems/k-empty-slots/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_683.java) | | Hard | +| 682 | [Baseball Game](https://leetcode.com/problems/baseball-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_682.java) | | Easy | +| 681 | [Next Closest Time](https://leetcode.com/problems/parents-closest-time/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_681.java) | | Medium | +| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_680.java) | | Easy | String +| 679 | [24 Game](https://leetcode.com/problems/24-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_679.java) | | Hard | Recursion +| 678 | [Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_678.java) | | Medium | Recursion, Greedy +| 677 | [Map Sum Pairs](https://leetcode.com/problems/map-sum-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_677.java) | | Medium | HashMap, Trie +| 676 | [Implement Magic Dictionary](https://leetcode.com/problems/implement-magic-dictionary/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_676.java) | | Medium | +| 675 | [Cut Off Trees for Golf Event](https://leetcode.com/problems/cut-off-trees-for-golf-event/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_675.java) | | Hard | BFS +| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_674.java) | | Easy | +| 673 | [Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_673.java) | | Medium | DP +| 672 | [Bulb Switcher II](https://leetcode.com/problems/bulb-switcher-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_672.java) | | Medium | Math +| 671 | [Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_671.java) | | Easy | Tree, DFS +| 670 | [Maximum Swap](https://leetcode.com/problems/maximum-swap/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_670.java) | | Medium | String +| 669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_669.java) | | Easy | Tree, DFS +| 668 | [Kth Smallest Number in Multiplication Table](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_668.java) | | Hard | Binary Search +| 667 | [Beautiful Arrangement II](https://leetcode.com/problems/beautiful-arrangement-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_667.java) | | Medium | Array +| 666 | [Path Sum IV](https://leetcode.com/problems/path-sum-iv/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_666.java) | | Medium | Tree, DFS +| 665 | [Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_665.java) | | Easy | +| 664 | [Strange Printer](https://leetcode.com/problems/strange-printer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_664.java) | | Hard | DP +| 663 | [Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_663.java) | | Medium | Tree +| 662 | [Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_662.java) | | Medium | BFS, DFS +| 661 | [Image Smoother](https://leetcode.com/problems/image-smoother/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_661.java) | | Easy | Array +| 660 | [Remove 9](https://leetcode.com/problems/remove-9/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_660.java) | | Hard | Math +| 659 | [Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_659.java) | | Medium | HashMap +| 658 | [Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_658.java) | | Medium | +| 657 | [Judge Route Circle](https://leetcode.com/problems/judge-route-circle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_657.java) | | Easy | +| 656 | [Coin Path](https://leetcode.com/problems/coin-path/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_656.java) | | Hard | DP +| 655 | [Print Binary Tree](https://leetcode.com/problems/print-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_655.java) | | Medium | Recursion +| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_654.java) | | Medium | Tree +| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_653.java) | | Easy | Tree +| 652 | [Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_652.java) | | Medium | Tree +| 651 | [4 Keys Keyboard](https://leetcode.com/problems/4-keys-keyboard/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_651.java) | | Medium | DP +| 650 | [2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_650.java) | | Medium | DP +| 649 | [Dota2 Senate](https://leetcode.com/problems/dota2-senate/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_649.java) | | Medium | Greedy +| 648 | [Replace Words](https://leetcode.com/problems/replace-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_648.java) | | Medium | Trie +| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_647.java) | | Medium | DP +| 646 | [Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_646.java) | | Medium | DP, Greedy, Array, Sorting +| 645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_645.java) | | Easy | +| 644 | [Maximum Average Subarray II](https://leetcode.com/problems/maximum-average-subarray-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_644.java) | | Hard | Binary Search +| 643 | [Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_643.java) | | Easy | +| 642 | [Design Search Autocomplete System](https://leetcode.com/problems/design-search-autocomplete-system/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_642.java) | | Hard | Design +| 640 | [Solve the Equation](https://leetcode.com/problems/solve-the-equation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_640.java) | | Medium | +| 639 | [Decode Ways II](https://leetcode.com/problems/decode-ways-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_639.java) | | Hard | DP +| 638 | [Shopping Offers](https://leetcode.com/problems/shopping-offers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_638.java) | | Medium | DP, DFS +| 637 | [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_637.java) | | Easy | +| 636 | [Exclusive Time of Functions](https://leetcode.com/problems/exclusive-time-of-functions/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_636.java) | | Medium | Stack +| 635 | [Design Log Storage System](https://leetcode.com/problems/design-log-storage-system/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_635.java) | | Medium | Design +| 634 | [Find the Derangement of An Array](https://leetcode.com/problems/find-the-derangement-of-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_634.java) | | Medium | Math +| 633 | [Sum of Square Numbers](https://leetcode.com/problems/sum-of-square-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_633.java) | | Easy | Binary Search +| 632 | [Smallest Range](https://leetcode.com/problems/smallest-range/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_632.java) | | Hard | Heap +| 631 | [Design Excel Sum Formula](https://leetcode.com/problems/design-excel-sum-formula/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_631.java) | | Hard | Design, Topological Sort +| 630 | [Course Schedule III](https://leetcode.com/problems/course-schedule-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_630.java) | | Hard | Heap, Greedy +| 629 | [K Inverse Pairs Array](https://leetcode.com/problems/k-inverse-pairs-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_629.java) | | Hard | DP +| 628 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_628.java) | | Easy | +| 625 | [Minimum Factorization](https://leetcode.com/problems/minimum-factorization/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_625.java) | | Medium | +| 624 | [Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_624.java) | | Easy | Sort, Array +| 623 | [Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_623.java) | | Medium | Tree +| 622 | [Design Circular Queue](https://leetcode.com/problems/design-circular-queue/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_622.java) | | Medium | Design, Queue +| 621 | [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_621.java) | | Medium | Greedy, Queue +| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_617.java) | | Easy | Tree, Recursion +| 616 | [Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_616.java) | | Medium | String +| 611 | [Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_611.java) | | Medium | Binary Search +| 609 | [Find Duplicate File in System](https://leetcode.com/problems/find-duplicate-file-in-system/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_609.java) | | Medium | HashMap +| 606 | [Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_606.java) | | Easy | Tree, Recursion +| 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_605.java) | | Easy | Array +| 604 | [Design Compressed String Iterator](https://leetcode.com/problems/design-compressed-string-iterator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_604.java) | | Easy | Design, String +| 600 | [Non-negative Integers without Consecutive Ones](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_600.java) | | Hard | Bit Manipulation, DP +| 599 | [Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_599.java) | | Easy | HashMap +| 598 | [Range Addition II](https://leetcode.com/problems/range-addition-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_598.java) | | Easy | +| 594 | [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_594.java) | | Easy | Array, HashMap +| 593 | [Valid Square](https://leetcode.com/problems/valid-square/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_593.java), [Javascript](./javascript/_593.js) | | Medium | Math +| 592 | [Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_592.java) | | Medium | Math +| 591 | [Tag Validator](https://leetcode.com/problems/tag-validator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_591.java) | | Hard | Stack, String +| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_590.java) | | Easy | DFS, recursion +| 589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_589.java) | | Easy | DFS, recursion +| 588 | [Design In-Memory File System](https://leetcode.com/problems/design-in-memory-file-system/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_588.java) | | Hard | Trie, Design +| 587 | [Erect the Fence](https://leetcode.com/problems/erect-the-fence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_587.java) | | Hard | Geometry +| 583 | [Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_583.java) | | Medium | DP +| 582 | [Kill Process](https://leetcode.com/problems/kill-process/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_582.java) | | Medium | Stack +| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_581.java) | | Easy | Array, Sort +| 576 | [Out of Boundary Paths](https://leetcode.com/problems/out-of-boundary-paths/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_576.java) | | Hard | DP, DFS +| 575 | [Distribute Candies](https://leetcode.com/problems/distribute-candies/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_575.java) | | Easy | Array +| 573 | [Squirrel Simulation](https://leetcode.com/problems/squirrel-simulation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_573.java) | | Medium | Math +| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_572.java) | | Easy | Tree +| 568 | [Maximum Vacation Days](https://leetcode.com/problems/maximum-vacation-days/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_568.java) | | Hard | DP +| 567 | [Permutation in String](https://leetcode.com/problems/permutation-in-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_567.java) | | Medium | Sliding Windows, Two Pointers +| 566 | [Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_566.java) | | Easy | +| 565 | [Array Nesting](https://leetcode.com/problems/array-nesting/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_565.java) | | Medium | +| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_563.java) | | Easy | Tree Recursion +| 562 | [Longest Line of Consecutive One in Matrix](https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_562.java) | | Medium | Matrix DP +| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_561.java) | | Easy | Array +| 560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_560.java) || Medium | Array, HashMap +| 559 | [Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_559.java) | | Easy | DFS, recursion +| 557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_557.java) | | Easy | String +| 556 | [Next Greater Element III](https://leetcode.com/problems/parents-greater-element-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_556.java) | | Medium | String +| 555 | [Split Concatenated Strings](https://leetcode.com/problems/split-concatenated-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_555.java) | | Medium | String +| 554 | [Brick Wall](https://leetcode.com/problems/brick-wall/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_554.java) | | Medium | HashMap +| 553 | [Optimal Division](https://leetcode.com/problems/optimal-division/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_553.java) | | Medium | String, Math +| 552 | [Student Attendance Record II](https://leetcode.com/problems/student-attendance-record-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_552.java) | | Hard | DP +| 551 | [Student Attendance Record I](https://leetcode.com/problems/student-attendance-record-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_551.java) | | Easy | String +| 549 | [Binary Tree Longest Consecutive Sequence II](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_549.java) | | Medium | Tree +| 548 | [Split Array with Equal Sum](https://leetcode.com/problems/split-array-with-equal-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_548.java) | | Medium | Array +| 547 | [Friend Circles](https://leetcode.com/problems/friend-circles/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_547.java) | | Medium | Union Find +| 546 | [Remove Boxes](https://leetcode.com/problems/remove-boxes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_546.java) | | Hard | DFS, DP +| 545 | [Boundary of Binary Tree](https://leetcode.com/problems/boundary-of-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_545.java) | | Medium | Recursion +| 544 | [Output Contest Matches](https://leetcode.com/problems/output-a824-matches/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_544.java) | | Medium | Recursion +| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_543.java) | | Easy | Tree/DFS/Recursion +| 542 | [01 Matrix](https://leetcode.com/problems/01-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_542.java) | | Medium | BFS +| 541 | [Reverse String II](https://leetcode.com/problems/reverse-string-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_541.java) | | Easy | String +| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_540.java) | | Medium | Array, Binary Search +| 539 | [Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_539.java) | | Medium | String +| 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_538.java) | | Easy | Tree +| 537 | [Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_537.java) | | Medium | Math, String +| 536 | [Construct Binary Tree from String](https://leetcode.com/problems/construct-binary-tree-from-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_536.java) | | Medium | Recursion, Stack +| 535 | [Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_535.java) | | Medium | Design +| 533 | [Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_533.java) | | Medium | HashMap +| 532 | [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_532.java) | | Easy | HashMap +| 531 | [Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_531.java) | | Medium | +| 530 | [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_530.java) | | Easy | DFS +| 529 | [Minesweeper](https://leetcode.com/problems/minesweeper/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_529.java) | | Medium | BFS +| 528 | [Random Pick with Weight](https://leetcode.com/problems/random-pick-with-weight/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_528.java) | | Medium | Math, Binary Search, Prefix Sum, Randomized +| 527 | [Word Abbreviation](https://leetcode.com/problems/word-abbreviation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_527.java) | | Hard | +| 526 | [Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_526.java) | | Medium | Backtracking +| 525 | [Contiguous Array](https://leetcode.com/problems/contiguous-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_525.java) | | Medium | HashMap +| 524 | [Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_524.java) | | Medium | Sort +| 523 | [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_523.java) | | Medium | DP +| 522 | [Longest Uncommon Subsequence II](https://leetcode.com/problems/longest-uncommon-subsequence-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_522.java) | | Medium | +| 521 | [Longest Uncommon Subsequence I](https://leetcode.com/problems/longest-uncommon-subsequence-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_521.java) | | Easy | +| 520 | [Detect Capital](https://leetcode.com/problems/detect-capital/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_520.java) | | Easy | +| 519 | [Random Flip Matrix](https://leetcode.com/problems/random-flip-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_519.java) | | Medium |HashTable, Math, Randomized, Reservoir Sampling +| 518 | [Coin Change 2](https://leetcode.com/problems/coin-change-2/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_518.java) | | Medium | Array, DP +| 517 | [Super Washing Machines](https://leetcode.com/problems/super-washing-machines/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_517.java) | | Hard | DP +| 516 | [Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_516.java) | | Medium | DP +| 515 | [Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_515.java) | | Medium | BFS +| 514 | [Freedom Trail](https://leetcode.com/problems/freedom-trail/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_514.java) | | Hard | DP +| 513 | [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_513.java) || Medium | BFS +| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_509.java) | [:tv:](https://www.youtube.com/watch?v=WPBTYmvcHXs) | Easy | Array +| 508 | [Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_508.java) || Medium | DFS, Tree +| 507 | [Perfect Number](https://leetcode.com/problems/perfect-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_507.java) | | Easy | Math +| 506 | [Relative Ranks](https://leetcode.com/problems/relative-ranks/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_506.java) | | Easy | +| 505 | [The Maze II](https://leetcode.com/problems/the-maze-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_505.java) | | Medium | BFS +| 504 | [Base 7](https://leetcode.com/problems/base-7/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_504.java) | | Easy | +| 503 | [Next Greater Element II](https://leetcode.com/problems/parents-greater-element-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_503.java) | | Medium | Stack +| 502 | [IPO](https://leetcode.com/problems/ipo/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_502.java) | | Hard | Heap, Greedy +| 501 | [Find Mode in Binary Tree](https://leetcode.com/problems/find-mode-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_501.java) | | Easy | Binary Tree +| 500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_500.java) | | Easy | +| 499 | [The Maze III](https://leetcode.com/problems/the-maze-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_499.java) | | Hard | BFS +| 496 | [Next Greater Element I](https://leetcode.com/problems/parents-greater-element-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_496.java) | | Easy | +| 498 | [Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_498.java) | | Medium | +| 495 | [Teemo Attacking](https://leetcode.com/problems/teemo-attacking/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_495.java) | | Medium | Array +| 494 | [Target Sum](https://leetcode.com/problems/target-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_494.java) | | Medium | +| 493 | [Reverse Pairs](https://leetcode.com/problems/reverse-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_493.java) | | Hard | Recursion +| 492 | [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_492.java) | | Easy | Array +| 491 | [Increasing Subsequences](https://leetcode.com/problems/increasing-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_491.java) | | Medium | Backtracking, DFS +| 490 | [The Maze](https://leetcode.com/problems/the-maze/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_490.java) | | Medium | BFS +| 488 | [Zuma Game](https://leetcode.com/problems/zuma-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_488.java) | | Hard | DFS, Backtracking +| 487 | [Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_487.java) | [:tv:](https://youtu.be/nKhteIRZ2Ok) | Medium | Array, Sliding Window +| 486 | [Predict the Winner](https://leetcode.com/problems/predict-the-winner/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_486.java) | | Medium | DP +| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_485.java) [Javascript](../master/javascript/_485.js) | [:tv:](https://youtu.be/nKhteIRZ2Ok) | Easy | Array +| 484 | [Find Permutation](https://leetcode.com/problems/find-permutation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_484.java) | | Medium | Array, String, Greedy +| 483 | [Smallest Good Base](https://leetcode.com/problems/smallest-good-base/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_483.java) | | Hard | Binary Search, Math +| 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_482.java) | | Medium | +| 481 | [Magical String](https://leetcode.com/problems/magical-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_481.java) || Medium | +| 480 | [Sliding Window Median](https://leetcode.com/problems/sliding-window-median/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_480.java) | | Hard | Heap +| 479 | [Largest Palindrome Product](https://leetcode.com/problems/largest-palindrome-product/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_479.java) | | Easy | +| 477 | [Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_477.java) | | Medium | Bit Manipulation +| 478 | [Generate Random Point in a Circle](https://leetcode.com/problems/generate-random-point-in-a-circle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_478.java) | | Medium | Math, Random, Rejection Sampling +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_476.java) | | Easy | Bit Manipulation +| 475 | [Heaters](https://leetcode.com/problems/heaters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_475.java) | | Easy | Array Binary Search +| 474 | [Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_474.java) | | Medium | DP +| 473 | [Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_473.java) | | Medium | Backtracking, DFS +| 472 | [Concatenated Words](https://leetcode.com/problems/concatenated-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_472.java) | | Hard | Trie, DP, DFS +| 471 | [Encode String with Shortest Length](https://leetcode.com/problems/encode-string-with-shortest-length/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_471.java) | | Hard | DP +| 469 | [Convex Polygon](https://leetcode.com/problems/convex-polygon/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_469.java) | | Medium | Math +| 468 | [Validate IP Address](https://leetcode.com/problems/validate-ip-address/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_468.java) | | Medium | String +| 467 | [Unique Substrings in Wraparound String](https://leetcode.com/problems/unique-substrings-in-wraparound-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_467.java) || Medium | DP +| 466 | [Count The Repetitions](https://leetcode.com/problems/count-the-repetitions/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_466.java) | | Hard | DP +| 465 | [Optimal Account Balancing](https://leetcode.com/problems/optimal-account-balancing/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_465.java) | | Hard | DP +| 464 | [Can I Win](https://leetcode.com/problems/can-i-win/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_464.java) | | Medium | DP +| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_463.java) | | Easy | +| 462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_462.java) || Medium | +| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_461.java), [C++](../master/cpp/_461.cpp) | | Easy | +| 460 | [LFU Cache](https://leetcode.com/problems/lfu-cache/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_460.java) | | Hard | Design, LinkedHashMap, HashMap +| 459 | [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_459.java) | | Easy | String, KMP +| 458 | [Poor Pigs](https://leetcode.com/problems/poor-pigs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_458.java) | | Easy | Math +| 457 | [Circular Array Loop](https://leetcode.com/problems/circular-array-loop/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_457.java) | | Medium | +| 456 | [132 Pattern](https://leetcode.com/problems/132-pattern/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_456.java) | | Medium | Stack +| 455 | [Assign Cookies](https://leetcode.com/problems/assign-cookies/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_455.java) | | Easy | +| 454 | [4Sum II](https://leetcode.com/problems/4sum-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_454.java) | | Medium | HashMap +| 453 | [Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_453.java) | | Easy | +| 452 | [Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_452.java) | | Medium | Array, Greedy +| 451 | [Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_451.java) | | Medium | HashMap +| 450 | [Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_450.java) | | Medium | Tree, Recursion +| 449 | [Serialize and Deserialize BST](https://leetcode.com/problems/serialize-and-deserialize-bst/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_449.java) | | Medium | BFS +| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_448.java) | | Easy | Array, HashMap +| 447 | [Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_447.java) | | Easy | HashMap +| 446 | [Arithmetic Slices II - Subsequence](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_446.java) | | Hard | DP +| 445 | [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_445.java) | | Medium | Stack, LinkedList +| 444 | [Sequence Reconstruction](https://leetcode.com/problems/sequence-reconstruction/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_444.java) | | Medium | Topological Sort, Graph +| 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_443.java) | | Easy | +| 442 | [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_442.java) | | Medium | Array +| 441 | [Arranging Coins](https://leetcode.com/problems/arrange-coins/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_441.java) | | Easy | +| 440 | [K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_440.java) | | Hard | +| 439 | [Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_439.java) | | Medium | Stack +| 438 | [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_438.java) | | Easy | Sliding Window +| 437 | [Path Sum III](https://leetcode.com/problems/path-sum-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_437.java) | | Easy | DFS, recursion +| 436 | [Find Right Interval](https://leetcode.com/problems/find-right-interval/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_436.java) | | Medium | Binary Search +| 435 | [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_435.java) | | Medium | Greedy +| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_434.java) | | Easy | +| 433 | [Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_433.java) | | Medium | BFS +| 432 | [All O`one Data Structure](https://leetcode.com/problems/all-oone-data-structure/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_432.java) | | Hard | Design +| 430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_430.java) | | Medium | LinkedList, DFS, Doubly-Linked List +| 429 | [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_429.java) | | Easy | BFS, Tree +| 427 | [Construct Quad Tree](https://leetcode.com/problems/construct-quad-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_427.java) | | Medium | Recursion +| 426 | [Convert Binary Search Tree to Sorted Doubly Linked List](https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_426.java) | | Medium | DFS, BST, Recursion +| 425 | [Word Squares](https://leetcode.com/problems/word-squares/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_425.java) | | Hard | Trie, Backtracking, Recursion +| 424 | [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_424.java) | | Medium | Sliding Window +| 423 | [Reconstruct Original Digits from English](https://leetcode.com/problems/reconstruct-original-digits-from-english/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_423.java) | | Medium | Math +| 422 | [Valid Word Square](https://leetcode.com/problems/valid-word-square/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_422.java) | | Easy | +| 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_421.java) | | Medium | Bit Manipulation, Trie +| 420 | [Strong Password Checker](https://leetcode.com/problems/strong-password-checker/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_420.java) | | Hard | +| 419 | [Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_419.java) | | Medium | DFS +| 418 | [Sentence Screen Fitting](https://leetcode.com/problems/sentence-screen-fitting/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_418.java) | | Medium | +| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_417.java) | | Medium | DFS +| 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_416.java), [C++](../master/cpp/_416.cpp) | | Medium | DP +| 415 | [Add Strings](https://leetcode.com/problems/add-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_415.java) | | Easy | +| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_414.java) | | Easy | +| 413 | [Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_413.java) | | Medium | DP +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_412.java) | | Easy | +| 411 | [Minimum Unique Word Abbreviation](https://leetcode.com/problems/minimum-unique-word-abbreviation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_411.java) | | Hard | NP-Hard, Backtracking, Trie, Recursion +| 410 | [Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_410.java) | | Hard | Binary Search, DP +| 409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_409.java) | | Easy | +| 408 | [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_408.java) | | Easy | +| 407 | [Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_407.java) | | Hard | Heap +| 406 | [Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_406.java) | | Medium | LinkedList, PriorityQueue +| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_405.java) | | Easy | +| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_404.java) | | Easy | +| 403 | [Frog Jump](https://leetcode.com/problems/frog-jump/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_403.java) | | Hard | DP +| 402 | [Remove K Digits](https://leetcode.com/problems/remove-k-digits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_402.java) | | Medium | Greedy, Stack +| 401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_401.java) | | Easy | +| 400 | [Nth Digit](https://leetcode.com/problems/nth-digit/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_400.java) | | Easy | +| 399 | [Evaluate Division](https://leetcode.com/problems/evaluate-division/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_399.java) | | Medium | Graph, DFS, Backtracking +| 398 | [Random Pick Index](https://leetcode.com/problems/random-pick-index/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_398.java) | | Medium | Reservoir Sampling +| 397 | [Integer Replacement](https://leetcode.com/problems/integer-replacement/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_397.java) | | Easy | BFS +| 396 | [Rotate Function](https://leetcode.com/problems/rotate-function/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_396.java) | | Easy | +| 395 | [Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_395.java) | | Medium | Recursion +| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_394.java) | | Medium | Stack Depth-first-search +| 393 | [UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_393.java) | | Medium | Bit Manipulation +| 392 | [Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_392.java) | | Medium | Array, String +| 391 | [Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_391.java) | | Hard | +| 390 | [Elimination Game](https://leetcode.com/problems/elimination-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_390.java) | | Medium | +| 389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_389.java) | || Easy | +| 388 | [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_388.java) | | Medium | Stack +| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_387.java) | | Easy | HashMap +| 386 | [Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_386.java) | | Medium | +| 385 | [Mini Parser](https://leetcode.com/problems/mini-parser/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_385.java) | | Medium | Stack +| 384 | [Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_384.java) | | Medium | +| 383 | [Ransom Note](https://leetcode.com/problems/ransom-note/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_383.java) | | Easy | String +| 382 | [Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_382.java) | | Medium | Reservoir Sampling +| 381 | [Insert Delete GetRandom O(1) - Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_381.java) || Hard | Design, Randomized, HashTable +| 380 | [Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_380.java) | | Medium | Design, HashMap +| 379 | [Design Phone Directory](https://leetcode.com/problems/design-phone-directory/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_379.java) | | Medium | +| 378 | [Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_378.java) | | Medium | Binary Search +| 377 | [Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_377.java) | | Medium | DP +| 376 | [Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_376.java) | | Medium | DP, Greedy +| 375 | [Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_375.java) | | Medium | DP +| 374 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_374.java) | | Easy | Binary Search +| 373 | [Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_373.java) | | Medium | Heap +| 372 | [Super Pow](https://leetcode.com/problems/super-pow/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_372.java) | | Medium | Math +| 371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_371.java) | | Easy | +| 370 | [Range Addition](https://leetcode.com/problems/range-addition/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_370.java) | | Medium | Array +| 369 | [Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_369.java) | | Medium | Linked List +| 368 | [Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_368.java) | | Medium | DP +| 367 | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_367.java) | | Medium | +| 366 | [Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_366.java) | | Medium | DFS +| 365 | [Water and Jug Problem](https://leetcode.com/problems/water-and-jug-problem/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_365.java) | | Medium | Math +| 364 | [Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_364.java) | | Medium | DFS +| 363 | [Max Sum of Rectangle No Larger Than K](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_363.java) | | Hard | DP +| 362 | [Design Hit Counter](https://leetcode.com/problems/design-hit-counter/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_362.java) | | Medium | Design +| 361 | [Bomb Enemy](https://leetcode.com/problems/bomb-enemy/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_361.java) | | Medium | +| 360 | [Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_360.java) | | Medium | Two Pointers, Math +| 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_359.java) | | Easy | HashMap +| 358 | [Rearrange String k Distance Apart](https://leetcode.com/problems/rearrange-string-k-distance-apart/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_358.java) | | Hard | HashMap, Heap, Greedy +| 357 | [Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_357.java) | | Medium | DP, Math +| 356 | [Line Reflection](https://leetcode.com/problems/line-reflection/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_356.java) | | Medium | HashSet +| 355 | [Design Twitter](https://leetcode.com/problems/design-twitter/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_355.java) | | Medium | Design, HashMap, Heap +| 354 | [Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_354.java) | | Hard | DP, Binary Search +| 353 | [Design Snake Game](https://leetcode.com/problems/design-snake-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_353.java) | | Medium | +| 352 | [Data Stream as Disjoint Intervals](https://leetcode.com/problems/data-stream-as-disjoint-intervals/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_352.java) | | Hard | TreeMap +| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_351.java) | | Medium | +| 350 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_350.java) | [:tv:](https://youtu.be/lKuK69-hMcc) | Easy | HashMap, Binary Search +| 349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_349.java) | [:tv:](https://youtu.be/XxStWmfXJRs) | Easy | Two Pointers, Binary Search +| 348 | [Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_348.java) | | Medium | Design +| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_347.java) | | Medium | HashTable, Heap, Bucket Sort +| 346 | [Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_346.java) | | Easy | Queue +| 345 | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_345.java) | | Easy | String +| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_344.java) | [:tv:](https://youtu.be/P68JPXtFyYg) | Easy | String +| 343 | [Integer Break](https://leetcode.com/problems/integer-break/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_343.java) | | Medium | Math +| 342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_342.java) | | Easy | Math +| 341 | [Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_341.java) | | Medium | Stack +| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_340.java) | | Hard | Sliding Window +| 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_339.java) | | Easy | DFS +| 338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_338.java) | | Medium | +| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_337.java) | | Medium | DP +| 336 | [Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_336.java) | | Hard | +| 335 | [Self Crossing](https://leetcode.com/problems/self-crossing/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_335.java) | | Hard | Math +| 334 | [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_334.java) | | Medium | Array, Greedy +| 333 | [Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_333.java) | | Medium | Tree +| 332 | [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_332.java) | | Medium | Graph, DFS +| 331 | [Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_331.java) | | Medium | Stack +| 330 | [Patching Array](https://leetcode.com/problems/patching-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_330.java) | | Hard | Greedy +| 329 | [Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_329.java) | | Hard | DFS, DP +| 328 | [Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_328.java) | | Medium | Linked List +| 327 | [Count of Range Sum](https://leetcode.com/problems/count-of-range-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_327.java) | | Hard | BST, Divide and Conquer +| 326 | [Power of Three](https://leetcode.com/problems/power-of-three/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_326.java) | | Easy | Math +| 325 | [Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_325.java) | | Medium | HashTable +| 324 | [Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_324.java) | | Medium | Sort +| 323 | [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_323.java) | | Medium | +| 322 | [Coin Change](https://leetcode.com/problems/coin-change/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_322.java) | | Medium | DP +| 321 | [Create Maximum Number](https://leetcode.com/problems/create-maximum-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_321.java) | | Hard +| 320 | [Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_320.java) | | Medium | Backtracking, Bit Manipulation +| 319 | [Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_319.java) | | Medium | Brainteaser +| 318 | [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_318.java) | | Medium | +| 317 | [Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_317.java) | | Hard | +| 316 | [Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_316.java) | | Hard | Stack, Recursion, Greedy +| 315 | [Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_315.java) | | Hard | Tree +| 314 | [Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_314.java) | | Medium | HashMap, BFS +| 313 | [Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_313.java) | | Medium | +| 312 | [Burst Balloons](https://leetcode.com/problems/burst-balloons/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_312.java) | | Hard | DP +| 311 | [Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_311.java) | | Medium | +| 310 | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_310.java) | | Medium | +| 309 | [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-cooldown/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_309.java) | | Medium | DP +| 308 | [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_308.java) | | Hard | Tree +| 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_307.java) | | Medium | Tree +| 306 | [Additive Number](https://leetcode.com/problems/additive-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_306.java) | | Medium | +| 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_305.java) | | Hard | Union Find +| 304 | [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_304.java) | | Medium | +| 303 | [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_303.java) | | Easy | +| 302 | [Smallest Rectangle Enclosing Black Pixels](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_302.java) | | Hard | DFS, BFS +| 301 | [Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_301.java) | | Hard | BFS +| 300 | [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_300.java) | | Medium | DP +| 299 | [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_299.java) | | Easy | +| 298 | [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_298.java) | | Medium | Tree +| 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_297.java) | | Hard | BFS +| 296 | [Best Meeting Point](https://leetcode.com/problems/best-meeting-point/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_296.java) | | Hard | +| 295 | [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_295.java) | | Hard | Heap +| 294 | [Flip Game II](https://leetcode.com/problems/flip-game-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_294.java) | | Medium | Backtracking +| 293 | [Flip Game](https://leetcode.com/problems/flip-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_293.java) | | Easy | +| 292 | [Nim Game](https://leetcode.com/problems/nim-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_292.java) | | Easy | +| 291 | [Word Pattern II](https://leetcode.com/problems/word-pattern-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_291.java) | | Hard | Recursion, Backtracking +| 290 | [Word Pattern](https://leetcode.com/problems/word-pattern/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_290.java) | | Easy | HashMap +| 289 | [Game of Life](https://leetcode.com/problems/game-of-life/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_289.java) | [:tv:](https://youtu.be/YZ-W5DrKPQ0) | Medium | +| 288 | [Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_288.java) | | Easy | +| 287 | [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_287.java) | | Medium | +| 286 | [Walls and Gates](https://leetcode.com/problems/walls-and-gates/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_286.java) | | Medium | BFS +| 285 | [Inorder Successor In BST](https://leetcode.com/problems/inorder-successor-in-bst/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_285.java) | | Medium | Tree +| 284 | [Peeking Iterator](https://leetcode.com/problems/peeking-iterator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_284.java) | | Medium | Design +| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_283.java) | [:tv:](https://youtu.be/39VJV4KVyi8) | Easy | +| 282 | [Expression Add Operators](https://leetcode.com/problems/expression-add-operators/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_282.java) | | Hard | +| 281 | [Zigzag Iterator](https://leetcode.com/problems/zigzag-iterator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_281.java) | | Medium | +| 280 | [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_280.java) | | Medium | +| 279 | [Perfect Squares](https://leetcode.com/problems/perfect-squares/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_279.java) | | Medium | +| 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_278.java) | [:tv:](https://youtu.be/E15djRphPj0) | Easy | Binary Search +| 277 | [Find the Celebrity](https://leetcode.com/problems/find-the-celebrity/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_277.java) | | Medium | +| 276 | [Paint Fence](https://leetcode.com/problems/paint-fence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_276.java) | | Easy | DP +| 275 | [H-Index II](https://leetcode.com/problems/h-index-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_275.java) | | Medium | Binary Search +| 274 | [H-Index](https://leetcode.com/problems/h-index/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_274.java) | | Medium | +| 273 | [Integer to English Words](https://leetcode.com/problems/integer-to-english-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_273.java) | | Hard | Math, String +| 272 | [Closest Binary Search Tree Value II](https://leetcode.com/problems/closest-binary-search-tree-value-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_272.java) | | Hard | Stack +| 271 | [Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_271.java) | | | Medium | +| 270 | [Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_270.java) | | | Easy | DFS +| 269 | [Alien Dictionary](https://leetcode.com/problems/alien-dictionary/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_269.java) | | | Hard | Topological Sort +| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_268.java) | | | Easy | Bit Manipulation +| 267 | [Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_267.java) | | Medium | +| 266 | [Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_266.java) | | Easy | +| 265 | [Paint House II](https://leetcode.com/problems/paint-house-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_265.java) | | Hard | DP +| 264 | [Ugly Number II](https://leetcode.com/problems/ugly-number-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_264.java) | | Medium | DP +| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_263.java) | | Easy | +| 261 | [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_261.java) | | Medium | +| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_260.java) | | Medium | +| 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_259.java) | | Medium | +| 258 | [Add Digits](https://leetcode.com/problems/add-digits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_258.java) | | Easy | +| 257 | [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_257.java) | || DFS/Recursion +| 256 | [Paint House](https://leetcode.com/problems/paint-house/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_256.java) | | Medium | DP +| 255 | [Verify Preorder Sequence in Binary Search Tree](https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_255.java) | | Medium | Tree +| 254 | [Factor Combinations](https://leetcode.com/problems/factor-combinations/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_254.java) | | Medium | Backtracking +| 253 | [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_253.java) | | Medium | Heap +| 252 | [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_252.java) | | Easy +| 251 | [Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_251.java) | | Medium | +| 250 | [Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_250.java) | | Medium | DFS +| 249 | [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_249.java) | || +| 248 | [Strobogrammatic Number III](https://leetcode.com/problems/strobogrammatic-number-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_248.java) | | Hard | Recursion, DFS +| 247 | [Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_247.java) | | Medium | Recursion +| 246 | [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_246.java) | | Easy +| 245 | [Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_245.java) | | Medium | +| 244 | [Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_244.java) | | Medium | HashMap +| 243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_243.java) | | Easy +| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_242.java) | [:tv:](https://youtu.be/7U3dMXiQBrU) | Easy +| 241 | [Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_241.java) | | Medium | Divide and Conquer +| 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_240.java) | | Medium | Binary Search +| 239 | [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_239.java) | | Hard | Heap +| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_238.java) | | Medium | Array +| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_237.java) | [:tv:](https://youtu.be/sW8ZaOTtvgI) | Easy | LinkedList +| 236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_236.java) | | Medium | DFS +| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_235.java) | [:tv:](https://youtu.be/ML6vGnziUaI) | Medium | DFS +| 234 | [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_234.java) | [:tv:](https://youtu.be/bOGh_3MTrdE) | Easy | Linked List +| 233 | [Number of Digit One](https://leetcode.com/problems/number-of-digit-one/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_233.java) | | Hard | Math +| 232 | [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_232.java) | | Medium | Stack, Design +| 231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_231.java) | | Easy | +| 230 | [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_230.java) | | Medium | Tree +| 229 | [Majority Element II](https://leetcode.com/problems/majority-element-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_229.java) | | Medium | +| 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_228.java) | | Medium | Array +| 227 | [Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_227.java) | | Medium | String +| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_226.java) | | Easy | DFS, recursion +| 225 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_225.java) | | Easy | Stack, Queue +| 224 | [Basic Calculator](https://leetcode.com/problems/basic-calculator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_224.java) | | Hard | Recursion +| 223 | [Rectangle Area](https://leetcode.com/problems/rectangle-area/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_223.java) | | Easy | +| 222 | [Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_222.java) | | Medium | Recursion +| 221 | [Maximal Square](https://leetcode.com/problems/maximal-square/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_221.java) | | Medium | Recursion +| 220 | [Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_220.java) | [:tv:](https://youtu.be/Cu7g9ovYHNI) | Medium | TreeSet +| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_219.java) | [:tv:](https://youtu.be/SFMCxqSeM94) | Easy | HashMap +| 218 | [The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_218.java) | | Hard | TreeMap, Design +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_217.java) | [:tv:](https://youtu.be/SFMCxqSeM94) | Easy | HashSet +| 216 | [Combination Sum III](https://leetcode.com/problems/combination-sum-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_216.java) | | Medium | Backtracking +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_215.java) | | Medium | Heap +| 214 | [Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_214.java) | | Hard | KMP +| 213 | [House Robber II](https://leetcode.com/problems/house-robber-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_213.java) | | Medium | DP +| 212 | [Word Search II](https://leetcode.com/problems/word-search-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/WordSearchII.java) | | Hard | Trie +| 211 | [Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_211.java) | | Medium | Trie +| 210 | [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_210.java) | | Medium | Adjacency List, BFS, Topological Sort +| 209 | [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_209.java) | | Medium | +| 208 | [Implement Trie](https://leetcode.com/problems/implement-trie-prefix-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_208.java) | [:tv:](https://youtu.be/Br7Wt4V5o1c) | Medium | Trie +| 207 | [Course Schedule](https://leetcode.com/problems/course-schedule/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_207.java) | | Medium | +| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_206.java) | [:tv:](https://youtu.be/N_Y12-5oa-w) | Easy | Linked List +| 205 | [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_205.java) | | Easy +| 204 | [Count Primes](https://leetcode.com/problems/count-primes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_204.java) | | Easy | The Sieve of Eratosthenes +| 203 | [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_203.java) | | Easy +| 202 | [Happy Number](https://leetcode.com/problems/happy-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_202.java) | | Easy +| 201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_201.java) | | Medium | Bit Manipulation +| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_200.java) | | Medium | Union Find, DFS +| 199 | [Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_199.java) | | Medium | BFS +| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_198.java) | | Easy | DP +| 191 | [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_191.java) | | Easy | Bit Manipulation +| 190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_190.java) | | Easy | Bit Manipulation +| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_189.java) | [:tv:](https://youtu.be/lTHTR_jsqAQ) | Easy +| 188 | [Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_188.java) | | Hard | DP +| 187 | [Repeated DNA Sequences](https://leetcode.com/problems/repeated-dna-sequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_187.java) | | Medium +| 186 | [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_186.java) | | Medium +| 179 | [Largest Number](https://leetcode.com/problems/largest-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_179.java) | | Medium | +| 174 | [Dungeon Game](https://leetcode.com/problems/dungeon-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_174.java) | | Hard | DP +| 173 | [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_173.java) | | Medium | Stack, Design +| 172 | [Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_172.java) | | Easy +| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_171.java) | | Easy +| 170 | [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_170.java) | | Easy +| 169 | [Majority Element](https://leetcode.com/problems/majority-element/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_169.java) | [:tv:](https://youtu.be/M1IL4hz0QrE) | Easy | +| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_168.java) | | Easy | +| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_167.java), [Javascript](../master/javascript/_167.js) | | Easy | Binary Search +| 166 | [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_166.java) | | Medium | HashMap +| 165 | [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_165.java) | | Easy | +| 164 | [Maximum Gap](https://leetcode.com/problems/maximum-gap/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_164.java) | | Hard | +| 163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_163.java) | || +| 162 | [Find Peak Element](https://leetcode.com/problems/find-peak-element/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_162.java) | | Binary Search | +| 161 | [One Edit Distance](https://leetcode.com/problems/one-edit-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_161.java) | || +| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_160.java) | | Easy | Linked List +| 159 | [Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_159.java) | | Hard | String, Sliding Window +| 158 | [Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_158.java) | | Hard | +| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_157.java) | | Easy | +| 156 | [Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_156.java) | | Medium | Tree, Recursion +| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_155.java) | | Easy | Stack +| 154 | [Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_154.java) | | Hard | Array, Binary Search +| 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_153.java) | | Medium | Array, Binary Search +| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_152.java) | | Medium | Array +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_151.java) | | Medium | String +| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_150.java) | | Medium +| 149 | [Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_149.java) | | Hard | +| 148 | [Sort List](https://leetcode.com/problems/sort-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_148.java) || Medium | Linked List, Sorting +| 147 | [Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_147.java) || Medium | Linked List +| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_146.java) | | Hard | Doubly Linked List, LinkedHashMap +| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_145.java) | [:tv:](https://youtu.be/B6XTLPpsW7k) | Easy | Binary Tree +| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_144.java) | [:tv:](https://youtu.be/367McfIeBDM) and [:tv:](https://youtu.be/vMHaqhiTn7Y) | Medium | Binary Tree +| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_143.java) | | Medium | +| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_142.java) | | Medium | Linked List +| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_141.java) | [:tv:](https://youtu.be/agkyC-rbgKM) | Easy | Linked List +| 140 | [Word Break II](https://leetcode.com/problems/word-break-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_140.java) | | Hard | Backtracking/DFS +| 139 | [Word Break](https://leetcode.com/problems/word-break/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_139.java) | [:tv:](https://youtu.be/iWenZCZEBIA) | Medium | DP, Pruning +| 138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_138.java) | | Medium | LinkedList, HashMap +| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_137.java) | | Medium | Bit Manipulation +| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_136.java) | [:tv:](https://youtu.be/gJ8VcJ8f_Vk) | Easy | Bit Manipulation +| 135 | [Candy](https://leetcode.com/problems/candy/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_135.java) | | Hard | Greedy +| 134 | [Gas Station](https://leetcode.com/problems/gas-station/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_134.java) | | Medium | Greedy +| 133 | [Clone Graph](https://leetcode.com/problems/clone-graph/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_133.java) | | Medium | HashMap, BFS, Graph +| 132 | [Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_132.java) | | Hard | +| 131 | [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_131.java) | | Medium | +| 130 | [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_130.java) | | Medium | +| 129 | [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_129.java) | | Medium | DFS +| 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_128.java) | | Hard | Union Find +| 127 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_127.java) | | Hard | BFS +| 126 | [Word Ladder II](https://leetcode.com/problems/word-ladder-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_126.java) | | Hard | BFS +| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_125.java) | | Easy | Two Pointers +| 124 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_124.java) | | Hard | Tree, DFS +| 123 | [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_123.java) | | Hard | DP +| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_122.java) | | Easy | Greedy +| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_121.java) | | Easy | +| 120 | [Triangle](https://leetcode.com/problems/triangle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_120.java) | | Medium | DP +| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_119.java) | [:tv:](https://www.youtube.com/watch?v=iVhmR1bzKoo) | Easy | +| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_118.java) | [:tv:](https://www.youtube.com/watch?v=TXd5lfP3Gac) | Easy | +| 117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-parents-right-pointers-in-each-node-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_117.java) | | Medium | BFS +| 116 | [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-parents-right-pointers-in-each-node/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_116.java) | | Medium | BFS +| 115 | [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_115.java) | | Hard | DP +| 114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_114.java) | | Medium | Tree +| 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_113.java) | | Medium | DFS, Backtracking +| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_112.java) | | Easy | DFS +| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_111.java) | | Easy | BFS, DFS +| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_110.java) | | Easy | DFS +| 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_109.java) | | Medium | DFS, Recursion +| 108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_108.java) | [:tv:](https://youtu.be/VVSnM5DGvjg) | Easy | Tree +| 107 | [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_107.java) | | Easy | BFS +| 106 | [Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_106.java) | | Medium | Recursion, Tree +| 105 | [Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_105.java) | | Medium | Recursion, Tree +| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_104.java) | [:tv:](https://youtu.be/dvmoHr5cN80) | Easy | DFS +| 103 | [Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_103.java) | | Medium | BFS,DFS +| 102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_102.java) | [:tv:](https://youtu.be/sFDNL6r5aDM) | Medium | BFS +| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_101.java) | [:tv:](https://www.youtube.com/watch?v=F85boSPtfKg) | Easy | DFS +| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_100.java) | [:tv:](https://www.youtube.com/watch?v=2Pe6e0KbgFI) | Easy | DFS +| 99 | [Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_99.java) | | Hard | +| 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_98.java) | [:tv:](https://youtu.be/kR5AxWHa9nc) | Medium | DFS/Recursion +| 97 | [Interleaving String](https://leetcode.com/problems/interleaving-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_97.java) | | Hard | DP +| 96 | [Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_96.java) | | Medium | Recursion, DP +| 95 | [Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_95.java) | | Medium | Recursion +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_94.java) | [:tv:](https://youtu.be/o_T8MswDI_Y) [:tv:](https://youtu.be/QxFOR8sQuB4) | Medium | Binary Tree +| 93 | [Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_93.java) | | Medium | Backtracking +| 92 | [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_92.java) | | Medium +| 91 | [Decode Ways](https://leetcode.com/problems/decode-ways/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_91.java) | | Medium | DP +| 90 | [Subsets II](https://leetcode.com/problems/subsets-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_90.java) || Medium | Backtracking +| 89 | [Gray Code](https://leetcode.com/problems/gray-code/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_89.java) || Medium | Bit Manipulation +| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_88.java) || Easy | +| 87 | [Scramble String](https://leetcode.com/problems/scramble-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_87.java) || Hard | Recursion +| 86 | [Partition List](https://leetcode.com/problems/partition-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_86.java) || Medium | Linked List +| 85 | [Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_85.java) || Hard | DP +| 84 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_84.java) || Hard | Array, Stack +| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_83.java) || Easy | Linked List +| 82 | [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_82.java) || Medium | Linked List +| 81 | [Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_81.java) || Medium | Binary Search +| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_80.java) || Medium | +| 79 | [Word Search](https://leetcode.com/problems/word-search/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_79.java) | | Medium | Backtracking, DFS +| 78 | [Subsets](https://leetcode.com/problems/subsets/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_78.java) || Medium | Backtracking +| 77 | [Combinations](https://leetcode.com/problems/combinations/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_77.java) || Medium | Backtracking +| 76 | [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_76.java) || Hard | Two Pointers +| 75 | [Sort Colors](https://leetcode.com/problems/sort-colors/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_75.java) || Medium | Two Pointers +| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_74.java) || Medium | Binary Search +| 73 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_73.java) || Medium | +| 72 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_72.java) || Hard | +| 71 | [Simplify Path](https://leetcode.com/problems/simplify-path/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_71.java) || Medium | Stack +| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_70.java) | [:tv:](https://youtu.be/ZMNRb9TYiQM) | Easy | DP +| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_69.java) || Easy | +| 68 | [Text Justification](https://leetcode.com/problems/text-justification/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_68.java) || Hard | +| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_67.java) || Easy | +| 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_66.java) | [:tv:](https://youtu.be/HKjt0f1N0GA) | Easy | +| 65 | [Valid Number](https://leetcode.com/problems/valid-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_65.java) || Hard | +| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_64.java) || Medium | DP +| 63 | [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_63.java) || Medium | DP +| 62 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_62.java) || Medium | DP +| 61 | [Rotate List](https://leetcode.com/problems/rotate-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_61.java) || Medium | Linked List +| 60 | [Permutation Sequence](https://leetcode.com/problems/permutation-sequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_60.java) || Medium | Math, Backtracking +| 59 | [Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_59.java) | [:tv:](https://www.youtube.com/watch?v=Sv9DK2C4rtc) | Medium | +| 58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_58.java) || Easy | +| 57 | [Insert Intervals](https://leetcode.com/problems/insert-interval/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_57.java) | [:tv:](https://youtu.be/gDVb3R4onIM) | Medium | Array, Sort +| 56 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_56.java) || Medium | Array, Sort +| 55 | [Jump Game](https://leetcode.com/problems/jump-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_55.java) || Medium | Greedy +| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_54.java) | [:tv:](https://www.youtube.com/watch?v=uYgoo8BdUAA) | Medium | Array +| 53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_53.java) || Easy | Array +| 52 | [N-Queens II](https://leetcode.com/problems/n-queens-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_52.java) || Hard | Backtracking +| 51 | [N-Queens](https://leetcode.com/problems/n-queens/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_51.java) || Hard | +| 50 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_50.java) || Medium | +| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_49.java) || Medium | HashMap +| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_48.java) | [:tv:](https://youtu.be/gCciKhaK2v8) | Medium | Array +| 47 | [Permutations II](https://leetcode.com/problems/permutations-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_47.java) || Medium | Backtracking +| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_46.java) | | Medium | Backtracking +| 45 | [Jump Game II](https://leetcode.com/problems/jump-game-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_45.java) || Hard | Array, Greedy +| 44 | [Wildcard Matching](https://leetcode.com/problems/wildcard-matching/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_44.java) || Hard | Backtracking, DP, Greedy, String +| 43 | [Multiply Strings](https://leetcode.com/problems/multiply-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_43.java) || Medium | Array, String +| 42 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_42.java) || Hard | +| 41 | [First Missing Positive](https://leetcode.com/problems/first-missing-positive/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_41.java) || Hard | Array +| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_40.java) || Medium | Backtracking +| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_39.java) || Medium | Backtracking +| 38 | [Count and Say](https://leetcode.com/problems/count-and-say/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_38.java) || Easy | Recursion, LinkedList +| 37 | [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_37.java) || Hard | +| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_36.java), [Javascript](./src/javascript/_36.js) || Medium | +| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_35.java) || Easy | Array +| 34 | [Search for a Range](https://leetcode.com/problems/search-for-a-range/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_34.java) || Medium | Array, Binary Search +| 33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_33.java) || Medium | Binary Search +| 32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_32.java) || Hard | Stack, DP +| 31 | [Next Permutation](https://leetcode.com/problems/parents-permutation) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_31.java), [C++](../master/cpp/_31.cpp) || Medium | Array +| 30 | [Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_30.java) || Hard | HashMap +| 29 | [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_29.java) || Medium | +| 28 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_28.java) || Easy | String +| 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_27.java) | | Easy | +| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_26.java) | [:tv:](https://youtu.be/nRKZC2JF7LU) | Easy | Array +| 25 | [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_25.java) | | Hard | Recursion, LinkedList +| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_24.java) || Medium | Recursion, LinkedList +| 23 | [Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_23.java) | [:tv:](https://www.youtube.com/watch?v=Llse1tImXQA) | Hard | Heap +| 22 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_22.java) || Medium | Backtracking +| 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_21.java) | [:tv:](https://youtu.be/N8WTaSSivEI) | Easy | Recursion +| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_20.java) | [:tv:](https://www.youtube.com/watch?v=eBbg5pnq5Zg) | Easy | Stack +| 19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_19.java) | [:tv:](https://youtu.be/Kka8VgyFZfc) | Medium | Linked List +| 18 | [4 Sum](https://leetcode.com/problems/4sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_18.java) || Medium | Two Pointers +| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_17.java), [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_17.js) || Medium | Backtracking +| 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_16.java) || Medium | Two Pointers +| 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_15.java), [C++](../master/cpp/_15.cpp) | [:tv:](https://www.youtube.com/watch?v=jeim_j8VdiM) | Medium | Two Pointers, Binary Search +| 14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_14.java) | [:tv:](https://www.youtube.com/watch?v=K1ps6d7YCy4) | Easy +| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_13.java) | | Easy | Math, String, HashTable +| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_12.java) || Medium | Math, String +| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_11.java) || Medium | +| 10 | [Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_10.java), [Javascript](../master/javascript/_10.js) || Hard | DP +| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_9.java), [C++](../master/cpp/_9.cpp) | | Easy +| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_8.java) | | Medium +| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_7.java), [C++](../master/cpp/_7.cpp) | [:tv:](https://youtu.be/tm1Yrb_SfBM) | Easy | +| 6 | [ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_6.java) | | Easy | +| 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_5.java) | | Medium | +| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_4.java), [C++](../master/cpp/_4.cpp) | | Hard | Divide and Conquer +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_3.java), [C++](../master/cpp/_3.cpp), [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_3.js) | | Medium | HashMap, Sliding Window +| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_2.java) | | Medium | LinkedList +| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_1.java), [C++](../master/cpp/_1.cpp), [Javascript](../master/javascript/_1.js) | [:tv:](https://www.youtube.com/watch?v=kPXOr6pW8KM&t=) | Easy | HashMap \ No newline at end of file diff --git a/paginated_contents/algorithms/2nd_thousand/README.md b/paginated_contents/algorithms/2nd_thousand/README.md new file mode 100644 index 0000000000..2547cfee04 --- /dev/null +++ b/paginated_contents/algorithms/2nd_thousand/README.md @@ -0,0 +1,472 @@ +| # | Title | Solutions | Video | Difficulty | Tag +|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------|------------|---------------------------------------------------------------------- +| 1996 | [The Number of Weak Characters in the Game](https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1996.java) || Medium || +| 1995 | [Count Special Quadruplets](https://leetcode.com/problems/count-special-quadruplets/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1995.java) || Easy || +| 1993 | [Operations on Tree](https://leetcode.com/problems/operations-on-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1993.java) || Medium | HashTable, DFS, Design, Tree +| 1992 | [Find All Groups of Farmland](https://leetcode.com/problems/find-all-groups-of-farmland/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1992.java) || Medium || +| 1991 | [Find the Middle Index in Array](https://leetcode.com/problems/find-the-middle-index-in-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1991.java) || Easy || +| 1985 | [Find the Kth Largest Integer in the Array](https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1985.java) || Medium || +| 1984 | [Minimum Difference Between Highest and Lowest of K Scores](https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1984.java) || Easy || +| 1981 | [Minimize the Difference Between Target and Chosen Elements](https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1981.java) || Medium | DP | +| 1980 | [Find Unique Binary String](https://leetcode.com/problems/find-unique-binary-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1980.java) || Medium || +| 1979 | [Find Greatest Common Divisor of Array](https://leetcode.com/problems/find-greatest-common-divisor-of-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1979.java) || Easy || +| 1974 | [Minimum Time to Type Word Using Special Typewriter](https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1974.java) || Easy || +| 1973 | [Count Nodes Equal to Sum of Descendants](https://leetcode.com/problems/count-nodes-equal-to-sum-of-descendants/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1973.java) || Medium | Tree, DFS | +| 1971 | [Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1971.java) || Easy | BFS, DFS, Graph | +| 1968 | [Array With Elements Not Equal to Average of Neighbors](https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1968.java) || Medium || +| 1967 | [Number of Strings That Appear as Substrings in Word](https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1967.java) || Easy || +| 1966 | [Binary Searchable Numbers in an Unsorted Array](https://leetcode.com/problems/binary-searchable-numbers-in-an-unsorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1966.java) || Medium | Array, Binary Search | +| 1961 | [Check If String Is a Prefix of Array](https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1961.java) || Easy || +| 1957 | [Delete Characters to Make Fancy String](https://leetcode.com/problems/delete-characters-to-make-fancy-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1957.java) || Easy | String | +| 1952 | [Three Divisors](https://leetcode.com/problems/three-divisors/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1952.java) || Easy || +| 1945 | [Sum of Digits of String After Convert](https://leetcode.com/problems/sum-of-digits-of-string-after-convert/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1945.java) || Easy || +| 1941 | [Check if All Characters Have Equal Number of Occurrences](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1941.java) || Easy || +| 1936 | [Add Minimum Number of Rungs](https://leetcode.com/problems/add-minimum-number-of-rungs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1936.java) || Medium || +| 1935 | [Maximum Number of Words You Can Type](https://leetcode.com/problems/maximum-number-of-words-you-can-type/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1935.java) || Easy | String | +| 1933 | [Check if String Is Decomposable Into Value-Equal Substrings](https://leetcode.com/problems/check-if-string-is-decomposable-into-value-equal-substrings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1933.java) || Easy | String | +| 1929 | [Concatenation of Array](https://leetcode.com/problems/concatenation-of-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1929.java) || Easy || +| 1926 | [Nearest Exit from Entrance in Maze](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1926.java) || Medium | DP, DFS, BFS | +| 1925 | [Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1925.java) || Easy | Array, Greedy | +| 1920 | [Build Array from Permutation](https://leetcode.com/problems/build-array-from-permutation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1920.java) || Easy || +| 1913 | [Maximum Product Difference Between Two Pairs](https://leetcode.com/problems/maximum-product-difference-between-two-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1913.java) || Easy | Sort | +| 1910 | [Remove All Occurrences of a Substring](https://leetcode.com/problems/remove-all-occurrences-of-a-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1904.java) | [:tv:](https://youtu.be/d74CJIqw268) | Medium | String | +| 1909 | [Remove One Element to Make the Array Strictly Increasing](https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1909.java) || Easy | Array | +| 1904 | [The Number of Full Rounds You Have Played](https://leetcode.com/problems/the-number-of-full-rounds-you-have-played/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1904.java) || Medium | String, Greedy | +| 1903 | [Largest Odd Number in String](https://leetcode.com/problems/largest-odd-number-in-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1903.java) | [:tv:](https://youtu.be/IIt_ARZzclY) | Easy | Greedy | +| 1899 | [Merge Triplets to Form Target Triplet](https://leetcode.com/problems/merge-triplets-to-form-target-triplet/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1899.java) || Medium | Array, Greedy | +| 1897 | [Redistribute Characters to Make All Strings Equal](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1897.java) || Easy | String, Greedy | +| 1894 | [Find the Student that Will Replace the Chalk](https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1894.java) || Medium | | +| 1893 | [Check if All the Integers in a Range Are Covered](https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1893.java) || Easy | Array, HashTable, Prefix Sum | +| 1891 | [Cutting Ribbons](https://leetcode.com/problems/cutting-ribbons/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1891.java) || Medium | Array, Binary Search | +| 1886 | [Determine Whether Matrix Can Be Obtained By Rotation](https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1886.java) || Easy | Array | +| 1880 | [Check if Word Equals Summation of Two Words](https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1880.java) || Easy | String | +| 1877 | [Minimize Maximum Pair Sum in Array](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1877.java) || Medium | Greedy, Sort | +| 1876 | [Substrings of Size Three with Distinct Characters](https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1876.java) || Easy | String | +| 1874 | [Minimize Product Sum of Two Arrays](https://leetcode.com/problems/minimize-product-sum-of-two-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1874.java) || Medium | Array, Greedy, Sorting | +| 1869 | [Longer Contiguous Segments of Ones than Zeros](https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1869.java) || Easy | Array, Two Pointers | +| 1868 | [Product of Two Run-Length Encoded Arrays](https://leetcode.com/problems/product-of-two-run-length-encoded-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1868.java) || Medium | Two Pointers | +| 1863 | [Sum of All Subset XOR Totals](https://leetcode.com/problems/sum-of-all-subset-xor-totals/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1863.java) || Easy | Backtracking, Recursion | +| 1862 | [Sum of Floored Pairs](https://leetcode.com/problems/sum-of-floored-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1862.java) || Hard | Math | +| 1861 | [Rotating the Box](https://leetcode.com/problems/rotating-the-box/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1861.java) | [:tv:](https://youtu.be/2LRnTMOiqSI) | Medium | Array, Two Pointers | +| 1860 | [Incremental Memory Leak](https://leetcode.com/problems/incremental-memory-leak/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1860.java) || Medium | Math | +| 1859 | [Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1859.java) || Easy | String, Sort | +| 1854 | [Maximum Population Year](https://leetcode.com/problems/maximum-population-year/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1854.java) || Easy | Array | +| 1848 | [Minimum Distance to the Target Element](https://leetcode.com/problems/minimum-distance-to-the-target-element/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1848.java) || Easy | Array | +| 1845 | [Seat Reservation Manager](https://leetcode.com/problems/seat-reservation-manager/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1845.java) || Medium | Heap, Design | +| 1844 | [Replace All Digits with Characters](https://leetcode.com/problems/replace-all-digits-with-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1844.java) || Easy | String | +| 1837 | [Sum of Digits in Base K](https://leetcode.com/problems/sum-of-digits-in-base-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1837.java) || Easy | Math, Bit Manipulation | +| 1836 | [Remove Duplicates From an Unsorted Linked List](https://leetcode.com/problems/remove-duplicates-from-an-unsorted-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1836.java) || Medium | HashTable, LinkedList | +| 1833 | [Maximum Ice Cream Bars](https://leetcode.com/problems/maximum-ice-cream-bars/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1833.java) || Medium | Array, Sort | +| 1832 | [Check if the Sentence Is Pangram](https://leetcode.com/problems/check-if-the-sentence-is-pangram/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1832.java) || Easy | String | +| 1829 | [Maximum XOR for Each Query](https://leetcode.com/problems/maximum-xor-for-each-query/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1829.java) || Medium | Bit Manipulation | +| 1828 | [Queries on Number of Points Inside a Circle](https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1828.java) | [:tv:](https://youtu.be/fU4oOBSsVMg) | Medium | Math | +| 1827 | [Minimum Operations to Make the Array Increasing](https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1827.java) || Easy | Array, Greedy | +| 1826 | [Faulty Sensor](https://leetcode.com/problems/faulty-sensor/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1826.java) || Easy | Array, Two Pointers | +| 1823 | [Find the Winner of the Circular Game](https://leetcode.com/problems/find-the-winner-of-the-circular-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1823.java) || Medium | Array | +| 1822 | [Sign of the Product of an Array](https://leetcode.com/problems/sign-of-the-product-of-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1822.java) || Easy | Math | +| 1817 | [Finding the Users Active Minutes](https://leetcode.com/problems/finding-the-users-active-minutes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1817.java) || Medium | HashTable | +| 1816 | [Truncate Sentence](https://leetcode.com/problems/truncate-sentence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1816.java) || Easy | String | +| 1814 | [Count Nice Pairs in an Array](https://leetcode.com/problems/count-nice-pairs-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1814.java) || Medium | Array, HashTable | +| 1813 | [Sentence Similarity III](https://leetcode.com/problems/sentence-similarity-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1813.java) | [:tv:](https://youtu.be/MMMd7dMv4Ak) | Medium | String | +| 1812 | [Determine Color of a Chessboard Square](https://leetcode.com/problems/determine-color-of-a-chessboard-square/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1812.java) || Easy | String | +| 1807 | [Evaluate the Bracket Pairs of a String](https://leetcode.com/problems/evaluate-the-bracket-pairs-of-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1807.java) || Medium | HashTable, String | +| 1806 | [Minimum Number of Operations to Reinitialize a Permutation](https://leetcode.com/problems/minimum-number-of-operations-to-reinitialize-a-permutation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1806.java) || Medium | Array, Greedy | +| 1805 | [Number of Different Integers in a String](https://leetcode.com/problems/number-of-different-integers-in-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1805.java) || Medium | String | +| 1804 | [Implement Trie II (Prefix Tree)](https://leetcode.com/problems/implement-trie-ii-prefix-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1804.java) || Medium | Trie, Design | +| 1800 | [Maximum Ascending Subarray Sum](https://leetcode.com/problems/maximum-ascending-subarray-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1800.java) || Easy | Two Pointers | +| 1797 | [Design Authentication Manager](https://leetcode.com/problems/design-authentication-manager/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1797.java) || Medium | HashTable, Design | +| 1796 | [Second Largest Digit in a String](https://leetcode.com/problems/second-largest-digit-in-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1796.java) || Easy | String | +| 1792 | [Maximum Average Pass Ratio](https://leetcode.com/problems/maximum-average-pass-ratio/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1792.java) || Medium | Heap | +| 1791 | [Find Center of Star Graph](https://leetcode.com/problems/find-center-of-star-graph/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1791.java) || Medium | Graph | +| 1790 | [Check if One String Swap Can Make Strings Equal](https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1790.java) || Easy | String | +| 1785 | [Minimum Elements to Add to Form a Given Sum](https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1785.java) || Medium | Greedy | +| 1784 | [Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1784.java) || Easy | Greedy | +| 1781 | [Sum of Beauty of All Substrings](https://leetcode.com/problems/sum-of-beauty-of-all-substrings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1781.java) || Medium | HashTable, String | +| 1780 | [Check if Number is a Sum of Powers of Three](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1780.java) || Medium | Math, Backtracking, Recursion | +| 1779 | [Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1779.java) || Easy | Array | +| 1775 | [Equal Sum Arrays With Minimum Number of Operations](https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1775.java) || Medium | Greedy | +| 1774 | [Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1774.java) || Medium | Greedy | +| 1773 | [Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1773.java) | [:tv:](https://youtu.be/eHk8TQIhvCk) | Easy | Array, String | +| 1772 | [Sort Features by Popularity](https://leetcode.com/problems/sort-features-by-popularity/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1772.java) | [:tv:](https://youtu.be/5629LqqeLAM) | Medium | HashTable, Sort | +| 1769 | [Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1769.java) || Medium | Array, Greedy | +| 1768 | [Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1768.java) || Easy | String | +| 1765 | [Map of Highest Peak](https://leetcode.com/problems/map-of-highest-peak/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1765.java) || Medium | BFS, Graph | +| 1764 | [Form Array by Concatenating Subarrays of Another Array](https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1764.java) || Medium | Array, Greedy | +| 1763 | [Longest Nice Substring](https://leetcode.com/problems/longest-nice-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1763.java) || Easy | String | +| 1762 | [Buildings With an Ocean View](https://leetcode.com/problems/buildings-with-an-ocean-view/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1762.java) || Medium | | +| 1759 | [Count Number of Homogenous Substrings](https://leetcode.com/problems/count-number-of-homogenous-substrings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1758.java) || Medium | String ,Greedy | +| 1758 | [Minimum Changes To Make Alternating Binary String](https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1758.java) || Easy | Greedy, Array | +| 1756 | [Design Most Recently Used Queue](https://leetcode.com/problems/design-most-recently-used-queue/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1756.java) || Medium | Array, Design, Dequeue | +| 1754 | [Largest Merge Of Two Strings](https://leetcode.com/problems/largest-merge-of-two-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1753.java) || Medium | Greedy, Suffix Array | +| 1753 | [Maximum Score From Removing Stones](https://leetcode.com/problems/maximum-score-from-removing-stones/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1753.java) || Medium | Math, Heap | +| 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1752.java) || Easy | Array | +| 1750 | [Minimum Length of String After Deleting Similar Ends](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1750.java) || Medium | Two Pointers | +| 1749 | [Maximum Absolute Sum of Any Subarray](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1749.java) || Medium | Greedy | +| 1748 | [Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1748.java) || Easy | Array, HashTable | +| 1746 | [Maximum Subarray Sum After One Operation](https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1746.java) || Medium | DP | +| 1745 | [Palindrome Partitioning IV](https://leetcode.com/problems/palindrome-partitioning-iv/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1745.java) || Hard | String, DP | +| 1743 | [Restore the Array From Adjacent Pairs](https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1743.java) || Medium | Greedy | +| 1742 | [Maximum Number of Balls in a Box](https://leetcode.com/problems/maximum-number-of-balls-in-a-box/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1742.java) || Easy | Array | +| 1740 | [Find Distance in a Binary Tree](https://leetcode.com/problems/find-distance-in-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1740.java) || Medium | DFS, BFS | +| 1736 | [Latest Time by Replacing Hidden Digits](https://leetcode.com/problems/latest-time-by-replacing-hidden-digits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1736.java) || Easy | String, Greedy | +| 1733 | [Minimum Number of People to Teach](https://leetcode.com/problems/minimum-number-of-people-to-teach/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1733.java) || Medium | Array, Greedy | +| 1732 | [Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1732.java) || Easy | Array | +| 1730 | [Shortest Path to Get Food](https://leetcode.com/problems/shortest-path-to-get-food/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1730.java) || Medium | BFS | +| 1727 | [Largest Submatrix With Rearrangements](https://leetcode.com/problems/largest-submatrix-with-rearrangements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1727.java) || Medium | Greedy, Sort | +| 1726 | [Tuple with Same Product](https://leetcode.com/problems/tuple-with-same-product/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1726.java) | [:tv:](https://youtu.be/asI_UBkXI0M) | Medium | Array | +| 1725 | [Number Of Rectangles That Can Form The Largest Square](https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1725.java) || Easy | Greedy | +| 1721 | [Swapping Nodes in a Linked List](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1721.java) || Medium | LinkedList | +| 1720 | [Decode XORed Array](https://leetcode.com/problems/decode-xored-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1720.java) || Easy | Bit Manipulation | +| 1718 | [Construct the Lexicographically Largest Valid Sequence](https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1718.java) || Medium | Backtracking, Recursion | +| 1717 | [Maximum Score From Removing Substrings](https://leetcode.com/problems/maximum-score-from-removing-substrings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1717.java) | [:tv:](https://youtu.be/9wZ-TWBreTg) | Medium | Greedy | +| 1716 | [Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1716.java) || Easy | Math, Greedy | +| 1711 | [Count Good Meals](https://leetcode.com/problems/count-good-meals/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1711.java) || Medium | Array, HashTable, Two Pointers | +| 1710 | [Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1710.java) || Easy | Greedy, Sort | +| 1708 | [Largest Subarray Length K](https://leetcode.com/problems/largest-subarray-length-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1708.java) || Easy | Array, Greedy | +| 1705 | [Maximum Number of Eaten Apples](https://leetcode.com/problems/maximum-number-of-eaten-apples/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1705.java) || Medium | Heap, Greedy | +| 1704 | [Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1704.java) || Easy | String | +| 1701 | [Average Waiting Time](https://leetcode.com/problems/average-waiting-time/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1701.java) || Medium | Array | +| 1700 | [Number of Students Unable to Eat Lunch](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1700.java) || Easy | Array | +| 1695 | [Maximum Erasure Value](https://leetcode.com/problems/maximum-erasure-value/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1695.java) || Medium | Two Pointers | +| 1694 | [Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1694.java) || Easy | String | +| 1690 | [Stone Game VII](https://leetcode.com/problems/stone-game-vii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1690.java) || Medium | DP | +| 1689 | [Partitioning Into Minimum Number Of Deci-Binary Numbers](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1689.java) || Medium | Greedy | +| 1688 | [Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1688.java) || Easy | Backtracking | +| 1686 | [Stone Game VI](https://leetcode.com/problems/stone-game-vi/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1686.java) || Medium | Greedy | +| 1685 | [Sum of Absolute Differences in a Sorted Array](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1685.java) | [:tv:](https://youtu.be/WYe644djV30) | Medium | Math, Greedy | +| 1684 | [Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1684.java) || Easy | String | +| 1680 | [Concatenation of Consecutive Binary Numbers](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1680.java) || Medium | Math | +| 1679 | [Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1679.java) || Medium | HashTable | +| 1678 | [Goal Parser Interpretation](https://leetcode.com/problems/goal-parser-interpretation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1678.java) || Easy | String | +| 1676 | [Lowest Common Ancestor of a Binary Tree IV](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iv/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1676.java) || Medium | Tree, DFS, Binary Tree | +| 1675 | [Minimize Deviation in Array](https://leetcode.com/problems/minimize-deviation-in-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1675.java) || Hard | Heap, Ordered Map | +| 1673 | [Find the Most Competitive Subsequence](https://leetcode.com/problems/find-the-most-competitive-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1673.java) | [:tv:](https://youtu.be/GBJFxSD3B_s) | Medium | Stack, Greedy | +| 1672 | [Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1672.java) || Easy | Array | +| 1670 | [Design Front Middle Back Queue](https://leetcode.com/problems/design-front-middle-back-queue/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1670.java) || Medium | Linked List, Design, Dequeu | +| 1669 | [Merge In Between Linked Lists](https://leetcode.com/problems/merge-in-between-linked-lists/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1669.java) || Medium | LinedList | +| 1668 | [Maximum Repeating Substring](https://leetcode.com/problems/maximum-repeating-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1668.java) || Easy | String | +| 1664 | [Ways to Make a Fair Array](https://leetcode.com/problems/ways-to-make-a-fair-array/) | [Javascript](./javascript/_1664.js) || Medium | Greedy | +| 1663 | [Smallest String With A Given Numeric Value](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1663.java) | [:tv:](https://youtu.be/o3MRJfsoUrw) | Medium | Greedy | +| 1662 | [Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1662.java) || Easy | String | +| 1660 | [Correct a Binary Tree](https://leetcode.com/problems/correct-a-binary-tree/) | [Javascript](./javascript/_1658.js), [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1660.java) || Medium | BFS, Tree | +| 1658 | [Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/) | [Javascript](./javascript/_1658.js), [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1658.java) || Medium | Greedy | +| 1657 | [Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1657.java) | [:tv:](https://youtu.be/-jXQK-UeChU) | Medium | Greedy | +| 1656 | [Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1656.java) || Easy | Array, Design | +| 1653 | [Minimum Deletions to Make String Balanced](https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1653.java) || Medium | Array, DP, Stack | +| 1652 | [Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1652.java) || Easy | Array | +| 1650 | [Lowest Common Ancestor of a Binary Tree III](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1650.java) || Medium | HashTable, Binary Tree, Tree | +| 1646 | [Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1646.java) || Easy | Array | +| 1644 | [Lowest Common Ancestor of a Binary Tree II](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1644.java) || Medium | Binary Tree, DFS | +| 1642 | [Furthest Building You Can Reach](https://leetcode.com/problems/furthest-building-you-can-reach/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1642.java) || Medium | Binary Search, Heap | +| 1641 | [Count Sorted Vowel Strings](https://leetcode.com/problems/count-sorted-vowel-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1641.java) | [:tv:](https://youtu.be/gdH4yfgfwiU) | Medium | Math, DP, Backtracking | +| 1640 | [Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1640.java) || Easy | Array, Sort | +| 1637 | [Widest Vertical Area Between Two Points Containing No Points](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/) | [Javascript](./javascript/_1637.js), [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1637.java) | | Medium | Sort | +| 1636 | [Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1636.java) || Easy | Array, Sort | +| 1630 | [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1630.java) || Medium | Sort | +| 1629 | [Slowest Key](https://leetcode.com/problems/slowest-key/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1629.java) || Easy | Array | +| 1628 | [Design an Expression Tree With Evaluate Function](https://leetcode.com/problems/design-an-expression-tree-with-evaluate-function/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1628.java) || Medium | Stack, Binary Tree, Design, Math | +| 1626 | [Best Team With No Conflicts](https://leetcode.com/problems/best-team-with-no-conflicts/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1626.java) || Medium | DP | +| 1625 | [Lexicographically Smallest String After Applying Operations](https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1625.java) || Medium | BFS, DFS | +| 1624 | [Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1624.java) | [:tv:](https://youtu.be/rfjeFs3JuYM) | Easy | String | +| 1620 | [Coordinate With Maximum Network Quality](https://leetcode.com/problems/coordinate-with-maximum-network-quality/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1620.java) | [:tv:](https://youtu.be/TqKDnzkRsh0) | Medium | Greedy | +| 1619 | [Mean of Array After Removing Some Elements](https://leetcode.com/problems/mean-of-array-after-removing-some-elements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1619.java) | [:tv:](https://youtu.be/vyrEra_OfDo) | Easy | Array | +| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1614.java) || Easy | String | +| 1609 | [Even Odd Tree](https://leetcode.com/problems/even-odd-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1609.java) || Medium | Tree | +| 1608 | [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1608.java) || Easy | Array | +| 1605 | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1605.java) || Medium | Greedy, Array, Matrix | +| 1604 | [Alert Using Same Key-Card Three or More Times in a One Hour Period](https://leetcode.com/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1604.java) || Medium | String, Ordered Map | +| 1603 | [Design Parking System](https://leetcode.com/problems/design-parking-system/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1603.java) || Easy | Design | +| 1602 | [Find Nearest Right Node in Binary Tree](https://leetcode.com/problems/find-nearest-right-node-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1602.java) || Medium | Tree, BFS | +| 1601 | [Maximum Number of Achievable Transfer Requests](https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1601.java) || Hard | Backtracking | +| 1600 | [Throne Inheritance](https://leetcode.com/problems/throne-inheritance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1600.java) || Medium | Tree, Design, DFS, HashMap | +| 1598 | [Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1598.java) || Easy | Stack | +| 1592 | [Rearrange Spaces Between Words](https://leetcode.com/problems/rearrange-spaces-between-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1592.java) || Easy | String | +| 1588 | [Sum of All Odd Length Subarrays](https://leetcode.com/problems/sum-of-all-odd-length-subarrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1588.java) || Easy | Array | +| 1583 | [Count Unhappy Friends](https://leetcode.com/problems/count-unhappy-friends/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1583.java) || Medium | Array | +| 1582 | [Special Positions in a Binary Matrix](https://leetcode.com/problems/special-positions-in-a-binary-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1582.java) || Easy | Array | +| 1577 | [Number of Ways Where Square of Number Is Equal to Product of Two Numbers](https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1577.java) || Medium | HashTable, Math | +| 1576 | [Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1576.java) | [:tv:](https://youtu.be/SJBDLYqrIsM) | Easy | String | +| 1574 | [Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1574.java) || Medium | Array, Binary Search | +| 1572 | [Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1572.java) || Easy | Array | +| 1570 | [Dot Product of Two Sparse Vectors](https://leetcode.com/problems/dot-product-of-two-sparse-vectors/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1570.java) || Easy | Array, HashTable, Two Pointers | +| 1567 | [Maximum Length of Subarray With Positive Product](https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1567.java) | [:tv:](https://youtu.be/bFer5PdsgpY) | Medium | Greedy | +| 1566 | [Detect Pattern of Length M Repeated K or More Times](https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1566.java) | [:tv:](https://youtu.be/aJAV_VgmjdE) | Easy | Array | +| 1561 | [Maximum Number of Coins You Can Get](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1561.java) | [:tv:](https://youtu.be/hPe9Z3TiUrA) | Medium | Sort | +| 1560 | [Most Visited Sector in a Circular Track](https://leetcode.com/problems/most-visited-sector-in-a-circular-track/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1560.java) || Easy | Array | +| 1558 | [Minimum Numbers of Function Calls to Make Target Array](https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1558.java) || Medium | Greedy | +| 1557 | [Minimum Number of Vertices to Reach All Nodes](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1557.java) | [:tv:](https://youtu.be/IfsiNU7J-6c) | Medium | Graph | +| 1556 | [Thousand Separator](https://leetcode.com/problems/thousand-separator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1556.java) | [:tv:](https://youtu.be/re2BnNbg598) | Easy | String | +| 1551 | [Minimum Operations to Make Array Equal](https://leetcode.com/problems/minimum-operations-to-make-array-equal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1551.java) | [:tv:](https://youtu.be/A-i2sxmBqAA) | Medium | Math | +| 1550 | [Three Consecutive Odds](https://leetcode.com/problems/three-consecutive-odds/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1550.java) | | Easy | Array | +| 1545 | [Find Kth Bit in Nth Binary String](https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1545.java) | [:tv:](https://youtu.be/34QYE5HAFy4) | Medium | String | +| 1544 | [Make The String Great](https://leetcode.com/problems/make-the-string-great/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1544.java) | [:tv:](https://youtu.be/Q-ycCXbUOck) | Easy | String, Stack | +| 1541 | [Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1541.java) | [:tv:](https://youtu.be/PEKAlnmbBCc) | Medium | String, Stack | +| 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1539.java) | [:tv:](https://youtu.be/p0P1JNHAB-c) | Easy | Array, HashTable | +| 1535 | [Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1535.java) | [:tv:](https://youtu.be/v6On1TQfMTU) | Medium | Array | +| 1534 | [Count Good Triplets](https://leetcode.com/problems/count-good-triplets/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1534.java) | | Easy | Array | +| 1530 | [Number of Good Leaf Nodes Pairs](https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1530.java) | | Medium | Tree, DFS | +| 1528 | [Shuffle String](https://leetcode.com/problems/shuffle-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1528.java) | | Easy | Sort | +| 1526 | [Minimum Number of Increments on Subarrays to Form a Target Array](https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1526.java) | | Hard | Segment Tree | +| 1525 | [Number of Good Ways to Split a String](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1525.java) | [:tv:](https://youtu.be/lRVpVUC5mQ4) | Medium | String, Bit Manipulation | +| 1524 | [Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1524.java) | | Medium | Array, Math | +| 1523 | [Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1523.java) | [:tv:](https://youtu.be/TkT-6WsmqY0) | Easy | Math | +| 1518 | [Water Bottles](https://leetcode.com/problems/water-bottles/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1518.java) | | Easy | Greedy | +| 1514 | [Path with Maximum Probability](https://leetcode.com/problems/path-with-maximum-probability/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1514.java) | | Medium | Graph | +| 1512 | [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1512.java) | | Easy | Array, HashTable, Math | +| 1509 | [Minimum Difference Between Largest and Smallest Value in Three Moves](https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1509.java) | | Medium | Array, Sort, Greedy | +| 1508 | [Range Sum of Sorted Subarray Sums](https://leetcode.com/problems/range-sum-of-sorted-subarray-sums/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1508.java) | | Medium | Array, Sort | +| 1507 | [Reformat Date](https://leetcode.com/problems/reformat-date/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1507.java) | | Easy | String | +| 1502 | [Can Make Arithmetic Progression From Sequence](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1502.java) | | Easy | Array, Sort | +| 1496 | [Path Crossing](https://leetcode.com/problems/path-crossing/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1496.java) | | Easy | String | +| 1493 | [Longest Subarray of 1's After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1493.java) | [:tv:](https://youtu.be/nKhteIRZ2Ok) | Medium | Array | +| 1492 | [The kth Factor of n](https://leetcode.com/problems/the-kth-factor-of-n/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1492.java) | | Medium | Math | +| 1491 | [Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1491.java) | | Easy | Array, Sort | +| 1490 | [Clone N-ary Tree](https://leetcode.com/problems/clone-n-ary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1490.java) | [:tv:](https://youtu.be/3Wnja3Bxeos) | Medium | HashTable, Tree, DFS, BFS | +| 1487 | [Making File Names Unique](https://leetcode.com/problems/making-file-names-unique/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1487.java) | | Medium | HashTable, String | +| 1486 | [XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1486.java) | | Medium | Array, Bit Manipulation | +| 1485 | [Clone Binary Tree With Random Pointer](https://leetcode.com/problems/clone-binary-tree-with-random-pointer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1485.java) | | Medium | HashTable, Tree, DFS, BFS | +| 1482 | [Minimum Number of Days to Make m Bouquets](https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1482.java) | | Medium | Array, Binary Search | +| 1481 | [Least Number of Unique Integers after K Removals](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1481.java) | | Medium | Array, Sort | +| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1480.java), [C++](../master/cpp/_1480.cpp) | | Easy | Array | +| 1476 | [Subrectangle Queries](https://leetcode.com/problems/subrectangle-queries/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1476.java) | | Medium | Array | +| 1475 | [Final Prices With a Special Discount in a Shop](https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1475.java) | | Easy | Array | +| 1474 | [Delete N Nodes After M Nodes of a Linked List](https://leetcode.com/problems/delete-n-nodes-after-m-nodes-of-a-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1474.java) | | Easy | LinkedList | +| 1472 | [Design Browser History](https://leetcode.com/problems/design-browser-history/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1472.java) | | Medium | Array, Design | +| 1471 | [The k Strongest Values in an Array](https://leetcode.com/problems/the-k-strongest-values-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1471.java) | | Medium | Array, Sort | +| 1470 | [Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1470.java), [C++](../master/cpp/_1470.cpp) | | Easy | Array | +| 1469 | [Find All The Lonely Nodes](https://leetcode.com/problems/find-all-the-lonely-nodes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1469.java) | | Easy | Tree, DFS | +| 1466 | [Reorder Routes to Make All Paths Lead to the City Zero](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1466.java) | | Medium | Tree, DFS, BFS | +| 1464 | [Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1464.java) | | Easy | Array | +| 1462 | [Course Schedule IV](https://leetcode.com/problems/course-schedule-iv/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1462.java) | | Medium | Topological Sort, DFS, BFS | +| 1461 | [Check If a String Contains All Binary Codes of Size K](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1461.java) | | Medium | String, Bit Manipulation | +| 1460 | [Make Two Arrays Equal by Reversing Sub-arrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1460.java) | | Easy | Array | +| 1457 | [Pseudo-Palindromic Paths in a Binary Tree](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1457.java) | | Medium | Bit Manipulation, Tree, DFS | +| 1456 | [Maximum Number of Vowels in a Substring of Given Length](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1456.java) | | Medium | String, Sliding Window | +| 1455 | [Check If a Word Occurs As a Prefix of Any Word in a Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1455.java) | | Easy | String | +| 1452 | [People Whose List of Favorite Companies Is Not a Subset of Another List](https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1452.java) | | Medium | String, Sort | +| 1451 | [Rearrange Words in a Sentence](https://leetcode.com/problems/rearrange-words-in-a-sentence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1451.java) | | Medium | String, Sort | +| 1450 | [Number of Students Doing Homework at a Given Time](https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1450.java) | | Easy | Array | +| 1448 | [Count Good Nodes in Binary Tree](https://leetcode.com/problems/count-good-nodes-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1448.java) | | Medium | Tree, DFS | +| 1447 | [Simplified Fractions](https://leetcode.com/problems/simplified-fractions/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1447.java) | | Medium | Math | +| 1446 | [Consecutive Characters](https://leetcode.com/problems/consecutive-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1446.java) | | Easy | String | +| 1441 | [Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1441.java) | | Easy | Stack | +| 1439 | [Find the Kth Smallest Sum of a Matrix With Sorted Rows](https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1439.java) | | Hard | Array, Binary Search, PriorityQueue, Matrix | +| 1438 | [Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1438.java) | | Medium | Array, Queue, Sliding Window, PriorityQueue, Monotonic Queue | +| 1437 | [Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1437.java) | | Medium | Array | +| 1436 | [Destination City](https://leetcode.com/problems/destination-city/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1436.java) | | Easy | String | +| 1432 | [Max Difference You Can Get From Changing an Integer](https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1432.java) | | Medium | String | +| 1431 | [Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1431.java), [C++](../master/cpp/_1431.cpp) | | Easy | Array | +| 1429 | [First Unique Number](https://leetcode.com/problems/first-unique-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1429.java) | | Medium | Array, HashTable, Design, Data Streams | +| 1428 | [Leftmost Column with at Least a One](https://leetcode.com/problems/leftmost-column-with-at-least-a-one/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1428.java) | | Medium | Array | +| 1427 | [Perform String Shifts](https://leetcode.com/problems/perform-string-shifts/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1427.java) | | Easy | Array, Math | +| 1426 | [Counting Elements](https://leetcode.com/problems/counting-elements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1426.java) | | Easy | Array | +| 1424 | [Diagonal Traverse II](https://leetcode.com/problems/diagonal-traverse-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1424.java) | | Medium | Matrix | +| 1423 | [Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1423.java) | | Medium | Array, DP, Sliding Window | +| 1422 | [Maximum Score After Splitting a String](https://leetcode.com/problems/maximum-score-after-splitting-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1422.java) | | Easy | String | +| 1418 | [Display Table of Food Orders in a Restaurant](https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1418.java) | | Medium | HashTable | +| 1417 | [Reformat The String](https://leetcode.com/problems/reformat-the-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1417.java) | | Easy | String | +| 1415 | [The k-th Lexicographical String of All Happy Strings of Length n](https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1415.java) | | Medium | Backtracking | +| 1414 | [Find the Minimum Number of Fibonacci Numbers Whose Sum Is K](https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1414.java) | | Medium | Array, Greedy | +| 1413 | [Minimum Value to Get Positive Step by Step Sum](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1413.java) | | Easy | Array | +| 1410 | [HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1410.java) | | Medium | String, Stack | +| 1409 | [Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1409.java) | | Medium | Array | +| 1408 | [String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1408.java) | | Easy | String | +| 1405 | [Longest Happy String](https://leetcode.com/problems/longest-happy-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1405.java) | | Medium | PriorityQueue, Greedy | +| 1403 | [Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1403.java) | | Easy | Greedy, Sort | +| 1402 | [Reducing Dishes](https://leetcode.com/problems/reducing-dishes/) | [Solution](../master/cpp/_1402.cpp) | | Hard | Dynamic Programming | +| 1401 | [Circle and Rectangle Overlapping](https://leetcode.com/problems/circle-and-rectangle-overlapping/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1401.java) | | Medium | Geometry | +| 1400 | [Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1400.java) | | Medium | Greedy | +| 1399 | [Count Largest Group](https://leetcode.com/problems/count-largest-group/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1399.java) | | Easy | Array | +| 1396 | [Design Underground System](https://leetcode.com/problems/design-underground-system/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1396.java) | | Medium | Design | +| 1395 | [Count Number of Teams](https://leetcode.com/problems/count-number-of-teams/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1395.java) | | Medium | Array | +| 1394 | [Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1394.java) | | Easy | Array | +| 1392 | [Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1392.java) | | Hard | String, Rolling Hash | +| 1390 | [Four Divisors](https://leetcode.com/problems/four-divisors/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1390.java) | | Medium | Math | +| 1389 | [Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1389.java) | | Easy | Array | +| 1388 | [Pizza With 3n Slices](https://leetcode.com/problems/pizza-with-3n-slices/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1388.java) | | Hard | DP | +| 1387 | [Sort Integers by The Power Value](https://leetcode.com/problems/sort-integers-by-the-power-value/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1387.java) | | Medium | Sort, Graph | +| 1386 | [Cinema Seat Allocation](https://leetcode.com/problems/cinema-seat-allocation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1386.java) | | Medium | Array, Greedy | +| 1385 | [Find the Distance Value Between Two Arrays](https://leetcode.com/problems/find-the-distance-value-between-two-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1385.java) | | Easy | Array | +| 1382 | [Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1382.java) | | Medium | Binary Search Tree | +| 1381 | [Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1381.java) | | Medium | Stack, Design | +| 1380 | [Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1380.java) | | Easy | Array | +| 1379 | [Find a Corresponding Node of a Binary Tree in a Clone of That Tree](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1379.java) | | Medium | Tree | +| 1377 | [Frog Position After T Seconds](https://leetcode.com/problems/frog-position-after-t-seconds/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1377.java) | | Hard | DFS, BFS | +| 1376 | [Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1376.java) | | Medium | DFS | +| 1375 | [Bulb Switcher III](https://leetcode.com/problems/bulb-switcher-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1375.java) | | Medium | Array | +| 1374 | [Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1374.java) | | Easy | String | +| 1373 | [Maximum Sum BST in Binary Tree](https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1373.java) | | Hard | DP, BST | +| 1372 | [Longest ZigZag Path in a Binary Tree](https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1372.java) | | Hard | DP, Tree | +| 1371 | [Find the Longest Substring Containing Vowels in Even Counts](https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1371.java) | | Medium | String | +| 1370 | [Increasing Decreasing String](https://leetcode.com/problems/increasing-decreasing-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1370.java) | | Easy | String, Sort | +| 1367 | [Linked List in Binary Tree](https://leetcode.com/problems/linked-list-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1367.java) | | Medium | DP, Linked List, Tree | +| 1366 | [Rank Teams by Votes](https://leetcode.com/problems/rank-teams-by-votes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1366.java) | | Medium | Array, Sort | +| 1365 | [How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1365.java) | | Easy | Array, HashTable | +| 1362 | [Closest Divisors](https://leetcode.com/problems/closest-divisors/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1362.java) | | Medium | Math | +| 1361 | [Validate Binary Tree Nodes](https://leetcode.com/problems/validate-binary-tree-nodes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1361.java) | | Medium | Graph +| 1360 | [Number of Days Between Two Dates](https://leetcode.com/problems/number-of-days-between-two-dates/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1360.java) | | Easy || +| 1358 | [Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1358.java) | | Medium | String | +| 1357 | [Apply Discount Every n Orders](https://leetcode.com/problems/apply-discount-every-n-orders/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1357.java) | | Medium | Design | +| 1356 | [Sort Integers by The Number of 1 Bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1356.java) | | Easy | Sort, Bit Manipulation | +| 1354 | [Construct Target Array With Multiple Sums](https://leetcode.com/problems/construct-target-array-with-multiple-sums/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1354.java) | | Hard | Greedy | +| 1353 | [Maximum Number of Events That Can Be Attended](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1353.java) | | Medium | Greedy, Sort, Segment Tree | +| 1352 | [Product of the Last K Numbers](https://leetcode.com/problems/product-of-the-last-k-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1352.java) | | Medium | Array, Design | +| 1351 | [Count Negative Numbers in a Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1351.java) | | Easy | Array, Binary Search | +| 1349 | [Maximum Students Taking Exam](https://leetcode.com/problems/maximum-students-taking-exam/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1349.java) | | Hard | Dynamic Programming | +| 1348 | [Tweet Counts Per Frequency](https://leetcode.com/problems/tweet-counts-per-frequency/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1348.java) | | Medium | Design | +| 1347 | [Minimum Number of Steps to Make Two Strings Anagram](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1347.java) | | Easy | String | +| 1346 | [Check If N and Its Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1346.java) | | Easy | Array | +| 1345 | [Jump Game IV](https://leetcode.com/problems/jump-game-iv/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1345.java) | | Hard | BFS | +| 1344 | [Angle Between Hands of a Clock](https://leetcode.com/problems/angle-between-hands-of-a-clock/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1344.java) | | Medium | Math | +| 1343 | [Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1343.java) | | Medium | Array | +| 1342 | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1342.java) | | Easy | Bit Manipulation | +| 1341 | [The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1341.java) | | Easy || +| 1339 | [Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1339.java) | | Medium | DFS, Tree | +| 1338 | [Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1338.java) | | Medium || +| 1337 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1337.java) | | Easy | String | +| 1334 | [Find the City With the Smallest Number of Neighbors at a Threshold Distance](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1334.java) | | Medium | Dijkstra's algorithm, Graph +| 1333 | [Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1333.java) | | Medium || +| 1332 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1332.java) | | Easy | String | +| 1331 | [Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1331.java) | | Easy || +| 1329 | [Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1329.java) | | Medium || +| 1325 | [Delete Leaves With a Given Value](https://leetcode.com/problems/delete-leaves-with-a-given-value/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1325.java) | | Medium | Tree | +| 1324 | [Print Words Vertically](https://leetcode.com/problems/print-words-vertically/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1324.java) | | Medium | String | +| 1323 | [Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1323.java) | | Easy | Math | +| 1317 | [Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1317.java) | | Easy || +| 1315 | [Sum of Nodes with Even-Valued Grandparent](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1315.java) | | Medium | Tree, DFS | +| 1314 | [Matrix Block Sum](https://leetcode.com/problems/matrix-block-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1314.java) | | Medium | Prefix Sum | +| 1313 | [Decompress Run-Length Encoded List](https://leetcode.com/problems/decompress-run-length-encoded-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1313.java) | | Easy | Array | +| 1305 | [All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1305.java) | | Medium || +| 1304 | [Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1304.java) | | Easy || +| 1302 | [Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1302.java) | | Medium || +| 1300 | [Sum of Mutated Array Closest to Target](https://leetcode.com/problems/sum-of-mutated-array-closest-to-target/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1300.java) | | Medium | Binary Search, Sorting | +| 1299 | [Replace Elements with Greatest Element on Right Side](https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1299.java) | | Easy || +| 1297 | [Maximum Number of Occurrences of a Substring](https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1297.java) | | Medium || +| 1296 | [Divide Array in Sets of K Consecutive Numbers](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1296.java) | | Medium || +| 1295 | [Find Numbers with Even Number of Digits](https://leetcode.com/problems/find-numbers-with-even-number-of-digits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1295.java) [Javascript](../master/javascript/_1295.js) | [:tv:](https://youtu.be/HRp8mNJvLZ0) | Easy || +| 1291 | [Sequential Digits](https://leetcode.com/problems/sequential-digits/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1291.java) | | Medium || +| 1290 | [Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1290.java) | | Easy || +| 1289 | [Minimum Falling Path Sum II](https://leetcode.com/problems/minimum-falling-path-sum-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1289.java) | | Hard | Dynamic Programming | +| 1287 | [Element Appearing More Than 25% In Sorted Array](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1287.java) | [:tv:](https://youtu.be/G74W8v2yVjY) | Easy || +| 1286 | [Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1286.java) | | Medium | Backtracking, Design | +| 1283 | [Find the Smallest Divisor Given a Threshold](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1283.java) | Medium | +| 1282 | [Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1282.java) | [:tv:](https://www.youtube.com/watch?v=wGgcRCpSAa8) | Medium || +| 1281 | [Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1281.java) | | Easy || +| 1277 | [Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1277.java) | | Medium || +| 1275 | [Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1275.java) | | Easy | Array | +| 1273 | [Delete Tree Nodes](https://leetcode.com/problems/delete-tree-nodes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1273.java) | | Medium | Dynamic Programming, DFS | +| 1271 | [Hexspeak](https://leetcode.com/problems/hexspeak/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1271.java) | | Easy || +| 1268 | [Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1268.java) | [:tv:](https://youtu.be/PLNDfB0Vg9Y) | Medium | String | +| 1267 | [Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1267.java) | | Medium || +| 1266 | [Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1266.java) | | Easy || +| 1265 | [Print Immutable Linked List in Reverse](https://leetcode.com/problems/print-immutable-linked-list-in-reverse//) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1265.java) | | Medium || +| 1261 | [Find Elements in a Contaminated Binary Tree](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1261.java) || Medium | Tree, HashTable | +| 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1260.java) | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s) | Easy || +| 1258 | [Synonymous Sentences](https://leetcode.com/problems/synonymous-sentences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1258.java) || Medium | Backtracking | +| 1257 | [Smallest Common Region](https://leetcode.com/problems/smallest-common-region/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1257.java) || Medium | Tree, HashTable, DFS, BFS | +| 1254 | [Number of Closed Islands](https://leetcode.com/problems/number-of-closed-islands/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1254.java) || Medium | BFS | +| 1252 | [Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1252.java) | | Easy || +| 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1249.java) | | Medium | String, Stack | +| 1243 | [Array Transformation](https://leetcode.com/problems/array-transformation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1243.java) | [:tv:](https://www.youtube.com/watch?v=MQ2i4T1l-Gs) | Easy || +| 1242 | [Web Crawler Multithreaded](https://leetcode.com/problems/web-crawler-multithreaded/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1242.java) | | Medium | Concurrency | +| 1237 | [Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1237.java) | | Easy || +| 1233 | [Remove Sub-Folders from the Filesystem](https://leetcode.com/problems/remove-sub-folders-from-the-filesystem/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1233.java) || Easy || +| 1232 | [Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1232.java) | [:tv:](https://www.youtube.com/watch?v=_tfiTQNZCbs) | Easy || +| 1230 | [Toss Strange Coins](https://leetcode.com/problems/toss-strange-coins/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1230.java) | | Medium |DP +| 1228 | [Missing Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1228.java) | | Easy || +| 1221 | [Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1221.java) | | Easy | Greedy | +| 1219 | [Path with Maximum Gold](https://leetcode.com/problems/path-with-maximum-gold/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1219.java) | | Medium | Backtracking | +| 1217 | [Play with Chips](https://leetcode.com/problems/play-with-chips/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1217.java) | | Easy | Array, Math, Greedy | +| 1214 | [Two Sum BSTs](https://leetcode.com/problems/two-sum-bsts/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1214.java) | | Medium | Binary Search Tree | +| 1213 | [Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1213.java) | [:tv:](https://www.youtube.com/watch?v=zceoOrHSHNQ) | Easy || +| 1209 | [Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1207.java) || Medium | Stack | +| 1207 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1207.java) | [:tv:](https://www.youtube.com/watch?v=_NYimlZY1PE) | Easy || +| 1200 | [Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1200.java) | [:tv:](https://www.youtube.com/watch?v=mH1aEjOEjcQ) | Easy || +| 1198 | [Find Smallest Common Element in All Rows](https://leetcode.com/problems/find-smallest-common-element-in-all-rows/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1198.java) | [:tv:](https://www.youtube.com/watch?v=RMiofZrTmWo) | Easy || +| 1197 | [Minimum Knight Moves](https://leetcode.com/problems/minimum-knight-moves/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1197.java) | | Medium | BFS | +| 1196 | [How Many Apples Can You Put into the Basket](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1196.java) | [:tv:](https://www.youtube.com/watch?v=UelshlMQNJM) | Easy || +| 1190 | [Reverse Substrings Between Each Pair of Parentheses](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1190.java) | | Medium | Stack | +| 1189 | [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1189.java) | [:tv:](https://youtu.be/LGgMZC0vj5s) | Easy || +| 1185 | [Day of the Week](https://leetcode.com/problems/day-of-the-week/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1185.java) | | Easy || +| 1184 | [Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1184.java) | [:tv:](https://www.youtube.com/watch?v=RFq7yA5iyhI) | Easy || +| 1182 | [Shortest Distance to Target Color](https://leetcode.com/problems/shortest-distance-to-target-color/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1182.java) || Medium | Binary Search | +| 1180 | [Count Substrings with Only One Distinct Letter](https://leetcode.com/problems/count-substrings-with-only-one-distinct-letter/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1180.java) | | Easy | Math, String | +| 1176 | [Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1176.java) | | Easy | Array, Sliding Window | +| 1175 | [Prime Arrangements](https://leetcode.com/problems/prime-arrangements/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1175.java) | | Easy | Math | +| 1171 | [Remove Zero Sum Consecutive Nodes from Linked List](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1171.java) | | Medium | LinkedList | +| 1165 | [Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1165.java) | | Easy || +| 1161 | [Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1161.java) | | Medium | Graph | +| 1160 | [Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1160.java) | | Easy || +| 1154 | [Day of the Year](https://leetcode.com/problems/day-of-the-year/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1154.java) | | Easy || +| 1152 | [Analyze User Website Visit Pattern](https://leetcode.com/problems/analyze-user-website-visit-pattern/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1152.java) | [:tv:](https://youtu.be/V510Lbtrm5s) | Medium | HashTable, Sort, Array | +| 1151 | [Minimum Swaps to Group All 1's Together](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1151.java) || Medium | Array, Sliding Window | +| 1150 | [Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1150.java) | [:tv:](https://youtu.be/-t2cdVs9cKk) | Easy || +| 1146 | [Snapshot Array](https://leetcode.com/problems/snapshot-array/) | [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_1146.js) [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1146.java) | | Medium || +| 1143 | [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1143.java) | | Medium | String, DP +| 1138 | [Alphabet Board Path](https://leetcode.com/problems/alphabet-board-path/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1138.java) | [:tv:](https://youtu.be/rk-aB4rEOyU) | Medium | HashTable, String | +| 1137 | [N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1137.java) | | Easy || +| 1136 | [Parallel Courses](https://leetcode.com/problems/parallel-courses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1136.java) | | Medium | Topological Sort +| 1134 | [Armstrong Number](https://leetcode.com/problems/armstrong-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1134.java) | [:tv:](https://www.youtube.com/watch?v=HTL7fd4HPf4) | Easy || +| 1133 | [Largest Unique Number](https://leetcode.com/problems/largest-unique-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1133.java) | [:tv:](https://youtu.be/Fecpt1YZlCs) | Easy || +| 1128 | [Number of Equivalent Domino Pairs](https://leetcode.com/problems/number-of-equivalent-domino-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1128.java) | [:tv:](https://www.youtube.com/watch?v=7EpEEHAAxyw) | Easy || +| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1122.java) | | Easy || +| 1170 | [Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1170.java) | | Easy || +| 1120 | [Maximum Average Subtree](https://leetcode.com/problems/maximum-average-subtree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1120.java) | | Medium | Tree, DFS +| 1119 | [Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1119.java) | [:tv:](https://www.youtube.com/watch?v=6KCBrIWEauw) | Easy || +| 1118 | [Number of Days in a Month](https://leetcode.com/problems/number-of-days-in-a-month/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1118.java) | | Easy || +| 1114 | [Print in Order](https://leetcode.com/problems/print-in-order/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1114.java) | | Easy || +| 1110 | [Delete Nodes And Return Forest](https://leetcode.com/problems/delete-nodes-and-return-forest/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1110.java) | | Medium | Tree, DFS, BFS | +| 1108 | [Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1108.java) | [:tv:](https://www.youtube.com/watch?v=FP0Na-pL0qk) | Easy || +| 1105 | [Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1105.java) || Medium | DP +| 1104 | [Path In Zigzag Labelled Binary Tree](https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1104.java) | | Medium | Math, Tree | +| 1103 | [Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1103.java) | | Easy | Math | +| 1100 | [Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1100.java) | | Medium | String, Sliding Window | +| 1099 | [Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1099.java) | [:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI) | Easy || +| 1094 | [Car Pooling](https://leetcode.com/problems/car-pooling/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1094.java) | | Medium | Array, Sorting, Heap, Simulation, Prefix Sum | +| 1090 | [Largest Values From Labels](https://leetcode.com/problems/largest-values-from-labels/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1090.java) | [:tv:](https://youtu.be/E0OkE3G95vU) | Medium | HashTable, Greedy | +| 1091 | [Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1091.java) | | Medium | BFS | +| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1089.java) | | Easy || +| 1087 | [Brace Expansion](https://leetcode.com/problems/brace-expansion/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1087.java) | | Medium | Backtracking | +| 1086 | [High Five](https://leetcode.com/problems/high-five/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1086.java) | [:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc) | Easy || +| 1085 | [Sum of Digits in the Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1085.java) | [:tv:](https://www.youtube.com/watch?v=GKYmPuHZpQg) | Easy || +| 1080 | [Insufficient Nodes in Root to Leaf Paths](https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1080.java) | | Medium | Tree, DFS +| 1079 | [Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1079.java) | | Medium || +| 1078 | [Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1078.java) | | Easy || +| 1071 | [Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1071.java) | | Easy || +| 1066 | [Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1066.java) | | Medium | Backtracking, DP | +| 1065 | [Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1065.java) | | Medium || +| 1062 | [Longest Repeating Substring](https://leetcode.com/problems/longest-repeating-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1062.java) | | Medium | String, Binary Search, DP, Rolling Hash, Suffix Array, Hash Function | +| 1061 | [Lexicographically Smallest Equivalent String](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1061.java) | [:tv:](https://youtu.be/HvCaMw58_94) | Medium | Union Find +| 1060 | [Missing Element in Sorted Array](https://leetcode.com/problems/missing-element-in-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1060.java) | | Medium ||Binary Search +| 1059 | [All Paths from Source Lead to Destination](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1059.java) | | Medium | DFS +| 1057 | [Campus Bikes](https://leetcode.com/problems/campus-bikes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1057.java) | | Medium ||Greedy, Sort +| 1056 | [Confusing Number](https://leetcode.com/problems/confusing-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1056.java) | | Easy || +| 1055 | [Fixed Point](https://leetcode.com/problems/fixed-point/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1055.java) | | Easy || +| 1051 | [Height Checker](https://leetcode.com/problems/height-checker/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1051.java) | | Easy || +| 1047 | [Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1047.java) | | Easy || +| 1046 | [Last Stone Weight](https://leetcode.com/problems/last-stone-weight/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1046.java) | [:tv:](https://youtu.be/IfElFyaEV8s) | Easy || +| 1043 | [Partition Array for Maximum Sum](https://leetcode.com/problems/partition-array-for-maximum-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1043.java) | | Medium | DP | +| 1038 | [Binary Search Tree to Greater Sum Tree](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1038.java) | | Medium | DFS, tree | +| 1037 | [Valid Boomerang](https://leetcode.com/problems/valid-boomerang/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1037.java) | | Easy | Math | +| 1034 | [Coloring A Border](https://leetcode.com/problems/coloring-a-border/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1034.java) | | Medium | DFS | +| 1033 | [Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1033.java) | | Easy | Math | +| 1030 | [Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1030.java) | | Easy | +| 1029 | [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1029.java) | | Easy | +| 1026 | [Maximum Difference Between Node and Ancestor](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1026.java) | | Medium | Tree, DFS, Binary Tree | +| 1025 | [Divisor Game](https://leetcode.com/problems/divisor-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1025.java) | | Easy | Math, DP, Brainteaser, Game Theory | +| 1024 | [Video Stitching](https://leetcode.com/problems/video-stitching/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1024.java) | | Medium | Array, DP, Greedy | +| 1022 | [Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1022.java) | | Easy | +| 1021 | [Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1021.java) | | Easy | +| 1020 | [Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1020.java) | | Medium | Graph, DFS, BFS, recursion | +| 1019 | [Next Greater Node In Linked List](https://leetcode.com/problems/next-greater-node-in-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1019.java) | | Medium | Linked List, Stack | +| 1018 | [Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1018.java) | | Easy | +| 1014 | [Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1014.java) | | Medium | +| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1013.java) | | Easy | +| 1011 | [Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1011.java) | | Medium | Binary Search | +| 1010 | [Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1010.java) | | Easy | +| 1009 | [Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1009.java) | | Easy | +| 1008 | [Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1008.java) | | Medium | Recursion +| 1005 | [Maximize Sum Of Array After K Negations](https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1005.java) | [:tv:](https://youtu.be/spiwTAuz1_4) | Easy | +| 1004 | [Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1004.java) | [:tv:](https://youtu.be/nKhteIRZ2Ok) | Medium | Two Pointers, Sliding Window +| 1003 | [Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1003.java) | | Medium | +| 1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1002.java) | | Easy | \ No newline at end of file diff --git a/paginated_contents/algorithms/3rd_thousand/README.md b/paginated_contents/algorithms/3rd_thousand/README.md new file mode 100644 index 0000000000..05470a0328 --- /dev/null +++ b/paginated_contents/algorithms/3rd_thousand/README.md @@ -0,0 +1,284 @@ +| # | Title | Solutions | Video | Difficulty | Tag +|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------|------------------------------------------|---------------------------------------------------------------------- +| 2996 | [Smallest Missing Integer Greater Than Sequential Prefix Sum](https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2996.java) | | Easy | +| 2980 | [Check if Bitwise OR Has Trailing Zeros](https://leetcode.com/problems/check-if-bitwise-or-has-trailing-zeros/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2980.java) | | Easy | Bit Manipulation +| 2976 | [Minimum Cost to Convert String I](https://leetcode.com/problems/minimum-cost-to-convert-string-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2976.java) | | Medium | Graph, Shortest Path +| 2974 | [Minimum Number Game](https://leetcode.com/problems/minimum-number-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2974.java) | | Easy | +| 2970 | [Count the Number of Incremovable Subarrays I](https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2970.java) | | Easy | +| 2965 | [Find Missing and Repeated Values](https://leetcode.com/problems/find-missing-and-repeated-values/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2965.java) | | Easy | +| 2960 | [Count Tested Devices After Test Operations](https://leetcode.com/problems/count-tested-devices-after-test-operations/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2960.java) | | Easy | +| 2956 | [Find Common Elements Between Two Arrays](https://leetcode.com/problems/find-common-elements-between-two-arrays/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2956.java) | | Easy | +| 2951 | [Find the Peaks](https://leetcode.com/problems/find-the-peaks/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2951.java) | | Easy | +| 2946 | [Matrix Similarity After Cyclic Shifts](https://leetcode.com/problems/matrix-similarity-after-cyclic-shifts/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2946.java) | | Easy | +| 2942 | [Find Words Containing Character](https://leetcode.com/problems/find-words-containing-character/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2942.java) | | Easy | +| 2937 | [Make Three Strings Equal](https://leetcode.com/problems/make-three-strings-equal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2937.java) | | Easy | +| 2932 | [Maximum Strong Pair XOR I](https://leetcode.com/problems/maximum-strong-pair-xor-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2932.java) | | Easy | +| 2928 | [Distribute Candies Among Children I](https://leetcode.com/problems/distribute-candies-among-children-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2928.java) | | Easy | +| 2923 | [Find Champion I](https://leetcode.com/problems/find-champion-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2923.java) | | Easy | +| 2917 | [Find the K-or of an Array](https://leetcode.com/problems/find-the-k-or-of-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2917.java) | | Easy | +| 2913 | [Subarrays Distinct Element Sum of Squares I](https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2913.java) | | Easy | +| 2908 | [Minimum Sum of Mountain Triplets I](https://leetcode.com/problems/minimum-sum-of-mountain-triplets-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2908.java) | | Easy | +| 2903 | [Find Indices With Index and Value Difference I](https://leetcode.com/problems/find-indices-with-index-and-value-difference-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2903.java) | | Easy | +| 2900 | [Longest Unequal Adjacent Groups Subsequence I](https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2900.java) | | Easy | +| 2899 | [Last Visited Integers](https://leetcode.com/problems/last-visited-integers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2899.java) | | Easy | +| 2894 | [Divisible and Non-divisible Sums Difference](https://leetcode.com/problems/divisible-and-non-divisible-sums-difference/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2894.java) | | Easy | +| 2873 | [Maximum Value of an Ordered Triplet I](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2873.java) | | Easy | +| 2869 | [Minimum Operations to Collect Elements](https://leetcode.com/problems/minimum-operations-to-collect-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2869.java) | | Easy | +| 2864 | [Maximum Odd Binary Number](https://leetcode.com/problems/maximum-odd-binary-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2864.java) | | Easy |Greedy +| 2859 | [Sum of Values at Indices With K Set Bits](https://leetcode.com/problems/sum-of-values-at-indices-with-k-set-bits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2859.java) | | Easy | +| 2855 | [Minimum Right Shifts to Sort the Array](https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2855.java) | | Easy | +| 2848 | [Points That Intersect With Cars](https://leetcode.com/problems/points-that-intersect-with-cars/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2848.java) | | Easy | +| 2843 | [Count Symmetric Integers](https://leetcode.com/problems/count-symmetric-integers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2843.java) | | Easy | +| 2839 | [Check if Strings Can be Made Equal With Operations I](https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2839.java) | | Easy | +| 2833 | [Furthest Point From Origin](https://leetcode.com/problems/furthest-point-from-origin/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2833.java) | | Easy | +| 2828 | [Check if a String Is an Acronym of Words](https://leetcode.com/problems/check-if-a-string-is-an-acronym-of-words/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2828.java) | | Easy | +| 2824 | [Count Pairs Whose Sum is Less than Target](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2824.java) | | Easy | +| 2815 | [Max Pair Sum in an Array](https://leetcode.com/problems/max-pair-sum-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2815.java) | | Easy | +| 2812 | [Find the Safest Path in a Grid](https://leetcode.com/problems/find-the-safest-path-in-a-grid/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2812.java) | | Medium |BFS +| 2810 | [Faulty Keyboard](https://leetcode.com/problems/faulty-keyboard/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2810.java) | | Easy | +| 2806 | [Account Balance After Rounded Purchase](https://leetcode.com/problems/account-balance-after-rounded-purchase/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2806.java) | | Easy | +| 2798 | [Number of Employees Who Met the Target](https://leetcode.com/problems/number-of-employees-who-met-the-target/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2798.java) | | Easy | +| 2788 | [Split Strings by Separator](https://leetcode.com/problems/split-strings-by-separator/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2788.java) | | Easy | +| 2784 | [Check if Array is Good](https://leetcode.com/problems/check-if-array-is-good/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2784.java) | | Easy | +| 2778 | [Sum of Squares of Special Elements](https://leetcode.com/problems/sum-of-squares-of-special-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2778.java) | | Easy | +| 2769 | [Find the Maximum Achievable Number](https://leetcode.com/problems/find-the-maximum-achievable-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2769.java) | | Easy | +| 2765 | [Longest Alternating Subarray](https://leetcode.com/problems/longest-alternating-subarray/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2765.java) | | Easy | +| 2760 | [Longest Even Odd Subarray With Threshold](https://leetcode.com/problems/longest-even-odd-subarray-with-threshold/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2760.java) | | Easy | +| 2751 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2751.java) | | Hard | Stack, Simulation +| 2748 | [Number of Beautiful Pairs](https://leetcode.com/problems/number-of-beautiful-pairs/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2748.java) | | Easy | +| 2744 | [Find Maximum Number of String Pairs](https://leetcode.com/problems/find-maximum-number-of-string-pairs/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2744.java) | | Easy | +| 2739 | [Total Distance Traveled](https://leetcode.com/problems/total-distance-traveled/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2739.java) | | Easy | +| 2733 | [Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2733.java) | | Easy | +| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2729.java) | | Easy | +| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2728.java) | | Easy | +| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2717.java) | | Easy | +| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2716.java) | [:tv:](https://youtu.be/aMJ3T0K8LjI) | Easy | +| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2710.java) | | Easy | +| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2706.java) | | Easy | +| 2697 | [Lexicographically Smallest Palindrome](https://leetcode.com/problems/lexicographically-smallest-palindrome/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2697.java) | | Easy | +| 2696 | [Minimum String Length After Removing Substrings](https://leetcode.com/problems/minimum-string-length-after-removing-substrings/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2696.java) | | Easy | +| 2689 | [Extract Kth Character From The Rope Tree](https://leetcode.com/problems/extract-kth-character-from-the-rope-tree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2689.java) | | Easy | Tree, DFS +| 2682 | [Find the Losers of the Circular Game](https://leetcode.com/problems/find-the-losers-of-the-circular-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2682.java) | | Easy | +| 2673 | [Make Costs of Paths Equal in a Binary Tree](https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2673.java) | | Medium |Tree, DFS, Greedy +| 2678 | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2678.java) | | Easy | +| 2670 | [Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2670.java) | | Easy | +| 2660 | [Determine the Winner of a Bowling Game](https://leetcode.com/problems/determine-the-winner-of-a-bowling-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2660.java) | | Easy | +| 2656 | [Maximum Sum With Exactly K Elements](https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2656.java) | | Easy | +| 2652 | [Sum Multiples](https://leetcode.com/problems/sum-multiples/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2652.java) | | Easy | +| 2651 | [Calculate Delayed Arrival Time](https://leetcode.com/problems/calculate-delayed-arrival-time/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2651.java) | | Easy | +| 2644 | [Find the Maximum Divisibility Score](https://leetcode.com/problems/find-the-maximum-divisibility-score/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2644.java) | | Easy | +| 2643 | [Row With Maximum Ones](https://leetcode.com/problems/row-with-maximum-ones/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2643.java) | | Easy | +| 2641 | [Cousins in Binary Tree II](https://leetcode.com/problems/cousins-in-binary-tree-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2641.java) | | Medium |Tree, BFS, HashTable +| 2639 | [Find the Width of Columns of a Grid](https://leetcode.com/problems/find-the-width-of-columns-of-a-grid/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2639.java) | | Easy | +| 2614 | [Prime In Diagonal](https://leetcode.com/problems/prime-in-diagonal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2614.java) | | Easy | +| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2605.java) | | Easy | +| 2609 | [Find the Longest Balanced Substring of a Binary String](https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2609.java) | | Easy | +| 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2600.java) | | Easy | +| 2596 | [Check Knight Tour Configuration](https://leetcode.com/problems/check-knight-tour-configuration/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2596.java) | [:tv:](https://youtu.be/OBht8NT_09c) | Medium | +| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2595.java) | | Easy | +| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2591.java) | | Easy | +| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2586.java) | | Easy | +| 2583 | [Kth Largest Sum in a Binary Tree](https://leetcode.com/problems/kth-largest-sum-in-a-binary-tree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2583.java) | | Medium | +| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2582.java) | | Easy | +| 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2578.java) | | Easy | +| 2574 | [Left and Right Sum Differences](https://leetcode.com/problems/left-and-right-sum-differences/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2574.java) | | Easy | +| 2570 | [Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2570.java) | | Easy | +| 2566 | [Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2566.java) | | Easy | +| 2562 | [Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2562.java) | | Easy | +| 2559 | [Count Vowel Strings in Ranges](https://leetcode.com/problems/count-vowel-strings-in-ranges/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2559.java) | | Medium | +| 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2558.java) | | Easy | +| 2554 | [Maximum Number of Integers to Choose From a Range I](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2554.java) | | Medium | +| 2553 | [Separate the Digits in an Array](https://leetcode.com/problems/separate-the-digits-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2553.java) | | Easy | +| 2549 | [Count Distinct Numbers on Board](https://leetcode.com/problems/count-distinct-numbers-on-board/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2549.java) || Easy | +| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2544.java) | [:tv:](https://youtu.be/IFRYDmhEWGw) | Easy | +| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2540.java) || Easy | +| 2536 | [Increment Submatrices by One](https://leetcode.com/problems/increment-submatrices-by-one/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2536.java) || Medium | +| 2535 | [Difference Between Element Sum and Digit Sum of an Array](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2535.java) || Easy | +| 2530 | [Maximal Score After Applying K Operations](https://leetcode.com/problems/maximal-score-after-applying-k-operations/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2530.java) | [:tv:](https://youtu.be/nsOipmYbRlc) | Medium | +| 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2529.java) | [:tv:](https://youtu.be/4uhvXmOp5Do) | Easy || +| 2525 | [Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2525.java) | [:tv:](https://youtu.be/dIciftyQXHo) | Easy || +| 2520 | [Count the Digits That Divide a Number](https://leetcode.com/problems/count-the-digits-that-divide-a-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2520.java) | [:tv:](https://youtu.be/7SHHsS5kPJ8) | Easy || +| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2515.java) || Easy || +| 2511 | [Maximum Enemy Forts That Can Be Captured](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2511.java) || Easy | Array, Two Pointers +| 2506 | [Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2506.java) || Easy || +| 2501 | [Longest Square Streak in an Array](https://leetcode.com/problems/longest-square-streak-in-an-array) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2501.java) || Easy || +| 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2500.java) || Easy || +| 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2496.java) || Easy || +| 2492 | [Minimum Score of a Path Between Two Cities](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2492.java) || Medium | Union Find +| 2490 | [Circular Sentence](https://leetcode.com/problems/circular-sentence/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2490.java) || Easy | +| 2481 | [Minimum Cuts to Divide a Circle](https://leetcode.com/problems/minimum-cuts-to-divide-a-circle/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2481.java) || Easy | +| 2487 | [Remove Nodes From Linked List](https://leetcode.com/problems/remove-nodes-from-linked-list/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2487.java) || Medium | LinkedList, Stack +| 2485 | [Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2485.java) || Easy || +| 2475 | [Number of Unequal Triplets in Array](https://leetcode.com/problems/number-of-unequal-triplets-in-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2475.java) || Easy || +| 2473 | [Minimum Cost to Buy Apples](https://leetcode.com/problems/minimum-cost-to-buy-apples/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2473.java) || Medium | Graph, Shortest Path, PriorityQueue/Heap +| 2467 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2469.java) || Easy || +| 2465 | [Number of Distinct Averages](https://leetcode.com/problems/number-of-distinct-averages/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2465.java) || Easy || +| 2460 | [Apply Operations to an Array](https://leetcode.com/problems/apply-operations-to-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2460.java) || Easy || +| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2455.java) || Easy || +| 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2451.java) || Easy | +| 2446 | [Determine if Two Events Have Conflict](https://leetcode.com/problems/determine-if-two-events-have-conflict/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2446.java) || Easy | +| 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2441.java) || Easy || +| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2437.java) | | Easy || +| 2433 | [Find The Original Array of Prefix Xor](https://leetcode.com/problems/find-the-original-array-of-prefix-xor/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2433.java) | [:tv:](https://youtu.be/idcT-p_DDrI) | Medium || +| 2432 | [The Employee That Worked on the Longest Task](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2432.java) || Easy || +| 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2427.java) || Easy || +| 2423 | [Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2423.java) || Easy || +| 2418 | [Sort the People](https://leetcode.com/problems/sort-the-people/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2418.java) || Easy || +| 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2413.java) || Easy || +| 2409 | [Count Days Spent Together](https://leetcode.com/problems/count-days-spent-together/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2409.java) || Easy || +| 2404 | [Most Frequent Even Element](https://leetcode.com/problems/most-frequent-even-element/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2404.java) || Easy || +| 2399 | [Check Distances Between Same Letters](https://leetcode.com/problems/check-distances-between-same-letters/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2399.java) || Medium || +| 2395 | [Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2395.java) || Easy || +| 2392 | [Build a Matrix With Conditions](https://leetcode.com/problems/build-a-matrix-with-conditions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2392.java) || Hard | Topological Sot, Graph, Matrix +| 2389 | [Longest Subsequence With Limited Sum](https://leetcode.com/problems/longest-subsequence-with-limited-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2389.java) || Easy | +| 2385 | [Amount of Time for Binary Tree to Be Infected](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2380.java) || Medium | BFS +| 2380 | [Time Needed to Rearrange a Binary String](https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2380.java) || Medium || +| 2379 | [Minimum Recolors to Get K Consecutive Black Blocks](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2379.java) || Easy || +| 2373 | [Largest Local Values in a Matrix](https://leetcode.com/problems/largest-local-values-in-a-matrix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2373.java) || Easy || +| 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2367.java) || Easy || +| 2363 | [Merge Similar Items](https://leetcode.com/problems/merge-similar-items/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2363.java) || Easy || +| 2357 | [Make Array Zero by Subtracting Equal Amounts](https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2357.java) || Easy || +| 2352 | [Equal Row and Column Pairs](https://leetcode.com/problems/equal-row-and-column-pairs/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2352.java) || Medium || +| 2351 | [First Letter to Appear Twice](https://leetcode.com/problems/first-letter-to-appear-twice/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2351.java) || Easy || +| 2347 | [Best Poker Hand](https://leetcode.com/problems/best-poker-hand/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2347.java) || Easy || +| 2341 | [Maximum Number of Pairs in Array](https://leetcode.com/problems/maximum-number-of-pairs-in-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2341.java) || Easy || +| 2340 | [Minimum Adjacent Swaps to Make a Valid Array](https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2340.java) || Medium | Greedy +| 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2335.java) || Easy || +| 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2331.java) || Easy || +| 2326 | [Spiral Matrix IV](https://leetcode.com/problems/spiral-matrix-iv/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2326.java) || Medium || +| 2325 | [Decode the Message](https://leetcode.com/problems/decode-the-message/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2325.java) || Easy || +| 2319 | [Check if Matrix Is X-Matrix](https://leetcode.com/problems/check-if-matrix-is-x-matrix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2319.java) || Easy || +| 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2316.java) || Medium | Union Find +| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2315.java) || Easy || +| 2309 | [Greatest English Letter in Upper and Lower Case](https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2309.java) || Easy || +| 2303 | [Calculate Amount Paid in Taxes](https://leetcode.com/problems/calculate-amount-paid-in-taxes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2303.java) || Easy || +| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2300.java) || Medium | Binary Search +| 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2299.java) || Easy || +| 2293 | [Min Max Game](https://leetcode.com/problems/min-max-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2293.java) || Easy || +| 2288 | [Apply Discount to Prices](https://leetcode.com/problems/apply-discount-to-prices/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2288.java) || Medium || +| 2287 | [Rearrange Characters to Make Target String](https://leetcode.com/problems/rearrange-characters-to-make-target-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2288.java) || Easy || +| 2284 | [Sender With Largest Word Count](https://leetcode.com/problems/sender-with-largest-word-count/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2284.java) || Medium || +| 2283 | [Check if Number Has Equal Digit Count and Digit Value](https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2283.java) || Easy || +| 2279 | [Maximum Bags With Full Capacity of Rocks](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2279.java) || Medium || +| 2278 | [Percentage of Letter in String](https://leetcode.com/problems/percentage-of-letter-in-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2278.java) || Easy || +| 2273 | [Find Resultant Array After Removing Anagrams](https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2273.java) || Easy || +| 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2270.java) || Medium || +| 2269 | [Find the K-Beauty of a Number](https://leetcode.com/problems/find-the-k-beauty-of-a-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2269.java) || Easy || +| 2265 | [Count Nodes Equal to Average of Subtree](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2265.java) || Medium | Tree, DFS +| 2264 | [Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2264.java) || Easy || +| 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2260.java) || Medium || +| 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2259.java) || Easy || +| 2256 | [Minimum Average Difference](https://leetcode.com/problems/minimum-average-difference/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2256.java) || Medium || +| 2255 | [Count Prefixes of a Given String](https://leetcode.com/problems/count-prefixes-of-a-given-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2255.java) || Easy || +| 2248 | [Intersection of Multiple Arrays](https://leetcode.com/problems/intersection-of-multiple-arrays/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2248.java) || Easy || +| 2244 | [Minimum Rounds to Complete All Tasks](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2244.java) || Medium || +| 2243 | [Calculate Digit Sum of a String](https://leetcode.com/problems/calculate-digit-sum-of-a-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2243.java) || Easy || +| 2239 | [Find Closest Number to Zero](https://leetcode.com/problems/find-closest-number-to-zero/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2239.java) || Easy || +| 2236 | [Root Equals Sum of Children](https://leetcode.com/problems/root-equals-sum-of-children/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2236.java) || Easy || +| 2235 | [Add Two Integers](https://leetcode.com/problems/add-two-integers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2235.java) | [:tv:](https://youtu.be/Qubhoqoks6I) | Easy || +| 2231 | [Largest Number After Digit Swaps by Parity](https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2231.java) || Easy || +| 2229 | [Check if an Array Is Consecutive](https://leetcode.com/problems/check-if-an-array-is-consecutive/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2229.java) || Easy || +| 2224 | [Minimum Number of Operations to Convert Time](https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2224.java) || Easy || +| 2220 | [Minimum Bit Flips to Convert Number](https://leetcode.com/problems/minimum-bit-flips-to-convert-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2220.java) || Easy || +| 2215 | [Find the Difference of Two Arrays](https://leetcode.com/problems/find-the-difference-of-two-arrays/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2215.java) || Easy || +| 2210 | [Count Hills and Valleys in an Array](https://leetcode.com/problems/count-hills-and-valleys-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2210.java) || Easy || +| 2208 | [Minimum Operations to Halve Array Sum](https://leetcode.com/problems/minimum-operations-to-halve-array-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2208.java) || Medium || +| 2206 | [Divide Array Into Equal Pairs](https://leetcode.com/problems/divide-array-into-equal-pairs/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2206.java) || Easy || +| 2201 | [Count Artifacts That Can Be Extracted](https://leetcode.com/problems/count-artifacts-that-can-be-extracted/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2201.java) || Medium || +| 2200 | [Find All K-Distant Indices in an Array](https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2200.java) || Easy || +| 2196 | [Create Binary Tree From Descriptions](https://leetcode.com/problems/create-binary-tree-from-descriptions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2196.java) || Medium | HashTable, Tree +| 2194 | [Cells in a Range on an Excel Sheet](https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2194.java) || Easy || +| 2192 | [All Ancestors of a Node in a Directed Acyclic Graph](https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2192.java) || Medium | Topological Sort, Graph +| 2191 | [Sort the Jumbled Numbers](https://leetcode.com/problems/sort-the-jumbled-numbers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2191.java) || Medium | Array, Soring +| 2190 | [Most Frequent Number Following Key In an Array](https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2190.java) || Easy || +| 2186 | [Minimum Number of Steps to Make Two Strings Anagram II](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2186.java) || Medium || +| 2185 | [Counting Words With a Given Prefix](https://leetcode.com/problems/counting-words-with-a-given-prefix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2185.java) || Easy || +| 2182 | [Construct String With Repeat Limit](https://leetcode.com/problems/construct-string-with-repeat-limit/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2182.java) || Medium || +| 2181 | [Merge Nodes in Between Zeros](https://leetcode.com/problems/merge-nodes-in-between-zeros/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2181.java) || Medium || +| 2180 | [Count Integers With Even Digit Sum](https://leetcode.com/problems/count-integers-with-even-digit-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2180.java) || Easy || +| 2177 | [Find Three Consecutive Integers That Sum to a Given Number](https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2177.java) || Medium || +| 2176 | [Count Equal and Divisible Pairs in an Array](https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2176.java) || Easy || +| 2169 | [Count Operations to Obtain Zero](https://leetcode.com/problems/count-operations-to-obtain-zero/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2169.java) || Easy || +| 2166 | [Design Bitset](https://leetcode.com/problems/design-bitset/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2166.java) || Medium || +| 2165 | [Smallest Value of the Rearranged Number](https://leetcode.com/problems/smallest-value-of-the-rearranged-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2165.java) || Medium || +| 2164 | [Sort Even and Odd Indices Independently](https://leetcode.com/problems/sort-even-and-odd-indices-independently/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2164.java) || Easy || +| 2161 | [Partition Array According to Given Pivot](https://leetcode.com/problems/partition-array-according-to-given-pivot/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2161.java) || Medium || +| 2160 | [Minimum Sum of Four Digit Number After Splitting Digits](https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2160.java) || Easy || +| 2156 | [Find Substring With Given Hash Value](https://leetcode.com/problems/find-substring-with-given-hash-value/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2156.java) || Medium || +| 2155 | [All Divisions With the Highest Score of a Binary Array](https://leetcode.com/problems/all-divisions-with-the-highest-score-of-a-binary-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2155.java) || Medium || +| 2154 | [Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2154.java) || Easy || +| 2150 | [Find All Lonely Numbers in the Array](https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2150.java) || Medium || +| 2149 | [Rearrange Array Elements by Sign](https://leetcode.com/problems/rearrange-array-elements-by-sign/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2149.java) || Medium || +| 2148 | [Count Elements With Strictly Smaller and Greater Elements](https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2148.java) || Easy || +| 2144 | [Minimum Cost of Buying Candies With Discount](https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2144.java) || Easy || +| 2139 | [Minimum Moves to Reach Target Score](https://leetcode.com/problems/minimum-moves-to-reach-target-score/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2139.java) || Medium || +| 2138 | [Divide a String Into Groups of Size k](https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2138.java) || Easy || +| 2135 | [Count Words Obtained After Adding a Letter](https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2135.java) || Medium || +| 2134 | [Minimum Swaps to Group All 1's Together II](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2134.java) || Medium |Array, Sliding Window +| 2133 | [Check if Every Row and Column Contains All Numbers](https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2133.java) || Easy || +| 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2130.java) || Medium || +| 2129 | [Capitalize the Title](https://leetcode.com/problems/capitalize-the-title/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2129.java) || Easy || +| 2126 | [Destroying Asteroids](https://leetcode.com/problems/destroying-asteroids/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2126.java) || Medium || +| 2125 | [Number of Laser Beams in a Bank](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2125.java) || Medium || +| 2124 | [Check if All A's Appears Before All B's](https://leetcode.com/problems/check-if-all-as-appears-before-all-bs/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2124.java) || Easy || +| 2120 | [Execution of All Suffix Instructions Staying in a Grid](https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2120.java) || Medium || +| 2119 | [A Number After a Double Reversal](https://leetcode.com/problems/a-number-after-a-double-reversal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2119.java) || Easy || +| 2116 | [Check if a Parentheses String Can Be Valid](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2116.java) || Medium || +| 2115 | [Find All Possible Recipes from Given Supplies](https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2115.java) || Medium | Topological Sort, HashTable +| 2114 | [Maximum Number of Words Found in Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2114.java) || Easy || +| 2110 | [Number of Smooth Descent Periods of a Stock](https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2110.java) || Medium || +| 2109 | [Adding Spaces to a String](https://leetcode.com/problems/adding-spaces-to-a-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2109.java) || Medium || +| 2108 | [Find First Palindromic String in the Array](https://leetcode.com/problems/find-first-palindromic-string-in-the-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2108.java) || Easy || +| 2103 | [Rings and Rods](https://leetcode.com/problems/rings-and-rods/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2103.java) || Easy || +| 2099 | [Find Subsequence of Length K With the Largest Sum](https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2099.java) || Easy || +| 2096 | [Step-By-Step Directions From a Binary Tree Node to Another](https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2096.java) || Medium | DFS, Tree +| 2095 | [Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2095.java) || Medium || +| 2094 | [Finding 3-Digit Even Numbers](https://leetcode.com/problems/finding-3-digit-even-numbers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2094.java) || Easy || +| 2091 | [Removing Minimum and Maximum From Array](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2091.java) || Medium || +| 2090 | [K Radius Subarray Averages](https://leetcode.com/problems/k-radius-subarray-averages/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2090.java) || Medium || +| 2089 | [Find Target Indices After Sorting Array](https://leetcode.com/problems/find-target-indices-after-sorting-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2089.java) || Easy || +| 2086 | [Minimum Number of Buckets Required to Collect Rainwater from Houses](https://leetcode.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2086.java) || Medium || +| 2085 | [Count Common Words With One Occurrence](https://leetcode.com/problems/count-common-words-with-one-occurrence/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2085.java) || Easy || +| 2080 | [Range Frequency Queries](https://leetcode.com/problems/range-frequency-queries/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2080.java) || Medium | Array, Binary Search | +| 2079 | [Watering Plants](https://leetcode.com/problems/watering-plants/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2079.java) || Medium || +| 2078 | [Two Furthest Houses With Different Colors](https://leetcode.com/problems/two-furthest-houses-with-different-colors/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2078.java) || Easy || +| 2076 | [Process Restricted Friend Requests](https://leetcode.com/problems/process-restricted-friend-requests/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2076.java) || Hard || +| 2075 | [Decode the Slanted Ciphertext](https://leetcode.com/problems/decode-the-slanted-ciphertext/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2075.java) || Medium || +| 2074 | [Reverse Nodes in Even Length Groups](https://leetcode.com/problems/reverse-nodes-in-even-length-groups/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2074.java) || Medium || +| 2073 | [Time Needed to Buy Tickets](https://leetcode.com/problems/time-needed-to-buy-tickets/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2073.java) || Easy || +| 2070 | [Most Beautiful Item for Each Query](https://leetcode.com/problems/most-beautiful-item-for-each-query/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2070.java) || Medium || +| 2068 | [Check Whether Two Strings are Almost Equivalent](https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2068.java) || Easy || +| 2063 | [Vowels of All Substrings](https://leetcode.com/problems/vowels-of-all-substrings/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2063.java) || Medium || +| 2062 | [Count Vowel Substrings of a String](https://leetcode.com/problems/count-vowel-substrings-of-a-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2062.java) || Easy || +| 2058 | [Find the Minimum and Maximum Number of Nodes Between Critical Points](https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2058.java) || Medium || +| 2057 | [Smallest Index With Equal Value](https://leetcode.com/problems/smallest-index-with-equal-value/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2057.java) || Easy || +| 2055 | [Plates Between Candles](https://leetcode.com/problems/plates-between-candles/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2055.java) || Medium || +| 2054 | [Two Best Non-Overlapping Events](https://leetcode.com/problems/two-best-non-overlapping-events/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2054.java) || Medium || +| 2053 | [Kth Distinct String in an Array](https://leetcode.com/problems/kth-distinct-string-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2053.java) || Easy || +| 2050 | [Parallel Courses III](https://leetcode.com/problems/parallel-courses-iii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2050.java) || Hard || +| 2049 | [Count Nodes With the Highest Score](https://leetcode.com/problems/count-nodes-with-the-highest-score/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2049.java) || Medium | DFS, Tree +| 2048 | [Next Greater Numerically Balanced Number](https://leetcode.com/problems/next-greater-numerically-balanced-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2048.java) || Medium || +| 2047 | [Number of Valid Words in a Sentence](https://leetcode.com/problems/number-of-valid-words-in-a-sentence/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2047.java) || Easy || +| 2044 | [Count Number of Maximum Bitwise-OR Subsets](https://leetcode.com/problems/count-number-of-maximum-bitwise-or-subsets/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2044.java) || Medium || +| 2043 | [Simple Bank System](https://leetcode.com/problems/simple-bank-system/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2043.java) || Medium || +| 2042 | [Check if Numbers Are Ascending in a Sentence](https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2042.java) || Easy || +| 2039 | [The Time When the Network Becomes Idle](https://leetcode.com/problems/the-time-when-the-network-becomes-idle/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2039.java) || Medium || +| 2038 | [Remove Colored Pieces if Both Neighbors are the Same Color](https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2038.java) || Medium || +| 2037 | [Minimum Number of Moves to Seat Everyone](https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2037.java) || Easy || +| 2034 | [Stock Price Fluctuation](https://leetcode.com/problems/stock-price-fluctuation/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2034.java) || Medium || +| 2033 | [Minimum Operations to Make a Uni-Value Grid](https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2033.java) || Medium || +| 2032 | [Two Out of Three](https://leetcode.com/problems/two-out-of-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2032.java) || Easy || +| 2028 | [Find Missing Observations](https://leetcode.com/problems/find-missing-observations/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2028.java) || Medium || +| 2027 | [Minimum Moves to Convert String](https://leetcode.com/problems/minimum-moves-to-convert-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2027.java) || Easy || +| 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2024.java) || Medium || +| 2023 | [Number of Pairs of Strings With Concatenation Equal to Target](https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2023.java) || Medium || +| 2022 | [Convert 1D Array Into 2D Array](https://leetcode.com/problems/convert-1d-array-into-2d-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2022.java) || Easy || +| 2018 | [Check if Word Can Be Placed In Crossword](https://leetcode.com/problems/check-if-word-can-be-placed-in-crossword/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2018.java) || Medium || +| 2017 | [Grid Game](https://leetcode.com/problems/grid-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2017.java) || Medium | Array, Matrix, Prefix Sum | +| 2016 | [Maximum Difference Between Increasing Elements](https://leetcode.com/problems/maximum-difference-between-increasing-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2016.java) || Easy || +| 2012 | [Sum of Beauty in the Array](https://leetcode.com/problems/sum-of-beauty-in-the-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2012.java) || Medium || +| 2011 | [Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2011.java) || Easy || +| 2007 | [Find Original Array From Doubled Array](https://leetcode.com/problems/find-original-array-from-doubled-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2007.java) || Medium || +| 2006 | [Count Number of Pairs With Absolute Difference K](https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2006.java) || Easy || +| 2001 | [Number of Pairs of Interchangeable Rectangles](https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/) | [Python3](https://github.com/fishercoder1534/Leetcode/blob/master/python3/2001.py), [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2001.java) || Medium || +| 2000 | [Reverse Prefix of Word](https://leetcode.com/problems/reverse-prefix-of-word/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2000.java) || Easy || \ No newline at end of file diff --git a/paginated_contents/algorithms/4th_thousand/README.md b/paginated_contents/algorithms/4th_thousand/README.md new file mode 100644 index 0000000000..5237fed1ea --- /dev/null +++ b/paginated_contents/algorithms/4th_thousand/README.md @@ -0,0 +1,116 @@ +| # | Title | Solutions | Video | Difficulty | Tag +|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|---------------------------------------------------------------------- +| 3502 | [Minimum Cost to Reach Every Position](https://leetcode.com/problems/minimum-cost-to-reach-every-position/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3502.java) | | Easy | +| 3491 | [Phone Number Prefix](https://leetcode.com/problems/phone-number-prefix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3491.java) | | Easy | +| 3477 | [Fruits Into Baskets II](https://leetcode.com/problems/fruits-into-baskets-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3477.java) | | Easy | +| 3471 | [Find the Largest Almost Missing Integer](https://leetcode.com/problems/find-the-largest-almost-missing-integer/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3471.java) | | Easy | +| 3402 | [Minimum Operations to Make Columns Strictly Increasing](https://leetcode.com/problems/minimum-operations-to-make-columns-strictly-increasing/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3402.java) | | Easy | +| 3396 | [Minimum Number of Operations to Make Elements in Array Distinct](https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3396.java) | | Easy | +| 3392 | [Count Subarrays of Length Three With a Condition](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3392.java) | | Easy | +| 3386 | [Button with Longest Push Time](https://leetcode.com/problems/button-with-longest-push-time/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3386.java) | | Easy | +| 3379 | [Transformed Array](https://leetcode.com/problems/transformed-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3379.java) | | Easy | +| 3375 | [Minimum Operations to Make Array Values Equal to K](https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3375.java) | | Easy | +| 3370 | [Smallest Number With All Set Bits](https://leetcode.com/problems/smallest-number-with-all-set-bits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3370.java) | | Easy | +| 3364 | [Minimum Positive Sum Subarray](https://leetcode.com/problems/minimum-positive-sum-subarray/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3364.java) | | Easy | +| 3360 | [Stone Removal Game](https://leetcode.com/problems/stone-removal-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3360.java) | | Easy | +| 3354 | [Make Array Elements Equal to Zero](https://leetcode.com/problems/make-array-elements-equal-to-zero/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3354.java) | | Easy | +| 3353 | [Minimum Total Operations](https://leetcode.com/problems/minimum-total-operations/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3353.java) | | Easy | +| 3349 | [Adjacent Increasing Subarrays Detection I](https://leetcode.com/problems/adjacent-increasing-subarrays-detection-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3349.java) | | Easy | +| 3345 | [Smallest Divisible Digit Product I](https://leetcode.com/problems/smallest-divisible-digit-product-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3345.java) | | Easy | +| 3340 | [Check Balanced String](https://leetcode.com/problems/check-balanced-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3340.java) | | Easy | +| 3330 | [Find the Original Typed String I](https://leetcode.com/problems/find-the-original-typed-string-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3330.java) | | Easy | +| 3324 | [Find the Sequence of Strings Appeared on the Screen](https://leetcode.com/problems/find-the-sequence-of-strings-appeared-on-the-screen/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3324.java) | | Easy | +| 3318 | [Find X-Sum of All K-Long Subarrays I](https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3318.java) | | Easy | +| 3314 | [Construct the Minimum Bitwise Array I](https://leetcode.com/problems/construct-the-minimum-bitwise-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3314.java) | | Easy | +| 3304 | [Find the K-th Character in String Game I](https://leetcode.com/problems/find-the-k-th-character-in-string-game-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3304.java) | | Easy | +| 3300 | [Minimum Element After Replacement With Digit Sum](https://leetcode.com/problems/minimum-element-after-replacement-with-digit-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3300java) | | Easy | +| 3289 | [The Two Sneaky Numbers of Digitville](https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3289.java) | | Easy | +| 3285 | [Find Indices of Stable Mountains](https://leetcode.com/problems/find-indices-of-stable-mountains/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3285.java) | | Easy | +| 3280 | [Convert Date to Binary](https://leetcode.com/problems/convert-date-to-binary/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3280.java) | | Easy | +| 3274 | [Check if Two Chessboard Squares Have the Same Color](https://leetcode.com/problems/check-if-two-chessboard-squares-have-the-same-color/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3274.java) | | Easy | +| 3270 | [Find the Key of the Numbers](https://leetcode.com/problems/find-the-key-of-the-numbers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3270.java) | | Easy | +| 3264 | [Final Array State After K Multiplication Operations I](https://leetcode.com/problems/final-array-state-after-k-multiplication-operations-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3264.java) | | Easy | +| 3263 | [Convert Doubly Linked List to Array I](https://leetcode.com/problems/convert-doubly-linked-list-to-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3263.java) | | Easy | +| 3258 | [Count Substrings That Satisfy K-Constraint I](https://leetcode.com/problems/count-substrings-that-satisfy-k-constraint-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3258.java) | | Easy | +| 3254 | [Find the Power of K-Size Subarrays I](https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3254.java) | | Easy | +| 3249 | [Count the Number of Good Nodes](https://leetcode.com/problems/count-the-number-of-good-nodes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3249.java) | | Medium | +| 3248 | [Snake in Matrix](https://leetcode.com/problems/snake-in-matrix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3248.java) | | Easy | +| 3243 | [Shortest Distance After Road Addition Queries I](https://leetcode.com/problems/shortest-distance-after-road-addition-queries-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3243.java) | | Medium | +| 3242 | [Design Neighbor Sum Service](https://leetcode.com/problems/design-neighbor-sum-service/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3242.java) | | Easy | +| 3241 | [Time Taken to Mark All Nodes](https://leetcode.com/problems/time-taken-to-mark-all-nodes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3241.java) | | Hard | +| 3240 | [Minimum Number of Flips to Make Binary Grid Palindromic II](https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3240.java) | | Medium | +| 3239 | [Minimum Number of Flips to Make Binary Grid Palindromic I](https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java) | | Easy | +| 3238 | [Find the Number of Winning Players](https://leetcode.com/problems/find-the-number-of-winning-players/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3238.java) | | Easy | +| 3237 | [Alt and Tab Simulation](https://leetcode.com/problems/alt-and-tab-simulation/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3237.java) | | Medium | +| 3234 | [Count the Number of Substrings With Dominant Ones](https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3234.java) | | Medium | Sliding Window +| 3233 | [Find the Count of Numbers Which Are Not Special](https://leetcode.com/problems/find-the-count-of-numbers-which-are-not-special/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3233.java) | | Medium | Math +| 3232 | [Find if Digit Game Can Be Won](https://leetcode.com/problems/find-if-digit-game-can-be-won/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3232.java) | | Easy | +| 3226 | [Number of Bit Changes to Make Two Integers Equal](https://leetcode.com/problems/number-of-bit-changes-to-make-two-integers-equal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3226.java) | | Easy | +| 3224 | [Minimum Array Changes to Make Differences Equal](https://leetcode.com/problems/minimum-array-changes-to-make-differences-equal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3224.java) | | Medium | +| 3223 | [Minimum Length of String After Operations](https://leetcode.com/problems/minimum-length-of-string-after-operations/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3223.java) | | Medium | +| 3222 | [Find the Winning Player in Coin Game](https://leetcode.com/problems/find-the-winning-player-in-coin-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3222.java) | | Easy | +| 3219 | [Minimum Cost for Cutting Cake II](https://leetcode.com/problems/minimum-cost-for-cutting-cake-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3219.java) | | Hard | Greedy +| 3218 | [Minimum Cost for Cutting Cake I](https://leetcode.com/problems/minimum-cost-for-cutting-cake-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3218.java) | | Medium | +| 3217 | [Delete Nodes From Linked List Present in Array](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3217.java) | | Medium | LinkedList +| 3216 | [Lexicographically Smallest String After a Swap](https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3216.java) | | Easy | Greedy +| 3212 | [Count Submatrices With Equal Frequency of X and Y](https://leetcode.com/problems/count-submatrices-with-equal-frequency-of-x-and-y/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3212.java) | | Medium | Prefix Sum +| 3211 | [Generate Binary Strings Without Adjacent Zeros](https://leetcode.com/problems/generate-binary-strings-without-adjacent-zeros/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3211.java) | | Medium | Recursion, Backtracking +| 3210 | [Find the Encrypted String](https://leetcode.com/problems/find-the-encrypted-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3210.java) | | Easy | +| 3208 | [Alternating Groups II](https://leetcode.com/problems/alternating-groups-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3208.java) | | Medium | +| 3206 | [Alternating Groups I](https://leetcode.com/problems/alternating-groups-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3206.java) | | Easy | +| 3200 | [Maximum Height of a Triangle](https://leetcode.com/problems/maximum-height-of-a-triangle/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3200.java) | | Easy | +| 3199 | [Count Triplets with Even XOR Set Bits I](https://leetcode.com/problems/count-triplets-with-even-xor-set-bits-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3199.java) | | Easy | +| 3196 | [Maximize Total Cost of Alternating Subarrays](https://leetcode.com/problems/maximize-total-cost-of-alternating-subarrays/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3196.java) | | Medium |DP +| 3195 | [Find the Minimum Area to Cover All Ones I](https://leetcode.com/problems/find-the-minimum-area-to-cover-all-ones-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3195.java) | | Medium | +| 3194 | [Minimum Average of Smallest and Largest Elements](https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3194.java) | | Easy | +| 3192 | [Minimum Operations to Make Binary Array Elements Equal to One II](https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3192.java) | | Medium | +| 3191 | [Minimum Operations to Make Binary Array Elements Equal to One I](https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3191.java) | | Medium | +| 3190 | [Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3190.java) | | Easy | +| 3189 | [Minimum Moves to Get a Peaceful Board](https://leetcode.com/problems/minimum-moves-to-get-a-peaceful-board/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3186.java) | | Medium | Greedy +| 3186 | [Maximum Total Damage With Spell Casting](https://leetcode.com/problems/maximum-total-damage-with-spell-casting/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3186.java) | | Medium | DP +| 3185 | [Count Pairs That Form a Complete Day II](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3185.java) | | Medium | +| 3184 | [Count Pairs That Form a Complete Day I](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3184.java) | | Easy | +| 3178 | [Find the Child Who Has the Ball After K Seconds](https://leetcode.com/problems/find-the-child-who-has-the-ball-after-k-seconds/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3178.java) | | Easy | +| 3175 | [Find The First Player to win K Games in a Row](https://leetcode.com/problems/find-the-first-player-to-win-k-games-in-a-row/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3175.java) | | Medium | +| 3174 | [Clear Digits](https://leetcode.com/problems/clear-digits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3174.java) | | Easy | +| 3173 | [Bitwise OR of Adjacent Elements](https://leetcode.com/problems/bitwise-or-of-adjacent-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3173.java) | | Easy | +| 3168 | [Minimum Number of Chairs in a Waiting Room](https://leetcode.com/problems/minimum-number-of-chairs-in-a-waiting-room/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3168.java) | | Easy | +| 3164 | [Find the Number of Good Pairs II](https://leetcode.com/problems/find-the-number-of-good-pairs-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3164.java) | | Medium | +| 3162 | [Find the Number of Good Pairs I](https://leetcode.com/problems/find-the-number-of-good-pairs-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3162.java) | | Easy | +| 3158 | [Find the XOR of Numbers Which Appear Twice](https://leetcode.com/problems/find-the-xor-of-numbers-which-appear-twice/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3158.java) | | Easy | +| 3157 | [Find the Level of Tree with Minimum Sum](https://leetcode.com/problems/find-the-level-of-tree-with-minimum-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3157.java) | | Medium |BFS +| 3151 | [Special Array I](https://leetcode.com/problems/special-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3151.java) | | Easy | +| 3146 | [Permutation Difference between Two Strings](https://leetcode.com/problems/permutation-difference-between-two-strings/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3146.java) | | Easy | +| 3142 | [Check if Grid Satisfies Conditions](https://leetcode.com/problems/check-if-grid-satisfies-conditions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3142.java) | | Easy | +| 3136 | [Valid Word](https://leetcode.com/problems/valid-word/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3136.java) | | Easy | +| 3131 | [Find the Integer Added to Array I](https://leetcode.com/problems/find-the-integer-added-to-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3131.java) | | Easy | +| 3127 | [Make a Square with the Same Color](https://leetcode.com/problems/make-a-square-with-the-same-color/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3127.java) | | Easy | +| 3120 | [Count the Number of Special Characters I](https://leetcode.com/problems/count-the-number-of-special-characters-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3120.java) | | Easy | +| 3114 | [Latest Time You Can Obtain After Replacing Characters](https://leetcode.com/problems/latest-time-you-can-obtain-after-replacing-characters/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3114.java) | | Easy | +| 3112 | [Minimum Time to Visit Disappearing Nodes](https://leetcode.com/problems/minimum-time-to-visit-disappearing-nodes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3112.java) | | Medium | Graph, Shortest Path +| 3110 | [Score of a String](https://leetcode.com/problems/score-of-a-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3110.java) | | Easy | +| 3099 | [Harshad Number](https://leetcode.com/problems/harshad-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3099.java) | | Easy | +| 3095 | [Shortest Subarray With OR at Least K I](https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3095.java) | | Easy | +| 3090 | [Maximum Length Substring With Two Occurrences](https://leetcode.com/problems/maximum-length-substring-with-two-occurrences/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3090.java) | | Easy | +| 3083 | [Existence of a Substring in a String and Its Reverse](https://leetcode.com/problems/existence-of-a-substring-in-a-string-and-its-reverse/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3083.java) | | Easy | +| 3079 | [Find the Sum of Encrypted Integers](https://leetcode.com/problems/find-the-sum-of-encrypted-integers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3079.java) | | Easy | +| 3074 | [Apple Redistribution into Boxes](https://leetcode.com/problems/apple-redistribution-into-boxes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3074.java) | | Easy | +| 3069 | [Distribute Elements Into Two Arrays I](https://leetcode.com/problems/distribute-elements-into-two-arrays-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3069.java) | | Easy | +| 3065 | [Minimum Operations to Exceed Threshold Value I](https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3065.java) | | Easy | +| 3063 | [Linked List Frequency](https://leetcode.com/problems/linked-list-frequency/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3063.java) | | Easy | +| 3062 | [Winner of the Linked List Game](https://leetcode.com/problems/winner-of-the-linked-list-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3062.java) | | Easy | +| 3046 | [Split the Array](https://leetcode.com/problems/split-the-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3046.java) | | Easy | +| 3042 | [Count Prefix and Suffix Pairs I](https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3042.java) | | Easy | +| 3038 | [Maximum Number of Operations With the Same Score I](https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3038.java) | | Easy | +| 3033 | [Modify the Matrix](https://leetcode.com/problems/modify-the-matrix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3033.java) | | Easy | +| 3032 | [Count Numbers With Unique Digits II](https://leetcode.com/problems/count-numbers-with-unique-digits-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3032.java) | | Easy | +| 3024 | [Type of Triangle](https://leetcode.com/problems/type-of-triangle/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3024.java) | | Easy | +| 3028 | [Ant on the Boundary](https://leetcode.com/problems/ant-on-the-boundary/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3028.java) | | Easy | +| 3019 | [Number of Changing Keys](https://leetcode.com/problems/number-of-changing-keys/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3019.java) | | Easy | +| 3016 | [Minimum Number of Pushes to Type Word II](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java) | | Medium | +| 3010 | [Divide an Array Into Subarrays With Minimum Cost I](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3010.java) | | Easy | +| 3014 | [Minimum Number of Pushes to Type Word I](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3014.java) | | Easy | +| 3006 | [Find Beautiful Indices in the Given Array I](https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3006.java) | | Medium | +| 3005 | [Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3005.java) | | Easy | +| 3004 | [Maximum Subtree of the Same Color](https://leetcode.com/problems/maximum-subtree-of-the-same-color/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3004.java) | | Medium | DFS, Tree +| 3000 | [Maximum Area of Longest Diagonal Rectangle](https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3000.java) | | Easy | diff --git a/paginated_contents/database/README.md b/paginated_contents/database/README.md new file mode 100644 index 0000000000..7e62ee9bf7 --- /dev/null +++ b/paginated_contents/database/README.md @@ -0,0 +1,124 @@ +| # | Title | Solutions | Video | Difficulty | Tag +|------|----------------|-----------------------------------------------------------------------------------------------------------------------|---------------|---------------|------------- +| 3198 |[Find Cities in Each State](https://leetcode.com/problems/find-cities-in-each-state/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_3198.sql) || Easy | +| 3172 |[Second Day Verification](https://leetcode.com/problems/second-day-verification/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_3172.sql) || Easy | +| 3150 |[Invalid Tweets II](https://leetcode.com/problems/invalid-tweets-ii/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_3150.sql) || Easy | +| 3059 |[Find All Unique Email Domains](https://leetcode.com/problems/find-all-unique-email-domains/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_3159.sql) || Easy | +| 3051 |[Find Candidates for Data Scientist Position](https://leetcode.com/problems/find-candidates-for-data-scientist-position/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_3051.sql) || Easy | +| 2990 |[Loan Types](https://leetcode.com/problems/loan-types/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_2990.sql) || Easy | +| 2879 |[Display the First Three Rows](https://leetcode.com/problems/display-the-first-three-rows/)| [Python3](https://github.com/fishercoder1534/Leetcode/blob/master/python3/2879.py) || Easy | +| 2205 |[The Number of Users That Are Eligible for Discount](https://leetcode.com/problems/the-number-of-users-that-are-eligible-for-discount/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_2205.sql) || Easy | +| 2082 |[The Number of Rich Customers](https://leetcode.com/problems/the-number-of-rich-customers/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_2082.sql) || Easy | +| 2072 |[The Winner University](https://leetcode.com/problems/the-winner-university/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_2072.sql) || Easy | +| 2026 |[Low-Quality Problems](https://leetcode.com/problems/low-quality-problems/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_2026.sql) || Easy | +| 1978 |[Employees Whose Manager Left the Company](https://leetcode.com/problems/employees-whose-manager-left-the-company/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1978.sql) || Easy | +| 1873 |[Calculate Special Bonus](https://leetcode.com/problems/calculate-special-bonus/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1873.sql) || Easy | +| 1853 |[Convert Date Format](https://leetcode.com/problems/convert-date-format/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1853.sql) || Easy | +| 1821 |[Find Customers With Positive Revenue this Year](https://leetcode.com/problems/find-customers-with-positive-revenue-this-year/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1821.sql) || Easy | +| 1809 |[Ad-Free Sessions](https://leetcode.com/problems/ad-free-sessions/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1809.sql) || Easy | +| 1795 |[Rearrange Products Table](https://leetcode.com/problems/rearrange-products-table/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1795.sql) || Easy | +| 1789 |[Primary Department for Each Employee](https://leetcode.com/problems/primary-department-for-each-employee/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1789.sql) || Easy | +| 1757 |[Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1757.sql) || Easy | +| 1741 |[Find Total Time Spent by Each Employee](https://leetcode.com/problems/find-total-time-spent-by-each-employee/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1741.sql) || Easy | +| 1729 |[Find Followers Count](https://leetcode.com/problems/find-followers-count/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1729.sql) || Easy | +| 1709 |[Biggest Window Between Visits](https://leetcode.com/problems/biggest-window-between-visits/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1709.sql) || Medium | +| 1693 |[Daily Leads and Partners](https://leetcode.com/problems/daily-leads-and-partners/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1693.sql) || Easy | +| 1683 |[Invalid Tweets](https://leetcode.com/problems/invalid-tweets/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1683.sql) || Easy | +| 1677 |[Product's Worth Over Invoices](https://leetcode.com/problems/products-worth-over-invoices/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1677.sql) || Easy | +| 1667 |[Fix Names in a Table](https://leetcode.com/problems/fix-names-in-a-table/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1667.sql) || Easy | +| 1661 |[Average Time of Process per Machine](https://leetcode.com/problems/average-time-of-process-per-machine/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1661.sql) || Easy | +| 1633 |[Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1633.sql) || Easy | +| 1623 |[All Valid Triplets That Can Represent a Country](https://leetcode.com/problems/all-valid-triplets-that-can-represent-a-country/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1623.sql) || Easy | +| 1607 |[Sellers With No Sales](https://leetcode.com/problems/sellers-with-no-sales/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1607.sql) || Easy | +| 1596 |[The Most Frequently Ordered Products for Each Customer](https://leetcode.com/problems/the-most-frequently-ordered-products-for-each-customer/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1596.sql) || Medium | +| 1571 |[Warehouse Manager](https://leetcode.com/problems/warehouse-manager/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1571.sql) || Easy | +| 1587 |[Bank Account Summary II](https://leetcode.com/problems/bank-account-summary-ii/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1587.sql) || Easy | +| 1581 |[Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1581.sql) || Easy | +| 1565 |[Unique Orders and Customers Per Month](https://leetcode.com/problems/unique-orders-and-customers-per-month/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1565.sql) || Easy | +| 1543 |[Fix Product Name Format](https://leetcode.com/problems/fix-product-name-format/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1543.sql) || Easy | +| 1527 |[Patients With a Condition](https://leetcode.com/problems/patients-with-a-condition/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1527.sql) || Easy | +| 1517 |[Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1517.sql) || Easy | +| 1511 |[Customer Order Frequency](https://leetcode.com/problems/customer-order-frequency/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1511.sql) || Easy | +| 1495 |[Friendly Movies Streamed Last Month](https://leetcode.com/problems/friendly-movies-streamed-last-month/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1495.sql) || Easy | +| 1435 |[Create a Session Bar Chart](https://leetcode.com/problems/create-a-session-bar-chart/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1435.sql) || Easy | +| 1484 |[Group Sold Products By The Date](https://leetcode.com/problems/group-sold-products-by-the-date/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1484.sql) || Easy | +| 1445 |[Apples & Oranges](https://leetcode.com/problems/apples-oranges/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1445.sql) || Medium | +| 1407 |[Top Travellers](https://leetcode.com/problems/top-travellers/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1407.sql) || Easy | +| 1393 |[Capital Gain/Loss](https://leetcode.com/problems/capital-gainloss/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1393.sql) || Easy | +| 1384 |[Total Sales Amount by Year](https://leetcode.com/problems/total-sales-amount-by-year/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1384.sql) || Hard | +| 1378 |[Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1378.sql) || Easy | +| 1371 |[The Number of Employees Which Report to Each Employee](https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1371.sql) || Easy | +| 1369 |[Get the Second Most Recent Activity](https://leetcode.com/problems/get-the-second-most-recent-activity/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1369.sql) || Hard | +| 1364 |[Number of Trusted Contacts of a Customer](https://leetcode.com/problems/number-of-trusted-contacts-of-a-customer/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1364.sql) || Medium | +| 1355 |[Activity Participants](https://leetcode.com/problems/activity-participants/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1355.sql) || Medium | +| 1350 |[Students With Invalid Departments](https://leetcode.com/problems/students-with-invalid-departments/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1350.sql) || Easy | +| 1341 |[Movie Rating](https://leetcode.com/problems/movie-rating/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1341.sql) || Medium | +| 1327 |[List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1327.sql) || Easy | +| 1322 |[Ads Performance](https://leetcode.com/problems/ads-performance/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1322.sql) || Easy | +| 1308 |[Running Total for Different Genders](https://leetcode.com/problems/running-total-for-different-genders/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1308.sql) || Medium | +| 1303 |[Find the Team Size](https://leetcode.com/problems/find-the-team-size/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1303.sql) || Easy | +| 1294 |[Weather Type in Each Country](https://leetcode.com/problems/weather-type-in-each-country/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1294.sql) | | Easy | +| 1285 |[Find the Start and End Number of Continuous Ranges](https://leetcode.com/problems/find-the-start-and-end-number-of-continuous-ranges/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1285.sql) || Medium | +| 1280 |[Students and Examinations](https://leetcode.com/problems/students-and-examinations/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1280.sql) | [:tv:](https://www.youtube.com/watch?v=ThbkV4Fs7iE)| Easy | +| 1270 |[All People Report to the Given Manager](https://leetcode.com/problems/all-people-report-to-the-given-manager/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1270.sql) || Medium | +| 1251 |[Average Selling Price](https://leetcode.com/problems/average-selling-price/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1251.sql) | | Easy | +| 1241 |[Number of Comments per Post](https://leetcode.com/problems/number-of-comments-per-post/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1241.sql) | | Easy | +| 1211 |[Queries Quality and Percentage](https://leetcode.com/problems/queries-quality-and-percentage/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1211.sql) | | Easy | +| 1179 |[Reformat Department Table](https://leetcode.com/problems/reformat-department-table/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1179.sql) | | Easy | +| 1173 |[Immediate Food Delivery I](https://leetcode.com/problems/immediate-food-delivery-i/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1173.sql) | | Easy | +| 1148 |[Article Views I](https://leetcode.com/problems/article-views-i/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1148.sql) | | Easy | +| 1142 |[User Activity for the Past 30 Days II](https://leetcode.com/problems/user-activity-for-the-past-30-days-ii/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1142.sql) | | Easy | +| 1141 |[User Activity for the Past 30 Days I](https://leetcode.com/problems/user-activity-for-the-past-30-days-i/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1141.sql) | | Easy | +| 1113 |[Reported Posts](https://leetcode.com/problems/reported-posts/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1113.sql) | | Easy | +| 1084 |[Sales Analysis III](https://leetcode.com/problems/sales-analysis-iii/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1084.sql) | | Easy | +| 1083 |[Sales Analysis II](https://leetcode.com/problems/sales-analysis-ii/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1083.sql) | | Easy | +| 1082 |[Sales Analysis I](https://leetcode.com/problems/sales-analysis-i/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1082.sql) | | Easy | +| 1076 |[Project Employees II](https://leetcode.com/problems/project-employees-ii/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1076.sql) | | Easy | +| 1075 |[Project Employees I](https://leetcode.com/problems/project-employees-i/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1075.sql) | | Easy | +| 1069 |[Product Sales Analysis II](https://leetcode.com/problems/product-sales-analysis-ii/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1069.sql) | | Easy | +| 1068 |[Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1068.sql) | | Easy | +| 1050 |[Actors and Directors Who Cooperated At Least Three Times](https://leetcode.com/problems/actors-and-directors-who-cooperated-at-least-three-times/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_1050.sql) || Easy | +| 627 |[Swap Salary](https://leetcode.com/problems/swap-salary/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_627.sql) | | Easy | +| 626 |[Exchange Seats](https://leetcode.com/problems/exchange-seats/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_626.sql) | | Medium | +| 620 |[Not Boring Movies](https://leetcode.com/problems/not-boring-movies/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_620.sql) | | Easy | +| 619 |[Biggest Single Number](https://leetcode.com/problems/biggest-single-number/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_619.sql) | | Easy | +| 618 |[Students Report By Geography](https://leetcode.com/problems/students-report-by-geography/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_618.sql) | | Hard | Session Variables +| 615 |[Average Salary: Departments VS Company](https://leetcode.com/problems/average-salary-departments-vs-company/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_615.sql) | | Hard| +| 614 |[Second Degree Follower](https://leetcode.com/problems/second-degree-follower/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_614.sql) | | Medium | Inner Join +| 613 |[Shortest Distance in a Line](https://leetcode.com/problems/shortest-distance-in-a-line/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_613.sql) || Easy| +| 612 |[Shortest Distance in a Plane](https://leetcode.com/problems/shortest-distance-in-a-plane/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_612.sql) || Medium| +| 610 |[Triangle Judgement](https://leetcode.com/problems/triangle-judgement/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_610.java) | | Easy | +| 608 |[Tree Node](https://leetcode.com/problems/tree-node/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_608.sql) | | Medium | Union +| 607 |[Sales Person](https://leetcode.com/problems/sales-person/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_607.sql) | | Easy | +| 603 |[Consecutive Available Seats](https://leetcode.com/problems/sales-person/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_603.sql) | | Easy | +| 602 |[Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_602.sql) | | Medium | +| 601 |[Human Traffic of Stadium](https://leetcode.com/problems/human-traffic-of-stadium/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_601.sql) | | Hard | +| 597 |[Friend Requests I: Overall Acceptance Rate](https://leetcode.com/problems/friend-requests-i-overall-acceptance-rate/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_597.sql) | | Easy | +| 596 |[Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_596.sql) || Easy | +| 595 |[Big Countries](https://leetcode.com/problems/big-countries/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_595.sql) | | Easy | +| 586 |[Customer Placing the Largest Number of Orders](https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_586.sql) | | Easy| +| 585 |[Investments in 2016](https://leetcode.com/problems/investments-in-2016/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_585.java) || Medium| +| 584 |[Find Customer Referee](https://leetcode.com/problems/find-customer-referee/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_584.java) || Easy| +| 580 |[Count Student Number in Departments](https://leetcode.com/problems/count-student-number-in-departments/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_580.sql) | |Medium | Left Join +| 578 |[Get Highest Answer Rate Question](https://leetcode.com/problems/get-highest-answer-rate-question/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_578.sql) || Medium | +| 577 |[Employee Bonus](https://leetcode.com/problems/employee-bonus/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_577.sql) || Easy | +| 574 |[Winning Candidate](https://leetcode.com/problems/winning-candidate/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_574.sql) || Medium | +| 571 |[Find Median Given Frequency of Numbers](https://leetcode.com/problems/find-median-given-frequency-of-numbers/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_571.sql) || Hard | +| 570 |[Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_570.sql) || Medium | +| 569 |[Median Employee Salary](https://leetcode.com/problems/median-employee-salary/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_569.sql) || Hard | +| 534 |[Game Play Analysis III](https://leetcode.com/problems/game-play-analysis-iii/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_534.sql) || Easy| +| 512 |[Game Play Analysis II](https://leetcode.com/problems/game-play-analysis-ii/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_512.sql) || Easy| +| 511 |[Game Play Analysis I](https://leetcode.com/problems/game-play-analysis-i/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_511.sql) || Easy| +| 262 |[Trips and Users](https://leetcode.com/problems/trips-and-users/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_262.sql) ||Hard| Inner Join +| 197 |[Rising Temperature](https://leetcode.com/problems/rising-temperature/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_197.sql) | | Easy| +| 196 |[Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_196.sql) | |Easy| +| 185 |[Department Top Three Salaries](https://leetcode.com/problems/department-top-three-salaries)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_185.sql) | | Hard| +| 184 |[Department Highest Salary](https://leetcode.com/problems/department-highest-salary)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_184.sql) | | Medium| +| 183 |[Customers Who Never Order](https://leetcode.com/problems/customers-who-never-order/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_183.sql) | | Easy| +| 182 |[Duplicate Emails](https://leetcode.com/problems/duplicate-emails/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_182.sql) | | Easy| +| 181 |[Employees Earning More Than Their Managers](https://leetcode.com/problems/employees-earning-more-than-their-managers/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_181.sql) | | Easy| +| 180 |[Consecutive Numbers](https://leetcode.com/problems/consecutive-numbers)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_180.sql) | | Medium| +| 178 |[Rank Scores](https://leetcode.com/problems/rank-scores/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_178.sql) | | Medium| +| 177 |[Nth Highest Salary](https://leetcode.com/problems/nth-highest-salary/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_177.sql) | | Medium| +| 176 |[Second Highest Salary](https://leetcode.com/problems/second-highest-salary/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_176.sql) | | Easy| +| 175 |[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)| [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/database/_175.sql) | | Easy| diff --git a/paginated_contents/javascript/README.md b/paginated_contents/javascript/README.md new file mode 100644 index 0000000000..61ac6b6428 --- /dev/null +++ b/paginated_contents/javascript/README.md @@ -0,0 +1,5 @@ +| # | Title | Solutions | Video | Difficulty | Tag +|------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|--------------------------------------|------------------------------------------|---------------------------------------------------------------------- +| 2703 | [Return Length of Arguments Passed](https://leetcode.com/problems/return-length-of-arguments-passed/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_2703.js) | | Easy | Javascript +| 2620 | [Counter](https://leetcode.com/problems/counter/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_2620.js) | | Easy | Javascript +| 2619 | [Array Prototype Last](https://leetcode.com/problems/array-prototype-last/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_2619.js) | | Easy | Javascript \ No newline at end of file diff --git a/paginated_contents/shell/README.md b/paginated_contents/shell/README.md new file mode 100644 index 0000000000..ee9117e84c --- /dev/null +++ b/paginated_contents/shell/README.md @@ -0,0 +1,6 @@ +| # | Title | Solutions | Video | Difficulty | Tag +|-----|----------------|---------------|---------------|---------------|------------- +|195|[Tenth Line](https://leetcode.com/problems/tenth-line/)|[Solution](https://github.com/fishercoder1534/Leetcode/blob/master/shell/TenthLine.sh)| | Easy| +|194|[Transpose File](https://leetcode.com/problems/transpose-file/)|[Solution](https://github.com/fishercoder1534/Leetcode/blob/master/shell/TransposeFile.sh)| | Medium| +|193|[Valid Phone Numbers](https://leetcode.com/problems/valid-phone-numbers/)|[Solution](https://github.com/fishercoder1534/Leetcode/blob/master/shell/ValidPhoneNumbers.sh)| | Easy| +|192|[Word Frequency](https://leetcode.com/problems/word-frequency/)|[Solution](https://github.com/fishercoder1534/Leetcode/blob/master/shell/_192.sh)| | Medium| \ No newline at end of file diff --git a/python3/2001.py b/python3/2001.py new file mode 100644 index 0000000000..a4268862d8 --- /dev/null +++ b/python3/2001.py @@ -0,0 +1,13 @@ +from fractions import * +from collections import Counter + +class Solution: + #credit: https://leetcode-cn.com/u/lucifer1004/ + def interchangeableRectangles(self, rectangles: List[List[int]]) -> int: + cnt = Counter() + for w, h in rectangles: + cnt[Fraction(w, h)] += 1 + ans = 0 + for value in cnt.values(): + ans += value * (value - 1) // 2 + return ans \ No newline at end of file diff --git a/python3/2879.py b/python3/2879.py new file mode 100644 index 0000000000..5444325892 --- /dev/null +++ b/python3/2879.py @@ -0,0 +1,4 @@ +import pandas as pd + +def selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame: + return employees.head(3) \ No newline at end of file diff --git a/src/main/java/com/fishercoder/common/classes/Employee.java b/src/main/java/com/fishercoder/common/classes/Employee.java index ba4ff400c0..f5b7460830 100644 --- a/src/main/java/com/fishercoder/common/classes/Employee.java +++ b/src/main/java/com/fishercoder/common/classes/Employee.java @@ -3,16 +3,16 @@ import java.util.List; public class Employee { - /** + /* * It's the unique id of each node; * unique id of this employee */ public int id; - /** + /* * the importance value of this employee */ public int importance; - /** + /* * the id of direct subordinates */ public List subordinates; diff --git a/src/main/java/com/fishercoder/common/classes/Interval.java b/src/main/java/com/fishercoder/common/classes/Interval.java index b6b9e2baf5..c5c6a40cc0 100644 --- a/src/main/java/com/fishercoder/common/classes/Interval.java +++ b/src/main/java/com/fishercoder/common/classes/Interval.java @@ -1,6 +1,6 @@ package com.fishercoder.common.classes; -/** +/* * This is a class used by one OJ problem: MeetingRooms */ public class Interval implements Comparable { @@ -45,7 +45,7 @@ public Interval(int s, int e) { @Override public int compareTo(Interval o) { int compareStart = o.start; - //ascending order + // ascending order return this.start - compareStart; } diff --git a/src/main/java/com/fishercoder/common/classes/ListNode.java b/src/main/java/com/fishercoder/common/classes/ListNode.java index 3e793f0225..272b215205 100644 --- a/src/main/java/com/fishercoder/common/classes/ListNode.java +++ b/src/main/java/com/fishercoder/common/classes/ListNode.java @@ -1,10 +1,9 @@ package com.fishercoder.common.classes; import com.fishercoder.common.utils.CommonUtils; - import java.util.List; -/** +/* * Normally, both val and next should be private attributes and generate getter and setter for them, * but for the convenience of leetcode solutions, I set them as public. */ @@ -52,36 +51,9 @@ public static ListNode createSinglyLinkedList() { return head; } - public static ListNode createSinglyLinkedList(List listValues) { - if (listValues == null || listValues.size() == 0) { - throw new IllegalArgumentException( - "Please pass in a valid listValues to create a singly linked list."); - } - ListNode head = new ListNode(listValues.get(0)); - ListNode tmp = head; - for (int i = 1; i < listValues.size(); i++) { - ListNode next = new ListNode(listValues.get(i)); - tmp.next = next; - tmp = tmp.next; - } - printList(head); - return head; - } - - public static void printList(ListNode head) { - ListNode temp = head; - System.out.println(); - while (temp != null) { - System.out.print(temp.val() + "\t"); - temp = temp.next; - } - } - public static void main(String... strings) { List values = CommonUtils.randomIntArrayGenerator(10, 20); - createSinglyLinkedList(values); ListNode head = createSinglyLinkedList(); - printList(head); System.out.println("The end."); } @@ -113,5 +85,4 @@ public int hashCode() { public String toString() { return "ListNode{" + "val=" + val + ", next=" + next + '}'; } - } diff --git a/src/main/java/com/fishercoder/common/classes/NestedInteger.java b/src/main/java/com/fishercoder/common/classes/NestedInteger.java index 4d592e2e50..dbd4c8e1e1 100644 --- a/src/main/java/com/fishercoder/common/classes/NestedInteger.java +++ b/src/main/java/com/fishercoder/common/classes/NestedInteger.java @@ -62,5 +62,4 @@ public static String printNi(NestedInteger thisNi, StringBuilder sb) { sb.append("]"); return sb.toString(); } - } diff --git a/src/main/java/com/fishercoder/common/classes/Node.java b/src/main/java/com/fishercoder/common/classes/Node.java index 78c3199b75..da1f63b492 100644 --- a/src/main/java/com/fishercoder/common/classes/Node.java +++ b/src/main/java/com/fishercoder/common/classes/Node.java @@ -2,27 +2,49 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class Node { - public int val; - public List children; - - public Node() { - } - - public Node(int val, List children) { - this.val = val; - this.children = children; - } - - public Node(int val) { - this.val = val; - this.children = new ArrayList<>(); - } - - //todo: implement this method - /**return a N-ary tree based on the preorder values*/ - public static Node createNaryTree(List preorderValues) { - return null; - } + public int val; + public List children; + + public Node() { + this.children = new ArrayList<>(); + } + + public Node(int val) { + this.val = val; + this.children = new ArrayList<>(); + } + + public Node(int val, List children) { + this.val = val; + this.children = children; + } + + // todo: implement this method + + /* + * return a N-ary tree based on the preorder values + */ + public static Node createNaryTree(List preorderValues) { + return null; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Node node = (Node) o; + return val == node.val && Objects.equals(children, node.children); + } + + @Override + public int hashCode() { + return Objects.hash(val, children); + } } diff --git a/src/main/java/com/fishercoder/common/classes/Point.java b/src/main/java/com/fishercoder/common/classes/Point.java index 0e853c8396..bbb211204b 100644 --- a/src/main/java/com/fishercoder/common/classes/Point.java +++ b/src/main/java/com/fishercoder/common/classes/Point.java @@ -1,6 +1,6 @@ package com.fishercoder.common.classes; -/** +/* * Created by fishercoder on 12/31/16. */ public class Point { diff --git a/src/main/java/com/fishercoder/common/classes/TreeLinkNode.java b/src/main/java/com/fishercoder/common/classes/TreeLinkNode.java deleted file mode 100644 index 2bc0e215d3..0000000000 --- a/src/main/java/com/fishercoder/common/classes/TreeLinkNode.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.fishercoder.common.classes; - -/** - * Created by fishercoder1534 on 10/5/16. - */ -public class TreeLinkNode { - public int val; - public TreeLinkNode left; - public TreeLinkNode right; - public TreeLinkNode next; - - public TreeLinkNode(int x) { - val = x; - } -} diff --git a/src/main/java/com/fishercoder/common/classes/UndirectedGraphNode.java b/src/main/java/com/fishercoder/common/classes/UndirectedGraphNode.java index 951725205c..7199fe4e99 100644 --- a/src/main/java/com/fishercoder/common/classes/UndirectedGraphNode.java +++ b/src/main/java/com/fishercoder/common/classes/UndirectedGraphNode.java @@ -3,11 +3,11 @@ import java.util.ArrayList; import java.util.List; -/** +/* * Created by fishercoder1534 on 9/30/16. */ public class UndirectedGraphNode { - public int label; + public int val; public List neighbors; @Override @@ -21,7 +21,7 @@ public boolean equals(Object o) { UndirectedGraphNode that = (UndirectedGraphNode) o; - if (label != that.label) { + if (val != that.val) { return false; } return neighbors != null ? neighbors.equals(that.neighbors) : that.neighbors == null; @@ -29,13 +29,13 @@ public boolean equals(Object o) { @Override public int hashCode() { - int result = label; + int result = val; result = 31 * result + (neighbors != null ? neighbors.hashCode() : 0); return result; } public UndirectedGraphNode(int x) { - label = x; + val = x; neighbors = new ArrayList<>(); } } diff --git a/src/main/java/com/fishercoder/common/utils/BTreePrinter.java b/src/main/java/com/fishercoder/common/utils/BTreePrinter.java index c0ed7be442..c09a4f060d 100644 --- a/src/main/java/com/fishercoder/common/utils/BTreePrinter.java +++ b/src/main/java/com/fishercoder/common/utils/BTreePrinter.java @@ -4,7 +4,7 @@ import java.util.Collections; import java.util.List; -/** +/* * Copied this class from * http://stackoverflow.com/questions/4965335/how-to-print-binary-tree-diagram * This is an awesome one! It prints out the tree in a very nice fashion. @@ -55,8 +55,7 @@ private static > void printNodeInternal( for (int j = 0; j < nodes.size(); j++) { BTreePrinter.printWhitespaces(firstSpaces - i); if (nodes.get(j) == null) { - BTreePrinter.printWhitespaces(endgeLines + endgeLines + i - + 1); + BTreePrinter.printWhitespaces(endgeLines + endgeLines + i + 1); continue; } @@ -94,8 +93,7 @@ private static > int maxLevel(Node node) { return 0; } - return Math.max(BTreePrinter.maxLevel(node.left), - BTreePrinter.maxLevel(node.right)) + 1; + return Math.max(BTreePrinter.maxLevel(node.left), BTreePrinter.maxLevel(node.right)) + 1; } private static boolean isAllElementsNull(List list) { @@ -171,7 +169,6 @@ private static Node test2() { return root; } - public static class Node> { Node left; Node right; @@ -181,5 +178,4 @@ public Node(T data) { this.data = data; } } - -} \ No newline at end of file +} diff --git a/src/main/java/com/fishercoder/common/utils/CommonUtils.java b/src/main/java/com/fishercoder/common/utils/CommonUtils.java index 85a1fd3983..4a2b10ca09 100644 --- a/src/main/java/com/fishercoder/common/utils/CommonUtils.java +++ b/src/main/java/com/fishercoder/common/utils/CommonUtils.java @@ -2,7 +2,6 @@ import com.fishercoder.common.classes.Interval; import com.fishercoder.common.classes.ListNode; - import java.util.ArrayList; import java.util.Deque; import java.util.List; @@ -13,7 +12,7 @@ public class CommonUtils { private static final int DEFAULT_TREE_SIZE = 10; private static final int DEFAULT_UPPER_BOUND = 100; - //How to make a method generic: declare in its method signature + // How to make a method generic: declare in its method signature public static void printArray_generic_type(T[] nums) { for (T i : nums) { System.out.print(i + ", "); @@ -22,8 +21,33 @@ public static void printArray_generic_type(T[] nums) { } public static void main(String... strings) { - Integer[] nums = new Integer[]{1, 2, 3, 4, 5}; + Integer[] nums = new Integer[] {1, 2, 3, 4, 5}; printArray_generic_type(nums); + String input1 = + "[\"zDkA\",\"GfAj\",\"lt\"],[\"GfAj\",\"rtupD\",\"og\",\"l\"],[\"rtupD\",\"IT\",\"jGcew\",\"ZwFqF\"],[\"og\",\"yVobt\",\"EjA\",\"piUyQ\"],[\"IT\",\"XFlc\",\"W\",\"rB\"],[\"l\",\"GwQg\",\"shco\",\"Dub\",\"KwgZq\"],[\"oXMG\",\"uqe\"],[\"sNyV\",\"WbrP\"]"; + String input2 = "[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]"; + CommonUtils.printListList(convertLeetCode2DStringArrayInputIntoJavaArray(input1)); + CommonUtils.printListList(convertLeetCode2DStringArrayInputIntoJavaArray(input2)); + CommonUtils.print( + convertLeetCode1DStringArrayInputIntoJavaArray( + "[\"abcsi\",\"abyzjgj\",\"advz\",\"ag\",\"agkgdkob\",\"agpr\",\"ail\"]")); + CommonUtils.print2DIntArray( + convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[448,931,123,345],[889],[214,962],[576,746,897]")); + } + + public static void printArray(boolean[] booleans) { + for (boolean i : booleans) { + System.out.print(i + ", "); + } + System.out.println(); + } + + public static void printArray(double[] booleans) { + for (double i : booleans) { + System.out.print(i + ", "); + } + System.out.println(); } public static void printArray(int[] nums) { @@ -52,11 +76,11 @@ public static void print(int num) { } public static void print(List list) { - System.out.println("----------------------------------------------------"); + System.out.println("List of string is: "); for (String str : list) { System.out.print(str + ", "); } - System.out.println(); + System.out.println("\n----------------------------------------------------"); } public static void println(String message) { @@ -78,8 +102,8 @@ public static List randomIntArrayGenerator(int size) { // overloaded method to take no argument public static List randomIntArrayGenerator() { - return CommonUtils.randomIntArrayGenerator(CommonUtils.DEFAULT_TREE_SIZE, - DEFAULT_UPPER_BOUND); + return CommonUtils.randomIntArrayGenerator( + CommonUtils.DEFAULT_TREE_SIZE, DEFAULT_UPPER_BOUND); } // this one has two other overloaded methods as above @@ -106,7 +130,8 @@ public static List uniqueIntArrayGenerator(int size) { } // @Notes(context = - // "I'm assuing only classes in this PACKAGE will call the following two methods, so just leave the modifier as default, i.e. no public, private, or protected.") + // "I'm assuing only classes in this PACKAGE will call the following two methods, so just leave + // the modifier as default, i.e. no public, private, or protected.") public static void printWhitespaces(int count) { for (int i = 0; i < count; i++) { System.out.print(" "); @@ -123,7 +148,7 @@ public static boolean isAllElementsNull(List list) { return true; } - /** + /* * If you want to print the reversed list out, you need to return the reversed list's head, * which was the end node of the original node. using the following function. */ @@ -208,7 +233,6 @@ public static void printMatrixGeneric(boolean[][] matrix) { System.out.println(); } System.out.println("----------------------------------------------------"); - } public static void printListList(List> res) { @@ -246,4 +270,184 @@ public static void print2DCharArray(char[][] arrayArrays) { } System.out.println(); } + + public static char[][] convertLeetCodeRegular2DCharArrayInputIntoJavaArray(String input) { + /*LeetCode 2-d char array usually comes in like this: + * ["#"," ","#"],[" "," ","#"],["#","c"," "] which is wrapped in double quotes instead of single quotes which makes it not usable in Java code. + * This method helps with the conversion.*/ + String[] arrays = input.split("],\\["); + // CommonUtils.printArray_generic_type(arrays); + int m = arrays.length; + int n = arrays[1].split(",").length; + char[][] ans = new char[m][n]; + for (int i = 0; i < m; i++) { + if (i == 0) { + String str = arrays[i].substring(1); + String[] strs = str.split(","); + for (int j = 0; j < strs.length; j++) { + ans[i][j] = strs[j].charAt(1); + } + } else if (i == m - 1) { + String str = arrays[i].substring(0, arrays[i].length() - 1); + String[] strs = str.split(","); + for (int j = 0; j < strs.length; j++) { + ans[i][j] = strs[j].charAt(1); + } + } else { + String[] strs = arrays[i].split(","); + for (int j = 0; j < strs.length; j++) { + ans[i][j] = strs[j].charAt(1); + } + } + } + return ans; + } + + public static int[][] convertLeetCodeRegularRectangleArrayInputIntoJavaArray(String input) { + /* + * LeetCode 2-d array input usually comes like this: it's a REGULAR rectangle + * [[448,931],[234,889],[214,962],[576,746]] + * The expected input for this method is: "[448,931],[234,889],[214,962],[576,746]" + * i.e. strip off the beginning and ending square brackets, that's it. + * The output of this method will be a standard Java 2-d array. + * */ + String[] arrays = input.split("],\\["); + // CommonUtils.printArray_generic_type(arrays); + int size = arrays[1].split(",").length; + int[][] output = new int[arrays.length][size]; + for (int i = 0; i < arrays.length; i++) { + if (i == 0) { + String str = arrays[i].substring(1); + String[] nums = str.split(","); + for (int j = 0; j < nums.length; j++) { + output[i][j] = Integer.parseInt(nums[j]); + } + } else if (i == arrays.length - 1) { + String str = arrays[i].substring(0, arrays[i].length() - 1); + String[] nums = str.split(","); + for (int j = 0; j < nums.length; j++) { + output[i][j] = Integer.parseInt(nums[j]); + } + } else { + String[] nums = arrays[i].split(","); + for (int j = 0; j < nums.length; j++) { + output[i][j] = Integer.parseInt(nums[j]); + } + } + } + // CommonUtils.print2DIntArray(output); + return output; + } + + public static int[][] convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(String input) { + /* + * LeetCode 2-d array input usually comes like this: each row could have different length + * [[448,931,123,345],[889],[214,962],[576,746,897]] + * The expected input for this method is: "[448,931,123,345],[889],[214,962],[576,746,897]" + * i.e. strip off the beginning and ending square brackets, that's it. + * The output of this method will be a standard Java 2-d array. + * */ + String[] arrays = input.split("],\\["); + int maxLen = 0; + int[] sizes = new int[arrays.length]; + for (int i = 0; i < arrays.length; i++) { + String[] strs = arrays[i].split(","); + maxLen = Math.max(maxLen, strs.length); + sizes[i] = strs.length; + } + int[][] output = new int[arrays.length][]; + if (arrays.length == 1) { + String str = arrays[0].substring(1, arrays[0].length() - 1); + String[] nums = str.split(","); + output[0] = new int[sizes[0]]; + for (int j = 0; j < sizes[0]; j++) { + output[0][j] = Integer.parseInt(nums[j]); + } + } else { + for (int i = 0; i < arrays.length; i++) { + if (i == 0) { + String str = arrays[i].substring(1); + String[] nums = str.split(","); + output[i] = new int[sizes[i]]; + for (int j = 0; j < sizes[i]; j++) { + output[i][j] = Integer.parseInt(nums[j]); + } + } else if (i == arrays.length - 1) { + String str = arrays[i].substring(0, arrays[i].length() - 1); + String[] nums = str.split(","); + output[i] = new int[sizes[i]]; + for (int j = 0; j < sizes[i]; j++) { + output[i][j] = Integer.parseInt(nums[j]); + } + } else { + String[] nums = arrays[i].split(","); + output[i] = new int[sizes[i]]; + for (int j = 0; j < sizes[i]; j++) { + output[i][j] = Integer.parseInt(nums[j]); + } + } + } + } + return output; + } + + public static List> convertLeetCode2DStringArrayInputIntoJavaArray(String input) { + /* + * How to copy LeetCode 2-d String array into this method: + * 1. remove the beginning and ending quotes; + * 2. put double quotes into this method parameter; + * 3. copy the input into the double quotes. + * + * LeetCode 2-d array input usually comes like this: each row could have different length + * [["A","B"],["C"],["B","C"],["D"]] + * The expected input for this method is: "[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]" + * just copy the LeetCode input: ["A","B"],["C"],["B","C"],["D"] into double quotes in Java, + * it'll auto escape the double quotes. + * i.e. strip off the beginning and ending square brackets, that's it. + * The output of this method will be a standard Java 2-d array. + * */ + String[] arrays = input.split("],\\["); + List> result = new ArrayList<>(); + for (int i = 0; i < arrays.length; i++) { + List level = new ArrayList<>(); + String[] strings; + if (i == 0) { + strings = arrays[i].substring(1).split(","); + } else if (i == arrays.length - 1) { + strings = arrays[i].substring(0, arrays[i].length() - 1).split(","); + } else { + strings = arrays[i].split(","); + } + for (int j = 0; j < strings.length; j++) { + level.add(strings[j]); + } + result.add(level); + } + return result; + } + + public static List convertLeetCode1DStringArrayInputIntoJavaArray(String input) { + /* + * LeetCode 2-d array input usually comes like this: each row could have different length + * ["A","B","C"] + * The expected input for this method is: "[\"A\",\"B\",\"C\"]" + * just copy the LeetCode input: ["A","B","C"] into double quotes in Java, + * it'll auto escape the double quotes. + * The output of this method will be a standard Java 1-d array. + * */ + String[] arrays = input.split(","); + List result = new ArrayList<>(); + for (int i = 0; i < arrays.length; i++) { + String word; + if (i == 0) { + word = arrays[i].substring(1); + } else if (i == arrays.length - 1) { + word = arrays[i].substring(0, arrays[i].length() - 1); + } else { + word = arrays[i]; + } + result.add(word); + } + return result; + } } diff --git a/src/main/java/com/fishercoder/common/utils/LinkedListUtils.java b/src/main/java/com/fishercoder/common/utils/LinkedListUtils.java index 90d25c9412..f8f57c7e8f 100644 --- a/src/main/java/com/fishercoder/common/utils/LinkedListUtils.java +++ b/src/main/java/com/fishercoder/common/utils/LinkedListUtils.java @@ -1,6 +1,7 @@ package com.fishercoder.common.utils; import com.fishercoder.common.classes.ListNode; +import java.util.List; public class LinkedListUtils { @@ -17,4 +18,29 @@ public static ListNode contructLinkedList(int[] nums) { } return pre.next; } + + public static void printList(ListNode head) { + ListNode temp = head; + System.out.println(); + while (temp != null) { + System.out.print(temp.val() + "\t"); + temp = temp.next; + } + System.out.println(); + } + + public static ListNode createSinglyLinkedList(List listValues) { + if (listValues == null || listValues.size() == 0) { + throw new IllegalArgumentException( + "Please pass in a valid listValues to create a singly linked list."); + } + ListNode head = new ListNode(listValues.get(0)); + ListNode tmp = head; + for (int i = 1; i < listValues.size(); i++) { + ListNode next = new ListNode(listValues.get(i)); + tmp.next = next; + tmp = tmp.next; + } + return head; + } } diff --git a/src/main/java/com/fishercoder/common/utils/Notes.java b/src/main/java/com/fishercoder/common/utils/Notes.java index f7527b5c6a..9d890b892e 100644 --- a/src/main/java/com/fishercoder/common/utils/Notes.java +++ b/src/main/java/com/fishercoder/common/utils/Notes.java @@ -13,10 +13,10 @@ String issue() default ""; String context() default ""; // this variable is used to state how I solved + // this problem, whether completely made it // myself, or copied it from online, or a // combination of both approaches. boolean needsReview() default true; - } diff --git a/src/main/java/com/fishercoder/common/utils/TreeUtils.java b/src/main/java/com/fishercoder/common/utils/TreeUtils.java index 458e030037..d2e75fb872 100644 --- a/src/main/java/com/fishercoder/common/utils/TreeUtils.java +++ b/src/main/java/com/fishercoder/common/utils/TreeUtils.java @@ -1,7 +1,6 @@ package com.fishercoder.common.utils; import com.fishercoder.common.classes.TreeNode; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -9,154 +8,153 @@ import java.util.List; import java.util.Queue; -/** +/* * This is a util class to contain all tree related methods. */ public class TreeUtils { -/** -* This method is to construct a normal binary tree. The input reads like -* this for [5, 3, 6, 2, 4, null, null, 1], i.e. preorder: - 5 - / \ - 3 6 - / \ / \ - 2 4 # # - / - 1 -*/ -@Notes(context = "This is usually how Leetcode OJ passes a binary tree into testing: " - + "https://leetcode.com/faq/#binary-tree, I wrote this function for my own ease of testing when copying" - + "the test case from Leetcode in the form of [1, null, 2, 3].") -public static TreeNode constructBinaryTree(List treeValues) { - TreeNode root = new TreeNode(treeValues.get(0)); - Queue queue = new LinkedList<>(); - queue.offer(root); - for (int i = 1; i < treeValues.size(); i++) { - TreeNode curr = queue.poll(); - if (treeValues.get(i) != null) { - curr.left = new TreeNode(treeValues.get(i)); - queue.offer(curr.left); - } - if (++i < treeValues.size() && treeValues.get(i) != null) { - curr.right = new TreeNode(treeValues.get(i)); - queue.offer(curr.right); - } - } - return root; -} - - public static void printBinaryTree(TreeNode root) { - CommonUtils.println("\nPrinting out the binary tree in a very visual manner as below:\n"); - - // imitating from BTreePrinter class - int maxLevel = TreeUtils.maxLevel(root); - - printNodeInternal(Collections.singletonList(root), 1, maxLevel); - } - - private static int maxLevel(TreeNode root) { - if (root == null) { - return 0; - } - - return Math.max(TreeUtils.maxLevel(root.left), - TreeUtils.maxLevel(root.right)) + 1; - } - - private static void printNodeInternal( - List list, int level, int maxLevel) { - if (list.isEmpty() || CommonUtils.isAllElementsNull(list)) { - return; - } - - int floor = maxLevel - level; - int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0))); - int firstSpaces = (int) Math.pow(2, (floor)) - 1; - int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1; - - CommonUtils.printWhitespaces(firstSpaces); - - List newNodes = new ArrayList<>(); - for (TreeNode node : list) { - if (node != null) { - System.out.print(node.val); - newNodes.add(node.left); - newNodes.add(node.right); - } else { - newNodes.add(null); - newNodes.add(null); - System.out.print(" "); - } - - CommonUtils.printWhitespaces(betweenSpaces); - } - System.out.println(""); - - for (int i = 1; i <= endgeLines; i++) { - for (int j = 0; j < list.size(); j++) { - CommonUtils.printWhitespaces(firstSpaces - i); - if (list.get(j) == null) { - CommonUtils.printWhitespaces(endgeLines + endgeLines + i - + 1); - continue; - } - - if (list.get(j).left != null) { - System.out.print("/"); - } else { - CommonUtils.printWhitespaces(1); - } - - CommonUtils.printWhitespaces(i + i - 1); - - if (list.get(j).right != null) { - System.out.print("\\"); - } else { - CommonUtils.printWhitespaces(1); - } - - CommonUtils.printWhitespaces(endgeLines + endgeLines - i); - } - - System.out.println(""); - } - - printNodeInternal(newNodes, level + 1, maxLevel); - } - - public static void inOrderTraversal(TreeNode root) { - inOrder(root); - } - - private static void inOrder(TreeNode root) { - if (root == null) { - return; - } - inOrder(root.left); - System.out.print(root.val + " "); - inOrder(root.right); - } - - public static void main(String... args) { - //test random int generator - List treeValues = CommonUtils.randomIntArrayGenerator(24); - - List treeValues2 = Arrays.asList(0, 1, 2, 3, 4, 5, 6); - - //test tree construction - // TreeNode root1 = bruteForceConstructBinaryTree(treeValues2); - // inOrderTraversal(root1); - // printBinaryTree(root1); - - // test tree construction - TreeNode root2 = constructBinaryTree(treeValues); - inOrderTraversal(root2); - printBinaryTree(root2); - - List treeVals = new ArrayList<>(Arrays.asList(1, null, 2, 3)); - CommonUtils.printList(treeVals); - root2 = constructBinaryTree(treeVals); - // inOrderTraversal(root2); - printBinaryTree(root2); - } + /* + * This method is to construct a normal binary tree. The input reads like + * this for [5, 3, 6, 2, 4, null, null, 1], i.e. preorder: + 5 + / \ + 3 6 + / \ / \ + 2 4 # # + / + 1 + */ + @Notes( + context = + "This is usually how Leetcode OJ passes a binary tree into testing: " + + "https://leetcode.com/faq/#binary-tree, I wrote this function for my own ease of testing when copying" + + "the test case from Leetcode in the form of [1, null, 2, 3].") + public static TreeNode constructBinaryTree(List treeValues) { + TreeNode root = new TreeNode(treeValues.get(0)); + Queue queue = new LinkedList<>(); + queue.offer(root); + for (int i = 1; i < treeValues.size(); i++) { + TreeNode curr = queue.poll(); + if (treeValues.get(i) != null) { + curr.left = new TreeNode(treeValues.get(i)); + queue.offer(curr.left); + } + if (++i < treeValues.size() && treeValues.get(i) != null) { + curr.right = new TreeNode(treeValues.get(i)); + queue.offer(curr.right); + } + } + return root; + } + + public static void printBinaryTree(TreeNode root) { + CommonUtils.println("\nPrinting out the binary tree in a very visual manner as below:\n"); + + // imitating from BTreePrinter class + int maxLevel = TreeUtils.maxLevel(root); + + printNodeInternal(Collections.singletonList(root), 1, maxLevel); + } + + private static int maxLevel(TreeNode root) { + if (root == null) { + return 0; + } + + return Math.max(TreeUtils.maxLevel(root.left), TreeUtils.maxLevel(root.right)) + 1; + } + + private static void printNodeInternal(List list, int level, int maxLevel) { + if (list.isEmpty() || CommonUtils.isAllElementsNull(list)) { + return; + } + + int floor = maxLevel - level; + int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0))); + int firstSpaces = (int) Math.pow(2, (floor)) - 1; + int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1; + + CommonUtils.printWhitespaces(firstSpaces); + + List newNodes = new ArrayList<>(); + for (TreeNode node : list) { + if (node != null) { + System.out.print(node.val); + newNodes.add(node.left); + newNodes.add(node.right); + } else { + newNodes.add(null); + newNodes.add(null); + System.out.print(" "); + } + + CommonUtils.printWhitespaces(betweenSpaces); + } + System.out.println(""); + + for (int i = 1; i <= endgeLines; i++) { + for (int j = 0; j < list.size(); j++) { + CommonUtils.printWhitespaces(firstSpaces - i); + if (list.get(j) == null) { + CommonUtils.printWhitespaces(endgeLines + endgeLines + i + 1); + continue; + } + + if (list.get(j).left != null) { + System.out.print("/"); + } else { + CommonUtils.printWhitespaces(1); + } + + CommonUtils.printWhitespaces(i + i - 1); + + if (list.get(j).right != null) { + System.out.print("\\"); + } else { + CommonUtils.printWhitespaces(1); + } + + CommonUtils.printWhitespaces(endgeLines + endgeLines - i); + } + + System.out.println(""); + } + + printNodeInternal(newNodes, level + 1, maxLevel); + } + + public static void inOrderTraversal(TreeNode root) { + inOrder(root); + } + + private static void inOrder(TreeNode root) { + if (root == null) { + return; + } + inOrder(root.left); + System.out.print(root.val + " "); + inOrder(root.right); + } + + public static void main(String... args) { + // test random int generator + List treeValues = CommonUtils.randomIntArrayGenerator(24); + + List treeValues2 = Arrays.asList(0, 1, 2, 3, 4, 5, 6); + + // test tree construction + // TreeNode root1 = bruteForceConstructBinaryTree(treeValues2); + // inOrderTraversal(root1); + // printBinaryTree(root1); + + // test tree construction + TreeNode root2 = constructBinaryTree(treeValues); + inOrderTraversal(root2); + printBinaryTree(root2); + + List treeVals = new ArrayList<>(Arrays.asList(1, null, 2, 3)); + CommonUtils.printList(treeVals); + root2 = constructBinaryTree(treeVals); + // inOrderTraversal(root2); + printBinaryTree(root2); + } } diff --git a/src/main/java/com/fishercoder/solutions/_1.java b/src/main/java/com/fishercoder/solutions/_1.java deleted file mode 100644 index 5e8c232568..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -public class _1 { - - public static class Solution1 { - public int[] twoSum(int[] nums, int target) { - Map map = new HashMap(); - for (int i = 0; i < nums.length; i++) { - if (map.containsKey(target - nums[i])) { - return new int[]{map.get(target - nums[i]), i}; - } else { - map.put(nums[i], i); - } - } - return new int[]{-1, -1}; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_1002.java b/src/main/java/com/fishercoder/solutions/_1002.java deleted file mode 100644 index eae905843b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1002.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1002. Find Common Characters - * - * Given an array A of strings made only from lowercase letters, - * return a list of all characters that show up in all strings within the list (including duplicates). - * For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer. - * - * You may return the answer in any order. - * - * Example 1: - * Input: ["bella","label","roller"] - * Output: ["e","l","l"] - * - * Example 2: - * Input: ["cool","lock","cook"] - * Output: ["c","o"] - * - * Note: - * 1 <= A.length <= 100 - * 1 <= A[i].length <= 100 - * A[i][j] is a lowercase letter - */ -public class _1002 { - public static class Solution1 { - public List commonChars(String[] A) { - int[][] charCount = new int[A.length][26]; - for (int i = 0; i < A.length; i++) { - for (char c : A[i].toCharArray()) { - charCount[i][c - 'a']++; - } - } - List result = new ArrayList<>(); - for (int i = 0; i < 26; i++) { - while (charCount[0][i] != 0) { - char c = (char) (i + 'a'); - boolean valid = true; - charCount[0][i]--; - for (int j = 1; j < A.length; j++) { - if (charCount[j][i] == 0) { - valid = false; - break; - } else { - charCount[j][i]--; - } - } - if (!valid) { - break; - } else { - result.add("" + c); - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1003.java b/src/main/java/com/fishercoder/solutions/_1003.java deleted file mode 100644 index e339d24aed..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1003.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Deque; - -/** - * 1003. Check If Word Is Valid After Substitutions - * - * We are given that the string "abc" is valid. - * - * From any valid string V, we may split V into two pieces X and Y such that X + Y (X concatenated with Y) is equal to V. (X or Y may be empty.) Then, X + "abc" + Y is also valid. - * - * If for example S = "abc", then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc". Examples of invalid strings are: "abccba", "ab", "cababc", "bac". - * - * Return true if and only if the given string S is valid. - * - * Note: - * * 1 <= S.length <= 20000 - * * S[i] is 'a', 'b', or 'c' - */ - -public class _1003 { - public static class Solution1 { - public boolean isValid(String S) { - Deque stack = new ArrayDeque<>(); - - for (char c : S.toCharArray()) { - if (c == 'c') { - if (stack.isEmpty() || stack.pop() != 'b') { - return false; - } - if (stack.isEmpty() || stack.pop() != 'a') { - return false; - } - } else { - stack.push(c); - } - } - - return stack.isEmpty(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1005.java b/src/main/java/com/fishercoder/solutions/_1005.java deleted file mode 100644 index 997ab019c4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1005.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.PriorityQueue; - -/** - * 1005. Maximize Sum Of Array After K Negations - * - * Given an array A of integers, we must modify the array in the following way: - * we choose an i and replace A[i] with -A[i], and we repeat this process K times in total. - * (We may choose the same index i multiple times.) - * - * Return the largest possible sum of the array after modifying it in this way. - * - * Example 1: - * Input: A = [4,2,3], K = 1 - * Output: 5 - * Explanation: Choose indices (1,) and A becomes [4,-2,3]. - * - * Example 2: - * Input: A = [3,-1,0,2], K = 3 - * Output: 6 - * Explanation: Choose indices (1, 2, 2) and A becomes [3,1,0,2]. - * Example 3: - * - * Input: A = [2,-3,-1,5,-4], K = 2 - * Output: 13 - * Explanation: Choose indices (1, 4) and A becomes [2,3,-1,5,4]. - * - * - * Note: - * - * 1 <= A.length <= 10000 - * 1 <= K <= 10000 - * -100 <= A[i] <= 100 - * */ -public class _1005 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/discuss/252228/A-very-simple-java-solution*/ - public int largestSumAfterKNegations(int[] A, int K) { - PriorityQueue minHeap = new PriorityQueue<>(); - for (int num : A) { - minHeap.offer(num); - } - while (K-- > 0) { - minHeap.offer(-minHeap.poll()); - } - int sum = 0; - while (!minHeap.isEmpty()) { - sum += minHeap.poll(); - } - return sum; - } - } - - public static class Solution2 { - /**credit: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/discuss/252254/JavaC%2B%2BPython-Sort*/ - public int largestSumAfterKNegations(int[] A, int K) { - Arrays.sort(A); - for (int i = 0; i < A.length && K > 0 && A[i] < 0; i++, K--) { - A[i] = -A[i]; - } - int sum = 0; - int min = Integer.MAX_VALUE; - for (int i = 0; i < A.length; i++) { - min = Math.min(min, A[i]); - sum += A[i]; - } - return K % 2 == 0 ? sum : sum - min * 2; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1008.java b/src/main/java/com/fishercoder/solutions/_1008.java deleted file mode 100644 index a72e0f5406..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1008.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 1008. Construct Binary Search Tree from Preorder Traversal - * - * Return the root node of a binary search tree that matches the given preorder traversal. - * (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val. - * Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) - * - * Example 1: - * - * Input: [8,5,1,7,10,12] - * Output: [8,5,10,1,7,null,12] - * - * 8 - * / \ - * 5 10 - * / \ \ - * 1 7 12 - * - * Note: - * - * 1 <= preorder.length <= 100 - * The values of preorder are distinct. - * */ -public class _1008 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/discuss/252232/JavaC%2B%2BPython-O(N)-Solution*/ - int i = 0; - - public TreeNode bstFromPreorder(int[] preorder) { - return bstFromPreorder(preorder, Integer.MAX_VALUE); - } - - private TreeNode bstFromPreorder(int[] preorder, int bound) { - if (i == preorder.length || preorder[i] > bound) { - return null; - } - TreeNode root = new TreeNode(preorder[i++]); - root.left = bstFromPreorder(preorder, root.val); - root.right = bstFromPreorder(preorder, bound); - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1009.java b/src/main/java/com/fishercoder/solutions/_1009.java deleted file mode 100644 index a89af95f85..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1009.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1009. Complement of Base 10 Integer - * - * Every non-negative integer N has a binary representation. - * For example, 5 can be represented as "101" in binary, - * 11 as "1011" in binary, and so on. - * - * Note that except for N = 0, there are no leading zeroes in any binary representation. - * - * The complement of a binary representation is the number in binary you get when - * changing every 1 to a 0 and 0 to a 1. For example, the complement of "101" in binary is "010" in binary. - * - * For a given number N in base-10, return the complement of it's binary representation as a base-10 integer. - * - * Example 1: - * Input: 5 - * Output: 2 - * Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10. - * - * Example 2: - * Input: 7 - * Output: 0 - * Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10. - * - * Example 3: - * Input: 10 - * Output: 5 - * Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10. - * - * Note: - * 0 <= N < 10^9 - * */ -public class _1009 { - public static class Solution1 { - public int bitwiseComplement(int N) { - if (N == 0) { - return 1; - } - List list = new ArrayList<>(); - while (N != 0) { - list.add(N & 1); - N >>= 1; - } - int result = 0; - int exp = list.size() - 1; - for (int i = list.size() - 1; i >= 0; i--) { - if (list.get(i) == 0) { - result += Math.pow(2, exp); - } - exp--; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_101.java b/src/main/java/com/fishercoder/solutions/_101.java deleted file mode 100644 index 1e7442888b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_101.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -public class _101 { - public static class Solution1 { - public boolean isSymmetric(TreeNode root) { - if (root == null) { - return true; - } - return isSymmetric(root.left, root.right); - } - - private boolean isSymmetric(TreeNode left, TreeNode right) { - if (left == null || right == null) { - return left == right; - } - return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1010.java b/src/main/java/com/fishercoder/solutions/_1010.java deleted file mode 100644 index 8a8b2f1551..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1010.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 1010. Pairs of Songs With Total Durations Divisible by 60 - * - * In a list of songs, the i-th song has a duration of time[i] seconds. - * - * Return the number of pairs of songs for which their total duration in seconds is divisible by 60. - * Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0. - * - * Example 1: - * Input: [30,20,150,100,40] - * Output: 3 - * Explanation: Three pairs have a total duration divisible by 60: - * (time[0] = 30, time[2] = 150): total duration 180 - * (time[1] = 20, time[3] = 100): total duration 120 - * (time[1] = 20, time[4] = 40): total duration 60 - * - * Example 2: - * Input: [60,60,60] - * Output: 3 - * Explanation: All three pairs have a total duration of 120, which is divisible by 60. - * - * Note: - * - * 1 <= time.length <= 60000 - * 1 <= time[i] <= 500 - * */ -public class _1010 { - public static class Solution1 { - /**Credit: https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/discuss/256726/Java-O(n)-code-w-comment-similar-to-Two-Sum - * - * Think of Problem 1: Two Sum - * Assume target is 60, each item in time % 60. - * Then this problem becomes very similar to Problem 1. - * */ - public int numPairsDivisibleBy60(int[] time) { - int result = 0; - Map map = new HashMap<>(); - for (int t : time) { - int d = (60 - t % 60) % 60; - if (map.containsKey(d)) { - result += map.get(d); - } - map.put(t % 60, map.getOrDefault(t % 60, 0) + 1); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1011.java b/src/main/java/com/fishercoder/solutions/_1011.java deleted file mode 100644 index 7b0f1e51f5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1011.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1011. Capacity To Ship Packages Within D Days - * - * A conveyor belt has packages that must be shipped from one port to another within D days. - * - * The i-th package on the conveyor belt has a weight of weights[i]. - * Each day, we load the ship with packages on the conveyor belt (in the order given by weights). - * We may not load more weight than the maximum weight capacity of the ship. - * - * Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days. - * - * Note: - * * 1 <= D <= weights.length <= 50000 - * * 1 <= weights[i] <= 500 - */ - -public class _1011 { - public static class Solution1 { - public int daysToShip(int[] weights, int capacity) { - int days = 0; - int currentShip = 0; - - for (int k : weights) { - if (currentShip + k > capacity) { - currentShip = 0; - days += 1; - } - currentShip += k; - } - - return days + 1; - } - - public int shipWithinDays(int[] weights, int D) { - - int sum = 0; - int max = 0; - - for (int k : weights) { - sum += k; - max = Math.max(max, k); - } - - // Minimum possible capacity needs to be as much as the heaviest package - int lower = max; - // Maximum possible capacity is the total weight of all packages - int upper = sum; - - if (daysToShip(weights, lower) <= D) { - return lower; - } - - // Guess is for capacity - int currentGuess; - int bestGuess = -1; - - // Binary search - while (lower <= upper) { - currentGuess = (upper + lower) / 2; - if (daysToShip(weights, currentGuess) <= D) { - bestGuess = currentGuess; - upper = currentGuess - 1; - } else { - lower = currentGuess + 1; - } - } - - return bestGuess; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1013.java b/src/main/java/com/fishercoder/solutions/_1013.java deleted file mode 100644 index c33ef2b6c4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1013.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1013. Partition Array Into Three Parts With Equal Sum - * - * Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums. - * Formally, we can partition the array if we can find indexes i+1 < j with - * (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1]) - * - * Example 1: - * Input: [0,2,1,-6,6,-7,9,1,2,0,1] - * Output: true - * Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1 - * - * Example 2: - * Input: [0,2,1,-6,6,7,9,-1,2,0,1] - * Output: false - * - * Example 3: - * Input: [3,3,6,5,-2,2,5,1,-9,4] - * Output: true - * Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4 - * - * Note: - * - * 3 <= A.length <= 50000 - * -10000 <= A[i] <= 10000*/ -public class _1013 { - public static class Solution1 { - public boolean canThreePartsEqualSum(int[] A) { - int sum = 0; - for (int i = 0; i < A.length; i++) { - sum += A[i]; - } - if (sum % 3 != 0) { - return false; - } - int equalSum = sum / 3; - int left = 0; - int leftSum = 0; - while (left < A.length - 2 && leftSum != equalSum) { - leftSum += A[left++]; - } - if (left > A.length - 2 || leftSum != equalSum) { - return false; - } - - int right = A.length - 1; - int rightSum = 0; - while (right > left && rightSum != equalSum) { - rightSum += A[right--]; - } - if (right < left || rightSum != equalSum) { - return false; - } - int middleSum = 0; - for (int i = left; i <= right; i++) { - middleSum += A[i]; - } - return middleSum == equalSum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1014.java b/src/main/java/com/fishercoder/solutions/_1014.java deleted file mode 100644 index fe77137f04..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1014.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1014. Best Sightseeing Pair - * - * Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, - * and two sightseeing spots i and j have distance j - i between them. - * - * The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : - * the sum of the values of the sightseeing spots, minus the distance between them. - * - * Return the maximum score of a pair of sightseeing spots. - * - * Note: - * * 2 <= A.length <= 50000 - * * 1 <= A[i] <= 1000 - */ - -public class _1014 { - public static class Solution1 { - public int maxScoreSightseeingPair(int[] A) { - int bestPrevious = A[0]; - int maxSum = 0; - - for (int i = 1; i < A.length; ++i) { - maxSum = Math.max(maxSum, bestPrevious + A[i] - i); - bestPrevious = Math.max(bestPrevious, A[i] + i); - } - - return maxSum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1018.java b/src/main/java/com/fishercoder/solutions/_1018.java deleted file mode 100644 index 40e5307ef4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1018.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1018. Binary Prefix Divisible By 5 - * - * Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-bit to least-significant-bit.) - * - * Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5. - * - * Example 1: - * - * Input: [0,1,1] - * Output: [true,false,false] - * Explanation: - * The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true. - * Example 2: - * - * Input: [1,1,1] - * Output: [false,false,false] - * Example 3: - * - * Input: [0,1,1,1,1,1] - * Output: [true,false,false,false,true,false] - * Example 4: - * - * Input: [1,1,1,0,1] - * Output: [false,false,false,false,false] - * - * - * Note: - * - * 1 <= A.length <= 30000 - * A[i] is 0 or 1 - * */ -public class _1018 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/266051/Java-beats-100*/ - public List prefixesDivBy5(int[] A) { - List result = new ArrayList<>(A.length); - int remainder = 0; - for (int a : A) { - remainder = ((remainder << 1) + a) % 5; - result.add(remainder == 0); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1019.java b/src/main/java/com/fishercoder/solutions/_1019.java deleted file mode 100644 index ce53a99bad..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1019.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 1019. Next Greater Node In Linked List - * - * We are given a linked list with head as the first node. Let's number the nodes in the list: node_1, node_2, node_3, ... etc. - * Each node may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, - * and j is the smallest possible choice. If such a j does not exist, the next larger value is 0. - * Return an array of integers answer, where answer[i] = next_larger(node_{i+1}). - * Note that in the example inputs (not outputs) below, arrays such as [2,1,5] represent the serialization of a linked list - * with a head node value of 2, second node value of 1, and third node value of 5. - * - * Example 1: - * Input: [2,1,5] - * Output: [5,5,0] - * - * Example 2: - * Input: [2,7,4,3,5] - * Output: [7,0,5,5,0] - * - * Example 3: - * Input: [1,7,5,1,9,2,5,1] - * Output: [7,9,9,9,0,5,0,0] - * - * Note: - * 1 <= node.val <= 10^9 for each node in the linked list. - * The given list has length in the range [0, 10000]. - * */ -public class _1019 { - public static class Solution1 { - public int[] nextLargerNodes(ListNode head) { - int len = findLength(head); - int[] result = new int[len]; - ListNode tmp = head; - int i = 0; - while (tmp != null) { - result[i++] = findNextLarger(tmp.val, tmp); - tmp = tmp.next; - } - return result; - } - - private int findNextLarger(int val, ListNode head) { - ListNode tmp = head.next; - while (tmp != null) { - if (tmp.val > val) { - return tmp.val; - } - tmp = tmp.next; - } - return 0; - } - - private int findLength(ListNode head) { - ListNode tmp = head; - int count = 0; - while (tmp != null) { - tmp = tmp.next; - count++; - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1021.java b/src/main/java/com/fishercoder/solutions/_1021.java deleted file mode 100644 index 8f3bc1a31a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1021.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/**1021. Remove Outermost Parentheses - * - * A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings. - * - * A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings. - * - * Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings. - * - * Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S. - * - * - * Example 1: - * Input: "(()())(())" - * Output: "()()()" - * Explanation: - * The input string is "(()())(())", with primitive decomposition "(()())" + "(())". - * After removing outer parentheses of each part, this is "()()" + "()" = "()()()". - * - * Example 2: - * Input: "(()())(())(()(()))" - * Output: "()()()()(())" - * Explanation: - * The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))". - * After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())". - * - * Example 3: - * Input: "()()" - * Output: "" - * Explanation: - * The input string is "()()", with primitive decomposition "()" + "()". - * After removing outer parentheses of each part, this is "" + "" = "". - * - * Note: - * - * S.length <= 10000 - * S[i] is "(" or ")" - * S is a valid parentheses string - * */ -public class _1021 { - public static class Solution1 { - public String removeOuterParentheses(String S) { - List primitives = new ArrayList<>(); - for (int i = 1; i < S.length(); i++) { - int initialI = i - 1; - int left = 1; - while (i < S.length() && left > 0) { - if (S.charAt(i) == '(') { - left++; - } else { - left--; - } - i++; - } - primitives.add(S.substring(initialI, i)); - } - StringBuilder sb = new StringBuilder(); - for (String primitive : primitives) { - sb.append(primitive.substring(1, primitive.length() - 1)); - } - return sb.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1022.java b/src/main/java/com/fishercoder/solutions/_1022.java deleted file mode 100644 index c98eed5624..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1022.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * Source: https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ - * - * 1022. Sum of Root To Leaf Binary Numbers - * - * Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13. - * - * For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. - * - * Return the sum of these numbers. - * - * Example 1: - * - * 1 - * / \ - * 0 1 - * / \ / \ - * 0 1 0 1 - * - * Input: [1,0,1,0,1,0,1] - * Output: 22 - * Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22 - * - * Note: - * - * The number of nodes in the tree is between 1 and 1000. - * node.val is 0 or 1. - * The answer will not exceed 2^31 - 1. - * */ -public class _1022 { - public static class Solution1 { - public int sumRootToLeaf(TreeNode root) { - List> paths = new ArrayList<>(); - dfs(root, paths, new ArrayList<>()); - int sum = 0; - for (List list : paths) { - int num = 0; - for (int i : list) { - num = (num << 1) + i; - } - sum += num; - } - return sum; - } - - private void dfs(TreeNode root, List> paths, List path) { - path.add(root.val); - if (root.left != null) { - dfs(root.left, paths, path); - path.remove(path.size() - 1); - } - if (root.right != null) { - dfs(root.right, paths, path); - path.remove(path.size() - 1); - } - if (root.left == null && root.right == null) { - paths.add(new ArrayList<>(path)); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1025.java b/src/main/java/com/fishercoder/solutions/_1025.java deleted file mode 100644 index 26be5b95fd..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1025.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1025. Divisor Game - * - * Alice and Bob take turns playing a game, with Alice starting first. - * Initially, there is a number N on the chalkboard. On each player's turn, that player makes a move consisting of: - * Choosing any x with 0 < x < N and N % x == 0. - * Replacing the number N on the chalkboard with N - x. - * Also, if a player cannot make a move, they lose the game. - * Return True if and only if Alice wins the game, assuming both players play optimally. - * - * Example 1: - * Input: 2 - * Output: true - * Explanation: Alice chooses 1, and Bob has no more moves. - * - * Example 2: - * Input: 3 - * Output: false - * Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves. - * - * Note: - * 1 <= N <= 1000 - * */ -public class _1025 { - public static class Solution1 { - public boolean divisorGame(int N) { - //TODO: implement it - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1029.java b/src/main/java/com/fishercoder/solutions/_1029.java deleted file mode 100644 index 558a50b1fc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1029.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1029. Two City Scheduling - * - * There are 2N people a company is planning to interview. - * The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[i][1]. - * Return the minimum cost to fly every person to a city such that exactly N people arrive in each city. - * - * Example 1: - * - * Input: [[10,20],[30,200],[400,50],[30,20]] - * Output: 110 - * Explanation: - * The first person goes to city A for a cost of 10. - * The second person goes to city A for a cost of 30. - * The third person goes to city B for a cost of 50. - * The fourth person goes to city B for a cost of 20. - * - * The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city. - * - * Note: - * - * 1 <= costs.length <= 100 - * It is guaranteed that costs.length is even. - * 1 <= costs[i][0], costs[i][1] <= 1000 - * */ -public class _1029 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/two-city-scheduling/discuss/280173/Java-4-lines-intuitive-solution - * and - * https://leetcode.com/problems/two-city-scheduling/discuss/278771/Java-sort-solution*/ - public int twoCitySchedCost(int[][] costs) { - Arrays.sort(costs, (a, b) -> (a[0] - a[1] - (b[0] - b[1]))); - int cost = 0; - for (int i = 0; i < costs.length; i++) { - if (i < costs.length / 2) { - cost += costs[i][0]; - } else { - cost += costs[i][1]; - } - } - return cost; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_103.java b/src/main/java/com/fishercoder/solutions/_103.java deleted file mode 100644 index 8c209a3744..0000000000 --- a/src/main/java/com/fishercoder/solutions/_103.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -public class _103 { - public static class Solution1 { - public List> zigzagLevelOrder(TreeNode root) { - Queue q = new LinkedList(); - List> levels = new ArrayList(); - if (root == null) { - return levels; - } - q.offer(root); - boolean forward = true; - while (!q.isEmpty()) { - int size = q.size(); - List level = new ArrayList(); - for (int i = 0; i < size; i++) { - TreeNode curr = q.poll(); - level.add(curr.val); - if (curr.left != null) { - q.offer(curr.left); - } - if (curr.right != null) { - q.offer(curr.right); - } - } - if (forward) { - forward = false; - levels.add(level); - } else { - Collections.reverse(level); - levels.add(level); - forward = true; - } - } - return levels; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1030.java b/src/main/java/com/fishercoder/solutions/_1030.java deleted file mode 100644 index 1d26f879ba..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1030.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -/** - * 1030. Matrix Cells in Distance Order - * - * We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C. - * - * Additionally, we are given a cell in that matrix with coordinates (r0, c0). - * - * Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that satisfies this condition.) - * - * Example 1: - * Input: R = 1, C = 2, r0 = 0, c0 = 0 - * Output: [[0,0],[0,1]] - * Explanation: The distances from (r0, c0) to other cells are: [0,1] - * - * Example 2: - * Input: R = 2, C = 2, r0 = 0, c0 = 1 - * Output: [[0,1],[0,0],[1,1],[1,0]] - * Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2] - * The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct. - * - * Example 3: - * Input: R = 2, C = 3, r0 = 1, c0 = 2 - * Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]] - * Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3] - * There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]]. - * - * Note: - * - * 1 <= R <= 100 - * 1 <= C <= 100 - * 0 <= r0 < R - * 0 <= c0 < C - * */ -public class _1030 { - public static class Solution1 { - public int[][] allCellsDistOrder(int R, int C, int r0, int c0) { - int[][] result = new int[R * C][2]; - Queue queue = new LinkedList<>(); - queue.offer(new int[]{r0, c0}); - boolean[][] visited = new boolean[R][C]; - int i = 0; - while (!queue.isEmpty()) { - int[] cell = queue.poll(); - int row = cell[0]; - int col = cell[1]; - - if (row < 0 || row >= R || col < 0 || col >= C || visited[row][col]) { - continue; - } - - visited[row][col] = true; - result[i++] = new int[]{row, col}; - queue.offer(new int[]{row, col + 1}); - queue.offer(new int[]{row + 1, col}); - queue.offer(new int[]{row - 1, col}); - queue.offer(new int[]{row, col - 1}); - - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1033.java b/src/main/java/com/fishercoder/solutions/_1033.java deleted file mode 100644 index 3c4759ae48..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1033.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1033. Moving Stones Until Consecutive - * - * Three stones are on a number line at positions a, b, and c. - * - * Each turn, you pick up a stone at an endpoint (ie., either the lowest or highest position stone), - * and move it to an unoccupied position between those endpoints. - * Formally, let's say the stones are currently at positions x, y, z with x < y < z. - * You pick up the stone at either position x or position z, and move that stone to an integer position k, with x < k < z and k != y. - * - * The game ends when you cannot make any more moves, ie. the stones are in consecutive positions. - * - * When the game ends, what is the minimum and maximum number of moves that you could have made? - * Return the answer as an length 2 array: answer = [minimum_moves, maximum_moves] - * - * Note: - * * 1 <= a <= 100 - * * 1 <= b <= 100 - * * 1 <= c <= 100 - * * a != b, b != c, c != a - */ - -public class _1033 { - public static class Solution1 { - private int minMoves(int x, int y, int z) { - // already consecutive integers, nothing to be done - if (x + 1 == y && y + 1 == z) { - return 0; - } - // one of the following (sample) cases: - // 1, 2, 8 (8 -> 3) - // 1, 7, 8 (1 -> 6) - // 1, 3, 8 (8 -> 2) - // 1, 6, 8 (1 -> 7) - if (y - x <= 2 || z - y <= 2) { - return 1; - } - - // move z to y + 1, x to y - 1 - return 2; - } - - private int maxMoves(int x, int y, int z) { - return z - x - 2; - } - - public int[] numMovesStones(int a, int b, int c) { - int[] t = {a, b, c}; - Arrays.sort(t); - - int min = minMoves(t[0], t[1], t[2]); - int max = maxMoves(t[0], t[1], t[2]); - - return new int[]{min, max}; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1037.java b/src/main/java/com/fishercoder/solutions/_1037.java deleted file mode 100644 index d20efa7d46..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1037.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1037. Valid Boomerang - * - * A boomerang is a set of 3 points that are all distinct and not in a straight line. - * - * Given a list of three points in the plane, return whether these points are a boomerang. - * - * Example 1: - * Input: [[1,1],[2,3],[3,2]] - * Output: true - * - * Example 2: - * Input: [[1,1],[2,2],[3,3]] - * Output: false - * - * Note: - * points.length == 3 - * points[i].length == 2 - * 0 <= points[i][j] <= 100 - * */ -public class _1037 { - public static class Solution1 { - public boolean isBoomerang(int[][] points) { - return (points[1][1] - points[0][1]) * (points[2][0] - points[0][0]) != (points[2][1] - points[0][1]) * (points[1][0] - points[0][0]); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1038.java b/src/main/java/com/fishercoder/solutions/_1038.java deleted file mode 100644 index 6f810f8bed..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1038.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 1038. Binary Search Tree to Greater Sum Tree - * - * Given the root of a binary search tree with distinct values, - * modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val. - * As a reminder, a binary search tree is a tree that satisfies these constraints: - * - * The left subtree of a node contains only nodes with keys less than the node's key. - * The right subtree of a node contains only nodes with keys greater than the node's key. - * Both the left and right subtrees must also be binary search trees. - * - * - * Example 1: - * 4(30) - * / \ - * 1(36) 6(21) - * / \ / \ - * 0(36) 2(35) 5(26) 7(15) - * \ \ - * 3(33) 8(8) - * - * Input: [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] - * Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8] - * - * - * Note: - * - * The number of nodes in the tree is between 1 and 100. - * Each node will have value between 0 and 100. - * The given tree is a binary search tree.*/ -public class _1038 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/discuss/286725/JavaC%2B%2BPython-Revered-Inorder-Traversal*/ - int greaterSum = 0; - - public TreeNode bstToGst(TreeNode root) { - if (root.right != null) { - bstToGst(root.right); - } - greaterSum = root.val = greaterSum + root.val; - if (root.left != null) { - bstToGst(root.left); - } - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_104.java b/src/main/java/com/fishercoder/solutions/_104.java deleted file mode 100644 index 27d3f3b0c5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_104.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; - -public class _104 { - - public static class Solution1 { - /** - * Recursive solution: - * Time: O(n) - * Space: O(n) - */ - public int maxDepth(TreeNode root) { - if (root == null) { - return 0; - } - return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; - } - } - - public static class Solution2 { - /** - * Iterative solution: - * Time: O(n) - * Space: O(n) - */ - public int maxDepth(TreeNode root) { - if (root == null) { - return 0; - } - LinkedList stack = new LinkedList<>(); - LinkedList depths = new LinkedList<>(); - stack.add(root); - depths.add(1); - - int depth = 0; - while (!stack.isEmpty()) { - TreeNode currentNode = stack.pollLast(); - int currentDepth = depths.pollLast(); - if (currentNode != null) { - depth = Math.max(depth, currentDepth); - stack.add(currentNode.right); - depths.add(currentDepth + 1); - stack.add(currentNode.left); - depths.add(currentDepth + 1); - } - } - return depth; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_1043.java b/src/main/java/com/fishercoder/solutions/_1043.java deleted file mode 100644 index dd2c706b99..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1043.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1043. Partition Array for Maximum Sum - * - * Given an integer array A, you partition the array into (contiguous) subarrays of length at most K. After partitioning, each subarray has their values changed to become the maximum value of that subarray. - * Return the largest sum of the given array after partitioning. - * - * Example 1: - * Input: A = [1,15,7,9,2,5,10], K = 3 - * Output: 84 - * Explanation: A becomes [15,15,15,9,10,10,10] - * - * Note: - * 1 <= K <= A.length <= 500 - * 0 <= A[i] <= 10^6 - * */ -public class _1043 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/partition-array-for-maximum-sum/discuss/290863/JavaC%2B%2BPython-DP*/ - public int maxSumAfterPartitioning(int[] A, int K) { - int N = A.length; - int[] dp = new int[N]; - for (int i = 0; i < N; i++) { - int curMax = 0; - for (int k = 1; k <= K && i - k + 1 >= 0; k++) { - curMax = Math.max(curMax, A[i - k + 1]); - dp[i] = Math.max(dp[i], (i >= k ? dp[i - k] : 0) + curMax * k); - } - } - return dp[N - 1]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1046.java b/src/main/java/com/fishercoder/solutions/_1046.java deleted file mode 100644 index d20c5bc0bc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1046.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.PriorityQueue; - -/** - * 1046. Last Stone Weight - * - * We have a collection of rocks, each rock has a positive integer weight. - * - * Each turn, we choose the two heaviest rocks and smash them together. - * Suppose the stones have weights x and y with x <= y. The result of this smash is: - * - * If x == y, both stones are totally destroyed; - * If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. - * At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.) - * - * Example 1: - * Input: [2,7,4,1,8,1] - * Output: 1 - * Explanation: - * We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then, - * we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then, - * we combine 2 and 1 to get 1 so the array converts to [1,1,1] then, - * we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone. - * - * Note: - * 1 <= stones.length <= 30 - * 1 <= stones[i] <= 1000 - * */ -public class _1046 { - public static class Solution1 { - public int lastStoneWeight(int[] stones) { - PriorityQueue heap = new PriorityQueue<>((a, b) -> b - a); - for (int stone : stones) { - heap.offer(stone); - } - while (!heap.isEmpty()) { - if (heap.size() >= 2) { - int one = heap.poll(); - int two = heap.poll(); - int diff = one - two; - heap.offer(diff); - } else { - return heap.poll(); - } - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1047.java b/src/main/java/com/fishercoder/solutions/_1047.java deleted file mode 100644 index e25da0696e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1047.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1047. Remove All Adjacent Duplicates In String - * - * Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them. - * - * We repeatedly make duplicate removals on S until we no longer can. - * - * Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique. - * - * - * - * Example 1: - * - * Input: "abbaca" - * Output: "ca" - * Explanation: - * For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. - * The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca". - * - * - * Note: - * - * 1 <= S.length <= 20000 - * S consists only of English lowercase letters.*/ -public class _1047 { - public static class Solution1 { - public String removeDuplicates(String S) { - StringBuilder sb = new StringBuilder(S); - for (int i = 0; i < S.length() - 1; i++) { - if (S.charAt(i) == S.charAt(i + 1)) { - return removeDuplicates(S.substring(0, i) + S.substring(i + 2)); - } - } - return sb.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1049.java b/src/main/java/com/fishercoder/solutions/_1049.java deleted file mode 100644 index a091d56e0a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1049.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1049. Last Stone Weight II - * - * We have a collection of rocks, each rock has a positive integer weight. - * Each turn, we choose any two rocks and smash them together. - * Suppose the stones have weights x and y with x <= y. The result of this smash is: - * If x == y, both stones are totally destroyed; - * If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. - * At the end, there is at most 1 stone left. Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.) - * - * Example 1: - * Input: [2,7,4,1,8,1] - * Output: 1 - * - * Explanation: - * We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, - * we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, - * we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, - * we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value. - * - * Note: - * 1 <= stones.length <= 30 - * 1 <= stones[i] <= 100 - * */ -public class _1049 { - public static class Solution1 { - public int lastStoneWeightII(int[] stones) { - //TODO: implement it - return 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_105.java b/src/main/java/com/fishercoder/solutions/_105.java deleted file mode 100644 index ac9fed0c8e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_105.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.HashMap; -import java.util.Map; - -public class _105 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/29838/5ms-java-clean-solution-with-caching use - * HashMap as the cache so that accessing inorder index becomes O(1) time Note: The first - * element of preorder array is the root! - */ - public TreeNode buildTree(int[] preorder, int[] inorder) { - Map inorderMap = new HashMap(); - for (int i = 0; i < inorder.length; i++) { - inorderMap.put(inorder[i], i); - } - - /**At the beginning, both start from 0 to nums.length-1*/ - return buildTree(preorder, 0, preorder.length - 1, inorderMap, 0, inorder.length - 1); - } - - private TreeNode buildTree(int[] preorder, int preStart, int preEnd, - Map inorderMap, int inStart, int inEnd) { - if (preStart > preEnd || inStart > inEnd) { - return null; - } - - TreeNode root = new TreeNode(preorder[preStart]); - int inRoot = inorderMap.get(preorder[preStart]); - int numsLeft = inRoot - inStart; - - /**It's easy to understand and remember: - * for the indices of inorder array: - * root.left should be inStart and inRoot-1 as new start and end indices - * root.right should be inRoot+1 and inEnd as new start and end indices - * - * since inRoot is being used already in this recursion call, that's why we use inRoot-1 and inRoot+1 - * this part is the same for both Leetcode 105 and Leetcode 106.*/ - root.left = buildTree(preorder, preStart + 1, preStart + numsLeft, inorderMap, inStart, inRoot - 1); - root.right = buildTree(preorder, preStart + numsLeft + 1, preEnd, inorderMap, inRoot + 1, inEnd); - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1051.java b/src/main/java/com/fishercoder/solutions/_1051.java deleted file mode 100644 index 6e98f44ec5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1051.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1051. Height Checker - * - * Students are asked to stand in non-decreasing order of heights for an annual photo. - * Return the minimum number of students not standing in the right positions. - * (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.) - * - * Example 1: - * Input: [1,1,4,2,1,3] - * Output: 3 - * Explanation: - * Students with heights 4, 3 and the last 1 are not standing in the right positions. - * - * Note: - * - * 1 <= heights.length <= 100 - * 1 <= heights[i] <= 100 - * */ -public class _1051 { - public static class Solution1 { - public int heightChecker(int[] heights) { - int[] originals = Arrays.copyOf(heights, heights.length); - Arrays.sort(heights); - int count = 0; - for (int i = 0; i < originals.length; i++) { - if (heights[i] != originals[i]) { - count++; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1055.java b/src/main/java/com/fishercoder/solutions/_1055.java deleted file mode 100644 index a1d0d16842..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1055.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.stream.IntStream; - -/** - * 1055. Fixed Point - * - * Given an array A of distinct integers sorted in ascending order, - * return the smallest index i that satisfies A[i] == i. Return -1 if no such i exists. - * - * Example 1: - * Input: [-10,-5,0,3,7] - * Output: 3 - * Explanation: - * For the given array, A[0] = -10, A[1] = -5, A[2] = 0, A[3] = 3, thus the output is 3. - * - * Example 2: - * Input: [0,2,5,8,17] - * Output: 0 - * Explanation: - * A[0] = 0, thus the output is 0. - * - * Example 3: - * Input: [-10,-5,3,4,7,9] - * Output: -1 - * Explanation: - * There is no such i that A[i] = i, thus the output is -1. - * - * Note: - * - * 1 <= A.length < 10^4 - * -10^9 <= A[i] <= 10^9 - * */ -public class _1055 { - public static class Solution1 { - public int fixedPoint(int[] A) { - return IntStream.range(0, A.length).filter(i -> A[i] == i).findFirst().orElse(-1); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1056.java b/src/main/java/com/fishercoder/solutions/_1056.java deleted file mode 100644 index 42bc7f7d98..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1056.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 1056. Confusing Number - * - * Given a number N, return true if and only if it is a confusing number, which satisfies the following condition: - * We can rotate digits by 180 degrees to form new digits. When 0, 1, 6, 8, 9 are rotated 180 degrees, - * they become 0, 1, 9, 8, 6 respectively. When 2, 3, 4, 5 and 7 are rotated 180 degrees, - * they become invalid. A confusing number is a number that when rotated 180 degrees becomes a different number with each digit valid. - * - * Example 1: - * Input: 6 - * Output: true - * Explanation: - * We get 9 after rotating 6, 9 is a valid number and 9!=6. - * - * - * Example 2: - * Input: 89 - * Output: true - * Explanation: - * We get 68 after rotating 89, 86 is a valid number and 86!=89. - * - * - * Example 3: - * Input: 11 - * Output: false - * Explanation: - * We get 11 after rotating 11, 11 is a valid number but the value remains the same, thus 11 is not a confusing number. - * - * Example 4: - * Input: 25 - * Output: false - * Explanation: - * We get an invalid number after rotating 25. - * - * Note: - * - * 0 <= N <= 10^9 - * After the rotation we can ignore leading zeros, for example if after rotation we have 0008 then this number is considered as just 8.*/ -public class _1056 { - public static class Solution1 { - Map map = new HashMap() {{ - put(0, 0); - put(1, 1); - put(8, 8); - put(6, 9); - put(9, 6); - } - }; - - public boolean confusingNumber(int N) { - if (N == 0) { - return false; - } - int newNumber = 0; - int originalN = N; - while (N != 0) { - newNumber *= 10; - int digit = N % 10; - if (!map.containsKey(digit)) { - return false; - } - digit = map.get(digit); - newNumber += digit; - N /= 10; - } - return newNumber != originalN; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1057.java b/src/main/java/com/fishercoder/solutions/_1057.java deleted file mode 100644 index 44175602b0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1057.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.PriorityQueue; -import java.util.TreeMap; - -/** - * 1057. Campus Bikes - * - * On a campus represented as a 2D grid, there are N workers and M bikes, with N <= M. Each worker and bike is a 2D coordinate on this grid. - * Our goal is to assign a bike to each worker. - * Among the available bikes and workers, we choose the (worker, bike) pair with the shortest Manhattan distance between each other, - * and assign the bike to that worker. (If there are multiple (worker, bike) pairs with the same shortest Manhattan distance, - * we choose the pair with the smallest worker index; if there are multiple ways to do that, - * we choose the pair with the smallest bike index). We repeat this process until there are no available workers. - * - * The Manhattan distance between two points p1 and p2 is Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|. - * Return a vector ans of length N, where ans[i] is the index (0-indexed) of the bike that the i-th worker is assigned to. - * - * Example 1: - * Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]] - * Output: [1,0] - * Explanation: - * Worker 1 grabs Bike 0 as they are closest (without ties), and Worker 0 is assigned Bike 1. So the output is [1, 0]. - * - * Example 2: - * Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]] - * Output: [0,2,1] - * Explanation: - * Worker 0 grabs Bike 0 at first. Worker 1 and Worker 2 share the same distance to Bike 2, - * thus Worker 1 is assigned to Bike 2, and Worker 2 will take Bike 1. So the output is [0,2,1]. - * - * Note: - * 0 <= workers[i][j], bikes[i][j] < 1000 - * All worker and bike locations are distinct. - * 1 <= workers.length <= bikes.length <= 1000 - * */ -public class _1057 { - public static class Solution1 { - public int[] assignBikes(int[][] workers, int[][] bikes) { - int w = workers.length; - int b = bikes.length; - TreeMap> treeMap = new TreeMap<>(); - for (int i = 0; i < w; i++) { - for (int j = 0; j < b; j++) { - int distance = Math.abs(workers[i][0] - bikes[j][0]) + Math.abs(workers[i][1] - bikes[j][1]); - if (!treeMap.containsKey(distance)) { - treeMap.put(distance, new PriorityQueue<>((x, y) -> x[0] == y[0] ? x[1] - y[1] : x[0] - y[0])); - } - treeMap.get(distance).add(new int[]{i, j}); - } - } - int[] ans = new int[w]; - for (int i = 0; i < w; i++) { - ans[i] = -1; - } - boolean[] assigned = new boolean[b]; - int workersHaveBikes = 0; - for (int dist : treeMap.keySet()) { - PriorityQueue workerBikePairs = treeMap.get(dist); - while (!workerBikePairs.isEmpty()) { - int[] workerBikePair = workerBikePairs.poll(); - if (ans[workerBikePair[0]] == -1 && !assigned[workerBikePair[1]]) { - assigned[workerBikePair[1]] = true; - ans[workerBikePair[0]] = workerBikePair[1]; - workersHaveBikes++; - } - } - if (workersHaveBikes == w) { - return ans; - } - } - return ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_106.java b/src/main/java/com/fishercoder/solutions/_106.java deleted file mode 100644 index 271923d8b5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_106.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.HashMap; -import java.util.Map; - -public class _106 { - public static class Solution1 { - - /** - * https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space - * Note: the last element of postorder array is the root! - * The idea is to take the last element in postorder as the root; find the position of the root - * in the inorder array; then locate the range for left sub-tree and right sub-tree and do - * recursion, use a hashmap to record the index of root in the inorder array. - */ - public TreeNode buildTree(int[] inorder, int[] postorder) { - if (inorder == null || postorder == null || inorder.length != postorder.length) { - return null; - } - HashMap inorderMap = new HashMap<>(); - for (int i = 0; i < inorder.length; i++) { - inorderMap.put(inorder[i], i); - } - /**At the beginning, both start from 0 to nums.length-1*/ - return buildTreeRecursively(inorderMap, 0, inorder.length - 1, postorder, 0, - postorder.length - 1); - } - - private TreeNode buildTreeRecursively(Map inorderMap, int inorderStart, - int inorderEnd, int[] postorder, int postorderStart, int postorderEnd) { - if (postorderStart > postorderEnd || inorderStart > inorderEnd) { - return null; - } - TreeNode root = new TreeNode(postorder[postorderEnd]); - int inRoot = inorderMap.get(postorder[postorderEnd]); - int numsLeft = inRoot - inorderStart; - - /**It's easy to understand and remember: - * for the indices of inorder array: - * inStart and inRoot-1 as new start and end indices - * inRoot+1 and inEnd as new start and end indices - * - * this is easy to understand and remember: since inRoot is already been used in this recursion call, so we're going to use inRoot-1 and inRoot+1 for next recursion call - * - * for the indices of postorder array: - * postorderStart and postorderStart+numsLeft-1 should be the new start and end indices - * postorderStart+numsLeft and postorderEnd-1 should be the new start and end indices - * - * this is also easy to understand and remember: - * since the last one in postorder is the root and we have used it in this recursion call already, so the end is definitely postorderEnd-1; - * then the postorderEnd for root.left is contiguous to the postorderStart of root.right, :)*/ - root.left = buildTreeRecursively(inorderMap, inorderStart, inRoot - 1, postorder, postorderStart, postorderStart + numsLeft - 1); - root.right = buildTreeRecursively(inorderMap, inRoot + 1, inorderEnd, postorder, postorderStart + numsLeft, postorderEnd - 1); - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1062.java b/src/main/java/com/fishercoder/solutions/_1062.java deleted file mode 100644 index ab2bc6e5a6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1062.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1062. Longest Repeating Substring - * - * Given a string S, find out the length of the longest repeating substring(s). Return 0 if no repeating substring exists. - * - * Example 1: - * Input: "abcd" - * Output: 0 - * Explanation: There is no repeating substring. - * - * Example 2: - * Input: "abbaba" - * Output: 2 - * Explanation: The longest repeating substrings are "ab" and "ba", each of which occurs twice. - * - * Example 3: - * Input: "aabcaabdaab" - * Output: 3 - * Explanation: The longest repeating substring is "aab", which occurs 3 times. - * - * Example 4: - * Input: "aaaaa" - * Output: 4 - * Explanation: The longest repeating substring is "aaaa", which occurs twice. - * - * Note: - * The string S consists of only lowercase English letters from 'a' - 'z'. - * 1 <= S.length <= 1500 - * */ -public class _1062 { - public static class Solution1 { - public int longestRepeatingSubstring(String S) { - //TODO: implement it - return 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1065.java b/src/main/java/com/fishercoder/solutions/_1065.java deleted file mode 100644 index 7bc8e894b6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1065.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -/** - * 1065. Index Pairs of a String - * - * Given a text string and words (a list of strings), - * return all index pairs [i, j] so that the substring text[i]...text[j] is in the list of words. - * - * Example 1: - * Input: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"] - * Output: [[3,7],[9,13],[10,17]] - * - * Example 2: - * Input: text = "ababa", words = ["aba","ab"] - * Output: [[0,1],[0,2],[2,3],[2,4]] - * Explanation: - * Notice that matches can overlap, see "aba" is found in [0,2] and [2,4]. - * - * Note: - * - * All strings contains only lowercase English letters. - * It's guaranteed that all strings in words are different. - * 1 <= text.length <= 100 - * 1 <= words.length <= 20 - * 1 <= words[i].length <= 50 - * Return the pairs [i,j] in sorted order (i.e. sort them by their first coordinate in case of ties sort them by their second coordinate). - * */ -public class _1065 { - public static class Solution1 { - public int[][] indexPairs(String text, String[] words) { - List> lists = new ArrayList<>(); - for (String word : words) { - lists.addAll(findAllMatchsForThisWord(word, text)); - } - if (lists.isEmpty()) { - return new int[][]{}; - } - Collections.sort(lists, (o1, o2) -> { - if (o1.get(0) > o2.get(0)) { - return 1; - } else if (o1.get(0) < o2.get(0)) { - return -1; - } else { - if (o1.get(1) > o2.get(1)) { - return 1; - } else { - return -1; - } - } - }); - int[][] result = new int[lists.size()][lists.get(0).size()]; - for (int i = 0; i < lists.size(); i++) { - result[i][0] = lists.get(i).get(0); - result[i][1] = lists.get(i).get(1); - } - return result; - } - - private List> findAllMatchsForThisWord(String word, String text) { - List> lists = new ArrayList<>(); - for (int i = 0; i <= text.length() - word.length(); i++) { - if (text.substring(i, i + word.length()).equals(word)) { - lists.add(Arrays.asList(i, i + word.length() - 1)); - } - } - return lists; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1066.java b/src/main/java/com/fishercoder/solutions/_1066.java deleted file mode 100644 index a3562ff213..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1066.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1066. Campus Bikes II - * - * On a campus represented as a 2D grid, there are N workers and M bikes, with N <= M. Each worker and bike is a 2D coordinate on this grid. - * We assign one unique bike to each worker so that the sum of the Manhattan distances between each worker and their assigned bike is minimized. - * The Manhattan distance between two points p1 and p2 is Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|. - * Return the minimum possible sum of Manhattan distances between each worker and their assigned bike. - * - * Example 1: - * Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]] - * Output: 6 - * Explanation: - * We assign bike 0 to worker 0, bike 1 to worker 1. The Manhattan distance of both assignments is 3, so the output is 6. - * - * Example 2: - * Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]] - * Output: 4 - * Explanation: - * We first assign bike 0 to worker 0, then assign bike 1 to worker 1 or worker 2, bike 2 to worker 2 or worker 1. - * Both assignments lead to sum of the Manhattan distances as 4. - * - * Note: - * 0 <= workers[i][0], workers[i][1], bikes[i][0], bikes[i][1] < 1000 - * All worker and bike locations are distinct. - * 1 <= workers.length <= bikes.length <= 10 - * */ -public class _1066 { - public static class Solution1 { - int minSum = Integer.MAX_VALUE; - - public int assignBikes(int[][] workers, int[][] bikes) { - backtracking(workers, bikes, 0, new boolean[bikes.length], 0); - return minSum; - } - - private void backtracking(int[][] workers, int[][] bikes, int workersIndex, boolean[] bikesAssigned, int currentSum) { - if (workersIndex >= workers.length) { - minSum = Math.min(minSum, currentSum); - return; - } - - if (currentSum > minSum) { - return; - } - - for (int j = 0; j < bikes.length; j++) { - if (!bikesAssigned[j]) { - bikesAssigned[j] = true; - backtracking(workers, bikes, workersIndex + 1, bikesAssigned, currentSum + dist(workers[workersIndex], bikes[j])); - bikesAssigned[j] = false; - } - } - } - - private int dist(int[] worker, int[] bike) { - return Math.abs(worker[0] - bike[0]) + Math.abs(worker[1] - bike[1]); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1078.java b/src/main/java/com/fishercoder/solutions/_1078.java deleted file mode 100644 index 9c971e8adf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1078.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -/** - * 5083. Occurrences After Bigram - *

- * Given words first and second, consider occurrences in some text of the form "first second third", - * where second comes immediately after first, and third comes immediately after second. - * For each such occurrence, add "third" to the answer, and return the answer. - *

- * Example 1: - * Input: text = "alice is a good girl she is a good student", first = "a", second = "good" - * Output: ["girl","student"] - *

- * Example 2: - * Input: text = "we will we will rock you", first = "we", second = "will" - * Output: ["we","rock"] - *

- * Note: - * 1 <= text.length <= 1000 - * text consists of space separated words, where each word consists of lowercase English letters. - * 1 <= first.length, second.length <= 10 - * first and second consist of lowercase English letters. - */ -public class _1078 { - public static class Solution1 { - public String[] findOcurrences(String text, String first, String second) { - String[] words = text.split(" "); - return IntStream - .range(0, words.length - 2) - .filter(i -> words[i].equals(first) && words[i + 1].equals(second)) - .mapToObj(i -> words[i + 2]) - .collect(Collectors.toList()) - .stream() - .toArray(String[]::new); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1079.java b/src/main/java/com/fishercoder/solutions/_1079.java deleted file mode 100644 index 46197ab0e6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1079.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.IntStream; - -/** - * 5087. Letter Tile Possibilities - * - * You have a set of tiles, where each tile has one letter tiles[i] printed on it. - * Return the number of possible non-empty sequences of letters you can make. - * - * Example 1: - * Input: "AAB" - * Output: 8 - * Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA". - * - * Example 2: - * Input: "AAABBC" - * Output: 188 - * - * Note: - * 1. 1 <= tiles.length <= 7 - * 2. tiles consists of uppercase English letters. - * */ -public class _1079 { - public static class Solution1 { - public int numTilePossibilities(String tiles) { - char[] chars = tiles.toCharArray(); - Arrays.sort(chars); - boolean[] used = new boolean[chars.length]; - StringBuilder sb = new StringBuilder(); - List result = new ArrayList<>(); - dfs(chars, used, sb, result); - return result.size(); - } - - private void dfs(char[] chars, boolean[] used, StringBuilder sb, List result) { - if (sb.length() != 0) { - result.add(sb.toString()); - } - IntStream.range(0, chars.length) - .filter(i -> !used[i]) - .filter(i -> i <= 0 || chars[i - 1] != chars[i] || used[i - 1]) - .forEach(i -> { - used[i] = true; - sb.append(chars[i]); - dfs(chars, used, sb, result); - used[i] = false; - sb.deleteCharAt(sb.length() - 1); - }); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_108.java b/src/main/java/com/fishercoder/solutions/_108.java deleted file mode 100644 index 542ed9b027..0000000000 --- a/src/main/java/com/fishercoder/solutions/_108.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 108. Convert Sorted Array to Binary Search Tree - * - * Given an array where elements are sorted in ascending order, convert it to a height balanced BST. - * For this problem, a height-balanced binary tree is defined as a binary tree in which - * the depth of the two subtrees of every node never differ by more than 1. - * - * Example: - * Given the sorted array: [-10,-3,0,5,9], - * One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: - * - * 0 - * / \ - * -3 9 - * / / - * -10 5 - */ -public class _108 { - - public static class Solution1 { - public TreeNode sortedArrayToBST(int[] num) { - return dfs(num, 0, num.length - 1); - } - - public TreeNode dfs(int[] num, int start, int end) { - if (start > end) { - return null; - } - int mid = (end + start) / 2; - TreeNode root = new TreeNode(num[mid]); - root.left = dfs(num, start, mid - 1); - root.right = dfs(num, mid + 1, end); - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1085.java b/src/main/java/com/fishercoder/solutions/_1085.java deleted file mode 100644 index 9fc619d982..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1085.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1085. Sum of Digits in the Minimum Number - * - * Given an array A of positive integers, let S be the sum of the digits of the minimal element of A. - * Return 0 if S is odd, otherwise return 1. - * - * Example 1: - * Input: [34,23,1,24,75,33,54,8] - * Output: 0 - * Explanation: - * The minimal element is 1, and the sum of those digits is S = 1 which is odd, so the answer is 0. - * - * Example 2: - * Input: [99,77,33,66,55] - * Output: 1 - * Explanation: - * The minimal element is 33, and the sum of those digits is S = 3 + 3 = 6 which is even, so the answer is 1. - * - * Note: - * 1 <= A.length <= 100 - * 1 <= A[i].length <= 100 - * */ -public class _1085 { - public static class Solution1 { - public int sumOfDigits(int[] A) { - int smallestNumber = A[0]; - for (int i = 1; i < A.length; i++) { - smallestNumber = Math.min(smallestNumber, A[i]); - } - int sum = 0; - while (smallestNumber > 0) { - sum += smallestNumber % 10; - smallestNumber /= 10; - } - return sum % 2 == 0 ? 1 : 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1086.java b/src/main/java/com/fishercoder/solutions/_1086.java deleted file mode 100644 index c6dd0fbdc3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1086.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.PriorityQueue; -import java.util.TreeMap; - -/** - * 1086. High Five - * - * Given a list of scores of different students, return the average score of each student's top five scores in the order of each student's id. - * Each entry items[i] has items[i][0] the student's id, and items[i][1] the student's score. - * The average score is calculated using integer division. - * - * Example 1: - * Input: [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]] - * Output: [[1,87],[2,88]] - * Explanation: - * The average of the student with id = 1 is 87. - * The average of the student with id = 2 is 88.6. But with integer division their average converts to 88. - * - * Note: - * 1 <= items.length <= 1000 - * items[i].length == 2 - * The IDs of the students is between 1 to 1000 - * The score of the students is between 1 to 100 - * For each student, there are at least 5 scores - * */ -public class _1086 { - public static class Solution1 { - public int[][] highFive(int[][] items) { - TreeMap> map = new TreeMap<>(); - for (int[] studentToScore : items) { - if (map.containsKey(studentToScore[0])) { - map.get(studentToScore[0]).add(studentToScore[1]); - } else { - List list = new ArrayList<>(); - list.add(studentToScore[1]); - map.put(studentToScore[0], list); - } - } - int[][] result = new int[map.size()][2]; - for (int id : map.keySet()) { - List scores = map.get(id); - Collections.sort(scores); - int sum = 0; - for (int i = scores.size() - 1; i >= scores.size() - 5 && i >= 0; i--) { - sum += scores.get(i); - } - result[id - 1][0] = id; - result[id - 1][1] = sum / 5; - } - return result; - } - } - - public static class Solution2 { - public int[][] highFive(int[][] items) { - TreeMap> treeMap = new TreeMap<>(); - for (int[] studentToScores : items) { - if (treeMap.containsKey(studentToScores[0])) { - PriorityQueue maxHeap = treeMap.get(studentToScores[0]); - maxHeap.offer(studentToScores[1]); - if (maxHeap.size() > 5) { - maxHeap.poll(); - } - treeMap.put(studentToScores[0], maxHeap); - } else { - PriorityQueue maxHeap = new PriorityQueue<>(); - maxHeap.offer(studentToScores[1]); - treeMap.put(studentToScores[0], maxHeap); - } - } - int[][] result = new int[treeMap.size()][2]; - for (int id : treeMap.keySet()) { - result[id - 1][0] = id; - int sum = 0; - PriorityQueue maxHeap = treeMap.get(id); - while (!maxHeap.isEmpty()) { - sum += maxHeap.poll(); - } - result[id - 1][1] = sum / 5; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1087.java b/src/main/java/com/fishercoder/solutions/_1087.java deleted file mode 100644 index 2b4a040cc8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1087.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 1087. Brace Expansion - * - * A string S represents a list of words. - * Each letter in the word has 1 or more options. If there is one option, the letter is represented as is. - * If there is more than one option, then curly braces delimit the options. - * For example, "{a,b,c}" represents options ["a", "b", "c"]. - * For example, "{a,b,c}d{e,f}" represents the list ["ade", "adf", "bde", "bdf", "cde", "cdf"]. - * - * Return all words that can be formed in this manner, in lexicographical order. - * - * Example 1: - * Input: "{a,b}c{d,e}f" - * Output: ["acdf","acef","bcdf","bcef"] - * - * Example 2: - * Input: "abcd" - * Output: ["abcd"] - * - * Note: - * 1 <= S.length <= 50 - * There are no nested curly brackets. - * All characters inside a pair of consecutive opening and ending curly brackets are different. - * */ -public class _1087 { - public static class Solution1 { - public String[] expand(String S) { - List letters = parse(S); - List result = backtracking(letters, 0, new StringBuilder(), new ArrayList<>()); - String[] r = result.stream().toArray(String[]::new); - Arrays.sort(r); - return r; - } - - private List backtracking(List letters, int start, StringBuilder sb, List result) { - if (start >= letters.size()) { - result.add(sb.toString()); - return result; - } - char[] chars = letters.get(start); - for (int i = 0; i < chars.length; i++) { - sb.append(chars[i]); - backtracking(letters, start + 1, sb, result); - sb.setLength(sb.length() - 1); - } - return result; - } - - private List parse(String s) { - List result = new ArrayList<>(); - for (int i = 0; i < s.length(); i++) { - if (s.charAt(i) == '{') { - int start = ++i; - while (i < s.length() && s.charAt(i) != '}') { - i++; - } - String[] strings = s.substring(start, i).split(","); - char[] chars = new char[strings.length]; - for (int j = 0; j < strings.length; j++) { - chars[j] = strings[j].charAt(0); - } - result.add(chars); - } else { - char[] chars = new char[1]; - chars[0] = s.charAt(i); - result.add(chars); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1089.java b/src/main/java/com/fishercoder/solutions/_1089.java deleted file mode 100644 index 733e7c1b4f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1089.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1089. Duplicate Zeros - * - * Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. - * - * Note that elements beyond the length of the original array are not written. - * - * Do the above modifications to the input array in place, do not return anything from your function. - * - * Example 1: - * Input: [1,0,2,3,0,4,5,0] - * Output: null - * Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4] - * - * Example 2: - * Input: [1,2,3] - * Output: null - * Explanation: After calling your function, the input array is modified to: [1,2,3] - * - * Note: - * - * 1 <= arr.length <= 10000 - * 0 <= arr[i] <= 9 - * */ -public class _1089 { - public static class Solution1 { - public void duplicateZeros(int[] arr) { - for (int i = 0; i < arr.length - 1; i++) { - if (arr[i] == 0) { - duplicateZeros(arr, i + 1); - i++; - } - } - } - - private void duplicateZeros(int[] arr, int zeroIndex) { - int tmp = arr[zeroIndex]; - arr[zeroIndex] = 0; - for (int i = zeroIndex + 1; i < arr.length; i++) { - int next = arr[i]; - arr[i] = tmp; - tmp = next; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_109.java b/src/main/java/com/fishercoder/solutions/_109.java deleted file mode 100644 index 98a1aa3011..0000000000 --- a/src/main/java/com/fishercoder/solutions/_109.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.classes.TreeNode; - -/** - * 109. Convert Sorted List to Binary Search Tree - * - * Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. - */ -public class _109 { - - public static class Solution1 { - - public TreeNode sortedListToBST(ListNode head) { - return toBstRecursively(head, null); - } - - public TreeNode toBstRecursively(ListNode start, ListNode end) { - if (start == end) { - return null; - } else { - ListNode mid = start; - ListNode fast = start; - while (fast != end && fast.next != end) { - mid = mid.next; - fast = fast.next.next; - } - - TreeNode root = new TreeNode(mid.val); - root.left = toBstRecursively(start, mid); - root.right = toBstRecursively(mid.next, end); - return root; - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_1099.java b/src/main/java/com/fishercoder/solutions/_1099.java deleted file mode 100644 index 957db08231..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1099.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1099. Two Sum Less Than K - * - * Given an array A of integers and integer K, - * return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. - * If no i, j exist satisfying this equation, return -1. - * - * Example 1: - * Input: A = [34,23,1,24,75,33,54,8], K = 60 - * Output: 58 - * Explanation: - * We can use 34 and 24 to sum 58 which is less than 60. - * - * Example 2: - * Input: A = [10,20,30], K = 15 - * Output: -1 - * Explanation: - * In this case it's not possible to get a pair sum less that 15. - * - * Note: - * 1 <= A.length <= 100 - * 1 <= A[i] <= 1000 - * 1 <= K <= 2000 - * */ -public class _1099 { - public static class Solution1 { - /** - * Time: O(n^2) - * Space: O(1) - * */ - public int twoSumLessThanK(int[] A, int K) { - int maxSum = Integer.MIN_VALUE; - for (int i = 0; i < A.length - 1; i++) { - for (int j = i + 1; j < A.length; j++) { - if (A[i] + A[j] < K) { - maxSum = Math.max(maxSum, A[i] + A[j]); - } - } - } - return maxSum == Integer.MIN_VALUE ? -1 : maxSum; - } - } - - public static class Solution2 { - /** - * Time: O(nlogn) - * Space: O(1) - * */ - public int twoSumLessThanK(int[] A, int K) { - Arrays.sort(A); - int left = 0; - int right = A.length - 1; - int sum = Integer.MIN_VALUE; - while (left < right) { - int newSum = A[left] + A[right]; - if (newSum < K && newSum > sum) { - sum = newSum; - } else if (newSum >= K) { - right--; - } else { - left++; - } - } - return sum == Integer.MIN_VALUE ? -1 : sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_11.java b/src/main/java/com/fishercoder/solutions/_11.java deleted file mode 100644 index 32557cb1fc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_11.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder.solutions; - -public class _11 { - public static class Solution1 { - public int maxArea(int[] height) { - int max = 0; - int i = 0; - int j = height.length - 1; - while (i < j) { - max = Math.max(Math.min(height[i], height[j]) * (j - i), max); - if (height[i] <= height[j]) { - // we need to find the shorter one, - // then calculate its area - i++; - } else { - j--; - } - } - return max; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_110.java b/src/main/java/com/fishercoder/solutions/_110.java deleted file mode 100644 index 7f3abbcd83..0000000000 --- a/src/main/java/com/fishercoder/solutions/_110.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 110. Balanced Binary Tree - * - * Given a binary tree, determine if it is height-balanced. - * For this problem, a height-balanced binary tree is defined as a binary tree in which - * the depth of the two subtrees of every node never differ by more than 1. - - Example 1: - Given the following tree [3,9,20,null,null,15,7]: - - 3 - / \ - 9 20 - / \ - 15 7 - - Return true. - - Example 2: - Given the following tree [1,2,2,3,3,null,null,4,4]: - - 1 - / \ - 2 2 - / \ - 3 3 -/ \ -4 4 - - Return false. - */ - -public class _110 { - - public static class Solution1 { - //recursively get the height of each subtree of each node, compare their difference, if greater than 1, then return false - //although this is working, but it's not efficient, since it repeatedly computes the heights of each node every time - //Its time complexity is O(n^2). - public boolean isBalanced(TreeNode root) { - if (root == null) { - return true; - } - if (Math.abs(getH(root.left) - getH(root.right)) > 1) { - return false; - } else { - return isBalanced(root.left) && isBalanced(root.right); - } - } - - private int getH(TreeNode root) { - if (root == null) { - return 0;//base case - } - int leftH = getH(root.left); - int rightH = getH(root.right); - return Math.max(leftH, rightH) + 1; - } - } - - public static class Solution2 { - - public boolean isBalanced(TreeNode root) { - return getH(root) != -1; - } - - private int getH(TreeNode root) { - if (root == null) { - return 0; - } - int leftH = getH(root.left); - if (leftH == -1) { - return -1; - } - int rightH = getH(root.right); - if (rightH == -1) { - return -1; - } - if (Math.abs(leftH - rightH) > 1) { - return -1; - } - return Math.max(leftH, rightH) + 1; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_1100.java b/src/main/java/com/fishercoder/solutions/_1100.java deleted file mode 100644 index 2cecc8b181..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1100.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 1100. Find K-Length Substrings With No Repeated Characters - * - * Given a string S, return the number of substrings of length K with no repeated characters. - * - * Example 1: - * Input: S = "havefunonleetcode", K = 5 - * Output: 6 - * Explanation: - * There are 6 substrings they are : 'havef','avefu','vefun','efuno','etcod','tcode'. - * - * Example 2: - * Input: S = "home", K = 5 - * Output: 0 - * Explanation: - * Notice K can be larger than the length of S. In this case is not possible to find any substring. - * - * Note: - * 1 <= S.length <= 10^4 - * All characters of S are lowercase English letters. - * 1 <= K <= 10^4 - * */ -public class _1100 { - public static class Solution1 { - public int numKLenSubstrNoRepeats(String S, int K) { - int count = 0; - Set set = new HashSet<>(); - for (int i = 0; i <= S.length() - K; i++) { - String string = S.substring(i, i + K); - boolean invalid = false; - for (char c : string.toCharArray()) { - if (!set.add(c)) { - invalid = true; - break; - } - } - count += invalid ? 0 : 1; - set.clear(); - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1103.java b/src/main/java/com/fishercoder/solutions/_1103.java deleted file mode 100644 index 737edcb5d0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1103.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 1103. Distribute Candies to People - * - * We distribute some number of candies, to a row of n = num_people people in the following way: - * We then give 1 candy to the first person, 2 candies to the second person, and so on until we give n candies to the last person. - * Then, we go back to the start of the row, giving n + 1 candies to the first person, n + 2 candies to the second person, - * and so on until we give 2 * n candies to the last person. - * This process repeats (with us giving one more candy each time, and moving to the start of the row - * after we reach the end) until we run out of candies. - * The last person will receive all of our remaining candies (not necessarily one more than the previous gift). - * Return an array (of length num_people and sum candies) that represents the final distribution of candies. - * - * Example 1: - * Input: candies = 7, num_people = 4 - * Output: [1,2,3,1] - * Explanation: - * On the first turn, ans[0] += 1, and the array is [1,0,0,0]. - * On the second turn, ans[1] += 2, and the array is [1,2,0,0]. - * On the third turn, ans[2] += 3, and the array is [1,2,3,0]. - * On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1]. - * - * Example 2: - * Input: candies = 10, num_people = 3 - * Output: [5,2,3] - * Explanation: - * On the first turn, ans[0] += 1, and the array is [1,0,0]. - * On the second turn, ans[1] += 2, and the array is [1,2,0]. - * On the third turn, ans[2] += 3, and the array is [1,2,3]. - * On the fourth turn, ans[0] += 4, and the final array is [5,2,3]. - * - * Constraints: - * 1 <= candies <= 10^9 - * 1 <= num_people <= 1000 - * */ -public class _1103 { - public static class Solution1 { - public int[] distributeCandies(int candies, int numPeople) { - Map map = new HashMap<>(); - int candy = 1; - while (candies > 0) { - for (int person = 1; person <= numPeople && candies > 0; person++, candy++) { - if (candies < candy) { - map.put(person, map.getOrDefault(person, 0) + candies); - candies -= candy; - break; - } else { - map.put(person, map.getOrDefault(person, 0) + candy); - candies -= candy; - } - } - } - int[] result = new int[numPeople]; - for (int i = 1; i <= numPeople; i++) { - if (map.containsKey(i)) { - result[i - 1] = map.get(i); - } else { - result[i - 1] = 0; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1108.java b/src/main/java/com/fishercoder/solutions/_1108.java deleted file mode 100644 index 248420e5a0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1108.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1108. Defanging an IP Address - * - * Given a valid (IPv4) IP address, return a defanged version of that IP address. - * A defanged IP address replaces every period "." with "[.]". - * - * Example 1: - * Input: address = "1.1.1.1" - * Output: "1[.]1[.]1[.]1" - * - * Example 2: - * Input: address = "255.100.50.0" - * Output: "255[.]100[.]50[.]0" - * - * Constraints: - * The given address is a valid IPv4 address. - * */ -public class _1108 { - public static class Solution1 { - public String defangIPaddr(String address) { - //String.replaceAll() takes in a regex which needs to be escaped - return address.replaceAll("\\.", "\\[\\.\\]"); - } - } - - public static class Solution2 { - public String defangIPaddr(String address) { - //String.replace() takes in a string which does NOT need to be escaped - return address.replace(".", "[.]"); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_111.java b/src/main/java/com/fishercoder/solutions/_111.java deleted file mode 100644 index 8bae3ba544..0000000000 --- a/src/main/java/com/fishercoder/solutions/_111.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 111. Minimum Depth of Binary Tree - * - * Given a binary tree, find its minimum depth. - * The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. - * */ - -public class _111 { - public static class Solution1 { - /**DFS*/ - public int minDepth(TreeNode root) { - if (root == null) { - return 0; - } - int left = minDepth(root.left); - int right = minDepth(root.right); - if (left == 0) { - return right + 1; - } - if (right == 0) { - return left + 1; - } - return Math.min(left, right) + 1; - } - } - - public static class Solution2 { - /**BFS*/ - public int minDepth(TreeNode root) { - if (root == null) { - return 0; - } - Queue q = new LinkedList(); - q.offer(root); - int level = 0; - while (!q.isEmpty()) { - level++; - int size = q.size(); - for (int i = 0; i < size; i++) { - TreeNode curr = q.poll(); - if (curr.left != null) { - q.offer(curr.left); - } - if (curr.right != null) { - q.offer(curr.right); - } - if (curr.left == null && curr.right == null) { - return level; - } - } - } - return level; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1118.java b/src/main/java/com/fishercoder/solutions/_1118.java deleted file mode 100644 index 1563c7ccde..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1118.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 1118. Number of Days in a Month - * - * Given a year Y and a month M, return how many days there are in that month. - * - * Example 1: - * Input: Y = 1992, M = 7 - * Output: 31 - * - * Example 2: - * Input: Y = 2000, M = 2 - * Output: 29 - * - * Example 3: - * Input: Y = 1900, M = 2 - * Output: 28 - * - * Note: - * 1583 <= Y <= 2100 - * 1 <= M <= 12 - * */ -public class _1118 { - public static class Solution1 { - public int numberOfDays(int Y, int M) { - int[] map = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - if (isLeapYear(Y) && M == 2) { - return 29; - } else { - return map[M]; - } - } - - private boolean isLeapYear(int year) { - return year % 4 == 0 && ((year % 100 == 0 && year % 400 == 0) || year % 100 != 0); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1119.java b/src/main/java/com/fishercoder/solutions/_1119.java deleted file mode 100644 index 1c2ca567f6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1119.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -/** - * 1119. Remove Vowels from a String - * - * Given a string S, remove the vowels 'a', 'e', 'i', 'o', and 'u' from it, and return the new string. - * - * Example 1: - * Input: "leetcodeisacommunityforcoders" - * Output: "ltcdscmmntyfrcdrs" - * - * Example 2: - * Input: "aeiou" - * Output: "" - * - * Note: - * S consists of lowercase English letters only. - * 1 <= S.length <= 1000 - * */ -public class _1119 { - public static class Solution1 { - public String removeVowels(String S) { - Set vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u')); - StringBuilder stringBuilder = new StringBuilder(); - for (char c : S.toCharArray()) { - if (!vowels.contains(c)) { - stringBuilder.append(c); - } - } - return stringBuilder.toString(); - } - } - - public static class Solution2 { - public String removeVowels(String S) { - return S.replaceAll("[aeiou]", ""); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_112.java b/src/main/java/com/fishercoder/solutions/_112.java deleted file mode 100644 index 138fa5d325..0000000000 --- a/src/main/java/com/fishercoder/solutions/_112.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 112. Path Sum - -Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. - -For example: -Given the below binary tree and sum = 22, - - 5 - / \ - 4 8 - / / \ - 11 13 4 - / \ \ - 7 2 1 - - return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.*/ -public class _112 { - public static class Solution1 { - public boolean hasPathSum(TreeNode root, int sum) { - if (root == null) { - return false; - } - if (root.val == sum && root.left == null && root.right == null) { - return true; - } - return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1122.java b/src/main/java/com/fishercoder/solutions/_1122.java deleted file mode 100644 index c429a6a893..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1122.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.TreeMap; - -/** - * 1122. Relative Sort Array - * - * Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1. - * Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. - * Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order. - * - * Example 1: - * Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] - * Output: [2,2,2,1,4,3,3,9,6,7,19] - * - * Constraints: - * arr1.length, arr2.length <= 1000 - * 0 <= arr1[i], arr2[i] <= 1000 - * Each arr2[i] is distinct. - * Each arr2[i] is in arr1. - * */ -public class _1122 { - public static class Solution1 { - public int[] relativeSortArray(int[] arr1, int[] arr2) { - TreeMap map = new TreeMap<>(); - Arrays.stream(arr1).forEach(num -> map.put(num, map.getOrDefault(num, 0) + 1)); - int[] result = new int[arr1.length]; - int i = 0; - for (int num : arr2) { - int count = map.get(num); - while (count-- > 0) { - result[i++] = num; - } - map.remove(num); - } - for (int key : map.keySet()) { - int count = map.get(key); - while (count-- > 0) { - result[i++] = key; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1123.java b/src/main/java/com/fishercoder/solutions/_1123.java deleted file mode 100644 index aa44b1aeca..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1123.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 1123. Lowest Common Ancestor of Deepest Leaves - * - * Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. - * Recall that: - * The node of a binary tree is a leaf if and only if it has no children - * The depth of the root of the tree is 0, and if the depth of a node is d, the depth of each of its children is d+1. - * The lowest common ancestor of a set S of nodes is the node A with the largest depth such that every node in S is in the subtree with root A. - * - * Example 1: - * Input: root = [1,2,3] - * Output: [1,2,3] - * Explanation: - * The deepest leaves are the nodes with values 2 and 3. - * The lowest common ancestor of these leaves is the node with value 1. - * The answer returned is a TreeNode object (not an array) with serialization "[1,2,3]". - * - * Example 2: - * Input: root = [1,2,3,4] - * Output: [4] - * - * Example 3: - * Input: root = [1,2,3,4,5] - * Output: [2,4,5] - * - * Constraints: - * The given tree will have between 1 and 1000 nodes. - * Each node of the tree will have a distinct value between 1 and 1000. - * */ -public class _1123 { - public static class Solution1 { - public TreeNode lcaDeepestLeaves(TreeNode root) { - return null; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1128.java b/src/main/java/com/fishercoder/solutions/_1128.java deleted file mode 100644 index 454f7e4891..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1128.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 1128. Number of Equivalent Domino Pairs - * - * Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a==c and b==d), or (a==d and b==c) - that is, one domino can be rotated to be equal to another domino. - * Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j]. - * - * Example 1: - * - * Input: dominoes = [[1,2],[2,1],[3,4],[5,6]] - * Output: 1 - * - * Constraints: - * 1 <= dominoes.length <= 40000 - * 1 <= dominoes[i][j] <= 9 - * */ -public class _1128 { - public static class Solution1 { - public int numEquivDominoPairs(int[][] dominoes) { - int m = dominoes.length; - Map map = new HashMap<>(); - int count = 0; - for (int i = 0; i < m; i++) { - int smaller = Math.min(dominoes[i][0], dominoes[i][1]); - int bigger = Math.max(dominoes[i][0], dominoes[i][1]); - int key = smaller * 10 + bigger; - count += map.getOrDefault(key, 0); - map.put(key, map.getOrDefault(key, 0) + 1); - } - return count; - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_113.java b/src/main/java/com/fishercoder/solutions/_113.java deleted file mode 100644 index e3b328c408..0000000000 --- a/src/main/java/com/fishercoder/solutions/_113.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 113. Path Sum II - - Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. - -For example: -Given the below binary tree and sum = 22, - 5 - / \ - 4 8 - / / \ - 11 13 4 - / \ / \ - 7 2 5 1 - - return -[ - [5,4,11,2], - [5,8,4,5] -] -*/ -public class _113 { - - public static class Solution1 { - public List> pathSum(TreeNode root, int sum) { - List> allPaths = new ArrayList(); - if (root == null) { - return allPaths; - } - dfs(root, new ArrayList(), allPaths, sum); - return allPaths; - } - - private void dfs(TreeNode root, List path, List> allPaths, int sum) { - path.add(root.val); - if (root.left != null) { - dfs(root.left, path, allPaths, sum - root.val); - } - if (root.right != null) { - dfs(root.right, path, allPaths, sum - root.val); - } - if (root.left == null && root.right == null) { - /**Check if sum equals root.val, not sum equals zero!*/ - if (sum == root.val) { - allPaths.add(new ArrayList(path)); - } - } - path.remove(path.size() - 1); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1133.java b/src/main/java/com/fishercoder/solutions/_1133.java deleted file mode 100644 index d4b4a56dba..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1133.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.TreeMap; - -/** - * 1133. Largest Unique Number - * - * Given an array of integers A, return the largest integer that only occurs once. - * If no integer occurs once, return -1. - * - * Example 1: - * Input: [5,7,3,9,4,9,8,3,1] - * Output: 8 - * Explanation: - * The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it's the answer. - * - * Example 2: - * Input: [9,9,8,8] - * Output: -1 - * Explanation: - * There is no number that occurs only once. - * - * Note: - * 1 <= A.length <= 2000 - * 0 <= A[i] <= 1000 - * */ -public class _1133 { - public static class Solution1 { - public int largestUniqueNumber(int[] A) { - TreeMap treeMap = new TreeMap<>((a, b) -> b - a); - for (int num : A) { - treeMap.put(num, treeMap.getOrDefault(num, 0) + 1); - } - for (int key : treeMap.keySet()) { - if (treeMap.get(key) == 1) { - return key; - } - } - return -1; - } - } - - public static class Solution2 { - public int largestUniqueNumber(int[] A) { - int[] count = new int[1001]; - for (int num : A) { - count[num]++; - } - for (int i = 1000; i >= 0; i--) { - if (count[i] == 1) { - return i; - } - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1134.java b/src/main/java/com/fishercoder/solutions/_1134.java deleted file mode 100644 index 7a340b9b13..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1134.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1134. Armstrong Number - * - * The k-digit number N is an Armstrong number if and only if the k-th power of each digit sums to N. - * Given a positive integer N, return true if and only if it is an Armstrong number. - * - * Example 1: - * Input: 153 - * Output: true - * Explanation: - * 153 is a 3-digit number, and 153 = 1^3 + 5^3 + 3^3. - * - * Example 2: - * Input: 123 - * Output: false - * Explanation: - * 123 is a 3-digit number, and 123 != 1^3 + 2^3 + 3^3 = 36. - * - * Note: - * 1 <= N <= 10^8 - * */ -public class _1134 { - public static class Solution1 { - public boolean isArmstrong(int N) { - int numOfDigits = 0; - int copyN = N; - while (copyN > 0) { - copyN /= 10; - numOfDigits++; - } - int sum = 0; - copyN = N; - while (N > 0) { - int digit = N % 10; - sum += Math.pow(digit, numOfDigits); - N /= 10; - } - return sum == copyN; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1137.java b/src/main/java/com/fishercoder/solutions/_1137.java deleted file mode 100644 index 5512a47a74..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1137.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.stream.IntStream; - -/** - * 1137. N-th Tribonacci Number - * - * The Tribonacci sequence Tn is defined as follows: - * - * T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0. - * - * Given n, return the value of Tn. - * - * Example 1: - * Input: n = 4 - * Output: 4 - * - * Explanation: - * T_3 = 0 + 1 + 1 = 2 - * T_4 = 1 + 1 + 2 = 4 - * - * Example 2: - * Input: n = 25 - * Output: 1389537 - * - * Constraints: - * 0 <= n <= 37 - * The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1. - * */ -public class _1137 { - public static class Solution1 { - public int tribonacci(int n) { - if (n <= 1) { - return n; - } - int[] numbers = new int[n + 1]; - numbers[0] = 0; - numbers[1] = 1; - numbers[2] = 1; - IntStream.rangeClosed(3, n).forEach(i -> numbers[i] = numbers[i - 1] + numbers[i - 2] + numbers[i - 3]); - return numbers[n]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_114.java b/src/main/java/com/fishercoder/solutions/_114.java deleted file mode 100644 index 327766b73d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_114.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 114. Flatten Binary Tree to Linked List - * - * Given a binary tree, flatten it to a linked list in-place. - - For example, - Given - - 1 - / \ - 2 5 -/ \ \ -3 4 6 - - The flattened tree should look like: - 1 - \ - 2 - \ - 3 - \ - 4 - \ - 5 - \ - 6 - - Hints: - If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. - */ -public class _114 { - - public static class Solution1 { - public void flatten(TreeNode root) { - while (root != null) { - if (root.left != null) { - TreeNode previousNode = root.left; - while (previousNode.right != null) { - previousNode = previousNode.right; - } - previousNode.right = root.right; - root.right = root.left; - root.left = null; - } - root = root.right; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_115.java b/src/main/java/com/fishercoder/solutions/_115.java deleted file mode 100644 index e993375fc8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_115.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 115. Distinct Subsequences - * - * Given a string S and a string T, count the number of distinct subsequences of S which equals T. A - * subsequence of a string is a new string which is formed from the original string by deleting some - * (can be none) of the characters without disturbing the relative positions of the remaining - * characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not). - * - * Here is an example: S = "rabbbit", T = "rabbit" Return 3. - */ - -public class _115 { - public static class Solution1 { - public int numDistinct(String s, String t) { - int m = s.length(); - int n = t.length(); - int[][] dp = new int[m + 1][n + 1]; - - char[] schar = s.toCharArray(); - char[] tchar = t.toCharArray(); - - for (int i = 0; i <= m; i++) { - dp[i][0] = 1; - } - - for (int j = 1; j <= n; j++) { - dp[0][j] = 0; - } - - for (int i = 1; i <= m; i++) { - for (int j = 1; j <= n; j++) { - if (schar[i - 1] == tchar[j - 1]) { - dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]; - } else { - dp[i][j] = dp[i - 1][j]; - } - } - } - return dp[m][n]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1150.java b/src/main/java/com/fishercoder/solutions/_1150.java deleted file mode 100644 index ddd244933d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1150.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1150. Check If a Number Is Majority Element in a Sorted Array - * - * Given an array nums sorted in non-decreasing order, and a number target, return True if and only if target is a majority element. - * A majority element is an element that appears more than N/2 times in an array of length N. - * - * Example 1: - * Input: nums = [2,4,5,5,5,5,5,6,6], target = 5 - * Output: true - * Explanation: - * The value 5 appears 5 times and the length of the array is 9. - * Thus, 5 is a majority element because 5 > 9/2 is true. - * - * Example 2: - * Input: nums = [10,100,101,101], target = 101 - * Output: false - * Explanation: - * The value 101 appears 2 times and the length of the array is 4. - * Thus, 101 is not a majority element because 2 > 4/2 is false. - * - * Note: - * 1 <= nums.length <= 1000 - * 1 <= nums[i] <= 10^9 - * 1 <= target <= 10^9 - **/ -public class _1150 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/discuss/358130/Java-just-one-binary-search-O(logN))-0ms-beats-100*/ - public boolean isMajorityElement(int[] nums, int target) { - int firstIndex = findFirstOccur(nums, target); - int plusHalfIndex = firstIndex + nums.length / 2; - return plusHalfIndex < nums.length && nums[plusHalfIndex] == target; - } - - private int findFirstOccur(int[] nums, int target) { - int left = 0; - int right = nums.length; - while (left < right) { - int mid = left + (right - left) / 2; - if (nums[mid] < target) { - left = mid + 1; - } else if (nums[mid] >= target) { - right = mid; - } - } - return left; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1154.java b/src/main/java/com/fishercoder/solutions/_1154.java deleted file mode 100644 index c8fd09fe0a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1154.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Calendar; - -/** - * 1154. Day of the Year - * - * Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year. - * - * Example 1: - * - * Input: date = "2019-01-09" - * Output: 9 - * Explanation: Given date is the 9th day of the year in 2019. - * Example 2: - * - * Input: date = "2019-02-10" - * Output: 41 - * Example 3: - * - * Input: date = "2003-03-01" - * Output: 60 - * Example 4: - * - * Input: date = "2004-03-01" - * Output: 61 - * - * Constraints: - * - * date.length == 10 - * date[4] == date[7] == '-', and all other date[i]'s are digits - * date represents a calendar date between Jan 1st, 1900 and Dec 31, 2019. - * */ -public class _1154 { - public static class Solution1 { - Calendar cal = Calendar.getInstance(); - - public int dayOfYear(String date) { - int year = Integer.parseInt(date.substring(0, 4)); - int month = Integer.parseInt(date.substring(5, 7)); - int day = Integer.parseInt(date.substring(8, 10)); - int thirtyDays = 30; - int thirtyOneDays = 31; - if (month == 1) { - return day; - } else if (month == 2) { - return day + thirtyOneDays; - } else { - int daysInFeb = isLeapYear(year) ? 29 : 28; - if (month == 3) { - return thirtyOneDays + daysInFeb + day; - } else if (month == 4) { - return 2 * thirtyOneDays + daysInFeb + day; - } else if (month == 5) { - return 2 * thirtyOneDays + daysInFeb + day + thirtyDays; - } else if (month == 6) { - return 3 * thirtyOneDays + daysInFeb + day + thirtyDays; - } else if (month == 7) { - return 3 * thirtyOneDays + daysInFeb + day + 2 * thirtyDays; - } else if (month == 8) { - return 4 * thirtyOneDays + daysInFeb + day + 2 * thirtyDays; - } else if (month == 9) { - return 5 * thirtyOneDays + daysInFeb + day + 2 * thirtyDays; - } else if (month == 10) { - return 5 * thirtyOneDays + daysInFeb + day + 3 * thirtyDays; - } else if (month == 11) { - return 6 * thirtyOneDays + daysInFeb + day + 3 * thirtyDays; - } else { - return 6 * thirtyOneDays + daysInFeb + day + 4 * thirtyDays; - } - } - } - - private boolean isLeapYear(int year) { - cal.set(Calendar.YEAR, year); - return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_116.java b/src/main/java/com/fishercoder/solutions/_116.java deleted file mode 100644 index 6dbb24551c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_116.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeLinkNode; - -/** - * 116. Populating Next Right Pointers in Each Node - - Given a binary tree - - struct TreeLinkNode { - TreeLinkNode *left; - TreeLinkNode *right; - TreeLinkNode *next; - } - Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. - - Initially, all next pointers are set to NULL. - - Note: - - You may only use constant extra space. - You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). - For example, - Given the following perfect binary tree, - 1 - / \ - 2 3 - / \ / \ - 4 5 6 7 - - After calling your function, the tree should look like: - 1 -> NULL - / \ - 2 -> 3 -> NULL - / \ / \ - 4->5->6->7 -> NULL - */ - -public class _116 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution - * based on level order traversal - */ - public void connect(TreeLinkNode root) { - - TreeLinkNode head = null; //head of the next level - TreeLinkNode prev = null; //the leading node on the next level - TreeLinkNode curr = root; //current node of current level - - while (curr != null) { - while (curr != null) { //iterate on the current level - //left child - if (curr.left != null) { - if (prev != null) { - prev.next = curr.left; - } else { - head = curr.left; - } - prev = curr.left; - } - //right child - if (curr.right != null) { - if (prev != null) { - prev.next = curr.right; - } else { - head = curr.right; - } - prev = curr.right; - } - //move to next node - curr = curr.next; - } - //move to next level - curr = head; - head = null; - prev = null; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1160.java b/src/main/java/com/fishercoder/solutions/_1160.java deleted file mode 100644 index 207889fb9f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1160.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 1160. Find Words That Can Be Formed by Characters - * - * You are given an array of strings words and a string chars. - * A string is good if it can be formed by characters from chars (each character can only be used once). - * Return the sum of lengths of all good strings in words. - * - * Example 1: - * - * Input: words = ["cat","bt","hat","tree"], chars = "atach" - * Output: 6 - * Explanation: - * The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6. - * - * Example 2: - * - * Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr" - * Output: 10 - * Explanation: - * The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10. - * - * Note: - * - * 1 <= words.length <= 1000 - * 1 <= words[i].length, chars.length <= 100 - * All strings contain lowercase English letters only. - * */ -public class _1160 { - public static class Solution1 { - public int countCharacters(String[] words, String chars) { - int length = 0; - Map map = new HashMap<>(); - for (char c : chars.toCharArray()) { - int count = map.getOrDefault(c, 0); - map.put(c, count + 1); - - } - for (String word : words) { - if (canForm(word, map)) { - length += word.length(); - } - } - return length; - } - - private boolean canForm(String word, final Map map) { - Map tmp = new HashMap<>(map); - for (Character c : word.toCharArray()) { - if (tmp.containsKey(c) && tmp.get(c) > 0) { - int count = tmp.get(c); - tmp.put(c, count - 1); - } else { - return false; - } - } - return true; - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1161.java b/src/main/java/com/fishercoder/solutions/_1161.java deleted file mode 100644 index b9bcfc7a4c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1161.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; -import java.util.Queue; -import java.util.TreeMap; - -/** - * 1161. Maximum Level Sum of a Binary Tree - * - * Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. - * Return the smallest level X such that the sum of all the values of nodes at level X is maximal. - * - * Example 1: - * 1 - * / \ - * 7 0 - * / \ - * 7 -8 - * - * Input: [1,7,0,7,-8,null,null] - * Output: 2 - * - * Explanation: - * Level 1 sum = 1. - * Level 2 sum = 7 + 0 = 7. - * Level 3 sum = 7 + -8 = -1. - * So we return the level with the maximum sum which is level 2. - * - * Note: - * The number of nodes in the given tree is between 1 and 10^4. - * -10^5 <= node.val <= 10^5 - * */ -public class _1161 { - public static class Solution1 { - public int maxLevelSum(TreeNode root) { - if (root == null) { - return 0; - } - Queue q = new LinkedList<>(); - q.offer(root); - TreeMap treeMap = new TreeMap<>((a, b) -> b - a); - int level = 1; - while (!q.isEmpty()) { - int size = q.size(); - int sum = 0; - for (int i = 0; i < size; i++) { - TreeNode curr = q.poll(); - sum += curr.val; - if (curr.left != null) { - q.offer(curr.left); - } - if (curr.right != null) { - q.offer(curr.right); - } - } - treeMap.put(sum, level); - level++; - } - return treeMap.firstEntry().getValue(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1165.java b/src/main/java/com/fishercoder/solutions/_1165.java deleted file mode 100644 index 47e1202430..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1165.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1165. Single-Row Keyboard - * - * There is a special keyboard with all keys in a single row. - * Given a string keyboard of length 26 indicating the layout of the keyboard (indexed from 0 to 25), - * initially your finger is at index 0. - * To type a character, you have to move your finger to the index of the desired character. The time taken to move your finger from index i to index j is |i - j|. - * - * You want to type a string word. Write a function to calculate how much time it takes to type it with one finger. - * - * Example 1: - * Input: keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba" - * Output: 4 - * Explanation: The index moves from 0 to 2 to write 'c' then to 1 to write 'b' then to 0 again to write 'a'. - * Total time = 2 + 1 + 1 = 4. - * - * Example 2: - * Input: keyboard = "pqrstuvwxyzabcdefghijklmno", word = "leetcode" - * Output: 73 - * - * Constraints: - * keyboard.length == 26 - * keyboard contains each English lowercase letter exactly once in some order. - * 1 <= word.length <= 10^4 - * word[i] is an English lowercase letter. - * */ -public class _1165 { - public static class Solution1 { - public int calculateTime(String keyboard, String word) { - int time = 0; - int fromIndex = 0; - for (char c : word.toCharArray()) { - int fingerMoves = Math.abs(fromIndex - keyboard.indexOf(c)); - fromIndex = keyboard.indexOf(c); - time += fingerMoves; - } - return time; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_117.java b/src/main/java/com/fishercoder/solutions/_117.java deleted file mode 100644 index 50bc950af3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_117.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeLinkNode; - -/** - * 117. Populating Next Right Pointers in Each Node II - * - * Follow up for problem "Populating Next Right Pointers in Each Node". - * What if the given tree could be any binary tree? Would your previous solution still work? - - Note: - - You may only use constant extra space. - For example, - Given the following binary tree, - 1 - / \ - 2 3 - / \ \ - 4 5 7 - After calling your function, the tree should look like: - 1 -> NULL - / \ - 2 -> 3 -> NULL - / \ \ - 4-> 5 -> 7 -> NULL */ - -public class _117 { - public static class Solution1 { - /**credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution - O(1) space, based on level order traversal*/ - public void connect(TreeLinkNode root) { - - TreeLinkNode head = null; //head of the next level - TreeLinkNode prev = null; //the leading node on the next level - TreeLinkNode cur = root; //current node of current level - - while (cur != null) { - - while (cur != null) { //iterate on the current level - //left child - if (cur.left != null) { - if (prev != null) { - prev.next = cur.left; - } else { - head = cur.left; - } - prev = cur.left; - } - //right child - if (cur.right != null) { - if (prev != null) { - prev.next = cur.right; - } else { - head = cur.right; - } - prev = cur.right; - } - //move to next node - cur = cur.next; - } - - //move to next level - cur = head; - head = null; - prev = null; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1175.java b/src/main/java/com/fishercoder/solutions/_1175.java deleted file mode 100644 index 8f46aa311b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1175.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1175. Prime Arrangements - * - * Return the number of permutations of 1 to n so that prime numbers are at prime indices (1-indexed.) - * (Recall that an integer is prime if and only if it is greater than 1, - * and cannot be written as a product of two positive integers both smaller than it.) - * Since the answer may be large, return the answer modulo 10^9 + 7. - * - * Example 1: - * Input: n = 5 - * Output: 12 - * Explanation: For example [1,2,5,4,3] is a valid permutation, but [5,2,3,4,1] is not because the prime number 5 is at index 1. - * - * Example 2: - * Input: n = 100 - * Output: 682289015 - * - * Constraints: - * 1 <= n <= 100 - * */ -public class _1175 { - public static class Solution1 { - public int numPrimeArrangements(int n) { - //TODO: implement it - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1176.java b/src/main/java/com/fishercoder/solutions/_1176.java deleted file mode 100644 index 7a284c9b5e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1176.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1176. Diet Plan Performance - * - * A dieter consumes calories[i] calories on the i-th day. - * Given an integer k, for every consecutive sequence of k days (calories[i], calories[i+1], ..., calories[i+k-1] - * for all 0 <= i <= n-k), they look at T, the total calories consumed during that sequence of k days (calories[i] + calories[i+1] + ... + calories[i+k-1]): - * If T < lower, they performed poorly on their diet and lose 1 point; - * If T > upper, they performed well on their diet and gain 1 point; - * Otherwise, they performed normally and there is no change in points. - * Initially, the dieter has zero points. Return the total number of points the dieter has after dieting for calories.length days. - * - * Note that the total points can be negative. - * - * Example 1: - * Input: calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3 - * Output: 0 - * Explanation: Since k = 1, we consider each element of the array separately and compare it to lower and upper. - * calories[0] and calories[1] are less than lower so 2 points are lost. - * calories[3] and calories[4] are greater than upper so 2 points are gained. - * - * Example 2: - * Input: calories = [3,2], k = 2, lower = 0, upper = 1 - * Output: 1 - * Explanation: Since k = 2, we consider subarrays of length 2. - * calories[0] + calories[1] > upper so 1 point is gained. - * - * Example 3: - * Input: calories = [6,5,0,0], k = 2, lower = 1, upper = 5 - * Output: 0 - * Explanation: - * calories[0] + calories[1] > upper so 1 point is gained. - * lower <= calories[1] + calories[2] <= upper so no change in points. - * calories[2] + calories[3] < lower so 1 point is lost. - * - * Constraints: - * 1 <= k <= calories.length <= 10^5 - * 0 <= calories[i] <= 20000 - * 0 <= lower <= upper - * */ -public class _1176 { - public static class Solution1 { - public int dietPlanPerformance(int[] calories, int k, int lower, int upper) { - int sum = 0; - int points = 0; - for (int i = 0, j = 0; i <= calories.length - k && j < calories.length; j++) { - sum += calories[j]; - if (j - i > k - 1) { - sum -= calories[i++]; - } - if (j - i == k - 1) { - if (sum > upper) { - points++; - } else if (sum < lower) { - points--; - } - } - } - return points; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_118.java b/src/main/java/com/fishercoder/solutions/_118.java deleted file mode 100644 index 7719e46db0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_118.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 118. Pascal's Triangle - * Given numRows, generate the first numRows of Pascal's triangle. - - For example, given numRows = 5, - Return - - [ - [1], - [1,1], - [1,2,1], - [1,3,3,1], - [1,4,6,4,1] - ] - - */ -public class _118 { - - public static class Solution1 { - /**fill out values from left to right*/ - public List> generate(int numRows) { - List> result = new ArrayList(); - List row = new ArrayList(); - for (int i = 0; i < numRows; i++) { - row.add(0, 1); - for (int j = 1; j < row.size() - 1; j++) { - row.set(j, row.get(j) + row.get(j + 1)); - } - result.add(new ArrayList(row)); - } - return result; - } - } - - public static class Solution2 { - /**fill out values from right to left - * credit: https://leetcode.com/problems/pascals-triangle/discuss/38141/My-concise-solution-in-Java/36127*/ - public List> generate(int numRows) { - List> result = new ArrayList(); - List row = new ArrayList(); - for (int i = 0; i < numRows; i++) { - for (int j = row.size() - 1; j >= 1; j--) { - row.set(j, row.get(j) + row.get(j - 1)); - } - row.add(1); - result.add(new ArrayList<>(row)); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1180.java b/src/main/java/com/fishercoder/solutions/_1180.java deleted file mode 100644 index f5c355d3e5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1180.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1180. Count Substrings with Only One Distinct Letter - * - * Given a string S, return the number of substrings that have only one distinct letter. - * - * Example 1: - * Input: S = "aaaba" - * Output: 8 - * Explanation: The substrings with one distinct letter are "aaa", "aa", "a", "b". - * "aaa" occurs 1 time. - * "aa" occurs 2 times. - * "a" occurs 4 times. - * "b" occurs 1 time. - * So the answer is 1 + 2 + 4 + 1 = 8. - * - * Example 2: - * Input: S = "aaaaaaaaaa" - * Output: 55 - * - * Constraints: - * 1 <= S.length <= 1000 - * S[i] consists of only lowercase English letters. - * */ -public class _1180 { - public static class Solution1 { - public int countLetters(String S) { - int count = 0; - for (int i = 0, j = 1; j < S.length() && i <= j; ) { - while (j < S.length() && S.charAt(i) == S.charAt(j)) { - j++; - } - count += countTimes(S.substring(i, j)); - i += S.substring(i, j).length(); - } - return count; - } - - private int countTimes(String str) { - int len = str.length(); - int times = 0; - while (len > 0) { - times += len--; - } - return times; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1182.java b/src/main/java/com/fishercoder/solutions/_1182.java deleted file mode 100644 index 44c79dae1e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1182.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 1182. Shortest Distance to Target Color - * - * You are given an array colors, in which there are three colors: 1, 2 and 3. - * You are also given some queries. Each query consists of two integers i and c, - * return the shortest distance between the given index i and the target color c. If there is no solution return -1. - * - * Example 1: - * Input: colors = [1,1,2,1,3,2,2,3,3], queries = [[1,3],[2,2],[6,1]] - * Output: [3,0,3] - * Explanation: - * The nearest 3 from index 1 is at index 4 (3 steps away). - * The nearest 2 from index 2 is at index 2 itself (0 steps away). - * The nearest 1 from index 6 is at index 3 (3 steps away). - * - * Example 2: - * Input: colors = [1,2], queries = [[0,3]] - * Output: [-1] - * Explanation: There is no 3 in the array. - * - * Constraints: - * 1 <= colors.length <= 5*10^4 - * 1 <= colors[i] <= 3 - * 1 <= queries.length <= 5*10^4 - * queries[i].length == 2 - * 0 <= queries[i][0] < colors.length - * 1 <= queries[i][1] <= 3 - * */ -public class _1182 { - public static class Solution1 { - public List shortestDistanceColor(int[] colors, int[][] queries) { - Map> map = buildMap(colors); - List result = new ArrayList<>(); - for (int[] query : queries) { - result.add(binarySearch(query[0], map.get(query[1]))); - } - return result; - } - - private Integer binarySearch(int index, List indices) { - if (indices == null) { - return -1; - } - int left = 0; - int right = indices.size() - 1; - int minDistance = Integer.MAX_VALUE; - while (left <= right) { - int mid = left + (right - left) / 2; - if (indices.get(mid) == index) { - return 0; - } else if (indices.get(mid) > index) { - minDistance = Math.min(minDistance, indices.get(mid) - index); - right = mid - 1; - } else { - minDistance = Math.min(minDistance, index - indices.get(mid)); - left = mid + 1; - } - } - return minDistance; - } - - private Map> buildMap(int[] colors) { - Map> map = new HashMap<>(); - for (int i = 0; i < colors.length; i++) { - if (!map.containsKey(colors[i])) { - map.put(colors[i], new ArrayList<>()); - } - map.get(colors[i]).add(i); - } - return map; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1184.java b/src/main/java/com/fishercoder/solutions/_1184.java deleted file mode 100644 index 2ec10b7d67..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1184.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1184. Distance Between Bus Stops - * - * A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n. - * The bus goes along both directions i.e. clockwise and counterclockwise. - * Return the shortest distance between the given start and destination stops. - * - * Example 1: - * Input: distance = [1,2,3,4], start = 0, destination = 1 - * Output: 1 - * Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1. - * - * Example 2: - * Input: distance = [1,2,3,4], start = 0, destination = 2 - * Output: 3 - * Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3. - * - * Example 3: - * Input: distance = [1,2,3,4], start = 0, destination = 3 - * Output: 4 - * Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4. - * - * Constraints: - * 1 <= n <= 10^4 - * distance.length == n - * 0 <= start, destination < n - * 0 <= distance[i] <= 10^4 - * */ -public class _1184 { - public static class Solution1 { - public int distanceBetweenBusStops(int[] distance, int start, int destination) { - if (start > destination) { - int tmp = start; - start = destination; - destination = tmp; - } - int clockwise = 0; - for (int i = start; i < destination; i++) { - clockwise += distance[i]; - } - int counterClockwise = 0; - for (int i = destination; i < distance.length; i++) { - counterClockwise += distance[i]; - } - for (int i = 0; i < start; i++) { - counterClockwise += distance[i]; - } - - return Math.min(clockwise, counterClockwise); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1185.java b/src/main/java/com/fishercoder/solutions/_1185.java deleted file mode 100644 index 0a48adcedb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1185.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1185. Day of the Week - * - * Given a date, return the corresponding day of the week for that date. - * The input is given as three integers representing the day, month and year respectively. - * Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}. - * - * Example 1: - * Input: day = 31, month = 8, year = 2019 - * Output: "Saturday" - * - * Example 2: - * Input: day = 18, month = 7, year = 1999 - * Output: "Sunday" - * - * Example 3: - * Input: day = 15, month = 8, year = 1993 - * Output: "Sunday" - * - * Constraints: - * The given dates are valid dates between the years 1971 and 2100. - * */ -public class _1185 { - public static class Solution1 { - /** - * Time: O(1) - * Space: O(1) - * - * Plain and simple algorithm: - * based on the fact that 1/1/1971 is a Friday and calculate the given day. - * */ - public String dayOfTheWeek(int day, int month, int year) { - String[] daysInTheWeek = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; - int[] daysInTheMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - int numberOfDays = 0; - for (int i = 1971; i < year; i++) { - numberOfDays += i % 4 == 0 ? 366 : 365; - } - if (year % 4 == 0) { - daysInTheMonth[1] = 29; - } - for (int i = 0; i < month - 1; i++) { - numberOfDays += daysInTheMonth[i]; - } - numberOfDays += day - 1; - return daysInTheWeek[(numberOfDays + 5) % 7]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1186.java b/src/main/java/com/fishercoder/solutions/_1186.java deleted file mode 100644 index 11b41f4b9a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1186.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1186. Maximum Subarray Sum with One Deletion - * - * Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. - * In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element - * left and the sum of the remaining elements is maximum possible. - * Note that the subarray needs to be non-empty after deleting one element. - * - * Example 1: - * Input: arr = [1,-2,0,3] - * Output: 4 - * Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value. - * - * Example 2: - * Input: arr = [1,-2,-2,3] - * Output: 3 - * Explanation: We just choose [3] and it's the maximum sum. - * - * Example 3: - * Input: arr = [-1,-1,-1,-1] - * Output: -1 - * Explanation: The final subarray needs to be non-empty. You can't choose [-1] and delete -1 from it, then get an empty subarray to make the sum equals to 0. - * - * Constraints: - * 1 <= arr.length <= 10^5 - * -10^4 <= arr[i] <= 10^4 - * */ -public class _1186 { - public static class Solution1 { - public int maximumSum(int[] arr) { - //TODO: implement it - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1189.java b/src/main/java/com/fishercoder/solutions/_1189.java deleted file mode 100644 index e04b0c733b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1189.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1189. Maximum Number of Balloons - * - * Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible. - * You can use each character in text at most once. Return the maximum number of instances that can be formed. - * - * Example 1: - * Input: text = "nlaebolko" - * Output: 1 - * - * Example 2: - * Input: text = "loonbalxballpoon" - * Output: 2 - * - * Example 3: - * Input: text = "leetcode" - * Output: 0 - * - * Constraints: - * 1 <= text.length <= 10^4 - * text consists of lower case English letters only. - * */ -public class _1189 { - public static class Solution1 { - public int maxNumberOfBalloons(String text) { - int[] counts = new int[26]; - for (char c : text.toCharArray()) { - counts[c - 'a']++; - } - return Math.min(counts[0], Math.min(counts[1], Math.min(counts[11] / 2, Math.min(counts[14] / 2, counts[13])))); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_119.java b/src/main/java/com/fishercoder/solutions/_119.java deleted file mode 100644 index 8ceff67bad..0000000000 --- a/src/main/java/com/fishercoder/solutions/_119.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 119. Pascal's Triangle II - * Given an index k, return the kth row of the Pascal's triangle. - - For example, given k = 3, - Return [1,3,3,1]. - - Note: - Could you optimize your algorithm to use only O(k) extra space? - */ - -public class _119 { - - public static class Solution1 { - public List getRow(int rowIndex) { - if (rowIndex < 0) { - return new ArrayList(); - } - List> result = new ArrayList(); - List row = new ArrayList(); - row.add(1); - result.add(row); - for (int i = 1; i <= rowIndex; i++) { - List newRow = new ArrayList(); - newRow.add(1); - List lastRow = result.get(i - 1); - for (int j = 1; j < lastRow.size(); j++) { - newRow.add(lastRow.get(j - 1) + lastRow.get(j)); - } - newRow.add(1); - result.add(newRow); - } - return result.get(result.size() - 1); - } - } - - public static class Solution2 { - /** O(k) space */ - public List getRow(int rowIndex) { - List row = new ArrayList<>(); - for (int i = 0; i <= rowIndex; i++) { - row.add(0, 1); - for (int j = 1; j < row.size() - 1; j++) { - row.set(j, row.get(j) + row.get(j + 1)); - } - } - return row; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1190.java b/src/main/java/com/fishercoder/solutions/_1190.java deleted file mode 100644 index 6434fb7da6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1190.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; -import java.util.Stack; - -/** - * 1190. Reverse Substrings Between Each Pair of Parentheses - * - * You are given a string s that consists of lower case English letters and brackets. - * Reverse the strings in each pair of matching parentheses, starting from the innermost one. - * Your result should not contain any brackets. - * - * Example 1: - * Input: s = "(abcd)" - * Output: "dcba" - * - * Example 2: - * Input: s = "(u(love)i)" - * Output: "iloveu" - * Explanation: The substring "love" is reversed first, then the whole string is reversed. - * - * Example 3: - * Input: s = "(ed(et(oc))el)" - * Output: "leetcode" - * Explanation: First, we reverse the substring "oc", then "etco", and finally, the whole string. - * - * Example 4: - * Input: s = "a(bcdefghijkl(mno)p)q" - * Output: "apmnolkjihgfedcbq" - * - * Constraints: - * 0 <= s.length <= 2000 - * s only contains lower case English characters and parentheses. - * It's guaranteed that all parentheses are balanced. - * */ -public class _1190 { - public static class Solution1 { - public String reverseParentheses(String s) { - Stack stack = new Stack<>(); - Queue queue = new LinkedList<>(); - for (char c : s.toCharArray()) { - if (c != ')') { - stack.push(c); - } else { - while (!stack.isEmpty() && stack.peek() != '(') { - queue.offer(stack.pop()); - } - if (!stack.isEmpty()) { - stack.pop();//pop off the open paren - } - while (!queue.isEmpty()) { - stack.push(queue.poll()); - } - } - } - StringBuilder sb = new StringBuilder(); - while (!stack.isEmpty()) { - sb.append(stack.pop()); - } - return sb.reverse().toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1196.java b/src/main/java/com/fishercoder/solutions/_1196.java deleted file mode 100644 index c00618a02c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1196.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1196. How Many Apples Can You Put into the Basket - * - * You have some apples, where arr[i] is the weight of the i-th apple. You also have a basket that can carry up to 5000 units of weight. - * Return the maximum number of apples you can put in the basket. - * - * Example 1: - * Input: arr = [100,200,150,1000] - * Output: 4 - * Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450. - * - * Example 2: - * Input: arr = [900,950,800,1000,700,800] - * Output: 5 - * Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them. - * - * Constraints: - * 1 <= arr.length <= 10^3 - * 1 <= arr[i] <= 10^3 - * */ -public class _1196 { - public static class Solution1 { - public int maxNumberOfApples(int[] arr) { - Arrays.sort(arr); - int sum = 0; - int i = 0; - for (; i < arr.length; i++) { - sum += arr[i]; - if (sum > 5000) { - break; - } - } - return i; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1198.java b/src/main/java/com/fishercoder/solutions/_1198.java deleted file mode 100644 index f3f45f6f01..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1198.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1198. Find Smallest Common Element in All Rows - * - * Given a matrix mat where every row is sorted in increasing order, return the smallest common element in all rows. - * If there is no common element, return -1. - * - * Example 1: - * Input: mat = [[1,2,3,4,5],[2,4,5,8,10],[3,5,7,9,11],[1,3,5,7,9]] - * Output: 5 - * - * Constraints: - * 1 <= mat.length, mat[i].length <= 500 - * 1 <= mat[i][j] <= 10^4 - * mat[i] is sorted in increasing order. - * */ -public class _1198 { - public static class Solution1 { - public int smallestCommonElement(int[][] mat) { - int m = mat.length; - int n = mat[0].length; - for (int j = 0; j < n; j++) { - int minCommon = mat[0][j]; - //we'll start from the second row - int i = 1; - for (; i < m; i++) { - if (thisRowHasThisNumber(mat[i], minCommon)) { - continue; - } else { - break; - } - } - if (i == m) { - return minCommon; - } - } - return -1; - } - - private boolean thisRowHasThisNumber(int[] nums, int target) { - int left = 0; - int right = nums.length - 1; - while (left <= right) { - int mid = left + (right - left) / 2; - if (target == nums[mid]) { - return true; - } else if (target > nums[mid]) { - left = mid + 1; - } else { - right = mid - 1; - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_120.java b/src/main/java/com/fishercoder/solutions/_120.java deleted file mode 100644 index 3d44f0fac4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_120.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.List; - -/** - * 120. Triangle - - Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. - - For example, given the following triangle - [ - [2], - [3,4], - [6,5,7], - [4,1,8,3] - ] - The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). - - Note: - Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.*/ - -public class _120 { - public static class Solution1 { - public int minimumTotal(List> triangle) { - int n = triangle.size(); - List cache = triangle.get(n - 1); - - for (int layer = n - 2; layer >= 0; layer--) { - //for each layer - for (int i = 0; i <= layer; i++) { - //check its very node - int value = Math.min(cache.get(i), cache.get(i + 1)) + triangle.get(layer).get(i); - cache.set(i, value); - } - } - return cache.get(0); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1200.java b/src/main/java/com/fishercoder/solutions/_1200.java deleted file mode 100644 index f9b3c63603..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1200.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 1200. Minimum Absolute Difference - * - * Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements. - * Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows - * a, b are from arr - * a < b - * b - a equals to the minimum absolute difference of any two elements in arr - * - * Example 1: - * Input: arr = [4,2,1,3] - * Output: [[1,2],[2,3],[3,4]] - * Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order. - * - * Example 2: - * Input: arr = [1,3,6,10,15] - * Output: [[1,3]] - * - * Example 3: - * Input: arr = [3,8,-10,23,19,-4,-14,27] - * Output: [[-14,-10],[19,23],[23,27]] - * - * Constraints: - * 2 <= arr.length <= 10^5 - * -10^6 <= arr[i] <= 10^6 - * */ -public class _1200 { - public static class Solution1 { - /** - * Time: O(nlogn) due to sorting - * Space: O(k) where k is the distinct number of differences between two numbers in the given array - * */ - public List> minimumAbsDifference(int[] arr) { - Arrays.sort(arr); - int minimumDiff = arr[1] - arr[0]; - List result = new ArrayList(); - result.add(Arrays.asList(arr[0], arr[1])); - for (int i = 1; i < arr.length - 1; i++) { - int diff = arr[i + 1] - arr[i]; - if (minimumDiff == diff) { - result.add(Arrays.asList(arr[i], arr[i + 1])); - } else if (minimumDiff > diff) { - minimumDiff = diff; - result = new ArrayList(); - result.add(Arrays.asList(arr[i], arr[i + 1])); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1207.java b/src/main/java/com/fishercoder/solutions/_1207.java deleted file mode 100644 index 0d432c54fe..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1207.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 1207. Unique Number of Occurrences - * - * Given an array of integers arr, - * write a function that returns true if and only if the number of occurrences of each value in the array is unique. - * - * Example 1: - * Input: arr = [1,2,2,1,1,3] - * Output: true - * Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences. - * - * Example 2: - * Input: arr = [1,2] - * Output: false - * - * Example 3: - * Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] - * Output: true - * - * Constraints: - * 1 <= arr.length <= 1000 - * -1000 <= arr[i] <= 1000 - * */ -public class _1207 { - public static class Solution1 { - public boolean uniqueOccurrences(int[] arr) { - Map map = new HashMap<>(); - Arrays.stream(arr).forEach(num -> { - map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1); - }); - Set set = new HashSet<>(); - return map.keySet().stream().mapToInt(key -> key).allMatch(key -> set.add(map.get(key))); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1209.java b/src/main/java/com/fishercoder/solutions/_1209.java deleted file mode 100644 index c66aff8a5f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1209.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 1209. Remove All Adjacent Duplicates in String II - * - * Given a string s, a k duplicate removal consists of choosing k adjacent and equal - * letters from s and removing them causing the left and the right side of the deleted substring to concatenate together. - * We repeatedly make k duplicate removals on s until we no longer can. - * Return the final string after all such duplicate removals have been made. - * It is guaranteed that the answer is unique. - * - * Example 1: - * Input: s = "abcd", k = 2 - * Output: "abcd" - * Explanation: There's nothing to delete. - * - * Example 2: - * Input: s = "deeedbbcccbdaa", k = 3 - * Output: "aa" - * Explanation: - * First delete "eee" and "ccc", get "ddbbbdaa" - * Then delete "bbb", get "dddaa" - * Finally delete "ddd", get "aa" - * - * Example 3: - * Input: s = "pbbcggttciiippooaais", k = 2 - * Output: "ps" - * - * Constraints: - * 1 <= s.length <= 10^5 - * 2 <= k <= 10^4 - * s only contains lower case English letters. - * */ -public class _1209 { - public static class Solution1 { - public String removeDuplicates(String s, int k) { - Stack stack = new Stack<>(); - char c = s.charAt(0); - stack.push(c); - int count = 1; - for (int i = 1; i < s.length(); i++) { - if (s.charAt(i) == c) { - count++; - stack.push(s.charAt(i)); - if (count == k) { - while (!stack.isEmpty() && stack.peek() == c) { - stack.pop(); - } - count = 0; - if (!stack.isEmpty()) { - c = stack.peek(); - while (!stack.isEmpty() && c == stack.peek()) { - count++; - stack.pop(); - } - int tmp = count; - while (tmp-- > 0) { - stack.push(c); - } - } - } - } else { - c = s.charAt(i); - stack.push(s.charAt(i)); - count = 1; - } - } - StringBuilder sb = new StringBuilder(); - while (!stack.isEmpty()) { - sb.append(stack.pop()); - } - return sb.reverse().toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_121.java b/src/main/java/com/fishercoder/solutions/_121.java deleted file mode 100644 index b7dfb91b01..0000000000 --- a/src/main/java/com/fishercoder/solutions/_121.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 121. Best Time to Buy and Sell Stock - * - * Say you have an array for which the ith element is the price of a given stock on day i. - * - * If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), - * design an algorithm to find the maximum profit. - * - * Example 1: - * Input: [7, 1, 5, 3, 6, 4] - * Output: 5 - * - * max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price) - * - * Example 2: - * Input: [7, 6, 4, 3, 1] - * Output: 0 - * - * In this case, no transaction is done, i.e. max profit = 0. - */ - -public class _121 { - - public static class Solution1 { - /** - * The key here is that you'll have to buy first, before you can sell. That means, if the lower - * price comes after a higher price, their combination won't work! Since you cannot sell first - * before you buy it. - */ - public int maxProfit(int[] prices) { - if (prices == null || prices.length == 0) { - return 0; - } - int minBuy = prices[0]; - int maxProfit = 0; - for (int i = 1; i < prices.length; i++) { - if (prices[i] < minBuy) { - minBuy = prices[i]; - } else { - maxProfit = Math.max(maxProfit, prices[i] - minBuy); - } - } - return maxProfit; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1213.java b/src/main/java/com/fishercoder/solutions/_1213.java deleted file mode 100644 index 765b50ab4d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1213.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1213. Intersection of Three Sorted Arrays - * - * Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, - * return a sorted array of only the integers that appeared in all three arrays. - * - * Example 1: - * Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8] - * Output: [1,5] - * Explanation: Only 1 and 5 appeared in the three arrays. - * - * Constraints: - * - * 1 <= arr1.length, arr2.length, arr3.length <= 1000 - * 1 <= arr1[i], arr2[i], arr3[i] <= 2000 - * */ -public class _1213 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/intersection-of-three-sorted-arrays/discuss/397603/Simple-Java-solution-beats-100*/ - public List arraysIntersection(int[] arr1, int[] arr2, int[] arr3) { - List result = new ArrayList(); - int i = 0; - int j = 0; - int k = 0; - while (i < arr1.length && j < arr2.length && k < arr3.length) { - if (arr1[i] == arr2[j] && arr1[i] == arr3[k]) { - result.add(arr1[i]); - i++; - j++; - k++; - } else if (arr1[i] < arr2[j]) { - i++; - } else if (arr2[j] < arr3[k]) { - j++; - } else { - k++; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1214.java b/src/main/java/com/fishercoder/solutions/_1214.java deleted file mode 100644 index bdacf9fa79..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1214.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1214. Two Sum BSTs - * - * Given two binary search trees, - * return True if and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integer target. - * - * Example 1: - * 2 1 - * / \ / \ - * 1 4 0 3 - * - * Input: root1 = [2,1,4], root2 = [1,0,3], target = 5 - * Output: true - * Explanation: 2 and 3 sum up to 5. - * - * Example 2: - * 0 5 - * / \ / \ - * -10 10 1 7 - * / \ - * 0 2 - * - * Input: root1 = [0,-10,10], root2 = [5,1,7,0,2], target = 18 - * Output: false - * - * Constraints: - * Each tree has at most 5000 nodes. - * -10^9 <= target, node.val <= 10^9 - * */ -public class _1214 { - public static class Solution1 { - public boolean twoSumBSTs(TreeNode root1, TreeNode root2, int target) { - if (root1 == null || root2 == null) { - return false; - } - List inorder1 = inorderDfs(root1, new ArrayList<>()); - List inorder2 = inorderDfs(root2, new ArrayList<>()); - return findTwoSum(inorder1, inorder2, target); - } - - private boolean findTwoSum(List sorted1, List sorted2, int target) { - for (int i = 0; i < sorted1.size(); i++) { - if (exists(sorted2, target - sorted1.get(i))) { - return true; - } - } - return false; - } - - private boolean exists(List sorted, int target) { - int left = 0; - int right = sorted.size() - 1; - while (left <= right) { - int mid = left + (right - left) / 2; - if (sorted.get(mid) == target) { - return true; - } else if (sorted.get(mid) < target) { - left = mid + 1; - } else { - right = mid - 1; - } - } - return false; - } - - private List inorderDfs(TreeNode root, List list) { - if (root == null) { - return list; - } - if (root.left != null) { - list = inorderDfs(root.left, list); - } - list.add(root.val); - if (root.right != null) { - list = inorderDfs(root.right, list); - } - return list; - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1217.java b/src/main/java/com/fishercoder/solutions/_1217.java deleted file mode 100644 index 79775e612c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1217.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1217. Play with Chips - * - * There are some chips, and the i-th chip is at position chips[i]. - * You can perform any of the two following types of moves any number of times (possibly zero) on any chip: - * - * Move the i-th chip by 2 units to the left or to the right with a cost of 0. - * Move the i-th chip by 1 unit to the left or to the right with a cost of 1. - * - * There can be two or more chips at the same position initially. - * - * Return the minimum cost needed to move all the chips to the same position (any position). - * - * Example 1: - * Input: chips = [1,2,3] - * Output: 1 - * Explanation: Second chip will be moved to positon 3 with cost 1. First chip will be moved to position 3 with cost 0. Total cost is 1. - * - * Example 2: - * Input: chips = [2,2,2,3,3] - * Output: 2 - * Explanation: Both fourth and fifth chip will be moved to position two with cost 1. Total minimum cost will be 2. - * - * Constraints: - * 1 <= chips.length <= 100 - * 1 <= chips[i] <= 10^9 - * */ -public class _1217 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/play-with-chips/discuss/398239/C%2B%2B-3-lines*/ - public int minCostToMoveChips(int[] chips) { - int chipsAtOddPosition = 0; - int chipsAtEvenPosition = 0; - for (int i = 0; i < chips.length; i++) { - if (chips[i] % 2 == 0) { - chipsAtEvenPosition++; - } else { - chipsAtOddPosition++; - } - } - return chipsAtEvenPosition > chipsAtOddPosition ? chipsAtOddPosition : chipsAtEvenPosition; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1219.java b/src/main/java/com/fishercoder/solutions/_1219.java deleted file mode 100644 index 7da7f50633..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1219.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 1219. Path with Maximum Gold - * - * In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. - * Return the maximum amount of gold you can collect under the conditions: - * Every time you are located in a cell you will collect all the gold in that cell. - * From your position you can walk one step to the left, right, up or down. - * You can't visit the same cell more than once. - * Never visit a cell with 0 gold. - * You can start and stop collecting gold from any position in the grid that has some gold. - * - * Example 1: - * Input: grid = [[0,6,0],[5,8,7],[0,9,0]] - * Output: 24 - * Explanation: - * [[0,6,0], - * [5,8,7], - * [0,9,0]] - * Path to get the maximum gold, 9 -> 8 -> 7. - * - * Example 2: - * Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] - * Output: 28 - * Explanation: - * [[1,0,7], - * [2,0,6], - * [3,4,5], - * [0,3,0], - * [9,0,20]] - * Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7. - * - * Constraints: - * 1 <= grid.length, grid[i].length <= 15 - * 0 <= grid[i][j] <= 100 - * There are at most 25 cells containing gold. - * */ -public class _1219 { - public static class Solution1 { - public int getMaximumGold(int[][] grid) { - Queue queue = new LinkedList<>(); - int m = grid.length; - int n = grid[0].length; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] > 0) { - queue.offer(new int[]{i, j}); - } - } - } - int maxGold = 0; - while (!queue.isEmpty()) { - int[] start = queue.poll(); - boolean[][] visited = new boolean[m][n]; - visited[start[0]][start[1]] = true; - maxGold = Math.max(maxGold, backtracking(grid, start, grid[start[0]][start[1]], visited)); - } - return maxGold; - } - - int[] directions = new int[]{0, 1, 0, -1, 0}; - - private int backtracking(int[][] grid, int[] start, int gold, boolean[][] visited) { - int max = gold; - for (int i = 0; i < directions.length - 1; i++) { - int nextX = start[0] + directions[i]; - int nextY = start[1] + directions[i + 1]; - if (nextX >= 0 && nextX < grid.length && nextY >= 0 && nextY < grid[0].length && !visited[nextX][nextY] && grid[nextX][nextY] > 0) { - visited[nextX][nextY] = true; - max = Math.max(max, backtracking(grid, new int[]{nextX, nextY}, gold + grid[nextX][nextY], visited)); - visited[nextX][nextY] = false; - } - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_122.java b/src/main/java/com/fishercoder/solutions/_122.java deleted file mode 100644 index e7707f2f9c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_122.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 122. Best Time to Buy and Sell Stock II - * - * Say you have an array for which the ith element is the price of a given stock on day i. - * Design an algorithm to find the maximum profit. You may complete as many transactions as you like - * (ie, buy one and sell one share of the stock multiple times). - * However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). - * */ - -public class _122 { - public static class Solution1 { - //peak and valley approach - public int maxProfit(int[] prices) { - int pro = 0; - int i = 0; - while (i < prices.length - 1) { - while (i < prices.length - 1 && prices[i] >= prices[i + 1]) { - i++; - } - int valley = prices[i]; - while (i < prices.length - 1 && prices[i] <= prices[i + 1]) { - i++; - } - int peak = prices[i]; - pro += peak - valley; - } - return pro; - } - } - - public static class Solution2 { - //simple one pass approach: the above solution could be simplied as below - public int maxProfit(int[] prices) { - int pro = 0; - for (int i = 0; i < prices.length - 1; i++) { - if (prices[i + 1] > prices[i]) { - pro += prices[i + 1] - prices[i]; - } - } - return pro; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1221.java b/src/main/java/com/fishercoder/solutions/_1221.java deleted file mode 100644 index 8c455eeeae..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1221.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1221. Split a String in Balanced Strings - * - * Balanced strings are those who have equal quantity of 'L' and 'R' characters. - * Given a balanced string s split it in the maximum amount of balanced strings. - * Return the maximum amount of splitted balanced strings. - * - * Example 1: - * Input: s = "RLRRLLRLRL" - * Output: 4 - * Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'. - * - * Example 2: - * Input: s = "RLLLLRRRLR" - * Output: 3 - * Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'. - * - * Example 3: - * Input: s = "LLLLRRRR" - * Output: 1 - * Explanation: s can be split into "LLLLRRRR". - * - * Example 4: - * Input: s = "RLRRRLLRLL" - * Output: 2 - * Explanation: s can be split into "RL", "RRRLLRLL", since each substring contains an equal number of 'L' and 'R' - * - * Constraints: - * 1 <= s.length <= 1000 - * s[i] = 'L' or 'R' - * */ -public class _1221 { - public static class Solution1 { - public int balancedStringSplit(String s) { - int i = 0; - int balancedCount = 0; - int lCount = 0; - int rCount = 0; - while (i < s.length()) { - if (s.charAt(i) == 'L') { - lCount++; - } else { - rCount++; - } - i++; - if (lCount != 0 && lCount == rCount) { - lCount = 0; - rCount = 0; - balancedCount++; - } - } - return balancedCount; - } - } - - public static class Solution2 { - public int balancedStringSplit(String s) { - int count = 0; - int result = 0; - int i = 0; - while (i < s.length()) { - if (s.charAt(i) == 'L') { - count++; - } else { - count--; - } - if (count == 0) { - result++; - } - i++; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1228.java b/src/main/java/com/fishercoder/solutions/_1228.java deleted file mode 100644 index 4cf8e844ca..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1228.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 1228. Missing Number In Arithmetic Progression - * - * In some array arr, the values were in arithmetic progression: the values arr[i+1] - arr[i] are all equal for every 0 <= i < arr.length - 1. - * Then, a value from arr was removed that was not the first or last value in the array. - * Return the removed value. - * - * Example 1: - * Input: arr = [5,7,11,13] - * Output: 9 - * Explanation: The previous array was [5,7,9,11,13]. - * - * Example 2: - * Input: arr = [15,13,12] - * Output: 14 - * Explanation: The previous array was [15,14,13,12]. - * - * Constraints: - * 3 <= arr.length <= 1000 - * 0 <= arr[i] <= 10^5 - * */ -public class _1228 { - public static class Solution1 { - /**A super verbose and inefficient but working way...*/ - public int missingNumber(int[] arr) { - Arrays.sort(arr); - Map> map = new HashMap<>(); - for (int i = 0; i < arr.length - 1; i++) { - int diff = arr[i + 1] - arr[i]; - List list = map.getOrDefault(diff, new ArrayList<>()); - list.add(i); - map.put(diff, list); - } - int smallDiff = arr[arr.length - 1]; - int bigDiff = 0; - for (int key : map.keySet()) { - smallDiff = Math.min(smallDiff, key); - bigDiff = Math.max(bigDiff, key); - } - return arr[map.get(bigDiff).get(0)] + smallDiff; - } - } - - public static class Solution2 { - /**credit: https://leetcode.com/problems/missing-number-in-arithmetic-progression/discuss/408474/JavaC%2B%2BPython-Arithmetic-Sum-and-Binary-Search*/ - public int missingNumber(int[] arr) { - int min = arr[0]; - int max = arr[0]; - int sum = 0; - for (int num : arr) { - max = Math.max(max, num); - min = Math.min(min, num); - sum += num; - } - return (max + min) * (arr.length + 1) / 2 - sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_123.java b/src/main/java/com/fishercoder/solutions/_123.java deleted file mode 100644 index 4ec308124d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_123.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 123. Best Time to Buy and Sell Stock III - * - * Say you have an array for which the ith element is the price of a given stock on day i. - * Design an algorithm to find the maximum profit. You may complete at most two transactions. - -Note: -You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). - */ - -public class _123 { - - public static class Solution1 { - //this is a very clear solution and very highly upvoted in Discuss, but not extensible to K solution. - public int maxProfit(int[] prices) { - int buy1 = Integer.MIN_VALUE; - int buy2 = Integer.MIN_VALUE; - int sell1 = 0; - int sell2 = 0; - for (int i = 0; i < prices.length; i++) { - buy1 = Math.max(buy1, -prices[i]); - sell1 = Math.max(sell1, buy1 + prices[i]); - buy2 = Math.max(buy2, sell1 - prices[i]); - sell2 = Math.max(sell2, buy2 + prices[i]); - } - return sell2; - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_1232.java b/src/main/java/com/fishercoder/solutions/_1232.java deleted file mode 100644 index 4ba244ae54..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1232.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder.solutions; - -public class _1232 { - public static class Solution1 { - /** - * To check if they share the same slope, we use this formula: - * - * check whether (y4 - y3)/(x4- x3) equals to (y2 - y1)/(x2 - x1) - * considering denominator could be zero, we'll change it to use multiplication instead of division, - * thus it becomes - * check whether (y4 - y3)*(x2 - x1) equals (x4 - x3)*(y2 - y1) - */ - public boolean checkStraightLine(int[][] coordinates) { - for (int i = 2; i < coordinates.length - 1; i++) { - if ((coordinates[1][0] - coordinates[0][0]) * (coordinates[i + 1][1] - coordinates[i][1]) - != (coordinates[1][1] - coordinates[0][1]) * (coordinates[i + 1][0] - coordinates[i][0])) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1237.java b/src/main/java/com/fishercoder/solutions/_1237.java deleted file mode 100644 index cedc09f9e2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1237.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 1237. Find Positive Integer Solution for a Given Equation - * - * Given a function f(x, y) and a value z, return all positive integer pairs x and y where f(x,y) == z. - * The function is constantly increasing, i.e.: - * - * f(x, y) < f(x + 1, y) - * f(x, y) < f(x, y + 1) - * The function interface is defined like this: - * - * interface CustomFunction { - * public: - * // Returns positive integer f(x, y) for any given positive integer x and y. - * int f(int x, int y); - * }; - * For custom testing purposes you're given an integer function_id and a target z as input, - * where function_id represent one function from an secret internal list, on the examples you'll know only two functions from the list. - * You may return the solutions in any order. - * - * Example 1: - * Input: function_id = 1, z = 5 - * Output: [[1,4],[2,3],[3,2],[4,1]] - * Explanation: function_id = 1 means that f(x, y) = x + y - * - * Example 2: - * Input: function_id = 2, z = 5 - * Output: [[1,5],[5,1]] - * Explanation: function_id = 2 means that f(x, y) = x * y - * - * Constraints: - * 1 <= function_id <= 9 - * 1 <= z <= 100 - * It's guaranteed that the solutions of f(x, y) == z will be on the range 1 <= x, y <= 1000 - * It's also guaranteed that f(x, y) will fit in 32 bit signed integer if 1 <= x, y <= 1000 - */ -public class _1237 { - - // This is the custom function interface. - // You should not implement it, or speculate about its implementation - abstract class CustomFunction { - // Returns f(x, y) for any given positive integers x and y. - // Note that f(x, y) is increasing with respect to both x and y. - // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1) - public abstract int f(int x, int y); - } - - public static class Solution1 { - /**Time: O(x*y) - * Space: O(1)*/ - public List> findSolution(CustomFunction customfunction, int z) { - List> result = new ArrayList<>(); - for (int x = 1; x <= 1000; x++) { - for (int y = 1; y <= 1000; y++) { - if (customfunction.f(x, y) == z) { - result.add(Arrays.asList(x, y)); - } - } - } - return result; - } - } - - public static class Solution2 { - /** - * linear search - * - * Time: O(x + y) - * Space: O(1) - */ - public List> findSolution(CustomFunction customfunction, int z) { - List> result = new ArrayList<>(); - int x = 1; - int y = 1000; - while (x < 1001 && y > 0) { - int functionResult = customfunction.f(x, y); - if (functionResult < z) { - x++; - } else if (functionResult > z) { - y--; - } else { - result.add(Arrays.asList(x++, y--)); - } - } - return result; - } - } - - public static class Solution3 { - /** - * binary search - * - * Time: O(xlogy) - * Space: O(1) - * */ - public List> findSolution(CustomFunction customfunction, int z) { - List> result = new ArrayList<>(); - for (int x = 1; x <= 1000; x++) { - int left = 1; - int right = 1001; - while (left < right) { - int y = (left + right) / 2; - if (customfunction.f(x, y) < z) { - left = y + 1; - } else { - right = y; - } - } - if (customfunction.f(x, left) == z) { - result.add(Arrays.asList(x, left)); - } - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_1243.java b/src/main/java/com/fishercoder/solutions/_1243.java deleted file mode 100644 index b4b432740e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1243.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -/** - * 1243. Array Transformation - * - * Given an initial array arr, every day you produce a new array using the array of the previous day. - * - * On the i-th day, you do the following operations on the array of day i-1 to produce the array of day i: - * - * If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented. - * If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented. - * The first and last elements never change. - * After some days, the array does not change. Return that final array. - * - * Example 1: - * Input: arr = [6,2,3,4] - * Output: [6,3,3,4] - * Explanation: - * On the first day, the array is changed from [6,2,3,4] to [6,3,3,4]. - * No more operations can be done to this array. - * - * Example 2: - * Input: arr = [1,6,3,4,3,5] - * Output: [1,4,4,4,4,5] - * Explanation: - * On the first day, the array is changed from [1,6,3,4,3,5] to [1,5,4,3,4,5]. - * On the second day, the array is changed from [1,5,4,3,4,5] to [1,4,4,4,4,5]. - * No more operations can be done to this array. - * - * Constraints: - * 1 <= arr.length <= 100 - * 1 <= arr[i] <= 100 - * */ -public class _1243 { - public static class Solution1 { - public List transformArray(int[] arr) { - int[] copy; - do { - copy = Arrays.copyOf(arr, arr.length); - for (int i = 1; i < arr.length - 1; i++) { - if (copy[i] < copy[i - 1] && copy[i] < copy[i + 1]) { - arr[i]++; - } else if (copy[i] > copy[i - 1] && copy[i] > copy[i + 1]) { - arr[i]--; - } - } - } while (!Arrays.equals(copy, arr)); - return Arrays.stream(arr) - .boxed() - .collect(Collectors.toList()); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1249.java b/src/main/java/com/fishercoder/solutions/_1249.java deleted file mode 100644 index a9585c5ab4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1249.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 1249. Minimum Remove to Make Valid Parentheses - * - * Given a string s of '(' , ')' and lowercase English characters. - * Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) - * so that the resulting parentheses string is valid and return any valid string. - * Formally, a parentheses string is valid if and only if: - * It is the empty string, contains only lowercase characters, or - * It can be written as AB (A concatenated with B), where A and B are valid strings, or - * It can be written as (A), where A is a valid string. - * - * Example 1: - * Input: s = "lee(t(c)o)de)" - * Output: "lee(t(c)o)de" - * Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted. - * - * Example 2: - * Input: s = "a)b(c)d" - * Output: "ab(c)d" - * - * Example 3: - * Input: s = "))((" - * Output: "" - * Explanation: An empty string is also valid. - * - * Example 4: - * Input: s = "(a(b(c)d)" - * Output: "a(b(c)d)" - * - * Constraints: - * 1 <= s.length <= 10^5 - * s[i] is one of '(' , ')' and lowercase English letters. - * */ -public class _1249 { - public static class Solution1 { - public String minRemoveToMakeValid(String s) { - //TODO: implement it - return ""; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_125.java b/src/main/java/com/fishercoder/solutions/_125.java deleted file mode 100644 index b849b4017b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_125.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 125. Valid Palindrome - - Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. - - For example, - "A man, a plan, a canal: Panama" is a palindrome. - "race a car" is not a palindrome. - - Note: - Have you consider that the string might be empty? This is a good question to ask during an interview. - For the purpose of this problem, we define empty string as valid palindrome. - */ - -public class _125 { - - public static class Solution1 { - public boolean isPalindrome(String s) { - int i = 0; - int j = s.length() - 1; - char[] chars = s.toCharArray(); - while (i < j) { - while (i < j && !Character.isLetterOrDigit(chars[i])) { - i++; - } - while (i < j && !Character.isLetterOrDigit(chars[j])) { - j--; - } - if (Character.toLowerCase(chars[i]) != Character.toLowerCase(chars[j])) { - return false; - } - i++; - j--; - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1252.java b/src/main/java/com/fishercoder/solutions/_1252.java deleted file mode 100644 index 7e8f20d0af..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1252.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1252. Cells with Odd Values in a Matrix - * - * Given n and m which are the dimensions of a matrix initialized by zeros and given an array indices where indices[i] = [ri, ci]. For each pair of [ri, ci] you have to increment all cells in row ri and column ci by 1. - * Return the number of cells with odd values in the matrix after applying the increment to all indices. - * - * Example 1: - * 0, 0, 0 1, 2, 1 1, 3, 1 - * 0, 0, 0 0, 1, 0 1, 3 ,1 - * - * Input: n = 2, m = 3, indices = [[0,1],[1,1]] - * Output: 6 - * Explanation: Initial matrix = [[0,0,0],[0,0,0]]. - * After applying first increment it becomes [[1,2,1],[0,1,0]]. - * The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers. - * - * Example 2: - * 0, 0 0, 1 2, 2 - * 0, 0 1, 2 2, 2 - * - * Input: n = 2, m = 2, indices = [[1,1],[0,0]] - * Output: 0 - * Explanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix. - * - * - * Constraints: - * 1 <= n <= 50 - * 1 <= m <= 50 - * 1 <= indices.length <= 100 - * 0 <= indices[i][0] < n - * 0 <= indices[i][1] < m - * */ -public class _1252 { - public static class Solution1 { - /** - * Time: O(m*n + k) where k is the length of indices - * Space: O(m*n) - * */ - public int oddCells(int n, int m, int[][] indices) { - int[][] matrix = new int[n][m]; - for (int i = 0; i < indices.length; i++) { - addOneToRow(matrix, indices[i][0]); - addOneToColumn(matrix, indices[i][1]); - } - int oddNumberCount = 0; - for (int i = 0; i < matrix.length; i++) { - for (int j = 0; j < matrix[0].length; j++) { - if (matrix[i][j] % 2 != 0) { - oddNumberCount++; - } - } - } - return oddNumberCount; - } - - private void addOneToColumn(int[][] matrix, int columnIndex) { - for (int i = 0; i < matrix.length; i++) { - matrix[i][columnIndex] += 1; - } - } - - private void addOneToRow(int[][] matrix, int rowIndex) { - for (int j = 0; j < matrix[0].length; j++) { - matrix[rowIndex][j] += 1; - } - } - } - - public static class Solution2 { - /** - * Time: O(m*n + k) where k is the length of indices - * Space: O(m + n) - */ - public int oddCells(int n, int m, int[][] indices) { - boolean[] row = new boolean[n]; - boolean[] column = new boolean[m]; - for (int[] index : indices) { - row[index[0]] ^= true; - column[index[1]] ^= true; - } - int oddNumberCount = 0; - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - oddNumberCount += row[i] ^ column[j] ? 1 : 0; - } - } - return oddNumberCount; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1258.java b/src/main/java/com/fishercoder/solutions/_1258.java deleted file mode 100644 index b4e114a44a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1258.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * 1258. Synonymous Sentences - * - * Given a list of pairs of equivalent words synonyms and a sentence text, Return all possible synonymous sentences sorted lexicographically. - * - * Example 1: - * Input: - * synonyms = [["happy","joy"],["sad","sorrow"],["joy","cheerful"]], - * text = "I am happy today but was sad yesterday" - * Output: - * ["I am cheerful today but was sad yesterday", - * ​​​​​​​"I am cheerful today but was sorrow yesterday", - * "I am happy today but was sad yesterday", - * "I am happy today but was sorrow yesterday", - * "I am joy today but was sad yesterday", - * "I am joy today but was sorrow yesterday"] - * - * Constraints: - * 0 <= synonyms.length <= 10 - * synonyms[i].length == 2 - * synonyms[0] != synonyms[1] - * All words consist of at most 10 English letters only. - * text is a single space separated sentence of at most 10 words. - * */ -public class _1258 { - public static class Solution1 { - public List generateSentences(List> synonyms, String text) { - String[] words = text.split(" "); - Map> map = buildSynonymDict(words, synonyms); - Set result = new HashSet<>(); - result.add(text); - for (int i = 0; i < words.length; i++) { - List list = new ArrayList<>(); - for (String next : result) { - list.add(next); - list.addAll(findAllSynonymForThisWord(next, i, map)); - } - result.addAll(list); - } - List list = new ArrayList<>(); - list.addAll(result); - Collections.sort(list); - return list; - } - - private List findAllSynonymForThisWord(String sentence, int i, Map> map) { - String[] words = sentence.split(" "); - List list = new ArrayList<>(); - Set synonyms = map.get(words[i]); - for (String s : synonyms) { - words[i] = s; - list.add(formWord(words)); - } - return list; - } - - private String formWord(String[] words) { - StringBuilder sb = new StringBuilder(); - for (String word : words) { - sb.append(word); - sb.append(" "); - } - return sb.substring(0, sb.length() - 1); - } - - private Map> buildSynonymDict(String[] words, List> synonyms) { - Map> map = new HashMap<>(); - for (String key : words) { - if (!map.containsKey(key)) { - map.put(key, new HashSet<>()); - } - map.get(key).add(key); - } - for (List list : synonyms) { - for (String key : map.keySet()) { - if (map.get(key).contains(list.get(0))) { - map.get(key).add(list.get(1)); - } else if (map.get(key).contains(list.get(1))) { - map.get(key).add(list.get(0)); - } - } - } - return map; - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_126.java b/src/main/java/com/fishercoder/solutions/_126.java deleted file mode 100644 index 78427b71ec..0000000000 --- a/src/main/java/com/fishercoder/solutions/_126.java +++ /dev/null @@ -1,144 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; - -/** - * 126. Word Ladder II - - Given two words (beginWord and endWord), and a dictionary's word list, - find all shortest transformation sequence(s) from beginWord to endWord, such that: - - Only one letter can be changed at a time - Each transformed word must exist in the word list. Note that beginWord is not a transformed word. - - For example, - Given: - beginWord = "hit" - endWord = "cog" - wordList = ["hot","dot","dog","lot","log","cog"] - - Return - [ - ["hit","hot","dot","dog","cog"], - ["hit","hot","lot","log","cog"] - ] - - Note: - Return an empty list if there is no such transformation sequence. - All words have the same length. - All words contain only lowercase alphabetic characters. - You may assume no duplicates in the word list. - You may assume beginWord and endWord are non-empty and are not the same. - */ - -public class _126 { - - public static class Solution1 { - /** Reference: https://discuss.leetcode.com/topic/2857/share-two-similar-java-solution-that-accpted-by-oj */ - - Map> map; - List> results; - - public List> findLadders(String start, String end, List dict) { - results = new ArrayList<>(); - if (dict.size() == 0) { - return results; - } - - int min = Integer.MAX_VALUE; - - Queue queue = new ArrayDeque<>(); - queue.add(start); - - map = new HashMap<>(); - - Map ladder = new HashMap<>(); - for (String string : dict) { - ladder.put(string, Integer.MAX_VALUE); - } - ladder.put(start, 0); - - dict.add(end); - //BFS: Dijisktra search - while (!queue.isEmpty()) { - - String word = queue.poll(); - - int step = ladder.get(word) - + 1;//'step' indicates how many steps are needed to travel to one word. - - if (step > min) { - break; - } - - for (int i = 0; i < word.length(); i++) { - StringBuilder builder = new StringBuilder(word); - for (char ch = 'a'; ch <= 'z'; ch++) { - builder.setCharAt(i, ch); - String newWord = builder.toString(); - if (ladder.containsKey(newWord)) { - - if (step > ladder.get(newWord)) { - //Check if it is the shortest path to one word. - continue; - } else if (step < ladder.get(newWord)) { - queue.add(newWord); - ladder.put(newWord, step); - } else { - // It is a KEY line. If one word already appeared in one ladder, - // Do not insert the same word inside the queue twice. Otherwise it gets TLE. - } - if (map.containsKey(newWord)) { - //Build adjacent Graph - map.get(newWord).add(word); - } else { - List list = new LinkedList(); - list.add(word); - map.put(newWord, list); - //It is possible to write three lines in one: - //map.put(new_word,new LinkedList(Arrays.asList(new String[]{word}))); - //Which one is better? - } - - if (newWord.equals(end)) { - min = step; - } - } - //End if dict contains new_word - } - //End:Iteration from 'a' to 'z' - } - //End:Iteration from the first to the last - } - //End While - - //BackTracking - LinkedList result = new LinkedList<>(); - backTrace(end, start, result); - - return results; - } - - private void backTrace(String word, String start, List list) { - if (word.equals(start)) { - list.add(0, start); - results.add(new ArrayList<>(list)); - list.remove(0); - return; - } - list.add(0, word); - if (map.get(word) != null) { - for (String s : map.get(word)) { - backTrace(s, start, list); - } - } - list.remove(0); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1260.java b/src/main/java/com/fishercoder/solutions/_1260.java deleted file mode 100644 index 0b6735f249..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1260.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -/** - * 1260. Shift 2D Grid - * - * Given a 2D grid of size m x n and an integer k. You need to shift the grid k times. - * - * In one shift operation: - * Element at grid[i][j] becomes at grid[i][j + 1]. - * Element at grid[i][n - 1] becomes at grid[i + 1][0]. - * Element at grid[n - 1][n - 1] becomes at grid[0][0]. - * Return the 2D grid after applying shift operation k times. - * - * Example 1: - * Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1 - * Output: [[9,1,2],[3,4,5],[6,7,8]] - * - * Example 2: - * Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4 - * Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]] - * - * Example 3: - * Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9 - * Output: [[1,2,3],[4,5,6],[7,8,9]] - * - * Constraints: - * m == grid.length - * n == grid[i].length - * 1 <= m <= 50 - * 1 <= n <= 50 - * -1000 <= grid[i][j] <= 1000 - * 0 <= k <= 100 - * */ -public class _1260 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/shift-2d-grid/discuss/431102/JavaPython-3-simple-code-using-mod*/ - public List> shiftGrid(int[][] grid, int k) { - int m = grid.length; - int n = grid[0].length; - int totalNumbers = m * n; - int start = totalNumbers - k % totalNumbers; - LinkedList> result = new LinkedList<>(); - for (int i = start; i < totalNumbers + start; i++) { - int moveIndex = i % totalNumbers; - int moveRow = moveIndex / n; - int moveColumn = moveIndex % n; - if ((i - start) % n == 0) { - result.add(new ArrayList<>()); - } - result.peekLast().add(grid[moveRow][moveColumn]); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1261.java b/src/main/java/com/fishercoder/solutions/_1261.java deleted file mode 100644 index ba1a6e32c0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1261.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 1261. Find Elements in a Contaminated Binary Tree - * - * Given a binary tree with the following rules: - * root.val == 0 - * If treeNode.val == x and treeNode.left != null, then treeNode.left.val == 2 * x + 1 - * If treeNode.val == x and treeNode.right != null, then treeNode.right.val == 2 * x + 2 - * Now the binary tree is contaminated, which means all treeNode.val have been changed to -1. - * - * You need to first recover the binary tree and then implement the FindElements class: - * FindElements(TreeNode* root) Initializes the object with a contamined binary tree, you need to recover it first. - * bool find(int target) Return if the target value exists in the recovered binary tree. - * - * Example 1: - * Input - * ["FindElements","find","find"] - * [[[-1,null,-1]],[1],[2]] - * Output - * [null,false,true] - * Explanation - * FindElements findElements = new FindElements([-1,null,-1]); - * findElements.find(1); // return False - * findElements.find(2); // return True - * - * Example 2: - * Input - * ["FindElements","find","find","find"] - * [[[-1,-1,-1,-1,-1]],[1],[3],[5]] - * Output - * [null,true,true,false] - * Explanation - * FindElements findElements = new FindElements([-1,-1,-1,-1,-1]); - * findElements.find(1); // return True - * findElements.find(3); // return True - * findElements.find(5); // return False - * - * Example 3: - * Input - * ["FindElements","find","find","find","find"] - * [[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]] - * Output - * [null,true,false,false,true] - * Explanation - * FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]); - * findElements.find(2); // return True - * findElements.find(3); // return False - * findElements.find(4); // return False - * findElements.find(5); // return True - * - * Constraints: - * TreeNode.val == -1 - * The height of the binary tree is less than or equal to 20 - * The total number of nodes is between [1, 10^4] - * Total calls of find() is between [1, 10^4] - * 0 <= target <= 10^6 - * */ -public class _1261 { - public static class Solution1 { - class FindElements { - - TreeNode recoveredRoot; - - public FindElements(TreeNode root) { - recoveredRoot = new TreeNode(0); - recoveredRoot = recoverTree(root, recoveredRoot); - } - - private TreeNode recoverTree(TreeNode root, TreeNode recoveredRoot) { - if (root == null) { - return recoveredRoot; - } - if (root.left != null) { - recoveredRoot.left = new TreeNode(recoveredRoot.val * 2 + 1); - } - if (root.right != null) { - recoveredRoot.right = new TreeNode(recoveredRoot.val * 2 + 2); - } - recoverTree(root.left, recoveredRoot.left); - recoverTree(root.right, recoveredRoot.right); - return recoveredRoot; - } - - public boolean find(int target) { - return find(recoveredRoot, target); - } - - private boolean find(TreeNode root, int target) { - if (root == null) { - return false; - } else if (root.val == target) { - return true; - } - return find(root.left, target) || find(root.right, target); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1265.java b/src/main/java/com/fishercoder/solutions/_1265.java deleted file mode 100644 index 789f24e8ea..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1265.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 1265. Print Immutable Linked List in Reverse - * - * You are given an immutable linked list, print out all values of each node in reverse with the help of the following interface: - * ImmutableListNode: An interface of immutable linked list, you are given the head of the list. - * You need to use the following functions to access the linked list (you can't access the ImmutableListNode directly): - * ImmutableListNode.printValue(): Print value of the current node. - * ImmutableListNode.getNext(): Return the next node. - * The input is only given to initialize the linked list internally. - * You must solve this problem without modifying the linked list. In other words, you must operate the linked list using only the mentioned APIs. - * - * Follow up: - * Could you solve this problem in: - * Constant space complexity? - * Linear time complexity and less than linear space complexity? - * - * Example 1: - * Input: head = [1,2,3,4] - * Output: [4,3,2,1] - * - * Example 2: - * Input: head = [0,-4,-1,3,-5] - * Output: [-5,3,-1,-4,0] - * - * Example 3: - * Input: head = [-2,0,6,4,4,-6] - * Output: [-6,4,4,6,0,-2] - * - * Constraints: - * The length of the linked list is between [1, 1000]. - * The value of each node in the linked list is between [-1000, 1000]. - * */ -public class _1265 { - - interface ImmutableListNode { - ImmutableListNode getNext(); - - void printValue(); - } - - public static class Solution1 { - public void printLinkedListInReverse(ImmutableListNode head) { - Stack stack = new Stack(); - while (head != null) { - stack.add(head); - head = head.getNext(); - } - while (!stack.isEmpty()) { - head = stack.pop(); - head.printValue(); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1266.java b/src/main/java/com/fishercoder/solutions/_1266.java deleted file mode 100644 index 5338a4111d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1266.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1266. Minimum Time Visiting All Points - * - * On a plane there are n points with integer coordinates points[i] = [xi, yi]. - * Your task is to find the minimum time in seconds to visit all points. - * - * You can move according to the next rules: - * In one second always you can either move vertically, horizontally by one unit or diagonally - * (it means to move one unit vertically and one unit horizontally in one second). - * You have to visit the points in the same order as they appear in the array. - * - * Example 1: - * Input: points = [[1,1],[3,4],[-1,0]] - * Output: 7 - * Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0] - * Time from [1,1] to [3,4] = 3 seconds - * Time from [3,4] to [-1,0] = 4 seconds - * Total time = 7 seconds - * - * Example 2: - * Input: points = [[3,2],[-2,2]] - * Output: 5 - * - * Constraints: - * points.length == n - * 1 <= n <= 100 - * points[i].length == 2 - * -1000 <= points[i][0], points[i][1] <= 1000 - * */ -public class _1266 { - public static class Solution1 { - /** - * Time: O(n) - * Space: O(1) - * - * credit: https://leetcode.com/problems/minimum-time-visiting-all-points/discuss/436142/Sum-of-Chebyshev-distance-between-two-consecutive-points - * */ - public int minTimeToVisitAllPoints(int[][] points) { - int minTime = 0; - for (int i = 0; i < points.length - 1; i++) { - minTime += chebyshevDistance(points[i], points[i + 1]); - } - return minTime; - } - - private int chebyshevDistance(int[] pointA, int[] pointB) { - return Math.max(Math.abs(pointA[0] - pointB[0]), Math.abs(pointA[1] - pointB[1])); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1267.java b/src/main/java/com/fishercoder/solutions/_1267.java deleted file mode 100644 index e2e0679a8d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1267.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.utils.CommonUtils; - -/** - * 1267. Count Servers that Communicate - * - * You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. - * Two servers are said to communicate if they are on the same row or on the same column. - * - * Return the number of servers that communicate with any other server. - * - * Example 1: - * Input: grid = [[1,0],[0,1]] - * Output: 0 - * Explanation: No servers can communicate with others. - * - * Example 2: - * Input: grid = [[1,0],[1,1]] - * Output: 3 - * Explanation: All three servers can communicate with at least one other server. - * - * Example 3: - * Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]] - * Output: 4 - * Explanation: The two servers in the first row can communicate with each other. - * The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server. - * - * Constraints: - * m == grid.length - * n == grid[i].length - * 1 <= m <= 250 - * 1 <= n <= 250 - * grid[i][j] == 0 or 1 - * */ -public class _1267 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/count-servers-that-communicate/discuss/436188/Java-or-Clean-And-Simple-or-Beats-100*/ - public int countServers(int[][] grid) { - int m = grid.length; - int n = grid[0].length; - int[] rowCount = new int[m]; - int[] columnCount = new int[n]; - int total = 0; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == 1) { - rowCount[i]++; - columnCount[j]++; - total++; - } - } - } - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == 1 && rowCount[i] == 1 && columnCount[j] == 1) { - total--; - } - } - } - return total; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_127.java b/src/main/java/com/fishercoder/solutions/_127.java deleted file mode 100644 index 5c2f4ed775..0000000000 --- a/src/main/java/com/fishercoder/solutions/_127.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 127. Word Ladder - * - * Given two words (beginWord and endWord), and a dictionary's word list, - * find the length of shortest transformation sequence from beginWord to endWord, such that: - * Only one letter can be changed at a time. - * Each transformed word must exist in the word list. Note that beginWord is not a transformed word. - - For example, - - Given: - beginWord = "hit" - endWord = "cog" - wordList = ["hot","dot","dog","lot","log","cog"] - - As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", return its length 5. - - Note: - - Return 0 if there is no such transformation sequence. - All words have the same length. - All words contain only lowercase alphabetic characters. - You may assume no duplicates in the word list. - You may assume beginWord and endWord are non-empty and are not the same. - */ - -public class _127 { - public static class Solution1 { - - public int ladderLength(String beginWord, String endWord, List wordList) { - Set beginSet = new HashSet<>(); - Set endSet = new HashSet<>(); - Set visited = new HashSet<>(); - Set dict = new HashSet<>(wordList); - int len = 1; - - beginSet.add(beginWord); - - if (dict.contains(endWord)) { - endSet.add(endWord); - } - - while (!beginSet.isEmpty() && !endSet.isEmpty()) { - Set nextBeginSet = new HashSet<>(); - for (String word : beginSet) { - char[] chars = word.toCharArray(); - for (int i = 0; i < chars.length; i++) { - for (char c = 'a'; c <= 'z'; c++) { - char old = chars[i]; - chars[i] = c; - String newWord = new String(chars); - if (endSet.contains(newWord)) { - return len + 1; - } - - if (!visited.contains(newWord) && dict.contains(newWord)) { - visited.add(newWord); - nextBeginSet.add(newWord); - } - chars[i] = old; - } - } - } - - beginSet = nextBeginSet; - len++; - } - return 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1271.java b/src/main/java/com/fishercoder/solutions/_1271.java deleted file mode 100644 index 294172d232..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1271.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -/** - * 1271. Hexspeak - * - * A decimal number can be converted to its Hexspeak representation by first converting it to an uppercase hexadecimal string, - * then replacing all occurrences of the digit 0 with the letter O, and the digit 1 with the letter I. - * Such a representation is valid if and only if it consists only of the letters in the set {"A", "B", "C", "D", "E", "F", "I", "O"}. - * Given a string num representing a decimal integer N, return the Hexspeak representation of N if it is valid, otherwise return "ERROR". - * - * Example 1: - * Input: num = "257" - * Output: "IOI" - * Explanation: 257 is 101 in hexadecimal. - * - * Example 2: - * Input: num = "3" - * Output: "ERROR" - * - * Constraints: - * 1 <= N <= 10^12 - * There are no leading zeros in the given string. - * All answers must be in uppercase letters. - * */ -public class _1271 { - public static class Solution1 { - public String toHexspeak(String num) { - long numInt = Long.parseLong(num); - String hexString = Long.toHexString(numInt); - StringBuilder sb = new StringBuilder(); - Set set = new HashSet<>(Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', '1', '0', 'a', 'b', 'c', 'd', 'e', 'f')); - for (char c : hexString.toCharArray()) { - if (!set.contains(c)) { - return "ERROR"; - } else if (c == '1') { - sb.append("I"); - } else if (c == '0') { - sb.append("O"); - } else { - sb.append(Character.toUpperCase(c)); - } - } - return sb.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1273.java b/src/main/java/com/fishercoder/solutions/_1273.java deleted file mode 100644 index f20c1bb03a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1273.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1273. Delete Tree Nodes - * - * A tree rooted at node 0 is given as follows: - * The number of nodes is nodes; - * The value of the i-th node is value[i]; - * The parent of the i-th node is parent[i]. - * Remove every subtree whose sum of values of nodes is zero. - * After doing so, return the number of nodes remaining in the tree. - * - * Example 1: - * 0 (1) - * / \ - * (-2) 1 2 (4) - * / / | \ - * (0)3 (-2)4 (-1)5 6(-1) - * - * Input: nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-1] - * Output: 2 - * - * Constraints: - * 1 <= nodes <= 10^4 - * -10^5 <= value[i] <= 10^5 - * parent.length == nodes - * parent[0] == -1 which indicates that 0 is the root. - * */ -public class _1273 { - public static class Solution1 { - public int deleteTreeNodes(int nodes, int[] parent, int[] value) { - int len = parent.length; - for (int i = len - 1; i > 0; ) { - int sum = 0; - int parentIndex = parent[i]; - while (i > 0 && parent[i] == parentIndex) { - sum += value[i--]; - } - //we'll reset the value to be the newly computed sum of this node and all of its children - value[parentIndex] = value[parentIndex] + sum; - } - //then we'll reset this node's children to be zero if this node's computed sum is zero - for (int i = 0; i < value.length; i++) { - if (value[i] == 0) { - for (int j = 0; j < parent.length; j++) { - if (parent[j] == i) { - value[j] = 0; - } - } - } - } - return (int) Arrays.stream(value).filter(i -> i != 0).count(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1275.java b/src/main/java/com/fishercoder/solutions/_1275.java deleted file mode 100644 index 4ee162c707..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1275.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1275. Find Winner on a Tic Tac Toe Game - * - * Tic-tac-toe is played by two players A and B on a 3 x 3 grid. - * - * Here are the rules of Tic-Tac-Toe: - * Players take turns placing characters into empty squares (" "). - * The first player A always places "X" characters, while the second player B always places "O" characters. - * "X" and "O" characters are always placed into empty squares, never on filled ones. - * The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal. - * The game also ends if all squares are non-empty. - * No more moves can be played if the game is over. - * Given an array moves where each element is another array of size 2 corresponding to the row and column of - * the grid where they mark their respective character in the order in which A and B play. - * - * Return the winner of the game if it exists (A or B), in case the game ends in a draw return "Draw", - * if there are still movements to play return "Pending". - * - * You can assume that moves is valid (It follows the rules of Tic-Tac-Toe), the grid is initially empty and A will play first. - * - * Example 1: - * Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] - * Output: "A" - * Explanation: "A" wins, he always plays first. - * "X " "X " "X " "X " "X " - * " " -> " " -> " X " -> " X " -> " X " - * " " "O " "O " "OO " "OOX" - * - * Example 2: - * Input: moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] - * Output: "B" - * Explanation: "B" wins. - * "X " "X " "XX " "XXO" "XXO" "XXO" - * " " -> " O " -> " O " -> " O " -> "XO " -> "XO " - * " " " " " " " " " " "O " - * - * Example 3: - * Input: moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] - * Output: "Draw" - * Explanation: The game ends in a draw since there are no moves to make. - * "XXO" - * "OOX" - * "XOX" - * - * Example 4: - * Input: moves = [[0,0],[1,1]] - * Output: "Pending" - * Explanation: The game has not finished yet. - * "X " - * " O " - * " " - * - * Constraints: - * 1 <= moves.length <= 9 - * moves[i].length == 2 - * 0 <= moves[i][j] <= 2 - * There are no repeated elements on moves. - * moves follow the rules of tic tac toe. - * */ -public class _1275 { - public static class Solution1 { - public String tictactoe(int[][] moves) { - String[][] board = new String[3][3]; - for (int i = 0; i < moves.length; i++) { - if (i % 2 == 0) { - board[moves[i][0]][moves[i][1]] = "X"; - } else { - board[moves[i][0]][moves[i][1]] = "O"; - } - if (i > 3) { - if (!wins(board).equals("")) { - return wins(board); - } - } - } - return moves.length == 9 ? "Draw" : "Pending"; - } - - private String wins(String[][] board) { - //check rows - for (int i = 0; i < 3; i++) { - if (board[i][0] == null) { - break; - } - String str = board[i][0]; - if (str.equals(board[i][1]) && str.equals(board[i][2])) { - return getWinner(str); - } - } - - //check columns - for (int j = 0; j < 3; j++) { - if (board[0][j] == null) { - break; - } - String str = board[0][j]; - if (str.equals(board[1][j]) && str.equals(board[2][j])) { - return getWinner(str); - } - } - - //check diagonals - if (board[1][1] == null) { - return ""; - } - String str = board[1][1]; - if (str.equals(board[0][0]) && str.equals(board[2][2]) || (str.equals(board[0][2]) && str.equals(board[2][0]))) { - return getWinner(str); - } - return ""; - } - - private String getWinner(String str) { - if (str.equals("X")) { - return "A"; - } else { - return "B"; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1277.java b/src/main/java/com/fishercoder/solutions/_1277.java deleted file mode 100644 index a1249a797b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1277.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1277. Count Square Submatrices with All Ones - * - * Given a m * n matrix of ones and zeros, return how many square submatrices have all ones. - * - * Example 1: - * Input: matrix = - * [ - * [0,1,1,1], - * [1,1,1,1], - * [0,1,1,1] - * ] - * Output: 15 - * Explanation: - * There are 10 squares of side 1. - * There are 4 squares of side 2. - * There is 1 square of side 3. - * Total number of squares = 10 + 4 + 1 = 15. - * - * Example 2: - * Input: matrix = - * [ - * [1,0,1], - * [1,1,0], - * [1,1,0] - * ] - * Output: 7 - * Explanation: - * There are 6 squares of side 1. - * There is 1 square of side 2. - * Total number of squares = 6 + 1 = 7. - * - * Constraints: - * 1 <= arr.length <= 300 - * 1 <= arr[0].length <= 300 - * 0 <= arr[i][j] <= 1 - * */ -public class _1277 { - public static class Solution1 { - /** - * In-place solution. - * - * credit: https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441306/Python-DP-solution - * */ - public int countSquares(int[][] matrix) { - int m = matrix.length; - int n = matrix[0].length; - int count = 0; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (matrix[i][j] > 0 && i > 0 && j > 0) { - matrix[i][j] = Math.min(matrix[i - 1][j - 1], Math.min(matrix[i - 1][j], matrix[i][j - 1])) + 1; - } - count += matrix[i][j]; - } - } - return count; - } - } - - public static class Solution2 { - /** - * Use m*n extra space solution. - * - * credit: https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441312/Java-Simple-DP-solution - */ - public int countSquares(int[][] matrix) { - int m = matrix.length; - int n = matrix[0].length; - int[][] dp = new int[m][n]; - for (int i = 0; i < m; i++) { - if (matrix[i][0] == 1) { - dp[i][0] = 1; - } - } - for (int j = 0; j < n; j++) { - if (matrix[0][j] == 1) { - dp[0][j] = 1; - } - } - for (int i = 1; i < m; i++) { - for (int j = 1; j < n; j++) { - if (matrix[i][j] == 1) { - dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1; - } - } - } - int count = 0; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - count += dp[i][j]; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_128.java b/src/main/java/com/fishercoder/solutions/_128.java deleted file mode 100644 index f23cff3b54..0000000000 --- a/src/main/java/com/fishercoder/solutions/_128.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 128. Longest Consecutive Sequence - - Given an unsorted array of integers, find the length of the longest consecutive elements sequence. - - For example, - Given [100, 4, 200, 1, 3, 2], - The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. - - Your algorithm should run in O(n) complexity. - */ -public class _128 { - public static class Solution1 { - public int longestConsecutive(int[] nums) { - Map map = new HashMap(); - // - UnionFind uf = new UnionFind(nums); - for (int i = 0; i < nums.length; i++) { - if (map.containsKey(nums[i])) { - continue; - } - map.put(nums[i], i); - if (map.containsKey(nums[i] - 1)) { - uf.union(i, map.get(nums[i] - 1)); - //note: we want to union this index and nums[i]-1's root index which we can get from the map - } - if (map.containsKey(nums[i] + 1)) { - uf.union(i, map.get(nums[i] + 1)); - } - } - return uf.maxUnion(); - } - - class UnionFind { - int[] ids; - - public UnionFind(int[] nums) { - ids = new int[nums.length]; - for (int i = 0; i < nums.length; i++) { - ids[i] = i; - } - } - - public void union(int i, int j) { - int x = find(ids, i); - int y = find(ids, j); - ids[x] = y; - } - - public int find(int[] ids, int i) { - while (i != ids[i]) { - ids[i] = ids[ids[i]]; - i = ids[i]; - } - return i; - } - - public boolean connected(int i, int j) { - return find(ids, i) == find(ids, j); - } - - public int maxUnion() { - //this is O(n) - int max = 0; - int[] count = new int[ids.length]; - for (int i = 0; i < ids.length; i++) { - count[find(ids, i)]++; - max = max < count[find(ids, i)] ? count[find(ids, i)] : max; - } - return max; - } - } - } - - public static class Solution2 { - //inspired by this solution: https://discuss.leetcode.com/topic/25493/simple-fast-java-solution-using-set - public int longestConsecutive(int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - - Set set = new HashSet(); - for (int i : nums) { - set.add(i); - } - int max = 1; - - for (int num : nums) { - if (set.remove(num)) { - int val = num; - int count = 1; - while (set.remove(val - 1)) { - val--;//we find all numbers that are smaller than num and remove them from the set - } - count += num - val; - - val = num; - while (set.remove(val + 1)) { - val++;//then we find all numbers that are bigger than num and also remove them from the set - } - count += val - num; - - max = Math.max(max, count); - } - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1281.java b/src/main/java/com/fishercoder/solutions/_1281.java deleted file mode 100644 index 0e84698d20..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1281.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1281. Subtract the Product and Sum of Digits of an Integer - * - * Given an integer number n, return the difference between the product of its digits and the sum of its digits. - * - * Example 1: - * Input: n = 234 - * Output: 15 - * Explanation: - * Product of digits = 2 * 3 * 4 = 24 - * Sum of digits = 2 + 3 + 4 = 9 - * Result = 24 - 9 = 15 - * - * Example 2: - * Input: n = 4421 - * Output: 21 - * Explanation: - * Product of digits = 4 * 4 * 2 * 1 = 32 - * Sum of digits = 4 + 4 + 2 + 1 = 11 - * Result = 32 - 11 = 21 - * - * Constraints: - * 1 <= n <= 10^5 - * */ -public class _1281 { - public static class Solution1 { - public int subtractProductAndSum(int n) { - int sum = 0; - int product = 1; - while (n > 0) { - int digit = n % 10; - n /= 10; - sum += digit; - product *= digit; - } - return product - sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1282.java b/src/main/java/com/fishercoder/solutions/_1282.java deleted file mode 100644 index 5ef453de1b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1282.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 1282. Group the People Given the Group Size They Belong To - * - * There are n people whose IDs go from 0 to n - 1 and each person belongs exactly to one group. - * Given the array groupSizes of length n telling the group size each person belongs to, return the groups there are and the people's IDs each group includes. - * You can return any solution in any order and the same applies for IDs. - * Also, it is guaranteed that there exists at least one solution. - * - * Example 1: - * Input: groupSizes = [3,3,3,3,3,1,3] - * Output: [[5],[0,1,2],[3,4,6]] - * Explanation: - * Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]]. - * - * Example 2: - * Input: groupSizes = [2,1,3,3,3,2] - * Output: [[1],[0,5],[2,3,4]] - * - * Constraints: - * groupSizes.length == n - * 1 <= n <= 500 - * 1 <= groupSizes[i] <= n - * */ -public class _1282 { - public static class Solution1 { - public List> groupThePeople(int[] groupSizes) { - Map> map = new HashMap<>(); - for (int i = 0; i < groupSizes.length; i++) { - List list = map.getOrDefault(groupSizes[i], new ArrayList<>()); - list.add(i); - map.put(groupSizes[i], list); - } - List> result = new ArrayList<>(); - for (int key : map.keySet()) { - List list = map.get(key); - int i = 0; - do { - result.add(list.subList(i, i + key)); - i += key; - } while (i + key <= list.size()); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1286.java b/src/main/java/com/fishercoder/solutions/_1286.java deleted file mode 100644 index c2dee2ad51..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1286.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1286. Iterator for Combination - * - * Design an Iterator class, which has: - * A constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments. - * A function next() that returns the next combination of length combinationLength in lexicographical order. - * A function hasNext() that returns True if and only if there exists a next combination. - * - * Example: - * CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the iterator. - * iterator.next(); // returns "ab" - * iterator.hasNext(); // returns true - * iterator.next(); // returns "ac" - * iterator.hasNext(); // returns true - * iterator.next(); // returns "bc" - * iterator.hasNext(); // returns false - * - * Constraints: - * 1 <= combinationLength <= characters.length <= 15 - * There will be at most 10^4 function calls per test. - * It's guaranteed that all calls of the function next are valid. - * */ -public class _1286 { - public static class Solution1 { - public static class CombinationIterator { - - List list; - int index; - int combinationLength; - boolean[] visited; - - public CombinationIterator(String characters, int combinationLength) { - this.index = 0; - this.list = new ArrayList<>(); - this.combinationLength = combinationLength; - this.visited = new boolean[characters.length()]; - buildAllCombinations(characters, 0, new StringBuilder(), visited); - } - - private void buildAllCombinations(String characters, int start, StringBuilder sb, boolean[] visited) { - if (sb.length() == combinationLength) { - list.add(sb.toString()); - return; - } else { - for (int i = start; i < characters.length(); ) { - if (!visited[i]) { - sb.append(characters.charAt(i)); - visited[i] = true; - buildAllCombinations(characters, i++, sb, visited); - visited[i - 1] = false; - sb.setLength(sb.length() - 1); - } else { - i++; - } - } - } - } - - public String next() { - return list.get(index++); - } - - public boolean hasNext() { - return index < list.size(); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1287.java b/src/main/java/com/fishercoder/solutions/_1287.java deleted file mode 100644 index 83179abd5c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1287.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1287. Element Appearing More Than 25% In Sorted Array - * - * Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. - * Return that integer. - * - * Example 1: - * Input: arr = [1,2,2,6,6,6,6,7,10] - * Output: 6 - * - * Constraints: - * - * 1 <= arr.length <= 10^4 - * 0 <= arr[i] <= 10^5 - * */ -public class _1287 { - public static class Solution1 { - public int findSpecialInteger(int[] arr) { - int quarter = arr.length / 4; - for (int i = 0; i < arr.length - quarter; i++) { - if (arr[i] == arr[i + quarter]) { - return arr[i]; - } - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1289.java b/src/main/java/com/fishercoder/solutions/_1289.java deleted file mode 100644 index d87f4deaa5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1289.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1289. Minimum Falling Path Sum II - * - * Given a square grid of integers arr, a falling path with non-zero shifts is a - * choice of exactly one element from each row of arr, such that no two elements chosen in adjacent rows are in the same column. - * Return the minimum sum of a falling path with non-zero shifts. - * - * Example 1: - * Input: arr = [[1,2,3],[4,5,6],[7,8,9]] - * Output: 13 - * Explanation: - * The possible falling paths are: - * [1,5,9], [1,5,7], [1,6,7], [1,6,8], - * [2,4,8], [2,4,9], [2,6,7], [2,6,8], - * [3,4,8], [3,4,9], [3,5,7], [3,5,9] - * The falling path with the smallest sum is [1,5,7], so the answer is 13. - * - * Constraints: - * 1 <= arr.length == arr[i].length <= 200 - * -99 <= arr[i][j] <= 99 - * */ -public class _1289 { - public static class Solution1 { - public int minFallingPathSum(int[][] arr) { - int size = arr.length; - int[][] dp = new int[size][size]; - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - if (i == 0) { - dp[i][j] = arr[i][j]; - } else { - int previousRowMin = Integer.MAX_VALUE; - for (int k = 0; k < size; k++) { - if (k != j) { - previousRowMin = Math.min(dp[i - 1][k], previousRowMin); - } - } - dp[i][j] = arr[i][j] + previousRowMin; - } - } - } - int result = dp[size - 1][size - 1]; - for (int i = 0; i < size; i++) { - result = Math.min(result, dp[size - 1][i]); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_129.java b/src/main/java/com/fishercoder/solutions/_129.java deleted file mode 100644 index f8cbae5e28..0000000000 --- a/src/main/java/com/fishercoder/solutions/_129.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 129. Sum Root to Leaf Numbers - * - * Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. - - An example is the root-to-leaf path 1->2->3 which represents the number 123. - - Find the total sum of all root-to-leaf numbers. - - For example, - - 1 - / \ - 2 3 - The root-to-leaf path 1->2 represents the number 12. - The root-to-leaf path 1->3 represents the number 13. - - Return the sum = 12 + 13 = 25. - - */ -public class _129 { - public static class Solution1 { - public int sumNumbers(TreeNode root) { - if (root == null) { - return 0; - } - List allNumbers = new ArrayList(); - dfs(root, new StringBuilder(), allNumbers); - int sum = 0; - for (int i : allNumbers) { - sum += i; - } - return sum; - } - - private void dfs(TreeNode root, StringBuilder sb, List allNumbers) { - sb.append(root.val); - if (root.left != null) { - dfs(root.left, sb, allNumbers); - } - if (root.right != null) { - dfs(root.right, sb, allNumbers); - } - if (root.left == null && root.right == null) { - allNumbers.add(Integer.parseInt(sb.toString())); - } - sb.deleteCharAt(sb.length() - 1); - } - } - - public static class Solution2 { - public int sumNumbers(TreeNode root) { - return dfs(root, 0); - } - - private int dfs(TreeNode root, int sum) { - if (root == null) { - return 0; - } - if (root.left == null && root.right == null) { - return sum * 10 + root.val; - } - return dfs(root.left, sum * 10 + root.val) + dfs(root.right, sum * 10 + root.val); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_1290.java b/src/main/java/com/fishercoder/solutions/_1290.java deleted file mode 100644 index 63db8ef804..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1290.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 1290. Convert Binary Number in a Linked List to Integer - * - * Given head which is a reference node to a singly-linked list. - * The value of each node in the linked list is either 0 or 1. - * The linked list holds the binary representation of a number. - * - * Return the decimal value of the number in the linked list. - * - * Example 1: - * Input: head = [1,0,1] - * Output: 5 - * Explanation: (101) in base 2 = (5) in base 10 - * - * Example 2: - * Input: head = [0] - * Output: 0 - * - * Example 3: - * Input: head = [1] - * Output: 1 - * - * Example 4: - * Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0] - * Output: 18880 - * - * Example 5: - * Input: head = [0,0] - * Output: 0 - * - * Constraints: - * - * The Linked List is not empty. - * Number of nodes will not exceed 30. - * Each node's value is either 0 or 1. - * */ -public class _1290 { - public static class Solution1 { - public int getDecimalValue(ListNode head) { - StringBuilder sb = new StringBuilder(); - while (head != null) { - sb.append(head.val); - head = head.next; - } - return Integer.parseInt(sb.toString(), 2); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1291.java b/src/main/java/com/fishercoder/solutions/_1291.java deleted file mode 100644 index f00c828c6f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1291.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.List; - -/** - * 1291. Sequential Digits - * - * An integer has sequential digits if and only if each digit in the number is one more than the previous digit. - * Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits. - * - * Example 1: - * Input: low = 100, high = 300 - * Output: [123,234] - * - * Example 2: - * Input: low = 1000, high = 13000 - * Output: [1234,2345,3456,4567,5678,6789,12345] - * - * Constraints: - * 10 <= low <= high <= 10^9 - * */ -public class _1291 { - public static class Solution1 { - public List sequentialDigits(int low, int high) { - //TODO: implement it - return null; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1295.java b/src/main/java/com/fishercoder/solutions/_1295.java deleted file mode 100644 index ee39bd638c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1295.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1295. Find Numbers with Even Number of Digits - * - * Given an array nums of integers, return how many of them contain an even number of digits. - * - * Example 1: - * Input: nums = [12,345,2,6,7896] - * Output: 2 - * Explanation: - * 12 contains 2 digits (even number of digits). - * 345 contains 3 digits (odd number of digits). - * 2 contains 1 digit (odd number of digits). - * 6 contains 1 digit (odd number of digits). - * 7896 contains 4 digits (even number of digits). - * Therefore only 12 and 7896 contain an even number of digits. - * - * Example 2: - * Input: nums = [555,901,482,1771] - * Output: 1 - * Explanation: - * Only 1771 contains an even number of digits. - * - * Constraints: - * 1 <= nums.length <= 500 - * 1 <= nums[i] <= 10^5 - * */ -public class _1295 { - public static class Solution1 { - public int findNumbers(int[] nums) { - int count = 0; - for (int num : nums) { - if (String.valueOf(num).length() % 2 == 0) { - count++; - } - } - return count; - } - } - - public static class Solution2 { - public int findNumbers(int[] nums) { - return (int) Arrays.stream(nums).filter(num -> String.valueOf(num).length() % 2 == 0).count(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1296.java b/src/main/java/com/fishercoder/solutions/_1296.java deleted file mode 100644 index a4bd066955..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1296.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.TreeMap; - -/** - * 1296. Divide Array in Sets of K Consecutive Numbers - * - * Given an array of integers nums and a positive integer k, - * find whether it's possible to divide this array into sets of k consecutive numbers - * Return True if its possible otherwise return False. - * - * Example 1: - * Input: nums = [1,2,3,3,4,4,5,6], k = 4 - * Output: true - * Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. - * - * Example 2: - * Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 - * Output: true - * Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. - * - * Example 3: - * Input: nums = [3,3,2,2,1,1], k = 3 - * Output: true - * - * Example 4: - * Input: nums = [1,2,3,4], k = 3 - * Output: false - * Explanation: Each array should be divided in subarrays of size 3. - * - * Constraints: - * 1 <= nums.length <= 10^5 - * 1 <= nums[i] <= 10^9 - * 1 <= k <= nums.length - * */ -public class _1296 { - public static class Solution1 { - public boolean isPossibleDivide(int[] nums, int k) { - if (nums.length % k != 0) { - return false; - } - TreeMap treeMap = new TreeMap(); - int min = nums[0]; - for (int num : nums) { - treeMap.put(num, treeMap.getOrDefault(num, 0) + 1); - min = Math.min(min, num); - } - while (!treeMap.isEmpty()) { - if (!isConsecutiveK(treeMap, min, k)) { - return false; - } - min = findNextMin(treeMap); - if (min == Integer.MIN_VALUE) { - break; - } - } - return true; - } - - private int findNextMin(TreeMap treeMap) { - return treeMap.isEmpty() ? Integer.MIN_VALUE : treeMap.entrySet().iterator().next().getKey(); - } - - private boolean isConsecutiveK(TreeMap treeMap, int min, int k) { - int count = treeMap.get(min); - treeMap.remove(min); - for (int i = 1; i < k; i++) { - int key = min + i; - if (!treeMap.containsKey(key) || treeMap.get(key) < count) { - return false; - } else if (treeMap.get(key) > count) { - treeMap.put(key, treeMap.get(key) - count); - } else { - treeMap.remove(key); - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1297.java b/src/main/java/com/fishercoder/solutions/_1297.java deleted file mode 100644 index 17731ffbd4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1297.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 1297. Maximum Number of Occurrences of a Substring - * - * Given a string s, return the maximum number of ocurrences of any substring under the following rules: - * - * The number of unique characters in the substring must be less than or equal to maxLetters. - * The substring size must be between minSize and maxSize inclusive. - * - * Example 1: - * Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4 - * Output: 2 - * Explanation: Substring "aab" has 2 ocurrences in the original string. - * It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize). - * - * Example 2: - * Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3 - * Output: 2 - * Explanation: Substring "aaa" occur 2 times in the string. It can overlap. - * - * Example 3: - * Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3 - * Output: 3 - * - * Example 4: - * Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3 - * Output: 0 - * - * Constraints: - * 1 <= s.length <= 10^5 - * 1 <= maxLetters <= 26 - * 1 <= minSize <= maxSize <= min(26, s.length) - * s only contains lowercase English letters. - * */ -public class _1297 { - public static class Solution1 { - public int maxFreq(String s, int maxLetters, int minSize, int maxSize) { - int maxFreq = 0; - for (int size = minSize; size <= maxSize; size++) { - maxFreq = Math.max(maxFreq, getMaxFreqWithThisSize(s, maxLetters, size)); - } - return maxFreq; - } - - private int getMaxFreqWithThisSize(String str, int maxLetters, int size) { - Map map = new HashMap<>(); - for (int i = 0; i <= str.length() - size; i++) { - String substring = str.substring(i, i + size); - Set set = new HashSet<>(); - for (int j = 0; j < substring.length(); j++) { - set.add(substring.charAt(j)); - if (set.size() > maxLetters) { - break; - } - } - if (set.size() <= maxLetters) { - map.put(substring, map.getOrDefault(substring, 0) + 1); - } - } - int max = 0; - for (String key : map.keySet()) { - max = Math.max(max, map.get(key)); - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1299.java b/src/main/java/com/fishercoder/solutions/_1299.java deleted file mode 100644 index 7c678479dc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1299.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.PriorityQueue; - -/** - * 1299. Replace Elements with Greatest Element on Right Side - * - * Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1. - * After doing so, return the array. - * - * Example 1: - * Input: arr = [17,18,5,4,6,1] - * Output: [18,6,6,6,1,-1] - * */ -public class _1299 { - public static class Solution1 { - public int[] replaceElements(int[] arr) { - PriorityQueue heap = new PriorityQueue<>((a, b) -> b - a); - for (int num : arr) { - heap.offer(num); - } - int[] result = new int[arr.length]; - for (int i = 0; i < arr.length - 1; i++) { - heap.remove(arr[i]); - result[i] = heap.peek(); - } - result[arr.length - 1] = -1; - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_13.java b/src/main/java/com/fishercoder/solutions/_13.java deleted file mode 100644 index 45226b3cd4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_13.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -public class _13 { - - public static class Solution1 { - public int romanToInt(String s) { - Map map = new HashMap(); - map.put('I', 1); - map.put('V', 5); - map.put('X', 10); - map.put('L', 50); - map.put('C', 100); - map.put('D', 500); - map.put('M', 1000); - - int result = 0; - for (int i = 0; i < s.length(); i++) { - if (i > 0 && map.get(s.charAt(i)) > map.get(s.charAt(i - 1))) { - result += map.get(s.charAt(i)) - 2 * map.get(s.charAt(i - 1)); - } else { - result += map.get(s.charAt(i)); - } - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_130.java b/src/main/java/com/fishercoder/solutions/_130.java deleted file mode 100644 index 6d97060e67..0000000000 --- a/src/main/java/com/fishercoder/solutions/_130.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 130. Surrounded Regions - * - * Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. - - A region is captured by flipping all 'O's into 'X's in that surrounded region. - - For example, - X X X X - X O O X - X X O X - X O X X - - After running your function, the board should be: - - X X X X - X X X X - X X X X - X O X X - */ -public class _130 { - - public static class Solution1 { - - /** - * I won't call this problem hard, it's just confusing, you'll definitely want to clarify what - * the problem means before coding. This problem eactually means: any grid that is 'O' but on - * the four edges, will never be marked to 'X'; furthermore, any grid that is 'O' and that is - * connected with the above type of 'O' will never be marked to 'X' as well; only all other - * nodes that has any one direct neighbor that is an 'X' will be marked to 'X'. - */ - - int[] dirs = new int[] {0, 1, 0, -1, 0}; - - public void solve(char[][] board) { - if (board == null || board.length == 0 || board[0].length == 0) { - return; - } - int m = board.length; - int n = board[0].length; - Queue queue = new LinkedList(); - //check first row and last row and mark all those '0' on these two rows to be '+' to let them be different from other 'O', - //at the same time, we put them into the queue to get ready for a BFS to mark all those adjacent 'O' nodes to '+' as well - for (int j = 0; j < n; j++) { - if (board[0][j] == 'O') { - board[0][j] = '+'; - queue.offer(new int[] {0, j}); - } - if (board[m - 1][j] == 'O') { - board[m - 1][j] = '+'; - queue.offer(new int[] {m - 1, j}); - } - } - - //check first column and last column too - for (int i = 0; i < m; i++) { - if (board[i][0] == 'O') { - board[i][0] = '+'; - queue.offer(new int[] {i, 0}); - } - if (board[i][n - 1] == 'O') { - board[i][n - 1] = '+'; - queue.offer(new int[] {i, n - 1}); - } - } - - while (!queue.isEmpty()) { - int[] curr = queue.poll(); - for (int i = 0; i < 4; i++) { - int x = curr[0] + dirs[i]; - int y = curr[1] + dirs[i + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') { - board[x][y] = '+'; - queue.offer(new int[] {x, y}); - } - } - } - - //now we can safely mark all other 'O' to 'X', also remember to put those '+' back to 'O' - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (board[i][j] == 'O') { - board[i][j] = 'X'; - } else if (board[i][j] == '+') { - board[i][j] = 'O'; - } - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1300.java b/src/main/java/com/fishercoder/solutions/_1300.java deleted file mode 100644 index c65db93b96..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1300.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1300. Sum of Mutated Array Closest to Target - * - * Given an integer array arr and a target value target, return the integer value such that when we change all the integers larger than value in the given array to be equal to value, - * the sum of the array gets as close as possible (in absolute difference) to target. - * In case of a tie, return the minimum such integer. - * Notice that the answer is not neccesarilly a number from arr. - * - * Example 1: - * Input: arr = [4,9,3], target = 10 - * Output: 3 - * Explanation: When using 3 arr converts to [3, 3, 3] which sums 9 and that's the optimal answer. - * - * Example 2: - * Input: arr = [2,3,5], target = 10 - * Output: 5 - * - * Example 3: - * Input: arr = [60864,25176,27249,21296,20204], target = 56803 - * Output: 11361 - * */ -public class _1300 { - public static class Solution1 { - public int findBestValue(int[] arr, int target) { - int ave = target / arr.length; - int min = findMin(arr); - int max = findMax(arr); - //if ave is the best value, what's the difference to target? - int closetDiff = findClosestDiffIfReplaceWithVal(arr, ave, target); - int bestValue = ave; - //extend candidate towards the right to see how close the sum could be to target - int candidateOnTheRight = ave; - while (candidateOnTheRight <= max) { - int thisOne = findClosestDiffIfReplaceWithVal(arr, ++candidateOnTheRight, target); - if (thisOne >= closetDiff) { - break; - } else { - closetDiff = thisOne; - bestValue = candidateOnTheRight; - } - } - - //extend candidate towards the left to see how close the sum could be to target - int candidateOnTheLeft = ave; - while (candidateOnTheLeft >= min) { - int thisOne = findClosestDiffIfReplaceWithVal(arr, --candidateOnTheLeft, target); - if (thisOne >= closetDiff) { - break; - } else { - closetDiff = thisOne; - bestValue = candidateOnTheLeft; - } - } - return bestValue; - } - - private int findClosestDiffIfReplaceWithVal(int[] arr, int replaceValue, int target) { - int sum = 0; - for (int i = 0; i < arr.length; i++) { - if (arr[i] > replaceValue) { - sum += replaceValue; - } else { - sum += arr[i]; - } - } - return Math.abs(sum - target); - } - - private int findMax(int[] arr) { - int max = arr[0]; - for (int i = 1; i < arr.length; i++) { - max = Math.max(max, arr[i]); - } - return max; - } - - private int findMin(int[] arr) { - int min = arr[0]; - for (int i = 1; i < arr.length; i++) { - min = Math.min(min, arr[i]); - } - return min; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1302.java b/src/main/java/com/fishercoder/solutions/_1302.java deleted file mode 100644 index 80f08ab0b9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1302.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 1302. Deepest Leaves Sum - * - * Given a binary tree, return the sum of values of its deepest leaves. - * - * Example 1: - * 1 - * / \ - * 2 3 - * / \ \ - * 4 5 6 - * / \ - * 7 8 - * - * Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8] - * Output: 15 - * - * Constraints: - * The number of nodes in the tree is between 1 and 10^4. - * The value of nodes is between 1 and 100. - * */ -public class _1302 { - public static class Solution1 { - public int deepestLeavesSum(TreeNode root) { - int depth = maxDepth(root); - return bfs(root, depth); - } - - private int bfs(TreeNode root, int depth) { - Queue queue = new LinkedList<>(); - queue.offer(root); - int currentLevel = 0; - int sum = 0; - while (!queue.isEmpty()) { - int size = queue.size(); - currentLevel++; - for (int i = 0; i < size; i++) { - TreeNode currNode = queue.poll(); - if (currentLevel == depth) { - sum += currNode.val; - } - if (currNode.left != null) { - queue.offer(currNode.left); - } - if (currNode.right != null) { - queue.offer(currNode.right); - } - } - } - return sum; - } - - private int maxDepth(TreeNode root) { - if (root == null) { - return 0; - } - return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1304.java b/src/main/java/com/fishercoder/solutions/_1304.java deleted file mode 100644 index dabf593aa3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1304.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1304. Find N Unique Integers Sum up to Zero - * - * Given an integer n, return any array containing n unique integers such that they add up to 0. - * - * Example 1: - * Input: n = 5 - * Output: [-7,-1,1,3,4] - * Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4]. - * - * Example 2: - * Input: n = 3 - * Output: [-1,0,1] - * - * Example 3: - * Input: n = 1 - * Output: [0] - * */ -public class _1304 { - public static class Solution1 { - public int[] sumZero(int n) { - int[] result = new int[n]; - int start = -n / 2; - for (int i = 0; i < n / 2; i++) { - result[i] = start++; - } - if (n % 2 == 0) { - start++; - } - for (int i = n / 2; i < n; i++) { - result[i] = start++; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1305.java b/src/main/java/com/fishercoder/solutions/_1305.java deleted file mode 100644 index b45b8f05f4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1305.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * 1305. All Elements in Two Binary Search Trees - * - * Given two binary search trees root1 and root2. - * Return a list containing all the integers from both trees sorted in ascending order. - * - * Example 1: - * Input: root1 = [2,1,4], root2 = [1,0,3] - * Output: [0,1,1,2,3,4] - * - * Example 2: - * Input: root1 = [0,-10,10], root2 = [5,1,7,0,2] - * Output: [-10,0,0,1,2,5,7,10] - * - * Example 3: - * Input: root1 = [], root2 = [5,1,7,0,2] - * Output: [0,1,2,5,7] - * - * Example 4: - * Input: root1 = [0,-10,10], root2 = [] - * Output: [-10,0,10] - * - * Example 5: - * Input: root1 = [1,null,8], root2 = [8,1] - * Output: [1,1,8,8] - * - * Constraints: - * Each tree has at most 5000 nodes. - * Each node's value is between [-10^5, 10^5].*/ -public class _1305 { - public static class Solution1 { - public List getAllElements(TreeNode root1, TreeNode root2) { - List list1 = getAllNodes(root1); - List list2 = getAllNodes(root2); - List merged = new ArrayList<>(); - merged.addAll(list1); - merged.addAll(list2); - Collections.sort(merged); - return merged; - } - - private List getAllNodes(TreeNode root) { - List list = new ArrayList<>(); - return inorder(root, list); - } - - List inorder(TreeNode root, List result) { - if (root == null) { - return result; - } - inorder(root.left, result); - result.add(root.val); - return inorder(root.right, result); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1309.java b/src/main/java/com/fishercoder/solutions/_1309.java deleted file mode 100644 index 790e745058..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1309.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 1309. Decrypt String from Alphabet to Integer Mapping - * - * Given a string s formed by digits ('0' - '9') and '#' . We want to map s to English lowercase characters as follows: - * Characters ('a' to 'i') are represented by ('1' to '9') respectively. - * Characters ('j' to 'z') are represented by ('10#' to '26#') respectively. - * Return the string formed after mapping. - * It's guaranteed that a unique mapping will always exist. - * - * Example 1: - * Input: s = "10#11#12" - * Output: "jkab" - * Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2". - * - * Example 2: - * Input: s = "1326#" - * Output: "acz" - * - * Example 3: - * Input: s = "25#" - * Output: "y" - * - * Example 4: - * Input: s = "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#" - * Output: "abcdefghijklmnopqrstuvwxyz" - * - * Constraints: - * 1 <= s.length <= 1000 - * s[i] only contains digits letters ('0'-'9') and '#' letter. - * s will be valid string such that mapping is always possible. - * */ -public class _1309 { - public static class Solution1 { - //TODO: very silly solution, optimze it - public String freqAlphabets(String s) { - Map map = new HashMap<>(); - map.put("1", "a"); - map.put("2", "b"); - map.put("3", "c"); - map.put("4", "d"); - map.put("5", "e"); - map.put("6", "f"); - map.put("7", "g"); - map.put("8", "h"); - map.put("9", "i"); - map.put("10#", "j"); - map.put("11#", "k"); - map.put("12#", "l"); - map.put("13#", "m"); - map.put("14#", "n"); - map.put("15#", "o"); - map.put("16#", "p"); - map.put("17#", "q"); - map.put("18#", "r"); - map.put("19#", "s"); - map.put("20#", "t"); - map.put("21#", "u"); - map.put("22#", "v"); - map.put("23#", "w"); - map.put("24#", "x"); - map.put("25#", "y"); - map.put("26#", "z"); - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < s.length(); ) { - if (Integer.parseInt("" + s.charAt(i)) == 1 && i + 1 < s.length() && i + 2 < s.length() && s.charAt(i + 2) == '#') { - sb.append(map.get(s.substring(i, i + 3))); - i += 3; - } else if (Integer.parseInt("" + s.charAt(i)) == 2 && i + 1 < s.length() && i + 2 < s.length() && s.charAt(i + 2) == '#') { - sb.append(map.get(s.substring(i, i + 3))); - i += 3; - } else { - sb.append(map.get("" + s.charAt(i))); - i++; - } - } - return sb.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_131.java b/src/main/java/com/fishercoder/solutions/_131.java deleted file mode 100644 index b8882d7a0d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_131.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 131. Palindrome Partitioning - - Given a string s, partition s such that every substring of the partition is a palindrome. - - Return all possible palindrome partitioning of s. - - For example, given s = "aab", - Return - - [ - ["aa","b"], - ["a","a","b"] - ] - - */ -public class _131 { - - public static class Solution1 { - public List> partition(String s) { - List> result = new ArrayList(); - int n = s.length(); - boolean[][] dp = new boolean[n][n]; - for (int i = 0; i < n; i++) { - for (int j = 0; j <= i; j++) { - if (s.charAt(j) == s.charAt(i) && (j + 1 >= i - 1 || dp[j + 1][i - 1])) { - // j+1 >= i-1 means j and i are adjance to each other or only one char apart from each other - //dp[j+1][i-1] means its inner substring is a palindrome, so as long as s.charAt(j) == s.charAt(i), then dp[j][i] must be a palindrome. - dp[j][i] = true; - } - } - } - - for (boolean[] list : dp) { - for (boolean b : list) { - System.out.print(b + ", "); - } - System.out.println(); - } - System.out.println(); - - backtracking(s, 0, dp, new ArrayList(), result); - - return result; - } - - void backtracking(String s, int start, boolean[][] dp, List temp, - List> result) { - if (start == s.length()) { - List newTemp = new ArrayList(temp); - result.add(newTemp); - } - for (int i = start; i < s.length(); i++) { - if (dp[start][i]) { - temp.add(s.substring(start, i + 1)); - backtracking(s, i + 1, dp, temp, result); - temp.remove(temp.size() - 1); - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1313.java b/src/main/java/com/fishercoder/solutions/_1313.java deleted file mode 100644 index 712f6bcbee..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1313.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1313. Decompress Run-Length Encoded List - * - * We are given a list nums of integers representing a list compressed with run-length encoding. - * Consider each adjacent pair of elements [a, b] = [nums[2*i], nums[2*i+1]] (with i >= 0). - * For each such pair, there are a elements with value b in the decompressed list. - * - * Return the decompressed list. - * - * Example 1: - * Input: nums = [1,2,3,4] - * Output: [2,4,4,4] - * Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2]. - * The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4]. - * At the end the concatenation [2] + [4,4,4,4] is [2,4,4,4]. - * - * Constraints: - * 2 <= nums.length <= 100 - * nums.length % 2 == 0 - * 1 <= nums[i] <= 100 - * */ -public class _1313 { - public static class Solution1 { - public int[] decompressRLElist(int[] nums) { - List list = new ArrayList<>(); - for (int i = 0; i < nums.length - 1; i += 2) { - int count = nums[i]; - int value = nums[i + 1]; - while (count-- > 0) { - list.add(value); - } - } - return list.stream().mapToInt(integer -> integer).toArray(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1314.java b/src/main/java/com/fishercoder/solutions/_1314.java deleted file mode 100644 index 9da8b4f046..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1314.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1314. Matrix Block Sum - * - * Given a m * n matrix mat and an integer K, - * return a matrix answer where each answer[i][j] is the sum of all elements - * mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix. - * - * Example 1: - * Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 - * Output: [[12,21,16],[27,45,33],[24,39,28]] - * - * Example 2: - * Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 - * Output: [[45,45,45],[45,45,45],[45,45,45]] - * - * Constraints: - * m == mat.length - * n == mat[i].length - * 1 <= m, n, K <= 100 - * 1 <= mat[i][j] <= 100 - * */ -public class _1314 { - public static class Solution1 { - public int[][] matrixBlockSum(int[][] mat, int K) { - int m = mat.length; - int n = mat[0].length; - int[][] answer = new int[m][n]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - List iRange = findRange(i, K, m); - List jRange = findRange(j, K, n); - int sum = 0; - for (int ii = 0; ii < iRange.size(); ii++) { - for (int jj = 0; jj < jRange.size(); jj++) { - sum += mat[iRange.get(ii)][jRange.get(jj)]; - } - } - answer[i][j] = sum; - } - } - return answer; - } - - private List findRange(int iOrJ, int k, int upper) { - int min = (iOrJ - k) < 0 ? 0 : (iOrJ - k); - int max = (iOrJ + k) >= upper ? (upper - 1) : (iOrJ + k); - List range = new ArrayList<>(); - for (int i = min; i <= max; i++) { - range.add(i); - } - return range; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1315.java b/src/main/java/com/fishercoder/solutions/_1315.java deleted file mode 100644 index ca62315558..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1315.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 1315. Sum of Nodes with Even-Valued Grandparent - * - * Given a binary tree, return the sum of values of nodes with even-valued grandparent. - * (A grandparent of a node is the parent of its parent, if it exists.) - * If there are no nodes with an even-valued grandparent, return 0. - * - * Example 1: - * 6 - * / \ - * 7 8 - * / \ / \ - * 2 7 1 3 - * / / \ \ - * 9 1 4 5 - * - * Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5] - * Output: 18 - * Explanation: The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents. - * - * Constraints: - * The number of nodes in the tree is between 1 and 10^4. - * The value of nodes is between 1 and 100. - * */ -public class _1315 { - public static class Solution1 { - public int sumEvenGrandparent(TreeNode root) { - if (root == null) { - return 0; - } - return dfs(root, root.left, 0) + dfs(root, root.right, 0); - } - - private int dfs(TreeNode grandparent, TreeNode parent, int sum) { - if (grandparent == null || parent == null) { - return sum; - } - if (grandparent.val % 2 == 0 && parent.left != null) { - sum += parent.left.val; - } - if (grandparent.val % 2 == 0 && parent.right != null) { - sum += parent.right.val; - } - sum = dfs(parent, parent.left, sum); - sum = dfs(parent, parent.right, sum); - return sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_132.java b/src/main/java/com/fishercoder/solutions/_132.java deleted file mode 100644 index b1773b986a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_132.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 132. Palindrome Partitioning II - - Given a string s, partition s such that every substring of the partition is a palindrome. - - Return the minimum cuts needed for a palindrome partitioning of s. - - For example, given s = "aab", - Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut. - - */ -public class _132 { - - /**This solution is cooler than Jiuzhang: https://discuss.leetcode.com/topic/32575/easiest-java-dp-solution-97-36*/ - - public static class Solution1 { - //cut[i] stands for the minimum number of cut needed to cut [0, i] into palindromes - //we initiazlie cut[i] with its max possible value which is i, this is because a single char is naturally a palindrome, so, we'll cut this string into all single-char substrings, which is the max cuts needed - - //dp[j][i] == true stands for s.substring(j,i) is a palindrome - public int minCut(String s) { - int n = s.length(); - char[] c = s.toCharArray(); - boolean[][] dp = new boolean[n][n]; - int[] cut = new int[n]; - - for (int i = 0; i < n; i++) { - cut[i] = i; - for (int j = 0; j <= i; j++) { - if (c[i] == c[j] && (j + 1 > i - 1 || dp[j + 1][i - 1])) { - dp[j][i] = true; - if (j == 0) { - cut[i] = 0; - } else { - cut[i] = (cut[i] < cut[j - 1] + 1) ? cut[i] : cut[j - 1] + 1; - } - } - } - } - - return cut[n - 1]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_133.java b/src/main/java/com/fishercoder/solutions/_133.java deleted file mode 100644 index 35b1ea2dee..0000000000 --- a/src/main/java/com/fishercoder/solutions/_133.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.UndirectedGraphNode; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.Queue; - -/** - * 133. Clone Graph - - Clone an undirected graph. - Each node in the graph contains a label and a list of its neighbors. - - - OJ's undirected graph serialization: - Nodes are labeled uniquely. - - We use # as a separator for each node, and , as a separator for node label and each neighbor of the node. - As an example, consider the serialized graph {0,1,2#1,2#2,2}. - - The graph has a total of three nodes, and therefore contains three parts as separated by #. - - First node is labeled as 0. Connect node 0 to both nodes 1 and 2. - Second node is labeled as 1. Connect node 1 to node 2. - Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle. - Visually, the graph looks like the following: - - 1 - / \ - / \ - 0 --- 2 - / \ - \_/ - - */ -public class _133 { - - public static class Solution1 { - public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { - if (node == null) { - return node; - } - - Map map = new HashMap(); - Queue queue = new LinkedList(); - UndirectedGraphNode root = new UndirectedGraphNode(node.label); - map.put(root.label, root); - queue.offer(node); - //remember to offer the original input node into the queue which contains all the information - while (!queue.isEmpty()) { - UndirectedGraphNode curr = queue.poll(); - for (UndirectedGraphNode eachNode : curr.neighbors) { - if (!map.containsKey(eachNode.label)) { - map.put(eachNode.label, new UndirectedGraphNode(eachNode.label)); - queue.offer(eachNode); - } - map.get(curr.label).neighbors.add(map.get(eachNode.label)); - } - } - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1338.java b/src/main/java/com/fishercoder/solutions/_1338.java deleted file mode 100644 index 7c3918cc82..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1338.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Collections; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 1338. Reduce Array Size to The Half - * - * Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array. - * Return the minimum size of the set so that at least half of the integers of the array are removed. - * - * Example 1: - * Input: arr = [3,3,3,3,5,5,5,2,2,7] - * Output: 2 - * Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array). - * Possible sets of size 2 are {3,5},{3,2},{5,2}. - * Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array. - * - * Example 2: - * Input: arr = [7,7,7,7,7,7] - * Output: 1 - * Explanation: The only possible set you can choose is {7}. This will make the new array empty. - * - * Example 3: - * Input: arr = [1,9] - * Output: 1 - * - * Example 4: - * Input: arr = [1000,1000,3,7] - * Output: 1 - * - * Example 5: - * Input: arr = [1,2,3,4,5,6,7,8,9,10] - * Output: 5 - * - * Constraints: - * 1 <= arr.length <= 10^5 - * arr.length is even. - * 1 <= arr[i] <= 10^5 - * */ -public class _1338 { - public static class Solution1 { - public int minSetSize(int[] arr) { - Map map = new HashMap<>(); - for (int i : arr) { - map.put(i, map.getOrDefault(i, 0) + 1); - } - List list = new ArrayList<>(); - for (int k : map.keySet()) { - list.add(map.get(k)); - } - Collections.sort(list, Collections.reverseOrder()); - int sum = 0; - int i = 0; - while (sum < arr.length / 2) { - sum += list.get(i++); - } - return i--; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1339.java b/src/main/java/com/fishercoder/solutions/_1339.java deleted file mode 100644 index 35c93e42fb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1339.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.HashSet; -import java.util.Set; - -/** - * 1339. Maximum Product of Splitted Binary Tree - * - * Given a binary tree root. Split the binary tree into two subtrees by removing 1 edge such that the product of the sums of the subtrees are maximized. - * Since the answer may be too large, return it modulo 10^9 + 7. - * - * Example 1: - * Input: root = [1,2,3,4,5,6] - * Output: 110 - * Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10) - * - * Example 2: - * Input: root = [1,null,2,3,4,null,null,5,6] - * Output: 90 - * Explanation: Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6) - * - * Example 3: - * Input: root = [2,3,9,10,7,8,6,5,4,11,1] - * Output: 1025 - * - * Example 4: - * Input: root = [1,1] - * Output: 1 - * - * Constraints: - * Each tree has at most 50000 nodes and at least 2 nodes. - * Each node's value is between [1, 10000]. - * */ -public class _1339 { - public static class Solution1 { - public int maxProduct(TreeNode root) { - Set set = new HashSet<>(); - int total = dfs(root, set); - long result = 0L; - for (long sum : set) { - result = Math.max(result, sum * (total - sum)); - } - return (int) (result % 1000000007); - } - - private int dfs(TreeNode root, Set set) { - if (root == null) { - return 0; - } - root.val += dfs(root.left, set); - root.val += dfs(root.right, set); - set.add((long) root.val); - return root.val; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_134.java b/src/main/java/com/fishercoder/solutions/_134.java deleted file mode 100644 index 8513854eba..0000000000 --- a/src/main/java/com/fishercoder/solutions/_134.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 134. Gas Station - * - * There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. - You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). - You begin the journey with an empty tank at one of the gas stations. - - Return the starting gas station's index if you can travel around the circuit once, otherwise return -1. - - Note: - The solution is guaranteed to be unique. - */ -public class _134 { - - public static class Solution1 { - /** Credit: https://discuss.leetcode.com/topic/5088/my-ac-is-o-1-space-o-n-running-time-solution-does-anybody-have-posted-this-solution */ - public int canCompleteCircuit(int[] gas, int[] cost) { - int start = gas.length - 1; - int end = 0; - int sum = gas[start] - cost[start]; - while (start > end) { - if (sum >= 0) { - sum += gas[end] - cost[end]; - end++; - } else { - start--; - sum += gas[start] - cost[start]; - } - } - return sum >= 0 ? start : -1; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_1341.java b/src/main/java/com/fishercoder/solutions/_1341.java deleted file mode 100644 index 0f3f788b9a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1341.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * 1341. The K Weakest Rows in a Matrix - * - * Given a m * n matrix mat of ones (representing soldiers) and zeros (representing civilians), - * return the indexes of the k weakest rows in the matrix ordered from the weakest to the strongest. - * A row i is weaker than row j, if the number of soldiers in row i is less than the number of soldiers in row j, - * or they have the same number of soldiers but i is less than j. Soldiers are always stand in the frontier of a row, - * that is, always ones may appear first and then zeros. - * - * Example 1: - * Input: mat = - * [[1,1,0,0,0], - * [1,1,1,1,0], - * [1,0,0,0,0], - * [1,1,0,0,0], - * [1,1,1,1,1]], - * k = 3 - * Output: [2,0,3] - * Explanation: - * The number of soldiers for each row is: - * row 0 -> 2 - * row 1 -> 4 - * row 2 -> 1 - * row 3 -> 2 - * row 4 -> 5 - * Rows ordered from the weakest to the strongest are [2,0,3,1,4] - * - * Example 2: - * Input: mat = - * [[1,0,0,0], - * [1,1,1,1], - * [1,0,0,0], - * [1,0,0,0]], - * k = 2 - * Output: [0,2] - * Explanation: - * The number of soldiers for each row is: - * row 0 -> 1 - * row 1 -> 4 - * row 2 -> 1 - * row 3 -> 1 - * Rows ordered from the weakest to the strongest are [0,2,3,1] - * - * Constraints: - * m == mat.length - * n == mat[i].length - * 2 <= n, m <= 100 - * 1 <= k <= m - * matrix[i][j] is either 0 or 1. - * */ -public class _1341 { - public static class Solution1 { - public int[] kWeakestRows(int[][] mat, int k) { - List list = new ArrayList<>(); - for (int i = 0; i < mat.length; i++) { - int soldiers = 0; - for (int j = 0; j < mat[0].length; j++) { - if (mat[i][j] == 1) { - soldiers++; - } else { - break; - } - } - list.add(new int[]{i, soldiers}); - } - Collections.sort(list, (a, b) -> a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]); - int[] result = new int[k]; - int i = 0; - while (i < k) { - result[i] = list.get(i++)[0]; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1342.java b/src/main/java/com/fishercoder/solutions/_1342.java deleted file mode 100644 index 825d5fe891..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1342.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1342. Number of Steps to Reduce a Number to Zero - * - * Given a non-negative integer num, return the number of steps to reduce it to zero. - * If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. - * - * Example 1: - * Input: num = 14 - * Output: 6 - * Explanation: - * Step 1) 14 is even; divide by 2 and obtain 7. - * Step 2) 7 is odd; subtract 1 and obtain 6. - * Step 3) 6 is even; divide by 2 and obtain 3. - * Step 4) 3 is odd; subtract 1 and obtain 2. - * Step 5) 2 is even; divide by 2 and obtain 1. - * Step 6) 1 is odd; subtract 1 and obtain 0. - * - * Example 2: - * Input: num = 8 - * Output: 4 - * Explanation: - * Step 1) 8 is even; divide by 2 and obtain 4. - * Step 2) 4 is even; divide by 2 and obtain 2. - * Step 3) 2 is even; divide by 2 and obtain 1. - * Step 4) 1 is odd; subtract 1 and obtain 0. - * - * Example 3: - * Input: num = 123 - * Output: 12 - * - * Constraints: - * 0 <= num <= 10^6 - * */ -public class _1342 { - public static class Solution1 { - public int numberOfSteps(int num) { - int steps = 0; - while (num != 0) { - if (num % 2 == 0) { - num /= 2; - } else { - num--; - } - steps++; - } - return steps; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1343.java b/src/main/java/com/fishercoder/solutions/_1343.java deleted file mode 100644 index 7dd759bd68..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1343.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold - * - * Given an array of integers arr and two integers k and threshold. - * Return the number of sub-arrays of size k and average greater than or equal to threshold. - * - * Example 1: - * Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 - * Output: 3 - * Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). - * - * Example 2: - * Input: arr = [1,1,1,1,1], k = 1, threshold = 0 - * Output: 5 - * - * Example 3: - * Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 - * Output: 6 - * Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. - * - * Example 4: - * Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 - * Output: 1 - * - * Example 5: - * Input: arr = [4,4,4,4], k = 4, threshold = 1 - * Output: 1 - * - * Constraints: - * 1 <= arr.length <= 10^5 - * 1 <= arr[i] <= 10^4 - * 1 <= k <= arr.length - * 0 <= threshold <= 10^4 - * */ -public class _1343 { - public static class Solution1 { - public int numOfSubarrays(int[] arr, int k, int threshold) { - int sum = 0; - for (int i = 0; i < k - 1; i++) { - sum += arr[i]; - } - int count = 0; - for (int i = k - 1; i < arr.length; i++) { - sum += arr[i]; - if (i - k >= 0) { - sum -= arr[i - k]; - } - if (sum / k >= threshold) { - count++; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1344.java b/src/main/java/com/fishercoder/solutions/_1344.java deleted file mode 100644 index a3609342c1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1344.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1344. Angle Between Hands of a Clock - * - * Given two numbers, hour and minutes. Return the smaller angle (in sexagesimal units) formed between the hour and the minute hand. - * - * Example 1: - * Input: hour = 12, minutes = 30 - * Output: 165 - * - * Example 2: - * Input: hour = 3, minutes = 30 - * Output: 75 - * - * Example 3: - * Input: hour = 3, minutes = 15 - * Output: 7.5 - * - * Example 4: - * Input: hour = 4, minutes = 50 - * Output: 155 - * - * Example 5: - * Input: hour = 12, minutes = 0 - * Output: 0 - * - * Constraints: - * 1 <= hour <= 12 - * 0 <= minutes <= 59 - * Answers within 10^-5 of the actual value will be accepted as correct. - * */ -public class _1344 { - public static class Solution1 { - public double angleClock(int hour, int minutes) { - double minAngle = minutes * 360 / 60; - double hourAnglePart1 = hour != 12 ? (hour * 360) / 12 : 0; - double hourAnglePart2 = (double) (30 * minutes) / (double) 60; - double hourAngle = hourAnglePart1 + hourAnglePart2; - double preResult = Math.abs(minAngle - (hourAngle)); - return preResult > 180 ? 360 - preResult : preResult; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1345.java b/src/main/java/com/fishercoder/solutions/_1345.java deleted file mode 100644 index e90565b904..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1345.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; - -/** - * 1345. Jump Game IV - * - * Given an array of integers arr, you are initially positioned at the first index of the array. - * In one step you can jump from index i to index: - * i + 1 where: i + 1 < arr.length. - * i - 1 where: i - 1 >= 0. - * j where: arr[i] == arr[j] and i != j. - * Return the minimum number of steps to reach the last index of the array. - * Notice that you can not jump outside of the array at any time. - * - * Example 1: - * Input: arr = [100,-23,-23,404,100,23,23,23,3,404] - * Output: 3 - * Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array. - * - * Example 2: - * Input: arr = [7] - * Output: 0 - * Explanation: Start index is the last index. You don't need to jump. - * - * Example 3: - * Input: arr = [7,6,9,6,9,6,9,7] - * Output: 1 - * Explanation: You can jump directly from index 0 to index 7 which is last index of the array. - * - * Example 4: - * Input: arr = [6,1,9] - * Output: 2 - * - * Example 5: - * Input: arr = [11,22,7,7,7,7,7,7,7,22,13] - * Output: 3 - * - * Constraints: - * 1 <= arr.length <= 5 * 10^4 - * -10^8 <= arr[i] <= 10^8 - * */ -public class _1345 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/jump-game-iv/discuss/502699/JavaC%2B%2B-BFS-Solution-Clean-code-O(N)*/ - public int minJumps(int[] arr) { - Map> valueToIndices = new HashMap<>(); - for (int i = 0; i < arr.length; i++) { - valueToIndices.computeIfAbsent(arr[i], k -> new LinkedList<>()).add(i); - } - boolean[] visited = new boolean[arr.length]; - Queue indexQueue = new LinkedList<>(); - indexQueue.offer(0); - int steps = 0; - while (!indexQueue.isEmpty()) { - int size = indexQueue.size(); - for (int i = 0; i < size; i++) { - int index = indexQueue.poll(); - if (index == arr.length - 1) { - return steps; - } - List nextPossibleIndices = valueToIndices.get(arr[index]); - nextPossibleIndices.add(index - 1); - nextPossibleIndices.add(index + 1); - for (int next : nextPossibleIndices) { - if (next >= 0 && next < arr.length && !visited[next]) { - visited[next] = true; - indexQueue.offer(next); - } - } - nextPossibleIndices.clear();//this line is the key to this entire algorithm to avoid TLE, explanation: https://leetcode.com/problems/jump-game-iv/discuss/502699/JavaC++-BFS-Solution-Clean-code-O(N)/445620 - } - steps++; - } - return 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1346.java b/src/main/java/com/fishercoder/solutions/_1346.java deleted file mode 100644 index bbba649c06..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1346.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1346. Check If N and Its Double Exist - * - * Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M). - * - * More formally check if there exists two indices i and j such that : - * - * i != j - * 0 <= i, j < arr.length - * arr[i] == 2 * arr[j] - * - * Example 1: - * Input: arr = [10,2,5,3] - * Output: true - * Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5. - * - * Example 2: - * Input: arr = [7,1,14,11] - * Output: true - * Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7. - * - * Example 3: - * Input: arr = [3,1,7,11] - * Output: false - * Explanation: In this case does not exist N and M, such that N = 2 * M. - * - * Constraints: - * 2 <= arr.length <= 500 - * -10^3 <= arr[i] <= 10^3 - * */ -public class _1346 { - public static class Solution1 { - public boolean checkIfExist(int[] arr) { - for (int i = 0; i < arr.length; i++) { - for (int j = 0; j < arr.length; j++) { - if (i != j && (arr[i] * 2 == arr[j] || arr[i] == arr[j] * 2)) { - return true; - } - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1347.java b/src/main/java/com/fishercoder/solutions/_1347.java deleted file mode 100644 index 66b5cfe1c3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1347.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1347. Minimum Number of Steps to Make Two Strings Anagram - * - * Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. - * Return the minimum number of steps to make t an anagram of s. - * An Anagram of a string is a string that contains the same characters with a different (or the same) ordering. - * - * Example 1: - * Input: s = "bab", t = "aba" - * Output: 1 - * Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s. - * - * Example 2: - * Input: s = "leetcode", t = "practice" - * Output: 5 - * Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s. - * - * Example 3: - * Input: s = "anagram", t = "mangaar" - * Output: 0 - * Explanation: "anagram" and "mangaar" are anagrams. - * - * Example 4: - * Input: s = "xxyyzz", t = "xxyyzz" - * Output: 0 - * - * Example 5: - * Input: s = "friend", t = "family" - * Output: 4 - * - * Constraints: - * 1 <= s.length <= 50000 - * s.length == t.length - * s and t contain lower-case English letters only. - * */ -public class _1347 { - public static class Solution1 { - public int minSteps(String s, String t) { - int[] counts = new int[26]; - for (char c : s.toCharArray()) { - counts[c - 'a']++; - } - for (char c : t.toCharArray()) { - if (counts[c - 'a'] > 0) { - counts[c - 'a']--; - } - } - return Arrays.stream(counts).sum(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1348.java b/src/main/java/com/fishercoder/solutions/_1348.java deleted file mode 100644 index b9330651ea..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1348.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -/** - * 1348. Tweet Counts Per Frequency - * - * Implement the class TweetCounts that supports two methods: - * - * 1. recordTweet(string tweetName, int time) - * Stores the tweetName at the recorded time (in seconds). - * 2. getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) - * Returns the total number of occurrences for the given tweetName per minute, hour, or day (depending on freq) starting from the startTime (in seconds) and ending at the endTime (in seconds). - * freq is always minute, hour or day, representing the time interval to get the total number of occurrences for the given tweetName. - * The first time interval always starts from the startTime, - * so the time intervals are [startTime, startTime + delta*1>, [startTime + delta*1, startTime + delta*2>, [startTime + delta*2, startTime + delta*3>, ... , [startTime + delta*i, min(startTime + delta*(i+1), endTime + 1)> for some non-negative number i and delta (which depends on freq). - * - * Example: - * Input - * ["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency","getTweetCountsPerFrequency","recordTweet","getTweetCountsPerFrequency"] - * [[],["tweet3",0],["tweet3",60],["tweet3",10],["minute","tweet3",0,59],["minute","tweet3",0,60],["tweet3",120],["hour","tweet3",0,210]] - * - * Output - * [null,null,null,null,[2],[2,1],null,[4]] - * - * Explanation - * TweetCounts tweetCounts = new TweetCounts(); - * tweetCounts.recordTweet("tweet3", 0); - * tweetCounts.recordTweet("tweet3", 60); - * tweetCounts.recordTweet("tweet3", 10); // All tweets correspond to "tweet3" with recorded times at 0, 10 and 60. - * tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // return [2]. The frequency is per minute (60 seconds), so there is one interval of time: 1) [0, 60> - > 2 tweets. - * tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // return [2, 1]. The frequency is per minute (60 seconds), so there are two intervals of time: 1) [0, 60> - > 2 tweets, and 2) [60,61> - > 1 tweet. - * tweetCounts.recordTweet("tweet3", 120); // All tweets correspond to "tweet3" with recorded times at 0, 10, 60 and 120. - * tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210); // return [4]. The frequency is per hour (3600 seconds), so there is one interval of time: 1) [0, 211> - > 4 tweets. - * - * Constraints: - * There will be at most 10000 operations considering both recordTweet and getTweetCountsPerFrequency. - * 0 <= time, startTime, endTime <= 10^9 - * 0 <= endTime - startTime <= 10^4 - * */ -public class _1348 { - public static class Solution1 { - public static class TweetCounts { - /** - * credit: https://leetcode.com/problems/tweet-counts-per-frequency/discuss/503453/Java-TreeMap-Accepted-Solution-Easy-Understand - */ - private Map> map; - - public TweetCounts() { - map = new HashMap<>(); - } - - public void recordTweet(String tweetName, int time) { - if (!map.containsKey(tweetName)) { - map.put(tweetName, new TreeMap<>()); - } - TreeMap tweetMap = map.get(tweetName); - tweetMap.put(time, tweetMap.getOrDefault(time, 0) + 1); - } - - public List getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) { - if (!map.containsKey(tweetName)) { - return null; - } - int interval; - if (freq.equals("minute")) { - interval = 60; - } else if (freq.equals("hour")) { - interval = 60 * 60; - } else { - interval = 60 * 60 * 24; - } - int size = ((endTime - startTime) / interval) + 1; - int[] buckets = new int[size]; - TreeMap tweetMap = map.get(tweetName); - for (Map.Entry entry : tweetMap.subMap(startTime, endTime + 1).entrySet()) { - int index = (entry.getKey() - startTime) / interval; - buckets[index] += entry.getValue(); - } - List result = new ArrayList<>(); - for (int num : buckets) { - result.add(num); - } - return result; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1349.java b/src/main/java/com/fishercoder/solutions/_1349.java deleted file mode 100644 index 914bef745f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1349.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1349. Maximum Students Taking Exam - * - * Given a m * n matrix seats that represent seats distributions in a classroom. - * If a seat is broken, it is denoted by '#' character otherwise it is denoted by a '.' character. - * Students can see the answers of those sitting next to the left, right, upper left and upper right, but he cannot see the answers of the student sitting directly in front or behind him. Return the maximum number of students that can take the exam together without any cheating being possible.. - * Students must be placed in seats in good condition. - * - * Example 1: - * Input: seats = [["#",".","#","#",".","#"], - * [".","#","#","#","#","."], - * ["#",".","#","#",".","#"]] - * Output: 4 - * Explanation: Teacher can place 4 students in available seats so they don't cheat on the exam. - * - * Example 2: - * Input: seats = [[".","#"], - * ["#","#"], - * ["#","."], - * ["#","#"], - * [".","#"]] - * Output: 3 - * Explanation: Place all students in available seats. - * - * Example 3: - * Input: seats = [["#",".",".",".","#"], - * [".","#",".","#","."], - * [".",".","#",".","."], - * [".","#",".","#","."], - * ["#",".",".",".","#"]] - * Output: 10 - * Explanation: Place students in available seats in column 1, 3 and 5. - * - * Constraints: - * seats contains only characters '.' and'#'. - * m == seats.length - * n == seats[i].length - * 1 <= m <= 8 - * 1 <= n <= 8 - * */ -public class _1349 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/maximum-students-taking-exam/discuss/503686/A-simple-tutorial-on-this-bitmasking-problem*/ - public int maxStudents(char[][] seats) { - int m = seats.length; - int n = seats[0].length; - int[] validRows = new int[m]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - validRows[i] = (validRows[i] << 1) + (seats[i][j] == '.' ? 1 : 0); - } - } - int stateSize = 1 << n; // There are 2^n states for n columns in binary format - int[][] dp = new int[m][stateSize]; - for (int i = 0; i < m; i++) { - Arrays.fill(dp[i], -1); - } - int ans = 0; - for (int i = 0; i < m; i++) { - for (int j = 0; j < stateSize; j++) { - if (((j & validRows[i]) == j) && ((j & (j << 1)) == 0)) { - if (i == 0) { - dp[i][j] = Integer.bitCount(j); - } else { - for (int k = 0; k < stateSize; k++) { - if (((k << 1) & j) == 0 && ((j << 1) & k) == 0 && dp[i - 1][k] != -1) { - dp[i][j] = Math.max(dp[i][j], dp[i - 1][k] + Integer.bitCount(j)); - } - } - } - ans = Math.max(ans, dp[i][j]); - } - } - } - return ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_135.java b/src/main/java/com/fishercoder/solutions/_135.java deleted file mode 100644 index 94c705c233..0000000000 --- a/src/main/java/com/fishercoder/solutions/_135.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 135. Candy - - There are N children standing in a line. Each child is assigned a rating value. - - You are giving candies to these children subjected to the following requirements: - - Each child must have at least one candy. - Children with a higher rating get more candies than their neighbors. - What is the minimum candies you must give? - */ -public class _135 { - - public static class Solution1 { - public int candy(int[] ratings) { - int[] candy = new int[ratings.length]; - for (int i = 0; i < ratings.length; i++) { - candy[i] = 1; - } - - for (int i = 0; i < ratings.length - 1; i++) { - if (ratings[i] < ratings[i + 1]) { - candy[i + 1] = candy[i] + 1; - } - } - - for (int i = ratings.length - 1; i > 0; i--) { - if (ratings[i] < ratings[i - 1]) { - candy[i - 1] = Math.max(candy[i - 1], candy[i] + 1); - } - } - - int sum = 0; - for (int i : candy) { - sum += i; - } - - return sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1351.java b/src/main/java/com/fishercoder/solutions/_1351.java deleted file mode 100644 index 2557222bf1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1351.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1351. Count Negative Numbers in a Sorted Matrix - * - * Given a m * n matrix grid which is sorted in non-increasing order both row-wise and column-wise. - * Return the number of negative numbers in grid. - * - * Example 1: - * Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]] - * Output: 8 - * Explanation: There are 8 negatives number in the matrix. - * - * Example 2: - * Input: grid = [[3,2],[1,0]] - * Output: 0 - * - * Example 3: - * Input: grid = [[1,-1],[-1,-1]] - * Output: 3 - * - * Example 4: - * Input: grid = [[-1]] - * Output: 1 - * - * Constraints: - * m == grid.length - * n == grid[i].length - * 1 <= m, n <= 100 - * -100 <= grid[i][j] <= 100 - * */ -public class _1351 { - public static class Solution1 { - public int countNegatives(int[][] grid) { - int count = 0; - for (int[] row : grid) { - for (int v : row) { - if (v < 0) { - count++; - } - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1352.java b/src/main/java/com/fishercoder/solutions/_1352.java deleted file mode 100644 index 930a430855..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1352.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1352. Product of the Last K Numbers - * - * Implement the class ProductOfNumbers that supports two methods: - * 1. add(int num) - * Adds the number num to the back of the current list of numbers. - * 2. getProduct(int k) - * Returns the product of the last k numbers in the current list. - * You can assume that always the current list has at least k numbers. - * At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing. - * - * Example: - * Input - * ["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"] - * [[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]] - * Output - * [null,null,null,null,null,null,20,40,0,null,32] - * Explanation - * ProductOfNumbers productOfNumbers = new ProductOfNumbers(); - * productOfNumbers.add(3); // [3] - * productOfNumbers.add(0); // [3,0] - * productOfNumbers.add(2); // [3,0,2] - * productOfNumbers.add(5); // [3,0,2,5] - * productOfNumbers.add(4); // [3,0,2,5,4] - * productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20 - * productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40 - * productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0 - * productOfNumbers.add(8); // [3,0,2,5,4,8] - * productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32 - * - * Constraints: - * There will be at most 40000 operations considering both add and getProduct. - * 0 <= num <= 100 - * 1 <= k <= 40000 - * */ -public class _1352 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/product-of-the-last-k-numbers/discuss/510260/JavaC%2B%2BPython-Prefix-Product*/ - public static class ProductOfNumbers { - - List list; - - public ProductOfNumbers() { - add(0); - } - - public void add(int num) { - if (num > 0) { - list.add(list.get(list.size() - 1) * num); - } else { - list = new ArrayList<>(); - list.add(1); - } - } - - public int getProduct(int k) { - int size = list.size(); - return k >= size ? 0 : (list.get(size - 1) / list.get(size - k - 1)); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1353.java b/src/main/java/com/fishercoder/solutions/_1353.java deleted file mode 100644 index 0108888ae9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1353.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.utils.CommonUtils; - -import java.util.Arrays; -import java.util.Collections; -import java.util.PriorityQueue; - -/** - * 1353. Maximum Number of Events That Can Be Attended - * - * Given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi. - * You can attend an event i at any day d where startTimei <= d <= endTimei. Notice that you can only attend one event at any time d. - * Return the maximum number of events you can attend. - * - * Example 1: - * Input: events = [[1,2],[2,3],[3,4]] - * Output: 3 - * Explanation: You can attend all the three events. - * One way to attend them all is as shown. - * Attend the first event on day 1. - * Attend the second event on day 2. - * Attend the third event on day 3. - * - * Example 2: - * Input: events= [[1,2],[2,3],[3,4],[1,2]] - * Output: 4 - * - * Example 3: - * Input: events = [[1,4],[4,4],[2,2],[3,4],[1,1]] - * Output: 4 - * - * Example 4: - * Input: events = [[1,100000]] - * Output: 1 - * - * Example 5: - * Input: events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]] - * Output: 7 - * - * Constraints: - * 1 <= events.length <= 10^5 - * events[i].length == 2 - * 1 <= events[i][0] <= events[i][1] <= 10^5 - * */ -public class _1353 { - public static class Solution1 { - /** - * Credit: https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/510263/JavaC%2B%2BPython-Priority-Queue - * - * 1. Sort events by start time, if ties, by end time; - * 2. From day 1 to day 100,000, we add all events that start on this day into a priorityqueue, - * also, we remove the events that closed on this day from the priorityqueue; - * 3. attend the event that ends on this day (earliest, i.e. greedy) and pop it out of the priorityqueue - * - * */ - public int maxEvents(int[][] events) { - Arrays.sort(events, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); - PriorityQueue heap = new PriorityQueue<>(); - int maxEvents = 0; - int i = 0; - for (int day = 1; day <= 100000; day++) { - while (i < events.length && events[i][0] == day) { - heap.offer(events[i++][1]); - } - while (heap.size() > 0 && heap.peek() < day) { - heap.poll(); - } - if (heap.size() > 0) { - heap.poll(); - maxEvents++; - } - } - return maxEvents; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1354.java b/src/main/java/com/fishercoder/solutions/_1354.java deleted file mode 100644 index cba20ee37e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1354.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Collections; -import java.util.PriorityQueue; - -/** - * 1354. Construct Target Array With Multiple Sums - * - * Given an array of integers target. From a starting array, A consisting of all 1's, you may perform the following procedure : - * let x be the sum of all elements currently in your array. - * choose index i, such that 0 <= i < target.size and set the value of A at index i to x. - * You may repeat this procedure as many times as needed. - * Return True if it is possible to construct the target array from A otherwise return False. - * - * Example 1: - * Input: target = [9,3,5] - * Output: true - * Explanation: Start with [1, 1, 1] - * [1, 1, 1], sum = 3 choose index 1 - * [1, 3, 1], sum = 5 choose index 2 - * [1, 3, 5], sum = 9 choose index 0 - * [9, 3, 5] Done - * - * Example 2: - * Input: target = [1,1,1,2] - * Output: false - * Explanation: Impossible to create target array from [1,1,1,1]. - * - * Example 3: - * Input: target = [8,5] - * Output: true - * - * Constraints: - * N == target.length - * 1 <= target.length <= 5 * 10^4 - * 1 <= target[i] <= 10^9 - * */ -public class _1354 { - public static class Solution1 { - /** - * 1. A good idea/trick to calculate the previous value of the largest number max: (2 * max - total). - * 2. Use a PriorityQueue to store the elements in reverse order to help us get the largest element in O(1) time - * 3. Also keep a variable of total sum - * - * reference: https://leetcode.com/problems/construct-target-array-with-multiple-sums/discuss/510214/C%2B%2B-Reaching-Points-Work-Backwards - */ - public boolean isPossible(int[] target) { - PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); - long sum = 0L; - for (int v : target) { - sum += v; - pq.offer((long) v); - } - while (sum > pq.size()) { - long max = pq.poll(); - Long prev = 2 * max - sum; - if (prev < 1) { - return false; - } - pq.offer(prev); - sum -= max; - sum += prev; - } - return sum == pq.size(); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_1356.java b/src/main/java/com/fishercoder/solutions/_1356.java deleted file mode 100644 index 79db451c61..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1356.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 1356. Sort Integers by The Number of 1 Bits - * - * Given an integer array arr. You have to sort the integers in the array in ascending order by the number of 1's in - * their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order. - * - * Return the sorted array. - * - * Example 1: - * Input: arr = [0,1,2,3,4,5,6,7,8] - * Output: [0,1,2,4,8,3,5,6,7] - * Explantion: [0] is the only integer with 0 bits. - * [1,2,4,8] all have 1 bit. - * [3,5,6] have 2 bits. - * [7] has 3 bits. - * The sorted array by bits is [0,1,2,4,8,3,5,6,7] - * - * Example 2: - * Input: arr = [1024,512,256,128,64,32,16,8,4,2,1] - * Output: [1,2,4,8,16,32,64,128,256,512,1024] - * Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order. - * - * Example 3: - * Input: arr = [10000,10000] - * Output: [10000,10000] - * - * Example 4: - * Input: arr = [2,3,5,7,11,13,17,19] - * Output: [2,3,5,17,7,11,13,19] - * - * Example 5: - * Input: arr = [10,100,1000,10000] - * Output: [10,100,10000,1000] - * - * Constraints: - * 1 <= arr.length <= 500 - * 0 <= arr[i] <= 10^4 - * */ -public class _1356 { - public static class Solution1 { - public int[] sortByBits(int[] arr) { - Map> map = new HashMap<>(); - for (int num : arr) { - int count = Integer.bitCount(num); - if (!map.containsKey(count)) { - map.put(count, new ArrayList<>()); - } - map.get(count).add(num); - } - int[] result = new int[arr.length]; - int i = 0; - for (int count : map.keySet()) { - List list = map.get(count); - Collections.sort(list); - for (int num : list) { - result[i++] = num; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1357.java b/src/main/java/com/fishercoder/solutions/_1357.java deleted file mode 100644 index 1d770452cd..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1357.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 1357. Apply Discount Every n Orders - * - * There is a sale in a supermarket, there will be a discount every n customer. - * There are some products in the supermarket where the id of the i-th product is products[i] and the price per unit of this product is prices[i]. - * The system will count the number of customers and when the n-th customer arrive he/she will have a discount on the bill. - * (i.e if the cost is x the new cost is x - (discount * x) / 100). Then the system will start counting customers again. - * The customer orders a certain amount of each product where product[i] is the id of the i-th product the customer ordered and - * amount[i] is the number of units the customer ordered of that product. - * - * Implement the Cashier class: - * Cashier(int n, int discount, int[] products, int[] prices) Initializes the object with n, the discount, the products and their prices. - * double getBill(int[] product, int[] amount) returns the value of the bill and apply the discount if needed. Answers within 10^-5 of the actual value will be accepted as correct. - * - * Example 1: - * Input - * ["Cashier","getBill","getBill","getBill","getBill","getBill","getBill","getBill"] - * [[3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]],[[1,2],[1,2]],[[3,7],[10,10]],[[1,2,3,4,5,6,7],[1,1,1,1,1,1,1]],[[4],[10]],[[7,3],[10,10]],[[7,5,3,1,6,4,2],[10,10,10,9,9,9,7]],[[2,3,5],[5,3,2]]] - * Output - * [null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0] - * Explanation - * Cashier cashier = new Cashier(3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]); - * cashier.getBill([1,2],[1,2]); // return 500.0, bill = 1 * 100 + 2 * 200 = 500. - * cashier.getBill([3,7],[10,10]); // return 4000.0 - * cashier.getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]); // return 800.0, The bill was 1600.0 but as this is the third customer, he has a discount of 50% which means his bill is only 1600 - 1600 * (50 / 100) = 800. - * cashier.getBill([4],[10]); // return 4000.0 - * cashier.getBill([7,3],[10,10]); // return 4000.0 - * cashier.getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]); // return 7350.0, Bill was 14700.0 but as the system counted three more customers, he will have a 50% discount and the bill becomes 7350.0 - * cashier.getBill([2,3,5],[5,3,2]); // return 2500.0 - * - * - * Constraints: - * 1 <= n <= 10^4 - * 0 <= discount <= 100 - * 1 <= products.length <= 200 - * 1 <= products[i] <= 200 - * There are not repeated elements in the array products. - * prices.length == products.length - * 1 <= prices[i] <= 1000 - * 1 <= product.length <= products.length - * product[i] exists in products. - * amount.length == product.length - * 1 <= amount[i] <= 1000 - * At most 1000 calls will be made to getBill. - * Answers within 10^-5 of the actual value will be accepted as correct. - * */ -public class _1357 { - public static class Solution1 { - public static class Cashier { - - int customerCount; - int n; - Map productsToPrices; - int discount; - - public Cashier(int n, int discount, int[] products, int[] prices) { - this.n = n; - this.customerCount = 0; - this.discount = discount; - this.productsToPrices = new HashMap<>(); - for (int i = 0; i < products.length; i++) { - productsToPrices.put(products[i], prices[i]); - } - } - - public double getBill(int[] product, int[] amount) { - double totalPrice = 0; - for (int i = 0; i < product.length; i++) { - totalPrice += productsToPrices.get(product[i]) * amount[i]; - } - if (customerCount + 1 == n) { - //apply discount - totalPrice *= (double) (100 - discount) / 100; - customerCount = 0; - } else { - customerCount++; - } - return totalPrice; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1358.java b/src/main/java/com/fishercoder/solutions/_1358.java deleted file mode 100644 index 57f38fe821..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1358.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1358. Number of Substrings Containing All Three Characters - * - * Given a string s consisting only of characters a, b and c. - * Return the number of substrings containing at least one occurrence of all these characters a, b and c. - * - * Example 1: - * Input: s = "abcabc" - * Output: 10 - * Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again). - * - * Example 2: - * Input: s = "aaacb" - * Output: 3 - * Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb". - * - * Example 3: - * Input: s = "abc" - * Output: 1 - * - * Constraints: - * 3 <= s.length <= 5 x 10^4 - * s only consists of a, b or c characters. - * */ -public class _1358 { - public static class Solution1 { - /**A classic sliding window problem, no dp or backtracking, just sliding window: use two pointers. - * my new favorite queustion! - * */ - public int numberOfSubstrings(String s) { - int[] counts = new int[3]; - int i = 0; - int n = s.length(); - int result = 0; - for (int j = 0; j < n; j++) { - counts[s.charAt(j) - 'a']++; - while (counts[0] > 0 && counts[1] > 0 && counts[2] > 0) { - counts[s.charAt(i++) - 'a']--; - } - result += i; - } - return result; - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_136.java b/src/main/java/com/fishercoder/solutions/_136.java deleted file mode 100644 index d661b23870..0000000000 --- a/src/main/java/com/fishercoder/solutions/_136.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - -/**136. Single Number - - Given a non-empty array of integers, every element appears twice except for one. Find that single one. - - Note: - Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? - - Example 1: - Input: [2,2,1] - Output: 1 - - Example 2: - Input: [4,1,2,1,2] - Output: 4 -*/ - -public class _136 { - - public static class Solution1 { - /** - * Approach 1: use set, since this problem explicitly states that every element appears twice - * and only one appears once so, we could safely remove the ones that are already in the set, - * O(n) time and O(n) space. HashTable approach works similarly like this one, but it could be - * more easily extend to follow-up questions. - */ - public int singleNumber(int[] nums) { - Set set = new HashSet(); - for (int i : nums) { - if (!set.add(i)) { - set.remove(i); - } - } - return set.iterator().next(); - } - } - - public static class Solution2 { - /** - * Approach 2: bit manipulation, use exclusive or ^ to solve this problem: we're using the trick - * here: every number ^ itself will become zero, so, the only remaining element will be the one - * that appeared only once. - */ - public int singleNumber(int[] nums) { - int res = 0; - for (int i : nums) { - res ^= i; - } - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1360.java b/src/main/java/com/fishercoder/solutions/_1360.java deleted file mode 100644 index c6d0933049..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1360.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1360. Number of Days Between Two Dates - * - * Write a program to count the number of days between two dates. - * The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples. - * - * Example 1: - * Input: date1 = "2019-06-29", date2 = "2019-06-30" - * Output: 1 - * - * Example 2: - * Input: date1 = "2020-01-15", date2 = "2019-12-31" - * Output: 15 - * - * Constraints: - * The given dates are valid dates between the years 1971 and 2100. - * */ -public class _1360 { - public static class Solution1 { - public int daysBetweenDates(String date1, String date2) { - String[] strings1 = date1.split("-"); - String[] strings2 = date2.split("-"); - return Math.abs(julianDay(Integer.parseInt(strings1[0]), Integer.parseInt(strings1[1]), Integer.parseInt(strings1[2])) - - julianDay(Integer.parseInt(strings2[0]), Integer.parseInt(strings2[1]), Integer.parseInt(strings2[2]))); - } - - public int julianDay(int year, int month, int day) { - int a = (14 - month) / 12; - int y = year + 4800 - a; - int m = month + 12 * a - 3; - int jdn = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045; - return jdn; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1361.java b/src/main/java/com/fishercoder/solutions/_1361.java deleted file mode 100644 index b6ee6d0a5c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1361.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 1361. Validate Binary Tree Nodes - * - * You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], - * return true if and only if all the given nodes form exactly one valid binary tree. - * If node i has no left child then leftChild[i] will equal -1, similarly for the right child. - * Note that the nodes have no values and that we only use the node numbers in this problem. - * - * Example 1: - * Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1] - * Output: true - * - * Example 2: - * Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1] - * Output: false - * - * Example 3: - * Input: n = 2, leftChild = [1,0], rightChild = [-1,-1] - * Output: false - * - * Example 4: - * Input: n = 6, leftChild = [1,-1,-1,4,-1,-1], rightChild = [2,-1,-1,5,-1,-1] - * Output: false - * - * Constraints: - * 1 <= n <= 10^4 - * leftChild.length == rightChild.length == n - * -1 <= leftChild[i], rightChild[i] <= n - 1 - * */ -public class _1361 { - public static class Solution1 { - public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) { - int[] indegree = new int[n]; - for (int i = 0; i < n; i++) { - if (leftChild[i] >= 0) { - indegree[leftChild[i]]++; - } - if (rightChild[i] >= 0) { - indegree[rightChild[i]]++; - } - } - //only one node has in-degree = 0 - int indegreeZero = 0; - for (int num : indegree) { - if (num == 0) { - indegreeZero++; - if (indegreeZero > 1) { - return false; - } - } - //every other node's in-degree must be exactly equal to 1 - if (num > 1) { - return false; - } - } - //no bi-directional pointing - Map> map = new HashMap<>(); - for (int i = 0; i < n; i++) { - map.put(i, new ArrayList<>()); - map.get(i).add(leftChild[i]); - map.get(i).add(rightChild[i]); - } - for (int node : map.keySet()) { - List children = map.get(node); - for (int child : children) { - if (map.containsKey(child) && map.get(child).contains(node)) { - return false; - } - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1362.java b/src/main/java/com/fishercoder/solutions/_1362.java deleted file mode 100644 index e67ade76c0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1362.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1362. Closest Divisors - * - * Given an integer num, find the closest two integers in absolute difference whose product equals num + 1 or num + 2. - * Return the two integers in any order. - * - * Example 1: - * Input: num = 8 - * Output: [3,3] - * Explanation: For num + 1 = 9, the closest divisors are 3 & 3, for num + 2 = 10, the closest divisors are 2 & 5, hence 3 & 3 is chosen. - * - * Example 2: - * Input: num = 123 - * Output: [5,25] - * - * Example 3: - * Input: num = 999 - * Output: [40,25] - * - * Constraints: - * 1 <= num <= 10^9 - * */ -public class _1362 { - public static class Solution1 { - public int[] closestDivisors(int num) { - int sqrt = (int) Math.sqrt(num); - int left = sqrt + 1; - int right = sqrt + 1; - long product = left * right; - while (product != (long) (num + 1) && product != (long) (num + 2)) { - if (product < (num + 1)) { - left++; - } else if (product > (num + 2)) { - right--; - } - product = left * right; - } - return new int[]{left, right}; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1365.java b/src/main/java/com/fishercoder/solutions/_1365.java deleted file mode 100644 index d496d64cf5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1365.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 1365. How Many Numbers Are Smaller Than the Current Number - * - * Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. - * That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i]. - * Return the answer in an array. - * - * Example 1: - * Input: nums = [8,1,2,2,3] - * Output: [4,0,1,1,3] - * Explanation: - * For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). - * For nums[1]=1 does not exist any smaller number than it. - * For nums[2]=2 there exist one smaller number than it (1). - * For nums[3]=2 there exist one smaller number than it (1). - * For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2). - * - * Example 2: - * Input: nums = [6,5,4,8] - * Output: [2,1,0,3] - * - * Example 3: - * Input: nums = [7,7,7,7] - * Output: [0,0,0,0] - * - * Constraints: - * 2 <= nums.length <= 500 - * 0 <= nums[i] <= 100 - * */ -public class _1365 { - public static class Solution1 { - public int[] smallerNumbersThanCurrent(int[] nums) { - int[] result = new int[nums.length]; - int[] tmp = Arrays.copyOf(nums, nums.length); - Arrays.sort(tmp); - List list = new ArrayList<>(); - for (int i : tmp) { - list.add(i); - } - for (int i = 0; i < nums.length; i++) { - result[i] = list.indexOf(nums[i]); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1366.java b/src/main/java/com/fishercoder/solutions/_1366.java deleted file mode 100644 index 8546b0b437..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1366.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.Comparator; - -/** - * 1366. Rank Teams by Votes - * - * In a special ranking system, each voter gives a rank from highest to lowest to all teams participated in the competition. - * The ordering of teams is decided by who received the most position-one votes. - * If two or more teams tie in the first position, we consider the second position to resolve the conflict, - * if they tie again, we continue this process until the ties are resolved. If two or more teams are still tied after considering all positions, - * we rank them alphabetically based on their team letter. - * Given an array of strings votes which is the votes of all voters in the ranking systems. Sort all teams according to the ranking system described above. - * Return a string of all teams sorted by the ranking system. - * - * Example 1: - * Input: votes = ["ABC","ACB","ABC","ACB","ACB"] - * Output: "ACB" - * Explanation: Team A was ranked first place by 5 voters. No other team was voted as first place so team A is the first team. - * Team B was ranked second by 2 voters and was ranked third by 3 voters. - * Team C was ranked second by 3 voters and was ranked third by 2 voters. - * As most of the voters ranked C second, team C is the second team and team B is the third. - * - * Example 2: - * Input: votes = ["WXYZ","XYZW"] - * Output: "XWYZ" - * Explanation: X is the winner due to tie-breaking rule. - * X has same votes as W for the first position but X has one vote as second position while W doesn't have any votes as second position. - * - * Example 3: - * Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"] - * Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK" - * Explanation: Only one voter so his votes are used for the ranking. - * - * Example 4: - * Input: votes = ["BCA","CAB","CBA","ABC","ACB","BAC"] - * Output: "ABC" - * Explanation: - * Team A was ranked first by 2 voters, second by 2 voters and third by 2 voters. - * Team B was ranked first by 2 voters, second by 2 voters and third by 2 voters. - * Team C was ranked first by 2 voters, second by 2 voters and third by 2 voters. - * There is a tie and we rank teams ascending by their IDs. - * - * Example 5: - * Input: votes = ["M","M","M","M"] - * Output: "M" - * Explanation: Only team M in the competition so it has the first rank. - * - * Constraints: - * 1 <= votes.length <= 1000 - * 1 <= votes[i].length <= 26 - * votes[i].length == votes[j].length for 0 <= i, j < votes.length. - * votes[i][j] is an English upper-case letter. - * All characters of votes[i] are unique. - * All the characters that occur in votes[0] also occur in votes[j] where 1 <= j < votes.length. - * */ -public class _1366 { - public static class Solution1 { - class Node { - int[] count = new int[26]; - char c; - - public Node(char c) { - this.c = c; - } - } - - public String rankTeams(String[] votes) { - Node[] nodes = new Node[26]; - for (int i = 0; i < 26; i++) { - nodes[i] = new Node((char) (i + 'A')); - } - for (String vote : votes) { - for (int i = 0; i < vote.length(); i++) { - nodes[vote.charAt(i) - 'A'].count[i]++; - } - } - Arrays.sort(nodes, new Comparator() { - @Override - public int compare(Node o1, Node o2) { - for (int i = 0; i < 26; i++) { - if (o1.count[i] != o2.count[i]) { - return o2.count[i] - o1.count[i]; - } - } - return o1.c - o2.c; - } - }); - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < votes[0].length(); i++) { - sb.append(nodes[i].c); - } - return sb.toString(); - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1367.java b/src/main/java/com/fishercoder/solutions/_1367.java deleted file mode 100644 index ec3c079f3a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1367.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1367. Linked List in Binary Tree - * - * Given a binary tree root and a linked list with head as the first node. - * Return True if all the elements in the linked list starting from the head correspond to some downward path - * connected in the binary tree otherwise return False. - * In this context downward path means a path that starts at some node and goes downwards. - * - * Example 1: - * Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] - * Output: true - * Explanation: Nodes in blue form a subpath in the binary Tree. - * - * Example 2: - * Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] - * Output: true - * - * Example 3: - * Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] - * Output: false - * Explanation: There is no path in the binary tree that contains all the elements of the linked list from head. - * - * Constraints: - * 1 <= node.val <= 100 for each node in the linked list and binary tree. - * The given linked list will contain between 1 and 100 nodes. - * The given binary tree will contain between 1 and 2500 nodes. - * */ -public class _1367 { - public static class Solution1 { - List> paths = new ArrayList<>(); - - public boolean isSubPath(ListNode head, TreeNode root) { - List list = getList(head); - findAllPaths(root, new ArrayList<>()); - for (List path : paths) { - if (path.size() >= list.size()) { - if (find(list, path)) { - return true; - } - } - } - return false; - } - - private boolean find(List list, List path) { - int i = 0; - int j = 0; - for (; i <= path.size() - list.size(); i++) { - j = 0; - int tmpI = i; - while (j < list.size() && tmpI < path.size() && list.get(j) == path.get(tmpI)) { - tmpI++; - j++; - } - if (j >= list.size()) { - return true; - } - } - return j >= list.size(); - } - - private void findAllPaths(TreeNode root, List path) { - if (root == null) { - return; - } - path.add(root.val); - if (root.left == null && root.right == null) { - paths.add(new ArrayList<>(path)); - path.remove(path.size() - 1); - return; - } - findAllPaths(root.left, path); - findAllPaths(root.right, path); - path.remove(path.size() - 1); - } - - private List getList(ListNode head) { - List list = new ArrayList<>(); - while (head != null) { - list.add(head.val); - head = head.next; - } - return list; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_137.java b/src/main/java/com/fishercoder/solutions/_137.java deleted file mode 100644 index f88c2e600e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_137.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 137. Single Number II - -Given an array of integers, every element appears three times except for one. Find that single one. - -Note: -Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? -*/ -public class _137 { - - public static class Solution1 { - public int singleNumber(int[] nums) { - Map map = new HashMap(); - for (int i : nums) { - map.put(i, map.getOrDefault(i, 0) + 1); - } - for (int key : map.keySet()) { - if (map.get(key) != 3) { - return key; - } - } - return 0; - } - } - - public static class Solution2 { - /** Credit: https://discuss.leetcode.com/topic/11877/detailed-explanation-and-generalization-of-the-bitwise-operation-method-for-single-numbers/2 */ - public int singleNumber(int[] nums) { - int counter1 = 0; - int counter2 = 0; - int mask = 0; - for (int num : nums) { - counter2 ^= counter1 & num; - counter1 ^= num; - mask = ~(counter1 & counter2); - counter1 &= mask; - counter2 &= mask; - } - return counter1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1370.java b/src/main/java/com/fishercoder/solutions/_1370.java deleted file mode 100644 index f4ec7be0c3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1370.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1370. Increasing Decreasing String - * - * Given a string s. You should re-order the string using the following algorithm: - * Pick the smallest character from s and append it to the result. - * Pick the smallest character from s which is greater than the last appended character to the result and append it. - * Repeat step 2 until you cannot pick more characters. - * Pick the largest character from s and append it to the result. - * Pick the largest character from s which is smaller than the last appended character to the result and append it. - * Repeat step 5 until you cannot pick more characters. - * Repeat the steps from 1 to 6 until you pick all characters from s. - * In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result. - * Return the result string after sorting s with this algorithm. - * - * Example 1: - * Input: s = "aaaabbbbcccc" - * Output: "abccbaabccba" - * Explanation: After steps 1, 2 and 3 of the first iteration, result = "abc" - * After steps 4, 5 and 6 of the first iteration, result = "abccba" - * First iteration is done. Now s = "aabbcc" and we go back to step 1 - * After steps 1, 2 and 3 of the second iteration, result = "abccbaabc" - * After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba" - * - * Example 2: - * Input: s = "rat" - * Output: "art" - * Explanation: The word "rat" becomes "art" after re-ordering it with the mentioned algorithm. - * - * Example 3: - * Input: s = "leetcode" - * Output: "cdelotee" - * - * Example 4: - * Input: s = "ggggggg" - * Output: "ggggggg" - * - * Example 5: - * Input: s = "spo" - * Output: "ops" - * - * Constraints: - * 1 <= s.length <= 500 - * s contains only lower-case English letters. - * */ -public class _1370 { - public static class Solution1 { - public String sortString(String s) { - int[] count = new int[26]; - for (char c : s.toCharArray()) { - count[c - 'a']++; - } - StringBuilder sb = new StringBuilder(); - while (sb.length() < s.length()) { - for (int i = 0; i < count.length; i++) { - if (count[i] != 0) { - char character = (char) (i + 'a'); - sb.append(character); - count[i]--; - } - } - for (int i = 25; i >= 0; i--) { - if (count[i] > 0) { - char character = (char) (i + 'a'); - sb.append(character); - count[i]--; - } - } - } - return sb.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1371.java b/src/main/java/com/fishercoder/solutions/_1371.java deleted file mode 100644 index 0c9fc84a73..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1371.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 1371. Find the Longest Substring Containing Vowels in Even Counts - * - * Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times. - * - * Example 1: - * Input: s = "eleetminicoworoep" - * Output: 13 - * Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u. - * - * Example 2: - * Input: s = "leetcodeisgreat" - * Output: 5 - * Explanation: The longest substring is "leetc" which contains two e's. - * - * Example 3: - * Input: s = "bcbcbc" - * Output: 6 - * Explanation: In this case, the given string "bcbcbc" is the longest because all vowels: a, e, i, o and u appear zero times. - * - * Constraints: - * 1 <= s.length <= 5 x 10^5 - * s contains only lowercase English letters. - * */ -public class _1371 { - public static class Solution1 { - public int findTheLongestSubstring(String s) { - int max = 0; - for (int i = 0; i < s.length(); i++) { - Map map = setupMap(); - if (s.length() - i < max) { - return max; - } - for (int j = i; j < s.length(); j++) { - Character b = s.charAt(j); - if (map.containsKey(b)) { - map.put(b, map.get(b) + 1); - } - if (allEven(map)) { - max = Math.max(max, j - i + 1); - } - } - } - return max; - } - - private Map setupMap() { - Map map = new HashMap<>(); - map.put('a', 0); - map.put('e', 0); - map.put('i', 0); - map.put('o', 0); - map.put('u', 0); - return map; - } - - private boolean allEven(Map map) { - for (char c : map.keySet()) { - if (map.get(c) % 2 != 0) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1372.java b/src/main/java/com/fishercoder/solutions/_1372.java deleted file mode 100644 index f222c24b61..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1372.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.sql.Struct; - -/** - * 1372. Longest ZigZag Path in a Binary Tree - * - * Given a binary tree root, a ZigZag path for a binary tree is defined as follow: - * Choose any node in the binary tree and a direction (right or left). - * If the current direction is right then move to the right child of the current node otherwise move to the left child. - * Change the direction from right to left or right to left. - * Repeat the second and third step until you can't move in the tree. - * Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). - * Return the longest ZigZag path contained in that tree. - * - * Example 1: - * Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] - * Output: 3 - * Explanation: Longest ZigZag path in blue nodes (right -> left -> right). - * - * Example 2: - * Input: root = [1,1,1,null,1,null,null,1,1,null,1] - * Output: 4 - * Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). - * - * Example 3: - * Input: root = [1] - * Output: 0 - * - * Constraints: - * Each tree has at most 50000 nodes.. - * Each node's value is between [1, 100]. - * */ -public class _1372 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/discuss/531808/Java-Recursion-Try-each-node-as-a-zigzag-root-then-return-valid-sum-to-parent*/ - int maxLength = 0; - - public int longestZigZag(TreeNode root) { - dfs(root, true); - return maxLength; - } - - private int dfs(TreeNode root, boolean isLeft) { - if (root == null) { - return 0; - } - int left = dfs(root.left, false); - int right = dfs(root.right, true); - maxLength = Math.max(maxLength, left); - maxLength = Math.max(maxLength, right); - return 1 + (isLeft ? left : right); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1373.java b/src/main/java/com/fishercoder/solutions/_1373.java deleted file mode 100644 index 98ff4d6426..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1373.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 1373. Maximum Sum BST in Binary Tree - * - * Given a binary tree root, the task is to return the maximum sum of all keys of any sub-tree which is also a Binary Search Tree (BST). - * Assume a BST is defined as follows: - * The left subtree of a node contains only nodes with keys less than the node's key. - * The right subtree of a node contains only nodes with keys greater than the node's key. - * Both the left and right subtrees must also be binary search trees. - * - * Example 1: - * Input: root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6] - * Output: 20 - * Explanation: Maximum sum in a valid Binary search tree is obtained in root node with key equal to 3. - * - * Example 2: - * Input: root = [4,3,null,1,2] - * Output: 2 - * Explanation: Maximum sum in a valid Binary search tree is obtained in a single root node with key equal to 2. - * - * Example 3: - * Input: root = [-4,-2,-5] - * Output: 0 - * Explanation: All values are negatives. Return an empty BST. - * - * Example 4: - * Input: root = [2,1,3] - * Output: 6 - * - * Example 5: - * Input: root = [5,4,8,3,null,6,3] - * Output: 7 - * - * Constraints: - * Each tree has at most 40000 nodes.. - * Each node's value is between [-4 * 10^4 , 4 * 10^4]. - * */ -public class _1373 { - public static class Solution1 { - /** - * credit: https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/discuss/532021/Java-Post-Order - * */ - public int maxSumBST(TreeNode root) { - return postOrder(root)[4]; - } - - /** - * result[0] means this tree is a BST - * result[1] means the sum of this tree - * result[2] means the left boundary - * result[3] means the right boundary - * result[4] means the global max sum - * */ - private int[] postOrder(TreeNode root) { - if (root == null) { - return new int[]{1, 0, Integer.MAX_VALUE, Integer.MIN_VALUE, 0}; - } - int[] leftSide = postOrder(root.left); - int[] rightSide = postOrder(root.right); - int localMax = Math.max(leftSide[4], rightSide[4]); - if (leftSide[0] == 1 && rightSide[0] == 1 && root.val > leftSide[3] && root.val < rightSide[2]) { - int sum = root.val + leftSide[1] + rightSide[1]; - return new int[]{1, sum, leftSide[2] == Integer.MAX_VALUE ? root.val : leftSide[2], rightSide[3] == Integer.MIN_VALUE ? root.val : rightSide[3], Math.max(localMax, sum)}; - } else { - return new int[]{0, 0, 0, 0, localMax}; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1374.java b/src/main/java/com/fishercoder/solutions/_1374.java deleted file mode 100644 index 758219cfd2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1374.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1374. Generate a String With Characters That Have Odd Counts - * - * Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times. - * The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them. - * - * Example 1: - * Input: n = 4 - * Output: "pppz" - * Explanation: "pppz" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love". - * - * Example 2: - * Input: n = 2 - * Output: "xy" - * Explanation: "xy" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as "ag" and "ur". - * - * Example 3: - * Input: n = 7 - * Output: "holasss" - * - * Constraints: - * 1 <= n <= 500 - * */ -public class _1374 { - public static class Solution1 { - public String generateTheString(int n) { - StringBuilder sb = new StringBuilder(); - if (n > 1 && n % 2 == 0) { - while (n-- > 1) { - sb.append("a"); - } - } else if (n > 1) { - while (n-- > 2) { - sb.append("a"); - } - sb.append("b"); - } - sb.append("z"); - return sb.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1375.java b/src/main/java/com/fishercoder/solutions/_1375.java deleted file mode 100644 index 1be0690c6f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1375.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1375. Bulb Switcher III - * - * There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. - * At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. - * Return the number of moments in which all turned on bulbs are blue. - * - * Example 1: - * Input: light = [2,1,3,5,4] - * Output: 3 - * Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. - * - * Example 2: - * Input: light = [3,2,4,1,5] - * Output: 2 - * Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). - * - * Example 3: - * Input: light = [4,1,2,3] - * Output: 1 - * Explanation: All bulbs turned on, are blue at the moment 3 (index-0). - * Bulb 4th changes to blue at the moment 3. - * - * Example 4: - * Input: light = [2,1,4,3,6,5] - * Output: 3 - * - * Example 5: - * Input: light = [1,2,3,4,5,6] - * Output: 6 - * - * Constraints: - * n == light.length - * 1 <= n <= 5 * 10^4 - * light is a permutation of [1, 2, ..., n] - * */ -public class _1375 { - public static class Solution1 { - public int numTimesAllBlue(int[] light) { - int blues = 0; - int[] status = new int[light.length];//0 means off, 1 means on - for (int i = 0; i < light.length; i++) { - status[light[i] - 1] = 1; - if (checkAllBlues(status, i)) { - blues++; - } - } - return blues; - } - - private boolean checkAllBlues(int[] status, int endIndex) { - for (int i = 0; i <= endIndex; i++) { - if (status[i] != 1) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1376.java b/src/main/java/com/fishercoder/solutions/_1376.java deleted file mode 100644 index 6639f98a79..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1376.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * 1376. Time Needed to Inform All Employees - * - * A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company has is the one with headID. - * Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also it's guaranteed that the subordination relationships have a tree structure. - * The head of the company wants to inform all the employees of the company of an urgent piece of news. He will inform his direct subordinates and they will inform their subordinates and so on until all employees know about the urgent news. - * The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e After informTime[i] minutes, all his direct subordinates can start spreading the news). - * Return the number of minutes needed to inform all the employees about the urgent news. - * - * Example 1: - * Input: n = 1, headID = 0, manager = [-1], informTime = [0] - * Output: 0 - * Explanation: The head of the company is the only employee in the company. - * - * Example 2: - * Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0] - * Output: 1 - * Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all. - * The tree structure of the employees in the company is shown. - * - * Example 3: - * Input: n = 7, headID = 6, manager = [1,2,3,4,5,6,-1], informTime = [0,6,5,4,3,2,1] - * Output: 21 - * Explanation: The head has id = 6. He will inform employee with id = 5 in 1 minute. - * The employee with id = 5 will inform the employee with id = 4 in 2 minutes. - * The employee with id = 4 will inform the employee with id = 3 in 3 minutes. - * The employee with id = 3 will inform the employee with id = 2 in 4 minutes. - * The employee with id = 2 will inform the employee with id = 1 in 5 minutes. - * The employee with id = 1 will inform the employee with id = 0 in 6 minutes. - * Needed time = 1 + 2 + 3 + 4 + 5 + 6 = 21. - * - * Example 4: - * Input: n = 15, headID = 0, manager = [-1,0,0,1,1,2,2,3,3,4,4,5,5,6,6], informTime = [1,1,1,1,1,1,1,0,0,0,0,0,0,0,0] - * Output: 3 - * Explanation: The first minute the head will inform employees 1 and 2. - * The second minute they will inform employees 3, 4, 5 and 6. - * The third minute they will inform the rest of employees. - * - * Example 5: - * Input: n = 4, headID = 2, manager = [3,3,-1,2], informTime = [0,0,162,914] - * Output: 1076 - * - * Constraints: - * 1 <= n <= 10^5 - * 0 <= headID < n - * manager.length == n - * 0 <= manager[i] < n - * manager[headID] == -1 - * informTime.length == n - * 0 <= informTime[i] <= 1000 - * informTime[i] == 0 if employee i has no subordinates. - * It is guaranteed that all the employees can be informed. - * */ -public class _1376 { - public static class Solution1 { - int maxMinutes = 0; - - public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) { - boolean[] visited = new boolean[manager.length]; - Set managerIdSet = new HashSet<>(); - for (int id : manager) { - managerIdSet.add(id); - } - Set visitedEmployees = new HashSet<>(); - for (int i = 0; i < n; i++) { - visitedEmployees.add(i); - } - - Map> map = new HashMap<>(); - for (int i = 0; i < manager.length; i++) { - if (!map.containsKey(manager[i])) { - map.put(manager[i], new ArrayList<>()); - } - map.get(manager[i]).add(i); - } - backtracking(visited, headID, 0, informTime, managerIdSet, visitedEmployees, map); - return maxMinutes; - } - - private void backtracking(boolean[] visited, int managerId, int currentMinutes, int[] informTime, Set managerIdSet, Set visitedEmployees, Map> map) { - if (visitedEmployees.contains(managerId)) { - visitedEmployees.remove(managerId); - } - if (!managerIdSet.contains(managerId)) { - maxMinutes = Math.max(currentMinutes, maxMinutes); - return; - } - if (visitedEmployees.isEmpty()) { - return; - } - visited[managerId] = true; - if (map.containsKey(managerId)) { - List suboridnates = map.get(managerId); - for (int subordinate : suboridnates) { - if (!visited[subordinate]) { - backtracking(visited, subordinate, currentMinutes + informTime[managerId], informTime, managerIdSet, visitedEmployees, map); - } - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1377.java b/src/main/java/com/fishercoder/solutions/_1377.java deleted file mode 100644 index e12198487a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1377.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; - -/** - * 1377. Frog Position After T Seconds - * - * Given an undirected tree consisting of n vertices numbered from 1 to n. - * A frog starts jumping from the vertex 1. - * In one second, the frog jumps from its current vertex to another unvisited vertex if they are directly connected. - * The frog can not jump back to a visited vertex. - * In case the frog can jump to several vertices it jumps randomly to one of them with the same probability, - * otherwise, when the frog can not jump to any unvisited vertex it jumps forever on the same vertex. - * The edges of the undirected tree are given in the array edges, where edges[i] = [fromi, toi] means that exists an edge connecting directly the vertices fromi and toi. - * Return the probability that after t seconds the frog is on the vertex target. - * - * Example 1: - * Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4 - * Output: 0.16666666666666666 - * Explanation: The figure above shows the given graph. - * The frog starts at vertex 1, jumping with 1/3 probability to the vertex 2 after second 1 and - * then jumping with 1/2 probability to vertex 4 after second 2. - * Thus the probability for the frog is on the vertex 4 after 2 seconds is 1/3 * 1/2 = 1/6 = 0.16666666666666666. - * - * Example 2: - * Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 1, target = 7 - * Output: 0.3333333333333333 - * Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 = 0.3333333333333333 probability to the vertex 7 after second 1. - * - * Example 3: - * Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 20, target = 6 - * Output: 0.16666666666666666 - * - * Constraints: - * 1 <= n <= 100 - * edges.length == n-1 - * edges[i].length == 2 - * 1 <= edges[i][0], edges[i][1] <= n - * 1 <= t <= 50 - * 1 <= target <= n - * Answers within 10^-5 of the actual value will be accepted as correct. - * */ -public class _1377 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/frog-position-after-t-seconds/discuss/532505/Java-Straightforward-BFS-Clean-code-O(N)*/ - public double frogPosition(int n, int[][] edges, int t, int target) { - List[] graph = new ArrayList[n]; - for (int i = 0; i < n; i++) { - graph[i] = new ArrayList<>(); - } - for (int[] edge : edges) { - graph[edge[0] - 1].add(edge[1] - 1); - graph[edge[1] - 1].add(edge[0] - 1); - } - boolean[] visited = new boolean[n]; - visited[0] = true; - double[] probabilities = new double[n]; - probabilities[0] = 1f; - Queue queue = new LinkedList<>(); - queue.offer(0); - while (!queue.isEmpty() && t-- > 0) { - for (int i = queue.size(); i > 0; i--) { - int vertex = queue.poll(); - int nextVerticesCount = 0; - for (int next : graph[vertex]) { - if (!visited[next]) { - nextVerticesCount++; - } - } - for (int next : graph[vertex]) { - if (!visited[next]) { - visited[next] = true; - queue.offer(next); - probabilities[next] = probabilities[vertex] / nextVerticesCount; - } - } - if (nextVerticesCount > 0) { - probabilities[vertex] = 0; - } - } - } - return probabilities[target - 1]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1379.java b/src/main/java/com/fishercoder/solutions/_1379.java deleted file mode 100644 index b3a640d92a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1379.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree - * - * Given two binary trees original and cloned and given a reference to a node target in the original tree. - * The cloned tree is a copy of the original tree. - * Return a reference to the same node in the cloned tree. - * Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree. - * - * Follow up: Solve the problem if repeated values on the tree are allowed. - * - * Example 1: - * Input: tree = [7,4,3,null,null,6,19], target = 3 - * Output: 3 - * Explanation: In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree. - * - * Example 2: - * Input: tree = [7], target = 7 - * Output: 7 - * - * Example 3: - * Input: tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4 - * Output: 4 - * - * Example 4: - * Input: tree = [1,2,3,4,5,6,7,8,9,10], target = 5 - * Output: 5 - * - * Example 5: - * Input: tree = [1,2,null,3], target = 2 - * Output: 2 - * - * Constraints: - * The number of nodes in the tree is in the range [1, 10^4]. - * The values of the nodes of the tree are unique. - * target node is a node from the original tree and is not null. - * */ -public class _1379 { - public static class Solution1 { - public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) { - if (original == null) { - return null; - } - if (original.val == target.val) { - return cloned; - } - TreeNode left = getTargetCopy(original.left, cloned.left, target); - if (left != null && left.val == target.val) { - return left; - } - return getTargetCopy(original.right, cloned.right, target); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_138.java b/src/main/java/com/fishercoder/solutions/_138.java deleted file mode 100644 index c5be14f25d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_138.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 138. Copy List with Random Pointer - * - * A linked list is given such that each node contains an additional random - * pointer which could point to any node in the list or null. - * Return a deep copy of the list. - */ - -public class _138 { - public static class Solution1 { - public Node copyRandomList(Node head) { - /**Key is the original nodes, value is the new nodes we're deep copying to.*/ - Map map = new HashMap(); - Node node = head; - - //loop for the first time: copy the node themselves with only labels - while (node != null) { - map.put(node, new Node(node.val)); - node = node.next; - } - - //loop for the second time: copy random and next pointers - node = head; - while (node != null) { - map.get(node).next = map.get(node.next); - map.get(node).random = map.get(node.random); - node = node.next; - } - - return map.get(head); - } - - // Definition for singly-linked list with a random pointer. - class Node { - int val; - - Node next; - Node random; - - Node(int x) { - this.val = x; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1380.java b/src/main/java/com/fishercoder/solutions/_1380.java deleted file mode 100644 index bd33b030f8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1380.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1380. Lucky Numbers in a Matrix - * - * Given a m * n matrix of distinct numbers, return all lucky numbers in the matrix in any order. - * A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column. - * - * Example 1: - * Input: matrix = [[3,7,8],[9,11,13],[15,16,17]] - * Output: [15] - * Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column - * - * Example 2: - * Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]] - * Output: [12] - * Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column. - * - * Example 3: - * Input: matrix = [[7,8],[1,2]] - * Output: [7] - * - * Constraints: - * m == mat.length - * n == mat[i].length - * 1 <= n, m <= 50 - * 1 <= matrix[i][j] <= 10^5. - * All elements in the matrix are distinct. - * */ -public class _1380 { - public static class Solution1 { - public List luckyNumbers(int[][] matrix) { - List result = new ArrayList<>(); - for (int i = 0; i < matrix.length; i++) { - for (int j = 0; j < matrix[0].length; j++) { - if (luckyInRow(matrix[i][j], matrix[i])) { - if (luckyInColumn(matrix[i][j], matrix, j)) { - result.add(matrix[i][j]); - } - } - } - } - return result; - } - - private boolean luckyInColumn(int number, int[][] matrix, int columnIndex) { - for (int i = 0; i < matrix.length; i++) { - if (number < matrix[i][columnIndex]) { - return false; - } - } - return true; - } - - private boolean luckyInRow(int number, int[] row) { - for (int num : row) { - if (number > num) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1381.java b/src/main/java/com/fishercoder/solutions/_1381.java deleted file mode 100644 index 61024c2e8a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1381.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1381. Design a Stack With Increment Operation - * - * Design a stack which supports the following operations. - * - * Implement the CustomStack class: - * - * CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number of elements in the stack or do nothing if the stack reached the maxSize. - * void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize. - * int pop() Pops and returns the top of stack or -1 if the stack is empty. - * void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack. - * - * Example 1: - * - * Input - * ["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"] - * [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]] - * Output - * [null,null,null,2,null,null,null,null,null,103,202,201,-1] - * Explanation - * CustomStack customStack = new CustomStack(3); // Stack is Empty [] - * customStack.push(1); // stack becomes [1] - * customStack.push(2); // stack becomes [1, 2] - * customStack.pop(); // return 2 --> Return top of the stack 2, stack becomes [1] - * customStack.push(2); // stack becomes [1, 2] - * customStack.push(3); // stack becomes [1, 2, 3] - * customStack.push(4); // stack still [1, 2, 3], Don't add another elements as size is 4 - * customStack.increment(5, 100); // stack becomes [101, 102, 103] - * customStack.increment(2, 100); // stack becomes [201, 202, 103] - * customStack.pop(); // return 103 --> Return top of the stack 103, stack becomes [201, 202] - * customStack.pop(); // return 202 --> Return top of the stack 102, stack becomes [201] - * customStack.pop(); // return 201 --> Return top of the stack 101, stack becomes [] - * customStack.pop(); // return -1 --> Stack is empty return -1. - * - * Constraints: - * 1 <= maxSize <= 1000 - * 1 <= x <= 1000 - * 1 <= k <= 1000 - * 0 <= val <= 100 - * At most 1000 calls will be made to each method of increment, push and pop each separately. - * */ -public class _1381 { - public static class Solution1 { - public static class CustomStack { - - List list; - int maxSize; - - public CustomStack(int maxSize) { - this.list = new ArrayList<>(); - this.maxSize = maxSize; - } - - public void push(int x) { - if (list.size() >= maxSize) { - return; - } else { - list.add(x); - } - } - - public int pop() { - if (!list.isEmpty()) { - return list.remove(list.size() - 1); - } else { - return -1; - } - } - - public void increment(int k, int val) { - for (int i = 0; i < k && i < list.size(); i++) { - list.set(i, list.get(i) + val); - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1382.java b/src/main/java/com/fishercoder/solutions/_1382.java deleted file mode 100644 index 57414f5eff..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1382.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1382. Balance a Binary Search Tree - * - * Given a binary search tree, return a balanced binary search tree with the same node values. - * A binary search tree is balanced if and only if the depth of the two subtrees of every node never differ by more than 1. - * If there is more than one answer, return any of them. - * - * Example 1: - * Input: root = [1,null,2,null,3,null,4,null,null] - * Output: [2,1,3,null,null,null,4] - * Explanation: This is not the only correct answer, [3,1,4,null,2,null,null] is also correct. - * - * Constraints: - * The number of nodes in the tree is between 1 and 10^4. - * The tree nodes will have distinct values between 1 and 10^5. - * */ -public class _1382 { - public static class Solution1 { - public TreeNode balanceBST(TreeNode root) { - List inorder = inorder(root, new ArrayList<>()); - return dfs(inorder, 0, inorder.size() - 1); - } - - private List inorder(TreeNode root, List list) { - if (root == null) { - return list; - } - inorder(root.left, list); - list.add(root.val); - return inorder(root.right, list); - } - - private TreeNode dfs(List nums, int start, int end) { - if (end < start) { - return null; - } - int mid = (start + end) / 2; - TreeNode root = new TreeNode(nums.get(mid)); - root.left = dfs(nums, start, mid - 1); - root.right = dfs(nums, mid + 1, end); - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1385.java b/src/main/java/com/fishercoder/solutions/_1385.java deleted file mode 100644 index e77dfcab60..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1385.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1385. Find the Distance Value Between Two Arrays - * - * Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays. - * The distance value is defined as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= d. - * - * Example 1: - * Input: arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2 - * Output: 2 - * Explanation: - * For arr1[0]=4 we have: - * |4-10|=6 > d=2 - * |4-9|=5 > d=2 - * |4-1|=3 > d=2 - * |4-8|=4 > d=2 - * For arr1[1]=5 we have: - * |5-10|=5 > d=2 - * |5-9|=4 > d=2 - * |5-1|=4 > d=2 - * |5-8|=3 > d=2 - * For arr1[2]=8 we have: - * |8-10|=2 <= d=2 - * |8-9|=1 <= d=2 - * |8-1|=7 > d=2 - * |8-8|=0 <= d=2 - * - * Example 2: - * Input: arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3 - * Output: 2 - * - * Example 3: - * Input: arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6 - * Output: 1 - * - * Constraints: - * 1 <= arr1.length, arr2.length <= 500 - * -10^3 <= arr1[i], arr2[j] <= 10^3 - * 0 <= d <= 100 - * */ -public class _1385 { - public static class Solution1 { - public int findTheDistanceValue(int[] arr1, int[] arr2, int d) { - int count = 0; - for (int i = 0; i < arr1.length; i++) { - int j = 0; - for (; j < arr2.length; j++) { - if (Math.abs(arr1[i] - arr2[j]) <= d) { - break; - } - } - if (j == arr2.length) { - count++; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1386.java b/src/main/java/com/fishercoder/solutions/_1386.java deleted file mode 100644 index 23b786869a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1386.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 1386. Cinema Seat Allocation - * - * A cinema has n rows of seats, numbered from 1 to n and there are ten seats in each row, labelled from 1 to 10 as shown in the figure above. - * Given the array reservedSeats containing the numbers of seats already reserved, for example, reservedSeats[i]=[3,8] means the seat located in row 3 and labelled with 8 is already reserved. - * Return the maximum number of four-person families you can allocate on the cinema seats. A four-person family occupies fours seats in one row, that are next to each other. - * Seats across an aisle (such as [3,3] and [3,4]) are not considered to be next to each other, however, It is permissible for the four-person family to be separated by an aisle, but in that case, exactly two people have to sit on each side of the aisle. - * - * Example 1: - * Input: n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]] - * Output: 4 - * Explanation: The figure above shows the optimal allocation for four families, where seats mark with blue are already reserved and contiguous seats mark with orange are for one family. - * - * Example 2: - * Input: n = 2, reservedSeats = [[2,1],[1,8],[2,6]] - * Output: 2 - * - * Example 3: - * Input: n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]] - * Output: 4 - * - * Constraints: - * 1 <= n <= 10^9 - * 1 <= reservedSeats.length <= min(10*n, 10^4) - * reservedSeats[i].length == 2 - * 1 <= reservedSeats[i][0] <= n - * 1 <= reservedSeats[i][1] <= 10 - * All reservedSeats[i] are distinct. - * */ -public class _1386 { - public static class Solution1 { - public int maxNumberOfFamilies(int n, int[][] reservedSeats) { - Map> map = new HashMap<>(); - for (int[] seat : reservedSeats) { - if (!map.containsKey(seat[0])) { - map.put(seat[0], new HashSet<>()); - } - map.get(seat[0]).add(seat[1]); - } - int count = (n - map.size()) * 2; - for (int key : map.keySet()) { - Set reservedOnes = map.get(key); - if (reservedOnes.size() > 6) { - continue; - } - if (!reservedOnes.contains(2) && !reservedOnes.contains(3) && !reservedOnes.contains(4) && !reservedOnes.contains(5) && !reservedOnes.contains(6) && !reservedOnes.contains(7) && !reservedOnes.contains(8) && !reservedOnes.contains(9)) { - count += 2; - } else if (!reservedOnes.contains(4) && !reservedOnes.contains(5) && !reservedOnes.contains(6) && !reservedOnes.contains(7)) { - count++; - } else if (!reservedOnes.contains(2) && !reservedOnes.contains(3) && !reservedOnes.contains(4) && !reservedOnes.contains(5)) { - count++; - } else if (!reservedOnes.contains(6) && !reservedOnes.contains(7) && !reservedOnes.contains(8) && !reservedOnes.contains(9)) { - count++; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1387.java b/src/main/java/com/fishercoder/solutions/_1387.java deleted file mode 100644 index 923806daab..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1387.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * 1387. Sort Integers by The Power Value - * - * The power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps: - * if x is even then x = x / 2 - * if x is odd then x = 3 * x + 1 - * - * For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1). - * Given three integers lo, hi and k. The task is to sort all integers in the interval [lo, hi] by the power value in ascending order, if two or more integers have the same power value sort them by ascending order. - * Return the k-th integer in the range [lo, hi] sorted by the power value. - * Notice that for any integer x (lo <= x <= hi) it is guaranteed that x will transform into 1 using these steps and that the power of x is will fit in 32 bit signed integer. - * - * Example 1: - * Input: lo = 12, hi = 15, k = 2 - * Output: 13 - * Explanation: The power of 12 is 9 (12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1) - * The power of 13 is 9 - * The power of 14 is 17 - * The power of 15 is 17 - * The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13. - * Notice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15. - * - * Example 2: - * Input: lo = 1, hi = 1, k = 1 - * Output: 1 - * - * Example 3: - * Input: lo = 7, hi = 11, k = 4 - * Output: 7 - * Explanation: The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14]. - * The interval sorted by power is [8, 10, 11, 7, 9]. - * The fourth number in the sorted array is 7. - * - * Example 4: - * Input: lo = 10, hi = 20, k = 5 - * Output: 13 - * - * Example 5: - * Input: lo = 1, hi = 1000, k = 777 - * Output: 570 - * - * Constraints: - * 1 <= lo <= hi <= 1000 - * 1 <= k <= hi - lo + 1 - * */ -public class _1387 { - public static class Solution1 { - public int getKth(int lo, int hi, int k) { - List power = new ArrayList<>(); - for (int i = lo; i <= hi; i++) { - power.add(new int[]{getSteps(i), i}); - } - Collections.sort(power, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); - return power.get(k - 1)[1]; - } - - private int getSteps(int number) { - int steps = 0; - while (number != 1) { - if (number % 2 == 0) { - number /= 2; - } else { - number = 3 * number + 1; - } - steps++; - } - return steps; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1388.java b/src/main/java/com/fishercoder/solutions/_1388.java deleted file mode 100644 index 01a3c4622b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1388.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 1388. Pizza With 3n Slices - * - * There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: - * You will pick any pizza slice. - * Your friend Alice will pick next slice in anti clockwise direction of your pick. - * Your friend Bob will pick next slice in clockwise direction of your pick. - * Repeat until there are no more slices of pizzas. - * Sizes of Pizza slices is represented by circular array slices in clockwise direction. - * - * Return the maximum possible sum of slice sizes which you can have. - * - * Example 1: - * Input: slices = [1,2,3,4,5,6] - * Output: 10 - * Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. - * - * Example 2: - * Input: slices = [8,9,8,6,1,1] - * Output: 16 - * Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. - * - * Example 3: - * Input: slices = [4,1,2,5,8,3,1,9,7] - * Output: 21 - * - * Example 4: - * Input: slices = [3,1,2] - * Output: 3 - * - * Constraints: - * 1 <= slices.length <= 500 - * slices.length % 3 == 0 - * 1 <= slices[i] <= 1000 - * */ -public class _1388 { - public static class Solution1 { - public int maxSizeSlices(int[] slices) { - int n = slices.length; - int[] b = Arrays.copyOf(slices, 2 * n); - for (int i = 0; i < n; i++) { - b[i + n] = slices[i]; - } - int[][] dp = new int[2 * n][2 * n]; - for (int len = 3; len <= n; len += 3) { - for (int i = 0; i + len - 1 < 2 * n; i++) { - int j = i + len - 1; - for (int k = i + 3; k <= j - 2; k += 3) { - dp[i][j] = Math.max(dp[i][j], dp[i][k - 1] + dp[k][j]); - } - for (int k = i + 1; k < j; k += 3) { - dp[i][j] = Math.max(dp[i][j], - (i + 1 <= k - 1 ? dp[i + 1][k - 1] : 0) - + b[k] + (k + 1 <= j - 1 ? dp[k + 1][j - 1] : 0) - ); - } - } - } - int ans = 0; - for (int i = 0; i < n; i++) { - ans = Math.max(ans, dp[i][i + n - 1]); - } - return ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1389.java b/src/main/java/com/fishercoder/solutions/_1389.java deleted file mode 100644 index 175ecf3c63..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1389.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 1389. Create Target Array in the Given Order - * - * Given two arrays of integers nums and index. Your task is to create target array under the following rules: - * Initially target array is empty. - * From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array. - * Repeat the previous step until there are no elements to read in nums and index. - * Return the target array. - * - * It is guaranteed that the insertion operations will be valid. - * - * Example 1: - * Input: nums = [0,1,2,3,4], index = [0,1,2,2,1] - * Output: [0,4,1,3,2] - * Explanation: - * nums index target - * 0 0 [0] - * 1 1 [0,1] - * 2 2 [0,1,2] - * 3 2 [0,1,3,2] - * 4 1 [0,4,1,3,2] - * - * Example 2: - * Input: nums = [1,2,3,4,0], index = [0,1,2,3,0] - * Output: [0,1,2,3,4] - * Explanation: - * nums index target - * 1 0 [1] - * 2 1 [1,2] - * 3 2 [1,2,3] - * 4 3 [1,2,3,4] - * 0 0 [0,1,2,3,4] - * - * Example 3: - * Input: nums = [1], index = [0] - * Output: [1] - * - * Constraints: - * 1 <= nums.length, index.length <= 100 - * nums.length == index.length - * 0 <= nums[i] <= 100 - * 0 <= index[i] <= i - * */ -public class _1389 { - public static class Solution1 { - public int[] createTargetArray(int[] nums, int[] index) { - List list = new ArrayList<>(); - for (int i = 0; i < nums.length; i++) { - list.add(index[i], nums[i]); - } - int[] target = new int[list.size()]; - for (int i = 0; i < target.length; i++) { - target[i] = list.get(i); - } - return target; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_139.java b/src/main/java/com/fishercoder/solutions/_139.java deleted file mode 100644 index c7e61f1035..0000000000 --- a/src/main/java/com/fishercoder/solutions/_139.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.List; - -/** - * 139. Word Break - * - * Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, - * determine if s can be segmented into a space-separated sequence of one or more dictionary words. - * You may assume the dictionary does not contain duplicate words. - - For example, given - s = "leetcode", - dict = ["leet", "code"]. - - Return true because "leetcode" can be segmented as "leet code". - - UPDATE (2017/1/4): - The wordDict parameter had been changed to a list of strings (instead of a set of strings). - Please reload the code definition to get the latest changes. - */ - -public class _139 { - - public static class Solution1 { - /** this beats 70.46% submission. */ - public boolean wordBreak(String s, List wordDict) { - int n = s.length(); - boolean[] dp = new boolean[n + 1]; - dp[0] = true; - for (int i = 1; i <= n; i++) { - for (int j = 0; j < i; j++) { - if (dp[j] && wordDict.contains(s.substring(j, i))) { - dp[i] = true; - break; - } - } - } - return dp[n]; - } - } - - public static class Solution2 { - /** - * Added pruning. - * this beats 89.91% submissions. - */ - public boolean wordBreak(String s, List wordDict) { - int maxLen = Integer.MIN_VALUE; - for (String word : wordDict) { - maxLen = (word.length() > maxLen) ? word.length() : maxLen; - } - - int n = s.length(); - boolean[] dp = new boolean[n + 1]; - dp[0] = true; - for (int i = 1; i <= n; i++) { - for (int j = 0; j < i; j++) { - if ((i - j) > maxLen) { - continue; - } - if (dp[j] && wordDict.contains(s.substring(j, i))) { - dp[i] = true; - break; - } - } - } - return dp[n]; - } - } - - public static class Solution3 { - /** - * Added pruning, plus start from the end to check. - * This beats 95.20% submissions. - */ - public boolean wordBreak(String s, List wordDict) { - int maxLen = Integer.MIN_VALUE; - for (String word : wordDict) { - maxLen = (word.length() > maxLen) ? word.length() : maxLen; - } - - int n = s.length(); - boolean[] dp = new boolean[n + 1]; - dp[0] = true; - for (int i = 1; i <= n; i++) { - for (int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen; - lastWordLength++) { - if (!dp[i - lastWordLength]) { - continue; - } - String sub = s.substring(i - lastWordLength, i); - if (wordDict.contains(sub)) { - dp[i] = true; - break; - } - } - } - return dp[n]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_1395.java b/src/main/java/com/fishercoder/solutions/_1395.java deleted file mode 100644 index ceea8f1c16..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1395.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 1395. Count Number of Teams - * - * There are n soldiers standing in a line. Each soldier is assigned a unique rating value. - * You have to form a team of 3 soldiers amongst them under the following rules: - * Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]). - * A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n). - * Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams). - * - * Example 1: - * Input: rating = [2,5,3,4,1] - * Output: 3 - * Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1). - * - * Example 2: - * Input: rating = [2,1,3] - * Output: 0 - * Explanation: We can't form any team given the conditions. - * - * Example 3: - * Input: rating = [1,2,3,4] - * Output: 4 - * - * Constraints: - * n == rating.length - * 1 <= n <= 200 - * 1 <= rating[i] <= 10^5 - * */ -public class _1395 { - public static class Solution1 { - public int numTeams(int[] rating) { - int count = 0; - //check increasing - for (int i = 0; i < rating.length - 2; i++) { - for (int j = i + 1; j < rating.length - 1; j++) { - if (rating[j] > rating[i]) { - for (int k = j + 1; k < rating.length; k++) { - if (rating[k] > rating[j]) { - count++; - } - } - } - } - } - - //check decreasing - for (int i = 0; i < rating.length - 2; i++) { - for (int j = i + 1; j < rating.length - 1; j++) { - if (rating[j] < rating[i]) { - for (int k = j + 1; k < rating.length; k++) { - if (rating[k] < rating[j]) { - count++; - } - } - } - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1396.java b/src/main/java/com/fishercoder/solutions/_1396.java deleted file mode 100644 index e5b7555258..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1396.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; - -/** - * 1396. Design Underground System - * - * Implement the class UndergroundSystem that supports three methods: - * - * 1. checkIn(int id, string stationName, int t) - * A customer with id card equal to id, gets in the station stationName at time t. - * A customer can only be checked into one place at a time. - * 2. checkOut(int id, string stationName, int t) - * A customer with id card equal to id, gets out from the station stationName at time t. - * 3. getAverageTime(string startStation, string endStation) - * Returns the average time to travel between the startStation and the endStation. - * The average time is computed from all the previous traveling from startStation to endStation that happened directly. - * Call to getAverageTime is always valid. - * You can assume all calls to checkIn and checkOut methods are consistent. That is, if a customer gets in at time t1 at some station, then it gets out at time t2 with t2 > t1. All events happen in chronological order. - * - * Example 1: - * Input - * ["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"] - * [[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]] - * Output - * [null,null,null,null,null,null,null,14.0,11.0,null,11.0,null,12.0] - * - * Explanation - * UndergroundSystem undergroundSystem = new UndergroundSystem(); - * undergroundSystem.checkIn(45, "Leyton", 3); - * undergroundSystem.checkIn(32, "Paradise", 8); - * undergroundSystem.checkIn(27, "Leyton", 10); - * undergroundSystem.checkOut(45, "Waterloo", 15); - * undergroundSystem.checkOut(27, "Waterloo", 20); - * undergroundSystem.checkOut(32, "Cambridge", 22); - * undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.0. There was only one travel from "Paradise" (at time 8) to "Cambridge" (at time 22) - * undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.0. There were two travels from "Leyton" to "Waterloo", a customer with id=45 from time=3 to time=15 and a customer with id=27 from time=10 to time=20. So the average time is ( (15-3) + (20-10) ) / 2 = 11.0 - * undergroundSystem.checkIn(10, "Leyton", 24); - * undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.0 - * undergroundSystem.checkOut(10, "Waterloo", 38); - * undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 12.0 - * - * Constraints: - * There will be at most 20000 operations. - * 1 <= id, t <= 10^6 - * All strings consist of uppercase, lowercase English letters and digits. - * 1 <= stationName.length <= 10 - * Answers within 10^-5 of the actual value will be accepted as correct. - * */ -public class _1396 { - public static class Solution1 { - public class UndergroundSystem { - - class StationAndTime { - String stationName; - int t; - - public StationAndTime(String stationName, int t) { - this.stationName = stationName; - this.t = t; - } - - public String getStation() { - return this.stationName; - } - - public int getTime() { - return this.t; - } - } - - Map averageTimeMap; - Map> travelerMap = new HashMap<>(); - - public UndergroundSystem() { - averageTimeMap = new HashMap<>(); - travelerMap = new HashMap<>(); - } - - public void checkIn(int id, String stationName, int t) { - if (!travelerMap.containsKey(id)) { - travelerMap.put(id, new LinkedList<>()); - } - travelerMap.get(id).add(new StationAndTime(stationName, t)); - } - - public void checkOut(int id, String stationName, int t) { - LinkedList list = travelerMap.get(id); - StationAndTime stationAndTime = list.getLast(); - String startToEndStation = stationAndTime.getStation() + "->" + stationName; - int duration = t - stationAndTime.getTime(); - if (!averageTimeMap.containsKey(startToEndStation)) { - averageTimeMap.put(startToEndStation, new double[]{duration, 1}); - } else { - double[] pair = averageTimeMap.get(startToEndStation); - double newAverage = (double) (pair[0] * pair[1] + duration) / (double) (pair[1] + 1); - averageTimeMap.put(startToEndStation, new double[]{newAverage, pair[1] + 1}); - } - } - - public double getAverageTime(String startStation, String endStation) { - return averageTimeMap.get(startStation + "->" + endStation)[0]; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_140.java b/src/main/java/com/fishercoder/solutions/_140.java deleted file mode 100644 index 3bf5043a26..0000000000 --- a/src/main/java/com/fishercoder/solutions/_140.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -/** - * 140. Word Break II - - Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, - add spaces in s to construct a sentence where each word is a valid dictionary word. - Return all such possible sentences. - - Note: - - The same word in the dictionary may be reused multiple times in the segmentation. - You may assume the dictionary does not contain duplicate words. - - Example 1: - - Input: - s = "catsanddog" - wordDict = ["cat", "cats", "and", "sand", "dog"] - Output: - [ - "cats and dog", - "cat sand dog" - ] - - Example 2: - - Input: - s = "pineapplepenapple" - wordDict = ["apple", "pen", "applepen", "pine", "pineapple"] - Output: - [ - "pine apple pen apple", - "pineapple pen apple", - "pine applepen apple" - ] - Explanation: Note that you are allowed to reuse a dictionary word. - - Example 3: - - Input: - s = "catsandog" - wordDict = ["cats", "dog", "sand", "and", "cat"] - Output: - [] - */ - -public class _140 { - public static class Solution1 { - public List wordBreak(String s, List wordDict) { - return dfs(s, wordDict, new HashMap<>()); - } - - List dfs(String s, List wordDict, HashMap> map) { - if (map.containsKey(s)) { - return map.get(s); - } - - ArrayList result = new ArrayList<>(); - if (s.length() == 0) { - result.add(""); - return result; - } - - for (String word : wordDict) { - if (s.startsWith(word)) { - List subList = dfs(s.substring(word.length()), wordDict, map); - for (String sub : subList) { - result.add(word + (sub.length() == 0 ? "" : " ") + sub); - } - } - } - map.put(s, result); - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_141.java b/src/main/java/com/fishercoder/solutions/_141.java deleted file mode 100644 index caf54c4b97..0000000000 --- a/src/main/java/com/fishercoder/solutions/_141.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -import java.util.HashSet; -import java.util.Set; - -/** - * 141. Linked List Cycle - - Given a linked list, determine if it has a cycle in it. - To represent a cycle in the given linked list, we use an integer - pos which represents the position (0-indexed) in the linked list where tail connects to. - If pos is -1, then there is no cycle in the linked list. - - Example 1: - Input: head = [3,2,0,-4], pos = 1 - Output: true - Explanation: There is a cycle in the linked list, where tail connects to the second node. - - Example 2: - Input: head = [1,2], pos = 0 - Output: true - Explanation: There is a cycle in the linked list, where tail connects to the first node. - - Example 3: - Input: head = [1], pos = -1 - Output: false - Explanation: There is no cycle in the linked list. - - Follow up: - Can you solve it using O(1) (i.e. constant) memory? - */ -public class _141 { - - public static class Solution1 { - public boolean hasCycle(ListNode head) { - Set set = new HashSet(); - while (head != null) { - if (!set.add(head)) { - return true; - } - head = head.next; - } - return false; - } - } - - public static class Solution2 { - public boolean hasCycle(ListNode head) { - ListNode slow = head; - ListNode fast = head; - while (fast != null && fast.next != null) { - fast = fast.next.next; - slow = slow.next; - if (fast == slow) { - return true; - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_142.java b/src/main/java/com/fishercoder/solutions/_142.java deleted file mode 100644 index dbe1235e9f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_142.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 142. Linked List Cycle II - - Given a linked list, return the node where the cycle begins. If there is no cycle, return null. - - Note: Do not modify the linked list. - - Follow up: - Can you solve it without using extra space? - */ -public class _142 { - - public static class Solution1 { - public ListNode detectCycle(ListNode head) { - ListNode slow = head; - ListNode fast = head; - while (fast != null && fast.next != null) { - slow = slow.next; - fast = fast.next.next; - if (slow == fast) { - ListNode slow2 = head; - while (slow2 != slow) { - slow = slow.next; - slow2 = slow2.next; - } - return slow; - } - } - return null; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1423.java b/src/main/java/com/fishercoder/solutions/_1423.java deleted file mode 100644 index b1c7002fb8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1423.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder.solutions; - -public class _1423 { - public static class Solution1 { - public int maxScore(int[] cardPoints, int k) { - int maxScore = 0; - if (cardPoints.length <= k) { - for (int point : cardPoints) { - maxScore += point; - } - return maxScore; - } - for (int i = 0; i < k; i++) { - maxScore += cardPoints[i]; - } - int runningSum = maxScore; - for (int i = cardPoints.length - 1, j = 1; i >= 0 && j <= k; i--, j++) { - runningSum = runningSum - cardPoints[k - j] + cardPoints[i]; - maxScore = Math.max(maxScore, runningSum); - } - return maxScore; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_143.java b/src/main/java/com/fishercoder/solutions/_143.java deleted file mode 100644 index 3c10f358c1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_143.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 143. Reorder List - * - * Given a singly linked list L: L0→L1→…→Ln-1→Ln, - reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… - - You must do this in-place without altering the nodes' values. - - For example, - Given {1,2,3,4}, reorder it to {1,4,2,3}. - */ -public class _143 { - public static class Solution1 { - public void reorderList(ListNode head) { - if (head == null || head.next == null) { - return; - } - /* first we use two pointers to separate this list into two parts */ - ListNode slowNode = head; - ListNode fastNode = head; - while (fastNode.next != null) { - fastNode = fastNode.next; - if (fastNode.next != null) { - fastNode = fastNode.next; - } else { - break; - } - slowNode = slowNode.next; - } - // two sublist heads - ListNode head1 = head; - ListNode head2 = slowNode.next; - // detach the two sublists; - slowNode.next = null; - - // reverse the second sublist - ListNode cur = head2; - ListNode post = cur.next; - cur.next = null; - while (post != null) { - ListNode temp = post.next; - post.next = cur; - cur = post; - post = temp; - } - head2 = cur;// the new head of the reversed sublist - - // merge the two sublists as required - ListNode p = head1; - ListNode q = head2; - while (q != null) { - ListNode temp1 = p.next; - ListNode temp2 = q.next; - p.next = q; - q.next = temp1; - p = temp1; - q = temp2; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_1437.java b/src/main/java/com/fishercoder/solutions/_1437.java deleted file mode 100644 index 1aab01b6ae..0000000000 --- a/src/main/java/com/fishercoder/solutions/_1437.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -public class _1437 { - public static class Solution1 { - public boolean kLengthApart(int[] nums, int k) { - List indexes = new ArrayList<>(); - for (int i = 0; i < nums.length; i++) { - if (nums[i] == 1) { - indexes.add(i); - } - } - for (int i = 0; i < indexes.size() - 1; i++) { - if (indexes.get(i + 1) - indexes.get(i) < k + 1) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_144.java b/src/main/java/com/fishercoder/solutions/_144.java deleted file mode 100644 index b37515746b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_144.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Deque; -import java.util.List; -import java.util.Stack; - -/** - * 144. Binary Tree Preorder Traversal - - Given a binary tree, return the preorder traversal of its nodes' values. - - For example: - Given binary tree {1,#,2,3}, - 1 - \ - 2 - / - 3 - return [1,2,3]. - - Note: Recursive solution is trivial, could you do it iteratively?*/ - -public class _144 { - public static class Solution1 { - public List preorderTraversal(TreeNode root) { - List list = new ArrayList<>(); - Stack stack = new Stack<>(); - stack.push(root); - while (!stack.isEmpty()) { - TreeNode curr = stack.pop(); - if (curr != null) { - list.add(curr.val); - stack.push(curr.right); - stack.push(curr.left); - } - } - return list; - } - } - - public static class Solution2 { - public List preorderTraversal(TreeNode root) { - List list = new ArrayList(); - return pre(root, list); - } - - List pre(TreeNode root, List list) { - if (root == null) { - return list; - } - list.add(root.val); - pre(root.left, list); - pre(root.right, list); - return list; - } - } - - public static class Solution3 { - public List preorderTraversal(TreeNode root) { - List list = new ArrayList<>(); - Stack stack = new Stack<>(); - while (!stack.isEmpty() || root != null) { - while (root != null) { - list.add(root.val); - stack.push(root); - root = root.left; - } - root = stack.pop(); - root = root.right; - } - return list; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_145.java b/src/main/java/com/fishercoder/solutions/_145.java deleted file mode 100644 index 16d5798069..0000000000 --- a/src/main/java/com/fishercoder/solutions/_145.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Stack; - -/** - * 145. Binary Tree Postorder Traversal - - Given a binary tree, return the postorder traversal of its nodes' values. - - For example: - Given binary tree {1,#,2,3}, - 1 - \ - 2 - / - 3 - return [3,2,1]. - - Note: Recursive solution is trivial, could you do it iteratively?*/ - -public class _145 { - public static class Solution1 { - /** - * A tricky one: Modify the code for pre-order traversal - * so that it becomes root->right->left, - * and then reverse the result to get left->right->root. - */ - public List postorderTraversal(TreeNode root) { - List result = new ArrayList(); - if (root == null) { - return result; - } - Stack stack = new Stack(); - stack.push(root); - while (!stack.isEmpty()) { - root = stack.pop(); - result.add(root.val); - if (root.left != null) { - stack.push(root.left); - } - if (root.right != null) { - stack.push(root.right); - } - } - Collections.reverse(result); - return result; - } - } - - public static class Solution2 { - /**Or use a LinkedList and add values to the head, then no reverse is needed. - * the linked list contents get added like this: - * - * root - * right, root - * left, right, root - * */ - public List postorderTraversal(TreeNode root) { - List list = new LinkedList<>(); - if (root == null) { - return list; - } - Stack stack = new Stack<>(); - stack.push(root); - while (!stack.isEmpty()) { - TreeNode curr = stack.pop(); - list.add(0, curr.val); - if (curr.left != null) { - stack.push(curr.left); - } - if (curr.right != null) { - stack.push(curr.right); - } - } - return list; - } - } - - public static class Solution3 { - public List postorderTraversal(TreeNode root) { - List result = new ArrayList(); - return post(root, result); - } - - List post(TreeNode root, List result) { - if (root == null) { - return result; - } - post(root.left, result); - post(root.right, result); - result.add(root.val); - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_146.java b/src/main/java/com/fishercoder/solutions/_146.java deleted file mode 100644 index 1b4cc7f988..0000000000 --- a/src/main/java/com/fishercoder/solutions/_146.java +++ /dev/null @@ -1,171 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * 146. LRU Cache - * - * Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. - - get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. - put(key, value) - Set or insert the value if the key is not already present. - When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. - - Follow up: - Could you do both operations in O(1) time complexity? - - Example: - - LRUCache cache = new LRUCache(2);//capacity - - cache.put(1, 1); - cache.put(2, 2); - cache.get(1); // returns 1 - cache.put(3, 3); // evicts key 2 - cache.get(2); // returns -1 (not found) - cache.put(4, 4); // evicts key 1 - cache.get(1); // returns -1 (not found) - cache.get(3); // returns 3 - cache.get(4); // returns 4 - */ - -public class _146 { - - public class Solution1 { - public class LRUCache { - /** - * The shortest implementation is to use LinkedHashMap: - * specify a size of the LinkedHashMap; - * override the removeEldestEntry method when its size exceeds max size: - * https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html#removeEldestEntry-java.util.Map.Entry- - * in the constructor, set the last boolean variable to be true: it means the ordering mode, - * if we set it to be true, it means in access order, false, means it's in insertion order: - * https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html#LinkedHashMap-int-float-boolean- - */ - - private Map cache; - private final int max; - - public LRUCache(int capacity) { - max = capacity; - cache = new LinkedHashMap(capacity, 1.0f, true) { - public boolean removeEldestEntry(Map.Entry eldest) { - return cache.size() > max; - } - }; - } - - public int get(int key) { - return cache.getOrDefault(key, -1); - } - - public void put(int key, int value) { - cache.put(key, value); - } - } - } - - public class Solution2 { - public class LRUCache { - /** - * The more verbose solution is to write a doubly linked list plus a map. - */ - private class Node { - int key; - int value; - - LRUCache.Node prev; - LRUCache.Node next; - - Node(int k, int v) { - this.key = k; - this.value = v; - } - - Node() { - this.key = 0; - this.value = 0; - } - } - - private int capacity; - private int count; - private LRUCache.Node head; - private LRUCache.Node tail; - private Map map; - // ATTN: the value should be Node type! This is the whole point of having a class called Node! - - public LRUCache(int capacity) { - this.capacity = capacity; - this.count = 0;// we need a count to keep track of the number of elements in the cache so - // that we know when to evict the LRU one from the cache - this.map = new HashMap(); - head = new LRUCache.Node(); - tail = new LRUCache.Node(); - head.next = tail; - tail.prev = head; - } - - public int get(int key) { - LRUCache.Node node = map.get(key); - // HashMap allows value to be null, this is superior than HashTable! - if (node == null) { - return -1; - } else { - - /**Do two operations: this makes the process more clear: - * remove the old node first, and then - * just add the node again. - * This will guarantee that this node will be at the latest position: - * the most recently used position.*/ - remove(node); - add(node); - - return node.value; - } - } - - public void put(int key, int value) { - LRUCache.Node node = map.get(key); - if (node == null) { - node = new LRUCache.Node(key, value); - map.put(key, node); - add(node); - count++; - - if (count > capacity) { - /** ATTN: It's tail.prev, not tail, because tail is always an invalid node, it - doesn't contain anything, it's always the tail.prev that is the last node in the - cache*/ - LRUCache.Node toDelete = tail.prev; - map.remove(toDelete.key); - remove(toDelete); - count--; - } - } else { - remove(node); - node.value = value; - add(node); - } - } - - private void remove(LRUCache.Node node) { - LRUCache.Node next = node.next; - LRUCache.Node prev = node.prev; - prev.next = next; - next.prev = prev; - } - - private void add(LRUCache.Node node) { - // ATTN: we'll always add the node into the first position: head.next!!!! - LRUCache.Node next = head.next; - head.next = node; - node.next = next; - node.prev = head; - next.prev = node; - } - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_147.java b/src/main/java/com/fishercoder/solutions/_147.java deleted file mode 100644 index ea4b20ecdc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_147.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 147. Insertion Sort List - * - * Sort a linked list using insertion sort. - * - * Algorithm of Insertion Sort: - * - * Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. - * At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. - * It repeats until no input elements remain. - * - * - * Example 1: - * - * Input: 4->2->1->3 - * Output: 1->2->3->4 - * - * Example 2: - * - * Input: -1->5->3->4->0 - * Output: -1->0->3->4->5 - */ -public class _147 { - - public static class Solution1 { - public ListNode insertionSortList(ListNode head) { - ListNode temp = head; - List list = new ArrayList<>(); - while (temp != null) { - list.add(temp.val); - temp = temp.next; - } - Integer[] nums = list.toArray(new Integer[list.size()]); - for (int i = 1; i < list.size(); i++) { - for (int j = i - 1; j >= 0; j--) { - if (nums[j] > nums[j + 1]) { - int tempNum = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = tempNum; - } - } - } - ListNode newHead = head; - for (int i = 0; i < nums.length; i++) { - newHead.val = nums[i]; - newHead = newHead.next; - } - return head; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_148.java b/src/main/java/com/fishercoder/solutions/_148.java deleted file mode 100644 index 6a7d791635..0000000000 --- a/src/main/java/com/fishercoder/solutions/_148.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 148. Sort List - * - * Sort a linked list in O(n log n) time using constant space complexity. - * - * Example 1: - * Input: 4->2->1->3 - * Output: 1->2->3->4 - * - * Example 2: - * Input: -1->5->3->4->0 - * Output: -1->0->3->4->5 - */ -public class _148 { - - public static class Solution1 { - /** - * Credit: https://discuss.leetcode.com/topic/18100/java-merge-sort-solution - * But this is not using constant space. - */ - public ListNode sortList(ListNode head) { - if (head == null || head.next == null) { - return head; - } - - //Step 1: split the list into halves - ListNode prev = null; - ListNode slow = head; - ListNode fast = head; - while (fast != null && fast.next != null) { - prev = slow; - fast = fast.next.next; - slow = slow.next; - } - prev.next = null; - - //step 2: sort each half - ListNode l1 = sortList(head); - ListNode l2 = sortList(slow); - - //step 3: merge the two halves - return merge(l1, l2); - } - - private ListNode merge(ListNode l1, ListNode l2) { - ListNode result = new ListNode(0); - ListNode tmp = result; - - while (l1 != null && l2 != null) { - if (l1.val < l2.val) { - tmp.next = l1; - l1 = l1.next; - } else { - tmp.next = l2; - l2 = l2.next; - } - tmp = tmp.next; - } - - if (l1 != null) { - tmp.next = l1; - } - if (l2 != null) { - tmp.next = l2; - } - return result.next; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_149.java b/src/main/java/com/fishercoder/solutions/_149.java deleted file mode 100644 index cac974e2b9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_149.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 149. Max Points on a Line - * - * Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. - * - * Example 1: - * Input: [[1,1],[2,2],[3,3]] - * Output: 3 - * Explanation: - * ^ - * | - * | o - * | o - * | o - * +-------------> - * 0 1 2 3 4 - * - * Example 2: - * Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] - * Output: 4 - * Explanation: - * ^ - * | - * | o - * | o o - * | o - * | o o - * +-------------------> - * 0 1 2 3 4 5 6 - * - * NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature. - */ -public class _149 { - - /** - * credits: https://leetcode.com/problems/max-points-on-a-line/discuss/328269/A-Java-solution-with-my-understanding - */ - public static class Solution1 { - public int maxPoints(int[][] points) { - if (points.length < 3) { - return points.length; - } - int max = 0; - Map map = new HashMap<>(); - for (int i = 0; i < points.length; i++) { - int dup = 1; - map.clear(); - for (int j = i + 1; j < points.length; j++) { - int dx = points[j][0] - points[i][0]; - int dy = points[j][1] - points[i][1]; - if (dx == 0 && dy == 0) { - dup++; - } else { - int gcd = getGcd(dx, dy); - long slope = ((long) (dy / gcd) << 32) + (dx / gcd); - map.put(slope, map.getOrDefault(slope, 0) + 1); - } - } - max = Math.max(max, dup); - for (Map.Entry entry : map.entrySet()) { - max = Math.max(max, entry.getValue() + dup); - } - } - return max; - } - - int getGcd(int a, int b) { - return b == 0 ? a : getGcd(b, a % b); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_15.java b/src/main/java/com/fishercoder/solutions/_15.java deleted file mode 100644 index df666051e1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_15.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class _15 { - - public static class Solution1 { - public List> threeSum(int[] nums) { - Arrays.sort(nums); - - List> result = new ArrayList<>(); - for (int i = 0; i < nums.length - 2; i++) { - if (i >= 1 && nums[i] == nums[i - 1]) { - continue; - } - int left = i + 1; - int right = nums.length - 1; - while (left < right) { - int sum = nums[i] + nums[left] + nums[right]; - if (sum == 0) { - result.add(Arrays.asList(nums[i], nums[left], nums[right])); - - while (left < right && nums[left] == nums[left + 1]) { - left++; - } - - while (left < right && nums[right] == nums[right - 1]) { - right--; - } - left++; - right--; - } else if (sum > 0) { - right--; - } else { - left++; - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_150.java b/src/main/java/com/fishercoder/solutions/_150.java deleted file mode 100644 index 13114b828c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_150.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; -import java.util.Stack; - -/** - * 150. Evaluate Reverse Polish Notation - * - * Evaluate the value of an arithmetic expression in Reverse Polish Notation. - * Valid operators are +, -, *, /. Each operand may be an integer or another expression. - * - * Note: - * Division between two integers should truncate toward zero. - * The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation. - * - * Example 1: - * Input: ["2", "1", "+", "3", "*"] - * Output: 9 - * Explanation: ((2 + 1) * 3) = 9 - * - * Example 2: - * Input: ["4", "13", "5", "/", "+"] - * Output: 6 - * Explanation: (4 + (13 / 5)) = 6 - * - * Example 3: - * Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] - * Output: 22 - * Explanation: - * ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 - * = ((10 * (6 / (12 * -11))) + 17) + 5 - * = ((10 * (6 / -132)) + 17) + 5 - * = ((10 * 0) + 17) + 5 - * = (0 + 17) + 5 - * = 17 + 5 - * = 22 - */ -public class _150 { - - public static class Solution1 { - public int evalRPN(String[] tokens) { - Stack stack = new Stack<>(); - Set op = new HashSet(); - op.add("+"); - op.add("-"); - op.add("*"); - op.add("/"); - - int exp = 0; - String operand1 = ""; - String operand2 = ""; - for (int i = 0; i < tokens.length; ) { - while ((i < tokens.length) && (!op.contains(tokens[i]))) { - stack.push(tokens[i]); - i++; - } - if (i == tokens.length) { - if (!stack.isEmpty()) { - return Integer.parseInt(stack.pop()); - } - } else if (op.contains(tokens[i])) { - if (!stack.isEmpty()) { - operand2 = stack.pop(); - } - if (!stack.isEmpty() && !op.contains(stack.peek())) { - operand1 = stack.pop(); - } - if (tokens[i].equals("+")) { - exp = Integer.parseInt(operand1) + Integer.parseInt(operand2); - } else if (tokens[i].equals("-")) { - exp = Integer.parseInt(operand1) - Integer.parseInt(operand2); - } else if (tokens[i].equals("*")) { - exp = Integer.parseInt(operand1) * Integer.parseInt(operand2); - } else if (tokens[i].equals("/")) { - exp = Integer.parseInt(operand1) / Integer.parseInt(operand2); - } else { - exp = Integer.parseInt(operand2); - } - stack.push(String.valueOf(exp)); - i++; - } - } - return Integer.parseInt(stack.pop()); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_151.java b/src/main/java/com/fishercoder/solutions/_151.java deleted file mode 100644 index 41c06e2a1f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_151.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Deque; - -/** - * 151. Reverse Words in a String - - Given an input string, reverse the string word by word. - For example, - Given s = "the sky is blue", - return "blue is sky the". - - Clarification: - - What constitutes a word? - A sequence of non-space characters constitutes a word. - Could the input string contain leading or trailing spaces? - Yes. However, your reversed string should not contain leading or trailing spaces. - How about multiple spaces between two words? - Reduce them to a single space in the reversed string. - */ - -public class _151 { - public static class Solution1 { - public String reverseWords(String s) { - s.trim(); - if (s == null || s.length() == 0) { - return ""; - } - String[] words = s.split(" "); - if (words == null || words.length == 0) { - return ""; - } - Deque stack = new ArrayDeque<>(); - for (String word : words) { - if (!word.equals("")) { - stack.offer(word); - } - } - StringBuilder stringBuilder = new StringBuilder(); - while (!stack.isEmpty()) { - stringBuilder.append(stack.pollLast()).append(" "); - } - return stringBuilder.substring(0, stringBuilder.length() - 1).toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_152.java b/src/main/java/com/fishercoder/solutions/_152.java deleted file mode 100644 index ae5b1af255..0000000000 --- a/src/main/java/com/fishercoder/solutions/_152.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 152. Maximum Product Subarray - - Find the contiguous subarray within an array (containing at least one number) which has the largest product. - - For example, given the array [2,3,-2,4], - the contiguous subarray [2,3] has the largest product = 6. - */ -public class _152 { - public static class Solution1 { - public int maxProduct(int[] nums) { - int pos = nums[0]; - int neg = nums[0]; - int max = nums[0]; - for (int i = 1; i < nums.length; i++) { - int temp = pos; - pos = Math.max(nums[i], nums[i] * ((nums[i] >= 0) ? pos : neg)); - neg = Math.min(nums[i], nums[i] * ((nums[i] >= 0) ? neg : temp)); - max = Math.max(max, pos); - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_153.java b/src/main/java/com/fishercoder/solutions/_153.java deleted file mode 100644 index 9a3d43a02d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_153.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 153. Find Minimum in Rotated Sorted Array - - Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. - - (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). - - Find the minimum element. - - You may assume no duplicate exists in the array. - - Example 1: - Input: [3,4,5,1,2] - Output: 1 - - Example 2: - Input: [4,5,6,7,0,1,2] - Output: 0 - - */ -public class _153 { - public static class Solution1 { - public int findMin(int[] nums) { - int left = 0; - int right = nums.length - 1; - if (nums[left] < nums[right]) { - return nums[left]; - } - int min = nums[0]; - while (left + 1 < right) { - int mid = left + (right - left) / 2; - min = Math.min(min, nums[mid]); - if (nums[mid] > nums[left]) { - min = Math.min(nums[left], min); - left = mid + 1; - } else if (nums[mid] < nums[left]) { - right = mid - 1; - } - } - min = Math.min(min, Math.min(nums[left], nums[right])); - return min; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_154.java b/src/main/java/com/fishercoder/solutions/_154.java deleted file mode 100644 index 930f8b13c1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_154.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 154. Find Minimum in Rotated Sorted Array II - - Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. - - (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). - - Find the minimum element. - - The array may contain duplicates. - - Example 1: - Input: [1,3,5] - Output: 1 - - Example 2: - Input: [2,2,2,0,1] - Output: 0 - - Note: - This is a follow up problem to Find Minimum in Rotated Sorted Array. - Would allow duplicates affect the run-time complexity? How and why? - - */ -public class _154 { - public static class Solution1 { - public int findMin(int[] nums) { - int left = 0; - int right = nums.length - 1; - if (nums[left] < nums[right]) { - return nums[left]; - } - int min = nums[0]; - while (left + 1 < right) { - int mid = left + (right - left) / 2; - min = Math.min(min, nums[mid]); - if (nums[mid] > nums[left]) { - min = Math.min(nums[left], min); - left = mid + 1; - } else if (nums[mid] < nums[left]) { - right = mid - 1; - } else { - left++; - } - } - min = Math.min(min, Math.min(nums[left], nums[right])); - return min; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_155.java b/src/main/java/com/fishercoder/solutions/_155.java deleted file mode 100644 index cea34763b4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_155.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 155. Min Stack - * Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. - - * push(x) -- Push element x onto stack. - * pop() -- Removes the element on top of the stack. - * top() -- Get the top element. - * getMin() -- Retrieve the minimum element in the stack. - - * Example: - * MinStack minStack = new MinStack(); - * minStack.push(-2); - * minStack.push(0); - * minStack.push(-3); - * minStack.getMin(); --> Returns -3. - * minStack.pop(); - * minStack.top(); --> Returns 0. - * minStack.getMin(); --> Returns -2. - */ - -public class _155 { - - public static class Solution1 { - class MinStack { - private Stack stack; - private int min; - - /** - * initialize your data structure here. - */ - public MinStack() { - stack = new Stack(); - min = Integer.MAX_VALUE; - } - - public void push(int x) { - /**All the trick happens here, we push the second minimum number onto the stack before we push the newer one, - * this way, when popping, we could always get the next minimum one in constant time.*/ - if (x <= min) { - stack.push(min); - min = x; - } - stack.push(x); - } - - public void pop() { - /**if the value on the top of the stack happens to be the current minimum, we'll pop twice and change - * the current min value to be the last min value */ - if (min == stack.pop()) { - min = stack.pop(); - } - } - - public int top() { - return stack.peek(); - } - - public int getMin() { - return min; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_157.java b/src/main/java/com/fishercoder/solutions/_157.java deleted file mode 100644 index cfc7117400..0000000000 --- a/src/main/java/com/fishercoder/solutions/_157.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 157. Read N Characters Given Read4 - * - * The API: int read4(char *buf) reads 4 characters at a time from a file. - * - * The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. - * - * By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file. - * - * Example 1: - * - * Input: buf = "abc", n = 4 - * Output: "abc" - * Explanation: The actual number of characters read is 3, which is "abc". - * - * Example 2: - * - * Input: buf = "abcde", n = 5 - * Output: "abcde" - * - * Note: - * The read function will only be called once for each test case. - * - */ -public class _157 { - public static class Solution1 { - public int read(char[] buf, int n) { - int index = 0; - int next = 0; - char[] buffer = new char[4]; - while (index < n && (next = read4(buffer)) != 0) { - for (int i = 0; i < next && index < n; index++, i++) { - buf[index] = buffer[i]; - } - } - return index; - } - - private int read4(char[] buffer) { - //this is a fake method to make Eclipse happy - return 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_158.java b/src/main/java/com/fishercoder/solutions/_158.java deleted file mode 100644 index ce12530b27..0000000000 --- a/src/main/java/com/fishercoder/solutions/_158.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 158. Read N Characters Given Read4 II - Call multiple times - * - * The API: int read4(char *buf) reads 4 characters at a time from a file. - * The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. - * By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file. - * - * Note: - * The read function may be called multiple times. - * - * Example 1: - * Given buf = "abc" - * read("abc", 1) // returns "a" - * read("abc", 2); // returns "bc" - * read("abc", 1); // returns "" - * - * Example 2: - * Given buf = "abc" - * read("abc", 4) // returns "abc" - * read("abc", 1); // returns "" - */ -public class _158 { - - public static class Solution1 { - /** - * @param buf Destination buffer - * @param n Maximum number of characters to read - * @return The number of characters read - */ - private int buffPtr = 0; - private int buffCnt = 0; - private char[] buff = new char[4]; - - public int read(char[] buf, int n) { - int ptr = 0; - while (ptr < n) { - if (buffPtr == 0) { - buffCnt = read4(buff); - } - if (buffCnt == 0) { - break; - } - while (ptr < n && buffPtr < buffCnt) { - buf[ptr++] = buff[buffPtr++]; - } - if (buffPtr >= buffCnt) { - buffPtr = 0; - } - } - return ptr; - } - - //This is a fake method to make IDE happy. - private int read4(char[] buff) { - return 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_159.java b/src/main/java/com/fishercoder/solutions/_159.java deleted file mode 100644 index 4c2ad40d60..0000000000 --- a/src/main/java/com/fishercoder/solutions/_159.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 159. Longest Substring with At Most Two Distinct Characters - * Given a string s , find the length of the longest substring t that contains at most 2 distinct characters. - * - * Example 1: - * Input: "eceba" - * Output: 3 - * Explanation: t is "ece" which its length is 3. - * - * Example 2: - * Input: "ccaabbb" - * Output: 5 - * Explanation: t is "aabbb" which its length is 5. - */ -public class _159 { - public static class Solution1 { - public int lengthOfLongestSubstringTwoDistinct(String s) { - if (s.length() < 1) { - return 0; - } - Map index = new HashMap<>(); - int lo = 0; - int hi = 0; - int maxLength = 0; - while (hi < s.length()) { - if (index.size() <= 2) { - char c = s.charAt(hi); - index.put(c, hi); - hi++; - } - if (index.size() > 2) { - int leftMost = s.length(); - for (int i : index.values()) { - leftMost = Math.min(leftMost, i); - } - char c = s.charAt(leftMost); - index.remove(c); - lo = leftMost + 1; - } - maxLength = Math.max(maxLength, hi - lo); - } - return maxLength; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_16.java b/src/main/java/com/fishercoder/solutions/_16.java deleted file mode 100644 index 419c2316e2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_16.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -public class _16 { - - public static class Solution1 { - public int threeSumClosest(int[] nums, int target) { - Arrays.sort(nums); - int len = nums.length; - if (len < 3) { - int sum = 0; - for (int i : nums) { - sum += i; - } - return sum; - } - int sum = nums[0] + nums[1] + nums[2]; - for (int i = 0; i < len - 2; i++) { - int left = i + 1; - int right = len - 1; - while (left < right) { - int thisSum = nums[i] + nums[left] + nums[right]; - if (Math.abs(target - thisSum) < Math.abs(target - sum)) { - sum = thisSum; - if (sum == target) { - return sum; - } - } else if (target > thisSum) { - left++; - } else { - right--; - } - } - } - return sum; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_160.java b/src/main/java/com/fishercoder/solutions/_160.java deleted file mode 100644 index 90dc2820ab..0000000000 --- a/src/main/java/com/fishercoder/solutions/_160.java +++ /dev/null @@ -1,154 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -import java.util.HashSet; -import java.util.Set; - -/** - * 160. Intersection of Two Linked Lists - Write a program to find the node at which the intersection of two singly linked lists begins. - - - For example, the following two linked lists: - - A: a1 → a2 - ↘ - c1 → c2 → c3 - ↗ - B: b1 → b2 → b3 - begin to intersect at node c1. - - Example 1: - A: 4 → 1 - ↘ - 8 → 4 → 5 - ↗ - B: 5 → 0 → 1 - - - Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 - Output: Reference of the node with value = 8 - Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). - From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. - There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B. - - - Example 2: - A: 0 -> 9 → 1 - ↘ - 2 → 4 - ↗ - B: 3 - - Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 - Output: Reference of the node with value = 2 - Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). - From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. - There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B. - - - Example 3: - A: 2 → 6 -> 4 - - B: 1 -> 5 - - Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 - Output: null - Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. - Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values. - Explanation: The two lists do not intersect, so return null. - - - Notes: - - If the two linked lists have no intersection at all, return null. - The linked lists must retain their original structure after the function returns. - You may assume there are no cycles anywhere in the entire linked structure. - Your code should preferably run in O(n) time and use only O(1) memory. - */ - -public class _160 { - - public static class Solution1 { - - public ListNode getIntersectionNode(ListNode headA, ListNode headB) { - int lenA = findLen(headA); - int lenB = findLen(headB); - /**align headA and headB to the same starting point and then move together until we find the intersection point*/ - while (lenA < lenB) { - headB = headB.next; - lenB--; - } - - while (lenB < lenA) { - headA = headA.next; - lenA--; - } - - while (headA != headB) { - headA = headA.next; - headB = headB.next; - } - - return headA; - } - - private int findLen(ListNode head) { - int len = 0; - while (head != null) { - head = head.next; - len++; - } - return len; - } - - } - - public static class Solution2 { - /** - * Most optimal solution: - * - * O(m+n) time - * O(1) space - * credit: https://discuss.leetcode.com/topic/28067/java-solution-without-knowing-the-difference-in-len*/ - public ListNode getIntersectionNode(ListNode headA, ListNode headB) { - if (headA == null || headB == null) { - return null; - } - - ListNode a = headA; - ListNode b = headB; - - /**if a and b have different lengths, then it will stop the loop after second iteration*/ - while (a != b) { - /**for the first iteration, it'll just reset the pointer to the head of another linkedlist*/ - a = a == null ? headB : a.next; - b = b == null ? headA : b.next; - } - return a; - } - } - - public static class Solution3 { - /** - * O(m+n) time - * O(Math.max(m, n)) space - * */ - public ListNode getIntersectionNode(ListNode headA, ListNode headB) { - Set set = new HashSet<>(); - while (headA != null) { - set.add(headA); - headA = headA.next; - } - - while (headB != null) { - if (set.contains(headB)) { - return headB; - } - headB = headB.next; - } - return null; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_161.java b/src/main/java/com/fishercoder/solutions/_161.java deleted file mode 100644 index 0acf1dda7b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_161.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 161. One Edit Distance - * - * Given two strings s and t, determine if they are both one edit distance apart. - * - * Note: - * There are 3 possiblities to satisify one edit distance apart: - * Insert a character into s to get t - * Delete a character from s to get t - * Replace a character of s to get t - * - * Example 1: - * Input: s = "ab", t = "acb" - * Output: true - * Explanation: We can insert 'c' into s to get t. - * - * Example 2: - * Input: s = "cab", t = "ad" - * Output: false - * Explanation: We cannot get t from s by only one step. - * - * Example 3: - * Input: s = "1203", t = "1213" - * Output: true - * Explanation: We can replace '0' with '1' to get t. - */ -public class _161 { - public static class Solution1 { - public boolean isOneEditDistance(String s, String t) { - char[] schar = s.toCharArray(); - char[] tchar = t.toCharArray(); - - if (Math.abs(s.length() - t.length()) == 1) { - char[] longer = (s.length() > t.length()) ? schar : tchar; - char[] shorter = (longer == schar) ? tchar : schar; - - int diffCnt = 0; - int i = 0; - int j = 0; - for (; i < shorter.length && j < longer.length; ) { - if (longer[j] != shorter[i]) { - diffCnt++; - j++; - } else { - i++; - j++; - } - } - return diffCnt == 1 - || diffCnt - == 0;//it could be the last char of the longer is the different one, in that case, diffCnt remains to be zero - } else if (s.length() == t.length()) { - int diffCnt = 0; - for (int i = 0; i < s.length(); i++) { - if (schar[i] != tchar[i]) { - diffCnt++; - } - if (diffCnt > 1) { - return false; - } - } - return diffCnt == 1; - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_162.java b/src/main/java/com/fishercoder/solutions/_162.java deleted file mode 100644 index 9327114d41..0000000000 --- a/src/main/java/com/fishercoder/solutions/_162.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 162. Find Peak Element - - A peak element is an element that is greater than its neighbors. - - Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. - - The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. - - You may imagine that nums[-1] = nums[n] = -∞. - - Example 1: - Input: nums = [1,2,3,1] - Output: 2 - Explanation: 3 is a peak element and your function should return the index number 2. - - Example 2: - Input: nums = [1,2,1,3,5,6,4] - Output: 1 or 5 - Explanation: Your function can return either index number 1 where the peak element is 2, - or index number 5 where the peak element is 6. - - Note: - Your solution should be in logarithmic complexity. - */ - -public class _162 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/29329/java-solution-and-explanation-using-invariants - * - * Basically, we need to keep this invariant: - * nums[left] > nums[left-1], then we could return left as the result - * or nums[right] > nums[right+1], then we could return right as the result - * - * Time: O(Ologn) - */ - public int findPeakElement(int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - int left = 0; - int right = nums.length - 1; - while (left + 1 < right) { - int mid = left + (right - left) / 2; - if (nums[mid] < nums[mid + 1]) { - left = mid; - } else { - right = mid; - } - } - return (left == nums.length - 1 || nums[left] > nums[left + 1]) ? left : right; - } - } - - public static class Solution2 { - /** - * My original O(n) solution. - */ - public int findPeakElement(int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - int n = nums.length; - int result = 0; - for (int i = 0; i < n; i++) { - if (i == 0 && n > 1 && nums[i] > nums[i + 1]) { - result = i; - break; - } else if (i == n - 1 && i > 0 && nums[i] > nums[i - 1]) { - result = i; - break; - } else if (i > 0 && i < n - 1 && nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) { - result = i; - break; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_163.java b/src/main/java/com/fishercoder/solutions/_163.java deleted file mode 100644 index 78603dc409..0000000000 --- a/src/main/java/com/fishercoder/solutions/_163.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 163. Missing Ranges - * - * Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges. - * For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"]. - */ -public class _163 { - public static class Solution1 { - public List findMissingRanges(int[] nums, int lower, int upper) { - List result = new ArrayList<>(); - long low = (long) lower - 1; - long up = 0; - for (int i = 0; i <= nums.length; i++) { - if (i == nums.length) { - up = (long) upper + 1; - } else { - up = nums[i]; - } - if (up == low + 2) { - result.add(low + 1 + ""); - } else if (up > low + 2) { - result.add((low + 1) + "->" + (up - 1)); - } - low = up; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_164.java b/src/main/java/com/fishercoder/solutions/_164.java deleted file mode 100644 index 8336ae84da..0000000000 --- a/src/main/java/com/fishercoder/solutions/_164.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 164. Maximum Gap - * - * Given an unsorted array, find the maximum difference between the successive elements in its sorted form. - * Return 0 if the array contains less than 2 elements. - * - * Example 1: - * Input: [3,6,9,1] - * Output: 3 - * Explanation: The sorted form of the array is [1,3,6,9], either - * (3,6) or (6,9) has the maximum difference 3. - * - * Example 2: - * Input: [10] - * Output: 0 - * Explanation: The array contains less than 2 elements, therefore return 0. - * - * Note: - * You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range. - * Try to solve it in linear time/space. - */ -public class _164 { - public static class Solution1 { - /** brute force solution */ - public int maximumGap(int[] nums) { - if (nums.length < 2) { - return 0; - } - - Arrays.sort(nums); - int max = Integer.MIN_VALUE; - for (int i = 1; i < nums.length; ) { - while (i < nums.length && nums[i] == nums[i - 1]) { - i++; - } - if (i == nums.length) { - i--; - max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max; - break; - } else { - max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max; - } - if (nums[i] != nums[i - 1]) { - i++; - } - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_165.java b/src/main/java/com/fishercoder/solutions/_165.java deleted file mode 100644 index c37248f175..0000000000 --- a/src/main/java/com/fishercoder/solutions/_165.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; -/** - * 165. Compare Version Numbers - - Compare two version numbers version1 and version2. - If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. - - You may assume that the version strings are non-empty and contain only digits and the . character. - The . character does not represent a decimal point and is used to separate number sequences. - For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision. - - Here is an example of version numbers ordering: - - 0.1 < 1.1 < 1.2 < 13.37 - */ - -public class _165 { - public static class Solution1 { - public int compareVersion(String version1, String version2) { - String[] v1s = version1.split( - "\\.");//escaping it is very important! Otherwise, it's not going to work as expected! - String[] v2s = version2.split("\\."); - int len = (v1s.length < v2s.length) ? v2s.length : v1s.length; - for (int i = 0; i < len; i++) { - if (v1s.length == i) { - while (i < len) { - if (Integer.parseInt(v2s[i]) > 0) { - return -1; - } - i++; - } - } else if (v2s.length == i) { - while (i < len) { - if (Integer.parseInt(v1s[i]) > 0) { - return 1; - } - i++; - } - } else { - if (Integer.parseInt(v1s[i]) > Integer.parseInt(v2s[i])) { - return 1; - } else if (Integer.parseInt(v2s[i]) > Integer.parseInt(v1s[i])) { - return -1; - } - } - } - return 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_166.java b/src/main/java/com/fishercoder/solutions/_166.java deleted file mode 100644 index d64bc20f35..0000000000 --- a/src/main/java/com/fishercoder/solutions/_166.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 166. Fraction to Recurring Decimal - * - * Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. - - If the fractional part is repeating, enclose the repeating part in parentheses. - - For example, - - Given numerator = 1, denominator = 2, return "0.5". - Given numerator = 2, denominator = 1, return "2". - Given numerator = 2, denominator = 3, return "0.(6)". - - */ -public class _166 { - public static class Solution1 { - /** credit: https://discuss.leetcode.com/topic/33311/simple-and-short-solution-in-java */ - public String fractionToDecimal(int numerator, int denominator) { - String sign = - (numerator >= 0 && denominator >= 0) || (numerator < 0 && denominator < 0) ? "" : "-"; - if (numerator == 0) { - return "0"; - } - long num = Math.abs((long) numerator); - long deno = Math.abs((long) denominator); - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(sign); - long integral = Math.abs(num / deno); - stringBuilder.append(integral); - if (numerator % denominator == 0) { - return stringBuilder.toString(); - } else { - stringBuilder.append("."); - } - long remainder = num % deno; - - Map map = new HashMap<>(); - while (!map.containsKey(remainder)) { - map.put(remainder, stringBuilder.length()); - long n = remainder * 10 / deno; - remainder = remainder * 10 % deno; - if (remainder != 0 || (remainder == 0 && !map.containsKey(remainder))) { - stringBuilder.append(n); - } - } - if (remainder != 0) { - stringBuilder.insert(map.get(remainder), "("); - stringBuilder.append(")"); - } - return stringBuilder.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_167.java b/src/main/java/com/fishercoder/solutions/_167.java deleted file mode 100644 index 1600a43f7b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_167.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 167. Two Sum II - Input array is sorted - - Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. - - The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. - - Note: - - Your returned answers (both index1 and index2) are not zero-based. - You may assume that each input would have exactly one solution and you may not use the same element twice. - - Example: - - Input: numbers = [2,7,11,15], target = 9 - Output: [1,2] - Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. - - */ - -public class _167 { - public static class Solution1 { - public int[] twoSum(int[] numbers, int target) { - int left = 0; - int right = numbers.length - 1; - while (left < right) { - long sum = numbers[left] + numbers[right]; - if (sum > target) { - right--; - } else if (sum < target) { - left++; - } else { - return new int[]{left + 1, right + 1}; - } - } - return new int[]{-1, -1}; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_168.java b/src/main/java/com/fishercoder/solutions/_168.java deleted file mode 100644 index f0150c0a9b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_168.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; -/** - * - * 168. Excel Sheet Column Title - - Given a positive integer, return its corresponding column title as appear in an Excel sheet. - - For example: - - 1 -> A - 2 -> B - 3 -> C - ... - 26 -> Z - 27 -> AA - 28 -> AB - ... - - Example 1: - - Input: 1 - Output: "A" - - Example 2: - - Input: 28 - Output: "AB" - - Example 3: - - Input: 701 - Output: "ZY" - - */ -public class _168 { - public static class Solution1 { - public String convertToTitle(int n) { - /**Get the right most digit first, move to the left, e.g. when n = 28, we get 'B' first, then we get 'A'.*/ - StringBuilder sb = new StringBuilder(); - while (n != 0) { - int temp = (n - 1) % 26; - sb.append((char) (temp + 65)); - n = (n - 1) / 26; - } - return sb.reverse().toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_169.java b/src/main/java/com/fishercoder/solutions/_169.java deleted file mode 100644 index 0fff23787d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_169.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 169. Majority Element - - Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. - You may assume that the array is non-empty and the majority element always exist in the array. - - Example 1: - Input: [3,2,3] - Output: 3 - - Example 2: - Input: [2,2,1,1,1,2,2] - Output: 2 - */ -public class _169 { - public static class Solution1 { - /**Moore Voting Algorithm - * How to understand this: - * 1. For a number to qualify as a majority element, it needs to occur more than 1/2 times, which - * means there are a max of only one such element in any given array. - * 2. E.g. given this array [1,2,3,1,1,1], two of the 1s will be balanced off by 2 and 3, still there are two 1s remaining in the end - * which is the majority element*/ - public int majorityElement(int[] nums) { - int count = 1; - int majority = nums[0]; - for (int i = 1; i < nums.length; i++) { - if (count == 0) { - count++; - majority = nums[i]; - } else if (nums[i] == majority) { - count++; - } else { - count--; - } - } - return majority; - } - } - - public static class Solution2 { - //bit manipulation - public int majorityElement(int[] nums) { - int[] bit = new int[32];//because an integer is 32 bits, so we use an array of 32 long - for (int num : nums) { - for (int i = 0; i < 32; i++) { - if ((num >> (31 - i) & 1) == 1) { - bit[i]++;//this is to compute each number's ones frequency - } - } - } - int res = 0; - //this below for loop is to construct the majority element: since every bit of this element would have appeared more than n/2 times - for (int i = 0; i < 32; i++) { - bit[i] = bit[i] > nums.length / 2 ? 1 - : 0;//we get rid of those that bits that are not part of the majority number - res += bit[i] * (1 << (31 - i)); - } - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_17.java b/src/main/java/com/fishercoder/solutions/_17.java deleted file mode 100644 index d9f602dce3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_17.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -public class _17 { - - public static class Solution1 { - public List letterCombinations(String digits) { - List result = new ArrayList(); - if (digits.length() == 0) { - return result; - } - - String[] digits2Letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; - - result.add("");//this line is important, otherwise result is empty and Java will default it to an empty String - for (int i = 0; i < digits.length(); i++) { - result = combine(digits2Letters[digits.charAt(i) - '0'], result); - } - - return result; - } - - List combine(String letters, List result) { - List newResult = new ArrayList(); - - for (int i = 0; i < letters.length(); i++) { - //the order of the two for loops doesn't matter, you could swap them and it still works. - for (String str : result) { - newResult.add(str + letters.charAt(i)); - } - } - return newResult; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_170.java b/src/main/java/com/fishercoder/solutions/_170.java deleted file mode 100644 index 743115ae5b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_170.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 170. Two Sum III - Data structure design - * - * Design and implement a TwoSum class. It should support the following operations: add and find. - * - * add - Add the number to an internal data structure. - * find - Find if there exists any pair of numbers which sum is equal to the value. - * - * Example 1: - * add(1); add(3); add(5); - * find(4) -> true - * find(7) -> false - * - * Example 2: - * add(3); add(1); add(2); - * find(3) -> true - * find(6) -> false - */ - -public class _170 { - public static class Solution1 { - private Map map = new HashMap(); - private List list = new ArrayList(); - - // Add the number to an internal data structure. - public void add(int number) { - list.add(number); - map.put(number, map.getOrDefault(number, 0) + 1); - } - - // Find if there exists any pair of numbers which sum is equal to the value. - public boolean find(int value) { - for (int i = 0; i < list.size(); i++) { - int val1 = list.get(i); - int val2 = value - val1; - if (map.containsKey(val2)) { - if (val1 == val2) { - if (map.get(val2) > 1) { - return true; - } - } else { - return true; - } - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_171.java b/src/main/java/com/fishercoder/solutions/_171.java deleted file mode 100644 index 2642418aa4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_171.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 170. Two Sum III - Data structure design - * - * Given a column title as appear in an Excel sheet, return its corresponding column number. - - For example: - - A -> 1 - B -> 2 - C -> 3 - ... - Z -> 26 - AA -> 27 - AB -> 28 - - */ -public class _171 { - - public static class Solution1 { - public int titleToNumber(String s) { - char[] c = s.toCharArray(); - int result = 0; - for (int i = s.length() - 1; i >= 0; i--) { - result += - (c[i] - 64) * ((int) Math.pow(26, s.length() - i - 1));//The ASCII value of A is 65 - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_173.java b/src/main/java/com/fishercoder/solutions/_173.java deleted file mode 100644 index 7e30e77de0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_173.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; -import java.util.Queue; -import java.util.Stack; - -/** - * 173. Binary Search Tree Iterator - * - * Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. - * - * Calling next() will return the next smallest number in the BST. - * - * Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree. - */ -public class _173 { - - public static class Solution1 { - - public static class BSTIterator { - private Queue queue; - - public BSTIterator(TreeNode root) { - queue = new LinkedList<>(); - if (root != null) { - dfs(root, queue); - } - } - - private void dfs(TreeNode root, Queue q) { - if (root.left != null) { - dfs(root.left, q); - } - q.offer(root.val); - if (root.right != null) { - dfs(root.right, q); - } - } - - public boolean hasNext() { - return !queue.isEmpty(); - } - - public int next() { - return queue.poll(); - } - } - } - - public static class Solution2 { - public static class BSTIterator { - /** - * This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we - * push all its right nodes into the stack if there are any. - * This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge - * since h could be much smaller than n. Cheers! - */ - - private Stack stack; - - public BSTIterator(TreeNode root) { - stack = new Stack(); - pushToStack(root, stack); - } - - private void pushToStack(TreeNode root, Stack stack) { - while (root != null) { - stack.push(root); - root = root.left; - } - } - - public boolean hasNext() { - return !stack.isEmpty(); - } - - public int next() { - TreeNode curr = stack.pop(); - pushToStack(curr.right, stack); - return curr.val; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_174.java b/src/main/java/com/fishercoder/solutions/_174.java deleted file mode 100644 index 905b9fb371..0000000000 --- a/src/main/java/com/fishercoder/solutions/_174.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.utils.CommonUtils; - -/** - * 174. Dungeon Game - - The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess. - - The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately. - - Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers). - - In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step. - - Write a function to determine the knight's minimum initial health so that he is able to rescue the princess. - - For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN. - -2 (K) -3 3 - -5 -10 1 - 10 30 -5 (P) - - Note: - - The knight's health has no upper bound. - Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned. - - */ -public class _174 { - - public static class Solution1 { - /** This problem should fill the dp matrix from bottom right. */ - public int calculateMinimumHP(int[][] dungeon) { - if (dungeon == null || dungeon.length == 0) { - return 0; - } - - int height = dungeon.length; - int width = dungeon[0].length; - int[][] dp = new int[height][width]; - dp[height - 1][width - 1] = - (dungeon[height - 1][width - 1] > 0) ? 1 : 1 - dungeon[height - 1][width - 1]; - - //fill the last column - for (int i = height - 2; i >= 0; i--) { - int temp = dp[i + 1][width - 1] - dungeon[i][width - 1]; - dp[i][width - 1] = Math.max(1, temp); - } - - //fill the last row - for (int j = width - 2; j >= 0; j--) { - int temp = dp[height - 1][j + 1] - dungeon[height - 1][j]; - dp[height - 1][j] = Math.max(temp, 1); - } - - for (int i = height - 2; i >= 0; i--) { - for (int j = width - 2; j >= 0; j--) { - int down = Math.max(1, dp[i + 1][j] - dungeon[i][j]); - int right = Math.max(1, dp[i][j + 1] - dungeon[i][j]); - dp[i][j] = Math.min(down, right); - } - } - - CommonUtils.printMatrix(dp); - return dp[0][0]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_179.java b/src/main/java/com/fishercoder/solutions/_179.java deleted file mode 100644 index 774656352e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_179.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.Comparator; - -/** - * 179. Largest Number - - Given a list of non negative integers, arrange them such that they form the largest number. - - Example 1: - - Input: [10,2] - Output: "210" - - Example 2: - - Input: [3,30,34,5,9] - Output: "9534330" - - Note: The result may be very large, so you need to return a string instead of an integer. - */ -public class _179 { - - public static class Solution1 { - public String largestNumber(int[] num) { - if (num.length == 0) { - return ""; - } - if (num.length == 1) { - return Integer.toString(num[0]); - } - String[] str = new String[num.length]; - for (int i = 0; i < num.length; i++) { - str[i] = Integer.toString(num[i]); - } - Arrays.sort(str, new StringComparator()); - StringBuilder sb = new StringBuilder(""); - for (int i = num.length - 1; i >= 0; i--) { - sb.append(str[i]); - } - if (sb.charAt(0) == '0') { - return "0"; - } - return sb.toString(); - } - - class StringComparator implements Comparator { - public int compare(String s1, String s2) { - if (s1.length() == 0 && s2.length() == 0) { - return 0; - } - if (s2.length() == 0) { - return 1; - } - if (s1.length() == 0) { - return -1; - } - for (int i = 0; i < s1.length() && i < s2.length(); i++) { - if (s1.charAt(i) > s2.charAt(i)) { - return 1; - } else if (s1.charAt(i) < s2.charAt(i)) { - return -1; - } - } - if (s1.length() == s2.length()) { - return 0; - } - if (s1.length() > s2.length()) { - if (s1.charAt(0) < s1.charAt(s2.length())) { - return 1; - } else if (s1.charAt(0) > s1.charAt(s2.length())) { - return -1; - } else { - return compare(s1.substring(s2.length()), s2); - } - } else { - if (s2.charAt(0) < s2.charAt(s1.length())) { - return -1; - } else if (s2.charAt(0) > s2.charAt(s1.length())) { - return 1; - } else { - return compare(s1, s2.substring(s1.length())); - } - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_18.java b/src/main/java/com/fishercoder/solutions/_18.java deleted file mode 100644 index cdb82bf63b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_18.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class _18 { - - public static class Solution1 { - public List> fourSum(int[] nums, int target) { - List> result = new ArrayList(); - if (nums == null || nums.length == 0) { - return result; - } - Arrays.sort(nums); - for (int i = 0; i < nums.length - 3; i++) { - if (i > 0 && nums[i - 1] == nums[i]) { - continue; - } - for (int j = i + 1; j < nums.length - 2; j++) { - if (j > i + 1 && nums[j - 1] == nums[j]) { - continue; - } - int left = j + 1; - int right = nums.length - 1; - while (left < right) { - int sum = nums[i] + nums[j] + nums[left] + nums[right]; - if (sum == target) { - result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); - while (left + 1 < right && nums[left] == nums[left + 1]) { - left++; - } - while (right - 1 > left && nums[right] == nums[right - 1]) { - right--; - } - left++; - right--; - } else if (sum > target) { - right--; - } else { - left++; - } - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_186.java b/src/main/java/com/fishercoder/solutions/_186.java deleted file mode 100644 index d2f34f6e09..0000000000 --- a/src/main/java/com/fishercoder/solutions/_186.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 186. Reverse Words in a String II - - Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. - - The input string does not contain leading or trailing spaces and the words are always separated by a single space. - - For example, - Given s = "the sky is blue", - return "blue is sky the". - - Could you do it in-place without allocating extra space? - */ -public class _186 { - public static class Solution1 { - public void reverseWords(char[] s) { - // Three steps to reverse - // 1, reverse the whole sentence - reverse(s, 0, s.length - 1); - // 2, reverse each word - int start = 0; - for (int i = 0; i < s.length; i++) { - if (s[i] == ' ') { - reverse(s, start, i - 1); - start = i + 1; - } - } - // 3, reverse the last word, if there is only one word this will solve the corner case - reverse(s, start, s.length - 1); - } - - private void reverse(char[] s, int start, int end) { - while (start < end) { - char temp = s[start]; - s[start++] = s[end]; - s[end--] = temp; - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_187.java b/src/main/java/com/fishercoder/solutions/_187.java deleted file mode 100644 index 736cb70797..0000000000 --- a/src/main/java/com/fishercoder/solutions/_187.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 187. Repeated DNA Sequences - * - * All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, - * for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. - * Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. - - For example, - - Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", - - Return: - ["AAAAACCCCC", "CCCCCAAAAA"]. - */ -public class _187 { - public static class Solution1 { - public List findRepeatedDnaSequences(String s) { - Map map = new HashMap(); - for (int i = 0; i < s.length() - 9; i++) { - String sequence = s.substring(i, i + 10); - map.put(sequence, map.getOrDefault(sequence, 0) + 1); - } - List repeatedSequences = new ArrayList<>(); - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() > 1) { - repeatedSequences.add(entry.getKey()); - } - } - return repeatedSequences; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_188.java b/src/main/java/com/fishercoder/solutions/_188.java deleted file mode 100644 index b5874737ad..0000000000 --- a/src/main/java/com/fishercoder/solutions/_188.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -/** - - 188. Best Time to Buy and Sell Stock IV - - Say you have an array for which the ith element is the price of a given stock on day i. - - Design an algorithm to find the maximum profit. You may complete at most k transactions. - - Note: - You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). - - Example 1: - Input: [2,4,1], k = 2 - Output: 2 - Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2. - - Example 2: - Input: [3,2,6,5,0,3], k = 2 - Output: 7 - Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. - Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. - - */ -public class _188 { - public static class Solution1 { - /** credit: https://discuss.leetcode.com/topic/8984/a-concise-dp-solution-in-java */ - public int maxProfit(int k, int[] prices) { - int len = prices.length; - if (k >= len / 2) { - return quickSolve(prices); - } - - int[][] t = new int[k + 1][len]; - for (int i = 1; i <= k; i++) { - int tmpMax = -prices[0]; - for (int j = 1; j < len; j++) { - t[i][j] = Math.max(t[i][j - 1], prices[j] + tmpMax); - tmpMax = Math.max(tmpMax, t[i - 1][j - 1] - prices[j]); - } - } - return t[k][len - 1]; - } - - private int quickSolve(int[] prices) { - int len = prices.length; - int profit = 0; - for (int i = 1; i < len; i++) { - // as long as there is a price gap, we gain a profit. - if (prices[i] > prices[i - 1]) { - profit += prices[i] - prices[i - 1]; - } - } - return profit; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_189.java b/src/main/java/com/fishercoder/solutions/_189.java deleted file mode 100644 index 6a7a73003e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_189.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 189. Rotate Array - - Given an array, rotate the array to the right by k steps, where k is non-negative. - - Example 1: - Input: [1,2,3,4,5,6,7] and k = 3 - Output: [5,6,7,1,2,3,4] - Explanation: - rotate 1 steps to the right: [7,1,2,3,4,5,6] - rotate 2 steps to the right: [6,7,1,2,3,4,5] - rotate 3 steps to the right: [5,6,7,1,2,3,4] - - Example 2: - Input: [-1,-100,3,99] and k = 2 - Output: [3,99,-1,-100] - Explanation: - rotate 1 steps to the right: [99,-1,-100,3] - rotate 2 steps to the right: [3,99,-1,-100] - - Note: - Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. - Could you do it in-place with O(1) extra space? - * */ - -public class _189 { - - public static class Solution1 { - /** - * O(n*k) time - * O(1) space - */ - public void rotate(int[] nums, int k) { - int tmp; - for (int i = 0; i < k; i++) { - tmp = nums[nums.length - 1]; - for (int j = nums.length - 1; j > 0; j--) { - nums[j] = nums[j - 1]; - } - nums[0] = tmp; - } - } - } - - public static class Solution2 { - /** - * using an extra array of the same size to copy it - * O(n) time - * O(n) space - */ - public void rotate(int[] nums, int k) { - int len = nums.length; - int[] tmp = new int[len]; - for (int i = 0; i < len; i++) { - tmp[(i + k) % len] = nums[i]; - } - for (int i = 0; i < len; i++) { - nums[i] = tmp[i]; - } - } - } - - public static class Solution3 { - /** - * reverse three times - * O(n) time - * O(1) space - */ - public void rotate(int[] nums, int k) { - int len = nums.length; - k %= len; - reverse(nums, 0, len - 1); - reverse(nums, 0, k - 1); - reverse(nums, k, len - 1); - } - - private void reverse(int[] nums, int start, int end) { - while (start < end) { - int tmp = nums[start]; - nums[start] = nums[end]; - nums[end] = tmp; - start++; - end--; - } - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_19.java b/src/main/java/com/fishercoder/solutions/_19.java deleted file mode 100644 index 64e9cab15b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_19.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.CommonUtils; - -public class _19 { - - public static class Solution1 { - /** - * Naive/most straightforward approach: - * go through the list, find its total length, then go through the list a second time: - * this time, pause at the delta point, then assign its next.next pointer to next. - * This approach has to traverse the list twice, not one-pass. - */ - public ListNode removeNthFromEnd(ListNode head, int n) { - ListNode temp = head; - int len = 0; - while (temp != null) { - temp = temp.next; - len++; - } - if (n == len) { - return head.next; - } - - temp = head; - int cut = len - n; - while (cut-- > 1) { - temp = temp.next; - } - if (temp.next != null) { - temp.next = temp.next.next; - return head; - } - return null; - } - } - - public static class Solution2 { - public ListNode removeNthFromEnd(ListNode head, int n) { - //this approach uses two pointers, fast moves first for n nodes, when fast reaches n, then we start to move slow - //then, when fast reaches null, slow reaches the point where the node should be deleted. - ListNode dummy = new ListNode(-1); - dummy.next = head; - ListNode slow = head; - ListNode fast = head; - int tempN = n; - while (tempN-- > 0) { - fast = fast.next; - } - - if (fast == null) { - if (n > 0) { - // this is for cases like this: [1,2] 2 or [1,2,3,4] 4, namely, remove the head of - // the list and return the second node from the original list - dummy.next = dummy.next.next; - } - return dummy.next; - } - - fast = fast.next;//we'll have to move fast pointer one node forward before moving the two together, this way, - //when fast reaches null, slow will be at the previous node to the node that should be deleted, thus, we can change the next pointer easily - - while (fast != null) { - fast = fast.next; - slow = slow.next; - } - - if (slow.next != null) { - slow.next = slow.next.next; - } - return dummy.next; - } - } - - public static class Solution3 { - //a more concise version using the same idea - public ListNode removeNthFromEnd(ListNode head, int n) { - ListNode dummy = new ListNode(-1); - dummy.next = head; - ListNode slow = dummy; - ListNode fast = dummy; - while (fast.next != null) { - if (n <= 0) { - slow = slow.next; - } - fast = fast.next; - n--; - } - if (slow.next != null) { - slow.next = slow.next.next; - } - return dummy.next; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_190.java b/src/main/java/com/fishercoder/solutions/_190.java deleted file mode 100644 index 023c6c8665..0000000000 --- a/src/main/java/com/fishercoder/solutions/_190.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 190. Reverse Bits - * Reverse bits of a given 32 bits unsigned integer. - - Example: - Input: 43261596 - Output: 964176192 - - Explanation: 43261596 represented in binary as 00000010100101000001111010011100, - return 964176192 represented in binary as 00111001011110000010100101000000. - - Follow up: - If this function is called many times, how would you optimize it? - */ - -public class _190 { - /**delimiting the binary string into 4 bits array will make it easier to see/visualize: - * original binary format: - * 0000,0010,1001,0100,0001,1110,1001,1100, - * after reversing, becomes: - * 0011,1001,0111,1000,0010,1001,0100,0000 - * The most right side digit shifted to the most left side, the 2nd right side digit shifted to the 2nd left side, so forth..*/ - - /**This post: http://stackoverflow.com/questions/2811319/difference-between-and - * gives a good explanation between logical right shift: ">>>" and arithmetic right shift: ">>". - * Basically, ">>" preserves the most left bit and treats it as the sign for this number, - * e.g. -2 represented in 8 bits is 11111110, thus -2 >> 1 will become 11111111, i.e. -1 - * notice its sign bit (the most left one bit) is preserved - * However, logical right shift ">>>" doesn't care about the first bit on the most left, - * it simply shifts every bit to the right. - * e.g. -2 >>> 1 would become 1111111111111111111111111111111, i.e. 2147483647*/ - - public static class Solution1 { - // you need treat n as an unsigned value - public int reverseBits(int n) { - int res = 0; - for (int i = 0; i < 32; i++) { - res += n & 1;//get the most right bit each time - n >>>= 1;//do UN-signed right shift by 1 each time - if (i < 31) { - res <<= - 1;//shift this number to the left by 1 each time, so that eventually, this number is reversed - } - } - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_191.java b/src/main/java/com/fishercoder/solutions/_191.java deleted file mode 100644 index a52181fcf9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_191.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 191. Number of 1 Bits - * - * Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). - * For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.*/ - -public class _191 { - - public static class Solution1 { - /**Doing bitwise AND operation between n and n-1 will always flip the least significant 1 bit in n to zero - example run for the above editorial solution: input 5, n will be 5&4 and becomes 4, - then in the next run, n will become 4&3 which is 0, thus exit the while loop.*/ - public int hammingWeight(int n) { - int bits = 0; - while (n != 0) { - bits++; - n &= (n - 1); - } - return bits; - } - } - - public static class Solution2 { - public int hammingWeight(int n) { - int bits = 0; - int mask = 1; - for (int i = 0; i < 32; i++) { - if ((n & mask) != 0) { - bits++; - } - mask <<= 1; - } - return bits; - } - } - - public static class Solution3 { - public int hammingWeight(int n) { - int bits = 0; - for (int i = 0; i < 32; i++) { - if ((n & 1) == 1) { - bits++; - } - if (n == 0) { - return bits; - } - /**must use unsigned right shift operator since the problem says this is an unsigned value*/ - n >>>= 1; - } - return bits; - } - } - - public static class Solution4 { - public int hammingWeight(int n) { - int bits = 0; - for (int i = 0; i < 32; i++) { - bits += n & 1; - n >>>= 1; - } - return bits; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_198.java b/src/main/java/com/fishercoder/solutions/_198.java deleted file mode 100644 index 8cdc615ee0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_198.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -/**198. House Robber - - You are a professional robber planning to rob houses along a street. - Each house has a certain amount of money stashed, - the only constraint stopping you from robbing each of them is that adjacent houses have security - system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. - Given a list of non-negative integers representing the amount of money of each house, - determine the maximum amount of money you can rob tonight without alerting the police. - - Example 1: - Input: [1,2,3,1] - Output: 4 - Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). - Total amount you can rob = 1 + 3 = 4. - - Example 2: - Input: [2,7,9,3,1] - Output: 12 - Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). - Total amount you can rob = 2 + 9 + 1 = 12. - */ -public class _198 { - - public static class Solution1 { - public int rob(int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - if (nums.length == 1) { - return nums[0]; - } - if (nums.length == 2) { - return Math.max(nums[0], nums[1]); - } - int[] dp = new int[nums.length]; - dp[0] = nums[0]; - dp[1] = Math.max(nums[0], nums[1]); - for (int i = 2; i < nums.length; i++) { - dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); - } - return dp[nums.length - 1]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_199.java b/src/main/java/com/fishercoder/solutions/_199.java deleted file mode 100644 index 9f0f7e8eec..0000000000 --- a/src/main/java/com/fishercoder/solutions/_199.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -/**199. Binary Tree Right Side View - -Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. - -For example: -Given the following binary tree, - - 1 <--- - / \ -2 3 <--- - \ \ - 5 4 <--- - -You should return [1, 3, 4]. */ - -public class _199 { - - public static class Solution1 { - /**credit: https://leetcode.com/problems/binary-tree-right-side-view/discuss/56012/My-simple-accepted-solution(JAVA)*/ - public List rightSideView(TreeNode root) { - List result = new ArrayList<>(); - rightView(root, result, 0); - return result; - } - - void rightView(TreeNode curr, List result, int currDepth) { - if (curr == null) { - return; - } - if (currDepth == result.size()) { - result.add(curr.val); - } - rightView(curr.right, result, currDepth + 1); - rightView(curr.left, result, currDepth + 1); - } - } - - public static class Solution2 { - /**BFS the tree*/ - public List rightSideView(TreeNode root) { - List result = new ArrayList<>(); - if (root == null) { - return result; - } - Queue q = new LinkedList<>(); - q.offer(root); - while (!q.isEmpty()) { - int size = q.size(); - for (int i = 0; i < size; i++) { - TreeNode curr = q.poll(); - if (i == size - 1) { - result.add(curr.val); - } - if (curr.left != null) { - q.offer(curr.left); - } - if (curr.right != null) { - q.offer(curr.right); - } - } - } - return result; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_2.java b/src/main/java/com/fishercoder/solutions/_2.java deleted file mode 100644 index 7bb909c485..0000000000 --- a/src/main/java/com/fishercoder/solutions/_2.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -public class _2 { - public static class Solution1 { - public ListNode addTwoNumbers(ListNode l1, ListNode l2) { - ListNode result = new ListNode(0); - ListNode tmp = result; - int sum = 0; - while (l1 != null || l2 != null) { - sum /= 10; - if (l1 != null) { - sum += l1.val; - l1 = l1.next; - } - if (l2 != null) { - sum += l2.val; - l2 = l2.next; - } - tmp.next = new ListNode(sum % 10); - tmp = tmp.next; - } - if (sum / 10 == 1) { - tmp.next = new ListNode(1);//this means there's a carry, so we add additional 1, e.g. [5] + [5] = [0, 1] - } - return result.val == 0 ? result.next : result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_200.java b/src/main/java/com/fishercoder/solutions/_200.java deleted file mode 100644 index a056d5a17c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_200.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 200. Number of Islands - * - * Given a 2d grid map of '1's (land) and '0's (water), - * count the number of islands. - * An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. - * You may assume all four edges of the grid are all surrounded by water. - - Example 1: - 11110 - 11010 - 11000 - 00000 - Answer: 1 - - Example 2: - 11000 - 11000 - 00100 - 00011 - Answer: 3 - - */ -public class _200 { - - public static class Solution1 { - - public int numIslands(char[][] grid) { - if (grid == null || grid.length == 0) { - return 0; - } - int count = 0; - int m = grid.length; - int n = grid[0].length; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == '1') { - count++; - dfs(grid, i, j, m, n); - } - } - } - return count; - } - - void dfs(char[][] grid, int i, int j, int m, int n) { - if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') { - return; - } - grid[i][j] = '0'; - dfs(grid, i + 1, j, m, n); - dfs(grid, i, j + 1, m, n); - dfs(grid, i - 1, j, m, n); - dfs(grid, i, j - 1, m, n); - } - } - - public static class Solution2 { - - class UnionFind { - int count; - int m; - int n; - int[] ids; - - public UnionFind(char[][] grid) { - m = grid.length; - n = grid[0].length; - ids = new int[m * n]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == '1') { - count++; - ids[i * n + j] = i * n + j; - } - } - } - } - - public void union(int i, int j) { - int x = find(ids, i); - int y = find(ids, j); - if (x != y) { - /**note: this is when x != y, only in this case, we should union these two nodes, which makes sense naturally.*/ - count--; - ids[x] = y;//ids[y] = x; //also works - } - } - - public int find(int[] ids, int i) { - if (ids[i] == i) { - return i; - } - return find(ids, ids[i]); - } - } - - public int numIslands(char[][] grid) { - if (grid == null || grid.length == 0 || grid[0].length == 0) { - return 0; - } - int[] dirs = new int[]{0, 1, 0, -1, 0}; - UnionFind uf = new UnionFind(grid); - int m = grid.length; - int n = grid[0].length; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == '1') { - for (int k = 0; k < 4; k++) { - int x = i + dirs[k]; - int y = j + dirs[k + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1') { - int id1 = i * n + j; - int id2 = x * n + y; - uf.union(id1, id2); - } - } - } - } - } - return uf.count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_201.java b/src/main/java/com/fishercoder/solutions/_201.java deleted file mode 100644 index c5df6ed0d4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_201.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 201. Bitwise AND of Numbers Range - * - * Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. - * - * Example 1: - * Input: [5,7] - * Output: 4 - * - * Example 2: - * Input: [0,1] - * Output: 0 - * - */ -public class _201 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/28538/java-python-easy-solution-with-explanation - * Bitwise AND operation within range actually turns out to be doing some operations with just these two boundary numbers: m and n - * e.g. 5 and 7, in binary, they are 101 and 111, the result for [5,7] is 5&6&7 which is 101&110&111 - * this we can understand it to be shifting the digits of m and n from left to right until they become the same, then we pad that number with zeroes on the right side - */ - public int rangeBitwiseAnd(int m, int n) { - int i = 0; - while (m != n) { - m >>= 1; - n >>= 1; - i++; - } - return (n << i); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_204.java b/src/main/java/com/fishercoder/solutions/_204.java deleted file mode 100644 index d9a484d754..0000000000 --- a/src/main/java/com/fishercoder/solutions/_204.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 204. Count Primes - * - * Description: - - Count the number of prime numbers less than a non-negative number, n. - - Hint: - - Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. - The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? - - As we know the number must not be divisible by any number > n / 2, - we can immediately cut the total iterations half by dividing only up to n / 2. Could we still do better? - - Let's write down all of 12's factors: - - 2 × 6 = 12 - 3 × 4 = 12 - 4 × 3 = 12 - 6 × 2 = 12 - As you can see, calculations of 4 × 3 and 6 × 2 are not necessary. Therefore, we only need to consider factors up to √n because, - if n is divisible by some number p, then n = p × q and since p ≤ q, we could derive that p ≤ √n. - - Our total runtime has now improved to O(n1.5), which is slightly better. Is there a faster approach? - - public int countPrimes(int n) { - int count = 0; - for (int i = 1; i < n; i++) { - if (isPrime(i)) count++; - } - return count; - } - - private boolean isPrime(int num) { - if (num <= 1) return false; - // Loop's ending condition is i * i <= num instead of i <= sqrt(num) - // to avoid repeatedly calling an expensive function sqrt(). - for (int i = 2; i * i <= num; i++) { - if (num % i == 0) return false; - } - return true; - } - - The Sieve of Eratosthenes is one of the most efficient ways to find all prime numbers up to n. - But don't let that name scare you, I promise that the concept is surprisingly simple. - - Sieve of Eratosthenes: algorithm steps for primes below 121. "Sieve of Eratosthenes Animation" by SKopp is licensed under CC BY 2.0. - We start off with a table of n numbers. Let's look at the first number, 2. We know all multiples of 2 must not be primes, so we mark - them off as non-primes. Then we look at the next number, 3. Similarly, all multiples of 3 such as 3 × 2 = 6, 3 × 3 = 9, ... must not - be primes, so we mark them off as well. Now we look at the next number, 4, which was already marked off. - What does this tell you? Should you mark off all multiples of 4 as well? - 4 is not a prime because it is divisible by 2, which means all multiples of 4 must also be divisible by 2 and were already marked off. - So we can skip 4 immediately and go to the next number, 5. - Now, all multiples of 5 such as 5 × 2 = 10, 5 × 3 = 15, 5 × 4 = 20, 5 × 5 = 25, ... can be marked off. - There is a slight optimization here, we do not need to start from 5 × 2 = 10. Where should we start marking off? - - In fact, we can mark off multiples of 5 starting at 5 × 5 = 25, because 5 × 2 = 10 was already marked off by multiple of 2, - similarly 5 × 3 = 15 was already marked off by multiple of 3. - Therefore, if the current number is p, we can always mark off multiples of p starting at p2, then in increments of p: p2 + p, p2 + 2p, ... - Now what should be the terminating loop condition? - - It is easy to say that the terminating loop condition is p < n, which is certainly correct but not efficient. Do you still remember Hint #3? - Yes, the terminating loop condition can be p < √n, as all non-primes ≥ √n must have already been marked off. When the loop terminates, - all the numbers in the table that are non-marked are prime. - - The Sieve of Eratosthenes uses an extra O(n) memory and its runtime complexity is O(n log log n). - For the more mathematically inclined readers, you can read more about its algorithm complexity on Wikipedia. - - public int countPrimes(int n) { - boolean[] isPrime = new boolean[n]; - for (int i = 2; i < n; i++) { - isPrime[i] = true; - } - - // Loop's ending condition is i * i < n instead of i < sqrt(n) - // to avoid repeatedly calling an expensive function sqrt(). - - for (int i = 2; i * i < n; i++) { - if (!isPrime[i]) continue; - for (int j = i * i; j < n; j += i) { - isPrime[j] = false; - } - } - int count = 0; - for (int i = 2; i < n; i++) { - if (isPrime[i]) count++; - } - - return count; - } - */ -public class _204 { - - public static class Solution1 { - public int countPrimes(int n) { - boolean[] notPrime = new boolean[n]; - int count = 0; - for (int i = 2; i < n; i++) { - if (!notPrime[i]) { - count++; - for (int j = 2; i * j < n; j++) { - notPrime[i * j] = true; - } - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_205.java b/src/main/java/com/fishercoder/solutions/_205.java deleted file mode 100644 index 6ff2981c3f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_205.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; -/** - * 205. Isomorphic Strings - * - * Given two strings s and t, determine if they are isomorphic. - - Two strings are isomorphic if the characters in s can be replaced to get t. - - All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself. - - For example, - Given "egg", "add", return true. - - Given "foo", "bar", return false. - - Given "paper", "title", return true. - - Note: - You may assume both s and t have the same length.*/ -public class _205 { - /**space should be O(1) since it only has alphabetic letters which are capped at 52.*/ - - public static class Solution1 { - public boolean isIsomorphic(String s, String t) { - if (s == null || s.length() == 0) { - return (t == null || t.length() == 0); - } - if (t == null || t.length() == 0) { - return (s == null || s.length() == 0); - } - char[] schar = s.toCharArray(); - char[] tchar = t.toCharArray(); - Map map = new HashMap(); - if (s.length() != t.length()) { - return false; - } - for (int i = 0; i < s.length(); i++) { - if (map.containsKey(schar[i])) { - if (map.get(schar[i]) != tchar[i]) { - return false; - } - } else { - if (map.containsValue(tchar[i])) { - return false;//this line is necessary for this case: ("ab", "aa") - } - map.put(schar[i], tchar[i]); - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_206.java b/src/main/java/com/fishercoder/solutions/_206.java deleted file mode 100644 index e41490c7ef..0000000000 --- a/src/main/java/com/fishercoder/solutions/_206.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 206. Reverse Linked List - * - * Reverse a singly linked list. - * - * Example: - * Input: 1->2->3->4->5->NULL - * Output: 5->4->3->2->1->NULL - * - * Follow up: - * A linked list can be reversed either iteratively or recursively. Could you implement both? - * */ -public class _206 { - - public static class Solution1 { - public ListNode reverseList(ListNode head) { - ListNode prev = null; - ListNode curr = head; - while (curr != null) { - ListNode next = curr.next; - curr.next = prev; - prev = curr; - curr = next; - } - return prev; - } - } - - public static class Solution2 { - public ListNode reverseList(ListNode head) { - return reverse(head, null); - } - - ListNode reverse(ListNode head, ListNode newHead) { - if (head == null) { - return newHead; - } - ListNode next = head.next; - head.next = newHead; - newHead = head; - head = next; - return reverse(head, newHead); - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_207.java b/src/main/java/com/fishercoder/solutions/_207.java deleted file mode 100644 index 500e2dc8b2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_207.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - -/** - * 207. Course Schedule - * - * There are a total of n courses you have to take, labeled from 0 to n - 1. - Some courses may have prerequisites, for example to take course 0 you have to first take course 1, - which is expressed as a pair: [0,1] - Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses? - - For example: - 2, [[1,0]] - There are a total of 2 courses to take. - To take course 1 you should have finished course 0. So it is possible. - - 2, [[1,0],[0,1]] - There are a total of 2 courses to take. - To take course 1 you should have finished course 0, - and to take course 0 you should also have finished course 1. So it is impossible. - - Note: - The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented. - You may assume that there are no duplicate edges in the input prerequisites. - click to show more hints. - - Hints: - This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. - Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. - Topological sort could also be done via BFS. - */ -public class _207 { - - public static class Solution1 { - public boolean canFinish(int numCourses, int[][] prerequisites) { - int[] indegree = new int[numCourses]; - for (int[] prereq : prerequisites) { - indegree[prereq[0]]++; - } - Set zeroDegree = new HashSet(); - for (int i = 0; i < numCourses; i++) { - if (indegree[i] == 0) { - zeroDegree.add(i); - } - } - if (zeroDegree.isEmpty()) { - return false; - } - - while (!zeroDegree.isEmpty()) { - Iterator it = zeroDegree.iterator(); - int course = it.next(); - zeroDegree.remove(course); - for (int[] prereq : prerequisites) { - if (prereq[1] == course) { - indegree[prereq[0]]--; - if (indegree[prereq[0]] == 0) { - zeroDegree.add(prereq[0]); - } - } - } - } - - for (int i : indegree) { - if (i != 0) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_208.java b/src/main/java/com/fishercoder/solutions/_208.java deleted file mode 100644 index e177a0211d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_208.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 208. Implement Trie (Prefix Tree) - * - * Implement a trie with insert, search, and startsWith methods. - * - * Example: - * - * Trie trie = new Trie(); - * - * trie.insert("apple"); - * trie.search("apple"); // returns true - * trie.search("app"); // returns false - * trie.startsWith("app"); // returns true - * trie.insert("app"); - * trie.search("app"); // returns true - * Note: - * - * You may assume that all inputs are consist of lowercase letters a-z. - * All inputs are guaranteed to be non-empty strings. - */ - -public class _208 { - public static class Solution1 { - class TrieNode { - - char val; - boolean isWord; - TrieNode[] children = new TrieNode[26]; - - // Initialize your data structure here. - public TrieNode() { - } - - public TrieNode(char c) { - this.val = c; - } - } - - public class Trie { - private TrieNode root; - - public Trie() { - root = new TrieNode(); - root.val = ' ';//initialize root to be an empty char, this is a common practice as how Wiki defines Trie data structure as well - } - - // Inserts a word into the trie. - public void insert(String word) { - TrieNode node = root; - for (int i = 0; i < word.length(); i++) { - if (node.children[word.charAt(i) - 'a'] == null) { - node.children[word.charAt(i) - 'a'] = new TrieNode(word.charAt(i)); - } - node = node.children[word.charAt(i) - 'a']; - } - node.isWord = true; - } - - // Returns if the word is in the trie. - public boolean search(String word) { - TrieNode node = root; - for (int i = 0; i < word.length(); i++) { - if (node.children[word.charAt(i) - 'a'] == null) { - return false; - } - node = node.children[word.charAt(i) - 'a']; - } - return node.isWord; - } - - // Returns if there is any word in the trie - // that starts with the given prefix. - public boolean startsWith(String prefix) { - TrieNode node = root; - for (int i = 0; i < prefix.length(); i++) { - if (node.children[prefix.charAt(i) - 'a'] == null) { - return false; - } - node = node.children[prefix.charAt(i) - 'a']; - } - return true; - } - } - - // Your Trie object will be instantiated and called as such: - // Trie trie = new Trie(); - // trie.insert("somestring"); - // trie.search("key"); - } -} diff --git a/src/main/java/com/fishercoder/solutions/_209.java b/src/main/java/com/fishercoder/solutions/_209.java deleted file mode 100644 index a6a93ccb53..0000000000 --- a/src/main/java/com/fishercoder/solutions/_209.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 209. Minimum Size Subarray Sum - * - * Given an array of n positive integers and a positive integer s, - * find the minimal length of a contiguous subarray of which the sum ≥ s. - * If there isn't one, return 0 instead. - - For example, given the array [2,3,1,2,4,3] and s = 7, - the subarray [4,3] has the minimal length under the problem constraint. - - click to show more practice. - - More practice: - If you have figured out the O(n) solution, - try coding another solution of which the time complexity is O(n log n). - */ -public class _209 { - - public static class Solution1 { - public int minSubArrayLen(int s, int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - int i = 0; - int j = 0; - int min = Integer.MAX_VALUE; - int sum = 0; - while (j < nums.length) { - sum += nums[j++]; - - while (sum >= s) { - min = Math.min(min, j - i); - sum -= nums[i++]; - } - } - return min == Integer.MAX_VALUE ? 0 : min; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_210.java b/src/main/java/com/fishercoder/solutions/_210.java deleted file mode 100644 index d466503ab3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_210.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.Queue; -import java.util.Set; - -/** - * 210. Course Schedule II - * - * There are a total of n courses you have to take, labeled from 0 to n - 1. - Some courses may have prerequisites, for example to take course 0 you have to first take course 1, - which is expressed as a pair: [0,1] - Given the total number of courses and a list of prerequisite pairs, - return the ordering of courses you should take to finish all courses. - - There may be multiple correct orders, - you just need to return one of them. - If it is impossible to finish all courses, return an empty array. - - For example: - - 2, [[1,0]] - There are a total of 2 courses to take. - To take course 1 you should have finished course 0. So the correct course order is [0,1] - - 4, [[1,0],[2,0],[3,1],[3,2]] - There are a total of 4 courses to take. - To take course 3 you should have finished both courses 1 and 2. - Both courses 1 and 2 should be taken after you finished course 0. - So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3]. - - Note: - The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented. - You may assume that there are no duplicate edges in the input prerequisites. - click to show more hints. - - Hints: - This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. - Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. - Topological sort could also be done via BFS. - */ -public class _210 { - - public static class Solution1 { - public int[] findOrder(int numCourses, int[][] prerequisites) { - int[] inDegree = new int[numCourses]; - for (int[] course : prerequisites) { - inDegree[course[0]]++; - } - - Set zeroDegree = new HashSet(); - Queue queue = new LinkedList(); - for (int i = 0; i < numCourses; i++) { - if (inDegree[i] == 0) { - zeroDegree.add(i); - queue.offer(i); - } - } - - if (zeroDegree.isEmpty()) { - return new int[0]; - } - - while (!zeroDegree.isEmpty()) { - Iterator it = zeroDegree.iterator(); - int course = it.next(); - zeroDegree.remove(course); - for (int[] pre : prerequisites) { - if (course == pre[1]) { - inDegree[pre[0]]--; - if (inDegree[pre[0]] == 0) { - zeroDegree.add(pre[0]); - queue.offer(pre[0]); - } - } - } - } - - for (int i = 0; i < numCourses; i++) { - if (inDegree[i] != 0) { - return new int[0]; - } - } - - int[] result = new int[queue.size()]; - int i = 0; - while (!queue.isEmpty()) { - result[i++] = queue.poll(); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_211.java b/src/main/java/com/fishercoder/solutions/_211.java deleted file mode 100644 index 2512233bcf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_211.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 211. Add and Search Word - Data structure design - * - * Design a data structure that supports the following two operations: - - void addWord(word) - bool search(word) - search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter. - - For example: - - addWord("bad") - addWord("dad") - addWord("mad") - search("pad") -> false - search("bad") -> true - search(".ad") -> true - search("b..") -> true - Note: - You may assume that all words are consist of lowercase letters a-z. - - You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. - */ -public class _211 { - public static class Solution1 { - public static class WordDictionary { - WordNode root; - - /** Initialize your data structure here. */ - public WordDictionary() { - root = new WordNode(); - } - - public void addWord(String word) { - char[] chars = word.toCharArray(); - addWord(chars, 0, root); - } - - private void addWord(char[] chars, int index, WordNode parent) { - char c = chars[index]; - int idx = c - 'a'; - WordNode node = parent.children[idx]; - if (node == null) { - node = new WordNode(); - parent.children[idx] = node; - } - if (chars.length == index + 1) { - node.isLeaf = true; - return; - } - addWord(chars, ++index, node); - } - - public boolean search(String word) { - return search(word.toCharArray(), 0, root); - } - - /** This is also a beautifully designed recursive function. */ - private boolean search(char[] chars, int index, WordNode parent) { - if (index == chars.length) { - if (parent.isLeaf) { - return true; - } - return false; - } - WordNode[] childNodes = parent.children; - char c = chars[index]; - if (c == '.') { - for (int i = 0; i < childNodes.length; i++) { - WordNode n = childNodes[i]; - if (n != null) { - boolean b = search(chars, index + 1, n); - if (b) { - return true; - } - } - } - return false; - } - WordNode node = childNodes[c - 'a']; - if (node == null) { - return false; - } - return search(chars, ++index, node); - } - - /** This is a cool/standard design for a Trie node class. */ - private class WordNode { - boolean isLeaf; - WordNode[] children = new WordNode[26]; - } - } - - /** - * Your WordDictionary object will be instantiated and called as such: - * WordDictionary obj = new WordDictionary(); - * obj.addWord(word); - * boolean param_2 = obj.search(word); - */ - } -} diff --git a/src/main/java/com/fishercoder/solutions/_212.java b/src/main/java/com/fishercoder/solutions/_212.java deleted file mode 100644 index 91730f3c94..0000000000 --- a/src/main/java/com/fishercoder/solutions/_212.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 212. Word Search II - * Given a 2D board and a list of words from the dictionary, find all words in the board. - - Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word. - - For example, - Given words = ["oath","pea","eat","rain"] and board = - - [ - ['o','a','a','n'], - ['e','t','a','e'], - ['i','h','k','r'], - ['i','f','l','v'] - ] - Return ["eat","oath"]. - Note: - You may assume that all inputs are consist of lowercase letters a-z. - - You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier? - - If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. - What kind of data structure could answer such query efficiently? - Does a hash table work? Why or why not? How about a Trie? - If you would like to learn how to implement a basic trie, please work on this problem: Implement Trie (Prefix Tree) first.*/ -public class _212 { - - public static class Solution1 { - public List findWords(char[][] board, String[] words) { - TrieNode root = buildTrie(words); - List result = new ArrayList(); - for (int i = 0; i < board.length; i++) { - for (int j = 0; j < board[0].length; j++) { - dfs(root, board, i, j, result); - } - } - return result; - } - - private void dfs(TrieNode root, char[][] board, int i, int j, List result) { - char c = board[i][j]; - - if (c == '#' || root.children[c - 'a'] == null) { - return; - } - - if (root.children[c - 'a'].word != null) { - result.add(root.children[c - 'a'].word); - root.children[c - 'a'].word = null;//de-duplicate - } - board[i][j] = '#';//mark it as visited to avoid cycles - if (i > 0) { - dfs(root.children[c - 'a'], board, i - 1, j, result); - } - if (j > 0) { - dfs(root.children[c - 'a'], board, i, j - 1, result); - } - if (i + 1 < board.length) { - dfs(root.children[c - 'a'], board, i + 1, j, result); - } - if (j + 1 < board[0].length) { - dfs(root.children[c - 'a'], board, i, j + 1, result); - } - - board[i][j] = c; - } - - private TrieNode root; - - class TrieNode { - String word; - TrieNode[] children = new TrieNode[26]; - } - - private TrieNode buildTrie(String[] words) { - TrieNode root = new TrieNode(); - for (String word : words) { - char[] chars = word.toCharArray(); - TrieNode temp = root; - for (char c : chars) { - if (temp.children[c - 'a'] == null) { - temp.children[c - 'a'] = new TrieNode(); - } - temp = temp.children[c - 'a']; - } - temp.word = word; - } - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_213.java b/src/main/java/com/fishercoder/solutions/_213.java deleted file mode 100644 index eed6c5cc28..0000000000 --- a/src/main/java/com/fishercoder/solutions/_213.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; -/** - * 213. House Robber II - - Note: This is an extension of House Robber. - - After robbing those houses on that street, - the thief has found himself a new place for his thievery - so that he will not get too much attention. - This time, all houses at this place are arranged in a circle. - That means the first house is the neighbor of the last one. - Meanwhile, the security system for these houses remain the same as for those in the previous street. - -Given a list of non-negative integers representing the amount of money of each house, - determine the maximum amount of money you can rob tonight without alerting the police. -*/ -public class _213 { - public static class Solution1 { - /** - * Another dp problem: - * separate them into two cases: - * 1. rob from house 1 to n-1, get max1 - * 2. rob from house 2 to n, get max2 take the max from the above two max - */ - public int rob(int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - if (nums.length == 1) { - return nums[0]; - } - if (nums.length == 2) { - return Math.max(nums[0], nums[1]); - } - int[] dp = new int[nums.length - 1]; - - //rob 1 to n-1 - dp[0] = nums[0]; - dp[1] = Math.max(nums[0], nums[1]); - for (int i = 2; i < nums.length - 1; i++) { - dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); - } - int prevMax = dp[nums.length - 2]; - - //rob 2 to n - dp = new int[nums.length - 1]; - dp[0] = nums[1]; - dp[1] = Math.max(nums[1], nums[2]); - for (int i = 3; i < nums.length; i++) { - dp[i - 1] = Math.max(dp[i - 3] + nums[i], dp[i - 2]); - } - return Math.max(prevMax, dp[nums.length - 2]); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_214.java b/src/main/java/com/fishercoder/solutions/_214.java deleted file mode 100644 index e20b704e3c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_214.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -/** - 214. Shortest Palindrome - - Given a string S, you are allowed to convert it to a palindrome - by adding characters in front of it. - Find and return the shortest palindrome you can find by performing this transformation. - - For example: - - Given "aacecaaa", return "aaacecaaa". - - Given "abcd", return "dcbabcd". - */ -public class _214 { - - public static class Solution1 { - /**credit: https://discuss.leetcode.com/topic/27261/clean-kmp-solution-with-super-detailed-explanation*/ - /** - * TODO: read it explanation and understand KMP completely. - */ - public String shortestPalindrome(String s) { - String temp = s + "#" + new StringBuilder(s).reverse().toString(); - int[] table = getTable(temp); - //get the maximum palin part in s starts from 0 - return new StringBuilder(s.substring(table[table.length - 1])).reverse().toString() + s; - } - - public int[] getTable(String s) { - //get lookup table - int[] table = new int[s.length()]; - - //pointer that points to matched char in prefix part - int index = 0; - //skip index 0, we will not match a string with itself - for (int i = 1; i < s.length(); i++) { - if (s.charAt(index) == s.charAt(i)) { - //we can extend match in prefix and postfix - table[i] = table[i - 1] + 1; - index++; - } else { - //match failed, we try to match a shorter substring - - //by assigning index to table[i-1], we will shorten the match string length, and jump to the - //prefix part that we used to match postfix ended at i - 1 - index = table[i - 1]; - - while (index > 0 && s.charAt(index) != s.charAt(i)) { - //we will try to shorten the match string length until we revert to the beginning of match (index 1) - index = table[index - 1]; - } - - //when we are here may either found a match char or we reach the boundary and still no luck - //so we need check char match - if (s.charAt(index) == s.charAt(i)) { - //if match, then extend one char - index++; - } - table[i] = index; - } - } - return table; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_216.java b/src/main/java/com/fishercoder/solutions/_216.java deleted file mode 100644 index 2283ae80bb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_216.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 216. Combination Sum III - * - * Find all possible combinations of k numbers that add up to a number n, - * given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. - - Example 1: - Input: k = 3, n = 7 - Output: [[1,2,4]] - - Example 2: - Input: k = 3, n = 9 - Output: [[1,2,6], [1,3,5], [2,3,4]] - */ -public class _216 { - - public static class Solution1 { - public List> combinationSum3(int k, int n) { - List> result = new ArrayList(); - int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; - backtracking(k, n, nums, 0, new ArrayList(), result); - return result; - } - - void backtracking(int k, int n, int[] nums, int start, List curr, List> result) { - if (n > 0) { - for (int i = start; i < nums.length; i++) { - curr.add(nums[i]); - backtracking(k, n - nums[i], nums, i + 1, curr, result); - curr.remove(curr.size() - 1); - } - } else if (n == 0 && curr.size() == k) { - result.add(new ArrayList(curr)); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_217.java b/src/main/java/com/fishercoder/solutions/_217.java deleted file mode 100644 index dd0c337f0e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_217.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 217. Contains Duplicate - * - * Given an array of integers, find if the array contains any - * duplicates. Your function should return true if any value appears at least twice in the array, - * and it should return false if every element is distinct. - */ -public class _217 { - public static class Solution1 { - public boolean containsDuplicate(int[] nums) { - if (nums == null || nums.length == 0) { - return false; - } - Set set = new HashSet(); - for (int i : nums) { - if (!set.add(i)) { - return true; - } - } - return false; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_218.java b/src/main/java/com/fishercoder/solutions/_218.java deleted file mode 100644 index ec8c12de37..0000000000 --- a/src/main/java/com/fishercoder/solutions/_218.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.TreeMap; -/** - * 218. The Skyline Problem - * - * A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. - * Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), - * write a program to output the skyline formed by these buildings collectively (Figure B). - * - * The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], - * where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, - * and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. - * You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0. - * - * For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] . - * The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] - * that uniquely defines a skyline. - * A key point is the left endpoint of a horizontal line segment. - * Note that the last key point, where the rightmost building ends, - * is merely used to mark the termination of the skyline, and always has zero height. - * Also, the ground in between any two adjacent buildings should be considered part of the skyline contour. - - For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ]. - - Notes: - - The number of buildings in any input list is guaranteed to be in the range [0, 10000]. - The input list is already sorted in ascending order by the left x position Li. - The output list must be sorted by the x position. - There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]*/ - -/**This video is super clear and helpful: https://www.youtube.com/watch?v=GSBLe8cKu0s - - Algorithm: -First observation: all the points in the final result come from the four angles that each building has -Scan through the horizontal lines -Use a PriorityQueue to hold each building, and make the PriorityQueue to sort on the height of the buildings -whenever we encounter the start of a building, we push it into the PriorityQueue, whenever we finished scanning that building, we remove it from the PriorityQueue -Also, in the scan process, we’ll keep updating the maxHeight in the PriorityQueue if we find a new maxHeight which means the building will be overshadowed by the new higher one - -Three edge cases (see the graph illustration in the above video at 12’18”): -when two buildings have the same start point, the one with higher height shows up in the final result -when two buildings have the same end point, the one with higher height shows up in the final result -when the start point of one building is is also the end point of another building, the one with higher height shows up in the final result - - We use TreeMap over a normal PriorityQueue: -For the sake of efficiency (better time complexity), we’ll use TreeMap which supports O(logn) for remove() operation, - this is the reason we choose TreeMap over a normal PriorityQueue in Java (PriorityQueue supports add() and getMaxVal() in both O(logn) time, however, for remove(), it does NOT.) -But TreeMap in Java supports all the three operations in O(logn) time.*/ - -public class _218 { - - public static class Solution1 { - - class BuildingPoint implements Comparable { - int x; - boolean isStart; - int h; - - public BuildingPoint(int x, boolean isStart, int h) { - this.x = x; - this.h = h; - this.isStart = isStart; - } - - @Override - public int compareTo(BuildingPoint o) { - if (this.x != o.x) { - return this.x - o.x; - } else { - if (this.isStart && o.isStart) { - return o.h - this.h; - } else if (this.isStart && !o.isStart) { - return -this.h - o.h; - } else if (!this.isStart && !o.isStart) { - return this.h - o.h; - } else { - return this.h + o.h; - } - } - } - } - - public List getSkyline(int[][] buildings) { - BuildingPoint[] bps = new BuildingPoint[buildings.length * 2]; - int index = 0; - for (int[] building : buildings) { - BuildingPoint bp1 = new BuildingPoint(building[0], true, building[2]); - BuildingPoint bp2 = new BuildingPoint(building[1], false, building[2]); - bps[index++] = bp1; - bps[index++] = bp2; - } - - //this is one key step: - Arrays.sort(bps); - - List result = new ArrayList(); - TreeMap treeMap = new TreeMap(); - treeMap.put(0, 1); - int prevMaxH = 0; - for (BuildingPoint bp : bps) { - //if it's a starting point, we'll add it into the final result - if (bp.isStart) { - if (treeMap.containsKey(bp.h)) { - treeMap.put(bp.h, treeMap.get(bp.h) + 1); - } else { - treeMap.put(bp.h, 1); - } - } else if (!bp.isStart) { - //if it's an ending point, we'll decrement/remove this entry - if (treeMap.containsKey(bp.h) && treeMap.get(bp.h) > 1) { - treeMap.put(bp.h, treeMap.get(bp.h) - 1); - } else { - treeMap.remove(bp.h); - } - } - - int currMaxH = treeMap.lastKey(); - if (currMaxH != prevMaxH) { - result.add(new int[]{bp.x, currMaxH}); - prevMaxH = currMaxH; - } - - } - - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_219.java b/src/main/java/com/fishercoder/solutions/_219.java deleted file mode 100644 index 2d58de2a1c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_219.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 219. Contains Duplicate II - Given an array of integers and an integer k, - find out whether there are two distinct indices i and j in the array - such that nums[i] = nums[j] and the absolute difference between i and j is at most k. - - Example 1: - Input: nums = [1,2,3,1], k = 3 - Output: true - - Example 2: - Input: nums = [1,0,1,1], k = 1 - Output: true - - Example 3: - Input: nums = [1,2,3,1,2,3], k = 2 - Output: false - */ -public class _219 { - - public static class Solution1 { - public boolean containsNearbyDuplicate(int[] nums, int k) { - Map map = new HashMap(); - for (int i = 0; i < nums.length; i++) { - if (map.containsKey(nums[i])) { - if (i - map.get(nums[i]) <= k) { - return true; - } else { - map.put(nums[i], i); - } - } else { - map.put(nums[i], i); - } - } - return false; - } - } - - public static class Solution2 { - public boolean containsNearbyDuplicate(int[] nums, int k) { - Set set = new HashSet<>(); - for (int i = 0; i < nums.length; i++) { - if (!set.add(nums[i])) { - return true; - } - if (set.size() > k) { - set.remove(nums[i - k]); - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_222.java b/src/main/java/com/fishercoder/solutions/_222.java deleted file mode 100644 index 5705949b53..0000000000 --- a/src/main/java/com/fishercoder/solutions/_222.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 222. Count Complete Tree Nodes - * - * Given a complete binary tree, count the number of nodes. - * Definition of a complete binary tree from Wikipedia: - * In a complete binary tree every level, - * except possibly the last, is completely filled, - * and all nodes in the last level are as far left as possible. - * It can have between 1 and 2h nodes inclusive at the last level h. - */ -public class _222 { - - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/21317/accepted-easy-understand-java-solution/2 - */ - public int countNodes(TreeNode root) { - int leftH = getLeftHeight(root); - int rightH = getRightHeight(root); - if (leftH == rightH) { - return (1 << leftH) - 1; - } else { - return 1 + countNodes(root.left) + countNodes(root.right); - } - } - - private int getRightHeight(TreeNode root) { - int height = 0; - while (root != null) { - root = root.right; - height++; - } - return height; - } - - private int getLeftHeight(TreeNode root) { - int height = 0; - while (root != null) { - root = root.left; - height++; - } - return height; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_223.java b/src/main/java/com/fishercoder/solutions/_223.java deleted file mode 100644 index 7b83b69170..0000000000 --- a/src/main/java/com/fishercoder/solutions/_223.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 223. Rectangle Area - * - * Find the total area covered by two rectilinear rectangles in a 2D plane. - * Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. - * Rectangle Area - - Example: - - Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2 - Output: 45 - - Note: Assume that the total area is never beyond the maximum possible value of int.*/ -public class _223 { - - public static class Solution1 { - public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { - int areaA = (C - A) * (D - B); - int areaB = (G - E) * (H - F); - - int top = Math.min(D, H); - int bottom = Math.max(B, F); - int left = Math.max(A, E); - int right = Math.min(C, G); - - int overlap = 0; - if (top > bottom && right > left) { - overlap = (top - bottom) * (right - left); - } - return areaA + areaB - overlap; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_224.java b/src/main/java/com/fishercoder/solutions/_224.java deleted file mode 100644 index 3075bb414e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_224.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -/** - * 224. Basic Calculator - * - * Implement a basic calculator to evaluate a simple expression string. - * The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . - * You may assume that the given expression is always valid. - - Some examples: - "1 + 1" = 2 - " 2-1 + 2 " = 3 - "(1+(4+5+2)-3)+(6+8)" = 23 - Note: Do not use the eval built-in library function. - */ -public class _224 { - - public static class Solution1 { - - public int calculate(String s) { - if (s == null || s.isEmpty()) { - return 0; - } - - s = s.replaceAll("\\s", ""); - char[] chars = s.toCharArray(); - List filteredStr = new ArrayList(); - for (int i = 0; i < chars.length; ) { - StringBuilder sb = new StringBuilder(); - while (i < chars.length && Character.isDigit(chars[i])) { - sb.append(chars[i]); - i++; - } - if (i == chars.length) { - if (sb.toString().length() != 0) { - filteredStr.add(sb.toString()); - } - } else { - if (sb.toString().length() != 0) { - filteredStr.add(sb.toString()); - } - if (chars[i] == '+' || chars[i] == '-' || chars[i] == '(' || chars[i] == ')') { - filteredStr.add(String.valueOf(chars[i])); - } - i++; - } - } - - for (String str : filteredStr) { - System.out.print(str); - } - - Stack stack1 = new Stack(); - Stack stack2 = new Stack(); - for (int i = 0; i < filteredStr.size(); ) { - while (i < filteredStr.size() && !filteredStr.get(i).equals(")")) { - stack1.push(filteredStr.get(i)); - i++; - } - if (i != filteredStr.size()) { - while (!stack1.isEmpty() && !stack1.peek().equals("(")) { - stack2.push(stack1.pop()); - } - stack1.pop(); - int exp = 0; - while (!stack2.isEmpty()) { - if (stack2.size() == 1) { - stack1.push(stack2.pop()); - break; - } - int operand1 = Integer.parseInt(stack2.pop()); - String operator = stack2.pop(); - int operand2 = Integer.parseInt(stack2.pop()); - if (operator.equals("+")) { - exp = operand1 + operand2; - } else if (operator.equals("-")) { - exp = operand1 - operand2; - } - stack2.push(String.valueOf(exp)); - } - i++; - } - } - - if (stack1.size() == 1) { - return Integer.parseInt(stack1.pop()); - } - - while (!stack1.isEmpty()) { - stack2.push(stack1.pop()); - } - while (!stack2.isEmpty()) { - if (stack2.size() == 1) { - stack1.push(stack2.pop()); - break; - } - int exp = 0; - int operand1 = Integer.parseInt(stack2.pop()); - String operator = stack2.pop(); - int operand2 = Integer.parseInt(stack2.pop()); - if (operator.equals("+")) { - exp = operand1 + operand2; - } else if (operator.equals("-")) { - exp = operand1 - operand2; - } - stack2.push(String.valueOf(exp)); - } - return Integer.parseInt(stack1.pop()); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_225.java b/src/main/java/com/fishercoder/solutions/_225.java deleted file mode 100644 index cf7e1a5acd..0000000000 --- a/src/main/java/com/fishercoder/solutions/_225.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; -/** - * 225. Implement Stack using Queues - * - * Implement the following operations of a stack using queues. - - push(x) -- Push element x onto stack. - pop() -- Removes the element on top of the stack. - top() -- Get the top element. - empty() -- Return whether the stack is empty. - - Notes: - You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue. - You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack). - Update (2015-06-11): - The class name of the Java function had been updated to MyStack instead of Stack.*/ - -public class _225 { - - public static class Solution1 { - class MyStack { - - Queue q = new LinkedList(); - - // Push element x onto stack. - public void push(int x) { - q.offer(x); - for (int i = 1; i < q.size(); i++) { - q.offer(q.remove()); - } - } - - // Removes the element on top of the stack. - public void pop() { - q.poll(); - } - - // Get the top element. - public int top() { - return q.peek(); - } - - // Return whether the stack is empty. - public boolean empty() { - return q.isEmpty(); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_226.java b/src/main/java/com/fishercoder/solutions/_226.java deleted file mode 100644 index ce0bc805ab..0000000000 --- a/src/main/java/com/fishercoder/solutions/_226.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 226. Invert Binary Tree - - Invert a binary tree. - - 4 - / \ - 2 7 - / \ / \ - 1 3 6 9 - - to - - 4 - / \ - 7 2 - / \ / \ - 9 6 3 1 - -Trivia: -This problem was inspired by this original tweet by Max Howell: -Google: 90% of our engineers use the software you wrote (Homebrew), - but you can�t invert a binary tree on a whiteboard so fuck off. - */ -public class _226 { - - public static class Solution1 { - public TreeNode invertTree(TreeNode root) { - if (root == null) { - return root; - } - Queue q = new LinkedList(); - q.offer(root); - while (!q.isEmpty()) { - TreeNode curr = q.poll(); - TreeNode temp = curr.left; - curr.left = curr.right; - curr.right = temp; - if (curr.left != null) { - q.offer(curr.left); - } - if (curr.right != null) { - q.offer(curr.right); - } - } - return root; - } - } - - public static class Solution2 { - public TreeNode invertTree(TreeNode root) { - if (root == null) { - return root; - } - TreeNode temp = root.left; - root.left = root.right; - root.right = temp; - invertTree(root.left); - invertTree(root.right); - return root; - } - } - - public static class Solution3 { - //more concise version - public TreeNode invertTree(TreeNode root) { - if (root == null) { - return root; - } - TreeNode temp = root.left; - root.left = invertTree(root.right); - root.right = invertTree(temp); - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_227.java b/src/main/java/com/fishercoder/solutions/_227.java deleted file mode 100644 index 036f16badb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_227.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Deque; - -/** - * 227. Basic Calculator II - * - * Implement a basic calculator to evaluate a simple expression string. - - The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. - - You may assume that the given expression is always valid. - - Some examples: - "3+2*2" = 7 - " 3/2 " = 1 - " 3+5 / 2 " = 5 - Note: Do not use the eval built-in library function. - */ -public class _227 { - - public static class Solution1 { - public int calculate(String s) { - if (s == null || s.length() == 0) { - return 0; - } - int len = s.length(); - Deque stack = new ArrayDeque<>(); - int num = 0; - char sign = '+'; - for (int i = 0; i < len; i++) { - if (Character.isDigit(s.charAt(i))) { - num = num * 10 + s.charAt(i) - '0'; - } - if ((!Character.isDigit(s.charAt(i))) && ' ' != s.charAt(i) || i == len - 1) { - if (sign == '+') { - stack.addLast(num); - } else if (sign == '-') { - stack.addLast(-num); - } else if (sign == '/') { - stack.addLast(stack.pollLast() / num); - } else if (sign == '*') { - stack.addLast(stack.pollLast() * num); - } - sign = s.charAt(i); - num = 0; - } - } - int result = 0; - while (!stack.isEmpty()) { - result += stack.poll(); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_229.java b/src/main/java/com/fishercoder/solutions/_229.java deleted file mode 100644 index 50e43891f2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_229.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 229. Majority Element II - * - * Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. - * The algorithm should run in linear time and in O(1) space. - * - * Example 1: - * Input: [3,2,3] - * Output: [3] - * - * Example 2: - * Input: [1,1,1,3,3,2,2,2] - * Output: [1,2] - - Hint: - How many majority elements could it possibly have? - Do you have a better hint? Suggest it! - */ -public class _229 { - - public static class Solution1 { - /**Moore Voting algorithm: - * This is an extension of Majority Element I, instead of one one majority, there could be a max of two majority elements, - * so we'll just use two counters to do the job.*/ - public List majorityElement(int[] nums) { - List result = new ArrayList<>(); - if (nums == null || nums.length == 0) { - return result; - } - int count1 = 0; - int count2 = 0; - int candidate1 = 0; - int candidate2 = 1; - for (int num : nums) { - if (num == candidate1) { - count1++; - } else if (num == candidate2) { - count2++; - } else if (count1 == 0) { - candidate1 = num; - count1 = 1; - } else if (count2 == 0) { - candidate2 = num; - count2 = 1; - } else { - count1--; - count2--; - } - } - count1 = 0; - count2 = 0; - for (int num : nums) { - if (num == candidate1) { - count1++; - } else if (num == candidate2) { - count2++; - } - } - if (count1 > nums.length / 3) { - result.add(candidate1); - } - if (count2 > nums.length / 3) { - result.add(candidate2); - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_230.java b/src/main/java/com/fishercoder/solutions/_230.java deleted file mode 100644 index 68fc42c50d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_230.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 230. Kth Smallest Element in a BST - * - * Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. - - Note: - You may assume k is always valid, 1 ? k ? BST's total elements. - - Follow up: - What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine? - - */ -public class _230 { - - public static class Solution1 { - public int kthSmallest(TreeNode root, int k) { - List inorderList = new ArrayList<>(); - inorder(root, inorderList); - return inorderList.get(k - 1); - } - - private void inorder(TreeNode root, List inorderList) { - if (root == null) { - return; - } - if (root.left != null) { - inorder(root.left, inorderList); - } - inorderList.add(root.val); - if (root.right != null) { - inorder(root.right, inorderList); - } - return; - } - } - - public static class Solution2 { - /** - * Inorder traversal gives the natural ordering of a BST, no need to sort. - */ - int count = 0; - int result = Integer.MIN_VALUE; - - public int kthSmallest(TreeNode root, int k) { - inorder(root, k); - return result; - } - - private void inorder(TreeNode root, int k) { - if (root == null) { - return; - } - inorder(root.left, k); - count++; - if (count == k) { - result = root.val; - return; - } - inorder(root.right, k); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_231.java b/src/main/java/com/fishercoder/solutions/_231.java deleted file mode 100644 index 40c6d9b6e6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_231.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 231. Power of Two - * - * Given an integer, write a function to determine if it is a power of two. - */ - -public class _231 { - public static class Solution1 { - public boolean isPowerOfTwo(int n) { - //after writing out the binary representation of some numbers: 1,2,4,8,16,32, you can easily figure out that - //every number that is power of two has only one bit that is 1 - //then we can apply that cool trick that we learned from {@link easy._191}: n&(n-1) which will clear the least significant bit in n to zero - return n > 0 && (n & (n - 1)) == 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_232.java b/src/main/java/com/fishercoder/solutions/_232.java deleted file mode 100644 index 34e25f07f0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_232.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 232. Implement Queue using Stacks - * - * Implement the following operations of a queue using stacks. - - push(x) -- Push element x to the back of queue. - pop() -- Removes the element from in front of queue. - peek() -- Get the front element. - empty() -- Return whether the queue is empty. - - Notes: - You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack. - You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue). - */ -public class _232 { - - public static class Solution1 { - class MyQueue { - - Stack input = new Stack(); - Stack output = new Stack(); - - // Push element x to the back of queue. - public void push(int x) { - input.push(x); - } - - // Removes the element from in front of queue. - public int pop() { - peek(); - return output.pop(); - } - - // Get the front element. - public int peek() { - if (output.isEmpty()) { - while (!input.isEmpty()) { - output.push(input.pop()); - } - } - return output.peek(); - } - - // Return whether the queue is empty. - public boolean empty() { - return input.isEmpty() && output.isEmpty(); - } - } - } -} - diff --git a/src/main/java/com/fishercoder/solutions/_233.java b/src/main/java/com/fishercoder/solutions/_233.java deleted file mode 100644 index c104b7212a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_233.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 233. Number of Digit One - * - * Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. - - For example: - Given n = 13, - Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. - - Hint: Beware of overflow. - */ -public class _233 { - public static class Solution1 { - public int countDigitOne(int n) { - int count = 0; - for (long k = 1; k <= n; k *= 10) { - long r = n / k; - long m = n % k; - // sum up the count of ones on every place k - count += (r + 8) / 10 * k + (r % 10 == 1 ? m + 1 : 0); - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_235.java b/src/main/java/com/fishercoder/solutions/_235.java deleted file mode 100644 index 262f3b4c9c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_235.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 235. Lowest Common Ancestor of a Binary Search Tree - * - * Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. - * According to the definition of LCA on Wikipedia: - * “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants - * (where we allow a node to be a descendant of itself).” - - _______6______ - / \ - ___2__ ___8__ - / \ / \ - 0 4 7 9 - / \ - 3 5 - - For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. - Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition. - - */ -public class _235 { - - public static class Solution1 { - public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { - if (root == null || p == root || q == root) { - return root; - } - if ((root.val - p.val) * (root.val - q.val) > 0) { - if (root.val - p.val > 0) { - return lowestCommonAncestor(root.left, p, q); - } - return lowestCommonAncestor(root.right, p, q); - } - return root; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_236.java b/src/main/java/com/fishercoder/solutions/_236.java deleted file mode 100644 index d50911a16d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_236.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 236. Lowest Common Ancestor of a Binary Tree - * - * Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. - * According to the definition of LCA on Wikipedia: - * “The lowest common ancestor is defined between two nodes v and w as the lowest node in T - * that has both v and w as descendants (where we allow a node to be a descendant of itself).” - - _______3______ - / \ - ___5__ ___1__ - / \ / \ - 6 _2 0 8 - / \ - 7 4 - - For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. - Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.*/ - -public class _236 { - public static class Solution1 { - - public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { - if (root == null || root == p || root == q) { - return root; - } - TreeNode left = lowestCommonAncestor(root.left, p, q); - TreeNode right = lowestCommonAncestor(root.right, p, q); - if (left != null && right != null) { - return root; - } - return left != null ? left : right; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_237.java b/src/main/java/com/fishercoder/solutions/_237.java deleted file mode 100644 index 41c9c3a1f3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_237.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 237. Delete Node in a Linked List - * - * Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. - * - * Given linked list -- head = [4,5,1,9], which looks like following: - * - * Example 1: - * Input: head = [4,5,1,9], node = 5 - * Output: [4,1,9] - * Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function. - * - * Example 2: - * Input: head = [4,5,1,9], node = 1 - * Output: [4,5,9] - * Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function. - * - * Note: - * The linked list will have at least two elements. - * All of the nodes' values will be unique. - * The given node will not be the tail and it will always be a valid node of the linked list. - * Do not return anything from your function. -*/ -public class _237 { - - public static class Solution1 { - public void deleteNode(ListNode node) { - node.val = node.next.val; - node.next = node.next.next; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_238.java b/src/main/java/com/fishercoder/solutions/_238.java deleted file mode 100644 index a5778b25f6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_238.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 238. Product of Array Except Self - * - * Given an array of n integers where n > 1, nums, - * return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. - * Solve it without division and in O(n). - * For example, given [1,2,3,4], return [24,12,8,6]. - - Follow up: - Could you solve it with constant space complexity? - (Note: The output array does not count as extra space for the purpose of space complexity analysis.) - */ - -public class _238 { - - public static class Solution1 { - /** - * Very straightforward idea: iterate through the array twice: - * first time: get res[i] = res[i-1]*nums[i-1] - * second time: have a variable called right, which means all the numbers product to its right, then do - * res[i] *= right; - * right *= nums[i]; - * that's it. - * - * This could be very well illustrated with this example: [1,2,3,4] - */ - public int[] productExceptSelf(int[] nums) { - int n = nums.length; - int[] result = new int[n]; - result[0] = 1; - for (int i = 1; i < n; i++) { - result[i] = result[i - 1] * nums[i - 1]; - } - int right = 1; - for (int i = n - 1; i >= 0; i--) { - result[i] *= right; - right *= nums[i]; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_239.java b/src/main/java/com/fishercoder/solutions/_239.java deleted file mode 100644 index cab926aae2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_239.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.PriorityQueue; - -/** - * 239. Sliding Window Maximum - * - * Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. - * You can only see the k numbers in the window. Each time the sliding window moves right by one position. - - For example: - - Given nums = [1,3,-1,-3,5,3,6,7], and k = 3. - - Window position Max - --------------- ----- - [1 3 -1] -3 5 3 6 7 3 - 1 [3 -1 -3] 5 3 6 7 3 - 1 3 [-1 -3 5] 3 6 7 5 - 1 3 -1 [-3 5 3] 6 7 5 - 1 3 -1 -3 [5 3 6] 7 6 - 1 3 -1 -3 5 [3 6 7] 7 - Therefore, return the max sliding window as [3,3,5,5,6,7]. - - Note: - You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array. - - Follow up: - Could you solve it in linear time? - - Hint: - How about using a data structure such as deque (double-ended queue)? - The queue size need not be the same as the window’s size. - Remove redundant elements and the queue should store only elements that need to be considered. - */ -public class _239 { - - public static class Solution1 { - public int[] maxSlidingWindow(int[] nums, int k) { - if (nums == null || nums.length == 0 || k == 0) { - return new int[0]; - } - PriorityQueue heap = new PriorityQueue<>((a, b) -> b - a); - int[] res = new int[nums.length - k + 1]; - for (int i = 0; i < nums.length; i++) { - if (i < k) { - heap.offer(nums[i]); - if (i == k - 1) { - res[0] = heap.peek(); - } - } else { - heap.remove(nums[i - k]); - heap.offer(nums[i]); - res[i - k + 1] = heap.peek(); - } - } - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_24.java b/src/main/java/com/fishercoder/solutions/_24.java deleted file mode 100644 index 4e0093e269..0000000000 --- a/src/main/java/com/fishercoder/solutions/_24.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -public class _24 { - public static class Solution1 { - public ListNode swapPairs(ListNode head) { - if (head == null || head.next == null) { - return head; - } - ListNode second = head.next; - ListNode third = second.next; - second.next = head; - head.next = swapPairs(third); - return second; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_240.java b/src/main/java/com/fishercoder/solutions/_240.java deleted file mode 100644 index bcfe462db8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_240.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 240. Search a 2D Matrix II - * - * Write an efficient algorithm that searches for a value in an m x n matrix. - * This matrix has the following properties: - - Integers in each row are sorted in ascending from left to right. - Integers in each column are sorted in ascending from top to bottom. - For example, - - Consider the following matrix: - - [ - [1, 4, 7, 11, 15], - [2, 5, 8, 12, 19], - [3, 6, 9, 16, 22], - [10, 13, 14, 17, 24], - [18, 21, 23, 26, 30] - ] - Given target = 5, return true. - - Given target = 20, return false. - */ -public class _240 { - - public static class Solution1 { - public boolean searchMatrix(int[][] matrix, int target) { - if (matrix == null || matrix.length == 0) { - return false; - } - int m = matrix.length; - int n = matrix[0].length; - int x = 0; - int y = n - 1; - while (x < m && y >= 0) { - if (target == matrix[x][y]) { - return true; - } else if (target > matrix[x][y]) { - x++; - } else { - y--; - } - } - return false; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_241.java b/src/main/java/com/fishercoder/solutions/_241.java deleted file mode 100644 index ef91d87c01..0000000000 --- a/src/main/java/com/fishercoder/solutions/_241.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.List; - -/** - * 241. Different Ways to Add Parentheses - * - * Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *. - - - Example 1 - Input: "2-1-1". - - ((2-1)-1) = 0 - (2-(1-1)) = 2 - Output: [0, 2] - - - Example 2 - Input: "2*3-4*5" - - (2*(3-(4*5))) = -34 - ((2*3)-(4*5)) = -14 - ((2*(3-4))*5) = -10 - (2*((3-4)*5)) = -10 - (((2*3)-4)*5) = 10 - Output: [-34, -14, -10, -10, 10] - - */ -public class _241 { - public static class Solution1 { - /**Time: O(n * 4^n / n^(3/2)) ~= n * Catalan numbers = n * (C(2n, n) - C(2n, n - 1)), - due to the size of the results is Catalan numbers, - and every way of evaluation is the length of the string, - so the time complexity is at most n * Catalan numbers. - Space: O(n * 4^n / n^(3/2)), the cache size of lookup is at most n * Catalan numbers.*/ - - /** - * Credit: https://discuss.leetcode.com/topic/19901/a-recursive-java-solution-284-ms - */ - public List diffWaysToCompute(String input) { - List answer = new LinkedList<>(); - int len = input.length(); - for (int i = 0; i < len; i++) { - if (input.charAt(i) == '+' - || input.charAt(i) == '-' - || input.charAt(i) == '*') { - String part1 = input.substring(0, i); - String part2 = input.substring(i + 1); - List answer1 = diffWaysToCompute(part1); - List answer2 = diffWaysToCompute(part2); - for (int a1 : answer1) { - for (int a2 : answer2) { - int result = 0; - switch (input.charAt(i)) { - case '+': - result = a1 + a2; - break; - case '-': - result = a1 - a2; - break; - case '*': - result = a1 * a2; - break; - default: - break; - } - answer.add(result); - } - } - } - } - if (answer.size() == 0) { - answer.add(Integer.valueOf(input)); - } - return answer; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_242.java b/src/main/java/com/fishercoder/solutions/_242.java deleted file mode 100644 index 6fc23ad0e4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_242.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 242. Valid Anagram - * Given two strings s and t, write a function to determine if t is an anagram of s. - - For example, - s = "anagram", t = "nagaram", return true. - s = "rat", t = "car", return false. - - Note: - You may assume the string contains only lowercase alphabets. - - Follow up: - What if the inputs contain unicode characters? How would you adapt your solution to such case? - */ - -public class _242 { - - public static class Solution1 { - public boolean isAnagram(String s, String t) { - char[] schar = s.toCharArray(); - char[] tchar = t.toCharArray(); - Arrays.sort(schar); - Arrays.sort(tchar); - return new String(schar).equals(new String(tchar)); - } - } - - public static class Solution2 { - public boolean isAnagram(String s, String t) { - if (s == null || t == null || s.length() != t.length()) { - return false; - } - int[] counts = new int[26]; - for (int i = 0; i < s.length(); i++) { - counts[s.charAt(i) - 'a']++; - counts[t.charAt(i) - 'a']--; - } - for (int i : counts) { - if (i != 0) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_243.java b/src/main/java/com/fishercoder/solutions/_243.java deleted file mode 100644 index a8d47b3aa9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_243.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 243. Shortest Word Distance - * - * Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. - - For example, - Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. - - Given word1 = “coding”, word2 = “practice”, return 3. - Given word1 = "makes", word2 = "coding", return 1. - - Note: - You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.*/ -public class _243 { - public static class Solution1 { - public int shortestDistance(String[] words, String word1, String word2) { - int p = -1; - int q = -1; - int min = Integer.MAX_VALUE; - for (int i = 0; i < words.length; i++) { - if (words[i].equals(word1)) { - p = i; - } - if (words[i].equals(word2)) { - q = i; - } - if (p != -1 && q != -1) { - min = Math.min(min, Math.abs(p - q)); - } - } - return min; - - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_244.java b/src/main/java/com/fishercoder/solutions/_244.java deleted file mode 100644 index 4eee383bf8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_244.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 244. Shortest Word Distance II - * - * This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it? - * Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list. - - For example, - Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. - - Given word1 = “coding”, word2 = “practice”, return 3. - Given word1 = "makes", word2 = "coding", return 1. - - Note: - You may assume that word1 does not equal to word2, and word1 and word2 are both in the list. - */ -public class _244 { - public static class Solution1 { - class WordDistance { - - private Map> map; - - public WordDistance(String[] words) { - map = new HashMap<>(); - for (int i = 0; i < words.length; i++) { - String w = words[i]; - if (map.containsKey(w)) { - map.get(w).add(i); - } else { - List list = new ArrayList<>(); - list.add(i); - map.put(w, list); - } - } - } - - public int shortest(String word1, String word2) { - List list1 = map.get(word1); - List list2 = map.get(word2); - int result = Integer.MAX_VALUE; - for (int i = 0, j = 0; i < list1.size() && j < list2.size(); ) { - int index1 = list1.get(i); - int index2 = list2.get(j); - if (index1 < index2) { - result = Math.min(result, index2 - index1); - i++; - } else { - result = Math.min(result, index1 - index2); - j++; - } - } - return result; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_245.java b/src/main/java/com/fishercoder/solutions/_245.java deleted file mode 100644 index 60813b90d7..0000000000 --- a/src/main/java/com/fishercoder/solutions/_245.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 245. Shortest Word Distance III - * - * This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2. - * Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. - * word1 and word2 may be the same and they represent two individual words in the list. - - For example, - Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. - - Given word1 = “makes”, word2 = “coding”, return 1. - Given word1 = "makes", word2 = "makes", return 3. - - Note: - You may assume word1 and word2 are both in the list. - */ -public class _245 { - - public static class Solution1 { - public int shortestWordDistance(String[] words, String word1, String word2) { - int p1 = -1; - int p2 = -1; - int min = Integer.MAX_VALUE; - for (int i = 0; i < words.length; i++) { - if (words[i].equals(word1)) { - if (word1.equals(word2)) { - if (p1 != -1 && i - p1 < min) { - min = i - p1; - } - p1 = i; - } else { - p1 = i; - if (p2 != -1 && p1 - p2 < min) { - min = p1 - p2; - } - } - } else if (words[i].equals(word2)) { - p2 = i; - if (p1 != -1 && p2 - p1 < min) { - min = p2 - p1; - } - } - } - return min; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_249.java b/src/main/java/com/fishercoder/solutions/_249.java deleted file mode 100644 index 17d01b99a8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_249.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class _249 { - - public static class Solution1 { - public List> groupStrings(String[] strings) { - - List> result = new ArrayList>(); - Map> map = new HashMap>(); - - for (String word : strings) { - String key = ""; - int offset = word.charAt(0) - 'a'; - for (int i = 1; i < word.length(); i++) { - key += (word.charAt(i) - offset + 26) % 26; - } - - if (!map.containsKey(key)) { - map.put(key, new ArrayList<>()); - } - map.get(key).add(word); - } - - for (List list : map.values()) { - Collections.sort(list); - result.add(list); - } - - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_25.java b/src/main/java/com/fishercoder/solutions/_25.java deleted file mode 100644 index a118ca3704..0000000000 --- a/src/main/java/com/fishercoder/solutions/_25.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -public class _25 { - - /**We use recursion to go all the way until the end: when the number of nodes are smaller than k; - * then we start to reverse each group of k nodes from the end towards the start.*/ - public ListNode reverseKGroup(ListNode head, int k) { - ListNode curr = head; - int count = 0; - while (curr != null && count != k) { - //find the k+1 node - curr = curr.next; - count++; - } - - if (count == k) { - /**after this below recursive call finishes, it'll return head; - * then this returned "head" will become "curr", while the head - * in its previous callstack is the real head after this call. - * Setting up a break point will make all of this crystal clear.*/ - curr = reverseKGroup(curr, k); - - while (count-- > 0) { - ListNode temp = head.next; - head.next = curr; - curr = head; - head = temp; - } - head = curr; - } - return head;//we run out of nodes before we hit count == k, so we'll just directly return head in this case as well - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_252.java b/src/main/java/com/fishercoder/solutions/_252.java deleted file mode 100644 index 82d7b2f2bc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_252.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Interval; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class _252 { - public static class Solution1 { - public boolean canAttendMeetings(Interval[] intervals) { - - List list = new ArrayList(); - for (Interval interval : intervals) { - list.add(interval); - } - - Collections.sort(list, (o1, o2) -> { - if (o1.start > o2.start) { - return 1; - } else { - return -1; - } - }); - - for (int i = 0; i < list.size() - 1; i++) { - if (list.get(i).end > list.get(i + 1).start) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_253.java b/src/main/java/com/fishercoder/solutions/_253.java deleted file mode 100644 index bcba4334cc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_253.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Interval; - -import java.util.Arrays; -import java.util.PriorityQueue; - -public class _253 { - public static class Solution1 { - - public int minMeetingRooms(Interval[] intervals) { - if (intervals == null || intervals.length == 0) { - return 0; - } - - // Sort the intervals by start time - Arrays.sort(intervals, (a, b) -> a.start - b.start); - - // Use a min heap to track the minimum end time of merged intervals - PriorityQueue heap = new PriorityQueue<>(intervals.length, (a, b) -> a.end - b.end); - - // start with the first meeting, put it to a meeting room - heap.offer(intervals[0]); - - for (int i = 1; i < intervals.length; i++) { - // get the meeting room that finishes earliest - Interval interval = heap.poll(); - - if (intervals[i].start >= interval.end) { - // if the current meeting starts right after - // there's no need for a new room, merge the interval - interval.end = intervals[i].end; - } else { - // otherwise, this meeting needs a new room - heap.offer(intervals[i]); - } - - // don't forget to put the meeting room back - heap.offer(interval); - } - - return heap.size(); - } - } -} - diff --git a/src/main/java/com/fishercoder/solutions/_258.java b/src/main/java/com/fishercoder/solutions/_258.java deleted file mode 100644 index b72536af8c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_258.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.fishercoder.solutions; - -public class _258 { - - public static class Solution1 { - //only three cases as the code shows - public int addDigits(int num) { - if (num == 0) { - return 0; - } - if (num % 9 == 0) { - return 9; - } - return num % 9; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_26.java b/src/main/java/com/fishercoder/solutions/_26.java deleted file mode 100644 index ccc4ce9be8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_26.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.fishercoder.solutions; - -public class _26 { - - public static class Solution1 { - /**Key: It doesn't matter what you leave beyond the returned length.*/ - public int removeDuplicates(int[] nums) { - int i = 0; - for (int j = 1; j < nums.length; j++) { - if (nums[i] != nums[j]) { - i++; - nums[i] = nums[j]; - } - } - return i + 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_260.java b/src/main/java/com/fishercoder/solutions/_260.java deleted file mode 100644 index 1e938d69d9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_260.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 260. Single Number III - * - * Given an array of numbers nums, - * in which exactly two elements appear only once and all the other elements appear exactly twice. - * Find the two elements that appear only once. - -For example: - -Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. - -Note: -The order of the result is not important. So in the above example, [5, 3] is also correct. -Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity? -*/ - -public class _260 { - - public static class Solution1 { - public int[] singleNumber(int[] nums) { - Map map = new HashMap(); - for (int i : nums) { - map.put(i, map.getOrDefault(i, 0) + 1); - } - - int[] res = new int[2]; - int index = 0; - for (int key : map.keySet()) { - if (map.get(key) == 1) { - res[index++] = key; - } - if (index == 2) { - break; - } - } - return res; - } - } - - public static class Solution2 { - /**Credit: https://discuss.leetcode.com/topic/21605/accepted-c-java-o-n-time-o-1-space-easy-solution-with-detail-explanations/2 - * - * some more explanation about this algorithm: - * two's complement: one number's two's complement number is computed as below: - * reverse all bits of this number and then add one: - * e.g. decimal number 2, in binary format: 0010 (4 bits) - * reversing every single bit becomes 1101, - * then add 1 to it, it becomes 1110 - * - * so - * num &= -num, in this case, 2 &= -2 becomes 2 - * */ - public int[] singleNumber(int[] nums) { - int diff = 0; - for (int num : nums) { - diff ^= num; - } - - //get least significant set bit - diff &= -diff; - - int[] result = new int[2]; - for (int num : nums) { - if ((num & diff) == 0) { - result[0] ^= num; - } else { - result[1] ^= num; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_261.java b/src/main/java/com/fishercoder/solutions/_261.java deleted file mode 100644 index b35b7e687f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_261.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -/** - *261. Graph Valid Tree - * - * Given n nodes labeled from 0 to n - 1 and a list of undirected edges - * (each edge is a pair of nodes), - * write a function to check whether these edges make up a valid tree. - - For example: - - Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true. - - Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false. - - Hint: - - Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], what should your return? Is this case a valid tree? - According to the definition of tree on Wikipedia: - “a tree is an undirected graph in which any two vertices are connected by exactly one path. - In other words, any connected graph without simple cycles is a tree.” - - Note: you can assume that no duplicate edges will appear in edges. - Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges. - */ -public class _261 { - - public static class Solution1 { - public boolean validTree(int n, int[][] edges) { - int[] nums = new int[n]; - for (int i = 0; i < n; i++) { - nums[i] = i; - } - - for (int i = 0; i < edges.length; i++) { - int x = find(nums, edges[i][0]); - int y = find(nums, edges[i][1]); - - if (x == y) { - return false; - } - - //union - nums[x] = y; - } - - return edges.length == n - 1; - } - - int find(int[] nums, int i) { - if (nums[i] == i) { - return i; - } - return find(nums, nums[i]); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_264.java b/src/main/java/com/fishercoder/solutions/_264.java deleted file mode 100644 index 2c64319e0c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_264.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 264. Ugly Number II - * - * Write a program to find the n-th ugly number. - - Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. - - Note that 1 is typically treated as an ugly number, and n does not exceed 1690. - */ -public class _264 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/21791/o-n-java-solution - */ - public int nthUglyNumber(int n) { - int[] ugly = new int[n]; - ugly[0] = 1; - int index2 = 0; - int index3 = 0; - int index5 = 0; - int factor2 = 2; - int factor3 = 3; - int factor5 = 5; - for (int i = 1; i < n; i++) { - int min = Math.min(Math.min(factor2, factor3), factor5); - ugly[i] = min; - if (factor2 == min) { - factor2 = 2 * ugly[++index2]; - } - if (factor3 == min) { - factor3 = 3 * ugly[++index3]; - } - if (factor5 == min) { - factor5 = 5 * ugly[++index5]; - } - } - return ugly[n - 1]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_265.java b/src/main/java/com/fishercoder/solutions/_265.java deleted file mode 100644 index 7326f85f4c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_265.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 265. Paint House II - * - * There are a row of n houses, each house can be painted with one of the k colors. - * The cost of painting each house with a certain color is different. - * You have to paint all the houses such that no two adjacent houses have the same color. - * The cost of painting each house with a certain color is represented by a n x k cost matrix. - * - * For example, costs[0][0] is the cost of painting house 0 with color 0; - * costs[1][2] is the cost of painting house 1 with color 2, - * and so on... - * - * Find the minimum cost to paint all houses. - - Note: - All costs are positive integers. - - Follow up: - Could you solve it in O(nk) runtime? - */ -public class _265 { - - public static class Solution1 { - public int minCostII(int[][] costs) { - if (costs == null || costs.length == 0) { - return 0; - } - - int n = costs.length; - int k = costs[0].length; - // min1 is the index of the 1st-smallest cost till previous house - // min2 is the index of the 2nd-smallest cost till previous house - int min1 = -1; - int min2 = -1; - - for (int i = 0; i < n; i++) { - int last1 = min1; - int last2 = min2; - min1 = -1; - min2 = -1; - - for (int j = 0; j < k; j++) { - if (j != last1) { - // current color j is different to last min1 - costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1]; - } else { - costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2]; - } - - // find the indices of 1st and 2nd smallest cost of painting current house i - if (min1 < 0 || costs[i][j] < costs[i][min1]) { - min2 = min1; - min1 = j; - } else if (min2 < 0 || costs[i][j] < costs[i][min2]) { - min2 = j; - } - } - } - return costs[n - 1][min1]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_268.java b/src/main/java/com/fishercoder/solutions/_268.java deleted file mode 100644 index c5100a98ee..0000000000 --- a/src/main/java/com/fishercoder/solutions/_268.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.fishercoder.solutions; - -public class _268 { - - public static class Solution1 { - /** - * we could take advantage of the array indices - * then a number xor with itself is zero, so after we xor the entire array with all of its indices, the missing number will show up. - */ - public int missingNumber(int[] nums) { - int xor = 0; - int i = 0; - for (; i < nums.length; i++) { - xor = xor ^ i ^ nums[i]; - } - return xor ^ i; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_269.java b/src/main/java/com/fishercoder/solutions/_269.java deleted file mode 100644 index 6a92bf3725..0000000000 --- a/src/main/java/com/fishercoder/solutions/_269.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Map; -import java.util.Queue; -import java.util.Set; - -/** - * 269. Alien Dictionary - * - * There is a new alien language which uses the latin alphabet. - * However, the order among letters are unknown to you. - * You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. - * Derive the order of letters in this language. - - For example, - Given the following words in dictionary, - - [ - "wrt", - "wrf", - "er", - "ett", - "rftt" - ] - The correct order is: "wertf". - - Note: - You may assume all letters are in lowercase. - If the order is invalid, return an empty string. - There may be multiple valid order of letters, return any one of them is fine. - */ -public class _269 { - public static class Solution1 { - - /** - * reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs - */ - public String alienOrder(String[] words) { - Map> map = new HashMap(); - Map degree = new HashMap<>(); - String result = ""; - if (words == null || words.length == 0) { - return result; - } - for (String s : words) { - for (char c : s.toCharArray()) { - degree.put(c, 0);//keeps overwriting it, the purpose is to create one entry - //for each letter in the degree map - } - } - for (int i = 0; i < words.length - 1; i++) { - String cur = words[i]; - String next = words[i + 1]; - int length = Math.min(cur.length(), next.length()); - for (int j = 0; j < length; j++) { - char c1 = cur.charAt(j); - char c2 = next.charAt(j); - if (c1 != c2) { - Set set = new HashSet<>(); - if (map.containsKey(c1)) { - set = map.get(c1); - } - if (!set.contains(c2)) { - set.add(c2); - map.put(c1, set); - degree.put(c2, degree.get(c2) + 1); - } - break; - } - } - } - Queue queue = new LinkedList<>(); - for (char c : degree.keySet()) { - if (degree.get(c) == 0) { - queue.add(c); - } - } - while (!queue.isEmpty()) { - char c = queue.remove(); - result += c; - if (map.containsKey(c)) { - for (char c2 : map.get(c)) { - degree.put(c2, degree.get(c2) - 1); - if (degree.get(c2) == 0) { - queue.add(c2); - } - } - } - } - if (result.length() != degree.size()) { - return ""; - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_27.java b/src/main/java/com/fishercoder/solutions/_27.java deleted file mode 100644 index cc50d4ccca..0000000000 --- a/src/main/java/com/fishercoder/solutions/_27.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.fishercoder.solutions; - -public class _27 { - - public static class Solution1 { - public int removeElement(int[] nums, int val) { - int i = 0; - for (int j = 0; j < nums.length; j++) { - if (nums[j] != val) { - nums[i++] = nums[j]; - } - } - return i; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_270.java b/src/main/java/com/fishercoder/solutions/_270.java deleted file mode 100644 index 3867640759..0000000000 --- a/src/main/java/com/fishercoder/solutions/_270.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -public class _270 { - - public static class Solution1 { - //A general tree solution, this finished in 1 ms - public int closestValue(TreeNode root, double target) { - if (root == null) { - return 0; - } - double delta = Double.MAX_VALUE; - return dfs(root, target, delta, root.val); - } - - private int dfs(TreeNode root, double target, double delta, int closestVal) { - if (Math.abs(root.val - target) < delta) { - closestVal = root.val; - delta = Math.abs(root.val - target); - } - int leftVal = closestVal; - if (root.left != null) { - leftVal = dfs(root.left, target, delta, closestVal); - } - int rightVal = closestVal; - if (root.right != null) { - rightVal = dfs(root.right, target, delta, closestVal); - } - return (Math.abs(leftVal - target) > Math.abs(rightVal - target)) ? rightVal : leftVal; - } - } - - public static class Solution2 { - // BST solution - // we can tailor the solution to use the BST feature: left subtrees are always smaller than the root the right subtrees - //this finished in 0 ms - public int closestValue(TreeNode root, double target) { - if (root == null) { - return 0; - } - return dfs(root, target, root.val); - } - - private int dfs(TreeNode root, double target, int minVal) { - if (root == null) { - return minVal; - } - if (Math.abs(root.val - target) < Math.abs(minVal - target)) { - minVal = root.val; - } - if (target < root.val) { - minVal = dfs(root.left, target, minVal); - } else { - minVal = dfs(root.right, target, minVal); - } - return minVal; - } - } - - public static class Solution3 { - //a more concise solution - public int closestValue(TreeNode root, double target) { - if (root == null) { - return 0; - } - return dfs(root, target, root.val); - } - - private int dfs(TreeNode root, double target, int minVal) { - if (root == null) { - return minVal; - } - if (Math.abs(root.val - target) < Math.abs(minVal - target)) { - minVal = root.val; - } - minVal = dfs(root.left, target, minVal); - minVal = dfs(root.right, target, minVal); - return minVal; - } - } - - public static class Solution4 { - //BST iterative solution - public int closestValue(TreeNode root, double target) { - long minVal = Long.MAX_VALUE; - while (root != null) { - if (Math.abs(root.val - target) < Math.abs(minVal - target)) { - minVal = root.val; - } - if (target < root.val) { - root = root.left; - } else { - root = root.right; - } - } - return minVal == Long.MAX_VALUE ? 0 : (int) minVal; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_271.java b/src/main/java/com/fishercoder/solutions/_271.java deleted file mode 100644 index 4eaaed5575..0000000000 --- a/src/main/java/com/fishercoder/solutions/_271.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 271. Encode and Decode Strings - * - * Design an algorithm to encode a list of strings to a string. - * The encoded string is then sent over the network and is decoded back to the original list of strings. - - Machine 1 (sender) has the function: - - string encode(vector strs) { - // ... your code - return encoded_string; - } - Machine 2 (receiver) has the function: - vector decode(string s) { - //... your code - return strs; - } - So Machine 1 does: - - string encoded_string = encode(strs); - and Machine 2 does: - - vector strs2 = decode(encoded_string); - strs2 in Machine 2 should be the same as strs in Machine 1. - - Implement the encode and decode methods. - - Note: - The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters. - Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless. - Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm. - */ -public class _271 { - public static class Solution1 { - // Encodes a list of strings to a single string. - public String encode(List strs) { - StringBuilder sb = new StringBuilder(); - for (String s : strs) { - sb.append(s.length()).append('/').append(s); - } - return sb.toString(); - } - - // Decodes a single string to a list of strings. - public List decode(String s) { - List result = new ArrayList<>(); - int i = 0; - while (i < s.length()) { - int slash = s.indexOf('/', i); - int size = Integer.valueOf(s.substring(i, slash)); - result.add(s.substring(slash + 1, slash + 1 + size)); - i = slash + size + 1; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_272.java b/src/main/java/com/fishercoder/solutions/_272.java deleted file mode 100644 index a43f3757fc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_272.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -/** - * 272. Closest Binary Search Tree Value II - * - * Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. - - Note: - Given target value is a floating point. - You may assume k is always valid, that is: k ≤ total nodes. - You are guaranteed to have only one unique set of k values in the BST that are closest to the target. - Follow up: - Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)? - - Hint: - - Consider implement these two helper functions: - getPredecessor(N), which returns the next smaller node to N. - getSuccessor(N), which returns the next larger node to N. - Try to assume that each node has a parent pointer, it makes the problem much easier. - Without parent pointer we just need to keep track of the path from the root to the current node using a stack. - You would need two stacks to track the path in finding predecessor and successor node separately. - - */ -public class _272 { - - public static class Solution1 { - public List closestKValues(TreeNode root, double target, int k) { - List res = new ArrayList(); - - Stack s1 = new Stack(); // predecessors - Stack s2 = new Stack(); // successors - - inorder(root, target, false, s1); - inorder(root, target, true, s2); - - while (k-- > 0) { - if (s1.isEmpty()) { - res.add(s2.pop()); - } else if (s2.isEmpty()) { - res.add(s1.pop()); - } else if (Math.abs(s1.peek() - target) < Math.abs(s2.peek() - target)) { - res.add(s1.pop()); - } else { - res.add(s2.pop()); - } - } - - return res; - } - - // inorder traversal - void inorder(TreeNode root, double target, boolean reverse, Stack stack) { - if (root == null) { - return; - } - - inorder(reverse ? root.right : root.left, target, reverse, stack); - // early terminate, no need to traverse the whole tree - if ((reverse && root.val <= target) || (!reverse && root.val > target)) { - return; - } - // track the value of current node - stack.push(root.val); - inorder(reverse ? root.left : root.right, target, reverse, stack); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_273.java b/src/main/java/com/fishercoder/solutions/_273.java deleted file mode 100644 index 44dd58252b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_273.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 273. Integer to English Words - * - * Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. - - For example, - 123 -> "One Hundred Twenty Three" - 12345 -> "Twelve Thousand Three Hundred Forty Five" - 1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" - - Hint: - Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. - Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. - There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out) - */ -public class _273 { - - public static class Solution1 { - private String[] belowTen = new String[]{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; - private String[] belowTwenty = new String[]{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; - private String[] belowHundred = new String[]{"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; - private String[] overThousand = new String[]{"Thousand", "Million", "Billion"}; - - public String numberToWords(int num) { - String result; - if (num == 0) { - return belowTen[num]; - } - - result = hundredHelper(num % 1000); - num = num / 1000; - int i = 0; - while (i < 3 && num > 0) { - if (num % 1000 > 0) { - result = hundredHelper(num % 1000) + overThousand[i] + " " + result; - } - num = num / 1000; - i++; - } - - return result.trim(); - } - - private String hundredHelper(int num) { - String nstr = ""; - if (num >= 100) { - nstr = belowTen[num / 100] + " Hundred "; - } - num = num % 100; - if (num >= 20) { - if (num % 10 != 0) { - nstr = nstr + belowHundred[num / 10 - 2] + " " + belowTen[num % 10] + " "; - } else { - nstr = nstr + belowHundred[num / 10 - 2] + " "; - } - } else if (num >= 10) { - nstr = nstr + belowTwenty[num % 10] + " "; - } else if (num > 0) { - nstr = nstr + belowTen[num] + " "; - } - return nstr; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_274.java b/src/main/java/com/fishercoder/solutions/_274.java deleted file mode 100644 index 0703877384..0000000000 --- a/src/main/java/com/fishercoder/solutions/_274.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 274. H-Index - * - * Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index. - * According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each." - * For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. - * Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3. - - Note: If there are several possible values for h, the maximum one is taken as the h-index. - - Hint: - An easy approach is to sort the array first. - What are the possible values of h-index? - A faster approach is to use extra space. - */ -public class _274 { - public static class Solution1 { - public int hIndex(int[] citations) { - if (citations == null || citations.length == 0) { - return 0; - } - - Arrays.sort(citations); - for (int i = 0; i < citations.length; i++) { - if (citations[i] >= citations.length - i) { - return citations.length - i; - } - } - return 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_275.java b/src/main/java/com/fishercoder/solutions/_275.java deleted file mode 100644 index 8082675708..0000000000 --- a/src/main/java/com/fishercoder/solutions/_275.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 275. H-Index II - * - * Follow up for H-Index: What if the citations array is sorted in ascending order? - * Could you optimize your algorithm? - */ -public class _275 { - public static class Solution1 { - public int hIndex(int[] citations) { - int left = 0; - int len = citations.length; - int right = len - 1; - while (left <= right) { - int mid = left + (right - left) / 2; - if (citations[mid] >= (len - mid)) { - right = mid - 1; - } else { - left = mid + 1; - } - } - return len - left; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_276.java b/src/main/java/com/fishercoder/solutions/_276.java deleted file mode 100644 index 97f713ae14..0000000000 --- a/src/main/java/com/fishercoder/solutions/_276.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 276. Paint Fence - - There is a fence with n posts, each post can be painted with one of the k colors. - - You have to paint all the posts such that no more than two adjacent fence posts have the same color. - - Return the total number of ways you can paint the fence. - - Note: - n and k are non-negative integers.*/ - -public class _276 { - public static class Solution1 { - public int numWays(int n, int k) { - if (n == 0) { - return 0; - } else if (n == 1) { - return k; - } - int sameColorCnt = k; - int diffColorCnt = k * (k - 1); - for (int i = 2; i < n; i++) { - int temp = diffColorCnt; - diffColorCnt = (diffColorCnt + sameColorCnt) * (k - 1); - sameColorCnt = temp; - } - return sameColorCnt + diffColorCnt; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_277.java b/src/main/java/com/fishercoder/solutions/_277.java deleted file mode 100644 index 31d80eeff5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_277.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 277. Find the Celebrity - * - * Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. - * The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them. - * Now you want to find out who the celebrity is or verify that there is not one. - * The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. - * You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense). - * You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n), your function should minimize the number of calls to knows. - * - * Note: There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return -1. - */ -public class _277 { - - public static class Solution1 { - public int findCelebrity(int n) { - int candidate = 0; - for (int i = 1; i < n; i++) { - if (knows(candidate, i)) { - candidate = i; - } - } - for (int i = 0; i < n; i++) { - if (i != candidate && (knows(candidate, i) || !knows(i, candidate))) { - return -1; - } - } - return candidate; - } - - //this is a mock-up method to make IDE happy.s - private boolean knows(int i, int candidate) { - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_278.java b/src/main/java/com/fishercoder/solutions/_278.java deleted file mode 100644 index 9710346668..0000000000 --- a/src/main/java/com/fishercoder/solutions/_278.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 278. First Bad Version - * - * You are a product manager and currently leading a team to develop a new product. - * Unfortunately, the latest version of your product fails the quality check. - * Since each version is developed based on the previous version, all the versions after a bad version are also bad. - * - * Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, - * which causes all the following ones to be bad. - * - * You are given an API bool isBadVersion(version) which will return whether version is bad. - * Implement a function to find the first bad version. You should minimize the number of calls to the API. - * - * Example: - * Given n = 5, and version = 4 is the first bad version. - * call isBadVersion(3) -> false - * call isBadVersion(5) -> true - * call isBadVersion(4) -> true - * Then 4 is the first bad version. - * */ -public class _278 { - - public static class Solution1 { - public int firstBadVersion(int n) { - int left = 1; - int right = n; - while (left < right) { - int mid = left + (right - left) / 2; - if (isBadVersion(mid)) { - right = mid; - } else { - left = mid + 1; - } - } - return left; - } - - private boolean isBadVersion(int left) { - //this is a fake method to make Eclipse happy - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_279.java b/src/main/java/com/fishercoder/solutions/_279.java deleted file mode 100644 index 3041682116..0000000000 --- a/src/main/java/com/fishercoder/solutions/_279.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 279. Perfect Squares - * - * Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. - * For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. - */ -public class _279 { - public static class Solution1 { - public int numSquares(int n) { - int result = n; - int num = 2; - while (num * num <= n) { - int temp1 = n / (num * num); - int temp2 = n % (num * num); - result = Math.min(result, temp1 + numSquares(temp2)); - num++; - } - return result; - } - } - - public static class Solution2 { - //DP solution - public int numSquares(int n) { - int[] dp = new int[n + 1]; - Arrays.fill(dp, Integer.MAX_VALUE); - dp[0] = 0; - dp[1] = 1; - for (int i = 1; i <= n; i++) { - int min = Integer.MAX_VALUE; - int j = 1; - while (i - j * j >= 0) { - min = Math.min(min, dp[i - j * j] + 1); - j++; - } - dp[i] = min; - } - return dp[n]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_28.java b/src/main/java/com/fishercoder/solutions/_28.java deleted file mode 100644 index 582899b0dc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_28.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.fishercoder.solutions; - -public class _28 { - - public static class Solution1 { - public int strStr(String haystack, String needle) { - if (haystack == null || needle == null || haystack.length() < needle.length()) { - return -1; - } - - for (int i = 0; i <= haystack.length() - needle.length(); i++) { - if (haystack.substring(i, i + needle.length()).equals(needle)) { - return i; - } - } - return -1; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_280.java b/src/main/java/com/fishercoder/solutions/_280.java deleted file mode 100644 index 19736762e6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_280.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder.solutions; - -/** - * Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... -For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]*/ -public class _280 { - public static class Solution1 { - public void wiggleSort(int[] nums) { - for (int i = 1; i < nums.length; i++) { - if ((i % 2 == 0 && nums[i] > nums[i - 1]) || (i % 2 == 1 && nums[i] < nums[i - 1])) { - swap(nums, i); - } - } - } - - void swap(int[] nums, int i) { - int temp = nums[i - 1]; - nums[i - 1] = nums[i]; - nums[i] = temp; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_281.java b/src/main/java/com/fishercoder/solutions/_281.java deleted file mode 100644 index 80d47b695e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_281.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -/** - * 281. Zigzag Iterator - * - * Given two 1d vectors, implement an iterator to return their elements alternately. - - For example, given two 1d vectors: - - v1 = [1, 2] - v2 = [3, 4, 5, 6] - By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 3, 2, 4, 5, 6]. - - Follow up: What if you are given k 1d vectors? How well can your code be extended to such cases? - - Clarification for the follow up question - Update (2015-09-18): - The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases. - If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic". For example, given the following input: - - [1,2,3] - [4,5,6,7] - [8,9] - It should return [1,4,8,2,5,9,3,6,7]. - */ -public class _281 { - - public static class Solution1 { - public static class ZigzagIterator { - private Iterator i; - private Iterator j; - private Iterator tmp; - - public ZigzagIterator(List v1, List v2) { - i = v2.iterator(); - j = v1.iterator(); - } - - public int next() { - if (j.hasNext()) { - tmp = j; - j = i; - i = tmp; - } - return i.next(); - } - - public boolean hasNext() { - return i.hasNext() || j.hasNext(); - } - } - } - - public static class Solution2 { - public static class ZigzagIterator { - - Queue> queue; - - public ZigzagIterator(List v1, List v2) { - queue = new LinkedList<>(); - if (v1 != null && !v1.isEmpty()) { - Iterator iterator1 = v1.iterator(); - queue.offer(iterator1); - } - if (v2 != null && !v2.isEmpty()) { - Iterator iterator2 = v2.iterator(); - queue.offer(iterator2); - } - } - - public boolean hasNext() { - return !queue.isEmpty(); - } - - public int next() { - Iterator iterator = queue.poll(); - int next = iterator.next(); - if (iterator.hasNext()) { - queue.offer(iterator); - } - return next; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_282.java b/src/main/java/com/fishercoder/solutions/_282.java deleted file mode 100644 index e032faede9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_282.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 282. Expression Add Operators - * - * Given a string that contains only digits 0-9 and a target value, - * return all possibilities to add binary operators (not unary) +, -, or * between the digits - * so they evaluate to the target value. - - Examples: - "123", 6 -> ["1+2+3", "1*2*3"] - "232", 8 -> ["2*3+2", "2+3*2"] - "105", 5 -> ["1*0+5","10-5"] - "00", 0 -> ["0+0", "0-0", "0*0"] - "3456237490", 9191 -> [] - */ -public class _282 { - - public static class Solution1 { - public List addOperators(String num, int target) { - List res = new ArrayList<>(); - StringBuilder sb = new StringBuilder(); - dfs(res, sb, num, 0, target, 0, 0); - return res; - - } - - private void dfs(List res, StringBuilder sb, String num, int pos, int target, long prev, long multi) { - if (pos == num.length()) { - if (target == prev) { - res.add(sb.toString()); - } - return; - } - for (int i = pos; i < num.length(); i++) { - if (num.charAt(pos) == '0' && i != pos) { - break; - } - long curr = Long.parseLong(num.substring(pos, i + 1)); - int len = sb.length(); - if (pos == 0) { - dfs(res, sb.append(curr), num, i + 1, target, curr, curr); - sb.setLength(len); - } else { - dfs(res, sb.append("+").append(curr), num, i + 1, target, prev + curr, curr); - sb.setLength(len); - - dfs(res, sb.append("-").append(curr), num, i + 1, target, prev - curr, -curr); - sb.setLength(len); - - dfs(res, sb.append("*").append(curr), num, i + 1, target, prev - multi + multi * curr, multi * curr); - sb.setLength(len); - } - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_283.java b/src/main/java/com/fishercoder/solutions/_283.java deleted file mode 100644 index 5673eb84e5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_283.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 283. Move Zeroes - * - * Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. - * - * For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. - * - * Note: - * You must do this in-place without making a copy of the array. - * Minimize the total number of operations.*/ -public class _283 { - public static class Solution1 { - public void moveZeroes(int[] nums) { - //keep the last non-zero index and keep overwriting it, then append zeroes to fill the end - int j = 0; - int i = 0; - for (; j < nums.length; j++) { - if (nums[j] != 0) { - nums[i++] = nums[j]; - } - } - for (; i < nums.length; i++) { - nums[i] = 0; - } - } - } - - public static class Solution2 { - public void moveZeroes(int[] nums) { - //this solutoin is the most optimal since it minimizes the number of operations - //the idea is to swap the non-zero element to the first zero number position - for (int i = 0, j = 0; i < nums.length && j < nums.length; j++) { - if (nums[j] != 0) { - int temp = nums[i]; - nums[i++] = nums[j]; - nums[j] = temp; - } - } - } - } - - //then I came up with this solution and got it AC'ed! Cheers! - //basically, find the next non-zero number and swap it with the current zero number - //Apparently it's not the most optimal, since this is basically an O(n^2) solution, then I turned to Editorial solutions - public static class Solution3 { - public void moveZeroes(int[] nums) { - for (int i = 0; i < nums.length - 1; i++) { - if (nums[i] == 0) { - int j = i + 1; - while (j < nums.length && nums[j] == 0) { - j++; - } - if (j >= nums.length) { - return; - } else { - int temp = nums[j]; - nums[j] = nums[i]; - nums[i] = temp; - } - } - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_284.java b/src/main/java/com/fishercoder/solutions/_284.java deleted file mode 100644 index b2910b8d6e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_284.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.Queue; - -/** - * 284. Peeking Iterator - * - * Given an Iterator class interface with methods: next() and hasNext(), - * design and implement a PeekingIterator that support - * the peek() operation -- it essentially peek() at the element that will be returned by the next call to next(). - - Here is an example. Assume that the iterator is initialized to the beginning of the queue: [1, 2, 3]. - - Call next() gets you 1, the first element in the queue. - - Now you call peek() and it returns 2, the next element. Calling next() after that still return 2. - - You call next() the final time and it returns 3, the last element. Calling hasNext() after that should return false. - - Follow up: How would you extend your design to be generic and work with all types, not just integer? - */ -public class _284 { - public static class Solution1 { - public static class PeekingIterator implements Iterator { - - private Queue queue; - - public PeekingIterator(Iterator iterator) { - // initialize any member here. - queue = new LinkedList<>(); - while (iterator.hasNext()) { - queue.add(iterator.next()); - } - } - - // Returns the next element in the iteration without advancing the iterator. - public Integer peek() { - return queue.peek(); - } - - // hasNext() and next() should behave the same as in the Iterator interface. - // Override them if needed. - @Override - public Integer next() { - return queue.poll(); - } - - @Override - public boolean hasNext() { - return !queue.isEmpty(); - } - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_285.java b/src/main/java/com/fishercoder/solutions/_285.java deleted file mode 100644 index 2365daaf6d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_285.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.Iterator; -import java.util.Map; -import java.util.TreeMap; - -/**285. Inorder Successor in BST - -Given a binary search tree and a node in it, find the in-order successor of that node in the BST. - -Note: If the given node has no in-order successor in the tree, return null. */ -public class _285 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/25698/java-python-solution-o-h-time-and-o-1-space-iterative - * The inorder traversal of a BST is the nodes in ascending order. - * To find a successor, you just need to find the smallest one that is larger than the given value since there are no duplicate values in a BST. - * It's just like the binary search in a sorted list. - *

- * The time complexity should be O(h) where h is the depth of the result node. - * succ is a pointer that keeps the possible successor. - * Whenever you go left the current root is the new possible successor, otherwise the it remains the same. - *

- * Only in a balanced BST O(h) = O(log n). In the worst case h can be as large as n. - */ - public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { - TreeNode successor = null; - while (root != null) { - if (p.val < root.val) { - successor = root; - root = root.left; - } else { - root = root.right; - } - } - return successor; - } - } - - public static class Solution2 { - public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { - TreeMap map = new TreeMap<>(); - inorderTraversal(root, map); - Iterator> iterator = map.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - if (entry.getValue() == p) { - if (iterator.hasNext()) { - return iterator.next().getValue(); - } else { - return null; - } - } - } - return null; - } - - private void inorderTraversal(TreeNode root, TreeMap map) { - if (root == null) { - return; - } - inorderTraversal(root.left, map); - map.put(root.val, root); - inorderTraversal(root.right, map); - return; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_286.java b/src/main/java/com/fishercoder/solutions/_286.java deleted file mode 100644 index d5886a3043..0000000000 --- a/src/main/java/com/fishercoder/solutions/_286.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * You are given a m x n 2D grid initialized with these three possible values. - - -1 - A wall or an obstacle. - 0 - A gate. - INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647. - Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF. - - For example, given the 2D grid: - INF -1 0 INF - INF INF INF -1 - INF -1 INF -1 - 0 -1 INF INF - - After running your function, the 2D grid should be: - 3 -1 0 1 - 2 2 1 -1 - 1 -1 2 -1 - 0 -1 3 4 - - */ -public class _286 { - public static class Solution1 { - - int[] dirs = new int[]{0, 1, 0, -1, 0}; - - public void wallsAndGates(int[][] rooms) { - if (rooms == null || rooms.length == 0 || rooms[0].length == 0) { - return; - } - int m = rooms.length; - int n = rooms[0].length; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (rooms[i][j] == 0) { - bfs(rooms, i, j, m, n); - } - } - } - } - - void bfs(int[][] rooms, int i, int j, int m, int n) { - for (int k = 0; k < 4; k++) { - int x = dirs[k] + i; - int y = dirs[k + 1] + j; - if (x >= 0 && y >= 0 && x < m && y < n && rooms[x][y] > rooms[i][j] + 1) { - rooms[x][y] = rooms[i][j] + 1; - bfs(rooms, x, y, m, n); - } - } - } - - } - - public static class Solution2 { - - //push all gates into the queue first, and then put all its neighbours into the queue with one distance to the gate, then continue to push the rest of the nodes into the queue, and put all their neighbours into the queue with the nodes' value plus one until the queue is empty - int[] dirs = new int[]{0, 1, 0, -1, 0}; - - public void wallsAndGates(int[][] rooms) { - if (rooms == null || rooms.length == 0 || rooms[0].length == 0) { - return; - } - int m = rooms.length; - int n = rooms[0].length; - Queue queue = new LinkedList(); - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (rooms[i][j] == 0) { - queue.offer(new int[]{i, j}); - } - } - } - - while (!queue.isEmpty()) { - int[] curr = queue.poll(); - for (int k = 0; k < 4; k++) { - int x = curr[0] + dirs[k]; - int y = curr[1] + dirs[k + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && rooms[x][y] == Integer.MAX_VALUE) { - rooms[x][y] = rooms[curr[0]][curr[1]] + 1; - queue.offer(new int[]{x, y}); - } - } - } - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_287.java b/src/main/java/com/fishercoder/solutions/_287.java deleted file mode 100644 index 55109da3a3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_287.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 287. Find the Duplicate Number - * - * Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), - * prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. - * - Note: - You must not modify the array (assume the array is read only). - You must use only constant, O(1) extra space. - Your runtime complexity should be less than O(n2). - There is only one duplicate number in the array, but it could be repeated more than once. - - */ -public class _287 { - - public static class Solution1 { - /**no-brainer, used O(n) space*/ - public int findDuplicate(int[] nums) { - Set set = new HashSet<>(); - int dup = 0; - for (int i = 0; i < nums.length; i++) { - if (!set.add(nums[i])) { - dup = nums[i]; - break; - } - } - return dup; - } - } - - public static class Solution2 { - /** O(1) space */ - public int findDuplicate(int[] nums) { - int slow = 0; - int fast = 0; - int finder = 0; - while (true) { - slow = nums[slow]; - fast = nums[nums[fast]]; - - if (slow == fast) { - break; - } - } - - while (true) { - slow = nums[slow]; - finder = nums[finder]; - if (slow == finder) { - return slow; - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_288.java b/src/main/java/com/fishercoder/solutions/_288.java deleted file mode 100644 index 6427ee7b01..0000000000 --- a/src/main/java/com/fishercoder/solutions/_288.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -/**An abbreviation of a word follows the form . Below are some examples of word abbreviations: - - a) it --> it (no abbreviation) - - 1 - b) d|o|g --> d1g - - 1 1 1 - 1---5----0----5--8 - c) i|nternationalizatio|n --> i18n - - 1 - 1---5----0 - d) l|ocalizatio|n --> l10n - Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word's abbreviation is unique if no other word from the dictionary has the same abbreviation. - - Example: - Given dictionary = [ "deer", "door", "cake", "card" ] - - isUnique("dear") -> - false - - isUnique("cart") -> - true - - isUnique("cane") -> - false - - isUnique("make") -> - true - */ -public class _288 { - public static class Solution1 { - - public class ValidWordAbbr { - private Map dict; - - public ValidWordAbbr(String[] dictionary) { - dict = new HashMap(); - for (String word : dictionary) { - String key = word.length() <= 2 ? word : (word.charAt(0) + String.valueOf(word.length() - 2) + word.charAt(word.length() - 1)); - if (dict.containsKey(key) && !dict.get(key).equals(word)) { - dict.put(key, ""); - } else { - dict.put(key, word); - } - } - } - - public boolean isUnique(String word) { - String key = word.length() <= 2 ? word : (word.charAt(0) + String.valueOf(word.length() - 2) + word.charAt(word.length() - 1)); - if (!dict.containsKey(key)) { - return true; - } else { - return dict.get(key) != "" && dict.get(key).equals(word); - } - } - } - } - - public static class Solution2 { - public class ValidWordAbbr { - - private Map> dict; - - public ValidWordAbbr(String[] dictionary) { - dict = new HashMap(); - for (String word : dictionary) { - String key = word.length() <= 2 ? word : (word.charAt(0) + String.valueOf(word.length() - 2) + word.charAt(word.length() - 1)); - if (dict.containsKey(key)) { - Set set = dict.get(key); - set.add(word); - dict.put(key, set); - } else { - Set set = new HashSet(); - set.add(word); - dict.put(key, set); - } - } - } - - public boolean isUnique(String word) { - String key = word.length() <= 2 ? word : (word.charAt(0) + String.valueOf(word.length() - 2) + word.charAt(word.length() - 1)); - if (!dict.containsKey(key)) { - return true; - } else { - Set set = dict.get(key); - if (set.size() != 1) { - return false; - } - Iterator it = set.iterator(); - return it.next().equals(word); - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_289.java b/src/main/java/com/fishercoder/solutions/_289.java deleted file mode 100644 index b202da1769..0000000000 --- a/src/main/java/com/fishercoder/solutions/_289.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 289. Game of Life - * - * According to the Wikipedia's article: "The Game of Life, also known simply as Life, - * is a cellular automaton devised by the British mathematician John Horton Conway in 1970." - - Given a board with m by n cells, each cell has an initial state live (1) or dead (0). - Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the - following four rules (taken from the above Wikipedia article): - - Any live cell with fewer than two live neighbors dies, as if caused by under-population. - Any live cell with two or three live neighbors lives on to the next generation. - Any live cell with more than three live neighbors dies, as if by over-population.. - Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. - Write a function to compute the next state (after one update) of the board given its current state. - - Follow up: - Could you solve it in-place? Remember that the board needs to be updated at the same time: - You cannot update some cells first and then use their updated values to update other cells. - In this question, we represent the board using a 2D array. - In principle, the board is infinite, which would cause problems when the active area encroaches the - border of the array. How would you address these problems?*/ - -public class _289 { - public static class Solution1 { - public void gameOfLife(int[][] board) { - int height = board.length; - int width = board[0].length; - int[][] next = new int[height][width]; - int[][] directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}}; - - for (int i = 0; i < board.length; i++) { - for (int j = 0; j < board[0].length; j++) { - int liveCellsCount = 0; - //count all its live cells - - for (int[] dir : directions) { - int x = i + dir[0]; - int y = j + dir[1]; - if (x >= 0 && y >= 0 && x < height && y < width && board[x][y] == 1) { - liveCellsCount++; - } - } - - if (board[i][j] == 1) { - if (liveCellsCount <= 3 && liveCellsCount >= 2) { - next[i][j] = 1; - } - } else if (board[i][j] == 0) { - if (liveCellsCount == 3) { - next[i][j] = 1; - } - } - } - } - - for (int i = 0; i < board.length; i++) { - for (int j = 0; j < board[0].length; j++) { - board[i][j] = next[i][j]; - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_29.java b/src/main/java/com/fishercoder/solutions/_29.java deleted file mode 100644 index d0b988b275..0000000000 --- a/src/main/java/com/fishercoder/solutions/_29.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -public class _29 { - - public static class Solution1 { - public int divide(int dividend, int divisor) { - if (divisor == 0 || (dividend == Integer.MIN_VALUE && divisor == -1)) { - return Integer.MAX_VALUE; - } - if (dividend != Integer.MIN_VALUE - && Math.abs(dividend) < Math.abs(divisor)) { - return 0; - } - if (divisor == Integer.MIN_VALUE) { - return (dividend == Integer.MIN_VALUE) ? 1 : 0; - } - - boolean flag = (dividend < 0) ^ (divisor < 0); - dividend = -Math.abs(dividend); - divisor = -Math.abs(divisor); - int[] num = new int[40]; - int[] multiple = new int[40]; - num[1] = divisor; - multiple[1] = 1; - - for (int i = 2; i < 32 && num[i - 1] < 0; ++i) { - num[i] = num[i - 1] << 1; - multiple[i] = multiple[i - 1] << 1; - } - - int result = 0; - int index = 1; - while (num[index] < 0) { - ++index; - } - index -= 1; - - while (dividend <= divisor) { - while (dividend <= num[index]) { - result += multiple[index]; - dividend -= num[index]; - } - --index; - } - return !flag ? result : -result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_290.java b/src/main/java/com/fishercoder/solutions/_290.java deleted file mode 100644 index 5b64ebe754..0000000000 --- a/src/main/java/com/fishercoder/solutions/_290.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 290. Word Pattern - * - * Given a pattern and a string str, find if str follows the same pattern. - * Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. - - Examples: - pattern = "abba", str = "dog cat cat dog" should return true. - pattern = "abba", str = "dog cat cat fish" should return false. - pattern = "aaaa", str = "dog cat cat dog" should return false. - pattern = "abba", str = "dog dog dog dog" should return false. - - Notes: - You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space. - */ - -public class _290 { - - public static class Solution1 { - public boolean wordPattern(String pattern, String str) { - String[] words = str.split(" "); - char[] patterns = pattern.toCharArray(); - Map map = new HashMap(); - if (patterns.length != words.length) { - return false; - } - for (int i = 0; i < patterns.length; i++) { - if (map.containsKey(patterns[i])) { - if (!map.get(patterns[i]).equals(words[i])) { - return false; - } - } else { - if (map.containsValue(words[i])) { - return false;//this is for this case: "abba", "dog dog dog dog" - } - map.put(patterns[i], words[i]); - } - } - return true; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_291.java b/src/main/java/com/fishercoder/solutions/_291.java deleted file mode 100644 index 4c607e72cf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_291.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 291. Word Pattern II - * - * Given a pattern and a string str, find if str follows the same pattern. - * Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str. - - Examples: - pattern = "abab", str = "redblueredblue" should return true. - pattern = "aaaa", str = "asdasdasdasd" should return true. - pattern = "aabb", str = "xyzabcxzyabc" should return false. - - Notes: - You may assume both pattern and str contains only lowercase letters. - */ -public class _291 { - - public static class Solution1 { - /** - * We can try recursively: - * say pattern is "abab", str is "redblueredblue" - * first we try if "a" matches with "r", "b" matches with "e", we find it's not, so we try to see if "b" matches "ed", and so on ... - * then eventually, we find this pattern: - * "a" matches "red" - * "b" matches "blue" - * then we'll just finish the str check based on this pattern - * */ - public boolean wordPatternMatch(String pattern, String str) { - Map map = new HashMap(); - Set set = new HashSet(); - return isMatch(str, 0, pattern, 0, map, set); - } - - private boolean isMatch(String str, int i, String pattern, int j, Map map, Set set) { - //base case - if (i == str.length() && j == pattern.length()) { - return true; - } - if (i == str.length() || j == pattern.length()) { - return false; - } - - char c = pattern.charAt(j); - - if (map.containsKey(c)) { - String s = map.get(c); - - //check to see if we can use s to match str.substring(i, i + s.length()) - if (!str.startsWith(s, i)) { - return false; - } - - //if it's match, great, then let's check the rest - return isMatch(str, i + s.length(), pattern, j + 1, map, set); - } - - for (int k = i; k < str.length(); k++) { - String p = str.substring(i, k + 1); - - if (set.contains(p)) { - continue; - } - - map.put(c, p); - set.add(p); - - //continue to match the rest - if (isMatch(str, k + 1, pattern, j + 1, map, set)) { - return true; - } - - //backtracking - map.remove(c); - set.remove(p); - } - - //we've tried everything, but still no luck - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_292.java b/src/main/java/com/fishercoder/solutions/_292.java deleted file mode 100644 index 8e892f9835..0000000000 --- a/src/main/java/com/fishercoder/solutions/_292.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder.solutions; - -/**You are playing the following Nim Game with your friend: - * There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. - * The one who removes the last stone will be the winner. You will take the first turn to remove the stones. - * Both of you are very clever and have optimal strategies for the game. - * Write a function to determine whether you can win the game given the number of stones in the heap. - * For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, - * the last stone will always be removed by your friend. - - Hint: - If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?*/ - -public class _292 { - - public static class Solution1 { - /** - * 1. If there are only 1 or 2 or 3 stones, you could always win by taking 1 or 2 or 3 stones; - * 2. If there are 4 stones, you could never win because no matter you tak 1 or 2 or 3 stones, you could never take the 4th one; - * 3. If there are 5 or 6 or 7 stones, you could always win because no matter how your opponent works, you'll always get the last one; - * 4. Then we could deduce that as long as the number is not divisible by 4, you could always win. - */ - - public boolean canWinNim(int n) { - return n % 4 != 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_293.java b/src/main/java/com/fishercoder/solutions/_293.java deleted file mode 100644 index 7633064129..0000000000 --- a/src/main/java/com/fishercoder/solutions/_293.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; -/** - * 293. Flip Game - * - * You are playing the following Flip Game with your friend: Given a string that contains only - * these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". - * The game ends when a person can no longer make a move and therefore the other person will be the winner. - Write a function to compute all possible states of the string after one valid move. - For example, given s = "++++", after one move, it may become one of the following states: - - [ - "--++", - "+--+", - "++--" - ] - If there is no valid move, return an empty list [].*/ -public class _293 { - - public static class Solutoin1 { - public List generatePossibleNextMoves(String s) { - List result = new ArrayList<>(); - for (int i = 1; i < s.length(); i++) { - if (s.charAt(i) == '+' && s.charAt(i - 1) == '+') { - result.add(s.substring(0, i - 1) + "--" + s.substring(i + 1)); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_294.java b/src/main/java/com/fishercoder/solutions/_294.java deleted file mode 100644 index ae67688977..0000000000 --- a/src/main/java/com/fishercoder/solutions/_294.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * You are playing the following Flip Game with your friend: - * Given a string that contains only these two characters: + and -, - * you and your friend take turns to flip two consecutive "++" into "--". - * The game ends when a person can no longer make a move and therefore the other person will be the winner. - - Write a function to determine if the starting player can guarantee a win. - - For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+". - - Follow up: - Derive your algorithm's runtime complexity. - */ -public class _294 { - - public static class Solution1 { - public boolean canWin(String s) { - List res = new ArrayList<>(); - char[] charArray = s.toCharArray(); - for (int i = 0; i < s.length() - 1; i++) { - if (charArray[i] == '+' && charArray[i + 1] == '+') { - //change these two bits to '-' - charArray[i] = '-'; - charArray[i + 1] = '-'; - res.add(String.valueOf(charArray)); - //change these two bits back to '+' for its next move - charArray[i] = '+'; - charArray[i + 1] = '+'; - } - } - /**The above part is the same of Flip Game I. - * The only added part is the following piece of logic (so-called backtracking.)*/ - for (String str : res) { - if (!canWin(str)) { - return true; - } - } - return false; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_295.java b/src/main/java/com/fishercoder/solutions/_295.java deleted file mode 100644 index c5614f5318..0000000000 --- a/src/main/java/com/fishercoder/solutions/_295.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Collections; -import java.util.PriorityQueue; -import java.util.Queue; - -/** - * 295. Find Median from Data Stream - * - * Median is the middle value in an ordered integer list. - * If the size of the list is even, there is no middle value. - * So the median is the mean of the two middle value. - - Examples: - [2,3,4] , the median is 3 - - [2,3], the median is (2 + 3) / 2 = 2.5 - - Design a data structure that supports the following two operations: - - void addNum(int num) - Add a integer number from the data stream to the data structure. - double findMedian() - Return the median of all elements so far. - For example: - - addNum(1) - addNum(2) - findMedian() -> 1.5 - addNum(3) - findMedian() -> 2 - */ -public class _295 { - /**A few key points for both following solutions: - * - * 1. always keep one queue one element more than the other if the number is odd, offer into that one - * first, then poll from that queue and offer into the other queue, then check whether that queue is smaller - * in size than the other, if so, poll one from the other queue and offer it into this queue - * - * 2. only need to check whether this bigger queue size is greater than the other queue when returning.*/ - - public static class Solution1 { - public static class MedianFinder { - private Queue large; - private Queue small; - - public MedianFinder() { - large = new PriorityQueue<>(); - small = new PriorityQueue<>(Collections.reverseOrder()); - } - - // Adds a number into the data structure. - public void addNum(int num) { - large.offer((long) num); - small.offer(large.poll()); - if (large.size() < small.size()) { - large.offer(small.poll()); - } - } - - // Returns the median of current data stream - public double findMedian() { - if (large.size() > small.size()) { - return large.peek(); - } - return (large.peek() + small.peek()) / 2.0; - } - } - } - - public static class Solution2 { - public static class MedianFinder { - /** - * credit: https://discuss.leetcode.com/topic/27521/short-simple-java-c-python-o-log-n-o-1 - * The idea is for sure to use two heaps, one is max heap, one is min heap, we always let the max heap be one element - * bigger than min heap if the total number of elements is not even. - * we could always get the median in O(1) time. - * 1. use Long type to avoid overflow - * 2. negate the numbers for small heap to save the effort for writing a reverse comparator, brilliant! - */ - - private Queue large; - private Queue small; - - /** - * initialize your data structure here. - */ - public MedianFinder() { - large = new PriorityQueue<>(); - small = new PriorityQueue<>(); - } - - // Adds a number into the data structure. - public void addNum(int num) { - large.offer((long) num); - small.offer(-large.poll()); - if (large.size() < small.size()) { - large.offer(-small.poll()); - } - } - - // Returns the median of current data stream - public double findMedian() { - if (large.size() > small.size()) { - return large.peek(); - } - return (large.peek() - small.peek()) / 2.0; - } - - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_296.java b/src/main/java/com/fishercoder/solutions/_296.java deleted file mode 100644 index 222e8ce1ac..0000000000 --- a/src/main/java/com/fishercoder/solutions/_296.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * 296: Best Meeting Point - * - * A group of two or more people wants to meet and minimize the total travel distance. - * You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. - * The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|. - - For example, given three people living at (0,0), (0,4), and (2,2): - - 1 - 0 - 0 - 0 - 1 - | | | | | - 0 - 0 - 0 - 0 - 0 - | | | | | - 0 - 0 - 1 - 0 - 0 - The point (0,2) is an ideal meeting point, as the total travel distance of 2+2+2=6 is minimal. So return 6. - */ -public class _296 { - public static class Solution1 { - public int minTotalDistance(int[][] grid) { - int m = grid.length; - int n = grid[0].length; - - List I = new ArrayList(m); - List J = new ArrayList(n); - - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == 1) { - I.add(i); - J.add(j); - } - } - } - - return getMin(I) + getMin(J); - } - - private int getMin(List list) { - int ret = 0; - - Collections.sort(list); - - int i = 0; - int j = list.size() - 1; - while (i < j) { - ret += list.get(j--) - list.get(i++); - } - - return ret; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_297.java b/src/main/java/com/fishercoder/solutions/_297.java deleted file mode 100644 index a99327dafc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_297.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 297. Serialize and Deserialize Binary Tree - * - * Serialization is the process of converting a data structure or object into a sequence of bits - * so that it can be stored in a file or memory buffer, - * or transmitted across a network connection link to be reconstructed later in the same or another computer environment. - * Design an algorithm to serialize and deserialize a binary tree. - * There is no restriction on how your serialization/deserialization algorithm should work. - * You just need to ensure that a binary tree can be serialized to a string and this string can - * be deserialized to the original tree structure. - - For example, you may serialize the following tree - - 1 - / \ - 2 3 - / \ - 4 5 - - as "[1,2,3,null,null,4,5]", - just the same as how LeetCode OJ serializes a binary tree. - You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself. - - Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless. - */ -public class _297 { - - public static class Solution1 { - /** - * The idea is very straightforward: - * use "#" as the terminator, do BFS, level order traversal to store all nodes values into a StringBuilder. - * When deserializing, also use a queue: pop the root into the queue first, then use a for loop to construct each node, - * then eventually just return the root. - */ - - // Encodes a tree to a single string. - public String serialize(TreeNode root) { - if (root == null) { - return ""; - } - - StringBuilder sb = new StringBuilder(); - Queue queue = new LinkedList(); - queue.offer(root); - while (!queue.isEmpty()) { - int size = queue.size(); - for (int i = 0; i < size; i++) { - TreeNode curr = queue.poll(); - if (curr == null) { - sb.append("# "); - continue; - } - sb.append(curr.val); - sb.append(" "); - queue.offer(curr.left); - queue.offer(curr.right); - } - } - return sb.toString(); - } - - // Decodes your encoded data to tree. - public TreeNode deserialize(String data) { - if (data == null || data.isEmpty()) { - return null; - } - - String[] nodes = data.split(" "); - TreeNode root = new TreeNode(Integer.valueOf(nodes[0])); - Queue queue = new LinkedList(); - queue.offer(root); - for (int i = 1; i < nodes.length; i++) { - TreeNode curr = queue.poll(); - if (!nodes[i].equals("#")) { - curr.left = new TreeNode(Integer.valueOf(nodes[i])); - queue.offer(curr.left); - } - if (!nodes[++i].equals("#")) { - curr.right = new TreeNode(Integer.valueOf(nodes[i])); - queue.offer(curr.right); - } - } - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_298.java b/src/main/java/com/fishercoder/solutions/_298.java deleted file mode 100644 index 3b6d0ea3d2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_298.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * Given a binary tree, find the length of the longest consecutive sequence path. - - The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. - The longest consecutive path need to be from parent to child (cannot be the reverse). - - For example, - 1 - \ - 3 - / \ - 2 4 - \ - 5 - Longest consecutive sequence path is 3-4-5, so return 3. - - 2 - \ - 3 - / - 2 - / -1 - Longest consecutive sequence path is 2-3,not3-2-1, so return 2. - */ -public class _298 { - - public static class Solution1 { - private int max = 1; - - public int longestConsecutive(TreeNode root) { - if (root == null) { - return 0; - } - dfs(root, 0, root.val); - return max; - } - - private void dfs(TreeNode root, int curr, int target) { - if (root == null) { - return; - } - if (root.val == target) { - curr++; - } else { - curr = 1; - } - max = Math.max(max, curr); - dfs(root.left, curr, root.val + 1); - dfs(root.right, curr, root.val + 1); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_299.java b/src/main/java/com/fishercoder/solutions/_299.java deleted file mode 100644 index 2d6a72ac26..0000000000 --- a/src/main/java/com/fishercoder/solutions/_299.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 299. Bulls and Cows - * - * You are playing the following Bulls and Cows game with your friend: - * You write down a number and ask your friend to guess what the number is. - * Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in - * both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). - * Your friend will use successive guesses and hints to eventually derive the secret number. - - For example: - - Secret number: "1807" - Friend's guess: "7810" - Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.) - Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return "1A3B". - - Please note that both secret number and friend's guess may contain duplicate digits, for example: - - Secret number: "1123" - Friend's guess: "0111" - In this case, the 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B". - You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.*/ -public class _299 { - public static class Solution1 { - public String getHint(String secret, String guess) { - int[] secretCows = new int[10]; - int[] guessCows = new int[10]; - int bulls = 0; - for (int i = 0; i < secret.length(); i++) { - if (guess.charAt(i) == secret.charAt(i)) { - bulls++; - } else { - secretCows[Character.getNumericValue(secret.charAt(i))]++; - guessCows[Character.getNumericValue(guess.charAt(i))]++; - } - } - int cows = 0; - for (int i = 0; i < 10; i++) { - cows += Math.min(secretCows[i], guessCows[i]); - } - return bulls + "A" + cows + "B"; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_3.java b/src/main/java/com/fishercoder/solutions/_3.java deleted file mode 100644 index e1f8b76053..0000000000 --- a/src/main/java/com/fishercoder/solutions/_3.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public class _3 { - - public static class Solution1 { - public int lengthOfLongestSubstring(String s) { - int result = 0; - Map map = new HashMap(); - for (int i = 0, j = i; j < s.length(); ) { - if (!map.containsKey(s.charAt(j)) || (map.containsKey(s.charAt(j)) && map.get(s.charAt(j)) == 0)) { - map.put(s.charAt(j), 1); - result = Math.max(j - i + 1, result); - j++; - } else { - map.put(s.charAt(i), map.get(s.charAt(i)) - 1); - i++; - } - } - return result; - } - } - - public static class Solution2 { - /** - * Sliding Window - * O(n) time - * O(min(m,n)) or O(k) space - */ - public int lengthOfLongestSubstring(String s) { - int n = s.length(); - Set set = new HashSet<>(); - int result = 0; - int i = 0; - int j = 0; - while (i < n && j < n) { - /**Try to extend the range i, j*/ - if (!set.contains(s.charAt(j))) { - set.add(s.charAt(j++)); - result = Math.max(result, j - i); - } else { - set.remove(s.charAt(i++)); - } - } - return result; - } - } - - public static class Solution3 { - /** - * Sliding Window - * O(n) time - * O(n) space - */ - public int lengthOfLongestSubstring(String s) { - if (s.length() == 0) { - return 0; - } - int max = 0; - Map map = new HashMap<>(); - /**Try to extend the range (i, j)*/ - for (int i = 0, j = 0; i < s.length(); i++) { - if (map.containsKey(s.charAt(i))) { - j = Math.max(j, map.get(s.charAt(i)) + 1); - } - map.put(s.charAt(i), i); - max = Math.max(max, i + 1 - j); - } - return max; - } - } - - public static class Solution4 { - /** - * Sliding Window Optimized - * O(n) time - * O(n) space - */ - public int lengthOfLongestSubstring(String s) { - if (s.length() == 0) { - return 0; - } - int max = 0; - int[] index = new int[128]; - /**Try to extend the range (i, j)*/ - for (int i = 0, j = 0; j < s.length(); j++) { - i = Math.max(index[s.charAt(j)], i); - max = Math.max(max, j - i + 1); - index[s.charAt(j)] = j + 1; - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_30.java b/src/main/java/com/fishercoder/solutions/_30.java deleted file mode 100644 index 575e490b97..0000000000 --- a/src/main/java/com/fishercoder/solutions/_30.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class _30 { - - public static class Solution1 { - /**TODO: this one is not AC'ed. fix this one.*/ - public List findSubstring(String s, String[] words) { - Map map = new HashMap<>(); - for (String word : words) { - map.put(word, 1); - } - List result = new ArrayList<>(); - int startIndex = 0; - int wordLen = words.length; - for (int i = 0; i < s.length(); i++) { - startIndex = i; - Map clone = new HashMap<>(map); - int matchedWord = 0; - for (int j = i + 1; j < s.length(); j++) { - String word = s.substring(i, j); - if (clone.containsKey(word) && clone.get(word) == 1) { - clone.put(word, 0); - i = j; - matchedWord++; - } - if (matchedWord == wordLen) { - boolean all = true; - for (String key : clone.keySet()) { - if (clone.get(key) != 0) { - all = false; - break; - } - } - if (all) { - result.add(startIndex); - } - matchedWord = 0; - } - } - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_300.java b/src/main/java/com/fishercoder/solutions/_300.java deleted file mode 100644 index 06833df364..0000000000 --- a/src/main/java/com/fishercoder/solutions/_300.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 300. Longest Increasing Subsequence - * - * Given an unsorted array of integers, find the length of longest increasing subsequence. - For example, - Given [10, 9, 2, 5, 3, 7, 101, 18], - The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. - Note that there may be more than one LIS combination, it is only necessary for you to return the length. - - Your algorithm should run in O(n2) complexity. - - Follow up: Could you improve it to O(nlogn) time complexity? - */ -public class _300 { - - public static class Solution1 { - - /** - * credit: https://discuss.leetcode.com/topic/28719/short-java-solution-using-dp-o-n-log-n - * The idea is that as you iterate the sequence, - * you keep track of the minimum value a subsequence of given length might end with, - * for all so far possible subsequence lengths. - * So dp[i] is the minimum value a subsequence of length i+1 might end with. - * Having this info, for each new number we iterate to, - * we can determine the longest subsequence where it can be appended using binary search. - * The final answer is the length of the longest subsequence we found so far. - */ - public int lengthOfLIS(int[] nums) { - int[] dp = new int[nums.length]; - int len = 0; - for (int x : nums) { - /**Java Doc of this binarySearch API: - * @return index of the search key, if it is contained in the array - * within the specified range; - * otherwise, (-(insertion point) - 1). The - * insertion point is defined as the point at which the - * key would be inserted into the array: the index of the first - * element in the range greater than the key, - * or toIndex if all - * elements in the range are less than the specified key. Note - * that this guarantees that the return value will be >= 0 if - * and only if the key is found.*/ - int index = Arrays.binarySearch(dp, 0, len, x); - if (index < 0) { - index = -(index + 1); - } - dp[index] = x; - if (index == len) { - len++; - } - } - return len; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_301.java b/src/main/java/com/fishercoder/solutions/_301.java deleted file mode 100644 index ec4ae15f15..0000000000 --- a/src/main/java/com/fishercoder/solutions/_301.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; -import java.util.Set; - -/** - * 301. Remove Invalid Parentheses - * - * Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. - - Note: The input string may contain letters other than the parentheses ( and ). - - Examples: - "()())()" -> ["()()()", "(())()"] - "(a)())()" -> ["(a)()()", "(a())()"] - ")(" -> [""] - */ -public class _301 { - - public static class Solution1 { - - public List removeInvalidParentheses(String s) { - List result = new ArrayList<>(); - if (s == null) { - return result; - } - - Set visited = new HashSet(); - Queue q = new LinkedList(); - - q.offer(s); - visited.add(s); - - boolean found = false; - - while (!q.isEmpty()) { - String curr = q.poll(); - if (isValid(curr)) { - found = true; - result.add(curr); - } - - if (found) { - continue;//this means if the initial input is already a valid one, we'll just directly return it and there's actually only one valid result - } - - for (int i = 0; i < curr.length(); i++) { - if (curr.charAt(i) != '(' && curr.charAt(i) != ')') { - continue;//this is to rule out those non-parentheses characters - } - - String next = curr.substring(0, i) + curr.substring(i + 1); - if (!visited.contains(next)) { - q.offer(next); - visited.add(next); - } - } - - } - return result; - } - - private boolean isValid(String str) { - char[] chars = str.toCharArray(); - int count = 0; - for (int i = 0; i < chars.length; i++) { - char c = chars[i]; - if (c == '(') { - count++; - } - if (c == ')') { - count--; - if (count == -1) { - return false; - } - } - } - return count == 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_302.java b/src/main/java/com/fishercoder/solutions/_302.java deleted file mode 100644 index 924cab2cc2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_302.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 302. Smallest Rectangle Enclosing Black Pixels - * - * An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. - * The black pixels are connected, i.e., there is only one black region. - * Pixels are connected horizontally and vertically. - * Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels. - - For example, given the following image: - - [ - "0010", - "0110", - "0100" - ] - and x = 0, y = 2, - Return 6. - */ -public class _302 { - public static class Solution1 { - private char[][] image; - - public int minArea(char[][] iImage, int x, int y) { - image = iImage; - int m = image.length; - int n = image[0].length; - int left = searchColumns(0, y, 0, m, true); - int right = searchColumns(y + 1, n, 0, m, false); - int top = searchRows(0, x, left, right, true); - int bottom = searchRows(x + 1, m, left, right, false); - return (right - left) * (bottom - top); - } - - private int searchColumns(int i, int j, int top, int bottom, boolean opt) { - while (i != j) { - int k = top; - int mid = (i + j) / 2; - while (k < bottom && image[k][mid] == '0') { - ++k; - } - if (k < bottom == opt) { - j = mid; - } else { - i = mid + 1; - } - } - return i; - } - - private int searchRows(int i, int j, int left, int right, boolean opt) { - while (i != j) { - int k = left; - int mid = (i + j) / 2; - while (k < right && image[mid][k] == '0') { - ++k; - } - if (k < right == opt) { - j = mid; - } else { - i = mid + 1; - } - } - return i; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_303.java b/src/main/java/com/fishercoder/solutions/_303.java deleted file mode 100644 index eb8d165596..0000000000 --- a/src/main/java/com/fishercoder/solutions/_303.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 303. Range Sum Query - Immutable - * Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. - - Example: - Given nums = [-2, 0, 3, -5, 2, -1] - - sumRange(0, 2) -> 1 - sumRange(2, 5) -> -1 - sumRange(0, 5) -> -3 - - Note: - You may assume that the array does not change. - There are many calls to sumRange function. - */ -public class _303 { - public static class NumArray { - int[] sums; - - public NumArray(int[] nums) { - sums = new int[nums.length]; - for (int i = 0; i < nums.length; i++) { - if (i == 0) { - sums[i] = nums[i]; - } else { - sums[i] = sums[i - 1] + nums[i]; - } - } - } - - public int sumRange(int i, int j) { - if (i == 0) { - return sums[j]; - } - return sums[j] - sums[i - 1]; - } - } -} - -// Your NumArray object will be instantiated and called as such: -// NumArray numArray = new NumArray(nums); -// numArray.sumRange(0, 1); -// numArray.sumRange(1, 2); \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_304.java b/src/main/java/com/fishercoder/solutions/_304.java deleted file mode 100644 index a79ddaf29f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_304.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 304. Range Sum Query 2D - Immutable - * - * Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). - - Range Sum Query 2D - The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8. - - Example: - Given matrix = [ - [3, 0, 1, 4, 2], - [5, 6, 3, 2, 1], - [1, 2, 0, 1, 5], - [4, 1, 0, 1, 7], - [1, 0, 3, 0, 5] - ] - - sumRegion(2, 1, 4, 3) -> 8 - sumRegion(1, 1, 2, 2) -> 11 - sumRegion(1, 2, 2, 4) -> 12 - Note: - You may assume that the matrix does not change. - There are many calls to sumRegion function. - You may assume that row1 ≤ row2 and col1 ≤ col2. - */ -public class _304 { - - public static class Solution1 { - public class NumMatrix { - - public NumMatrix(int[][] matrix) { - if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { - return; - } - - /**The dimensions of this tot matrix is actually 1 bigger than the given matrix, cool!*/ - tot = new int[matrix.length + 1][matrix[0].length + 1]; - for (int i = 0; i < matrix.length; i++) { - for (int j = 0; j < matrix[0].length; j++) { - tot[i + 1][j + 1] = - matrix[i][j] + tot[i + 1][j] + tot[i][j + 1] - tot[i][j]; - } - } - } - - public int sumRegion(int row1, int col1, int row2, int col2) { - return tot[row2 + 1][col2 + 1] - tot[row2 + 1][col1] - tot[row1][col2 + 1] - + tot[row1][col1]; - } - - int[][] tot; - } - } -/** - * Your NumMatrix object will be instantiated and called as such: - * NumMatrix obj = new NumMatrix(matrix); - * int param_1 = obj.sumRegion(row1,col1,row2,col2); - */ -} diff --git a/src/main/java/com/fishercoder/solutions/_305.java b/src/main/java/com/fishercoder/solutions/_305.java deleted file mode 100644 index a0073320d4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_305.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 305. Number of Islands II - * - * A 2d grid map of m rows and n columns is initially filled with water. - * We may perform an addLand operation which turns the water at position (row, col) into a land. - * Given a list of positions to operate, count the number of islands after each addLand operation. - * An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. - * You may assume all four edges of the grid are all surrounded by water. - - Example: - - Given m = 3, n = 3, positions = [[0,0], [0,1], [1,2], [2,1]]. - Initially, the 2d grid grid is filled with water. (Assume 0 represents water and 1 represents land). - - 0 0 0 - 0 0 0 - 0 0 0 - Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land. - - 1 0 0 - 0 0 0 Number of islands = 1 - 0 0 0 - Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land. - - 1 1 0 - 0 0 0 Number of islands = 1 - 0 0 0 - Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land. - - 1 1 0 - 0 0 1 Number of islands = 2 - 0 0 0 - Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land. - - 1 1 0 - 0 0 1 Number of islands = 3 - 0 1 0 - We return the result as an array: [1, 1, 2, 3] - - Challenge: - - Can you do it in time complexity O(k log mn), where k is the length of the positions? - */ -public class _305 { - public static class Solution1 { - - public int find(int[] father, int id) { - int tf = father[id]; - while (tf != father[tf]) { - tf = father[tf]; - } - int cur = id; - int tmp; - while (father[cur] != tf) { - tmp = father[cur]; - father[cur] = tf; - cur = tmp; - } - return tf; - } - - public void union(int[] father, int[] sz, int id1, int id2) { - int tf1 = find(father, id1); - int tf2 = find(father, id2); - if (tf1 != tf2) { - if (sz[tf1] > sz[tf2]) { - father[tf2] = tf1; - sz[tf1] += sz[tf2]; - } else { - father[tf1] = tf2; - sz[tf2] += sz[tf1]; - } - } - } - - public List numIslands2(int m, int n, int[][] positions) { - if (m == 0 || n == 0) { - return new ArrayList<>(); - } - ArrayList res = new ArrayList(); - int[] father = new int[m * n]; - for (int i = 0; i < father.length; i++) { - father[i] = -1; - } - int[] sz = new int[m * n]; - int[] dr = {0, 0, -1, 1}; - int[] dc = {-1, 1, 0, 0}; - int r; - int c; - int nr; - int nc; - int count = 0; - for (int i = 0; i < positions.length; i++) { - r = positions[i][0]; - c = positions[i][1]; - count++; - father[r * n + c] = r * n + c; - sz[r * n + c] = 1; - for (int j = 0; j < 4; j++) { - nr = r + dr[j]; - nc = c + dc[j]; - if (nr >= 0 && nr < m && nc >= 0 && nc < n && father[nr * n + nc] != -1) { - if (find(father, r * n + c) != find(father, nr * n + nc)) { - count--; - union(father, sz, r * n + c, nr * n + nc); - } - } - } - res.add(count); - } - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_306.java b/src/main/java/com/fishercoder/solutions/_306.java deleted file mode 100644 index b268d09fcd..0000000000 --- a/src/main/java/com/fishercoder/solutions/_306.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 306. Additive Number - * - * Additive number is a string whose digits can form additive sequence. - - A valid additive sequence should contain at least three numbers. - Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two. - - For example: - "112358" is an additive number because the digits can form an additive sequence: 1, 1, 2, 3, 5, 8. - - 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8 - "199100199" is also an additive number, the additive sequence is: 1, 99, 100, 199. - 1 + 99 = 100, 99 + 100 = 199 - Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid. - - Given a string containing only digits '0'-'9', write a function to determine if it's an additive number. - - Follow up: - How would you handle overflow for very large input integers? - */ -public class _306 { - public static class Solution1 { - /** Credit: https://discuss.leetcode.com/topic/29856/java-recursive-and-iterative-solutions/2 */ - public boolean isAdditiveNumber(String num) { - int n = num.length(); - for (int i = 1; i <= n / 2; ++i) { - for (int j = 1; Math.max(j, i) <= n - i - j; ++j) { - if (isValid(i, j, num)) { - return true; - } - } - } - return false; - } - - private boolean isValid(int i, int j, String num) { - if (num.charAt(0) == '0' && i > 1) { - return false; - } - if (num.charAt(i) == '0' && j > 1) { - return false; - } - String sum; - Long x1 = Long.parseLong(num.substring(0, i)); - Long x2 = Long.parseLong(num.substring(i, i + j)); - for (int start = i + j; start != num.length(); start += sum.length()) { - x2 = x2 + x1; - x1 = x2 - x1; - sum = x2.toString(); - if (!num.startsWith(sum, start)) { - return false; - } - } - return true; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_307.java b/src/main/java/com/fishercoder/solutions/_307.java deleted file mode 100644 index 1f8e18f972..0000000000 --- a/src/main/java/com/fishercoder/solutions/_307.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 307. Range Sum Query - Mutable - * - * Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. - - The update(i, val) function modifies nums by updating the element at index i to val. - Example: - Given nums = [1, 3, 5] - - sumRange(0, 2) -> 9 - update(1, 2) - sumRange(0, 2) -> 8 - Note: - The array is only modifiable by the update function. - You may assume the number of calls to update and sumRange function is distributed evenly. - */ -public class _307 { - - public static class Solution1 { - class NumArray { - class SegmentTreeNode { - SegmentTreeNode left; - SegmentTreeNode right; - int start; - int end; - int sum; - - public SegmentTreeNode(int start, int end) { - this.start = start; - this.end = end; - this.left = null; - this.right = null; - this.sum = 0; - } - } - - private SegmentTreeNode root = null; - - public NumArray(int[] nums) { - root = buildSegmentTree(nums, 0, nums.length - 1); - } - - SegmentTreeNode buildSegmentTree(int[] nums, int start, int end) { - if (start > end) { - return null; - } else { - SegmentTreeNode root = new SegmentTreeNode(start, end); - if (start == end) { - root.sum = nums[start]; - } else { - int mid = start + (end - start) / 2; - root.left = buildSegmentTree(nums, start, mid); - root.right = buildSegmentTree(nums, mid + 1, end); - root.sum = root.left.sum + root.right.sum; - } - return root; - } - } - - void update(int i, int val) { - update(root, i, val); - } - - void update(SegmentTreeNode root, int pos, int val) { - if (root.start == root.end) { - root.sum = val; - } else { - int mid = root.start + (root.end - root.start) / 2; - if (pos <= mid) { - update(root.left, pos, val); - } else { - update(root.right, pos, val); - } - root.sum = root.left.sum + root.right.sum; - } - } - - public int sumRange(int i, int j) { - return sumRange(root, i, j); - } - - int sumRange(SegmentTreeNode root, int start, int end) { - if (root.end == end && root.start == start) { - return root.sum; - } else { - int mid = root.start + (root.end - root.start) / 2; - if (end <= mid) { - return sumRange(root.left, start, end); - } else if (start >= mid + 1) { - return sumRange(root.right, start, end); - } else { - return sumRange(root.right, mid + 1, end) + sumRange(root.left, start, mid); - } - } - } - } - } -} - diff --git a/src/main/java/com/fishercoder/solutions/_308.java b/src/main/java/com/fishercoder/solutions/_308.java deleted file mode 100644 index a6a7b0eab7..0000000000 --- a/src/main/java/com/fishercoder/solutions/_308.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 308. Range Sum Query 2D - Mutable - * - * Given a 2D matrix matrix, - * find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). - - Range Sum Query 2D - The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8. - - Example: - Given matrix = [ - [3, 0, 1, 4, 2], - [5, 6, 3, 2, 1], - [1, 2, 0, 1, 5], - [4, 1, 0, 1, 7], - [1, 0, 3, 0, 5] - ] - - sumRegion(2, 1, 4, 3) -> 8 - update(3, 2, 2) - sumRegion(2, 1, 4, 3) -> 10 - Note: - The matrix is only modifiable by the update function. - You may assume the number of calls to update and sumRegion function is distributed evenly. - You may assume that row1 ≤ row2 and col1 ≤ col2. - */ -public class _308 { - public static class Solution1 { - public class NumMatrix { - int[][] nums; - int[][] tree; - int height; - int width; - - public NumMatrix(int[][] matrix) { - if (matrix.length == 0 || matrix[0].length == 0) { - return; - } - height = matrix.length; - width = matrix[0].length; - this.nums = new int[height][width]; - this.tree = new int[height + 1][width + 1]; - for (int i = 0; i < height; i++) { - for (int j = 0; j < width; j++) { - update(i, j, matrix[i][j]); - } - } - } - - public void update(int rowIndex, int colIndex, int newVal) { - if (height == 0 || width == 0) { - return; - } - int delta = newVal - nums[rowIndex][colIndex]; - nums[rowIndex][colIndex] = newVal; - for (int i = rowIndex + 1; i <= height; i += i & (-i)) { - for (int j = colIndex + 1; j <= width; j += j & (-j)) { - tree[i][j] += delta;//just use its previous value plus delta is good - } - } - } - - public int sumRegion(int row1, int col1, int row2, int col2) { - if (height == 0 || width == 0) { - return 0; - } - return sum(row2 + 1, col2 + 1) + sum(row1, col1) - sum(row1, col2 + 1) - sum( - row2 + 1, col1); - } - - private int sum(int row, int col) { - int sum = 0; - for (int i = row; i > 0; i -= i & (-i)) { - for (int j = col; j > 0; j -= j & (-j)) { - sum += tree[i][j]; - } - } - return sum; - } - } - - /** - * Your NumMatrix object will be instantiated and called as such: - * NumMatrix obj = new NumMatrix(matrix); - * obj.update(row,col,val); - * int param_2 = obj.sumRegion(row1,col1,row2,col2); - */ - } -} diff --git a/src/main/java/com/fishercoder/solutions/_31.java b/src/main/java/com/fishercoder/solutions/_31.java deleted file mode 100644 index 474974f1a4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_31.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -public class _31 { - public static class Solution1 { - /** - * Leetcode has a very good article to illustrate this problem and with animation: - * https://leetcode.com/articles/next-permutation/ - * 1. if the array is already in decrementing order, then there's no next larger permutation possible. - * 2. if not, start from the end of the array, find the first pair of numbers that break the decrementing order - * 3. then from that index going to the right again, find the element that is closest bigger than this number, swap them - * 4. reverse the right half of this array after this index - */ - - public void nextPermutation(int[] nums) { - int i = nums.length - 2; - while (i >= 0 && nums[i] >= nums[i + 1]) { - i--; - } - if (i >= 0) { - int j = nums.length - 1; - while (j >= 0 && nums[i] >= nums[j]) { - j--; - } - - swap(nums, i, j); - } - - reverse(nums, i + 1); - } - - private void reverse(int[] nums, int start) { - int end = nums.length - 1; - while (start <= end) { - int tmp = nums[start]; - nums[start++] = nums[end]; - nums[end--] = tmp; - } - } - - private void swap(int[] nums, int i, int j) { - int tmp = nums[i]; - nums[i] = nums[j]; - nums[j] = tmp; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_310.java b/src/main/java/com/fishercoder/solutions/_310.java deleted file mode 100644 index d2e5196b68..0000000000 --- a/src/main/java/com/fishercoder/solutions/_310.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.fishercoder.solutions; - - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 310. Minimum Height Trees - * - * For a undirected graph with tree characteristics, we can choose any node as the root. - * The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). - * Given such a graph, write a function to find all the MHTs and return a list of their root labels. - - Format - The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels). - - You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges. - - Example 1: - - Given n = 4, edges = [[1, 0], [1, 2], [1, 3]] - - 0 - | - 1 - / \ - 2 3 - return [1] - - Example 2: - - Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]] - - 0 1 2 - \ | / - 3 - | - 4 - | - 5 - return [3, 4] - - Hint: - - How many MHTs can a graph have at most? - Note: - - (1) According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.” - - (2) The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf. - */ -public class _310 { - - public static class Solution1 { - public List findMinHeightTrees(int n, int[][] edges) { - if (n == 1) { - return Collections.singletonList(0); - } - - List> adj = new ArrayList<>(n); - for (int i = 0; i < n; ++i) { - adj.add(new HashSet<>()); - } - for (int[] edge : edges) { - adj.get(edge[0]).add(edge[1]); - adj.get(edge[1]).add(edge[0]); - } - - List leaves = new ArrayList<>(); - for (int i = 0; i < n; ++i) { - if (adj.get(i).size() == 1) { - leaves.add(i); - } - } - - while (n > 2) { - n -= leaves.size(); - List newLeaves = new ArrayList<>(); - for (int i : leaves) { - int j = adj.get(i).iterator().next(); - adj.get(j).remove(i); - if (adj.get(j).size() == 1) { - newLeaves.add(j); - } - } - leaves = newLeaves; - } - return leaves; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_311.java b/src/main/java/com/fishercoder/solutions/_311.java deleted file mode 100644 index 937c8e1fcf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_311.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 311. Sparse Matrix Multiplication - * - * Given two sparse matrices A and B, return the result of AB. - - You may assume that A's column number is equal to B's row number. - - Example: - - A = [ - [ 1, 0, 0], - [-1, 0, 3] - ] - - B = [ - [ 7, 0, 0 ], - [ 0, 0, 0 ], - [ 0, 0, 1 ] - ] - - | 1 0 0 | | 7 0 0 | | 7 0 0 | - AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 | - | 0 0 1 | - - */ -public class _311 { - - public static class Solution1 { - public int[][] multiply(int[][] A, int[][] B) { - int m = A.length; - int n = A[0].length; - int p = B[0].length; - int[][] C = new int[m][p]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (A[i][j] != 0) { - for (int k = 0; k < p; k++) { - if (B[j][k] != 0) { - C[i][k] += A[i][j] * B[j][k]; - } - } - } - } - } - return C; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_312.java b/src/main/java/com/fishercoder/solutions/_312.java deleted file mode 100644 index 03a9788f51..0000000000 --- a/src/main/java/com/fishercoder/solutions/_312.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 312. Burst Balloons - * - * Given n balloons, indexed from 0 to n-1. - * Each balloon is painted with a number on it represented by array nums. - * You are asked to burst all the balloons. - * If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. - * Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent. - - Find the maximum coins you can collect by bursting the balloons wisely. - - Note: - (1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them. - (2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100 - - Example: - - Given [3, 1, 5, 8] - - Return 167 - - nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] - coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167 - */ -public class _312 { - - public static class Solution1 { - public int maxCoins(int[] iNums) { - int[] nums = new int[iNums.length + 2]; - int n = 1; - for (int x : iNums) { - if (x > 0) { - nums[n++] = x; - } - } - nums[0] = nums[n++] = 1; - - int[][] dp = new int[n][n]; - for (int k = 2; k < n; ++k) { - for (int left = 0; left < n - k; ++left) { - int right = left + k; - for (int i = left + 1; i < right; ++i) { - dp[left][right] = Math.max(dp[left][right], - nums[left] * nums[i] * nums[right] + dp[left][i] + dp[i][right]); - } - } - } - return dp[0][n - 1]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_313.java b/src/main/java/com/fishercoder/solutions/_313.java deleted file mode 100644 index 5ebae7028a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_313.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 313. Super Ugly Number - * - * Write a program to find the nth super ugly number. - * Super ugly numbers are positive numbers whose all prime factors - * are in the given prime list primes of size k. - * For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] - * is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4. - * - * Note: - * (1) 1 is a super ugly number for any given primes. - * (2) The given numbers in primes are in ascending order. - * (3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000. - * (4) The nth super ugly number is guaranteed to fit in a 32-bit signed integer. - */ -public class _313 { - - public static class Solution1 { - public int nthSuperUglyNumber(int n, int[] primes) { - int[] ret = new int[n]; - ret[0] = 1; - - int[] indexes = new int[primes.length]; - - for (int i = 1; i < n; i++) { - ret[i] = Integer.MAX_VALUE; - - for (int j = 0; j < primes.length; j++) { - ret[i] = Math.min(ret[i], primes[j] * ret[indexes[j]]); - } - - for (int j = 0; j < indexes.length; j++) { - if (ret[i] == primes[j] * ret[indexes[j]]) { - indexes[j]++; - } - } - } - return ret[n - 1]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_314.java b/src/main/java/com/fishercoder/solutions/_314.java deleted file mode 100644 index e7c8e6ae79..0000000000 --- a/src/main/java/com/fishercoder/solutions/_314.java +++ /dev/null @@ -1,161 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; -import java.util.TreeMap; - -/** - * 314. Binary Tree Vertical Order Traversal - * - * Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). - - If two nodes are in the same row and column, the order should be from left to right. - - Examples: - - Given binary tree [3,9,20,null,null,15,7], - 3 - /\ - / \ - 9 20 - /\ - / \ - 15 7 - return its vertical order traversal as: - [ - [9], - [3,15], - [20], - [7] - ] - - Given binary tree [3,9,8,4,0,1,7], - 3 - /\ - / \ - 9 8 -/\ /\ -/ \/ \ -4 01 7 - return its vertical order traversal as: - [ - [4], - [9], - [3,0,1], - [8], - [7] - ] - - Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5), - 3 - /\ - / \ - 9 8 - /\ /\ - / \/ \ -4 01 7 - /\ - / \ - 5 2 - return its vertical order traversal as: - [ - [4], - [9,5], - [3,0,1], - [8,2], - [7] - ] - */ -public class _314 { - public static class Solution1 { - public List> verticalOrder(TreeNode root) { - List> result = new ArrayList(); - if (root == null) { - return result; - } - Queue bfsQ = new LinkedList(); - Queue indexQ = new LinkedList(); - TreeMap> map = new TreeMap(); - bfsQ.offer(root); - indexQ.offer( - 0);//we set the root as index 0, left will be negative, right will be positive - while (!bfsQ.isEmpty()) { - int qSize = bfsQ.size(); - for (int i = 0; i < qSize; i++) { - TreeNode curr = bfsQ.poll(); - int index = indexQ.poll(); - if (map.containsKey(index)) { - map.get(index).add(curr.val); - } else if (!map.containsKey(index)) { - List list = new ArrayList(); - list.add(curr.val); - map.put(index, list); - } - if (curr.left != null) { - bfsQ.offer(curr.left); - indexQ.offer(index - 1); - } - if (curr.right != null) { - bfsQ.offer(curr.right); - indexQ.offer(index + 1); - } - } - } - for (int i : map.keySet()) { - result.add(map.get(i)); - } - return result; - } - } - - public static class Solution2 { - public List> verticalOrder(TreeNode root) { - List> result = new ArrayList(); - if (root == null) { - return result; - } - Queue bfsQ = new LinkedList(); - Queue indexQ = new LinkedList(); - HashMap> map = new HashMap(); - bfsQ.offer(root); - indexQ.offer( - 0);//we set the root as index 0, left will be negative, right will be positive - int min = 0; - int max = 0; - while (!bfsQ.isEmpty()) { - int qSize = bfsQ.size(); - for (int i = 0; i < qSize; i++) { - TreeNode curr = bfsQ.poll(); - int index = indexQ.poll(); - if (map.containsKey(index)) { - map.get(index).add(curr.val); - } else if (!map.containsKey(index)) { - List list = new ArrayList(); - list.add(curr.val); - map.put(index, list); - } - if (curr.left != null) { - bfsQ.offer(curr.left); - indexQ.offer(index - 1); - min = Math.min(min, index - 1); - } - if (curr.right != null) { - bfsQ.offer(curr.right); - indexQ.offer(index + 1); - max = Math.max(max, index + 1); - } - } - } - for (int i = min; i <= max; i++) { - result.add(map.get(i)); - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_315.java b/src/main/java/com/fishercoder/solutions/_315.java deleted file mode 100644 index 3ba6dda355..0000000000 --- a/src/main/java/com/fishercoder/solutions/_315.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.List; - -/** - * 315. Count of Smaller Numbers After Self - * - * You are given an integer array nums and you have to return a new counts array. - * The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. - - Example: - - Given nums = [5, 2, 6, 1] - - To the right of 5 there are 2 smaller elements (2 and 1). - To the right of 2 there is only 1 smaller element (1). - To the right of 6 there is 1 smaller element (1). - To the right of 1 there is 0 smaller element. - Return the array [2, 1, 1, 0]. - - */ -public class _315 { - - public static class Solution1 { - class Node { - int val; - int sum; - int dup = 1; - Node left; - Node right; - - public Node(int v, int s) { - this.val = v; - this.sum = s; - } - } - - public List countSmaller(int[] nums) { - Integer[] ans = new Integer[nums.length]; - Node root = null; - for (int i = nums.length - 1; i >= 0; i--) { - root = insertNode(nums[i], root, i, 0, ans); - } - return Arrays.asList(ans); - } - - Node insertNode(int val, Node node, int i, int prevSum, Integer[] ans) { - if (node == null) { - node = new Node(val, 0); - ans[i] = prevSum; - } else if (val == node.val) { - node.dup += 1; - ans[i] = prevSum + node.sum; - } else if (val > node.val) { - node.right = insertNode(val, node.right, i, prevSum + node.sum + node.dup, ans); - } else { - node.sum += 1; - node.left = insertNode(val, node.left, i, prevSum, ans); - } - - return node; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_316.java b/src/main/java/com/fishercoder/solutions/_316.java deleted file mode 100644 index c07f1aabd6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_316.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Deque; - -/** - * 316. Remove Duplicate Letters - * - * Given a string which contains only lowercase letters, - * remove duplicate letters so that every letter appear once and only once. - * You must make sure your result is the smallest in lexicographical order among all possible results. - - Example: - Given "bcabc" - Return "abc" - - Given "cbacdcbc" - Return "acdb" - */ -public class _316 { - public static class Solution1 { - /** credit: https://discuss.leetcode.com/topic/32259/java-solution-using-stack-with-comments/2 */ - public String removeDuplicateLetters(String s) { - int[] res = new int[26]; //will contain number of occurences of character (i+'a') - boolean[] visited = - new boolean[26]; //will contain if character (i+'a') is present in current result Stack - char[] ch = s.toCharArray(); - for (char c : ch) { //count number of occurences of character - res[c - 'a']++; - } - Deque st = new ArrayDeque<>(); // answer stack - int index; - for (char c : ch) { - index = c - 'a'; - res[index]--; //decrement number of characters remaining in the string to be analysed - if (visited[index]) { - //if character is already present in stack, dont bother - continue; - } - //if current character is smaller than last character in stack which occurs later in the string again - //it can be removed and added later e.g stack = bc remaining string abc then a can pop b and then c - while (!st.isEmpty() && c < st.peek() && res[st.peek() - 'a'] != 0) { - visited[st.pop() - 'a'] = false; - } - st.push(c); //add current character and mark it as visited - visited[index] = true; - } - - StringBuilder sb = new StringBuilder(); - //pop character from stack and build answer string from back - while (!st.isEmpty()) { - sb.insert(0, st.pop()); - } - return sb.toString(); - } - } - - public static class Solution2 { - /** - * Credit: https://discuss.leetcode.com/topic/31404/a-short-o-n-recursive-greedy-solution - */ - public String removeDuplicateLetters(String s) { - int[] count = new int[26]; - int pos = 0; // the position for the smallest s[i] - for (int i = 0; i < s.length(); i++) { - count[s.charAt(i) - 'a']++; - } - for (int i = 0; i < s.length(); i++) { - if (s.charAt(i) < s.charAt(pos)) { - pos = i; - } - count[s.charAt(i) - 'a']--; - if (count[s.charAt(i) - 'a'] == 0) { - break; - } - } - return s.length() == 0 ? "" : s.charAt(pos) + removeDuplicateLetters( - s.substring(pos + 1).replaceAll("" + s.charAt(pos), "")); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_317.java b/src/main/java/com/fishercoder/solutions/_317.java deleted file mode 100644 index e072ca7eab..0000000000 --- a/src/main/java/com/fishercoder/solutions/_317.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 317. Shortest Distance from All Buildings - * - * You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. - * You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where: - - Each 0 marks an empty land which you can pass by freely. - Each 1 marks a building which you cannot pass through. - Each 2 marks an obstacle which you cannot pass through. - - For example, given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2): - - 1 - 0 - 2 - 0 - 1 - | | | | | - 0 - 0 - 0 - 0 - 0 - | | | | | - 0 - 0 - 1 - 0 - 0 - - The point (1,2) is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal. So return 7. - - Note: - There will be at least one building. If it is not possible to build such house according to the above rules, return -1. - */ - -public class _317 { - public static class Solution1 { - public int shortestDistance(int[][] grid) { - int m = grid.length; - if (m == 0) { - return -1; - } - int n = grid[0].length; - int[][] reach = new int[m][n]; - int[][] distance = new int[m][n]; - int[] shift = new int[] {0, 1, 0, -1, - 0};//how these five elements is ordered is important since it denotes the neighbor of the current node - int numBuilding = 0; - - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == 1) { - numBuilding++; - int dist = 1; - boolean[][] visited = new boolean[m][n]; - - Queue q = new LinkedList(); - q.offer(new int[] {i, j}); - while (!q.isEmpty()) { - int size = q.size(); - for (int l = 0; l < size; l++) { - int[] current = q.poll(); - for (int k = 0; k < 4; k++) { - int nextRow = current[0] + shift[k]; - int nextCol = current[1] + shift[k + 1]; - if (nextRow >= 0 - && nextRow < m - && nextCol >= 0 - && nextCol < n - && !visited[nextRow][nextCol] - && grid[nextRow][nextCol] == 0) { - distance[nextRow][nextCol] += dist; - visited[nextRow][nextCol] = true; - reach[nextRow][nextCol]++; - q.offer(new int[] {nextRow, nextCol}); - } - } - } - dist++; - } - } - } - } - - int result = Integer.MAX_VALUE; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == 0 && reach[i][j] == numBuilding && distance[i][j] < result) { - result = distance[i][j]; - } - } - } - return result == Integer.MAX_VALUE ? -1 : result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_318.java b/src/main/java/com/fishercoder/solutions/_318.java deleted file mode 100644 index c1eafa1f8b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_318.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -/**318. Maximum Product of Word Lengths - * -Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. - -Example 1: -Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"] -Return 16 -The two words can be "abcw", "xtfn". - -Example 2: -Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"] -Return 4 -The two words can be "ab", "cd". - -Example 3: -Given ["a", "aa", "aaa", "aaaa"] -Return 0 -No such pair of words.*/ -public class _318 { - public static class Solution1 { - //Inspired by this awesome post: https://discuss.leetcode.com/topic/35539/java-easy-version-to-understand - //Idea: this question states that all words consisted of lower case (total only 26 unique chars), - //this is a big hint that we could use integer (total 32 bits) to represent each char - //values[i] means how many unique characters this string words[i] has - public int maxProduct(String[] words) { - if (words == null || words.length == 0) { - return 0; - } - int len = words.length; - int[] values = new int[len]; - for (int i = 0; i < words.length; i++) { - String word = words[i]; - for (int j = 0; j < words[i].length(); j++) { - values[i] |= 1 << (word.charAt(j) - - 'a');//the reason for left shift by this number "word.charAt(j) -'a'" is for 'a', otherwise 'a' - 'a' will be zero and 'a' will be missed out. - } - } - int maxProduct = 0; - for (int i = 0; i < words.length; i++) { - for (int j = 0; j < words.length; j++) { - //check if values[i] AND values[j] equals to zero, this means they share NO common chars - if ((values[i] & values[j]) == 0 - && words[i].length() * words[j].length() > maxProduct) { - maxProduct = words[i].length() * words[j].length(); - } - } - } - return maxProduct; - } - } - - public static void main(String... strings) { - _318 test = new _318(); - String[] words = new String[]{"abcw", "baz", "foo", "bar", "xtfn", "abcdef"}; - - //The following is to understand what does left shift by 1 mean: - //the tricky part is to understand how it's written for me: - // "x << y" means left shift x by y bits - //left shift is equivalent to multiplication of powers of 2, so "4 << 1" equals to " 4 * 2^1" - //similarly, "4 << 3" equals to "4 * 2^3" which equals "4 * 8" - String sample = "f"; - int bits = 0; - int shiftLeftByHowMany = 0; - int shiftLeftResult = 0; - for (int j = 0; j < sample.length(); j++) { - shiftLeftByHowMany = sample.charAt(j) - 'a'; - shiftLeftResult = 1 << shiftLeftByHowMany; - bits |= 1 << (sample.charAt(j) - 'a'); - //this means shift left 1 by "sample.charAt(j) -'a'" bits - System.out.println("nonShiftLeft = " + shiftLeftByHowMany + "\tnonShiftLeft binary form is: " + Integer.toBinaryString(shiftLeftByHowMany) - + "\nshiftLeft = " + shiftLeftResult + "\tshiftLeft binary form is: " + Integer.toBinaryString(shiftLeftResult) - + "\nbits = " + bits + "\tbits binary form is: " + Integer.toBinaryString(bits)); - System.out.println(shiftLeftResult == (1 * Math.pow(2, shiftLeftByHowMany))); - } - - //similarly, right shift is written like this: "x >> y", means shift x by y bits - //4 >> 3 equals 4 * 2^3, see below: - System.out.println(4 * 8 == (4 * Math.pow(2, 3))); - } -} diff --git a/src/main/java/com/fishercoder/solutions/_319.java b/src/main/java/com/fishercoder/solutions/_319.java deleted file mode 100644 index fa0a92e3db..0000000000 --- a/src/main/java/com/fishercoder/solutions/_319.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 319. Bulb Switcher - * - * There are n bulbs that are initially off. You first turn on all the bulbs. - * Then, you turn off every second bulb. On the third round, - * you toggle every third bulb (turning on if it's off or turning off if it's on). - * For the ith round, you toggle every i bulb. For the nth round, you only - * toggle the last bulb. Find how many bulbs are on after n rounds. - - Example: - - Given n = 3. - - At first, the three bulbs are [off, off, off]. - After first round, the three bulbs are [on, on, on]. - After second round, the three bulbs are [on, off, on]. - After third round, the three bulbs are [on, off, off]. - - So you should return 1, because there is only one bulb is on. - */ -public class _319 { - - public static class Solution1 { - public int bulbSwitch(int n) { - if (n < 2) { - return n; - } - return (int) Math.sqrt(n); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_32.java b/src/main/java/com/fishercoder/solutions/_32.java deleted file mode 100644 index 367fcfac45..0000000000 --- a/src/main/java/com/fishercoder/solutions/_32.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -public class _32 { - public static class Solution1 { - public int longestValidParentheses(String s) { - int result = 0; - Stack stack = new Stack(); - stack.push(-1); - for (int i = 0; i < s.length(); i++) { - if (s.charAt(i) == '(') { - stack.push(i); - } else { - stack.pop(); - if (stack.isEmpty()) { - stack.push(i); - } else { - result = Math.max(result, i - stack.peek()); - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_320.java b/src/main/java/com/fishercoder/solutions/_320.java deleted file mode 100644 index 820dd94d02..0000000000 --- a/src/main/java/com/fishercoder/solutions/_320.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 320. Generalized Abbreviation - * - * Write a function to generate the generalized abbreviations of a word. - - Example: - - Given word = "word", return the following list (order does not matter): - ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"] - */ -public class _320 { - - public static class Solution1 { - public List generateAbbreviations(String word) { - List result = new ArrayList<>(); - backtrack(word, result, 0, "", 0); - return result; - } - - private void backtrack(String word, List result, int position, String current, - int count) { - if (position == word.length()) { - if (count > 0) { - current += count; - } - result.add(current); - } else { - backtrack(word, result, position + 1, current, count + 1); - backtrack(word, result, position + 1, - current + (count > 0 ? count : "") + word.charAt(position), 0); - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_321.java b/src/main/java/com/fishercoder/solutions/_321.java deleted file mode 100644 index a273e4fee4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_321.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 321. Create Maximum Number - * - * Given two arrays of length m and n with digits 0-9 representing two numbers. - * Create the maximum number of length k <= m + n from digits of the two. - * The relative order of the digits from the same array must be preserved. - * Return an array of the k digits. You should try to optimize your time and space complexity. - - Example 1: - nums1 = [3, 4, 6, 5] - nums2 = [9, 1, 2, 5, 8, 3] - k = 5 - return [9, 8, 6, 5, 3] - - Example 2: - nums1 = [6, 7] - nums2 = [6, 0, 4] - k = 5 - return [6, 7, 6, 0, 4] - - Example 3: - nums1 = [3, 9] - nums2 = [8, 9] - k = 3 - return [9, 8, 9] - */ -public class _321 { - public static class Solution1 { - public int[] maxNumber(int[] nums1, int[] nums2, int k) { - int n = nums1.length; - int m = nums2.length; - int[] ans = new int[k]; - for (int i = Math.max(0, k - m); i <= k && i <= n; ++i) { - //what is this and why? - int[] candidate = merge(maxArray(nums1, i), maxArray(nums2, k - i), k); - if (greater(candidate, 0, ans, 0)) { - ans = candidate; - } - } - return ans; - } - - private boolean greater(int[] nums1, int i, int[] nums2, int j) { - while (i < nums1.length && j < nums2.length && nums1[i] == nums2[j]) { - i++; - j++; - } - return j == nums2.length || (i < nums1.length && nums1[i] > nums2[j]); - } - - private int[] merge(int[] nums1, int[] nums2, int k) { - int[] ans = new int[k]; - for (int i = 0, j = 0, r = 0; r < k; r++) { - ans[r] = greater(nums1, i, nums2, j) ? nums1[i++] : nums2[j++]; - } - return ans; - } - - private int[] maxArray(int[] nums, int k) { - int n = nums.length; - int[] ans = new int[k]; - for (int i = 0, j = 0; i < n; i++) { - while (n - i + j > k && j > 0 && ans[j - 1] < nums[i]) { - j--; - } - if (j < k) { - ans[j++] = nums[i]; - } - } - return ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_322.java b/src/main/java/com/fishercoder/solutions/_322.java deleted file mode 100644 index 81a158f027..0000000000 --- a/src/main/java/com/fishercoder/solutions/_322.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 322. Coin Change - * - * You are given coins of different denominations and a total amount of money amount. - * Write a function to compute the fewest number of coins that you need to make up that amount. - * If that amount of money cannot be made up by any combination of the coins, return -1. - - Example 1: - coins = [1, 2, 5], amount = 11 - return 3 (11 = 5 + 5 + 1) - - Example 2: - coins = [2], amount = 3 - return -1. - - Note: - You may assume that you have an infinite number of each kind of coin. - */ -public class _322 { - - public static class Solution1 { - public int coinChange(int[] coins, int amount) { - if (amount < 1) { - return 0; - } - int[] count = new int[amount]; - int result = helper(coins, amount, count); - return result; - } - - //remaining means the remaining coins after the last step; - //count[remaining] means the minimum number of coins to sum up to remaining - private int helper(int[] coins, int remaining, int[] count) { - if (remaining < 0) { - return -1;//not valid case, thus, per problem description, we should return -1 - } - if (remaining == 0) { - return 0;//completed, this is also a base case for this recursive function - } - if (count[remaining - 1] != 0) { - return count[remaining - 1];//already computed, so reuse it. - } - int min = Integer.MAX_VALUE; - for (int coin : coins) { - int res = helper(coins, remaining - coin, count); - if (res >= 0 && res < min) { - min = 1 + res; - } - } - return count[remaining - 1] = (min == Integer.MAX_VALUE) ? -1 : min; - } - } - - public static class Solution2 { - //dp solution - public int coinChange(int[] coins, int amount) { - int max = amount + 1; - int[] dp = new int[max]; - Arrays.fill(dp, max);// initial the dp array with amount + 1 which is not valid case. - dp[0] = 0;//initial amount 0 = 0; - for (int i = 1; i <= amount; i++) { - for (int j = 0; j < coins.length; j++) { - if (coins[j] <= i) { - //the dp[coins[j]] will ba a valid case, then if dp[i - coins[j]] is valid - dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); - // then we update dp[i], otherwise dp[i] = max; - } - } - } - return dp[amount] > amount ? -1 : dp[amount]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_323.java b/src/main/java/com/fishercoder/solutions/_323.java deleted file mode 100644 index 10bbb39f5f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_323.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -/** - * 323. Number of Connected Components in an Undirected Graph - * - * Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph. - - Example 1: - - 0 3 - | | - 1 --- 2 4 - - Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2. - - Example 2: - - 0 4 - | | - 1 --- 2 --- 3 - - Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 1. - - Note: - You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges. - */ -public class _323 { - public static class Solution1 { - - public int countComponents(int n, int[][] edges) { - if (n <= 1) { - return n; - } - - List> adList = new ArrayList<>(); - for (int i = 0; i < n; i++) { - adList.add(new ArrayList<>()); - } - - for (int[] edge : edges) { - adList.get(edge[0]).add(edge[1]); - adList.get(edge[1]).add(edge[0]); - } - - for (List list : adList) { - for (int i : list) { - System.out.print(i + ", "); - } - System.out.println(); - } - - boolean[] visited = new boolean[n]; - int count = 0; - for (int i = 0; i < n; i++) { - if (!visited[i]) { - count++; - Queue q = new LinkedList<>(); - q.offer(i); - while (!q.isEmpty()) { - int index = q.poll(); - visited[index] = true; - for (int j : adList.get(index)) { - if (!visited[j]) { - q.offer(j); - } - } - } - } - } - - return count; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_324.java b/src/main/java/com/fishercoder/solutions/_324.java deleted file mode 100644 index 9451279805..0000000000 --- a/src/main/java/com/fishercoder/solutions/_324.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Collections; -import java.util.PriorityQueue; - -/** - * 324. Wiggle Sort II - * - * Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... - - Example: - (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. - (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2]. - - Note: - You may assume all input has valid answer. - - Follow Up: - Can you do it in O(n) time and/or in-place with O(1) extra space? - */ -public class _324 { - - public static class Solution1 { - /** Credit: https://discuss.leetcode.com/topic/41464/step-by-step-explanation-of-index-mapping-in-java */ - public void wiggleSort(int[] nums) { - int median = findKthLargest(nums, (nums.length + 1) / 2); - int n = nums.length; - - int left = 0; - int i = 0; - int right = n - 1; - - while (i <= right) { - - if (nums[newIndex(i, n)] > median) { - swap(nums, newIndex(left++, n), newIndex(i++, n)); - } else if (nums[newIndex(i, n)] < median) { - swap(nums, newIndex(right--, n), newIndex(i, n)); - } else { - i++; - } - } - } - - private int findKthLargest(int[] nums, int k) { - PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); - for (int i : nums) { - maxHeap.offer(i); - } - while (k-- > 1) { - maxHeap.poll(); - } - return maxHeap.poll(); - } - - private void swap(int[] nums, int i, int j) { - int tmp = nums[i]; - nums[i] = nums[j]; - nums[j] = tmp; - } - - private int newIndex(int index, int n) { - return (1 + 2 * index) % (n | 1); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_325.java b/src/main/java/com/fishercoder/solutions/_325.java deleted file mode 100644 index d22f8a112a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_325.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 325. Maximum Size Subarray Sum Equals k - * - * Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. - * - * Note: - * The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range. - * - * Example 1: - * Given nums = [1, -1, 5, -2, 3], k = 3, - * return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest) - * - * Example 2: - * Given nums = [-2, -1, 2, 1], k = 1, - * return 2. (because the subarray [-1, 2] sums to 1 and is the longest) - * - * Follow Up: - * Can you do it in O(n) time?*/ - -public class _325 { - - public static class Solution1 { - public int maxSubArrayLen(int[] nums, int k) { - Map map = new HashMap(); - int sum = 0; - int max = 0; - for (int i = 0; i < nums.length; i++) { - sum += nums[i]; - if (sum == k) { - max = i + 1; - } else if (map.containsKey(sum - k)) { - max = Math.max(max, i - map.get(sum - k)); - } - if (!map.containsKey(sum)) { - map.put(sum, i); - } - } - return max; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_326.java b/src/main/java/com/fishercoder/solutions/_326.java deleted file mode 100644 index b0d05fbf36..0000000000 --- a/src/main/java/com/fishercoder/solutions/_326.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 326. Power of Three - * - * Given an integer, write a function to determine if it is a power of three. - * - * Follow up: - * Could you do it without using any loop / recursion? -*/ - -public class _326 { - public static class Solution1 { - //regular method that has a loop - public boolean isPowerOfThree(int n) { - if (n < 3 && n != 1) { - return false; - } - while (n != 1) { - if (n % 3 != 0) { - return false; - } - n /= 3; - } - return true; - } - } - - public static class Solution2 { - //find the max possible integer that is a power of 3, then do modulor with this number - public boolean isPowerOfThree(int n) { - return (n > 0 && 1162261467 % n == 0); - } - } - - public static class Solution3 { - //Editorial solution: it's pretty elegant to use base conversion which can be easily extended to any radix k - //Idea: for a number in base 10, if it's power of 10, then it must be in this format: 10, 100, 1000... with a leading one and all trailing zeros - //similarly, if a number is power of 3, then in its base 3 format, it must be in this format as well: 10, 100, 1000, 1000... - //some Java built-in function could help us along the way: - public boolean isPowerOfThree(int n) { - return Integer.toString(n, 3).matches("^10*$"); - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_328.java b/src/main/java/com/fishercoder/solutions/_328.java deleted file mode 100644 index 0e02971b5a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_328.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 328. Odd Even Linked List - * - * Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. - - You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity. - - Example: - Given 1->2->3->4->5->NULL, - return 1->3->5->2->4->NULL. - - Note: - The relative order inside both the even and odd groups should remain as it was in the input. - The first node is considered odd, the second node even and so on ... - */ -public class _328 { - - public static class Solution1 { - public ListNode oddEvenList(ListNode head) { - if (head != null) { - ListNode odd = head; - ListNode even = head.next; - ListNode evenHead = even; - - while (even != null && even.next != null) { - odd.next = odd.next.next; - even.next = even.next.next; - odd = odd.next; - even = even.next; - } - - odd.next = evenHead; - } - return head; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_329.java b/src/main/java/com/fishercoder/solutions/_329.java deleted file mode 100644 index 62183af322..0000000000 --- a/src/main/java/com/fishercoder/solutions/_329.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 329. Longest Increasing Path in a Matrix - * - * Given an integer matrix, find the length of the longest increasing path. - * From each cell, you can either move to four directions: left, right, up or down. - * You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed). - - Example 1: - - nums = [ - [9,9,4], - [6,6,8], - [2,1,1] - ] - Return 4 - The longest increasing path is [1, 2, 6, 9]. - - Example 2: - - nums = [ - [3,4,5], - [3,2,6], - [2,2,1] - ] - Return 4 - The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed. - */ - -public class _329 { - - public static class Solution1 { - final int[] dirs = new int[]{0, 1, 0, -1, 0}; - - public int longestIncreasingPath(int[][] matrix) { - if (matrix == null || matrix.length == 0) { - return 0; - } - int max = 0; - int[][] cache = new int[matrix.length][matrix[0].length]; - for (int i = 0; i < matrix.length; i++) { - for (int j = 0; j < matrix[0].length; j++) { - int len = dfs(matrix, i, j, cache); - max = Math.max(len, max); - } - } - return max; - } - - int dfs(int[][] matrix, int row, int col, int[][] cache) { - if (cache[row][col] != 0) { - return cache[row][col]; - } - int max = 1; - for (int i = 0; i < 4; i++) { - int nextRow = row + dirs[i]; - int nextCol = col + dirs[i + 1]; - if (nextRow < 0 || nextRow >= matrix.length || nextCol < 0 || nextCol >= matrix[0].length || matrix[nextRow][nextCol] <= matrix[row][col]) { - continue; - } - int len = 1 + dfs(matrix, nextRow, nextCol, cache); - max = Math.max(max, len); - } - cache[row][col] = max; - return max; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_33.java b/src/main/java/com/fishercoder/solutions/_33.java deleted file mode 100644 index eae4bae5a0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_33.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.fishercoder.solutions; - -public class _33 { - - public static class Solution1 { - - public int search(int[] nums, int target) { - if (nums == null || nums.length == 0) { - return -1; - } - int minIdx = findMinIdx(nums); - if (target == nums[minIdx]) { - return minIdx; - } - int m = nums.length; - int start = (target <= nums[m - 1]) ? minIdx : 0; - int end = (target > nums[m - 1]) ? minIdx : m - 1; - - while (start <= end) { - int mid = start + (end - start) / 2; - if (nums[mid] == target) { - return mid; - } else if (target > nums[mid]) { - start = mid + 1; - } else { - end = mid - 1; - } - } - return -1; - } - - private int findMinIdx(int[] nums) { - int start = 0; - int end = nums.length - 1; - - while (start < end) { - int mid = start + (end - start) / 2; - if (nums[mid] > nums[end]) { - start = mid + 1; - } else { - end = mid; - } - } - return start; - } - } - - public static class Solution2 { - public int search(int[] nums, int target) { - if (nums == null || nums.length == 0) { - return -1; - } - int lo = 0; - int hi = nums.length - 1; - while (lo < hi) { - int mid = (lo + hi) / 2; - if (nums[mid] == target) { - return mid; - } - - if (nums[lo] <= nums[mid]) { - if (target >= nums[lo] && target < nums[mid]) { - hi = mid - 1; - } else { - lo = mid + 1; - } - } else { - if (target > nums[mid] && target <= nums[hi]) { - lo = mid + 1; - } else { - hi = mid - 1; - } - } - } - return nums[lo] == target ? lo : -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_330.java b/src/main/java/com/fishercoder/solutions/_330.java deleted file mode 100644 index 98d224de0f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_330.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 330. Patching Array - * - * Given a sorted positive integer array nums and an integer n, - * add/patch elements to the array such that any number in range [1, n] - * inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required. - - Example 1: - nums = [1, 3], n = 6 - Return 1. - - Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4. - Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3]. - Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6]. - So we only need 1 patch. - - Example 2: - nums = [1, 5, 10], n = 20 - Return 2. - The two patches can be [2, 4]. - - Example 3: - nums = [1, 2, 2], n = 5 - Return 0. - */ -public class _330 { - - public static class Solution1 { - /** - * credit: https://leetcode.com/articles/patching-array/ and https://discuss.leetcode.com/topic/35494/solution-explanation/2 - * - * Let miss be the smallest sum in [0,n] that we might be missing. Meaning we already know we - * can build all sums in [0,miss). Then if we have a number num <= miss in the given array, we - * can add it to those smaller sums to build all sums in [0,miss+num). If we don't, then we must - * add such a number to the array, and it's best to add miss itself, to maximize the reach. - * - * Example: Let's say the input is nums = [1, 2, 4, 13, 43] and n = 100. We need to ensure that - * all sums in the range [1,100] are possible. Using the given numbers 1, 2 and 4, we can - * already build all sums from 0 to 7, i.e., the range [0,8). But we can't build the sum 8, and - * the next given number (13) is too large. So we insert 8 into the array. Then we can build all - * sums in [0,16). Do we need to insert 16 into the array? No! We can already build the sum 3, - * and adding the given 13 gives us sum 16. We can also add the 13 to the other sums, extending - * our range to [0,29). And so on. The given 43 is too large to help with sum 29, so we must - * insert 29 into our array. This extends our range to [0,58). But then the 43 becomes useful - * and expands our range to [0,101). At which point we're done. - */ - - public int minPatches(int[] nums, int n) { - long misses = 1;//use long to avoid integer addition overflow - int patches = 0; - int i = 0; - while (misses <= n) { - if (i < nums.length && nums[i] <= misses) { //miss is covered - misses += nums[i++]; - } else { //patch miss to the array - misses += misses; - patches++;//increase the answer - } - } - return patches; - } - - public List findPatches(int[] nums, int n) { - long misses = 1;//use long to avoid integer addition overflow - List patches = new ArrayList<>(); - int i = 0; - while (misses <= n) { - if (i < nums.length && nums[i] <= misses) { //miss is covered - misses += nums[i++]; - } else { //patch miss to the array - patches.add((int) misses);//increase the answer - misses += misses; - } - } - return patches; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_331.java b/src/main/java/com/fishercoder/solutions/_331.java deleted file mode 100644 index 8eaf500460..0000000000 --- a/src/main/java/com/fishercoder/solutions/_331.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Deque; - -/** - * 331. Verify Preorder Serialization of a Binary Tree - * - * One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. - - _9_ - / \ - 3 2 - / \ / \ - 4 1 # 6 - / \ / \ / \ - # # # # # # - - For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. - - Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. - - Each comma separated value in the string must be either an integer or a character '#' representing null pointer. - - You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". - - Example 1: - "9,3,4,#,#,1,#,#,2,#,6,#,#" - Return true - - Example 2: - "1,#" - Return false - - Example 3: - "9,#,#,1" - Return false - - - */ -public class _331 { - - public static class Solution1 { - public boolean isValidSerialization(String preorder) { - /**Idea: we keep inserting the string into the stack, if it's a number, we just push it onto the stack; - * if it's a "#", we see if the top of the stack is a "#" or not, - * 1. if it's a "#", we pop it and keep popping numbers from the stack, - * 2. if it's not a "#", we push the "#" onto the stack*/ - if (preorder == null || preorder.length() == 0) { - return false; - } - String[] pre = preorder.split(","); - Deque stack = new ArrayDeque<>(); - for (int i = 0; i < pre.length; i++) { - while (pre[i].equals("#") && !stack.isEmpty() && stack.peekLast().equals("#")) { - stack.pollLast(); - if (stack.isEmpty()) { - return false; - } - stack.pollLast(); - } - stack.addLast(pre[i]); - } - return stack.size() == 1 && stack.peekLast().equals("#"); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_332.java b/src/main/java/com/fishercoder/solutions/_332.java deleted file mode 100644 index da0df9d8b5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_332.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.PriorityQueue; - -/** - * 332. Reconstruct Itinerary - * - * Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], - * reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK. - - Note: - If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. - For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"]. - - All airports are represented by three capital letters (IATA code). - - You may assume all tickets form at least one valid itinerary. - Example 1: - tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]] - Return ["JFK", "MUC", "LHR", "SFO", "SJC"]. - - Example 2: - tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]] - Return ["JFK","ATL","JFK","SFO","ATL","SFO"]. - Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order. - */ -public class _332 { - - public static class Solution1 { - /** credit: https://discuss.leetcode.com/topic/36383/share-my-solution */ - public List findItinerary(String[][] tickets) { - Map> flights = new HashMap<>(); - LinkedList path = new LinkedList<>(); - for (String[] ticket : tickets) { - flights.putIfAbsent(ticket[0], new PriorityQueue<>()); - flights.get(ticket[0]).add(ticket[1]); - } - dfs("JFK", flights, path); - return path; - } - - public void dfs(String departure, Map> flights, - LinkedList path) { - PriorityQueue arrivals = flights.get(departure); - while (arrivals != null && !arrivals.isEmpty()) { - dfs(arrivals.poll(), flights, path); - } - path.addFirst(departure); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_333.java b/src/main/java/com/fishercoder/solutions/_333.java deleted file mode 100644 index 6f41715b81..0000000000 --- a/src/main/java/com/fishercoder/solutions/_333.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/**333. Largest BST Subtree - * - * Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), - * where largest means subtree with largest number of nodes in it. - - Note: - A subtree must include all of its descendants. - Here's an example: - 10 - / \ - 5 15 - / \ \ - 1 8 7 - - The Largest BST Subtree in this case is the highlighted one (5,1,8). - The return value is the subtree's size, which is 3. - - Follow up: - Can you figure out ways to solve it with O(n) time complexity? - */ - -public class _333 { - public static class Solution1 { - /**credit: https://discuss.leetcode.com/topic/36995/share-my-o-n-java-code-with-brief-explanation-and-comments*/ - class Result { // (size, rangeLower, rangeUpper) -- size of current tree, range of current tree [rangeLower, rangeUpper] - int size; - int lower; - int upper; - - Result(int size, int lower, int upper) { - this.size = size; - this.lower = lower; - this.upper = upper; - } - } - - int max = 0; - - public int largestBSTSubtree(TreeNode root) { - if (root == null) { - return 0; - } - traverse(root); - return max; - } - - private Result traverse(TreeNode root) { - if (root == null) { - return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE); - } - Result left = traverse(root.left); - Result right = traverse(root.right); - if (left.size == -1 || right.size == -1 || root.val <= left.upper || root.val >= right.lower) { - return new Result(-1, 0, 0); - } - int size = left.size + 1 + right.size; - max = Math.max(size, max); - return new Result(size, Math.min(left.lower, root.val), Math.max(right.upper, root.val)); - } - } - - public int largestBSTSubtree(TreeNode root) { - if (root == null) { - return 0; - } - if (isBST(root)) { - return getNodes(root); - } - return Math.max(find(root.left), find(root.right)); - } - - int find(TreeNode root) { - if (isBST(root)) { - return getNodes(root); - } - return Math.max(find(root.left), find(root.right)); - } - - int getNodes(TreeNode root) { - if (root == null) { - return 0; - } - return dfsCount(root); - } - - int dfsCount(TreeNode root) { - if (root == null) { - return 0; - } - return dfsCount(root.left) + dfsCount(root.right) + 1; - } - - boolean isBST(TreeNode root) { - if (root == null) { - return true; - } - return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE); - } - - boolean dfs(TreeNode root, long min, long max) { - if (root == null) { - return true; - } - if (root.val <= min || root.val >= max) { - return false; - } - return dfs(root.left, min, root.val) && dfs(root.right, root.val, max); - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_334.java b/src/main/java/com/fishercoder/solutions/_334.java deleted file mode 100644 index f4a2c166b1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_334.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 334. Increasing Triplet Subsequence - * - * Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. - * - * Formally the function should: - * Return true if there exists i, j, k - * such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false. - * Your algorithm should run in O(n) time complexity and O(1) space complexity. - - Examples: - Given [1, 2, 3, 4, 5], - return true. - - Given [5, 4, 3, 2, 1], - return false. - */ -public class _334 { - - public static class Solution1 { - /**Time: O(n^2) - * Space: O(1)*/ - public boolean increasingTriplet(int[] nums) { - if (nums == null || nums.length == 0) { - return false; - } - int small = nums[0]; - int medium = Integer.MAX_VALUE; - for (int i = 1; i < nums.length; i++) { - small = Math.min(small, nums[i - 1]); - if (nums[i] > small) { - medium = Math.min(medium, nums[i]); - for (int j = i + 1; j < nums.length; j++) { - if (nums[j] > nums[i] || nums[j] > medium) { - return true; - } - } - } - } - return false; - } - } - - public static class Solution2 { - /** - * Time: O(n) - * Space: O(1) - */ - public boolean increasingTriplet(int[] nums) { - int small = Integer.MAX_VALUE; - int big = Integer.MAX_VALUE; - for (int num : nums) { - if (num <= small) { - small = num; - } else if (num <= big) { - big = num; - } else { - return true; - } - } - return false; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_335.java b/src/main/java/com/fishercoder/solutions/_335.java deleted file mode 100644 index b40ff4c6ec..0000000000 --- a/src/main/java/com/fishercoder/solutions/_335.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 335. Self Crossing - * - * You are given an array x of n positive numbers. - * You start at point (0,0) and moves x[0] metres to the north, - * then x[1] metres to the west, - * x[2] metres to the south, - * x[3] metres to the east and so on. - * In other words, after each move your direction changes counter-clockwise. - - Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not. - - Example 1: - Given x = - [2, 1, 1, 2] - , - ????? - ? ? - ???????> - ? - - Return true (self crossing) - Example 2: - Given x = - [1, 2, 3, 4] - , - ???????? - ? ? - ? - ? - ?????????????> - - Return false (not self crossing) - Example 3: - Given x = - [1, 1, 1, 1] - , - ????? - ? ? - ?????> - - Return true (self crossing) - - */ -public class _335 { - public static class Solution1 { - /** reference: https://discuss.leetcode.com/topic/38014/java-oms-with-explanation/2 */ - public boolean isSelfCrossing(int[] x) { - int l = x.length; - if (l <= 3) { - return false; - } - - for (int i = 3; i < l; i++) { - if (x[i] >= x[i - 2] && x[i - 1] <= x[i - 3]) { - return true; //Fourth line crosses first line and onward - } - if (i >= 4) { - if (x[i - 1] == x[i - 3] && x[i] + x[i - 4] >= x[i - 2]) { - return true; // Fifth line meets first line and onward - } - } - if (i >= 5) { - if (x[i - 2] - x[i - 4] >= 0 - && x[i] >= x[i - 2] - x[i - 4] - && x[i - 1] >= x[i - 3] - x[i - 5] - && x[i - 1] <= x[i - 3]) { - return true; // Sixth line crosses first line and onward - } - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_336.java b/src/main/java/com/fishercoder/solutions/_336.java deleted file mode 100644 index 0868cc8382..0000000000 --- a/src/main/java/com/fishercoder/solutions/_336.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/**336. Palindrome Pairs - * - * Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. - - Example 1: - Given words = ["bat", "tab", "cat"] - Return [[0, 1], [1, 0]] - The palindromes are ["battab", "tabbat"] - Example 2: - Given words = ["abcd", "dcba", "lls", "s", "sssll"] - Return [[0, 1], [1, 0], [3, 2], [2, 4]] - The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"] - */ -public class _336 { - - public static class Solution1 { - public List> palindromePairs(String[] words) { - List> pairs = new ArrayList(); - Map map = new HashMap(); - for (int i = 0; i < words.length; i++) { - map.put(words[i], i); - } - - for (int i = 0; i < words.length; i++) { - int l = 0; - int r = 0; - while (l <= r) { - String s = words[i].substring(l, r); - Integer j = map.get(new StringBuilder(s).reverse().toString()); - if (j != null && j != i && isPalindrome( - words[i].substring(l == 0 ? r : 0, l == 0 ? words[i].length() : l))) { - pairs.add( - Arrays.asList(l == 0 ? new Integer[] {i, j} : new Integer[] {j, i})); - } - if (r < words[i].length()) { - r++; - } else { - l++; - } - } - } - return pairs; - } - - private boolean isPalindrome(String s) { - for (int i = 0; i < s.length() / 2; i++) { - if (s.charAt(i) != s.charAt(s.length() - 1 - i)) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_337.java b/src/main/java/com/fishercoder/solutions/_337.java deleted file mode 100644 index 65602b6d75..0000000000 --- a/src/main/java/com/fishercoder/solutions/_337.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.HashMap; -import java.util.Map; - -/**337. House Robber III - - The thief has found himself a new place for his thievery again. - There is only one entrance to this area, called the "root." - Besides the root, each house has one and only one parent house. - After a tour, the smart thief realized that "all houses in this place forms a binary tree". - It will automatically contact the police if two directly-linked houses were broken into on the same night. - -Determine the maximum amount of money the thief can rob tonight without alerting the police. - -Example 1: - 3 - / \ - 2 3 - \ \ - 3 1 -Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. - -Example 2: - 3 - / \ - 4 5 - / \ \ - 1 3 1 -Maximum amount of money the thief can rob = 4 + 5 = 9. -*/ - -public class _337 { - - public static class Solution1 { - //simple recursion without cacheing: 1189 ms - public int rob(TreeNode root) { - if (root == null) { - return 0; - } - - int val = 0; - if (root.left != null) { - val += rob(root.left.left) + rob(root.left.right); - } - if (root.right != null) { - val += rob(root.right.left) + rob(root.right.right); - } - val = Math.max(val + root.val, rob(root.left) + rob(root.right)); - return val; - } - } - - public static class Solution2 { - //same idea, but with cacheing via a hashmap: 8 ms - public int rob_dp(TreeNode root) { - Map map = new HashMap<>(); - return getMaxValue(root, map); - } - - private int getMaxValue(TreeNode root, Map map) { - if (root == null) { - return 0; - } - if (map.containsKey(root)) { - return map.get(root); - } - - int val = 0; - if (root.left != null) { - val += getMaxValue(root.left.left, map) + getMaxValue(root.left.right, map); - } - if (root.right != null) { - val += getMaxValue(root.right.left, map) + getMaxValue(root.right.right, map); - } - int max = Math.max(root.val + val, - getMaxValue(root.left, map) + getMaxValue(root.right, map)); - map.put(root, max); - return max; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_338.java b/src/main/java/com/fishercoder/solutions/_338.java deleted file mode 100644 index 713d09a65d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_338.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -/**338. Counting Bits -Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. - -Example: -For num = 5 you should return [0,1,1,2,1,2]. - -Follow up: - -It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass? -Space complexity should be O(n). -Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language. - -Hint: - -You should make use of what you have produced already. -Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous. -Or does the odd/even status of the number help you in calculating the number of 1s? - -* -* -*/ -public class _338 { - public static class Solution1 { - //use the most regular method to get it AC'ed first - public int[] countBits(int num) { - int[] ones = new int[num + 1]; - for (int i = 0; i <= num; i++) { - ones[i] = countOnes(i); - } - return ones; - } - - private int countOnes(int i) { - int ones = 0; - while (i != 0) { - ones++; - i &= (i - 1); - } - return ones; - } - } - - private class Solution2 { - /**lixx2100's post is cool:https://discuss.leetcode.com/topic/40162/three-line-java-solution - An easy recurrence for this problem is f[i] = f[i / 2] + i % 2 - and then we'll use bit manipulation to express the above recursion function - right shift by 1 means to divide by 2 - AND with 1 means to modulo 2 - this is so cool!*/ - public int[] countBits(int num) { - int[] ones = new int[num + 1]; - for (int i = 1; i <= num; i++) { - ones[i] = ones[i >> 1] + (i & 1); - } - return ones; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_339.java b/src/main/java/com/fishercoder/solutions/_339.java deleted file mode 100644 index 12946069f1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_339.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.NestedInteger; - -import java.util.List; - -/** - * 339. Nested List Weight Sum - * - * Given a nested list of integers, return the sum of all integers in the list weighted by their depth. - * Each element is either an integer, or a list -- whose elements may also be integers or other lists. - * - * Example 1: - * Input: [[1,1],2,[1,1]] - * Output: 10 - * Explanation: Four 1's at depth 2, one 2 at depth 1. - * - * Example 2: - * Input: [1,[4,[6]]] - * Output: 27 - * Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27. - * */ -public class _339 { - public static class Solution1 { - private int sum = 0; - - public int depthSum(List nestedList) { - return dfs(nestedList, 1); - } - - private int dfs(List nestedList, int depth) { - for (NestedInteger ni : nestedList) { - if (ni.isInteger()) { - sum += depth * ni.getInteger(); - } else { - dfs(ni.getList(), depth + 1); - } - } - return sum; - } - } - - public static class Solution2 { - public int depthSum(List nestedList) { - return dfs(nestedList, 1); - } - - private int dfs(List nestedList, int depth) { - int sum = 0; - for (NestedInteger ni : nestedList) { - sum += ni.isInteger() ? depth * ni.getInteger() : dfs(ni.getList(), depth + 1); - } - return sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_34.java b/src/main/java/com/fishercoder/solutions/_34.java deleted file mode 100644 index 9e369871ae..0000000000 --- a/src/main/java/com/fishercoder/solutions/_34.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -public class _34 { - - public int[] searchRange(int[] nums, int target) { - int[] range = new int[2]; - range[0] = -1; - range[1] = -1; - if (nums == null || nums.length == 0) { - return range; - } - int start = 0; - int end = nums.length - 1; - while (start + 1 < end) { - int mid = start + (end - start) / 2; - if (nums[mid] == target) { - int left = mid; - while (left - 1 >= 0 && nums[left] == nums[left - 1]) { - left--; - } - range[0] = left; - int right = mid; - while (right + 1 < nums.length && nums[right] == nums[right + 1]) { - right++; - } - range[1] = right; - break; - } else if (nums[mid] < target) { - start = mid; - } else { - end = mid; - } - } - - if (nums[start] == target) { - range[0] = start; - while (start + 1 < nums.length && nums[start] == nums[start + 1]) { - start++; - } - range[1] = start; - } else if (nums[end] == target) { - range[1] = end; - while (end - 1 >= 0 && nums[end] == nums[end - 1]) { - end--; - } - range[0] = end; - } - return range; - } -} diff --git a/src/main/java/com/fishercoder/solutions/_340.java b/src/main/java/com/fishercoder/solutions/_340.java deleted file mode 100644 index 06d8c71fb4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_340.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 340. Longest Substring with At Most K Distinct Characters - * - * Given a string, find the length of the longest substring T that contains at most k distinct characters. - - For example, Given s = “eceba” and k = 2, - - T is "ece" which its length is 3. - - */ -public class _340 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/41671/15-lines-java-solution-using-slide-window - */ - public int lengthOfLongestSubstringKDistinct(String s, int k) { - int[] count = new int[256]; - int left = 0; - int result = 0; - int num = 0; - for (int right = 0; right < s.length(); right++) { - if (count[s.charAt(right)]++ == 0) { - num++; - } - if (num > k) { - while (--count[s.charAt(left++)] > 0) {}; - num--; - } - result = Math.max(result, right - left + 1); - } - return result; - } - } - - public static class Solution2 { - /**This is a more generic solution for any characters, not limited to ASCII characters.*/ - public int lengthOfLongestSubstringKDistinct(String s, int k) { - Map map = new HashMap<>(); - int longest = 0; - int left = 0; - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - map.put(c, map.getOrDefault(c, 0) + 1); - while (map.size() > k) { - char leftChar = s.charAt(left); - if (map.containsKey(leftChar)) { - map.put(leftChar, map.get(leftChar) - 1); - if (map.get(leftChar) == 0) { - map.remove(leftChar); - } - } - left++; - } - longest = Math.max(longest, i - left + 1); - } - return longest; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_341.java b/src/main/java/com/fishercoder/solutions/_341.java deleted file mode 100644 index 0a49e6e738..0000000000 --- a/src/main/java/com/fishercoder/solutions/_341.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.NestedInteger; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -/** - * 341. Flatten Nested List Iterator - * - * Given a nested list of integers, implement an iterator to flatten it. - * Each element is either an integer, or a list -- whose elements may also be integers or other lists. - * - * Example 1: - * Given the list [[1,1],2,[1,1]], - * By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]. - * - * Example 2: - * Given the list [1,[4,[6]]], - * By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]. - */ -public class _341 { - - public static class Solution1 { - public static class NestedIterator implements Iterator { - - private Queue flattenedList; - - public NestedIterator(List nestedList) { - flattenedList = new LinkedList<>(); - constructList(nestedList); - } - - private void constructList(List nestedList) { - for (NestedInteger nestedInteger : nestedList) { - if (nestedInteger.isInteger()) { - flattenedList.add(nestedInteger.getInteger()); - } else { - constructList(nestedInteger.getList()); - } - } - } - - @Override - public Integer next() { - return flattenedList.poll(); - } - - @Override - public boolean hasNext() { - return !flattenedList.isEmpty(); - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_342.java b/src/main/java/com/fishercoder/solutions/_342.java deleted file mode 100644 index db2aa98658..0000000000 --- a/src/main/java/com/fishercoder/solutions/_342.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 342. Power of Four - * - * Given an integer (signed 32 bits), write a function to check whether it is a power of 4. - * - * Example 1: - * Input: 16 - * Output: true - * - * Example 2: - * Input: 5 - * Output: false - * - * Follow up: Could you solve it without loops/recursion? - * */ -public class _342 { - public static class Solution1 { - //Just dive a little bit deeper, you can realize that another important feature of a number - //that is power of four is that its only single one bit must appear on the odd position, and power of two won't meet this requirement - //decimal number 8 has binary format: 0000-0000-0000-0000-0000-0000-0000-1000 - //decimal number 16 has binary format: 0000-0000-0000-0000-0000-0000-0001-0000 - //hex number 0x55555555 has binary format: 1010-1010-1010-1010-1010-1010-1010-1010 - //thus, doing AND with 0x55555 will check if the only one bit is located on the odd position, thus ruling out those that are power of 2 but not power of 4 - public boolean isPowerOfFour(int num) { - return (num > 0 && 1073741824 % num == 0 && (num & 0x55555555) != 0); - } - } - - public static class Solution2 { - public boolean isPowerOfFour(int num) { - //^ means to match the beginning of a line - //$ means to match the end of a line - //* means zero or more of the preceding character - return Integer.toString(num, 4).matches("^10*$"); - } - } - - public static class Solution3 { - //a regular loop method to make it AC'ed - public boolean isPowerOfFour(int num) { - if (num < 4 && num != 1) { - return false; - } - while (num != 1) { - if (num % 4 != 0) { - return false; - } - num /= 4; - } - return true; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_343.java b/src/main/java/com/fishercoder/solutions/_343.java deleted file mode 100644 index f417bb0576..0000000000 --- a/src/main/java/com/fishercoder/solutions/_343.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 343. Integer Break - * - * Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. - - For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). - - Note: You may assume that n is not less than 2 and not larger than 58. - - Hint: - - There is a simple O(n) solution to this problem. - You may check the breaking results of n ranging from 7 to 10 to discover the regularities. - */ -public class _343 { - public static class Solution1 { - public int integerBreak(int n) { - if (n == 2) { - return 1; - } else if (n == 3) { - return 2; - } else if (n % 3 == 0) { - return (int) Math.pow(3, n / 3); - } else if (n % 3 == 1) { - return 2 * 2 * (int) Math.pow(3, (n - 4) / 3); - } else { - return 2 * (int) Math.pow(3, n / 3); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_344.java b/src/main/java/com/fishercoder/solutions/_344.java deleted file mode 100644 index c76805a9de..0000000000 --- a/src/main/java/com/fishercoder/solutions/_344.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 344. Reverse String - * - * Write a function that takes a string as input and returns the string reversed. - - Example: - Given s = "hello", return "olleh". - */ -public class _344 { - public static class Solution1 { - public String reverseString(String s) { - int i = 0; - int j = s.length() - 1; - char[] chars = s.toCharArray(); - while (i < j) { - char temp = chars[i]; - chars[i++] = chars[j]; - chars[j--] = temp; - } - return new String(chars); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_345.java b/src/main/java/com/fishercoder/solutions/_345.java deleted file mode 100644 index 0e6fd09894..0000000000 --- a/src/main/java/com/fishercoder/solutions/_345.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -/** - * 345. Reverse Vowels of a String - * - * Write a function that takes a string as input and reverse only the vowels of a string. - - Example 1: - Given s = "hello", return "holle". - - Example 2: - Given s = "leetcode", return "leotcede". - - Note: - The vowels does not include the letter "y".*/ -public class _345 { - public static class Solution1 { - public String reverseVowels(String s) { - StringBuilder sb = new StringBuilder(s); - Set vowels = new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')); - //use two pointers approach would be the fastest - int i = 0; - int j = s.length() - 1; - while (i < j) { - char left = s.charAt(i); - char right = s.charAt(j); - while (i < j && !vowels.contains(left)) { - i++; - left = s.charAt(i); - } - while (i < j && !vowels.contains(right)) { - j--; - right = s.charAt(j); - } - char temp = left; - sb.setCharAt(i, right); - sb.setCharAt(j, temp); - i++; - j--; - } - return sb.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_346.java b/src/main/java/com/fishercoder/solutions/_346.java deleted file mode 100644 index 38db270a49..0000000000 --- a/src/main/java/com/fishercoder/solutions/_346.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 346. Moving Average from Data Stream - * - * Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. - - For example, - MovingAverage m = new MovingAverage(3); - m.next(1) = 1 - m.next(10) = (1 + 10) / 2 - m.next(3) = (1 + 10 + 3) / 3 - m.next(5) = (10 + 3 + 5) / 3 - */ -public class _346 { - - public static class Solution1 { - class MovingAverage { - - private Queue q; - private Long sum; - private int max; - - /** - * Initialize your data structure here. - */ - public MovingAverage(int size) { - q = new LinkedList(); - sum = 0L; - max = size; - } - - public double next(int val) { - if (q.size() < max) { - q.offer(val); - sum += val; - return (double) sum / q.size(); - } else { - int first = q.poll(); - sum -= first; - q.offer(val); - sum += val; - return (double) sum / q.size(); - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_347.java b/src/main/java/com/fishercoder/solutions/_347.java deleted file mode 100644 index 28e6c34c83..0000000000 --- a/src/main/java/com/fishercoder/solutions/_347.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.PriorityQueue; -import java.util.Queue; - -/** - * 347. Top K Frequent Elements - * - * Given a non-empty array of integers, return the k most frequent elements. - - For example, - Given [1,1,1,2,2,3] and k = 2, return [1,2]. - - Note: - You may assume k is always valid, 1 ≤ k ≤ number of unique elements. - Your algorithm's time complexity must be better than O(n log n), where n is the array's size.*/ - -public class _347 { - - public static class Solution1 { - /** - * Use buckets to hold numbers of the same frequency - * It's averaged at 30 ms on Leetcode. - */ - public List topKFrequent(int[] nums, int k) { - Map map = new HashMap(); - for (int i : nums) { - map.put(i, map.getOrDefault(i, 0) + 1); - } - - ArrayList[] bucket = new ArrayList[nums.length + 1]; - for (Entry e : map.entrySet()) { - int frequency = e.getValue(); - if (bucket[frequency] == null) { - bucket[frequency] = new ArrayList(); - } - bucket[frequency].add(e.getKey()); - } - List result = new ArrayList<>(); - for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) { - if (bucket[i] != null) { - for (int j = 0; j < bucket[i].size(); j++) { - result.add((int) bucket[i].get(j)); - } - } - } - - return result; - } - } - - public static class Solution2 { - /** - * Use hashtable and heap, it's averaged at 100 ms on Leetocde. - */ - public List topKFrequent(int[] nums, int k) { - // construct the frequency map first, and then iterate through the map - // and put them into the heap, this is O(n) - Map map = new HashMap(); - for (int num : nums) { - map.put(num, map.getOrDefault(num, 0) + 1); - } - - // build heap, this is O(logn) - Queue> heap = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue()); - for (Entry entry : map.entrySet()) { - heap.offer(entry); - } - - List res = new ArrayList(); - while (k-- > 0) { - res.add(heap.poll().getKey()); - } - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_348.java b/src/main/java/com/fishercoder/solutions/_348.java deleted file mode 100644 index 1c92c69125..0000000000 --- a/src/main/java/com/fishercoder/solutions/_348.java +++ /dev/null @@ -1,131 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 348. Design Tic-Tac-Toe - * - * Design a Tic-tac-toe game that is played between two players on a n x n grid. - - You may assume the following rules: - - A move is guaranteed to be valid and is placed on an empty block. - Once a winning condition is reached, no more moves is allowed. - A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game. - - Example: - - Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board. - - TicTacToe toe = new TicTacToe(3); - - toe.move(0, 0, 1); -> Returns 0 (no one wins) - |X| | | - | | | | // Player 1 makes a move at (0, 0). - | | | | - - toe.move(0, 2, 2); -> Returns 0 (no one wins) - |X| |O| - | | | | // Player 2 makes a move at (0, 2). - | | | | - - toe.move(2, 2, 1); -> Returns 0 (no one wins) - |X| |O| - | | | | // Player 1 makes a move at (2, 2). - | | |X| - - toe.move(1, 1, 2); -> Returns 0 (no one wins) - |X| |O| - | |O| | // Player 2 makes a move at (1, 1). - | | |X| - - toe.move(2, 0, 1); -> Returns 0 (no one wins) - |X| |O| - | |O| | // Player 1 makes a move at (2, 0). - |X| |X| - - toe.move(1, 0, 2); -> Returns 0 (no one wins) - |X| |O| - |O|O| | // Player 2 makes a move at (1, 0). - |X| |X| - - toe.move(2, 1, 1); -> Returns 1 (player 1 wins) - |X| |O| - |O|O| | // Player 1 makes a move at (2, 1). - |X|X|X| - - Follow up: - Could you do better than O(n2) per move() operation? - - Hint: - - Could you trade extra space such that move() operation can be done in O(1)? - You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal. - - */ -public class _348 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/44548/java-o-1-solution-easy-to-understand - * - * Key: in order to win a TicTacToe, you must have the entire row or column, thus, we don't need - * to keep track of the entire n^2 board. We only need to keep a count for each row and column. - * If at any time, a row or column matches the size of the board, then that player has won. - */ - public static class TicTacToe { - - private int diagonal; - /** - * This is diagonal: |X| | | | |X| | | | |X| So, its condition is always like this: if (row == - * col) - */ - - private int antidiagonal; - /** - * This is antidiagonal: | | |X| | |X| | |X| | | So, its condition is always like this: if - * (col == size - row - 1) - */ - private int[] rows; - private int[] cols; - - /** - * Initialize your data structure here. - */ - public TicTacToe(int n) { - rows = new int[n]; - cols = new int[n]; - } - - /** - * Player {player} makes a move at ({row}, {col}). - * - * @param row The row of the board. - * @param col The column of the board. - * @param player The player, can be either 1 or 2. - * @return The current winning condition, can be either: 0: No one wins. 1: Player 1 wins. 2: - * Player 2 wins. - */ - public int move(int row, int col, int player) { - int toAdd = player == 1 ? 1 : -1; - - rows[row] += toAdd; - cols[col] += toAdd; - int size = rows.length; - - if (row == col) { - diagonal += toAdd; - } - if (col == (size - row - 1)) { - antidiagonal += toAdd; - } - - if (Math.abs(rows[row]) == size - || Math.abs(cols[col]) == size - || Math.abs(antidiagonal) == size - || Math.abs(diagonal) == size) { - return player; - } - - return 0; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_349.java b/src/main/java/com/fishercoder/solutions/_349.java deleted file mode 100644 index bdd0319b27..0000000000 --- a/src/main/java/com/fishercoder/solutions/_349.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; -import java.util.stream.Collectors; - -/** - * 349. Intersection of Two Arrays - * - * Given two arrays, write a function to compute their intersection. - * - * Example 1: - * Input: nums1 = [1,2,2,1], nums2 = [2,2] - * Output: [2] - * - * Example 2: - * Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] - * Output: [9,4] - * - * Note: - * Each element in the result must be unique. - * The result can be in any order. - */ -public class _349 { - - public static class Solution1 { - //credit: https://leetcode.com/articles/intersection-of-two-arrays/ - //Time: O(m+n) on average, O(m*n) in worse case - //Space: O(m+n) - public int[] intersection(int[] nums1, int[] nums2) { - Set set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet()); - Set set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet()); - set1.retainAll(set2); - int[] intersection = new int[set1.size()]; - int i = 0; - for (int num : set1) { - intersection[i++] = num; - } - return intersection; - } - } - - public static class Solution2 { - //Time: O(nlgn) - public int[] intersection(int[] nums1, int[] nums2) { - Arrays.sort(nums2); - Set intersect = new HashSet(); - for (int i : nums1) { - if (binarySearch(i, nums2)) { - intersect.add(i); - } - } - int[] result = new int[intersect.size()]; - int i = 0; - for (int num : intersect) { - result[i++] = num; - } - return result; - } - - private boolean binarySearch(int i, int[] nums) { - int left = 0; - int right = nums.length - 1; - while (left <= right) { - int mid = left + (right - left) / 2; - if (nums[mid] == i) { - return true; - } else if (nums[mid] > i) { - right = mid - 1; - } else { - left = mid + 1; - } - } - return false; - } - } - - public static class Solution3 { - //use two pointers - //credit: https://leetcode.com/problems/intersection-of-two-arrays/discuss/81969/Three-Java-Solutions - public int[] intersection(int[] nums1, int[] nums2) { - Arrays.sort(nums1); - Arrays.sort(nums2); - int i = 0; - int j = 0; - Set set = new HashSet<>(); - while (i < nums1.length && j < nums2.length) { - if (nums1[i] == nums2[j]) { - set.add(nums1[i++]); - j++; - } else if (nums1[i] < nums2[j]) { - i++; - } else { - j++; - } - } - int[] intersection = new int[set.size()]; - int k = 0; - for (int num : set) { - intersection[k++] = num; - } - return intersection; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_35.java b/src/main/java/com/fishercoder/solutions/_35.java deleted file mode 100644 index eb0665c93a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_35.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.fishercoder.solutions; - -public class _35 { - - public static class Solution1 { - public int searchInsert(int[] nums, int target) { - int len = nums.length; - for (int i = 0; i < len; i++) { - if (target <= nums[i]) { - return i; - } - } - return len; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_350.java b/src/main/java/com/fishercoder/solutions/_350.java deleted file mode 100644 index 6ef7bf0216..0000000000 --- a/src/main/java/com/fishercoder/solutions/_350.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Arrays; - -/** - * 350. Intersection of Two Arrays II - * - * Given two arrays, write a function to compute their intersection. - - Example: - Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. - - Note: - Each element in the result should appear as many times as it shows in both arrays. - The result can be in any order. - - Follow up: - What if the given array is already sorted? How would you optimize your algorithm? - What if nums1's size is small compared to nums2's size? Which algorithm is better? - What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once? - */ -public class _350 { - - public static class Solution1 { - public int[] intersect(int[] nums1, int[] nums2) { - Map map = new HashMap(); - for (int i : nums1) { - map.put(i, map.getOrDefault(i, 0) + 1); - } - - List list = new ArrayList(); - for (int i : nums2) { - if (map.containsKey(i) && map.get(i) > 0) { - list.add(i); - map.put(i, map.get(i) - 1); - } - } - return list.stream().mapToInt(i -> i).toArray(); - } - } - - public static class Solution2 { - public int[] intersect(int[] nums1, int[] nums2) { - Arrays.sort(nums1); - Arrays.sort(nums2); - int i = 0; - int j = 0; - List list = new ArrayList<>(); - while (i < nums1.length && j < nums2.length) { - if (nums1[i] == nums2[j]) { - list.add(nums1[i]); - i++; - j++; - } else if (nums1[i] < nums2[j]) { - i++; - } else { - j++; - } - } - return list.stream().mapToInt(k -> k).toArray(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_351.java b/src/main/java/com/fishercoder/solutions/_351.java deleted file mode 100644 index 9a8ac62c47..0000000000 --- a/src/main/java/com/fishercoder/solutions/_351.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 351. Android Unlock Patterns - * - * Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys. - - Rules for a valid pattern: - Each pattern must connect at least m keys and at most n keys. - All the keys must be distinct. - If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed. - The order of keys used matters. - - Explanation: - | 1 | 2 | 3 | - | 4 | 5 | 6 | - | 7 | 8 | 9 | - Invalid move: 4 - 1 - 3 - 6 - Line 1 - 3 passes through key 2 which had not been selected in the pattern. - - Invalid move: 4 - 1 - 9 - 2 - Line 1 - 9 passes through key 5 which had not been selected in the pattern. - - Valid move: 2 - 4 - 1 - 3 - 6 - Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern - - Valid move: 6 - 5 - 4 - 1 - 9 - 2 - Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern. - - Example: - Given m = 1, n = 1, return 9. - - - */ -public class _351 { - - public static class Solution1 { - private int[][] jumps; - private boolean[] visited; - - public int numberOfPatterns(int m, int n) { - jumps = new int[10][10]; - jumps[1][3] = jumps[3][1] = 2; - jumps[4][6] = jumps[6][4] = 5; - jumps[7][9] = jumps[9][7] = 8; - jumps[1][7] = jumps[7][1] = 4; - jumps[2][8] = jumps[8][2] = jumps[1][9] = jumps[9][1] = 5; - jumps[9][3] = jumps[3][9] = 6; - jumps[3][7] = jumps[7][3] = 5; - visited = new boolean[10]; - int count = 0; - count += dfs(1, 1, 0, m, n) - * 4;//1,3,7,9 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4 - count += dfs(2, 1, 0, m, n) - * 4;//2,4,6,8 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4 - count += dfs(5, 1, 0, m, n); - return count; - } - - private int dfs(int num, int len, int count, int m, int n) { - if (len >= m) { - count++; - } - len++; - if (len > n) { - return count; - } - visited[num] = true; - for (int next = 1; next <= 9; next++) { - int jump = jumps[num][next]; - if (!visited[next] && (jump == 0 || visited[jump])) { - count = dfs(next, len, count, m, n); - } - } - visited[num] = false;//backtracking - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_352.java b/src/main/java/com/fishercoder/solutions/_352.java deleted file mode 100644 index d656acbd4a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_352.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Interval; - -import java.util.ArrayList; -import java.util.List; -import java.util.TreeMap; - -/** - * 352. Data Stream as Disjoint Intervals - * - * Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. - - For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: - - [1, 1] - [1, 1], [3, 3] - [1, 1], [3, 3], [7, 7] - [1, 3], [7, 7] - [1, 3], [6, 7] - - Follow up: - What if there are lots of merges and the number of disjoint intervals are small compared to the data stream's size? - */ -public class _352 { - - public static class Solution1 { - public static class SummaryRanges { - - /** - * Use treemap, key is the start of the interval. - */ - TreeMap treeMap; - - /** - * Initialize your data structure here. - */ - public SummaryRanges() { - treeMap = new TreeMap<>(); - } - - public void addNum(int val) { - if (treeMap.containsKey(val)) { - return; - } - Integer lower = treeMap.lowerKey(val); - Integer higher = treeMap.higherKey(val); - if (lower != null - && higher != null - && treeMap.get(lower).end + 1 == val - && higher == val + 1) { - treeMap.get(lower).end = treeMap.get(higher).end; - treeMap.remove(higher); - } else if (lower != null && treeMap.get(lower).end + 1 >= val) { - treeMap.get(lower).end = Math.max(treeMap.get(lower).end, val); - } else if (higher != null && higher == val + 1) { - treeMap.put(val, new Interval(val, treeMap.get(higher).end)); - treeMap.remove(higher); - } else { - treeMap.put(val, new Interval(val, val)); - } - } - - public List getIntervals() { - return new ArrayList<>(treeMap.values()); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_353.java b/src/main/java/com/fishercoder/solutions/_353.java deleted file mode 100644 index 332be9672c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_353.java +++ /dev/null @@ -1,144 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Deque; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Set; - -/** - * Design a Snake game that is played on a device with screen size = width x height. Play the game online if you are not familiar with the game. - *

- * The snake is initially positioned at the top left corner (0,0) with length = 1 unit. - *

- * You are given a list of food's positions in row-column order. When a snake eats the food, its length and the game's score both increase by 1. - *

- * Each food appears one by one on the screen. For example, the second food will not appear until the first food was eaten by the snake. - *

- * When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake. - *

- * Example: - * Given width = 3, height = 2, and food = [[1,2],[0,1]]. - *

- * Snake snake = new Snake(width, height, food); - *

- * Initially the snake appears at position (0,0) and the food at (1,2). - *

- * |S| | | - * | | |F| - *

- * snake.move("R"); -> Returns 0 - *

- * | |S| | - * | | |F| - *

- * snake.move("D"); -> Returns 0 - *

- * | | | | - * | |S|F| - *

- * snake.move("R"); -> Returns 1 (Snake eats the first food and right after that, the second food appears at (0,1) ) - *

- * | |F| | - * | |S|S| - *

- * snake.move("U"); -> Returns 1 - *

- * | |F|S| - * | | |S| - *

- * snake.move("L"); -> Returns 2 (Snake eats the second food) - *

- * | |S|S| - * | | |S| - *

- * snake.move("U"); -> Returns -1 (Game over because snake collides with border) - */ -public class _353 { - public class SnakeGame { - private Set set;//Use a set to hold all occupied points for the snake body, this is for easy access for the case of eating its own body - private Deque body;//use a queue to hold all occupied points for the snake body as well, this is for easy access to update the tail - int[][] food; - int score; - int foodIndex; - int width; - int height; - - /** - * Initialize your data structure here. - * - * @param width - screen width - * @param height - screen height - * @param food - A list of food positions - * E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. - */ - public SnakeGame(int width, int height, int[][] food) { - this.set = new HashSet(); - set.add(0);//initially at [0][0] - this.body = new LinkedList(); - body.offerLast(0); - this.food = food; - this.width = width; - this.height = height; - } - - /** - * Moves the snake. - * - * @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down - * @return The game's score after the move. Return -1 if game over. - * Game over when snake crosses the screen boundary or bites its body. - */ - public int move(String direction) { - if (score == -1) { - return -1; - } - - //compute head - int rowHead = body.peekFirst() / width; - int colHead = body.peekFirst() % width; - switch (direction) { - case "U": - rowHead--; - break; - case "D": - rowHead++; - break; - case "L": - colHead--; - break; - default: - colHead++; - } - int newHead = rowHead * width + colHead; - - set.remove(body.peekLast());//we'll remove the tail from set for now to see if it hits its tail - //if it hits the boundary - if (set.contains(newHead) || rowHead < 0 || colHead < 0 || rowHead >= height || colHead >= width) { - return score = -1; - } - - //add head for the following two normal cases: - set.add(newHead); - body.offerFirst(newHead); - - //normal eat case: keep tail, add head - if (foodIndex < food.length && rowHead == food[foodIndex][0] && colHead == food[foodIndex][1]) { - set.add(body.peekLast());//old tail does not change, so add it back to set since we removed it earlier - foodIndex++; - return ++score; - } - - - //normal move case without eating: move head and remove tail - body.pollLast(); - return score; - - } - } - -/** - * Your SnakeGame object will be instantiated and called as such: - * SnakeGame obj = new SnakeGame(width, height, food); - * int param_1 = obj.move(direction); - */ -} diff --git a/src/main/java/com/fishercoder/solutions/_354.java b/src/main/java/com/fishercoder/solutions/_354.java deleted file mode 100644 index dfd3941af6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_354.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 354. Russian Doll Envelopes - * - * You have a number of envelopes with widths and heights given as a pair of integers (w, h). - * One envelope can fit into another if and only if both the width and height of one envelope is greater than - * the width and height of the other envelope. - - What is the maximum number of envelopes can you Russian doll? (put one inside other) - - Example: - Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]). - - */ -public class _354 { - public static class Solution1 { - /** reference: https://discuss.leetcode.com/topic/47469/java-nlogn-solution-with-explanation */ - public int maxEnvelopes(int[][] envelopes) { - if (envelopes == null || envelopes.length == 0 - || envelopes[0].length == 0 || envelopes[0].length != 2) { - return 0; - } - Arrays.sort(envelopes, (int[] a, int[] b) -> { - if (a[0] == b[0]) { - return b[1] - a[1]; - } else { - return a[0] - b[0]; - } - } - ); - int[] dp = new int[envelopes.length]; - int len = 0; - for (int[] envelope : envelopes) { - int index = Arrays.binarySearch(dp, 0, len, envelope[1]); - if (index < 0) { - index = -(index + 1); - } - dp[index] = envelope[1]; - if (index == len) { - len++; - } - } - return len; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_356.java b/src/main/java/com/fishercoder/solutions/_356.java deleted file mode 100644 index 6d38057446..0000000000 --- a/src/main/java/com/fishercoder/solutions/_356.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 356. Line Reflection - * - * Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given points. - - Example 1: - Given points = [[1,1],[-1,1]], return true. - - Example 2: - Given points = [[1,1],[-1,-1]], return false. - - Follow up: - Could you do better than O(n2)? - - Hint: - - Find the smallest and largest x-value for all points. - If there is a line then it should be at y = (minX + maxX) / 2. - For each point, make sure that it has a reflected point in the opposite side. - */ -public class _356 { - public static class Solution1 { - /** credit: https://discuss.leetcode.com/topic/48172/simple-java-hashset-solution */ - public boolean isReflected(int[][] points) { - int max = Integer.MIN_VALUE; - int min = Integer.MAX_VALUE; - Set set = new HashSet<>(); - for (int[] point : points) { - max = Math.max(max, point[0]); - min = Math.min(min, point[0]); - String str = point[0] + "a" + point[1]; - set.add(str); - } - int sum = max + min; - for (int[] p : points) { - String str = (sum - p[0]) + "a" + p[1]; - if (!set.contains(str)) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_357.java b/src/main/java/com/fishercoder/solutions/_357.java deleted file mode 100644 index 00da3f1d59..0000000000 --- a/src/main/java/com/fishercoder/solutions/_357.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 357. Count Numbers with Unique Digits - * Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. - - Example: - Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]) - - Hint: - A direct way is to use the backtracking approach. - Backtracking should contains three states which are - (the current number, number of steps to get that number and a bitmask which - represent which number is marked as visited so far in the current number). - Start with state (0,0,0) and count all valid number till we reach number of steps equals to 10n. - This problem can also be solved using a dynamic programming approach and some knowledge of combinatorics. - Let f(k) = count of numbers with unique digits with length equals k. - f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0]. - */ -public class _357 { - - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/47983/java-dp-o-1-solution Following the hint. - * Let f(n) = count of number with unique digits of length n. f(1) = 10. (0, 1, 2, 3, ...., 9) - * f(2) = 9 * 9. Because for each number i from 1, ..., 9, we can pick j to form a 2-digit - * number ij and there are 9 numbers that are different from i for j to choose from. f(3) = f(2) - * * 8 = 9 * 9 * 8. Because for each number with unique digits of length 2, say ij, we can pick - * k to form a 3 digit number ijk and there are 8 numbers that are different from i and j for k - * to choose from. Similarly f(4) = f(3) * 7 = 9 * 9 * 8 * 7.... ... f(10) = 9 * 9 * 8 * 7 * 6 * - * ... * 1 f(11) = 0 = f(12) = f(13).... any number with length > 10 couldn't be unique digits - * number. The problem is asking for numbers from 0 to 10^n. Hence return f(1) + f(2) + .. + - * f(n) As @4acreg suggests, There are only 11 different ans. You can create a lookup table for - * it. This problem is O(1) in essence. - */ - public int countNumbersWithUniqueDigits(int n) { - if (n == 0) { - return 1; - } - int res = 10; - int uniqueDigits = 9; - int availableNumber = 9; - while (n-- > 1 && availableNumber > 0) { - uniqueDigits = uniqueDigits * availableNumber; - res += uniqueDigits; - availableNumber--; - } - return res; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_358.java b/src/main/java/com/fishercoder/solutions/_358.java deleted file mode 100644 index c9a21ca2f6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_358.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.PriorityQueue; -import java.util.Queue; - -/** - * 358. Rearrange String k Distance Apart - * - * Given a non-empty string s and an integer k, rearrange the string such that the same characters are at least distance k from each other. - - All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string "". - Example 1: - s = "aabbcc", k = 3 - Result: "abcabc" - The same letters are at least distance 3 from each other. - - Example 2: - s = "aaabc", k = 3 - Answer: "" - It is not possible to rearrange the string. - - Example 3: - s = "aaadbbcc", k = 2 - Answer: "abacabcd" - Another possible answer is: "abcabcda" - The same letters are at least distance 2 from each other. - */ -public class _358 { - - public static class Solution1 { - public String rearrangeString(String s, int k) { - Map count = new HashMap<>(); - for (char c : s.toCharArray()) { - count.put(c, count.getOrDefault(c, 0) + 1); - } - - PriorityQueue> heap = - new PriorityQueue<>((a, b) -> b.getValue() - a.getValue()); - heap.addAll(count.entrySet()); - - Queue> waitQueue = new LinkedList<>(); - - StringBuilder stringBuilder = new StringBuilder(); - while (!heap.isEmpty()) { - Map.Entry entry = heap.poll(); - stringBuilder.append(entry.getKey()); - entry.setValue(entry.getValue() - 1); - waitQueue.offer(entry); - if (waitQueue.size() < k) { - continue; //there's only k-1 chars in the waitHeap, not full yet - } - Map.Entry front = waitQueue.poll(); - if (front.getValue() > 0) { - heap.offer(front); - } - } - - return stringBuilder.length() == s.length() ? stringBuilder.toString() : ""; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_359.java b/src/main/java/com/fishercoder/solutions/_359.java deleted file mode 100644 index 17f8739985..0000000000 --- a/src/main/java/com/fishercoder/solutions/_359.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -/** - * 359. Logger Rate Limiter - * - * Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds. - - Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false. - - It is possible that several messages arrive roughly at the same time. - - Example: - - Logger logger = new Logger(); - - // logging string "foo" at timestamp 1 - logger.shouldPrintMessage(1, "foo"); returns true; - - // logging string "bar" at timestamp 2 - logger.shouldPrintMessage(2,"bar"); returns true; - - // logging string "foo" at timestamp 3 - logger.shouldPrintMessage(3,"foo"); returns false; - - // logging string "bar" at timestamp 8 - logger.shouldPrintMessage(8,"bar"); returns false; - - // logging string "foo" at timestamp 10 - logger.shouldPrintMessage(10,"foo"); returns false; - - // logging string "foo" at timestamp 11 - logger.shouldPrintMessage(11,"foo"); returns true;*/ -public class _359 { - - public static class Solution1 { - class Logger { - - private Map map; - private Set set; - - /** - * Initialize your data structure here. - */ - public Logger() { - map = new HashMap(); - set = new HashSet(); - } - - /** - * Returns true if the message should be printed in the given timestamp, otherwise returns - * false. The timestamp is in seconds granularity. - */ - public boolean shouldPrintMessage(int timestamp, String message) { - if (!set.contains(message)) { - map.put(message, timestamp); - set.add(message); - return true; - } else { - if (timestamp - map.get(message) < 10) { - return false; - } else { - map.put(message, timestamp); - return true; - } - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_360.java b/src/main/java/com/fishercoder/solutions/_360.java deleted file mode 100644 index f80833b0c8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_360.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 360. Sort Transformed Array - * - * Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f(x) = ax2 + bx + c to each element x in the array. - - The returned array must be in sorted order. - - Expected time complexity: O(n) - - Example: - nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5, - - Result: [3, 9, 15, 33] - - nums = [-4, -2, 2, 4], a = -1, b = 3, c = 5 - - Result: [-23, -5, 1, 7] - - Credits: - Special thanks to @elmirap for adding this problem and creating all test cases. - */ -public class _360 { - - public static class Solution1 { - //credit: https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution - //in sum, only two cases: when a >= 0 or when a < 0, this simplifies logic - public int[] sortTransformedArray(int[] nums, int a, int b, int c) { - int n = nums.length; - int[] sorted = new int[n]; - int i = 0; - int j = n - 1; - int index = a >= 0 ? n - 1 : 0; - while (i <= j) { - if (a >= 0) { - sorted[index--] = - function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function( - nums[i++], a, b, c) : function(nums[j--], a, b, c); - } else { - sorted[index++] = - function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function( - nums[j--], a, b, c) : function(nums[i++], a, b, c); - } - } - return sorted; - } - - private int function(int num, int a, int b, int c) { - return a * (num * num) + b * num + c; - } - } - - public static class Solution2 { - public int[] sortTransformedArray(int[] nums, int a, int b, int c) { - int[] result = new int[nums.length]; - for (int i = 0; i < nums.length; i++) { - result[i] = function(nums[i], a, b, c); - } - Arrays.sort(result); - return result; - } - - private int function(int num, int a, int b, int c) { - return a * (num * num) + b * num + c; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_361.java b/src/main/java/com/fishercoder/solutions/_361.java deleted file mode 100644 index 30713e5a2e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_361.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 361. Bomb Enemy - * - * Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb. - The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed. - Note that you can only put the bomb at an empty cell. - - Example: - For the given grid - - 0 E 0 0 - E 0 W E - 0 E 0 0 - - return 3. (Placing a bomb at (1,1) kills 3 enemies) - */ -public class _361 { - - public static class Solution1 { - public int maxKilledEnemies(char[][] grid) { - int m = grid.length; - if (grid == null || m == 0) { - return 0; - } - int n = grid[0].length; - - int[][] max = new int[m][n]; - - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - - if (grid[i][j] == '0') { - int count = 0; - - //count all possible hits in its upward direction - for (int k = j - 1; k >= 0; k--) { - if (grid[i][k] == 'E') { - count++; - } else if (grid[i][k] == 'W') { - break; - } - } - - //count all possible hits in its downward direction - for (int k = j + 1; k < n; k++) { - if (grid[i][k] == 'E') { - count++; - } else if (grid[i][k] == 'W') { - break; - } - } - - //count all possible hits in its right direction - for (int k = i + 1; k < m; k++) { - if (grid[k][j] == 'E') { - count++; - } else if (grid[k][j] == 'W') { - break; - } - } - - //count all possible hits in its left direction - for (int k = i - 1; k >= 0; k--) { - if (grid[k][j] == 'E') { - count++; - } else if (grid[k][j] == 'W') { - break; - } - } - - max[i][j] = count; - } - } - } - - int result = 0; - - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (max[i][j] > result) { - result = max[i][j]; - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_362.java b/src/main/java/com/fishercoder/solutions/_362.java deleted file mode 100644 index 034bd05023..0000000000 --- a/src/main/java/com/fishercoder/solutions/_362.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.fishercoder.solutions; -/** - * 362. Design Hit Counter - * - * Design a hit counter which counts the number of hits received in the past 5 minutes. - - Each function accepts a timestamp parameter (in seconds granularity) and you may assume - that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing). - You may assume that the earliest timestamp starts at 1. - - It is possible that several hits arrive roughly at the same time. - - Example: - HitCounter counter = new HitCounter(); - - // hit at timestamp 1. - counter.hit(1); - - // hit at timestamp 2. - counter.hit(2); - - // hit at timestamp 3. - counter.hit(3); - - // get hits at timestamp 4, should return 3. - counter.getHits(4); - - // hit at timestamp 300. - counter.hit(300); - - // get hits at timestamp 300, should return 4. - counter.getHits(300); - - // get hits at timestamp 301, should return 3. - counter.getHits(301); - Follow up: - What if the number of hits per second could be very large? Does your design scale? - */ - -public class _362 { - - public static class Solution1 { - public static class HitCounter { - /** - * Reference: https://discuss.leetcode.com/topic/48758/super-easy-design-o-1-hit-o-s-gethits-no-fancy-data-structure-is-needed, - * I added one more field k to make it more generic. - * It basically maintains a window of size 300, use modular to update the index. - */ - private int[] times; - private int[] hits; - private int k; - - /** - * Initialize your data structure here. - */ - public HitCounter() { - k = 300; - times = new int[k]; - hits = new int[k]; - } - - /** - * Record a hit. - * - * @param timestamp - The current timestamp (in seconds granularity). - */ - public void hit(int timestamp) { - int index = timestamp % k; - if (times[index] != timestamp) { - times[index] = timestamp; - hits[index] = 1; - } else { - hits[index]++; - } - } - - /** - * Return the number of hits in the past 5 minutes. - * - * @param timestamp - The current timestamp (in seconds granularity). - */ - public int getHits(int timestamp) { - int total = 0; - for (int i = 0; i < k; i++) { - if (timestamp - times[i] < k) { - total += hits[i]; - } - } - return total; - } - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_363.java b/src/main/java/com/fishercoder/solutions/_363.java deleted file mode 100644 index a7a26994d8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_363.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.TreeSet; - -/** - * 363. Max Sum of Rectangle No Larger Than K - * - * Given a non-empty 2D matrix matrix and an integer k, - * find the max sum of a rectangle in the matrix such that its sum is no larger than k. - - Example: - Given matrix = [ - [1, 0, 1], - [0, -2, 3] - ] - k = 2 - The answer is 2. Because the sum of rectangle [[0, 1], [-2, 3]] is 2 and 2 is the max number no larger than k (k = 2). - - Note: - The rectangle inside the matrix must have an area > 0. - What if the number of rows is much larger than the number of columns? - */ -public class _363 { - public static class Solution1 { - /** reference: https://discuss.leetcode.com/topic/48854/java-binary-search-solution-time-complexity-min-m-n-2-max-m-n-log-max-m-n */ - public int maxSumSubmatrix(int[][] matrix, int k) { - int row = matrix.length; - if (row == 0) { - return 0; - } - int col = matrix[0].length; - int m = Math.min(row, col); - int n = Math.max(row, col); - //indicating sum up in every row or every column - boolean colIsBig = col > row; - int res = Integer.MIN_VALUE; - for (int i = 0; i < m; i++) { - int[] array = new int[n]; - // sum from row j to row i - for (int j = i; j >= 0; j--) { - int val = 0; - TreeSet set = new TreeSet<>(); - set.add(0); - //traverse every column/row and sum up - for (int p = 0; p < n; p++) { - array[p] = array[p] + (colIsBig ? matrix[j][p] : matrix[p][j]); - val = val + array[p]; - //use TreeMap to binary search previous sum to get possible result - Integer subres = set.ceiling(val - k); - if (null != subres) { - res = Math.max(res, val - subres); - } - set.add(val); - } - } - } - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_364.java b/src/main/java/com/fishercoder/solutions/_364.java deleted file mode 100644 index 34977fe536..0000000000 --- a/src/main/java/com/fishercoder/solutions/_364.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.NestedInteger; - -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -/** - * 364. Nested List Weight Sum II - * - * Given a nested list of integers, return the sum of all integers in the list weighted by their depth. - - Each element is either an integer, or a list -- whose elements may also be integers or other lists. - - Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight. - - Example 1: - Given the list [[1,1],2,[1,1]], return 8. (four 1's at depth 1, one 2 at depth 2) - - Example 2: - Given the list [1,[4,[6]]], return 17. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17)*/ -public class _364 { - - public static class Solution1 { - public int depthSumInverse(List nestedList) { - Queue q = new LinkedList<>(); - for (NestedInteger next : nestedList) { - q.offer(next); - } - int prev = 0; - int total = 0; - - while (!q.isEmpty()) { - int size = q.size(); - int levelSum = 0; - for (int i = 0; i < size; i++) { - NestedInteger next = q.poll(); - if (next.isInteger()) { - levelSum += next.getInteger(); - } else { - List list = next.getList(); - for (NestedInteger n : list) { - q.offer(n); - } - } - } - prev += levelSum; - total += prev; - } - return total; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_365.java b/src/main/java/com/fishercoder/solutions/_365.java deleted file mode 100644 index e23c2cbfd3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_365.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 365. Water and Jug Problem - * - * You are given two jugs with capacities x and y litres. - * There is an infinite amount of water supply available. - * You need to determine whether it is possible to measure exactly z litres using these two jugs. - * - If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end. - - Operations allowed: - - Fill any of the jugs completely with water. - Empty any of the jugs. - Pour water from one jug into another till the other jug is completely full or the first jug itself is empty. - - Example 1: (From the famous "Die Hard" example) - Input: x = 3, y = 5, z = 4 - Output: True - - Example 2: - Input: x = 2, y = 6, z = 5 - Output: False - - */ -public class _365 { - - public static class Solution1 { - public boolean canMeasureWater(int x, int y, int z) { - if (x + y < z) { - return false; - } - if (x == z || y == z || x + y == z) { - return true; - } - return z % gcd(x, y) == 0; - } - - int gcd(int x, int y) { - while (y != 0) { - int temp = y; - y = x % y; - x = temp; - } - return x; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_366.java b/src/main/java/com/fishercoder/solutions/_366.java deleted file mode 100644 index 0663c09968..0000000000 --- a/src/main/java/com/fishercoder/solutions/_366.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 366. Find Leaves of Binary Tree - * - * Given a binary tree, collect a tree's nodes as if you were doing this: - * Collect and remove all leaves, repeat until the tree is empty. - - Example: - Given binary tree - 1 - / \ - 2 3 - / \ - 4 5 - Returns [4, 5, 3], [2], [1]. - - Explanation: - 1. Removing the leaves [4, 5, 3] would result in this tree: - - 1 - / - 2 - 2. Now removing the leaf [2] would result in this tree: - - 1 - 3. Now removing the leaf [1] would result in the empty tree: - - [] - Returns [4, 5, 3], [2], [1]. - - */ -public class _366 { - - public static class Solution1 { - List> result = new ArrayList<>(); - - public List> findLeaves(TreeNode root) { - dfs(root); - return result; - } - - int dfs(TreeNode root) { - if (root == null) { - return 0; - } - int level = Math.max(dfs(root.left), dfs(root.right)) + 1; - if (result.size() < level) { - result.add(new ArrayList<>()); - } - result.get(level - 1).add(root.val); - return level; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_368.java b/src/main/java/com/fishercoder/solutions/_368.java deleted file mode 100644 index e3a5606298..0000000000 --- a/src/main/java/com/fishercoder/solutions/_368.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 368. Largest Divisible Subset - * - * Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. - - If there are multiple solutions, return any subset is fine. - - Example 1: - - nums: [1,2,3] - - Result: [1,2] (of course, [1,3] will also be ok) - Example 2: - - nums: [1,2,4,8] - - Result: [1,2,4,8] - */ -public class _368 { - - public static class Solution1 { - /** Credit: https://discuss.leetcode.com/topic/49652/classic-dp-solution-similar-to-lis-o-n-2 */ - public List largestDivisibleSubset(int[] nums) { - int len = nums.length; - int[] count = new int[len]; - int[] pre = new int[len]; - Arrays.sort(nums); - int max = 0; - int index = -1; - for (int i = 0; i < len; i++) { - count[i] = 1; - pre[i] = -1; - for (int j = i - 1; j >= 0; j--) { - if (nums[i] % nums[j] == 0) { - if (1 + count[j] > count[i]) { - count[i] = count[j] + 1; - pre[i] = j; - } - } - } - if (count[i] > max) { - max = count[i]; - index = i; - } - } - List res = new ArrayList<>(); - while (index != -1) { - res.add(nums[index]); - index = pre[index]; - } - return res; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_369.java b/src/main/java/com/fishercoder/solutions/_369.java deleted file mode 100644 index 7cf64e9896..0000000000 --- a/src/main/java/com/fishercoder/solutions/_369.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 369. Plus One Linked List - * - * Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer. - - You may assume the integer do not contain any leading zero, except the number 0 itself. - - The digits are stored such that the most significant digit is at the head of the list. - - Example: - Input: - 1->2->3 - - Output: - 1->2->4 - */ -public class _369 { - - public static class Solution1 { - public ListNode plusOne(ListNode head) { - //get the length of the list and take out the value of each node and store them into an array - ListNode temp = head; - int len = 0; - while (temp != null) { - len++; - temp = temp.next; - } - - int[] nums = new int[len]; - temp = head; - int j = 0; - while (temp != null) { - nums[j++] = temp.val; - temp = temp.next; - } - - //plus one into this array: nums - for (int i = len - 1; i >= 0; i--) { - if (nums[i] != 9) { - nums[i]++; - break; - } else { - nums[i] = 0; - } - } - - //still assuming the first value in the list should not be zero as it's representing a valid number, although it's in a list - ListNode pre = new ListNode(-1); - if (nums[0] == 0) { - //in this case, let's just construct a new linked list and return: only first node value is 1, all the rest is 0 - ListNode newHead = new ListNode(1); - ListNode result = newHead; - int count = 0; - while (count++ < len) { - newHead.next = new ListNode(0); - newHead = newHead.next; - } - return result; - } else { - pre.next = head; - for (int i = 0; i < len; i++) { - head.val = nums[i]; - head = head.next; - } - return pre.next; - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_370.java b/src/main/java/com/fishercoder/solutions/_370.java deleted file mode 100644 index 85c018c985..0000000000 --- a/src/main/java/com/fishercoder/solutions/_370.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 370. Range Addition - * - * Assume you have an array of length n initialized with all 0's and are given k update operations. - * Each operation is represented as a triplet: [startIndex, endIndex, inc] - * which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc. - * Return the modified array after all k operations were executed. - - Example: - - Given: - - length = 5, - updates = [ - [1, 3, 2], - [2, 4, 3], - [0, 2, -2] - ] - - Output: - - [-2, 0, 3, 5, 3] - Explanation: - - Initial state: - [ 0, 0, 0, 0, 0 ] - - After applying operation [1, 3, 2]: - [ 0, 2, 2, 2, 0 ] - - After applying operation [2, 4, 3]: - [ 0, 2, 5, 5, 3 ] - - After applying operation [0, 2, -2]: - [-2, 0, 3, 5, 3 ] - - Hint: - Thinking of using advanced data structures? You are thinking it too complicated. - For each update operation, do you really need to update all elements between i and j? - Update only the first and end element is sufficient. - The optimal time complexity is O(k + n) and uses O(1) extra space. - */ - -public class _370 { - public static class Solution1 { - public int[] getModifiedArray(int length, int[][] updates) { - int[] nums = new int[length]; - int k = updates.length; - for (int i = 0; i < k; i++) { - int start = updates[i][0]; - int end = updates[i][1]; - int inc = updates[i][2]; - nums[start] += inc; - if (end < length - 1) { - nums[end + 1] -= inc; - } - } - - int sum = 0; - for (int i = 0; i < length; i++) { - sum += nums[i]; - nums[i] = sum; - } - return nums; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_371.java b/src/main/java/com/fishercoder/solutions/_371.java deleted file mode 100644 index 8eea9f5eed..0000000000 --- a/src/main/java/com/fishercoder/solutions/_371.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 371. Sum of Two Integers - * - * Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. - - Example: - Given a = 1 and b = 2, return 3. - */ -public class _371 { - - public static class Solution1 { - /** reference: http://stackoverflow.com/questions/9070937/adding-two-numbers-without-operator-clarification */ - public int getSum(int a, int b) { - if (b == 0) { - return a; - } - int sum = a ^ b; - int carry = (a & b) << 1; - return getSum(sum, carry); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_372.java b/src/main/java/com/fishercoder/solutions/_372.java deleted file mode 100644 index 00c5b9c54e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_372.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 372. Super Pow - * - * Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array. - - Example1: - - a = 2 - b = [3] - - Result: 8 - Example2: - - a = 2 - b = [1,0] - - Result: 1024 - */ -public class _372 { - public static class Solution1 { - public int superPow(int a, int[] b) { - if (a % 1337 == 0) { - return 0; - } - int p = 0; - for (int i : b) { - p = (p * 10 + i) % 1140; - } - if (p == 0) { - p += 1140; - } - return power(a, p, 1337); - } - - private int power(int a, int n, int mod) { - a %= mod; - int result = 1; - while (n != 0) { - if ((n & 1) != 0) { - result = result * a % mod; - } - a = a * a % mod; - n >>= 1; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_373.java b/src/main/java/com/fishercoder/solutions/_373.java deleted file mode 100644 index 96f3eaae54..0000000000 --- a/src/main/java/com/fishercoder/solutions/_373.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; -import java.util.PriorityQueue; -import java.util.Queue; - -/** - * 373. Find K Pairs with Smallest Sums - * - * You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. - * Define a pair (u,v) which consists of one element from the first array and one element from the second array. - * Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums. - - Example 1: - Given nums1 = [1,7,11], nums2 = [2,4,6], k = 3 - Return: [1,2],[1,4],[1,6] - The first 3 pairs are returned from the sequence: - [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6] - - Example 2: - Given nums1 = [1,1,2], nums2 = [1,2,3], k = 2 - Return: [1,1],[1,1] - The first 2 pairs are returned from the sequence: - [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3] - - Example 3: - Given nums1 = [1,2], nums2 = [3], k = 3 - Return: [1,3],[2,3] - All possible pairs are returned from the sequence: - [1,3],[2,3] - - */ -public class _373 { - public static class Solution1 { - - final int[][] neighbors = new int[][] {{0, 1}, {1, 0}}; - - public List kSmallestPairs(int[] nums1, int[] nums2, int k) { - List result = new ArrayList<>(); - if (nums1 == null - || nums2 == null - || k == 0 - || nums1.length == 0 - || nums2.length == 0) { - return result; - } - Queue meanHeap = new PriorityQueue<>(); - meanHeap.offer(new Pair(0, 0, nums1[0] + nums2[0])); - boolean[][] visited = new boolean[nums1.length][nums2.length]; - visited[0][0] = true;//we start form (0,0), so mark it as visited - while (k > 0 && !meanHeap.isEmpty()) { - Pair pair = meanHeap.poll(); - result.add(new int[] {nums1[pair.row], nums2[pair.col]}); - k--; - for (int[] neighbor : neighbors) { - int nextRow = pair.row + neighbor[0]; - int nextCol = pair.col + neighbor[1]; - if (nextRow < 0 - || nextCol < 0 - || nextRow >= nums1.length - || nextCol >= nums2.length - || visited[nextRow][nextCol]) { - continue; - } - visited[nextRow][nextCol] = true; - meanHeap.offer(new Pair(nextRow, nextCol, nums1[nextRow] + nums2[nextCol])); - } - } - - return result; - } - - class Pair implements Comparable { - int row; - int col; - int sum; - - public Pair(int row, int col, int sum) { - this.row = row; - this.col = col; - this.sum = sum; - } - - @Override - public int compareTo(Pair that) { - return this.sum - that.sum; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_374.java b/src/main/java/com/fishercoder/solutions/_374.java deleted file mode 100644 index f0d4b37029..0000000000 --- a/src/main/java/com/fishercoder/solutions/_374.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 374. Guess Number Higher or Lower - * We are playing the Guess Game. The game is as follows: - * I pick a number from 1 to n. You have to guess which number I picked. - * Every time you guess wrong, I'll tell you whether the number is higher or lower. - * You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0): - - -1 : My number is lower - 1 : My number is higher - 0 : Congrats! You got it! - Example: - n = 10, I pick 6. - Return 6. - - */ -public class _374 { - public static class Solution1 { - /** - * The core problem/trouble to solve this problem is to figure out the problem description: this - * API: guess(int num) means to take your guess num and let you know if your guessed num is - * bigger or smaller than the answer. That's why if num > target, it returns -1 which means the - * target is smaller than your guess!!! - */ - - public int guessNumber(int n) { - int left = 1; - int right = n; - while (left + 1 < right) { - int mid = left + (right - left) / 2; - int g = guess(mid); - if (g == 0) { - return mid; - } else if (g > 0) { - left = mid; - } else { - right = mid; - } - } - if (guess(left) == 0) { - return left; - } - return right; - } - - /** - * This is a fake guess method that I wrote just to compile/test, I'll have to change it to - * another number other than 6 based on the number to be found. - */ - private int guess(int num) { - if (num > 6) { - return -1; - } else if (num < 6) { - return 1; - } else { - return 0; - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_375.java b/src/main/java/com/fishercoder/solutions/_375.java deleted file mode 100644 index 3f2fca7ef8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_375.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 375. Guess Number Higher or Lower II - * - * We are playing the Guess Game. The game is as follows: - * I pick a number from 1 to n. You have to guess which number I picked. - * Every time you guess wrong, I'll tell you whether the number I picked is higher or lower. - * However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked. - - Example: - - n = 10, I pick 8. - - First round: You guess 5, I tell you that it's higher. You pay $5. - Second round: You guess 7, I tell you that it's higher. You pay $7. - Third round: You guess 9, I tell you that it's lower. You pay $9. - - Game over. 8 is the number I picked. - - You end up paying $5 + $7 + $9 = $21. - Given a particular n ≥ 1, find out how much money you need to have to guarantee a win. - - Hint: - - The best strategy to play the game is to minimize the maximum loss you could possibly face. Another strategy is to minimize the expected loss. Here, we are interested in the first scenario. - Take a small example (n = 3). What do you end up paying in the worst case? - Check out this article if you're still stuck. - The purely recursive implementation of minimax would be worthless for even a small n. You MUST use dynamic programming. - As a follow-up, how would you modify your code to solve the problem of minimizing the expected loss, instead of the worst-case loss? - */ -public class _375 { - public static class Solution1 { - public int getMoneyAmount(int n) { - int[][] table = new int[n + 1][n + 1]; - return dp(table, 1, n); - } - - private int dp(int[][] table, int s, int e) { - if (s >= e) { - return 0; - } - if (table[s][e] != 0) { - return table[s][e]; - } - int res = Integer.MAX_VALUE; - for (int i = s; i <= e; i++) { - int temp = i + Math.max(dp(table, s, i - 1), dp(table, i + 1, e)); - res = Math.min(res, temp); - } - table[s][e] = res; - return res; - } - } - - public static class Solution2 { - public int getMoneyAmount(int n) { - if (n == 1) { - return 0; - } - int[][] dp = new int[n + 1][n + 1]; - for (int x = 1; x < n; x++) { - for (int i = 0; i + x <= n; i++) { - int j = i + x; - dp[i][j] = Integer.MAX_VALUE; - for (int k = i; k <= j; k++) { - dp[i][j] = Math.min(dp[i][j], k + Math.max(k - 1 >= i ? dp[i][k - 1] : 0, j >= k + 1 ? dp[k + 1][j] : 0)); - } - } - } - for (int i = 0; i < n + 1; i++) { - for (int j = 0; j < n + 1; j++) { - System.out.print(dp[i][j] + " "); - } - System.out.println(); - } - return dp[1][n]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_376.java b/src/main/java/com/fishercoder/solutions/_376.java deleted file mode 100644 index 613bdfd86f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_376.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 376. Wiggle Subsequence - * - * A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly - * alternate between positive and negative. - * The first difference (if one exists) may be either positive or negative. - * A sequence with fewer than two elements is trivially a wiggle sequence. - - For example, [1,7,4,9,2,5] is a wiggle sequence because the differences - (6,-3,5,-7,3) are alternately positive and negative. - In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, - the first because its first two differences are positive and the second because its last difference is zero. - - Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. - A subsequence is obtained by deleting some number of elements (eventually, also zero) - from the original sequence, leaving the remaining elements in their original order. - - Examples: - Input: [1,7,4,9,2,5] - Output: 6 - The entire sequence is a wiggle sequence. - - Input: [1,17,5,10,13,15,10,5,16,8] - Output: 7 - There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8]. - - Input: [1,2,3,4,5,6,7,8,9] - Output: 2 - - Follow up: - Can you do it in O(n) time? - */ -public class _376 { - - public static class Solution1 { - public int wiggleMaxLength(int[] nums) { - if (nums.length < 2) { - return nums.length; - } - int prevDiff = nums[1] - nums[0]; - int count = (prevDiff != 0) ? 2 : 1; - for (int i = 2; i < nums.length; i++) { - int diff = nums[i] - nums[i - 1]; - /**ATTN: prevDiff could be zero. e.g. [3,3,3,2,5] - * but diff needs to exactly greater than zero*/ - if ((prevDiff <= 0 && diff > 0) || (prevDiff >= 0) && diff < 0) { - count++; - prevDiff = diff; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_377.java b/src/main/java/com/fishercoder/solutions/_377.java deleted file mode 100644 index f825c8400b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_377.java +++ /dev/null @@ -1,154 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 377. Combination Sum IV - * - * Given an integer array with all positive numbers and no duplicates, - * find the number of possible combinations that add up to a positive integer target. - - Example: - - nums = [1, 2, 3] - target = 4 - - The possible combination ways are: - (1, 1, 1, 1) - (1, 1, 2) - (1, 2, 1) - (1, 3) - (2, 1, 1) - (2, 2) - (3, 1) - - Note that different sequences are counted as different combinations. - - Therefore the output is 7. - - Follow up: - What if negative numbers are allowed in the given array? - How does it change the problem? - What limitation we need to add to the question to allow negative numbers? - */ - -public class _377 { - - public static class Solution1 { - /** - * this normal backtracking recursive solution will end up in MLE by this testcase: [4,2,1], 32 - */ - public int combinationSum4(int[] nums, int target) { - List> result = new ArrayList(); - Arrays.sort(nums); - backtracking(nums, target, new ArrayList(), result); - return result.size(); - } - - private void backtracking(int[] nums, int target, List list, - List> result) { - if (target == 0) { - result.add(new ArrayList(list)); - } else if (target > 0) { - for (int i = 0; i < nums.length; i++) { - list.add(nums[i]); - backtracking(nums, target - nums[i], list, result); - list.remove(list.size() - 1); - } - } - } - } - - public static class Solution2 { - /** - * Since we don't need to get all of the combinations, instead, - * we only need to get the possible count, I can use only a count instead of "List> result" - * However, it also ended up in TLE by this testcase: [1,2,3], 32 - */ - public static int count = 0; - - public int combinationSum4(int[] nums, int target) { - Arrays.sort(nums); - backtracking(nums, target, new ArrayList()); - return count; - } - - private void backtracking(int[] nums, int target, List list) { - if (target == 0) { - count++; - } else if (target > 0) { - for (int i = 0; i < nums.length; i++) { - list.add(nums[i]); - backtracking(nums, target - nums[i], list); - list.remove(list.size() - 1); - } - } - } - } - - public static class Solution3 { - /** - * Time: O(n^2) - * Space: O(n) - * - * Since this question doesn't require to return all the combination result, instead, it just wants one number, we could use DP - * the idea is similar to Climbing Stairs. - * - * The idea is very clear as the code speaks for itself: - * It's easy to find the routine - * dp[0] = 0; - * dp[1] = 1; - * ... - * - * Reference: https://discuss.leetcode.com/topic/52186/my-3ms-java-dp-solution - */ - public int combinationSum4(int[] nums, int target) { - Arrays.sort(nums); - int[] result = new int[target + 1]; - for (int i = 1; i < result.length; i++) { - for (int num : nums) { - if (num > i) { - break; - } else if (num == i) { - result[i]++; - } else { - result[i] += result[i - num]; - } - } - } - return result[target]; - } - } - - public static class Solution4 { - /** - * Time: O(n) - * Space: O(n) - * - * Reference: https://discuss.leetcode.com/topic/52255/java-recursion-solution-using-hashmap-as-memory - */ - public static Map map = new HashMap<>();//need to remove public static before submitting on Leetcode as it doesn't reset static variables - - public int combinationSum4(int[] nums, int target) { - if (nums == null || nums.length == 0 || target < 0) { - return 0; - } - if (target == 0) { - return 1; - } - if (map.containsKey(target)) { - return map.get(target); - } - int count = 0; - for (int num : nums) { - count += combinationSum4(nums, target - num); - } - map.put(target, count); - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_378.java b/src/main/java/com/fishercoder/solutions/_378.java deleted file mode 100644 index 4772cf0fda..0000000000 --- a/src/main/java/com/fishercoder/solutions/_378.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * 378. Kth Smallest Element in a Sorted Matrix - * Given a n x n matrix where each of the rows and columns are - * sorted in ascending order, find the kth smallest element in the matrix. - -Note that it is the kth smallest element in the sorted order, not the kth distinct element. - -Example: - -matrix = [ - [ 1, 5, 9], - [10, 11, 13], - [12, 13, 15] -], -k = 8, - -return 13. - -Note: -You may assume k is always valid, 1 ≤ k ≤ n2. - */ - -public class _378 { - public static class Solution1 { - /** - * brute force made it AC'ed, extreme test case needed for OJ - */ - public int kthSmallest(int[][] matrix, int k) { - List list = new ArrayList(); - int n = matrix.length; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - list.add(matrix[i][j]); - } - } - Collections.sort(list); - return list.get(k - 1); - } - } - - public static class Solution2 { - /** - * Binary Search : The idea is to pick a mid number, then compare it with the elements in each row, we start form - * end of row util we find the element is less than the mid, the left side element is all less than mid; keep tracking elements - * that less than mid and compare with k, then update the k. - */ - public int kthSmallestBS(int[][] matrix, int k) { - int row = matrix.length - 1; - int col = matrix[0].length - 1; - int lo = matrix[0][0]; - int hi = matrix[row][col]; - while (lo < hi) { - int mid = lo + (hi - lo) / 2; - int count = 0; - int j = col; - for (int i = 0; i <= row; i++) { - while (j >= 0 && matrix[i][j] > mid) { - j--; - } - count += (j + 1); - } - if (count < k) { - lo = mid + 1; - } else { - hi = mid; - } - } - return lo; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_379.java b/src/main/java/com/fishercoder/solutions/_379.java deleted file mode 100644 index c969634d83..0000000000 --- a/src/main/java/com/fishercoder/solutions/_379.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Queue; -import java.util.Set; - -/** - * 379. Design Phone Directory - * - * Design a Phone Directory which supports the following operations: - - get: Provide a number which is not assigned to anyone. - check: Check if a number is available or not. - release: Recycle or release a number. - Example: - - // Init a phone directory containing a total of 3 numbers: 0, 1, and 2. - PhoneDirectory directory = new PhoneDirectory(3); - - // It can return any available phone number. Here we assume it returns 0. - directory.get(); - - // Assume it returns 1. - directory.get(); - - // The number 2 is available, so return true. - directory.check(2); - - // It returns 2, the only number that is left. - directory.get(); - - // The number 2 is no longer available, so return false. - directory.check(2); - - // Release number 2 back to the pool. - directory.release(2); - - // Number 2 is available again, return true. - directory.check(2); - */ -public class _379 { - - public static class Solution1 { - private class PhoneDirectory { - private Queue phoneDir; - private Set used; - - /** - * Initialize your data structure here - * - * @param maxNumbers - The maximum numbers that can be stored in the phone directory. - */ - public PhoneDirectory(int maxNumbers) { - phoneDir = new LinkedList(); - int number = 0; - while (maxNumbers-- > 0) { - phoneDir.add(number++); - } - used = new HashSet(); - } - - /** - * Provide a number which is not assigned to anyone. - * - * @return - Return an available number. Return -1 if none is available. - */ - public int get() { - if (phoneDir.peek() == null) { - return -1; - } - int newNumber = phoneDir.poll(); - used.add(newNumber); - return newNumber; - } - - /** - * Check if a number is available or not. - */ - public boolean check(int number) { - return !used.contains(number); - } - - /** - * Recycle or release a number. - */ - public void release(int number) { - if (used.remove(number)) { - phoneDir.add(number); - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_380.java b/src/main/java/com/fishercoder/solutions/_380.java deleted file mode 100644 index eccff73176..0000000000 --- a/src/main/java/com/fishercoder/solutions/_380.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.List; -import java.util.ArrayList; -import java.util.Map; -import java.util.Random; - -/** - * 380. Insert Delete GetRandom O(1) - * Design a data structure that supports all following operations in average O(1) time. - * - * insert(val): Inserts an item val to the set if not already present. - * remove(val): Removes an item val from the set if present. - * getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned. - * Example: - * - * // Init an empty set. - * RandomizedSet randomSet = new RandomizedSet(); - * - * // Inserts 1 to the set. Returns true as 1 was inserted successfully. - * randomSet.insert(1); - * - * // Returns false as 2 does not exist in the set. - * randomSet.remove(2); - * - * // Inserts 2 to the set, returns true. Set now contains [1,2]. - * randomSet.insert(2); - * - * // getRandom should return either 1 or 2 randomly. - * randomSet.getRandom(); - * - * // Removes 1 from the set, returns true. Set now contains [2]. - * randomSet.remove(1); - * - * // 2 was already in the set, so return false. - * randomSet.insert(2); - * - * // Since 2 is the only number in the set, getRandom always return 2. - * randomSet.getRandom(); - */ - -public class _380 { - - public static class Solution1 { - /** - * credit: https://leetcode.com/problems/insert-delete-getrandom-o1/discuss/85401/Java-solution-using-a-HashMap-and-an-ArrayList-along-with-a-follow-up.-(131-ms) - */ - public static class RandomizedSet { - Map locationMap; - List list; - Random random; - - /** - * Initialize your data structure here. - */ - public RandomizedSet() { - locationMap = new HashMap<>(); - list = new ArrayList<>(); - random = new Random(); - } - - /** - * Inserts a value to the set. Returns true if the set did not already contain the specified element. - */ - public boolean insert(int val) { - if (locationMap.containsKey(val)) { - return false; - } else { - locationMap.put(val, list.size()); - list.add(val); - return true; - } - } - - /** - * Removes a value from the set. Returns true if the set contained the specified element. - */ - public boolean remove(int val) { - if (!locationMap.containsKey(val)) { - return false; - } else { - int location = locationMap.get(val); - /**if it's not the last one, then swap the last one with this val*/ - if (location < list.size() - 1) { - int lastOne = list.get(list.size() - 1); - list.set(location, lastOne); - locationMap.put(lastOne, location); - } - locationMap.remove(val); - list.remove(list.size() - 1); - return true; - } - } - - /** - * Get a random element from the set. - */ - public int getRandom() { - return list.get(random.nextInt(list.size())); - } - - } - } - - /** - * Your _380 object will be instantiated and called as such: - * _380 obj = new _380(); - * boolean param_1 = obj.insert(val); - * boolean param_2 = obj.delete(val); - * int param_3 = obj.getRandom(); - */ - public static class Solution2 { - /**This is not ideal and not meeting average O(1) time for all three operations.*/ - public static class RandomizedSet { - - Map forwardMap; - //key is auto increment index, value if the inserted val - Map reverseMap;//the other way around - int index; - Random random; - - /** - * Initialize your data structure here. - */ - public RandomizedSet() { - forwardMap = new HashMap(); - reverseMap = new HashMap(); - index = 0; - random = new Random(); - } - - /** - * Inserts a value to the set. Returns true if the set did not already contain the specified - * element. - */ - public boolean insert(int val) { - if (forwardMap.containsValue(val)) { - return false; - } else { - forwardMap.put(index, val); - reverseMap.put(val, index++); - return true; - } - } - - /** - * Deletes a value from the set. Returns true if the set contained the specified element. - */ - public boolean remove(int val) { - if (forwardMap.containsValue(val)) { - int key = reverseMap.get(val); - reverseMap.remove(val); - forwardMap.remove(key); - return true; - } else { - return false; - } - } - - /** - * Get a random element from the set. - */ - public int getRandom() { - int max = forwardMap.size(); - if (max == 1) { - return forwardMap.get(index - 1); - } - int randomNum = random.nextInt(max); - while (!forwardMap.containsKey(randomNum)) { - randomNum = random.nextInt(max); - } - return forwardMap.get(randomNum); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_381.java b/src/main/java/com/fishercoder/solutions/_381.java deleted file mode 100644 index 959fea5597..0000000000 --- a/src/main/java/com/fishercoder/solutions/_381.java +++ /dev/null @@ -1,107 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; -import java.util.Random; - -/**381. Insert Delete GetRandom O(1) - Duplicates allowed - * -Design a data structure that supports all following operations in average O(1) time. - -Note: Duplicate elements are allowed. -insert(val): Inserts an item val to the collection. -remove(val): Removes an item val from the collection if present. -getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains. -Example: - -// Init an empty collection. -_381 collection = new _381(); - -// Inserts 1 to the collection. Returns true as the collection did not contain 1. -collection.insert(1); - -// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1]. -collection.insert(1); - -// Inserts 2 to the collection, returns true. Collection now contains [1,1,2]. -collection.insert(2); - -// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3. -collection.getRandom(); - -// Removes 1 from the collection, returns true. Collection now contains [1,2]. -collection.remove(1); - -// getRandom should return 1 and 2 both equally likely. -collection.getRandom();*/ -public class _381 { - public static class Solution1 { - - Map forwardMap; - //key is the to-be-inserted number, value is its auto-incremented index - Map reverseMap;//the other way around - int index; - Random rand; - - /** - * Initialize your data structure here. - */ - public Solution1() { - forwardMap = new HashMap(); - reverseMap = new HashMap(); - index = 0; - rand = new Random(); - } - - /** - * Inserts a value to the collection. Returns true if the collection did not already contain the - * specified element. - */ - public boolean insert(int val) { - boolean contains; - if (reverseMap.containsValue(val)) { - contains = true; - } else { - contains = false; - } - forwardMap.put(val, - index);//this will overwrite the preivous key with a new index if the key already exists - reverseMap.put(index, val); - index++; - return contains; - } - - /** - * Removes a value from the collection. Returns true if the collection contained the specified - * element. - */ - public boolean remove(int val) { - boolean contains; - if (reverseMap.containsValue(val)) { - contains = true; - if (forwardMap.containsKey(val)) { - int i = forwardMap.get(val); - forwardMap.remove(val); - reverseMap.remove(i); - } else { - //remove the entry in revserve map that has val as its value - reverseMap.values().remove(val); - } - } else { - contains = false; - } - return contains; - } - - /** - * Get a random element from the collection. - */ - public int getRandom() { - int randNum = rand.nextInt(index); - while (!reverseMap.containsKey(randNum)) { - randNum = rand.nextInt(index); - } - return reverseMap.get(randNum); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_382.java b/src/main/java/com/fishercoder/solutions/_382.java deleted file mode 100644 index d98ff9f9bd..0000000000 --- a/src/main/java/com/fishercoder/solutions/_382.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -import java.util.HashMap; -import java.util.Map; -import java.util.Random; - -/**382. Linked List Random Node - * -Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. - -Follow up: -What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space? - -Example: - -// Init a singly linked list [1,2,3]. -ListNode head = new ListNode(1); -head.next = new ListNode(2); -head.next.next = new ListNode(3); -Solution solution = new Solution(head); - -// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning. -solution.getRandom(); -*/ -public class _382 { - - public static class Solution1 { - private Map map; - private Random rand; - - /** - * @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at least one node. - */ - public Solution1(ListNode head) { - map = new HashMap(); - rand = new Random(); - int i = 0; - while (head != null) { - map.put(i++, head); - head = head.next; - } - } - - /** - * Returns a random node's value. - */ - public int getRandom() { - return map.get(rand.nextInt(map.size())).val; - } - } -} - diff --git a/src/main/java/com/fishercoder/solutions/_383.java b/src/main/java/com/fishercoder/solutions/_383.java deleted file mode 100644 index 41309403c8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_383.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 383 Ransom Note - * - * Given an arbitrary ransom note string and another string containing letters - * from all the magazines, write a function that will return true if - * the ransom note can be constructed from the magazines ; otherwise, it will return false. - * Each letter in the magazine string can only be used once in your ransom note. - - Note: - - You may assume that both strings contain only lowercase letters. - canConstruct("a", "b") -> false - canConstruct("aa", "ab") -> false - canConstruct("aa", "aab") -> true -*/ -public class _383 { - - public static class Solution1 { - public boolean canConstruct(String ransomNote, String magazine) { - char[] mchars = magazine.toCharArray(); - int[] mcnt = new int[256]; - for (int i = 0; i < mchars.length; i++) { - mcnt[mchars[i] - 'a']++; - } - - char[] rchars = ransomNote.toCharArray(); - for (int i = 0; i < rchars.length; i++) { - if (mcnt[rchars[i] - 'a'] <= 0) { - return false; - } - mcnt[rchars[i] - 'a']--; - } - return true; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_384.java b/src/main/java/com/fishercoder/solutions/_384.java deleted file mode 100644 index 389a72766e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_384.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.fishercoder.solutions; -/** - * 384. Shuffle an Array -Shuffle a set of numbers without duplicates. - -Example: - -// Init an array with set 1, 2, and 3. -int[] nums = {1,2,3}; -Solution solution = new Solution(nums); - -// Shuffle the array [1,2,3] and return its result. - Any permutation of [1,2,3] must equally likely to be returned. -solution.shuffle(); - -// Resets the array back to its original configuration [1,2,3]. -solution.reset(); - -// Returns the random shuffling of array [1,2,3]. -solution.shuffle();*/ -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; -import java.util.Random; - -public class _384 { - - public static class Solution1 { - //Note: the problem states that this is a set without duplicates which makes building all combinations easier - - private List> combinations; - private int[] original; - private Random random; - - public Solution1(int[] nums) { - original = nums; - random = new Random(); - combinations = buildAllComb(nums); - } - - //insert next value into all possible positions, I wrote this method myself, of course it could be simplified to not use a queue - //but it just naturally came into my mind that I used a queue - private List> buildAllComb(int[] nums) { - List> result = new ArrayList(); - if (nums == null || nums.length == 0) { - return result; - } - - List list = new ArrayList(); - list.add(nums[0]); - Queue> q = new LinkedList(); - q.offer(list); - for (int i = 1; i < nums.length; i++) { - int qSize = q.size(); - for (int k = 0; k < qSize; k++) { - List currList = q.poll(); - for (int j = 0; j <= currList.size(); j++) { - List newL = new ArrayList(currList); - newL.add(j, nums[i]); - q.offer(newL); - } - } - } - while (!q.isEmpty()) { - result.add(q.poll()); - } - return result; - } - - /** - * Resets the array to its original configuration and return it. - */ - public int[] reset() { - return original; - } - - /** - * Returns a random shuffling of the array. - */ - public int[] shuffle() { - if (original == null || original.length == 0) { - return original; - } - int randomIndex = random.nextInt(combinations.size()); - List list = combinations.get(randomIndex); - int[] result = new int[list.size()]; - for (int i = 0; i < list.size(); i++) { - result[i] = list.get(i); - } - return result; - } - } -} - diff --git a/src/main/java/com/fishercoder/solutions/_385.java b/src/main/java/com/fishercoder/solutions/_385.java deleted file mode 100644 index f56b68c060..0000000000 --- a/src/main/java/com/fishercoder/solutions/_385.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.NestedInteger; - -import java.util.Stack; - -/** - * 385. Mini Parser - * - * Given a nested list of integers represented as a string, - * implement a parser to deserialize it. - - Each element is either an integer, or a list -- - whose elements may also be integers or other lists. - - Note: You may assume that the string is well-formed: - - String is non-empty. - String does not contain white spaces. - String contains only digits 0-9, [, - ,, ]. - Example 1: - - Given s = "324", - - You should return a NestedInteger object which contains a single integer 324. - Example 2: - - Given s = "[123,[456,[789]]]", - - Return a NestedInteger object containing a nested list with 2 elements: - - 1. An integer containing value 123. - 2. A nested list containing two elements: - i. An integer containing value 456. - ii. A nested list with one element: - a. An integer containing value 789.*/ -public class _385 { - - public static class Solution1 { - //Lessons: ask the interviewer to clarify the input, for this question, the input could be "324", "[324]", they are different - //the former should return a nested integer with one single integer, the latter should return a nested integer with a list - - //Idea: - //if it's '[', we just construct a new nested integer and push it onto the stack - //if it's a number, we parse the whole number and add to the previous nested integer object - //if it's ',', we'll just continue; - //if it's ']', we'll just pop one nested integer from the working stack and assign it to the result - - public NestedInteger deserialize(String s) { - if (s == null || s.isEmpty() || s.length() == 0) { - return new NestedInteger(); - } - Stack workStack = new Stack<>(); - NestedInteger result = null; - StringBuilder sb = new StringBuilder(); - int i = 0; - //if it's just a single number, then we'll just return a nested integer with one integer - if (s.charAt(i) != '[') { - sb.setLength(0); - while (i < s.length() && ((Character.getNumericValue(s.charAt(i)) < 10 - && Character.getNumericValue(s.charAt(i)) >= 0) || s.charAt(i) == '-')) { - sb.append(s.charAt(i)); - i++; - } - int num = Integer.parseInt(sb.toString()); - return new NestedInteger(num); - } else { - //all other cases, we'll return a nested integer with a list - while (i < s.length()) { - if (s.charAt(i) == '[') { - NestedInteger ni = new NestedInteger(); - // we'll put this one into its last one if there's one on the workStack - if (!workStack.isEmpty()) { - NestedInteger lastNi = workStack.pop(); - lastNi.add(ni); - workStack.push(lastNi);// then push it back - } - workStack.push(ni); - i++; - } else if (s.charAt(i) == ',') { - i++; - } else if (s.charAt(i) == ']') { - NestedInteger completedNi = workStack.pop(); - result = completedNi; - i++; - } else { - // then it must be a number - sb.setLength(0); - while (i < s.length() - && ((Character.getNumericValue(s.charAt(i)) < 10 && Character - .getNumericValue(s.charAt(i)) >= 0) || s.charAt(i) == '-')) { - sb.append(s.charAt(i)); - i++; - } - int num = Integer.parseInt(sb.toString()); - NestedInteger ni = null; - if (!workStack.isEmpty()) { - ni = workStack.pop(); - } else { - ni = new NestedInteger(); - } - // case 1: if this one contains one integer - if (ni.isInteger()) { - // we'll add it to this ni - ni.add(new NestedInteger(num)); - } else if (ni.getList() != null && ni.getList().size() != 0) { - // case 2: if this one contains a nested integer - // we'll get the last nested integer and add this one to it - ni.add(new NestedInteger(num)); - } else { - // case 3: if this is an empty nested integer - if (i > 0) { - ni.add(new NestedInteger(num)); - } else { - ni.setInteger(num); - } - } - workStack.push(ni); - if (i == s.length()) { - return ni;// this is for test cases like this: "324", there's no '[' or ']' - } - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_386.java b/src/main/java/com/fishercoder/solutions/_386.java deleted file mode 100644 index e3f9e236d3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_386.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 386. Lexicographical Numbers - * - * Given an integer n, return 1 - n in lexicographical order. - * - * For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. - * - * Please optimize your algorithm to use less time and space. The input size may be as large as - * 5,000,000. - */ -public class _386 { - public static class Solution1 { - //Radix sort doesn't apply here! Don't confuse yourself! - - //rewrote their solution from Python to Java:https://discuss.leetcode.com/topic/54986/python-memory-limit-exceeded-for-problem-386/17 - public List lexicalOrder(int n) { - List result = new ArrayList(); - int i = 1; - while (true) { - result.add(i); - if (i * 10 <= n) { - i *= 10; - } else { - while (i % 10 == 9 || i == n) { - i /= 10; - } - if (i == 0) { - return result; - } - i++; - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_387.java b/src/main/java/com/fishercoder/solutions/_387.java deleted file mode 100644 index d984595ab3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_387.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 387. First Unique Character in a String Given a string, find the first non-repeating character in - * it and return it's index. If it doesn't exist, return -1. - * - * Examples: - * - * s = "leetcode" return 0. - * - * s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters. - */ - -public class _387 { - public static class Solution1 { - public static int firstUniqChar(String s) { - int[] freq = new int[26]; - for (int i = 0; i < s.length(); i++) { - freq[s.charAt(i) - 'a']++; - } - for (int i = 0; i < s.length(); i++) { - if (freq[s.charAt(i) - 'a'] == 1) { - return i; - } - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_388.java b/src/main/java/com/fishercoder/solutions/_388.java deleted file mode 100644 index 362ff56075..0000000000 --- a/src/main/java/com/fishercoder/solutions/_388.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 388. Longest Absolute File Path - * - * Suppose we abstract our file system by a string in the following manner: - * - * The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: - * - * dir - * subdir1 - * subdir2 - * file.ext - * The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext. - * - * The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents: - * - * dir - * subdir1 - * file1.ext - * subsubdir1 - * subdir2 - * subsubdir2 - * file2.ext - * The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. - * subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext. - * - * We are interested in finding the longest (number of characters) absolute path to a file within our file system. - * For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes). - * - * Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0. - * - * Note: - * - * The name of a file contains at least a . and an extension. - * The name of a directory or sub-directory will not contain a .. - * Time complexity required: O(n) where n is the size of the input string. - * - * Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.*/ -public class _388 { - public static class Solution1 { - public int lengthLongestPath(String input) { - Stack stack = new Stack(); - int longestLen = 0; - int currDirLen = 0; - int i = 0; - int currLevel = 0; - int nextLevel = 0; - boolean isFile = false; - Character period = '.'; - Character space = ' '; - while (i < input.length()) { - currLevel = nextLevel; - int currStrLen = 0; - while (i < input.length() && (Character.isLetterOrDigit(input.charAt(i)) - || period.equals(input.charAt(i)) || space.equals(input.charAt(i)))) { - if (period.equals(input.charAt(i))) { - isFile = true; - } - i++; - currStrLen++; - } - if (isFile) { - longestLen = Math.max(longestLen, currDirLen + currStrLen); - } else { - currDirLen += currStrLen + 1; - stack.push(currStrLen + 1); - } - - nextLevel = 0; - i = i + 1;//increment one to let it pass "\n" and start from "\t" - while (i < input.length() - 1 && input.substring(i, i + 1).equals("\t")) { - nextLevel++; - i = i + 1; - } - - if (nextLevel < currLevel) { - int j = 0; - if (isFile) { - while (!stack.isEmpty() && j < (currLevel - nextLevel)) { - currDirLen -= stack.pop(); - j++; - } - } else { - while (!stack.isEmpty() && j <= (currLevel - nextLevel)) { - currDirLen -= stack.pop(); - j++; - } - } - } else if (nextLevel == currLevel) { - if (!isFile && !stack.isEmpty()) { - currDirLen -= stack.pop(); - } - } - - if (nextLevel == 0) { - currDirLen = 0; - stack.clear(); - } - - isFile = false; - } - - return longestLen; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_389.java b/src/main/java/com/fishercoder/solutions/_389.java deleted file mode 100644 index fb8b2f7788..0000000000 --- a/src/main/java/com/fishercoder/solutions/_389.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; -/** - * 389. Find the Difference - * - * Given two strings s and t which consist of only lowercase letters. - - String t is generated by random shuffling string s and then add - one more letter at a random position. - - Find the letter that was added in t. - - Example: - - Input: - s = "abcd" - t = "abcde" - - Output: - e - - Explanation: 'e' is the letter that was added.*/ - -public class _389 { - public static class Solution1 { - public char findTheDifference(String s, String t) { - int[] counts = new int[128]; - char[] schars = s.toCharArray(); - char[] tchars = t.toCharArray(); - for (int i = 0; i < schars.length; i++) { - counts[schars[i]]++; - } - for (int i = 0; i < tchars.length; i++) { - counts[tchars[i]]--; - } - char result = 'a'; - for (int i = 0; i < 128; i++) { - if (counts[i] != 0) { - result = (char) i; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_390.java b/src/main/java/com/fishercoder/solutions/_390.java deleted file mode 100644 index 458ce1fb77..0000000000 --- a/src/main/java/com/fishercoder/solutions/_390.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 390. Elimination Game - * - * There is a list of sorted integers from 1 to n. - * Starting from left to right, - * remove the first number and every other number afterward until you reach the end of the list. - * Repeat the previous step again, but this time from right to left, - * remove the right most number and every other number from the remaining numbers. - * We keep repeating the steps again, alternating left to right and right to left, until a single number remains. - * Find the last number that remains starting with a list of length n. - - Example: - - Input: - n = 9, - 1 2 3 4 5 6 7 8 9 - 2 4 6 8 - 2 6 - 6 - - Output: - 6 - - */ -public class _390 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/55870/share-my-solutions-for-contest-2 instead of - * literally removing half of the elements in each scan, this solution is just moving the - * pointer to point to next start position So brilliant! - */ - public int lastRemaining(int n) { - int remaining = n; - int start = 1; - int step = 2; - boolean forward = true; - while (remaining > 1) { - remaining /= 2; - if (forward) { - start = start + step * remaining - step / 2; - } else { - start = start - step * remaining + step / 2; - } - step *= 2; - forward = !forward; - } - return start; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_391.java b/src/main/java/com/fishercoder/solutions/_391.java deleted file mode 100644 index d380ecfdda..0000000000 --- a/src/main/java/com/fishercoder/solutions/_391.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 391. Perfect Rectangle - * - * Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. - Each rectangle is represented as a bottom-left point and a top-right point. - For example, a unit square is represented as [1,1,2,2]. (coordinate of bottom-left point is (1, 1) and top-right point is (2, 2)). - - Example 1: - - rectangles = [ - [1,1,3,3], - [3,1,4,2], - [3,2,4,4], - [1,3,2,4], - [2,3,3,4] - ] - - Return true. All 5 rectangles together form an exact cover of a rectangular region. - - Example 2: - - rectangles = [ - [1,1,2,3], - [1,3,2,4], - [3,1,4,2], - [3,2,4,4] - ] - - Return false. Because there is a gap between the two rectangular regions. - - Example 3: - - rectangles = [ - [1,1,3,3], - [3,1,4,2], - [1,3,2,4], - [3,2,4,4] - ] - - Return false. Because there is a gap in the top center. - - Example 4: - - rectangles = [ - [1,1,3,3], - [3,1,4,2], - [1,3,2,4], - [2,2,4,4] - ] - - Return false. Because two of the rectangles overlap with each other. - */ -public class _391 { - public static class Solution1 { - /** credit: https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java */ - public boolean isRectangleCover(int[][] rectangles) { - if (rectangles.length == 0 || rectangles[0].length == 0) { - return false; - } - - int x1 = Integer.MAX_VALUE; - int x2 = Integer.MIN_VALUE; - int y1 = Integer.MAX_VALUE; - int y2 = Integer.MIN_VALUE; - - Set set = new HashSet<>(); - int area = 0; - - for (int[] rect : rectangles) { - x1 = Math.min(rect[0], x1); - y1 = Math.min(rect[1], y1); - x2 = Math.max(rect[2], x2); - y2 = Math.max(rect[3], y2); - - area += (rect[2] - rect[0]) * (rect[3] - rect[1]); - - String s1 = rect[0] + " " + rect[1]; - String s2 = rect[0] + " " + rect[3]; - String s3 = rect[2] + " " + rect[3]; - String s4 = rect[2] + " " + rect[1]; - - if (!set.add(s1)) { - set.remove(s1); - } - if (!set.add(s2)) { - set.remove(s2); - } - if (!set.add(s3)) { - set.remove(s3); - } - if (!set.add(s4)) { - set.remove(s4); - } - } - - if (!set.contains(x1 + " " + y1) || !set.contains(x1 + " " + y2) || !set.contains( - x2 + " " + y1) || !set.contains(x2 + " " + y2) || set.size() != 4) { - return false; - } - - return area == (x2 - x1) * (y2 - y1); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_392.java b/src/main/java/com/fishercoder/solutions/_392.java deleted file mode 100644 index 0b6d6da7b9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_392.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 392. Is Subsequence - * - * Given a string s and a string t, check if s is subsequence of t. - - You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100). - - A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). - - Example 1: - s = "abc", t = "ahbgdc" - - Return true. - - Example 2: - s = "axc", t = "ahbgdc" - - Return false. - - Follow up: - If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? - - */ -public class _392 { - - public static class Solution1 { - public boolean isSubsequence(String s, String t) { - int left = 0; - for (int i = 0; i < s.length(); i++) { - boolean foundI = false; - int j = left; - for (; j < t.length(); j++) { - if (s.charAt(i) == t.charAt(j)) { - left = j + 1; - foundI = true; - break; - } - } - if (j == t.length() && !foundI) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_393.java b/src/main/java/com/fishercoder/solutions/_393.java deleted file mode 100644 index a44ab81575..0000000000 --- a/src/main/java/com/fishercoder/solutions/_393.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 393. UTF-8 Validation - * - * A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: - - For 1-byte character, the first bit is a 0, followed by its unicode code. - For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10. - This is how the UTF-8 encoding would work: - - Char. number range | UTF-8 octet sequence - (hexadecimal) | (binary) - --------------------+--------------------------------------------- - 0000 0000-0000 007F | 0xxxxxxx - 0000 0080-0000 07FF | 110xxxxx 10xxxxxx - 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx - 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx - Given an array of integers representing the data, return whether it is a valid utf-8 encoding. - - Note: - The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data. - - Example 1: - data = [197, 130, 1], which represents the octet sequence: 11000101 10000010 00000001. - Return true. - - It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character. - - - Example 2: - data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100. - Return false. - - The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character. - The next byte is a continuation byte which starts with 10 and that's correct. - But the second continuation byte does not start with 10, so it is invalid. - */ -public class _393 { - - public static class Solution1 { - /** credit: https://discuss.leetcode.com/topic/58338/bit-manipulation-java-6ms/4 */ - public boolean validUtf8(int[] data) { - int count = 0; - for (int d : data) { - if (count == 0) { - if ((d >> 5) == 0b110) { - count = 1; - } else if ((d >> 4) == 0b1110) { - count = 2; - } else if ((d >> 3) == 0b11110) { - count = 3; - } else if ((d >> 7) == 1) { - return false; - } - } else { - if ((d >> 6) != 0b10) { - return false; - } else { - count--; - } - } - } - return count == 0; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_394.java b/src/main/java/com/fishercoder/solutions/_394.java deleted file mode 100644 index 52d5cc2868..0000000000 --- a/src/main/java/com/fishercoder/solutions/_394.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/**394. Decode String - * - * Given an encoded string, return it's decoded string. - * The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. - * Note that k is guaranteed to be a positive integer. - * You may assume that the input string is always valid; - * No extra white spaces, square brackets are well-formed, etc. - * Furthermore, you may assume that the original data does not contain any digits and - * that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4]. - * - * Examples: - * s = "3[a]2[bc]", return "aaabcbc". - * s = "3[a2[c]]", return "accaccacc". - * s = "2[abc]3[cd]ef", return "abcabccdcdcdef". - * */ - -public class _394 { - - public static class Solution1 { - public String decodeString(String s) { - Stack count = new Stack<>(); - Stack str = new Stack<>(); - - int idx = 0; - str.push(""); - - while (idx < s.length()) { - if (s.charAt(idx) >= '0' && s.charAt(idx) <= '9') { - int start = idx; - while (s.charAt(idx + 1) >= '0' && s.charAt(idx + 1) <= '9') { - idx++; - } - - count.push(Integer.parseInt(s.substring(start, idx + 1))); - } else if (s.charAt(idx) == '[') { - str.push(""); - } else if (s.charAt(idx) == ']') { - String st = str.pop(); - StringBuilder sb = new StringBuilder(); - int n = count.pop(); - - for (int j = 0; j < n; j++) { - sb.append(st); - } - - str.push(str.pop() + sb.toString()); - } else { - str.push(str.pop() + s.charAt(idx)); - } - - idx++; - } - - return str.pop(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_395.java b/src/main/java/com/fishercoder/solutions/_395.java deleted file mode 100644 index 57b8def451..0000000000 --- a/src/main/java/com/fishercoder/solutions/_395.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 395. Longest Substring with At Least K Repeating Characters - * - * Find the length of the longest substring T of a given string - * (consists of lowercase letters only) - * such that every character in T appears no less than k times. - - Example 1: - Input: - s = "aaabb", k = 3 - - Output: - 3 - - The longest substring is "aaa", as 'a' is repeated 3 times. - - - Example 2: - Input: - s = "ababbc", k = 2 - - Output: - 5 - - The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times. - */ - -public class _395 { - public static class Solution1 { - /**Reference: https://discuss.leetcode.com/topic/57372/java-divide-and-conquer-recursion-solution*/ - public int longestSubstring(String s, int k) { - return findLongestSubstring(s.toCharArray(), 0, s.length(), k); - } - - int findLongestSubstring(char[] chars, int start, int end, int k) { - /**Base case 1 of 2*/ - if (end - start < k) { - return 0; - } - int[] count = new int[26]; - for (int i = start; i < end; i++) { - int index = chars[i] - 'a'; - count[index]++; - } - - /**For every character in the above frequency table*/ - for (int i = 0; i < 26; i++) { - if (count[i] < k && count[i] > 0) { - for (int j = start; j < end; j++) { - if (chars[j] == i + 'a') { - int left = findLongestSubstring(chars, start, j, k); - int right = findLongestSubstring(chars, j + 1, end, k); - return Math.max(left, right); - } - } - } - } - /**Base case 2 of 2: - * when any characters in this substring has repeated at least k times, then this entire substring is a valid answer*/ - return end - start; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_396.java b/src/main/java/com/fishercoder/solutions/_396.java deleted file mode 100644 index d1febc3f1a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_396.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 396. Rotate Function - * - * Given an array of integers A and let n to be its length. - - Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow: - - F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]. - - Calculate the maximum value of F(0), F(1), ..., F(n-1). - - Note: - n is guaranteed to be less than 105. - - Example: - - A = [4, 3, 2, 6] - - F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25 - F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16 - F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23 - F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26 - - So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.*/ - -public class _396 { - public static class Solution1 { - /** - * F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1] - */ - public int maxRotateFunction(int[] A) { - if (A == null || A.length == 0) { - return 0; - } - int[] F = new int[A.length]; - int[] B = A; - int max = Integer.MIN_VALUE; - for (int i = 0; i < A.length; i++) { - F[i] = compute(B); - max = Math.max(max, F[i]); - B = rotate(B); - } - return max; - } - - private int compute(int[] b) { - int sum = 0; - for (int i = 0; i < b.length; i++) { - sum += i * b[i]; - } - return sum; - } - - private int[] rotate(int[] a) { - int first = a[0]; - for (int i = 1; i < a.length; i++) { - a[i - 1] = a[i]; - } - a[a.length - 1] = first; - return a; - } - } - - public static class Solution2 { - /** - * Reference: https://discuss.leetcode.com/topic/58459/java-o-n-solution-with-explanation - */ - public int maxRotateFunction(int[] A) { - int allSum = 0; - int len = A.length; - int F = 0; - for (int i = 0; i < len; i++) { - F += i * A[i]; - allSum += A[i]; - } - int max = F; - for (int i = len - 1; i >= 1; i--) { - F = F + allSum - len * A[i]; - max = Math.max(F, max); - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_398.java b/src/main/java/com/fishercoder/solutions/_398.java deleted file mode 100644 index a52b6abd7c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_398.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 398. Random Pick Index - * - * Given an array of integers with possible duplicates, - * randomly output the index of a given target number. You can assume that the given target number must exist in the array. - - Note: - The array size can be very large. Solution that uses too much extra space will not pass the judge. - - Example: - - int[] nums = new int[] {1,2,3,3,3}; - Solution solution = new Solution(nums); - - // pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning. - solution.pick(3); - - // pick(1) should return 0. Since in the array only nums[0] is equal to 1. - solution.pick(1);*/ -public class _398 { - - //TODO: use reservoir sampling to solve it again - - public static class Solution { - //brute force - int[] input; - java.util.Random rand = new java.util.Random(); - - public Solution(int[] nums) { - input = nums; - } - - public int pick(int target) { - List list = new ArrayList(); - for (int i = 0; i < input.length; i++) { - if (input[i] == target) { - list.add(i); - } - } - if (list.size() == 1) { - return list.get(0); - } - int randomIndex = rand.nextInt(list.size()); - return list.get(randomIndex); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_399.java b/src/main/java/com/fishercoder/solutions/_399.java deleted file mode 100644 index 790684296c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_399.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; - -/** - * 399. Evaluate Division - * - * Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). - * Given some queries, return the answers. If the answer does not exist, return -1.0. - - Example: - Given a / b = 2.0, b / c = 3.0. - queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? . - return [6.0, 0.5, -1.0, 1.0, -1.0 ]. - - The input is: vector> equations, vector& values, vector> queries , - where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector. - - According to the example above: - - equations = [ ["a", "b"], ["b", "c"] ], - values = [2.0, 3.0], - queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]. - - The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction. - */ -public class _399 { - - public static class Solution1 { - /** - * Credit: https://discuss.leetcode.com/topic/59146/java-ac-solution-using-graph - * - * Image a/b = k as a link between node a and b, the weight from a to b is k, the reverse link - * is 1/k. Query is to find a path between two nodes. - */ - public double[] calcEquation(String[][] equations, double[] values, String[][] queries) { - Map> pairs = new HashMap<>(); - Map> valuePairs = new HashMap<>(); - for (int i = 0; i < equations.length; i++) { - String[] equation = equations[i]; - if (!pairs.containsKey(equation[0])) { - pairs.put(equation[0], new ArrayList<>()); - valuePairs.put(equation[0], new ArrayList<>()); - } - if (!pairs.containsKey(equation[1])) { - pairs.put(equation[1], new ArrayList<>()); - valuePairs.put(equation[1], new ArrayList<>()); - } - pairs.get(equation[0]).add(equation[1]); - pairs.get(equation[1]).add(equation[0]); - valuePairs.get(equation[0]).add(values[i]); - valuePairs.get(equation[1]).add(1 / values[i]); - } - - double[] result = new double[queries.length]; - for (int i = 0; i < queries.length; i++) { - String[] query = queries[i]; - result[i] = dfs(query[0], query[1], pairs, valuePairs, new HashSet<>(), 1.0); - if (result[i] == 0.0) { - result[i] = -1.0; - } - } - return result; - } - - private double dfs(String start, String end, Map> pairs, - Map> valuePairs, HashSet set, double value) { - if (set.contains(start)) { - return 0.0; - } - if (!pairs.containsKey(start)) { - return 0.0; - } - if (start.equals(end)) { - return value; - } - set.add(start); - - List stringList = pairs.get(start); - List valueList = valuePairs.get(start); - double tmp = 0.0; - for (int i = 0; i < stringList.size(); i++) { - tmp = dfs(stringList.get(i), end, pairs, valuePairs, set, value * valueList.get(i)); - if (tmp != 0.0) { - break; - } - } - set.remove(start); - return tmp; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_40.java b/src/main/java/com/fishercoder/solutions/_40.java deleted file mode 100644 index ac5f0b7c79..0000000000 --- a/src/main/java/com/fishercoder/solutions/_40.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class _40 { - - public static class Solution1 { - public List> combinationSum2(int[] candidates, int target) { - List> result = new ArrayList(); - Arrays.sort(candidates); - backtracking(candidates, 0, result, target, new ArrayList()); - return result; - } - - void backtracking(int[] candidates, int start, List> result, int target, - List curr) { - if (target > 0) { - for (int i = start; i < candidates.length; i++) { - if (candidates[i] > target || (i > start && candidates[i - 1] == candidates[i])) { - continue; - } - curr.add(candidates[i]); - backtracking(candidates, i + 1, result, target - candidates[i], curr); - curr.remove(curr.size() - 1); - } - } else if (target == 0) { - result.add(new ArrayList(curr)); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_400.java b/src/main/java/com/fishercoder/solutions/_400.java deleted file mode 100644 index 8a20bbac4a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_400.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 400. Nth Digit - * - * Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... - - Note: - n is positive and will fit within the range of a 32-bit signed integer (n < 231). - - Example 1: - Input: - 3 - Output: - 3 - - Example 2: - Input: - 11 - Output: - 0 - - Explanation: - The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10. - */ -public class _400 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/59314/java-solution: - * - * 1. find the length of the number where the nth digit is from 2. find the actual number where - * the nth digit is from 3. find the nth digit and return - */ - public int findNthDigit(int n) { - int len = 1; - long count = 9; - int start = 1; - - while (n > len * count) { - n -= len * count; - len += 1; - count *= 10; - start *= 10; - } - - start += (n - 1) / len; - String s = Integer.toString(start); - return Character.getNumericValue(s.charAt((n - 1) % len)); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_401.java b/src/main/java/com/fishercoder/solutions/_401.java deleted file mode 100644 index 78172e876b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_401.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 401. Binary Watch - * - * A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). - * Each LED represents a zero or one, with the least significant bit on the right. - - For example, the above binary watch reads "3:25". - - Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent. - - Example: - - Input: n = 1 - Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"] - Note: - The order of output does not matter. - The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00". - The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02". - */ -public class _401 { - - public static class Solution1 { - public List readBinaryWatch(int num) { - List times = new ArrayList<>(); - for (int h = 0; h < 12; h++) { - for (int m = 0; m < 60; m++) { - if (Integer.bitCount(h * 64 + m) == num) { - times.add(String.format("%d:%02d", h, - m));//%02 means to pad this two-digit decimal number on the left with zeroes - } - } - } - return times; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_402.java b/src/main/java/com/fishercoder/solutions/_402.java deleted file mode 100644 index cac718554b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_402.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 402. Remove K Digits - * - * Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. - - Note: - The length of num is less than 10002 and will be ≥ k. - The given num does not contain any leading zero. - Example 1: - - Input: num = "1432219", k = 3 - Output: "1219" - Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest. - Example 2: - - Input: num = "10200", k = 1 - Output: "200" - Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes. - Example 3: - - Input: num = "10", k = 2 - Output: "0" - Explanation: Remove all the digits from the number and it is left with nothing which is 0. - */ -public class _402 { - public static class Solution1 { - - /** credit: https://discuss.leetcode.com/topic/59412/a-greedy-method-using-stack-o-n-time-and-o-n-space */ - public String removeKdigits(String num, int k) { - int digits = num.length() - k; - char[] stack = new char[num.length()]; - int top = 0; - - for (int i = 0; i < num.length(); i++) { - char c = num.charAt(i); - while (top > 0 && stack[top - 1] > c && k > 0) { - top--; - k--; - } - stack[top++] = c; - } - - int index = 0; - while (index < digits && stack[index] == '0') { - index++; - } - return index == digits ? "0" : new String(stack, index, digits - index); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_403.java b/src/main/java/com/fishercoder/solutions/_403.java deleted file mode 100644 index e3ade6584e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_403.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 403. Frog Jump - * - * A frog is crossing a river. - * The river is divided into x units and at each unit there may or may not exist a stone. - * The frog can jump on a stone, but it must not jump into the water. - - Given a list of stones' positions (in units) in sorted ascending order, - determine if the frog is able to cross the river by landing on the last stone. - Initially, the frog is on the first stone and assume the first jump must be 1 unit. - - If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. - Note that the frog can only jump in the forward direction. - - Note: - - The number of stones is ≥ 2 and is < 1,100. - Each stone's position will be a non-negative integer < 231. - The first stone's position is always 0. - - Example 1: - - [0,1,3,5,6,8,12,17] - - There are a total of 8 stones. - The first stone at the 0th unit, second stone at the 1st unit, - third stone at the 3rd unit, and so on... - The last stone at the 17th unit. - - Return true. The frog can jump to the last stone by jumping - 1 unit to the 2nd stone, then 2 units to the 3rd stone, then - 2 units to the 4th stone, then 3 units to the 6th stone, - 4 units to the 7th stone, and 5 units to the 8th stone. - - - Example 2: - - [0,1,2,3,4,8,9,11] - - Return false. There is no way to jump to the last stone as - the gap between the 5th and 6th stone is too large. - - */ -public class _403 { - - public static class Solution1 { - /** - * Reference: https://discuss.leetcode.com/topic/59903/very-easy-to-understand-java-solution-with-explanations/2 - * and https://leetcode.com/articles/frog-jump/#approach-5-using-dynamic-programmingaccepted - */ - public boolean canCross(int[] stones) { - if (stones.length == 0) { - return true; - } - Map> map = new HashMap<>(stones.length); - map.put(0, new HashSet<>()); - map.get(0).add(1); - for (int i = 1; i < stones.length; i++) { - map.put(stones[i], new HashSet<>()); - } - - for (int i = 0; i < stones.length; i++) { - int stone = stones[i]; - for (int step : map.get(stone)) { - int reach = step + stone; - if (reach == stones[stones.length - 1]) { - return true; - } - Set set = map.get(reach); - if (set != null) { - set.add(step); - if (step - 1 > 0) { - set.add(step - 1); - } - set.add(step + 1); - } - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_404.java b/src/main/java/com/fishercoder/solutions/_404.java deleted file mode 100644 index 7611e4c1dc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_404.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; -/** - * 404. Sum of Left Leaves - * - * Find the sum of all left leaves in a given binary tree. - - Example: - - 3 - / \ - 9 20 - / \ - 15 7 - - There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.*/ -public class _404 { - public static class Solution1 { - public int sumOfLeftLeaves(TreeNode root) { - int result = 0; - if (root == null) { - return result; - } - return dfs(root, result, false); - } - - private int dfs(TreeNode root, int result, boolean left) { - if (root.left == null && root.right == null && left) { - result += root.val; - return result; - } - int leftResult = 0; - if (root.left != null) { - left = true; - leftResult = dfs(root.left, result, left); - } - int rightResult = 0; - if (root.right != null) { - left = false; - rightResult = dfs(root.right, result, left); - } - return leftResult + rightResult; - } - } - - public static class Solution2 { - - public int sumOfLeftLeaves(TreeNode root) { - int sum = 0; - if (root == null) { - return sum; - } - if (root.left != null) { - if (root.left.left == null && root.left.right == null) { - sum += root.left.val; - } else { - sum += sumOfLeftLeaves(root.left); - } - } - if (root.right != null) { - sum += sumOfLeftLeaves(root.right); - } - return sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_405.java b/src/main/java/com/fishercoder/solutions/_405.java deleted file mode 100644 index d1fd3ea260..0000000000 --- a/src/main/java/com/fishercoder/solutions/_405.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 405. Convert a Number to Hexadecimal - - Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used. - - Note: - - All letters in hexadecimal (a-f) must be in lowercase. - The hexadecimal string must not contain extra leading 0s. - If the number is zero, it is represented by a single zero character '0'; - otherwise, the first character in the hexadecimal string will not be the zero character. - The given number is guaranteed to fit within the range of a 32-bit signed integer. - You must not use any method provided by the library which converts/formats the number to hex directly. - - Example 1: - Input: - 26 - - Output: - "1a" - - Example 2: - Input: - -1 - - Output: - "ffffffff" - - */ -public class _405 { - - public static class Solution1 { - public String toHex(int num) { - char[] hexChars = - new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', - 'e', 'f'}; - String result = ""; - while (num != 0) { - result = hexChars[(num & 15)] + result; - num >>>= 4; - } - return result.isEmpty() ? "0" : result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_406.java b/src/main/java/com/fishercoder/solutions/_406.java deleted file mode 100644 index dcf6218212..0000000000 --- a/src/main/java/com/fishercoder/solutions/_406.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.Comparator; -import java.util.LinkedList; -import java.util.List; - -/** - * 406. Queue Reconstruction by Height - * - * Suppose you have a random list of people standing in a queue. - * Each person is described by a pair of integers (h, k), - * where h is the height of the person and k is the number of people in - * front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue. - - Note: - The number of people is less than 1,100. - - Example - - Input: - [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] - - Output: - [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]] - - */ -public class _406 { - - public static class Solution1 { - /** - * Credit: https://discuss.leetcode.com/topic/60437/java-solution-using-priorityqueue-and-linkedlist - */ - public int[][] reconstructQueue(int[][] people) { - Arrays.sort(people, new Comparator() { - public int compare(int[] p1, int[] p2) { - return p1[0] != p2[0] ? Integer.compare(p2[0], p1[0]) - : Integer.compare(p1[1], p2[1]); - } - }); - List list = new LinkedList(); - for (int[] ppl : people) { - list.add(ppl[1], ppl); - } - return list.toArray(new int[people.length][]); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_407.java b/src/main/java/com/fishercoder/solutions/_407.java deleted file mode 100644 index 5c8cd62a83..0000000000 --- a/src/main/java/com/fishercoder/solutions/_407.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.PriorityQueue; - -/** - * 407. Trapping Rain Water II - * - * Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. - - Note: - Both m and n are less than 110. The height of each unit cell is greater than 0 and is less than 20,000. - - Example: - - Given the following 3x6 height map: - [ - [1,4,3,1,3,2], - [3,2,1,3,2,4], - [2,3,3,2,3,1] - ] - - Return 4. - - The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain. - - After the rain, water are trapped between the blocks. The total volume of water trapped is 4. - - */ -public class _407 { - public static class Solution1 { - /** Reference: https://discuss.leetcode.com/topic/60418/java-solution-using-priorityqueue */ - public class Cell { - int row; - int col; - int height; - - public Cell(int row, int col, int height) { - this.row = row; - this.col = col; - this.height = height; - } - } - - public int trapRainWater(int[][] heights) { - if (heights == null || heights.length == 0 || heights[0].length == 0) { - return 0; - } - - PriorityQueue queue = new PriorityQueue<>(1, (a, b) -> a.height - b.height); - - int m = heights.length; - int n = heights[0].length; - boolean[][] visited = new boolean[m][n]; - - // Initially, add all the Cells which are on borders to the queue. - for (int i = 0; i < m; i++) { - visited[i][0] = true; - visited[i][n - 1] = true; - queue.offer(new Cell(i, 0, heights[i][0])); - queue.offer(new Cell(i, n - 1, heights[i][n - 1])); - } - - for (int i = 0; i < n; i++) { - visited[0][i] = true; - visited[m - 1][i] = true; - queue.offer(new Cell(0, i, heights[0][i])); - queue.offer(new Cell(m - 1, i, heights[m - 1][i])); - } - - // from the borders, pick the shortest cell visited and check its neighbors: - // if the neighbor is shorter, collect the water it can trap and update its height as its height plus the water trapped - // add all its neighbors to the queue. - int[][] dirs = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; - int res = 0; - while (!queue.isEmpty()) { - Cell cell = queue.poll(); - for (int[] dir : dirs) { - int row = cell.row + dir[0]; - int col = cell.col + dir[1]; - if (row >= 0 && row < m && col >= 0 && col < n && !visited[row][col]) { - visited[row][col] = true; - res += Math.max(0, cell.height - heights[row][col]); - queue.offer(new Cell(row, col, Math.max(heights[row][col], cell.height))); - } - } - } - - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_408.java b/src/main/java/com/fishercoder/solutions/_408.java deleted file mode 100644 index ec5f15aaf1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_408.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 408. Valid Word Abbreviation - * - * Given a non-empty string s and an abbreviation abbr, - * return whether the string matches with the given abbreviation. - * A string such as "word" contains only the following valid abbreviations: - - ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"] - Notice that only the above abbreviations are valid abbreviations of the string "word". - Any other string is not a valid abbreviation of "word". - - Note: - Assume s contains only lowercase letters and abbr contains only lowercase letters and digits. - - Example 1: - Given s = "internationalization", abbr = "i12iz4n": - - Return true. - Example 2: - Given s = "apple", abbr = "a2e": - - Return false. - */ -public class _408 { - - public static class Solution1 { - public boolean validWordAbbreviation(String word, String abbr) { - if (abbr.length() > word.length()) { - return false; - } else { - char[] abbrChars = abbr.toCharArray(); - char[] wordChars = word.toCharArray(); - if (abbr.length() == word.length()) { - boolean prevDigit = false; - for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++, j++) { - if (Character.isDigit(abbrChars[i]) && !prevDigit) { - prevDigit = true; - if (Character.getNumericValue(abbrChars[i]) != 1) { - return false; - } - } else if (Character.isDigit(abbrChars[i]) && prevDigit) { - return false; - } else if (abbrChars[i] != wordChars[j]) { - return false; - } else if (prevDigit) { - prevDigit = false; - } - } - return true; - } else { - StringBuilder stringBuilder = new StringBuilder(); - boolean firstDigit = true; - for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++) { - while (i < abbrChars.length && Character.isDigit(abbrChars[i])) { - if (firstDigit && Character.getNumericValue(abbrChars[i]) == 0) { - return false; - } - stringBuilder.append(abbrChars[i]); - i++; - firstDigit = false; - } - firstDigit = true; - if (!stringBuilder.toString().isEmpty()) { - int number = Integer.valueOf(stringBuilder.toString()); - j += number; - stringBuilder.setLength(0); - } - if ((i >= abbrChars.length && j < wordChars.length) || (i < abbrChars.length - && j >= wordChars.length)) { - return false; - } - if (i < abbrChars.length - && j < wordChars.length - && abbrChars[i] != wordChars[j]) { - return false; - } - if (j > wordChars.length && i <= abbrChars.length) { - return false; - } - j++; - } - return true; - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_409.java b/src/main/java/com/fishercoder/solutions/_409.java deleted file mode 100644 index 0c843afb64..0000000000 --- a/src/main/java/com/fishercoder/solutions/_409.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 409. Longest Palindrome - * - * Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. - * This is case sensitive, for example "Aa" is not considered a palindrome here. - * - * Note: - * Assume the length of given string will not exceed 1,010. - * - * Example: - * Input: - * "abccccdd" - * - * Output: - * 7 - * - * Explanation: - * One longest palindrome that can be built is "dccaccd", whose length is 7. - */ -public class _409 { - public static class Solution1 { - public int longestPalindrome(String s) { - int[] counts = new int[56]; - for (char c : s.toCharArray()) { - if (Character.isUpperCase(c)) { - counts[c - 'A' + 33]++; - } else { - counts[c - 'a']++; - } - } - boolean hasOdd = false; - int len = 0; - for (int i = 0; i < 56; i++) { - if (counts[i] % 2 != 0) { - hasOdd = true; - if (counts[i] > 1) { - len += counts[i] - 1; - } - } else { - len += counts[i]; - } - } - return hasOdd ? len + 1 : len; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_411.java b/src/main/java/com/fishercoder/solutions/_411.java deleted file mode 100644 index a2b7144565..0000000000 --- a/src/main/java/com/fishercoder/solutions/_411.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 411. Minimum Unique Word Abbreviation - * - * A string such as "word" contains the following abbreviations: - - ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"] - - Given a target string and a set of strings in a dictionary, - find an abbreviation of this target string with the smallest possible length such - that it does not conflict with abbreviations of the strings in the dictionary. - - Each number or letter in the abbreviation is considered length = 1. For example, the abbreviation "a32bc" has length = 4. - - Note: - - In the case of multiple answers as shown in the second example below, you may return any one of them. - Assume length of target string = m, and dictionary size = n. You may assume that m ≤ 21, n ≤ 1000, and log2(n) + m ≤ 20. - - Examples: - - "apple", ["blade"] -> "a4" (because "5" or "4e" conflicts with "blade") - - "apple", ["plain", "amber", "blade"] -> "1p3" (other valid answers include "ap3", "a3e", "2p2", "3le", "3l1"). - * - */ -public class _411 { - public static class Solution1 { - /** Credit: https://discuss.leetcode.com/topic/61346/trie-bruteforce */ - class Trie { - Trie[] children = new Trie[26]; - boolean isWord = false; - } - - Trie root = new Trie(); - List abbrs; - - public String minAbbreviation(String target, String[] dictionary) { - for (String s : dictionary) { - addTrie(s); - } - - for (int i = 0; i < target.length(); i++) { - abbrs = new ArrayList<>(); - abbrGenerator(target, 0, "", 0, i + 1); - for (String s : abbrs) { - if (search(s, root, 0, 0) == false) { - return s; - } - } - } - return ""; - } - - public void addTrie(String s) { - Trie cur = root; - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (cur.children[c - 'a'] == null) { - cur.children[c - 'a'] = new Trie(); - } - cur = cur.children[c - 'a']; - } - cur.isWord = true; - } - - public boolean search(String target, Trie root, int i, int loop) { - if (root == null) { - return false; - } - - if (loop != 0) { - for (int a = 0; a < 26; a++) { - if (search(target, root.children[a], i, loop - 1)) { - return true; - } - } - return false; - } - if (i == target.length()) { - if (root.isWord) { - return true; - } - return false; - } - if (Character.isDigit(target.charAt(i))) { - int tmp = 0; - while (i < target.length() && Character.isDigit(target.charAt(i))) { - tmp = tmp * 10 + target.charAt(i) - '0'; - i++; - } - return search(target, root, i, tmp); - } else { - return search(target, root.children[target.charAt(i) - 'a'], i + 1, 0); - } - } - - public void abbrGenerator(String target, int i, String tmp, int abbr, int num) { - if (i == target.length()) { - if (num == 0 && abbr == 0) { - abbrs.add(tmp); - } - if (num == 1 && abbr != 0) { - abbrs.add(tmp + abbr); - } - return; - } - if (num <= 0) { - return; - } - char cur = target.charAt(i); - abbrGenerator(target, i + 1, abbr == 0 ? tmp + cur : tmp + abbr + cur, 0, - abbr == 0 ? num - 1 : num - 2); - abbrGenerator(target, i + 1, tmp, abbr + 1, num); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_412.java b/src/main/java/com/fishercoder/solutions/_412.java deleted file mode 100644 index b4cf753507..0000000000 --- a/src/main/java/com/fishercoder/solutions/_412.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 412. Fizz Buzz - * - * Write a program that outputs the string representation of numbers from 1 to n. - - But for multiples of three it should output “Fizz” instead of the number and for - the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “_412”. - - Example: - - n = 15, - - Return: - [ - "1", - "2", - "Fizz", - "4", - "Buzz", - "Fizz", - "7", - "8", - "Fizz", - "Buzz", - "11", - "Fizz", - "13", - "14", - "_412" - ]*/ -public class _412 { - - public static class Solution1 { - public List fizzBuzz(int n) { - List result = new ArrayList(); - for (int i = 1; i <= n; i++) { - if (i % 3 == 0 && i % 5 == 0) { - result.add("_412"); - } else if (i % 3 == 0) { - result.add("Fizz"); - } else if (i % 5 == 0) { - result.add("Buzz"); - } else { - result.add(Integer.toString(i)); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_413.java b/src/main/java/com/fishercoder/solutions/_413.java deleted file mode 100644 index 6451bfffed..0000000000 --- a/src/main/java/com/fishercoder/solutions/_413.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 413. Arithmetic Slices - * - * A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. - - For example, these are arithmetic sequence: - - 1, 3, 5, 7, 9 - 7, 7, 7, 7 - 3, -1, -5, -9 - - The following sequence is not arithmetic. - - 1, 1, 2, 5, 7 - - A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N. - - A slice (P, Q) of array A is called arithmetic if the sequence: - A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. - - The function should return the number of arithmetic slices in the array A. - - Example: - - A = [1, 2, 3, 4] - - return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself. - */ -public class _413 { - - public static class Solution1 { - //credit: https://discuss.leetcode.com/topic/62884/2ms-java-o-n-time-o-1-space-solution - public int numberOfArithmeticSlices(int[] A) { - int sum = 0; - int len = 2; - for (int i = 2; i < A.length; i++) { - if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) { - len++; - } else { - if (len > 2) { - sum += calculateSlices(len); - } - len = 2;//reset it to 2 - } - } - if (len > 2) { - sum += calculateSlices(len); - } - return sum; - } - - int calculateSlices(int len) { - return (len - 1) * (len - 2) / 2; - } - } - - class Solution2 { - //credit: https://discuss.leetcode.com/topic/63302/simple-java-solution-9-lines-2ms - public int numberOfArithmeticSlices(int[] A) { - int sum = 0; - int curr = 0; - for (int i = 2; i < A.length; i++) { - if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) { - curr++; - sum += curr; - } else { - curr = 0; - } - } - return sum; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_414.java b/src/main/java/com/fishercoder/solutions/_414.java deleted file mode 100644 index 9bed637e8d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_414.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 414. Third Maximum Number - * - * Given a non-empty array of integers, return the third maximum number in this array. - * If it does not exist, return the maximum number. The time complexity must be in O(n). - - Example 1: - Input: [3, 2, 1] - - Output: 1 - - Explanation: The third maximum is 1. - Example 2: - Input: [1, 2] - - Output: 2 - - Explanation: The third maximum does not exist, so the maximum (2) is returned instead. - Example 3: - Input: [2, 2, 3, 1] - - Output: 1 - - Explanation: Note that the third maximum here means the third maximum distinct number. - Both numbers with value 2 are both considered as second maximum. - */ -public class _414 { - - public static class Solution1 { - public int thirdMax(int[] nums) { - long max1 = Long.MIN_VALUE; - long max2 = Long.MIN_VALUE; - long max3 = Long.MIN_VALUE; - for (int i : nums) { - max1 = Math.max(max1, i); - } - for (int i : nums) { - if (i == max1) { - continue; - } - max2 = Math.max(max2, i); - } - for (int i : nums) { - if (i == max1 || i == max2) { - continue; - } - max3 = Math.max(max3, i); - } - return (int) (max3 == Long.MIN_VALUE ? max1 : max3); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_415.java b/src/main/java/com/fishercoder/solutions/_415.java deleted file mode 100644 index 2069230d92..0000000000 --- a/src/main/java/com/fishercoder/solutions/_415.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 415. Add Strings - * - * Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. - - Note: - - 1. The length of both num1 and num2 is < 5100. - 2. Both num1 and num2 contains only digits 0-9. - 3. Both num1 and num2 does not contain any leading zero. - 4. You must not use any built-in BigInteger library or convert the inputs to integer directly. - */ -public class _415 { - - public static class Solution1 { - public String addStrings(String num1, String num2) { - if (num1 == null || num1.length() == 0) { - return num2; - } else if (num2 == null || num2.length() == 0) { - return num1; - } - - int i = num1.length() - 1; - int j = num2.length() - 1; - long carry = 0; - long sum = 0; - StringBuilder sb = new StringBuilder(); - char[] char1 = num1.toCharArray(); - char[] char2 = num2.toCharArray(); - while (i >= 0 || j >= 0) { - sum = carry; - if (i >= 0) { - sum += Character.getNumericValue(char1[i--]); - } - if (j >= 0) { - sum += Character.getNumericValue(char2[j--]); - } - carry = sum / 10; - sb.append(sum % 10); - } - if (carry != 0) { - sb.append(carry); - } - - return sb.reverse().toString(); - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_416.java b/src/main/java/com/fishercoder/solutions/_416.java deleted file mode 100644 index 683b32ae51..0000000000 --- a/src/main/java/com/fishercoder/solutions/_416.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 416. Partition Equal Subset Sum - * - * Given a non-empty array containing only positive integers, - * find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. - - Note: - - Each of the array element will not exceed 100. - The array size will not exceed 200. - - Example 1: - - Input: [1, 5, 11, 5] - - Output: true - - Explanation: The array can be partitioned as [1, 5, 5] and [11]. - - - Example 2: - - Input: [1, 2, 3, 5] - - Output: false - - Explanation: The array cannot be partitioned into equal sum subsets. - */ -public class _416 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/67539/0-1-knapsack-detailed-explanation - */ - public boolean canPartition(int[] nums) { - int sum = 0; - for (int num : nums) { - sum += num; - } - - if ((sum & 1) == 1) { - return false; - } - - sum /= 2; - - int n = nums.length; - boolean[][] dp = new boolean[n + 1][sum + 1]; - for (int i = 0; i < dp.length; i++) { - Arrays.fill(dp[i], false); - } - - dp[0][0] = true; - - for (int i = 1; i < n + 1; i++) { - dp[i][0] = true; - } - - for (int j = 1; j < sum + 1; j++) { - dp[0][j] = false; - } - - for (int i = 1; i < n + 1; i++) { - for (int j = 1; j < sum + 1; j++) { - dp[i][j] = dp[i - 1][j]; - if (j >= nums[i - 1]) { - dp[i][j] = (dp[i][j] || dp[i - 1][j - nums[i - 1]]); - } - } - } - - return dp[n][sum]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_417.java b/src/main/java/com/fishercoder/solutions/_417.java deleted file mode 100644 index f6ab5dd6bf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_417.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; -/** - * 417. Pacific Atlantic Water Flow - * - * Given the following 5x5 matrix: - - Pacific ~ ~ ~ ~ ~ - ~ 1 2 2 3 (5) * - ~ 3 2 3 (4) (4) * - ~ 2 4 (5) 3 1 * - ~ (6) (7) 1 4 5 * - ~ (5) 1 1 2 4 * - * * * * * Atlantic - -Return: - -[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).*/ - -public class _417 { - public static class Solution1 { - /** - * Credit: looked at this post: https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean - *

- * One typical trick to work on 2d grid problems is to go through the border and put proper ones into a queue if using BFS. - */ - public List pacificAtlantic(int[][] matrix) { - - List result = new ArrayList(); - if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { - return result; - } - - int m = matrix.length; - int n = matrix[0].length; - boolean[][] pacific = new boolean[m][n]; - boolean[][] atlantic = new boolean[m][n]; - - for (int i = 0; i < m; i++) { - dfs(matrix, pacific, Integer.MIN_VALUE, i, 0); - dfs(matrix, atlantic, Integer.MIN_VALUE, i, n - 1); - } - - for (int i = 0; i < n; i++) { - dfs(matrix, pacific, Integer.MIN_VALUE, 0, i); - dfs(matrix, atlantic, Integer.MIN_VALUE, m - 1, i); - } - - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (pacific[i][j] && atlantic[i][j]) { - result.add(new int[]{i, j}); - } - } - } - - return result; - } - - void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y) { - int m = matrix.length; - int n = matrix[0].length; - if (x < 0 || y < 0 || x >= m || y >= n || matrix[x][y] < height || visited[x][y]) { - return; - } - visited[x][y] = true; - dfs(matrix, visited, matrix[x][y], x + 1, y); - dfs(matrix, visited, matrix[x][y], x, y + 1); - dfs(matrix, visited, matrix[x][y], x - 1, y); - dfs(matrix, visited, matrix[x][y], x, y - 1); - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_418.java b/src/main/java/com/fishercoder/solutions/_418.java deleted file mode 100644 index 99c80aedeb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_418.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 418. Sentence Screen Fitting - * - * Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen. - - Note: - - A word cannot be split into two lines. - The order of words in the sentence must remain unchanged. - Two consecutive words in a line must be separated by a single space. - Total words in the sentence won't exceed 100. - Length of each word is greater than 0 and won't exceed 10. - 1 ≤ rows, cols ≤ 20,000. - - Example 1: - - Input: - rows = 2, cols = 8, sentence = ["hello", "world"] - - Output: - 1 - - Explanation: - hello--- - world--- - - The character '-' signifies an empty space on the screen. - - Example 2: - - Input: - rows = 3, cols = 6, sentence = ["a", "bcd", "e"] - - Output: - 2 - - Explanation: - a-bcd- - e-a--- - bcd-e- - - The character '-' signifies an empty space on the screen. - - Example 3: - - Input: - rows = 4, cols = 5, sentence = ["I", "had", "apple", "pie"] - - Output: - 1 - - Explanation: - I-had - apple - pie-I - had-- - - The character '-' signifies an empty space on the screen. - - */ -public class _418 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/62455/21ms-18-lines-java-solution - *

- * 1. String s = String.join(" ", sentence) + " " ;. This line gives us a formatted sentence to be put to our screen. - * 2. start is the counter for how many valid characters from s have been put to our screen. - * 3. if (s.charAt(start % l) == ' ') is the situation that we don't need an extra space for current row. The current row could be successfully fitted. So that we need to increase our counter by using start++. - * 4. The else is the situation, which the next word can't fit to current row. So that we need to remove extra characters from next word. - * 5. start / s.length() is (# of valid characters) / our formatted sentence. - */ - public int wordsTyping(String[] sentence, int rows, int cols) { - String s = String.join(" ", sentence) + " "; - int start = 0; - int l = s.length(); - for (int i = 0; i < rows; i++) { - start += cols; - if (s.charAt(start % l) == ' ') { - start++; - } else { - while (start > 0 && s.charAt((start - 1) % l) != ' ') { - start--; - } - } - } - return start / s.length(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_419.java b/src/main/java/com/fishercoder/solutions/_419.java deleted file mode 100644 index b10fd3e299..0000000000 --- a/src/main/java/com/fishercoder/solutions/_419.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 419. Battleships in a Board - * - * Given an 2D board, count how many battleships are in it. - * The battleships are represented with 'X's, empty slots are represented with '.'s. - * You may assume the following rules: - - You receive a valid board, made of only battleships or empty slots. - Battleships can only be placed horizontally or vertically. - In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size. - At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships. - - Example: - - X..X - ...X - ...X - - In the above board there are 2 battleships. - - Invalid Example: - - ...X - XXXX - ...X - - This is an invalid board that you will not receive - as battleships will always have a cell separating between them. - - Follow up: - Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board? - - */ -public class _419 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/62970/simple-java-solution, - *

- * This solution does NOT modify original input. - * Basically, it only counts the top-left one while ignoring all other parts of one battleship, - * using the top-left one as a representative for one battle. - * This is achieved by counting cells that don't have 'X' to the left and above them. - */ - public int countBattleships(char[][] board) { - if (board == null || board.length == 0) { - return 0; - } - int count = 0; - int m = board.length; - int n = board[0].length; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (board[i][j] == '.') { - continue;//if it can pass this line, then board[i][j] must be 'X' - } - if (j > 0 && board[i][j - 1] == 'X') { - continue;//then we check if its left is 'X' - } - if (i > 0 && board[i - 1][j] == 'X') { - continue;//also check if its top is 'X' - } - count++; - } - } - return count; - } - } - - public static class Solution2 { - /** - * My original solution, actually modified the input. I just undo it at the end. - */ - public int countBattleships(char[][] board) { - if (board == null || board.length == 0) { - return 0; - } - int result = 0; - int m = board.length; - int n = board[0].length; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (board[i][j] == 'X') { - result++; - dfs(board, i, j, m, n); - } - } - } - - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (board[i][j] == '#') { - board[i][j] = 'X'; - } - } - } - return result; - } - - private void dfs(char[][] board, int x, int y, int m, int n) { - if (x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'X') { - return; - } - if (board[x][y] == 'X') { - board[x][y] = '#'; - } - dfs(board, x + 1, y, m, n); - dfs(board, x, y + 1, m, n); - dfs(board, x - 1, y, m, n); - dfs(board, x, y - 1, m, n); - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_420.java b/src/main/java/com/fishercoder/solutions/_420.java deleted file mode 100644 index e617d8ba8e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_420.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 420. Strong Password Checker - * - * A password is considered strong if below conditions are all met: - - It has at least 6 characters and at most 20 characters. - It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit. - It must NOT contain three repeating characters in a row ("...aaa..." is weak, but "...aa...a..." is strong, assuming other conditions are met). - Write a function strongPasswordChecker(s), that takes a string s as input, and return the MINIMUM - change required to make s a strong password. If s is already strong, return 0. - - Insertion, deletion or replace of any one character are all considered as one change. - */ -public class _420 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/63854/o-n-java-solution-by-analyzing-changes-allowed-to-fix-each-condition - */ - public int strongPasswordChecker(String s) { - int res = 0; - int a = 1; - int A = 1; - int d = 1; - char[] carr = s.toCharArray(); - int[] arr = new int[carr.length]; - - for (int i = 0; i < arr.length; ) { - if (Character.isLowerCase(carr[i])) { - a = 0; - } - if (Character.isUpperCase(carr[i])) { - A = 0; - } - if (Character.isDigit(carr[i])) { - d = 0; - } - - int j = i; - while (i < carr.length && carr[i] == carr[j]) { - i++; - } - arr[j] = i - j; - } - - int totalMissing = (a + A + d); - - if (arr.length < 6) { - res += totalMissing + Math.max(0, 6 - (arr.length + totalMissing)); - } else { - int overLen = Math.max(arr.length - 20, 0); - int leftOver = 0; - res += overLen; - - for (int k = 1; k < 3; k++) { - for (int i = 0; i < arr.length && overLen > 0; i++) { - if (arr[i] < 3 || arr[i] % 3 != (k - 1)) { - continue; - } - arr[i] -= Math.min(overLen, k); - overLen -= k; - } - } - - for (int i = 0; i < arr.length; i++) { - if (arr[i] >= 3 && overLen > 0) { - int need = arr[i] - 2; - arr[i] -= overLen; - overLen -= need; - } - - if (arr[i] >= 3) { - leftOver += arr[i] / 3; - } - } - - res += Math.max(totalMissing, leftOver); - } - - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_421.java b/src/main/java/com/fishercoder/solutions/_421.java deleted file mode 100644 index 5a9aa4bd6a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_421.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. - - Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. - - Could you do this in O(n) runtime? - - Example: - - Input: [3, 10, 5, 25, 2, 8] - - Output: 28 - - Explanation: The maximum result is 5 ^ 25 = 28. - */ -public class _421 { - - public static class Solution1 { - //credit: https://discuss.leetcode.com/topic/63213/java-o-n-solution-using-bit-manipulation-and-hashmap/7 - public int findMaximumXOR(int[] nums) { - int max = 0; - int mask = 0; - for (int i = 31; i >= 0; i--) { - mask |= (1 << i);//the mask will grow like this: 100...000, 110...000, 111...000 to 111...111, each time, we only get the most left part of all numbers in the given array - System.out.println("mask = " + Integer.toBinaryString(mask)); - Set set = new HashSet<>(); - for (int num : nums) { - System.out.println("num = " + Integer.toBinaryString(num)); - set.add(num & mask); - System.out.println("mask & num = " + Integer.toBinaryString(mask & num)); - } - - int candidate = max | (1 << i); - System.out.println("candidate = " + Integer.toBinaryString(candidate)); - /**Reason behind this: if a ^ prefix = candidate, then a ^ candidate = prefix, also prefix ^ candidate = a - * in this below code: we use this one: prefix ^ candidate = a*/ - for (int prefix : set) { - System.out.println("candidate ^ prefix = " + Integer.toBinaryString(candidate ^ prefix)); - if (set.contains(candidate ^ prefix)) { - max = candidate; - } - } - System.out.println("max = " + max); - System.out.println("i = " + i); - System.out.println("==============================================="); - } - return max; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_422.java b/src/main/java/com/fishercoder/solutions/_422.java deleted file mode 100644 index 348daa7396..0000000000 --- a/src/main/java/com/fishercoder/solutions/_422.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.List; - -/** - * 422. Valid Word Square - * - * Given a sequence of words, check whether it forms a valid word square. - - A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns). - - Note: - The number of words given is at least 1 and does not exceed 500. - Word length will be at least 1 and does not exceed 500. - Each word contains only lowercase English alphabet a-z. - Example 1: - - Input: - [ - "abcd", - "bnrt", - "crmy", - "dtye" - ] - - Output: - true - - Explanation: - The first row and first column both read "abcd". - The second row and second column both read "bnrt". - The third row and third column both read "crmy". - The fourth row and fourth column both read "dtye". - - Therefore, it is a valid word square. - Example 2: - - Input: - [ - "abcd", - "bnrt", - "crm", - "dt" - ] - - Output: - true - - Explanation: - The first row and first column both read "abcd". - The second row and second column both read "bnrt". - The third row and third column both read "crm". - The fourth row and fourth column both read "dt". - - Therefore, it is a valid word square. - Example 3: - - Input: - [ - "ball", - "area", - "read", - "lady" - ] - - Output: - false - - Explanation: - The third row reads "read" while the third column reads "lead". - - Therefore, it is NOT a valid word square. - */ -public class _422 { - - public static class Solution1 { - public boolean validWordSquare(List words) { - for (int i = 0; i < words.size(); i++) { - String word = words.get(i); - for (int j = 0; j < word.length(); j++) { - if (j >= words.size()) { - return false; - } - if (i >= words.get(j).length()) { - return false; - } - if (word.charAt(j) != words.get(j).charAt(i)) { - return false; - } - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_423.java b/src/main/java/com/fishercoder/solutions/_423.java deleted file mode 100644 index d8efe78f9c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_423.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 423. Reconstruct Original Digits from English - * - * Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order. - - Note: - Input contains only lowercase English letters. - Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted. - Input length is less than 50,000. - - Example 1: - Input: "owoztneoer" - - Output: "012" - Example 2: - Input: "fviefuro" - - Output: "45" - - */ -public class _423 { - - public static class Solution1 { - public String originalDigits(String s) { - /**we can use one char as a representative to uniquely stand for one number, - * for some numbers that we cannot find a unique representive, we can dedup. - * e.g. for number one, if we use 'o' as its representive, then 'o' also exists in numbers 2, 4 and 0, so - * we need to dedupe the 'o' in those numbers. - * Also, the order to dedupe matters: - * we'll have to dedupe for counts[3], counts[5], counts[7] first before we dedupe counts[1] and counts[9].*/ - int[] counts = new int[10]; - for (int i = 0; i < s.length(); i++) { - if (s.charAt(i) == 'o') { - counts[1]++;//2,4,0 - } - if (s.charAt(i) == 'w') { - counts[2]++; - } - if (s.charAt(i) == 'h') { - counts[3]++;//8 - } - if (s.charAt(i) == 'u') { - counts[4]++; - } - if (s.charAt(i) == 'f') { - counts[5]++;//4 - } - if (s.charAt(i) == 'x') { - counts[6]++; - } - if (s.charAt(i) == 'v') { - counts[7]++;//5 - } - if (s.charAt(i) == 'g') { - counts[8]++; - } - if (s.charAt(i) == 'i') { - counts[9]++;//5,6,8 - } - if (s.charAt(i) == 'z') { - counts[0]++; - } - } - - counts[3] -= counts[8]; - counts[5] -= counts[4]; - counts[7] -= counts[5]; - counts[1] = counts[1] - (counts[2] + counts[4] + counts[0]); - counts[9] = counts[9] - (counts[5] + counts[6] + counts[8]); - - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < 10; i++) { - for (int j = 0; j < counts[i]; j++) { - stringBuilder.append(i); - } - } - return stringBuilder.toString(); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_424.java b/src/main/java/com/fishercoder/solutions/_424.java deleted file mode 100644 index 2cdf2fa365..0000000000 --- a/src/main/java/com/fishercoder/solutions/_424.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 424. Longest Repeating Character Replacement - * - * Given a string that consists of only uppercase English letters, - * you can replace any letter in the string with another letter at most k times. - * Find the length of a longest substring containing all repeating letters you can get after performing the above operations. - - Note: - Both the string's length and k will not exceed 104. - - Example 1: - - Input: - s = "ABAB", k = 2 - - Output: - 4 - - Explanation: - Replace the two 'A's with two 'B's or vice versa. - Example 2: - - Input: - s = "AABABBA", k = 1 - - Output: - 4 - - Explanation: - Replace the one 'A' in the middle with 'B' and form "AABBBBA". - The substring "BBBB" has the longest repeating letters, which is 4. - */ -public class _424 { - - public static class Solution1 { - //credit: https://discuss.leetcode.com/topic/63494/java-12-lines-o-n-sliding-window-solution-with-explanation - public int characterReplacement(String s, int k) { - int len = s.length(); - int[] count = new int[26]; - int start = 0; - int maxCount = 0; - int maxLength = 0; - for (int end = 0; end < len; end++) { - maxCount = Math.max(maxCount, ++count[s.charAt(end) - 'A']); - while (end - start + 1 - maxCount > k) { - count[s.charAt(start) - 'A']--; - start++; - } - maxLength = Math.max(maxLength, end - start + 1); - } - return maxLength; - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_425.java b/src/main/java/com/fishercoder/solutions/_425.java deleted file mode 100644 index 88b5bf441a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_425.java +++ /dev/null @@ -1,162 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 425. Word Squares - * - * Given a set of words (without duplicates), find all word squares you can build from them. - - A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns). - - For example, the word sequence ["ball","area","lead","lady"] forms a word square because each word reads the same both horizontally and vertically. - - b a l l - a r e a - l e a d - l a d y - - Note: - - There are at least 1 and at most 1000 words. - All words will have the exact same length. - Word length is at least 1 and at most 5. - Each word contains only lowercase English alphabet a-z. - - Example 1: - - Input: - ["area","lead","wall","lady","ball"] - - Output: - [ - [ "wall", - "area", - "lead", - "lady" - ], - [ "ball", - "area", - "lead", - "lady" - ] - ] - - Explanation: - The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters). - - - Example 2: - - Input: - ["abat","baba","atan","atal"] - - Output: - [ - [ "baba", - "abat", - "baba", - "atan" - ], - [ "baba", - "abat", - "baba", - "atal" - ] - ] - - Explanation: - The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters). - */ -public class _425 { - - public static class Solution1 { - /** - * Credit: https://discuss.leetcode.com/topic/63516/explained-my-java-solution-using-trie-126ms-16-16/2 - */ - - class TrieNode { - List startWith; - TrieNode[] children; - - TrieNode() { - startWith = new ArrayList<>(); - children = new TrieNode[26]; - } - } - - class Trie { - TrieNode root; - - Trie(String[] words) { - root = new TrieNode(); - for (String word : words) { - TrieNode cur = root; - for (char ch : word.toCharArray()) { - int index = ch - 'a'; - if (cur.children[index] == null) { - cur.children[index] = new TrieNode(); - } - cur.children[index].startWith.add(word); - cur = cur.children[index]; - } - } - } - - List findByPrefix(String prefix) { - List ans = new ArrayList<>(); - TrieNode cur = root; - for (char ch : prefix.toCharArray()) { - int index = ch - 'a'; - if (cur.children[index] == null) { - return ans; - } - - cur = cur.children[index]; - } - ans.addAll(cur.startWith); - return ans; - } - } - - public List> wordSquares(String[] words) { - List> ans = new ArrayList<>(); - if (words == null || words.length == 0) { - return ans; - } - int len = words[0].length(); - Trie trie = new Trie(words); - List ansBuilder = new ArrayList<>(); - for (String w : words) { - ansBuilder.add(w); - search(len, trie, ans, ansBuilder); - ansBuilder.remove(ansBuilder.size() - 1); - } - - return ans; - } - - private void search(int len, Trie trie, List> ans, - List ansBuilder) { - if (ansBuilder.size() == len) { - ans.add(new ArrayList<>(ansBuilder)); - return; - } - - int idx = ansBuilder.size(); - StringBuilder prefixBuilder = new StringBuilder(); - for (String s : ansBuilder) { - prefixBuilder.append(s.charAt(idx)); - } - List startWith = trie.findByPrefix(prefixBuilder.toString()); - for (String sw : startWith) { - ansBuilder.add(sw); - search(len, trie, ans, ansBuilder); - ansBuilder.remove(ansBuilder.size() - 1); - } - } - } - -} - diff --git a/src/main/java/com/fishercoder/solutions/_429.java b/src/main/java/com/fishercoder/solutions/_429.java deleted file mode 100644 index 6328848bef..0000000000 --- a/src/main/java/com/fishercoder/solutions/_429.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Node; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -/** - * 429. N-ary Tree Level Order Traversal - * - * Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). - * - * For example, given a 3-ary tree: - * - * 1 - * / | \ - * 3 2 4 - * / \ - * 5 6 - * - * We should return its level order traversal: - * - * [ - * [1], - * [3,2,4], - * [5,6] - * ] - * - * - * Note: - * - * The depth of the tree is at most 1000. - * The total number of nodes is at most 5000. - */ -public class _429 { - public static class Solution1 { - public List> levelOrder(Node root) { - List> result = new ArrayList<>(); - if (root == null) { - return result; - } - Queue queue = new LinkedList<>(); - queue.offer(root); - while (!queue.isEmpty()) { - int size = queue.size(); - List level = new ArrayList<>(); - for (int i = 0; i < size; i++) { - Node currentNode = queue.poll(); - if (currentNode != null) { - level.add(currentNode.val); - for (Node child : currentNode.children) { - queue.offer(child); - } - } - } - result.add(level); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_43.java b/src/main/java/com/fishercoder/solutions/_43.java deleted file mode 100644 index d5e9ea4376..0000000000 --- a/src/main/java/com/fishercoder/solutions/_43.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -public class _43 { - - /**Inspired by https://discuss.leetcode.com/topic/30508/easiest-java-solution-with-graph-explanation - * Basically, the rule we can find is that products of each two digits will land in this position in the final product: - * i+j and i+j+1*/ - public String multiply(String num1, String num2) { - if (isZero(num1) || isZero(num2)) { - return "0"; - } - int[] a1 = new int[num1.length()]; - int[] a2 = new int[num2.length()]; - int[] product = new int[num1.length() + num2.length()]; - - for (int i = a1.length - 1; i >= 0; i--) { - for (int j = a2.length - 1; j >= 0; j--) { - int thisProduct = Character.getNumericValue(num1.charAt(i)) * Character.getNumericValue(num2.charAt(j)); - product[i + j + 1] += thisProduct % 10; - if (product[i + j + 1] >= 10) { - product[i + j + 1] %= 10; - product[i + j]++; - } - product[i + j] += thisProduct / 10; - if (product[i + j] >= 10) { - product[i + j] %= 10; - product[i + j - 1]++; - } - } - } - - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < product.length; i++) { - if (i == 0 && product[i] == 0) { - continue; - } - stringBuilder.append(product[i]); - } - return stringBuilder.toString(); - } - - private boolean isZero(String num) { - for (char c : num.toCharArray()) { - if (c != '0') { - return false; - } - } - return true; - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_434.java b/src/main/java/com/fishercoder/solutions/_434.java deleted file mode 100644 index feede74ac8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_434.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder.solutions; -/** - * 434. Number of Segments in a String - * - * Count the number of segments in a string, - * where a segment is defined to be a contiguous sequence of non-space characters. - * - * Please note that the string does not contain any non-printable characters. - -Example: - -Input: "Hello, my name is John" -Output: 5*/ -public class _434 { - - public static class Solution1 { - public int countSegments(String s) { - if (s == null || s.isEmpty()) { - return 0; - } - String[] segments = s.split(" "); - int count = 0; - for (String seg : segments) { - if (seg.equals("")) { - continue; - } - count++; - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_435.java b/src/main/java/com/fishercoder/solutions/_435.java deleted file mode 100644 index 24bbe502e2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_435.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Interval; - -import java.util.Arrays; -import java.util.Collections; - -/** - * 435. Non-overlapping Intervals - * - * Given a collection of intervals, - * find the minimum number of intervals you need to remove to make the rest of the - * intervals non-overlapping. - - Note: - You may assume the interval's end point is always bigger than its start point. - Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other. - - Example 1: - Input: [ [1,2], [2,3], [3,4], [1,3] ] - Output: 1 - Explanation: [1,3] can be removed and the rest of intervals are non-overlapping. - - Example 2: - Input: [ [1,2], [1,2], [1,2] ] - Output: 2 - Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping. - - Example 3: - Input: [ [1,2], [2,3] ] - Output: 0 - Explanation: You don't need to remove any of the intervals since they're already non-overlapping. - */ - -public class _435 { - - public static class Solution1 { - /** - * credit:: https://discuss.leetcode.com/topic/65828/java-solution-with-clear-explain - * and https://discuss.leetcode.com/topic/65594/java-least-is-most - * Sort the intervals by their end time, if equal, then sort by their start time. - */ - public int eraseOverlapIntervals(Interval[] intervals) { - Collections.sort(Arrays.asList(intervals), (o1, o2) -> { - if (o1.end != o2.end) { - return o1.end - o2.end; - } else { - return o2.start - o1.start; - } - }); - int end = Integer.MIN_VALUE; - int count = 0; - for (Interval interval : intervals) { - if (interval.start >= end) { - end = interval.end; - } else { - count++; - } - } - return count; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_436.java b/src/main/java/com/fishercoder/solutions/_436.java deleted file mode 100644 index 848e802d26..0000000000 --- a/src/main/java/com/fishercoder/solutions/_436.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Interval; - -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -/**436. Find Right Interval - * - * Given a set of intervals, for each of the interval i, check if there exists an interval j whose start - * point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i. - * - * For any interval i, you need to store the minimum interval j's index, - * which means that the interval j has the minimum start point to build the "right" relationship for interval i. - * If the interval j doesn't exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array. - * - * Note: - * - * You may assume the interval's end point is always bigger than its start point. - * You may assume none of these intervals have the same start point. - * - * Example 1: - * Input: [ [1,2] ] - * Output: [-1] - * Explanation: There is only one interval in the collection, so it outputs -1. - * - * Example 2: - * Input: [ [3,4], [2,3], [1,2] ] - * Output: [-1, 0, 1] - * Explanation: There is no satisfied "right" interval for [3,4]. - * For [2,3], the interval [3,4] has minimum-"right" start point; - * For [1,2], the interval [2,3] has minimum-"right" start point. - * - * Example 3: - * Input: [ [1,4], [2,3], [3,4] ] - * Output: [-1, 2, -1] - * Explanation: There is no satisfied "right" interval for [1,4] and [3,4]. - * For [2,3], the interval [3,4] has minimum-"right" start point. - * */ - -public class _436 { - - public static class Solution1 { - public int[] findRightInterval(Interval[] intervals) { - if (intervals == null || intervals.length == 0) { - return new int[0]; - } - int[] result = new int[intervals.length]; - result[0] = -1; - Interval last = intervals[intervals.length - 1]; - Interval first = intervals[0]; - Map map = new HashMap(); - for (int i = 0; i < intervals.length; i++) { - map.put(intervals[i], i); - } - - Collections.sort(Arrays.asList(intervals), (o1, o2) -> o1.start - o2.start); - - for (int i = 1; i < intervals.length; i++) { - //TODO: use binary search for the minimum start interval for interval[i-1] instead of a while loop - int tmp = i - 1; - int tmpI = i; - while (tmpI < intervals.length && intervals[tmpI].start < intervals[tmp].end) { - tmpI++; - } - if (tmpI < intervals.length) { - result[map.get(intervals[tmp])] = map.get(intervals[tmpI]); - } else { - result[map.get(intervals[tmp])] = -1; - } - } - if (result[intervals.length - 1] == 0 && last.end > first.start) { - result[intervals.length - 1] = -1; - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_437.java b/src/main/java/com/fishercoder/solutions/_437.java deleted file mode 100644 index b97aee6c55..0000000000 --- a/src/main/java/com/fishercoder/solutions/_437.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 437. Path Sum III - * - * You are given a binary tree in which each node contains an integer value. - - Find the number of paths that sum to a given value. - - The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes). - - The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000. - - Example: - - root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 - - 10 - / \ - 5 -3 - / \ \ - 3 2 11 - / \ \ -3 -2 1 - - Return 3. The paths that sum to 8 are: - - 1. 5 -> 3 - 2. 5 -> 2 -> 1 - 3. -3 -> 11 - */ - -public class _437 { - - public static class Solution1 { - public int pathSum(TreeNode root, int sum) { - if (root == null) { - return 0; - } - return pathSumFrom(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); - } - - private int pathSumFrom(TreeNode root, int sum) { - if (root == null) { - return 0; - } - return (root.val == sum ? 1 : 0) + pathSumFrom(root.left, sum - root.val) + pathSumFrom(root.right, sum - root.val); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_438.java b/src/main/java/com/fishercoder/solutions/_438.java deleted file mode 100644 index 31ef0c5f71..0000000000 --- a/src/main/java/com/fishercoder/solutions/_438.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 438. Find All Anagrams in a String - * - * Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. - Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. - - The order of output does not matter. - - Example 1: - Input: - s: "cbaebabacd" p: "abc" - Output: - [0, 6] - - Explanation: - The substring with start index = 0 is "cba", which is an anagram of "abc". - The substring with start index = 6 is "bac", which is an anagram of "abc". - - Example 2: - Input: - s: "abab" p: "ab" - Output: - [0, 1, 2] - - Explanation: - The substring with start index = 0 is "ab", which is an anagram of "ab". - The substring with start index = 1 is "ba", which is an anagram of "ab". - The substring with start index = 2 is "ab", which is an anagram of "ab".*/ - -public class _438 { - public static class Solution1 { - /** - * O(m*n) solution, my original and most intuitive one, but sort of brute force, when m is close to n, it becomes O(n^2) runtime complexity. - */ - public List findAnagrams(String s, String p) { - List result = new ArrayList(); - for (int i = 0; i <= s.length() - p.length(); i++) { - if (isAnagram(s.substring(i, i + p.length()), p)) { - result.add(i); - } - } - return result; - } - - private boolean isAnagram(String s, String p) { - int[] c = new int[26]; - for (int i = 0; i < s.length(); i++) { - c[s.charAt(i) - 'a']++; - c[p.charAt(i) - 'a']--; - } - - for (int i : c) { - if (i != 0) { - return false; - } - } - return true; - } - } - - - public static class Solution2 { - /** - * Sliding Window - */ - public List findAnagrams(String s, String p) { - List result = new ArrayList(); - int[] hash = new int[26]; - for (char c : p.toCharArray()) { - hash[c - 'a']++; - } - int start = 0; - int end = 0; - int count = p.length(); - while (end < s.length()) { - if (hash[s.charAt(end) - 'a'] > 0) { - count--; - } - hash[s.charAt(end) - 'a']--; - end++; - - if (count == 0) { - result.add(start); - } - - if ((end - start) == p.length()) { - if (hash[s.charAt(start) - 'a'] >= 0) { - count++; - } - hash[s.charAt(start) - 'a']++; - start++; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_439.java b/src/main/java/com/fishercoder/solutions/_439.java deleted file mode 100644 index 80e20062e9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_439.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Deque; - -/** - * 439. Ternary Expression Parser - * - * Given a string representing arbitrarily nested ternary expressions, calculate the result of the expression. - * You can always assume that the given expression is valid and only consists of digits 0-9, ?, :, T and F (T and F represent True and False respectively). - - Note: - - The length of the given string is ≤ 10000. - Each number will contain only one digit. - The conditional expressions group right-to-left (as usual in most languages). - The condition will always be either T or F. That is, the condition will never be a digit. - The result of the expression will always evaluate to either a digit 0-9, T or F. - - Example 1: - - Input: "T?2:3" - - Output: "2" - - Explanation: If true, then result is 2; otherwise result is 3. - - - Example 2: - - Input: "F?1:T?4:5" - - Output: "4" - - Explanation: The conditional expressions group right-to-left. Using parenthesis, it is read/evaluated as: - - "(F ? 1 : (T ? 4 : 5))" "(F ? 1 : (T ? 4 : 5))" - -> "(F ? 1 : 4)" or -> "(T ? 4 : 5)" - -> "4" -> "4" - Example 3: - - Input: "T?T?F:5:3" - - Output: "F" - - Explanation: The conditional expressions group right-to-left. Using parenthesis, it is read/evaluated as: - - "(T ? (T ? F : 5) : 3)" "(T ? (T ? F : 5) : 3)" - -> "(T ? F : 3)" or -> "(T ? F : 5)" - -> "F" -> "F" - */ -public class _439 { - - public static class Solution1 { - /** - * Below is my original solution, but looking at Discuss, a more concise way is to use just one - * stack, process it from right to left, example: https://discuss.leetcode.com/topic/64409/very-easy-1-pass-stack-solution-in-java-no-string-concat - */ - - public String parseTernary(String expression) { - Deque stack = new ArrayDeque<>(); - Deque tmpStack = new ArrayDeque<>(); - for (char c : expression.toCharArray()) { - stack.addFirst(c); - } - while (!stack.isEmpty()) { - if (stack.peek() != '?') { - tmpStack.addFirst(stack.pollFirst()); - } else { - char char1 = tmpStack.removeFirst(); - tmpStack.removeFirst();//remove ':' - char char2 = tmpStack.removeFirst(); - stack.removeFirst();//remove '?' - char judge = stack.removeFirst(); - tmpStack.addFirst(judge == 'T' ? char1 : char2); - while (!tmpStack.isEmpty()) { - stack.addFirst(tmpStack.pollFirst()); - } - } - if (stack.size() == 1) { - break; - } - } - return Character.toString(stack.removeFirst()); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_440.java b/src/main/java/com/fishercoder/solutions/_440.java deleted file mode 100644 index e9ef0f29a3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_440.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 440. K-th Smallest in Lexicographical Order - * - * Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. - - Note: 1 ≤ k ≤ n ≤ 109. - - Example: - - Input: - n: 13 k: 2 - - Output: - 10 - - Explanation: - The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10. - */ -public class _440 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/64624/concise-easy-to-understand-java-5ms-solution-with-explaination/2 - */ - public int findKthNumber(int n, int k) { - int curr = 1; - k = k - 1; - while (k > 0) { - int steps = calSteps(n, curr, curr + 1); - if (steps <= k) { - curr += 1; - k -= steps; - } else { - curr *= 10; - k -= 1; - } - } - return curr; - } - - //use long in case of overflow - public int calSteps(int n, long n1, long n2) { - int steps = 0; - while (n1 <= n) { - steps += Math.min(n + 1, n2) - n1; - n1 *= 10; - n2 *= 10; - } - return steps; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_441.java b/src/main/java/com/fishercoder/solutions/_441.java deleted file mode 100644 index 523555a5c6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_441.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 441. Arranging Coins - * - * You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. - - Given n, find the total number of full staircase rows that can be formed. - - n is a non-negative integer and fits within the range of a 32-bit signed integer. - - Example 1: - - n = 5 - - The coins can form the following rows: - ¤ - ¤ ¤ - ¤ ¤ - - Because the 3rd row is incomplete, we return 2. - - - Example 2: - - n = 8 - - The coins can form the following rows: - ¤ - ¤ ¤ - ¤ ¤ ¤ - ¤ ¤ - - Because the 4th row is incomplete, we return 3.*/ -public class _441 { - - public static class Solution1 { - public int arrangeCoins(int n) { - if (n < 2) { - return n; - } - int row = 0; - int count = 0; - long sum = 0; - while (sum < n) { - row += 1; - sum += row; - count++; - } - if (sum == n) { - return count; - } - return count - 1; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_442.java b/src/main/java/com/fishercoder/solutions/_442.java deleted file mode 100644 index 581d527e17..0000000000 --- a/src/main/java/com/fishercoder/solutions/_442.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 442. Find All Duplicates in an Array - * - * Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. - - Find all the elements that appear twice in this array. - - Could you do it without extra space and in O(n) runtime? - - Example: - Input: - [4,3,2,7,8,2,3,1] - - Output: - [2,3] - */ -public class _442 { - - public static class Solution1 { - //O(n) space - //O(n) time - public List findDuplicates(int[] nums) { - Set set = new HashSet(); - List result = new ArrayList(); - for (int i : nums) { - if (!set.add(i)) { - result.add(i); - } - } - return result; - } - } - - public static class Solution2 { - //O(1) space - //O(n) time - public List findDuplicates(int[] nums) { - List result = new ArrayList(); - for (int i = 0; i < nums.length; i++) { - int index = Math.abs(nums[i]) - 1; - if (nums[index] < 0) { - result.add(Math.abs(index + 1)); - } - nums[index] = -nums[index]; - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_443.java b/src/main/java/com/fishercoder/solutions/_443.java deleted file mode 100644 index 6bf205da4a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_443.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 443. String Compression - * - * Given an array of characters, compress it in-place. - * The length after compression must always be smaller than or equal to the original array. - * Every element of the array should be a character (not int) of length 1. - * After you are done modifying the input array in-place, return the new length of the array. - - Example 1: - Input: - ["a","a","b","b","c","c","c"] - - Output: - Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"] - - Explanation: - "aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3". - - Example 2: - Input: - ["a"] - - Output: - Return 1, and the first 1 characters of the input array should be: ["a"] - - Explanation: - Nothing is replaced. - - Example 3: - Input: - ["a","b","b","b","b","b","b","b","b","b","b","b","b"] - - Output: - Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"]. - - Explanation: - Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12". - Notice each digit has it's own entry in the array. - - Note: - All characters have an ASCII value in [35, 126]. - 1 <= len(chars) <= 1000. - */ -public class _443 { - public static class Solution1 { - /**This is breaking the rules, it's not in-place.*/ - public int compress(char[] chars) { - if (chars == null || chars.length == 0) { - return 0; - } - StringBuilder sb = new StringBuilder(); - int count = 1; - char prev = chars[0]; - for (int i = 1; i < chars.length; i++) { - if (chars[i] == prev) { - count++; - } else { - if (count > 1) { - sb.append(prev); - sb.append(count); - } else if (count == 1) { - sb.append(prev); - } - prev = chars[i]; - count = 1; - } - } - sb.append(prev); - if (count > 1) { - sb.append(count); - } - int i = 0; - for (char c : sb.toString().toCharArray()) { - chars[i++] = c; - } - return sb.length(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_444.java b/src/main/java/com/fishercoder/solutions/_444.java deleted file mode 100644 index ef5ec1aaa9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_444.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; -import java.util.Set; - -/** - * 444. Sequence Reconstruction - * - * Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. - * The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 104. - * Reconstruction means building a shortest common supersequence of the sequences in seqs - * (i.e., a shortest sequence so that all sequences in seqs are subsequences of it). - * Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence. - - Example 1: - Input: - org: [1,2,3], seqs: [[1,2],[1,3]] - - Output: - false - - Explanation: - [1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed. - - Example 2: - Input: - org: [1,2,3], seqs: [[1,2]] - - Output: - false - - Explanation: - The reconstructed sequence can only be [1,2]. - - Example 3: - Input: - org: [1,2,3], seqs: [[1,2],[1,3],[2,3]] - - Output: - true - - Explanation: - The sequences [1,2], [1,3], and [2,3] can uniquely reconstruct the original sequence [1,2,3]. - - Example 4: - Input: - org: [4,1,5,2,6,3], seqs: [[5,2,6,3],[4,1,5,2]] - - Output: - true - */ -public class _444 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/65948/java-solution-using-bfs-topological-sort - */ - public boolean sequenceReconstruction(int[] org, List> seqs) { - Map> map = new HashMap<>(); - Map indegree = new HashMap<>(); - for (List seq : seqs) { - if (seq.size() == 1) { - if (!map.containsKey(seq.get(0))) { - map.put(seq.get(0), new HashSet<>()); - indegree.put(seq.get(0), 0); - } - } else { - for (int i = 0; i < seq.size() - 1; i++) { - if (!map.containsKey(seq.get(i))) { - map.put(seq.get(i), new HashSet<>()); - indegree.put(seq.get(i), 0); - } - - if (!map.containsKey(seq.get(i + 1))) { - map.put(seq.get(i + 1), new HashSet<>()); - indegree.put(seq.get(i + 1), 0); - } - - if (map.get(seq.get(i)).add(seq.get(i + 1))) { - indegree.put(seq.get(i + 1), indegree.get(seq.get(i + 1)) + 1); - } - } - } - } - - Queue queue = new LinkedList<>(); - for (Integer key : indegree.keySet()) { - if (indegree.get(key) == 0) { - queue.offer(key); - } - } - - int index = 0; - while (!queue.isEmpty()) { - int size = queue.size(); - if (size > 1) { - return false; - } - int curr = queue.poll(); - if (index == org.length || curr != org[index++]) { - return false; - } - for (int next : map.get(curr)) { - indegree.put(next, indegree.get(next) - 1); - if (indegree.get(next) == 0) { - queue.offer(next); - } - } - } - return index == org.length && index == map.size(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_446.java b/src/main/java/com/fishercoder/solutions/_446.java deleted file mode 100644 index d0f4758731..0000000000 --- a/src/main/java/com/fishercoder/solutions/_446.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 446. Arithmetic Slices II - Subsequence - * - * A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. - - For example, these are arithmetic sequences: - - 1, 3, 5, 7, 9 - 7, 7, 7, 7 - 3, -1, -5, -9 - - The following sequence is not arithmetic. - - 1, 1, 2, 5, 7 - - A zero-indexed array A consisting of N numbers is given. A subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N. - - A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2. - - The function should return the number of arithmetic subsequence slices in the array A. - - The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1. - - - Example: - - Input: [2, 4, 6, 8, 10] - - Output: 7 - - Explanation: - All arithmetic subsequence slices are: - [2,4,6] - [4,6,8] - [6,8,10] - [2,4,6,8] - [4,6,8,10] - [2,4,6,8,10] - [2,6,10] - */ -public class _446 { - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution - */ - public int numberOfArithmeticSlices(int[] A) { - int res = 0; - Map[] map = new Map[A.length]; - - for (int i = 0; i < A.length; i++) { - map[i] = new HashMap<>(i); - - for (int j = 0; j < i; j++) { - long diff = (long) A[i] - A[j]; - if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) { - continue; - } - - int d = (int) diff; - int c1 = map[i].getOrDefault(d, 0); - int c2 = map[j].getOrDefault(d, 0); - res += c2; - map[i].put(d, c1 + c2 + 1); - } - } - - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_447.java b/src/main/java/com/fishercoder/solutions/_447.java deleted file mode 100644 index 5eba7c40bf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_447.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 447. Number of Boomerangs - * - * Given n points in the plane that are all pairwise distinct, - * a "boomerang" is a tuple of points (i, j, k) such that the distance - * between i and j equals the distance between i and k (the order of the tuple matters). - * - * Find the number of boomerangs. - * You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive). - - Example: - Input: - [[0,0],[1,0],[2,0]] - - Output: - 2 - - Explanation: - The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]*/ -public class _447 { - - public static class Solution1 { - /** - * Looked at these two posts: https://discuss.leetcode.com/topic/66587/clean-java-solution-o-n-2-166ms and - * https://discuss.leetcode.com/topic/66521/share-my-straightforward-solution-with-hashmap-o-n-2, basically, - * have a HashMap, key is the distance, value is the number of points that are this distance apart to this point. - * Note: we clear up this map every time after we traverse one point with the rest of the other points. - *

- * Time complexity: O(n^2) - * Space complexity: O(n) - */ - - public int numberOfBoomerangs(int[][] points) { - int result = 0; - if (points == null || points.length == 0 || points[0].length == 0) { - return result; - } - int totalPts = points.length; - Map map = new HashMap(); - for (int i = 0; i < totalPts; i++) { - for (int j = 0; j < totalPts; j++) { - if (i == j) { - continue; - } - long d = calcDistance(points[i], points[j]); - map.put(d, map.getOrDefault(d, 0) + 1); - } - - for (int val : map.values()) { - result += val * (val - 1); - } - map.clear(); - } - return result; - } - - private long calcDistance(int[] p1, int[] p2) { - long x = p2[0] - p1[0]; - long y = p2[1] - p1[1]; - return x * x + y * y; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_449.java b/src/main/java/com/fishercoder/solutions/_449.java deleted file mode 100644 index f54eb70e13..0000000000 --- a/src/main/java/com/fishercoder/solutions/_449.java +++ /dev/null @@ -1,169 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 449. Serialize and Deserialize BST - * - * Serialization is the process of converting a data structure or - * object into a sequence of bits so that it can be stored in a file or memory buffer, - * or transmitted across a network connection link to be reconstructed later in the same or another computer environment. - * - * Design an algorithm to serialize and deserialize a binary search tree. - * There is no restriction on how your serialization/deserialization algorithm should work. - * You just need to ensure that a binary search tree can be serialized to a string and this - * string can be deserialized to the original tree structure. - * The encoded string should be as compact as possible. - - Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless. - */ - -public class _449 { - - public static class Solution1 { - /** - * Preorder - * Reference: https://discuss.leetcode.com/topic/97922/pre-or-post-order-with-only-keeping-one-bound-beat-98-and-95 - */ - - // Encodes a tree to a single string. - public String serialize(TreeNode root) { - if (root == null) { - return null; - } - StringBuilder stringBuilder = new StringBuilder(); - return serialize(root, stringBuilder); - } - - private String serialize(TreeNode root, StringBuilder stringBuilder) { - if (root == null) { - return null; - } - stringBuilder.append(root.val).append(" "); - serialize(root.left, stringBuilder); - serialize(root.right, stringBuilder); - return stringBuilder.toString(); - } - - // Decodes your encoded data to tree. - public TreeNode deserialize(String data) { - if (data == null || data.length() == 0) { - return null; - } - String[] values = data.split(" "); - int[] index = new int[]{0};/**TODO: Why must use an int array, instead of just an int?*/ - return deserialize(values, index, Integer.MAX_VALUE); - } - - private TreeNode deserialize(String[] values, int[] index, int maxValue) { - if (index[0] >= values.length || Integer.valueOf(values[index[0]]) >= maxValue) { - return null; - } - TreeNode root = new TreeNode(Integer.valueOf(values[index[0]++])); - root.left = deserialize(values, index, root.val); - root.right = deserialize(values, index, maxValue); - return root; - } - } - - public static class Solution2 { - /** - * Postorder - * Reference: https://discuss.leetcode.com/topic/97922/pre-or-post-order-with-only-keeping-one-bound-beat-98-and-95 - */ - - public String serialize(TreeNode root) { - if (root == null) { - return null; - } - StringBuilder sb = new StringBuilder(); - return serialize(root, sb); - } - - private String serialize(TreeNode root, StringBuilder sb) { - if (root == null) { - return null; - } - serialize(root.left, sb); - serialize(root.right, sb); - sb.append(root.val).append(" "); - return sb.toString(); - } - - // Decodes your encoded data to tree. - public TreeNode deserialize(String data) { - if (data == null || data.length() == 0) { - return null; - } - String[] values = data.split(" "); - int[] index = new int[]{values.length - 1};/**TODO: This is not just one element any more like in the preorder solution above*/ - return deserialize(values, index, Integer.MIN_VALUE); - } - - private TreeNode deserialize(String[] values, int[] index, int minValue) { - if (index[0] < 0 || Integer.valueOf(values[index[0]]) < minValue) { - return null; - } - TreeNode root = new TreeNode(Integer.valueOf(values[index[0]--])); - root.right = deserialize(values, index, root.val); - root.left = deserialize(values, index, minValue); - return root; - } - } - - public static class Solution3 { - /** - * This is a generic solution that applies to both BT and BST. And also the easiest to follow. - */ - - // Encodes a tree to a single string. - public String serialize(TreeNode root) { - Queue queue = new LinkedList<>(); - StringBuilder stringBuilder = new StringBuilder(); - if (root == null) { - return stringBuilder.toString(); - } - queue.offer(root); - while (!queue.isEmpty()) { - int size = queue.size(); - for (int i = 0; i < size; i++) { - TreeNode curr = queue.poll(); - if (curr == null) { - stringBuilder.append("# "); - } else { - stringBuilder.append(curr.val + " "); - queue.offer(curr.left); - queue.offer(curr.right); - } - } - } - return stringBuilder.toString(); - } - - // Decodes your encoded data to tree. - public TreeNode deserialize(String data) { - if (data == null || data.length() == 0) { - return null; - } - String[] nodes = data.split(" "); - TreeNode root = new TreeNode(Integer.valueOf(nodes[0])); - Queue queue = new LinkedList<>(); - queue.offer(root); - for (int i = 1; i < nodes.length; i++) { - TreeNode curr = queue.poll(); - if (!nodes[i].equals("#")) { - curr.left = new TreeNode(Integer.valueOf(nodes[i])); - queue.offer(curr.left); - } - if (!nodes[++i].equals("#")) { - curr.right = new TreeNode(Integer.valueOf(nodes[i])); - queue.offer(curr.right); - } - } - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_45.java b/src/main/java/com/fishercoder/solutions/_45.java deleted file mode 100644 index 54e0a577b2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_45.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.fishercoder.solutions; - -public class _45 { - public static class Solution1 { - public int jump(int[] nums) { - int stepCount = 0; - int lastJumpMax = 0; - int currentJumpMax = 0; - for (int i = 0; i < nums.length - 1; i++) { - currentJumpMax = Math.max(currentJumpMax, i + nums[i]); - if (i == lastJumpMax) { - stepCount++; - lastJumpMax = currentJumpMax; - } - } - return stepCount; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_450.java b/src/main/java/com/fishercoder/solutions/_450.java deleted file mode 100644 index 22b3f24ee1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_450.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 450. Delete Node in a BST - * - * Given a root node reference of a BST and a key, delete the node with the given key in the BST. - * Return the root node reference (possibly updated) of the BST. - - Basically, the deletion can be divided into two stages: - - Search for a node to remove. - If the node is found, delete the node. - Note: Time complexity should be O(height of tree). - - Example: - - root = [5,3,6,2,4,null,7] - key = 3 - - 5 - / \ - 3 6 - / \ \ - 2 4 7 - - Given key to delete is 3. So we find the node with value 3 and delete it. - - One valid answer is [5,4,6,2,null,null,7], shown in the following BST. - - 5 - / \ - 4 6 - / \ - 2 7 - - Another valid answer is [5,2,6,null,4,null,7]. - - 5 - / \ - 2 6 - \ \ - 4 7 - */ -public class _450 { - public static class Solution1 { - - /** - * credit: https://discuss.leetcode.com/topic/65792/recursive-easy-to-understand-java-solution - * Steps: - * 1. Recursively find the node that has the same value as the key, while setting the left/right nodes equal to the returned subtree - * 2. Once the node is found, have to handle the below 4 cases - * a. node doesn't have left or right - return null - * b. node only has left subtree- return the left subtree - * c. node only has right subtree- return the right subtree - * d. node has both left and right - find the minimum value in the right subtree, set that value to the currently found node, then recursively delete the minimum value in the right subtree - */ - public TreeNode deleteNode(TreeNode root, int key) { - if (root == null) { - return root; - } - if (root.val > key) { - root.left = deleteNode(root.left, key); - } else if (root.val < key) { - root.right = deleteNode(root.right, key); - } else { - if (root.left == null) { - return root.right; - } else if (root.right == null) { - return root.left; - } - - TreeNode minNode = getMin(root.right); - root.val = minNode.val; - root.right = deleteNode(root.right, root.val); - } - return root; - } - - private TreeNode getMin(TreeNode node) { - while (node.left != null) { - node = node.left; - } - return node; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_451.java b/src/main/java/com/fishercoder/solutions/_451.java deleted file mode 100644 index 5c4b185fe0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_451.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 451. Sort Characters By Frequency - * - * Given a string, sort it in decreasing order based on the frequency of characters. - - Example 1: - - Input: - "tree" - - Output: - "eert" - - Explanation: - 'e' appears twice while 'r' and 't' both appear once. - So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. - - Example 2: - - Input: - "cccaaa" - - Output: - "cccaaa" - - Explanation: - Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer. - Note that "cacaca" is incorrect, as the same characters must be together. - - Example 3: - - Input: - "Aabb" - - Output: - "bbAa" - - Explanation: - "bbaA" is also a valid answer, but "Aabb" is incorrect. - Note that 'A' and 'a' are treated as two different characters. - - */ -public class _451 { - - public static class Solution1 { - public String frequencySort(String s) { - Map map = new HashMap(); - for (char c : s.toCharArray()) { - map.put(c, map.getOrDefault(c, 0) + 1); - } - List> list = new ArrayList<>(map.entrySet()); - Collections.sort(list, (o1, o2) -> (o2.getValue() - o1.getValue())); - StringBuilder stringBuilder = new StringBuilder(); - for (Map.Entry entry : list) { - for (int i = 0; i < entry.getValue(); i++) { - stringBuilder.append(entry.getKey()); - } - } - return stringBuilder.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_452.java b/src/main/java/com/fishercoder/solutions/_452.java deleted file mode 100644 index 89d15304ea..0000000000 --- a/src/main/java/com/fishercoder/solutions/_452.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - *452. Minimum Number of Arrows to Burst Balloons - * - * There are a number of spherical balloons spread in two-dimensional space. - * For each balloon, provided input is the start and end coordinates of the horizontal diameter. - * Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of start and end of the diameter suffice. - * Start is always smaller than end. There will be at most 104 balloons. - * - * An arrow can be shot up exactly vertically from different points along the x-axis. - * A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. - * There is no limit to the number of arrows that can be shot. - * An arrow once shot keeps travelling up infinitely. - * The problem is to find the minimum number of arrows that must be shot to burst all balloons. - - Example: - - Input: - [[10,16], [2,8], [1,6], [7,12]] - - Output: - 2 - - Explanation: - One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons). - */ -public class _452 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/66579/java-greedy-soution/6 - */ - public int findMinArrowShots(int[][] points) { - if (points == null || points.length == 0) { - return 0; - } - // sort points based on their end point. - Arrays.sort(points, (p1, p2) -> Integer.compare(p1[1], p2[1])); - int currentEnd = points[0][1]; - int count = 1; - for (int[] p : points) { - // if the point starts after currentEnd, it means this balloons not been bursted. Then we shot the balloon in its end point. Otherwise, means this balloon has been bursted, then ignore it. - if (p[0] > currentEnd) { - count++; - currentEnd = p[1]; - } else { - continue; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_453.java b/src/main/java/com/fishercoder/solutions/_453.java deleted file mode 100644 index e6198e2e82..0000000000 --- a/src/main/java/com/fishercoder/solutions/_453.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 453. Minimum Moves to Equal Array Elements - * - * Given a non-empty integer array of size n, - * find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. - - Example: - - Input: - [1,2,3] - - Output: - 3 - - Explanation: - Only three moves are needed (remember each move increments two elements): - - [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]*/ - -public class _453 { - public static class Solution1 { - /** - * Credit: https://discuss.leetcode.com/topic/66557/java-o-n-solution-short - * i.e. Add 1 to n-1 elements basically equals to subtracting 1 from one element. So the easiest way - * to make all elements in this array equal is to make all of them equal to the minimum element. - */ - public int minMoves(int[] nums) { - if (nums.length == 0) { - return 0; - } - int min = nums[0]; - for (int n : nums) { - min = Math.min(min, n); - } - int res = 0; - for (int n : nums) { - res += n - min; - } - return res; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_454.java b/src/main/java/com/fishercoder/solutions/_454.java deleted file mode 100644 index 608869ff39..0000000000 --- a/src/main/java/com/fishercoder/solutions/_454.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 454. 4Sum II - * - * Given four lists A, B, C, D of integer values, - * compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. - * To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. - * All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. - - Example: - - Input: - A = [ 1, 2] - B = [-2,-1] - C = [-1, 2] - D = [ 0, 2] - - Output: - 2 - - Explanation: - The two tuples are: - 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 - 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0*/ - -public class _454 { - - public static class Solution1 { - public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { - Map map = new HashMap(); - int result = 0; - int len = A.length; - for (int i = 0; i < len; i++) { - for (int j = 0; j < len; j++) { - int sum = A[i] + B[j]; - if (map.containsKey(sum)) { - map.put(sum, map.get(sum) + 1); - } else { - map.put(sum, 1); - } - } - } - - for (int i = 0; i < len; i++) { - for (int j = 0; j < len; j++) { - int sum = -(C[i] + D[j]); - if (map.containsKey(sum)) { - result += map.get(sum); - } - } - } - - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_455.java b/src/main/java/com/fishercoder/solutions/_455.java deleted file mode 100644 index 107b507572..0000000000 --- a/src/main/java/com/fishercoder/solutions/_455.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 455. Assign Cookies - * - * Assume you are an awesome parent and want to give your children some cookies. - * But, you should give each child at most one cookie. - * Each child i has a greed factor gi, which is the minimum size of a cookie - * that the child will be content with; - * and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, - * and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. - - Note: - You may assume the greed factor is always positive. - You cannot assign more than one cookie to one child. - - Example 1: - Input: [1,2,3], [1,1] - Output: 1 - Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. - And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content. - You need to output 1. - - Example 2: - Input: [1,2], [1,2,3] - Output: 2 - Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. - You have 3 cookies and their sizes are big enough to gratify all of the children, - You need to output 2.*/ - -public class _455 { - - public static class Solution1 { - public int findContentChildren(int[] g, int[] s) { - Arrays.sort(g); - Arrays.sort(s); - - int result = 0; - for (int i = 0, j = 0; i < g.length && j < s.length; ) { - if (s[j] >= g[i]) { - result++; - i++; - } - j++; - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_456.java b/src/main/java/com/fishercoder/solutions/_456.java deleted file mode 100644 index 61b3c12fe9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_456.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Deque; -import java.util.LinkedList; -/** - * 456. 132 Pattern - * - * Given a sequence of n integers a1, a2, ..., an, - * a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. - * Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. - - Note: n will be less than 15,000. - - Example 1: - Input: [1, 2, 3, 4] - - Output: False - - Explanation: There is no 132 pattern in the sequence. - Example 2: - Input: [3, 1, 4, 2] - - Output: True - - Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. - Example 3: - Input: [-1, 3, 2, 0] - - Output: True - - Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0]. - */ - -public class _456 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/67881/single-pass-c-o-n-space-and-time-solution-8-lines-with-detailed-explanation - * It scans only once, this is the power of using correct data structure. - * It goes from the right to the left. - * It keeps pushing elements into the stack, - * but it also keeps poping elements out of the stack as long as the current element is bigger than this number. - */ - public boolean find132pattern(int[] nums) { - Deque stack = new LinkedList<>(); - - int s3 = Integer.MIN_VALUE; - for (int i = nums.length - 1; i >= 0; i--) { - if (nums[i] < s3) { - return true; - } else { - while (!stack.isEmpty() && nums[i] > stack.peek()) { - s3 = Math.max(s3, stack.pop()); - } - } - stack.push(nums[i]); - } - - return false; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_457.java b/src/main/java/com/fishercoder/solutions/_457.java deleted file mode 100644 index 2c73fd0f8a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_457.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 457. Circular Array Loop - * - * You are given an array of positive and negative integers. - * If a number n at an index is positive, then move forward n steps. - * Conversely, if it's negative (-n), move backward n steps. - * - * Assume the first element of the array is forward next to the last element, - * and the last element is backward next to the first element. - * Determine if there is a loop in this array. - * A loop starts and ends at a particular index with more than 1 element along the loop. The loop must be "forward" or "backward'. - * - * Example 1: Given the array [2, -1, 1, 2, 2], there is a loop, from index 0 -> 2 -> 3 -> 0. - * Example 2: Given the array [-1, 2], there is no loop. - * - * Note: The given array is guaranteed to contain no element "0". - * - * Can you do it in O(n) time complexity and O(1) space complexity? - */ -public class _457 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/66894/java-slow-fast-pointer-solution - */ - public boolean circularArrayLoop(int[] nums) { - int n = nums.length; - for (int i = 0; i < n; i++) { - if (nums[i] == 0) { - continue; - } - // slow/fast pointer - int j = i; - int k = getIndex(i, nums); - while (nums[k] * nums[i] > 0 && nums[getIndex(k, nums)] * nums[i] > 0) { - if (j == k) { - // check for loop with only one element - if (j == getIndex(j, nums)) { - break; - } - return true; - } - j = getIndex(j, nums); - k = getIndex(getIndex(k, nums), nums); - } - // loop not found, set all element along the way to 0 - j = i; - int val = nums[i]; - while (nums[j] * val > 0) { - int next = getIndex(j, nums); - nums[j] = 0; - j = next; - } - } - return false; - } - - public int getIndex(int i, int[] nums) { - int n = nums.length; - return i + nums[i] >= 0 ? (i + nums[i]) % n : n + ((i + nums[i]) % n); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_458.java b/src/main/java/com/fishercoder/solutions/_458.java deleted file mode 100644 index caba3de4af..0000000000 --- a/src/main/java/com/fishercoder/solutions/_458.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 458. Poor Pigs - * - * There are 1000 buckets, one and only one of them contains poison, - * the rest are filled with water. - * They all look the same. - * If a pig drinks that poison it will die within 15 minutes. - * What is the minimum amount of pigs you need to figure out which bucket contains the poison within one hour. - * Answer this question, and write an algorithm for the follow-up general case. - - Follow-up: - If there are n buckets and a pig drinking poison will die within m minutes, - how many pigs (x) you need to figure out the "poison" bucket within p minutes? There is exact one bucket with poison. - - */ -public class _458 { - - public static class Solution1 { - public int poorPigs(int buckets, int minutesToDie, int minutesToTest) { - if (buckets-- == 1) { - return 0; - } - int base = minutesToTest / minutesToDie + 1; - int count = 0; - while (buckets > 0) { - buckets /= base; - count++; - } - return count; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_459.java b/src/main/java/com/fishercoder/solutions/_459.java deleted file mode 100644 index b32335b4c4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_459.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 459. Repeated Substring Pattern - * Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. - * You may assume the given string consists of lowercase English letters only and its length will not exceed 10000. - - Example 1: - Input: "abab" - - Output: True - - Explanation: It's the substring "ab" twice. - Example 2: - Input: "aba" - - Output: False - Example 3: - Input: "abcabcabcabc" - - Output: True - - Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.) - */ - -public class _459 { - - public static class Solution1 { - public boolean repeatedSubstringPattern(String s) { - int len = s.length(); - for (int i = len / 2; i >= 1; i--) { - String pattern = s.substring(0, i); - if (len % i == 0) { - String formedString = formStringByCopying(pattern, len / i); - if (formedString.equals(s)) { - return true; - } - } - } - return false; - } - - private String formStringByCopying(String pattern, int k) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < k; i++) { - sb.append(pattern); - } - return sb.toString(); - } - } - - public static class Solution2 { - /** - * credit: https://discuss.leetcode.com/topic/68089/repeated-substring-pattern-simple-java-solution-using-kmp - */ - public static boolean repeatedSubstringPattern(String str) { - //build the KMP pattern. - int n = str.length(); - int cur = 0; - int j = 1; - int[] pattern = new int[n]; - pattern[0] = 0; - - while (j < n) { - if (str.charAt(cur) == str.charAt(j)) { - pattern[j++] = ++cur; - } else { - if (cur == 0) { - pattern[j++] = 0; - } else { - cur = pattern[cur - 1];//start from beginning of current matching pattern. - } - } - } - - return (pattern[n - 1] > 0 && n % (n - pattern[n - 1]) == 0); - } - } - - public static class Solution3 { - public static boolean repeatedSubstringPattern(String str) { - String s = str + str; - return s.substring(1, s.length() - 1).contains(str); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_46.java b/src/main/java/com/fishercoder/solutions/_46.java deleted file mode 100644 index 31fb52c46b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_46.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -public class _46 { - - public static class Solution1 { - public List> permute(int[] nums) { - List> result = new ArrayList(); - result.add(new ArrayList<>()); - return backtracking(nums, 0, result); - } - - private List> backtracking(int[] nums, int index, List> result) { - if (index == nums.length) { - return result; - } - List> newResult = new ArrayList<>(); - for (List eachList : result) { - for (int i = 0; i <= eachList.size(); i++) { - List newList = new ArrayList<>(eachList); - newList.add(i, nums[index]); - newResult.add(newList); - } - } - result = newResult; - return backtracking(nums, index + 1, result); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_460.java b/src/main/java/com/fishercoder/solutions/_460.java deleted file mode 100644 index 6169d4e8d6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_460.java +++ /dev/null @@ -1,131 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.LinkedHashSet; - -/** - 460. LFU Cache - - Design and implement a data structure for Least Frequently Used (LFU) cache. - It should support the following operations: get and put. - - get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. - put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, - it should invalidate the least frequently used item before inserting a new item. - For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), - the least recently used key would be evicted. - - Follow up: - Could you do both operations in O(1) time complexity? - - Example: - - LFUCache cache = new LFUCache( 2 /* capacity ); - - cache.put(1, 1); - cache.put(2, 2); - cache.get(1); // returns 1 - cache.put(3, 3); // evicts key 2 - cache.get(2); // returns -1 (not found) - cache.get(3); // returns 3. - cache.put(4, 4); // evicts key 1. - cache.get(1); // returns -1 (not found) - cache.get(3); // returns 3 - cache.get(4); // returns 4 - - */ - -public class _460 { - public static class Solution1 { - /** - * Wikipedia: The simplest method to employ an LFU algorithm is to assign a counter to every - * block that is loaded into the cache. Each time a reference is made to that block the counter - * is increased by one. When the cache reaches capacity and has a new block waiting to be - * inserted the system will search for the block with the lowest counter and remove it from the - * cache. - * Policy to handle frequency ties: based on timestamp, the entries that get set into cache earlier will be evicted first. - */ - - public static class LFUCache { - /** - * credit: https://discuss.leetcode.com/topic/69737/java-o-1-very-easy-solution-using-3-hashmaps-and-linkedhashset/2 - */ - - HashMap keyToValue; - /** - * key is the key, value is the value - */ - HashMap keyToCount; - /** - * key is the key, value if the count of the key/value pair - */ - HashMap> countToLRUKeys; - /** - * key is count, value is a set of keys that have the same key, but keeps insertion order - */ - int cap; - int minimumCount; - - public LFUCache(int capacity) { - this.minimumCount = -1; - this.cap = capacity; - this.keyToValue = new HashMap<>(); - this.keyToCount = new HashMap<>(); - this.countToLRUKeys = new HashMap<>(); - this.countToLRUKeys.put(1, new LinkedHashSet<>()); - } - - public int get(int key) { - if (!keyToValue.containsKey(key)) { - return -1; - } - int count = keyToCount.get(key); - keyToCount.put(key, count + 1); - countToLRUKeys.get(count).remove(key); - - if (count == minimumCount && countToLRUKeys.get(count).size() == 0) { - /**This means this key's count equals to current minimumCount - * AND - * this count doesn't have any entries in the cache. - * So, we'll increment minimumCount by 1 to get the next LFU cache entry - * when we need to evict.*/ - minimumCount++; - } - - if (!countToLRUKeys.containsKey(count + 1)) { - countToLRUKeys.put(count + 1, new LinkedHashSet<>()); - } - countToLRUKeys.get(count + 1).add(key); - - return keyToValue.get(key); - } - - public void put(int key, int value) { - if (cap <= 0) { - return; - } - - if (keyToValue.containsKey(key)) { - /**If the key is already in the cache, we can simply overwrite this entry; - * then call get(key) which will do the update work.*/ - keyToValue.put(key, value); - get(key); - return; - } - - /**If the key is not in the cache, we'll check the size first, evict the LFU entry first, - * then insert this one into cache.*/ - if (keyToValue.size() >= cap) { - int evit = countToLRUKeys.get(minimumCount).iterator().next(); - countToLRUKeys.get(minimumCount).remove(evit); - keyToValue.remove(evit); - } - keyToValue.put(key, value); - keyToCount.put(key, 1); - countToLRUKeys.get(1).add(key);/**Because we put this key/value into cache for the first time, so its count is 1*/ - minimumCount = 1;/**For the same above reason, minimumCount is of course 1*/ - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_461.java b/src/main/java/com/fishercoder/solutions/_461.java deleted file mode 100644 index d9b68cf92c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_461.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 461. Hamming Distance - * - * The Hamming distance between two integers is the number of positions at which the corresponding bits are different. - - Given two integers x and y, calculate the Hamming distance. - - Note: - 0 ≤ x, y < 231. - - Example: - - Input: x = 1, y = 4 - - Output: 2 - - Explanation: - 1 (0 0 0 1) - 4 (0 1 0 0) - ↑ ↑ - - The above arrows point to positions where the corresponding bits are different. - */ -public class _461 { - public static class Solution1 { - public int hammingDistance(int x, int y) { - int n = x ^ y; - int count = 0; - while (n != 0) { - count++; - n &= (n - 1); - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_462.java b/src/main/java/com/fishercoder/solutions/_462.java deleted file mode 100644 index 3cd796660c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_462.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 462. Minimum Moves to Equal Array Elements II - * - * Given a non-empty integer array, - * find the minimum number of moves required to make all array elements equal, - * where a move is incrementing a selected element by 1 or decrementing a selected element by 1. - * - * You may assume the array's length is at most 10,000. - - Example: - - Input: - [1,2,3] - - Output: - 2 - - Explanation: - Only two moves are needed (remember each move increments or decrements one element): - - [1,2,3] => [2,2,3] => [2,2,2] - */ - -public class _462 { - - public static class Solution1 { - public int minMoves2(int[] nums) { - /**sort this array, find the median of this array as the pivot*/ - Arrays.sort(nums); - int result = 0; - int result1 = 0; - if (nums.length % 2 != 0) { - int median = nums[nums.length / 2]; - for (int n : nums) { - result += Math.abs(n - median); - } - return result; - } else { - int median1 = nums[nums.length / 2]; - for (int n : nums) { - result1 += Math.abs(n - median1); - } - int result2 = 0; - int median2 = nums[nums.length / 2 - 1]; - for (int n : nums) { - result2 += Math.abs(n - median2); - } - result1 = Math.min(result1, result2); - return result1; - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_463.java b/src/main/java/com/fishercoder/solutions/_463.java deleted file mode 100644 index 28ea07744c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_463.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; -/** - * 463. Island Perimeter - * - * You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. - * Grid cells are connected horizontally/vertically (not diagonally). - * The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). - * The island doesn't have "lakes" (water inside that isn't connected to the water around the island). - * One cell is a square with side length 1. - * The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island. - - Example: - - [[0,1,0,0], - [1,1,1,0], - [0,1,0,0], - [1,1,0,0]] - - Answer: 16 - Explanation: The perimeter is the 16 yellow stripes in the image below: - */ -public class _463 { - - public static class Solution1 { - /** - * Inspired by this post: https://discuss.leetcode.com/topic/68983/java-9-line-solution-add-4-for-each-land-and-remove-2-for-each-internal-edge - * 1. we increment the count by 4 whenever we encounter an island - * 2. also, we check in two directions: island's left and island's top, we only check these two directions, - * see if this island has any island neighbors, if so, we'll deduct two from it. - */ - public int islandPerimeter(int[][] grid) { - int count = 0; - for (int i = 0; i < grid.length; i++) { - for (int j = 0; j < grid[0].length; j++) { - if (grid[i][j] == 1) { - count += 4; - if (i > 0 && grid[i - 1][j] == 1) { - count -= 2; - } - if (j > 0 && grid[i][j - 1] == 1) { - count -= 2; - } - } - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_464.java b/src/main/java/com/fishercoder/solutions/_464.java deleted file mode 100644 index 8c7b80ba07..0000000000 --- a/src/main/java/com/fishercoder/solutions/_464.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 464. Can I Win - * - * In the "100 game," two players take turns adding, to a running total, any integer from 1..10. - * The player who first causes the running total to reach or exceed 100 wins. - * What if we change the game so that players cannot re-use integers? - * For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100. - * Given an integer maxChoosableInteger and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally. - *You can always assume that maxChoosableInteger will not be larger than 20 and desiredTotal will not be larger than 300. - - Example - - Input: - maxChoosableInteger = 10 - desiredTotal = 11 - - Output: - false - - Explanation: - No matter which integer the first player choose, the first player will lose. - The first player can choose an integer from 1 up to 10. - If the first player choose 1, the second player can only choose integers from 2 up to 10. - The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal. - Same with other integers chosen by the first player, the second player will always win. - */ -public class _464 { - public static class Solution1 { - /** - * Credit: https://discuss.leetcode.com/topic/68896/java-solution-using-hashmap-with-detailed-explanation - */ - - Map map; - boolean[] used; - - public boolean canIWin(int maxChoosableInteger, int desiredTotal) { - int sum = (1 + maxChoosableInteger) * maxChoosableInteger / 2; - if (sum < desiredTotal) { - return false; - } - if (desiredTotal <= 0) { - return true; - } - - map = new HashMap(); - used = new boolean[maxChoosableInteger + 1]; - return helper(desiredTotal); - } - - public boolean helper(int desiredTotal) { - if (desiredTotal <= 0) { - return false; - } - int key = format(used); - if (!map.containsKey(key)) { - // try every unchosen number as next step - for (int i = 1; i < used.length; i++) { - if (!used[i]) { - used[i] = true; - // check whether this lead to a win (i.e. the other player lose) - if (!helper(desiredTotal - i)) { - map.put(key, true); - used[i] = false; - return true; - } - used[i] = false; - } - } - map.put(key, false); - } - return map.get(key); - } - - // transfer boolean[] to an Integer - public int format(boolean[] used) { - int num = 0; - for (boolean b : used) { - num <<= 1; - if (b) { - num |= 1; - } - } - return num; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_465.java b/src/main/java/com/fishercoder/solutions/_465.java deleted file mode 100644 index e903725ff2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_465.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Stack; - -/** - * 465. Optimal Account Balancing - * - * A group of friends went on holiday and sometimes lent each other money. - * For example, Alice paid for Bill's lunch for $10. - * Then later Chris gave Alice $5 for a taxi ride. - * We can model each transaction as a tuple (x, y, z) which means person x gave person y $z. - * Assuming Alice, Bill, and Chris are person 0, 1, and 2 respectively (0, 1, 2 are the person's ID), - * the transactions can be represented as [[0, 1, 10], [2, 0, 5]]. - - Given a list of transactions between a group of people, return the minimum number of transactions required to settle the debt. - - Note: - - A transaction will be given as a tuple (x, y, z). Note that x ? y and z > 0. - Person's IDs may not be linear, e.g. we could have the persons 0, 1, 2 or we could also have the persons 0, 2, 6. - Example 1: - - Input: - [[0,1,10], [2,0,5]] - - Output: - 2 - - Explanation: - Person #0 gave person #1 $10. - Person #2 gave person #0 $5. - - Two transactions are needed. One way to settle the debt is person #1 pays person #0 and #2 $5 each. - Example 2: - - Input: - [[0,1,10], [1,0,1], [1,2,5], [2,0,5]] - - Output: - 1 - - Explanation: - Person #0 gave person #1 $10. - Person #1 gave person #0 $1. - Person #1 gave person #2 $5. - Person #2 gave person #0 $5. - - Therefore, person #1 only need to give person #0 $4, and all debt is settled. - */ -public class _465 { - public static class Solution1 { - /** - * Credit: https://discuss.leetcode.com/topic/68948/easy-java-solution-with-explanation - */ - public int minTransfers(int[][] transactions) { - if (transactions == null || transactions.length == 0) { - return 0; - } - Map acc = new HashMap<>(); - for (int i = 0; i < transactions.length; i++) { - int id1 = transactions[i][0]; - int id2 = transactions[i][1]; - int m = transactions[i][2]; - acc.put(id1, acc.getOrDefault(id1, 0) - m); - acc.put(id2, acc.getOrDefault(id2, 0) + m); - } - List negs = new ArrayList<>(); - List poss = new ArrayList<>(); - for (Integer key : acc.keySet()) { - int m = acc.get(key); - if (m == 0) { - continue; - } - if (m < 0) { - negs.add(-m); - } else { - poss.add(m); - } - } - int ans = Integer.MAX_VALUE; - Stack stNeg = new Stack<>(); - Stack stPos = new Stack<>(); - for (int i = 0; i < 1000; i++) { - for (Integer num : negs) { - stNeg.push(num); - } - for (Integer num : poss) { - stPos.push(num); - } - int cur = 0; - while (!stNeg.isEmpty()) { - int n = stNeg.pop(); - int p = stPos.pop(); - cur++; - if (n == p) { - continue; - } - if (n > p) { - stNeg.push(n - p); - } else { - stPos.push(p - n); - } - } - ans = Math.min(ans, cur); - Collections.shuffle(negs); - Collections.shuffle(poss); - } - return ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_466.java b/src/main/java/com/fishercoder/solutions/_466.java deleted file mode 100644 index 69c0fd4af0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_466.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 466. Count The Repetitions - * - * Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] ="abcabcabc". - - On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such that it becomes s1. - For example, “abc” can be obtained from “abdbec” based on our definition, but it can not be obtained from “acbbe”. - You are given two non-empty strings s1 and s2 (each at most 100 characters long) and two integers 0 ≤ n1 ≤ 106 and 1 ≤ n2 ≤ 106. - Now consider the strings S1 and S2, where S1=[s1,n1] and S2=[s2,n2]. Find the maximum integer m such that [S2,m] can be obtained from S1. - - Example: - - Input: - s1="acb", n1=4 - s2="ab", n2=2 - - Return: - 2 - - */ -public class _466 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/70707/ugly-java-brute-force-solution-but-accepted-1088ms - */ - public int getMaxRepetitions(String s1, int n1, String s2, int n2) { - char[] s1chars = s1.toCharArray(); - char[] s2chars = s2.toCharArray(); - int i = 0; - int j = 0; - int count1 = 0; - int count2 = 0; - while (count1 < n1) { - if (s1chars[i] == s2chars[j]) { - j++; - if (j == s2.length()) { - j = 0; - count2++; - } - } - i++; - if (i == s1.length()) { - i = 0; - count1++; - } - } - return count2 / n2; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_467.java b/src/main/java/com/fishercoder/solutions/_467.java deleted file mode 100644 index 7bb9418ed4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_467.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 467. Unique Substrings in Wraparound String - * - * Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", - * so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". - * Now we have another string p. - * Your job is to find out how many unique non-empty substrings of p are present in s. - * In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s. - * Note: p consists of only lowercase English letters and the size of p might be over 10000. - - Example 1: - Input: "a" - Output: 1 - Explanation: Only the substring "a" of string "a" is in the string s. - - Example 2: - Input: "cac" - Output: 2 - Explanation: There are two substrings "a", "c" of string "cac" in the string s. - - Example 3: - Input: "zab" - Output: 6 - Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s. - */ -public class _467 { - public static class Solution1 { - /** - * A naive solution would definitely result in TLE. - * Since the pattern keeps repeating, DP is the way to go. - * Because the input consists merely of lowercase English letters, we could maintain an array of size 26, - * keep updating this array, counting the substrings that end with this letter, keep updating it with the largest one possible. - *

- * Inspired by this: https://discuss.leetcode.com/topic/70658/concise-java-solution-using-dp - */ - public int findSubstringInWraproundString(String p) { - if (p == null || p.isEmpty()) { - return 0; - } - if (p.length() < 2) { - return p.length(); - } - int count = 0; - int[] dp = new int[26]; - dp[p.charAt(0) - 'a'] = 1; - int len = 1; - for (int i = 1; i < p.length(); i++) { - if (p.charAt(i) - 1 == p.charAt(i - 1) || (p.charAt(i) == 'a' && p.charAt(i - 1) == 'z')) { - len++; - } else { - len = 1; - } - dp[p.charAt(i) - 'a'] = Math.max(len, dp[p.charAt(i) - 'a']); - } - - for (int i : dp) { - count += i; - } - return count; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_468.java b/src/main/java/com/fishercoder/solutions/_468.java deleted file mode 100644 index d1c9de5ada..0000000000 --- a/src/main/java/com/fishercoder/solutions/_468.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -/** - * 468. Validate IP Address - * - * Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither. - * IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, - * each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1; - * Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid. - - IPv6 addresses are represented as eight groups of four hexadecimal digits, - each group representing 16 bits. - The groups are separated by colons (":"). - For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. - Also, we could omit some leading zeros among four hexadecimal digits and - some low-case characters in the address to upper-case ones, - so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases). - - However, we don't replace a consecutive group of zero value with a single empty - group using two consecutive colons (::) to pursue simplicity. - For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address. - - Besides, extra leading zeros in the IPv6 is also invalid. - For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid. - - Note: You may assume there is no extra space or special characters in the input string. - - Example 1: - Input: "172.16.254.1" - Output: "IPv4" - Explanation: This is a valid IPv4 address, return "IPv4". - - Example 2: - Input: "2001:0db8:85a3:0:0:8A2E:0370:7334" - Output: "IPv6" - Explanation: This is a valid IPv6 address, return "IPv6". - - Example 3: - Input: "256.256.256.256" - Output: "Neither" - Explanation: This is neither a IPv4 address nor a IPv6 address. - validIPAddress */ -public class _468 { - - public static class Solution1 { - - static final String NEITHER = "Neither"; - - public String validIPAddress(String IP) { - if (IP.contains(".")) { - return isValidIPv4(IP); - } else if (IP.contains(":")) { - return isValidIPv6(IP); - } else { - return NEITHER; - } - } - - private String isValidIPv6(String IP) { - if (getDelimiterCount(IP, ':') != 7) { - return NEITHER; - } - String[] bytes = IP.split("\\:"); - if (bytes.length != 8) { - return NEITHER; - } - for (int i = 0; i < 8; i++) { - if (hasInvalidIPV6Char(bytes[i])) { - return NEITHER; - } - try { - if (bytes[i].length() > 4) { - return NEITHER; - } - int intNum = Integer.parseInt(bytes[i], 16); - if (intNum < 0) { - return NEITHER; - } - if (i == 0 && intNum != 0 && bytes[i].charAt(0) == '0') { - return NEITHER; - } - } catch (Exception e) { - return NEITHER; - } - - } - return "IPv6"; - } - - private String isValidIPv4(String IP) { - if (getDelimiterCount(IP, '.') != 3) { - return NEITHER; - } - String[] bytes = IP.split("\\."); - if (bytes.length != 4) { - return NEITHER; - } - for (String num : bytes) { - try { - int intNum = Integer.parseInt(num); - if (intNum > 255 || intNum < 0) { - return NEITHER; - } - if (intNum != 0) { - for (int i = 0; i < num.length(); i++) { - if (num.charAt(i) == '0') { - return NEITHER; - } else { - break; - } - } - } else if (intNum == 0) { - if (num.length() != 1) { - return NEITHER; - } - } - } catch (Exception e) { - return NEITHER; - } - } - return "IPv4"; - } - - private boolean hasInvalidIPV6Char(String str) { - Set set = new HashSet<>(Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F')); - for (char c : str.toCharArray()) { - if (!set.contains(c)) { - return true; - } - } - return false; - } - - private int getDelimiterCount(String ip, char delimiter) { - int count = 0; - for (char c : ip.toCharArray()) { - if (c == delimiter) { - count++; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_469.java b/src/main/java/com/fishercoder/solutions/_469.java deleted file mode 100644 index 44a7f7fb44..0000000000 --- a/src/main/java/com/fishercoder/solutions/_469.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.List; - -/** - * 469. Convex Polygon - * - * Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). - - Note: - - There are at least 3 and at most 10,000 points. - Coordinates are in the range -10,000 to 10,000. - You may assume the polygon formed by given points is always a simple polygon (Simple polygon definition). - In other words, we ensure that exactly two edges intersect at each vertex, and that edges otherwise don't intersect each other. - - Example 1: - - [[0,0],[0,1],[1,1],[1,0]] - - Answer: True - - Example 2: - - [[0,0],[0,10],[10,10],[10,0],[5,5]] - - Answer: False - - */ -public class _469 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/70706/beyond-my-knowledge-java-solution-with-in-line-explanation - */ - public boolean isConvex(List> points) { - // For each set of three adjacent points A, B, C, find the cross product AB · BC. If the sign of - // all the cross products is the same, the angles are all positive or negative (depending on the - // order in which we visit them) so the polygon is convex. - boolean gotNegative = false; - boolean gotPositive = false; - int numPoints = points.size(); - int B; - int C; - for (int A = 0; A < numPoints; A++) { - // Trick to calc the last 3 points: n - 1, 0 and 1. - B = (A + 1) % numPoints; - C = (B + 1) % numPoints; - - int crossProduct = - crossProductLength( - points.get(A).get(0), points.get(A).get(1), - points.get(B).get(0), points.get(B).get(1), - points.get(C).get(0), points.get(C).get(1)); - if (crossProduct < 0) { - gotNegative = true; - } else if (crossProduct > 0) { - gotPositive = true; - } - if (gotNegative && gotPositive) { - return false; - } - } - - // If we got this far, the polygon is convex. - return true; - } - - // Return the cross product AB x BC. - // The cross product is a vector perpendicular to AB and BC having length |AB| * |BC| * Sin(theta) and - // with direction given by the right-hand rule. For two vectors in the X-Y plane, the result is a - // vector with X and Y components 0 so the Z component gives the vector's length and direction. - private int crossProductLength(int Ax, int Ay, int Bx, int By, int Cx, int Cy) { - // Get the vectors' coordinates. - int BAx = Ax - Bx; - int BAy = Ay - By; - int BCx = Cx - Bx; - int BCy = Cy - By; - - // Calculate the Z coordinate of the cross product. - return (BAx * BCy - BAy * BCx); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_47.java b/src/main/java/com/fishercoder/solutions/_47.java deleted file mode 100644 index 433a2689bb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_47.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class _47 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/31445/really-easy-java-solution-much-easier-than-the-solutions-with-very-high-vote - */ - public List> permuteUnique(int[] nums) { - List> result = new ArrayList(); - if (nums == null || nums.length == 0) { - return result; - } - boolean[] used = new boolean[nums.length]; - List list = new ArrayList(); - Arrays.sort(nums); - dfs(nums, used, list, result); - return result; - } - - - private void dfs(int[] nums, boolean[] used, List list, List> result) { - if (list.size() == nums.length) { - result.add(new ArrayList(list)); - return; - } - for (int i = 0; i < nums.length; i++) { - if (used[i]) { - continue; - } - if (i > 0 && nums[i - 1] == nums[i] && !used[i - 1]) { - continue; - } - /** - * For this line, both !used[i-1] and used[i-1] will AC. It is because the first one makes sure when - * duplicates are selected, the order is ascending (index from small to large). However, - * the second one means the descending order. - */ - used[i] = true; - list.add(nums[i]); - dfs(nums, used, list, result); - used[i] = false; - list.remove(list.size() - 1); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_471.java b/src/main/java/com/fishercoder/solutions/_471.java deleted file mode 100644 index c53f1f33f9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_471.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 471. Encode String with Shortest Length - * - Given a non-empty string, encode the string such that its encoded length is the shortest. - - The encoding rule is: k[encoded_string], where the encoded_string inside the square - brackets is being repeated exactly k times. - - Note: - k will be a positive integer and encoded string will not be empty or have extra space. - You may assume that the input string contains only lowercase English letters. The string's length is at most 160. - If an encoding process does not make the string shorter, - then do not encode it. If there are several solutions, return any of them is fine. - - Example 1: - - Input: "aaa" - Output: "aaa" - Explanation: There is no way to encode it such that it is shorter than the input string, so we do not encode it. - Example 2: - - Input: "aaaaa" - Output: "5[a]" - Explanation: "5[a]" is shorter than "aaaaa" by 1 character. - Example 3: - - Input: "aaaaaaaaaa" - Output: "10[a]" - Explanation: "a9[a]" or "9[a]a" are also valid solutions, both of them have the same length = 5, which is the same as "10[a]". - Example 4: - - Input: "aabcaabcd" - Output: "2[aabc]d" - Explanation: "aabc" occurs twice, so one answer can be "2[aabc]d". - Example 5: - - Input: "abbbabbbcabbbabbbc" - Output: "2[2[abbb]c]" - Explanation: "abbbabbbc" occurs twice, but "abbbabbbc" can also be encoded to "2[abbb]c", so one answer can be "2[2[abbb]c]". - */ -public class _471 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/71963/accepted-solution-in-java - */ - public String encode(String s) { - String[][] dp = new String[s.length()][s.length()]; - - for (int l = 0; l < s.length(); l++) { - for (int i = 0; i < s.length() - l; i++) { - int j = i + l; - String substr = s.substring(i, j + 1); - // Checking if string length < 5. In that case, we know that encoding will not help. - if (j - i < 4) { - dp[i][j] = substr; - } else { - dp[i][j] = substr; - // Loop for trying all results that we get after dividing the strings into 2 and combine the results of 2 substrings - for (int k = i; k < j; k++) { - if ((dp[i][k] + dp[k + 1][j]).length() < dp[i][j].length()) { - dp[i][j] = dp[i][k] + dp[k + 1][j]; - } - } - - // Loop for checking if string can itself found some pattern in it which could be repeated. - for (int k = 0; k < substr.length(); k++) { - String repeatStr = substr.substring(0, k + 1); - if (repeatStr != null - && substr.length() % repeatStr.length() == 0 - && substr.replaceAll(repeatStr, "").length() == 0) { - String ss = substr.length() / repeatStr.length() + "[" + dp[i][i + k] + "]"; - if (ss.length() < dp[i][j].length()) { - dp[i][j] = ss; - } - } - } - } - } - } - return dp[0][s.length() - 1]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_472.java b/src/main/java/com/fishercoder/solutions/_472.java deleted file mode 100644 index 405e96640f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_472.java +++ /dev/null @@ -1,178 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 472. Concatenated Words - * - * Given a list of words, please write a program that returns all concatenated words in the given list of words. - - A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array. - - Example: - Input: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"] - - Output: ["catsdogcats","dogcatsdog","ratcatdogcat"] - - Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats"; - "dogcatsdog" can be concatenated by "dog", "cats" and "dog"; - "ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat". - Note: - The number of elements of the given array will not exceed 10,000 - The length sum of elements in the given array will not exceed 600,000. - All the input string will only include lower case letters. - The returned elements order does not matter. - - */ - -public class _472 { - - public static class Solution1 { - private TrieNode root; - private int maxWordLen; - - public List findAllConcatenatedWordsInADict(String[] words) { - ResultType result = buildTrie(words); - root = result.root; - maxWordLen = result.maxWordLen; - - List validConcatenatedWords = new ArrayList(); - for (String word : words) { - if (word == null || word.length() == 0) { - continue; - } - remove(word, root);/** every word is comprised of every word itself, thus this word itself needs to be removed first for checking it*/ - int n = word.length(); - boolean[] dp = new boolean[n + 1]; - dp[0] = true; - - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= i && j <= maxWordLen; j++) { - if (!dp[i - j]) { - continue; - } - - String subWord = word.substring(i - j, i); - if (contains(subWord, root)) { - dp[i] = true; - break; - } - } - } - - if (dp[n]) { - validConcatenatedWords.add(word); - } - undoRemove(word, root); - } - return validConcatenatedWords; - } - - public ResultType buildTrie(String[] words) { - ResultType result = new ResultType(); - - TrieNode root = new TrieNode(); - int maxWordLen = 0; - - for (String word : words) { - maxWordLen = Math.max(maxWordLen, word.length()); - char[] chars = word.toCharArray(); - TrieNode node = root; - for (int i = 0; i < chars.length; i++) { - char c = chars[i]; - if (node.children[c - 'a'] == null) { - node.children[c - 'a'] = new TrieNode(); - } - node = node.children[c - 'a']; - } - node.isWord = true; - } - - result.root = root; - result.maxWordLen = maxWordLen; - return result; - } - - public class ResultType { - int maxWordLen; - TrieNode root; - } - - // Returns true if the word is in the trie. - public boolean contains(String word, TrieNode root) { - TrieNode node = root; - for (int i = 0; i < word.length(); i++) { - if (node.children[word.charAt(i) - 'a'] == null) { - return false; - } - node = node.children[word.charAt(i) - 'a']; - } - return node.isWord; - } - - // mark that word on - public void undoRemove(String word, TrieNode root) { - TrieNode node = root; - for (int i = 0; i < word.length(); i++) { - node = node.children[word.charAt(i) - 'a']; - } - node.isWord = true; - } - - // mark that word off, we are not really deleting that word - public void remove(String word, TrieNode root) { - TrieNode node = root; - for (int i = 0; i < word.length(); i++) { - node = node.children[word.charAt(i) - 'a']; - } - node.isWord = false; - } - - class TrieNode { - boolean isWord; - TrieNode[] children = new TrieNode[26]; - - public TrieNode() { - } - } - } - - public static class Solution2 { - public List findAllConcatenatedWordsInADict(String[] words) { - List result = new ArrayList<>(); - Set preWords = new HashSet<>(); - /**Words could only be formed by other words that are shorter than itself, so we sort them based on their lengths first.*/ - Arrays.sort(words, (s1, s2) -> s1.length() - s2.length()); - - for (int i = 0; i < words.length; i++) { - if (canForm(words[i], preWords)) { - result.add(words[i]); - } - preWords.add(words[i]); - } - - return result; - } - - boolean canForm(String word, Set dict) { - if (dict.isEmpty()) { - return false; - } - boolean[] dp = new boolean[word.length() + 1]; - dp[0] = true; - for (int i = 1; i <= word.length(); i++) { - for (int j = 0; j < i; j++) { - if (dp[j] && dict.contains(word.substring(j, i))) { - dp[i] = true; - break; - } - } - } - return dp[word.length()]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_473.java b/src/main/java/com/fishercoder/solutions/_473.java deleted file mode 100644 index cdded65e24..0000000000 --- a/src/main/java/com/fishercoder/solutions/_473.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 473. Matchsticks to Square - * - * Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, - * please find out a way you can make one square by using up all those matchsticks. - * You should not break any stick, but you can link them up, and each matchstick must be used exactly one time. - * Your input will be several matchsticks the girl has, represented with their stick length. - * Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has. - - Example 1: - Input: [1,1,2,2,2] - Output: true - Explanation: You can form a square with length 2, one side of the square came two sticks with length 1. - - Example 2: - Input: [3,3,3,3,4] - Output: false - Explanation: You cannot find a way to form a square with all the matchsticks. - - Note: - The length sum of the given matchsticks is in the range of 0 to 10^9. - The length of the given matchstick array will not exceed 15. - */ -public class _473 { - - public static class Solution1 { - /** - * Partially inspired by: https://discuss.leetcode.com/topic/72107/java-dfs-solution-with-explanation/2 - * One hidden requirement: you'll have to use up all of the given matchsticks, nothing could be left behind. - */ - public boolean makesquare(int[] nums) { - if (nums == null || nums.length < 4) { - return false; - } - Arrays.sort(nums); - reverse(nums); - int sum = 0; - for (int i : nums) { - sum += i; - } - if (sum % 4 != 0) { - return false; - } - - return dfs(nums, new int[4], 0, sum / 4); - } - - private boolean dfs(int[] nums, int[] sums, int index, int target) { - if (index == nums.length) { - if (sums[0] == target && sums[1] == target && sums[2] == target) { - return true; - } - return false; - } - for (int i = 0; i < 4; i++) { - if (sums[i] + nums[index] > target) { - continue; - } - sums[i] += nums[index]; - if (dfs(nums, sums, index + 1, target)) { - return true; - } - sums[i] -= nums[index]; - } - return false; - } - - private void reverse(int[] nums) { - int left = 0; - int right = nums.length - 1; - while (left < right) { - int tmp = nums[left]; - nums[left] = nums[right]; - nums[right] = tmp; - left++; - right--; - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_474.java b/src/main/java/com/fishercoder/solutions/_474.java deleted file mode 100644 index c905e88713..0000000000 --- a/src/main/java/com/fishercoder/solutions/_474.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 474. Ones and Zeroes - * - * In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue. - * For now, suppose you are a dominator of m 0s and n 1s respectively. - * On the other hand, there is an array with strings consisting of only 0s and 1s. - * Now your task is to find the maximum number of strings that you can form with given m 0s and n 1s. - * Each 0 and 1 can be used at most once. - - Note: - - The given numbers of 0s and 1s will both not exceed 100. The size of given string array won't exceed 600. - - Example 1: - Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3 - Output: 4 - Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0” - - Example 2: - Input: Array = {"10", "0", "1"}, m = 1, n = 1 - Output: 2 - Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1". - - */ -public class _474 { - public static class Solution1 { - public int findMaxForm(String[] strs, int m, int n) { - int[][] dp = new int[m + 1][n + 1]; - for (String str : strs) { - int[] count = count(str); - for (int i = m; i >= count[0]; i--) { - for (int j = n; j >= count[1]; j--) { - dp[i][j] = Math.max(dp[i][j], dp[i - count[0]][j - count[1]] + 1); - } - } - } - return dp[m][n]; - } - - private int[] count(String str) { - int[] res = new int[2]; - for (int i = 0; i < str.length(); i++) { - res[str.charAt(i) - '0']++; - } - return res; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_475.java b/src/main/java/com/fishercoder/solutions/_475.java deleted file mode 100644 index 4a97000a9a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_475.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 475. - * - Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. - - Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters. - - So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters. - - Note: - Numbers of houses and heaters you are given are non-negative and will not exceed 25000. - Positions of houses and heaters you are given are non-negative and will not exceed 10^9. - As long as a house is in the heaters' warm radius range, it can be warmed. - All the heaters follow your radius standard and the warm radius will the same. - - Example 1: - Input: [1,2,3],[2] - Output: 1 - Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed. - - Example 2: - Input: [1,2,3,4],[1,4] - Output: 1 - Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed. - */ -public class _475 { - - public static class Solution1 { - //credit: https://discuss.leetcode.com/topic/71460/short-and-clean-java-binary-search-solution - public int findRadius(int[] houses, int[] heaters) { - Arrays.sort(heaters); - int radius = Integer.MIN_VALUE; - for (int house : houses) { - int index = Arrays.binarySearch(heaters, house); - if (index < 0) { - index = ~index; - } - int distance1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE; - int distance2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE; - - radius = Math.max(radius, Math.min(distance1, distance2)); - } - return radius; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_477.java b/src/main/java/com/fishercoder/solutions/_477.java deleted file mode 100644 index 940d14c3be..0000000000 --- a/src/main/java/com/fishercoder/solutions/_477.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 477. Total Hamming Distance - * - * The Hamming distance between two integers is the number of positions at which the corresponding bits are different. - - Now your job is to find the total Hamming distance between all pairs of the given numbers. - - Example: - Input: 4, 14, 2 - Output: 6 - Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just - showing the four bits relevant in this case). So the answer will be: - HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. - - Note: - Elements of the given array are in the range of 0 to 10^9 - Length of the array will not exceed 10^4. - */ -public class _477 { - public static class Solution1 { - public int totalHammingDistance(int[] nums) { - int r = 0; - for (int i = 0; i < 32; i++) { - int one = 0; - int zero = 0; - int bit = 1 << i; - for (int n : nums) { - if ((n & bit) != 0) { - one++; - } else { - zero++; - } - } - r += one * zero; - } - return r; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_48.java b/src/main/java/com/fishercoder/solutions/_48.java deleted file mode 100644 index 85fcb91f5d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_48.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.utils.CommonUtils; - -public class _48 { - - /**Note: this is an n*n matrix, in other words, it's a square, this makes it easier as well.*/ - - public static class Solution1 { - //Time: O(n^2) - //Space: O(1) - public void rotate(int[][] matrix) { - /**First swap the elements on the diagonal, then reverse each row: - * 1, 2, 3 1, 4, 7 7, 4, 1 - * 4, 5, 6 becomes 2, 5, 8 becomes 8, 5, 2 - * 7, 8, 9 3, 6, 9 9, 6, 3 - **/ - int m = matrix.length; - for (int i = 0; i < m; i++) { - for (int j = i; j < m; j++) { - /**ATTN: j starts from i, so that the diagonal changes with itself, results in no change.*/ - int tmp = matrix[i][j]; - matrix[i][j] = matrix[j][i]; - matrix[j][i] = tmp; - } - } - CommonUtils.print2DIntArray(matrix); - - /**then reverse*/ - for (int i = 0; i < m; i++) { - int left = 0; - int right = m - 1; - while (left < right) { - int tmp = matrix[i][left]; - matrix[i][left] = matrix[i][right]; - matrix[i][right] = tmp; - left++; - right--; - } - } - } - } - - public static class Solution2 { - /**First swap the rows bottom up, then swap the element on the diagonal: - * - * 1, 2, 3 7, 8, 9 7, 4, 1 - * 4, 5, 6 becomes 4, 5, 6 becomes 8, 5, 2 - * 7, 8, 9 1, 2, 3 9, 6, 3 - * - * Time: O(n^2) - * Space: O(1) - */ - public void rotate(int[][] matrix) { - int m = matrix.length; - int n = matrix[0].length; - int top = 0; - int bottom = n - 1; - while (top < bottom) { - int[] tmp = matrix[top]; - matrix[top] = matrix[bottom]; - matrix[bottom] = tmp; - top++; - bottom--; - } - - for (int i = 0; i < m; i++) { - for (int j = i + 1; j < n; j++) { - int tmp = matrix[i][j]; - matrix[i][j] = matrix[j][i]; - matrix[j][i] = tmp; - } - } - } - } - - public static class Solution3 { - public void rotate(int[][] matrix) { - int n = matrix.length; - for (int i = 0; i < n / 2; i++) { - for (int j = i; j < n - i - 1; j++) { - //save the top - int top = matrix[i][j]; - - //move left to top - matrix[i][j] = matrix[n - 1 - j][i]; - - //move bottom to left - matrix[n - 1 - j][i] = matrix[n - i - 1][n - 1 - j]; - - //move right to bottom - matrix[n - i - 1][n - 1 - j] = matrix[j][n - i - 1]; - - //move top to right - matrix[j][n - i - 1] = top; - } - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_480.java b/src/main/java/com/fishercoder/solutions/_480.java deleted file mode 100644 index b5817534b3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_480.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Collections; -import java.util.PriorityQueue; - -/** - * 480. Sliding Window Median - * - * Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. - - Examples: - [2,3,4] , the median is 3 - - [2,3], the median is (2 + 3) / 2 = 2.5 - - Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. - You can only see the k numbers in the window. Each time the sliding window moves right by one position. - Your job is to output the median array for each window in the original array. - - For example, - Given nums = [1,3,-1,-3,5,3,6,7], and k = 3. - - Window position Median - --------------- ----- - [1 3 -1] -3 5 3 6 7 1 - 1 [3 -1 -3] 5 3 6 7 -1 - 1 3 [-1 -3 5] 3 6 7 -1 - 1 3 -1 [-3 5 3] 6 7 3 - 1 3 -1 -3 [5 3 6] 7 5 - 1 3 -1 -3 5 [3 6 7] 6 - - Therefore, return the median sliding window as [1,-1,-1,3,5,6]. - - Note: - You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array. - */ -public class _480 { - public static class Solution1 { - - /** - * You cannot simply use minus sign '-' to denote the descending order, because e.g. 3 and -3 might both exist in this array, - * so we'll have to use the original numbers themselves to store in the heaps. - */ - private PriorityQueue minHeap = new PriorityQueue<>(); - private PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); - - public double[] medianSlidingWindow(int[] nums, int k) { - int n = nums.length - k + 1; - if (n <= 0) { - return new double[0]; - } - double[] result = new double[n]; - - for (int i = 0; i <= nums.length; i++) { - if (i >= k) { - result[i - k] = getMedian(); - remove(nums[i - k]); - } - if (i < nums.length) { - add(nums[i]); - } - } - - return result; - } - - private double getMedian() { - if (maxHeap.isEmpty() && minHeap.isEmpty()) { - return 0; - } - - if (maxHeap.size() == minHeap.size()) { - return ((double) maxHeap.peek() + (double) minHeap.peek()) / 2.0; - } else { - return (double) minHeap.peek(); - } - } - - private void remove(int num) { - if (num < getMedian()) { - maxHeap.remove(num); - } else { - minHeap.remove(num); - } - if (maxHeap.size() > minHeap.size()) { - minHeap.add(maxHeap.poll()); - } - if (minHeap.size() - maxHeap.size() > 1) { - maxHeap.add(minHeap.poll()); - } - } - - private void add(int num) { - if (num < getMedian()) { - maxHeap.add(num); - } else { - minHeap.add(num); - } - if (maxHeap.size() > minHeap.size()) { - minHeap.add(maxHeap.poll()); - } - if (minHeap.size() - maxHeap.size() > 1) { - maxHeap.add(minHeap.poll()); - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_481.java b/src/main/java/com/fishercoder/solutions/_481.java deleted file mode 100644 index 0cfb8627f9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_481.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 481. Magical String - * - * A magical string S consists of only '1' and '2' and obeys the following rules: - - The string S is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string S itself. - - The first few elements of string S is the following: S = "1221121221221121122……" - - If we group the consecutive '1's and '2's in S, it will be: - - 1 22 11 2 1 22 1 22 11 2 11 22 ...... - - and the occurrences of '1's or '2's in each group are: - - 1 2 2 1 1 2 1 2 2 1 2 2 ...... - - You can see that the occurrence sequence above is the S itself. - - Given an integer N as input, return the number of '1's in the first N number in the magical string S. - - Note: N will not exceed 100,000. - - Example 1: - Input: 6 - Output: 3 - Explanation: The first 6 elements of magical string S is "12211" and it contains three 1's, so return 3. - */ -public class _481 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/74917/simple-java-solution-using-one-array-and-two-pointers - * Algorithm: - * - * 1. Create an int array a and initialize the first 3 elements with 1, 2, 2. - * 2. Create two pointers head and tail. head points to the number which will be used to generate new numbers. - * tail points to the next empty position to put the new number. Then keep generating new numbers until tail >= n. - * 3. Need to create the array 1 element more than n to avoid overflow because the last round head might points to a number 2. - * 4. A trick to flip number back and forth between 1 and 2: num = num ^ 3 - */ - public int magicalString(int n) { - if (n <= 0) { - return 0; - } - if (n <= 3) { - return 1; - } - - int[] a = new int[n + 1]; - a[0] = 1; - a[1] = 2; - a[2] = 2; - - int head = 2; - int tail = 3; - int num = 1; - int result = 1; - - while (tail < n) { - for (int i = 0; i < a[head]; i++) { - a[tail] = num; - if (num == 1 && tail < n) { - result++; - } - tail++; - } - num = num ^ 3; - head++; - } - - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_482.java b/src/main/java/com/fishercoder/solutions/_482.java deleted file mode 100644 index 637478db14..0000000000 --- a/src/main/java/com/fishercoder/solutions/_482.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 482. License Key Formatting - * - * Now you are given a string S, which represents a software license key which we would like to format. - * The string S is composed of alphanumerical characters and dashes. - * The dashes split the alphanumerical characters within the string into groups. - * (i.e. if there are m dashes, the string is split into m+1 groups). - * The dashes in the given string are possibly misplaced. - * We want each group of characters to be of length K - * (except for possibly the first group, which could be shorter, but still must contain at least one character). - * To satisfy this requirement, we will reinsert dashes. Additionally, all the lower case letters in the string must be converted to upper case. - * So, you are given a non-empty string S, representing a license key to format, and an integer K. - * And you need to return the license key formatted according to the description above. - - Example 1: - Input: S = "2-4A0r7-4k", K = 4 - Output: "24A0-R74K" - Explanation: The string S has been split into two parts, each part has 4 characters. - - Example 2: - Input: S = "2-4A0r7-4k", K = 3 - Output: "24-A0R-74K" - Explanation: The string S has been split into three parts, each part has 3 characters except the first part as it could be shorter as said above. - - Note: - The length of string S will not exceed 12,000, and K is a positive integer. - String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-). - String S is non-empty. - */ -public class _482 { - public static class Solution1 { - public String licenseKeyFormatting(String S, int K) { - StringBuilder stringBuilder = new StringBuilder(); - char[] SChars = S.toCharArray(); - for (int i = S.length() - 1, j = 0; i >= 0; ) { - if (j < K) { - if (SChars[i] != '-') { - if (SChars[i] >= 'a' && SChars[i] <= 'z') { - stringBuilder.append(Character.toUpperCase(SChars[i])); - } else { - stringBuilder.append(SChars[i]); - } - j++; - } - i--; - } else if (j == K) { - j = 0; - stringBuilder.append('-'); - } - } - if (stringBuilder.length() > 1 && stringBuilder.substring(stringBuilder.length() - 1).equals("-")) { - return stringBuilder.reverse().substring(1); - } - return stringBuilder.reverse().toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_483.java b/src/main/java/com/fishercoder/solutions/_483.java deleted file mode 100644 index 5e5d91c70a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_483.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -import java.math.BigInteger; - -/** - * 483. Smallest Good Base - * - * For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. - - Now given a string representing n, you should return the smallest good base of n in string format. - - Example 1: - Input: "13" - Output: "3" - Explanation: 13 base 3 is 111. - - Example 2: - Input: "4681" - Output: "8" - Explanation: 4681 base 8 is 11111. - - Example 3: - Input: "1000000000000000000" - Output: "999999999999999999" - Explanation: 1000000000000000000 base 999999999999999999 is 11. - - Note: - The range of n is [3, 10^18]. - The string representing n is always valid and will not have leading zeros. - - */ -public class _483 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/82130/java-solution-with-hand-writing-explain - */ - public String smallestGoodBase(String n) { - long nn = Long.parseLong(n); - long res = 0; - for (int k = 60; k >= 2; k--) { - long start = 2; - long end = nn; - while (start < end) { - long m = start + (end - start) / 2; - - BigInteger left = BigInteger.valueOf(m); - left = left.pow(k).subtract(BigInteger.ONE); - BigInteger right = BigInteger.valueOf(nn).multiply(BigInteger.valueOf(m).subtract(BigInteger.ONE)); - int cmr = left.compareTo(right); - if (cmr == 0) { - res = m; - break; - } else if (cmr < 0) { - start = m + 1; - } else { - end = m; - } - } - - if (res != 0) { - break; - } - } - return "" + res; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_484.java b/src/main/java/com/fishercoder/solutions/_484.java deleted file mode 100644 index 684c4ec46e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_484.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 484. Find Permutation - * - * By now, you are given a secret signature consisting of character 'D' and 'I'. - * 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers. - * And our secret signature was constructed by a special integer array, which contains uniquely all the different number from 1 to n (n is the length of the secret signature plus 1). - * For example, the secret signature "DI" can be constructed by array [2,1,3] or [3,1,2], - * but won't be constructed by array [3,2,4] or [2,1,3,4], which are both illegal constructing special string that can't represent the "DI" secret signature. - - On the other hand, now your job is to find the lexicographically smallest permutation of [1, 2, ... n] could refer to the given secret signature in the input. - - Example 1: - Input: "I" - Output: [1,2] - Explanation: [1,2] is the only legal initial spectial string can construct secret signature "I", where the number 1 and 2 construct an increasing relationship. - - Example 2: - Input: "DI" - Output: [2,1,3] - Explanation: Both [2,1,3] and [3,1,2] can construct the secret signature "DI", - but since we want to find the one with the smallest lexicographical permutation, you need to output [2,1,3] - - Note: - The input string will only contain the character 'D' and 'I'. - The length of input string is a positive integer and will not exceed 10,000 - */ -public class _484 { - public static class Solution1 { - - /** - * credit:https://discuss.leetcode.com/topic/76221/java-o-n-clean-solution-easy-to-understand - * - * For example, given IDIIDD we start with sorted sequence 1234567 - * Then for each k continuous D starting at index i we need to reverse [i, i+k] portion of the sorted sequence. - * - * e.g. - * IDIIDD - * - * 1234567 // sorted - * 1324765 // answer - */ - public int[] findPermutation(String s) { - int[] result = new int[s.length() + 1]; - for (int i = 0; i <= s.length(); i++) { - result[i] = i + 1; - } - for (int i = 0; i < s.length(); i++) { - if (s.charAt(i) == 'D') { - int left = i; - while (i < s.length() && s.charAt(i) == 'D') { - i++; - } - reverse(result, left, i); - } - } - return result; - } - - private void reverse(int[] result, int left, int i) { - while (left < i) { - int temp = result[left]; - result[left] = result[i]; - result[i] = temp; - left++; - i--; - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_485.java b/src/main/java/com/fishercoder/solutions/_485.java deleted file mode 100644 index 22cf8bad41..0000000000 --- a/src/main/java/com/fishercoder/solutions/_485.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 485. Max Consecutive Ones - * - * Given a binary array, find the maximum number of consecutive 1s in this array. - * - * Example 1: - * - * Input: [1,1,0,1,1,1] - * Output: 3 - * Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. - * - * Note: - * The input array will only contain 0 and 1. - * The length of input array is a positive integer and will not exceed 10,000 - * */ -public class _485 { - public static class Solution1 { - public int findMaxConsecutiveOnes(int[] nums) { - int maxLen = 0; - for (int i = 0; i < nums.length; i++) { - int currentLen = 0; - while (i < nums.length && nums[i] == 1) { - i++; - currentLen++; - } - maxLen = Math.max(maxLen, currentLen); - } - return maxLen; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_486.java b/src/main/java/com/fishercoder/solutions/_486.java deleted file mode 100644 index 1d8d786816..0000000000 --- a/src/main/java/com/fishercoder/solutions/_486.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 486. Predict the Winner - * - * Given an array of scores that are non-negative integers. - * Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. - * Each time a player picks a number, that number will not be available for the next player. - * This continues until all the scores have been chosen. - * The player with the maximum score wins. - * Given an array of scores, predict whether player 1 is the winner. - * You can assume each player plays to maximize his score. - - Example 1: - Input: [1, 5, 2] - Output: False - Explanation: Initially, player 1 can choose between 1 and 2. - If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). - So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. - Hence, player 1 will never be the winner and you need to return False. - - Example 2: - Input: [1, 5, 233, 7] - Output: True - Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233. - Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. - - Note: - 1 <= length of the array <= 20. - Any scores in the given array are non-negative integers and will not exceed 10,000,000. - If the scores of both players are equal, then player 1 is still the winner. - */ -public class _486 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/76312/java-1-line-recursion-solution - * Explanation - * So assuming the sum of the array it SUM, so eventually player1 and player2 will split the SUM between themselves. - * For player1 to win, he/she has to get more than what player2 gets. - * If we think from the prospective of one player, then what he/she gains each time is a plus, - * while, what the other player gains each time is a minus. Eventually if player1 can have a >0 total, player1 can win. - * - * Helper function simulate this process. In each round: - * if e==s, there is no choice but have to select nums[s] - * otherwise, this current player has 2 options: - * --> nums[s]-helper(nums,s+1,e): this player select the front item, leaving the other player a choice from s+1 to e - * --> nums[e]-helper(nums,s,e-1): this player select the tail item, leaving the other player a choice from s to e-1 - * Then take the max of these two options as this player's selection, return it. - */ - public boolean predictTheWinner(int[] nums) { - return helper(nums, 0, nums.length - 1, new Integer[nums.length][nums.length]) >= 0; - } - - private int helper(int[] nums, int start, int end, Integer[][] mem) { - if (mem[start][end] == null) { - mem[start][end] = start == end ? nums[end] : - Math.max(nums[end] - helper(nums, start, end - 1, mem), - nums[start] - helper(nums, start + 1, end, mem)); - } - return mem[start][end]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_487.java b/src/main/java/com/fishercoder/solutions/_487.java deleted file mode 100644 index b7365b0edc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_487.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder.solutions; - -/** - * Given a binary array, find the maximum number of consecutive 1s in this array. - - Example 1: - Input: [1,1,0,1,1,1] - Output: 3 - Explanation: The first two digits or the last three digits are consecutive 1s. - The maximum number of consecutive 1s is 3. - Note: - - The input array will only contain 0 and 1. - The length of input array is a positive integer and will not exceed 10,000 - */ -public class _487 { - - public static int findMaxConsecutiveOnes(int[] nums) { - int maxOnes = 0; - for (int i = 0; i < nums.length; i++) { - int newOnes = 0; - while (i < nums.length && nums[i] == 1) { - newOnes++; - i++; - } - maxOnes = Math.max(maxOnes, newOnes); - } - return maxOnes; - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_488.java b/src/main/java/com/fishercoder/solutions/_488.java deleted file mode 100644 index b5e79cb24d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_488.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 488. Zuma Game - * - * Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). - * You also have several balls in your hand. - - Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). - Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed. - - Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1. - - Examples: - - Input: "WRRBBW", "RB" - Output: -1 - Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW - - Input: "WWRRBBWW", "WRBRW" - Output: 2 - Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty - - Input:"G", "GGGGG" - Output: 2 - Explanation: G -> G[G] -> GG[G] -> empty - - Input: "RBYYBBRRB", "YRBGB" - Output: 3 - Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty - - Note: - - You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color. - The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input. - The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input. - Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'. - */ -public class _488 { - - public static class Solution1 { - - /** - * credit: https://discuss.leetcode.com/topic/79820/short-java-solution-beats-98 - * Two layer of recursion, pretty cool! - */ - - int maxcount = 6; // the max balls you need will not exceed 6 since "The number of balls in your hand won't exceed 5" - - public int findMinStep(String board, String hand) { - int[] handCount = new int[26]; - for (int i = 0; i < hand.length(); ++i) { - ++handCount[hand.charAt(i) - 'A']; - } - int result = dfs(board + "#", handCount); // append a "#" to avoid special process while j==board.length, make the code shorter. - return result == maxcount ? -1 : result; - } - - private int dfs(String s, int[] handCount) { - s = removeConsecutive(s); - if (s.equals("#")) { - return 0; - } - int result = maxcount; - int need = 0; - for (int i = 0, j = 0; j < s.length(); ++j) { - if (s.charAt(j) == s.charAt(i)) { - continue; - } - need = 3 - (j - i); //balls need to remove current consecutive balls. - if (handCount[s.charAt(i) - 'A'] >= need) { - handCount[s.charAt(i) - 'A'] -= need; - result = Math.min(result, need + dfs(s.substring(0, i) + s.substring(j), handCount)); - handCount[s.charAt(i) - 'A'] += need; - } - i = j; - } - return result; - } - - //remove consecutive balls longer than 3 - private String removeConsecutive(String board) { - for (int i = 0, j = 0; j < board.length(); ++j) { - if (board.charAt(j) == board.charAt(i)) { - continue; - } - if (j - i >= 3) { - return removeConsecutive(board.substring(0, i) + board.substring(j)); - } else { - i = j; - } - } - return board; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_49.java b/src/main/java/com/fishercoder/solutions/_49.java deleted file mode 100644 index 7374a0ea85..0000000000 --- a/src/main/java/com/fishercoder/solutions/_49.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class _49 { - - public static class Solution1 { - public List> groupAnagrams(String[] strs) { - Map> map = new HashMap<>(); - for (String word : strs) { - char[] c = word.toCharArray(); - Arrays.sort(c); - String key = new String(c); - if (!map.containsKey(key)) { - map.put(key, new ArrayList<>()); - } - map.get(key).add(word); - } - return new ArrayList<>(map.values()); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_490.java b/src/main/java/com/fishercoder/solutions/_490.java deleted file mode 100644 index 5be7b088a0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_490.java +++ /dev/null @@ -1,107 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. - - Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination. - - The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes. - - Example 1 - - Input 1: a maze represented by a 2D array - - 0 0 1 0 0 - 0 0 0 0 0 - 0 0 0 1 0 - 1 1 0 1 1 - 0 0 0 0 0 - - Input 2: start coordinate (rowStart, colStart) = (0, 4) - Input 3: destination coordinate (rowDest, colDest) = (4, 4) - - Output: true - Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right. - - Example 2 - - Input 1: a maze represented by a 2D array - - 0 0 1 0 0 - 0 0 0 0 0 - 0 0 0 1 0 - 1 1 0 1 1 - 0 0 0 0 0 - - Input 2: start coordinate (rowStart, colStart) = (0, 4) - Input 3: destination coordinate (rowDest, colDest) = (3, 2) - - Output: false - Explanation: There is no way for the ball to stop at the destination. - - Note: - There is only one ball and one destination in the maze. - Both the ball and the destination exist on an empty space, and they will not be at the same position initially. - The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls. - The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100. - */ -public class _490 { - /** - * BFS: the key part of this algorithm is that: this is a ball that won't stop rolling until it hits a wall. - * This means we'll have to store all the empty spaces where the ball was forced to stop into the queue, these are - * the only places where the next starting points could be. - * Then we use BFS to traverse through all four directions of each starting point. - *

- * Also, another point to note is: it must be a stop point for the ball. - */ - public boolean hasPath(int[][] maze, int[] start, int[] destination) { - if (start[0] == destination[0] && destination[0] == destination[1]) { - return true; - } - final int[] directions = new int[]{-1, 0, 1, 0, -1}; - Queue queue = new LinkedList<>(); - queue.offer(new Point(start[0], start[1])); - int m = maze.length; - int n = maze[0].length; - boolean[][] visited = new boolean[m][n]; - visited[start[0]][start[1]] = true; - - while (!queue.isEmpty()) { - Point curr = queue.poll(); - int x = curr.x; - int y = curr.y;//keep the original value - for (int i = 0; i < directions.length - 1; i++) { - int xx = x; - int yy = y;//use temp variables to move - while (xx >= 0 && yy >= 0 && xx < m && yy < n && maze[xx][yy] == 0) { - xx += directions[i]; - yy += directions[i + 1]; - } - xx -= directions[i]; - yy -= directions[i + 1]; - if (visited[xx][yy]) { - continue; - } - visited[xx][yy] = true; - if (destination[0] == xx && destination[1] == yy) { - return true; - } - queue.offer(new Point(xx, yy)); - } - } - return false; - } - - class Point { - int x; - int y; - - public Point(int x, int y) { - this.x = x; - this.y = y; - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_491.java b/src/main/java/com/fishercoder/solutions/_491.java deleted file mode 100644 index 39bccc343f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_491.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 491. Increasing Subsequences - * - * Given an integer array, your task is to find all the different possible increasing subsequences of the given array, - * and the length of an increasing subsequence should be at least 2 . - - Example: - Input: [4, 6, 7, 7] - Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]] - - Note: - The length of the given array will not exceed 15. - The range of integer in the given array is [-100,100]. - The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence. - */ -public class _491 { - - public static class Solution1 { - public List> findSubsequences(int[] nums) { - if (nums == null || nums.length == 1) { - return new ArrayList<>(); - } - Set> answer = new HashSet<>(); - List list = new ArrayList<>(); - return new ArrayList<>(backtracking(nums, 0, list, answer)); - } - - private Set> backtracking(int[] nums, int start, List currList, - Set> answer) { - if (currList.size() >= 2) { - answer.add(new ArrayList<>(currList)); - } - for (int i = start; i < nums.length; i++) { - if (currList.size() == 0 || currList.get(currList.size() - 1) <= nums[i]) { - currList.add(nums[i]); - backtracking(nums, i + 1, currList, answer); - currList.remove(currList.size() - 1); - } - } - return answer; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_492.java b/src/main/java/com/fishercoder/solutions/_492.java deleted file mode 100644 index d4bfb8352b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_492.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 492. Construct the Rectangle - * - * For a web developer, it is very important to know how to design a web page's size. - * So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, - * whose length L and width W satisfy the following requirements: - - 1. The area of the rectangular web page you designed must equal to the given target area. - 2. The width W should not be larger than the length L, which means L >= W. - 3. The difference between length L and width W should be as small as possible. - - You need to output the length L and the width W of the web page you designed in sequence. - Example: - Input: 4 - Output: [2, 2] - Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. - But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2. - - Note: - The given area won't exceed 10,000,000 and is a positive integer - The web page's width and length you designed must be positive integers. - - */ -public class _492 { - - public static class Solution1 { - public int[] constructRectangle(int area) { - int i = 0; - int j = area; - int[] result = new int[2]; - while (i <= j) { - long product = i * j; - if (product == area) { - result[0] = j--; - result[1] = i++; - } else if (product > area) { - j--; - } else { - i++; - } - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_493.java b/src/main/java/com/fishercoder/solutions/_493.java deleted file mode 100644 index f143d5e80c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_493.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 493. Reverse Pairs - * - * Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. - - You need to return the number of important reverse pairs in the given array. - - Example1: - - Input: [1,3,2,3,1] - Output: 2 - Example2: - - Input: [2,4,3,5,1] - Output: 3 - - Note: - The length of the given array will not exceed 50,000. - All the numbers in the input array are in the range of 32-bit integer. - */ -public class _493 { - - public static class Solution1 { - - /** - * reference: https://discuss.leetcode.com/topic/78933/very-short-and-clear-mergesort-bst-java-solutions - */ - public int reversePairs(int[] nums) { - return mergeSort(nums, 0, nums.length - 1); - } - - private int mergeSort(int[] nums, int start, int end) { - if (start >= end) { - return 0; - } - int mid = start + (end - start) / 2; - int cnt = mergeSort(nums, start, mid) + mergeSort(nums, mid + 1, end); - for (int i = start, j = mid + 1; i <= mid; i++) { - /**it has to be 2.0 instead of 2, otherwise it's going to stack overflow, i.e. test3 is going to fail*/ - while (j <= end && nums[i] > nums[j] * 2.0) { - j++; - } - cnt += j - (mid + 1); - } - Arrays.sort(nums, start, end + 1); - return cnt; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_494.java b/src/main/java/com/fishercoder/solutions/_494.java deleted file mode 100644 index 78ddf9159b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_494.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 494. Target Sum - * - * You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. - * Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. - * Find out how many ways to assign symbols to make sum of integers equal to target S. - - Example 1: - - Input: nums is [1, 1, 1, 1, 1], S is 3. - Output: 5 - Explanation: - - -1+1+1+1+1 = 3 - +1-1+1+1+1 = 3 - +1+1-1+1+1 = 3 - +1+1+1-1+1 = 3 - +1+1+1+1-1 = 3 - - There are 5 ways to assign symbols to make the sum of nums be target 3. - - Note: - - The length of the given array is positive and will not exceed 20. - The sum of elements in the given array will not exceed 1000. - Your output answer is guaranteed to be fitted in a 32-bit integer. - - */ -public class _494 { - - public static class Solution1 { - public int findTargetSumWays(int[] nums, int S) { - return find(0, nums, S); - } - - private int find(int p, int[] nums, int sum) { - if (p == nums.length) { - if (sum == 0) { - return 1; - } else { - return 0; - } - } - return find(p + 1, nums, sum + nums[p]) + find(p + 1, nums, sum - nums[p]); - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_495.java b/src/main/java/com/fishercoder/solutions/_495.java deleted file mode 100644 index 1c3f01e244..0000000000 --- a/src/main/java/com/fishercoder/solutions/_495.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 495. Teemo Attacking - * - * In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. - * Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, - * you need to output the total time that Ashe is in poisoned condition. - - You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately. - - Example 1: - Input: [1,4], 2 - Output: 4 - Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately. - This poisoned status will last 2 seconds until the end of time point 2. - And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds. - So you finally need to output 4. - - Example 2: - Input: [1,2], 2 - Output: 3 - Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned. - This poisoned status will last 2 seconds until the end of time point 2. - However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status. - Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3. - So you finally need to output 3. - - Note: - You may assume the length of given time series array won't exceed 10000. - You may assume the numbers in the Teemo's attacking time series and his poisoning time duration per attacking are non-negative integers, which won't exceed 10,000,000. - */ -public class _495 { - - public static class Solution1 { - public int findPoisonedDuration(int[] timeSeries, int duration) { - if (timeSeries == null || timeSeries.length == 0 || duration == 0) { - return 0; - } - int totalDuration = 0; - for (int i = 0; i < timeSeries.length - 1; i++) { - if ((timeSeries[i + 1] - timeSeries[i]) >= duration) { - totalDuration += duration; - } else { - totalDuration += (timeSeries[i + 1] - timeSeries[i]); - } - } - totalDuration += duration;//plus the last one duration - return totalDuration; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_496.java b/src/main/java/com/fishercoder/solutions/_496.java deleted file mode 100644 index b79ebe9a80..0000000000 --- a/src/main/java/com/fishercoder/solutions/_496.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; -import java.util.Stack; - -/** - * 496. Next Greater Element I - * - * You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. - * Find all the next greater numbers for nums1's elements in the corresponding places of nums2. - * The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number. - - Example 1: - Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. - Output: [-1,3,-1] - Explanation: - For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1. - For number 1 in the first array, the next greater number for it in the second array is 3. - For number 2 in the first array, there is no next greater number for it in the second array, so output -1. - - Example 2: - Input: nums1 = [2,4], nums2 = [1,2,3,4]. - Output: [3,-1] - Explanation: - For number 2 in the first array, the next greater number for it in the second array is 3. - For number 4 in the first array, there is no next greater number for it in the second array, so output -1. - - Note: - All elements in nums1 and nums2 are unique. - The length of both nums1 and nums2 would not exceed 1000. - */ -public class _496 { - - public static class Solution1 { - public int[] nextGreaterElement(int[] findNums, int[] nums) { - Stack stack = new Stack(); - Map map = new HashMap(); - for (int i = 0; i < nums.length; i++) { - while (!stack.isEmpty() && nums[i] > stack.peek()) { - map.put(stack.pop(), nums[i]); - } - stack.push(nums[i]); - } - - while (!stack.isEmpty()) { - map.put(stack.pop(), -1); - } - - int[] result = new int[findNums.length]; - for (int i = 0; i < findNums.length; i++) { - result[i] = map.get(findNums[i]); - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_498.java b/src/main/java/com/fishercoder/solutions/_498.java deleted file mode 100644 index 1ded8a5cc9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_498.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 498. Diagonal Traverse - * - * Given a matrix of m x N elements (m rows, N columns), return all elements of the matrix in diagonal order - * as shown in the below image. - - Example: - - Input: - [ - [ 1, 2, 3 ], - [ 4, 5, 6 ], - [ 7, 8, 9 ] - ] - Output: [1,2,4,7,5,3,6,8,9] - - Note: - - The total number of elements of the given matrix will not exceed 10,000. - - */ -public class _498 { - -public static class Solutoin1 { - /** - * Reference: https://discuss.leetcode.com/topic/77865/concise-java-solution/2 - * Just keep walking the matrix, when hitting the four borders (top, bottom, left or right), - * just directions and keep walking. - */ - public int[] findDiagonalOrder(int[][] matrix) { - - if (matrix == null || matrix.length == 0) { - return new int[0]; - } - int m = matrix.length; - int n = matrix[0].length; - int[] result = new int[m * n]; - int d = 1; - int i = 0; - int j = 0; - for (int k = 0; k < m * n; ) { - result[k++] = matrix[i][j]; - i -= d; - j += d; - - if (i >= m) { - i = m - 1; - j += 2; - d = -d; - } - if (j >= n) { - j = n - 1; - i += 2; - d = -d; - } - if (i < 0) { - i = 0; - d = -d; - } - if (j < 0) { - j = 0; - d = -d; - } - } - return result; - } -} - -} diff --git a/src/main/java/com/fishercoder/solutions/_499.java b/src/main/java/com/fishercoder/solutions/_499.java deleted file mode 100644 index f4001fdbc0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_499.java +++ /dev/null @@ -1,139 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.PriorityQueue; -import java.util.Queue; - -/** - * 499. The Maze III - * - * There is a ball in a maze with empty spaces and walls. - * The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r), - * but it won't stop rolling until hitting a wall. - * - * When the ball stops, it could choose the next direction. - * There is also a hole in this maze. The ball will drop into the hole if it rolls on to the hole. - * - * Given the ball position, the hole position and the maze, - * find out how the ball could drop into the hole by moving the shortest distance. - * - * The distance is defined by the number of empty spaces traveled by the ball from the start - * position (excluded) to the hole (included). - * - * Output the moving directions by using 'u', 'd', 'l' and 'r'. - * Since there could be several different shortest ways, you should output the lexicographically smallest way. - * - * If the ball cannot reach the hole, output "impossible". - * The maze is represented by a binary 2D array. - * 1 means the wall and 0 means the empty space. - * You may assume that the borders of the maze are all walls. - * The ball and the hole coordinates are represented by row and column indexes. - - Example 1 - - Input 1: a maze represented by a 2D array - - 0 0 0 0 0 - 1 1 0 0 1 - 0 0 0 0 0 - 0 1 0 0 1 - 0 1 0 0 0 - - Input 2: ball coordinate (rowBall, colBall) = (4, 3) - Input 3: hole coordinate (rowHole, colHole) = (0, 1) - - Output: "lul" - Explanation: There are two shortest ways for the ball to drop into the hole. - The first way is left -> up -> left, represented by "lul". - The second way is up -> left, represented by 'ul'. - Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' < 'u'. So the output is "lul". - - Example 2 - - Input 1: a maze represented by a 2D array - - 0 0 0 0 0 - 1 1 0 0 1 - 0 0 0 0 0 - 0 1 0 0 1 - 0 1 0 0 0 - - Input 2: ball coordinate (rowBall, colBall) = (4, 3) - Input 3: hole coordinate (rowHole, colHole) = (3, 0) - Output: "impossible" - Explanation: The ball cannot reach the hole. - - Note: - There is only one ball and one hole in the maze. - Both the ball and hole exist on an empty space, and they will not be at the same position initially. - The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls. - The maze contains at least 2 empty spaces, and the width and the height of the maze won't exceed 30.*/ -public class _499 { - public static class Solutoin1 { - /**credit: https://discuss.leetcode.com/topic/77474/similar-to-the-maze-ii-easy-understanding-java-bfs-solution*/ - - public String findShortestWay(int[][] maze, int[] ball, int[] hole) { - - final int[] directions = new int[]{-1, 0, 1, 0, -1}; - Queue heap = new PriorityQueue<>(); - heap.offer(new Point(ball[0], ball[1], 0, "")); - int m = maze.length; - int n = maze[0].length; - Point[][] points = new Point[m][n]; - for (int i = 0; i < m * n; i++) { - points[i / n][i % n] = new Point(i / n, i % n);//initialize the length array - } - String[] ds = new String[]{"u", "r", "d", "l"}; - while (!heap.isEmpty()) { - Point curr = heap.poll(); - if (points[curr.x][curr.y].compareTo(curr) <= 0) { - continue;//if we have already found a shorter route - } - points[curr.x][curr.y] = curr; - for (int i = 0; i < directions.length - 1; i++) { - int x = curr.x; - int y = curr.y; - int distance = curr.distance;//use temp variables to move - //we need below while loop to find only "stop" points that could be put into the queue - while (x >= 0 && y >= 0 && x < m && y < n && maze[x][y] == 0 && (x != hole[0] || y != hole[1])) { - x += directions[i]; - y += directions[i + 1]; - distance++; - } - if (x != hole[0] || y != hole[1]) { - x -= directions[i]; - y -= directions[i + 1]; - distance--; - } - heap.offer(new Point(x, y, distance, curr.path + ds[i])); - } - } - return points[hole[0]][hole[1]].distance == Integer.MAX_VALUE ? "impossible" : points[hole[0]][hole[1]].path; - } - - class Point implements Comparable { - int x; - int y; - int distance; - String path; - - public Point(int x, int y) { - this.x = x; - this.y = y; - this.distance = Integer.MAX_VALUE; - this.path = ""; - } - - public Point(int x, int y, int distance, String path) { - this.x = x; - this.y = y; - this.distance = distance; - this.path = path; - } - - @Override - public int compareTo(Point o) { - return (this.distance == o.distance) ? this.path.compareTo(o.path) : this.distance - o.distance; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_5.java b/src/main/java/com/fishercoder/solutions/_5.java deleted file mode 100644 index c050deef8b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_5.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -public class _5 { - - public static class Solution1 { - private int low; - private int maxLen; - - public String longestPalindrome(String s) { - int len = s.length(); - if (len < 2) { - return s; - } - - for (int i = 0; i < len - 1; i++) { - extendPalindrome(s, i, i); // assume odd length, try to extend Palindrome as possible - extendPalindrome(s, i, i + 1); // assume even length. - } - return s.substring(low, low + maxLen); - } - - private void extendPalindrome(String s, int left, int right) { - while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { - left--; - right++; - } - if (maxLen < right - left - 1) { - low = left + 1; - maxLen = right - left - 1; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_50.java b/src/main/java/com/fishercoder/solutions/_50.java deleted file mode 100644 index 69f5558824..0000000000 --- a/src/main/java/com/fishercoder/solutions/_50.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -public class _50 { - - public static class Solution1 { - /** - * Time: O(logn) - * Space: O(logn) - */ - public double myPow(double x, int n) { - long N = n; - if (N < 0) { - x = 1 / x; - N = -N; - } - return fastPow(x, N); - } - - private double fastPow(double x, long n) { - if (n == 0) { - return 1.0; - } - double half = fastPow(x, n / 2); - if (n % 2 == 0) { - return half * half; - } else { - return half * half * x; - } - } - } - - public static class Solution2 { - /** - * Time: O(logn) - * Space: O(1) - */ - public double myPow(double x, int n) { - long N = n; - if (N < 0) { - x = 1 / x; - N = -N; - } - double answer = 1; - double currentProduct = x; - for (long i = N; i > 0; i /= 2) { - if (i % 2 == 1) { - answer = answer * currentProduct; - } - currentProduct *= currentProduct; - } - return answer; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_500.java b/src/main/java/com/fishercoder/solutions/_500.java deleted file mode 100644 index 76dae39d2b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_500.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 500. Keyboard Row - * - * Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below. - * - * American keyboard - * - * Example 1: - * Input: ["Hello", "Alaska", "Dad", "Peace"] - * Output: ["Alaska", "Dad"] - * - * Note: - * You may use one character in the keyboard more than once. - * You may assume the input string will only contain letters of alphabet. - */ -public class _500 { - - public String[] findWords(String[] words) { - final Set row1 = new HashSet<>(Arrays.asList('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p')); - final Set row2 = new HashSet<>(Arrays.asList('a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l')); - final Set row3 = new HashSet<>(Arrays.asList('z', 'x', 'c', 'v', 'b', 'n', 'm')); - final List> setList = Arrays.asList(row1, row2, row3); - List wordList = new ArrayList<>(); - for (String word : words) { - String lowerCaseWord = word.toLowerCase(); - boolean can = true; - for (Set row : setList) { - for (char c : lowerCaseWord.toCharArray()) { - if (!row.contains(c)) { - can = false; - break; - } - } - if (can) { - wordList.add(word); - break; - } - can = true; - } - } - String[] result = new String[wordList.size()]; - for (int i = 0; i < wordList.size(); i++) { - result[i] = wordList.get(i); - } - return result; - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_502.java b/src/main/java/com/fishercoder/solutions/_502.java deleted file mode 100644 index eb9bd73647..0000000000 --- a/src/main/java/com/fishercoder/solutions/_502.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.PriorityQueue; - -/** - * 502. IPO - * - * Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects. - - You are given several projects. For each project i, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding project. Initially, you have W capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital. - - To sum up, pick a list of at most k distinct projects from given projects to maximize your final capital, and output your final maximized capital. - - Example 1: - Input: k=2, W=0, Profits=[1,2,3], Capital=[0,1,1]. - - Output: 4 - - Explanation: Since your initial capital is 0, you can only start the project indexed 0. - After finishing it you will obtain profit 1 and your capital becomes 1. - With capital 1, you can either start the project indexed 1 or the project indexed 2. - Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital. - Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4. - Note: - You may assume all numbers in the input are non-negative integers. - The length of Profits array and Capital array will not exceed 50,000. - The answer is guaranteed to fit in a 32-bit signed integer. - - */ -public class _502 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues - */ - public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) { - PriorityQueue capitalHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]); - PriorityQueue profitHeap = new PriorityQueue<>((a, b) -> b[1] - a[1]); - for (int i = 0; i < Profits.length; i++) { - capitalHeap.add(new int[]{Capital[i], Profits[i]}); - } - while (k-- > 0) { - while (!capitalHeap.isEmpty() && capitalHeap.peek()[0] <= W) { - profitHeap.add(capitalHeap.poll()); - } - if (profitHeap.isEmpty()) { - break; - } - W += profitHeap.poll()[1]; - } - return W; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_503.java b/src/main/java/com/fishercoder/solutions/_503.java deleted file mode 100644 index b72c4177cf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_503.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 503. Next Greater Element II - * - * Given a circular array (the next element of the last element is the first element of the array), - * print the Next Greater Number for every element. - * The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, - * which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number. - - Example 1: - Input: [1,2,1] - Output: [2,-1,2] - Explanation: The first 1's next greater number is 2; - The number 2 can't find next greater number; - The second 1's next greater number needs to search circularly, which is also 2. - Note: The length of given array won't exceed 10000. - */ -public class _503 { - - public static class Solution1 { - /** - * Credit: https://discuss.leetcode.com/topic/77881/typical-ways-to-solve-circular-array-problems-java-solution - * Note: we store INDEX into the stack, reversely, the larger index put at the bottom of the stack, the smaller index at the top - */ - public int[] nextGreaterElements(int[] nums) { - if (nums == null || nums.length == 0) { - return nums; - } - int len = nums.length; - Stack stack = new Stack<>(); - for (int i = len - 1; i >= 0; i--) { - stack.push(i); - //push all indexes into the stack reversely - } - int[] result = new int[len]; - for (int i = len - 1; i >= 0; i--) { - result[i] = -1; - //initialize it to be -1 in case we cannot find its next greater element in the array - while (!stack.isEmpty() && (nums[stack.peek()] <= nums[i])) { - stack.pop(); - } - if (!stack.isEmpty()) { - result[i] = nums[stack.peek()]; - } - stack.push(i); - } - return result; - } - } - - public static class Solution2 { - /** - * credit: https://leetcode.com/articles/next-greater-element-ii/ - */ - public int[] nextGreaterElements(int[] nums) { - int[] result = new int[nums.length]; - Stack stack = new Stack<>(); - for (int i = nums.length * 2 - 1; i >= 0; i--) { - while (!stack.isEmpty() && nums[stack.peek()] <= nums[i % nums.length]) { - stack.pop(); - } - result[i % nums.length] = stack.isEmpty() ? -1 : nums[stack.peek()]; - stack.push(i % nums.length); - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_504.java b/src/main/java/com/fishercoder/solutions/_504.java deleted file mode 100644 index f47b7aa54d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_504.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 504. Base 7 - * - * Given an integer, return its base 7 string representation. - Example 1: - Input: 100 - Output: "202" - - Example 2: - Input: -7 - Output: "-10" - - Note: The input will be in range of [-1e7, 1e7]. - */ -public class _504 { - - public static class Solution1 { - public String convertToBase7(int num) { - return String.valueOf(Integer.toString(num, 7)); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_505.java b/src/main/java/com/fishercoder/solutions/_505.java deleted file mode 100644 index 919ab64180..0000000000 --- a/src/main/java/com/fishercoder/solutions/_505.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 505. The Maze II - * - * There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, - * but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. - * Given the ball's start position, the destination and the maze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1. - * The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes. - - Example 1 - - Input 1: a maze represented by a 2D array - - 0 0 1 0 0 - 0 0 0 0 0 - 0 0 0 1 0 - 1 1 0 1 1 - 0 0 0 0 0 - - Input 2: start coordinate (rowStart, colStart) = (0, 4) - Input 3: destination coordinate (rowDest, colDest) = (4, 4) - - Output: 12 - Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right. - The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12. - - Example 2 - - Input 1: a maze represented by a 2D array - - 0 0 1 0 0 - 0 0 0 0 0 - 0 0 0 1 0 - 1 1 0 1 1 - 0 0 0 0 0 - - Input 2: start coordinate (rowStart, colStart) = (0, 4) - Input 3: destination coordinate (rowDest, colDest) = (3, 2) - - Output: -1 - Explanation: There is no way for the ball to stop at the destination. - - Note: - There is only one ball and one destination in the maze. - Both the ball and the destination exist on an empty space, and they will not be at the same position initially. - The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls. - The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100. - */ -public class _505 { - - public static class Solution1 { - /** - * The difference between II and I of this problem: - * the extra array is not boolean type any more, but int type, and it's recording the length of each point to start point. - */ - public int shortestDistance(int[][] maze, int[] start, int[] destination) { - - final int[] directions = new int[]{-1, 0, 1, 0, -1}; - Queue queue = new LinkedList<>(); - queue.offer(new Point(start[0], start[1], 0)); - int m = maze.length; - int n = maze[0].length; - int[][] length = new int[m][n]; - for (int i = 0; i < m * n; i++) { - length[i / n][i % n] = Integer.MAX_VALUE;//initialize the length array - } - - while (!queue.isEmpty()) { - Point curr = queue.poll(); - if (length[curr.x][curr.y] <= curr.distance) { - continue; - } - length[curr.x][curr.y] = curr.distance; - for (int i = 0; i < directions.length - 1; i++) { - int x = curr.x; - int y = curr.y; - int distance = curr.distance;//use temp variables to move - //we need below while loop to find only "stop" points that could be put into the queue - while (x >= 0 && y >= 0 && x < m && y < n && maze[x][y] == 0) { - x += directions[i]; - y += directions[i + 1]; - distance++; - } - x -= directions[i]; - y -= directions[i + 1]; - distance--; - queue.offer(new Point(x, y, distance)); - } - } - return length[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : length[destination[0]][destination[1]]; - - } - - class Point { - int x; - int y; - int distance; - - public Point(int x, int y, int distance) { - this.x = x; - this.y = y; - this.distance = distance; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_506.java b/src/main/java/com/fishercoder/solutions/_506.java deleted file mode 100644 index 6ecd971b8d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_506.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * 506. Relative Ranks - * - * Given scores of N athletes, find their relative ranks and the people with the top three highest scores, - * who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal". - - Example 1: - Input: [5, 4, 3, 2, 1] - Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"] - Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". - For the left two athletes, you just need to output their relative ranks according to their scores. - - Note: - N is a positive integer and won't exceed 10,000. - All the scores of athletes are guaranteed to be unique. - */ -public class _506 { - - public static class Solution1 { - public String[] findRelativeRanks(int[] nums) { - int[] tmp = new int[nums.length]; - for (int i = 0; i < nums.length; i++) { - tmp[i] = nums[i]; - } - Arrays.sort(tmp); - Map rankMap = new HashMap(); - int len = nums.length; - for (int i = len - 1; i >= 0; i--) { - if (i == len - 1) { - rankMap.put(tmp[i], "Gold Medal"); - } else if (i == len - 2) { - rankMap.put(tmp[i], "Silver Medal"); - } else if (i == len - 3) { - rankMap.put(tmp[i], "Bronze Medal"); - } else { - rankMap.put(tmp[i], String.valueOf(len - i)); - } - } - String[] result = new String[len]; - for (int i = 0; i < len; i++) { - result[i] = rankMap.get(nums[i]); - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_507.java b/src/main/java/com/fishercoder/solutions/_507.java deleted file mode 100644 index a58bd6c045..0000000000 --- a/src/main/java/com/fishercoder/solutions/_507.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 507. Perfect Number - * - * We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself. - * Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not. - - Example: - Input: 28 - Output: True - Explanation: 28 = 1 + 2 + 4 + 7 + 14 - - Note: The input number n will not exceed 100,000,000. (1e8) - */ -public class _507 { - - public static class Solution1 { - public boolean checkPerfectNumber(int num) { - if (num == 1) { - return false; - } - int sum = 0; - for (int i = 2; i <= Math.sqrt(num); i++) { - if (num % i == 0) { - sum += i; - if (i != num / i) { - sum += num / i; - } - } - } - sum++; - return sum == num; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_508.java b/src/main/java/com/fishercoder/solutions/_508.java deleted file mode 100644 index 63766d358c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_508.java +++ /dev/null @@ -1,142 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -/** - * 508. Most Frequent Subtree Sum - * - * Given the root of a tree, you are asked to find the most frequent subtree sum. - * The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). - * So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order. - - Examples 1 - Input: - 5 - / \ - 2 -3 - return [2, -3, 4], since all the values happen only once, return all of them in any order. - - Examples 2 - Input: - - 5 - / \ - 2 -5 - return [2], since 2 happens twice, however -5 only occur once. - - Note: You may assume the sum of values in any subtree is in the range of 32-bit signed integer. - */ -public class _508 { - - public static class Solution1 { - //my purely original but verbose solution - public int[] findFrequentTreeSum(TreeNode root) { - if (root == null) { - return new int[]{}; - } - - Map map = new HashMap(); - postOrder(root, map); - - Map frequencyMap = new HashMap<>(); - for (Map.Entry entry : map.entrySet()) { - frequencyMap.put((Integer) entry.getValue(), frequencyMap.getOrDefault(entry.getValue(), 0) + 1); - } - - List> list = new LinkedList<>(frequencyMap.entrySet()); - Collections.sort(list, (o1, o2) -> (o2.getValue()).compareTo(o1.getValue())); - - int mostFrequency = list.get(0).getValue(); - List topFrequencyList = new ArrayList<>(); - topFrequencyList.add(list.get(0).getKey()); - int i = 1; - while (i < list.size() && list.get(i).getValue() == mostFrequency) { - topFrequencyList.add(list.get(i).getKey()); - i++; - } - - int[] result = new int[topFrequencyList.size()]; - for (int j = 0; j < topFrequencyList.size(); j++) { - result[j] = topFrequencyList.get(j); - } - - return result; - } - - private int postOrder(TreeNode root, Map map) { - int left = 0; - int right = 0; - if (root.left != null) { - left = postOrder(root.left, map); - } - if (root.right != null) { - right = postOrder(root.right, map); - } - if (root.left == null && root.right == null) { - map.put(root, root.val); - return root.val; - } - int sum = left + right + root.val; - map.put(root, sum); - return sum; - } - } - - public static class Solution2 { - //my 2nd purely original but verbose solution - public int[] findFrequentTreeSum(TreeNode root) { - Map map = new HashMap<>(); - dfs(root, map); - List> entryList = new ArrayList<>(map.entrySet()); - Collections.sort(entryList, (a, b) -> b.getValue() - a.getValue()); - List list = new ArrayList<>(); - for (int i = 0; i < entryList.size(); i++) { - if (list.size() == 0) { - list.add(entryList.get(i).getKey()); - } else { - if (map.get(list.get(0)) == entryList.get(i).getValue()) { - list.add(entryList.get(i).getKey()); - } else { - break; - } - } - } - int[] result = new int[list.size()]; - for (int i = 0; i < list.size(); i++) { - result[i] = list.get(i); - } - return result; - } - - private int dfs(TreeNode root, Map map) { - if (root == null) { - return 0; - } - if (root.left == null && root.right == null) { - map.put(root.val, map.getOrDefault(root.val, 0) + 1); - return root.val; - } - int leftVal = 0; - if (root.left != null) { - leftVal = dfs(root.left, map); - } - int rightVal = 0; - if (root.right != null) { - rightVal = dfs(root.right, map); - } - int val = leftVal + rightVal + root.val; - map.put(val, map.getOrDefault(val, 0) + 1); - return val; - } - } - - //a more concise and space-efficient solution: https://discuss.leetcode.com/topic/77775/verbose-java-solution-postorder-traverse-hashmap-18ms - //the key difference between the above post and my original solution is that it's using Frequency as the key of the HashMap -} diff --git a/src/main/java/com/fishercoder/solutions/_509.java b/src/main/java/com/fishercoder/solutions/_509.java deleted file mode 100644 index 7f44ce4290..0000000000 --- a/src/main/java/com/fishercoder/solutions/_509.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 509. Fibonacci Number - * - * The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, - * such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, - * - * F(0) = 0, F(1) = 1 - * F(N) = F(N - 1) + F(N - 2), for N > 1. - * Given N, calculate F(N). - * - * Example 1: - * Input: 2 - * Output: 1 - * Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. - * - * Example 2: - * Input: 3 - * Output: 2 - * Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. - * - * Example 3: - * Input: 4 - * Output: 3 - * Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3. - * - * Note: - * 0 ≤ N ≤ 30. - */ -public class _509 { - public static class Solution1 { - /** - * Time: O(n) - * Space: O(n) - */ - public int fib(int N) { - List list = new ArrayList<>(); - list.add(0); - list.add(1); - for (int i = 2; i <= N; i++) { - list.add(list.get(i - 1) + list.get(i - 2)); - } - return list.get(N); - } - } - - public static class Solution2 { - /** - * Time: O(n) - * Space: O(n) - */ - public int fib(int N) { - if (N < 2) { - return N; - } - int[] fibNums = new int[N + 1]; - fibNums[0] = 0; - fibNums[1] = 1; - for (int i = 2; i <= N; i++) { - fibNums[i] = fibNums[i - 1] + fibNums[i - 2]; - } - return fibNums[N]; - } - } - - public static class Solution3 { - /** - * Time: O(n) - * Space: O(1) - */ - public int fib(int N) { - if (N < 2) { - return N; - } - int a = 0; - int b = 1; - while (N-- > 1) { - int sum = a + b; - a = b; - b = sum; - } - return b; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_513.java b/src/main/java/com/fishercoder/solutions/_513.java deleted file mode 100644 index b10e709d86..0000000000 --- a/src/main/java/com/fishercoder/solutions/_513.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 513. Find Bottom Left Tree Value - * - * Given a binary tree, find the leftmost value in the last row of the tree. - - Example 1: - Input: - - 2 - / \ - 1 3 - - Output: - 1 - Example 2: - Input: - - - 1 - / \ - 2 3 - / / \ - 4 5 6 - / - 7 - - Output: - 7 - Note: You may assume the tree (i.e., the given root node) is not NULL. - */ -public class _513 { - - public static class Solution1 { - public int findBottomLeftValue(TreeNode root) { - Queue queue = new LinkedList<>(); - queue.offer(root); - TreeNode leftMost = root; - while (!queue.isEmpty()) { - int size = queue.size(); - for (int i = 0; i < size; i++) { - TreeNode curr = queue.poll(); - if (i == 0) { - leftMost = curr; - } - if (curr.left != null) { - queue.offer(curr.left); - } - if (curr.right != null) { - queue.offer(curr.right); - } - } - } - return leftMost.val; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_514.java b/src/main/java/com/fishercoder/solutions/_514.java deleted file mode 100644 index 5217fcfb61..0000000000 --- a/src/main/java/com/fishercoder/solutions/_514.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 514. Freedom Trail - * - * In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called - * the "Freedom Trail Ring", and use the dial to spell a specific keyword in order to open the door. - - Given a string ring, which represents the code engraved on the outer ring and another string key, - which represents the keyword needs to be spelled. - You need to find the minimum number of steps in order to spell all the characters in the keyword. - - Initially, the first character of the ring is aligned at 12:00 direction. - You need to spell all the characters in the string key one by one by - rotating the ring clockwise or anticlockwise to make each character of the string key - aligned at 12:00 direction and then by pressing the center button. - - At the stage of rotating the ring to spell the key character key[i]: - You can rotate the ring clockwise or anticlockwise one place, which counts as 1 step. - The final purpose of the rotation is to align one of the string ring's characters at the 12:00 direction, - where this character must equal to the character key[i]. - If the character key[i] has been aligned at the 12:00 direction, - you need to press the center button to spell, which also counts as 1 step. - After the pressing, you could begin to spell the next character in the key (next stage), otherwise, you've finished all the spelling. - - Example: - Input: ring = "godding", key = "gd" - Output: 4 - Explanation: - For the first key character 'g', since it is already in place, we just need 1 step to spell this character. - For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo". - Also, we need 1 more step for spelling. - So the final output is 4. - - Note: - Length of both ring and key will be in range 1 to 100. - There are only lowercase letters in both strings and might be some duplcate characters in both strings. - It's guaranteed that string key could always be spelled by rotating the string ring. - */ -public class _514 { - - public static class Solution1 { - public int findRotateSteps(String ring, String key) { - int n = ring.length(); - int m = key.length(); - int[][] dp = new int[m + 1][n]; - for (int i = m - 1; i >= 0; i--) { - for (int j = 0; j < n; j++) { - dp[i][j] = Integer.MAX_VALUE; - for (int k = 0; k < n; k++) { - if (ring.charAt(k) == key.charAt(i)) { - int diff = Math.abs(j - k); - int step = Math.min(diff, n - diff); - dp[i][j] = Math.min(dp[i][j], step + dp[i + 1][k]); - } - } - } - } - return dp[0][0] + m; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_516.java b/src/main/java/com/fishercoder/solutions/_516.java deleted file mode 100644 index d4a28534a5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_516.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 516. Longest Palindromic Subsequence - * - * Given a string s, find the longest palindromic subsequence's length in s. - * You may assume that the maximum length of s is 1000. - - Example 1: - Input: - "bbbab" - Output: - 4 - One possible longest palindromic subsequence is "bbbb". - - Example 2: - Input: - "cbbd" - Output: - 2 - One possible longest palindromic subsequence is "bb". - */ -public class _516 { - - public static class Solution1 { - /** - * Inspired by https://discuss.leetcode.com/topic/78603/straight-forward-java-dp-solution - * dp[i][j] means the longest palindromic subsequence's length of substring(i, j) - * so, in the end, we return dp[0][s.length() - 1] which means the longest palindromic subsequence - * of this whole string. - */ - public int longestPalindromeSubseq(String s) { - int[][] dp = new int[s.length()][s.length()]; - for (int i = s.length() - 1; i >= 0; i--) { - dp[i][i] = 1;//initialization - for (int j = i + 1; j < s.length(); j++) { - if (s.charAt(i) == s.charAt(j)) { - dp[i][j] = dp[i + 1][j - 1] + 2; - } else { - dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]); - } - } - } - return dp[0][s.length() - 1]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_517.java b/src/main/java/com/fishercoder/solutions/_517.java deleted file mode 100644 index fd100bdc6f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_517.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 517. Super Washing Machines - * - * You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. - - For each move, you could choose any m (1 ? m ? n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time . - - Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1. - - Example1 - - Input: [1,0,5] - - Output: 3 - - Explanation: - 1st move: 1 0 <-- 5 => 1 1 4 - 2nd move: 1 <-- 1 <-- 4 => 2 1 3 - 3rd move: 2 1 <-- 3 => 2 2 2 - Example2 - - Input: [0,3,0] - - Output: 2 - - Explanation: - 1st move: 0 <-- 3 0 => 1 2 0 - 2nd move: 1 2 --> 0 => 1 1 1 - Example3 - - Input: [0,2,0] - - Output: -1 - - Explanation: - It's impossible to make all the three washing machines have the same number of dresses. - Note: - The range of n is [1, 10000]. - The range of dresses number in a super washing machine is [0, 1e5]. - - */ -public class _517 { - public static class Solution1 { - /** - * Reference: https://discuss.leetcode.com/topic/79938/super-short-easy-java-o-n-solution - */ - public int findMinMoves(int[] machines) { - int total = 0; - for (int i : machines) { - total += i; - } - if (total % machines.length != 0) { - return -1; - } - int avg = total / machines.length; - int cnt = 0; - int max = 0; - for (int load : machines) { - cnt += load - avg; //load-avg is "gain/lose" - max = Math.max(Math.max(max, Math.abs(cnt)), load - avg); - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_520.java b/src/main/java/com/fishercoder/solutions/_520.java deleted file mode 100644 index f0d05cc581..0000000000 --- a/src/main/java/com/fishercoder/solutions/_520.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 520. Detect Capital - * - * Given a word, you need to judge whether the usage of capitals in it is right or not. - * We define the usage of capitals in a word to be right when one of the following cases holds: - - All letters in this word are capitals, like "USA". - All letters in this word are not capitals, like "leetcode". - Only the first letter in this word is capital if it has more than one letter, like "Google". - Otherwise, we define that this word doesn't use capitals in a right way. - - Example 1: - Input: "USA" - Output: True - - Example 2: - Input: "FlaG" - Output: False - - Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters. - */ -public class _520 { - public static class Solution1 { - public boolean detectCapitalUse(String word) { - char[] words = word.toCharArray(); - boolean firstLetterCap = false; - if (Character.isUpperCase(words[0])) { - firstLetterCap = true; - } - - if (firstLetterCap) { - if (words.length >= 2) { - int i = 2; - if (Character.isUpperCase(words[1])) { - //then all following must be all uppercase - while (i < words.length) { - if (!Character.isUpperCase(words[i])) { - return false; - } - i++; - } - return true; - } else { - //then all following must be all lowercase - while (i < words.length) { - if (!Character.isLowerCase(words[i])) { - return false; - } - i++; - } - return true; - } - } - return true; - } else { - //then all following must be all lowercase - int i = 1; - while (i < words.length) { - if (!Character.isLowerCase(words[i])) { - return false; - } - i++; - } - return true; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_521.java b/src/main/java/com/fishercoder/solutions/_521.java deleted file mode 100644 index be5de9514b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_521.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 521. Longest Uncommon Subsequence I - * - * Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. - * The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. - * A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. - * Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. - * The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. - * If the longest uncommon subsequence doesn't exist, return -1. - - Example 1: - Input: "aba", "cdc" - Output: 3 - Explanation: The longest uncommon subsequence is "aba" (or "cdc"), - because "aba" is a subsequence of "aba", - but not a subsequence of any other strings in the group of two strings. - - Note: - Both strings' lengths will not exceed 100. - Only letters from a ~ z will appear in input strings. - */ -public class _521 { - public static class Solution1 { - /** - * The gotcha point of this question is: - * 1. if a and b are identical, then there will be no common subsequence, return -1 - * 2. else if a and b are of equal length, then any one of them will be a subsequence of the other string - * 3. else if a and b are of different length, then the longer one is a required subsequence because the longer string cannot be a subsequence of the shorter one - * Or in other words, when a.length() != b.length(), no subsequence of b will be equal to a, so return Math.max(a.length(), b.length()) - */ - public int findLUSlength(String a, String b) { - if (a.equals(b)) { - return -1; - } - return Math.max(a.length(), b.length()); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_522.java b/src/main/java/com/fishercoder/solutions/_522.java deleted file mode 100644 index 2970588c23..0000000000 --- a/src/main/java/com/fishercoder/solutions/_522.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.Comparator; - -/** - * 522. Longest Uncommon Subsequence II - * - * Given a list of strings, you need to find the longest uncommon subsequence among them. - * The longest uncommon subsequence is defined as the longest subsequence of one of these strings and - * this subsequence should not be any subsequence of the other strings. - * A subsequence is a sequence that can be derived from one sequence by deleting some characters - * without changing the order of the remaining elements. - * Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. - * The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. - * If the longest uncommon subsequence doesn't exist, return -1. - - Example 1: - Input: "aba", "cdc", "eae" - Output: 3 - Note: - - All the given strings' lengths will not exceed 10. - The length of the given list will be in the range of [2, 50]. - */ -public class _522 { - - public static class Solution1 { - /**Idea: if there's such a LUS there in the list, it must be one of the strings in the given list, - so we'll just go through the list and check if one string is NOT subsequence of any others, if so, return it, otherwise, return -1*/ - public int findLUSlength(String[] strs) { - Arrays.sort(strs, new Comparator() { - @Override - public int compare(String o1, String o2) { - return o2.length() - o1.length(); - } - }); - - for (int i = 0; i < strs.length; i++) { - boolean found = true; - for (int j = 0; j < strs.length; j++) { - if (i == j) { - continue; - } else if (isSubsequence(strs[i], strs[j])) { - found = false; - break; - } - } - if (found) { - return strs[i].length(); - } - } - return -1; - } - - private boolean isSubsequence(String a, String b) { - int i = 0; - for (int j = 0; i < a.length() && j < b.length(); j++) { - if (a.charAt(i) == b.charAt(j)) { - i++; - } - } - return i == a.length(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_523.java b/src/main/java/com/fishercoder/solutions/_523.java deleted file mode 100644 index a6cc9a9b0f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_523.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 523. Continuous Subarray Sum - * - * Given a list of non-negative numbers and a target integer k, - * write a function to check if the array has a continuous subarray of size at least 2 - * that sums up to the multiple of k, that is, sums up to n*k where n is also an integer. - - Example 1: - Input: [23, 2, 4, 6, 7], k=6 - Output: True - Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6. - - Example 2: - Input: [23, 2, 6, 4, 7], k=6 - Output: True - Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42. - - Note: - The length of the array won't exceed 10,000. - You may assume the sum of all the numbers is in the range of a signed 32-bit integer. - - */ -public class _523 { - - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/80793/java-o-n-time-o-k-space/20 - * "The reason we use modulus is: - * (a+(n*x))%x is same as (a%x) - * e.g. in case of the array [23,2,6,4,7] the running sum is [23,25,31,35,42] - * and the remainders are [5,1,1,5,0]. - * We got reminder 5 at index 0 and at index 3. - * That means, in between these two indexes we must have added a number which is multiple of the k. - * Hope this clarifies your doubt :)" - */ - public boolean checkSubarraySum(int[] nums, int k) { - Map map = new HashMap<>(); - map.put(0, -1); - int sum = 0; - for (int i = 0; i < nums.length; i++) { - sum += nums[i]; - if (k != 0) { - /**Because if k == 0, sum %= k will throw ArithmeticException.*/ - sum %= k; - } - Integer prev = map.get(sum); - if (prev != null) { - if (i - prev > 1) { - /**This makes sure that it has length at least 2*/ - return true; - } - } else { - map.put(sum, i); - } - } - return false; - } - } - - public static class Solution2 { - public boolean checkSubarraySum(int[] nums, int k) { - if (nums == null || nums.length == 0) { - return false; - } - - //Two continuous zeroes will form a subarray of length 2 with sum 0, 0*k = 0 will always be true - for (int i = 0; i < nums.length - 1; i++) { - if (nums[i] == 0 && nums[i + 1] == 0) { - return true; - } - } - - //then k cannot be zero any more - if (k == 0 || nums.length < 2) { - return false; - } - - int[] preSums = new int[nums.length + 1]; - for (int i = 1; i <= nums.length; i++) { - preSums[i] = preSums[i - 1] + nums[i - 1]; - } - - for (int i = 1; i <= nums.length; i++) { - for (int j = 0; j < i - 1; j++) { - if ((preSums[i] - preSums[j]) % k == 0) { - return true; - } - } - } - return false; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_524.java b/src/main/java/com/fishercoder/solutions/_524.java deleted file mode 100644 index 668cd14877..0000000000 --- a/src/main/java/com/fishercoder/solutions/_524.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Collections; -import java.util.List; - -/** - * 524. Longest Word in Dictionary through Deleting - * - * Given a string and a string dictionary, find the longest string in the dictionary that - * can be formed by deleting some characters of the given string. - * If there are more than one possible results, - * return the longest word with the smallest lexicographical order. - * If there is no possible result, return the empty string. - - Example 1: - Input: - s = "abpcplea", d = ["ale","apple","monkey","plea"] - - Output: - "apple" - Example 2: - Input: - s = "abpcplea", d = ["a","b","c"] - - Output: - "a" - - Note: - All the strings in the input will only contain lower-case letters. - The size of the dictionary won't exceed 1,000. - The length of all the strings in the input won't exceed 1,000. - */ -public class _524 { - - public static class Solution1 { - public String findLongestWord(String s, List d) { - Collections.sort(d, (a, b) -> a.length() == b.length() ? a.compareTo(b) : b.length() - a.length()); - for (String dictWord : d) { - int i = 0; - for (char c : s.toCharArray()) { - if (i < dictWord.length() && dictWord.charAt(i) == c) { - i++; - } - } - if (i == dictWord.length()) { - return dictWord; - } - } - return ""; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_525.java b/src/main/java/com/fishercoder/solutions/_525.java deleted file mode 100644 index 19b7bb77f2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_525.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 525. Contiguous Array - * - * Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. - - Example 1: - Input: [0,1] - Output: 2 - Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. - - Example 2: - Input: [0,1,0] - Output: 2 - Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. - - Note: The length of the given binary array will not exceed 50,000. - */ -public class _525 { - - public static class Solution1 { - //credit: https://leetcode.com/articles/contiguous-array/#approach-3-using-hashmap-accepted - public int findMaxLength(int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - int count = 0; - int max = 0; - Map map = new HashMap(); - map.put(0, -1);//initialize the map, which means at index zero, the length of contiguous subarray is -1 - for (int i = 0; i < nums.length; i++) { - count += nums[i] == 1 ? 1 : -1; - if (map.containsKey(count)) { - max = Math.max(i - map.get(count), max); - } else { - map.put(count, i);//only when the map does not have this key, we put it in the map, this avoids overwriting the map - //also, it helps us keep the most leftside value in the map to help us compute the longest contigous array length - } - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_526.java b/src/main/java/com/fishercoder/solutions/_526.java deleted file mode 100644 index a23a1b04f9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_526.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 526. Beautiful Arrangement - * - * Suppose you have N integers from 1 to N. - * We define a beautiful arrangement as an array that is constructed by these N numbers successfully - * if one of the following is true for the ith position (1 ≤ i ≤ N) in this array: - * The number at the ith position is divisible by i. - * i is divisible by the number at the ith position. - * Now given N, how many beautiful arrangements can you construct? - - Example 1: - - Input: 2 - Output: 2 - - Explanation: - - The first beautiful arrangement is [1, 2]: - Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1). - Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2). - - The second beautiful arrangement is [2, 1]: - Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1). - Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1. - - Note: - N is a positive integer and will not exceed 15. - */ -public class _526 { - public static class Solution1 { - /** - * A good post to look at: https://discuss.leetcode.com/topic/79916/java-solution-backtracking - * and there's a generic template afterwards for backtracking problems - */ - - int count = 0; - - public int countArrangement(int N) { - backtracking(N, new int[N + 1], 1); - return count; - } - - private void backtracking(int N, int[] used, int pos) { - if (pos > N) { - count++; - return; - } - - for (int i = 1; i <= N; i++) { - if (used[i] == 0 && (i % pos == 0 || pos % i == 0)) { - used[i] = 1; - backtracking(N, used, pos + 1); - used[i] = 0; - } - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_527.java b/src/main/java/com/fishercoder/solutions/_527.java deleted file mode 100644 index 37cfeeab1e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_527.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; - -/** - * 527. Word Abbreviation - * - * Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. - - Begin with the first character and then the number of characters abbreviated, which followed by the last character. - If there are any conflict, that is more than one words share the same abbreviation, - a longer prefix is used instead of only the first character until making the map - from word to abbreviation become unique. - In other words, a final abbreviation cannot map to more than one original words. - If the abbreviation doesn't make the word shorter, then keep it as original. - - Example: - Input: ["like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"] - Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"] - - Note: - Both n and the length of each word will not exceed 400. - The length of each word is greater than 1. - The words consist of lowercase English letters only. - The return answers should be in the same order as the original array. - */ -public class _527 { - - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/82613/really-simple-and-straightforward-java-solution - */ - public List wordsAbbreviation(List dict) { - int len = dict.size(); - String[] ans = new String[len]; - int[] prefix = new int[len]; - for (int i = 0; i < len; i++) { - prefix[i] = 1; - ans[i] = abbreviate(dict.get(i), 1); // make abbreviation for each string - } - for (int i = 0; i < len; i++) { - while (true) { - HashSet set = new HashSet<>(); - for (int j = i + 1; j < len; j++) { - if (ans[j].equals(ans[i])) { - set.add(j); // check all strings with the same abbreviation - } - } - if (set.isEmpty()) { - break; - } - set.add(i); - for (int k : set) { - ans[k] = abbreviate(dict.get(k), ++prefix[k]); // increase the prefix - } - } - } - return Arrays.asList(ans); - } - - private String abbreviate(String word, int k) { - if (k + 2 >= word.length()) { - return word; - } - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(word.substring(0, k)); - stringBuilder.append(word.length() - 1 - k); - stringBuilder.append(word.substring(word.length() - 1)); - return stringBuilder.toString(); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_529.java b/src/main/java/com/fishercoder/solutions/_529.java deleted file mode 100644 index d23d47b299..0000000000 --- a/src/main/java/com/fishercoder/solutions/_529.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 529. Minesweeper - * - * Let's play the minesweeper game (Wikipedia, online game)! - - You are given a 2D char matrix representing the game board. 'm' represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine. - - Now given the next click position (row and column indices) among all the unrevealed squares ('m' or 'E'), return the board after revealing this position according to the following rules: - - If a mine ('m') is revealed, then the game is over - change it to 'X'. - If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent unrevealed squares should be revealed recursively. - If an empty square ('E') with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines. - Return the board when no more squares will be revealed. - - Example 1: - Input: - - [['E', 'E', 'E', 'E', 'E'], - ['E', 'E', 'm', 'E', 'E'], - ['E', 'E', 'E', 'E', 'E'], - ['E', 'E', 'E', 'E', 'E']] - - Click : [3,0] - - Output: - - [['B', '1', 'E', '1', 'B'], - ['B', '1', 'm', '1', 'B'], - ['B', '1', '1', '1', 'B'], - ['B', 'B', 'B', 'B', 'B']] - - Explanation: - - - Example 2: - Input: - - [['B', '1', 'E', '1', 'B'], - ['B', '1', 'm', '1', 'B'], - ['B', '1', '1', '1', 'B'], - ['B', 'B', 'B', 'B', 'B']] - - Click : [1,2] - - Output: - - [['B', '1', 'E', '1', 'B'], - ['B', '1', 'X', '1', 'B'], - ['B', '1', '1', '1', 'B'], - ['B', 'B', 'B', 'B', 'B']] - - Explanation: - - Note: - The range of the input matrix's height and width is [1,50]. - The click position will only be an unrevealed square ('m' or 'E'), which also means the input board contains at least one clickable square. - The input board won't be a stage when game is over (some mines have been revealed). - For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares. - */ -public class _529 { - - public static class Solution1 { - public char[][] updateBoard(char[][] board, int[] click) { - int m = board.length; - int n = board[0].length; - Queue queue = new LinkedList(); - queue.offer(click); - while (!queue.isEmpty()) { - int[] curr = queue.poll(); - int currRow = curr[0]; - int currCol = curr[1]; - if (board[currRow][currCol] == 'M') { - /**This also covers the corner case: when click[] happens to be on a mine, then it'll exit directly. - * Otherwise, we'll just continue and mark this cell to be 'M' and keep processing all 'E' cells in the queue.*/ - board[currRow][currCol] = 'X'; - } else { - /**scan all four directions of this curr cell, count all mines, this includes 'X' and 'M' */ - int count = 0; - for (int i = -1; i < 2; i++) { - for (int j = -1; j < 2; j++) { - if (i == 0 && j == 0) { - continue; - } - int nextRow = currRow + i; - int nextCol = currCol + j; - if (nextRow >= m || nextRow < 0 || nextCol >= n || nextCol < 0) { - continue; - } - if (board[nextRow][nextCol] == 'M' || board[nextRow][nextCol] == 'X') { - count++; - } - } - } - - if (count > 0) { - /**There are mines around this cell, so update it with the number of mines*/ - board[currRow][currCol] = (char) (count + '0'); - } else { - /**There is no mines around this cell, so update it to be 'B'*/ - board[currRow][currCol] = 'B'; - - /**then we'll also check all of its four surrounding cells, if it's 'E'. we'll also update it to be 'B' and offer it into the queue*/ - for (int i = -1; i < 2; i++) { - for (int j = -1; j < 2; j++) { - if (i == 0 && j == 0) { - continue; - } - int nextRow = currRow + i; - int nextCol = currCol + j; - if (nextRow >= m || nextRow < 0 || nextCol >= n || nextCol < 0) { - continue; - } - if (board[nextRow][nextCol] == 'E') { - /**we offer 'E' cells into the queue*/ - queue.offer(new int[]{nextRow, nextCol}); - /**then update this cell to be 'B' */ - board[nextRow][nextCol] = 'B'; - } - } - } - } - } - } - return board; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_53.java b/src/main/java/com/fishercoder/solutions/_53.java deleted file mode 100644 index 8e11da34c5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_53.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.fishercoder.solutions; - -public class _53 { - - public static class Solution1 { - /** - * https://leetcode.com/problems/maximum-subarray/discuss/20211/accepted-on-solution-in-java - */ - public int maxSubArray(int[] nums) { - int maxSoFar = nums[0]; - int maxEndingHere = nums[0]; - for (int i = 1; i < nums.length; i++) { - maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]); - maxSoFar = Math.max(maxEndingHere, maxSoFar); - } - return maxSoFar; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_530.java b/src/main/java/com/fishercoder/solutions/_530.java deleted file mode 100644 index a7bedd23db..0000000000 --- a/src/main/java/com/fishercoder/solutions/_530.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.Iterator; -import java.util.TreeSet; - -/** - * 530. Minimum Absolute Difference in BST - * - * Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. - - Example: - - Input: - - 1 - \ - 3 - / - 2 - - Output: - 1 - - Explanation: - The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3). - Note: There are at least two nodes in this BST. - */ -public class _530 { - - public static class Solution1 { - public int getMinimumDifference(TreeNode root) { - TreeSet treeset = new TreeSet<>(); - treeset.add(root.val); - dfs(root, treeset); - int diff = Integer.MAX_VALUE; - Iterator iterator = treeset.iterator(); - int prev = iterator.next(); - while (iterator.hasNext()) { - int current = iterator.next(); - diff = Math.min(diff, Math.abs(current - prev)); - prev = current; - } - return diff; - } - - private void dfs(TreeNode root, TreeSet treeset) { - if (root.left != null) { - treeset.add(root.left.val); - dfs(root.left, treeset); - } - if (root.right != null) { - treeset.add(root.right.val); - dfs(root.right, treeset); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_531.java b/src/main/java/com/fishercoder/solutions/_531.java deleted file mode 100644 index 38a144ce3c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_531.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 531. Lonely Pixel I - * - * Given a picture consisting of black and white pixels, find the number of black lonely pixels. - * The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively. - * A black lonely pixel is character 'B' that located at a specific position where the same row and same column don't have any other black pixels. - - Example: - Input: - [['W', 'W', 'B'], - ['W', 'B', 'W'], - ['B', 'W', 'W']] - - Output: 3 - Explanation: All the three 'B's are black lonely pixels. - - Note: - The range of width and height of the input 2D array is [1,500]. - */ -public class _531 { - - public static class Solution1 { - public int findLonelyPixel(char[][] picture) { - int m = picture.length; - int n = picture[0].length; - int count = 0; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (picture[i][j] == 'B' && isLonely(i, j, picture, m, n)) { - count++; - } - } - } - return count; - } - - private boolean isLonely(int row, int col, char[][] picture, int m, int n) { - for (int i = 0; i < m; i++) { - if (i != row) { - if (picture[i][col] == 'B') { - return false; - } - } - } - - for (int j = 0; j < n; j++) { - if (j != col) { - if (picture[row][j] == 'B') { - return false; - } - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_532.java b/src/main/java/com/fishercoder/solutions/_532.java deleted file mode 100644 index f3008c678c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_532.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 532. K-diff Pairs in an Array - * - * Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. - * Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k. - - Example 1: - Input: [3, 1, 4, 1, 5], k = 2 - Output: 2 - Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5). - Although we have two 1s in the input, we should only return the number of unique pairs. - - Example 2: - Input:[1, 2, 3, 4, 5], k = 1 - Output: 4 - Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5). - - Example 3: - Input: [1, 3, 1, 5, 4], k = 0 - Output: 1 - Explanation: There is one 0-diff pair in the array, (1, 1). - - Note: - The pairs (i, j) and (j, i) count as the same pair. - The length of the array won't exceed 10,000. - All the integers in the given input belong to the range: [-1e7, 1e7]. - */ -public class _532 { - - public static class Solution1 { - public int findPairs(int[] nums, int k) { - if (nums == null || nums.length == 0 || k < 0) { - return 0; - } - - Map map = new HashMap(); - for (int num : nums) { - map.put(num, map.getOrDefault(num, 0) + 1); - } - - int answer = 0; - for (int key : map.keySet()) { - if (k == 0) { - if (map.get(key) >= 2) { - answer++; - } - } else { - if (map.containsKey(key + k)) { - answer++; - } - } - } - return answer; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_533.java b/src/main/java/com/fishercoder/solutions/_533.java deleted file mode 100644 index 3412cc4ae4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_533.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 533. Lonely Pixel II - * - * Given a picture consisting of black and white pixels, and a positive integer N, - * find the number of black pixels located at some specific row R and column C that align with all the following rules: - - Row R and column C both contain exactly N black pixels. - For all rows that have a black pixel at column C, they should be exactly the same as row R - The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively. - - Example: - Input: - [['W', 'B', 'W', 'B', 'B', 'W'], - ['W', 'B', 'W', 'B', 'B', 'W'], - ['W', 'B', 'W', 'B', 'B', 'W'], - ['W', 'W', 'B', 'W', 'B', 'W']] - - N = 3 - Output: 6 - Explanation: All the bold 'B' are the black pixels we need (all 'B's at column 1 and 3). - 0 1 2 3 4 5 column index - 0 [['W', 'B', 'W', 'B', 'B', 'W'], - 1 ['W', 'B', 'W', 'B', 'B', 'W'], - 2 ['W', 'B', 'W', 'B', 'B', 'W'], - 3 ['W', 'W', 'B', 'W', 'B', 'W']] - row index - - Take 'B' at row R = 0 and column C = 1 as an example: - Rule 1, row R = 0 and column C = 1 both have exactly N = 3 black pixels. - Rule 2, the rows have black pixel at column C = 1 are row 0, row 1 and row 2. They are exactly the same as row R = 0. - - Note: - The range of width and height of the input 2D array is [1,200]. - */ -public class _533 { - public static class Solution1 { - /** - * Credit: https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/5 - * This program is very well designed to do things: - * 1. it scans the entire matrix once, but does two things in this scan: - * first it records an array of cols that keeps count of how many 'B' are there in each column; - * second, at the end of traversing each column, it puts this entire row as the key into a HashMap - * when there N number of 'B's in this row. - * 2. then we could go through the HashMap keyset: - * if one row has N number of 'B's, we go through this row's each column to see if any element in this row is 'B' and also that element's column has N 'B's - */ - public int findBlackPixel(char[][] picture, int N) { - if (picture == null || picture.length == 0 || picture[0].length == 0) { - return 0; - } - int m = picture.length; - int n = picture[0].length; - int[] cols = new int[n]; - Map map = new HashMap<>(); - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < m; i++) { - int count = 0; - for (int j = 0; j < n; j++) { - if (picture[i][j] == 'B') { - count++; - cols[j]++; - } - stringBuilder.append(picture[i][j]); - } - if (count == N) { - /**we use this entire row string as key for the map*/ - map.put(stringBuilder.toString(), map.getOrDefault(stringBuilder.toString(), 0) + 1); - } - stringBuilder.setLength(0); - } - - int answer = 0; - for (String key : map.keySet()) { - if (map.get(key) != N) { - continue; - } - for (int i = 0; i < n; i++) { - if (key.charAt(i) == 'B' && cols[i] == N) { - answer += N; - } - } - } - return answer; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_535.java b/src/main/java/com/fishercoder/solutions/_535.java deleted file mode 100644 index 3975feeeb0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_535.java +++ /dev/null @@ -1,135 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; -import java.util.Random; - -/** - * 535. Encode and Decode TinyURL - * - * TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl - * and it returns a short URL such as http://tinyurl.com/4e9iAk. - * Design the encode and decode methods for the TinyURL service. - * There is no restriction on how your encode/decode algorithm should work. - * You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL. - - Note: Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless. - */ - -public class _535 { - - public static class Solution1 { - /** - * Simple counter approach - * Analysis: - * The range of URLs that can be decoded is limited by the range of Integer. - * If excessively large number of URLs have to be encoded, after the range of Integer is exceeded, - * integer overflow could lead to overwriting previous URL's encodings. - * The length of the URL isn't necessary shorter than incoming URL. - * One potential security issue with this problem is that it's very easy to predict the next code generated, - * since this pattern is very easy to be detected. - */ - public class Codec { - int i = 0; - Map map = new HashMap(); - public static final String PREFIX = "http://tinyurl/"; - - public String encode(String longUrl) { - map.put(i, longUrl); - return PREFIX + i++; - } - - public String decode(String shortUrl) { - return map.get(Integer.parseInt(shortUrl.substring(PREFIX.length()))); - } - } - } - - public static class Solution2 { - /** - * Use Java built-in HashCode - * Analysis: - * hashCode() does NOT generate unique codes for different strings, collision might happen. - * As the number of URLs increase, the probability of getting collision increases which leads to failure. - */ - public class Codec { - Map map = new HashMap<>(); - public static final String PREFIX = "http://tinyurl/"; - - // Encodes a URL to a shortened URL. - public String encode(String longUrl) { - /**I don't need to create a local variable to cache longUrl.hashCode() - * since Java's String cache it already. :) Look at its source code.*/ - map.put(longUrl.hashCode(), longUrl); - return PREFIX + longUrl.hashCode(); - } - - // Decodes a shortened URL to its original URL. - public String decode(String shortUrl) { - return map.get(Integer.parseInt(shortUrl.substring(PREFIX.length()))); - } - } - } - - public static class Solution3 { - /**Use a random number*/ - Map map = new HashMap<>(); - Random random = new Random(); - public static final String PREFIX = "http://tinyurl/"; - - // Encodes a URL to a shortened URL. - public String encode(String longUrl) { - int num = random.nextInt(Integer.MAX_VALUE); - while (map.containsKey(num)) { - num = random.nextInt(Integer.MAX_VALUE); - } - map.put(num, longUrl); - return PREFIX + num; - } - - // Decodes a shortened URL to its original URL. - public String decode(String shortUrl) { - return map.get(Integer.parseInt(shortUrl.substring(PREFIX.length()))); - } - } - - public static class Solution4 { - /** - * Use a random but fixed length encoding - * Analysis: - * 1. This is the most optimal solution so far. - * 2. The number of URLs that can be encoded can be as big as Math.pow((10 + 26*2), FIXED_LENGTH) - * 3. The length of the shortened URL is fixed at a certain length, which could be a significant reduce for large URLs - * 4. The performance of this scheme is pretty good, due to much smaller probability of encountering collision - * 5. Predicting pattern/encoding isn't possible in this case since random numbers are used. - */ - Map map = new HashMap<>(); - public static final String PREFIX = "http://tinyurl/"; - public static final int FIXED_LENGTH = 7; - Random random = new Random(); - String alphabet = "0123456789abcdefghijklmnopgrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - - // Encodes a URL to a shortened URL. - public String encode(String longUrl) { - String shortKey = getRandomFixedLengthKey(); - while (map.containsKey(shortKey)) { - shortKey = getRandomFixedLengthKey(); - } - map.put(shortKey, longUrl); - return PREFIX + shortKey; - } - - private String getRandomFixedLengthKey() { - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < FIXED_LENGTH; i++) { - stringBuilder.append(alphabet.charAt(random.nextInt(alphabet.length()))); - } - return stringBuilder.toString(); - } - - // Decodes a shortened URL to its original URL. - public String decode(String shortUrl) { - return map.get(shortUrl.substring(PREFIX.length())); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_536.java b/src/main/java/com/fishercoder/solutions/_536.java deleted file mode 100644 index baae27fc2b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_536.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayDeque; -import java.util.Deque; - -/** - * 536. Construct Binary Tree from String - * - * You need to construct a binary tree from a string consisting of parenthesis and integers. - The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. - The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure. - You always start to construct the left child node of the parent first if it exists. - - Example: - Input: "4(2(3)(1))(6(5))" - Output: return the tree root node representing the following tree: - - 4 - / \ - 2 6 - / \ / - 3 1 5 - - Note: - There will only be '(', ')', '-' and '0' ~ '9' in the input string. - An empty tree is represented by "" instead of "()". - */ - -public class _536 { - - public static class Solution1 { - public TreeNode str2tree(String s) { - if (s.equals("")) { - return null; - } - int firstParen = s.indexOf("("); - int val = firstParen == -1 ? Integer.parseInt(s) : Integer.parseInt(s.substring(0, firstParen)); - TreeNode cur = new TreeNode(val); - if (firstParen == -1) { - return cur; - } - int start = firstParen; - int leftParenCount = 0; - for (int i = start; i < s.length(); i++) { - if (s.charAt(i) == '(') { - leftParenCount++; - } else if (s.charAt(i) == ')') { - leftParenCount--; - } - if (leftParenCount == 0 && start == firstParen) { - cur.left = str2tree(s.substring(start + 1, i)); - start = i + 1; - } else if (leftParenCount == 0) { - cur.right = str2tree(s.substring(start + 1, i)); - } - } - return cur; - } - } - - public static class Solution2 { - public TreeNode str2tree(String s) { - Deque stack = new ArrayDeque<>(); - for (int i = 0, j = i; i < s.length(); i++, j = i) { - char c = s.charAt(i); - if (c == ')') { - stack.pop(); - } else if (c >= '0' && c <= '9' || c == '-') { - while (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') { - i++; - } - TreeNode curr = new TreeNode(Integer.valueOf(s.substring(j, i + 1))); - if (!stack.isEmpty()) { - TreeNode parent = stack.peek(); - if (parent.left != null) { - parent.right = curr; - } else { - parent.left = curr; - } - } - stack.push(curr); - } - } - return stack.isEmpty() ? null : stack.peek(); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_537.java b/src/main/java/com/fishercoder/solutions/_537.java deleted file mode 100644 index c7a0c02637..0000000000 --- a/src/main/java/com/fishercoder/solutions/_537.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.stream.Stream; - -/** - * 537. Complex Number Multiplication - * - * Given two strings representing two complex numbers. - - You need to return a string representing their multiplication. Note i2 = -1 according to the definition. - - Example 1: - Input: "1+1i", "1+1i" - Output: "0+2i" - Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i. - - Example 2: - Input: "1+-1i", "1+-1i" - Output: "0+-2i" - Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i. - - Note: - The input strings will not have extra blank. - The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form. - */ -public class _537 { - - public static class Solution1 { - public String complexNumberMultiply(String a, String b) { - String[] part1And2 = a.split("\\+"); - String[] part3And4 = b.split("\\+"); - String product1 = String.valueOf(Integer.parseInt(part1And2[0]) * Integer.parseInt(part3And4[0]));//this is real number multiplication - String product2 = multiply(part1And2[0], part3And4[1]); - String product3 = multiply(part3And4[0], part1And2[1]); - String product4 = multiplyTwoIs(part3And4[1], part1And2[1]); - String twoISum = sumTwoI(product2, product3); - String numberValue = String.valueOf(Integer.valueOf(product1) + Integer.valueOf(product4)); - return numberValue + "+" + twoISum; - } - - private String sumTwoI(String product2, String product3) { - int number2 = Integer.parseInt(product2.substring(0, product2.length() - 1)); - int number3 = Integer.parseInt(product3.substring(0, product3.length() - 1)); - return String.valueOf(number2 + number3) + "i"; - } - - private String multiplyTwoIs(String p, String q) { - int number1 = Integer.parseInt(p.substring(0, p.length() - 1)); - int number2 = Integer.parseInt(q.substring(0, q.length() - 1)); - int numberProduct = number1 * number2; - return String.valueOf(-numberProduct); - } - - private String multiply(String p, String withI) { - int numberPart = Integer.parseInt(withI.substring(0, withI.length() - 1)); - return String.valueOf(numberPart * Integer.valueOf(p)) + "i"; - } - } - - public static class Solution2 { - /** - * (a + bi) * (c + di) could become (ac - bd) + (ad + bc)*i - * Thus, we have the following function - */ - public String complexNumberMultiply(String a, String b) { - int[] coefficients1 = Stream.of(a.split("\\+|i")).mapToInt(Integer::parseInt).toArray(); - int[] coefficients2 = Stream.of(b.split("\\+|i")).mapToInt(Integer::parseInt).toArray(); - return (coefficients1[0] * coefficients2[0] - coefficients1[1] * coefficients2[1]) + "+" - + (coefficients1[0] * coefficients2[1] + coefficients1[1] * coefficients2[0] + "i"); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_539.java b/src/main/java/com/fishercoder/solutions/_539.java deleted file mode 100644 index 6eb71c17c9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_539.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.List; - -/** - * 539. Minimum Time Difference - * - * Given a list of 24-hour clock time points in "Hour:Minutes" format, - * find the minimum minutes difference between any two time points in the list. - - Example 1: - Input: ["23:59","00:00"] - Output: 1 - - Note: - The number of time points in the given list is at least 2 and won't exceed 20000. - The input time is legal and ranges from 00:00 to 23:59. - */ -public class _539 { - - public static class Soluiton1 { - - public int findMinDifference(List timePoints) { - /**there are in total 24*60 = 1440 possible time points*/ - final int ALL_POSSIBLE_TIMEPOINTS = 1440; - boolean[] allTimePoints = new boolean[ALL_POSSIBLE_TIMEPOINTS]; - for (String eachTime : timePoints) { - String[] timeParts = eachTime.split(":"); - int hour = Integer.valueOf(timeParts[0]); - int minute = Integer.valueOf(timeParts[1]); - int value = hour * 60 + minute; - if (allTimePoints[value]) { - return 0; - } - allTimePoints[value] = true; - } - - int min = Integer.MAX_VALUE; - int prev = 0; - int first = Integer.MAX_VALUE; - int last = Integer.MIN_VALUE; - for (int i = 0; i < ALL_POSSIBLE_TIMEPOINTS; i++) { - if (allTimePoints[i]) { - if (first != Integer.MAX_VALUE) { - min = Math.min(min, i - prev); - } - first = Math.min(first, i); - last = Math.max(last, i); - prev = i; - } - } - min = Math.min(min, (ALL_POSSIBLE_TIMEPOINTS - last + first)); - return min; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_54.java b/src/main/java/com/fishercoder/solutions/_54.java deleted file mode 100644 index 16949b86e4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_54.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -public class _54 { - - public static class Solution1 { - /** - * credit: https://leetcode.com/problems/spiral-matrix/discuss/20599/Super-Simple-and-Easy-to-Understand-Solution/185257 - */ - public List spiralOrder(int[][] matrix) { - if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { - return new ArrayList<>(); - } - int m = matrix.length; - int n = matrix[0].length; - List result = new ArrayList(); - int left = 0; - int right = n - 1; - int top = 0; - int bottom = m - 1; - while (result.size() < m * n) { - for (int j = left; j <= right && result.size() < m * n; j++) { - result.add(matrix[top][j]); - } - top++; - for (int i = top; i <= bottom && result.size() < m * n; i++) { - result.add(matrix[i][right]); - } - right--; - for (int j = right; j >= left && result.size() < m * n; j--) { - result.add(matrix[bottom][j]); - } - bottom--; - for (int i = bottom; i >= top && result.size() < m * n; i--) { - result.add(matrix[i][left]); - } - left++; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_540.java b/src/main/java/com/fishercoder/solutions/_540.java deleted file mode 100644 index 47e7d7e2a1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_540.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 540. Single Element in a Sorted Array - * - * Given a sorted array consisting of only integers where every - * element appears twice except for one element which appears once. - * Find this single element that appears only once. - - Example 1: - Input: [1,1,2,3,3,4,4,8,8] - Output: 2 - Example 2: - Input: [3,3,7,7,10,11,11] - Output: 10 - Note: Your solution should run in O(log n) time and O(1) space. - */ -public class _540 { - public static class Solution1 { - public int singleNonDuplicate(int[] nums) { - int result = 0; - for (int i = 0; i < nums.length; i++) { - result ^= nums[i]; - } - return result; - } - } - - public static class Solution2 { - public int singleNonDuplicate(int[] nums) { - int start = 0; - int end = nums.length - 1; - while (start < end) { - int mid = start + (end - start) / 2; - if (nums[mid] != nums[mid + 1] && nums[mid] != nums[mid - 1]) { - return nums[mid]; - } else if (nums[mid] == nums[mid + 1] && mid % 2 == 0) { - start = mid + 1; - } else if (nums[mid] == nums[mid - 1] && mid % 2 == 1) { - start = mid + 1; - } else { - end = mid - 1; - } - } - return nums[start]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_541.java b/src/main/java/com/fishercoder/solutions/_541.java deleted file mode 100644 index f47975d773..0000000000 --- a/src/main/java/com/fishercoder/solutions/_541.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 541. Reverse String II - * - * Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. - * If there are less than k characters left, reverse all of them. - * If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original. - - Example: - Input: s = "abcdefg", k = 2 - Output: "bacdfeg" - - Restrictions: - The string consists of lower English letters only. - Length of the given string and k will in the range [1, 10000] - */ -public class _541 { - - public static class Solution1 { - public String reverseStr(String s, int k) { - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < s.length(); i = i + 2 * k) { - if (s.length() >= (i + k)) { - stringBuilder.append(new StringBuilder(s.substring(i, i + k)).reverse()); - } else { - stringBuilder.append(new StringBuilder(s.substring(i)).reverse()); - break; - } - if ((i + 2 * k) <= s.length()) { - stringBuilder.append(s.substring(i + k, i + 2 * k)); - } else { - stringBuilder.append(s.substring(i + k)); - } - } - return stringBuilder.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_542.java b/src/main/java/com/fishercoder/solutions/_542.java deleted file mode 100644 index e3c96da60e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_542.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Deque; -import java.util.LinkedList; -import java.util.List; - -/** - * 542. 01 Matrix - * - * Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. - - The distance between two adjacent cells is 1. - Example 1: - Input: - - 0 0 0 - 0 1 0 - 0 0 0 - Output: - 0 0 0 - 0 1 0 - 0 0 0 - Example 2: - Input: - - 0 0 0 - 0 1 0 - 1 1 1 - Output: - 0 0 0 - 0 1 0 - 1 2 1 - - Note: - The number of elements of the given matrix will not exceed 10,000. - There are at least one 0 in the given matrix. - The cells are adjacent in only four directions: up, down, left and right. - */ -public class _542 { - - public static class Solution1 { - - public List> updateMatrix(List> matrix) { - int m = matrix.size(); - int n = matrix.get(0).size(); - Deque deque = new LinkedList<>(); - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (matrix.get(i).get(j) == 0) { - deque.offer(new int[]{i, j}); - } else { - matrix.get(i).set(j, Integer.MAX_VALUE); - } - } - } - - final int[] dirs = new int[]{0, 1, 0, -1, 0}; - while (!deque.isEmpty()) { - int[] currentCell = deque.poll(); - for (int i = 0; i < dirs.length - 1; i++) { - int nextRow = currentCell[0] + dirs[i]; - int nextCol = currentCell[1] + dirs[i + 1]; - if (nextRow < 0 || nextCol < 0 || nextRow >= m || nextCol >= n || matrix.get(nextRow).get(nextCol) <= matrix.get(currentCell[0]).get(currentCell[1]) + 1) { - continue; - } - deque.offer(new int[]{nextRow, nextCol}); - matrix.get(nextRow).set(nextCol, matrix.get(currentCell[0]).get(currentCell[1]) + 1); - } - } - return matrix; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_543.java b/src/main/java/com/fishercoder/solutions/_543.java deleted file mode 100644 index 9dfae1e6f7..0000000000 --- a/src/main/java/com/fishercoder/solutions/_543.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 543. Diameter of Binary Tree - * - * Given a binary tree, you need to compute the length of the diameter of the tree. - * The diameter of a binary tree is the length of the longest path between any two nodes in a tree. - * This path may or may not pass through the root. - - Example: - Given a binary tree - 1 - / \ - 2 3 - / \ - 4 5 - Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3]. - - Note: The length of path between two nodes is represented by the number of edges between them. - */ -public class _543 { - - public static class Solution1 { - /**This is a very great problem for practicing recursion: - * 1. What dfs() returns is the max height it should pick from either its left or right subtree, that's - * what the int return type stands for; - * 2. And during the recursion, we can keep updating the global variable: "diameter"; - * 3. When computing length/height of a subtree, we should take the max of its left and right, then plus one - * and left height should be like this - * int left = dfs(root.left); - * instead of dfs(root.left) + 1; - * we'll only plus one at the end - * */ - int diameter = 0; - - public int diameterOfBinaryTree(TreeNode root) { - dfs(root); - return diameter; - } - - private int dfs(TreeNode root) { - if (root == null) { - return 0; - } - int left = dfs(root.left); - int right = dfs(root.right); - diameter = Math.max(diameter, left + right); - return Math.max(left, right) + 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_544.java b/src/main/java/com/fishercoder/solutions/_544.java deleted file mode 100644 index 7b4cd5620f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_544.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, - * like make the rank 1 team play with the rank nth team, which is a good strategy to make the contest more interesting. - * Now, you're given n teams, you need to output their final contest matches in the form of a string. - * The n teams are given in the form of positive integers from 1 to n, which represents their initial rank. - * (Rank 1 is the strongest team and Rank n is the weakest team.) - * We'll use parentheses('(', ')') and commas(',') to represent the contest team pairing - parentheses('(' , ')') - * for pairing and commas(',') for partition. During the pairing process in each round, - * you always need to follow the strategy of making the rather strong one pair with the rather weak one. - - Example 1: - Input: 2 - Output: (1,2) - - Explanation: - Initially, we have the team 1 and the team 2, placed like: 1,2. - Then we pair the team (1,2) together with '(', ')' and ',', which is the final answer. - - - Example 2: - Input: 4 - Output: ((1,4),(2,3)) - - Explanation: - In the first round, we pair the team 1 and 4, the team 2 and 3 together, as we need to make the strong team and weak team together. - And we got (1,4),(2,3). - In the second round, the winners of (1,4) and (2,3) need to play again to generate the final winner, so you need to add the paratheses outside them. - And we got the final answer ((1,4),(2,3)). - - - Example 3: - Input: 8 - Output: (((1,8),(4,5)),((2,7),(3,6))) - - Explanation: - First round: (1,8),(2,7),(3,6),(4,5) - Second round: ((1,8),(4,5)),((2,7),(3,6)) - Third round: (((1,8),(4,5)),((2,7),(3,6))) - Since the third round will generate the final winner, you need to output the answer (((1,8),(4,5)),((2,7),(3,6))). - - Note: - The n is in range [2, 212]. - We ensure that the input n can be converted into the form 2k, where k is a positive integer. - */ -public class _544 { - - public String findContestMatch(int n) { - List pairs = new ArrayList<>(); - int left = 1; - int right = n; - while (left < right) { - pairs.add("(" + left + "," + right + ")"); - left++; - right--; - } - if (n == 2) { - return pairs.get(0); - } - return generateFinal(pairs, n / 2); - } - - private String generateFinal(List pairs, int n) { - if (n > 2) { - int size = pairs.size(); - int left = 0; - int right = size - 1; - List newPairs = new ArrayList<>(); - while (left < right) { - String newPair = "(" + pairs.get(left) + "," + pairs.get(right) + ")"; - newPairs.add(newPair); - left++; - right--; - } - return generateFinal(newPairs, n / 2); - } - return "(" + pairs.get(0) + "," + pairs.get(1) + ")"; - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_545.java b/src/main/java/com/fishercoder/solutions/_545.java deleted file mode 100644 index 73fb18e869..0000000000 --- a/src/main/java/com/fishercoder/solutions/_545.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 545. Boundary of Binary Tree - * - * Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root. - * Boundary includes left boundary, addLeaves, and right boundary in order without duplicate nodes. - * Left boundary is defined as the path from root to the left-most node. - * Right boundary is defined as the path from root to the right-most node. - * If the root doesn't have left subtree or right subtree, - * then the root itself is left boundary or right boundary. - * Note this definition only applies to the input binary tree, and not applies to any subtrees. - * The left-most node is defined as a leaf node you could reach when you always firstly travel - * to the left subtree if exists. If not, travel to the right subtree. - * Repeat until you reach a leaf node. - * The right-most node is also defined by the same way with left and right exchanged. - - Example 1 - Input: - 1 - \ - 2 - / \ - 3 4 - - Ouput: - [1, 3, 4, 2] - - Explanation: - The root doesn't have left subtree, so the root itself is left boundary. - The addLeaves are node 3 and 4. - The right boundary are node 1,2,4. Note the anti-clockwise direction means you should output reversed right boundary. - So order them in anti-clockwise without duplicates and we have [1,3,4,2]. - - - Example 2 - Input: - ____1_____ - / \ - 2 3 - / \ / -4 5 6 - / \ / \ - 7 8 9 10 - - Ouput: - [1,2,4,7,8,9,10,6,3] - - Explanation: - The left boundary are node 1,2,4. (4 is the left-most node according to definition) - The addLeaves are node 4,7,8,9,10. - The right boundary are node 1,3,6,10. (10 is the right-most node). - So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3]. - - */ -public class _545 { - public static class Solution1 { - public List boundaryOfBinaryTree(TreeNode root) { - List nodes = new ArrayList<>(); - if (root == null) { - return nodes; - } - - nodes.add(root.val); - leftBoundary(root.left, nodes); - addLeaves(root.left, nodes); - addLeaves(root.right, nodes); - rightBoundary(root.right, nodes); - return nodes; - } - - public void leftBoundary(TreeNode root, List nodes) { - if (root == null || (root.left == null && root.right == null)) { - /**we don't want to add any LEAVES in leftBoundary and rightBoundary functions either, - * that's why we have the later condition in the if branch.*/ - return; - } - nodes.add(root.val);// add BEFORE child visit - if (root.left == null) { - leftBoundary(root.right, nodes); - } else { - leftBoundary(root.left, nodes); - } - } - - public void rightBoundary(TreeNode root, List nodes) { - if (root == null || (root.right == null && root.left == null)) { - return; - } - if (root.right == null) { - rightBoundary(root.left, nodes); - } else { - rightBoundary(root.right, nodes); - } - nodes.add(root.val); // add AFTER child visit(reverse) - } - - public void addLeaves(TreeNode root, List nodes) { - if (root == null) { - return; - } - if (root.left == null && root.right == null) { - nodes.add(root.val); - return; - } - addLeaves(root.left, nodes); - addLeaves(root.right, nodes); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_546.java b/src/main/java/com/fishercoder/solutions/_546.java deleted file mode 100644 index 490660ce4b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_546.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 546. Remove Boxes - * - * Given several boxes with different colors represented by different positive numbers. - * You may experience several rounds to remove boxes until there is no box left. - * Each time you can choose some continuous boxes with the same color (composed of k boxes, k >= 1), remove them and get k*k points. - * Find the maximum points you can get. - - Example 1: - Input: - - [1, 3, 2, 2, 2, 3, 4, 3, 1] - Output: - 23 - - Explanation: - [1, 3, 2, 2, 2, 3, 4, 3, 1] - ----> [1, 3, 3, 4, 3, 1] (3*3=9 points) - ----> [1, 3, 3, 3, 1] (1*1=1 points) - ----> [1, 1] (3*3=9 points) - ----> [] (2*2=4 points) - Note: The number of boxes n would not exceed 100. - - */ - -public class _546 { - public static class Solution1 { - /** - * credit: https://leetcode.com/articles/remove-boxes/#approach-2-using-dp-with-memorizationaccepted - * - * For an entry in dp[l][r][k], l represents the starting index of the subarray, - * r represents the ending index of the subarray - * and k represents the number of elements similar to the r​th element - * following it which can be combined to obtain the point information to be stored in dp[l][r][k]. - */ - public int removeBoxes(int[] boxes) { - int[][][] dp = new int[100][100][100]; - return calculatePoints(boxes, dp, 0, boxes.length - 1, 0); - } - - public int calculatePoints(int[] boxes, int[][][] dp, int l, int r, int k) { - if (l > r) { - return 0; - } - if (dp[l][r][k] != 0) { - return dp[l][r][k]; - } - while (r > l && boxes[r] == boxes[r - 1]) { - r--; - k++; - } - dp[l][r][k] = calculatePoints(boxes, dp, l, r - 1, 0) + (k + 1) * (k + 1); - for (int i = l; i < r; i++) { - if (boxes[i] == boxes[r]) { - dp[l][r][k] = Math.max(dp[l][r][k], - calculatePoints(boxes, dp, l, i, k + 1) + calculatePoints(boxes, dp, i + 1, r - 1, 0)); - } - } - return dp[l][r][k]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_547.java b/src/main/java/com/fishercoder/solutions/_547.java deleted file mode 100644 index a42e54c2f4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_547.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder.solutions; - -/** - *547. Friend Circles - * - *There are N students in a class. - * Some of them are friends, while some are not. Their friendship is transitive in nature. - * For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. - * And we defined a friend circle is a group of students who are direct or indirect friends. - - Given a N*N matrix m representing the friend relationship between students in the class. - If m[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. - And you have to output the total number of friend circles among all the students. - - Example 1: - Input: - [[1,1,0], - [1,1,0], - [0,0,1]] - Output: 2 - Explanation:The 0th and 1st students are direct friends, so they are in a friend circle. - The 2nd student himself is in a friend circle. So return 2. - - Example 2: - Input: - [[1,1,0], - [1,1,1], - [0,1,1]] - Output: 1 - Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends, - so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1. - - Note: - N is in range [1,200]. - m[i][i] = 1 for all students. - If m[i][j] = 1, then m[j][i] = 1. - */ -public class _547 { - - public static class Solution1 { - public int findCircleNum(int[][] M) { - if (M == null || M.length == 0 || M[0].length == 0) { - return 0; - } - int m = M.length;//number of rows in this matrix - UnionFind unionFind = new UnionFind(m); - for (int i = 0; i < m; i++) { - for (int j = i + 1; j < m; j++) { - if (M[i][j] == 1) { - unionFind.union(i, j); - } - } - } - return unionFind.count; - } - - class UnionFind { - int count; - int[] root; - - public UnionFind(int m) { - root = new int[m]; - for (int i = 0; i < m; i++) { - root[i] = i; - } - count = m; - } - - public void union(int i, int j) { - int x = find(root, i); - int y = find(root, j); - if (x != y) { - count--; - root[x] = y;//path compression - } - } - - public int find(int[] ids, int i) { - if (ids[i] == i) { - return i; - } - return find(ids, ids[i]); - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_548.java b/src/main/java/com/fishercoder/solutions/_548.java deleted file mode 100644 index 24d25482b3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_548.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 548. Split Array with Equal Sum - * - * Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: - * 0 < i, i + 1 < j, j + 1 < k < n - 1 - * Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be equal. - * where we define that subarray (L, R) represents a slice of the original array starting from the element indexed L to the element indexed R. - - Example: - Input: [1,2,1,2,1,2,1] - Output: True - Explanation: - i = 1, j = 3, k = 5. - sum(0, i - 1) = sum(0, 0) = 1 - sum(i + 1, j - 1) = sum(2, 2) = 1 - sum(j + 1, k - 1) = sum(4, 4) = 1 - sum(k + 1, n - 1) = sum(6, 6) = 1 - - Note: - 1 <= n <= 2000. - Elements in the given array will be in range [-1,000,000, 1,000,000]. - */ -public class _548 { - - public static class Solution1 { - public boolean splitArray(int[] nums) { - int len = nums.length; - if (len < 7) { - return false; - } - int[] sum = new int[len]; - sum[0] = nums[0]; - for (int i = 1; i < len; i++) { - sum[i] = sum[i - 1] + nums[i]; - } - for (int j = 3; j < len - 3; j++) { - Set set = new HashSet<>(); - for (int i = 1; i < j - 1; i++) { - if (sum[i - 1] == sum[j - 1] - sum[i]) { - /**this is sum(0, i-1) and sum(i+1, j-1)*/ - set.add(sum[i - 1]); - } - } - for (int k = j + 2; k < len - 1; k++) { - if (sum[k - 1] - sum[j] == sum[len - 1] - sum[k] && set.contains(sum[k - 1] - sum[j])) { - /**this is sum(j+1, k-1) and sum(k+1, len-1)*/ - return true; - } - } - } - return false; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_549.java b/src/main/java/com/fishercoder/solutions/_549.java deleted file mode 100644 index 40fb288c40..0000000000 --- a/src/main/java/com/fishercoder/solutions/_549.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 549. Binary Tree Longest Consecutive Sequence II - * - * Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. - Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, - but the path [1,2,4,3] is not valid. On the other hand, the path can be in the child-Parent-child order, where not necessarily be parent-child order. - - Example 1: - Input: - 1 - / \ - 2 3 - Output: 2 - - Explanation: The longest consecutive path is [1, 2] or [2, 1]. - Example 2: - Input: - 2 - / \ - 1 3 - Output: 3 - Explanation: The longest consecutive path is [1, 2, 3] or [3, 2, 1]. - Note: All the values of tree nodes are in the range of [-1e7, 1e7]. - */ -public class _549 { - - public static class Solution1 { - int max = 0; - - public int longestConsecutive(TreeNode root) { - longestPath(root); - return max; - } - - private int[] longestPath(TreeNode root) { - if (root == null) { - return new int[]{0, 0}; - } - int increasing = 1; - int decreasing = 1; - if (root.left != null) { - int[] left = longestPath(root.left); - if (root.val == root.left.val + 1) { - decreasing = left[1] + 1; - } else if (root.val == root.left.val - 1) { - increasing = left[0] + 1; - } - } - - if (root.right != null) { - int[] right = longestPath(root.right); - if (root.val == root.right.val + 1) { - decreasing = Math.max(right[1] + 1, decreasing); - } else if (root.val == root.right.val - 1) { - increasing = Math.max(right[0] + 1, increasing); - } - } - - max = Math.max(max, decreasing + increasing - 1); - return new int[]{increasing, decreasing}; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_55.java b/src/main/java/com/fishercoder/solutions/_55.java deleted file mode 100644 index 0afbe7defb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_55.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.fishercoder.solutions; - -public class _55 { - - public static class Solution1 { - public boolean canJump(int[] nums) { - int farthest = nums[0]; - for (int i = 0; i < nums.length; i++) { - if (i <= farthest && nums[i] + i > farthest) { - //i <= farthest is to make sure that this current i is within the current range - // nums[i]+i > farthest is to make sure that it's necessary to update farthest with current nums[i]+i - farthest = nums[i] + i; - } - } - return farthest >= nums.length - 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_551.java b/src/main/java/com/fishercoder/solutions/_551.java deleted file mode 100644 index 9c16c6ddca..0000000000 --- a/src/main/java/com/fishercoder/solutions/_551.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 551. Student Attendance Record I - * - * You are given a string representing an attendance record for a student. The record only contains the following three characters: - - 'A' : Absent. - 'L' : Late. - 'P' : Present. - A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late). - - You need to return whether the student could be rewarded according to his attendance record. - - Example 1: - Input: "PPALLP" - Output: True - - Example 2: - Input: "PPALLL" - Output: False - - */ -public class _551 { - - public static class Solution1 { - public boolean checkRecord(String s) { - int aCount = 0; - for (int i = 0; i < s.length(); i++) { - if (s.charAt(i) == 'A') { - aCount++; - if (aCount > 1) { - return false; - } - } else if (s.charAt(i) == 'L') { - int continuousLCount = 0; - while (i < s.length() && s.charAt(i) == 'L') { - i++; - continuousLCount++; - if (continuousLCount > 2) { - return false; - } - } - i--; - } - } - return true; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_552.java b/src/main/java/com/fishercoder/solutions/_552.java deleted file mode 100644 index 65f1756546..0000000000 --- a/src/main/java/com/fishercoder/solutions/_552.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 552. Student Attendance Record II - * - * Given a positive integer n, return the number of all possible attendance records with length n, - * which will be regarded as rewardable. The answer may be very large, return it after mod 109 + 7. - - A student attendance record is a string that only contains the following three characters: - - 'A' : Absent. - 'L' : Late. - 'P' : Present. - - A record is regarded as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late). - - Example 1: - - Input: n = 2 - Output: 8 - - Explanation: - There are 8 records with length 2 will be regarded as rewardable: - "PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL" - Only "AA" won't be regarded as rewardable owing to more than one absent times. - - Note: The value of n won't exceed 100,000. - */ -public class _552 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/86526/improving-the-runtime-from-o-n-to-o-log-n - */ - public int checkRecord(int n) { - final int MOD = 1000000007; - int[][][] f = new int[n + 1][2][3]; - - f[0] = new int[][]{{1, 1, 1}, {1, 1, 1}}; - for (int i = 1; i <= n; i++) { - for (int j = 0; j < 2; j++) { - for (int k = 0; k < 3; k++) { - int val = f[i - 1][j][2]; // ...P - if (j > 0) { - val = (val + f[i - 1][j - 1][2]) % MOD; // ...A - } - if (k > 0) { - val = (val + f[i - 1][j][k - 1]) % MOD; // ...L - } - f[i][j][k] = val; - } - } - } - return f[n][1][2]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_553.java b/src/main/java/com/fishercoder/solutions/_553.java deleted file mode 100644 index 8ef3d46d6e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_553.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.StringJoiner; - -/** - * 553. Optimal Division - * - * Given a list of positive integers, the adjacent integers will perform the float division. For example, [2,3,4] -> 2 / 3 / 4. - However, you can add any number of parenthesis at any position to change the priority of operations. - You should find out how to add parenthesis to get the maximum result, - and return the corresponding expression in string format. - Your expression should NOT contain redundant parenthesis. - - Example: - Input: [1000,100,10,2] - Output: "1000/(100/10/2)" - Explanation: - 1000/(100/10/2) = 1000/((100/10)/2) = 200 - However, the bold parenthesis in "1000/((100/10)/2)" are redundant, - since they don't influence the operation priority. So you should return "1000/(100/10/2)". - - Other cases: - 1000/(100/10)/2 = 50 - 1000/(100/(10/2)) = 50 - 1000/100/10/2 = 0.5 - 1000/100/(10/2) = 2 - Note: - - The length of the input array is [1, 10]. - Elements in the given array will be in range [2, 1000]. - There is only one optimal division for each test case. - */ -public class _553 { - public static class Solution1 { - /** - * Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_553.java - */ - public String optimalDivision(int[] nums) { - /**https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html: - * StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. - * The String "[George:Sally:Fred]" may be constructed as follows: - StringJoiner sj = new StringJoiner(":", "[", "]"); - sj.add("George").add("Sally").add("Fred"); - String desiredString = sj.toString();*/ - - if (nums.length == 1) { - return "" + nums[0]; - } - if (nums.length == 2) { - return nums[0] + "/" + nums[1]; - } - - /**Tricky one: the solution is fixed: always wrap the one from the second until the last. - * Another important thing to note that such way could work is that: - * the prerequisite is: Elements will be in range [2,1000], so no elements are smaller than 1.*/ - StringJoiner stringJoiner = new StringJoiner("/"); - for (int i = 1; i < nums.length; i++) { - stringJoiner.add("" + nums[i]); - } - return String.format("%d/(%s)", nums[0], stringJoiner.toString()); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_554.java b/src/main/java/com/fishercoder/solutions/_554.java deleted file mode 100644 index ac12214ead..0000000000 --- a/src/main/java/com/fishercoder/solutions/_554.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 554. Brick Wall - * - * There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. - * The bricks have the same height but different width. - * You want to draw a vertical line from the top to the bottom and cross the least bricks. - * The brick wall is represented by a list of rows. - * Each row is a list of integers representing the width of each brick in this row from left to right. - * If your line go through the edge of a brick, - * then the brick is not considered as crossed. - * You need to find out how to draw the line to cross the least bricks and return the number of crossed bricks. - * You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks. - - Example: - Input: - [[1,2,2,1], - [3,1,2], - [1,3,2], - [2,4], - [3,1,2], - [1,3,1,1]] - - Output: 2 - Explanation: - - Note: - The width sum of bricks in different rows are the same and won't exceed INT_MAX. - The number of bricks in each row is in range [1,10,000]. The height of wall is in range [1,10,000]. Total number of bricks of the wall won't exceed 20,000. - */ -public class _554 { - public static class Solution1 { - /** - * credit: https://leetcode.com/articles/brick-wall/ - * - * we make use of a HashMap - * map which is used to store entries in the form: - * (sum,count). Here, - * sum refers to the cumulative sum of the bricks' widths encountered in the current row, and - * count refers to the number of times the corresponding sum is obtained. Thus, - * sum in a way, represents the positions of the bricks's boundaries relative to the leftmost boundary. - * - * This is done based on the following observation: - * We will never obtain the same value of sum twice while traversing over a particular row. - * Thus, if the sum value is repeated while traversing over the rows, it means some row's brick boundary coincides with some previous row's brick boundary. - * This fact is accounted for by incrementing the corresponding count value. - * But, for every row, we consider the sum only upto the second last brick, since the last boundary isn't a valid boundary for the solution. - */ - - public int leastBricks(List> wall) { - Map map = new HashMap(); - for (List row : wall) { - int sum = 0; - for (int i = 0; i < row.size() - 1; i++) { - //NOTE: i < row.size()-1 - sum += row.get(i); - if (map.containsKey(sum)) { - map.put(sum, map.get(sum) + 1); - } else { - map.put(sum, 1); - } - } - } - int result = wall.size(); - for (int key : map.keySet()) { - result = Math.min(result, wall.size() - map.get(key)); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_555.java b/src/main/java/com/fishercoder/solutions/_555.java deleted file mode 100644 index ab29eb2d3d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_555.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 555. Split Concatenated Strings - * - * Given a list of strings, you could concatenate these strings together into a loop, - * where for each string you could choose to reverse it or not. - * - * Among all the possible loops, you need to find the lexicographically biggest string after cutting the loop, - * which will make the looped string into a regular one. - * - * Specifically, to find the lexicographically biggest string, you need to experience two phases: - * - * 1. Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given. - * 2. Cut and make one breakpoint in any place of the loop, which will make the looped string into a regular one starting from - * the character at the cutpoint. - * - * And your job is to find the lexicographically biggest one among all the possible regular strings. - - Example: - Input: "abc", "xyz" - Output: "zyxcba" - - Explanation: You can get the looped string "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-", - where '-' represents the looped status. - The answer string came from the fourth looped one, - where you could cut from the middle character 'a' and get "zyxcba". - - Note: - The input strings will only contain lowercase letters. - The total length of all the strings will not over 1,000. - - */ -public class _555 { - - public static class Solution1 { - - /** - * credit: https://discuss.leetcode.com/topic/86477/neat-java-solution - * and article: https://leetcode.com/articles/split-assembled-strings/#approach-3-optimized-solution-accepted - */ - public String splitLoopedString(String[] strs) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < strs.length; i++) { - sb.setLength(0); - String reverse = sb.append(strs[i]).reverse().toString(); - if (strs[i].compareTo(reverse) < 0) { - strs[i] = reverse; - } - } - String result = ""; - for (int i = 0; i < strs.length; i++) { - sb.setLength(0); - String reverse = sb.append(strs[i]).reverse().toString(); - for (String str : new String[]{strs[i], reverse}) { - for (int k = 0; k < str.length(); k++) { - sb.setLength(0); - sb.append(str.substring(k)); - for (int j = i + 1; j < strs.length; j++) { - sb.append(strs[j]); - } - for (int j = 0; j < i; j++) { - sb.append(strs[j]); - } - sb.append(str.substring(0, k)); - if (sb.toString().compareTo(result) > 0) { - result = sb.toString(); - } - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_557.java b/src/main/java/com/fishercoder/solutions/_557.java deleted file mode 100644 index 91c2cf0dba..0000000000 --- a/src/main/java/com/fishercoder/solutions/_557.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 557. Reverse Words in a String III - * - * Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. - - Example 1: - Input: "Let's take LeetCode contest" - Output: "s'teL ekat edoCteeL tsetnoc" - Note: In the string, each word is separated by single space and there will not be any extra space in the string. - */ -public class _557 { - - public static class Solution1 { - public String reverseWords(String s) { - StringBuilder stringBuilder = new StringBuilder(); - StringBuilder sbUtils = new StringBuilder(); - for (String word : s.split(" ")) { - sbUtils.setLength(0); - sbUtils.append(word); - stringBuilder.append(sbUtils.reverse().toString()); - stringBuilder.append(" "); - } - stringBuilder.setLength(stringBuilder.length() - 1); - return stringBuilder.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_559.java b/src/main/java/com/fishercoder/solutions/_559.java deleted file mode 100644 index c82be1ae52..0000000000 --- a/src/main/java/com/fishercoder/solutions/_559.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Node; -import java.util.ArrayList; -import java.util.List; - -/** - * 559. Maximum Depth of N-ary Tree - * - * Given a n-ary tree, find its maximum depth. - * - * The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. - * - * For example, given a 3-ary tree: - * 1 - * / | \ - * 3 2 4 - * / \ - * 5 6 - * - * We should return its max depth, which is 3. - * - * Note: - * - * The depth of the tree is at most 1000. - * The total number of nodes is at most 5000. - */ -public class _559 { - public static class Solution1 { - public int maxDepth(Node root) { - int maxDepth = 0; - if (root == null) { - return maxDepth; - } - List> allPaths = new ArrayList<>(); - List currentPath = new ArrayList<>(); - dfs(root, currentPath, allPaths); - for (List path : allPaths) { - maxDepth = Math.max(path.size(), maxDepth); - } - return maxDepth; - } - - private void dfs(Node root, List currentPath, List> allPaths) { - if (root == null) { - allPaths.add(new ArrayList<>(currentPath)); - } - if (root.children != null && !root.children.isEmpty()) { - currentPath.add(root.val); - for (Node child : root.children) { - dfs(child, new ArrayList<>(currentPath), allPaths); - } - } - if (root.children == null || root.children.isEmpty()) { - currentPath.add(root.val); - allPaths.add(new ArrayList<>(currentPath)); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_56.java b/src/main/java/com/fishercoder/solutions/_56.java deleted file mode 100644 index 72e96ff36e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_56.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Interval; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class _56 { - - public static class Solution1 { - public List merge(List intervals) { - if (intervals.size() <= 1) { - return intervals; - } - - Collections.sort(intervals, (o1, o2) -> o1.start - o2.start); - - List result = new ArrayList(); - for (int i = 0; i < intervals.size(); i++) { - int start = intervals.get(i).start; - int end = intervals.get(i).end; - while (i < intervals.size() && end >= intervals.get(i).start) { - end = Math.max(end, intervals.get(i).end); - i++; - } - result.add(new Interval(start, end)); - i--; - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_560.java b/src/main/java/com/fishercoder/solutions/_560.java deleted file mode 100644 index 6f4fdfe1fe..0000000000 --- a/src/main/java/com/fishercoder/solutions/_560.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 560. Subarray Sum Equals K - * - * Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. - - Example 1: - Input:nums = [1,1,1], k = 2 - Output: 2 - Note: - The length of the array is in range [1, 20,000]. - The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7]. - */ -public class _560 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/87850/java-solution-presum-hashmap - * We know the key to solve this problem is SUM[i, j]. - * So if we know SUM[0, i - 1] and SUM[0, j], - * then we can easily get SUM[i, j] via (SUM[0, j] - SUM[0, i-1]). - * To achieve this, we just need to go through the array, - * calculate the current sum and save number of all seen PreSum to a HashMap. - *

- * Time complexity O(n), Space complexity O(n). - */ - public int subarraySum(int[] nums, int k) { - Map preSum = new HashMap(); - int sum = 0; - int result = 0; - preSum.put(0, 1); - for (int i = 0; i < nums.length; i++) { - sum += nums[i]; - if (preSum.containsKey(sum - k)) { - result += preSum.get(sum - k); - } - preSum.put(sum, preSum.getOrDefault(sum, 0) + 1); - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_561.java b/src/main/java/com/fishercoder/solutions/_561.java deleted file mode 100644 index 37f2c0a34a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_561.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -/** - * Created by fishercoder on 4/23/17. - */ - -import java.util.Arrays; - -/** - * Array Partition I: - * - * - * Given an array of 2n integers, your task is to group these integers into n pairs of integer, - * say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. - - Example 1: - Input: [1,4,3,2] - - Output: 4 - Explanation: n is 2, and the maximum sum of pairs is 4. - - Note: - n is a positive integer, which is in the range of [1, 10000]. - All the integers in the array will be in the range of [-10000, 10000].*/ -public class _561 { - - public static class Solution1 { - public int arrayPairSum(int[] nums) { - Arrays.sort(nums); - int sum = 0; - for (int i = 0; i < nums.length; i += 2) { - sum += nums[i]; - } - return sum; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_562.java b/src/main/java/com/fishercoder/solutions/_562.java deleted file mode 100644 index d077e8266f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_562.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -/** - * Created by fishercoder on 4/23/17. - * - * 562. Longest Line of Consecutive One in Matrix - * - * Given a 01 matrix m, find the longest line of consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal. - - Example: - - Input: - [[0,1,1,0], - [0,1,1,0], - [0,0,0,1]] - - Output: 3 - Hint: The number of elements in the given matrix will not exceed 10,000. - */ -public class _562 { - - public static class Solution1 { - public int longestLine(int[][] M) { - if (M == null || M.length == 0) { - return 0; - } - int[][] directions = new int[][]{ - {-1, 0}, - {-1, 1}, - {0, 1}, - {1, 1}, - {1, 0}, - {1, -1}, - {0, -1}, - {-1, -1}, - }; - int longestLine = 0; - int m = M.length; - int n = M[0].length; - int[][][] cache = new int[m][n][8]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (M[i][j] == 1) { - for (int k = 0; k < directions.length; k++) { - int nextI = i + directions[k][0]; - int nextJ = j + directions[k][1]; - int thisLine = 1; - if (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && cache[nextI][nextJ][k] != 0) { - thisLine += cache[nextI][nextJ][k]; - cache[i][j][k] = thisLine; - } else { - while (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && M[nextI][nextJ] == 1) { - thisLine++; - cache[i][j][k] = thisLine; - nextI += directions[k][0]; - nextJ += directions[k][1]; - } - } - longestLine = Math.max(longestLine, thisLine); - } - } - } - } - return longestLine; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_563.java b/src/main/java/com/fishercoder/solutions/_563.java deleted file mode 100644 index ca691f2daf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_563.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 563. Binary Tree Tilt - * - * Given a binary tree, return the tilt of the whole tree. - * The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values - * and the sum of all right subtree node values. - * Null node has tilt 0. - * The tilt of the whole tree is defined as the sum of all nodes' tilt. - - Example: - Input: - 1 - / \ - 2 3 - - Output: 1 - - Explanation: - Tilt of node 2 : 0 - Tilt of node 3 : 0 - Tilt of node 1 : |2-3| = 1 - Tilt of binary tree : 0 + 0 + 1 = 1 - - Note: - The sum of node values in any subtree won't exceed the range of 32-bit integer. - All the tilt values won't exceed the range of 32-bit integer.*/ -public class _563 { - public static class Solution1 { - - int tilt = 0; - - public int findTilt(TreeNode root) { - findTiltDfs(root); - return tilt; - } - - public int findTiltDfs(TreeNode root) { - if (root == null) { - return 0; - } - int leftTilt = 0; - if (root.left != null) { - leftTilt = findTiltDfs(root.left); - } - int rightTilt = 0; - if (root.right != null) { - rightTilt = findTiltDfs(root.right); - } - if (root.left == null && root.right == null) { - return root.val; - } - tilt += Math.abs(leftTilt - rightTilt); - return leftTilt + rightTilt + root.val; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_564.java b/src/main/java/com/fishercoder/solutions/_564.java deleted file mode 100644 index ed3d123e6c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_564.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 564. Find the Closest Palindrome - * - * Given an integer n, find the closest integer (not including itself), which is a palindrome. - - The 'closest' is defined as absolute difference minimized between two integers. - - Example 1: - - Input: "123" - Output: "121" - - Note: - - The input n is a positive integer represented by string, whose length will not exceed 18. - If there is a tie, return the smaller one as answer. - - */ -public class _564 { - public static class Solution1 { - - public String nearestPalindromic(String n) { - if (n.length() >= 2 && allNine(n)) { - String s = "1"; - for (int i = 0; i < n.length() - 1; i++) { - s += "0"; - } - s += "1"; - return s; - } - boolean isOdd = (n.length() % 2 != 0); - String left = n.substring(0, (n.length() + 1) / 2); - long[] increment = {-1, 0, +1}; - String ret = n; - long minDiff = Long.MAX_VALUE; - for (long i : increment) { - String s = getPalindrom(Long.toString(Long.parseLong(left) + i), isOdd); - if (n.length() >= 2 && (s.length() != n.length() || Long.parseLong(s) == 0)) { - s = ""; - for (int j = 0; j < n.length() - 1; j++) { - s += "9"; - } - } - long diff = s.equals(n) ? Long.MAX_VALUE : Math.abs(Long.parseLong(s) - Long.parseLong(n)); - if (diff < minDiff) { - minDiff = diff; - ret = s; - } - } - return ret; - } - - private String getPalindrom(String s, boolean isOdd) { - String right = new StringBuilder(s).reverse().toString(); - return isOdd ? s.substring(0, s.length() - 1) + right : s + right; - } - - private boolean allNine(String s) { - for (int i = 0; i < s.length(); i++) { - if (s.charAt(i) != '9') { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_565.java b/src/main/java/com/fishercoder/solutions/_565.java deleted file mode 100644 index 7fbc30ddd1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_565.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 565. Array Nesting - * - A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1]. - - Sets S[K] for 0 <= K < N are defined as follows: - - S[K] = { A[K], A[A[K]], A[A[A[K]]], ... }. - - Sets S[K] are finite for each K and should NOT contain duplicates. - - Write a function that given an array A consisting of N integers, return the size of the largest set S[K] for this array. - - Example 1: - Input: A = [5,4,0,3,1,6,2] - Output: 4 - Explanation: - A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2. - - One of the longest S[K]: - S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0} - Note: - N is an integer within the range [1, 20,000]. - The elements of A are all distinct. - Each element of array A is an integer within the range [0, N-1]. - */ -public class _565 { - - public static class Solution1 { - public int arrayNesting(int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - boolean[] visited = new boolean[nums.length]; - int answer = 0; - for (int i : nums) { - int count = 0; - int j = i; - while (j >= 0 && j < nums.length && !visited[j]) { - count++; - visited[j] = true; - j = nums[j]; - } - answer = Math.max(answer, count); - } - return answer; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_566.java b/src/main/java/com/fishercoder/solutions/_566.java deleted file mode 100644 index 2ced9445d7..0000000000 --- a/src/main/java/com/fishercoder/solutions/_566.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 566. Reshape the Matrix - * - In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. - You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively. - The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were. - If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix. - - Example 1: - Input: - nums = - [[1,2], - [3,4]] - r = 1, c = 4 - Output: - [[1,2,3,4]] - Explanation: - The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list. - - Example 2: - Input: - nums = - [[1,2], - [3,4]] - r = 2, c = 4 - Output: - [[1,2], - [3,4]] - Explanation: - There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix. - - Note: - The height and width of the given matrix is in range [1, 100]. - The given r and c are all positive. - */ -public class _566 { - - public static class Solution1 { - public int[][] matrixReshape(int[][] nums, int r, int c) { - if (nums == null || nums.length == 0) { - return nums; - } - int m = nums.length; - int n = nums[0].length; - if (r * c > m * n) { - return nums; - } - int k = 0; - int[][] result = new int[r][c]; - for (int i = 0; i < r; i++) { - for (int j = 0; j < c; j++, k++) { - result[i][j] = nums[k / n][k % n]; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_567.java b/src/main/java/com/fishercoder/solutions/_567.java deleted file mode 100644 index bc638e1339..0000000000 --- a/src/main/java/com/fishercoder/solutions/_567.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 567. Permutation in String - * - * Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. - * In other words, one of the first string's permutations is the substring of the second string. - - Example 1: - Input:s1 = "ab" s2 = "eidbaooo" - Output:True - Explanation: s2 contains one permutation of s1 ("ba"). - - Example 2: - Input:s1= "ab" s2 = "eidboaoo" - Output: False - - Note: - The input strings only contain lower case letters. - The length of both given strings is in range [1, 10,000]. - */ -public class _567 { - - public static class Solution1 { - /** - * credit: sliding window: https://discuss.leetcode.com/topic/87845/java-solution-sliding-window - */ - public boolean checkInclusion(String s1, String s2) { - int len1 = s1.length(); - int len2 = s2.length(); - if (len1 > len2) { - return false; - } - - int[] count = new int[26]; - for (int i = 0; i < len1; i++) { - count[s1.charAt(i) - 'a']++; - count[s2.charAt(i) - 'a']--; - } - - if (allZeroes(count)) { - return true; - } - - for (int i = len1; i < len2; i++) { - count[s2.charAt(i) - 'a']--; - count[s2.charAt(i - len1) - 'a']++; - if (allZeroes(count)) { - return true; - } - } - - return false; - } - - private boolean allZeroes(int[] count) { - for (int i : count) { - if (i != 0) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_568.java b/src/main/java/com/fishercoder/solutions/_568.java deleted file mode 100644 index f94fa56640..0000000000 --- a/src/main/java/com/fishercoder/solutions/_568.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 568. Maximum Vacation Days - * - * LeetCode wants to give one of its best employees the option to travel among N cities to collect algorithm problems. - * But all work and no play makes Jack a dull boy, - * you could take vacations in some particular cities and weeks. - * Your job is to schedule the traveling to maximize the number of - * vacation days you could take, but there are certain rules and restrictions you need to follow. - - Rules and restrictions: - You can only travel among N cities, represented by indexes from 0 to N-1. Initially, you are in the city indexed 0 on Monday. - The cities are connected by flights. - The flights are represented as a N*N matrix (not necessary symmetrical), - called flights representing the airline status from the city i to the city j. - If there is no flight from the city i to the city j, flights[i][j] = 0; - Otherwise, flights[i][j] = 1. Also, flights[i][i] = 0 for all i. - - You totally have K weeks (each week has 7 days) to travel. - You can only take flights at most once per day and can only take flights on each week's Monday morning. - Since flight time is so short, we don't consider the impact of flight time. - For each city, you can only have restricted vacation days in different weeks, - given an N*K matrix called days representing this relationship. - For the value of days[i][j], it represents the maximum days you could take vacation in the city i in the week j. - You're given the flights matrix and days matrix, and you need to output the maximum vacation days you could take during K weeks. - - Example 1: - Input:flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]] - Output: 12 - Explanation: - Ans = 6 + 3 + 3 = 12. - - One of the best strategies is: - 1st week : fly from city 0 to city 1 on Monday, and play 6 days and work 1 day. - (Although you start at city 0, we could also fly to and start at other cities since it is Monday.) - 2nd week : fly from city 1 to city 2 on Monday, and play 3 days and work 4 days. - 3rd week : stay at city 2, and play 3 days and work 4 days. - - - Example 2: - Input:flights = [[0,0,0],[0,0,0],[0,0,0]], days = [[1,1,1],[7,7,7],[7,7,7]] - Output: 3 - Explanation: - Ans = 1 + 1 + 1 = 3. - - Since there is no flights enable you to move to another city, you have to stay at city 0 for the whole 3 weeks. - For each week, you only have one day to play and six days to work. - So the maximum number of vacation days is 3. - - - Example 3: - Input:flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[7,0,0],[0,7,0],[0,0,7]] - Output: 21 - Explanation: - Ans = 7 + 7 + 7 = 21 - - One of the best strategies is: - 1st week : stay at city 0, and play 7 days. - 2nd week : fly from city 0 to city 1 on Monday, and play 7 days. - 3rd week : fly from city 1 to city 2 on Monday, and play 7 days. - - Note: - N and K are positive integers, which are in the range of [1, 100]. - In the matrix flights, all the values are integers in the range of [0, 1]. - In the matrix days, all the values are integers in the range [0, 7]. - You could stay at a city beyond the number of vacation days, - but you should work on the extra days, which won't be counted as vacation days. - If you fly from the city A to the city B and take the vacation on that day, - the deduction towards vacation days will count towards the vacation days of city B in that week. - We don't consider the impact of flight hours towards the calculation of vacation days. - */ -public class _568 { - - public static class Solution1 { - /** - * credit: https://leetcode.com/articles/maximum-vacation-days/#approach-2-using-dfs-with-memoization-accepted - */ - public int maxVacationDays(int[][] flights, int[][] days) { - int[][] memo = new int[flights.length][days[0].length]; - for (int[] l : memo) { - Arrays.fill(l, Integer.MIN_VALUE); - } - return dfs(flights, days, 0, 0, memo); - } - - public int dfs(int[][] flights, int[][] days, int curCity, int weekno, int[][] memo) { - if (weekno == days[0].length) { - return 0; - } - if (memo[curCity][weekno] != Integer.MIN_VALUE) { - return memo[curCity][weekno]; - } - int maxvac = 0; - for (int i = 0; i < flights.length; i++) { - if (flights[curCity][i] == 1 || i == curCity) { - int vac = days[i][weekno] + dfs(flights, days, i, weekno + 1, memo); - maxvac = Math.max(maxvac, vac); - } - } - memo[curCity][weekno] = maxvac; - return maxvac; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_57.java b/src/main/java/com/fishercoder/solutions/_57.java deleted file mode 100644 index cabf6d440a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_57.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Interval; - -import java.util.ArrayList; -import java.util.List; - -public class _57 { - - public static class Solution1 { - public List insert(List intervals, Interval newInterval) { - List result = new ArrayList<>(); - int i = 0; - // add all the intervals ending before newInterval starts - while (i < intervals.size() && intervals.get(i).end < newInterval.start) { - result.add(intervals.get(i++)); - } - // merge all overlapping intervals to one considering newInterval - while (i < intervals.size() && intervals.get(i).start <= newInterval.end) { - newInterval = new Interval( // we could mutate newInterval here also - Math.min(newInterval.start, intervals.get(i).start), - Math.max(newInterval.end, intervals.get(i).end)); - i++; - } - result.add(newInterval); - // add all the rest - while (i < intervals.size()) { - result.add(intervals.get(i++)); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_572.java b/src/main/java/com/fishercoder/solutions/_572.java deleted file mode 100644 index 06506fe133..0000000000 --- a/src/main/java/com/fishercoder/solutions/_572.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 572. Subtree of Another Tree - * - Given two non-empty binary trees s and t, - check whether tree t has exactly the same structure and node values with a subtree of s. - A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself. - - Example 1: - Given tree s: - - 3 - / \ - 4 5 - / \ - 1 2 - Given tree t: - 4 - / \ - 1 2 - Return true, because t has the same structure and node values with a subtree of s. - - Example 2: - Given tree s: - - 3 - / \ - 4 5 - / \ - 1 2 - / - 0 - - Given tree t: - 4 - / \ - 1 2 - Return false. - */ -public class _572 { - - public static class Solution1 { - public boolean isSubtree(TreeNode s, TreeNode t) { - if (s == null && t == null) { - return true; - } - boolean isSubTree = false; - if (s != null && t != null && s.val == t.val) { - isSubTree = isSameTree(s, t); - } - if (isSubTree) { - return true; - } - boolean isSubTreeLeft = false; - if (s.left != null) { - isSubTreeLeft = isSubtree(s.left, t); - } - if (isSubTreeLeft) { - return true; - } - boolean isSubTreeRight = false; - if (s.right != null) { - isSubTreeRight = isSubtree(s.right, t); - } - if (isSubTreeRight) { - return true; - } - return false; - } - - private boolean isSameTree(TreeNode p, TreeNode q) { - if (p == null || q == null) { - return p == q; - } - return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); - } - } - - public static class Solution2 { - public boolean isSubtree(TreeNode s, TreeNode t) { - if (s == null) { - return false; - } - if (same(s, t)) { - return true; - } - return isSubtree(s.left, t) || isSubtree(s.right, t); - } - - private boolean same(TreeNode s, TreeNode t) { - if (s == null || t == null) { - return s == t; - } - if (s.val != t.val) { - return false; - } - return same(s.left, t.left) && same(s.right, t.right); - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_573.java b/src/main/java/com/fishercoder/solutions/_573.java deleted file mode 100644 index 41f63bb306..0000000000 --- a/src/main/java/com/fishercoder/solutions/_573.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 573. Squirrel Simulation - * - * There's a tree, a squirrel, and several nuts. - * Positions are represented by the cells in a 2D grid. - * Your goal is to find the minimal distance for the squirrel to collect all the nuts and - * put them under the tree one by one. - * The squirrel can only take at most one nut at one time and can move in four directions - - * up, down, left and right, to the adjacent cell. - * The distance is represented by the number of moves. - - Example 1: - - Input: - Height : 5 - Width : 7 - Tree position : [2,2] - Squirrel : [4,4] - Nuts : [[3,0], [2,5]] - Output: 12 - - Explanation: - - Note: - - All given positions won't overlap. - The squirrel can take at most one nut at one time. - The given positions of nuts have no order. - Height and width are positive integers. 3 <= height * width <= 10,000. - The given positions contain at least one nut, only one tree and one squirrel. - */ -public class _573 { - - public static class Solution1 { - - /** - * reference: https://leetcode.com/articles/squirrel-simulation - * - * 1. The order in which to pick the nuts does not matter except the first one - * because for all the other nuts, the squirrel needs to travel back and forth. - * - * 2. For the first nut to be picked, there's some distance we might be able to save, what is this distance? - * It is the difference between the squirrel's original starting point to the first nut and that the distance from this - * first nut to the tree. - * This is because, only for the first nut, the squirrel does NOT need to travel back and forth, it only needs to travel from - * its starting position to the nut position and then return to the tree. - * - * 3. For the rest of all other nuts, the squirrel always needs to go back and forth. - * - * 4. So how can we find the first nut to go to so that we could have the maximum saved distance? - * We iterate through all of the nuts and keep updating the savedDist as below: - */ - public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) { - int totalDist = 0; - int savedDist = Integer.MIN_VALUE; - for (int[] nut : nuts) { - totalDist += (getDist(nut, tree) * 2);//it needs to travel back and forth, so times two - savedDist = Math.max(savedDist, getDist(nut, tree) - getDist(nut, squirrel)); - } - return totalDist - savedDist; - } - - private int getDist(int[] pointA, int[] pointB) { - return Math.abs(pointA[0] - pointB[0]) + Math.abs(pointA[1] - pointB[1]); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_575.java b/src/main/java/com/fishercoder/solutions/_575.java deleted file mode 100644 index e44ef3e8c5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_575.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 575. Distribute Candies - * - Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain. - - Example 1: - Input: candies = [1,1,2,2,3,3] - Output: 3 - Explanation: - There are three different kinds of candies (1, 2 and 3), and two candies for each kind. - Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too. - The sister has three different kinds of candies. - Example 2: - Input: candies = [1,1,2,3] - Output: 2 - Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1]. - The sister has two different kinds of candies, the brother has only one kind of candies. - Note: - - The length of the given array is in range [2, 10,000], and will be even. - The number in given array is in range [-100,000, 100,000]. - */ -public class _575 { - public static class Solution1 { - public int distributeCandies(int[] candies) { - Arrays.sort(candies); - int sisCount = 0; - for (int i = 0; i < candies.length; i++) { - int val = candies[i]; - sisCount++; - if (sisCount >= candies.length / 2) { - return candies.length / 2; - } - while (i < candies.length && candies[i] == val) { - i++; - } - i--; - } - return sisCount; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_576.java b/src/main/java/com/fishercoder/solutions/_576.java deleted file mode 100644 index f1781a1675..0000000000 --- a/src/main/java/com/fishercoder/solutions/_576.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 576. Out of Boundary Paths - * - * There is an m by n grid with a ball. - * Given the start coordinate (i,j) of the ball, - * you can move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). - * However, you can at most move N times. Find out the number of paths to move the ball out of grid boundary. - * The answer may be very large, return it after mod 109 + 7. - - Example 1: - - Input:m = 2, n = 2, N = 2, i = 0, j = 0 - Output: 6 - Explanation: - - Example 2: - - Input:m = 1, n = 3, N = 3, i = 0, j = 1 - Output: 12 - Explanation: - - Note: - - Once you move the ball out of boundary, you cannot move it back. - The length and height of the grid is in range [1,50]. - N is in range [0,50]. - - */ -public class _576 { - public static class Solution1 { - /** - * reference: https://leetcode.com/articles/out-of-boundary-paths/#approach-2-recursion-with-memoization-accepted - */ - public int findPaths(int m, int n, int N, int x, int y) { - int M = 1000000000 + 7; - int[][] dp = new int[m][n]; - dp[x][y] = 1; - int count = 0; - for (int moves = 1; moves <= N; moves++) { - int[][] temp = new int[m][n]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (i == m - 1) { - count = (count + dp[i][j]) % M; - } - if (j == n - 1) { - count = (count + dp[i][j]) % M; - } - if (i == 0) { - count = (count + dp[i][j]) % M; - } - if (j == 0) { - count = (count + dp[i][j]) % M; - } - temp[i][j] = (((i > 0 ? dp[i - 1][j] : 0) + (i < m - 1 ? dp[i + 1][j] : 0)) % M - + ((j > 0 ? dp[i][j - 1] : 0) + (j < n - 1 ? dp[i][j + 1] : 0)) % M) % M; - } - } - dp = temp; - } - return count; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_581.java b/src/main/java/com/fishercoder/solutions/_581.java deleted file mode 100644 index 337310b421..0000000000 --- a/src/main/java/com/fishercoder/solutions/_581.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 581. Shortest Unsorted Continuous Subarray - * - * Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. - - You need to find the shortest such subarray and output its length. - - Example 1: - Input: [2, 6, 4, 8, 10, 9, 15] - Output: 5 - Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order. - - Note: - Then length of the input array is in range [1, 10,000]. - The input array may contain duplicates, so ascending order here means <=. - - */ -public class _581 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/89282/java-o-n-time-o-1-space - * Use start and end to keep track of the minimum subarray nums[start...end] which must be sorted for the entire array nums. - * If start < end < 0 at the end of the for loop, then the array is already fully sorted. - * - * Time: O(n) - * Space: O(1) - */ - public int findUnsortedSubarray(int[] nums) { - int n = nums.length; - int start = -1; - int end = -2; - int min = nums[n - 1]; - int max = nums[0]; - for (int i = 1; i < n; i++) { - max = Math.max(max, nums[i]); - min = Math.min(min, nums[n - 1 - i]); - if (nums[i] < max) { - end = i; - } - if (nums[n - 1 - i] > min) { - start = n - 1 - i; - } - } - return end - start + 1; - } - } - - public static class Solution2 { - /** - * Time: O(nlogn) - * Space: O(n) - */ - public int findUnsortedSubarray(int[] nums) { - int[] clones = nums.clone(); - Arrays.sort(clones); - int start = nums.length; - int end = 0; - for (int i = 0; i < nums.length; i++) { - if (clones[i] != nums[i]) { - start = Math.min(start, i); - end = Math.max(end, i); - } - } - return (end - start > 0) ? end - start + 1 : 0; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_582.java b/src/main/java/com/fishercoder/solutions/_582.java deleted file mode 100644 index eb32347c85..0000000000 --- a/src/main/java/com/fishercoder/solutions/_582.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Deque; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -/** - * 582. Kill Process - * - * Given n processes, each process has a unique PID (process id) and its PPID (parent process id). - - Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers. - - We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID. - - Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer. - - Example 1: - Input: - pid = [1, 3, 10, 5] - ppid = [3, 0, 5, 3] - kill = 5 - Output: [5,10] - - Explanation: - 3 - / \ - 1 5 - / - 10 - - Kill 5 will also kill 10. - - Note: - The given kill id is guaranteed to be one of the given PIDs. - n >= 1. - */ -public class _582 { - - public static class Solution1 { - public List killProcess(List pid, List ppid, int kill) { - Map> map = new HashMap<>(); - for (int i = 0; i < pid.size(); i++) { - map.putIfAbsent(ppid.get(i), new LinkedList<>()); - map.get(ppid.get(i)).add(pid.get(i)); - } - List result = new LinkedList<>(); - Deque stack = new ArrayDeque<>(); - stack.offer(kill); - while (!stack.isEmpty()) { - int curr = stack.poll(); - result.add(curr); - List list = map.get(curr); - if (list != null) { - stack.addAll(list); - } - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_583.java b/src/main/java/com/fishercoder/solutions/_583.java deleted file mode 100644 index fcf4841285..0000000000 --- a/src/main/java/com/fishercoder/solutions/_583.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 583. Delete Operation for Two Strings - * - * Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string. - - Example 1: - Input: "sea", "eat" - Output: 2 - - Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea". - - Note: - The length of given words won't exceed 500. - Characters in given words can only be lower-case letters. - */ -public class _583 { - - public static class Solution1 { - - public int minDistance(String word1, String word2) { - int m = word1.length(); - int n = word2.length(); - int[][] dp = new int[m + 1][n + 1]; - for (int i = 1; i <= m; i++) { - for (int j = 1; j <= n; j++) { - dp[i][j] = word1.charAt(i - 1) == word2.charAt(j - 1) ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]); - } - } - return m + n - 2 * dp[m][n]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_587.java b/src/main/java/com/fishercoder/solutions/_587.java deleted file mode 100644 index 002adfe0fe..0000000000 --- a/src/main/java/com/fishercoder/solutions/_587.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Point; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 587. Erect the Fence - * - * There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden. - * Your job is to fence the entire garden using the minimum length of rope as it is expensive. - * The garden is well fenced only if all the trees are enclosed. - * Your task is to help find the coordinates of trees which are exactly located on the fence perimeter. - - Example 1: - Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] - Output: [[1,1],[2,0],[4,2],[3,3],[2,4]] - Explanation: - - Example 2: - Input: [[1,2],[2,2],[4,2]] - Output: [[1,2],[2,2],[4,2]] - Explanation: - - Even you only have trees in a line, you need to use rope to enclose them. - - Note: - All trees should be enclosed together. You cannot cut the rope to enclose trees that will separate them in more than one group. - All input integers will range from 0 to 100. - The garden has at least one tree. - All coordinates are distinct. - Input points have NO order. No order required for output. - */ -public class _587 { - public static class Solution1 { - - /** - * credit: https://discuss.leetcode.com/topic/89323/java-solution-convex-hull-algorithm-gift-wrapping-aka-jarvis-march - * There are couple of ways to solve Convex Hull problem. https://en.wikipedia.org/wiki/Convex_hull_algorithms - * The following code implements Gift wrapping aka Jarvis march algorithm - * https://en.wikipedia.org/wiki/Gift_wrapping_algorithm and - * also added logic to handle case of multiple Points in a line - * because original Jarvis march algorithm assumes no three points are collinear. - * It also uses knowledge in this problem https://leetcode.com/problems/convex-polygon. - * Disscussion: https://discuss.leetcode.com/topic/70706/beyond-my-knowledge-java-solution-with-in-line-explanation - */ - public List outerTrees(Point[] points) { - Set result = new HashSet<>(); - - // Find the leftmost point - Point first = points[0]; - int firstIndex = 0; - for (int i = 1; i < points.length; i++) { - if (points[i].x < first.x) { - first = points[i]; - firstIndex = i; - } - } - result.add(first); - - Point cur = first; - int curIndex = firstIndex; - do { - Point next = points[0]; - int nextIndex = 0; - for (int i = 1; i < points.length; i++) { - if (i == curIndex) { - continue; - } - int cross = crossProductLength(cur, points[i], next); - if (nextIndex == curIndex || cross > 0 - // Handle collinear points - || (cross == 0 && distance(points[i], cur) > distance(next, cur))) { - next = points[i]; - nextIndex = i; - } - } - // Handle collinear points - for (int i = 0; i < points.length; i++) { - if (i == curIndex) { - continue; - } - int cross = crossProductLength(cur, points[i], next); - if (cross == 0) { - result.add(points[i]); - } - } - - cur = next; - curIndex = nextIndex; - - } while (curIndex != firstIndex); - - return new ArrayList<>(result); - } - - private int crossProductLength(Point A, Point B, Point C) { - // Get the vectors' coordinates. - int BAx = A.x - B.x; - int BAy = A.y - B.y; - int BCx = C.x - B.x; - int BCy = C.y - B.y; - - // Calculate the Z coordinate of the cross product. - return (BAx * BCy - BAy * BCx); - } - - private int distance(Point p1, Point p2) { - return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_588.java b/src/main/java/com/fishercoder/solutions/_588.java deleted file mode 100644 index 518fa2d689..0000000000 --- a/src/main/java/com/fishercoder/solutions/_588.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 588. Design In-Memory File System - * - * Design an in-memory file system to simulate the following functions: - - ls: Given a path in string format. If it is a file path, return a list that only contains this file's name. - If it is a directory path, return the list of file and directory names in this directory. - Your output (file and directory names together) should in lexicographic order. - - mkdir: Given a directory path that does not exist, - you should make a new directory according to the path. - If the middle directories in the path don't exist either, - you should create them as well. This function has void return type. - - addContentToFile: Given a file path and file content in string format. - If the file doesn't exist, you need to create that file containing given content. - If the file already exists, you need to append given content to original content. - This function has void return type. - - readContentFromFile: Given a file path, return its content in string format. - - Example: - Input: - ["FileSystem","ls","mkdir","addContentToFile","ls","readContentFromFile"] - [[],["/"],["/a/b/c"],["/a/b/c/d","hello"],["/"],["/a/b/c/d"]] - Output: - [null,[],null,null,["a"],"hello"] - - Note: - - You can assume all file or directory paths are absolute paths which begin with / and do not end with / except that the path is just "/". - You can assume that all operations will be passed valid parameters and users will not attempt to retrieve file content or list a directory or file that does not exist. - You can assume that all directory names and file names only contain lower-case letters, and same names won't exist in the same directory. - - */ -public class _588 { - public static class Solution1 { - /** - * Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_588.java - */ - public static class FileSystem { - - class TrieNode { - boolean isFile = false; - Map map; - String name; - StringBuilder stringBuilder; - - TrieNode(String name) { - this.name = name; - map = new HashMap<>(); - stringBuilder = new StringBuilder(); - } - } - - TrieNode root = new TrieNode(""); - - TrieNode dfs(String path) { - TrieNode node = root; - for (String eachPath : path.split("/")) { - if (eachPath.isEmpty()) { - continue; - } - if (!node.map.containsKey(eachPath)) { - node.map.put(eachPath, new TrieNode(eachPath)); - } - node = node.map.get(eachPath); - } - return node; - } - - public FileSystem() { - } - - public List ls(String path) { - TrieNode node = dfs(path); - if (node.isFile) { - return Arrays.asList(node.name); - } else { - List files = new ArrayList(node.map.keySet()); - Collections.sort(files); - return files; - } - } - - public void mkdir(String path) { - dfs(path); - } - - public void addContentToFile(String filePath, String content) { - TrieNode node = dfs(filePath); - node.isFile = true; - node.stringBuilder.append(content); - } - - public String readContentFromFile(String filePath) { - return dfs(filePath).stringBuilder.toString(); - } - } - } - -/** - * Your FileSystem object will be instantiated and called as such: - * FileSystem obj = new FileSystem(); - * List param_1 = obj.ls(path); - * obj.mkdir(path); - * obj.addContentToFile(filePath,content); - * String param_4 = obj.readContentFromFile(filePath); - */ -} diff --git a/src/main/java/com/fishercoder/solutions/_589.java b/src/main/java/com/fishercoder/solutions/_589.java deleted file mode 100644 index 22e08282a4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_589.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Node; -import java.util.ArrayList; -import java.util.List; - -/** - * 589. N-ary Tree Preorder Traversal - * - * Given an n-ary tree, return the preorder traversal of its nodes' values. - * - * For example, given a 3-ary tree: - * - * 1 - * / | \ - * 3 2 4 - * / \ - * 5 6 - * - * Return its preorder traversal as: [1,3,5,6,2,4]. - * - * Note: - * - * Recursive solution is trivial, could you do it iteratively? - */ -public class _589 { - public static class Solution1 { - public List preorder(Node root) { - List result = new ArrayList<>(); - if (root == null) { - return result; - } - dfs(root, result); - return result; - } - - private void dfs(Node root, List result) { - if (root == null) { - return; - } - result.add(root.val); - if (root.children.size() > 0) { - for (Node child : root.children) { - dfs(child, result); - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_59.java b/src/main/java/com/fishercoder/solutions/_59.java deleted file mode 100644 index 650ad01a71..0000000000 --- a/src/main/java/com/fishercoder/solutions/_59.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -public class _59 { - - public static class Solution1 { - /** - * credit: https://leetcode.com/problems/spiral-matrix-ii/discuss/22289/My-Super-Simple-Solution.-Can-be-used-for-both-Spiral-Matrix-I-and-II/21907 - */ - public int[][] generateMatrix(int n) { - int[][] matrix = new int[n][n]; - if (n == 0) { - return matrix; - } - int value = 1; - int top = 0; - int bottom = n - 1; - int left = 0; - int right = n - 1; - while (left <= right && top <= bottom) { - for (int j = left; j <= right; j++) { - matrix[top][j] = value++; - } - top++; - for (int i = top; i <= bottom; i++) { - matrix[i][right] = value++; - } - right--; - for (int j = right; j >= left; j--) { - matrix[bottom][j] = value++; - } - bottom--; - for (int i = bottom; i >= top; i--) { - matrix[i][left] = value++; - } - left++; - } - return matrix; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_590.java b/src/main/java/com/fishercoder/solutions/_590.java deleted file mode 100644 index d458db0ac1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_590.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Node; -import java.util.ArrayList; -import java.util.List; - -/** - * 590. N-ary Tree Postorder Traversal - * - * Given an n-ary tree, return the postorder traversal of its nodes' values. - * - * For example, given a 3-ary tree: - * - * 1 - * / | \ - * 3 2 4 - * / \ - * 5 6 - * - * Return its postorder traversal as: [5,6,3,2,4,1]. - * - * Note: - * - * Recursive solution is trivial, could you do it iteratively? - */ -public class _590 { - public static class Solution1 { - public List postorder(Node root) { - List result = new ArrayList<>(); - if (root == null) { - return result; - } - dfs(root, result); - result.add(root.val); - return result; - } - - private void dfs(Node root, List result) { - if (root == null) { - return; - } - if (root.children.size() > 0) { - for (Node child : root.children) { - dfs(child, result); - result.add(child.val); - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_591.java b/src/main/java/com/fishercoder/solutions/_591.java deleted file mode 100644 index 627e531fca..0000000000 --- a/src/main/java/com/fishercoder/solutions/_591.java +++ /dev/null @@ -1,158 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Deque; - -/** - * 591. Tag Validator - * - * Given a string representing a code snippet, - * you need to implement a tag validator to parse the code and return whether it is valid. - * A code snippet is valid if all the following rules hold: - - 1. The code must be wrapped in a valid closed tag. Otherwise, the code is invalid. - - 2. A closed tag (not necessarily valid) has exactly the following format : TAG_CONTENT. - Among them, is the start tag, and is the end tag. - The TAG_NAME in start and end tags should be the same. - A closed tag is valid if and only if the TAG_NAME and TAG_CONTENT are valid. - - 3. A valid TAG_NAME only contain upper-case letters, and has length in range [1,9]. Otherwise, the TAG_NAME is invalid. - - 4. A valid TAG_CONTENT may contain other valid closed tags, cdata and any characters (see note1) EXCEPT unmatched <, - unmatched start and end tag, and unmatched or closed tags with invalid TAG_NAME. Otherwise, the TAG_CONTENT is invalid. - - 5. A start tag is unmatched if no end tag exists with the same TAG_NAME, and vice versa. - However, you also need to consider the issue of unbalanced when tags are nested. - - -6. A < is unmatched if you cannot find a subsequent >. And when you find a < or should be parsed as TAG_NAME (not necessarily valid). - -7. The cdata has the following format : . - The range of CDATA_CONTENT is defined as the characters between . - - 8. CDATA_CONTENT may contain any characters. - The function of cdata is to forbid the validator to parse CDATA_CONTENT, - so even it has some characters that can be parsed as tag (no matter valid or invalid), you should treat it as regular characters. - - Valid Code Examples: - Input: "

This is the first line ]]>
" - - Output: True - - Explanation: - - The code is wrapped in a closed tag :
and
. - - The TAG_NAME is valid, the TAG_CONTENT consists of some characters and cdata. - - Although CDATA_CONTENT has unmatched start tag with invalid TAG_NAME, it should be considered as plain text, not parsed as tag. - - So TAG_CONTENT is valid, and then the code is valid. Thus return true. - - - Input: "
>> ![cdata[]] ]>]]>]]>>]
" - - Output: True - - Explanation: - - We first separate the code into : start_tag|tag_content|end_tag. - - start_tag -> "
" - - end_tag -> "
" - - tag_content could also be separated into : text1|cdata|text2. - - text1 -> ">> ![cdata[]] " - - cdata -> "]>]]>", where the CDATA_CONTENT is "
]>" - - text2 -> "]]>>]" - - - The reason why start_tag is NOT "
>>" is because of the rule 6. - The reason why cdata is NOT "]>]]>]]>" is because of the rule 7. - Invalid Code Examples: - Input: " " - Output: False - Explanation: Unbalanced. If "" is closed, then "" must be unmatched, and vice versa. - - Input: "
div tag is not closed
" - Output: False - - Input: "
unmatched <
" - Output: False - - Input: "
closed tags with invalid tag name 123
" - Output: False - - Input: "
unmatched tags with invalid tag name and
" - Output: False - - Input: "
unmatched start tag and unmatched end tag
" - Output: False - - Note: - For simplicity, you could assume the input code (including the any characters mentioned above) - only contain letters, digits, '<','>','/','!','[',']' and ' '. - */ -public class _591 { - - public static class Solution1 { - - /** - * Credit: https://discuss.leetcode.com/topic/91300/java-solution-use-startswith-and-indexof - */ - public boolean isValid(String code) { - Deque stack = new ArrayDeque<>(); - for (int i = 0; i < code.length(); ) { - if (i > 0 && stack.isEmpty()) { - return false; - } - if (code.startsWith("", j); - if (i < 0) { - return false; - } - i += 3;//"]]>" length is 3 - } else if (code.startsWith("", j); - if (i < 0 || i == j || i - j > 9) { - return false; - } - for (int k = j; k < i; k++) { - if (!Character.isUpperCase(code.charAt(k))) { - return false; - } - } - String s = code.substring(j, i++); - if (stack.isEmpty() || !stack.pop().equals(s)) { - return false; - } - } else if (code.startsWith("<", i)) { - int j = i + 1; - i = code.indexOf(">", j); - if (i < 0 || i == j || i - j > 9) { - return false; - } - for (int k = j; k < i; k++) { - if (!Character.isUpperCase(code.charAt(k))) { - return false; - } - } - String s = code.substring(j, i++); - stack.push(s); - } else { - i++; - } - } - return stack.isEmpty(); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_592.java b/src/main/java/com/fishercoder/solutions/_592.java deleted file mode 100644 index 21d1d94755..0000000000 --- a/src/main/java/com/fishercoder/solutions/_592.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 592. Fraction Addition and Subtraction - * - * Given a string representing an expression of fraction addition and subtraction, - * you need to return the calculation result in string format. - * The final result should be irreducible fraction. - * If your final result is an integer, - * say 2, you need to change it to the format of fraction that has denominator 1. - * So in this case, 2 should be converted to 2/1. - - Example 1: - Input:"-1/2+1/2" - Output: "0/1" - - Example 2: - Input:"-1/2+1/2+1/3" - Output: "1/3" - - Example 3: - Input:"1/3-1/2" - Output: "-1/6" - - Example 4: - Input:"5/3+1/3" - Output: "2/1" - - Note: - The input string only contains '0' to '9', '/', '+' and '-'. So does the output. - Each fraction (input and output) has format ±numerator/denominator. - If the first input fraction or the output is positive, then '+' will be omitted. - The input only contains valid irreducible fractions, - where the numerator and denominator of each fraction will always be in the range [1,10]. - If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above. - The number of given fractions will be in the range [1,10]. - The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int. - */ -public class _592 { - - public static class Solution1 { - - /** - * Credit: https://discuss.leetcode.com/topic/89993/java-solution-fraction-addition-and-gcd - */ - public String fractionAddition(String expression) { - List nums = new ArrayList<>(); - int i = 0; - int j = 0; - while (j <= expression.length()) { - if (j == expression.length() || j != i && (expression.charAt(j) == '-' || expression.charAt(j) == '+')) { - if (expression.charAt(i) == '+') { - nums.add(expression.substring(i + 1, j)); - } else { - nums.add(expression.substring(i, j)); - } - i = j; - } - j++; - } - - String result = "0/1"; - for (String frac : nums) { - result = add(result, frac); - } - return result; - } - - private String add(String result, String frac) { - String[] frac1 = frac.split("/"); - String[] frac2 = result.split("/"); - int n1 = Integer.parseInt(frac1[0]); - int d1 = Integer.parseInt(frac1[1]); - int n2 = Integer.parseInt(frac2[0]); - int d2 = Integer.parseInt(frac2[1]); - int numerator = n1 * d2 + n2 * d1; - int denominator = d1 * d2; - if (numerator == 0) { - return "0/1"; - } - - boolean negative = numerator * denominator < 0; - numerator = Math.abs(numerator); - denominator = Math.abs(denominator); - int gcd = getGCD(numerator, denominator); - - return (negative ? "-" : "") + (numerator / gcd) + "/" + (denominator / gcd); - } - - private int getGCD(int a, int b) { - if (a == 0 || b == 0) { - return a + b; - } - return getGCD(b, a % b); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_594.java b/src/main/java/com/fishercoder/solutions/_594.java deleted file mode 100644 index 371493c61d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_594.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 594. Longest Harmonious Subsequence - * - * We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1. - - Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences. - - Example 1: - Input: [1,3,2,2,5,2,3,7] - Output: 5 - - Explanation: The longest harmonious subsequence is [3,2,2,2,3]. - Note: The length of the input array will not exceed 20,000. - - */ -public class _594 { - public static class Solution1 { - public int findLHS(int[] nums) { - Map map = new HashMap<>(); - for (int i : nums) { - map.put(i, map.getOrDefault(i, 0) + 1); - } - int max = 0; - for (int i = 0; i < nums.length; i++) { - if (map.containsKey(nums[i] + 1)) { - max = Math.max(max, map.get(nums[i]) + map.get(nums[i] + 1)); - } - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_598.java b/src/main/java/com/fishercoder/solutions/_598.java deleted file mode 100644 index 24b99a9975..0000000000 --- a/src/main/java/com/fishercoder/solutions/_598.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 598. Range Addition II - - Given an m * n matrix m initialized with all 0's and several update operations. - Operations are represented by a 2D array, - and each operation is represented by an array with two positive integers a and b, - which means m[i][j] should be added by one for all 0 <= i < a and 0 <= j < b. - - You need to count and return the number of maximum integers in the matrix after performing all the operations. - - Example 1: - Input: - m = 3, n = 3 - operations = [[2,2],[3,3]] - Output: 4 - - Explanation: - Initially, m = - [[0, 0, 0], - [0, 0, 0], - [0, 0, 0]] - - After performing [2,2], m = - [[1, 1, 0], - [1, 1, 0], - [0, 0, 0]] - - After performing [3,3], m = - [[2, 2, 1], - [2, 2, 1], - [1, 1, 1]] - - So the maximum integer in m is 2, and there are four of it in m. So return 4. - Note: - The range of m and n is [1,40000]. - The range of a is [1,m], and the range of b is [1,n]. - The range of operations size won't exceed 10,000. - */ - -public class _598 { - - public static class Solution1 { - public int maxCount(int m, int n, int[][] ops) { - int x = m; - int y = n; - for (int[] op : ops) { - x = Math.min(x, op[0]); - y = Math.min(y, op[1]); - } - return x * y; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_599.java b/src/main/java/com/fishercoder/solutions/_599.java deleted file mode 100644 index 1419a0ec34..0000000000 --- a/src/main/java/com/fishercoder/solutions/_599.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 599. Minimum Index Sum of Two Lists - * - Suppose Andy and Doris want to choose a restaurant for dinner, - and they both have a list of favorite restaurants represented by strings. - You need to help them find out their common interest with the least list index sum. - If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer. - - Example 1: - Input: - ["Shogun", "Tapioca Express", "Burger King", "KFC"] - ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"] - Output: ["Shogun"] - Explanation: The only restaurant they both like is "Shogun". - - Example 2: - Input: - ["Shogun", "Tapioca Express", "Burger King", "KFC"] - ["KFC", "Shogun", "Burger King"] - Output: ["Shogun"] - Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1). - - Note: - The length of both lists will be in the range of [1, 1000]. - The length of strings in both lists will be in the range of [1, 30]. - The index is starting from 0 to the list length minus 1. - No duplicates in both lists. - */ -public class _599 { - public static class Solution1 { - public String[] findRestaurant(String[] list1, String[] list2) { - if (list1 == null || list2 == null) { - return new String[0]; - } - Map map1 = putIntoMap(list1); - Map map2 = putIntoMap(list2); - int leastIndexSum = Integer.MAX_VALUE; - List resultList = new ArrayList<>(); - for (String key1 : map1.keySet()) { - if (map2.containsKey(key1)) { - int indexSum = map1.get(key1) + map2.get(key1); - if (indexSum < leastIndexSum) { - resultList.clear(); - resultList.add(key1); - leastIndexSum = indexSum; - } else if (indexSum == leastIndexSum) { - resultList.add(key1); - } - } - } - String[] result = new String[resultList.size()]; - for (int i = 0; i < resultList.size(); i++) { - result[i] = resultList.get(i); - } - return result; - } - - private Map putIntoMap(String[] list) { - Map map = new HashMap<>(); - for (int i = 0; i < list.length; i++) { - map.put(list[i], i); - } - return map; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_6.java b/src/main/java/com/fishercoder/solutions/_6.java deleted file mode 100644 index 9f22f83c81..0000000000 --- a/src/main/java/com/fishercoder/solutions/_6.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder.solutions; - -public class _6 { - public static class Solution1 { - public String convert(String s, int numRows) { - StringBuilder[] sb = new StringBuilder[numRows]; - char[] c = s.toCharArray(); - int len = s.length(); - for (int i = 0; i < numRows; i++) { - sb[i] = new StringBuilder();//this is an important step to initialize it - } - int i = 0; - while (i < len) { - for (int index = 0; index < numRows && i < len; index++) { - sb[index].append(c[i++]);// vertically down - } - - for (int index = numRows - 2; index >= 1 && i < len; index--) { - /**Why it should start from numRows - 2? Think of the example when numRows = 3 - the starting point of obliquely going up is 1, which is numRows-2.*/ - sb[index].append(c[i++]);// obliquely up - } - } - - for (i = 1; i < numRows; i++) { - sb[0].append(sb[i]); - } - return sb[0].toString(); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_600.java b/src/main/java/com/fishercoder/solutions/_600.java deleted file mode 100644 index 3583b7d5dc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_600.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 600. Non-negative Integers without Consecutive Ones - * - * Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT contain consecutive ones. - - Example 1: - Input: 5 - Output: 5 - Explanation: - Here are the non-negative integers <= 5 with their corresponding binary representations: - 0 : 0 - 1 : 1 - 2 : 10 - 3 : 11 - 4 : 100 - 5 : 101 - Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule. - - Note: 1 <= n <= 109 - */ -public class _600 { - - public static class Solution1 { - /** - * Credit: https://leetcode.com/articles/non-negative-integers-without-consecutive-ones/#approach-3-using-bit-manipulation-accepted - */ - public int findIntegers(int num) { - int[] f = new int[32]; - f[0] = 1; - f[1] = 2; - for (int i = 2; i < f.length; i++) { - f[i] = f[i - 1] + f[i - 2]; - } - int i = 30; - int sum = 0; - int prevBit = 0; - while (i >= 0) { - if ((num & (1 << i)) != 0) { - sum += f[i]; - if (prevBit == 1) { - sum--; - break; - } - prevBit = 1; - } else { - prevBit = 0; - } - i--; - } - return sum + 1; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_604.java b/src/main/java/com/fishercoder/solutions/_604.java deleted file mode 100644 index 5fe582e75d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_604.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Deque; - -/** - * 604. Design Compressed String Iterator - * - * Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. - The given compressed string will be in the form of each letter followed by a positive integer representing - the number of this letter existing in the original uncompressed string. - - next() - if the original string still has uncompressed characters, return the next letter; Otherwise return a white space. - hasNext() - Judge whether there is any letter needs to be uncompressed. - - Note: - Please remember to RESET your class variables declared in StringIterator, - as static/class variables are persisted across multiple test cases. Please see here for more details. - - Example: - - StringIterator iterator = new StringIterator("L1e2t1C1o1d1e1"); - - iterator.next(); // return 'L' - iterator.next(); // return 'e' - iterator.next(); // return 'e' - iterator.next(); // return 't' - iterator.next(); // return 'C' - iterator.next(); // return 'o' - iterator.next(); // return 'd' - iterator.hasNext(); // return true - iterator.next(); // return 'e' - iterator.hasNext(); // return false - iterator.next(); // return ' ' - - */ -public class _604 { - public static class Solution1 { - public static class StringIterator { - - Deque deque; - - public StringIterator(String compressedString) { - deque = new ArrayDeque<>(); - int len = compressedString.length(); - int i = 0; - while (i < len) { - int j = i + 1; - while (j < len && Character.isDigit(compressedString.charAt(j))) { - j++; - } - deque.addLast(new int[]{compressedString.charAt(i) - 'A', Integer.parseInt(compressedString.substring(i + 1, j))}); - i = j; - } - } - - public char next() { - if (deque.isEmpty()) { - return ' '; - } - int[] top = deque.peek(); - top[1]--; - if (top[1] == 0) { - deque.pollFirst(); - } - return (char) ('A' + top[0]); - } - - public boolean hasNext() { - return !deque.isEmpty(); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_605.java b/src/main/java/com/fishercoder/solutions/_605.java deleted file mode 100644 index e4d110b0ac..0000000000 --- a/src/main/java/com/fishercoder/solutions/_605.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 605. Can Place Flowers - * - * Suppose you have a long flowerbed in which some of the plots are planted and some are not. - * However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. - - Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), - and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule. - - Example 1: - Input: flowerbed = [1,0,0,0,1], n = 1 - Output: True - - Example 2: - Input: flowerbed = [1,0,0,0,1], n = 2 - Output: False - - Note: - The input array won't violate no-adjacent-flowers rule. - The input array size is in the range of [1, 20000]. - n is a non-negative integer which won't exceed the input array size. - - */ -public class _605 { - - public static class Solution1 { - public boolean canPlaceFlowers(int[] flowerbed, int n) { - int count = 0; - int i = 0; - while (i < flowerbed.length) { - if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) { - count++; - flowerbed[i] = 1; - } - if (count >= n) { - return true; - } - i++; - } - if (count >= n) { - return true; - } - return false; - } - } - - public static class Solution2 { - public boolean canPlaceFlowers(int[] flowerbed, int n) { - int len = flowerbed.length; - if (len == 1) { - if ((flowerbed[0] == 0 && n <= 1) || n == 0) { - return true; - } - return false; - } - if (flowerbed[0] == 0 && flowerbed[1] == 0) { - flowerbed[0] = 1; - n--; - } - for (int i = 1; i < len - 1; i++) { - if (flowerbed[i] == 0 && flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) { - n--; - flowerbed[i] = 1;//modify the input, discuss this with interviwer, if not allowed, then have a copy of this input and modify copy - } - if (n <= 0) { - return true; - } - } - if (len >= 2 && flowerbed[len - 2] == 0 && flowerbed[len - 1] == 0) { - n--; - } - if (n <= 0) { - return true; - } - return false; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_606.java b/src/main/java/com/fishercoder/solutions/_606.java deleted file mode 100644 index e69b81fc86..0000000000 --- a/src/main/java/com/fishercoder/solutions/_606.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 606. Construct String from Binary Tree - * - * You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. - The null node needs to be represented by empty parenthesis pair "()". - And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string - and the original binary tree. - - Example 1: - Input: Binary tree: [1,2,3,4] - 1 - / \ - 2 3 - / -4 - - Output: "1(2(4))(3)" - - Explanation: Originallay it needs to be "1(2(4)())(3()())", - but you need to omit all the unnecessary empty parenthesis pairs. - And it will be "1(2(4))(3)". - - Example 2: - Input: Binary tree: [1,2,3,null,4] - 1 - / \ - 2 3 - \ - 4 - - Output: "1(2()(4))(3)" - - Explanation: Almost the same as the first example, - except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output. - */ -public class _606 { - - public static class Solution1 { - public String tree2str(TreeNode t) { - if (t == null) { - return ""; - } - String result = "" + t.val; - String left = tree2str(t.left); - String right = tree2str(t.right); - if (left.equals("") && right.equals("")) { - return result; - } - if (left.equals("")) { - return result + "()(" + right + ")"; - } - if (right.equals("")) { - return result + "(" + left + ")"; - } - return result + "(" + left + ")(" + right + ")"; - } - } - - public static class Solution2 { - public String tree2str(TreeNode t) { - if (t == null) { - return ""; - } - StringBuilder sb = new StringBuilder(); - preorder(t, sb); - return sb.toString(); - } - - private void preorder(TreeNode root, StringBuilder sb) { - if (root == null) { - return; - } - sb.append(root.val); - if (root.left != null) { - sb.append("("); - preorder(root.left, sb); - sb.append(")"); - } - if (root.right != null) { - if (root.left == null) { - sb.append("()"); - } - sb.append("("); - preorder(root.right, sb); - sb.append(")"); - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_609.java b/src/main/java/com/fishercoder/solutions/_609.java deleted file mode 100644 index 19a9ec768f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_609.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 609. Find Duplicate File in System - * - * Given a list of directory info including directory path, and all the files with contents in this directory, - * you need to find out all the groups of duplicate files in the file system in terms of their paths. - * A group of duplicate files consists of at least two files that have exactly the same content. - - A single directory info string in the input list has the following format: - - "root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)" - - It means there are n files (f1.txt, f2.txt ... fn.txt with content f1_content, f2_content ... fn_content, respectively) - in directory root/d1/d2/.../dm. Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory. - - The output is a list of group of duplicate file paths. - For each group, it contains all the file paths of the files that have the same content. - A file path is a string that has the following format: - - "directory_path/file_name.txt" - - Example 1: - - Input: - ["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"] - - Output: - [["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]] - - Note: - No order is required for the final output. - You may assume the directory name, - file name and file content only has letters and digits, and the length of file content is in the range of [1,50]. - - The number of files given is in the range of [1,20000]. - You may assume no files or directories share the same name in a same directory. - - You may assume each given directory info represents a unique directory. Directory path and file infos are separated by a single blank space. - - Follow up beyond contest: - Imagine you are given a real file system, how will you search files? DFS or BFS ? - If the file content is very large (GB level), how will you modify your solution? - If you can only read the file by 1kb each time, how will you modify your solution? - What is the time complexity of your modified solution? What is the most time consuming part and memory consuming part of it? How to optimize? - How to make sure the duplicated files you find are not false positive? - */ -public class _609 { - - public static class Solution1 { - public List> findDuplicate(String[] paths) { - Map> map = new HashMap<>();//key is the file content, value is the list of directories that has this directory/file - for (String path : paths) { - String[] dirAndFiles = path.split(" "); - for (int i = 1; i < dirAndFiles.length; i++) { - String content = dirAndFiles[i].substring(dirAndFiles[i].indexOf("(") + 1, dirAndFiles[i].indexOf(")")); - if (!map.containsKey(content)) { - map.put(content, new ArrayList<>()); - } - List dirs = map.get(content); - dirs.add(dirAndFiles[0] + "/" + dirAndFiles[i].substring(0, dirAndFiles[i].indexOf("("))); - map.put(content, dirs); - } - } - - List> result = new ArrayList<>(); - for (String content : map.keySet()) { - if (map.get(content).size() > 1) { - List dupFile = new ArrayList<>(); - for (String dir : map.get(content)) { - dupFile.add(dir); - } - result.add(dupFile); - } - } - return result; - } - } - - /**Answers to follow-up questions: - * 1. Imagine you are given a real file system, how will you search files? DFS or BFS ? - * A: Both BFS and DFS could do the work, but BFS will use extra memory, however, BFS takes advantage of memory locality, so BFS could be faster. - * - * 2. If the file content is very large (GB level), how will you modify your solution? - * A: We'll fist map all files according to their sizes, since files with different sizes are guaranteed to be different, then - * we can hash a small part of the files using MD5, SHA256, etc. Only when their md5 or sha256 is the same, we'll compare the contents byte by byte. - * - * 3. If you can only read the file by 1kb each time, how will you modify your solution? - * A: This is not going to change the solution, we can hash this 1kb chunk, and then also only compare byte by byte when it's necessary. - * - * 4. What is the time complexity of your modified solution? What is the most time consuming part and memory consuming part of it? How to optimize? - * A: O(n^2*k), in the worst time, we'll have to compare the file with every other file, k is the length of the file. - * Comparing the file (by size, by hash and eventually byte by byte) is the most time consuming part. - * Generating hash for every file will be the most memory consuming part. - * - * 5. How to make sure the duplicated files you find are not false positive? - * A: Size comparision, hash detection, byte by byte check, etc. will pretty sure to rule out false positive. - * */ - -} diff --git a/src/main/java/com/fishercoder/solutions/_611.java b/src/main/java/com/fishercoder/solutions/_611.java deleted file mode 100644 index 4922e1a778..0000000000 --- a/src/main/java/com/fishercoder/solutions/_611.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 611. Valid Triangle Number - * Given an array consists of non-negative integers, - * your task is to count the number of triplets chosen - * from the array that can make triangles if we take them as side lengths of a triangle. - - Example 1: - Input: [2,2,3,4] - Output: 3 - - Explanation: - Valid combinations are: - 2,3,4 (using the first 2) - 2,3,4 (using the second 2) - 2,2,3 - - Note: - The length of the given array won't exceed 1000. - The integers in the given array are in the range of [0, 1000]. - */ -public class _611 { - public static class Solution1 { - /** - * Rule: among three sides, we need to find whether the longest of the three is smaller than the sum of the two shorter one. - * If so, then these three could form a valid triangle. - */ - - public int triangleNumber(int[] nums) { - if (nums == null || nums.length == 0) { - return 0; - } - Arrays.sort(nums); - int triplets = 0; - for (int i = 2; i < nums.length; i++) { - int left = 0; - int right = i - 1; - while (left < right) { - if (nums[i] < nums[left] + nums[right]) { - triplets += (right - left); - right--; - } else { - left++; - } - } - } - return triplets; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_616.java b/src/main/java/com/fishercoder/solutions/_616.java deleted file mode 100644 index 95cf8b4975..0000000000 --- a/src/main/java/com/fishercoder/solutions/_616.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 616. Add Bold Tag in String - * - * Given a string s and a list of strings dict, - * you need to add a closed pair of bold tag and - * to wrap the substrings in s that exist in dict. - * If two such substrings overlap, you need to wrap them together by only one pair of closed bold tag. - * Also, if two substrings wrapped by bold tags are consecutive, you need to combine them. - - Example 1: - Input: - s = "abcxyz123" - dict = ["abc","123"] - Output: - "abcxyz123" - - Example 2: - Input: - s = "aaabbcc" - dict = ["aaa","aab","bc"] - Output: - "aaabbcc" - - Note: - The given dict won't contain duplicates, and its length won't exceed 100. - All the strings in input have length in range [1, 1000]. - */ -public class _616 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/92112/java-solution-boolean-array - */ - public String addBoldTag(String s, String[] dict) { - boolean[] shouldBold = new boolean[s.length()]; - for (int i = 0, end = 0; i < s.length(); i++) { - for (String word : dict) { - if (s.startsWith(word, i)) { - end = Math.max(end, i + word.length()); - } - } - shouldBold[i] = end > i; - } - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < s.length(); i++) { - if (!shouldBold[i]) { - stringBuilder.append(s.charAt(i)); - continue; - } - int j = i; - while (j < s.length() && shouldBold[j]) { - j++; - } - stringBuilder.append("" + s.substring(i, j) + ""); - i = j - 1; - } - return stringBuilder.toString(); - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_617.java b/src/main/java/com/fishercoder/solutions/_617.java deleted file mode 100644 index 1f84927785..0000000000 --- a/src/main/java/com/fishercoder/solutions/_617.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 617. Merge Two Binary Trees - * - * Given two binary trees and imagine that when you put one of them to cover the other, - * some nodes of the two trees are overlapped while the others are not. - You need to merge them into a new binary tree. - The merge rule is that if two nodes overlap, - then sum node values up as the new value of the merged node. - Otherwise, the NOT null node will be used as the node of new tree. - - Example 1: - Input: - Tree 1 Tree 2 - 1 2 - / \ / \ - 3 2 1 3 - / \ \ -5 4 7 - Output: - Merged tree: - 3 - / \ - 4 5 - / \ \ - 5 4 7 - Note: The merging process must start from the root nodes of both trees. - */ -public class _617 { - - public static class Solution1 { - public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { - if (t1 == null) { - return t2; - } - if (t2 == null) { - return t1; - } - TreeNode mergedNode = new TreeNode(t1.val + t2.val); - mergedNode.left = mergeTrees(t1.left, t2.left); - mergedNode.right = mergeTrees(t1.right, t2.right); - return mergedNode; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_621.java b/src/main/java/com/fishercoder/solutions/_621.java deleted file mode 100644 index 5675d8d382..0000000000 --- a/src/main/java/com/fishercoder/solutions/_621.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.PriorityQueue; - -/** - * 621. Task Scheduler - * - * Given a char array representing tasks CPU need to do. - * It contains capital letters A to Z where different letters represent different tasks. - * Tasks could be done without original order. - * Each task could be done in one interval. For each interval, CPU could finish one task or just be idle. - * However, there is a non-negative cooling interval n that means between two same tasks, - * there must be at least n intervals that CPU are doing different tasks or just be idle. - * You need to return the least number of intervals the CPU will take to finish all the given tasks. - - Example 1: - Input: tasks = ['A','A','A','B','B','B'], n = 2 - Output: 8 - Explanation: A -> B -> idle -> A -> B -> idle -> A -> B. - - Note: - The number of tasks is in the range [1, 10000]. - The integer n is in the range [0, 100]. - */ -public class _621 { - - public static class Solution1 { - - public int leastInterval(char[] tasks, int n) { - Map map = new HashMap<>(); - for (char c : tasks) { - map.put(c, map.getOrDefault(c, 0) + 1); - } - PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b.total - a.total); - for (Map.Entry entry : map.entrySet()) { - maxHeap.offer(new Task(entry.getValue(), entry.getKey())); - } - int times = 0; - while (!maxHeap.isEmpty()) { - int i = 0; - List temp = new ArrayList<>(); - while (i <= n) { - if (!maxHeap.isEmpty()) { - if (maxHeap.peek().total > 1) { - Task curr = maxHeap.poll(); - temp.add(new Task(curr.total - 1, curr.character)); - } else { - maxHeap.poll(); - } - } - times++; - if (maxHeap.isEmpty() && temp.size() == 0) { - break; - } - i++; - } - for (Task task : temp) { - maxHeap.offer(task); - } - } - return times; - } - - class Task { - int total; - char character; - - public Task(int total, char character) { - this.total = total; - this.character = character; - } - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_623.java b/src/main/java/com/fishercoder/solutions/_623.java deleted file mode 100644 index 41e5a5cc7b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_623.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 623. Add One Row to Tree - * - * Given the root of a binary tree, - * then value v and depth d, - * you need to add a row of nodes with value v at the given depth d. - * The root node is at depth 1. - - The adding rule is: given a positive integer depth d, - for each NOT null tree nodes N in depth d-1, - create two tree nodes with value v as N's left subtree root and right subtree root. - And N's original left subtree should be the left subtree of the new left subtree root, - its original right subtree should be the right subtree of the new right subtree root. - If depth d is 1 that means there is no depth d-1 at all, - then create a tree node with value v as the new root of the whole - original tree, and the original tree is the new root's left subtree. - - Example 1: - Input: - A binary tree as following: - 4 - / \ - 2 6 - / \ / - 3 1 5 - - v = 1 - - d = 2 - - Output: - 4 - / \ - 1 1 - / \ - 2 6 - / \ / - 3 1 5 - - - Example 2: - Input: - A binary tree as following: - 4 - / - 2 - / \ - 3 1 - - v = 1 - - d = 3 - - Output: - 4 - / - 2 - / \ - 1 1 - / \ - 3 1 - - Note: - The given d is in range [1, maximum depth of the given tree + 1]. - The given binary tree has at least one tree node. - */ -public class _623 { - - public static class Solution1 { - - public TreeNode addOneRow(TreeNode root, int v, int d) { - if (d == 1) { - TreeNode newRoot = new TreeNode(v); - newRoot.left = root; - return newRoot; - } else { - dfs(root, v, d); - return root; - } - } - - private void dfs(TreeNode root, int v, int d) { - if (root == null) { - return; - } - if (d == 2) { - TreeNode newLeft = new TreeNode(v); - TreeNode newRight = new TreeNode(v); - newLeft.left = root.left; - newRight.right = root.right; - root.left = newLeft; - root.right = newRight; - } else { - dfs(root.left, v, d - 1); - dfs(root.right, v, d - 1); - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_624.java b/src/main/java/com/fishercoder/solutions/_624.java deleted file mode 100644 index 141cd07e87..0000000000 --- a/src/main/java/com/fishercoder/solutions/_624.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * 624. Maximum Distance in Arrays - * - * Given m arrays, and each array is sorted in ascending order. - * Now you can pick up two integers from two different arrays (each array picks one) - * and calculate the distance. We define the distance between two - * integers a and b to be their absolute difference |a-b|. Your task is to find the maximum distance. - - Example 1: - Input: - [[1,2,3], - [4,5], - [1,2,3]] - - Output: 4 - - Explanation: - - One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array. - - Note: - Each given array will have at least 1 number. There will be at least two non-empty arrays. - The total number of the integers in all the m arrays will be in the range of [2, 10000]. - The integers in the m arrays will be in the range of [-10000, 10000]. - */ -public class _624 { - - public static class Solution1 { - public int maxDistance(int[][] arrays) { - List max = new ArrayList<>(); - for (int[] array : arrays) { - max.add(array[array.length - 1]); - } - Collections.sort(max); - int ans = Integer.MIN_VALUE; - for (int[] array : arrays) { - int big = array[array.length - 1] == max.get(max.size() - 1) ? max.get(max.size() - 2) : max.get(max.size() - 1); - ans = Math.max(ans, big - array[0]); - } - return ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_625.java b/src/main/java/com/fishercoder/solutions/_625.java deleted file mode 100644 index 00997aa739..0000000000 --- a/src/main/java/com/fishercoder/solutions/_625.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 625. Minimum Factorization - * - * Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a. - - If there is no answer or the answer is not fit in 32-bit signed integer, then return 0. - - Example 1 - Input: - 48 - Output: - 68 - - Example 2 - Input: - 15 - Output: - 35 - */ -public class _625 { - - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/92854/java-solution-result-array - * and https://leetcode.com/articles/minimum-factorization/#approach-3-using-factorizationaccepted - */ - public int smallestFactorization(int a) { - //case 1: a < 10 - if (a < 10) { - return a; - } - - //case 2: start with 9 and try every possible digit - List resultArray = new ArrayList<>(); - for (int i = 9; i > 1; i--) { - //if current digit divides a, then store all occurences of current digit in res - while (a % i == 0) { - a = a / i; - resultArray.add(i); - } - } - - //if a could not be broken in form of digits, return 0 - if (a != 0) { - return 0; - } - - //get the result from the result array in reverse order - long result = 0; - for (int i = resultArray.size() - 1; i >= 0; i--) { - result = result * 10 + resultArray.get(i); - if (result > Integer.MAX_VALUE) { - return 0; - } - } - return (int) result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_628.java b/src/main/java/com/fishercoder/solutions/_628.java deleted file mode 100644 index 572be632db..0000000000 --- a/src/main/java/com/fishercoder/solutions/_628.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 628. Maximum Product of Three Numbers - * - * Given an integer array, find three numbers whose product is maximum and output the maximum product. - Example 1: - Input: [1,2,3] - Output: 6 - - Example 2: - Input: [1,2,3,4] - Output: 24 - - Note: - The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000]. - Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer. - */ -public class _628 { - - public static class Solution1 { - public int maximumProduct(int[] nums) { - Arrays.sort(nums); - int product = 1; - if (nums.length >= 3) { - for (int i = nums.length - 1; i >= nums.length - 3; i--) { - product *= nums[i]; - } - int anotherProduct = nums[0] * nums - [1] * nums[nums.length - 1]; - product = Math.max(product, anotherProduct); - } else { - for (int i = 0; i < nums.length; i++) { - product *= nums[i]; - } - } - return product; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_629.java b/src/main/java/com/fishercoder/solutions/_629.java deleted file mode 100644 index c12a217f0a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_629.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 629. K Inverse Pairs Array - * - * Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. - We define an inverse pair as following: For ith and jth element in the array, - if i < j and a[i] > a[j] then it's an inverse pair; Otherwise, it's not. - Since the answer may very large, the answer should be modulo 109 + 7. - - Example 1: - Input: n = 3, k = 0 - Output: 1 - Explanation: - Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pair. - - Example 2: - Input: n = 3, k = 1 - Output: 2 - Explanation: - The array [1,3,2] and [2,1,3] have exactly 1 inverse pair. - - Note: - The integer n is in the range [1, 1000] and k is in the range [0, 1000]. - */ -public class _629 { - - public static class Solution1 { - - /** - * reference: https://leetcode.com/articles/k-inverse-pairs-array/#approach-5-another-optimized-dynamic-programming-approachaccepted - * and - * https://discuss.leetcode.com/topic/93815/java-dp-o-nk-solution - */ - public int kInversePairs(int n, int k) { - int mod = 1000000007; - if (k > n * (n - 1) / 2 || k < 0) { - return 0; - } - if (k == 0 || k == n * (n - 1) / 2) { - return 1; - } - long[][] dp = new long[n + 1][k + 1]; - dp[2][0] = 1; - dp[2][1] = 1; - for (int i = 3; i <= n; i++) { - dp[i][0] = 1; - for (int j = 1; j <= Math.min(k, i * (i - 1) / 2); j++) { - dp[i][j] = dp[i][j - 1] + dp[i - 1][j]; - if (j >= i) { - dp[i][j] -= dp[i - 1][j - i]; - } - dp[i][j] = (dp[i][j] + mod) % mod; - } - } - return (int) dp[n][k]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_630.java b/src/main/java/com/fishercoder/solutions/_630.java deleted file mode 100644 index ec58a8eb36..0000000000 --- a/src/main/java/com/fishercoder/solutions/_630.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.PriorityQueue; - -/** - * 630. Course Schedule III - * - * There are n different online courses numbered from 1 to n. - * Each course has some duration(course length) t and closed on dth day. - * A course should be taken continuously for t days and must be finished before or on the dth day. You will start at the 1st day. - - Given n online courses represented by pairs (t,d), your task is to find the maximal number of courses that can be taken. - - Example: - Input: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]] - Output: 3 - - Explanation: - There're totally 4 courses, but you can take 3 courses at most: - First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day. - Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day. - Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day. - The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date. - - Note: - The integer 1 <= d, t, n <= 10,000. - You can't take two courses simultaneously. - */ -public class _630 { - public static class Solution1 { - /** - * Reference: https://discuss.leetcode.com/topic/93790/short-java-code-using-priorityqueue - * Sort by finish date!!! This is greedy! We should take those classes that finish early first. - */ - public int scheduleCourse(int[][] courses) { - Arrays.sort(courses, (a, b) -> a[1] - b[1]); - int day = 0; - PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); - for (int[] course : courses) { - day += course[0]; - maxHeap.offer(course[0]); - if (day > course[1]) { - day -= maxHeap.poll();//drop the previous courses that took the most time - } - } - return maxHeap.size(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_631.java b/src/main/java/com/fishercoder/solutions/_631.java deleted file mode 100644 index d75940e9a3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_631.java +++ /dev/null @@ -1,190 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Stack; - -/** - * 631. Design Excel Sum Formula - * - * Your task is to design the basic function of Excel and implement the function of sum formula. - * Specifically, you need to implement the following functions: - - Excel(int H, char W): - This is the constructor. - The inputs represents the height and width of the Excel form. - H is a positive integer, range from 1 to 26. - It represents the height. - W is a character range from 'A' to 'Z'. - It represents that the width is the number of characters from 'A' to W. - The Excel form content is represented by a height * width 2D integer array C, it should be initialized to zero. - You should assume that the first row of C starts from 1, and the first column of C starts from 'A'. - - void Set(int row, char column, int val): - Change the value at C(row, column) to be val. - - int Get(int row, char column): - Return the value at C(row, column). - - int Sum(int row, char column, List of Strings : numbers): - This function calculate and set the value at C(row, column), - where the value should be the sum of cells represented by numbers. - This function return the sum result at C(row, column). - This sum formula should exist until this cell is overlapped by another value or another sum formula. - - numbers is a list of strings that each string represent a cell or a range of cells. - If the string represent a single cell, - then it has the following format : ColRow. For example, "F7" represents the cell at (7, F). - If the string represent a range of cells, - then it has the following format : ColRow1:ColRow2. - The range will always be a rectangle, - and ColRow1 represent the position of the top-left cell, - and ColRow2 represents the position of the bottom-right cell. - - - Example 1: - Excel(3,"C"); - // construct a 3*3 2D array with all zero. - // A B C - // 1 0 0 0 - // 2 0 0 0 - // 3 0 0 0 - - Set(1, "A", 2); - // set C(1,"A") to be 2. - // A B C - // 1 2 0 0 - // 2 0 0 0 - // 3 0 0 0 - - Sum(3, "C", ["A1", "A1:B2"]); - // set C(3,"C") to be the sum of value at C(1,"A") - //and the values sum of the rectangle range whose top-left cell is C(1,"A") and bottom-right cell is C(2,"B"). Return 4. - // A B C - // 1 2 0 0 - // 2 0 0 0 - // 3 0 0 4 - - Set(2, "B", 2); - // set C(2,"B") to be 2. Note C(3, "C") should also be changed. - // A B C - // 1 2 0 0 - // 2 0 2 0 - // 3 0 0 6 - - Note: - You could assume that there won't be any circular sum reference. - For example, A1 = sum(B1) and B1 = sum(A1). - The test cases are using double-quotes to represent a character. - Please remember to RESET your class variables declared in class Excel, - as static/class variables are persisted across multiple test cases. Please see here for more details. - */ -public class _631 { - - public static class Solution1 { - /** - * Credit: https://leetcode.com/articles/design-excel-sum-formula/#approach-1-using-topological-sortaccepted - */ - public static class Excel { - - Formula[][] formulas; - - class Formula { - Formula(HashMap c, int v) { - val = v; - cells = c; - } - - HashMap cells; - int val; - } - - Stack stack = new Stack<>(); - - public Excel(int H, char W) { - formulas = new Formula[H][(W - 'A') + 1]; - } - - public int get(int r, char c) { - if (formulas[r - 1][c - 'A'] == null) { - return 0; - } - return formulas[r - 1][c - 'A'].val; - } - - public void set(int r, char c, int v) { - formulas[r - 1][c - 'A'] = new Formula(new HashMap(), v); - topologicalSort(r - 1, c - 'A'); - execute_stack(); - } - - public int sum(int r, char c, String[] strs) { - HashMap cells = convert(strs); - int summ = calculate_sum(r - 1, c - 'A', cells); - set(r, c, summ); - formulas[r - 1][c - 'A'] = new Formula(cells, summ); - return summ; - } - - public void topologicalSort(int r, int c) { - for (int i = 0; i < formulas.length; i++) { - for (int j = 0; j < formulas[0].length; j++) { - if (formulas[i][j] != null && formulas[i][j].cells.containsKey("" + (char) ('A' + c) + (r + 1))) { - topologicalSort(i, j); - } - } - } - stack.push(new int[]{r, c}); - } - - public void execute_stack() { - while (!stack.isEmpty()) { - int[] top = stack.pop(); - if (formulas[top[0]][top[1]].cells.size() > 0) { - calculate_sum(top[0], top[1], formulas[top[0]][top[1]].cells); - } - } - } - - public HashMap convert(String[] strs) { - HashMap res = new HashMap<>(); - for (String st : strs) { - if (st.indexOf(":") < 0) { - res.put(st, res.getOrDefault(st, 0) + 1); - } else { - String[] cells = st.split(":"); - int si = Integer.parseInt(cells[0].substring(1)); - int ei = Integer.parseInt(cells[1].substring(1)); - char sj = cells[0].charAt(0); - char ej = cells[1].charAt(0); - for (int i = si; i <= ei; i++) { - for (char j = sj; j <= ej; j++) { - res.put("" + j + i, res.getOrDefault("" + j + i, 0) + 1); - } - } - } - } - return res; - } - - public int calculate_sum(int r, int c, HashMap cells) { - int sum = 0; - for (String s : cells.keySet()) { - int x = Integer.parseInt(s.substring(1)) - 1; - int y = s.charAt(0) - 'A'; - sum += (formulas[x][y] != null ? formulas[x][y].val : 0) * cells.get(s); - } - formulas[r][c] = new Formula(cells, sum); - return sum; - } - } - } - - /** - * Your Excel object will be instantiated and called as such: - * Excel obj = new Excel(H, W); - * obj.set(r,c,v); - * int param_2 = obj.get(r,c); - * int param_3 = obj.sum(r,c,strs); - */ - -} diff --git a/src/main/java/com/fishercoder/solutions/_632.java b/src/main/java/com/fishercoder/solutions/_632.java deleted file mode 100644 index 00c0a3321c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_632.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.List; -import java.util.PriorityQueue; - -/** - * 632. Smallest Range - * - * You have k lists of sorted integers in ascending order. - * Find the smallest range that includes at least one number from each of the k lists. - - We define the range [a,b] is smaller than range [c,d] if b-a < d-c or a < c if b-a == d-c. - - Example 1: - Input:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] - Output: [20,24] - Explanation: - List 1: [4, 10, 15, 24,26], 24 is in range [20,24]. - List 2: [0, 9, 12, 20], 20 is in range [20,24]. - List 3: [5, 18, 22, 30], 22 is in range [20,24]. - - Note: - The given list may contain duplicates, so ascending order means >= here. - 1 <= k <= 3500 - -105 <= value of elements <= 105. - For Java users, please note that the input type has been changed to List>. - And after you reset the code template, you'll see this point. - - */ -public class _632 { - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/94445/java-code-using-priorityqueue-similar-to-merge-k-array/2 - */ - public int[] smallestRange(List> nums) { - PriorityQueue minHeap = new PriorityQueue<>(nums.size(), (a, b) -> a[0] - b[0]); - /**int[] array consists of three numbers: value; which list in nums; index of value in this list*/ - - int max = nums.get(0).get(0); - for (int i = 0; i < nums.size(); i++) { - minHeap.offer(new int[]{nums.get(i).get(0), i, 0}); - max = Math.max(max, nums.get(i).get(0)); - } - int minRange = Integer.MAX_VALUE; - int start = -1; - while (minHeap.size() == nums.size()) { - int[] curr = minHeap.poll(); - if (max - curr[0] < minRange) { - minRange = max - curr[0]; - start = curr[0]; - } - if (curr[2] + 1 < nums.get(curr[1]).size()) { - curr[0] = nums.get(curr[1]).get(curr[2] + 1); - curr[2]++; - minHeap.offer(curr); - max = Math.max(max, curr[0]); - } - } - return new int[]{start, start + minRange}; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_633.java b/src/main/java/com/fishercoder/solutions/_633.java deleted file mode 100644 index 9006850386..0000000000 --- a/src/main/java/com/fishercoder/solutions/_633.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 633. Sum of Square Numbers - * - * Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. - - Example 1: - Input: 5 - Output: True - Explanation: 1 * 1 + 2 * 2 = 5 - - Example 2: - Input: 3 - Output: False - */ -public class _633 { - public static class Solution1 { - public boolean judgeSquareSum(int c) { - if (c < 0) { - return false; - } - int left = 0; - int right = (int) (Math.sqrt(c)); - while (left <= right) { - int curr = left * left + right * right; - if (curr > c) { - right--; - } else if (curr < c) { - left++; - } else { - return true; - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_634.java b/src/main/java/com/fishercoder/solutions/_634.java deleted file mode 100644 index e2372d8f9d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_634.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 634. Find the Derangement of An Array - * - * In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position. - - There's originally an array consisting of n integers from 1 to n in ascending order, you need to find the number of derangement it can generate. - - Also, since the answer may be very large, you should return the output mod 109 + 7. - - Example 1: - Input: 3 - Output: 2 - Explanation: The original array is [1,2,3]. The two derangements are [2,3,1] and [3,1,2]. - - Note: - n is in the range of [1, 106]. - */ -public class _634 { - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/94442/java-5-lines-o-1-space-solution - * and https://leetcode.com/articles/find-derangements/#approach-5-using-formula-accepted - */ - private static final int M = 1000000007; - - public int findDerangement(int n) { - long ans = 1; - for (int i = 1; i <= n; i++) { - ans = (i * ans % M + (i % 2 == 0 ? 1 : -1)) % M; - } - return (int) ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_635.java b/src/main/java/com/fishercoder/solutions/_635.java deleted file mode 100644 index 5c0ab9e21f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_635.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - -/** - * 635. Design Log Storage System - * - * You are given several logs that each log contains a unique id and timestamp. - * Timestamp is a string that has the following format: Year:Month:Day:Hour:Minute:Second, - * for example, 2017:01:01:23:59:59. All domains are zero-padded decimal numbers. - * Design a log storage system to implement the following functions: - - void Put(int id, string timestamp): Given a log's unique id and timestamp, store the log in your storage system. - - int[] Retrieve(String start, String end, String granularity): - Return the id of logs whose timestamps are within the range from start to end. - Start and end all have the same format as timestamp. - However, granularity means the time level for consideration. - For example, start = "2017:01:01:23:59:59", end = "2017:01:02:23:59:59", granularity = "Day", - it means that we need to find the logs within the range from Jan. 1st 2017 to Jan. 2nd 2017. - - Example 1: - put(1, "2017:01:01:23:59:59"); - put(2, "2017:01:01:22:59:59"); - put(3, "2016:01:01:00:00:00"); - retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Year"); // return [1,2,3], because you need to return all logs within 2016 and 2017. - retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Hour"); // return [1,2], because you need to return all logs start from 2016:01:01:01 to 2017:01:01:23, where log 3 is left outside the range. - - Note: - There will be at most 300 operations of Put or Retrieve. - Year ranges from [2000,2017]. Hour ranges from [00,23]. - Output for Retrieve has no order required. - */ -public class _635 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/94449/concise-java-solution - */ - public static class LogSystem { - - /** - * These indices denote and string endings of timestamps of different granularity, i.e. - * timestamp[1] in timestamps: "2017:01:01:22:59:59" - * -> 2017: 4, 01: 7, 01: 10, 22: 13, 59: 16, 59: 19 - */ - - List timestamps; - List units; - int[] indices; - - public LogSystem() { - timestamps = new LinkedList<>(); - units = Arrays.asList("Year", "Month", "Day", "Hour", "Minute", "Second"); - indices = new int[]{4, 7, 10, 13, 16, 19}; - } - - public void put(int id, String timestamp) { - timestamps.add(new String[]{Integer.toString(id), timestamp}); - } - - public List retrieve(String s, String e, String gra) { - List res = new LinkedList<>(); - int index = units.indexOf(gra); - int stringEnd = indices[index]; - for (String[] timestamp : timestamps) { - if (timestamp[1].substring(0, stringEnd).compareTo(s.substring(0, stringEnd)) >= 0 - && timestamp[1].substring(0, stringEnd).compareTo(e.substring(0, stringEnd)) <= 0) { - res.add(Integer.parseInt(timestamp[0])); - } - } - return res; - } - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_636.java b/src/main/java/com/fishercoder/solutions/_636.java deleted file mode 100644 index 34b36a5a11..0000000000 --- a/src/main/java/com/fishercoder/solutions/_636.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Deque; -import java.util.LinkedList; -import java.util.List; - -/** - * 636. Exclusive Time of Functions - * - * Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, - * find the exclusive time of these functions. - * Each function has a unique id, start from 0 to n-1. - * A function may be called recursively or by another function. - * A log is a string has this format : function_id:start_or_end:timestamp. - * For example, "0:start:0" means function 0 starts from the very beginning of time 0. - * "0:end:0" means function 0 ends to the very end of time 0. - * Exclusive time of a function is defined as the time spent within this function, - * the time spent by calling other functions should not be considered as this function's exclusive time. - * You should return the exclusive time of each function sorted by their function id. - - Example 1: - Input: - n = 2 - logs = - ["0:start:0", - "1:start:2", - "1:end:5", - "0:end:6"] - Output:[3, 4] - Explanation: - Function 0 starts at time 0, then it executes 2 units of time and reaches the end of time 1. - Now function 0 calls function 1, function 1 starts at time 2, executes 4 units of time and end at time 5. - Function 0 is running again at time 6, and also end at the time 6, thus executes 1 unit of time. - So function 0 totally execute 2 + 1 = 3 units of time, and function 1 totally execute 4 units of time. - - Note: - Input logs will be sorted by timestamp, NOT log id. - Your output should be sorted by function id, which means the 0th element of your output corresponds to the exclusive time of function 0. - Two functions won't start or end at the same time. - Functions could be called recursively, and will always end. - 1 <= n <= 100 - */ -public class _636 { - public static class Solution1 { - /** - * Based on the example, it's difficult to see how function 2 executes 4 units of time, actually - * we can add 1 to all end times to make it easier to understand and AC'ed. - */ - public int[] exclusiveTime(int n, List logs) { - /**Stack is the way to go: - * we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp, - * we'll pop this logId only when we encounter this logId's end timestamp. - * Meanwhile, we keep a counter called prevTime, - * whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/ - Deque stack = new LinkedList<>(); - int[] result = new int[n]; - int prevTime = 0; - for (String log : logs) { - String[] parts = log.split(":"); - if (!stack.isEmpty()) { - result[stack.peek()] += Integer.parseInt(parts[2]) - prevTime; - } - prevTime = Integer.parseInt(parts[2]); - if (parts[1].equals("start")) { - stack.addFirst(Integer.parseInt(parts[0]));//i.e. stack.push() - } else { - prevTime++; - //remember to have result pluse 1 to match the problem AC criteria - result[stack.pollFirst()]++;//i.e. stack.pop() - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_637.java b/src/main/java/com/fishercoder/solutions/_637.java deleted file mode 100644 index cb776480e8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_637.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -/** - * 637. Average of Levels in Binary Tree - * - Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. - - Example 1: - Input: - 3 - / \ -9 20 - / \ - 15 7 - - Output: [3, 14.5, 11] - Explanation: - - The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11]. - Note: - The range of node's value is in the range of 32-bit signed integer. - */ -public class _637 { - - public static class Solution1 { - public List averageOfLevels(TreeNode root) { - List result = new ArrayList<>(); - if (root == null) { - return result; - } - Queue queue = new LinkedList<>(); - queue.offer(root); - while (!queue.isEmpty()) { - int size = queue.size(); - double sum = 0.0; - for (int i = 0; i < size; i++) { - TreeNode curr = queue.poll(); - if (curr != null) { - sum += curr.val; - } - if (curr.left != null) { - queue.offer(curr.left); - } - if (curr.right != null) { - queue.offer(curr.right); - } - } - result.add(sum / size); - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_638.java b/src/main/java/com/fishercoder/solutions/_638.java deleted file mode 100644 index efbd3e737b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_638.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 638. Shopping Offers - * - * In LeetCode Store, there are some kinds of items to sell. Each item has a price. - * However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price. - * You are given the each item's price, a set of special offers, - * and the number we need to buy for each item. - * The job is to output the lowest price you have to pay for exactly certain items as given, - * where you could make optimal use of the special offers. - * Each special offer is represented in the form of an array, - * the last number represents the price you need to pay for this special offer, - * other numbers represents how many specific items you could get if you buy this offer. - * You could use any of special offers as many times as you want. - - Example 1: - Input: [2,5], [[3,0,5],[1,2,10]], [3,2] - Output: 14 - Explanation: - There are two kinds of items, A and B. Their prices are $2 and $5 respectively. - In special offer 1, you can pay $5 for 3A and 0B - In special offer 2, you can pay $10 for 1A and 2B. - You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A. - - Example 2: - Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1] - Output: 11 - Explanation: - The price of A is $2, and $3 for B, $4 for C. - You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C. - You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C. - You cannot add more items, though only $9 for 2A ,2B and 1C. - - Note: - There are at most 6 kinds of items, 100 special offers. - For each item, you need to buy at most 6 of them. - You are not allowed to buy more items than you want, even if that would lower the overall price. - */ -public class _638 { - public static class Solution1 { - /** - * reference: https://leetcode.com/articles/shopping-offers/#approach-1-using-recursion-accepted - */ - public int shoppingOffers(List price, List> special, List needs) { - return shopping(price, special, needs, 0); - } - - public int shopping(List price, List> special, List needs, int i) { - if (i == special.size()) { - return dot(needs, price); - } - ArrayList clone = new ArrayList(needs); - int j = 0; - for (j = 0; j < special.get(i).size() - 1; j++) { - int diff = clone.get(j) - special.get(i).get(j); - if (diff < 0) { - break; - } - clone.set(j, diff); - } - if (j == special.get(i).size() - 1) { - return Math.min(special.get(i).get(j) + shopping(price, special, clone, i), shopping(price, special, needs, i + 1)); - } else { - return shopping(price, special, needs, i + 1); - } - } - - public int dot(List a, List b) { - int sum = 0; - for (int i = 0; i < a.size(); i++) { - sum += a.get(i) * b.get(i); - } - return sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_639.java b/src/main/java/com/fishercoder/solutions/_639.java deleted file mode 100644 index 6f5b5c96c8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_639.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 639. Decode Ways II - * - * A message containing letters from A-Z is being encoded to numbers using the following mapping way: - - 'A' -> 1 - 'B' -> 2 - ... - 'Z' -> 26 - Beyond that, now the encoded string can also contain the character '*', - which can be treated as one of the numbers from 1 to 9. - - Given the encoded message containing digits and the character '*', - return the total number of ways to decode it. - - Also, since the answer may be very large, you should return the output mod 109 + 7. - - Example 1: - Input: "*" - Output: 9 - Explanation: The encoded message can be decoded to the string: "A", "B", "C", "D", "E", "F", "G", "H", "I". - Example 2: - Input: "1*" - Output: 9 + 9 = 18 - - Note: - The length of the input string will fit in range [1, 105]. - The input string will only contain the character '*' and digits '0' - '9'. - - */ -public class _639 { - public static class Solution1 { - /** - * reference: https://leetcode.com/articles/decode-ways-ii/#approach-2-dynamic-programming-accepted - */ - int m = 1000000007; - - public int numDecodings(String s) { - long[] dp = new long[s.length() + 1]; - dp[0] = 1; - dp[1] = s.charAt(0) == '*' ? 9 : s.charAt(0) == '0' ? 0 : 1; - for (int i = 1; i < s.length(); i++) { - if (s.charAt(i) == '*') { - dp[i + 1] = 9 * dp[i]; - if (s.charAt(i - 1) == '1') { - dp[i + 1] = (dp[i + 1] + 9 * dp[i - 1]) % m; - } else if (s.charAt(i - 1) == '2') { - dp[i + 1] = (dp[i + 1] + 6 * dp[i - 1]) % m; - } else if (s.charAt(i - 1) == '*') { - dp[i + 1] = (dp[i + 1] + 15 * dp[i - 1]) % m; - } - } else { - dp[i + 1] = s.charAt(i) != '0' ? dp[i] : 0; - if (s.charAt(i - 1) == '1') { - dp[i + 1] = (dp[i + 1] + dp[i - 1]) % m; - } else if (s.charAt(i - 1) == '2' && s.charAt(i) <= '6') { - dp[i + 1] = (dp[i + 1] + dp[i - 1]) % m; - } else if (s.charAt(i - 1) == '*') { - dp[i + 1] = (dp[i + 1] + (s.charAt(i) <= '6' ? 2 : 1) * dp[i - 1]) % m; - } - } - } - return (int) dp[s.length()]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_640.java b/src/main/java/com/fishercoder/solutions/_640.java deleted file mode 100644 index e55e84ada0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_640.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 640. Solve the Equation - * - * Solve a given equation and return the value of x in the form of string "x=#value". - * The equation contains only '+', '-' operation, the variable x and its coefficient. - - If there is no solution for the equation, return "No solution". - - If there are infinite solutions for the equation, return "Infinite solutions". - - If there is exactly one solution for the equation, we ensure that the value of x is an integer. - - Example 1: - Input: "x+5-3+x=6+x-2" - Output: "x=2" - - Example 2: - Input: "x=x" - Output: "Infinite solutions" - - Example 3: - Input: "2x=x" - Output: "x=0" - - Example 4: - Input: "2x+3x-6x=x+2" - Output: "x=-1" - - Example 5: - Input: "x=x+2" - Output: "No solution" - - */ -public class _640 { - public static class Solution1 { - /** - * Reference: https://discuss.leetcode.com/topic/95203/concise-java-solution/7 - */ - public String solveEquation(String equation) { - String[] parts = equation.split("="); - int[] left = evaluate(parts[0]); - int[] right = evaluate(parts[1]); - if (left[0] == right[0] && left[1] == right[1]) { - return "Infinite solutions"; - } else if (left[0] == right[0]) { - return "No solution"; - } - return "x=" + (right[1] - left[1]) / (left[0] - right[0]); - } - - private int[] evaluate(String part) { - int[] result = new int[2];//result[0] is the coefficient for x, result[1] is the coefficient for constants - String[] tokens = part.split("(?=[+-])"); // ()for match group; ?= for match and include in res; [+-] means + or -; - for (String token : tokens) { - if (token.equals("+x") || token.equals("x")) { - result[0]++; - } else if (token.equals("-x")) { - result[0]--; - } else if (token.contains("x")) { - result[0] += Integer.parseInt(token.substring(0, token.length() - 1)); - } else { - result[1] += Integer.parseInt(token); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_642.java b/src/main/java/com/fishercoder/solutions/_642.java deleted file mode 100644 index 4cd4724a6a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_642.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -/** - * 642. Design Search Autocomplete System - * - * Design a search autocomplete system for a search engine. Users may input a sentence (at least one word and end with a special character '#'). For each character they type except '#', you need to return the top 3 historical hot sentences that have prefix the same as the part of sentence already typed. Here are the specific rules: - - The hot degree for a sentence is defined as the number of times a user typed the exactly same sentence before. - The returned top 3 hot sentences should be sorted by hot degree (The first is the hottest one). If several sentences have the same degree of hot, you need to use ASCII-code order (smaller one appears first). - If less than 3 hot sentences exist, then just return as many as you can. - When the input is a special character, it means the sentence ends, and in this case, you need to return an empty list. - Your job is to implement the following functions: - - The constructor function: - - AutocompleteSystem(String[] sentences, int[] times): This is the constructor. The input is historical data. Sentences is a string array consists of previously typed sentences. Times is the corresponding times a sentence has been typed. Your system should record these historical data. - - Now, the user wants to input a new sentence. The following function will provide the next character the user types: - - List input(char c): The input c is the next character typed by the user. The character will only be lower-case letters ('a' to 'z'), blank space (' ') or a special character ('#'). Also, the previously typed sentence should be recorded in your system. The output will be the top 3 historical hot sentences that have prefix the same as the part of sentence already typed. - - - Example: - Operation: AutocompleteSystem(["i love you", "island","ironman", "i love leetcode"], [5,3,2,2]) - The system have already tracked down the following sentences and their corresponding times: - "i love you" : 5 times - "island" : 3 times - "ironman" : 2 times - "i love leetcode" : 2 times - Now, the user begins another search: - - Operation: input('i') - Output: ["i love you", "island","i love leetcode"] - Explanation: - There are four sentences that have prefix "i". Among them, "ironman" and "i love leetcode" have same hot degree. Since ' ' has ASCII code 32 and 'r' has ASCII code 114, "i love leetcode" should be in front of "ironman". Also we only need to output top 3 hot sentences, so "ironman" will be ignored. - - Operation: input(' ') - Output: ["i love you","i love leetcode"] - Explanation: - There are only two sentences that have prefix "i ". - - Operation: input('a') - Output: [] - Explanation: - There are no sentences that have prefix "i a". - - Operation: input('#') - Output: [] - Explanation: - The user finished the input, the sentence "i a" should be saved as a historical sentence in system. And the following input will be counted as a new search. - - Note: - The input sentence will always start with a letter and end with '#', and only one blank space will exist between two words. - The number of complete sentences that to be searched won't exceed 100. The length of each sentence including those in the historical data won't exceed 100. - Please use double-quote instead of single-quote when you write test cases even for a character input. - Please remember to RESET your class variables declared in class AutocompleteSystem, as static/class variables are persisted across multiple test cases. Please see here for more details. - */ -public class _642 { - public static class Solution1 { - - /** - * reference: https://discuss.leetcode.com/topic/96150/java-solution-trie-and-priorityqueue/3 - */ - public class AutocompleteSystem { - - Map map; - List> answers; - StringBuilder stringBuilder; - - public AutocompleteSystem(String[] sentences, int[] times) { - map = new HashMap<>(); - answers = new ArrayList<>(); - stringBuilder = new StringBuilder(); - - for (int i = 0; i < sentences.length; i++) { - map.put(sentences[i], map.getOrDefault(sentences[i], 0) + times[i]); - } - } - - public List input(char c) { - List result = new ArrayList<>(); - if (c == '#') { - map.put(stringBuilder.toString(), map.getOrDefault(stringBuilder.toString(), 0) + 1); - stringBuilder.setLength(0); - answers.clear();/**The user has finished typing, so we'll clean answers to get ready for next search*/ - } else { - stringBuilder.append(c); - /**when its length is 1, we find all the prefix that is a match and put them into answers, - * then for the rest, we'll just remove those that are not match with the prefix any more, we do this logic in else branch*/ - if (stringBuilder.length() == 1) { - for (Map.Entry entry : map.entrySet()) { - if (entry.getKey().startsWith(stringBuilder.toString())) { - answers.add(entry); - } - } - Collections.sort(answers, (a, b) -> a.getValue() == b.getValue() ? a.getKey().compareTo(b.getKey()) : b.getValue() - a.getValue()); - } else { - for (Iterator> iterator = answers.iterator(); iterator.hasNext(); ) { - if (!iterator.next().getKey().startsWith(stringBuilder.toString())) { - iterator.remove(); - } - } - } - for (int i = 0; i < 3 && i < answers.size(); i++) { - result.add(answers.get(i).getKey()); - } - } - return result; - } - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_643.java b/src/main/java/com/fishercoder/solutions/_643.java deleted file mode 100644 index 19b49bfadd..0000000000 --- a/src/main/java/com/fishercoder/solutions/_643.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 643. Maximum Average Subarray I - * Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. - * And you need to output the maximum average value. - * Example 1: - * Input: [1,12,-5,-6,50,3], k = 4 - * Output: 12.75 - * Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75 - * Note: - * 1 <= k <= n <= 30,000. - * Elements of the given array will be in the range [-10,000, 10,000]. - */ -public class _643 { - - public static class Solution1 { - public double findMaxAverage(int[] nums, int k) { - double sum = 0; - double maxAve = Integer.MIN_VALUE; - for (int i = 0; i < nums.length; i++) { - if (k <= i) { - sum -= nums[i - k]; - } - sum += nums[i]; - if ((i + 1) >= k) { - maxAve = Math.max(maxAve, sum / k); - } - } - return maxAve; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_644.java b/src/main/java/com/fishercoder/solutions/_644.java deleted file mode 100644 index ba6d0281b1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_644.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 644. Maximum Average Subarray II - *

- * Given an array consisting of n integers, find the contiguous subarray whose length is greater than - * or equal to k that has the maximum average value. And you need to output the maximum average value. - *

- * Example 1: - * Input: [1,12,-5,-6,50,3], k = 4 - * Output: 12.75 - * Explanation: - * when length is 5, maximum average value is 10.8, - * when length is 6, maximum average value is 9.16667. - * Thus return 12.75. - *

- * Note: - * 1 <= k <= n <= 10,000. - * Elements of the given array will be in range [-10,000, 10,000]. - * The answer with the calculation error less than 10-5 will be accepted. - */ -public class _644 { - /**reference: https://leetcode.com/articles/maximum-average-subarray-ii/#approach-2-using-binary-search-accepted - * https://discuss.leetcode.com/topic/96123/java-solution-o-nlogm-binary-search-the-answer/13*/ - - /** - * To understand the idea behind this method, let's look at the following points. - * Firstly, we know that the value of the average could lie between the range (min, max)(min,max). - * Here, minmin and maxmax refer to the minimum and the maximum values out of the given numsnums array. - * This is because, the average can't be lesser than the minimum value and can't be larger than the maximum value. - * But, in this case, we need to find the maximum average of a subarray with atleast kk elements. - * The idea in this method is to try to approximate(guess) the solution and to try to find if this solution really exists. - * If it exists, we can continue trying to approximate the solution even to a further precise value, - * but choosing a larger number as the next approximation. - * But, if the initial guess is wrong, and the initial maximum average value(guessed) isn't possible, - * we need to try with a smaller number as the next approximate. - *

- * Now, instead of doing the guesses randomly, we can make use of Binary Search. - * With minmin and maxmax as the initial numbers to begin with, - * we can find out the midmid of these two numbers given by (min+max)/2(min+max)/2. - * Now, we need to find if a subarray with length greater than or equal to kk is possible with an average sum greater than this midmid value. - */ - public double findMaxAverage(int[] nums, int k) { - double l = -10000; - double r = 10000; - while (r - l > 10e-7) { - double mid = (l + r) / 2; - if (getMaxSubbaraySumOfSizeK(nums, k, mid) >= 0) { - l = mid; - } else { - r = mid; - } - } - return (l + r) / 2; - } - - private double getMaxSubbaraySumOfSizeK(int[] nums, int k, double mid) { - double sum = 0.0; - for (int i = 0; i <= k - 1; i++) { - sum += nums[i] - mid; - } - double maxSum = sum; - double prev = nums[0] - mid; - for (int i = k; i < nums.length; i++) { - sum = sum - nums[i - k] + nums[i]; - maxSum = Math.max(maxSum, Math.max(sum, sum + prev)); - prev = Math.max(nums[i - k + 1], nums[i - k + 1] + prev) - mid; - } - return maxSum; - } -} diff --git a/src/main/java/com/fishercoder/solutions/_645.java b/src/main/java/com/fishercoder/solutions/_645.java deleted file mode 100644 index 3567ecc3d6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_645.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 645. Set Mismatch - * - * The set S originally contains numbers from 1 to n. - * But unfortunately, due to the data error, one of the numbers in - * the set got duplicated to another number in the set, - * which results in repetition of one number and loss of another number. - - Given an array nums representing the data status of this set after the error. - Your task is to firstly find the number occurs twice and then find the number - that is missing. Return them in the form of an array. - - Example 1: - Input: nums = [1,2,2,4] - Output: [2,3] - - Note: - The given array size will in the range [2, 10000]. - The given array's numbers won't have any order. - */ -public class _645 { - public static class Solution1 { - public int[] findErrorNums(int[] nums) { - int[] result = new int[2]; - Arrays.sort(nums); - for (int i = 0; i < nums.length - 1; i++) { - if (nums[i + 1] == nums[i]) { - result[0] = nums[i]; - } - } - long temp = 0; - for (int i = 0; i < nums.length; i++) { - temp += i + 1; - temp -= nums[i]; - } - temp += result[0]; - result[1] = (int) temp; - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_646.java b/src/main/java/com/fishercoder/solutions/_646.java deleted file mode 100644 index bd8c4fe72d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_646.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 646. Maximum Length of Pair Chain - * - * You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. - - Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion. - - Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order. - - Example 1: - Input: [[1,2], [2,3], [3,4]] - Output: 2 - Explanation: The longest chain is [1,2] -> [3,4] - - Note: - The number of given pairs will be in the range [1, 1000]. - */ -public class _646 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/96804/java-o-nlog-n-time-o-1-space - */ - public int findLongestChain(int[][] pairs) { - Arrays.sort(pairs, (o1, o2) -> o1[1] - o2[1]); - int result = 0; - int n = pairs.length; - int i = -1; - while (++i < n) { - result++; - int curEnd = pairs[i][1]; - while (i + 1 < n && pairs[i + 1][0] <= curEnd) { - /**This means, we'll keep incrementing i until pairs[i+1][0] is - * exactly greater than curEnd.*/ - i++; - } - } - return result; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_647.java b/src/main/java/com/fishercoder/solutions/_647.java deleted file mode 100644 index 61692b43a3..0000000000 --- a/src/main/java/com/fishercoder/solutions/_647.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 647. Palindromic Substrings - * - * Given a string, your task is to count how many palindromic substrings in this string. - * The substrings with different start indexes or end indexes are counted - * as different substrings even they consist of same characters. - - Example 1: - Input: "abc" - Output: 3 - Explanation: Three palindromic strings: "a", "b", "c". - - Example 2: - Input: "aaa" - Output: 6 - Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". - - Note: - The input string length won't exceed 1000. - - */ - -public class _647 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/96819/java-solution-8-lines-extendpalindrome - */ - public int countSubstrings(String s) { - int count = 0; - for (int i = 0; i < s.length(); i++) { - count += extendPalindrome(s, i, i);//odd length - count += extendPalindrome(s, i, i + 1);//even length - } - return count; - } - - private int extendPalindrome(String s, int left, int right) { - int count = 0; - while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { - count++; - left--; - right++; - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_648.java b/src/main/java/com/fishercoder/solutions/_648.java deleted file mode 100644 index 2767a6d110..0000000000 --- a/src/main/java/com/fishercoder/solutions/_648.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.List; - -/** - * 648. Replace Words - - * In English, we have a concept called root, - * which can be followed by some other words to form another longer word - * - let's call this word successor. - * For example, the root an, followed by other, which can form another word another. - * Now, given a dictionary consisting of many roots and a sentence. - * You need to replace all the successor in the sentence with the root forming it. - * If a successor has many roots can form it, replace it with the root with the shortest length. - - You need to output the sentence after the replacement. - - Example 1: - Input: dict = ["cat", "bat", "rat"] - sentence = "the cattle was rattled by the battery" - Output: "the cat was rat by the bat" - - Note: - The input will only have lower-case letters. - 1 <= dict words number <= 1000 - 1 <= sentence words number <= 1000 - 1 <= root length <= 100 - 1 <= sentence words length <= 1000 - - */ -public class _648 { - public static class Solution1 { - public String replaceWords(List dict, String sentence) { - String[] tokens = sentence.split(" "); - TrieNode trie = buildTrie(dict); - return replaceWords(tokens, trie); - } - - private String replaceWords(String[] tokens, TrieNode root) { - StringBuilder stringBuilder = new StringBuilder(); - for (String token : tokens) { - stringBuilder.append(getShortestReplacement(token, root)); - stringBuilder.append(" "); - } - return stringBuilder.substring(0, stringBuilder.length() - 1); - } - - private String getShortestReplacement(String token, final TrieNode root) { - TrieNode temp = root; - StringBuilder stringBuilder = new StringBuilder(); - for (char c : token.toCharArray()) { - stringBuilder.append(c); - if (temp.children[c - 'a'] != null) { - if (temp.children[c - 'a'].isWord) { - return stringBuilder.toString(); - } - temp = temp.children[c - 'a']; - } else { - return token; - } - } - return token; - } - - private TrieNode buildTrie(List dict) { - TrieNode root = new TrieNode(' '); - for (String word : dict) { - TrieNode temp = root; - for (char c : word.toCharArray()) { - if (temp.children[c - 'a'] == null) { - temp.children[c - 'a'] = new TrieNode(c); - } - temp = temp.children[c - 'a']; - } - temp.isWord = true; - } - return root; - } - - public class TrieNode { - char val; - TrieNode[] children; - boolean isWord; - - public TrieNode(char val) { - this.val = val; - this.children = new TrieNode[26]; - this.isWord = false; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_649.java b/src/main/java/com/fishercoder/solutions/_649.java deleted file mode 100644 index f204dd7f74..0000000000 --- a/src/main/java/com/fishercoder/solutions/_649.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 649. Dota2 Senate - * - * In the world of Dota2, there are two parties: the Radiant and the Dire. - - The Dota2 senate consists of senators coming from two parties. - Now the senate wants to make a decision about a change in the Dota2 game. - The voting for this change is a round-based procedure. - In each round, each senator can exercise one of the two rights: - - 1. Ban one senator's right: - A senator can make another senator lose all his rights in this and all the following rounds. - 2. Announce the victory: - If this senator found the senators who still have rights to vote are all from the same party, - he can announce the victory and make the decision about the change in the game. - Given a string representing each senator's party belonging. - The character 'R' and 'D' represent the Radiant party and the Dire party respectively. - Then if there are n senators, the size of the given string will be n. - - The round-based procedure starts from the first senator to the last senator in the given order. - This procedure will last until the end of voting. - All the senators who have lost their rights will be skipped during the procedure. - - Suppose every senator is smart enough and will play the best strategy for his own party, - you need to predict which party will finally announce the victory and make the change in the Dota2 game. - The output should be Radiant or Dire. - - Example 1: - Input: "RD" - Output: "Radiant" - Explanation: The first senator comes from Radiant and he can just ban the next senator's right in the round 1. - And the second senator can't exercise any rights any more since his right has been banned. - And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote. - - Example 2: - Input: "RDD" - Output: "Dire" - Explanation: - The first senator comes from Radiant and he can just ban the next senator's right in the round 1. - And the second senator can't exercise any rights anymore since his right has been banned. - And the third senator comes from Dire and he can ban the first senator's right in the round 1. - And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote. - Note: - The length of the given string will in the range [1, 10,000]. - - */ -public class _649 { - - public static class Solution1 { - public String predictPartyVictory(String senate) { - Queue radiantQ = new LinkedList<>(); - Queue direQ = new LinkedList<>(); - int len = senate.length(); - for (int i = 0; i < len; i++) { - if (senate.charAt(i) == 'R') { - radiantQ.offer(i); - } else { - direQ.offer(i); - } - } - while (!radiantQ.isEmpty() && !direQ.isEmpty()) { - int radiantIndex = radiantQ.poll(); - int direIndex = direQ.poll(); - if (radiantIndex < direIndex) { - /**Radiant will ban Dire in this case, so we'll add radiant index back to the queue plus n*/ - radiantQ.offer(radiantIndex + len); - } else { - direQ.offer(direIndex + len); - } - } - return radiantQ.isEmpty() ? "Dire" : "Radiant"; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_650.java b/src/main/java/com/fishercoder/solutions/_650.java deleted file mode 100644 index f5e7507d29..0000000000 --- a/src/main/java/com/fishercoder/solutions/_650.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 650. 2 Keys Keyboard - * - * Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: - - Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). - Paste: You can paste the characters which are copied last time. - Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. - - Example 1: - Input: 3 - Output: 3 - - Explanation: - Intitally, we have one character 'A'. - In step 1, we use Copy All operation. - In step 2, we use Paste operation to get 'AA'. - In step 3, we use Paste operation to get 'AAA'. - - Note: - The n will be in the range [1, 1000]. - */ -public class _650 { - - public static class Solution1 { - public int minSteps(int n) { - int[] dp = new int[n + 1]; - for (int i = 2; i <= n; i++) { - dp[i] = i;//we assign i to dp[i] first, because for a lot of cases, e.g. for most cases when i is odd, its min steps is i itself, if it's not, we can overwrite it later - for (int j = i - 1; j > 1; j--) { - //traverse backwards, whenever it's divisible by j, we'll update dp[i] because it's guaranteed to be smaller when j is smaller. - if (i % j == 0) { - dp[i] = dp[j] + (i / j); - break; - } - } - } - return dp[n]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_651.java b/src/main/java/com/fishercoder/solutions/_651.java deleted file mode 100644 index 4a20e565cf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_651.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 651. 4 Keys Keyboard - * - * Imagine you have a special keyboard with the following keys: - - Key 1: (A): Prints one 'A' on screen. - - Key 2: (Ctrl-A): Select the whole screen. - - Key 3: (Ctrl-C): Copy selection to buffer. - - Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. - - Now, you can only press the keyboard for N times (with the above four keys), find out the maximum numbers of 'A' you can print on screen. - - Example 1: - Input: N = 3 - Output: 3 - Explanation: - We can at most get 3 A's on screen by pressing following key sequence: - A, A, A - - Example 2: - Input: N = 7 - Output: 9 - Explanation: - We can at most get 9 A's on screen by pressing following key sequence: - A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V - - Note: - 1 <= N <= 50 - Answers will be in the range of 32-bit signed integer. - */ -public class _651 { - - public static class Solution1 { - /** - * Minimum needs to be more than 3 A's in a row, otherwise "Ctrl A, Ctrl C, Ctrl V" will make fewer A's than directly - * copying A's with the equal number of steps. - * E.g. when n == 5, - * if we do 5 this: A, A, Ctrl A, Ctrl C, Ctrl V, => this will result in only AAAA (4 A's) - * while if we do A, A, A, A, A, => this will result in AAAAA (5 A's) - * So, at a minimum, we need to have 3 A's, then it's worth to do "Ctrl A, Ctrl C, Ctrl V".. - * That's why we have j = 3 in the inner for loop below. - */ - public int maxA(int N) { - int[] dp = new int[N + 1]; - for (int i = 1; i <= N; i++) { - dp[i] = i; - for (int j = 3; j < i; j++) { - dp[i] = Math.max(dp[i], dp[i - j] * (j - 1)); - } - } - return dp[N]; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_652.java b/src/main/java/com/fishercoder/solutions/_652.java deleted file mode 100644 index 0f8b0ba721..0000000000 --- a/src/main/java/com/fishercoder/solutions/_652.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; -import java.util.Set; - -/** - * 652. Find Duplicate Subtrees - * - * Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them. - - Two trees are duplicate if they have the same structure with same node values. - - Example 1: - 1 - / \ - 2 3 - / / \ - 4 2 4 - / - 4 - The following are two duplicate subtrees: - 2 - / - 4 - and - 4 - - Therefore, you need to return above trees' root in the form of a list. - */ -public class _652 { - public static class Solution1 { - - /**credit: https://discuss.leetcode.com/topic/97584/java-concise-postorder-traversal-solution*/ - - /** - * You don't actually need to check if every other tree is a duplicate of current node, - * just when you go through each node, you'll see whether there's already one in the map, - * since map.containsKey() checks this TreeNode. - */ - public List findDuplicateSubtrees(TreeNode root) { - List res = new LinkedList<>(); - postorder(root, new HashMap<>(), res); - return res; - } - - private String postorder(TreeNode curr, HashMap map, List res) { - if (curr == null) { - return "#"; - } - String serial = curr.val + "," + postorder(curr.left, map, res) + "," + postorder(curr.right, map, res); - if (map.getOrDefault(serial, 0) == 1) { - res.add(curr); - } - map.put(serial, map.getOrDefault(serial, 0) + 1); - return serial; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_653.java b/src/main/java/com/fishercoder/solutions/_653.java deleted file mode 100644 index f65b6e011e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_653.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.List; - -/** - * 653. Two Sum IV - Input is a BST - * - * Given a Binary Search Tree and a target number, - * return true if there exist two elements in the BST such that their sum is equal to the given target. - - Example 1: - Input: - 5 - / \ - 3 6 - / \ \ -2 4 7 - -Target = 9 -Output: True - -Example 2: -Input: - 5 - / \ - 3 6 - / \ \ -2 4 7 - -Target = 28 -Output: False - - */ -public class _653 { - - public static class Solution1 { - public boolean findTarget(TreeNode root, int k) { - if (root == null) { - return false; - } - List list = new ArrayList<>(); - dfs(root, list); - for (int i = 0; i < list.size() - 1; i++) { - for (int j = i + 1; j < list.size(); j++) { - if (list.get(i) + list.get(j) == k) { - return true; - } - } - } - return false; - } - - private void dfs(TreeNode root, List list) { - list.add(root.val); - if (root.left != null) { - dfs(root.left, list); - } - if (root.right != null) { - dfs(root.right, list); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_654.java b/src/main/java/com/fishercoder/solutions/_654.java deleted file mode 100644 index a0999402bc..0000000000 --- a/src/main/java/com/fishercoder/solutions/_654.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 654. Maximum Binary Tree - * - * Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: - * - * The root is the maximum number in the array. - * The left subtree is the maximum tree constructed from left part subarray divided by the maximum number. - * The right subtree is the maximum tree constructed from right part subarray divided by the maximum number. - * Construct the maximum tree by the given array and output the root node of this tree. - - Example 1: - Input: [3,2,1,6,0,5] - Output: return the tree root node representing the following tree: - - 6 - / \ - 3 5 - \ / - 2 0 - \ - 1 - - Note: - The size of the given array will be in the range [1,1000]. - */ -public class _654 { - - public static class Solution1 { - /** - * Completely my original solution: - * - * As the problem states, I always broke the array into two halves and make notes - * of current max node, then in the recursive call, we can recursively search - * from its left part to construct its left subtree and its right part to construct its - * right subtree.*/ - public TreeNode constructMaximumBinaryTree(int[] nums) { - int max = Integer.MIN_VALUE; - int maxIndex = -1; - for (int i = 0; i < nums.length; i++) { - if (nums[i] > max) { - max = nums[i]; - maxIndex = i; - } - } - TreeNode root = new TreeNode(max); - return constructMaxTree(root, maxIndex, nums, 0, nums.length - 1); - } - - private TreeNode constructMaxTree(TreeNode root, int rootIndex, int[] nums, int start, int end) { - if (rootIndex > start) { - int max = Integer.MIN_VALUE; - int maxIndex = -1; - for (int i = start; i < rootIndex; i++) { - if (max < nums[i]) { - max = nums[i]; - maxIndex = i; - } - } - root.left = constructMaxTree(new TreeNode(max), maxIndex, nums, start, rootIndex - 1); - } - if (rootIndex < end) { - int max = Integer.MIN_VALUE; - int maxIndex = -1; - for (int i = rootIndex + 1; i <= end; i++) { - if (max < nums[i]) { - max = nums[i]; - maxIndex = i; - } - } - root.right = constructMaxTree(new TreeNode(max), maxIndex, nums, rootIndex + 1, end); - } - return root; - } - } - - public static class Solution2 { - /** - * Completely my original solution as well, but more concise. - */ - public TreeNode constructMaximumBinaryTree(int[] nums) { - if (nums == null || nums.length == 0) { - return null; - } - return construct(nums, 0, nums.length - 1); - } - - TreeNode construct(int[] nums, int start, int end) { - if (start > end) { - return null; - } - int[] maxArray = findMax(nums, start, end); - TreeNode root = new TreeNode(maxArray[0]); - root.left = construct(nums, start, maxArray[1] - 1); - root.right = construct(nums, maxArray[1] + 1, end); - return root; - } - - int[] findMax(int[] nums, int start, int end) { - int max = nums[start]; - int maxIndex = start; - for (int i = start + 1; i <= end; i++) { - if (max < nums[i]) { - maxIndex = i; - max = nums[i]; - } - } - return new int[]{max, maxIndex}; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_655.java b/src/main/java/com/fishercoder/solutions/_655.java deleted file mode 100644 index 17c837aebb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_655.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -/** - * 655. Print Binary Tree - * - * Print a binary tree in an m*n 2D string array following these rules: - * 1. The row number m should be equal to the height of the given binary tree. - * 2. The column number n should always be an odd number. - * 3. The root node's value (in string format) should be put in the exactly middle of the first row it can be put. - * The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). - * You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. - * The left-bottom part and the right-bottom part should have the same size. - * Even if one subtree is none while the other is not, - * you don't need to print anything for the none subtree but - * still need to leave the space as large as that for the other subtree. - * However, if two subtrees are none, then you don't need to leave space for both of them. - * 4. Each unused space should contain an empty string "". - * 5. Print the subtrees following the same rules. - - Example 1: - Input: - 1 - / - 2 - Output: - [["", "1", ""], - ["2", "", ""]] - - Example 2: - Input: - 1 - / \ - 2 3 - \ - 4 - Output: - [["", "", "", "1", "", "", ""], - ["", "2", "", "", "", "3", ""], - ["", "", "4", "", "", "", ""]] - - Example 3: - Input: - 1 - / \ - 2 5 - / - 3 - / - 4 - Output: - - [["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""] - ["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""] - ["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""] - ["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]] - - Note: The height of binary tree is in the range of [1, 10] - */ -public class _655 { - - public static class Solution1 { - - /** - * Reference: https://discuss.leetcode.com/topic/98381/java-recursive-solution - * and https://leetcode.com/articles/print-binary-tree/ - */ - public List> printTree(TreeNode root) { - List> result = new LinkedList<>(); - int height = root == null ? 1 : getHeight(root); - int rows = height; - int columns = (int) (Math.pow(2, height) - 1); - List row = new ArrayList<>(); - for (int i = 0; i < columns; i++) { - row.add(""); - } - for (int i = 0; i < rows; i++) { - result.add(new ArrayList<>(row)); - } - populateResult(root, result, 0, rows, 0, columns - 1); - return result; - } - - private void populateResult(TreeNode root, List> result, int row, int totalRows, int i, int j) { - if (row == totalRows || root == null) { - return; - } - result.get(row).set((i + j) / 2, Integer.toString(root.val)); - populateResult(root.left, result, row + 1, totalRows, i, (i + j) / 2 - 1); - populateResult(root.right, result, row + 1, totalRows, (i + j) / 2 + 1, j); - } - - private int getHeight(TreeNode root) { - if (root == null) { - return 0; - } - return 1 + Math.max(getHeight(root.left), getHeight(root.right)); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_656.java b/src/main/java/com/fishercoder/solutions/_656.java deleted file mode 100644 index b21c5d240d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_656.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 656. Coin Path - * - * Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. - * The integer B denotes that from any place (suppose the index is i) in the array A, - * you can jump to any one of the place in the array A indexed i+1, i+2, …, i+B if this place can be jumped to. - * Also, if you step on the index i, you have to pay Ai coins. If Ai is -1, it means you can’t jump to the place indexed i in the array. - * Now, you start from the place indexed 1 in the array A, - * and your aim is to reach the place indexed N using the minimum coins. - * You need to return the path of indexes (starting from 1 to N) in the array you should take to get to the place indexed N using minimum coins. - * If there are multiple paths with the same cost, return the lexicographically smallest such path. - * If it's not possible to reach the place indexed N then you need to return an empty array. - - Example 1: - Input: [1,2,4,-1,2], 2 - Output: [1,3,5] - - Example 2: - Input: [1,2,4,-1,2], 1 - Output: [] - - Note: - Path Pa1, Pa2, ..., Pan is lexicographically smaller than Pb1, Pb2, ..., Pbm, - if and only if at the first i where Pai and Pbi differ, Pai < Pbi; when no such i exists, then n < m. - A1 >= 0. A2, ..., AN (if exist) will in the range of [-1, 100]. - Length of A is in the range of [1, 1000]. - B is in the range of [1, 100]. - */ - -public class _656 { - public static class Solution1 { - - /** - * Time: O(n*B) - * Reference: https://leetcode.com/articles/coin-path/#approach-3-using-dynamic-programming-accepted - */ - public List cheapestJump(int[] A, int B) { - int[] next = new int[A.length]; - long[] dp = new long[A.length]; - Arrays.fill(next, -1); - List res = new ArrayList(); - for (int i = A.length - 2; i >= 0; i--) { - long minCost = Integer.MAX_VALUE; - for (int j = i + 1; j <= i + B && j < A.length; j++) { - if (A[j] >= 0) { - long cost = A[i] + dp[j]; - if (cost < minCost) { - minCost = cost; - next[i] = j; - } - } - } - dp[i] = minCost; - } - int i; - for (i = 0; i < A.length && next[i] > 0; i = next[i]) { - res.add(i + 1); - } - if (i == A.length - 1 && A[i] >= 0) { - res.add(A.length); - } else { - return new ArrayList<>(); - } - return res; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_657.java b/src/main/java/com/fishercoder/solutions/_657.java deleted file mode 100644 index b30353d0aa..0000000000 --- a/src/main/java/com/fishercoder/solutions/_657.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 657. Judge Route Circle - * - * Initially, there is a Robot at position (0, 0). - * Given a sequence of its moves, judge if this robot makes a circle, - * which means it moves back to the original place. - - The move sequence is represented by a string. - And each move is represent by a character. - The valid robot moves are R (Right), L (Left), U (Up) and D (down). - The output should be true or false representing whether the robot makes a circle. - - Example 1: - Input: "UD" - Output: true - - Example 2: - Input: "LL" - Output: false - */ - -public class _657 { - public static class Solution1 { - public boolean judgeCircle(String moves) { - int hori = 0; - int verti = 0; - for (char c : moves.toCharArray()) { - if (c == 'U') { - verti++; - } else if (c == 'D') { - verti--; - } else if (c == 'L') { - hori--; - } else if (c == 'R') { - hori++; - } - } - return verti == 0 && hori == 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_658.java b/src/main/java/com/fishercoder/solutions/_658.java deleted file mode 100644 index b3a0dba014..0000000000 --- a/src/main/java/com/fishercoder/solutions/_658.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 658. Find K Closest Elements - * - * Given a sorted array, two integers k and x, - * find the k closest elements to x in the array. - * The result should also be sorted in ascending order. - * If there is a tie, the smaller elements are always preferred. - - Example 1: - Input: [1,2,3,4,5], k=4, x=3 - Output: [1,2,3,4] - - Example 2: - Input: [1,2,3,4,5], k=4, x=-1 - Output: [1,2,3,4] - - Note: - The value k is positive and will always be smaller than the length of the sorted array. - Length of the given array is positive and will not exceed 104 - Absolute value of elements in the array and x will not exceed 104 - */ -public class _658 { - public static class Solution1 { - public List findClosestElements(List arr, int k, int x) { - List result = new ArrayList<>(); - int index = findInsertPosition(arr, x); - int right = 0; - if (arr.get(index) == x) { - result.add(arr.get(index)); - right = index + 1; - k--; - } else { - right = index; - } - int left = index - 1; - while (k-- > 0) { - if (left >= 0 && right < arr.size()) { - if (x - arr.get(left) <= arr.get(right) - x) { - result.add(0, arr.get(left--)); - } else { - result.add(result.size(), arr.get(right++)); - } - } else if (left >= 0) { - result.add(0, arr.get(left--)); - } else if (right < arr.size()) { - result.add(result.size(), arr.get(right++)); - } - } - return result; - } - - private int findInsertPosition(List arr, int x) { - if (arr == null || arr.size() == 0) { - return 0; - } - int len = arr.size(); - for (int i = 0; i < len; i++) { - if (arr.get(0) > x) { - return 0; - } else if (arr.get(len - 1) < x) { - return len; - } else if (arr.get(i) < x && arr.get(i + 1) > x) { - return i + 1; - } else if (arr.get(i) == x) { - return i; - } - } - return len; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_659.java b/src/main/java/com/fishercoder/solutions/_659.java deleted file mode 100644 index 01cd42da6e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_659.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 659. Split Array into Consecutive Subsequences - * - * You are given an integer array sorted in ascending order (may contain duplicates), - * you need to split them into several subsequences, - * where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. - - Example 1: - Input: [1,2,3,3,4,5] - Output: True - Explanation: - You can split them into two consecutive subsequences : - 1, 2, 3 - 3, 4, 5 - - Example 2: - Input: [1,2,3,3,4,4,5,5] - Output: True - Explanation: - You can split them into two consecutive subsequences : - 1, 2, 3, 4, 5 - 3, 4, 5 - - Example 3: - Input: [1,2,3,4,4,5] - Output: False - - Note: - The length of the input is in range of [1, 10000] - */ - -public class _659 { - - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/99187/java-o-n-time-o-n-space - */ - public boolean isPossible(int[] nums) { - Map freqMap = new HashMap<>(); - for (int i : nums) { - freqMap.put(i, freqMap.getOrDefault(i, 0) + 1); - } - Map appendFreqMap = new HashMap<>(); - for (int i : nums) { - if (freqMap.get(i) == 0) { - continue; - } else if (appendFreqMap.getOrDefault(i, 0) > 0) { - appendFreqMap.put(i, appendFreqMap.get(i) - 1); - appendFreqMap.put(i + 1, appendFreqMap.getOrDefault(i + 1, 0) + 1); - } else if (freqMap.getOrDefault(i + 1, 0) > 0 && freqMap.getOrDefault(i + 2, 0) > 0) { - freqMap.put(i + 1, freqMap.get(i + 1) - 1); - freqMap.put(i + 2, freqMap.get(i + 2) - 1); - appendFreqMap.put(i + 3, appendFreqMap.getOrDefault(i + 3, 0) + 1); - } else { - return false; - } - freqMap.put(i, freqMap.get(i) - 1); - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_66.java b/src/main/java/com/fishercoder/solutions/_66.java deleted file mode 100644 index 48a3f43884..0000000000 --- a/src/main/java/com/fishercoder/solutions/_66.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder.solutions; - -public class _66 { - - public static class Solution1 { - /** - * credit: https://leetcode.com/problems/plus-one/discuss/24082/My-Simple-Java-Solution - */ - public int[] plusOne(int[] digits) { - int len = digits.length; - for (int i = len - 1; i >= 0; i--) { - if (digits[i] < 9) { - digits[i]++; - return digits; - } - digits[i] = 0; - } - int[] newNumber = new int[len + 1]; - newNumber[0] = 1; - return newNumber; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_660.java b/src/main/java/com/fishercoder/solutions/_660.java deleted file mode 100644 index 85d2bc1e77..0000000000 --- a/src/main/java/com/fishercoder/solutions/_660.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 660. Remove 9 - * - * Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... - - So now, you will have a new integer sequence: 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ... - - Given a positive integer n, you need to return the n-th integer after removing. Note that 1 will be the first integer. - - Example 1: - Input: 9 - Output: 10 - - Hint: n will not exceed 9 x 10^8. - */ -public class _660 { - - public static class Solution1 { - public int newInteger(int n) { - return Integer.parseInt(Integer.toString(n, 9)); - } - } - - public static class Solution2 { - public int newInteger(int n) { - int result = 0; - int base = 1; - while (n > 0) { - result += n % 9 * base; - n /= 9; - base *= 10; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_661.java b/src/main/java/com/fishercoder/solutions/_661.java deleted file mode 100644 index 7815718d66..0000000000 --- a/src/main/java/com/fishercoder/solutions/_661.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 661. Image Smoother - * - * Given a 2D integer matrix m representing the gray scale of an image, - * you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of - * all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can. - - Example 1: - Input: - [[1,1,1], - [1,0,1], - [1,1,1]] - - Output: - [[0, 0, 0], - [0, 0, 0], - [0, 0, 0]] - - Explanation: - For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0 - For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0 - For the point (1,1): floor(8/9) = floor(0.88888889) = 0 - - Note: - The value in the given matrix is in the range of [0, 255]. - The length and width of the given matrix are in the range of [1, 150]. - - */ -public class _661 { - public static class Solution1 { - public int[][] imageSmoother(int[][] M) { - if (M == null || M.length == 0) { - return M; - } - int m = M.length; - int n = M[0].length; - int[][] result = new int[m][n]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - bfs(M, i, j, result, m, n); - } - } - return result; - } - - private void bfs(int[][] M, int i, int j, int[][] result, int m, int n) { - int sum = M[i][j]; - int denominator = 1; - if (j + 1 < n) { - sum += M[i][j + 1]; - denominator++; - } - if (i + 1 < m && j + 1 < n) { - sum += M[i + 1][j + 1]; - denominator++; - } - if (i + 1 < m) { - sum += M[i + 1][j]; - denominator++; - } - if (i + 1 < m && j - 1 >= 0) { - sum += M[i + 1][j - 1]; - denominator++; - } - if (j - 1 >= 0) { - sum += M[i][j - 1]; - denominator++; - } - if (i - 1 >= 0 && j - 1 >= 0) { - sum += M[i - 1][j - 1]; - denominator++; - } - if (i - 1 >= 0) { - sum += M[i - 1][j]; - denominator++; - } - if (i - 1 >= 0 && j + 1 < n) { - sum += M[i - 1][j + 1]; - denominator++; - } - result[i][j] = sum / denominator; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_662.java b/src/main/java/com/fishercoder/solutions/_662.java deleted file mode 100644 index c1910364e4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_662.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.AbstractMap; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; - -/** - * 662. Maximum Width of Binary Tree - * - * Given a binary tree, write a function to get the maximum width of the given tree. - * The width of a tree is the maximum width among all levels. - * The binary tree has the same structure as a full binary tree, but some nodes are null. - * The width of one level is defined as the length between the end-nodes - * (the leftmost and right most non-null nodes in the level, - * where the null nodes between the end-nodes are also counted into the length calculation. - - Example 1: - Input: - - 1 - / \ - 3 2 - / \ \ - 5 3 9 - - Output: 4 - Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9). - - Example 2: - Input: - - 1 - / - 3 - / \ - 5 3 - - Output: 2 - Explanation: The maximum width existing in the third level with the length 2 (5,3). - - Example 3: - Input: - - 1 - / \ - 3 2 - / - 5 - - Output: 2 - Explanation: The maximum width existing in the second level with the length 2 (3,2). - Example 4: - Input: - - 1 - / \ - 3 2 - / \ - 5 9 - / \ - 6 7 - - Output: 8 - Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7). - - Note: Answer will in the range of 32-bit signed integer. - */ -public class _662 { - public static class Solution1 { - /** - * Use a map to store the node to value map, - * we use root as index 1, then its left child is 2*i-1 and right child is 2*i - */ - public int widthOfBinaryTree(TreeNode root) { - if (root == null) { - return 0; - } - - Queue> queue = new LinkedList<>(); - queue.offer(new AbstractMap.SimpleEntry<>(root, 1)); - int max = 1; - while (!queue.isEmpty()) { - int size = queue.size(); - List> list = new ArrayList<>(); - for (int i = 0; i < size; i++) { - Map.Entry curr = queue.poll(); - if (curr.getKey().left != null) { - Map.Entry newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().left, curr.getValue() * 2 - 1); - queue.offer(newEntry); - list.add(newEntry); - } - if (curr.getKey().right != null) { - Map.Entry newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().right, curr.getValue() * 2); - queue.offer(newEntry); - list.add(newEntry); - } - } - if (list.size() > 1) { - max = Math.max(list.get(list.size() - 1).getValue() - list.get(0).getValue() + 1, max); - } - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_663.java b/src/main/java/com/fishercoder/solutions/_663.java deleted file mode 100644 index c7550d5fb9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_663.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.HashMap; -import java.util.Map; - -/** - * 663. Equal Tree Partition - * - * Given a binary tree with n nodes, - * your task is to check if it's possible to partition the tree to two trees which have the equal sum of values - * after removing exactly one edge on the original tree. - - Example 1: - Input: - 5 - / \ - 10 10 - / \ - 2 3 - - Output: True - Explanation: - 5 - / -10 - - Sum: 15 - - 10 - / \ -2 3 - - Sum: 15 - - - Example 2: - Input: - 1 - / \ - 2 10 - / \ - 2 20 - - Output: False - Explanation: You can't split the tree into two trees with equal sum after removing exactly one edge on the tree. - - Note: - The range of tree node value is in the range of [-100000, 100000]. - 1 <= n <= 10000 - - */ -public class _663 { - public static class Solution1 { - /** - * The idea is that we use a map to store the sum of each node, then in the end, - * we check if any node has a sum that is exactly half of total sum. - */ - public boolean checkEqualTree(TreeNode root) { - Map map = new HashMap<>(); - int totalSum = sumForEachNode(root, map); - if (totalSum % 2 != 0 || map.size() < 2) { - return false; - } - for (TreeNode key : map.keySet()) { - if (map.get(key) == totalSum / 2) { - return true; - } - } - return false; - } - - private int sumForEachNode(TreeNode root, Map map) { - if (root == null) { - return 0; - } - if (root.left == null && root.right == null) { - map.put(root, root.val); - return root.val; - } - int leftVal = 0; - if (root.left != null) { - leftVal = sumForEachNode(root.left, map); - } - int rightVal = 0; - if (root.right != null) { - rightVal = sumForEachNode(root.right, map); - } - int val = root.val + leftVal + rightVal; - map.put(root, val); - return val; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_664.java b/src/main/java/com/fishercoder/solutions/_664.java deleted file mode 100644 index 416676b374..0000000000 --- a/src/main/java/com/fishercoder/solutions/_664.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 664. Strange Printer - * - * There is a strange printer with the following two special requirements: - * The printer can only print a sequence of the same character each time. - * At each turn, the printer can print new characters starting from and ending at any places, and will cover the original existing characters. - * Given a string consists of lower English letters only, your job is to count the minimum number of turns the printer needed in order to print it. - - Example 1: - Input: "aaabbb" - Output: 2 - Explanation: Print "aaa" first and then print "bbb". - - Example 2: - Input: "aba" - Output: 2 - Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'. - - Hint: Length of the given string will not exceed 100. - */ -public class _664 { - public static class Solution1 { - /** - * reference: https://discuss.leetcode.com/topic/100137/java-solution-dp - */ - public int strangePrinter(String s) { - int n = s.length(); - if (n == 0) { - return 0; - } - - int[][] dp = new int[101][101]; - for (int i = 0; i < n; i++) { - dp[i][i] = 1; - } - - for (int i = 1; i < n; i++) { - for (int j = 0; j < n - i; j++) { - dp[j][j + i] = i + 1; - for (int k = j + 1; k <= j + i; k++) { - int temp = dp[j][k - 1] + dp[k][j + i]; - if (s.charAt(k - 1) == s.charAt(j + i)) { - temp--; - } - dp[j][j + i] = Math.min(dp[j][j + i], temp); - } - } - } - return dp[0][n - 1]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_666.java b/src/main/java/com/fishercoder/solutions/_666.java deleted file mode 100644 index 281b46be01..0000000000 --- a/src/main/java/com/fishercoder/solutions/_666.java +++ /dev/null @@ -1,165 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.HashMap; -import java.util.Map; - -/** - * 666. Path Sum IV - * If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digits integers. - - For each integer in this list: - - The hundreds digit represents the depth D of this node, 1 <= D <= 4. - - The tens digit represents the position P of this node in the level it belongs to, 1 <= P <= 8. - The position is the same as that in a full binary tree. - - The units digit represents the value V of this node, 0 <= V <= 9. - - Given a list of ascending three-digits integers representing a binary with the depth smaller than 5. - You need to return the totalSum of all paths from the root towards the leaves. - - Example 1: - - Input: [113, 215, 221] - Output: 12 - Explanation: - The tree that the list represents is: - 3 - / \ - 5 1 - - The path totalSum is (3 + 5) + (3 + 1) = 12. - - Example 2: - - Input: [113, 221] - Output: 4 - Explanation: - The tree that the list represents is: - 3 - \ - 1 - - The path totalSum is (3 + 1) = 4. - - */ -public class _666 { - public static class Solution1 { - /**OMG, since it's no larger than depth 5, I've got a hardcoded solution here.... - * By "harcoded", I mean the constructTree() method.*/ - public int totalSum = 0; - - public int pathSum(int[] nums) { - TreeNode root = constructTree(nums); - if (root == null) { - return 0; - } - computePathSum(root, 0); - return totalSum; - } - - private void computePathSum(TreeNode root, int pathSum) { - pathSum += root.val; - if (root.left != null) { - computePathSum(root.left, pathSum); - } - if (root.right != null) { - computePathSum(root.right, pathSum); - } - if (root.left == null && root.right == null) { - totalSum += pathSum; - } -// pathSum -= root.val; - /**this line is not necessary as I'm passing pathSum as a local variable around, so it's always updated - it's AC'ed with or without this line*/ - } - - private TreeNode constructTree(int[] nums) { - if (nums == null || nums.length == 0) { - return null; - } - TreeNode root = new TreeNode(Integer.parseInt(Integer.toString(nums[0]).substring(2, 3))); - //depth 2 - for (int i = 1; i < nums.length; i++) { - if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) { - root.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) { - root.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } - } - - //depth 3 - for (int i = 2; i < nums.length; i++) { - if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) { - root.left.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) { - root.left.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) { - root.right.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) { - root.right.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } - } - - //depth 4 - for (int i = 3; i < nums.length; i++) { - if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) { - root.left.left.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) { - root.left.left.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) { - root.left.right.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) { - root.left.right.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 5) { - root.right.left.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 6) { - root.right.left.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 7) { - root.right.right.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 8) { - root.right.right.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); - } - } - - return root; - } - } - - public static class Solution2 { - public int totalSum = 0; - - public int pathSum(int[] nums) { - Map map = new HashMap<>(); - for (int i = 0; i < nums.length; i++) { - int key = nums[i] / 10; - int value = nums[i] % 10; - map.put(key, value); - } - dfs(nums[0] / 10, 0, map); - return totalSum; - } - - private void dfs(int node, int preSum, Map map) { - int level = node / 10; - int pos = node % 10; - int leftChild = (level + 1) * 10 + pos * 2 - 1; - int rightChild = (level + 1) * 10 + pos * 2; - int currSum = preSum + map.get(node); - if (!map.containsKey(leftChild) && !map.containsKey(rightChild)) { - totalSum += currSum; - return; - } - - if (map.containsKey(leftChild)) { - dfs(leftChild, currSum, map); - } - if (map.containsKey(rightChild)) { - dfs(rightChild, currSum, map); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_667.java b/src/main/java/com/fishercoder/solutions/_667.java deleted file mode 100644 index 1a57c80b72..0000000000 --- a/src/main/java/com/fishercoder/solutions/_667.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 667. Beautiful Arrangement II - * - * Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n - * and obeys the following requirement: - * Suppose this list is [a1, a2, a3, ... , an], - * then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers. - * If there are multiple answers, print any of them. - - Example 1: - - Input: n = 3, k = 1 - Output: [1, 2, 3] - Explanation: The [1, 2, 3] has three different positive integers ranging from 1 to 3, and the [1, 1] has exactly 1 distinct integer: 1. - - Example 2: - - Input: n = 3, k = 2 - Output: [1, 3, 2] - Explanation: The [1, 3, 2] has three different positive integers ranging from 1 to 3, and the [2, 1] has exactly 2 distinct integers: 1 and 2. - - Note: - - The n and k are in the range 1 <= k < n <= 104. - */ - -public class _667 { - - public static class Solutoin1 { - /**This brute force solution will result in TLE as soon as n = 10 and k = 4.*/ - public int[] constructArray(int n, int k) { - List> allPermutaions = findAllPermutations(n); - int[] result = new int[n]; - for (List perm : allPermutaions) { - if (isBeautifulArrangement(perm, k)) { - convertListToArray(result, perm); - break; - } - } - return result; - } - - private void convertListToArray(int[] result, List perm) { - for (int i = 0; i < perm.size(); i++) { - result[i] = perm.get(i); - } - } - - private boolean isBeautifulArrangement(List perm, int k) { - Set diff = new HashSet<>(); - for (int i = 0; i < perm.size() - 1; i++) { - diff.add(Math.abs(perm.get(i) - perm.get(i + 1))); - } - return diff.size() == k; - } - - private List> findAllPermutations(int n) { - List> result = new ArrayList<>(); - backtracking(new ArrayList<>(), result, n); - return result; - } - - private void backtracking(List list, List> result, int n) { - if (list.size() == n) { - result.add(new ArrayList<>(list)); - return; - } - for (int i = 1; i <= n; i++) { - if (list.contains(i)) { - continue; - } - list.add(i); - backtracking(list, result, n); - list.remove(list.size() - 1); - } - } - } - - public static class Solutoin2 { - /**This is a very smart solution: - * First, we can see that the max value k could reach is n-1 which - * comes from a sequence like this: - * when n = 8, k = 5, one possible sequence is: - * 1, 8, 2, 7, 3, 4, 5, 6 - * absolute diffs are: - * 7, 6, 5, 4, 1, 1, 1 - * so, there are total 5 distinct integers. - * - * So, we can just form such a sequence by putting the first part first and - * decrement k along the way, when k becomes 1, we just put the rest numbers in order.*/ - public int[] constructArray(int n, int k) { - int[] result = new int[n]; - int left = 1; - int right = n; - for (int i = 0; i < n && left <= right; i++) { - if (k > 1) { - result[i] = k-- % 2 != 0 ? left++ : right--; - } else { - result[i] = k % 2 != 0 ? left++ : right--; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_668.java b/src/main/java/com/fishercoder/solutions/_668.java deleted file mode 100644 index faabfbc5c5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_668.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.PriorityQueue; - -/** - * 668. Kth Smallest Number in Multiplication Table - * - * Nearly every one have used the Multiplication Table. - * But could you find out the k-th smallest number quickly from the multiplication table? - * Given the height m and the length n of a m * n Multiplication Table, - * and a positive integer k, you need to return the k-th smallest number in this table. - - Example 1: - Input: m = 3, n = 3, k = 5 - Output: - Explanation: - The Multiplication Table: - 1 2 3 - 2 4 6 - 3 6 9 - - The 5-th smallest number is 3 (1, 2, 2, 3, 3). - - - Example 2: - Input: m = 2, n = 3, k = 6 - Output: - Explanation: - The Multiplication Table: - 1 2 3 - 2 4 6 - - The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6). - - Note: - - The m and n will be in the range [1, 30000]. - The k will be in the range [1, m * n] - */ - -public class _668 { - public static class Solution1 { - /** - * This brute force approach resulted in - * TLE on Leetcode and - * OOM error by _668test.test3() when running in my localhost: - * java.lang.OutOfMemoryError: Java heap space - * at java.util.Arrays.copyOf(Arrays.java:3210) - * at java.util.Arrays.copyOf(Arrays.java:3181) - * at java.util.PriorityQueue.grow(PriorityQueue.java:300) - * at java.util.PriorityQueue.offer(PriorityQueue.java:339) - */ - public int findKthNumber(int m, int n, int k) { - PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a - b); - for (int i = 1; i <= m; i++) { - for (int j = 1; j <= n; j++) { - minHeap.offer(i * j); - } - } - while (k-- > 1) { - minHeap.poll(); - } - return minHeap.peek(); - } - } - - public static class Solution2 { - /**reference: https://discuss.leetcode.com/topic/101132/java-solution-binary-search*/ - public int findKthNumber(int m, int n, int k) { - int low = 1; - int high = m * n + 1; - while (low < high) { - int mid = low + (high - low) / 2; - int c = count(mid, m, n); - if (c >= k) { - high = mid; - } else { - low = mid + 1; - } - } - return high; - } - - int count(int v, int m, int n) { - int count = 0; - for (int i = 1; i <= m; i++) { - int temp = Math.min(v / i, n); - count += temp; - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_669.java b/src/main/java/com/fishercoder/solutions/_669.java deleted file mode 100644 index 38712dde5c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_669.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 669. Trim a Binary Search Tree - * - * Given a binary search tree and the lowest and highest boundaries as L and R, - * trim the tree so that all its elements lies in [L, R] (R >= L). - * You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree. - - Example 1: - - Input: - 1 - / \ - 0 2 - - L = 1 - R = 2 - - Output: - 1 - \ - 2 - - Example 2: - - Input: - 3 - / \ -0 4 - \ - 2 - / -1 - - L = 1 - R = 3 - - Output: - 3 - / - 2 - / -1 - - */ -public class _669 { - public static class Solution1 { - public TreeNode trimBST(TreeNode root, int L, int R) { - if (root == null) { - return root; - } - - if (root.val > R) { - return trimBST(root.left, L, R); - } - - if (root.val < L) { - return trimBST(root.right, L, R); - } - - root.left = trimBST(root.left, L, R); - root.right = trimBST(root.right, L, R); - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_670.java b/src/main/java/com/fishercoder/solutions/_670.java deleted file mode 100644 index 53d2562ea6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_670.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 670. Maximum Swap - * - * Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. - * Return the maximum valued number you could get. - - Example 1: - Input: 2736 - Output: 7236 - Explanation: Swap the number 2 and the number 7. - - Example 2: - Input: 9973 - Output: 9973 - Explanation: No swap. - - Note: - The given number is in the range [0, 108] - - */ -public class _670 { - public static class Solution1 { - public int maximumSwap(int num) { - String numStr = String.valueOf(num); - int max = num; - for (int i = 0; i < numStr.length() - 1; i++) { - for (int j = i + 1; j < numStr.length(); j++) { - if (numStr.charAt(i) < numStr.charAt(j)) { - StringBuilder sb = new StringBuilder(numStr); - sb.replace(i, i + 1, String.valueOf(numStr.charAt(j))); - sb.replace(j, j + 1, String.valueOf(numStr.charAt(i))); - max = Math.max(max, Integer.parseInt(sb.toString())); - } - } - } - return max; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_671.java b/src/main/java/com/fishercoder/solutions/_671.java deleted file mode 100644 index a4f36f2652..0000000000 --- a/src/main/java/com/fishercoder/solutions/_671.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.Iterator; -import java.util.Set; -import java.util.TreeSet; - -/** - * 671. Second Minimum Node In a Binary Tree - * - * Given a non-empty special binary tree consisting of nodes with the non-negative value, - * where each node in this tree has exactly two or zero sub-node. - * If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. - * Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree. - * If no such second minimum value exists, output -1 instead. - - Example 1: - - Input: - 2 - / \ - 2 5 - / \ - 5 7 - - Output: 5 - Explanation: The smallest value is 2, the second smallest value is 5. - - Example 2: - - Input: - 2 - / \ -2 2 - - Output: -1 - Explanation: The smallest value is 2, but there isn't any second smallest value. - - */ -public class _671 { - public static class Solution1 { - public int findSecondMinimumValue(TreeNode root) { - if (root == null) { - return -1; - } - Set set = new TreeSet<>(); - dfs(root, set); - Iterator iterator = set.iterator(); - int count = 0; - while (iterator.hasNext()) { - count++; - int result = iterator.next(); - if (count == 2) { - return result; - } - } - return -1; - } - - private void dfs(TreeNode root, Set set) { - if (root == null) { - return; - } - set.add(root.val); - dfs(root.left, set); - dfs(root.right, set); - return; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_672.java b/src/main/java/com/fishercoder/solutions/_672.java deleted file mode 100644 index a64f319e5a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_672.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.utils.CommonUtils; - -/** - * 672. Bulb Switcher II - * There is a room with n lights which are turned on initially and 4 buttons on the wall. - * After performing exactly m unknown operations towards buttons, - * you need to return how many different kinds of status of the n lights could be. - * Suppose n lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are given below: - - Flip all the lights. - Flip lights with even numbers. - Flip lights with odd numbers. - Flip lights with (3k + 1) numbers, k = 0, 1, 2, ... - - Example 1: - - Input: n = 1, m = 1. - Output: 2 - Explanation: Status can be: [on], [off] - - Example 2: - - Input: n = 2, m = 1. - Output: 3 - Explanation: Status can be: [on, off], [off, on], [off, off] - - Example 3: - - Input: n = 3, m = 1. - Output: 4 - Explanation: Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on]. - - Note: n and m both fit in range [0, 1000]. - */ -public class _672 { - public static class Solution1 { - public int flipLights(int n, int m) { - if (m < 1) { - return 1; - } - int[][] dp = new int[m][n]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (j == 0) { - dp[i][j] = 2; - } else if (i == 0 && j == 1) { - dp[i][j] = 3; - } else if (i == 0) { - dp[i][j] = 4; - } else if (i == 1 && j == 1) { - dp[i][j] = 4; - } else if (i == 1 && j > 1) { - dp[i][j] = 7; - } else if (j == 1) { - dp[i][j] = 4; - } else if (i == 1) { - dp[i][j] = 7; - } else { - dp[i][j] = 8; - } - } - } - CommonUtils.print2DIntArray(dp); - return dp[m - 1][n - 1]; - } - } - - public static class Solution2 { - public int flipLights(int n, int m) { - if (n == 1 && m > 0) { - return 2; - } else if (n == 2 && m == 1) { - return 3; - } else if ((n > 2 && m == 1) || (n == 2 && m > 1)) { - return 4; - } else if (n > 2 && m == 2) { - return 7; - } else if (n > 2 && m > 2) { - return 8; - } else { - return 1; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_673.java b/src/main/java/com/fishercoder/solutions/_673.java deleted file mode 100644 index b8cc27f7bf..0000000000 --- a/src/main/java/com/fishercoder/solutions/_673.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 673. Number of Longest Increasing Subsequence - * Given an unsorted array of integers, find the number of longest increasing subsequence. - - Example 1: - - Input: [1,3,5,4,7] - Output: 2 - Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. - - Example 2: - - Input: [2,2,2,2,2] - Output: 5 - Explanation: The length of longest continuous increasing subsequence is 1, - and there are 5 subsequences' length is 1, so output 5. - - Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int. - */ -public class _673 { - public static class Solution1 { - /** - * Reference: https://discuss.leetcode.com/topic/103020/java-c-simple-dp-solution-with-explanation - */ - public int findNumberOfLIS(int[] nums) { - int n = nums.length; - int[] cnt = new int[n]; - int[] len = new int[n]; - int max = 0; - int count = 0; - for (int i = 0; i < n; i++) { - len[i] = cnt[i] = 1; - for (int j = 0; j < i; j++) { - if (nums[i] > nums[j]) { - if (len[i] == len[j] + 1) { - cnt[i] += cnt[j]; - } - if (len[i] < len[j] + 1) { - len[i] = len[j] + 1; - cnt[i] = cnt[j]; - } - } - } - - if (max == len[i]) { - count += cnt[i]; - } - - if (len[i] > max) { - max = len[i]; - count = cnt[i]; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_674.java b/src/main/java/com/fishercoder/solutions/_674.java deleted file mode 100644 index 5a23402454..0000000000 --- a/src/main/java/com/fishercoder/solutions/_674.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 674. Longest Continuous Increasing Subsequence - * Given an unsorted array of integers, find the length of longest continuous increasing subsequence. - - Example 1: - Input: [1,3,5,4,7] - Output: 3 - Explanation: The longest continuous increasing subsequence is [1,3,5], - its length is 3. Even though [1,3,5,7] is also an increasing subsequence, - it's not a continuous one where 5 and 7 are separated by 4. - - Example 2: - Input: [2,2,2,2,2] - Output: 1 - Explanation: The longest continuous increasing subsequence is [2], its length is 1. - - Note: Length of the array will not exceed 10,000. - */ -public class _674 { - public static class Solution1 { - public int findLengthOfLCIS(int[] nums) { - int longest = 0; - for (int i = 0; i < nums.length; i++) { - int len = 1; - for (int j = i + 1; j < nums.length; j++) { - if (nums[j - 1] < nums[j]) { - len++; - continue; - } else { - break; - } - } - longest = Math.max(longest, len); - } - return longest; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_675.java b/src/main/java/com/fishercoder/solutions/_675.java deleted file mode 100644 index f97286dae5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_675.java +++ /dev/null @@ -1,140 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.List; -import java.util.PriorityQueue; -import java.util.Queue; - -/** - * 675. Cut Off Trees for Golf Event - * - * You are asked to cut off trees in a forest for a golf event. - * The forest is represented as a non-negative 2D map, in this map: - * 0 represents the obstacle can't be reached. - * 1 represents the ground can be walked through. - * - * The place with number bigger than 1 represents a tree can be walked through, - * and this positive number represents the tree's height. - * - * You are asked to cut off all the trees in this forest in the order of tree's height - - * always cut off the tree with lowest height first. - * And after cutting, the original place has the tree will become a grass (value 1). - * - * You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees. - * - * If you can't cut off all the trees, output -1 in that situation. - * You are guaranteed that no two trees have the same height and there is at least one tree needs to be cut off. - * - * Example 1: - * Input: - * [ - * [1,2,3], - * [0,0,4], - * [7,6,5] - * ] - * Output: 6 - * - * Example 2: - * Input: - * [ - * [1,2,3], - * [0,0,0], - * [7,6,5] - * ] - * Output: -1 - * - * Example 3: - * Input: - * [ - * [2,3,4], - * [0,0,5], - * [8,7,6] - * ] - * Output: 6 - * - * Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking. - * Hint: size of the given matrix will not exceed 50x50. - */ - -public class _675 { - public static class Solution1 { - public int cutOffTree(List> forest) { - if (forest == null || forest.isEmpty() || forest.size() == 0 || forest.get(0).get(0) == 0) { - return -1; - } - int m = forest.size(); - int n = forest.get(0).size(); - /**cut trees in ascending order*/ - PriorityQueue heap = new PriorityQueue<>((a, b) -> a.height - b.height); - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (forest.get(i).get(j) > 1) { - /**This is important: we'll add trees that are only taller than 1!!!*/ - heap.offer(new Tree(i, j, forest.get(i).get(j))); - } - } - } - - int sum = 0; - Tree start = new Tree(); - while (!heap.isEmpty()) { - Tree curr = heap.poll(); - int step = bfs(forest, curr, start, m, n); - if (step == -1) { - return -1; - } - sum += step; - start = curr; - } - return sum; - } - - private int bfs(List> forest, Tree target, Tree start, int m, int n) { - int[] dirs = new int[]{0, 1, 0, -1, 0}; - boolean[][] visited = new boolean[m][n]; - Queue queue = new LinkedList<>(); - queue.offer(start); - visited[start.x][start.y] = true; - int step = 0; - while (!queue.isEmpty()) { - int size = queue.size(); - for (int k = 0; k < size; k++) { - Tree tree = queue.poll(); - if (tree.x == target.x && tree.y == target.y) { - return step; - } - - for (int i = 0; i < 4; i++) { - int nextX = tree.x + dirs[i]; - int nextY = tree.y + dirs[i + 1]; - if (nextX < 0 || nextY < 0 || nextX >= m || nextY >= n || visited[nextX][nextY] || forest.get(nextX).get(nextY) == 0) { - continue; - } - queue.offer(new Tree(nextX, nextY, forest.get(nextX).get(nextY))); - visited[nextX][nextY] = true; - } - } - step++; - } - return -1; - } - - class Tree { - int x; - int y; - int height; - - public Tree(int x, int y, int height) { - this.x = x; - this.y = y; - this.height = height; - } - - public Tree() { - this.x = 0; - this.y = 0; - this.height = 0; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_676.java b/src/main/java/com/fishercoder/solutions/_676.java deleted file mode 100644 index 887ecb7321..0000000000 --- a/src/main/java/com/fishercoder/solutions/_676.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 676. Implement Magic Dictionary - * Implement a magic directory with buildDict, and search methods. - * For the method buildDict, you'll be given a list of non-repetitive words to build a dictionary. - * For the method search, you'll be given a word, - * and judge whether if you modify exactly one character into another character in this word, - * the modified word is in the dictionary you just built. - - Example 1: - - Input: buildDict(["hello", "leetcode"]), Output: Null - Input: search("hello"), Output: False - Input: search("hhllo"), Output: True - Input: search("hell"), Output: False - Input: search("leetcoded"), Output: False - - Note: - - You may assume that all the inputs are consist of lowercase letters a-z. - For contest purpose, the test data is rather small by now. - You could think about highly efficient algorithm after the contest. - Please remember to RESET your class variables declared in class MagicDictionary, - as static/class variables are persisted across multiple test cases. Please see here for more details. - - */ -public class _676 { - - public static class Solution1 { - public static class MagicDictionary { - - Set wordSet; - - /** - * Initialize your data structure here. - */ - public MagicDictionary() { - wordSet = new HashSet<>(); - } - - /** - * Build a dictionary through a list of words - */ - public void buildDict(String[] dict) { - for (String word : dict) { - wordSet.add(word); - } - } - - /** - * Returns if there is any word in the trie that equals to the given word after modifying exactly one character - */ - public boolean search(String word) { - for (String candidate : wordSet) { - if (modifyOneChar(word, candidate)) { - return true; - } - } - return false; - } - - private boolean modifyOneChar(String word, String candidate) { - if (word.length() != candidate.length()) { - return false; - } - int diff = 0; - for (int i = 0; i < word.length(); i++) { - if (word.charAt(i) != candidate.charAt(i)) { - diff++; - } - if (diff > 1) { - return false; - } - } - return diff == 1; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_677.java b/src/main/java/com/fishercoder/solutions/_677.java deleted file mode 100644 index ed15c3eaa0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_677.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 677. Map Sum Pairs - * - * Implement a MapSum class with insert, and sum methods. - * - * For the method insert, you'll be given a pair of (string, integer). - * The string represents the key and the integer represents the value. - * If the key already existed, then the original key-value pair will be overridden to the new one. - * - * For the method sum, you'll be given a string representing the prefix, - * and you need to return the sum of all the pairs' value whose key starts with the prefix. - * - * Example 1: - * Input: insert("apple", 3), Output: Null - * Input: sum("ap"), Output: 3 - * Input: insert("app", 2), Output: Null - * Input: sum("ap"), Output: 5 - */ -public class _677 { - public static class Solution1 { - - public static class MapSum { - - Map map; - - /** - * Initialize your data structure here. - */ - public MapSum() { - map = new HashMap<>(); - } - - public void insert(String key, int val) { - map.put(key, val); - } - - public int sum(String prefix) { - int sum = 0; - for (String key : map.keySet()) { - if (key.startsWith(prefix)) { - sum += map.get(key); - } - } - return sum; - } - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_679.java b/src/main/java/com/fishercoder/solutions/_679.java deleted file mode 100644 index 102a58f205..0000000000 --- a/src/main/java/com/fishercoder/solutions/_679.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.stream.IntStream; - -/** - * 679. 24 Game - * - * You have 4 cards each containing a number from 1 to 9. - * You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24. - - Example 1: - Input: [4, 1, 8, 7] - Output: True - Explanation: (8-4) * (7-1) = 24 - - Example 2: - Input: [1, 2, 1, 2] - Output: False - - Note: - The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12. - Every operation done is between two numbers. - In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed. - You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12. - - */ - -public class _679 { - public static class Solution1 { - /**Since there are only 4 cards and only 4 operations, we can iterate through all possible combinations, there's a total of 9216 possibilities: - * 1. we pick two out of four cards, with order (since order matters for division), 4 * 3 = 12, then pick one of four operations: 12 * 4 = 48; - * 2. then we pick two from these three numbers: 12 * 4 * 3 * 4 * 2 = 1152 - * 3. then we pick the remaining two: 1152 * 2 * 4 = 9216 (with order and out of 4 operations)*/ - public boolean judgePoint24(int[] nums) { - return dfs(IntStream.of(nums).mapToDouble(num -> num).toArray()); - } - - private boolean dfs(double[] nums) { - if (nums.length == 1) { - return Math.abs(nums[0] - 24) < 1e-8;//1e-8 means 0.000000001, i.e. 10^(-8) - } - - for (int i = 0; i < nums.length; i++) { - for (int j = 0; j < nums.length; j++) { - if (i != j) { - int len = 0; - double[] a = new double[nums.length - 1]; - for (int k = 0; k < nums.length; k++) { - if (k != i && k != j) { - a[len++] = nums[k]; - } - } - - a[len] = nums[i] + nums[j]; - if (dfs(a)) { - return true; - } - - a[len] = nums[i] - nums[j]; - if (dfs(a)) { - return true; - } - - a[len] = nums[i] * nums[j]; - if (dfs(a)) { - return true; - } - - if (nums[j] > 1e-8) { - a[len] = nums[i] / nums[j]; - if (dfs(a)) { - return true; - } - } - } - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_681.java b/src/main/java/com/fishercoder/solutions/_681.java deleted file mode 100644 index a4f0e26d33..0000000000 --- a/src/main/java/com/fishercoder/solutions/_681.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 681. Next Closest Time - * - * Given a time represented in the format "HH:MM", - * form the next closest time by reusing the current digits. - * There is no limit on how many times a digit can be reused. - * You may assume the given input string is always valid. - * For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid. - - Example 1: - - Input: "19:34" - Output: "19:39" - Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, - which occurs 5 minutes later. It is not 19:33, because this occurs 23 hours and 59 minutes later. - - Example 2: - - Input: "23:59" - Output: "22:22" - Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. - It may be assumed that the returned time is next day's time since it is smaller than the input time numerically. - - */ -public class _681 { - public static class Solution1 { - public String nextClosestTime(String time) { - int cur = 60 * Integer.parseInt(time.substring(0, 2)); - cur += Integer.parseInt(time.substring(3)); - Set allowed = new HashSet(); - for (char c : time.toCharArray()) { - if (c != ':') { - allowed.add(c - '0'); - } - } - - while (true) { - cur = (cur + 1) % (24 * 60); - int[] digits = new int[]{cur / 60 / 10, cur / 60 % 10, cur % 60 / 10, cur % 60 % 10}; - search: - { - for (int d : digits) { - if (!allowed.contains(d)) { - break search; - } - } - return String.format("%02d:%02d", cur / 60, cur % 60); - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_682.java b/src/main/java/com/fishercoder/solutions/_682.java deleted file mode 100644 index d193c98e1e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_682.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 682. Baseball Game - * - * You're now a baseball game point recorder. - - Given a list of strings, each string can be one of the 4 following types: - - Integer (one round's score): Directly represents the number of points you get in this round. - "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points. - "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points. - "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed. - - Each round's operation is permanent and could have an impact on the round before and the round after. - - You need to return the sum of the points you could get in all the rounds. - - Example 1: - - Input: ["5","2","C","D","+"] - Output: 15 - Explanation: - Round 1: You could get 5 points. The sum is: 5. - Round 2: You could get 2 points. The sum is: 7. - Operation 1: The round 2's data was invalid. The sum is: 5. - Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15. - Round 4: You could get 5 + 10 = 15 points. The sum is: 30. - - Example 2: - - Input: ["5","-2","4","C","D","9","+","+"] - Output: 27 - Explanation: - Round 1: You could get 5 points. The sum is: 5. - Round 2: You could get -2 points. The sum is: 3. - Round 3: You could get 4 points. The sum is: 7. - Operation 1: The round 3's data is invalid. The sum is: 3. - Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1. - Round 5: You could get 9 points. The sum is: 8. - Round 6: You could get -4 + 9 = 5 points. The sum is 13. - Round 7: You could get 9 + 5 = 14 points. The sum is 27. - - Note: - The size of the input list will be between 1 and 1000. - Every integer represented in the list will be between -30000 and 30000. - */ -public class _682 { - - public static class Solution1 { - public int calPoints(String[] ops) { - Stack stack = new Stack<>(); - int sum = 0; - int firstLast = Integer.MIN_VALUE; - int secondLast = Integer.MIN_VALUE; - for (String op : ops) { - if (op.equals("+")) { - if (!stack.isEmpty()) { - firstLast = stack.pop(); - } - if (!stack.isEmpty()) { - secondLast = stack.pop(); - } - int thisRoundPoints = firstLast + secondLast; - - if (secondLast != Integer.MIN_VALUE) { - stack.push(secondLast); - } - if (firstLast != Integer.MIN_VALUE) { - stack.push(firstLast); - } - stack.push(thisRoundPoints); - sum += thisRoundPoints; - - firstLast = Integer.MIN_VALUE; - secondLast = Integer.MIN_VALUE; - } else if (op.equals("D")) { - if (!stack.isEmpty()) { - int thisRoundPoints = stack.peek() * 2; - stack.push(thisRoundPoints); - sum += thisRoundPoints; - } - } else if (op.equals("C")) { - if (!stack.isEmpty()) { - int removedData = stack.pop(); - sum -= removedData; - } - } else { - Integer val = Integer.parseInt(op); - sum += val; - stack.push(val); - } - } - return sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_683.java b/src/main/java/com/fishercoder/solutions/_683.java deleted file mode 100644 index 89cd7ca36a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_683.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 683. K Empty Slots - * - * There is a garden with N slots. In each slot, there is a flower. - * - * The N flowers will bloom one by one in N days. - * - * In each day, there will be exactly one flower blooming and it will be in the status of blooming since then. - * - * Given an array flowers consists of number from 1 to N. Each number in the array represents the place where the flower will open in that day. - * For example, flowers[i] = x means that the unique flower that blooms at day i will be at position x, - * where i and x will be in the range from 1 to N. - * - * Also given an integer k, you need to output in which day there exists two flowers in the status of blooming, - * and also the number of flowers between them is k and these flowers are not blooming. - * If there isn't such day, output -1. - - Example 1: - Input: - flowers: [1,3,2] - k: 1 - Output: 2 - Explanation: In the second day, the first and the third flower have become blooming. - - Example 2: - Input: - flowers: [1,2,3] - k: 1 - Output: -1 - - Note: - The given array will be in the range [1, 20000]. - - */ -public class _683 { - - public static class Solution1 { - /**credit: https://discuss.leetcode.com/topic/104771/java-c-simple-o-n-solution*/ - public int kEmptySlots(int[] flowers, int k) { - int[] days = new int[flowers.length]; - for (int i = 0; i < flowers.length; i++) { - days[flowers[i] - 1] = i + 1; - } - int left = 0; - int right = k + 1; - int result = Integer.MAX_VALUE; - for (int i = 0; right < flowers.length; i++) { - if (days[i] < days[left] || days[i] <= days[right]) { - if (i == right) { - result = Math.min(result, Math.max(days[left], days[right])); - } - left = i; - right = k + 1 + i; - } - } - return result == Integer.MAX_VALUE ? -1 : result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_684.java b/src/main/java/com/fishercoder/solutions/_684.java deleted file mode 100644 index 830efa20b9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_684.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 684. Redundant Connection - * - * We are given a "tree" in the form of a 2D-array, with distinct values for each node. - * In the given 2D-array, each element pair [u, v] represents that v is a child of u in the tree. - * We can remove exactly one redundant pair in this "tree" to make the result a (rooted) tree. - * You need to find and output such a pair. If there are multiple answers for this question, output the one appearing last in the 2D-array. There is always at least one answer. - - Example 1: - Input: [[1,2], [1,3], [2,3]] - Output: [2,3] - Explanation: Original tree will be like this: - 1 - / \ - 2 - 3 - - - Example 2: - Input: [[1,2], [1,3], [3,1]] - Output: [3,1] - Explanation: Original tree will be like this: - 1 - / \\ - 2 3 - - Note: - The size of the input 2D-array will be between 1 and 1000. - Every integer represented in the 2D-array will be between 1 and 2000. - */ -public class _684 { - - public static class Solution1 { - /** - * This is my original solution. A little verbose. - */ - class UnionFind { - int[] ids; - Set nodes; - Set visitedNodes; - int[] redundantConn; - int m; - int n; - - public UnionFind(int[][] edges) { - m = edges.length; - n = edges[0].length; - nodes = new HashSet<>(); - visitedNodes = new HashSet<>(); - redundantConn = new int[]{}; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - nodes.add(edges[i][j]); - } - } - ids = new int[nodes.size()]; - for (int i = 0; i < ids.length; i++) { - ids[i] = i + 1; - } - } - - public int[] union(int[] edge) { - int x = find(edge[0] - 1); - int y = find(edge[1] - 1); - - if (x == y && visitedNodes.contains(edge[0]) && visitedNodes.contains(edge[1])) { - redundantConn = edge; - } - - if (x != y) { - if (x < y) { - ids[y] = x + 1; - } else { - ids[x] = y + 1; - } - } - - visitedNodes.add(edge[0]); - visitedNodes.add(edge[1]); - return redundantConn; - } - - private int find(int id) { - if (isTree()) { - return ids[id]; - } - if ((id + 1) != ids[id]) { - return find(ids[id] - 1); - } - return id; - } - - private boolean isTree() { - Set set = new HashSet<>(); - for (int i : ids) { - set.add(i); - } - return set.size() == 1; - } - } - - public int[] findRedundantConnection(int[][] edges) { - UnionFind unionFind = new UnionFind(edges); - int[] result = new int[]{}; - for (int[] edge : edges) { - result = unionFind.union(edge); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_686.java b/src/main/java/com/fishercoder/solutions/_686.java deleted file mode 100644 index 8ecb6ec833..0000000000 --- a/src/main/java/com/fishercoder/solutions/_686.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 686. Repeated String Match - * - * Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. - * If no such solution, return -1. - * For example, with A = "abcd" and B = "cdabcdab". - * Return 3, because by repeating A three times (“abcdabcdabcd”), - * B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd"). - - Note: - The length of A and B will be between 1 and 10000. - */ - -public class _686 { - public static class Solution1 { - public int repeatedStringMatch(String A, String B) { - Set set = new HashSet<>(); - for (char c : A.toCharArray()) { - set.add(c); - } - for (char c : B.toCharArray()) { - if (!set.contains(c)) { - return -1; - } - } - StringBuilder stringBuilder = new StringBuilder(A); - for (int i = 0; i < B.length(); i++) { - if (stringBuilder.toString().contains(B)) { - return i + 1; - } - stringBuilder.append(A); - } - return -1; - } - } - - public static class Solution2 { - /** - * Time: O(N(N+M)) - * Space: O(N + M) - * */ - public int repeatedStringMatch(String A, String B) { - int count = 1; - StringBuilder sb = new StringBuilder(A); - for (; sb.length() < B.length(); count++) { - sb.append(A); - } - if (sb.indexOf(B) >= 0) { - return count; - } - sb.append(A); - if (sb.indexOf(B) >= 0) { - return count + 1; - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_687.java b/src/main/java/com/fishercoder/solutions/_687.java deleted file mode 100644 index fdefefa727..0000000000 --- a/src/main/java/com/fishercoder/solutions/_687.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 687. Longest Univalue Path - * - * Given a binary tree, find the length of the longest path where each node in the path has the same value. - * This path may or may not pass through the root. - - Note: The length of path between two nodes is represented by the number of edges between them. - - Example 1: - Input: - 5 - / \ - 4 5 - / \ \ - 1 1 5 - - Output: - 2 - - Example 2: - Input: - - 1 - / \ - 4 5 - / \ \ - 4 4 5 - Output: - 2 - - Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000. - */ - -public class _687 { - public static class Solution1 { - /** - * Use a one element array to pass in and out is a common technique for handling tree questions. - */ - public int longestUnivaluePath(TreeNode root) { - int[] result = new int[1]; - if (root != null) { - dfs(root, result); - } - return result[0]; - } - - // calculate longest univalue path from root to leaves - // In addition, the maximum univalue path cross the root node is calculated and then global maximum is udpated. - private int dfs(TreeNode root, int[] result) { - int leftPath = root.left == null ? 0 : dfs(root.left, result); - int rightPath = root.right == null ? 0 : dfs(root.right, result); - int leftResult = (root.left != null && root.left.val == root.val) ? leftPath + 1 : 0; - int rightResult = (root.right != null && root.right.val == root.val) ? rightPath + 1 : 0; - result[0] = Math.max(result[0], leftResult + rightResult); - return Math.max(leftResult, rightResult); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_688.java b/src/main/java/com/fishercoder/solutions/_688.java deleted file mode 100644 index 09223d5307..0000000000 --- a/src/main/java/com/fishercoder/solutions/_688.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.LinkedList; -import java.util.Queue; - -/** - * 688. Knight Probability in Chessboard - * - * On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. - * The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1). - * A chess knight has 8 possible moves it can make, as illustrated below. - * Each move is two squares in a cardinal direction, then one square in an orthogonal direction. - * Each time the knight is to move, it chooses one of eight possible moves uniformly at random - * (even if the piece would go off the chessboard) and moves there. - * The knight continues moving until it has made exactly K moves or has moved off the chessboard. - * Return the probability that the knight remains on the board after it has stopped moving. - - Example: - - Input: 3, 2, 0, 0 - Output: 0.0625 - Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board. - From each of those positions, there are also two moves that will keep the knight on the board. - The total probability the knight stays on the board is 0.0625. - - Note: - N will be between 1 and 25. - K will be between 0 and 100. - The knight always initially starts on the board. - */ -public class _688 { - - public static class Solution1 { - /** - * This BFS solution results in TLE on Leetcode. - */ - public double knightProbability(int N, int K, int r, int c) { - int[][] directions = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}}; - Queue queue = new LinkedList<>(); - queue.offer(new int[]{r, c}); - int level = K; - while (level-- > 0) { - int size = queue.size(); - for (int i = 0; i < size; i++) { - int[] curr = queue.poll(); - for (int[] direction : directions) { - int x = curr[0] + direction[0]; - int y = curr[1] + direction[1]; - if (x >= 0 && x < N && y >= 0 && y < N) { - queue.offer(new int[]{x, y}); - } - } - } - } - double prob = queue.size(); - for (int i = 0; i < K; i++) { - prob /= 8; - } - return prob; - } - } - - public static class Solution2 { - /** - * Let f[r][c][k] mean the probability that the knight is still on board after k steps, - * we can deduce a recursion from its k-1 steps - * In addition, instead of using a 3-d array, we can only keep the most recent two layers, - * i.e. using only two 2-d arrays. - */ - public double knightProbability(int N, int K, int r, int c) { - int[][] directions = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}}; - double[][] dp0 = new double[N][N]; - for (double[] row : dp0) { - Arrays.fill(row, 1); - } - for (int k = 0; k < K; k++) { - double[][] dp1 = new double[N][N]; - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - for (int l = 0; l < directions.length; l++) { - int[] direction = directions[l]; - int x = i + direction[0]; - int y = j + direction[1]; - if (x >= 0 && y >= 0 && x < N && y < N) { - dp1[i][j] += dp0[x][y]; - } - } - } - } - dp0 = dp1; - } - return dp0[r][c] / Math.pow(8, K); - } - } - - public static void main(String... args) { - System.out.println((double) 2 / 8); - } -} diff --git a/src/main/java/com/fishercoder/solutions/_689.java b/src/main/java/com/fishercoder/solutions/_689.java deleted file mode 100644 index a779791985..0000000000 --- a/src/main/java/com/fishercoder/solutions/_689.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 689. Maximum Sum of 3 Non-Overlapping Subarrays - * - * In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. - * Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. - * Return the result as a list of indices representing the starting position of each interval (0-indexed). - * If there are multiple answers, return the lexicographically smallest one. - - Example: - Input: [1,2,1,2,6,7,5,1], 2 - Output: [0, 3, 5] - Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5]. - We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger. - - Note: - nums.length will be between 1 and 20000. - nums[i] will be between 1 and 65535. - k will be between 1 and floor(nums.length / 3). - */ -public class _689 { - public static class Solution1 { - /**we basically need to find the interval (i, i+k-1) as the middle interval, where k <= i <= n-2k - * then this interval (0, i-1) will be the left interval - * the interval (i+k, n-1) will be the right interval. - * - * Please pay special attention to the variable name I use here: this `k` is not a random one, it's the `k` - * from the passed in parameter. - * - * Credit: https://discuss.leetcode.com/topic/105577/c-java-dp-with-explanation-o-n/*/ - public int[] maxSumOfThreeSubarrays(int[] nums, int k) { - if (nums == null || nums.length == 0) { - return new int[]{}; - } - int n = nums.length; - int[] sums = new int[n + 1]; - for (int i = 0; i < n; i++) { - sums[i + 1] = sums[i] + nums[i]; - } - - int[] leftMax = new int[n]; - for (int i = k, total = sums[k] - sums[0]; i < n; i++) { - if (sums[i + 1] - sums[i + 1 - k] > total) { - leftMax[i] = i + 1 - k; - total = sums[i + 1] - sums[i + 1 - k]; - } else { - leftMax[i] = leftMax[i - 1]; - } - } - - int[] rightMax = new int[n]; - rightMax[n - k] = n - k; - for (int i = n - k - 1, total = sums[n] - sums[n - k]; i >= 0; i--) { - if (sums[i + k] - sums[i] >= total) { - rightMax[i] = i; - total = sums[i + k] - sums[i]; - } else { - rightMax[i] = rightMax[i + 1]; - } - } - - //try to find all possible middle intervals - int[] result = new int[3]; - int max = 0; - for (int i = k; i <= n - 2 * k; i++) { - int left = leftMax[i - 1]; - int right = rightMax[i + k]; - int total = (sums[i + k] - sums[i]) + (sums[left + k] - sums[left]) + (sums[right + k] - sums[right]); - if (total > max) { - max = total; - result[0] = left; - result[1] = i; - result[2] = right; - } - } - return result; - } - } - - public static class Solution2 { - /**reference: https://leetcode.com/articles/maximum-sum-of-3-non-overlapping-intervals*/ - } -} diff --git a/src/main/java/com/fishercoder/solutions/_690.java b/src/main/java/com/fishercoder/solutions/_690.java deleted file mode 100644 index f7198d53f1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_690.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.Employee; - -import java.util.List; -import java.util.stream.Collectors; - -/** - * 690. Employee Importance - * - * You are given a data structure of employee information, which includes the employee's unique id, - * his importance value and his direct subordinates' id. - * For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. - * They have importance value 15, 10 and 5, respectively. - * Then employee 1 has a data structure like [1, 15, [2]], and - * employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. - * Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct. - * Now given the employee information of a company, and an employee id, - * you need to return the total importance value of this employee and all his subordinates. - - Example 1: - - Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 - Output: 11 - Explanation: - Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. - They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11. - - Note: - - One employee has at most one direct leader and may have several subordinates. - The maximum number of employees won't exceed 2000. - - */ -public class _690 { - - public static class Solution1 { - - int total = 0; - - public int getImportance(List employees, int id) { - Employee manager = employees.stream().filter(e -> e.id == id).collect(Collectors.toList()).get(0); - total += manager.importance; - manager.subordinates.forEach(subId -> getImportance(employees, subId)); - - /**The above line is equivalent to below for loop*/ -// for (int subId : manager.subordinates) { -// getImportance(employees, subId); -// } - - return total; - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_691.java b/src/main/java/com/fishercoder/solutions/_691.java deleted file mode 100644 index 5868da922d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_691.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 691. Stickers to Spell Word - * - * We are given N different types of stickers. Each sticker has a lowercase English word on it. - * You would like to spell out the given target string by cutting individual letters from your collection of stickers and rearranging them. - * You can use each sticker more than once if you want, and you have infinite quantities of each sticker. - * What is the minimum number of stickers that you need to spell out the target? If the task is impossible, return -1. - - Example 1: - Input: - ["with", "example", "science"], "thehat" - Output: - 3 - - Explanation: - We can use 2 "with" stickers, and 1 "example" sticker. - After cutting and rearrange the letters of those stickers, we can form the target "thehat". - Also, this is the minimum number of stickers necessary to form the target string. - - Example 2: - Input: - ["notice", "possible"], "basicbasic" - Output: - -1 - - Explanation: - We can't form the target "basicbasic" from cutting letters from the given stickers. - - Note: - stickers has length in the range [1, 50]. - stickers consists of lowercase English words (without apostrophes). - target has length in the range [1, 15], and consists of lowercase English letters. - In all test cases, all words were chosen randomly from the 1000 most common US English words, and the target was chosen as a concatenation of two random words. - The time limit may be more challenging than usual. It is expected that a 50 sticker test case can be solved within 35ms on average. - */ -public class _691 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/106273/c-java-python-dp-memoization-with-optimization-29-ms-c/2 - */ - public int minStickers(String[] stickers, String target) { - int m = stickers.length; - int[][] mp = new int[m][26]; - Map dp = new HashMap<>(); - for (int i = 0; i < m; i++) { - for (char c : stickers[i].toCharArray()) { - mp[i][c - 'a']++; - } - } - dp.put("", 0); - return helper(dp, mp, target); - } - - private int helper(Map dp, int[][] mp, String target) { - if (dp.containsKey(target)) { - return dp.get(target); - } - int ans = Integer.MAX_VALUE; - int n = mp.length; - int[] tar = new int[26]; - for (char c : target.toCharArray()) { - tar[c - 'a']++; - } - // try every sticker - for (int i = 0; i < n; i++) { - // optimization - if (mp[i][target.charAt(0) - 'a'] == 0) { - continue; - } - StringBuilder sb = new StringBuilder(); - // apply a sticker on every character a-z - for (int j = 0; j < 26; j++) { - if (tar[j] > 0) { - for (int k = 0; k < Math.max(0, tar[j] - mp[i][j]); k++) { - sb.append((char) ('a' + j)); - } - } - } - String s = sb.toString(); - int tmp = helper(dp, mp, s); - if (tmp != -1) { - ans = Math.min(ans, 1 + tmp); - } - } - dp.put(target, ans == Integer.MAX_VALUE ? -1 : ans); - return dp.get(target); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_692.java b/src/main/java/com/fishercoder/solutions/_692.java deleted file mode 100644 index 5f68924c3c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_692.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.SortedSet; -import java.util.TreeSet; - -/** - * 692. Top K Frequent Words - * - * Given a non-empty list of words, return the k most frequent elements. - * Your answer should be sorted by frequency from highest to lowest. - * If two words have the same frequency, then the word with the lower alphabetical order comes first. - - Example 1: - Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 - Output: ["i", "love"] - Explanation: "i" and "love" are the two most frequent words. - Note that "i" comes before "love" due to a lower alphabetical order. - - Example 2: - Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 - Output: ["the", "is", "sunny", "day"] - Explanation: "the", "is", "sunny" and "day" are the four most frequent words, - with the number of occurrence being 4, 3, 2 and 1 respectively. - - Note: - You may assume k is always valid, 1 ≤ k ≤ number of unique elements. - Input words contain only lowercase letters. - - Follow up: - Try to solve it in O(n log k) time and O(n) extra space. - Can you solve it in O(n) time with only O(k) extra space? - - */ -public class _692 { - - public static class Solution1 { - /** - * O(n) extra space - * O(nlogk) time - * */ - public List topKFrequent(String[] words, int k) { - Map map = new HashMap<>(); - for (String word : words) { - map.put(word, map.getOrDefault(word, 0) + 1); - } - - SortedSet> sortedset = new TreeSet<>( - (e1, e2) -> { - if (e1.getValue() != e2.getValue()) { - return e2.getValue() - e1.getValue(); - } else { - return e1.getKey().compareToIgnoreCase(e2.getKey()); - } - }); - sortedset.addAll(map.entrySet()); - - List result = new ArrayList<>(); - Iterator> iterator = sortedset.iterator(); - while (iterator.hasNext() && k-- > 0) { - result.add(iterator.next().getKey()); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_693.java b/src/main/java/com/fishercoder/solutions/_693.java deleted file mode 100644 index 88e217c411..0000000000 --- a/src/main/java/com/fishercoder/solutions/_693.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 693. Binary Number with Alternating Bits - * - * Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. - - Example 1: - Input: 5 - Output: True - Explanation: - The binary representation of 5 is: 101 - - Example 2: - Input: 7 - Output: False - Explanation: - The binary representation of 7 is: 111. - - Example 3: - Input: 11 - Output: False - Explanation: - The binary representation of 11 is: 1011. - - Example 4: - Input: 10 - Output: True - Explanation: - The binary representation of 10 is: 1010. - */ - -public class _693 { - public static class Solution1 { - public boolean hasAlternatingBits(int n) { - String binaryStr = Integer.toBinaryString(n); - for (int i = 1; i < binaryStr.length(); i++) { - if (binaryStr.charAt(i - 1) == binaryStr.charAt(i)) { - return false; - } - } - return true; - } - } - - public static class Solution2 { - public boolean hasAlternatingBits(int n) { - return Integer.bitCount(((n >> 1) ^ n) + 1) == 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_694.java b/src/main/java/com/fishercoder/solutions/_694.java deleted file mode 100644 index d64a8f7d5c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_694.java +++ /dev/null @@ -1,214 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 694. Number of Distinct Islands - * - * Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) - * connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. - * Count the number of distinct islands. - * An island is considered to be the same as another if and only if - * one island can be translated (and not rotated or reflected) to equal the other. - - Example 1: - - 11000 - 11000 - 00011 - 00011 - - Given the above grid map, return 1. - - Example 2: - - 11011 - 10000 - 00001 - 11011 - - Given the above grid map, return 3. - - Notice that: - - 11 - 1 - - and - - 1 - 11 - - are considered different island shapes, because we do not consider reflection / rotation. - - Note: The length of each dimension in the given grid does not exceed 50. - */ -public class _694 { - public static class Solution1 { - /** - * My original idea: - * my not fully working yet: the equals() and hashcode() methods need to be refined - * because HashSet is not really filtering the islands wiht the same shape. - */ - class Quadrilateral { - int[] topLeft; - int[] bottomLeft; - int[] topRight; - int[] bottomRight; - int area; - - public Quadrilateral(int i, int j) { - this.area = 0; - this.topLeft = new int[]{i, j}; - this.topRight = new int[]{i, j}; - this.bottomLeft = new int[]{i, j}; - this.bottomRight = new int[]{i, j}; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof Quadrilateral)) { - return false; - } - - Quadrilateral that = (Quadrilateral) o; - return this.area == that.area && checkDistance(that); - } - - private boolean checkDistance(Quadrilateral that) { - int thisTop = computeDistance(this.topLeft, this.topRight); - int thatTop = computeDistance(that.topLeft, that.topRight); - if (thisTop != thatTop) { - return false; - } - int thisRight = computeDistance(this.topRight, this.bottomRight); - int thatRight = computeDistance(that.topRight, that.bottomRight); - if (thisRight != thatRight) { - return false; - } - int thisBottom = computeDistance(this.bottomRight, this.bottomLeft); - int thatBottom = computeDistance(that.bottomRight, that.bottomLeft); - if (thisBottom != thatBottom) { - return false; - } - int thisLeft = computeDistance(this.bottomLeft, this.topLeft); - int thatLeft = computeDistance(that.bottomLeft, that.topLeft); - return thisLeft == thatLeft; - } - - private int computeDistance(int[] A, int[] B) { - return (int) (Math.pow(A[0] - B[0], 2) + Math.pow(A[1] - B[1], 2)); - } - - @Override - public int hashCode() { - return area + computeDistance(this.topLeft, this.topRight) + computeDistance(this.topRight, this.bottomRight) - + computeDistance(this.bottomRight, this.bottomLeft) + computeDistance(this.bottomLeft, this.topLeft); - } - - public void addPoint(int i, int j) { - //todo: check wether this point (i,j) is in the range, if not, expand the range - if (i == topRight[0]) { - topRight[1] = Math.max(topRight[1], j); - } - if (j == topRight[1]) { - topRight[0] = Math.min(topRight[1], i); - } - if (i == topLeft[0]) { - topLeft[1] = Math.min(topLeft[1], j); - } - if (j == topLeft[1]) { - topLeft[0] = Math.min(topLeft[0], i); - } - if (i == bottomLeft[0]) { - bottomLeft[1] = Math.min(bottomLeft[1], j); - } - if (j == bottomLeft[1]) { - bottomLeft[0] = Math.max(bottomLeft[0], i); - } - if (j == bottomRight[1]) { - bottomRight[0] = Math.max(bottomRight[0], i); - } - if (i == bottomRight[0]) { - bottomRight[1] = Math.max(bottomRight[1], j); - } - } - - public void addArea() { - this.area++; - } - } - - public int numDistinctIslands(int[][] grid) { - Set set = new HashSet<>(); - int m = grid.length; - int n = grid[0].length; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == 1) { - Quadrilateral quadrilateral = dfs(grid, i, j, m, n, new Quadrilateral(i, j)); - set.add(quadrilateral); - } - } - } - return set.size(); - } - - private Quadrilateral dfs(int[][] grid, int i, int j, int m, int n, Quadrilateral quadrilateral) { - if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 0) { - return quadrilateral; - } - grid[i][j] = 0; - quadrilateral.addPoint(i, j); - quadrilateral.addArea(); - quadrilateral = dfs(grid, i + 1, j, m, n, quadrilateral); - quadrilateral = dfs(grid, i - 1, j, m, n, quadrilateral); - quadrilateral = dfs(grid, i, j + 1, m, n, quadrilateral); - quadrilateral = dfs(grid, i, j - 1, m, n, quadrilateral); - return quadrilateral; - } - } - - public static class Solution2 { - int[][] directions = new int[][]{ - {0, 1}, - {1, 0}, - {0, -1}, - {-1, 0} - }; - - public int numDistinctIslands(int[][] grid) { - int m = grid.length; - int n = grid[0].length; - Set>> uniqueShapeIslands = new HashSet<>(); - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - List> island = new ArrayList<>(); - if (dfs(i, j, i, j, grid, m, n, island)) { - uniqueShapeIslands.add(island); - } - } - } - return uniqueShapeIslands.size(); - } - - private boolean dfs(int i0, int j0, int i, int j, int[][] grid, int m, int n, List> island) { - if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] <= 0) { - return false; - } - island.add(Arrays.asList(i - i0, j - j0)); - grid[i][j] *= -1; - for (int k = 0; k < 4; k++) { - dfs(i0, j0, i + directions[k][0], j + directions[k][1], grid, m, n, island); - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_695.java b/src/main/java/com/fishercoder/solutions/_695.java deleted file mode 100644 index 6e4a10238c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_695.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 695. Max Area of Island - * - * Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) - * connected 4-directionally (horizontal or vertical.) - * You may assume all four edges of the grid are surrounded by water. - * Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.) - - Example 1: - - [[0,0,1,0,0,0,0,1,0,0,0,0,0], - [0,0,0,0,0,0,0,1,1,1,0,0,0], - [0,1,1,0,1,0,0,0,0,0,0,0,0], - [0,1,0,0,1,1,0,0,1,0,1,0,0], - [0,1,0,0,1,1,0,0,1,1,1,0,0], - [0,0,0,0,0,0,0,0,0,0,1,0,0], - [0,0,0,0,0,0,0,1,1,1,0,0,0], - [0,0,0,0,0,0,0,1,1,0,0,0,0]] - - Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally. - - Example 2: - - [[0,0,0,0,0,0,0,0]] - - Given the above grid, return 0. - - Note: The length of each dimension in the given grid does not exceed 50. - */ - -public class _695 { - - public static class Solution1 { - public int maxAreaOfIsland(int[][] grid) { - if (grid == null || grid.length == 0) { - return 0; - } - int m = grid.length; - int n = grid[0].length; - int max = 0; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (grid[i][j] == 1) { - int area = dfs(grid, i, j, m, n, 0); - max = Math.max(area, max); - } - } - } - return max; - } - - int dfs(int[][] grid, int i, int j, int m, int n, int area) { - if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) { - return area; - } - grid[i][j] = 0; - area++; - area = dfs(grid, i + 1, j, m, n, area); - area = dfs(grid, i, j + 1, m, n, area); - area = dfs(grid, i - 1, j, m, n, area); - area = dfs(grid, i, j - 1, m, n, area); - return area; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_696.java b/src/main/java/com/fishercoder/solutions/_696.java deleted file mode 100644 index 05ed13e480..0000000000 --- a/src/main/java/com/fishercoder/solutions/_696.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 696. Count Binary Substrings - * - * Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, - * and all the 0's and all the 1's in these substrings are grouped consecutively. - * Substrings that occur multiple times are counted the number of times they occur. - - Example 1: - - Input: "00110011" - Output: 6 - Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01". - - Notice that some of these substrings repeat and are counted the number of times they occur. - - Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together. - - Example 2: - - Input: "10101" - Output: 4 - Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's. - - Note: - s.length will be between 1 and 50,000. - s will only consist of "0" or "1" characters. - */ -public class _696 { - public static class Solution1 { - public int countBinarySubstrings(String s) { - int n = s.length(); - /**a[i][0] denotes from most left up to i (inclusive), how many consecutive 0's - * a[i][1] denotes from most left up to i (inclusive), how many consecutive 1's*/ - int[][] a = new int[n][2]; - /**a[i][0] denotes from i (inclusive) to the most right, how many consecutive 0's - * b[i][0] denotes from i (inclusive) to the most right, how many consecutive 1's*/ - int[][] b = new int[n][2]; - for (int i = 0; i < n; i++) { - if (s.charAt(i) == '0') { - a[i][0] = 1 + (i - 1 >= 0 ? a[i - 1][0] : 0); - } else { - a[i][1] = 1 + (i - 1 >= 0 ? a[i - 1][1] : 0); - } - } - for (int i = n - 1; i >= 0; i--) { - if (s.charAt(i) == '0') { - b[i][0] = 1 + (i + 1 < n ? b[i + 1][0] : 0); - } else { - b[i][1] = 1 + (i + 1 < n ? b[i + 1][1] : 0); - } - - } - long ans = 0; - for (int i = 0; i + 1 < n; i++) { - ans += Math.min(a[i][0], b[i + 1][1]); - ans += Math.min(a[i][1], b[i + 1][0]); - } - return (int) ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_697.java b/src/main/java/com/fishercoder/solutions/_697.java deleted file mode 100644 index 1ff075c521..0000000000 --- a/src/main/java/com/fishercoder/solutions/_697.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 697. Degree of an Array - * - * Given a non-empty array of non-negative integers nums, - * the degree of this array is defined as the maximum frequency of any one of its elements. - * Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums. - - Example 1: - - Input: [1, 2, 2, 3, 1] - Output: 2 - Explanation: - The input array has a degree of 2 because both elements 1 and 2 appear twice. - Of the subarrays that have the same degree: - [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2] - The shortest length is 2. So return 2. - - Example 2: - Input: [1,2,2,3,1,4,2] - Output: 6 - - Note: - nums.length will be between 1 and 50,000. - nums[i] will be an integer between 0 and 49,999. - */ -public class _697 { - public static class Solution1 { - public int findShortestSubArray(int[] nums) { - Map map = new HashMap<>(); - for (int i = 0; i < nums.length; i++) { - if (map.containsKey(nums[i])) { - map.put(nums[i], map.get(nums[i]) + 1); - } else { - map.put(nums[i], 1); - } - } - int degree = -1; - for (int key : map.keySet()) { - degree = Math.max(degree, map.get(key)); - } - List candidateNums = new ArrayList(); - for (int key : map.keySet()) { - if (map.get(key) == degree) { - candidateNums.add(key); - } - } - int shortest = Integer.MAX_VALUE; - for (int candidate : candidateNums) { - shortest = Math.min(shortest, findLength(nums, candidate)); - } - return shortest; - } - - int findLength(int[] arr, int candidate) { - int firstAppearance = Integer.MAX_VALUE; - for (int i = 0; i < arr.length; i++) { - if (arr[i] == candidate) { - firstAppearance = i; - break; - } - } - int lastAppearance = arr.length - 1; - for (int i = arr.length - 1; i > firstAppearance; i--) { - if (arr[i] == candidate) { - lastAppearance = i; - break; - } - } - return (lastAppearance - firstAppearance) + 1; - } - } - - public static class Solution2 { - public int findShortestSubArray(int[] nums) { - Map count = new HashMap<>(); - Map left = new HashMap<>(); - Map right = new HashMap<>(); - - for (int i = 0; i < nums.length; i++) { - count.put(nums[i], count.getOrDefault(nums[i], 0) + 1); - if (!left.containsKey(nums[i])) { - left.put(nums[i], i); - } - right.put(nums[i], i); - } - - int result = nums.length; - int degree = Collections.max(count.values()); - for (int num : count.keySet()) { - if (count.get(num) == degree) { - result = Math.min(result, right.get(num) - left.get(num) + 1); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_698.java b/src/main/java/com/fishercoder/solutions/_698.java deleted file mode 100644 index 0dd794098d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_698.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 698. Partition to K Equal Sum Subsets - * - * Given an array of integers nums and a positive integer k, - * find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. - - Example 1: - - Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 - Output: True - Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums. - - Note: - 1 <= k <= len(nums) <= 16. - 0 < nums[i] < 10000. - */ -public class _698 { - - public static class Solution1 { - public boolean canPartitionKSubsets(int[] nums, int k) { - long sum = 0; - for (int num : nums) { - sum += num; - } - if (sum % k != 0) { - return false; - } - int equalSum = (int) (sum / k); - boolean[] visited = new boolean[nums.length]; - return canPartition(nums, visited, 0, k, 0, 0, equalSum); - } - - private boolean canPartition(int[] nums, boolean[] visited, int startIndex, int k, int currSum, int currNum, int target) { - if (k == 1) { - return true; - } - if (currSum == target && currNum > 0) { - /**Everytime when we get currSum == target, we'll start from index 0 and look up the numbers that are not used yet - * and try to find another sum that could equal to target*/ - return canPartition(nums, visited, 0, k - 1, 0, 0, target); - } - for (int i = startIndex; i < nums.length; i++) { - if (!visited[i]) { - visited[i] = true; - if (canPartition(nums, visited, i + 1, k, currSum + nums[i], currNum++, target)) { - return true; - } - visited[i] = false; - } - } - return false; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_699.java b/src/main/java/com/fishercoder/solutions/_699.java deleted file mode 100644 index 4562bd06c5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_699.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 699. Falling Squares - * - * On an infinite number line (x-axis), we drop given squares in the order they are given. - * The i-th square dropped (positions[i] = (left, side_length)) is a - * square with the left-most point being positions[i][0] and sidelength positions[i][1]. - * The square is dropped with the bottom edge parallel to the number line, and - * from a higher height than all currently landed squares. We wait for each square to stick before dropping the next. - * The squares are infinitely sticky on their bottom edge, and will - * remain fixed to any positive length surface they touch (either the number line or another square). - * Squares dropped adjacent to each other will not stick together prematurely. - * Return a list ans of heights. - * Each height ans[i] represents the current highest height of any square we have dropped, - * after dropping squares represented by positions[0], positions[1], ..., positions[i]. - - Example 1: - - Input: [[1, 2], [2, 3], [6, 1]] - Output: [2, 5, 5] - Explanation: - - - After the first drop of positions[0] = [1, 2]: - _aa - _aa - ------- - The maximum height of any square is 2. - - - After the second drop of positions[1] = [2, 3]: - __aaa - __aaa - __aaa - _aa__ - _aa__ - -------------- - The maximum height of any square is 5. - The larger square stays on top of the smaller square despite where its center - of gravity is, because squares are infinitely sticky on their bottom edge. - - - After the third drop of positions[1] = [6, 1]: - __aaa - __aaa - __aaa - _aa - _aa___a - -------------- - The maximum height of any square is still 5. - - Thus, we return an answer of [2, 5, 5]. - - - Example 2: - - Input: [[100, 100], [200, 100]] - Output: [100, 100] - Explanation: Adjacent squares don't get stuck prematurely - only their bottom edge can stick to surfaces. - - Note: - 1 <= positions.length <= 1000. - 1 <= positions[0] <= 10^8. - 1 <= positions[1] <= 10^6. - */ - -public class _699 { - public static class Solution1 { - /**credit: https://discuss.leetcode.com/topic/107107/easy-understood-o-n-2-solution-with-explanation*/ - public List fallingSquares(int[][] positions) { - List intervals = new ArrayList<>(); - List result = new ArrayList<>(); - int height = 0; - for (int[] position : positions) { - Interval curr = new Interval(position[0], position[0] + position[1] - 1, position[1]); - height = Math.max(height, getHeight(intervals, curr)); - result.add(height); - } - return result; - } - - private int getHeight(List intervals, Interval curr) { - int preMaxHeight = 0; - for (Interval interval : intervals) { - if (interval.end < curr.start || interval.start > curr.end) { - continue; - } - preMaxHeight = Math.max(preMaxHeight, interval.height); - } - curr.height += preMaxHeight; - intervals.add(curr); - return curr.height; - } - - class Interval { - int start; - int end; - int height; - - public Interval(int start, int end, int height) { - this.start = start; - this.end = end; - this.height = height; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_70.java b/src/main/java/com/fishercoder/solutions/_70.java deleted file mode 100644 index c131b48fc9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_70.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder.solutions; - -public class _70 { - public static class Solution1 { - /** - * O(n) time - * O(n) space - */ - public int climbStairs(int n) { - if (n == 1) { - return n; - } - int[] dp = new int[n + 1]; - dp[1] = 1; - dp[2] = 2; - for (int i = 3; i <= n; i++) { - dp[i] = dp[i - 1] + dp[i - 2]; - } - return dp[n]; - } - } - - public static class Solution2 { - /** - * O(n) time - * O(1) space - */ - public int climbStairs(int n) { - if (n == 1) { - return n; - } - int stepOne = 1; - int stepTwo = 2; - for (int i = 3; i <= n; i++) { - int tmp = stepTwo; - stepTwo = stepOne + stepTwo; - stepOne = tmp; - } - return stepTwo; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_700.java b/src/main/java/com/fishercoder/solutions/_700.java deleted file mode 100644 index 25a5415491..0000000000 --- a/src/main/java/com/fishercoder/solutions/_700.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 700. Search in a Binary Search Tree - * - * Given the root node of a binary search tree (BST) and a value. - * You need to find the node in the BST that the node's value equals the given value. - * Return the subtree rooted with that node. If such node doesn't exist, you should return NULL. - * - * For example, - * - * Given the tree: - * 4 - * / \ - * 2 7 - * / \ - * 1 3 - * - * And the value to search: 2 - * You should return this subtree: - * - * 2 - * / \ - * 1 3 - * In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL. - * - * Note that an empty tree is represented by NULL, - * therefore you would see the expected output (serialized tree format) as [], not null. - * */ -public class _700 { - public static class Solution1 { - public TreeNode searchBST(TreeNode root, int val) { - if (root == null) { - return null; - } else if (root.val == val) { - return root; - } else if (root.val > val) { - return searchBST(root.left, val); - } else { - return searchBST(root.right, val); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_701.java b/src/main/java/com/fishercoder/solutions/_701.java deleted file mode 100644 index 6adee4875b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_701.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 701. Insert into a Binary Search Tree - * - * Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. - * Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them. - * - * For example, - * - * Given the tree: - * 4 - * / \ - * 2 7 - * / \ - * 1 3 - * And the value to insert: 5 - * - * You can return this binary search tree: - * - * 4 - * / \ - * 2 7 - * / \ / - * 1 3 5 - * - * This tree is also valid: - * - * 5 - * / \ - * 2 7 - * / \ - * 1 3 - * \ - * 4 - */ -public class _701 { - public static class Solution1 { - public TreeNode insertIntoBST(TreeNode root, int val) { - if (root == null) { - return new TreeNode(val); - } - if (root.val < val) { - root.right = insertIntoBST(root.right, val); - } else { - root.left = insertIntoBST(root.left, val); - } - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_703.java b/src/main/java/com/fishercoder/solutions/_703.java deleted file mode 100644 index 7e7bb11291..0000000000 --- a/src/main/java/com/fishercoder/solutions/_703.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.PriorityQueue; - -/** - * 703. Kth Largest Element in a Stream - * - * Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element. - * Your KthLargest class will have a constructor which accepts an integer K and an integer array nums, - * which contains initial elements from the stream. - * For each call to the method KthLargest.add, return the element representing the kth largest element in the stream. - * - * Example: - * - * int K = 3; - * int[] arr = [4,5,8,2]; - * KthLargest kthLargest = new KthLargest(3, arr); - * kthLargest.add(3); // returns 4 - * kthLargest.add(5); // returns 5 - * kthLargest.add(10); // returns 5 - * kthLargest.add(9); // returns 8 - * kthLargest.add(4); // returns 8 - * Note: - * You may assume that nums' length ≥ K-1 and K ≥ 1. - */ -public class _703 { - public static class Solution1 { - public static class KthLargest { - PriorityQueue heap; - int maxK; - - public KthLargest(int k, int[] nums) { - heap = new PriorityQueue<>(Collections.reverseOrder()); - for (int num : nums) { - heap.offer(num); - } - maxK = k; - } - - public int add(int val) { - List tmp = new ArrayList<>(); - int result = 0; - int tmpK = maxK; - heap.offer(val); - while (tmpK-- > 0) { - result = heap.poll(); - tmp.add(result); - } - for (int num : tmp) { - heap.offer(num); - } - return result; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_704.java b/src/main/java/com/fishercoder/solutions/_704.java deleted file mode 100644 index 610f3dbd3e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_704.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 704. Binary Search - * - * Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. - * If target exists, then return its index, otherwise return -1. - * - * Example 1: - * - * Input: nums = [-1,0,3,5,9,12], target = 9 - * Output: 4 - * Explanation: 9 exists in nums and its index is 4 - * - * Example 2: - * - * Input: nums = [-1,0,3,5,9,12], target = 2 - * Output: -1 - * Explanation: 2 does not exist in nums so return -1 - * - * Note: - * - * You may assume that all elements in nums are unique. - * n will be in the range [1, 10000]. - * The value of each element in nums will be in the range [-9999, 9999]. - */ -public class _704 { - public static class Solution1 { - public int search(int[] nums, int target) { - int left = 0; - int right = nums.length - 1; - if (target < nums[left] || target > nums[right]) { - return -1; - } - if (nums[left] == target) { - return left; - } else if (nums[right] == target) { - return right; - } - while (left <= right) { - int mid = left + (right - left) / 2; - if (target == nums[mid]) { - return mid; - } else if (target > nums[mid]) { - left = mid + 1; - } else { - right = mid - 1; - } - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_705.java b/src/main/java/com/fishercoder/solutions/_705.java deleted file mode 100644 index 677c4cc371..0000000000 --- a/src/main/java/com/fishercoder/solutions/_705.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 705. Design HashSet - * - * Design a HashSet without using any built-in hash table libraries. - * - * To be specific, your design should include these functions: - * - * add(value): Insert a value into the HashSet. - * contains(value) : Return whether the value exists in the HashSet or not. - * remove(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing. - * - * Example: - * - * MyHashSet hashSet = new MyHashSet(); - * hashSet.add(1); - * hashSet.add(2); - * hashSet.contains(1); // returns true - * hashSet.contains(3); // returns false (not found) - * hashSet.add(2); - * hashSet.contains(2); // returns true - * hashSet.remove(2); - * hashSet.contains(2); // returns false (already removed) - * - * Note: - * - * All values will be in the range of [0, 1000000]. - * The number of operations will be in the range of [1, 10000]. - * Please do not use the built-in HashSet library. - */ -public class _705 { - public static class Solution1 { - class MyHashSet { - Map map; - - /** Initialize your data structure here. */ - public MyHashSet() { - map = new HashMap<>(); - } - - public void add(int key) { - map.put(key, 0); - } - - public void remove(int key) { - if (map.containsKey(key)) { - map.remove(key); - } - } - - /** Returns true if this set contains the specified element */ - public boolean contains(int key) { - return map.containsKey(key); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_706.java b/src/main/java/com/fishercoder/solutions/_706.java deleted file mode 100644 index b19a611234..0000000000 --- a/src/main/java/com/fishercoder/solutions/_706.java +++ /dev/null @@ -1,107 +0,0 @@ -package com.fishercoder.solutions; - -/**706. Design HashMap - * - * Design a HashMap without using any built-in hash table libraries. - * - * To be specific, your design should include these functions: - * - * put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value. - * get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key. - * remove(key) : Remove the mapping for the value key if this map contains the mapping for the key. - * - * Example: - * - * MyHashMap hashMap = new MyHashMap(); - * hashMap.put(1, 1); - * hashMap.put(2, 2); - * hashMap.get(1); // returns 1 - * hashMap.get(3); // returns -1 (not found) - * hashMap.put(2, 1); // update the existing value - * hashMap.get(2); // returns 1 - * hashMap.remove(2); // remove the mapping for 2 - * hashMap.get(2); // returns -1 (not found) - * - * Note: - * - * All keys and values will be in the range of [0, 1000000]. - * The number of operations will be in the range of [1, 10000]. - * Please do not use the built-in HashMap library.*/ -public class _706 { - public static class Solution1 { - /** - * credit: https://leetcode.com/problems/design-hashmap/discuss/152746/Java-Solution - */ - class MyHashMap { - - final ListNode[] nodes = new ListNode[10000]; - - public void put(int key, int value) { - int i = idx(key); - if (nodes[i] == null) { - nodes[i] = new ListNode(-1, -1); - } - ListNode prev = find(nodes[i], key); - if (prev.next == null) { - prev.next = new ListNode(key, value); - } else { - prev.next.val = value; - } - } - - public int get(int key) { - int i = idx(key); - if (nodes[i] == null) { - return -1; - } - ListNode node = find(nodes[i], key); - return node.next == null ? -1 : node.next.val; - } - - public void remove(int key) { - int i = idx(key); - if (nodes[i] == null) { - return; - } - ListNode prev = find(nodes[i], key); - if (prev.next == null) { - return; - } - prev.next = prev.next.next; - } - - int idx(int key) { - return Integer.hashCode(key) % nodes.length; - } - - ListNode find(ListNode bucket, int key) { - ListNode node = bucket; - ListNode prev = null; - while (node != null && node.key != key) { - prev = node; - node = node.next; - } - return prev; - } - - class ListNode { - int key; - int val; - ListNode next; - - ListNode(int key, int val) { - this.key = key; - this.val = val; - } - } - } - -/** - * Your MyHashMap object will be instantiated and called as such: - * MyHashMap obj = new MyHashMap(); - * obj.put(key,value); - * int param_2 = obj.get(key); - * obj.remove(key); - */ - } -} diff --git a/src/main/java/com/fishercoder/solutions/_709.java b/src/main/java/com/fishercoder/solutions/_709.java deleted file mode 100644 index d992bca599..0000000000 --- a/src/main/java/com/fishercoder/solutions/_709.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder.solutions; - -/**709. To Lower Case - * - * Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. - * - * Example 1: - * - * Input: "Hello" - * Output: "hello" - * Example 2: - * - * Input: "here" - * Output: "here" - * Example 3: - * - * Input: "LOVELY" - * Output: "lovely"*/ -public class _709 { - public static class Solution1 { - public String toLowerCase(String str) { - return str.toLowerCase(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_71.java b/src/main/java/com/fishercoder/solutions/_71.java deleted file mode 100644 index bafd104e11..0000000000 --- a/src/main/java/com/fishercoder/solutions/_71.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.Deque; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Set; - -public class _71 { - - public static class Solution1 { - public String simplifyPath(String path) { - Deque stack = new LinkedList<>(); - Set skipSet = new HashSet<>(Arrays.asList("..", ".", "")); - for (String dir : path.split("/")) { - if (dir.equals("..") && !stack.isEmpty()) { - stack.pop(); - } else if (!skipSet.contains(dir)) { - stack.push(dir); - } - } - String result = ""; - for (String dir : stack) { - result = "/" + dir + result; - } - return result.isEmpty() ? "/" : result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_712.java b/src/main/java/com/fishercoder/solutions/_712.java deleted file mode 100644 index 9224d570c1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_712.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 712. Minimum ASCII Delete Sum for Two Strings - * - * Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. - - Example 1: - Input: s1 = "sea", s2 = "eat" - Output: 231 - Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum. - Deleting "t" from "eat" adds 116 to the sum. - At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this. - - Example 2: - Input: s1 = "delete", s2 = "leet" - Output: 403 - Explanation: Deleting "dee" from "delete" to turn the string into "let", - adds 100[d]+101[e]+101[e] to the sum. Deleting "e" from "leet" adds 101[e] to the sum. - At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403. - If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher. - - Note: - 0 < s1.length, s2.length <= 1000. - All elements of each string will have an ASCII value in [97, 122]. - */ -public class _712 { - public static class Solution1 { - //credit: https://leetcode.com/articles/minimum-ascii-delete-sum-for-two-strings/ - public int minimumDeleteSum(String s1, String s2) { - int[][] dp = new int[s1.length() + 1][s2.length() + 1]; - - for (int i = s1.length() - 1; i >= 0; i--) { - dp[i][s2.length()] = dp[i + 1][s2.length()] + s1.codePointAt(i); - } - - for (int j = s2.length() - 1; j >= 0; j--) { - dp[s1.length()][j] = dp[s1.length()][j + 1] + s2.codePointAt(j); - } - - for (int i = s1.length() - 1; i >= 0; i--) { - for (int j = s2.length() - 1; j >= 0; j--) { - if (s1.charAt(i) == s2.charAt(j)) { - dp[i][j] = dp[i + 1][j + 1]; - } else { - dp[i][j] = Math.min(dp[i + 1][j] + s1.codePointAt(i), dp[i][j + 1] + s2.codePointAt(j)); - } - } - } - - return dp[0][0]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_713.java b/src/main/java/com/fishercoder/solutions/_713.java deleted file mode 100644 index 98ccaccd7d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_713.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 713. Subarray Product Less Than K - * - * Your are given an array of positive integers nums. - * Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k. - - Example 1: - Input: nums = [10, 5, 2, 6], k = 100 - Output: 8 - Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]. - Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k. - Note: - - 0 < nums.length <= 50000. - 0 < nums[i] < 1000. - 0 <= k < 10^6. - - */ -public class _713 { - public static class Solution1 { - /**O(n^2) solution, accepted initially, then Leetcode added one test case to fail it.*/ - public int numSubarrayProductLessThanK(int[] nums, int k) { - int result = 0; - for (int i = 0; i < nums.length; i++) { - int product = nums[i]; - if (product < k) { - result++; - for (int j = i + 1; j < nums.length; j++) { - product *= nums[j]; - if (product < k) { - result++; - } else { - break; - } - } - } - } - return result; - } - } - - public static class Solution2 { - public int numSubarrayProductLessThanK(int[] nums, int k) { - if (k < 2) { - return 0; - } - int result = 0; - int product = 1; - for (int i = 0, right = 0; right < nums.length; right++) { - product *= nums[right]; - while (i < nums.length && product >= k) { - product /= nums[i++]; - } - result += right - i + 1; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_714.java b/src/main/java/com/fishercoder/solutions/_714.java deleted file mode 100644 index 35193b0d03..0000000000 --- a/src/main/java/com/fishercoder/solutions/_714.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 714. Best Time to Buy and Sell Stock with Transaction Fee - * - * Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; - * and a non-negative integer fee representing a transaction fee. - * You may complete as many transactions as you like, - * but you need to pay the transaction fee for each transaction. - * You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.) - - Return the maximum profit you can make. - - Example 1: - Input: prices = [1, 3, 2, 8, 4, 9], fee = 2 - Output: 8 - Explanation: The maximum profit can be achieved by: - Buying at prices[0] = 1 - Selling at prices[3] = 8 - Buying at prices[4] = 4 - Selling at prices[5] = 9 - The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8. - - Note: - 0 < prices.length <= 50000. - 0 < prices[i] < 50000. - 0 <= fee < 50000. - */ -public class _714 { - public static class Solution1 { - /**O(n) time - * O(n) space - * credit: https://discuss.leetcode.com/topic/108009/java-c-clean-code-dp-greedy - * */ - public int maxProfit(int[] prices, int fee) { - int n = prices.length; - if (n < 2) { - return 0; - } - int[] hold = new int[n]; - int[] sell = new int[n]; - hold[0] = -prices[0]; - for (int i = 1; i < prices.length; i++) { - hold[i] = Math.max(hold[i - 1], sell[i - 1] - prices[i]); - sell[i] = Math.max(sell[i - 1], hold[i - 1] - fee + prices[i]); - } - return sell[n - 1]; - } - } - - public static class Solution2 { - /**O(n) time - * O(1) space - * credit: https://leetcode.com/articles/best-time-to-buy-and-sell-stock-with-transaction-fee/ - * - * cash: the max profit we could have if we did not have a share of stock in hand - * hold: the max profit we could have if we hold one share of stack in hand - * - * to transition from the i-th day to the i+1 th day, we have two options: - * 1. sell our stock: cash = Math.max(cash, hold + prices[i] - fee) - * 2. buy a stock: hold = Math.max(hold, cash - prices[i]) - * */ - public int maxProfit(int[] prices, int fee) { - int cash = 0; - int hold = -prices[0]; - for (int i = 1; i < prices.length; i++) { - cash = Math.max(cash, hold + prices[i] - fee); - hold = Math.max(hold, cash - prices[i]); - } - return cash; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_716.java b/src/main/java/com/fishercoder/solutions/_716.java deleted file mode 100644 index 4ca4b23e2a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_716.java +++ /dev/null @@ -1,215 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Stack; -import java.util.TreeMap; - -/** - * 716. Max Stack - * - * Design a max stack that supports push, pop, top, peekMax and popMax. - * - push(x) -- Push element x onto stack. - pop() -- Remove the element on top of the stack and return it. - top() -- Get the element on the top. - peekMax() -- Retrieve the maximum element in the stack. - popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one. - - Example 1: - MaxStack stack = new MaxStack(); - stack.push(5); - stack.push(1); - stack.push(5); - stack.top(); -> 5 - stack.popMax(); -> 5 - stack.top(); -> 1 - stack.peekMax(); -> 5 - stack.pop(); -> 1 - stack.top(); -> 5 - Note: - -1e7 <= x <= 1e7 - Number of operations won't exceed 10000. - The last four operations won't be called when stack is empty. - */ -public class _716 { - public static class Solution1 { - /**This is O(n) for popMax() and pop() while O(1) for the other three operations which is UN-acceptable during an interview! - * We need to do better than O(n) time complexity in order to ace the interview! - * But O(1) is impossible, so let's aim for O(logn).*/ - public static class MaxStack { - - private int max; - private Stack stack; - - /** - * initialize your data structure here. - */ - public MaxStack() { - max = Integer.MIN_VALUE; - stack = new Stack<>(); - } - - public void push(int x) { - if (x > max) { - max = x; - } - stack.push(x); - } - - public int pop() { - if (stack.peek() == max) { - int result = stack.pop(); - max = findMax(); - return result; - } else { - return stack.pop(); - } - } - - private int findMax() { - if (!stack.isEmpty()) { - Iterator iterator = stack.iterator(); - int max = stack.peek(); - while (iterator.hasNext()) { - max = Math.max(max, iterator.next()); - } - return max; - } else { - max = Integer.MIN_VALUE; - return max; - } - } - - public int top() { - return stack.peek(); - } - - public int peekMax() { - return max; - } - - public int popMax() { - Stack tmp = new Stack<>(); - int result = 0; - while (!stack.isEmpty()) { - if (stack.peek() != max) { - tmp.push(stack.pop()); - } else { - result = stack.pop(); - break; - } - } - while (!tmp.isEmpty()) { - stack.push(tmp.pop()); - } - max = findMax(); - return result; - } - } - } - - public static class Solution2 { - /** Use a treemap and a doubly linked list to achieve O(logn) time complexity. */ - - static class Node { - int val; - Node prev; - Node next; - - public Node(int val) { - this.val = val; - } - } - - static class DoublyLinkedList { - Node head; - Node tail; - - public DoublyLinkedList() { - head = new Node(0); - tail = new Node(0); - head.next = tail; - tail.prev = head; - } - - public Node add(int val) { - /**For this doubly linked list, we always add it to the end of the list*/ - Node x = new Node(val); - x.next = tail; - x.prev = tail.prev; - tail.prev.next = x; - tail.prev = tail.prev.next; - return x; - } - - public int pop() { - /**for pop(), we always pop one from the tail of the doubly linked list*/ - return unlink(tail.prev).val; - } - - public Node unlink(Node node) { - node.prev.next = node.next; - node.next.prev = node.prev; - return node; - } - - public int peek() { - return tail.prev.val; - } - } - - public static class MaxStack { - TreeMap> treeMap; - /** - * the reason we have a list of nodes as treemap's value is because one value could be pushed - * multiple times into this MaxStack and we want to keep track of all of them. - */ - DoublyLinkedList doublyLinkedList; - - /** initialize your data structure here. */ - public MaxStack() { - treeMap = new TreeMap(); - doublyLinkedList = new DoublyLinkedList(); - } - - public void push(int x) { - Node node = doublyLinkedList.add(x); - if (!treeMap.containsKey(x)) { - treeMap.put(x, new ArrayList<>()); - } - treeMap.get(x).add(node); - } - - public int pop() { - int val = doublyLinkedList.pop(); - List nodes = treeMap.get(val); - nodes.remove(nodes.size() - 1); - if (nodes.isEmpty()) { - treeMap.remove(val); - } - return val; - } - - public int top() { - return doublyLinkedList.peek(); - } - - public int peekMax() { - return treeMap.lastKey(); - } - - public int popMax() { - int max = treeMap.lastKey(); - List nodes = treeMap.get(max); - Node node = nodes.remove(nodes.size() - 1); - doublyLinkedList.unlink(node); - if (nodes.isEmpty()) { - treeMap.remove(max); - } - return max; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_717.java b/src/main/java/com/fishercoder/solutions/_717.java deleted file mode 100644 index 816cd481ed..0000000000 --- a/src/main/java/com/fishercoder/solutions/_717.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 717. 1-bit and 2-bit Characters - * - * We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). - * Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero. - - Example 1: - Input: - bits = [1, 0, 0] - Output: True - Explanation: - The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character. - - Example 2: - Input: - bits = [1, 1, 1, 0] - Output: False - Explanation: - The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character. - - Note: - 1 <= len(bits) <= 1000. - bits[i] is always 0 or 1. - */ -public class _717 { - public static class Solution1 { - public boolean isOneBitCharacter(int[] bits) { - int n = bits.length; - int i = 0; - while (i < n - 1) { - if (bits[i] == 0) { - i++; - } else { - i += 2; - } - } - return i == n - 1 ? true : false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_718.java b/src/main/java/com/fishercoder/solutions/_718.java deleted file mode 100644 index 3d67aa2856..0000000000 --- a/src/main/java/com/fishercoder/solutions/_718.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 718. Maximum Length of Repeated Subarray - * - * Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. - - Example 1: - Input: - A: [1,2,3,2,1] - B: [3,2,1,4,7] - Output: 3 - - Explanation: - The repeated subarray with maximum length is [3, 2, 1]. - Note: - 1 <= len(A), len(B) <= 1000 - 0 <= A[i], B[i] < 100 - */ -public class _718 { - public static class Solution1 { - public int findLength(int[] A, int[] B) { - if (A == null || B == null || A.length == 0 || B.length == 0) { - return 0; - } - int[][] dp = new int[A.length + 1][B.length + 1]; - int result = 0; - for (int i = A.length - 1; i >= 0; i--) { - for (int j = B.length - 1; j >= 0; j--) { - if (A[i] == B[j]) { - dp[i][j] = dp[i + 1][j + 1] + 1; - } - result = Math.max(result, dp[i][j]); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_719.java b/src/main/java/com/fishercoder/solutions/_719.java deleted file mode 100644 index 0863860364..0000000000 --- a/src/main/java/com/fishercoder/solutions/_719.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.PriorityQueue; - -/** - * 719. Find K-th Smallest Pair Distance - * - * Given an integer array, return the k-th smallest distance among all the pairs. - * The distance of a pair (A, B) is defined as the absolute difference between A and B. - - Example 1: - Input: - nums = [1,3,1] - k = 1 - Output: 0 - Explanation: - Here are all the pairs: - (1,3) -> 2 - (1,1) -> 0 - (3,1) -> 2 - Then the 1st smallest distance pair is (1,1), and its distance is 0. - - Note: - 2 <= len(nums) <= 10000. - 0 <= nums[i] < 1000000. - 1 <= k <= len(nums) * (len(nums) - 1) / 2. - */ - -public class _719 { - public static class Solution1 { - /** - * This brute force solution results in TLE of course. - */ - public int smallestDistancePair(int[] nums, int k) { - if (nums == null || nums.length == 0) { - return 0; - } - PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a - b); - for (int i = 0; i < nums.length - 1; i++) { - for (int j = i + 1; j < nums.length; j++) { - minHeap.offer(Math.abs(nums[j] - nums[i])); - } - } - - int result = 0; - while (k-- > 0) { - result = minHeap.poll(); - } - return result; - } - } - - public static class Solution2 { - /** - * credit: https://leetcode.com/articles/find-k-th-smallest-pair-distance/#approach-3-binary-search-sliding-window-accepted - */ - public int smallestDistancePair(int[] nums, int k) { - Arrays.sort(nums); - int lo = 0; - int hi = nums[nums.length - 1] - nums[0]; - while (lo < hi) { - int mi = (lo + hi) / 2; - int count = 0; - int left = 0; - for (int right = 0; right < nums.length; ++right) { - while (nums[right] - nums[left] > mi) { - left++; - } - count += right - left; - } - //count = number of pairs with distance <= mi - if (count >= k) { - hi = mi; - } else { - lo = mi + 1; - } - } - return lo; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_72.java b/src/main/java/com/fishercoder/solutions/_72.java deleted file mode 100644 index f72df19a2e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_72.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -public class _72 { - - public static class Solution1 { - public int minDistance(String word1, String word2) { - int m = word1.length(); - int n = word2.length(); - if (m == 0) { - return n; - } - if (n == 0) { - return m; - } - char[] str1 = word1.toCharArray(); - char[] str2 = word2.toCharArray(); - - int[][] table = new int[m + 1][n + 1]; - for (int i = 0; i < m + 1; i++) { - table[i][0] = i; - } - for (int j = 0; j < n + 1; j++) { - table[0][j] = j; - } - for (int i = 1; i < m + 1; i++) { - for (int j = 1; j < n + 1; j++) { - int cost = 0; - if (str1[i - 1] != str2[j - 1]) { - cost = 1; - } - table[i][j] = Math.min(Math.min(table[i - 1][j] + 1, table[i][j - 1] + 1), - table[i - 1][j - 1] + cost); - } - } - return table[m][n]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_720.java b/src/main/java/com/fishercoder/solutions/_720.java deleted file mode 100644 index 49fad34c68..0000000000 --- a/src/main/java/com/fishercoder/solutions/_720.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 720. Longest Word in Dictionary. - * - * Given a list of strings words representing an English Dictionary, - * find the longest word in words that can be built one character at a time by other words in words. - * If there is more than one possible answer, return the longest word with the smallest lexicographical order. - * If there is no answer, return the empty string. - - Example 1: - Input: - words = ["w","wo","wor","worl", "world"] - Output: "world" - Explanation: - The word "world" can be built one character at a time by "w", "wo", "wor", and "worl". - - Example 2: - Input: - words = ["a", "banana", "app", "appl", "ap", "apply", "apple"] - Output: "apple" - Explanation: - Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply". - - Note: - All the strings in the input will only contain lowercase letters. - The length of words will be in the range [1, 1000]. - The length of words[i] will be in the range [1, 30]. - */ - -public class _720 { - public static class Solution1 { - public String longestWord(String[] words) { - TrieNode root = buildTrie(words); - return findLongestWord(root, words); - } - - private String findLongestWord(TrieNode root, String[] words) { - String longestWord = ""; - for (String word : words) { - if (longestWord.length() > word.length() || (longestWord.length() == word.length() && (longestWord.compareToIgnoreCase(word) < 0))) { - continue; - } - TrieNode tmp = root; - boolean validWord = true; - for (char c : word.toCharArray()) { - if (tmp.children[c - 'a'] != null) { - tmp = tmp.children[c - 'a']; - if (!tmp.isWord) { - validWord = false; - break; - } - } - } - if (validWord) { - longestWord = word; - } - } - return longestWord; - } - - private TrieNode buildTrie(String[] words) { - TrieNode root = new TrieNode(' '); - for (String word : words) { - TrieNode tmp = root; - for (char c : word.toCharArray()) { - if (tmp.children[c - 'a'] == null) { - tmp.children[c - 'a'] = new TrieNode(c); - } - tmp = tmp.children[c - 'a']; - } - tmp.isWord = true; - } - return root; - } - - class TrieNode { - char val; - boolean isWord; - TrieNode[] children; - - public TrieNode(char val) { - this.val = val; - this.isWord = false; - this.children = new TrieNode[26]; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_721.java b/src/main/java/com/fishercoder/solutions/_721.java deleted file mode 100644 index 28d7e8144b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_721.java +++ /dev/null @@ -1,153 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.Stack; - -/** - * 721. Accounts Merge - * - * Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account. - * Now, we would like to merge these accounts. - * Two accounts definitely belong to the same person if there is some email that is common to both accounts. - * Note that even if two accounts have the same name, they may belong to different people as people could have the same name. - * A person can have any number of accounts initially, but all of their accounts definitely have the same name. - * After merging the accounts, return the accounts in the following format: - * the first element of each account is the name, and the rest of the elements are emails in sorted order. - * The accounts themselves can be returned in any order. - - Example 1: - Input: - accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]] - Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'], ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]] - - Explanation: - The first and third John's are the same person as they have the common email "johnsmith@mail.com". - The second John and Mary are different people as none of their email addresses are used by other accounts. - We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], - ['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted. - - Note: - The length of accounts will be in the range [1, 1000]. - The length of accounts[i] will be in the range [1, 10]. - The length of accounts[i][j] will be in the range [1, 30]. - */ -public class _721 { - - public static class Solution1 { - /**credit: https://leetcode.com/articles/accounts-merge/#approach-1-depth-first-search-accepted - * - * Time Complexity: O(∑ai*logai) where a​i is the length of accounts[i]. - * Without the log factor, this is the complexity to build the graph and search for each component. The log factor is for sorting each component at the end. - * Space Complexity: O(∑ai) the space used by the graph and search. - * .*/ - public List> accountsMerge(List> accounts) { - Map emailToName = new HashMap(); - Map> graph = new HashMap(); - for (List account : accounts) { - String name = ""; - for (String email : account) { - if (name == "") { - name = email; - continue; - } - graph.computeIfAbsent(email, x -> new ArrayList<>()).add(account.get(1)); - graph.computeIfAbsent(account.get(1), x -> new ArrayList<>()).add(email); - emailToName.put(email, name); - } - } - - Set seen = new HashSet(); - List> ans = new ArrayList(); - for (String email : graph.keySet()) { - if (!seen.contains(email)) { - seen.add(email); - Stack stack = new Stack(); - stack.push(email); - List component = new ArrayList(); - while (!stack.empty()) { - String node = stack.pop(); - component.add(node); - for (String nei : graph.get(node)) { - if (!seen.contains(nei)) { - seen.add(nei); - stack.push(nei); - } - } - } - Collections.sort(component); - component.add(0, emailToName.get(email)); - ans.add(component); - } - } - return ans; - } - } - - public static class Solution2 { - /**credit: https://leetcode.com/articles/accounts-merge/#approach-2-union-find-accepted - * DSU stands for Disjoint Set Union: https://en.wikipedia.org/wiki/Disjoint-set_data_structure - * - * Time complexity: O(AlogA) - * Space complexity: O(A) - * */ - public List> accountsMerge(List> accounts) { - DSU dsu = new DSU(); - Map emailToName = new HashMap<>(); - Map emailToId = new HashMap<>(); - int id = 0; - for (List account : accounts) { - String name = ""; - for (String email : account) { - if (name.equals("")) { - name = email; - continue; - } - emailToName.put(email, name); - if (!emailToId.containsKey(email)) { - emailToId.put(email, id++); - } - dsu.union(emailToId.get(account.get(1)), emailToId.get(email)); - } - } - - Map> ans = new HashMap<>(); - for (String email : emailToName.keySet()) { - int index = dsu.find(emailToId.get(email)); - ans.computeIfAbsent(index, x -> new ArrayList()).add(email); - } - for (List component : ans.values()) { - Collections.sort(component); - component.add(0, emailToName.get(component.get(0))); - } - return new ArrayList<>(ans.values()); - } - - class DSU { - int[] parent; - - public DSU() { - parent = new int[10001]; - for (int i = 0; i <= 10000; i++) { - parent[i] = i; - } - } - - public int find(int x) { - if (parent[x] != x) { - parent[x] = find(parent[x]); - } - return parent[x]; - } - - public void union(int x, int y) { - parent[find(x)] = find(y); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_723.java b/src/main/java/com/fishercoder/solutions/_723.java deleted file mode 100644 index e39609ec06..0000000000 --- a/src/main/java/com/fishercoder/solutions/_723.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 723. Candy Crush - * - * This question is about implementing a basic elimination algorithm for Candy Crush. - * Given a 2D integer array board representing the grid of candy, different positive integers board[i][j] - * represent different types of candies. - * A value of board[i][j] = 0 represents that the cell at position (i, j) is empty. - * The given board represents the state of the game following the player's move. - * Now, you need to restore the board to a stable state by crushing candies according to the following rules: - * - * If three or more candies of the same type are adjacent vertically or horizontally, - * "crush" them all at the same time - these positions become empty. - * After crushing all candies simultaneously, if an empty space on the board has candies on top of itself, - * then these candies will drop until they hit a candy or bottom at the same time. - * (No new candies will drop outside the top boundary.) - * After the above steps, there may exist more candies that can be crushed. - * If so, you need to repeat the above steps. - * If there does not exist more candies that can be crushed (ie. the board is stable), then return the current board. - * You need to perform the above rules until the board becomes stable, then return the current board. - - Example 1: - - Input: - board = - [[110,5,112,113,114],[210,211,5,213,214],[310,311,3,313,314],[410,411,412,5,414],[5,1,512,3,3],[610,4,1,613,614],[710,1,2,713,714],[810,1,2,1,1],[1,1,2,2,2],[4,1,4,4,1014]] - - Output: - [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[110,0,0,0,114],[210,0,0,0,214],[310,0,0,113,314],[410,0,0,213,414],[610,211,112,313,614],[710,311,412,613,714],[810,411,512,713,1014]] - Explanation: - - Note: - The length of board will be in the range [3, 50]. - The length of board[i] will be in the range [3, 50]. - Each board[i][j] will initially start as an integer in the range [1, 2000]. - - */ -public class _723 { - public static class Solution1 { - /**credit: https://leetcode.com/articles/candy-crush/*/ - public int[][] candyCrush(int[][] board) { - int row = board.length; - int col = board[0].length; - boolean todo = false; - for (int i = 0; i < row; i++) { - for (int j = 0; j < col - 2; j++) { - int v = Math.abs(board[i][j]); - if (v != 0 && v == Math.abs(board[i][j + 1]) && v == Math.abs(board[i][j + 2])) { - //mark all of them to be negative - board[i][j] = board[i][j + 1] = board[i][j + 2] = -v; - todo = true; - } - } - } - - for (int i = 0; i < row - 2; i++) { - for (int j = 0; j < col; j++) { - int v = Math.abs(board[i][j]); - if (v != 0 && v == Math.abs(board[i + 1][j]) && v == Math.abs(board[i + 2][j])) { - board[i + 1][j] = board[i + 2][j] = board[i][j] = -v; - todo = true; - } - } - } - - for (int j = 0; j < col; j++) { - int wr = row - 1; - for (int i = row - 1; i >= 0; i--) { - if (board[i][j] > 0) { - board[wr--][j] = board[i][j]; - } - } - while (wr >= 0) { - board[wr--][j] = 0; - } - } - - return todo ? candyCrush(board) : board; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_724.java b/src/main/java/com/fishercoder/solutions/_724.java deleted file mode 100644 index 7b9d20b34d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_724.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 724. Find Pivot Index - * - * Given an array of integers nums, write a method that returns the "pivot" index of this array. - * We define the pivot index as the index where the sum of the numbers to the left of the index is equal - * to the sum of the numbers to the right of the index. - * If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index. - - Example 1: - Input: - nums = [1, 7, 3, 6, 5, 6] - Output: 3 - Explanation: - The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3. - Also, 3 is the first index where this occurs. - - Example 2: - Input: - nums = [1, 2, 3] - Output: -1 - Explanation: - There is no index that satisfies the conditions in the problem statement. - - Note: - The length of nums will be in the range [0, 10000]. - Each element nums[i] will be an integer in the range [-1000, 1000]. - */ -public class _724 { - public static class Solution1 { - /**Space: O(n) - * Time: O(n)*/ - public int pivotIndex(int[] nums) { - if (nums == null || nums.length == 0) { - return -1; - } - int[] sums = new int[nums.length]; - sums[0] = nums[0]; - for (int i = 1; i < nums.length; i++) { - sums[i] = sums[i - 1] + nums[i]; - } - for (int i = 0; i < nums.length; i++) { - if (i == 0 && 0 == sums[nums.length - 1] - sums[i] || (i > 0 && sums[i - 1] == sums[nums.length - 1] - sums[i])) { - return i; - } - } - return -1; - } - } - - public static class Solution2 { - /**Space: O(1) - * Time: O(n)*/ - public int pivotIndex(int[] nums) { - int total = 0; - for (int num : nums) { - total += num; - } - int sum = 0; - for (int i = 0; i < nums.length; sum += nums[i++]) { - if (sum * 2 == total - nums[i]) { - return i; - } - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_725.java b/src/main/java/com/fishercoder/solutions/_725.java deleted file mode 100644 index e27e023055..0000000000 --- a/src/main/java/com/fishercoder/solutions/_725.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 725. Split Linked List in Parts - * - * Given a (singly) linked list with head node root, - * write a function to split the linked list into k consecutive linked list "parts". - * The length of each part should be as equal as possible: - * no two parts should have a size differing by more than 1. This may lead to some parts being null. - * The parts should be in order of occurrence in the input list, - * and parts occurring earlier should always have a size greater than or equal parts occurring later. - * Return a List of ListNode's representing the linked list parts that are formed. - - Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ] - - Example 1: - Input: - root = [1, 2, 3], k = 5 - Output: [[1],[2],[3],[],[]] - Explanation: - The input and each element of the output are ListNodes, not arrays. - For example, the input root has root.val = 1, root.next.val = 2, \root.next.next.val = 3, and root.next.next.next = null. - The first element output[0] has output[0].val = 1, output[0].next = null. - The last element output[4] is null, but it's string representation as a ListNode is []. - - Example 2: - Input: - root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3 - Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]] - Explanation: - The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts. - - Note: - The length of root will be in the range [0, 1000]. - Each value of a node in the input will be an integer in the range [0, 999]. - k will be an integer in the range [1, 50]. - - */ -public class _725 { - public static class Solution1 { - /**My very original solution, but verbose.*/ - public ListNode[] splitListToParts(ListNode root, int k) { - int len = getLength(root); - int aveSize = len / k; - int extra = len % k; - ListNode[] result = new ListNode[k]; - for (int i = 0; i < k; i++) { - result[i] = root; - int aveSizeTmp = aveSize; - aveSizeTmp += extra-- > 0 ? 1 : 0; - int aveSizeTmp2 = aveSizeTmp; - while (aveSizeTmp-- > 0) { - root = root.next; - } - if (result[i] != null) { - ListNode tmp = result[i]; - while (tmp.next != null && aveSizeTmp2-- > 1) { - tmp = tmp.next; - } - tmp.next = null; - } - } - return result; - } - - private int getLength(ListNode root) { - int len = 0; - ListNode tmp = root; - while (tmp != null) { - len++; - tmp = tmp.next; - } - return len; - } - } - - public static class Solution2 { - /**More concise version*/ - public ListNode[] splitListToParts(ListNode root, int k) { - int len = getLength(root); - int aveSize = len / k; - int extra = len % k; - ListNode[] result = new ListNode[k]; - ListNode prev = null; - for (int i = 0; i < k; i++, extra--) { - result[i] = root; - for (int j = 0; j < aveSize + (extra > 0 ? 1 : 0); j++) { - prev = root; - root = root.next; - } - if (prev != null) { - prev.next = null; - } - } - return result; - } - - private int getLength(ListNode root) { - int len = 0; - ListNode tmp = root; - while (tmp != null) { - len++; - tmp = tmp.next; - } - return len; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_727.java b/src/main/java/com/fishercoder/solutions/_727.java deleted file mode 100644 index c749c84411..0000000000 --- a/src/main/java/com/fishercoder/solutions/_727.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 727. Minimum Window Subsequence - * - * Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W. - * If there is no such window in S that covers all characters in T, - * return the empty string "". If there are multiple such minimum-length windows, return the one with the left-most starting index. - - Example 1: - Input: - S = "abcdebdde", T = "bde" - Output: "bcde" - - Explanation: - "bcde" is the answer because it occurs before "bdde" which has the same length. - "deb" is not a smaller window because the elements of T in the window must occur in order. - - Note: - All the strings in the input will only contain lowercase letters. - The length of S will be in the range [1, 20000]. - The length of T will be in the range [1, 100]. - */ -public class _727 { - public static class Solution1 { - /** - * This naive brute force results in TLE. - */ - public String minWindow(String S, String T) { - String result = S; - for (int i = 0; i < S.length(); i++) { - for (int j = i + T.length(); j <= S.length(); j++) { - String sub = S.substring(i, j); - if (sub.length() < result.length() && isSubsequence(T, sub)) { - result = sub; - } - } - } - return result.equals(S) ? "" : result; - } - - private boolean isSubsequence(String T, String sub) { - int i = 0; - for (int j = 0; i < T.length() && j < sub.length(); j++) { - if (T.charAt(i) == sub.charAt(j)) { - i++; - } - } - return i == T.length(); - } - } - - public static class Solution2 { - /**credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_727.java*/ - public String minWindow(String S, String T) { - int[][] dp = new int[S.length() + 1][T.length() + 1]; - int INFINITY = 1 << 29; - Arrays.fill(dp[0], INFINITY); - dp[0][0] = 0; - for (int i = 1; i <= S.length(); i++) { - for (int j = 1; j <= T.length(); j++) { - dp[i][j] = dp[i - 1][j] + 1; - if (S.charAt(i - 1) == T.charAt(j - 1)) { - dp[i][j] = dp[i - 1][j - 1] + 1; - } - } - } - int ans = INFINITY; - int tail = -1; - for (int i = 1; i <= S.length(); i++) { - if (dp[i][T.length()] < ans) { - ans = dp[i][T.length()]; - tail = i; - } - } - return ans == INFINITY ? "" : S.substring(tail - ans, tail); - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_728.java b/src/main/java/com/fishercoder/solutions/_728.java deleted file mode 100644 index d8af46d370..0000000000 --- a/src/main/java/com/fishercoder/solutions/_728.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 728. Self Dividing Numbers - * - * A self-dividing number is a number that is divisible by every digit it contains. - * For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. - * Also, a self-dividing number is not allowed to contain the digit zero. - * Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible. - - Example 1: - Input: - left = 1, right = 22 - Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22] - Note: - - The boundaries of each input argument are 1 <= left <= right <= 10000. - */ -public class _728 { - public static class Solution1 { - public List selfDividingNumbers(int left, int right) { - List result = new ArrayList<>(); - for (int num = left; num <= right; num++) { - if (isSelfDividing(num)) { - result.add(num); - } - } - return result; - } - - private boolean isSelfDividing(int num) { - int tmp = num; - while (tmp != 0) { - int digit = tmp % 10; - if (digit == 0 || num % digit != 0) { - return false; - } - tmp /= 10; - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_729.java b/src/main/java/com/fishercoder/solutions/_729.java deleted file mode 100644 index be4ba98cdd..0000000000 --- a/src/main/java/com/fishercoder/solutions/_729.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; -import java.util.TreeMap; - -/** - * 729. My Calendar I - * - * Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking. - * Your class will have the method, book(int start, int end). - * Formally, this represents a booking on the half open interval [start, end), - * the range of real numbers x such that start <= x < end. - * A double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.) - * For each call to the method MyCalendar.book, - * return true if the event can be added to the calendar successfully without causing a double booking. - * Otherwise, return false and do not add the event to the calendar. - - Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end) - - Example 1: - MyCalendar(); - MyCalendar.book(10, 20); // returns true - MyCalendar.book(15, 25); // returns false - MyCalendar.book(20, 30); // returns true - - Explanation: - - The first event can be booked. The second can't because time 15 is already booked by another event. - The third event can be booked, as the first event takes every time less than 20, but not including 20. - Note: - - The number of calls to MyCalendar.book per test case will be at most 1000. - In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9]. - - */ -public class _729 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/111205/java-8-liner-treemap - */ - public static class MyCalendar { - TreeMap calendar; - - public MyCalendar() { - calendar = new TreeMap<>(); - } - - public boolean book(int start, int end) { - Integer floorKey = calendar.floorKey(start); - if (floorKey != null && calendar.get(floorKey) > start) { - return false; - } - Integer ceilingKey = calendar.ceilingKey(start); - if (ceilingKey != null && ceilingKey < end) { - return false; - } - calendar.put(start, end); - return true; - } - } - } - - public static class Solution2 { - public class MyCalendar { - List calendar; - - MyCalendar() { - calendar = new ArrayList(); - } - - public boolean book(int start, int end) { - for (int i = 0; i < calendar.size(); i++) { - if (calendar.get(i)[0] < end && start < calendar.get(i)[1]) { - return false; - } - } - calendar.add(new int[]{start, end}); - return true; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_733.java b/src/main/java/com/fishercoder/solutions/_733.java deleted file mode 100644 index 180f964755..0000000000 --- a/src/main/java/com/fishercoder/solutions/_733.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 733. Flood Fill - * - * An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). - * Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image. - * To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting - * pixel of the same color as the starting pixel, - * plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. - * Replace the color of all of the aforementioned pixels with the newColor. - * At the end, return the modified image. - - Example 1: - Input: - image = [[1,1,1],[1,1,0],[1,0,1]] - sr = 1, sc = 1, newColor = 2 - Output: [[2,2,2],[2,2,0],[2,0,1]] - - Explanation: - From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected - by a path of the same color as the starting pixel are colored with the new color. - Note the bottom corner is not colored 2, because it is not 4-directionally connected - to the starting pixel. - - Note: - The length of image and image[0] will be in the range [1, 50]. - The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length. - The value of each color in image[i][j] and newColor will be an integer in [0, 65535]. - */ -public class _733 { - public static class Solution1 { - public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { - int[] directions = new int[]{0, 1, 0, -1, 0}; - int m = image.length; - int n = image[0].length; - int originalValue = image[sr][sc]; - image[sr][sc] = newColor; - - boolean[][] visited = new boolean[m][n]; - - Queue queue = new LinkedList<>(); - queue.offer(new int[]{sr, sc}); - while (!queue.isEmpty()) { - int[] curr = queue.poll(); - visited[curr[0]][curr[1]] = true; - for (int i = 0; i < directions.length - 1; i++) { - int nextR = curr[0] + directions[i]; - int nextC = curr[1] + directions[i + 1]; - if (nextR < 0 || nextC < 0 || nextR >= m || nextC >= n || image[nextR][nextC] != originalValue || visited[nextR][nextC]) { - continue; - } - image[nextR][nextC] = newColor; - queue.offer(new int[]{nextR, nextC}); - } - } - return image; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_734.java b/src/main/java/com/fishercoder/solutions/_734.java deleted file mode 100644 index f4e91aa217..0000000000 --- a/src/main/java/com/fishercoder/solutions/_734.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 734. Sentence Similarity - * - * Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. - * For example, "great acting skills" and "fine drama talent" are similar, if the similar word pairs are pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]]. - * Note that the similarity relation is not transitive. For example, if "great" and "fine" are similar, and "fine" and "good" are similar, "great" and "good" are not necessarily similar. - * Also, a word is always similar with itself. For example, the sentences words1 = ["great"], words2 = ["great"], pairs = [] are similar, even though there are no specified similar word pairs. - - Note: - - The length of words1 and words2 will not exceed 1000. - The length of pairs will not exceed 2000. - The length of each pairs[i] will be 2. - The length of each words[i] and pairs[i][j] will be in the range [1, 20]. - */ -public class _734 { - public static class Solution1 { - public boolean areSentencesSimilar(String[] words1, String[] words2, String[][] pairs) { - if (words1.length != words2.length) { - return false; - } - for (int i = 0; i < words1.length; i++) { - if (!isSimilar(words1[i], words2[i], pairs)) { - return false; - } - } - return true; - } - - private boolean isSimilar(String word1, String word2, String[][] pairs) { - if (word1.equals(word2)) { - return true; - } - for (int i = 0; i < pairs.length; i++) { - String[] pair = pairs[i]; - if (pair[0].equals(word1) && pair[1].equals(word2)) { - return true; - } - if (pair[0].equals(word2) && pair[1].equals(word1)) { - return true; - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_735.java b/src/main/java/com/fishercoder/solutions/_735.java deleted file mode 100644 index 8375c63571..0000000000 --- a/src/main/java/com/fishercoder/solutions/_735.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 735. Asteroid Collision - * - * We are given an array asteroids of integers representing asteroids in a row. - * For each asteroid, the absolute value represents its size, and the sign represents its direction - * (positive meaning right, negative meaning left). Each asteroid moves at the same speed. - * Find out the state of the asteroids after all collisions. - * If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet. - - Example 1: - Input: - asteroids = [5, 10, -5] - Output: [5, 10] - Explanation: - The 10 and -5 collide resulting in 10. The 5 and 10 never collide. - - Example 2: - Input: - asteroids = [8, -8] - Output: [] - Explanation: - The 8 and -8 collide exploding each other. - - Example 3: - Input: - asteroids = [10, 2, -5] - Output: [10] - Explanation: - The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10. - - Example 4: - Input: - asteroids = [-2, -1, 1, 2] - Output: [-2, -1, 1, 2] - Explanation: - The -2 and -1 are moving left, while the 1 and 2 are moving right. - Asteroids moving the same direction never meet, so no asteroids will meet each other. - - Note: - The length of asteroids will be at most 10000. - Each asteroid will be a non-zero integer in the range [-1000, 1000].. - - */ -public class _735 { - public static class Solution1 { - public int[] asteroidCollision(int[] asteroids) { - Stack stack = new Stack(); - for (int i = 0; i < asteroids.length; i++) { - if (!stack.isEmpty() && stack.peek() > 0 && asteroids[i] < 0) { - if (Math.abs(stack.peek()) < Math.abs(asteroids[i])) { - stack.pop(); - stack.push(asteroids[i]); - collide(stack); - } else if (Math.abs(stack.peek()) == Math.abs(asteroids[i])) { - stack.pop(); - } - } else { - stack.push(asteroids[i]); - } - } - int[] result = new int[stack.size()]; - int i = stack.size(); - while (!stack.isEmpty()) { - result[--i] = stack.pop(); - } - return result; - } - - private void collide(Stack stack) { - do { - Integer top = stack.pop(); - if (!stack.isEmpty() && stack.peek() * top < 0) { - if (stack.peek() < Math.abs(top)) { - stack.pop(); - stack.push(top); - } else if (stack.peek() == Math.abs(top)) { - stack.pop(); - break; - } else { - break; - } - } else if (stack.isEmpty() || stack.peek() * top > 0) { - stack.push(top); - break; - } - } while (!stack.isEmpty()); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_737.java b/src/main/java/com/fishercoder/solutions/_737.java deleted file mode 100644 index 7cdc228543..0000000000 --- a/src/main/java/com/fishercoder/solutions/_737.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 737. Sentence Similarity II - * - * Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. - * For example, words1 = ["great", "acting", "skills"] and words2 = ["fine", "drama", "talent"] are similar, - * if the similar word pairs are pairs = [["great", "good"], ["fine", "good"], ["acting","drama"], ["skills","talent"]]. - * Note that the similarity relation is transitive. - * - * For example, if "great" and "good" are similar, and "fine" and "good" are similar, then "great" and "fine" are similar. - * Similarity is also symmetric. For example, "great" and "fine" being similar is the same as "fine" and "great" being similar. - * Also, a word is always similar with itself. For example, the sentences words1 = ["great"], words2 = ["great"], pairs = [] are similar, even though there are no specified similar word pairs. - * Finally, sentences can only be similar if they have the same number of words. So a sentence like words1 = ["great"] can never be similar to words2 = ["doubleplus","good"]. - - Note: - - The length of words1 and words2 will not exceed 1000. - The length of pairs will not exceed 2000. - The length of each pairs[i] will be 2. - The length of each words[i] and pairs[i][j] will be in the range [1, 20]. - - */ -public class _737 { - public static class Solution1 { - public boolean areSentencesSimilarTwo(String[] words1, String[] words2, String[][] pairs) { - if (words1.length != words2.length) { - return false; - } - - Map map = new HashMap<>(); - for (String[] pair : pairs) { - if (!map.containsKey(pair[0])) { - map.put(pair[0], pair[0]); - } - if (!map.containsKey(pair[1])) { - map.put(pair[1], pair[1]); - } - - setParent(map, pair[0], pair[1]); - } - - for (int i = 0; i < words1.length; i++) { - String parent1 = getParent(map, words1[i]); - String parent2 = getParent(map, words2[i]); - if (!parent1.equals(parent2)) { - return false; - } - } - return true; - } - - private void setParent(Map map, String word1, String word2) { - String parent1 = getParent(map, word1); - String parent2 = getParent(map, word2); - map.put(parent1, parent2); - } - - private String getParent(Map map, String word) { - if (!map.containsKey(word)) { - return word; - } - while (!word.equals(map.get(word))) { - word = map.get(word); - } - return word; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_738.java b/src/main/java/com/fishercoder/solutions/_738.java deleted file mode 100644 index 2bdad8db68..0000000000 --- a/src/main/java/com/fishercoder/solutions/_738.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 738. Monotone Increasing Digits - * - * Given a non-negative integer N, find the largest number that is less than or equal to N with monotone increasing digits. - * (Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.) - - Example 1: - Input: N = 10 - Output: 9 - - Example 2: - Input: N = 1234 - Output: 1234 - - Example 3: - Input: N = 332 - Output: 299 - - Note: N is an integer in the range [0, 10^9]. - */ -public class _738 { - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/112808/simple-python-solution-w-explanation/2 - */ - public int monotoneIncreasingDigits(int N) { - String s = Integer.toString(N); - int index = -1; - for (int i = s.length() - 2; i >= 0; i--) { - if (s.charAt(i) > s.charAt(i + 1) || (index != -1 && s.charAt(index) == s.charAt(i))) { - index = i; - } - } - return index == -1 ? N : N - Integer.parseInt(s.substring(index + 1, s.length())) - 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_739.java b/src/main/java/com/fishercoder/solutions/_739.java deleted file mode 100644 index 247410744b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_739.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 739. Daily Temperatures - * - * Given a list of daily temperatures, produce a list that, - * for each day in the input, tells you how many days you would have to wait until a warmer temperature. - * If there is no future day for which this is possible, put 0 instead. - * For example, given the list temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0]. - * - * Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100]. - */ -public class _739 { - - public static class Solution1 { - public int[] dailyTemperatures(int[] temperatures) { - if (temperatures == null || temperatures.length == 0) { - return temperatures; - } - int[] result = new int[temperatures.length]; - for (int i = 0; i < temperatures.length; i++) { - for (int j = i + 1; j < temperatures.length; j++) { - if (temperatures[j] > temperatures[i]) { - result[i] = j - i; - break; - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_740.java b/src/main/java/com/fishercoder/solutions/_740.java deleted file mode 100644 index 961c1cefa4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_740.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 740. Delete and Earn - * - * Given an array nums of integers, you can perform operations on the array. - * In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1. - * You start with 0 points. Return the maximum number of points you can earn by applying such operations. - - Example 1: - Input: nums = [3, 4, 2] - Output: 6 - Explanation: - Delete 4 to earn 4 points, consequently 3 is also deleted. - Then, delete 2 to earn 2 points. 6 total points are earned. - - Example 2: - Input: nums = [2, 2, 3, 3, 3, 4] - Output: 9 - Explanation: - Delete 3 to earn 3 points, deleting both 2's and the 4. - Then, delete 3 again to earn 3 points, and 3 again to earn 3 points. - 9 total points are earned. - - Note: - The length of nums is at most 20000. - Each element nums[i] is an integer in the range [1, 10000].*/ - -public class _740 { - public static class Solution1 { - /** - * Since the number is within range [1, 10000], we can build another array: - * each number in the array denotes the total sum of this number that appears in this array - * and - * use the numbers themselves in the indices of another array - * - * credit: https://leetcode.com/problems/delete-and-earn/discuss/109895/JavaC++-Clean-Code-with-Explanation*/ - public int deleteAndEarn(int[] nums) { - int n = 10001; - int[] values = new int[n]; - for (int num : nums) { - values[num] += num; - } - - int take = 0; - int skip = 0; - for (int i = 0; i < n; i++) { - int takeI = skip + values[i]; - int skipI = Math.max(skip, take); - take = takeI; - skip = skipI; - } - return Math.max(take, skip); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_742.java b/src/main/java/com/fishercoder/solutions/_742.java deleted file mode 100644 index caee2cee7e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_742.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Map; -import java.util.Queue; -import java.util.Set; - -/** - * 742. Closest Leaf in a Binary Tree - * - * Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. - * Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children. - * In the following examples, the input tree is represented in flattened form row by row. The actual root tree given will be a TreeNode object. - * - * Example 1: - * Input: - * root = [1, 3, 2], k = 1 - * - * Diagram of binary tree: - * 1 - * / \ - * 3 2 - * - * Output: 2 (or 3) - * Explanation: Either 2 or 3 is the nearest leaf node to the target of 1. - * - * Example 2: - * Input: - * root = [1], k = 1 - * Output: 1 - * Explanation: The nearest leaf node is the root node itself. - * - * Example 3: - * Input: - * root = [1,2,3,4,null,null,null,5,null,6], k = 2 - * - * Diagram of binary tree: - * 1 - * / \ - * 2 3 - * / - * 4 - * / - * 5 - * / - * 6 - * - * Output: 3 - * Explanation: The leaf node with value 3 (and not the leaf node with value 6) is nearest to the node with value 2. - * Note: - * root represents a binary tree with at least 1 node and at most 1000 nodes. - * Every node has a unique node.val in range [1, 1000]. - * There exists some node in the given binary tree for which node.val == k. - * */ -public class _742 { - public static class Solution1 { - public int findClosestLeaf(TreeNode root, int k) { - Map> graph = new HashMap<>(); - Set leaves = new HashSet<>(); - buildGraph(root, graph, null, leaves); - if (leaves.contains(k)) { - return k; - } - //Now we can do a BFS traversal - Queue queue = new LinkedList<>(); - Set directNeighbors = graph.get(k); - Set visited = new HashSet<>();//use a visited set to prevent cycles and not adding the target node itself - visited.add(k); - for (int node : directNeighbors) { - queue.offer(node); - visited.add(node); - } - while (!queue.isEmpty()) { - int size = queue.size(); - for (int i = 0; i < size; i++) { - int curr = queue.poll(); - if (leaves.contains(curr)) { - return curr; - } - Set nextNodes = graph.get(curr); - for (int next : nextNodes) { - if (!visited.contains(next)) { - queue.offer(next); - visited.add(next); - } - } - } - } - return root.val; - } - - private void buildGraph(TreeNode root, Map> map, TreeNode parent, Set leaves) { - if (root == null) { - return; - } - if (!map.containsKey(root.val)) { - map.put(root.val, new HashSet<>()); - } - if (root.left != null) { - map.get(root.val).add(root.left.val); - } - if (root.right != null) { - map.get(root.val).add(root.right.val); - } - if (parent != null) { - map.get(root.val).add(parent.val); - } - if (root.left == null && root.right == null) { - leaves.add(root.val); - } - buildGraph(root.left, map, root, leaves); - buildGraph(root.right, map, root, leaves); - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_744.java b/src/main/java/com/fishercoder/solutions/_744.java deleted file mode 100644 index 53b40640a1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_744.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 744. Find Smallest Letter Greater Than Target - * - * Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger than the given target. - * Letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'. - - Examples: - Input: - letters = ["c", "f", "j"] - target = "a" - Output: "c" - - Input: - letters = ["c", "f", "j"] - target = "c" - Output: "f" - - Input: - letters = ["c", "f", "j"] - target = "d" - Output: "f" - - Input: - letters = ["c", "f", "j"] - target = "g" - Output: "j" - - Input: - letters = ["c", "f", "j"] - target = "j" - Output: "c" - - Input: - letters = ["c", "f", "j"] - target = "k" - Output: "c" - - Note: - letters has a length in range [2, 10000]. - letters consists of lowercase letters, and contains at least 2 unique letters. - target is a lowercase letter. - */ -public class _744 { - public static class Solution1 { - public char nextGreatestLetter(char[] letters, char target) { - if (letters[0] > target) { - return letters[0]; - } - int left = 0; - int right = letters.length - 1; - while (left < right) { - int mid = left + (right - left) / 2; - if (letters[mid] > target) { - while (letters[mid] > target) { - mid--; - } - return letters[++mid]; - } else { - left = mid + 1; - } - } - if (right < letters.length && letters[right] > target) { - return letters[right]; - } - return letters[0]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_746.java b/src/main/java/com/fishercoder/solutions/_746.java deleted file mode 100644 index 181972766b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_746.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 746. Min Cost Climbing Stairs - * - * On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). - * Once you pay the cost, you can either climb one or two steps. - * You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1. - - Example 1: - Input: cost = [10, 15, 20] - Output: 15 - Explanation: Cheapest is start on cost[1], pay that cost and go to the top. - - Example 2: - Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] - Output: 6 - Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3]. - - Note: - cost will have a length in the range [2, 1000]. - Every cost[i] will be an integer in the range [0, 999]. - */ - -public class _746 { - public static class Solution1 { - public int minCostClimbingStairs(int[] cost) { - int[] dp = new int[cost.length]; - dp[0] = cost[0]; - dp[1] = cost[1]; - for (int i = 2; i < cost.length; i++) { - dp[i] = Math.min(dp[i - 1] + cost[i], dp[i - 2] + cost[i]); - } - return Math.min(dp[cost.length - 1], dp[cost.length - 2]); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_747.java b/src/main/java/com/fishercoder/solutions/_747.java deleted file mode 100644 index 0325871490..0000000000 --- a/src/main/java/com/fishercoder/solutions/_747.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * 747. Largest Number Greater Than Twice of Others - * - * In a given integer array nums, there is always exactly one largest element. - * Find whether the largest element in the array is at least twice as much as every other number in the array. - * If it is, return the index of the largest element, otherwise return -1. - - Example 1: - Input: nums = [3, 6, 1, 0] - Output: 1 - Explanation: 6 is the largest integer, and for every other number in the array x, - 6 is more than twice as big as x. The index of value 6 is 1, so we return 1. - - Example 2: - Input: nums = [1, 2, 3, 4] - Output: -1 - Explanation: 4 isn't at least as big as twice the value of 3, so we return -1. - - Note: - nums will have a length in the range [1, 50]. - Every nums[i] will be an integer in the range [0, 99]. - */ -public class _747 { - - public static class Solution1 { - public int dominantIndex(int[] nums) { - Map map = new HashMap<>(); - int max; - int secondMax; - for (int i = 0; i < nums.length; i++) { - map.put(nums[i], i); - } - Arrays.sort(nums); - max = nums[nums.length - 1]; - secondMax = nums[nums.length - 2]; - if (max >= 2 * secondMax) { - return map.get(max); - } else { - return -1; - } - } - } - - public static class Solution2 { - public int dominantIndex(int[] nums) { - int max = Integer.MIN_VALUE; - int maxIndex = -1; - for (int i = 0; i < nums.length; i++) { - if (nums[i] > max) { - max = nums[i]; - maxIndex = i; - } - } - for (int i = 0; i < nums.length; i++) { - if (nums[i] * 2 > max && i != maxIndex) { - return -1; - } - } - return maxIndex; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_748.java b/src/main/java/com/fishercoder/solutions/_748.java deleted file mode 100644 index 1db44d4390..0000000000 --- a/src/main/java/com/fishercoder/solutions/_748.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 748. Shortest Completing Word - * - * Find the minimum length word from a given dictionary words, - * which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate - * - * Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word. - * It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array. - * The license plate might have the same letter occurring multiple times. For example, - * given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does. - - Example 1: - Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"] - Output: "steps" - Explanation: The smallest length word that contains the letters "S", "P", "S", and "T". - Note that the answer is not "step", because the letter "s" must occur in the word twice. - Also note that we ignored case for the purposes of comparing whether a letter exists in the word. - - Example 2: - Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"] - Output: "pest" - Explanation: There are 3 smallest length words that contains the letters "s". - We return the one that occurred first. - - Note: - licensePlate will be a string with length in range [1, 7]. - licensePlate will contain digits, spaces, or letters (uppercase or lowercase). - words will have a length in the range [10, 1000]. - Every words[i] will consist of lowercase letters, and have length in range [1, 15]. - - */ -public class _748 { - - public static class Solution1 { - public String shortestCompletingWord(String licensePlate, String[] words) { - int[] counts = new int[26]; - for (char c : licensePlate.toCharArray()) { - if (Character.isAlphabetic(c)) { - counts[Character.toLowerCase(c) - 'a']++; - } - } - String result = ""; - for (String word : words) { - if (isComplete(word, counts)) { - if (result.equals("")) { - result = word; - } else if (word.length() < result.length()) { - result = word; - } - } - } - return result; - } - - private boolean isComplete(String word, int[] counts) { - int[] tmp = counts.clone(); - for (char c : word.toCharArray()) { - if (tmp[c - 'a'] > 0) { - tmp[c - 'a']--; - } - } - for (int i : tmp) { - if (i != 0) { - return false; - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_749.java b/src/main/java/com/fishercoder/solutions/_749.java deleted file mode 100644 index 922f485f5c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_749.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 749. Contain Virus - * - * A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. - * The world is modeled as a 2-D array of cells, where 0 represents uninfected cells, - * and 1 represents cells contaminated with the virus. - * A wall (and only one wall) can be installed between any two 4-directionally adjacent cells, on the shared boundary. - * Every night, the virus spreads to all neighboring cells in all four directions unless blocked by a wall. - * Resources are limited. Each day, you can install walls around only one region -- - * the affected area (continuous block of infected cells) that threatens the most uninfected cells the following night. There will never be a tie. - * Can you save the day? If so, what is the number of walls required? If not, and the world becomes fully infected, return the number of walls used. - - Example 1: - Input: grid = - [[0,1,0,0,0,0,0,1], - [0,1,0,0,0,0,0,1], - [0,0,0,0,0,0,0,1], - [0,0,0,0,0,0,0,0]] - - Output: 10 - Explanation: - There are 2 contaminated regions. - On the first day, add 5 walls to quarantine the viral region on the left. The board after the virus spreads is: - - [[0,1,0,0,0,0,1,1], - [0,1,0,0,0,0,1,1], - [0,0,0,0,0,0,1,1], - [0,0,0,0,0,0,0,1]] - - On the second day, add 5 walls to quarantine the viral region on the right. The virus is fully contained. - - - Example 2: - Input: grid = - [[1,1,1], - [1,0,1], - [1,1,1]] - - Output: 4 - Explanation: Even though there is only one cell saved, there are 4 walls built. - Notice that walls are only built on the shared boundary of two different cells. - - - Example 3: - Input: grid = - [[1,1,1,0,0,0,0,0,0], - [1,0,1,0,1,1,1,1,1], - [1,1,1,0,0,0,0,0,0]] - Output: 13 - Explanation: The region on the left only builds two new walls. - Note: - The number of rows and columns of grid will each be in the range [1, 50]. - Each grid[i][j] will be either 0 or 1. - Throughout the described process, there is always a contiguous viral region that will infect strictly more uncontaminated squares in the next round. - */ -public class _749 { - public static class Solution1 { - public int containVirus(int[][] grid) { - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_75.java b/src/main/java/com/fishercoder/solutions/_75.java deleted file mode 100644 index d94ef21ef4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_75.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder.solutions; - -public class _75 { - - public static class Solution1 { - public void sortColors(int[] nums) { - int zero = 0; - int two = nums.length - 1; - for (int i = 0; i <= two; ) { - if (nums[i] == 0 && i > zero) { - swap(nums, i, zero++); - } else if (nums[i] == 2 && i < two) { - swap(nums, i, two--); - } else { - i++; - } - } - } - - void swap(int[] nums, int m, int n) { - int temp = nums[m]; - nums[m] = nums[n]; - nums[n] = temp; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_750.java b/src/main/java/com/fishercoder/solutions/_750.java deleted file mode 100644 index 3574f23137..0000000000 --- a/src/main/java/com/fishercoder/solutions/_750.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 750. Number Of Corner Rectangles - * - * Given a grid where each entry is only 0 or 1, find the number of corner rectangles. - * A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle. - * Note that only the corners need to have the value 1. Also, all four 1s used must be distinct. - - Example 1: - Input: grid = - [[1, 0, 0, 1, 0], - [0, 0, 1, 0, 1], - [0, 0, 0, 1, 0], - [1, 0, 1, 0, 1]] - Output: 1 - Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4]. - - Example 2: - Input: grid = - [[1, 1, 1], - [1, 1, 1], - [1, 1, 1]] - Output: 9 - Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle. - - Example 3: - Input: grid = - [[1, 1, 1, 1]] - Output: 0 - Explanation: Rectangles must have four distinct corners. - - Note: - The number of rows and columns of grid will each be in the range [1, 200]. - Each grid[i][j] will be either 0 or 1. - The number of 1s in the grid will be at most 6000.*/ -public class _750 { - public static class Solution1 { - public int countCornerRectangles(int[][] grid) { - if (grid == null || grid.length < 2) { - return 0; - } - int m = grid.length; - int n = grid[0].length; - int count = 0; - for (int i = 0; i < m - 1; i++) { - for (int j = 0; j < n - 1; j++) { - if (grid[i][j] == 1) { - for (int jNext = j + 1; jNext < n; jNext++) { - if (grid[i][jNext] == 1) { - for (int iNext = i + 1; iNext < m; iNext++) { - if (grid[iNext][j] == 1 && grid[iNext][jNext] == 1) { - count++; - } - } - } - } - } - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_751.java b/src/main/java/com/fishercoder/solutions/_751.java deleted file mode 100644 index 1cc4aeaac5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_751.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.List; - -/** - * 751. IP to CIDR - * - * Given a start IP address ip and a number of ips we need to cover n, - * return a representation of the range as a list (of smallest possible length) of CIDR blocks. - * A CIDR block is a string consisting of an IP, followed by a slash, and then the prefix length. - * For example: "123.45.67.89/20". That prefix length "20" represents the number of common prefix bits in the specified range. - * - * Example 1: - * Input: ip = "255.0.0.7", n = 10 - * Output: ["255.0.0.7/32","255.0.0.8/29","255.0.0.16/32"] - * Explanation: - * The initial ip address, when converted to binary, looks like this (spaces added for clarity): - * 255.0.0.7 -> 11111111 00000000 00000000 00000111 - * - * The address "255.0.0.7/32" specifies all addresses with a common prefix of 32 bits to the given address, - * ie. just this one address. - * - * The address "255.0.0.8/29" specifies all addresses with a common prefix of 29 bits to the given address: - * 255.0.0.8 -> 11111111 00000000 00000000 00001000 - * Addresses with common prefix of 29 bits are: - * 11111111 00000000 00000000 00001000 - * 11111111 00000000 00000000 00001001 - * 11111111 00000000 00000000 00001010 - * 11111111 00000000 00000000 00001011 - * 11111111 00000000 00000000 00001100 - * 11111111 00000000 00000000 00001101 - * 11111111 00000000 00000000 00001110 - * 11111111 00000000 00000000 00001111 - * - * The address "255.0.0.16/32" specifies all addresses with a common prefix of 32 bits to the given address, - * ie. just 11111111 00000000 00000000 00010000. - * - * In total, the answer specifies the range of 10 ips starting with the address 255.0.0.7 . - * - * There were other representations, such as: - * ["255.0.0.7/32","255.0.0.8/30", "255.0.0.12/30", "255.0.0.16/32"], - * but our answer was the shortest possible. - * - * Also note that a representation beginning with say, "255.0.0.7/30" would be incorrect, - * because it includes addresses like 255.0.0.4 = 11111111 00000000 00000000 00000100 - * that are outside the specified range. - * Note: - * ip will be a valid IPv4 address. - * Every implied address ip + x (for x < n) will be a valid IPv4 address. - * n will be an integer in the range [1, 1000]. - * */ -public class _751 { - public static class Solution1 { - public List ipToCIDR(String ip, int n) { - //TODO: implement it - return null; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_754.java b/src/main/java/com/fishercoder/solutions/_754.java deleted file mode 100644 index af1f0e198f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_754.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 754. Reach a Number - * - * You are standing at position 0 on an infinite number line. There is a goal at position target. - * On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps. - * Return the minimum number of steps required to reach the destination. - - Example 1: - Input: target = 3 - Output: 2 - Explanation: - On the first move we step from 0 to 1. - On the second step we step from 1 to 3. - - Example 2: - Input: target = 2 - Output: 3 - Explanation: - On the first move we step from 0 to 1. - On the second move we step from 1 to -1. - On the third move we step from -1 to 2. - - Note: - target will be a non-zero integer in the range [-10^9, 10^9]. - */ - -public class _754 { - public static class Solution1 { - /**Two case: - * 1. go to the right, and reach the goal exactly. - * 2. go over the goal by several steps: - * by even number, then you can choose one of the steps that went right to go back to the left (the step is half of what you went over) - * by odd number, then you keep going until you are over by an even number.*/ - public int reachNumber(int target) { - int absTarget = Math.abs(target); - int steps = 1; - int sum = 0; - while (sum < absTarget || (sum - absTarget) % 2 == 1) { - sum += steps; - steps++; - } - return steps - 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_755.java b/src/main/java/com/fishercoder/solutions/_755.java deleted file mode 100644 index e3f5287265..0000000000 --- a/src/main/java/com/fishercoder/solutions/_755.java +++ /dev/null @@ -1,154 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 755. Pour Water - * - * We are given an elevation map, heights[i] representing the height of the terrain at that index. - * The width at each index is 1. After V units of water fall at index K, how much water is at each index? - * Water first drops at index K and rests on top of the highest terrain or water at that index. - * Then, it flows according to the following rules: - - If the droplet would eventually fall by moving left, then move left. - Otherwise, if the droplet would eventually fall by moving right, then move right. - Otherwise, rise at it's current position. - - Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in that direction. Also, "level" means the height of the terrain plus any water in that column. - We can assume there's infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than 1 grid block - each unit of water has to be in exactly one block. - - Example 1: - Input: heights = [2,1,1,2,1,2,2], V = 4, K = 3 - Output: [2,2,2,3,2,2,2] - Explanation: - # # - # # - ## # ### - ######### - 0123456 <- index - - The first drop of water lands at index K = 3: - - # # - # w # - ## # ### - ######### - 0123456 - - When moving left or right, the water can only move to the same level or a lower level. - (By level, we mean the total height of the terrain plus any water in that column.) - Since moving left will eventually make it fall, it moves left. - (A droplet "made to fall" means go to a lower height than it was at previously.) - - # # - # # - ## w# ### - ######### - 0123456 - - Since moving left will not make it fall, it stays in place. The next droplet falls: - - # # - # w # - ## w# ### - ######### - 0123456 - - Since the new droplet moving left will eventually make it fall, it moves left. - Notice that the droplet still preferred to move left, - even though it could move right (and moving right makes it fall quicker.) - - # # - # w # - ## w# ### - ######### - 0123456 - - # # - # # - ##ww# ### - ######### - 0123456 - - After those steps, the third droplet falls. - Since moving left would not eventually make it fall, it tries to move right. - Since moving right would eventually make it fall, it moves right. - - # # - # w # - ##ww# ### - ######### - 0123456 - - # # - # # - ##ww#w### - ######### - 0123456 - - Finally, the fourth droplet falls. - Since moving left would not eventually make it fall, it tries to move right. - Since moving right would not eventually make it fall, it stays in place: - - # # - # w # - ##ww#w### - ######### - 0123456 - - The final answer is [2,2,2,3,2,2,2]: - - # - ####### - ####### - 0123456 - - - Example 2: - Input: heights = [1,2,3,4], V = 2, K = 2 - Output: [2,3,3,4] - Explanation: - The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower height. - - - Example 3: - Input: heights = [3,1,3], V = 5, K = 1 - Output: [4,4,4] - Note: - - heights will have length in [1, 100] and contain integers in [0, 99]. - V will be in range [0, 2000]. - K will be in range [0, heights.length - 1]. - */ - -public class _755 { - public static class Solution1 { - public int[] pourWater(int[] heights, int V, int K) { - int index; - while (V > 0) { - index = K; - for (int i = K - 1; i >= 0; i--) { - if (heights[i] > heights[index]) { - break; - } else if (heights[i] < heights[index]) { - index = i; - } - } - if (index != K) { - heights[index]++; - V--; - continue; - } - - for (int i = K + 1; i < heights.length; i++) { - if (heights[i] > heights[index]) { - break; - } else if (heights[i] < heights[index]) { - index = i; - } - } - heights[index]++; - V--; - } - return heights; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_756.java b/src/main/java/com/fishercoder/solutions/_756.java deleted file mode 100644 index 7788b6d959..0000000000 --- a/src/main/java/com/fishercoder/solutions/_756.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 756. Pyramid Transition Matrix - * - * We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like `'Z'`. - * For every block of color `C` we place not in the bottom row, - * we are placing it on top of a left block of color `A` and right block of color `B`. - * We are allowed to place the block there only if `(A, B, C)` is an allowed triple. - * We start with a bottom row of bottom, - * represented as a single string. We also start with a list of allowed triples allowed. - * Each allowed triple is represented as a string of length 3. - * Return true if we can build the pyramid all the way to the top, otherwise false. - - Example 1: - Input: bottom = "XYZ", allowed = ["XYD", "YZE", "DEA", "FFF"] - Output: true - Explanation: - We can stack the pyramid like this: - A - / \ - D E - / \ / \ - X Y Z - - This works because ('X', 'Y', 'D'), ('Y', 'Z', 'E'), and ('D', 'E', 'A') are allowed triples. - - Example 2: - Input: bottom = "XXYX", allowed = ["XXX", "XXY", "XYX", "XYY", "YXZ"] - Output: false - Explanation: - We can't stack the pyramid to the top. - Note that there could be allowed triples (A, B, C) and (A, B, D) with C != D. - - Note: - bottom will be a string with length in range [2, 8]. - allowed will have length in range [0, 200]. - Letters in all strings will be chosen from the set {'A', 'B', 'C', 'D', 'E', 'F', 'G'}. - */ -public class _756 { - public static class Solution1 { - /**credit: https://discuss.leetcode.com/topic/116042/java-solution-map-backtracking*/ - public boolean pyramidTransition(String bottom, List allowed) { - Map> map = new HashMap<>(); - for (String s : allowed) { - String key = s.substring(0, 2); - if (!map.containsKey(key)) { - map.put(key, new ArrayList<>()); - } - map.get(key).add(s.substring(2)); - } - - return helper(bottom, map); - } - - private boolean helper(String bottom, Map> map) { - if (bottom.length() == 1) { - return true; - } - for (int i = 0; i < bottom.length() - 1; i++) { - if (!map.containsKey(bottom.substring(i, i + 2))) { - return false; - } - } - List ls = new ArrayList<>(); - getList(bottom, 0, new StringBuilder(), ls, map); - for (String s : ls) { - if (helper(s, map)) { - return true; - } - } - return false; - } - - private void getList(String bottom, int idx, StringBuilder sb, List ls, - Map> map) { - if (idx == bottom.length() - 1) { - ls.add(sb.toString()); - return; - } - for (String s : map.get(bottom.substring(idx, idx + 2))) { - sb.append(s); - getList(bottom, idx + 1, sb, ls, map); - sb.deleteCharAt(sb.length() - 1); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_758.java b/src/main/java/com/fishercoder/solutions/_758.java deleted file mode 100644 index aba1b6c2f0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_758.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 758. Bold Words in String - * - * Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any letters between and tags become bold. - * The returned string should use the least number of tags possible, and of course the tags should form a valid combination. - * For example, given that words = ["ab", "bc"] and S = "aabcd", we should return "aabcd". - * Note that returning "aabcd" would use more tags, so it is incorrect. - - Note: - - words has length in range [0, 50]. - words[i] has length in range [1, 10]. - S has length in range [0, 500]. - All characters in words[i] and S are lowercase letters. - - */ - -public class _758 { - public static class Solution1 { - /**Interestingly, this problem is exactly the same as 616, using 616's code could get it AC'ed.*/ - public String boldWords(String[] words, String S) { - boolean[] shouldBold = new boolean[S.length()]; - for (int i = 0, end = 0; i < S.length(); i++) { - for (String word : words) { - if (S.startsWith(word, i)) { - end = Math.max(end, i + word.length()); - } - } - shouldBold[i] = end > i; - } - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < S.length(); i++) { - if (!shouldBold[i]) { - stringBuilder.append(S.charAt(i)); - continue; - } - int j = i; - while (j < S.length() && shouldBold[j]) { - j++; - } - stringBuilder.append("" + S.substring(i, j) + ""); - i = j - 1; - } - return stringBuilder.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_76.java b/src/main/java/com/fishercoder/solutions/_76.java deleted file mode 100644 index d117bd5237..0000000000 --- a/src/main/java/com/fishercoder/solutions/_76.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder.solutions; - -public class _76 { - - public static class Solution1 { - public String minWindow(String s, String t) { - int[] counts = new int[256]; - for (char c : t.toCharArray()) { - counts[c]++; - } - - int start = 0; - int end = 0; - int minStart = 0; - int minLen = Integer.MAX_VALUE; - int counter = t.length(); - while (end < s.length()) { - if (counts[s.charAt(end)] > 0) { - counter--; - } - - counts[s.charAt(end)]--; - end++; - - while (counter == 0) { - if (end - start < minLen) { - minStart = start; - minLen = end - start; - } - counts[s.charAt(start)]++; - if (counts[s.charAt(start)] > 0) { - counter++; - } - start++; - } - } - - if (minLen == Integer.MAX_VALUE) { - return ""; - } - return s.substring(minStart, minStart + minLen); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_760.java b/src/main/java/com/fishercoder/solutions/_760.java deleted file mode 100644 index 7baad94d3a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_760.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 760. Find Anagram Mappings - * - * Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizing the order of the elements in A. - * We want to find an index mapping P, from A to B. A mapping P[i] = j means the ith element in A appears in B at index j. - * These lists A and B may contain duplicates. If there are multiple answers, output any of them. - - For example, given - - A = [12, 28, 46, 32, 50] - B = [50, 12, 32, 46, 28] - - We should return - [1, 4, 3, 2, 0] - as P[0] = 1 because the 0th element of A appears at B[1], and P[1] = 4 because the 1st element of A appears at B[4], and so on. - - Note: - - A, B have equal lengths in range [1, 100]. - A[i], B[i] are integers in range [0, 10^5]. - */ -public class _760 { - public static class Solution1 { - public int[] anagramMappings(int[] A, int[] B) { - int[] result = new int[A.length]; - for (int i = 0; i < A.length; i++) { - for (int j = 0; j < B.length; j++) { - if (A[i] == B[j]) { - result[i] = j; - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_762.java b/src/main/java/com/fishercoder/solutions/_762.java deleted file mode 100644 index ef6e3d7b39..0000000000 --- a/src/main/java/com/fishercoder/solutions/_762.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 762. Prime Number of Set Bits in Binary Representation - * - * Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. - * (Recall that the number of set bits an integer has is the number of 1s present when written in binary. - * For example, 21 written in binary is 10101 which has 3 set bits. Also, 1 is not a prime.) - - Example 1: - - Input: L = 6, R = 10 - Output: 4 - - Explanation: - 6 -> 110 (2 set bits, 2 is prime) - 7 -> 111 (3 set bits, 3 is prime) - 9 -> 1001 (2 set bits , 2 is prime) - 10->1010 (2 set bits , 2 is prime) - - Example 2: - - Input: L = 10, R = 15 - Output: 5 - - Explanation: - 10 -> 1010 (2 set bits, 2 is prime) - 11 -> 1011 (3 set bits, 3 is prime) - 12 -> 1100 (2 set bits, 2 is prime) - 13 -> 1101 (3 set bits, 3 is prime) - 14 -> 1110 (3 set bits, 3 is prime) - 15 -> 1111 (4 set bits, 4 is not prime) - - Note: - - L, R will be integers L <= R in the range [1, 10^6]. - R - L will be at most 10000. - */ - -public class _762 { - public static class Solution1 { - public int countPrimeSetBits(int L, int R) { - int count = 0; - for (int i = L; i <= R; i++) { - if (hasPrimeNumberSetBits(i)) { - count++; - } - } - return count; - } - - private boolean hasPrimeNumberSetBits(int num) { - int k = getSetBits(num); - if (k <= 1) { - return false; - } - for (int i = 2; i * i <= k; i++) { - if (k % i == 0) { - return false; - } - } - return true; - } - - private int getSetBits(int n) { - int bits = 0; - while (n != 0) { - bits++; - n &= (n - 1); - } - return bits; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_763.java b/src/main/java/com/fishercoder/solutions/_763.java deleted file mode 100644 index 0a5168bf1c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_763.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 763. Partition Labels - * - * A string S of lowercase letters is given. - * We want to partition this string into as many parts as possible so that each letter appears - * in at most one part, and return a list of integers representing the size of these parts. - - Example 1: - Input: S = "ababcbacadefegdehijhklij" - Output: [9,7,8] - Explanation: - The partition is "ababcbaca", "defegde", "hijhklij". - This is a partition so that each letter appears in at most one part. - A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts. - - Note: - S will have length in range [1, 500]. - S will consist of lowercase letters ('a' to 'z') only. - */ -public class _763 { - - public static class Solution1 { - public List partitionLabels(String S) { - List result = new ArrayList<>(); - int[] last = new int[26]; - /**This is the key step: - * we find the last occurrence of each letter and record them in last[]*/ - for (int i = 0; i < S.length(); i++) { - last[S.charAt(i) - 'a'] = i; - } - /**record the last end index of the current substring*/ - int end = 0; - int start = 0; - for (int i = 0; i < S.length(); i++) { - end = Math.max(end, last[S.charAt(i) - 'a']); - if (end == i) { - result.add(end - start + 1); - start = end + 1; - } - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_764.java b/src/main/java/com/fishercoder/solutions/_764.java deleted file mode 100644 index f32eb6df10..0000000000 --- a/src/main/java/com/fishercoder/solutions/_764.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 764. Largest Plus Sign - * - * In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, - * except those cells in the given list mines which are 0. - * What is the largest axis-aligned plus sign of 1s contained in the grid? Return the order of the plus sign. - * If there is none, return 0. - * - * An "axis-aligned plus sign of 1s of order k" has some center grid[x][y] = 1 along with 4 arms of length k-1 going up, down, left, and right, and made of 1s. - * This is demonstrated in the diagrams below. - * Note that there could be 0s or 1s beyond the arms of the plus sign, - * only the relevant area of the plus sign is checked for 1s. - - Examples of Axis-Aligned Plus Signs of Order k: - - Order 1: - 000 - 010 - 000 - - Order 2: - 00000 - 00100 - 01110 - 00100 - 00000 - - Order 3: - 0000000 - 0001000 - 0001000 - 0111110 - 0001000 - 0001000 - 0000000 - - - Example 1: - Input: N = 5, mines = [[4, 2]] - Output: 2 - Explanation: - 11111 - 11111 - 11111 - 11111 - 11011 - In the above grid, the largest plus sign can only be order 2. One of them is marked in bold. - - Example 2: - Input: N = 2, mines = [] - Output: 1 - Explanation: - There is no plus sign of order 2, but there is of order 1. - - Example 3: - Input: N = 1, mines = [[0, 0]] - Output: 0 - Explanation: - There is no plus sign, so return 0. - - Note: - N will be an integer in the range [1, 500]. - mines will have length at most 5000. - mines[i] will be length 2 and consist of integers in the range [0, N-1]. - (Additionally, programs submitted in C, C++, or C# will be judged with a slightly smaller time limit.) - - */ - -public class _764 { - public static class Solution1 { - /**Brute force - * - * Time: O(N^3) - * Space: O(mines.length)*/ - public int orderOfLargestPlusSign(int N, int[][] mines) { - Set banned = new HashSet<>(); - for (int[] mine : mines) { - banned.add(mine[0] * N + mine[1]); - } - int result = 0; - for (int row = 0; row < N; row++) { - for (int col = 0; col < N; col++) { - int k = 0; - while (k <= row && row < N - k && k <= col && col < N - k - && !banned.contains((row - k) * N + col) - && !banned.contains((row + k) * N + col) - && !banned.contains(row * N + col - k) - && !banned.contains(row * N + col + k)) { - k++; - } - result = Math.max(result, k); - } - } - return result; - } - } - - public static class Solution2 { - /**Dp - * - * Time: O(N^2) - * Space: O(N^2) - * Credit: https://leetcode.com/articles/largest-plus-sign/*/ - public int orderOfLargestPlusSign(int N, int[][] mines) { - Set banned = new HashSet<>(); - for (int[] mine : mines) { - banned.add(mine[0] * N + mine[1]); - } - - int[][] dp = new int[N][N]; - - for (int row = 0; row < N; row++) { - int count = 0; - for (int col = 0; col < N; col++) { - count = banned.contains(row * N + col) ? 0 : count + 1; - dp[row][col] = count; - } - - count = 0; - for (int col = N - 1; col >= 0; col--) { - count = banned.contains(row * N + col) ? 0 : count + 1; - dp[row][col] = Math.min(dp[row][col], count); - } - } - - int result = 0; - for (int col = 0; col < N; col++) { - int count = 0; - for (int row = 0; row < N; row++) { - count = banned.contains(row * N + col) ? 0 : count + 1; - dp[row][col] = Math.min(dp[row][col], count); - } - - count = 0; - for (int row = N - 1; row >= 0; row--) { - count = banned.contains(row * N + col) ? 0 : count + 1; - dp[row][col] = Math.min(dp[row][col], count); - result = Math.max(result, dp[row][col]); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_765.java b/src/main/java/com/fishercoder/solutions/_765.java deleted file mode 100644 index e9d2dbae77..0000000000 --- a/src/main/java/com/fishercoder/solutions/_765.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 765. Couples Holding Hands - * - * N couples sit in 2N seats arranged in a row and want to hold hands. - * We want to know the minimum number of swaps so that every couple is sitting side by side. - * A swap consists of choosing any two people, then they stand up and switch seats. - * The people and seats are represented by an integer from 0 to 2N-1, the couples are numbered in order, - * the first couple being (0, 1), the second couple being (2, 3), and so on with the last couple being (2N-2, 2N-1). - * The couples' initial seating is given by row[i] being the value of the person who is initially sitting in the i-th seat. - - Example 1: - - Input: row = [0, 2, 1, 3] - Output: 1 - Explanation: We only need to swap the second (row[1]) and third (row[2]) person. - - Example 2: - - Input: row = [3, 2, 0, 1] - Output: 0 - Explanation: All couples are already seated side by side. - - Note: - len(row) is even and in the range of [4, 60]. - row is guaranteed to be a permutation of 0...len(row)-1. - */ - -public class _765 { - public static class Solution1 { - public int minSwapsCouples(int[] row) { - int swaps = 0; - for (int i = 0; i < row.length - 1; i += 2) { - int coupleValue = row[i] % 2 == 0 ? row[i] + 1 : row[i] - 1; - if (row[i + 1] != coupleValue) { - swaps++; - int coupleIndex = findIndex(row, coupleValue); - swap(row, coupleIndex, i + 1); - } - } - return swaps; - } - - private void swap(int[] row, int i, int j) { - int tmp = row[i]; - row[i] = row[j]; - row[j] = tmp; - } - - private int findIndex(int[] row, int value) { - for (int i = 0; i < row.length; i++) { - if (row[i] == value) { - return i; - } - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_766.java b/src/main/java/com/fishercoder/solutions/_766.java deleted file mode 100644 index c4668381b4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_766.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -/**766. Toeplitz Matrix - * - * A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. - - Now given an M x N matrix, return True if and only if the matrix is Toeplitz. - - Example 1: - - Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] - Output: True - Explanation: - 1234 - 5123 - 9512 - - In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]", - and in each diagonal all elements are the same, so the answer is True. - - Example 2: - - Input: matrix = [[1,2],[2,2]] - Output: False - Explanation: - The diagonal "[1, 2]" has different elements. - - Note: - - matrix will be a 2D array of integers. - matrix will have a number of rows and columns in range [1, 20]. - matrix[i][j] will be integers in range [0, 99]. - - */ -public class _766 { - public static class Solution1 { - public boolean isToeplitzMatrix(int[][] matrix) { - int m = matrix.length; - int n = matrix[0].length; - int i = 0; - int j = 0; - int sameVal = matrix[i][j]; - while (++i < m && ++j < n) { - if (matrix[i][j] != sameVal) { - return false; - } - } - - for (i = 1, j = 0; i < m; i++) { - int tmpI = i; - int tmpJ = j; - sameVal = matrix[i][j]; - while (++tmpI < m && ++tmpJ < n) { - if (matrix[tmpI][tmpJ] != sameVal) { - return false; - } - } - } - for (i = 0, j = 1; j < n; j++) { - int tmpJ = j; - int tmpI = i; - sameVal = matrix[tmpI][tmpJ]; - while (++tmpI < m && ++tmpJ < n) { - if (matrix[tmpI][tmpJ] != sameVal) { - return false; - } - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_767.java b/src/main/java/com/fishercoder/solutions/_767.java deleted file mode 100644 index 62722ec68d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_767.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; -import java.util.PriorityQueue; - -/** - * 767. Reorganize String - * - * Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. - - If possible, output any possible result. If not possible, return the empty string. - - Example 1: - - Input: S = "aab" - Output: "aba" - - Example 2: - - Input: S = "aaab" - Output: "" - - Note: - - S will consist of lowercase letters and have length in range [1, 500]. - */ - -public class _767 { - public static class Solution1 { - public String reorganizeString(String S) { - Map map = new HashMap<>(); - for (char c : S.toCharArray()) { - map.put(c, map.getOrDefault(c, 0) + 1); - } - int len = S.length(); - for (char c : map.keySet()) { - if ((len % 2 == 0 && map.get(c) > len / 2) || (len % 2 != 0 && map.get(c) >= len / 2 + 2)) { - return ""; - } - } - PriorityQueue queue = new PriorityQueue<>((a, b) -> b.count - a.count); - for (char c : map.keySet()) { - queue.offer(new CustChar(c, map.get(c))); - } - - StringBuilder sb = new StringBuilder(); - while (!queue.isEmpty()) { - CustChar curr = queue.poll(); - char c = curr.c; - if (sb.length() > 0 && sb.charAt(sb.length() - 1) != c) { - sb.append(c); - if (curr.count > 1) { - queue.offer(new CustChar(c, curr.count - 1)); - } - } else if (sb.length() == 0) { - sb.append(c); - if (curr.count > 1) { - queue.offer(new CustChar(c, curr.count - 1)); - } - } else if (sb.length() > 0 && sb.charAt(sb.length() - 1) == c && !queue.isEmpty()) { - CustChar next = queue.poll(); - sb.append(next.c); - if (next.count > 1) { - queue.offer(new CustChar(next.c, next.count - 1)); - } - queue.offer(curr); - } - } - return sb.toString(); - } - - class CustChar { - Character c; - int count; - - public CustChar(Character c, int count) { - this.c = c; - this.count = count; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_769.java b/src/main/java/com/fishercoder/solutions/_769.java deleted file mode 100644 index 96221b5aa4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_769.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 769. Max Chunks To Make Sorted - - Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into some number of "chunks" (partitions), and individually sort each chunk. - After concatenating them, the result equals the sorted array. - - What is the most number of chunks we could have made? - - Example 1: - - Input: arr = [4,3,2,1,0] - Output: 1 - Explanation: - Splitting into two or more chunks will not return the required result. - For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn't sorted. - - Example 2: - - Input: arr = [1,0,2,3,4] - Output: 4 - Explanation: - We can split into two chunks, such as [1, 0], [2, 3, 4]. - However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible. - - Note: - - arr will have length in range [1, 10]. - arr[i] will be a permutation of [0, 1, ..., arr.length - 1]. - - */ -public class _769 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/max-chunks-to-make-sorted/discuss/113520/Java-solution-left-max-and-right-min.*/ - public int maxChunksToSorted(int[] arr) { - int len = arr.length; - - int[] maxOfLeft = new int[len]; - maxOfLeft[0] = arr[0]; - for (int i = 1; i < len; i++) { - maxOfLeft[i] = Math.max(arr[i], maxOfLeft[i - 1]); - } - - int[] minOfRight = new int[len]; - minOfRight[len - 1] = arr[len - 1]; - for (int i = len - 2; i >= 0; i--) { - minOfRight[i] = Math.min(minOfRight[i + 1], arr[i]); - } - - int result = 0; - for (int i = 0; i < len - 1; i++) { - if (maxOfLeft[i] <= minOfRight[i + 1]) { - result++; - } - } - return result + 1; - } - } - - public static class Solution2 { - /**credit: https://leetcode.com/articles/max-chunks-to-make-sorted-i/*/ - public int maxChunksToSorted(int[] arr) { - int ans = 0; - int max = 0; - for (int i = 0; i < arr.length; ++i) { - max = Math.max(max, arr[i]); - if (max == i) { - ans++; - } - } - return ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_77.java b/src/main/java/com/fishercoder/solutions/_77.java deleted file mode 100644 index 562af3ab8c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_77.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -public class _77 { - - public static class Solution1 { - public List> combine(int n, int k) { - List> result = new ArrayList(); - int[] nums = new int[n]; - for (int i = 0; i < n; i++) { - nums[i] = i + 1; - } - backtracking(k, 0, nums, new ArrayList(), result); - return result; - } - - void backtracking(int k, int start, int[] nums, List curr, List> result) { - if (curr.size() == k) { - result.add(new ArrayList(curr)); - } else if (curr.size() < k) { - for (int i = start; i < nums.length; i++) { - curr.add(nums[i]); - backtracking(k, i + 1, nums, curr, result); - curr.remove(curr.size() - 1); - } - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_771.java b/src/main/java/com/fishercoder/solutions/_771.java deleted file mode 100644 index fb3751f83a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_771.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 771. Jewels and Stones - - You're given strings J representing the types of stones that are jewels, and S representing the stones you have. - Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels. - The letters in J are guaranteed distinct, and all characters in J and S are letters. - Letters are case sensitive, so "a" is considered a different type of stone from "A". - - Example 1: - Input: J = "aA", S = "aAAbbbb" - Output: 3 - - Example 2: - Input: J = "z", S = "ZZ" - Output: 0 - - Note: - S and J will consist of letters and have length at most 50. - The characters in J are distinct. - */ - -public class _771 { - public static class Solution1 { - public int numJewelsInStones(String J, String S) { - Set set = new HashSet<>(); - for (char c : J.toCharArray()) { - set.add(c); - } - int count = 0; - for (char c : S.toCharArray()) { - if (set.contains(c)) { - count++; - } - } - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_776.java b/src/main/java/com/fishercoder/solutions/_776.java deleted file mode 100644 index 21c45aebe7..0000000000 --- a/src/main/java/com/fishercoder/solutions/_776.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 776. Split BST - * - * Given a Binary Search Tree (BST) with root node root, and a target value V, - * split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, - * while the other subtree has all nodes that are greater than the target value. - * It's not necessarily the case that the tree contains a node with value V. - * Additionally, most of the structure of the original tree should remain. - * Formally, for any child C with parent P in the original tree, - * if they are both in the same subtree after the split, then node C should still have the parent P. - * You should output the root TreeNode of both subtrees after splitting, in any order. - - Example 1: - - Input: root = [4,2,6,1,3,5,7], V = 2 - Output: [[2,1],[4,3,6,null,null,5,7]] - - Explanation: - Note that root, output[0], and output[1] are TreeNode objects, not arrays. - - The given tree [4,2,6,1,3,5,7] is represented by the following diagram: - - 4 - / \ - 2 6 - / \ / \ - 1 3 5 7 - - while the diagrams for the outputs are: - - 4 - / \ -3 6 and 2 - / \ / - 5 7 1 - - Note: - - The size of the BST will not exceed 50. - The BST is always valid and each node's value is different. - */ - -public class _776 { - public static class Solution1 { - /** credit: https://discuss.leetcode.com/topic/119481/recursive-java-solution */ - public TreeNode[] splitBST(TreeNode root, int V) { - TreeNode small = new TreeNode(0); - TreeNode big = new TreeNode(0); - split(root, V, small, big); - return new TreeNode[] {small.right, big.left}; - } - - private void split(TreeNode root, int v, TreeNode small, TreeNode big) { - if (root == null) { - return; - } - if (root.val <= v) { - small.right = root; - TreeNode right = root.right; - root.right = null; - split(right, v, root, big); - } else { - big.left = root; - TreeNode left = root.left; - root.left = null; - split(left, v, small, root); - } - } - } - - public static class Solution2 { - /** credit: https://leetcode.com/articles/split-bst/ */ - public TreeNode[] splitBST(TreeNode root, int V) { - if (root == null) { - return new TreeNode[] {null, null}; - } else if (root.val <= V) { - TreeNode[] result = splitBST(root.right, V); - root.right = result[0]; - result[0] = root; - return result; - } else { - TreeNode[] result = splitBST(root.left, V); - root.left = result[1]; - result[1] = root; - return result; - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_779.java b/src/main/java/com/fishercoder/solutions/_779.java deleted file mode 100644 index b2d6c49cc0..0000000000 --- a/src/main/java/com/fishercoder/solutions/_779.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 779. K-th Symbol in Grammar - * - * On the first row, we write a 0. Now in every subsequent row, - * we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. - * Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). - - Examples: - Input: N = 1, K = 1 - Output: 0 - - Input: N = 2, K = 1 - Output: 0 - - Input: N = 2, K = 2 - Output: 1 - - Input: N = 4, K = 5 - Output: 1 - - Explanation: - row 1: 0 - row 2: 01 - row 3: 0110 - row 4: 01101001 - - Note: - - N will be an integer in the range [1, 30]. - K will be an integer in the range [1, 2^(N-1)]. - */ - -public class _779 { - public static class Solution1 { - /**Time: O(2^n) - * Space: O(2^n) - * This will result int TLE.*/ - public int kthGrammar(int N, int K) { - List> lists = new ArrayList<>(); - lists.add(Arrays.asList(0)); - for (int i = 1; i <= N; i++) { - List curr = new ArrayList<>(); - List prev = lists.get(i - 1); - for (int j = 0; j < prev.size(); j++) { - if (prev.get(j) == 0) { - curr.add(0); - curr.add(1); - } else { - curr.add(1); - curr.add(0); - } - } - lists.add(curr); - } - return lists.get(N).get(K - 1); - } - } - - public static class Solution2 { - /**Time: O(logn) - * Space: O(1)*/ - public int kthGrammar(int N, int K) { - return Integer.bitCount(K - 1) % 2; - } - - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_783.java b/src/main/java/com/fishercoder/solutions/_783.java deleted file mode 100644 index 584b98139e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_783.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; -import java.util.ArrayList; -import java.util.List; - -/** - * 783. Minimum Distance Between BST Nodes - * - * Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree. - - Example : - - Input: root = [4,2,6,1,3,null,null] - Output: 1 - Explanation: - Note that root is a TreeNode object, not an array. - - The given tree [4,2,6,1,3,null,null] is represented by the following diagram: - - 4 - / \ - 2 6 - / \ - 1 3 - - while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2. - - Note: - - The size of the BST will be between 2 and 100. - The BST is always valid, each node's value is an integer, and each node's value is different. - */ - -public class _783 { - public static class Solution1 { - public int minDiffInBST(TreeNode root) { - List inorder = new ArrayList<>(); - inorder(root, inorder); - return findMinDiff(inorder); - } - - private int findMinDiff(List inorder) { - int minDiff = Integer.MAX_VALUE; - for (int i = 1; i < inorder.size(); i++) { - minDiff = Math.min(minDiff, inorder.get(i) - inorder.get(i - 1)); - } - return minDiff; - } - - private void inorder(TreeNode root, List inorder) { - if (root == null) { - return; - } - inorder(root.left, inorder); - inorder.add(root.val); - inorder(root.right, inorder); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_784.java b/src/main/java/com/fishercoder/solutions/_784.java deleted file mode 100644 index 3178d8e9f7..0000000000 --- a/src/main/java/com/fishercoder/solutions/_784.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 784. Letter Case Permutation - * - * Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. - * Return a list of all possible strings we could create. - - Examples: - Input: S = "a1b2" - Output: ["a1b2", "a1B2", "A1b2", "A1B2"] - - Input: S = "3z4" - Output: ["3z4", "3Z4"] - - Input: S = "12345" - Output: ["12345"] - - Note: - - S will be a string with length at most 12. - S will consist only of letters or digits. - - */ - -public class _784 { - public static class Solution1 { - public List letterCasePermutation(String S) { - Set result = new HashSet<>(); - result.add(S); - for (int i = 0; i < S.length(); i++) { - if (Character.isAlphabetic(S.charAt(i))) { - Set newResult = new HashSet<>(); - for (String word : result) { - if (Character.isUpperCase(word.charAt(i))) { - StringBuilder sb = new StringBuilder(); - for (int j = 0; j < i; j++) { - sb.append(word.charAt(j)); - } - sb.append(Character.toLowerCase(word.charAt(i))); - for (int j = i + 1; j < word.length(); j++) { - sb.append(word.charAt(j)); - } - newResult.add(sb.toString()); - } else { - StringBuilder sb = new StringBuilder(); - for (int j = 0; j < i; j++) { - sb.append(word.charAt(j)); - } - sb.append(Character.toUpperCase(word.charAt(i))); - for (int j = i + 1; j < word.length(); j++) { - sb.append(word.charAt(j)); - } - newResult.add(sb.toString()); - } - } - result.addAll(newResult); - } - } - return new ArrayList<>(result); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_788.java b/src/main/java/com/fishercoder/solutions/_788.java deleted file mode 100644 index 6cd5478f9c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_788.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 788. Rotated Digits - * - * X is a good number if after rotating each digit individually by 180 degrees, - * we get a valid number that is different from X. - * A number is valid if each digit remains a digit after rotation. - * 0, 1, and 8 rotate to themselves; - * 2 and 5 rotate to each other; - * 6 and 9 rotate to each other, - * and the rest of the numbers do not rotate to any other number. - - Now given a positive number N, how many numbers X from 1 to N are good? - - Example: - Input: 10 - Output: 4 - - Explanation: - There are four good numbers in the range [1, 10] : 2, 5, 6, 9. - Note that 1 and 10 are not good numbers, since they remain unchanged after rotating. - - Note: N will be in range [1, 10000]. - */ -public class _788 { - public static class Solution1 { - public int rotatedDigits(int N) { - int count = 0; - Map map = new HashMap<>(); - map.put('0', "0"); - map.put('1', "1"); - map.put('8', "8"); - map.put('2', "5"); - map.put('5', "2"); - map.put('6', "9"); - map.put('9', "6"); - for (int i = 1; i <= N; i++) { - if (isRotatedNumber(i, map)) { - count++; - } - } - return count; - } - - private boolean isRotatedNumber(int num, Map map) { - String originalNum = String.valueOf(num); - StringBuilder sb = new StringBuilder(); - for (char c : String.valueOf(num).toCharArray()) { - if (!map.containsKey(c)) { - return false; - } else { - sb.append(map.get(c)); - } - } - return !originalNum.equals(sb.toString()); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_789.java b/src/main/java/com/fishercoder/solutions/_789.java deleted file mode 100644 index 89ed5e6d93..0000000000 --- a/src/main/java/com/fishercoder/solutions/_789.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 789. Escape The Ghosts - * - * You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is - * (target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], ghosts[i][1]). - * - * Each turn, you and all ghosts simultaneously *may* move in one of 4 cardinal directions: north, east, west, or - * south, going from the previous point to a new point 1 unit of distance away. - * - * You escape if and only if you can reach the target before any ghost reaches you (for any given moves the ghosts - * may take.) If you reach any square (including the target) at the same time as a ghost, it doesn't count as an - * escape. - * - * Return True if and only if it is possible to escape. - */ - -public class _789 { - - public static class Solution { - public boolean escapeGhosts(int[][] ghosts, int[] target) { - int[] currPos = {0, 0}; - int selfDist = getDist(currPos, target); - - for (int[] ghost : ghosts) { - int ghostDist = getDist(ghost, target); - if (ghostDist <= selfDist) { - return false; - } - } - return true; - } - - private int getDist(int[] p1, int[] p2) { - return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_79.java b/src/main/java/com/fishercoder/solutions/_79.java deleted file mode 100644 index d0504f2dce..0000000000 --- a/src/main/java/com/fishercoder/solutions/_79.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder.solutions; - -public class _79 { - - public static class Solution1 { - - public boolean exist(char[][] board, String word) { - int m = board.length; - int n = board[0].length; - boolean[][] visited = new boolean[m][n]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (dfs(board, visited, i, j, word, 0)) { - return true; - } - } - } - return false; - } - - final int[] dirs = new int[]{0, 1, 0, -1, 0}; - - boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index) { - if (index >= word.length() || word.charAt(index) != board[row][col]) { - return false; - } else if (index == word.length() - 1 && word.charAt(index) == board[row][col]) { - visited[row][col] = true; - return true; - } - visited[row][col] = true;//set it to true for this case - boolean result = false; - for (int i = 0; i < 4; i++) { - int nextRow = row + dirs[i]; - int nextCol = col + dirs[i + 1]; - if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) { - continue; - } - result = dfs(board, visited, nextRow, nextCol, word, index + 1); - if (result) { - return result; - } else { - visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!! - } - } - return result; - } - } - - public static class Solution2 { - //credit: https://discuss.leetcode.com/topic/21142/my-java-solution - - boolean[][] visited; - - public boolean exist(char[][] board, String word) { - int m = board.length; - int n = board[0].length; - visited = new boolean[m][n]; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (word.charAt(0) == board[i][j] && search(board, word, i, j, 0)) { - return true; - } - } - } - return false; - } - - boolean search(char[][] board, String word, int i, int j, int pos) { - if (pos == word.length()) { - return true; - } - if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || word.charAt(pos) != board[i][j] || visited[i][j]) { - return false; - } - visited[i][j] = true; - if (search(board, word, i + 1, j, pos + 1) - || search(board, word, i - 1, j, pos + 1) - || search(board, word, i, j + 1, pos + 1) - || search(board, word, i, j - 1, pos + 1)) { - return true; - } - - visited[i][j] = false; - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_791.java b/src/main/java/com/fishercoder/solutions/_791.java deleted file mode 100644 index 7acb446860..0000000000 --- a/src/main/java/com/fishercoder/solutions/_791.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 791. Custom Sort String - - S and T are strings composed of lowercase letters. In S, no letter occurs more than once. - - S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string. - - Return any permutation of T (as a string) that satisfies this property. - - Example : - Input: - S = "cba" - T = "abcd" - Output: "cbad" - Explanation: - "a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a". - Since "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs. - - Note: - - S has length at most 26, and no character is repeated in S. - T has length at most 200. - S and T consist of lowercase letters only. - - */ -public class _791 { - public static class Solution1 { - public String customSortString(String S, String T) { - Map map = new HashMap<>(); - for (char c : T.toCharArray()) { - map.put(c, map.getOrDefault(c, 0) + 1); - } - StringBuilder sb = new StringBuilder(); - for (char c : S.toCharArray()) { - if (map.containsKey(c)) { - int count = map.get(c); - while (count-- > 0) { - sb.append(c); - } - map.remove(c); - } - } - for (char c : map.keySet()) { - int count = map.get(c); - while (count-- > 0) { - sb.append(c); - } - } - return sb.toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_796.java b/src/main/java/com/fishercoder/solutions/_796.java deleted file mode 100644 index 5cc078295b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_796.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 796. Rotate String - - We are given two strings, A and B. - - A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A. - - Example 1: - Input: A = 'abcde', B = 'cdeab' - Output: true - - Example 2: - Input: A = 'abcde', B = 'abced' - Output: false - - Note: - - A and B will have length at most 100. - */ -public class _796 { - public static class Solution1 { - public boolean rotateString(String A, String B) { - if (A.length() != B.length()) { - return false; - } - for (int i = 0; i < A.length(); i++) { - if ((A.substring(i) + A.substring(0, i)).equals(B)) { - return true; - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_799.java b/src/main/java/com/fishercoder/solutions/_799.java deleted file mode 100644 index 85a0ccfd17..0000000000 --- a/src/main/java/com/fishercoder/solutions/_799.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 799. Champagne Tower - - We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so on until the 100th row. Each glass holds one cup (250ml) of champagne. - Then, some champagne is poured in the first glass at the top. - When the top most glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it. - When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on. - (A glass at the bottom row has it's excess champagne fall on the floor.) - For example, after one cup of champagne is poured, the top most glass is full. - After two cups of champagne are poured, the two glasses on the second row are half full. - After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now. - After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below. - Now after pouring some non-negative integer cups of champagne, return how full the j-th glass in the i-th row is (both i and j are 0 indexed.) - - Example 1: - Input: poured = 1, query_glass = 1, query_row = 1 - Output: 0.0 - Explanation: We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty. - - Example 2: - Input: poured = 2, query_glass = 1, query_row = 1 - Output: 0.5 - Explanation: We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange. - - Note: - poured will be in the range of [0, 10 ^ 9]. - query_glass and query_row will be in the range of [0, 99]. - - * - */ -public class _799 { - public static class Solution1 { - public double champagneTower(int poured, int queryRow, int queryGlass) { - double[][] dp = new double[101][101]; - dp[0][0] = poured; - for (int row = 0; row <= queryRow; row++) { - for (int col = 0; col <= row; col++) { - double quantity = (dp[row][col] - 1.0) / 2.0; - if (quantity > 0) { - dp[row + 1][col] += quantity; - dp[row + 1][col + 1] += quantity; - } - } - } - return Math.min(dp[queryRow][queryGlass], 1.0); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_8.java b/src/main/java/com/fishercoder/solutions/_8.java deleted file mode 100644 index 5b32e6905a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_8.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -public class _8 { - - public static class Solution1 { - /** - * four corner cases: - * 1. discards all leading zeroes - * 2. sign of the number - * 3. overflow - * 4. invalid input - * */ - public int myAtoi(String str) { - int pointer = 0; - int result = 0; - while (pointer < str.length() && Character.isWhitespace(str.charAt(pointer))) { - pointer++; - } - if (pointer == str.length()) { - return 0; - } - boolean negativeFlag = (str.charAt(pointer) == '-'); - if (str.charAt(pointer) == '+' || str.charAt(pointer) == '-') { - pointer++; - } - for (; pointer < str.length(); pointer++) { - if (str.charAt(pointer) > '9' || str.charAt(pointer) < '0') { - break; - } else { - int digit = str.charAt(pointer) - '0'; - if (!negativeFlag && result > (Integer.MAX_VALUE - digit) / 10) { - return Integer.MAX_VALUE; - } else if (negativeFlag && result < (Integer.MIN_VALUE + digit) / 10) { - return Integer.MIN_VALUE; - } - result = result * 10 + (negativeFlag ? -digit : digit); - } - } - return result; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_800.java b/src/main/java/com/fishercoder/solutions/_800.java deleted file mode 100644 index a48871e748..0000000000 --- a/src/main/java/com/fishercoder/solutions/_800.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 800. Similar RGB Color - - In the following, every capital letter represents some hexadecimal digit from 0 to f. - - The red-green-blue color "#AABBCC" can be written as "#ABC" in shorthand. For example, "#15c" is shorthand for the color "#1155cc". - - Now, say the similarity between two colors "#ABCDEF" and "#UVWXYZ" is -(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2. - - Given the color "#ABCDEF", return a 7 character color that is most similar to #ABCDEF, and has a shorthand (that is, it can be represented as some "#XYZ" - - Example 1: - Input: color = "#09f166" - Output: "#11ee66" - Explanation: - The similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73. - This is the highest among any shorthand color. - - Note: - - color is a string of length 7. - color is a valid RGB color: for i > 0, color[i] is a hexadecimal digit from 0 to f - Any answer which has the same (highest) similarity as the best answer will be accepted. - All inputs and outputs should use lowercase letters, and the output is 7 characters. - - */ -public class _800 { - public static class Solution1 { - public String similarRGB(String color) { - List allShortHandCombinations = computeAllShorthandCombinations(); - int minSimilarity = Integer.MIN_VALUE; - String result = ""; - for (String candidate : allShortHandCombinations) { - int similarity = computeSimilarity(candidate, color); - if (similarity > minSimilarity) { - result = candidate; - minSimilarity = similarity; - } - } - return result; - } - - private int computeSimilarity(String candidate, String color) { - return -(Integer.parseInt(candidate.substring(1, 3), 16) - Integer.parseInt( - color.substring(1, 3), 16)) * (Integer.parseInt(candidate.substring(1, 3), 16) - - Integer.parseInt(color.substring(1, 3), 16)) - - (Integer.parseInt(candidate.substring(3, 5), 16) - Integer.parseInt( - color.substring(3, 5), 16)) * (Integer.parseInt(candidate.substring(3, 5), 16) - - Integer.parseInt(color.substring(3, 5), 16)) - - (Integer.parseInt(candidate.substring(5, 7), 16) - Integer.parseInt( - color.substring(5, 7), 16)) * (Integer.parseInt(candidate.substring(5, 7), 16) - - Integer.parseInt(color.substring(5, 7), 16)); - } - - private List computeAllShorthandCombinations() { - List result = new ArrayList<>(); - List hexNumber = new ArrayList<>( - Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', - 'f')); - for (int i = 0; i < hexNumber.size(); i++) { - for (int j = 0; j < hexNumber.size(); j++) { - for (int k = 0; k < hexNumber.size(); k++) { - StringBuilder sb = new StringBuilder(); - sb.append("#"); - sb.append(hexNumber.get(i)); - sb.append(hexNumber.get(i)); - sb.append(hexNumber.get(j)); - sb.append(hexNumber.get(j)); - sb.append(hexNumber.get(k)); - sb.append(hexNumber.get(k)); - result.add(sb.toString()); - } - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_804.java b/src/main/java/com/fishercoder/solutions/_804.java deleted file mode 100644 index 207e6672cd..0000000000 --- a/src/main/java/com/fishercoder/solutions/_804.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 804. Unique Morse Code Words - - International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on. - - For convenience, the full table for the 26 letters of the English alphabet is given below: - - [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] - - Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-.-....-", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word. - - Return the number of different transformations among all words we have. - - Example: - Input: words = ["gin", "zen", "gig", "msg"] - Output: 2 - Explanation: - The transformation of each word is: - "gin" -> "--...-." - "zen" -> "--...-." - "gig" -> "--...--." - "msg" -> "--...--." - - There are 2 different transformations, "--...-." and "--...--.". - - Note: - The length of words will be at most 100. - Each words[i] will have length in range [1, 12]. - words[i] will only consist of lowercase letters. - */ -public class _804 { - public static class Solution1 { - public int uniqueMorseRepresentations(String[] words) { - String[] morseCodes = - new String[] {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", - "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", - ".--", "-..-", "-.--", "--.."}; - Set concatenation = new HashSet<>(); - StringBuilder sb = new StringBuilder(); - for (String word : words) { - sb.setLength(0); - for (char c : word.toCharArray()) { - sb.append(morseCodes[c - 'a']); - } - concatenation.add(sb.toString()); - } - return concatenation.size(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_806.java b/src/main/java/com/fishercoder/solutions/_806.java deleted file mode 100644 index 2fae24de9b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_806.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 806. Number of Lines To Write String - - We are to write the letters of a given string S, from left to right into lines. - Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. - We are given an array widths, an array where widths[0] is the width of 'a', widths[1] is the width of 'b', ..., and widths[25] is the width of 'z'. - - Now answer two questions: - how many lines have at least one character from S, - and what is the width used by the last such line? - Return your answer as an integer list of length 2. - - Example : - Input: - widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10] - S = "abcdefghijklmnopqrstuvwxyz" - Output: [3, 60] - Explanation: - All letters have the same length of 10. To write all 26 letters, - we need two full lines and one line with 60 units. - - Example : - Input: - widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10] - S = "bbbcccdddaaa" - Output: [2, 4] - Explanation: - All letters except 'a' have the same length of 10, and - "bbbcccdddaa" will cover 9 * 10 + 2 * 4 = 98 units. - For the last 'a', it is written on the second line because - there is only 2 units left in the first line. - So the answer is 2 lines, plus 4 units in the second line. - - Note: - The length of S will be in the range [1, 1000]. - S will only contain lowercase letters. - widths is an array of length 26. - widths[i] will be in the range of [2, 10]. - - */ -public class _806 { - public static class Solution1 { - public int[] numberOfLines(int[] widths, String S) { - int numOfLines = 1; - int offsetInCurrentLine = 0; - for (char c : S.toCharArray()) { - if (offsetInCurrentLine + widths[c - 'a'] < 100) { - offsetInCurrentLine += widths[c - 'a']; - } else if (offsetInCurrentLine + widths[c - 'a'] == 100) { - numOfLines++; - offsetInCurrentLine = 0; - } else { - numOfLines++; - offsetInCurrentLine = widths[c - 'a']; - } - } - return new int[] {numOfLines, offsetInCurrentLine}; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_807.java b/src/main/java/com/fishercoder/solutions/_807.java deleted file mode 100644 index b77aa4e309..0000000000 --- a/src/main/java/com/fishercoder/solutions/_807.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.utils.CommonUtils; - -/** - * 807. Max Increase to Keep City Skyline - * - * In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. - * We are allowed to increase the height of any number of buildings, - * by any amount (the amounts can be different for different buildings). Height 0 is considered to be a building as well. - * At the end, the "skyline" when viewed from all four directions of the grid, i.e. top, bottom, left, and right, - * must be the same as the skyline of the original grid. A city's skyline is the outer contour of the rectangles - * formed by all the buildings when viewed from a distance. See the following example. - * What is the maximum total sum that the height of the buildings can be increased? - * - * Example: - * Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]] - * Output: 35 - * Explanation: - * The grid is: - * [ [3, 0, 8, 4], - * [2, 4, 5, 7], - * [9, 2, 6, 3], - * [0, 3, 1, 0] ] - * - * The skyline viewed from top or bottom is: [9, 4, 8, 7] - * The skyline viewed from left or right is: [8, 7, 9, 3] - * The grid after increasing the height of buildings without affecting skylines is: - * gridNew = [ [8, 4, 8, 7], - * [7, 4, 7, 7], - * [9, 4, 8, 7], - * [3, 3, 3, 3] ] - * - * Notes: - * 1 < grid.length = grid[0].length <= 50. - * All heights grid[i][j] are in the range [0, 100]. - * All buildings in grid[i][j] occupy the entire grid cell: that is, they are a 1 x 1 x grid[i][j] rectangular prism. - * */ -public class _807 { - public static class Solution1 { - public int maxIncreaseKeepingSkyline(int[][] grid) { - int size = grid.length; - int[] horizontalLimits = new int[size]; - int[] verticalLimits = new int[size]; - for (int i = 0; i < size; i++) { - int horizontalLimit = grid[i][0]; - for (int j = 1; j < size; j++) { - horizontalLimit = Math.max(horizontalLimit, grid[i][j]); - } - horizontalLimits[i] = horizontalLimit; - } - for (int j = 0; j < size; j++) { - int verticalLimit = grid[0][j]; - for (int i = 1; i < size; i++) { - verticalLimit = Math.max(verticalLimit, grid[i][j]); - } - verticalLimits[j] = verticalLimit; - } - int increases = 0; - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - if (grid[i][j] != horizontalLimits[i] && grid[i][j] != verticalLimits[j]) { - increases += Math.min(horizontalLimits[i], verticalLimits[j]) - grid[i][j]; - } - } - } - return increases; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_81.java b/src/main/java/com/fishercoder/solutions/_81.java deleted file mode 100644 index beda5e1e89..0000000000 --- a/src/main/java/com/fishercoder/solutions/_81.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -public class _81 { - - public static class Solution1 { - public boolean search(int[] nums, int target) { - int start = 0; - int end = nums.length - 1; - - //check each num so we will check start == end - //We always get a sorted part and a half part - //we can check sorted part to decide where to go next - while (start <= end) { - int mid = start + (end - start) / 2; - if (nums[mid] == target) { - return true; - } - - //if left part is sorted - if (nums[start] < nums[mid]) { - if (target < nums[start] || target > nums[mid]) { - //target is in rotated part - start = mid + 1; - } else { - end = mid - 1; - } - } else if (nums[start] > nums[mid]) { - //right part is rotated - - //target is in rotated part - if (target < nums[mid] || target > nums[end]) { - end = mid - 1; - } else { - start = mid + 1; - } - } else { - //duplicates, we know nums[mid] != target, so nums[start] != target - //based on current information, we can only move left pointer to skip one cell - //thus in the worst case, we would have target: 2, and array like 11111111, then - //the running time would be O(n) - start++; - } - } - return false; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_811.java b/src/main/java/com/fishercoder/solutions/_811.java deleted file mode 100644 index 22fcafea87..0000000000 --- a/src/main/java/com/fishercoder/solutions/_811.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 811. Subdomain Visit Count - * - * A website domain like "discuss.leetcode.com" consists of various subdomains. - * At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". - * When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly. - * Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, - * followed by the address. - * An example of a count-paired domain might be "9001 discuss.leetcode.com". - * We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), - * that explicitly counts the number of visits to each subdomain. - * - * Example 1: - * Input: - * ["9001 discuss.leetcode.com"] - * Output: - * ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"] - * Explanation: - * We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times. - * - * Example 2: - * Input: - * ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"] - * Output: - * ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"] - * Explanation: - * We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times. - * - * Notes: - * The length of cpdomains will not exceed 100. - * The length of each domain name will not exceed 100. - * Each address will have either 1 or 2 "." characters. - * The input count in any count-paired domain will not exceed 10000. - */ -public class _811 { - public static class Solution1 { - public List subdomainVisits(String[] cpdomains) { - Map map = new HashMap<>(); - for (String each : cpdomains) { - String[] pair = each.split(" "); - String[] subDomains = pair[1].split("\\."); - StringBuilder sb = new StringBuilder(); - for (int i = subDomains.length - 1; i >= 0; i--) { - if (i < subDomains.length - 1) { - sb.insert(0, "."); - } - sb.insert(0, subDomains[i]); - map.put(sb.toString(), map.getOrDefault(sb.toString(), 0) + Integer.parseInt(pair[0])); - } - } - List result = new ArrayList<>(); - for (String key : map.keySet()) { - result.add(map.get(key) + " " + key); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_814.java b/src/main/java/com/fishercoder/solutions/_814.java deleted file mode 100644 index 378f2808d7..0000000000 --- a/src/main/java/com/fishercoder/solutions/_814.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 814. Binary Tree Pruning - * - * We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. - * Return the same tree where every subtree (of the given tree) not containing a 1 has been removed. - * (Recall that the subtree of a node X is X, plus every node that is a descendant of X.) - * - * Example 1: - * Input: [1,null,0,0,1] - * Output: [1,null,0,null,1] - * Explanation: - * Only the red nodes satisfy the property "every subtree not containing a 1". - * The diagram on the right represents the answer. - * 1 1 - * \ \ - * 0 ----> 0 - * / \ \ - * 0 1 1 - * - * - * Example 2: - * Input: [1,0,1,0,0,0,1] - * Output: [1,null,1,null,1] - * 1 1 - * / \ \ - * 0 1 ----> 1 - * / \ / \ \ - * 0 0 0 1 1 - * - * - * Example 3: - * Input: [1,1,0,1,1,0,1,0] - * Output: [1,1,0,1,1,null,1] - * 1 1 - * / \ / \ - * 1 0 -----> 1 0 - * / \ / \ / \ \ - * 1 1 0 1 1 1 1 - * / - * 0 - * - * - * Note: - * - * The binary tree will have at most 100 nodes. - * The value of each node will only be 0 or 1.*/ -public class _814 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/binary-tree-pruning/discuss/122730/C%2B%2BJavaPython-Self-Explaining-Solution-and-2-lines*/ - public TreeNode pruneTree(TreeNode root) { - if (root == null) { - return root; - } - root.left = pruneTree(root.left); - root.right = pruneTree(root.right); - if (root.left == null && root.right == null && root.val == 0) { - return null; - } - return root; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_819.java b/src/main/java/com/fishercoder/solutions/_819.java deleted file mode 100644 index 37198ea56d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_819.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 819. Most Common Word - - Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. - It is guaranteed there is at least one word that isn't banned, and that the answer is unique. - Words in the list of banned words are given in lowercase, and free of punctuation. - Words in the paragraph are not case sensitive. The answer is in lowercase. - - Example: - Input: - paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." - banned = ["hit"] - Output: "ball" - Explanation: - "hit" occurs 3 times, but it is a banned word. - "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. - Note that words in the paragraph are not case sensitive, - that punctuation is ignored (even if adjacent to words, such as "ball,"), - and that "hit" isn't the answer even though it occurs more because it is banned. - - Note: - - 1 <= paragraph.length <= 1000. - 1 <= banned.length <= 100. - 1 <= banned[i].length <= 10. - The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.) - paragraph only consists of letters, spaces, or the punctuation symbols !?',;. - Different words in paragraph are always separated by a space. - There are no hyphens or hyphenated words. - Words only consist of letters, never apostrophes or other punctuation symbols. - */ -public class _819 { - public static class Solution1 { - public String mostCommonWord(String paragraph, String[] banned) { - Set bannedSet = new HashSet(Arrays.asList(banned)); - String[] words = paragraph.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+"); - Map map = new HashMap<>(); - Arrays.stream(words) - .filter(word -> !bannedSet.contains(word)) - .forEach(word -> map.put(word, map.getOrDefault(word, 0) + 1)); - String result = ""; - int freq = 0; - for (String key : map.keySet()) { - if (map.get(key) > freq) { - result = key; - freq = map.get(key); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_82.java b/src/main/java/com/fishercoder/solutions/_82.java deleted file mode 100644 index c85c54001d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_82.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -public class _82 { - public static class Solution1 { - public ListNode deleteDuplicates(ListNode head) { - if (head == null) { - return head; - } - ListNode fakeHead = new ListNode(-1); - fakeHead.next = head; - ListNode pre = fakeHead; - ListNode curr = head; - while (curr != null) { - while (curr.next != null && curr.val == curr.next.val) { - curr = curr.next; - } - if (pre.next == curr) { - pre = pre.next; - } else { - pre.next = curr.next; - } - curr = curr.next; - } - return fakeHead.next; - } - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_821.java b/src/main/java/com/fishercoder/solutions/_821.java deleted file mode 100644 index 38656b801f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_821.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.TreeSet; - -/** - * 821. Shortest Distance to a Character - - Given a string S and a character C, - return an array of integers representing the shortest distance from the character C in the string. - - Example 1: - - Input: S = "loveleetcode", C = 'e' - Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0] - - Note: - - S string length is in [1, 10000]. - C is a single character, and guaranteed to be in string S. - All letters in S and C are lowercase. - - */ -public class _821 { - - public static class Solution1 { - public int[] shortestToChar(String S, char C) { - int[] result = new int[S.length()]; - TreeSet cIndices = new TreeSet(); - for (int i = 0; i < S.length(); i++) { - if (S.charAt(i) == C) { - cIndices.add(i); - } - } - for (int i = 0; i < S.length(); i++) { - int leftDist = Integer.MAX_VALUE; - if (cIndices.floor(i) != null) { - leftDist = Math.abs(cIndices.floor(i) - i); - } - int rightDist = Integer.MAX_VALUE; - if (cIndices.ceiling(i) != null) { - rightDist = Math.abs(cIndices.ceiling(i) - i); - } - result[i] = Math.min(leftDist, rightDist); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_824.java b/src/main/java/com/fishercoder/solutions/_824.java deleted file mode 100644 index ea2c8c17e9..0000000000 --- a/src/main/java/com/fishercoder/solutions/_824.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -/** - * 824. Goat Latin - * - * A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. - * - * We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.) - * - * The rules of Goat Latin are as follows: - * - * If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word. - * For example, the word 'apple' becomes 'applema'. - * - * If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma". - * For example, the word "goat" becomes "oatgma". - * - * Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1. - * For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on. - * - * Return the final sentence representing the conversion from S to Goat Latin. - * - * Example 1: - * - * Input: "I speak Goat Latin" - * Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa" - * - * Example 2: - * - * Input: "The quick brown fox jumped over the lazy dog" - * Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa" - * - * Notes: - * - * S contains only uppercase, lowercase and spaces. Exactly one space between each word. - * 1 <= S.length <= 100. - */ -public class _824 { - - public static class Solution1 { - public String toGoatLatin(String S) { - StringBuilder sb = new StringBuilder(); - Set vowels = - new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')); - String[] words = S.split(" "); - for (int i = 0; i < words.length; i++) { - if (vowels.contains(words[i].charAt(0))) { - String newWord = words[i] + "ma"; - int j = i + 1; - while (j-- > 0) { - newWord += 'a'; - } - sb.append(newWord); - sb.append(" "); - } else { - StringBuilder subSb = new StringBuilder(words[i].substring(1)); - subSb.append(words[i].charAt(0)); - subSb.append("ma"); - int j = i + 1; - while (j-- > 0) { - subSb.append("a"); - } - sb.append(subSb.toString()); - sb.append(" "); - } - } - return sb.substring(0, sb.length() - 1); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_830.java b/src/main/java/com/fishercoder/solutions/_830.java deleted file mode 100644 index ea1058c408..0000000000 --- a/src/main/java/com/fishercoder/solutions/_830.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 830. Positions of Large Groups - * - * In a string S of lowercase letters, these letters form consecutive groups of the same character. - * For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy". - * Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group. - * The final answer should be in lexicographic order. - * - * Example 1: - * Input: "abbxxxxzzy" - * Output: [[3,6]] - * Explanation: "xxxx" is the single large group with starting 3 and ending positions 6. - * - * Example 2: - * Input: "abc" - * Output: [] - * Explanation: We have "a","b" and "c" but no large group. - * - * Example 3: - * Input: "abcdddeeeeaabbbcd" - * Output: [[3,5],[6,9],[12,14]] - * - * Note: 1 <= S.length <= 1000 - */ -public class _830 { - public static class Solution1 { - public List> largeGroupPositions(String S) { - List> result = new ArrayList<>(); - char[] chars = S.toCharArray(); - for (int i = 0; i < chars.length; ) { - char first = chars[i]; - int j = i + 1; - while (j < chars.length && first == chars[j]) { - j++; - } - if ((j - i) >= 3) { - result.add(Arrays.asList(i, j - 1)); - } - i = j; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_832.java b/src/main/java/com/fishercoder/solutions/_832.java deleted file mode 100644 index 1fe29be46d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_832.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 832. Flipping an Image - * - * Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. - * To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1]. - * To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0]. - * - * Example 1: - * Input: [[1,1,0],[1,0,1],[0,0,0]] - * Output: [[1,0,0],[0,1,0],[1,1,1]] - * Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]]. - * Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]] - * - * Example 2: - * Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]] - * Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]] - * Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]]. - * Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]] - * - * Notes: - * 1 <= A.length = A[0].length <= 20 - * 0 <= A[i][j] <= 1 - */ -public class _832 { - public static class Solution1 { - public int[][] flipAndInvertImage(int[][] A) { - int m = A.length; - int n = A[0].length; - int[][] result = new int[m][n]; - for (int i = 0; i < m; i++) { - int[] flipped = (reverse(A[i])); - result[i] = invert(flipped); - } - return result; - } - - private int[] invert(int[] flipped) { - int[] result = new int[flipped.length]; - for (int i = 0; i < flipped.length; i++) { - if (flipped[i] == 0) { - result[i] = 1; - } else { - result[i] = 0; - } - } - return result; - } - - private int[] reverse(int[] nums) { - for (int i = 0, j = nums.length - 1; i < j; i++, j--) { - int tmp = nums[i]; - nums[i] = nums[j]; - nums[j] = tmp; - } - return nums; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_836.java b/src/main/java/com/fishercoder/solutions/_836.java deleted file mode 100644 index f2c3b3cbb8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_836.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 836. Rectangle Overlap - * - * A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of - * its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. - * Two rectangles overlap if the area of their intersection is positive. - * To be clear, two rectangles that only touch at the corner or edges do not overlap. - * Given two (axis-aligned) rectangles, return whether they overlap. - * - * Example 1: - * Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3] - * Output: true - * - * Example 2: - * Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1] - * Output: false - * - * Notes: - * Both rectangles rec1 and rec2 are lists of 4 integers. - * All coordinates in rectangles will be between -10^9 and 10^9. - * */ -public class _836 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/rectangle-overlap/discuss/132340/C%2B%2BJavaPython-1-line-Solution-1D-to-2D*/ - public boolean isRectangleOverlap(int[] rec1, int[] rec2) { - return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_840.java b/src/main/java/com/fishercoder/solutions/_840.java deleted file mode 100644 index d3cd0937c8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_840.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 840. Magic Squares In Grid - * - * A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, - * column, and both diagonals all have the same sum. - * - * Given an grid of integers, how many 3 x 3 "magic square" subgrids are there? (Each subgrid is contiguous). - * - * Example 1: - * - * Input: [[4,3,8,4], - * [9,5,1,9], - * [2,7,6,2]] - * - * Output: 1 - * - * Explanation: - * The following subgrid is a 3 x 3 magic square: - * 438 - * 951 - * 276 - * - * while this one is not: - * 384 - * 519 - * 762 - * - * In total, there is only one magic square inside the given grid. - * Note: - * - * 1 <= grid.length <= 10 - * 1 <= grid[0].length <= 10 - * 0 <= grid[i][j] <= 15 - */ -public class _840 { - public static class Solution1 { - public int numMagicSquaresInside(int[][] grid) { - int m = grid.length; - int n = grid[0].length; - int count = 0; - for (int i = 0; i < m - 2; i++) { - for (int j = 0; j < n - 2; j++) { - Set set = new HashSet<>(); - int sum = grid[i][j] + grid[i][j + 1] + grid[i][j + 2]; - if (sum == grid[i + 1][j] + grid[i + 1][j + 1] + grid[i + 1][j + 2] - && sum == grid[i + 2][j] + grid[i + 2][j + 1] + grid[i + 2][j + 2] - - && sum == grid[i][j] + grid[i + 1][j] + grid[i + 2][j] - && sum == grid[i][j + 1] + grid[i + 1][j + 1] + grid[i + 2][j + 1] - && sum == grid[i][j + 2] + grid[i + 1][j + 2] + grid[i + 2][j + 2] - - && sum == grid[i][j] + grid[i + 1][j + 1] + grid[i + 2][j + 2] - && sum == grid[i][j + 2] + grid[i + 1][j + 1] + grid[i + 2][j] - - && set.add(grid[i][j]) && isLegit(grid[i][j]) - && set.add(grid[i][j + 1]) && isLegit(grid[i][j + 1]) - && set.add(grid[i][j + 2]) && isLegit(grid[i][j + 2]) - && set.add(grid[i + 1][j]) && isLegit(grid[i + 1][j]) - && set.add(grid[i + 1][j + 1]) && isLegit(grid[i + 1][j + 1]) - && set.add(grid[i + 1][j + 2]) && isLegit(grid[i + 1][j + 2]) - && set.add(grid[i + 2][j]) && isLegit(grid[i + 2][j]) - && set.add(grid[i + 2][j + 1]) && isLegit(grid[i + 2][j + 1]) - && set.add(grid[i + 2][j + 2]) && isLegit(grid[i + 2][j + 2]) - ) { - count++; - } - } - } - return count; - } - - private boolean isLegit(int num) { - return num <= 9 && num >= 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_841.java b/src/main/java/com/fishercoder/solutions/_841.java deleted file mode 100644 index e0aab9a3f7..0000000000 --- a/src/main/java/com/fishercoder/solutions/_841.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; -import java.util.Set; - -/** - * 841. Keys and Rooms - * - * There are N rooms and you start in room 0. - * Each room has a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room. - * Formally, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, ..., N-1] where N = rooms.length. - * A key rooms[i][j] = v opens the room with number v. - * Initially, all the rooms start locked (except for room 0). - * You can walk back and forth between rooms freely. - * Return true if and only if you can enter every room. - * - * Example 1: - * Input: [[1],[2],[3],[]] - * Output: true - * Explanation: - * We start in room 0, and pick up key 1. - * We then go to room 1, and pick up key 2. - * We then go to room 2, and pick up key 3. - * We then go to room 3. Since we were able to go to every room, we return true. - * - * Example 2: - * Input: [[1,3],[3,0,1],[2],[0]] - * Output: false - * Explanation: We can't enter the room with number 2. - * - * Note: - * 1 <= rooms.length <= 1000 - * 0 <= rooms[i].length <= 1000 - * The number of keys in all rooms combined is at most 3000. - * */ -public class _841 { - public static class Solution1 { - public boolean canVisitAllRooms(List> rooms) { - Set unvisitedRooms = new HashSet<>(); - for (int i = 1; i < rooms.size(); i++) { - unvisitedRooms.add(i); - } - List keys = rooms.get(0); - Queue queue = new LinkedList<>(); - for (int key : keys) { - queue.offer(key); - } - while (!queue.isEmpty()) { - int size = queue.size(); - for (int i = 0; i < size; i++) { - int roomIndex = queue.poll(); - unvisitedRooms.remove(roomIndex); - for (int j = 0; j < rooms.get(roomIndex).size(); j++) { - Integer nextRoom = rooms.get(roomIndex).get(j); - if (unvisitedRooms.contains(nextRoom) && !queue.contains(nextRoom)) { - queue.offer(nextRoom); - } - } - } - } - return unvisitedRooms.isEmpty(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_844.java b/src/main/java/com/fishercoder/solutions/_844.java deleted file mode 100644 index d1e8facab4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_844.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 844. Backspace String Compare - * - * Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. - * - * Example 1: - * Input: S = "ab#c", T = "ad#c" - * Output: true - * Explanation: Both S and T become "ac". - * - * Example 2: - * Input: S = "ab##", T = "c#d#" - * Output: true - * Explanation: Both S and T become "". - * - * Example 3: - * Input: S = "a##c", T = "#a#c" - * Output: true - * Explanation: Both S and T become "c". - * - * Example 4: - * Input: S = "a#c", T = "b" - * Output: false - * Explanation: S becomes "c" while T becomes "b". - * Note: - * - * 1 <= S.length <= 200 - * 1 <= T.length <= 200 - * S and T only contain lowercase letters and '#' characters. - * Follow up: - * - * Can you solve it in O(N) time and O(1) space? - */ -public class _844 { - public static class Solution1 { - public boolean backspaceCompare(String S, String T) { - String processedS = process(S); - String processedT = process(T); - return processedS.equals(processedT); - } - - private String process(String str) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < str.length(); i++) { - if (str.charAt(i) == '#') { - if (sb.length() > 0) { - sb.deleteCharAt(sb.length() - 1); - } - } else { - sb.append(str.charAt(i)); - } - } - return sb.reverse().toString(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_847.java b/src/main/java/com/fishercoder/solutions/_847.java deleted file mode 100644 index f627b17e77..0000000000 --- a/src/main/java/com/fishercoder/solutions/_847.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 847. Shortest Path Visiting All Nodes - * - * An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. - * graph.length = N, and j != i is in the list graph[i] exactly once, if and only if nodes i and j are connected. - * Return the length of the shortest path that visits every node. - * You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges. - * - * Example 1: - * Input: [[1,2,3],[0],[0],[0]] - * Output: 4 - * Explanation: One possible path is [1,0,2,0,3] - * - * Example 2: - * Input: [[1],[0,2,4],[1,3,4],[2],[1,2]] - * Output: 4 - * Explanation: One possible path is [0,1,4,2,3] - * - * Note: - * 1 <= graph.length <= 12 - * 0 <= graph[i].length < graph.length - * */ -public class _847 { - public static class Solution1 { - public int shortestPathLength(int[][] graph) { - //TODO: implement this - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_849.java b/src/main/java/com/fishercoder/solutions/_849.java deleted file mode 100644 index 9ec8aaee24..0000000000 --- a/src/main/java/com/fishercoder/solutions/_849.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 849. Maximize Distance to Closest Person - * - * In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty. - * There is at least one empty seat, and at least one person sitting. - * Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized. - * Return that maximum distance to closest person. - * - * Example 1: - * Input: [1,0,0,0,1,0,1] - * Output: 2 - * Explanation: - * If Alex sits in the second open seat (seats[2]), then the closest person has distance 2. - * If Alex sits in any other open seat, the closest person has distance 1. - * Thus, the maximum distance to the closest person is 2. - * - * Example 2: - * Input: [1,0,0,0] - * Output: 3 - * Explanation: - * If Alex sits in the last seat, the closest person is 3 seats away. - * This is the maximum distance possible, so the answer is 3. - * Note: - * - * 1 <= seats.length <= 20000 - * seats contains only 0s or 1s, at least one 0, and at least one 1. - * */ -public class _849 { - public static class Solution1 { - int maxDist = 0; - - public int maxDistToClosest(int[] seats) { - for (int i = 0; i < seats.length; i++) { - if (seats[i] == 0) { - extend(seats, i); - } - } - return maxDist; - } - - private void extend(int[] seats, int position) { - int left = position - 1; - int right = position + 1; - int leftMinDistance = 1; - while (left >= 0) { - if (seats[left] == 0) { - leftMinDistance++; - left--; - } else { - break; - } - } - int rightMinDistance = 1; - while (right < seats.length) { - if (seats[right] == 0) { - rightMinDistance++; - right++; - } else { - break; - } - } - int maxReach = 0; - if (position == 0) { - maxReach = rightMinDistance; - } else if (position == seats.length - 1) { - maxReach = leftMinDistance; - } else { - maxReach = Math.min(leftMinDistance, rightMinDistance); - } - maxDist = Math.max(maxDist, maxReach); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_852.java b/src/main/java/com/fishercoder/solutions/_852.java deleted file mode 100644 index fff882f43c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_852.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder.solutions; - -/**852. Peak Index in a Mountain Array - * - * Let's call an array A a mountain if the following properties hold: - * - * A.length >= 3 - * There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] - * Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]. - * - * Example 1: - * - * Input: [0,1,0] - * Output: 1 - * Example 2: - * - * Input: [0,2,1,0] - * Output: 1 - * Note: - * - * 3 <= A.length <= 10000 - * 0 <= A[i] <= 10^6 - * A is a mountain, as defined above.*/ -public class _852 { - public static class Solution1 { - public int peakIndexInMountainArray(int[] A) { - for (int i = 1; i < A.length - 1; i++) { - if (A[i] > A[i + 1]) { - return i; - } - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_856.java b/src/main/java/com/fishercoder/solutions/_856.java deleted file mode 100644 index 4bc940edb5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_856.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 856. Score of Parentheses - * - * Given a balanced parentheses string S, compute the score of the string based on the following rule: - * () has score 1 - * AB has score A + B, where A and B are balanced parentheses strings. - * (A) has score 2 * A, where A is a balanced parentheses string. - * - * Example 1: - * Input: "()" - * Output: 1 - * - * Example 2: - * Input: "(())" - * Output: 2 - * - * Example 3: - * Input: "()()" - * Output: 2 - * - * Example 4: - * Input: "(()(()))" - * Output: 6 - * - * Note: - * S is a balanced parentheses string, containing only ( and ). - * 2 <= S.length <= 50 - * */ -public class _856 { - public static class Solution1 { - public int scoreOfParentheses(String S) { - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_859.java b/src/main/java/com/fishercoder/solutions/_859.java deleted file mode 100644 index 925a291faa..0000000000 --- a/src/main/java/com/fishercoder/solutions/_859.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 859. Buddy Strings - * - * Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B. - * - * - * - * Example 1: - * - * Input: A = "ab", B = "ba" - * Output: true - * Example 2: - * - * Input: A = "ab", B = "ab" - * Output: false - * Example 3: - * - * Input: A = "aa", B = "aa" - * Output: true - * Example 4: - * - * Input: A = "aaaaaaabc", B = "aaaaaaacb" - * Output: true - * Example 5: - * - * Input: A = "", B = "aa" - * Output: false - * - * - * Note: - * - * 0 <= A.length <= 20000 - * 0 <= B.length <= 20000 - * A and B consist only of lowercase letters. - */ -public class _859 { - public static class Solution1 { - public boolean buddyStrings(String A, String B) { - if (A.length() != B.length()) { - return false; - } - Character c1 = null; - Character c2 = null; - Set set = new HashSet<>(); - int count = 0; - for (int i = 0; i < A.length(); i++) { - if (A.charAt(i) != B.charAt(i)) { - if (count > 2) { - return false; - } - if (c1 == null) { - c1 = B.charAt(i); - c2 = A.charAt(i); - count++; - continue; - } - if (c1 != A.charAt(i) || c2 != B.charAt(i)) { - return false; - } - count++; - } - set.add(A.charAt(i)); - } - return count == 2 || (count == 0 && set.size() < A.length()); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_86.java b/src/main/java/com/fishercoder/solutions/_86.java deleted file mode 100644 index dca163e160..0000000000 --- a/src/main/java/com/fishercoder/solutions/_86.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -public class _86 { - public static class Solution1 { - public ListNode partition(ListNode head, int x) { - if (head == null || head.next == null) { - return head; - } - ListNode left = new ListNode(0); - ListNode right = new ListNode(0); - ListNode less = left; - ListNode greater = right; - while (head != null) { - if (head.val < x) { - less.next = head; - less = less.next; - } else { - greater.next = head; - greater = greater.next; - } - head = head.next; - } - greater.next = null; - less.next = right.next; - return left.next; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_860.java b/src/main/java/com/fishercoder/solutions/_860.java deleted file mode 100644 index f424ffd561..0000000000 --- a/src/main/java/com/fishercoder/solutions/_860.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 860. Lemonade Change - * - * At a lemonade stand, each lemonade costs $5. - * - * Customers are standing in a queue to buy from you, - * and order one at a time (in the order specified by bills). - * Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. - * You must provide the correct change to each customer, - * so that the net transaction is that the customer pays $5. - * Note that you don't have any change in hand at first. - * Return true if and only if you can provide every customer with correct change. - * - * Example 1: - * Input: [5,5,5,10,20] - * Output: true - * Explanation: - * From the first 3 customers, we collect three $5 bills in order. - * From the fourth customer, we collect a $10 bill and give back a $5. - * From the fifth customer, we give a $10 bill and a $5 bill. - * Since all customers got correct change, we output true. - * - * Example 2: - * Input: [5,5,10] - * Output: true - * - * Example 3: - * Input: [10,10] - * Output: false - * - * Example 4: - * Input: [5,5,10,10,20] - * Output: false - * Explanation: - * From the first two customers in order, we collect two $5 bills. - * For the next two customers in order, we collect a $10 bill and give back a $5 bill. - * For the last customer, we can't give change of $15 back because we only have two $10 bills. - * Since not every customer received correct change, the answer is false. - * - * - * Note: - * 0 <= bills.length <= 10000 - * bills[i] will be either 5, 10, or 20. - */ -public class _860 { - public static class Solution1 { - public boolean lemonadeChange(int[] bills) { - Map map = new HashMap<>(); - for (int bill : bills) { - if (bill == 5) { - map.put(5, map.getOrDefault(5, 0) + 1); - } else if (bill == 10) { - if (!map.containsKey(5)) { - return false; - } else { - map.put(5, map.get(5) - 1); - if (map.get(5) == 0) { - map.remove(5); - } - map.put(10, map.getOrDefault(10, 0) + 1); - } - } else { - if (!map.containsKey(5)) { - return false; - } else { - if (!map.containsKey(10)) { - if (!map.containsKey(5) || map.get(5) < 3) { - return false; - } else { - map.put(5, map.get(5) - 3); - if (map.get(5) == 0) { - map.remove(5); - } - } - } else { - if (!map.containsKey(5)) { - return false; - } else { - map.put(5, map.get(5) - 1); - if (map.get(5) == 0) { - map.remove(5); - } - map.put(10, map.get(10) - 1); - if (map.get(10) == 0) { - map.remove(10); - } - } - } - } - map.put(20, map.getOrDefault(20, 0) + 1); - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_867.java b/src/main/java/com/fishercoder/solutions/_867.java deleted file mode 100644 index 5c787b02c1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_867.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 867. Transpose Matrix - * - * Given a matrix A, return the transpose of A. - * The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix. - * - * Example 1: - * - * Input: [[1,2,3],[4,5,6],[7,8,9]] - * Output: [[1,4,7],[2,5,8],[3,6,9]] - * Example 2: - * - * Input: [[1,2,3],[4,5,6]] - * Output: [[1,4],[2,5],[3,6]] - * - * Note: - * - * 1 <= A.length <= 1000 - * 1 <= A[0].length <= 1000 - * */ -public class _867 { - public static class Solution1 { - public int[][] transpose(int[][] A) { - int[][] result = new int[A[0].length][A.length]; - for (int i = 0; i < A.length; i++) { - for (int j = 0; j < A[0].length; j++) { - result[j][i] = A[i][j]; - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_868.java b/src/main/java/com/fishercoder/solutions/_868.java deleted file mode 100644 index af53f788f8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_868.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 868. Binary Gap - * - * Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N. - * - * If there aren't two consecutive 1's, return 0. - * - * Example 1: - * Input: 22 - * Output: 2 - * Explanation: - * 22 in binary is 0b10110. - * In the binary representation of 22, there are three ones, and two consecutive pairs of 1's. - * The first consecutive pair of 1's have distance 2. - * The second consecutive pair of 1's have distance 1. - * The answer is the largest of these two distances, which is 2. - * - * Example 2: - * Input: 5 - * Output: 2 - * Explanation: - * 5 in binary is 0b101. - * - * Example 3: - * Input: 6 - * Output: 1 - * Explanation: - * 6 in binary is 0b110. - * - * Example 4: - * Input: 8 - * Output: 0 - * Explanation: - * 8 in binary is 0b1000. - * There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0. - * - * Note: - * 1 <= N <= 10^9 - */ -public class _868 { - public static class Solution1 { - public int binaryGap(int N) { - String bin = Integer.toBinaryString(N); - List oneIndexes = new ArrayList<>(); - for (int i = 0; i < bin.length(); i++) { - if (bin.charAt(i) == '1') { - oneIndexes.add(i); - } - } - int maxGap = 0; - for (int i = 0; i < oneIndexes.size() - 1; i++) { - maxGap = Math.max(oneIndexes.get(i + 1) - oneIndexes.get(i), maxGap); - } - return maxGap; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_872.java b/src/main/java/com/fishercoder/solutions/_872.java deleted file mode 100644 index 3a23420a07..0000000000 --- a/src/main/java/com/fishercoder/solutions/_872.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; -import java.util.ArrayList; -import java.util.List; - -/** - * 872. Leaf-Similar Trees - * - * Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence. - * - * 3 - * / \ - * 5 1 - * / \ / \ - * 6 2 9 8 - * / \ - * 7 4 - * - * For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8). - * - * Two binary trees are considered leaf-similar if their leaf value sequence is the same. - * - * Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar. - * - * Note: - * - * Both of the given trees will have between 1 and 100 nodes. - */ -public class _872 { - public static class Solution1 { - public boolean leafSimilar(TreeNode root1, TreeNode root2) { - List leaves1 = new ArrayList<>(); - List leaves2 = new ArrayList<>(); - preorder(root1, leaves1); - preorder(root2, leaves2); - return leaves1.equals(leaves2); - } - - private void preorder(TreeNode root, - List leaves) { - if (root == null) { - return; - } - if (root.left == null && root.right == null) { - leaves.add(root.val); - } - preorder(root.left, leaves); - preorder(root.right, leaves); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_876.java b/src/main/java/com/fishercoder/solutions/_876.java deleted file mode 100644 index 41b865f17f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_876.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 876. Middle of the Linked List - * - * Given a non-empty, singly linked list with head node head, return a middle node of linked list. - * - * If there are two middle nodes, return the second middle node. - * - * Example 1: - * - * Input: [1,2,3,4,5] - * Output: Node 3 from this list (Serialization: [3,4,5]) - * The returned node has value 3. (The judge's serialization of this node is [3,4,5]). - * Note that we returned a ListNode object ans, such that: - * ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL. - * - * Example 2: - * - * Input: [1,2,3,4,5,6] - * Output: Node 4 from this list (Serialization: [4,5,6]) - * Since the list has two middle nodes with values 3 and 4, we return the second one. - * - * Note: - * - * The number of nodes in the given list will be between 1 and 100. - */ -public class _876 { - public static class Solution1 { - public ListNode middleNode(ListNode head) { - ListNode fast = head; - ListNode slow = head; - while (fast != null && fast.next != null) { - fast = fast.next.next; - slow = slow.next; - } - return slow; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_880.java b/src/main/java/com/fishercoder/solutions/_880.java deleted file mode 100644 index 86678af563..0000000000 --- a/src/main/java/com/fishercoder/solutions/_880.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 880. Decoded String at Index - * - * An encoded string S is given. To find and write the decoded string to a tape, - * the encoded string is read one character at a time and the following steps are taken: - * If the character read is a letter, that letter is written onto the tape. - * If the character read is a digit (say d), the entire current tape is repeatedly written d-1 more times in total. - * Now for some encoded string S, and an index K, find and return the K-th letter (1 indexed) in the decoded string. - * - * Example 1: - * Input: S = "leet2code3", K = 10 - * Output: "o" - * Explanation: - * The decoded string is "leetleetcodeleetleetcodeleetleetcode". - * The 10th letter in the string is "o". - * - * Example 2: - * Input: S = "ha22", K = 5 - * Output: "h" - * Explanation: - * The decoded string is "hahahaha". The 5th letter is "h". - * - * Example 3: - * Input: S = "a2345678999999999999999", K = 1 - * Output: "a" - * Explanation: - * The decoded string is "a" repeated 8301530446056247680 times. The 1st letter is "a". - * - * Note: - * 2 <= S.length <= 100 - * S will only contain lowercase letters and digits 2 through 9. - * S starts with a letter. - * 1 <= K <= 10^9 - * The decoded string is guaranteed to have less than 2^63 letters. - * */ -public class _880 { - public static class Solution1 { - public String decodeAtIndex(String S, int K) { - //TODO: implement it - return ""; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_883.java b/src/main/java/com/fishercoder/solutions/_883.java deleted file mode 100644 index 8b2bca1389..0000000000 --- a/src/main/java/com/fishercoder/solutions/_883.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 883. Projection Area of 3D Shapes - * - * On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. - * Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j). - * Now we view the projection of these cubes onto the xy, yz, and zx planes. - * A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane. - * Here, we are viewing the "shadow" when looking at the cubes from the top, the front, and the side. - * Return the total area of all three projections. - * - * Example 1: - * Input: [[2]] - * Output: 5 - * - * Example 2: - * Input: [[1,2],[3,4]] - * Output: 17 - * Explanation: - * Here are the three projections ("shadows") of the shape made with each axis-aligned plane. - * - * Example 3: - * Input: [[1,0],[0,2]] - * Output: 8 - * - * Example 4: - * Input: [[1,1,1],[1,0,1],[1,1,1]] - * Output: 14 - * - * Example 5: - * Input: [[2,2,2],[2,1,2],[2,2,2]] - * Output: 21 - * - * Note: - * 1 <= grid.length = grid[0].length <= 50 - * 0 <= grid[i][j] <= 50 - * */ -public class _883 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/projection-area-of-3d-shapes/discuss/156726/C%2B%2BJavaPython-Straight-Forward-One-Pass*/ - public int projectionArea(int[][] grid) { - int n = grid.length; - int area = 0; - for (int i = 0; i < n; i++) { - int x = 0; - int y = 0; - for (int j = 0; j < n; j++) { - x = Math.max(x, grid[i][j]); - y = Math.max(x, grid[j][i]); - if (grid[i][j] > 0) { - area++; - } - } - area += x + y; - } - return area; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_884.java b/src/main/java/com/fishercoder/solutions/_884.java deleted file mode 100644 index c2f0f218ad..0000000000 --- a/src/main/java/com/fishercoder/solutions/_884.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 884. Uncommon Words from Two Sentences - * - * We are given two sentences A and B. (A sentence is a string of space separated words. Each word consists only of lowercase letters.) - * - * A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence. - * - * Return a list of all uncommon words. - * - * You may return the list in any order. - * - * - * - * Example 1: - * - * Input: A = "this apple is sweet", B = "this apple is sour" - * Output: ["sweet","sour"] - * Example 2: - * - * Input: A = "apple apple", B = "banana" - * Output: ["banana"] - * - * - * Note: - * - * 0 <= A.length <= 200 - * 0 <= B.length <= 200 - * A and B both contain only spaces and lowercase letters. - */ -public class _884 { - public static class Solution1 { - public String[] uncommonFromSentences(String A, String B) { - Map map = new HashMap<>(); - for (String word : A.split(" ")) { - map.put(word, map.getOrDefault(word, 0) + 1); - } - - for (String word : B.split(" ")) { - map.put(word, map.getOrDefault(word, 0) + 1); - } - List result = new ArrayList<>(); - for (String key : map.keySet()) { - if (map.get(key) == 1) { - result.add(key); - } - } - String[] strs = new String[result.size()]; - result.toArray(strs); - return strs; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_885.java b/src/main/java/com/fishercoder/solutions/_885.java deleted file mode 100644 index a9e8610a92..0000000000 --- a/src/main/java/com/fishercoder/solutions/_885.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 885. Spiral Matrix III - * - * On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. - * Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column. - * Now, we walk in a clockwise spiral shape to visit every position in this grid. - * Whenever we would move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.) - * Eventually, we reach all R * C spaces of the grid. - * Return a list of coordinates representing the positions of the grid in the order they were visited. - * - * Example 1: - * Input: R = 1, C = 4, r0 = 0, c0 = 0 - * Output: [[0,0],[0,1],[0,2],[0,3]] - * - * Example 2: - * Input: R = 5, C = 6, r0 = 1, c0 = 4 - * Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]] - * - * Note: - * 1 <= R <= 100 - * 1 <= C <= 100 - * 0 <= r0 < R - * 0 <= c0 < C - * */ -public class _885 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/spiral-matrix-iii/discuss/158977/Java-15-lines-concise-solution-with-comments*/ - public int[][] spiralMatrixIII(int R, int C, int r0, int c0) { - int[] directions = new int[]{0, 1, 0, -1, 0}; - int[][] result = new int[R * C][2]; - int i = 0; - result[i++] = new int[]{r0, c0}; - int len = 0; - int d = 0; - while (i < R * C) { - if (d == 0 || d == 2) { - //plus one when moving east or west - len++; - } - for (int k = 0; k < len; k++) { - r0 += directions[d]; - c0 += directions[d + 1]; - if (r0 >= 0 && r0 < R && c0 >= 0 && c0 < C) { - result[i++] = new int[]{r0, c0}; - } - } - d = (d + 1) % 4; - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_888.java b/src/main/java/com/fishercoder/solutions/_888.java deleted file mode 100644 index 2d330faffb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_888.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; - -/** - * 888. Fair Candy Swap - * - * Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] - * is the size of the j-th bar of candy that Bob has. - * - * Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the - * same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.) - * - * Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the - * size of the candy bar that Bob must exchange. - * - * If there are multiple answers, you may return any one of them. It is guaranteed an answer exists. - * - * - * - * Example 1: - * - * Input: A = [1,1], B = [2,2] - * Output: [1,2] - * Example 2: - * - * Input: A = [1,2], B = [2,3] - * Output: [1,2] - * Example 3: - * - * Input: A = [2], B = [1,3] - * Output: [2,3] - * Example 4: - * - * Input: A = [1,2,5], B = [2,4] - * Output: [5,4] - */ - -public class _888 { - public static class Solution1 { - public int[] fairCandySwap(int[] A, int[] B) { - int aSum = 0; - int bSum = 0; - int diff = 0; - int[] ans = new int[2]; - for (int bar : A) { - aSum += bar; - } - for (int bar : B) { - bSum += bar; - } - diff = aSum - bSum; - HashSet set = new HashSet<>(); - for (int bar : A) { - set.add(bar); - } - for (int bar : B) { - if (set.contains(bar + diff / 2)) { - ans[0] = bar + diff / 2; - ans[1] = bar; - break; - } - } - return ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_89.java b/src/main/java/com/fishercoder/solutions/_89.java deleted file mode 100644 index 0129628ae5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_89.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -public class _89 { - - public static class Solution1 { - public List grayCode(int n) { - List result = new ArrayList(); - for (int i = 0; i < (1 << n); i++) { - result.add(i ^ (i >> 1)); - } - return result; - } - } - - public static class Solution2 { - public List grayCode(int n) { - List result = new ArrayList(); - for (int i = 0; i < Math.pow(2, n); i++) { - result.add(i ^ (i >> 1)); - } - return result; - } - } - - public static void main(String... args) { - System.out.println("-----------------------------------------------------------------------------------------"); - System.out.println("How to understand i << n? It means n to the power of two, see below. So we have an equivalent solution, which is solution2."); - System.out.println("1 << 2: " + (1 << 2)); - System.out.println("1 << 3: " + (1 << 3)); - System.out.println("1 << 4: " + (1 << 4)); - System.out.println("1 << 5: " + (1 << 5)); - System.out.println("1 << 6: " + (1 << 6)); - System.out.println("-----------------------------------------------------------------------------------------"); - System.out.println("How to understand i >> 1? It means to shift the number i to the right by 1 bit, see below"); - System.out.println("0 >> 1: " + (0 >> 1)); - System.out.println("1 >> 1: " + (1 >> 1)); - System.out.println("2 >> 1: " + (2 >> 1)); - System.out.println("3 >> 1: " + (3 >> 1)); - System.out.println("4 >> 1: " + (4 >> 1)); - System.out.println("5 >> 1: " + (5 >> 1)); - System.out.println("6 >> 1: " + (6 >> 1)); - } - -} diff --git a/src/main/java/com/fishercoder/solutions/_890.java b/src/main/java/com/fishercoder/solutions/_890.java deleted file mode 100644 index 1149aedc9d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_890.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * 890. Find and Replace Pattern - * - * You have a list of words and a pattern, and you want to know which words in words matches the pattern. - * A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. - * (Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.) - * Return a list of the words in words that match the given pattern. - * You may return the answer in any order. - * - * Example 1: - * - * Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb" - * Output: ["mee","aqq"] - * Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}. - * "ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, - * since a and b map to the same letter. - * - * Note: - * 1 <= words.length <= 50 - * 1 <= pattern.length = words[i].length <= 20 - */ -public class _890 { - public static class Solution1 { - public List findAndReplacePattern(String[] words, String pattern) { - List result = new ArrayList<>(); - for (String word : words) { - Map map = new HashMap<>(); - Set set = new HashSet<>(); - boolean match = true; - for (int i = 0; i < pattern.length(); i++) { - if (map.containsKey(pattern.charAt(i))) { - if (word.charAt(i) != map.get(pattern.charAt(i))) { - match = false; - break; - } - } else { - map.put(pattern.charAt(i), word.charAt(i)); - if (!set.add(word.charAt(i))) { - match = false; - } - } - } - if (match) { - result.add(word); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_893.java b/src/main/java/com/fishercoder/solutions/_893.java deleted file mode 100644 index fcfb270910..0000000000 --- a/src/main/java/com/fishercoder/solutions/_893.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; -import java.util.stream.Collectors; - -/** - * 893. Groups of Special-Equivalent Strings - * - * You are given an array A of strings. - * A move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S. - * Two strings S and T are special-equivalent if after any number of moves onto S, S == T. - * For example, S = "zzxy" and T = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz" - * that swap S[0] and S[2], then S[1] and S[3]. - * - * Now, a group of special-equivalent strings from A is a non-empty subset of A such that: - * Every pair of strings in the group are special equivalent, and; - * The group is the largest size possible (ie., there isn't a string S not in the group such that S is special equivalent to every string in the group) - * Return the number of groups of special-equivalent strings from A. - * - * Example 1: - * Input: ["abcd","cdab","cbad","xyzz","zzxy","zzyx"] - * Output: 3 - * Explanation: - * One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings are all pairwise special equivalent to these. - * The other two groups are ["xyzz", "zzxy"] and ["zzyx"]. Note that in particular, "zzxy" is not special equivalent to "zzyx". - * - * Example 2: - * Input: ["abc","acb","bac","bca","cab","cba"] - * Output: 3 - * - * Note: - * 1 <= A.length <= 1000 - * 1 <= A[i].length <= 20 - * All A[i] have the same length. - * All A[i] consist of only lowercase letters. - * */ -public class _893 { - - public static class Solution1 { - /**my original solution, a bit lengthy: - * generate a unique signaure as key for each equivelant group and sum them up*/ - public int numSpecialEquivGroups(String[] A) { - return Arrays.stream(A).map(this::getCommonKey).collect(Collectors.toSet()).size(); - } - - private String getCommonKey(String word) { - char[] oddIndexed = new char[word.length() / 2]; - char[] evenIndexed = new char[word.length() / 2 + (word.length() % 2 == 0 ? 0 : 1)]; - char[] array = word.toCharArray(); - for (int i = 0; i < array.length - 1; i += 2) { - evenIndexed[i / 2] = array[i]; - oddIndexed[i / 2] = array[i + 1]; - } - if (word.length() % 2 != 0) { - evenIndexed[evenIndexed.length - 1] = array[array.length - 1]; - } - Arrays.sort(oddIndexed); - Arrays.sort(evenIndexed); - return new StringBuffer().append(new String(evenIndexed)).append(new String(oddIndexed)).toString(); - } - } - - public static class Solution2 { - /** - * more concise solution: https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163413/Java-Concise-Set-Solution - * but somehow a bit slower than mine: 12 ms vs 7ms - * I guess due to the problem constraint and this: "1 <= A[i].length <= 20" to have made this problem simpler - */ - public int numSpecialEquivGroups(String[] A) { - Set set = new HashSet<>(); - for (String str : A) { - int[] odd = new int[26]; - int[] even = new int[26]; - for (int i = 0; i < str.length(); i++) { - if (i % 2 == 0) { - even[str.charAt(i) - 'a']++; - } else { - odd[str.charAt(i) - 'a']++; - } - } - String key = Arrays.toString(even) + Arrays.toString(odd); - set.add(key); - } - return set.size(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_896.java b/src/main/java/com/fishercoder/solutions/_896.java deleted file mode 100644 index fb121369e4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_896.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 896. Monotonic Array - * - * An array is monotonic if it is either monotone increasing or monotone decreasing. - * - * An array A is monotone increasing if for all i <= j, A[i] <= A[j]. - * An array A is monotone decreasing if for all i <= j, A[i] >= A[j]. - * - * Return true if and only if the given array A is monotonic. - * - * Example 1: - * Input: [1,2,2,3] - * Output: true - * - * Example 2: - * Input: [6,5,4,4] - * Output: true - * - * Example 3: - * Input: [1,3,2] - * Output: false - * - * Example 4: - * Input: [1,2,4,5] - * Output: true - * - * Example 5: - * Input: [1,1,1] - * Output: true - * - * Note: - * - * 1 <= A.length <= 50000 - * -100000 <= A[i] <= 100000 - */ -public class _896 { - public static class Solution1 { - public boolean isMonotonic(int[] A) { - int i = 0; - for (; i < A.length - 1; i++) { - if (A[i] <= A[i + 1]) { - continue; - } else { - break; - } - } - if (i == A.length - 1) { - return true; - } - i = 0; - for (; i < A.length - 1; i++) { - if (A[i] >= A[i + 1]) { - continue; - } else { - break; - } - } - return i == A.length - 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_897.java b/src/main/java/com/fishercoder/solutions/_897.java deleted file mode 100644 index 58b411aa79..0000000000 --- a/src/main/java/com/fishercoder/solutions/_897.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; -import java.util.ArrayList; -import java.util.List; - -/** - * 897. Increasing Order Search Tree - * - * Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child. - * - * Example 1: - * Input: [5,3,6,2,4,null,8,1,null,null,null,7,9] - * - * 5 - * / \ - * 3 6 - * / \ \ - * 2 4 8 - * / / \ - * 1 7 9 - * - * Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9] - * - * 1 - * \ - * 2 - * \ - * 3 - * \ - * 4 - * \ - * 5 - * \ - * 6 - * \ - * 7 - * \ - * 8 - * \ - * 9 - * Note: - * - * The number of nodes in the given tree will be between 1 and 100. - * Each node will have a unique integer value from 0 to 1000. - * - */ -public class _897 { - public static class Solution1 { - public TreeNode increasingBST(TreeNode root) { - List inorderList = new ArrayList<>(); - inorderTraversal(root, inorderList); - return constructTree(inorderList); - } - - private TreeNode constructTree(List inorderList) { - if (inorderList.isEmpty() || inorderList.size() == 0) { - return null; - } - TreeNode root = new TreeNode(inorderList.get(0)); - TreeNode tmp = root; - for (int i = 1; i < inorderList.size(); i++) { - tmp.right = new TreeNode(inorderList.get(i)); - tmp = tmp.right; - } - return root; - } - - private void inorderTraversal(TreeNode root, List inorderList) { - if (root == null) { - return; - } - if (root.left != null) { - inorderTraversal(root.left, inorderList); - } - inorderList.add(root.val); - if (root.right != null) { - inorderTraversal(root.right, inorderList); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_900.java b/src/main/java/com/fishercoder/solutions/_900.java deleted file mode 100644 index 68c62e2e26..0000000000 --- a/src/main/java/com/fishercoder/solutions/_900.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 900. RLE Iterator - * - * Write an iterator that iterates through a run-length encoded sequence. - * - * The iterator is initialized by RLEIterator(int[] A), where A is a run-length encoding of some sequence. - * More specifically, for all even i, A[i] tells us the number of times that the non-negative integer value A[i+1] is repeated in the sequence. - * - * The iterator supports one function: next(int n), - * which exhausts the next n elements (n >= 1) and returns the last element exhausted in this way. - * If there is no element left to exhaust, next returns -1 instead. - * - * For example, we start with A = [3,8,0,9,2,5], which is a run-length encoding of the - * sequence [8,8,8,5,5]. - * This is because the sequence can be read as "three eights, zero nines, two fives". - * - * Example 1: - * - * Input: ["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]] - * Output: [null,8,8,5,-1] - * Explanation: - * RLEIterator is initialized with RLEIterator([3,8,0,9,2,5]). - * This maps to the sequence [8,8,8,5,5]. - * RLEIterator.next is then called 4 times: - * .next(2) exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5]. - * .next(1) exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5]. - * .next(1) exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5]. - * .next(2) exhausts 2 terms, returning -1. This is because the first term exhausted was 5, - * but the second term did not exist. Since the last term exhausted does not exist, we return -1. - * - * Note: - * - * 0 <= A.length <= 1000 - * A.length is an even integer. - * 0 <= A[i] <= 10^9 - * There are at most 1000 calls to RLEIterator.next(int n) per test case. - * Each call to RLEIterator.next(int n) will have 1 <= n <= 10^9. - */ -public class _900 { - public static class Solution1 { - public static class RLEIterator { - - int index; - int[] array; - - public RLEIterator(int[] A) { - index = 0; - array = A; - } - - public int next(int n) { - int lastElement = -1; - while (n > 0 && index < array.length) { - if (array[index] > n) { - array[index] -= n; - lastElement = array[index + 1]; - break; - } else if (array[index] == n) { - array[index] = 0; - lastElement = array[index + 1]; - index += 2; - break; - } else { - n -= array[index]; - index += 2; - } - } - return lastElement; - } - - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_905.java b/src/main/java/com/fishercoder/solutions/_905.java deleted file mode 100644 index 945683d61c..0000000000 --- a/src/main/java/com/fishercoder/solutions/_905.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 905. Sort Array By Parity - * - * Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. - * - * You may return any answer array that satisfies this condition. - * - * Example 1: - * - * Input: [3,1,2,4] - * Output: [2,4,3,1] - * The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. - * - * Note: - * - * 1 <= A.length <= 5000 - * 0 <= A[i] <= 5000 - * */ -public class _905 { - public static class Solution1 { - public int[] sortArrayByParity(int[] A) { - for (int i = 0, j = A.length - 1; i < j; i++) { - if (A[i] % 2 == 0) { - continue; - } else { - while (j > i && A[j] % 2 != 0) { - j--; - } - swap(A, i, j); - } - } - return A; - } - - private void swap(int[] A, int i, int j) { - int tmp = A[i]; - A[i] = A[j]; - A[j] = tmp; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_908.java b/src/main/java/com/fishercoder/solutions/_908.java deleted file mode 100644 index daffa98dd4..0000000000 --- a/src/main/java/com/fishercoder/solutions/_908.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 908. Smallest Range I - * - * Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i]. - * - * After this process, we have some array B. - * - * Return the smallest possible difference between the maximum value of B and the minimum value of B. - * - * - * - * Example 1: - * - * Input: A = [1], K = 0 - * Output: 0 - * Explanation: B = [1] - * Example 2: - * - * Input: A = [0,10], K = 2 - * Output: 6 - * Explanation: B = [2,8] - * Example 3: - * - * Input: A = [1,3,6], K = 3 - * Output: 0 - * Explanation: B = [3,3,3] or B = [4,4,4] - * - * - * Note: - * - * 1 <= A.length <= 10000 - * 0 <= A[i] <= 10000 - * 0 <= K <= 10000 - */ -public class _908 { - public static class Solution1 { - public int smallestRangeI(int[] A, int K) { - Arrays.sort(A); - int smallestPlus = A[0] + K; - int biggestMinus = A[A.length - 1] - K; - int diff = biggestMinus - smallestPlus; - if (diff > 0) { - return diff; - } else { - return 0; - } - } - } - - public static class Solution2 { - public int smallestRangeI(int[] A, int K) { - int min = A[0]; - int max = A[0]; - - for (int k : A) { - min = Math.min(min, k); - max = Math.max(max, k); - } - - return Math.max(max - min - 2 * K, 0); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_91.java b/src/main/java/com/fishercoder/solutions/_91.java deleted file mode 100644 index 8bb82270eb..0000000000 --- a/src/main/java/com/fishercoder/solutions/_91.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder.solutions; - -public class _91 { - /** - * Credit: https://discuss.leetcode.com/topic/35840/java-clean-dp-solution-with-explanation - * I used a dp array of size n + 1 to save subproblem solutions. - * dp[0] means an empty string will have one way to decode, - * dp[1] means the way to decode a string of size 1. - * I then check one digit and two digit combination and save the results along the way. - * In the end, dp[n] will be the end result. - */ - - public static class Solution1 { - public int numDecodings(String s) { - if (s == null || s.length() == 0) { - return 0; - } - int[] dp = new int[s.length() + 1]; - dp[0] = 1; - dp[1] = (s.charAt(0) != '0') ? 1 : 0; - for (int i = 2; i <= s.length(); i++) { - int first = Integer.valueOf(s.substring(i - 1, i)); - int second = Integer.valueOf(s.substring(i - 2, i)); - if (first > 0 && first <= 9) { - dp[i] += dp[i - 1]; - } - if (second >= 10 && second <= 26) { - dp[i] += dp[i - 2]; - } - } - return dp[s.length()]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_912.java b/src/main/java/com/fishercoder/solutions/_912.java deleted file mode 100644 index 0abd11eb5a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_912.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 912. Sort an Array - * - * Given an array of integers nums, sort the array in ascending order. - * - * Example 1: - * - * Input: [5,2,3,1] - * Output: [1,2,3,5] - * Example 2: - * - * Input: [5,1,1,2,0,0] - * Output: [0,0,1,1,2,5] - * - * - * Note: - * - * 1 <= A.length <= 10000 - * -50000 <= A[i] <= 50000 - * */ -public class _912 { - public static class Solution1 { - public int[] sortArray(int[] nums) { - Arrays.sort(nums); - return nums; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_914.java b/src/main/java/com/fishercoder/solutions/_914.java deleted file mode 100644 index 7e7509b502..0000000000 --- a/src/main/java/com/fishercoder/solutions/_914.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 914. X of a Kind in a Deck of Cards - * - * In a deck of cards, each card has an integer written on it. - * Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where: - * Each group has exactly X cards. - * All the cards in each group have the same integer. - * - * Example 1: - * Input: [1,2,3,4,4,3,2,1] - * Output: true - * Explanation: Possible partition [1,1],[2,2],[3,3],[4,4] - * - * Example 2: - * Input: [1,1,1,2,2,2,3,3] - * Output: false - * Explanation: No possible partition. - * - * Example 3: - * Input: [1] - * Output: false - * Explanation: No possible partition. - * - * Example 4: - * Input: [1,1] - * Output: true - * Explanation: Possible partition [1,1] - * - * Example 5: - * Input: [1,1,2,2,2,2] - * Output: true - * Explanation: Possible partition [1,1],[2,2],[2,2] - */ -public class _914 { - public static class Solution1 { - public boolean hasGroupsSizeX(int[] deck) { - //Size too small for partitions - if (deck.length < 2) { - return false; - } - - //Track repetitions of values in deck array - Map mapReps = new HashMap<>(); - for (int card : deck) { - if (!mapReps.containsKey(card)) { - mapReps.put(card, 1); - } else { - mapReps.put(card, mapReps.get(card) + 1); - } - } - - //Create array of map values - int num = 0; - int[] arrReps = new int[mapReps.size()]; - for (Map.Entry e : mapReps.entrySet()) { - arrReps[num++] = e.getValue(); - } - - //Find greatest common denominator - num = arrGCD(arrReps, arrReps.length); - - //If gcd of all repetitions is greater than 1, it's partitionable. - return num > 1; - } - - private int gcd(int a, int b) { - return b == 0 ? a : gcd(b, a % b); - } - - private int arrGCD(int[] arr, int n) { - int result = arr[0]; - for (int i = 1; i < n; i++) { - result = gcd(arr[i], result); - } - - return result; - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_917.java b/src/main/java/com/fishercoder/solutions/_917.java deleted file mode 100644 index e950ceb5af..0000000000 --- a/src/main/java/com/fishercoder/solutions/_917.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 917. Reverse Only Letters - * - * Given a string S, return the "reversed" string where all characters - * that are not a letter stay in the same place, and all letters reverse their positions. - * - * Example 1: - * - * Input: "ab-cd" - * Output: "dc-ba" - * Example 2: - * - * Input: "a-bC-dEf-ghIj" - * Output: "j-Ih-gfE-dCba" - * Example 3: - * - * Input: "Test1ng-Leet=code-Q!" - * Output: "Qedo1ct-eeLg=ntse-T!" - * - * - * Note: - * - * S.length <= 100 - * 33 <= S[i].ASCIIcode <= 122 - * S doesn't contain \ or " - * */ -public class _917 { - public static class Solution1 { - public String reverseOnlyLetters(String S) { - char[] array = S.toCharArray(); - for (int i = 0, j = array.length - 1; i < j;) { - if (Character.isLetter(array[i]) && Character.isLetter(array[j])) { - char temp = array[i]; - array[i++] = array[j]; - array[j--] = temp; - } else if (Character.isLetter(array[i])) { - j--; - } else if (Character.isLetter(array[j])) { - i++; - } else { - i++; - j--; - } - } - return new String(array); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_92.java b/src/main/java/com/fishercoder/solutions/_92.java deleted file mode 100644 index c8a16a5a4b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_92.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -public class _92 { - - public static class Solution1 { - /** - * credit: https://discuss.leetcode.com/topic/8976/simple-java-solution-with-clear-explanation - */ - public ListNode reverseBetween(ListNode head, int m, int n) { - // use four nodes, pre, start, then, dummy - // just reverse the nodes along the way - ListNode dummy = new ListNode(-1); - dummy.next = head; - ListNode pre = dummy; - for (int i = 0; i < m - 1; i++) { - pre = pre.next; - } - - ListNode start = pre.next;// start is the node prior to reversing, in the given example, - // start is node with value 1 - ListNode then = start.next;// then is the node that we'll start to reverse, in the given - // example, it's 2 - - for (int i = 0; i < n - m; i++) { - // pay special attention to this for loop, it's assigning then.next to start.next, it - // didn't initialize a new node - // this does exactly what I desired to do, but I just didn't figure out how to implement - // it, thumbs up to the OP! - start.next = then.next; - then.next = pre.next; - pre.next = then; - then = start.next; - } - - return dummy.next; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_921.java b/src/main/java/com/fishercoder/solutions/_921.java deleted file mode 100644 index a80825c849..0000000000 --- a/src/main/java/com/fishercoder/solutions/_921.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 921. Minimum Add to Make Parentheses Valid - * - * Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', and in any positions ) - * so that the resulting parentheses string is valid. - * - * Formally, a parentheses string is valid if and only if: - * It is the empty string, or - * It can be written as AB (A concatenated with B), where A and B are valid strings, or - * It can be written as (A), where A is a valid string. - * Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid. - * - * Example 1: - * Input: "())" - * Output: 1 - * - * Example 2: - * Input: "(((" - * Output: 3 - * - * Example 3: - * Input: "()" - * Output: 0 - * - * Example 4: - * Input: "()))((" - * Output: 4 - * - * Note: - * S.length <= 1000 - * S only consists of '(' and ')' characters. - * */ -public class _921 { - public static class Solution1 { - public int minAddToMakeValid(String S) { - Stack stack = new Stack<>(); - for (char c : S.toCharArray()) { - if (c == ')') { - if (!stack.isEmpty() && stack.peek() == '(') { - stack.pop(); - } else { - stack.push(c); - } - } else { - stack.push(c); - } - } - return stack.size(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_922.java b/src/main/java/com/fishercoder/solutions/_922.java deleted file mode 100644 index e8f63a7991..0000000000 --- a/src/main/java/com/fishercoder/solutions/_922.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 922. Sort Array By Parity II - * - * Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. - * Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even. - * You may return any answer array that satisfies this condition. - * - * Example 1: - * - * Input: [4,2,5,7] - * Output: [4,5,2,7] - * Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted. - * - * - * Note: - * 2 <= A.length <= 20000 - * A.length % 2 == 0 - * 0 <= A[i] <= 1000 - * */ -public class _922 { - public static class Solution1 { - public int[] sortArrayByParityII(int[] A) { - for (int i = 0, j = 1; i < A.length - 1 && j < A.length;) { - if (A[i] % 2 != 0 && A[j] % 2 == 0) { - int tmp = A[i]; - A[i] = A[j]; - A[j] = tmp; - i += 2; - j += 2; - } - while (i < A.length - 1 && A[i] % 2 == 0) { - i += 2; - } - while (j < A.length && A[j] % 2 != 0) { - j += 2; - } - } - return A; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_925.java b/src/main/java/com/fishercoder/solutions/_925.java deleted file mode 100644 index 1f834928d7..0000000000 --- a/src/main/java/com/fishercoder/solutions/_925.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 925. Long Pressed Name - * - * Your friend is typing his name into a keyboard. - * Sometimes, when typing a character c, the key might get long pressed, - * and the character will be typed 1 or more times. - * - * You examine the typed characters of the keyboard. - * Return True if it is possible that it was your friends name, - * with some characters (possibly none) being long pressed. - * - * Example 1: - * - * Input: name = "alex", typed = "aaleex" - * Output: true - * Explanation: 'a' and 'e' in 'alex' were long pressed. - * Example 2: - * - * Input: name = "saeed", typed = "ssaaedd" - * Output: false - * Explanation: 'e' must have been pressed twice, but it wasn't in the typed output. - * Example 3: - * - * Input: name = "leelee", typed = "lleeelee" - * Output: true - * Example 4: - * - * Input: name = "laiden", typed = "laiden" - * Output: true - * Explanation: It's not necessary to long press any character. - * - * Note: - * name.length <= 1000 - * typed.length <= 1000 - * The characters of name and typed are lowercase letters. - * */ -public class _925 { - public static class Solution1 { - public boolean isLongPressedName(String name, String typed) { - int i = 0; - for (int j = 0; i < name.length() && j < typed.length(); i++) { - if (name.charAt(i) != typed.charAt(j)) { - return false; - } else if (i < name.length() - 1 && name.charAt(i) != name.charAt(i + 1)) { - j++; - while (j < typed.length() && name.charAt(i) == typed.charAt(j)) { - j++; - } - } else { - j++; - } - - } - return i == name.length(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_929.java b/src/main/java/com/fishercoder/solutions/_929.java deleted file mode 100644 index 6602c0acca..0000000000 --- a/src/main/java/com/fishercoder/solutions/_929.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 929. Unique Email Addresses - * - * Every email consists of a local name and a domain name, separated by the @ sign. - * For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name. - * Besides lowercase letters, these emails may contain '.'s or '+'s. - * If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. - * For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address. (Note that this rule does not apply for domain names.) - * If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. - * This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com. (Again, this rule does not apply for domain names.) - * It is possible to use both of these rules at the same time. - * Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails? - * - * Example 1: - * - * Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"] - * Output: 2 - * - * Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails - * - * Note: - * - * 1 <= emails[i].length <= 100 - * 1 <= emails.length <= 100 - * Each emails[i] contains exactly one '@' character. - * All local and domain names are non-empty. - * Local names do not start with a '+' character. - * */ -public class _929 { - public static class Solution1 { - public int numUniqueEmails(String[] emails) { - Map> map = new HashMap<>(); - for (String email : emails) { - String[] parts = email.split("@"); - if (!map.containsKey(parts[1])) { - map.put(parts[1], new HashSet<>()); - } - String filteredLocalName = parts[0].substring(0, parts[0].indexOf('+')); - filteredLocalName = filteredLocalName.replace(".", ""); - map.get(parts[1]).add(filteredLocalName); - } - return map.keySet().stream().mapToInt(key -> map.get(key).size()).sum(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_931.java b/src/main/java/com/fishercoder/solutions/_931.java deleted file mode 100644 index 14f414b5d2..0000000000 --- a/src/main/java/com/fishercoder/solutions/_931.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 931. Minimum Falling Path Sum - * - * Given a square array of integers A, we want the minimum sum of a falling path through A. - * A falling path starts at any element in the first row, and chooses one element from each row. - * The next row's choice must be in a column that is different from the previous row's column by at most one. - * - * Example 1: - * Input: [[1,2,3],[4,5,6],[7,8,9]] - * Output: 12 - * Explanation: - * The possible falling paths are: - * [1,4,7], [1,4,8], [1,5,7], [1,5,8], [1,5,9] - * [2,4,7], [2,4,8], [2,5,7], [2,5,8], [2,5,9], [2,6,8], [2,6,9] - * [3,5,7], [3,5,8], [3,5,9], [3,6,8], [3,6,9] - * The falling path with the smallest sum is [1,4,7], so the answer is 12. - * - * Note: - * 1 <= A.length == A[0].length <= 100 - * -100 <= A[i][j] <= 100 - * */ -public class _931 { - public static class Solution1 { - public int minFallingPathSum(int[][] A) { - int size = A.length; - int[][] dp = new int[size][size]; - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - if (i == 0) { - dp[i][j] = A[i][j]; - } else { - int lastRow = dp[i - 1][j]; - if (j - 1 >= 0) { - lastRow = Math.min(dp[i - 1][j - 1], lastRow); - } - if (j + 1 < size) { - lastRow = Math.min(dp[i - 1][j + 1], lastRow); - } - dp[i][j] = lastRow + A[i][j]; - } - } - } - int minSum = Integer.MAX_VALUE; - for (int i = 0; i < size; i++) { - minSum = Math.min(minSum, dp[size - 1][i]); - } - return minSum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_933.java b/src/main/java/com/fishercoder/solutions/_933.java deleted file mode 100644 index 7c5d2697a1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_933.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Deque; -import java.util.LinkedList; - -/** - * 933. Number of Recent Calls - * - * Write a class RecentCounter to count recent requests. - * It has only one method: ping(int t), where t represents some time in milliseconds. - * Return the number of pings that have been made from 3000 milliseconds ago until now. - * Any ping with time in [t - 3000, t] will count, including the current ping. - * It is guaranteed that every call to ping uses a strictly larger value of t than before. - * - * Example 1: - * Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]] - * Output: [null,1,2,3,3] - * - * Note: - * Each test case will have at most 10000 calls to ping. - * Each test case will call ping with strictly increasing values of t. - * Each call to ping will have 1 <= t <= 10^9. - * */ -public class _933 { - public static class Solution1 { - public static class RecentCounter { - - Deque deque; - - public RecentCounter() { - deque = new LinkedList<>(); - } - - public int ping(int t) { - while (!deque.isEmpty() && t - deque.getFirst() > 3000) { - deque.removeFirst(); - } - deque.addLast(t); - return deque.size(); - } - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_935.java b/src/main/java/com/fishercoder/solutions/_935.java deleted file mode 100644 index d5b320ba8a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_935.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 935. Knight Dialer - * - * A chess knight can move as indicated in the chess diagram below: - * - * https://assets.leetcode.com/uploads/2018/10/12/knight.png - * - * |---|---|---| - * | 1 | 2 | 3 | - * |---|---|---| - * | 4 | 5 | 6 | - * |---|---|---| - * | 7 | 8 | 9 | - * |---|---|---| - * | x | 0 | x | - * |---|---|---| - * - * This time, we place our chess knight on any numbered key of a phone pad (indicated above), and the knight makes N-1 hops. - * Each hop must be from one key to another numbered key. - * - * Each time it lands on a key (including the initial placement of the knight), it presses the number of that key, pressing N digits total. - * - * How many distinct numbers can you dial in this manner? - * - * Since the answer may be large, output the answer modulo 10^9 + 7. - * - * Note: - * * 1 <= N <= 5000 - */ - -public class _935 { - /* - * The intuition is to calculate the number of ways - * we can reach a key k after i hops, based on the number of ways we can reach keys x after i-1 hops - * s.t. the knight can move from x to k in one move - * For example, - * We can reach 6 in 3 ways after 1 hop (1 -> 6, 7 -> 6 or 0 -> 6) - * We can reach 8 in 2 ways after 1 hop (1 -> 8 or 3 -> 8) - * Thus, we can reach 1 in 5 ways after 2 hops: - * . 1. 1 -> 6 -> 1 - * . 2. 7 -> 6 -> 1 - * . 3. 0 -> 6 -> 1 - * 4. 1 -> 8 -> 1 - * 5. 3 -> 8 -> 1 - */ - public static class Solution1 { - private static final int MOD = 1000_000_007; - - // whereFromHere[i] is an array of keys that can be reached from the ith digit - private static final int[][] whereFromHere = { - {4, 6}, {6, 8}, {7, 9}, {4, 8}, // 0, 1, 2, 3 - {3, 9, 0}, {}, {1, 7, 0}, // 4, 5, 6 - {2, 6}, {1, 3}, {2, 4} // 7, 8, 9 - }; - - public int knightDialer(int N) { - // a[i] is the number of ways we can end up on the ith digit - // The initial array is for N = 1, i.e. for 0 hops. - long[] a = new long[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - - // Simulate N - 1 hops - for (int i = 0; i < N - 1; ++i) { - long[] tmp = new long[10]; - - // For each digit - for (int j = 0; j < 10; j++) { - // Which other digits can we reach? - for (int k : whereFromHere[j]) { - tmp[j] = (tmp[j] + a[k]) % MOD; - } - } - - // Sanity checks based on symmetry of the keypad - assert tmp[1] == tmp[3]; - assert tmp[4] == tmp[6]; - assert tmp[7] == tmp[9]; - - a = tmp; - } - - long ans = 0; - for (long k : a) { - ans = (ans + k) % MOD; - } - - return (int) ans; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_937.java b/src/main/java/com/fishercoder/solutions/_937.java deleted file mode 100644 index 5f4ba57b95..0000000000 --- a/src/main/java/com/fishercoder/solutions/_937.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; -import java.util.TreeMap; - -/** - * 937. Reorder Log Files - * - * You have an array of logs. Each log is a space delimited string of words. - * - * For each log, the first word in each log is an alphanumeric identifier. Then, either: - * - * Each word after the identifier will consist only of lowercase letters, or; - * Each word after the identifier will consist only of digits. - * We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier. - * - * Reorder the logs so that all of the letter-logs come before any digit-log. - * The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. - * The digit-logs should be put in their original order. - * - * Return the final order of the logs. - * - * Example 1: - * - * Input: ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"] - * Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"] - * - * Note: - * 0 <= logs.length <= 100 - * 3 <= logs[i].length <= 100 - * logs[i] is guaranteed to have an identifier, and a word after the identifier. - */ -public class _937 { - public static class Solution1 { - public String[] reorderLogFiles(String[] logs) { - TreeMap letterLogMap = new TreeMap<>(); - List digitLogList = new ArrayList<>(); - for (String log : logs) { - int firstSpaceIndex = log.indexOf(' '); - String id = log.substring(0, firstSpaceIndex); - if (Character.isAlphabetic(log.charAt(firstSpaceIndex + 1))) { - String key = log.substring(firstSpaceIndex + 1) + id; - letterLogMap.put(key, log); - } else { - digitLogList.add(log); - } - } - String[] reorderedLogs = new String[logs.length]; - int i = 0; - for (String key : letterLogMap.keySet()) { - reorderedLogs[i++] = letterLogMap.get(key); - } - for (String log : digitLogList) { - reorderedLogs[i++] = log; - } - return reorderedLogs; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_938.java b/src/main/java/com/fishercoder/solutions/_938.java deleted file mode 100644 index d2674fcb7a..0000000000 --- a/src/main/java/com/fishercoder/solutions/_938.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; -import java.util.ArrayList; -import java.util.List; - -/** - * 938. Range Sum of BST - * - * Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). - * - * The binary search tree is guaranteed to have unique values. - * - * Example 1: - * Input: root = [10,5,15,3,7,null,18], L = 7, R = 15 - * Output: 32 - * - * Example 2: - * Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10 - * Output: 23 - * - * Note: - * The number of nodes in the tree is at most 10000. - * The final answer is guaranteed to be less than 2^31. - */ -public class _938 { - public static class Solution1 { - public int rangeSumBST(TreeNode root, int L, int R) { - if (root == null) { - return 0; - } - List list = new ArrayList<>(); - dfs(root, L, R, list); - return list.stream().mapToInt(num -> num).sum(); - } - - private void dfs(TreeNode root, int l, int r, List list) { - if (root == null) { - return; - } - if (root.val <= r && root.val >= l) { - list.add(root.val); - } - dfs(root.left, l, r, list); - dfs(root.right, l, r, list); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_941.java b/src/main/java/com/fishercoder/solutions/_941.java deleted file mode 100644 index 79b2f5e322..0000000000 --- a/src/main/java/com/fishercoder/solutions/_941.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 941. Valid Mountain Array - * - * Given an array A of integers, return true if and only if it is a valid mountain array. - * - * Recall that A is a mountain array if and only if: - * - * A.length >= 3 - * There exists some i with 0 < i < A.length - 1 such that: - * A[0] < A[1] < ... A[i-1] < A[i] - * A[i] > A[i+1] > ... > A[B.length - 1] - * - * - * Example 1: - * - * Input: [2,1] - * Output: false - * Example 2: - * - * Input: [3,5,5] - * Output: false - * Example 3: - * - * Input: [0,3,2,1] - * Output: true - * - * - * Note: - * - * 0 <= A.length <= 10000 - * 0 <= A[i] <= 10000 - * */ -public class _941 { - public static class Solution1 { - public boolean validMountainArray(int[] A) { - int i = 0; - for (; i < A.length - 1; i++) { - if (A[i] < A[i + 1]) { - continue; - } else if (A[i] == A[i + 1]) { - return false; - } else { - break; - } - } - if (i == 0 || i >= A.length - 1) { - return false; - } - for (; i < A.length - 1; i++) { - if (A[i] > A[i + 1]) { - continue; - } else { - return false; - } - } - return i == A.length - 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_942.java b/src/main/java/com/fishercoder/solutions/_942.java deleted file mode 100644 index 7ce292e4ea..0000000000 --- a/src/main/java/com/fishercoder/solutions/_942.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Queue; - -/** - * 942. DI String Match - * - * Given a string S that only contains "I" (increase) or "D" (decrease), let N = S.length. - * - * Return any permutation A of [0, 1, ..., N] such that for all i = 0, ..., N-1: - * - * If S[i] == "I", then A[i] < A[i+1] - * If S[i] == "D", then A[i] > A[i+1] - * - * Example 1: - * Input: "IDID" - * Output: [0,4,1,3,2] - * - * Example 2: - * Input: "III" - * Output: [0,1,2,3] - * - * Example 3: - * Input: "DDI" - * Output: [3,2,0,1] - * - * Note: - * 1 <= S.length <= 10000 - * S only contains characters "I" or "D". - */ -public class _942 { - public static class Solution1 { - public int[] diStringMatch(String S) { - Queue deque = new ArrayDeque<>(); - for (int i = 0; i <= S.length(); i++) { - deque.add(i); - } - int[] result = new int[S.length() + 1]; - for (int i = 0; i <= S.length(); i++) { - if (i == S.length()) { - result[i] = ((ArrayDeque) deque).pollLast(); - } else if (S.charAt(i) == 'I') { - result[i] = ((ArrayDeque) deque).pollFirst(); - } else { - result[i] = ((ArrayDeque) deque).pollLast(); - } - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_944.java b/src/main/java/com/fishercoder/solutions/_944.java deleted file mode 100644 index 366b49c105..0000000000 --- a/src/main/java/com/fishercoder/solutions/_944.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 944. Delete Columns to Make Sorted - * - * We are given an array A of N lowercase letter strings, all of the same length. - * Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. - * For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, - * then the final array after deletions is ["bef", "vyz"], and the remaining columns of A are ["b","v"], ["e","y"], - * and ["f","z"]. (Formally, the c-th column is [A[0][c], A[1][c], ..., A[A.length-1][c]].) - * - * Suppose we chose a set of deletion indices D such that after deletions, each remaining column in A is in non-decreasing sorted order. - * - * Return the minimum possible value of D.length. - * - * Example 1: - * - * Input: ["cba","daf","ghi"] - * Output: 1 - * Explanation: - * After choosing D = {1}, each column ["c","d","g"] and ["a","f","i"] are in non-decreasing sorted order. - * If we chose D = {}, then a column ["b","a","h"] would not be in non-decreasing sorted order. - * Example 2: - * - * Input: ["a","b"] - * Output: 0 - * Explanation: D = {} - * Example 3: - * - * Input: ["zyx","wvu","tsr"] - * Output: 3 - * Explanation: D = {0, 1, 2} - * - * - * Note: - * - * 1 <= A.length <= 100 - * 1 <= A[i].length <= 1000 - * */ -public class _944 { - public static class Solution1 { - public int minDeletionSize(String[] A) { - if (A == null || A.length == 0) { - return 0; - } - int deletion = 0; - for (int i = 0; i < A[0].length(); i++) { - for (int j = 0; j < A.length - 1; j++) { - if (A[j].charAt(i) > A[j + 1].charAt(i)) { - deletion++; - break; - } - } - } - return deletion; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_946.java b/src/main/java/com/fishercoder/solutions/_946.java deleted file mode 100644 index 536bc8277b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_946.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Stack; - -/** - * 946. Validate Stack Sequences - * - * Given two sequences pushed and popped with distinct values, - * return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack. - * - * Example 1: - * Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] - * Output: true - * Explanation: We might do the following sequence: - * push(1), push(2), push(3), push(4), pop() -> 4, - * push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 - * - * Example 2: - * Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2] - * Output: false - * Explanation: 1 cannot be popped before 2. - * - * Note: - * 0 <= pushed.length == popped.length <= 1000 - * 0 <= pushed[i], popped[i] < 1000 - * pushed is a permutation of popped. - * pushed and popped have distinct values. - * */ -public class _946 { - public static class Solution1 { - public boolean validateStackSequences(int[] pushed, int[] popped) { - if (pushed == null || popped == null || pushed.length == 0 || popped.length == 0) { - return true; - } - Stack stack = new Stack<>(); - stack.push(pushed[0]); - int i = 1; - int j = 0; - while (!stack.isEmpty() || j < popped.length) { - if (j < popped.length && !stack.isEmpty() && stack.peek() == popped[j]) { - stack.pop(); - j++; - } else { - if (i < pushed.length) { - stack.push(pushed[i++]); - } else { - return stack.isEmpty(); - } - } - } - return stack.isEmpty(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_950.java b/src/main/java/com/fishercoder/solutions/_950.java deleted file mode 100644 index 7fe293bc2b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_950.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayDeque; -import java.util.Arrays; -import java.util.Deque; - -/** - * 950. Reveal Cards In Increasing Order - * - * In a deck of cards, every card has a unique integer. You can order the deck in any order you want. - * Initially, all the cards start face down (unrevealed) in one deck. - * Now, you do the following steps repeatedly, until all cards are revealed: - * Take the top card of the deck, reveal it, and take it out of the deck. - * If there are still cards in the deck, put the next top card of the deck at the bottom of the deck. - * If there are still unrevealed cards, go back to step 1. Otherwise, stop. - * Return an ordering of the deck that would reveal the cards in increasing order. - * - * The first entry in the answer is considered to be the top of the deck. - * - * Example 1: - * Input: [17,13,11,2,3,5,7] - * Output: [2,13,3,11,5,17,7] - * - * Explanation: - * We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it. - * After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck. - * We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13]. - * We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11]. - * We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17]. - * We reveal 7, and move 13 to the bottom. The deck is now [11,17,13]. - * We reveal 11, and move 17 to the bottom. The deck is now [13,17]. - * We reveal 13, and move 17 to the bottom. The deck is now [17]. - * We reveal 17. - * Since all the cards revealed are in increasing order, the answer is correct. - * - * Note: - * 1 <= A.length <= 1000 - * 1 <= A[i] <= 10^6 - * A[i] != A[j] for all i != j - */ -public class _950 { - public static class Solution1 { - public int[] deckRevealedIncreasing(int[] deck) { - Arrays.sort(deck); - Deque deque = new ArrayDeque<>(); - for (int i = deck.length - 1; i >= 0; i--) { - if (i != deck.length - 1) { - deque.addFirst(deque.pollLast()); - } - deque.addFirst(deck[i]); - } - int[] result = new int[deck.length]; - int i = 0; - while (!deque.isEmpty()) { - result[i++] = deque.pollFirst(); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_951.java b/src/main/java/com/fishercoder/solutions/_951.java deleted file mode 100644 index cd520261b1..0000000000 --- a/src/main/java/com/fishercoder/solutions/_951.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 951. Flip Equivalent Binary Trees - * - * For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. - * - * A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations. - * - * Write a function that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1 and root2. - * - * Note: - * * Each tree will have at most 100 nodes. - * * Each value in each tree will be a unique integer in the range [0, 99]. - */ - -public class _951 { - public static class Solution1 { - public boolean flipEquiv(TreeNode root1, TreeNode root2) { - if (root1 == null && root2 == null) { - return true; - } - if (root1 == null || root2 == null) { - return false; - } - - if (root1.val != root2.val) { - return false; - } - - return ( - (flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right)) - || (flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left)) - ); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_953.java b/src/main/java/com/fishercoder/solutions/_953.java deleted file mode 100644 index c3dab20a16..0000000000 --- a/src/main/java/com/fishercoder/solutions/_953.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.Map; - -/** - * 953. Verifying an Alien Dictionary - * - * In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. - * - * Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language. - * - * Example 1: - * - * Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz" - * Output: true - * Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted. - * - * Example 2: - * - * Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz" - * Output: false - * Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted. - * - * Example 3: - * - * Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz" - * Output: false - * Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info). - * - * - * Note: - * 1 <= words.length <= 100 - * 1 <= words[i].length <= 20 - * order.length == 26 - * All characters in words[i] and order are english lowercase letters. - */ -public class _953 { - public static class Solution1 { - public boolean isAlienSorted(String[] words, String order) { - if (words.length == 1) { - return true; - } - - Map map = new HashMap<>(); - for (int i = 0; i < order.length(); i++) { - map.put(order.charAt(i), i); - } - - for (int i = 0; i < words.length - 1; i++) { - String firstWord = words[i]; - String secondWord = words[i + 1]; - if (!sorted(firstWord, secondWord, map)) { - return false; - } - } - return true; - } - - private boolean sorted(String firstWord, String secondWord, Map map) { - for (int i = 0; i < Math.min(firstWord.length(), secondWord.length()); i++) { - if (firstWord.charAt(i) == secondWord.charAt(i)) { - continue; - } else { - if (map.get(firstWord.charAt(i)) > map.get(secondWord.charAt(i))) { - return false; - } else { - return true; - } - } - } - return firstWord.length() <= secondWord.length(); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_958.java b/src/main/java/com/fishercoder/solutions/_958.java deleted file mode 100644 index 61fb3b570f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_958.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 958. Check Completeness of a Binary Tree - * - * Given a binary tree, determine if it is a complete binary tree. - * Definition of a complete binary tree from Wikipedia: - * In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. - * It can have between 1 and 2h nodes inclusive at the last level h. - * - * Example 1: - * 1 - * / \ - * 2 3 - * / \ / - * 4 5 6 - * - * Input: [1,2,3,4,5,6] - * Output: true - * Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}), - * and all nodes in the last level ({4, 5, 6}) are as far left as possible. - * - * Example 2: - * 1 - * / \ - * 2 3 - * / \ \ - * 4 5 7 - * Input: [1,2,3,4,5,null,7] - * Output: false - * Explanation: The node with value 7 isn't as far left as possible. - * - * Note: - * The tree will have between 1 and 100 nodes. - * */ -public class _958 { - public static class Solution1 { - public boolean isCompleteTree(TreeNode root) { - Queue queue = new LinkedList<>(); - queue.offer(root); - boolean shouldHaveNoMoreChildren = false; - while (!queue.isEmpty()) { - int size = queue.size(); - for (int i = 0; i < size; i++) { - TreeNode curr = queue.poll(); - if (shouldHaveNoMoreChildren && (curr.left != null || curr.right != null)) { - return false; - } - if (curr.left == null && curr.right != null) { - return false; - } - if (curr.left != null) { - queue.offer(curr.left); - } - if (curr.right == null) { - shouldHaveNoMoreChildren = true; - } else { - queue.offer(curr.right); - } - } - } - return true; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_961.java b/src/main/java/com/fishercoder/solutions/_961.java deleted file mode 100644 index 2181c8b368..0000000000 --- a/src/main/java/com/fishercoder/solutions/_961.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -/** - * 961. N-Repeated Element in Size 2N Array - * - * In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. - * - * Return the element repeated N times. - * - * Example 1: - * - * Input: [1,2,3,3] - * Output: 3 - * Example 2: - * - * Input: [2,1,2,5,3,2] - * Output: 2 - * Example 3: - * - * Input: [5,1,5,2,5,3,5,4] - * Output: 5 - * - * - * Note: - * - * 4 <= A.length <= 10000 - * 0 <= A[i] < 10000 - * A.length is even - * */ -public class _961 { - public static class Solution1 { - public int repeatedNTimes(int[] A) { - Set set = new HashSet<>(); - for (int num : A) { - if (!set.add(num)) { - return num; - } - } - return -1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_965.java b/src/main/java/com/fishercoder/solutions/_965.java deleted file mode 100644 index 4726fdbe2f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_965.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 965. Univalued Binary Tree - * - * A binary tree is univalued if every node in the tree has the same value. - * - * Return true if and only if the given tree is univalued. - * - * Example 1: - * - * 1 - * / \ - * 1 1 - * / \ \ - * 1 1 1 - * - * Input: [1,1,1,1,1,null,1] - * Output: true - * - * - * Example 2: - * 2 - * / \ - * 2 2 - * / \ - * 5 2 - * - * Input: [2,2,2,5,2] - * Output: false - * - * - * Note: - * - * The number of nodes in the given tree will be in the range [1, 100]. - * Each node's value will be an integer in the range [0, 99]. - */ -public class _965 { - public static class Solution1 { - public boolean isUnivalTree(TreeNode root) { - if (root == null) { - return true; - } - return dfs(root, root.val); - } - - private boolean dfs(TreeNode root, int value) { - if (root == null) { - return true; - } - if (root.val != value) { - return false; - } - return dfs(root.left, value) && dfs(root.right, value); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_966.java b/src/main/java/com/fishercoder/solutions/_966.java deleted file mode 100644 index b2f126039e..0000000000 --- a/src/main/java/com/fishercoder/solutions/_966.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * 966. Vowel Spellchecker - * - * Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word. - * - * For a given query word, the spell checker handles two categories of spelling mistakes: - * - * Capitalization: If the query matches a word in the wordlist (case-insensitive), then the query word is returned with - * the same case as the case in the wordlist. - * Example: wordlist = ["yellow"], query = "YellOw": correct = "yellow" - * Example: wordlist = ["Yellow"], query = "yellow": correct = "Yellow" - * Example: wordlist = ["yellow"], query = "yellow": correct = "yellow" - * - * Vowel Errors: If after replacing the vowels ('a', 'e', 'i', 'o', 'u') of the query word with any vowel individually, - * it matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the - * match in the wordlist. - * Example: wordlist = ["YellOw"], query = "yollow": correct = "YellOw" - * Example: wordlist = ["YellOw"], query = "yeellow": correct = "" (no match) - * Example: wordlist = ["YellOw"], query = "yllw": correct = "" (no match) - * - * In addition, the spell checker operates under the following precedence rules: - * - * When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back. - * When the query matches a word up to capitlization, you should return the first such match in the wordlist. - * When the query matches a word up to vowel errors, you should return the first such match in the wordlist. - * If the query has no matches in the wordlist, you should return the empty string. - * - * Given some queries, return a list of words answer, where answer[i] is the correct word for query = queries[i]. - * - * Example 1: - * - * Input: wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"] - * Output: ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"] - * - * Note: - * - * 1 <= wordlist.length <= 5000 - * 1 <= queries.length <= 5000 - * 1 <= wordlist[i].length <= 7 - * 1 <= queries[i].length <= 7 - * All strings in wordlist and queries consist only of english letters. - * - * */ - -public class _966 { - - public static class Solution1 { - public String[] spellchecker(String[] wordlist, String[] queries) { - Map caseMap = new HashMap<>(); - Set set = new HashSet<>(); - - // Case Part Mapping - for (String word : wordlist) { - if (!caseMap.containsKey(word.toLowerCase())) { - caseMap.put(word.toLowerCase(), word); - } - - set.add(word); - } - - // Vowel Part Mapping - Map vowelMap = new HashMap<>(); - for (String word : wordlist) { - String genericVal = makeGenericVowel(word); - if (!vowelMap.containsKey(genericVal)) { - vowelMap.put(genericVal, word); - } - } - - String[] ans = new String[queries.length]; - - for (int i = 0; i < queries.length; i++) { - if (set.contains(queries[i])) { - ans[i] = queries[i]; - } else if (caseMap.containsKey(queries[i].toLowerCase())) { - ans[i] = caseMap.get(queries[i].toLowerCase()); - } else if (vowelMap.containsKey(makeGenericVowel(queries[i]))) { - ans[i] = vowelMap.get(makeGenericVowel(queries[i])); - } else { - ans[i] = ""; - } - } - - return ans; - } - - private String makeGenericVowel(String s) { - String vowel = "aeiou"; - char[] ch = s.toLowerCase().toCharArray(); - for (int i = 0; i < ch.length; i++) { - if (vowel.indexOf(ch[i]) != -1) { - ch[i] = '#'; - } - } - - return String.valueOf(ch); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_97.java b/src/main/java/com/fishercoder/solutions/_97.java deleted file mode 100644 index 8e545f7c13..0000000000 --- a/src/main/java/com/fishercoder/solutions/_97.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -public class _97 { - public static class Solution1 { - public boolean isInterleave(String s1, String s2, String s3) { - int m = s1.length(); - int n = s2.length(); - if (m + n != s3.length()) { - return false; - } - - boolean[][] dp = new boolean[m + 1][n + 1]; - - dp[0][0] = true; - - for (int i = 0; i < m; i++) { - if (s1.charAt(i) == s3.charAt(i)) { - dp[i + 1][0] = true; - } else { - //if one char fails, that means it breaks, the rest of the chars won't matter any more. - //Mian and I found one missing test case on Lintcode: ["b", "aabccc", "aabbbcb"] - //if we don't break, here, Lintcode could still accept this code, but Leetcode fails it. - break; - } - } - - for (int j = 0; j < n; j++) { - if (s2.charAt(j) == s3.charAt(j)) { - dp[0][j + 1] = true; - } else { - break; - } - } - - for (int i = 1; i <= m; i++) { - for (int j = 1; j <= n; j++) { - int k = i + j - 1; - dp[i][j] = (s1.charAt(i - 1) == s3.charAt(k) && dp[i - 1][j]) - || (s2.charAt(j - 1) == s3.charAt(k) && dp[i][j - 1]); - } - } - - return dp[m][n]; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_970.java b/src/main/java/com/fishercoder/solutions/_970.java deleted file mode 100644 index 982607335f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_970.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * 970. Powerful Integers - * - * Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. - * - * Return a list of all powerful integers that have value less than or equal to bound. - * - * You may return the answer in any order. In your answer, each value should occur at most once. - * - * Example 1: - * - * Input: x = 2, y = 3, bound = 10 - * Output: [2,3,4,5,7,9,10] - * Explanation: - * 2 = 2^0 + 3^0 - * 3 = 2^1 + 3^0 - * 4 = 2^0 + 3^1 - * 5 = 2^1 + 3^1 - * 7 = 2^2 + 3^1 - * 9 = 2^3 + 3^0 - * 10 = 2^0 + 3^2 - * - * Example 2: - * - * Input: x = 3, y = 5, bound = 15 - * Output: [2,4,6,8,10,14] - * - * - * Note: - * 1 <= x <= 100 - * 1 <= y <= 100 - * 0 <= bound <= 10^6 - */ -public class _970 { - public static class Solution1 { - /**This approach results in Time Limit Exceeded since it's apparently doing - * redundant checks.*/ - public List powerfulIntegers(int x, int y, int bound) { - Set result = new HashSet<>(); - int small = x; - int big = y; - if (x > y) { - small = y; - big = x; - } - int maxPower = bound / small; - for (int i = 0; i <= maxPower + 1; i++) { - for (int j = 0; j <= maxPower + 1; j++) { - int sum = (int) (Math.pow(small, i) + Math.pow(big, j)); - if (sum <= bound) { - result.add(sum); - } - } - } - List list = new ArrayList<>(result); - Collections.sort(list); - return list; - } - } - - public static class Solution2 { - /** credit: https://leetcode.com/problems/powerful-integers/discuss/214212/JavaC%2B%2BPython-Brute-Force */ - public List powerfulIntegers(int x, int y, int bound) { - Set result = new HashSet<>(); - for (int i = 1; i < bound; i *= x > 1 ? x : bound + 1) { - for (int j = 1; i + j <= bound; j *= y > 1 ? y : bound + 1) { - result.add(i + j); - } - } - return new ArrayList<>(result); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_973.java b/src/main/java/com/fishercoder/solutions/_973.java deleted file mode 100644 index 1781322858..0000000000 --- a/src/main/java/com/fishercoder/solutions/_973.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.PriorityQueue; - -/** - * 973. K Closest Points to Origin - * - * We have a list of points on the plane. Find the K closest points to the origin (0, 0). - * - * (Here, the distance between two points on a plane is the Euclidean distance.) - * - * You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.) - * - * - * - * Example 1: - * - * Input: points = [[1,3],[-2,2]], K = 1 - * Output: [[-2,2]] - * Explanation: - * The distance between (1, 3) and the origin is sqrt(10). - * The distance between (-2, 2) and the origin is sqrt(8). - * Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. - * We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]]. - * Example 2: - * - * Input: points = [[3,3],[5,-1],[-2,4]], K = 2 - * Output: [[3,3],[-2,4]] - * (The answer [[-2,4],[3,3]] would also be accepted.) - * - * - * Note: - * - * 1 <= K <= points.length <= 10000 - * -10000 < points[i][0] < 10000 - * -10000 < points[i][1] < 10000 - * */ -public class _973 { - - public static class Solution1 { - public int[][] kClosest(int[][] points, int K) { - int[][] ans = new int[K][2]; - - PriorityQueue pq = new PriorityQueue<>((o1, o2) -> { - double dist1 = getDistance(o1); - double dist2 = getDistance(o2); - - if (dist1 > dist2) { - return 1; - } else if (dist1 < dist2) { - return -1; - } else { - return 0; - } - }); - - for (int[] point : points) { - pq.add(point); - } - - for (int i = 0; i < K; i++) { - ans[i] = pq.poll(); - } - - return ans; - } - - private double getDistance(int[] point) { - return Math.sqrt(Math.pow(point[0], 2) + Math.pow(point[1], 2)); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_976.java b/src/main/java/com/fishercoder/solutions/_976.java deleted file mode 100644 index 5a2ef78295..0000000000 --- a/src/main/java/com/fishercoder/solutions/_976.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 976. Largest Perimeter Triangle - * - * Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 - * of these lengths. - * - * If it is impossible to form any triangle of non-zero area, return 0. - * */ - -public class _976 { - - public static class Solution1 { - public int largestPerimeter(int[] A) { - Arrays.sort(A); - int n = A.length; - - for (int i = n - 1; i > 1; i--) { - if (A[i] < A[i - 1] + A[i - 2]) { - return A[i] + A[i - 1] + A[i - 2]; - } - } - - return 0; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_977.java b/src/main/java/com/fishercoder/solutions/_977.java deleted file mode 100644 index ad34aee23f..0000000000 --- a/src/main/java/com/fishercoder/solutions/_977.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.Arrays; - -/** - * 977. Squares of a Sorted Array - * - * Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. - * - * Example 1: - * Input: [-4,-1,0,3,10] - * Output: [0,1,9,16,100] - * - * Example 2: - * Input: [-7,-3,2,3,11] - * Output: [4,9,9,49,121] - * - * - * Note: - * 1 <= A.length <= 10000 - * -10000 <= A[i] <= 10000 - * A is sorted in non-decreasing order. - */ -public class _977 { - public static class Solution1 { - public int[] sortedSquares(int[] A) { - int[] result = new int[A.length]; - for (int i = 0; i < A.length; i++) { - result[i] = (int) Math.pow(A[i], 2); - } - Arrays.sort(result); - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_979.java b/src/main/java/com/fishercoder/solutions/_979.java deleted file mode 100644 index f0f76659e8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_979.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -/** - * 979. Distribute Coins in Binary Tree - * - * Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. - * In one move, we may choose two adjacent nodes and move one coin from one node to another. (The move may be from parent to child, or from child to parent.) - * Return the number of moves required to make every node have exactly one coin. - * - * Example 1: - * - * 3 - * / \ - * 0 0 - * - * Input: [3,0,0] - * Output: 2 - * Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child. - * - * Example 2: - * - * 0 - * / \ - * 3 0 - * - * Input: [0,3,0] - * Output: 3 - * Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child. - * - * Example 3: - * - * 0 - * / \ - * 1 2 - * - * Input: [1,0,2] - * Output: 2 - * - * Example 4: - * - * 1 - * / \ - * 0 0 - * \ - * 3 - * - * Input: [1,0,0,null,3] - * Output: 4 - * - * - * Note: - * 1<= N <= 100 - * 0 <= node.val <= N - * */ -public class _979 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/distribute-coins-in-binary-tree/discuss/221930/JavaC%2B%2BPython-Recursive-Solution*/ - int moves = 0; - - public int distributeCoins(TreeNode root) { - dfs(root); - return moves; - } - - int dfs(TreeNode root) { - if (root == null) { - return 0; - } - int left = dfs(root.left); - int right = dfs(root.right); - moves += Math.abs(left) + Math.abs(right); - return root.val + left + right - 1; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_980.java b/src/main/java/com/fishercoder/solutions/_980.java deleted file mode 100644 index d3dd6d55a5..0000000000 --- a/src/main/java/com/fishercoder/solutions/_980.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 980. Unique Paths III - * - * On a 2-dimensional grid, there are 4 types of squares: - * 1 represents the starting square. There is exactly one starting square. - * 2 represents the ending square. There is exactly one ending square. - * 0 represents empty squares we can walk over. - * -1 represents obstacles that we cannot walk over. - * Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once. - * - * Example 1: - * Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]] - * Output: 2 - * Explanation: We have the following two paths: - * 1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2) - * 2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2) - * - * Example 2: - * Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]] - * Output: 4 - * Explanation: We have the following four paths: - * 1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3) - * 2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3) - * 3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3) - * 4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3) - * - * Example 3: - * Input: [[0,1],[2,0]] - * Output: 0 - * Explanation: - * There is no path that walks over every empty square exactly once. - * Note that the starting and ending square can be anywhere in the grid. - * - * Note: - * 1 <= grid.length * grid[0].length <= 20 - * */ -public class _980 { - public static class Solution1 { - - int[] directions = new int[]{0, 1, 0, -1, 0}; - int paths = 0; - - public int uniquePathsIII(int[][] grid) { - int[] start = findStart(grid); - int m = grid.length; - int n = grid[0].length; - boolean[][] visited = new boolean[m][n]; - visited[start[0]][start[1]] = true; - return backtracking(grid, m, n, visited, start); - } - - private int backtracking(int[][] grid, int m, int n, boolean[][] visited, int[] start) { - for (int i = 0; i < directions.length - 1; i++) { - int nextX = directions[i] + start[0]; - int nextY = directions[i + 1] + start[1]; - if (nextX >= 0 && nextX < m && nextY >= 0 && nextY < n && grid[nextX][nextY] != -1 && !visited[nextX][nextY]) { - if (grid[nextX][nextY] == 2) { - if (allZeroesVisited(visited, grid)) { - paths++; - return paths; - } else { - continue; - } - } - visited[nextX][nextY] = true; - backtracking(grid, m, n, visited, new int[]{nextX, nextY}); - visited[nextX][nextY] = false; - } - } - return paths; - } - - private boolean allZeroesVisited(boolean[][] visited, int[][] grid) { - for (int i = 0; i < grid.length; i++) { - for (int j = 0; j < grid[0].length; j++) { - if (grid[i][j] == 0 && !visited[i][j]) { - return false; - } - } - } - return true; - } - - private int[] findStart(int[][] grid) { - for (int i = 0; i < grid.length; i++) { - for (int j = 0; j < grid[0].length; j++) { - if (grid[i][j] == 1) { - return new int[]{i, j}; - } - } - } - return null; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_985.java b/src/main/java/com/fishercoder/solutions/_985.java deleted file mode 100644 index 48515eee4b..0000000000 --- a/src/main/java/com/fishercoder/solutions/_985.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 985. Sum of Even Numbers After Queries - * - * We have an array A of integers, and an array queries of queries. - * For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A. - * (Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.) - * Return the answer to all queries. Your answer array should have answer[i] as the answer to the i-th query. - * - * Example 1: - * - * Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]] - * Output: [8,6,2,4] - * Explanation: - * At the beginning, the array is [1,2,3,4]. - * After adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8. - * After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6. - * After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2. - * After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4. - * - * Note: - * - * 1 <= A.length <= 10000 - * -10000 <= A[i] <= 10000 - * 1 <= queries.length <= 10000 - * -10000 <= queries[i][0] <= 10000 - * 0 <= queries[i][1] < A.length - */ -public class _985 { - public static class Solution1 { - public int[] sumEvenAfterQueries(int[] A, int[][] queries) { - int[] result = new int[A.length]; - for (int i = 0; i < A.length; i++) { - int col = queries[i][1]; - A[col] = A[col] + queries[i][0]; - result[i] = computeEvenSum(A); - } - return result; - } - - private int computeEvenSum(int[] A) { - int sum = 0; - for (int num : A) { - if (num % 2 == 0) { - sum += num; - } - } - return sum; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_986.java b/src/main/java/com/fishercoder/solutions/_986.java deleted file mode 100644 index 25e8256f18..0000000000 --- a/src/main/java/com/fishercoder/solutions/_986.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.List; - -/** - * 986. Interval List Intersections - * - * Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order. - * Return the intersection of these two interval lists. - * - * (Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. - * The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. - * For example, the intersection of [1, 3] and [2, 4] is [2, 3].) - * - * Example 1: - * Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]] - * Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]] - * Reminder: The inputs and the desired output are lists of Interval objects, and not arrays or lists. - * - * Note: - * 0 <= A.length < 1000 - * 0 <= B.length < 1000 - * 0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9 - * NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature. - * */ -public class _986 { - public static class Solution1 { - public int[][] intervalIntersection(int[][] A, int[][] B) { - int i = 0; - int j = 0; - List list = new ArrayList<>(); - while (i < A.length && j < B.length) { - int start = Math.max(A[i][0], B[j][0]); - int end = Math.min(A[i][1], B[j][1]); - if (start <= end) { - list.add(new int[]{start, end}); - } - if (end == A[i][1]) { - i++; - } else { - j++; - } - } - int[][] result = new int[list.size()][2]; - for (int k = 0; k < list.size(); k++) { - result[k] = list.get(k); - } - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_987.java b/src/main/java/com/fishercoder/solutions/_987.java deleted file mode 100644 index 80be8f69e6..0000000000 --- a/src/main/java/com/fishercoder/solutions/_987.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.List; -import java.util.ArrayList; -import java.util.PriorityQueue; -import java.util.TreeMap; - -/** - * 987. Vertical Order Traversal of a Binary Tree - * - * Given a binary tree, return the vertical order traversal of its nodes values. - * For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1). - * Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates). - * If two nodes have the same position, then the value of the node that is reported first is the value that is smaller. - * Return an list of non-empty reports in order of X coordinate. Every report will have a list of values of nodes. - * - * Example 1: - * - * 3 - * / \ - * 9 20 - * / \ - * 15 7 - * - * Input: [3,9,20,null,null,15,7] - * Output: [[9],[3,15],[20],[7]] - * Explanation: - * Without loss of generality, we can assume the root node is at position (0, 0): - * Then, the node with value 9 occurs at position (-1, -1); - * The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2); - * The node with value 20 occurs at position (1, -1); - * The node with value 7 occurs at position (2, -2). - * - * - * Example 2: - * - * 1 - * / \ - * 2 3 - * / \ / \ - * 4 5 6 7 - * - * Input: [1,2,3,4,5,6,7] - * Output: [[4],[2],[1,5,6],[3],[7]] - * Explanation: - * The node with value 5 and the node with value 6 have the same position according to the given scheme. - * However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6. - * - * Note: - * - * The tree will have between 1 and 1000 nodes. - * Each node's value will be between 0 and 1000. - * */ -public class _987 { - public static class Solution1 { - /**credit: https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/discuss/231148/Java-TreeMap-Solution*/ - public List> verticalTraversal(TreeNode root) { - TreeMap>> map = new TreeMap<>(); - dfs(root, 0, 0, map); - List> list = new ArrayList<>(); - for (TreeMap> ys : map.values()) { - list.add(new ArrayList<>()); - for (PriorityQueue nodes : ys.values()) { - while (!nodes.isEmpty()) { - list.get(list.size() - 1).add(nodes.poll()); - } - } - } - return list; - } - - private void dfs(TreeNode root, int x, int y, TreeMap>> map) { - if (root == null) { - return; - } - if (!map.containsKey(x)) { - map.put(x, new TreeMap<>()); - } - if (!map.get(x).containsKey(y)) { - map.get(x).put(y, new PriorityQueue<>()); - } - map.get(x).get(y).offer(root.val); - dfs(root.left, x - 1, y + 1, map); - dfs(root.right, x + 1, y + 1, map); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_988.java b/src/main/java/com/fishercoder/solutions/_988.java deleted file mode 100644 index 770aadd277..0000000000 --- a/src/main/java/com/fishercoder/solutions/_988.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 988. Smallest String Starting From Leaf - * - * Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to 'z': - * a value of 0 represents 'a', a value of 1 represents 'b', and so on. - * Find the lexicographically smallest string that starts at a leaf of this tree and ends at the root. - * (As a reminder, any shorter prefix of a string is lexicographically smaller: for example, "ab" is lexicographically smaller than "aba". - * A leaf of a node is a node that has no children.) - * - * Example 1: - * Input: [0,1,2,3,4,3,4] - * Output: "dba" - * - * Example 2: - * Input: [25,1,3,1,3,0,2] - * Output: "adz" - * - * Example 3: - * Input: [2,2,1,null,1,0,null,0] - * Output: "abc" - * - * Note: - * The number of nodes in the given tree will be between 1 and 8500. - * Each node in the tree will have a value between 0 and 25. - * */ -public class _988 { - public static class Solution1 { - public String smallestFromLeaf(TreeNode root) { - final List paths = new ArrayList<>(); - Map map = new HashMap<>(); - map.put(0, 'a'); - map.put(1, 'b'); - map.put(2, 'c'); - map.put(3, 'd'); - map.put(4, 'e'); - map.put(5, 'f'); - map.put(6, 'g'); - map.put(7, 'h'); - map.put(8, 'i'); - map.put(9, 'j'); - map.put(10, 'k'); - map.put(11, 'l'); - map.put(12, 'm'); - map.put(13, 'n'); - map.put(14, 'o'); - map.put(15, 'p'); - map.put(16, 'q'); - map.put(17, 'r'); - map.put(18, 's'); - map.put(19, 't'); - map.put(20, 'u'); - map.put(21, 'v'); - map.put(22, 'w'); - map.put(23, 'x'); - map.put(24, 'y'); - map.put(25, 'z'); - dfs(root, "", paths, map); - return findSmallest(paths); - } - - private String findSmallest(List paths) { - List reversed = new ArrayList<>(); - for (String path : paths) { - StringBuilder sb = new StringBuilder(); - sb.append(path); - reversed.add(sb.reverse().toString()); - } - Collections.sort(reversed); - return reversed.get(0); - } - - private void dfs(TreeNode root, String path, List paths, Map map) { - if (root == null) { - return; - } - path += map.get(root.val); - if (root.left == null && root.right == null) { - paths.add(path); - } - dfs(root.left, path, paths, map); - dfs(root.right, path, paths, map); - path = path.substring(0, path.length() - 1); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_989.java b/src/main/java/com/fishercoder/solutions/_989.java deleted file mode 100644 index dac940c049..0000000000 --- a/src/main/java/com/fishercoder/solutions/_989.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * 989. Add to Array-Form of Integer - * - * For a non-negative integer X, the array-form of X is an array of its digits in left to right order. For example, if X = 1231, then the array form is [1,2,3,1]. - * - * Given the array-form A of a non-negative integer X, return the array-form of the integer X+K. - * - * Example 1: - * - * Input: A = [1,2,0,0], K = 34 - * Output: [1,2,3,4] - * Explanation: 1200 + 34 = 1234 - * Example 2: - * - * Input: A = [2,7,4], K = 181 - * Output: [4,5,5] - * Explanation: 274 + 181 = 455 - * Example 3: - * - * Input: A = [2,1,5], K = 806 - * Output: [1,0,2,1] - * Explanation: 215 + 806 = 1021 - * Example 4: - * - * Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1 - * Output: [1,0,0,0,0,0,0,0,0,0,0] - * Explanation: 9999999999 + 1 = 10000000000 - * - * Note: - * - * 1 <= A.length <= 10000 - * 0 <= A[i] <= 9 - * 0 <= K <= 10000 - * If A.length > 1, then A[0] != 0 - */ -public class _989 { - public static class Solution1 { - public List addToArrayForm(int[] A, int K) { - List kDigitsReversed = new ArrayList<>(); - int divisor = 10; - while (K != 0) { - kDigitsReversed.add(K % divisor); - K /= 10; - } - List result = new ArrayList<>(); - int prevFlow = 0; - for (int i = A.length - 1, j = 0; i >= 0 || j < kDigitsReversed.size(); i --, j++) { - int sum; - if (i >= 0 && j < kDigitsReversed.size()) { - sum = A[i] + kDigitsReversed.get(j); - } else if (i >= 0) { - sum = A[i]; - } else { - sum = kDigitsReversed.get(j); - } - int flow = 0; - if (prevFlow != 0) { - sum += prevFlow; - } - if (sum > 9) { - flow = 1; - } - sum %= 10; - prevFlow = flow; - result.add(sum); - } - if (prevFlow != 0) { - result.add(prevFlow); - } - Collections.reverse(result); - return result; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_99.java b/src/main/java/com/fishercoder/solutions/_99.java deleted file mode 100644 index 4b9873578d..0000000000 --- a/src/main/java/com/fishercoder/solutions/_99.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -public class _99 { - public static class Solution1 { - TreeNode firstElement = null; - TreeNode secondElement = null; - - TreeNode prevElement = new TreeNode(Integer.MIN_VALUE); - - public void recoverTree(TreeNode root) { - traverseTree(root); - - //swap the two elements - int temp = firstElement.val; - firstElement.val = secondElement.val; - secondElement.val = temp; - } - - private void traverseTree(TreeNode root) { - if (root == null) { - return; - } - - traverseTree(root.left); - - //prevElement means the one previous to the current root, refer to in-order traversal, previous element must be smaller than the current root - //if it's bigger, then we find the first element, thus we store it in the variable called firstElement - if (firstElement == null && prevElement.val >= root.val) { - firstElement = prevElement; - } - - if (firstElement != null && prevElement.val >= root.val) { - secondElement = root; - } - - //this is the last step in the "do some business logic", so we'll always to have update the previous node to be the current root before it traverses the right subtree - //since the current root will be the new previous node for the right subtree. - prevElement = root; - - traverseTree(root.right); - } - - } -} diff --git a/src/main/java/com/fishercoder/solutions/_993.java b/src/main/java/com/fishercoder/solutions/_993.java deleted file mode 100644 index 87e8591019..0000000000 --- a/src/main/java/com/fishercoder/solutions/_993.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Queue; -import java.util.Set; - -public class _993 { - public static class Solution1 { - public boolean isCousins(TreeNode root, int x, int y) { - Queue queue = new LinkedList<>(); - queue.offer(root); - while (!queue.isEmpty()) { - int size = queue.size(); - for (int i = 0; i < size; i++) { - TreeNode current = queue.poll(); - if (current.left != null) { - queue.offer(current.left); - } - if (current.right != null) { - queue.offer(current.right); - } - if (current.left != null && current.right != null) { - if (current.left.val == x && current.right.val == y - || current.left.val == y && current.right.val == x) { - return false; - } - } - } - if (checkQueue(queue, x, y)) { - return true; - } - } - return false; - } - - private boolean checkQueue(Queue queue, int x, int y) { - Set set = new HashSet<>(); - Queue tmp = new LinkedList<>(queue); - while (!tmp.isEmpty()) { - set.add(tmp.poll().val); - } - return set.contains(x) && set.contains(y); - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_994.java b/src/main/java/com/fishercoder/solutions/_994.java deleted file mode 100644 index ac4d954dba..0000000000 --- a/src/main/java/com/fishercoder/solutions/_994.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * 994. Rotting Oranges - * - * In a given grid, each cell can have one of three values: - * - * the value 0 representing an empty cell; - * the value 1 representing a fresh orange; - * the value 2 representing a rotten orange. - * Every minute, any fresh orange that is adjacent (4-directionally) to a - * rotten orange becomes rotten. - * Return the minimum number of minutes that must elapse until no cell has a fresh orange. - * If this is impossible, return -1 instead. - * - * Example 1: - * Input: [[2,1,1],[1,1,0],[0,1,1]] - * Output: 4 - * - * Example 2: - * Input: [[2,1,1],[0,1,1],[1,0,1]] - * Output: -1 - * Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally. - * - * Example 3: - * Input: [[0,2]] - * Output: 0 - * Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0. - * - * Note: - * 1 <= grid.length <= 10 - * 1 <= grid[0].length <= 10 - * grid[i][j] is only 0, 1, or 2. - */ -public class _994 { - public static class Solution1 { - int[] directions = new int[]{0, 1, 0, -1, 0}; - - public int orangesRotting(int[][] grid) { - Queue rottens = new LinkedList<>(); - for (int i = 0; i < grid.length; i++) { - for (int j = 0; j < grid[0].length; j++) { - if (grid[i][j] == 2) { - rottens.add(new int[]{i, j}); - } - } - } - int times = 0; - while (!rottens.isEmpty()) { - int size = rottens.size(); - boolean counted = false; - for (int k = 0; k < size; k++) { - int[] rotten = rottens.poll(); - for (int i = 0; i < 4; i++) { - int x = rotten[0] + directions[i]; - int y = rotten[1] + directions[i + 1]; - if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length && grid[x][y] == 1) { - grid[x][y] = 2; - if (!counted) { - times++; - } - counted = true; - rottens.add(new int[]{x, y}); - } - } - } - } - for (int i = 0; i < grid.length; i++) { - for (int j = 0; j < grid[0].length; j++) { - if (grid[i][j] == 1) { - return -1; - } - } - } - return times; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_997.java b/src/main/java/com/fishercoder/solutions/_997.java deleted file mode 100644 index 4a2c6ede45..0000000000 --- a/src/main/java/com/fishercoder/solutions/_997.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.HashSet; -import java.util.Set; - -public class _997 { - public static class Solution1 { - public int findJudge(int N, int[][] trust) { - int[] trustPoints = new int[N]; - Set trustOthers = new HashSet<>(); - for (int[] eachTrust : trust) { - trustPoints[eachTrust[1] - 1]++; - trustOthers.add(eachTrust[0]); - } - int judge = -1; - for (int i = 0; i < trustPoints.length; i++) { - if (trustPoints[i] == N - 1 && !trustOthers.contains(i + 1)) { - judge = i + 1; - } - } - return judge; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/_999.java b/src/main/java/com/fishercoder/solutions/_999.java deleted file mode 100644 index 53ecb3cef8..0000000000 --- a/src/main/java/com/fishercoder/solutions/_999.java +++ /dev/null @@ -1,151 +0,0 @@ -package com.fishercoder.solutions; - -/** - * 999. Available Captures for Rook - * - * On an 8 x 8 chessboard, there is one white rook. - * There also may be empty squares, white bishops, and black pawns. - * These are given as characters 'R', '.', 'B', and 'p' respectively. - * Uppercase characters represent white pieces, and lowercase characters represent black pieces. - * - * The rook moves as in the rules of Chess: - * it chooses one of four cardinal directions (north, east, west, and south), - * then moves in that direction until it chooses to stop, reaches the edge of the board, - * or captures an opposite colored pawn by moving to the same square it occupies. - * Also, rooks cannot move into the same square as other friendly bishops. - * - * Return the number of pawns the rook can capture in one move. - * - * Example 1: - * - * Input:[ - * 8 [".",".",".",".",".",".",".","."], - * 7 [".",".",".","p",".",".",".","."], - * 6 [".",".",".","R",".",".",".","p"], - * 5 [".",".",".",".",".",".",".","."], - * 4 [".",".",".",".",".",".",".","."], - * 3 [".",".",".","p",".",".",".","."], - * 2 [".",".",".",".",".",".",".","."], - * 1 [".",".",".",".",".",".",".","."]] - * a b c d e f g h - * - * Output: 3 - * Explanation: - * In this example the rook is able to capture all the pawns. - * - * Example 2: - * - * Input:[ - * 8 [".",".",".",".",".",".",".","."], - * 7 [".","p","p","p","p","p",".","."], - * 6 [".","p","p","B","p","p",".","."], - * 5 [".","p","B","R","B","p",".","."], - * 4 [".","p","p","B","p","p",".","."], - * 3 [".","p","p","p","p","p",".","."], - * 2 [".",".",".",".",".",".",".","."], - * 1 [".",".",".",".",".",".",".","."]] - * a b c d e f g h - * - * Output: 0 - * Explanation: - * Bishops are blocking the rook to capture any pawn. - * - * Example 3: - * - * Input: [ - * 8 [".",".",".",".",".",".",".","."], - * 7 [".",".",".","p",".",".",".","."], - * 6 [".",".",".","p",".",".",".","."], - * 5 ["p","p",".","R",".","p","B","."], - * 4 [".",".",".",".",".",".",".","."], - * 3 [".",".",".","B",".",".",".","."], - * 2 [".",".",".","p",".",".",".","."], - * 1 [".",".",".",".",".",".",".","."]] - * a b c d e f g h - * - * Output: 3 - * Explanation: - * The rook can capture the pawns at positions b5, d6 and f5. - * - * Note: - * board.length == board[i].length == 8 - * board[i][j] is either 'R', '.', 'B', or 'p' - * There is exactly one cell with board[i][j] == 'R' - */ -public class _999 { - public static class Solution1 { - int[] directions = new int[] {0, 1, 0, -1, 0}; - - public int numRookCaptures(char[][] board) { - int m = board.length; - int n = board[0].length; - int rowR = -1; - int colR = -1; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (board[i][j] == 'R') { - rowR = i; - colR = j; - break; - } - } - } - int count = 0; - for (int i = 0; i < 4; i++) { - int neighborRow = rowR + directions[i]; - int neighborCol = colR + directions[i + 1]; - if (neighborRow >= 0 && neighborRow < m - && neighborCol >= 0 && neighborCol < n - && board[neighborRow][neighborCol] != 'B') { - if (directions[i] == 0 && directions[i + 1] == 1) { - while (neighborCol < n) { - if (board[neighborRow][neighborCol] == 'p') { - count++; - break; - } else if (board[neighborRow][neighborCol] == 'B') { - break; - } else { - neighborCol++; - } - } - } else if (directions[i] == 1 && directions[i + 1] == 0) { - while (neighborRow < m) { - if (board[neighborRow][neighborCol] == 'p') { - count++; - break; - } else if (board[neighborRow][neighborCol] == 'B') { - break; - } else { - neighborRow++; - } - } - } else if (directions[i] == 0 && directions[i + 1] == -1) { - while (neighborCol >= 0) { - if (board[neighborRow][neighborCol] == 'p') { - count++; - break; - } else if (board[neighborRow][neighborCol] == 'B') { - break; - } else { - neighborCol--; - } - } - } else { - while (neighborRow >= 0) { - if (board[neighborRow][neighborCol] == 'p') { - count++; - break; - } else if (board[neighborRow][neighborCol] == 'B') { - break; - } else { - neighborRow--; - } - } - } - } - } - - return count; - } - } -} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_1.java b/src/main/java/com/fishercoder/solutions/firstthousand/_1.java new file mode 100644 index 0000000000..2d25899181 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_1.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1 { + + public static class Solution1 { + public int[] twoSum(int[] nums, int target) { + Map map = new HashMap(); + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(target - nums[i])) { + return new int[] {map.get(target - nums[i]), i}; + } else { + map.put(nums[i], i); + } + } + return new int[] {-1, -1}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_10.java b/src/main/java/com/fishercoder/solutions/firstthousand/_10.java similarity index 84% rename from src/main/java/com/fishercoder/solutions/_10.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_10.java index acb00a2f81..fe293fd0fa 100644 --- a/src/main/java/com/fishercoder/solutions/_10.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_10.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _10 { @@ -9,9 +9,9 @@ public boolean isMatch(String s, String p) { } boolean[][] dp = new boolean[s.length() + 1][p.length() + 1]; dp[0][0] = true; - for (int i = 0; i < p.length(); i++) { //here's the p's length, not s's + for (int i = 0; i < p.length(); i++) { // here's the p's length, not s's if (p.charAt(i) == '*' && dp[0][i - 1]) { - dp[0][i + 1] = true; //here's y axis should be i+1 + dp[0][i + 1] = true; // here's y axis should be i+1 } } for (int i = 0; i < s.length(); i++) { @@ -31,5 +31,4 @@ public boolean isMatch(String s, String p) { return dp[s.length()][p.length()]; } } - } diff --git a/src/main/java/com/fishercoder/solutions/_100.java b/src/main/java/com/fishercoder/solutions/firstthousand/_100.java similarity index 88% rename from src/main/java/com/fishercoder/solutions/_100.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_100.java index 4825f00e1d..457b4168f7 100644 --- a/src/main/java/com/fishercoder/solutions/_100.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_100.java @@ -1,14 +1,14 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -public class _100 { - public static class Solution1 { - public boolean isSameTree(TreeNode p, TreeNode q) { - if (p == null || q == null) { - return p == q; - } - return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); - } - } -} +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _100 { + public static class Solution1 { + public boolean isSameTree(TreeNode p, TreeNode q) { + if (p == null || q == null) { + return p == q; + } + return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_101.java b/src/main/java/com/fishercoder/solutions/firstthousand/_101.java new file mode 100644 index 0000000000..1b27500f0d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_101.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _101 { + public static class Solution1 { + public boolean isSymmetric(TreeNode root) { + if (root == null) { + return true; + } + return isSymmetric(root.left, root.right); + } + + private boolean isSymmetric(TreeNode left, TreeNode right) { + if (left == null || right == null) { + return left == right; + } + return left.val == right.val + && isSymmetric(left.left, right.right) + && isSymmetric(left.right, right.left); + } + } + + public static class Solution2 { + /* + * The same as the above solution, just a bit more verbose. + */ + public boolean isSymmetric(TreeNode root) { + if (root == null) { + return true; + } + return isSymmetric(root.left, root.right); + } + + private boolean isSymmetric(TreeNode left, TreeNode right) { + if (left == null && right == null) { + return true; + } else if (left == null || right == null) { + return false; + } + if (left.val == right.val) { + return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left); + } else { + return false; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_102.java b/src/main/java/com/fishercoder/solutions/firstthousand/_102.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_102.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_102.java index 074de639cf..f9d29eef2e 100644 --- a/src/main/java/com/fishercoder/solutions/_102.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_102.java @@ -1,38 +1,37 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -public class _102 { - - public static class Solution1 { - public List> levelOrder(TreeNode root) { - List> result = new ArrayList<>(); - if (root == null) { - return result; - } - Queue queue = new LinkedList(); - queue.offer(root); - while (!queue.isEmpty()) { - List thisLevel = new ArrayList(); - int size = queue.size(); - for (int i = 0; i < size; i++) { - TreeNode curr = queue.poll(); - thisLevel.add(curr.val); - if (curr.left != null) { - queue.offer(curr.left); - } - if (curr.right != null) { - queue.offer(curr.right); - } - } - result.add(thisLevel); - } - return result; - } - } -} +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _102 { + + public static class Solution1 { + public List> levelOrder(TreeNode root) { + List> result = new ArrayList<>(); + if (root == null) { + return result; + } + Queue queue = new LinkedList(); + queue.offer(root); + while (!queue.isEmpty()) { + List thisLevel = new ArrayList(); + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + thisLevel.add(curr.val); + if (curr.left != null) { + queue.offer(curr.left); + } + if (curr.right != null) { + queue.offer(curr.right); + } + } + result.add(thisLevel); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_103.java b/src/main/java/com/fishercoder/solutions/firstthousand/_103.java new file mode 100644 index 0000000000..e3601e4d22 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_103.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _103 { + public static class Solution1 { + public List> zigzagLevelOrder(TreeNode root) { + Queue queue = new LinkedList(); + List> levels = new ArrayList(); + if (root == null) { + return levels; + } + queue.offer(root); + boolean forward = true; + while (!queue.isEmpty()) { + int size = queue.size(); + List level = new ArrayList(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + level.add(curr.val); + if (curr.left != null) { + queue.offer(curr.left); + } + if (curr.right != null) { + queue.offer(curr.right); + } + } + if (!forward) { + Collections.reverse(level); + } + forward = !forward; + levels.add(level); + } + return levels; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_104.java b/src/main/java/com/fishercoder/solutions/firstthousand/_104.java new file mode 100644 index 0000000000..c3e3d5db6b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_104.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; + +public class _104 { + + public static class Solution1 { + /* + * Recursive solution: + * Time: O(n) + * Space: O(n) + */ + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } + } + + public static class Solution2 { + /* + * A more verbose recursive solution for easier understanding. + */ + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + int left = maxDepth(root.left); + int right = maxDepth(root.right); + return 1 + Math.max(left, right); + } + } + + public static class Solution3 { + /* + * Iterative solution: + * Time: O(n) + * Space: O(n) + */ + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + LinkedList stack = new LinkedList<>(); + LinkedList depths = new LinkedList<>(); + stack.add(root); + depths.add(1); + + int depth = 0; + while (!stack.isEmpty()) { + TreeNode currentNode = stack.pollLast(); + int currentDepth = depths.pollLast(); + if (currentNode != null) { + depth = Math.max(depth, currentDepth); + stack.add(currentNode.right); + depths.add(currentDepth + 1); + stack.add(currentNode.left); + depths.add(currentDepth + 1); + } + } + return depth; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_105.java b/src/main/java/com/fishercoder/solutions/firstthousand/_105.java new file mode 100644 index 0000000000..55ab07bedf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_105.java @@ -0,0 +1,71 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.Map; + +public class _105 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/29838/5ms-java-clean-solution-with-caching use + * HashMap as the cache so that accessing inorder index becomes O(1) time Note: The first + * element of preorder array is the root! + * + * Using a pen and paper to visualize this helps a great deal! + * preorder array has the root at the head, then we could use this number as a pivot in the inorder array: all elements on the left of this pivot should form a left subtree of this pivot + * and anything on the right side of this pivot in the inorder array should form a right subtree of this pivot. + */ + public TreeNode buildTree(int[] preorder, int[] inorder) { + Map inorderMap = new HashMap(); + for (int i = 0; i < inorder.length; i++) { + inorderMap.put(inorder[i], i); + } + + /*At the beginning, both start from 0 to nums.length-1*/ + return buildTree(preorder, 0, preorder.length - 1, inorderMap, 0, inorder.length - 1); + } + + private TreeNode buildTree( + int[] preorder, + int preStart, + int preEnd, + Map inorderMap, + int inStart, + int inEnd) { + if (preStart > preEnd || inStart > inEnd) { + return null; + } + + TreeNode root = new TreeNode(preorder[preStart]); + int inRoot = inorderMap.get(preorder[preStart]); + // This line is the key to figure out how many nodes should be on the left subtree + int numsLeft = inRoot - inStart; + + /*It's easy to understand and remember: + * for the indices of inorder array: + * root.left should be inStart and inRoot-1 as new start and end indices + * root.right should be inRoot+1 and inEnd as new start and end indices + * + * since inRoot is being used already in this recursion call, that's why we use inRoot-1 and inRoot+1 + * this part is the same for both Leetcode 105 and Leetcode 106.*/ + root.left = + buildTree( + preorder, + preStart + 1, + preStart + numsLeft, + inorderMap, + inStart, + inRoot - 1); + root.right = + buildTree( + preorder, + preStart + numsLeft + 1, + preEnd, + inorderMap, + inRoot + 1, + inEnd); + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_106.java b/src/main/java/com/fishercoder/solutions/firstthousand/_106.java new file mode 100644 index 0000000000..9df004e368 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_106.java @@ -0,0 +1,128 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.Map; + +public class _106 { + public static class Solution1 { + + /* + * https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space + * Note: the last element of postorder array is the root! + * The idea is to take the last element in postorder as the root; find the position of the root + * in the inorder array; then locate the range for left sub-tree and right sub-tree and do + * recursion, use a hashmap to record the index of root in the inorder array. + */ + public TreeNode buildTree(int[] inorder, int[] postorder) { + if (inorder == null || postorder == null || inorder.length != postorder.length) { + return null; + } + HashMap inorderMap = new HashMap<>(); + for (int i = 0; i < inorder.length; i++) { + inorderMap.put(inorder[i], i); + } + /*At the beginning, both start from 0 to nums.length-1*/ + return buildTreeRecursively( + inorderMap, 0, inorder.length - 1, postorder, 0, postorder.length - 1); + } + + private TreeNode buildTreeRecursively( + Map inorderMap, + int inorderStart, + int inorderEnd, + int[] postorder, + int postorderStart, + int postorderEnd) { + if (postorderStart > postorderEnd || inorderStart > inorderEnd) { + return null; + } + TreeNode root = new TreeNode(postorder[postorderEnd]); + int inRoot = inorderMap.get(postorder[postorderEnd]); + int numsLeft = inRoot - inorderStart; + + /*It's easy to understand and remember: + * for the indices of inorder array: + * inStart and inRoot-1 as new start and end indices + * inRoot+1 and inEnd as new start and end indices + * + * this is easy to understand and remember: since inRoot is already been used in this recursion call, so we're going to use inRoot-1 and inRoot+1 for next recursion call + * + * for the indices of postorder array: + * postorderStart and postorderStart+numsLeft-1 should be the new start and end indices + * postorderStart+numsLeft and postorderEnd-1 should be the new start and end indices + * + * this is also easy to understand and remember: + * since the last one in postorder is the root and we have used it in this recursion call already, so the end is definitely postorderEnd-1; + * then the postorderEnd for root.left is contiguous to the postorderStart of root.right, :)*/ + root.left = + buildTreeRecursively( + inorderMap, + inorderStart, + inRoot - 1, + postorder, + postorderStart, + postorderStart + numsLeft - 1); + root.right = + buildTreeRecursively( + inorderMap, + inRoot + 1, + inorderEnd, + postorder, + postorderStart + numsLeft, + postorderEnd - 1); + return root; + } + } + + public static class Solution2 { + /* + * My own solution after inspiration from LeetCode 105. + * I go from the right to the left for the postorder array, also, I build the tree by forming the right subtree first and then the left subtree. + * A bit different from using numsLeft from LeetCode 106, I use numsRight, meaning the number of nodes needed to form the right subtree in the inorder array. + */ + public TreeNode buildTree(int[] inorder, int[] postorder) { + Map inMap = new HashMap<>(); + for (int i = 0; i < inorder.length; i++) { + inMap.put(inorder[i], i); + } + return helper( + postorder, inorder, inMap, postorder.length - 1, 0, 0, inorder.length - 1); + } + + private TreeNode helper( + int[] postorder, + int[] inorder, + Map inMap, + int postStart, + int postEnd, + int inStart, + int inEnd) { + if (postStart < postEnd) { + return null; + } + int inRoot = inMap.get(postorder[postStart]); + int numsRight = inEnd - inRoot; + TreeNode node = new TreeNode(postorder[postStart]); + node.right = + helper( + postorder, + inorder, + inMap, + postStart - 1, + postStart - numsRight, + inRoot + 1, + inEnd); + node.left = + helper( + postorder, + inorder, + inMap, + postStart - numsRight - 1, + postEnd, + inStart, + inRoot - 1); + return node; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_107.java b/src/main/java/com/fishercoder/solutions/firstthousand/_107.java similarity index 88% rename from src/main/java/com/fishercoder/solutions/_107.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_107.java index 4444edf861..9b3df89fea 100644 --- a/src/main/java/com/fishercoder/solutions/_107.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_107.java @@ -1,39 +1,38 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.TreeNode; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -public class _107 { - public static class Solution1 { - public List> levelOrder(TreeNode root) { - List> result = new ArrayList(); - if (root == null) { - return result; - } - Queue q = new LinkedList(); - q.offer(root); - while (!q.isEmpty()) { - List thisLevel = new ArrayList<>(); - int qSize = q.size(); - for (int i = 0; i < qSize; i++) { - TreeNode curr = q.poll(); - thisLevel.add(curr.val); - if (curr.left != null) { - q.offer(curr.left); - } - if (curr.right != null) { - q.offer(curr.right); - } - } - result.add(thisLevel); - } - Collections.reverse(result); - return result; - } - } -} +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _107 { + public static class Solution1 { + public List> levelOrderBottom(TreeNode root) { + List> result = new ArrayList(); + if (root == null) { + return result; + } + Queue q = new LinkedList(); + q.offer(root); + while (!q.isEmpty()) { + List thisLevel = new ArrayList<>(); + int qSize = q.size(); + for (int i = 0; i < qSize; i++) { + TreeNode curr = q.poll(); + thisLevel.add(curr.val); + if (curr.left != null) { + q.offer(curr.left); + } + if (curr.right != null) { + q.offer(curr.right); + } + } + result.add(thisLevel); + } + Collections.reverse(result); + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_108.java b/src/main/java/com/fishercoder/solutions/firstthousand/_108.java new file mode 100644 index 0000000000..6d7134af3e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_108.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _108 { + + public static class Solution1 { + public TreeNode sortedArrayToBST(int[] num) { + return dfs(num, 0, num.length - 1); + } + + public TreeNode dfs(int[] num, int left, int right) { + if (left > right) { + return null; + } + int mid = left + (right - left) / 2; + TreeNode node = new TreeNode(num[mid]); + node.left = dfs(num, left, mid - 1); + node.right = dfs(num, mid + 1, right); + return node; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_109.java b/src/main/java/com/fishercoder/solutions/firstthousand/_109.java new file mode 100644 index 0000000000..6797ba78a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_109.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.classes.TreeNode; + +public class _109 { + public static class Solution1 { + public TreeNode sortedListToBST(ListNode head) { + return toBstRecursively(head, null); + } + + public TreeNode toBstRecursively(ListNode start, ListNode end) { + if (start == end) { + return null; + } else { + ListNode slow = start; + ListNode fast = start; + // here is the key: we check if fast != end, not fast != null + while (fast != end && fast.next != end) { + slow = slow.next; + fast = fast.next.next; + } + + TreeNode root = new TreeNode(slow.val); + root.left = toBstRecursively(start, slow); + root.right = toBstRecursively(slow.next, end); + return root; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_11.java b/src/main/java/com/fishercoder/solutions/firstthousand/_11.java new file mode 100644 index 0000000000..669d209382 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_11.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +public class _11 { + public static class Solution1 { + /* + * Time: O(n^2) + * This brute force solution is NOT accepted on LeetCode due to TLE. + */ + public int maxArea(int[] height) { + int maxArea = 0; + for (int left = 0; left < height.length - 1; left++) { + for (int right = height.length - 1; left < right; right--) { + int area = (right - left) * Math.min(height[left], height[right]); + maxArea = Math.max(maxArea, area); + } + } + return maxArea; + } + } + + public static class Solution2 { + /* + * Two pointer technique. + * Well explained here: https://leetcode.com/problems/container-with-most-water/discuss/6100/Simple-and-clear-proofexplanation + */ + public int maxArea(int[] height) { + int max = 0; + int left = 0; + int right = height.length - 1; + while (left < right) { + max = Math.max(Math.min(height[left], height[right]) * (right - left), max); + if (height[left] <= height[right]) { + /*if this height is shorter, then we'll need to move it to the right to find a higher one so that it's possible to find a larger area.*/ + left++; + } else { + right--; + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_110.java b/src/main/java/com/fishercoder/solutions/firstthousand/_110.java new file mode 100644 index 0000000000..3f0aef4316 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_110.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _110 { + + public static class Solution1 { + // recursively get the height of each subtree of each node, compare their difference, if + // greater than 1, then return false + // although this is working, but it's not efficient, since it repeatedly computes the + // heights of each node every time + // Its time complexity is O(n^2). + public boolean isBalanced(TreeNode root) { + if (root == null) { + return true; + } + if (Math.abs(getH(root.left) - getH(root.right)) > 1) { + return false; + } else { + return isBalanced(root.left) && isBalanced(root.right); + } + } + + private int getH(TreeNode root) { + if (root == null) { + return 0; // base case + } + int leftH = getH(root.left); + int rightH = getH(root.right); + return Math.max(leftH, rightH) + 1; + } + } + + public static class Solution2 { + + public boolean isBalanced(TreeNode root) { + return getH(root) != -1; + } + + private int getH(TreeNode root) { + if (root == null) { + return 0; + } + int leftH = getH(root.left); + if (leftH == -1) { + return -1; + } + int rightH = getH(root.right); + if (rightH == -1) { + return -1; + } + if (Math.abs(leftH - rightH) > 1) { + return -1; + } + return Math.max(leftH, rightH) + 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_111.java b/src/main/java/com/fishercoder/solutions/firstthousand/_111.java new file mode 100644 index 0000000000..2239535404 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_111.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; + +public class _111 { + public static class Solution1 { + /* + * DFS + */ + public int minDepth(TreeNode root) { + if (root == null) { + return 0; + } + int left = minDepth(root.left); + int right = minDepth(root.right); + if (left == 0) { + return right + 1; + } + if (right == 0) { + return left + 1; + } + return Math.min(left, right) + 1; + } + } + + public static class Solution2 { + /* + * BFS + */ + public int minDepth(TreeNode root) { + if (root == null) { + return 0; + } + Queue q = new LinkedList(); + q.offer(root); + int level = 0; + while (!q.isEmpty()) { + level++; + int size = q.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = q.poll(); + if (curr.left != null) { + q.offer(curr.left); + } + if (curr.right != null) { + q.offer(curr.right); + } + if (curr.left == null && curr.right == null) { + return level; + } + } + } + return level; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_112.java b/src/main/java/com/fishercoder/solutions/firstthousand/_112.java new file mode 100644 index 0000000000..d329d84099 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_112.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _112 { + public static class Solution1 { + public boolean hasPathSum(TreeNode root, int sum) { + if (root == null) { + return false; + } + if (root.val == sum && root.left == null && root.right == null) { + return true; + } + return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); + } + } + + public static class Solution2 { + /* + * My completely original solution on 9/23/2021. + */ + public boolean hasPathSum(TreeNode root, int targetSum) { + return dfs(root, 0, targetSum); + } + + private boolean dfs(TreeNode root, int sum, int targetSum) { + if (root == null) { + return false; + } + sum += root.val; + if (root.left == null && root.right == null) { + return sum == targetSum; + } + return dfs(root.left, sum, targetSum) || dfs(root.right, sum, targetSum); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_113.java b/src/main/java/com/fishercoder/solutions/firstthousand/_113.java new file mode 100644 index 0000000000..e063bf6be0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_113.java @@ -0,0 +1,69 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _113 { + + public static class Solution1 { + public List> pathSum(TreeNode root, int sum) { + List> allPaths = new ArrayList(); + if (root == null) { + return allPaths; + } + dfs(root, new ArrayList(), allPaths, sum); + return allPaths; + } + + private void dfs(TreeNode root, List path, List> allPaths, int sum) { + path.add(root.val); + if (root.left != null) { + dfs(root.left, path, allPaths, sum - root.val); + } + if (root.right != null) { + dfs(root.right, path, allPaths, sum - root.val); + } + if (root.left == null && root.right == null) { + /*Check if sum equals root.val, not sum equals zero!*/ + if (sum == root.val) { + allPaths.add(new ArrayList(path)); + } + } + path.remove(path.size() - 1); + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/27/2021. + * A classic backtracking problem/solution. + */ + public List> pathSum(TreeNode root, int targetSum) { + List> ans = new ArrayList<>(); + backtracking(root, new ArrayList<>(), targetSum, 0, ans); + return ans; + } + + private void backtracking( + TreeNode root, + List path, + int targetSum, + int currentSum, + List> ans) { + if (root == null) { + return; + } + path.add(root.val); + currentSum += root.val; + if (currentSum == targetSum && root.left == null && root.right == null) { + ans.add(new ArrayList<>(path)); + path.remove(path.size() - 1); // backtracking + return; + } + backtracking(root.left, path, targetSum, currentSum, ans); + backtracking(root.right, path, targetSum, currentSum, ans); + path.remove(path.size() - 1); // backtracking + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_114.java b/src/main/java/com/fishercoder/solutions/firstthousand/_114.java new file mode 100644 index 0000000000..a21b1531a6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_114.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _114 { + + public static class Solution1 { + public void flatten(TreeNode root) { + while (root != null) { + if (root.left != null) { + TreeNode previousNode = root.left; + while (previousNode.right != null) { + previousNode = previousNode.right; + } + previousNode.right = root.right; + root.right = root.left; + root.left = null; + } + root = root.right; + } + } + } + + public static class Solution2 { + + /* + * Credit: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/solution/ + */ + public void flatten(TreeNode root) { + flat(root); + } + + private TreeNode flat(TreeNode curr) { + if (curr == null) { + return null; + } + if (curr.left == null && curr.right == null) { + return curr; + } + TreeNode leftTail = flat(curr.left); + TreeNode rightTail = flat(curr.right); + if (leftTail != null) { + leftTail.right = curr.right; + curr.right = curr.left; + curr.left = null; + } + return rightTail == null ? leftTail : rightTail; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_115.java b/src/main/java/com/fishercoder/solutions/firstthousand/_115.java new file mode 100644 index 0000000000..adf7a8ac80 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_115.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +public class _115 { + public static class Solution1 { + public int numDistinct(String s, String t) { + int m = s.length(); + int n = t.length(); + int[][] dp = new int[m + 1][n + 1]; + + char[] schar = s.toCharArray(); + char[] tchar = t.toCharArray(); + + for (int i = 0; i <= m; i++) { + dp[i][0] = 1; + } + + for (int j = 1; j <= n; j++) { + dp[0][j] = 0; + } + + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + if (schar[i - 1] == tchar[j - 1]) { + dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]; + } else { + dp[i][j] = dp[i - 1][j]; + } + } + } + return dp[m][n]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_116.java b/src/main/java/com/fishercoder/solutions/firstthousand/_116.java new file mode 100644 index 0000000000..53ce763257 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_116.java @@ -0,0 +1,91 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _116 { + public static class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node(int x) { + this.val = x; + } + } + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution + * based on level order traversal + */ + public Node connect(Node root) { + + Node head = null; // head of the next level + Node prev = null; // the leading node on the next level + Node curr = root; // current node of current level + + while (curr != null) { + while (curr != null) { // iterate on the current level + // left child + if (curr.left != null) { + if (prev != null) { + prev.next = curr.left; + } else { + head = curr.left; + } + prev = curr.left; + } + // right child + if (curr.right != null) { + if (prev != null) { + prev.next = curr.right; + } else { + head = curr.right; + } + prev = curr.right; + } + // move to next node + curr = curr.next; + } + // move to next level + curr = head; + head = null; + prev = null; + } + return root; + } + } + + public static class Solution2 { + /* + * My complete original solution on 10/10/2021, although with O(h) extra space. + */ + public Node connect(Node root) { + if (root == null) { + return null; + } + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + Node curr = queue.poll(); + if (i < size - 1) { + curr.next = queue.peek(); + } else { + curr.next = null; + } + if (curr.left != null) { + queue.offer(curr.left); + } + if (curr.right != null) { + queue.offer(curr.right); + } + } + } + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_117.java b/src/main/java/com/fishercoder/solutions/firstthousand/_117.java new file mode 100644 index 0000000000..050bac08c3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_117.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +public class _117 { + + public static class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node(int x) { + this.val = x; + } + } + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution + * O(1) space, based on level order traversal + */ + public Node connect(Node root) { + + Node head = null; // head of the next level + Node prev = null; // the leading node on the next level + Node cur = root; // current node of current level + + while (cur != null) { + + while (cur != null) { // iterate on the current level + // left child + if (cur.left != null) { + if (prev != null) { + prev.next = cur.left; + } else { + head = cur.left; + } + prev = cur.left; + } + // right child + if (cur.right != null) { + if (prev != null) { + prev.next = cur.right; + } else { + head = cur.right; + } + prev = cur.right; + } + // move to next node + cur = cur.next; + } + + // move to next level + cur = head; + head = null; + prev = null; + } + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_118.java b/src/main/java/com/fishercoder/solutions/firstthousand/_118.java new file mode 100644 index 0000000000..63a37f7c9b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_118.java @@ -0,0 +1,69 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _118 { + + public static class Solution1 { + /* + * fill out values from left to right + */ + public List> generate(int numRows) { + List> result = new ArrayList(); + List row = new ArrayList(); + for (int i = 0; i < numRows; i++) { + row.add(0, 1); + for (int j = 1; j < row.size() - 1; j++) { + row.set(j, row.get(j) + row.get(j + 1)); + } + result.add(new ArrayList(row)); + } + return result; + } + } + + public static class Solution2 { + /* + * fill out values from right to left + * credit: https://leetcode.com/problems/pascals-triangle/discuss/38141/My-concise-solution-in-Java/36127 + */ + public List> generate(int numRows) { + List> result = new ArrayList(); + List row = new ArrayList(); + for (int i = 0; i < numRows; i++) { + for (int j = row.size() - 1; j >= 1; j--) { + row.set(j, row.get(j) + row.get(j - 1)); + } + row.add(1); + result.add(new ArrayList<>(row)); + } + return result; + } + } + + public static class Solution3 { + /* + * my completely original solution on 9/15/2021 + */ + public List> generate(int numRows) { + List> ans = new ArrayList<>(); + for (int i = 0; i < numRows; i++) { + if (ans.isEmpty()) { + ans.add(Arrays.asList(1)); + } else { + List prev = ans.get(ans.size() - 1); + List curr = new ArrayList<>(prev.size() + 1); + curr.add(1); + for (int j = 0; j < prev.size() - 1; j++) { + curr.add(prev.get(j) + prev.get(j + 1)); + } + curr.add(1); + ans.add(curr); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_119.java b/src/main/java/com/fishercoder/solutions/firstthousand/_119.java new file mode 100644 index 0000000000..5b422c088a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_119.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _119 { + + public static class Solution1 { + public List getRow(int rowIndex) { + if (rowIndex < 0) { + return new ArrayList(); + } + List> result = new ArrayList(); + List row = new ArrayList(); + row.add(1); + result.add(row); + for (int i = 1; i <= rowIndex; i++) { + List newRow = new ArrayList(); + newRow.add(1); + List lastRow = result.get(i - 1); + for (int j = 1; j < lastRow.size(); j++) { + newRow.add(lastRow.get(j - 1) + lastRow.get(j)); + } + newRow.add(1); + result.add(newRow); + } + return result.get(result.size() - 1); + } + } + + public static class Solution2 { + /* + * O(k) space + */ + public List getRow(int rowIndex) { + List row = new ArrayList<>(); + for (int i = 0; i <= rowIndex; i++) { + row.add(0, 1); + for (int j = 1; j < row.size() - 1; j++) { + row.set(j, row.get(j) + row.get(j + 1)); + } + } + return row; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_12.java b/src/main/java/com/fishercoder/solutions/firstthousand/_12.java similarity index 81% rename from src/main/java/com/fishercoder/solutions/_12.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_12.java index d9e8ee2962..083ec78b74 100644 --- a/src/main/java/com/fishercoder/solutions/_12.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_12.java @@ -1,15 +1,14 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _12 { public static class Solution1 { public String intToRoman(int num) { - String[] M = new String[]{"", "M", "MM", "MMM"}; + String[] M = new String[] {"", "M", "MM", "MMM"}; String[] C = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; String[] X = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; String[] I = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10]; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_120.java b/src/main/java/com/fishercoder/solutions/firstthousand/_120.java new file mode 100644 index 0000000000..57b88ac6d1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_120.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.List; + +public class _120 { + public static class Solution1 { + public int minimumTotal(List> triangle) { + int n = triangle.size(); + List cache = triangle.get(n - 1); + + for (int layer = n - 2; layer >= 0; layer--) { + // for each layer + for (int i = 0; i <= layer; i++) { + // check its very node + int value = + Math.min(cache.get(i), cache.get(i + 1)) + triangle.get(layer).get(i); + cache.set(i, value); + } + } + return cache.get(0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_121.java b/src/main/java/com/fishercoder/solutions/firstthousand/_121.java new file mode 100644 index 0000000000..3d4814a788 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_121.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.firstthousand; + +public class _121 { + + public static class Solution1 { + /* + * My very original but not super optimal solution. + */ + public int maxProfit(int[] prices) { + int[] maxPriceAfterThisDay = new int[prices.length]; + int maxSell; + for (int i = prices.length - 1; i > 0; i--) { + maxSell = Math.max(maxPriceAfterThisDay[i], prices[i]); + maxPriceAfterThisDay[i] = maxSell; + } + int[] minPriceBeforeThisDay = new int[prices.length]; + int minBuy = prices[0]; + for (int i = 0; i < prices.length - 1; i++) { + minBuy = Math.min(prices[i], minBuy); + minPriceBeforeThisDay[i] = minBuy; + } + int maxPro = 0; + for (int i = 0; i < prices.length - 1; i++) { + maxPro = Math.max(maxPro, maxPriceAfterThisDay[i + 1] - minPriceBeforeThisDay[i]); + } + return maxPro; + } + } + + public static class Solution2 { + /* + * The key here is that you'll have to buy first, before you can sell. That means, if the lower + * price comes after a higher price, their combination won't work! Since you cannot sell first + * before you buy it. + */ + public int maxProfit(int[] prices) { + if (prices == null || prices.length == 0) { + return 0; + } + int minBuy = prices[0]; + int maxProfit = 0; + for (int i = 1; i < prices.length; i++) { + if (prices[i] < minBuy) { + minBuy = prices[i]; + } else { + maxProfit = Math.max(maxProfit, prices[i] - minBuy); + } + } + return maxProfit; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_122.java b/src/main/java/com/fishercoder/solutions/firstthousand/_122.java new file mode 100644 index 0000000000..2d97438b07 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_122.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +public class _122 { + public static class Solution1 { + // peak and valley approach + public int maxProfit(int[] prices) { + int pro = 0; + int i = 0; + while (i < prices.length - 1) { + while (i < prices.length - 1 && prices[i] >= prices[i + 1]) { + i++; + } + int valley = prices[i]; + while (i < prices.length - 1 && prices[i] <= prices[i + 1]) { + i++; + } + int peak = prices[i]; + pro += peak - valley; + } + return pro; + } + } + + public static class Solution2 { + // simple one pass approach: the above solution could be simplified as below + + /* + * Or this approach could be understood as: + * We'll sell and buy on the same day as long as this day's stock price is higher than the previous day, a good example is this array: [1, 2, 3, 4, 5]. + * As this problem states that:"you can buy it then immediately sell it on the same day". Likewise, we can buy it back immediately as we sell it on the same day. + */ + public int maxProfit(int[] prices) { + int pro = 0; + for (int i = 0; i < prices.length - 1; i++) { + if (prices[i + 1] > prices[i]) { + pro += prices[i + 1] - prices[i]; + } + } + return pro; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_123.java b/src/main/java/com/fishercoder/solutions/firstthousand/_123.java new file mode 100644 index 0000000000..5887b854b0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_123.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _123 { + + public static class Solution1 { + // this is a very clear solution and very highly upvoted in Discuss, but not extensible to K + // solution. + public int maxProfit(int[] prices) { + int buy1 = Integer.MIN_VALUE; + int buy2 = Integer.MIN_VALUE; + int sell1 = 0; + int sell2 = 0; + for (int i = 0; i < prices.length; i++) { + buy1 = Math.max(buy1, -prices[i]); + sell1 = Math.max(sell1, buy1 + prices[i]); + buy2 = Math.max(buy2, sell1 - prices[i]); + sell2 = Math.max(sell2, buy2 + prices[i]); + } + return sell2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_124.java b/src/main/java/com/fishercoder/solutions/firstthousand/_124.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_124.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_124.java index 0c888ed04d..98d18bcbfd 100644 --- a/src/main/java/com/fishercoder/solutions/_124.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_124.java @@ -1,7 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; - import java.util.HashMap; import java.util.Map; @@ -30,7 +29,7 @@ private int dfs(TreeNode root) { } public static class Solution2 { - /** + /* * This one uses a map to cache, but surprisingly, it's 10% slower than all submissions compared * with solution1 */ diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_125.java b/src/main/java/com/fishercoder/solutions/firstthousand/_125.java new file mode 100644 index 0000000000..7764553bc4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_125.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _125 { + public static class Solution1 { + public boolean isPalindrome(String s) { + int left = 0; + int right = s.length() - 1; + char[] chars = s.toCharArray(); + while (left < right) { + while (left < right && !Character.isLetterOrDigit(chars[left])) { + left++; + } + while (left < right && !Character.isLetterOrDigit(chars[right])) { + right--; + } + if (Character.toLowerCase(chars[left]) != Character.toLowerCase(chars[right])) { + return false; + } + left++; + right--; + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_126.java b/src/main/java/com/fishercoder/solutions/firstthousand/_126.java new file mode 100644 index 0000000000..020cac75bc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_126.java @@ -0,0 +1,148 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +/* +* 126. Word Ladder II + +Given two words (beginWord and endWord), and a dictionary's word list, +find all shortest transformation sequence(s) from beginWord to endWord, such that: + +Only one letter can be changed at a time +Each transformed word must exist in the word list. Note that beginWord is not a transformed word. + +For example, +Given: +beginWord = "hit" +endWord = "cog" +wordList = ["hot","dot","dog","lot","log","cog"] + +Return +[ +["hit","hot","dot","dog","cog"], +["hit","hot","lot","log","cog"] +] + +Note: +Return an empty list if there is no such transformation sequence. +All words have the same length. +All words contain only lowercase alphabetic characters. +You may assume no duplicates in the word list. +You may assume beginWord and endWord are non-empty and are not the same. +*/ + +public class _126 { + + public static class Solution1 { + /* Reference: https://discuss.leetcode.com/topic/2857/share-two-similar-java-solution-that-accpted-by-oj */ + + Map> map; + List> results; + + public List> findLadders(String start, String end, List dict) { + results = new ArrayList<>(); + if (dict.size() == 0) { + return results; + } + + int min = Integer.MAX_VALUE; + + Queue queue = new ArrayDeque<>(); + queue.add(start); + + map = new HashMap<>(); + + Map ladder = new HashMap<>(); + for (String string : dict) { + ladder.put(string, Integer.MAX_VALUE); + } + ladder.put(start, 0); + + dict.add(end); + // BFS: Dijisktra search + while (!queue.isEmpty()) { + + String word = queue.poll(); + + int step = + ladder.get(word) + + 1; // 'step' indicates how many steps are needed to travel to one + // word. + + if (step > min) { + break; + } + + for (int i = 0; i < word.length(); i++) { + StringBuilder builder = new StringBuilder(word); + for (char ch = 'a'; ch <= 'z'; ch++) { + builder.setCharAt(i, ch); + String newWord = builder.toString(); + if (ladder.containsKey(newWord)) { + + if (step > ladder.get(newWord)) { + // Check if it is the shortest path to one word. + continue; + } else if (step < ladder.get(newWord)) { + queue.add(newWord); + ladder.put(newWord, step); + } else { + // It is a KEY line. If one word already appeared in one ladder, + // Do not insert the same word inside the queue twice. Otherwise it + // gets TLE. + } + if (map.containsKey(newWord)) { + // Build adjacent Graph + map.get(newWord).add(word); + } else { + List list = new LinkedList(); + list.add(word); + map.put(newWord, list); + // It is possible to write three lines in one: + // map.put(new_word,new LinkedList(Arrays.asList(new + // String[]{word}))); + // Which one is better? + } + + if (newWord.equals(end)) { + min = step; + } + } + // End if dict contains new_word + } + // End:Iteration from 'a' to 'z' + } + // End:Iteration from the first to the last + } + // End While + + // BackTracking + LinkedList result = new LinkedList<>(); + backTrace(end, start, result); + + return results; + } + + private void backTrace(String word, String start, List list) { + if (word.equals(start)) { + list.add(0, start); + results.add(new ArrayList<>(list)); + list.remove(0); + return; + } + list.add(0, word); + if (map.get(word) != null) { + for (String s : map.get(word)) { + backTrace(s, start, list); + } + } + list.remove(0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_127.java b/src/main/java/com/fishercoder/solutions/firstthousand/_127.java new file mode 100644 index 0000000000..3bfc57a51a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_127.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _127 { + public static class Solution1 { + + public int ladderLength(String beginWord, String endWord, List wordList) { + Set beginSet = new HashSet<>(); + Set endSet = new HashSet<>(); + Set visited = new HashSet<>(); + Set dict = new HashSet<>(wordList); + int len = 1; + + beginSet.add(beginWord); + + if (dict.contains(endWord)) { + endSet.add(endWord); + } + + while (!beginSet.isEmpty() && !endSet.isEmpty()) { + Set nextBeginSet = new HashSet<>(); + for (String word : beginSet) { + char[] chars = word.toCharArray(); + for (int i = 0; i < chars.length; i++) { + for (char c = 'a'; c <= 'z'; c++) { + char old = chars[i]; + chars[i] = c; + String newWord = new String(chars); + if (endSet.contains(newWord)) { + return len + 1; + } + + if (!visited.contains(newWord) && dict.contains(newWord)) { + visited.add(newWord); + nextBeginSet.add(newWord); + } + chars[i] = old; + } + } + } + + beginSet = nextBeginSet; + len++; + } + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_128.java b/src/main/java/com/fishercoder/solutions/firstthousand/_128.java new file mode 100644 index 0000000000..3ff62dffd0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_128.java @@ -0,0 +1,171 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +public class _128 { + public static class Solution1 { + public int longestConsecutive(int[] nums) { + Map map = new HashMap(); + // + UnionFind uf = new UnionFind(nums); + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i])) { + continue; + } + map.put(nums[i], i); + if (map.containsKey(nums[i] - 1)) { + uf.union(i, map.get(nums[i] - 1)); + // note: we want to union this index and nums[i]-1's root index which we can get + // from the map + } + if (map.containsKey(nums[i] + 1)) { + uf.union(i, map.get(nums[i] + 1)); + } + } + return uf.maxUnion(); + } + + class UnionFind { + int[] ids; + + public UnionFind(int[] nums) { + ids = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + ids[i] = i; + } + } + + public void union(int i, int j) { + int x = find(ids, i); + int y = find(ids, j); + ids[x] = y; + } + + public int find(int[] ids, int i) { + while (i != ids[i]) { + ids[i] = ids[ids[i]]; + i = ids[i]; + } + return i; + } + + public boolean connected(int i, int j) { + return find(ids, i) == find(ids, j); + } + + public int maxUnion() { + // this is O(n) + int max = 0; + int[] count = new int[ids.length]; + for (int i = 0; i < ids.length; i++) { + count[find(ids, i)]++; + max = max < count[find(ids, i)] ? count[find(ids, i)] : max; + } + return max; + } + } + } + + public static class Solution2 { + // inspired by this solution: + // https://discuss.leetcode.com/topic/25493/simple-fast-java-solution-using-set + public int longestConsecutive(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + + Set set = new HashSet(); + for (int i : nums) { + set.add(i); + } + int max = 1; + + for (int num : nums) { + if (set.remove(num)) { + int val = num; + int count = 1; + while (set.remove(val - 1)) { + val--; // we find all numbers that are smaller than num and remove them from + // the set + } + count += num - val; + + val = num; + while (set.remove(val + 1)) { + val++; // then we find all numbers that are bigger than num and also remove + // them from the set + } + count += val - num; + + max = Math.max(max, count); + } + } + return max; + } + } + + public static class Solution3 { + /* + * O(n) time complexity. + */ + public int longestConsecutive(int[] nums) { + Set set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + + int longestStreak = 0; + for (int num : set) { + // we'll go through this set instead of nums, this makes a big difference in time + // complexity, esp. based on LeetCode test cases + if (!set.contains(num - 1)) { + int currentNum = num; + int currentStreak = 1; + + while (set.contains(currentNum + 1)) { + currentNum += 1; + currentStreak += 1; + } + longestStreak = Math.max(longestStreak, currentStreak); + } + } + return longestStreak; + } + } + + public static class Solution4 { + /* + * O(nlogn) time complexity + */ + public int longestConsecutive(int[] nums) { + if (nums.length == 0) { + return 0; + } + TreeSet treeSet = new TreeSet<>(); + for (int i : nums) { + treeSet.add(i); // O(logn) time complexity for each add() call + } + int ans = 1; + Iterator it = treeSet.iterator(); + Integer curr = it.next(); + int len = 1; + while (it.hasNext()) { + Integer next = it.next(); + if (curr + 1 == next) { + len++; + } else { + len = 1; + } + curr = next; + ans = Math.max(ans, len); + } + ans = Math.max(ans, len); + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_129.java b/src/main/java/com/fishercoder/solutions/firstthousand/_129.java new file mode 100644 index 0000000000..da8f46235d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_129.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _129 { + public static class Solution1 { + public int sumNumbers(TreeNode root) { + if (root == null) { + return 0; + } + List allNumbers = new ArrayList(); + dfs(root, new StringBuilder(), allNumbers); + return allNumbers.stream().mapToInt(i -> i).sum(); + } + + private void dfs(TreeNode root, StringBuilder sb, List allNumbers) { + sb.append(root.val); + if (root.left != null) { + dfs(root.left, sb, allNumbers); + } + if (root.right != null) { + dfs(root.right, sb, allNumbers); + } + if (root.left == null && root.right == null) { + allNumbers.add(Integer.parseInt(sb.toString())); + } + // this is to delete the last value. since it's guaranteed that the value is between + // [0,9], so only one char needs to be deleted. + // however if the value is >= 10 then this approach needs to be adjusted + sb.deleteCharAt(sb.length() - 1); + } + } + + public static class Solution2 { + public int sumNumbers(TreeNode root) { + return dfs(root, 0); + } + + private int dfs(TreeNode root, int sum) { + if (root == null) { + return 0; + } + if (root.left == null && root.right == null) { + return sum * 10 + root.val; + } + int left = dfs(root.left, sum * 10 + root.val); + int right = dfs(root.right, sum * 10 + root.val); + return left + right; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_13.java b/src/main/java/com/fishercoder/solutions/firstthousand/_13.java new file mode 100644 index 0000000000..21c9fce9cd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_13.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _13 { + + public static class Solution1 { + /* + * This is the most concise/elegant version to solve this problem: + * 1. use a map to hold the roman to numeric mappings; + * 2. we always add the number (corresponding from the char) first, then once we find that a later char has a corresponding value that is bigger than the previous char, + * we deduct 2 * previousCharCorrespondingValue + *

+ * e.g. XIV step by step is computed below: + * 0 + 10 = 10 + * 10 + 1 = 11 + * 11 + (5 - 2*1) = 14 + */ + public int romanToInt(String s) { + Map map = new HashMap(); + map.put('I', 1); + map.put('V', 5); + map.put('X', 10); + map.put('L', 50); + map.put('C', 100); + map.put('D', 500); + map.put('M', 1000); + + int result = 0; + for (int i = 0; i < s.length(); i++) { + if (i > 0 && map.get(s.charAt(i)) > map.get(s.charAt(i - 1))) { + result += map.get(s.charAt(i)) - 2 * map.get(s.charAt(i - 1)); + } else { + result += map.get(s.charAt(i)); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_130.java b/src/main/java/com/fishercoder/solutions/firstthousand/_130.java new file mode 100644 index 0000000000..a43a26a0a8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_130.java @@ -0,0 +1,169 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +public class _130 { + + public static class Solution1 { + + /* + * I won't call this problem hard, it's just confusing, you'll definitely want to clarify what + * the problem means before coding. This problem actually means: any grid that is 'O' but on + * the four edges, will never be marked to 'X'; furthermore, any grid that is 'O' and that is + * connected with the above type of 'O' will never be marked to 'X' as well; only all other + * nodes that has any one direct neighbor that is an 'X' will be marked to 'X'. + */ + + int[] dirs = new int[] {0, 1, 0, -1, 0}; + + public void solve(char[][] board) { + if (board == null || board.length == 0 || board[0].length == 0) { + return; + } + int m = board.length; + int n = board[0].length; + Queue queue = new LinkedList(); + // check first row and last row and mark all those '0' on these two rows to be '+' to + // let them be different from other 'O', + // at the same time, we put them into the queue to get ready for a BFS to mark all those + // adjacent 'O' nodes to '+' as well + for (int j = 0; j < n; j++) { + if (board[0][j] == 'O') { + board[0][j] = '+'; + queue.offer(new int[] {0, j}); + } + if (board[m - 1][j] == 'O') { + board[m - 1][j] = '+'; + queue.offer(new int[] {m - 1, j}); + } + } + + // check first column and last column too + for (int i = 0; i < m; i++) { + if (board[i][0] == 'O') { + board[i][0] = '+'; + queue.offer(new int[] {i, 0}); + } + if (board[i][n - 1] == 'O') { + board[i][n - 1] = '+'; + queue.offer(new int[] {i, n - 1}); + } + } + + while (!queue.isEmpty()) { + int[] curr = queue.poll(); + for (int i = 0; i < 4; i++) { + int x = curr[0] + dirs[i]; + int y = curr[1] + dirs[i + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') { + board[x][y] = '+'; + queue.offer(new int[] {x, y}); + } + } + } + + // now we can safely mark all other 'O' to 'X', also remember to put those '+' back to + // 'O' + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == 'O') { + board[i][j] = 'X'; + } else if (board[i][j] == '+') { + board[i][j] = 'O'; + } + } + } + } + } + + public static class Solution2 { + /* + * My completely original solution on 11/1/2021, again, using a pen and paper to visualize my thought process and list out all key steps helps a lot! + * 1. scan through this board; + * 2. whenever we find an 'O', we'll do BFS to find all connected points and use the first 'O' as its head point for this entire connected region; + * 3. whenever we visit a point, mark it as visited. + */ + public void solve(char[][] board) { + int m = board.length; + int n = board[0].length; + boolean[][] visited = new boolean[m][n]; + Map> headMap = new HashMap<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (!visited[i][j] && board[i][j] == 'O') { + boolean capturable = bfs(i, j, board, visited, headMap); + if (capturable) { + capture(headMap, board); + } + } + } + } + } + + private void capture(Map> headMap, char[][] board) { + int m = board.length; + int n = board[0].length; + for (int head : headMap.keySet()) { + List list = headMap.get(head); + for (int[] p : list) { + board[p[0]][p[1]] = 'X'; + } + int x = head / m; + int y = head % n; + board[x][y] = 'X'; + } + } + + private boolean bfs( + int startI, + int startJ, + char[][] board, + boolean[][] visited, + Map> headMap) { + boolean capturable = true; + Queue queue = new LinkedList<>(); + int m = board.length; + int n = board[0].length; + queue.offer(new int[] {startI, startJ}); + int head = startI * n + startJ; + List list = headMap.getOrDefault(head, new ArrayList<>()); + list.add(new int[] {startI, startJ}); + int[] directions = new int[] {0, 1, 0, -1, 0}; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + int[] curr = queue.poll(); + if (curr[0] == 0 || curr[0] == m - 1 || curr[1] == 0 || curr[1] == n - 1) { + capturable = false; + } + visited[curr[0]][curr[1]] = true; + for (int j = 0; j < directions.length - 1; j++) { + int newx = directions[j] + curr[0]; + int newy = directions[j + 1] + curr[1]; + if (newx >= 0 + && newx < m + && newy >= 0 + && newy < n + && !visited[newx][newy] + && board[newx][newy] == 'O') { + queue.offer(new int[] {newx, newy}); + visited[newx][newy] = true; + list.add(new int[] {newx, newy}); + } + } + } + } + if (!capturable) { + headMap.remove(head); + } else { + headMap.put(head, list); + } + return capturable; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_131.java b/src/main/java/com/fishercoder/solutions/firstthousand/_131.java new file mode 100644 index 0000000000..3be190c0fe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_131.java @@ -0,0 +1,87 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _131 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/palindrome-partitioning/solution/ + * DFS + backtracking + */ + public List> partition(String s) { + List> result = new ArrayList<>(); + dfs(0, result, new ArrayList<>(), s); + return result; + } + + private void dfs(int start, List> result, List currentList, String s) { + if (start >= s.length()) { + result.add(new ArrayList<>(currentList)); + } + for (int end = start; end < s.length(); end++) { + if (isPalindrome(s, start, end)) { + currentList.add(s.substring(start, end + 1)); + dfs(end + 1, result, currentList, s); + currentList.remove(currentList.size() - 1); + } + } + } + + private boolean isPalindrome(String s, int start, int end) { + while (start < end) { + if (s.charAt(start++) != s.charAt(end--)) { + return false; + } + } + return true; + } + } + + public static class Solution2 { + public List> partition(String s) { + List> result = new ArrayList(); + int n = s.length(); + boolean[][] dp = new boolean[n][n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j <= i; j++) { + if (s.charAt(j) == s.charAt(i) && (j + 1 >= i - 1 || dp[j + 1][i - 1])) { + // j+1 >= i-1 means j and i are adjance to each other or only one char apart + // from each other + // dp[j+1][i-1] means its inner substring is a palindrome, so as long as + // s.charAt(j) == s.charAt(i), then dp[j][i] must be a palindrome. + dp[j][i] = true; + } + } + } + + for (boolean[] list : dp) { + for (boolean b : list) { + System.out.print(b + ", "); + } + System.out.println(); + } + System.out.println(); + + backtracking(s, 0, dp, new ArrayList(), result); + + return result; + } + + void backtracking( + String s, int start, boolean[][] dp, List temp, List> result) { + if (start == s.length()) { + List newTemp = new ArrayList(temp); + result.add(newTemp); + } + for (int i = start; i < s.length(); i++) { + if (dp[start][i]) { + temp.add(s.substring(start, i + 1)); + backtracking(s, i + 1, dp, temp, result); + temp.remove(temp.size() - 1); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_132.java b/src/main/java/com/fishercoder/solutions/firstthousand/_132.java new file mode 100644 index 0000000000..0aa3265e9c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_132.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.firstthousand; + +/* +* 132. Palindrome Partitioning II + +Given a string s, partition s such that every substring of the partition is a palindrome. + +Return the minimum cuts needed for a palindrome partitioning of s. + +For example, given s = "aab", +Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut. + +*/ +public class _132 { + + /*This solution is cooler than Jiuzhang: https://discuss.leetcode.com/topic/32575/easiest-java-dp-solution-97-36*/ + + public static class Solution1 { + // cut[i] stands for the minimum number of cut needed to cut [0, i] into palindromes + // we initiazlie cut[i] with its max possible value which is i, this is because a single + // char is naturally a palindrome, so, we'll cut this string into all single-char + // substrings, which is the max cuts needed + + // dp[j][i] == true stands for s.substring(j,i) is a palindrome + public int minCut(String s) { + int n = s.length(); + char[] c = s.toCharArray(); + boolean[][] dp = new boolean[n][n]; + int[] cut = new int[n]; + + for (int i = 0; i < n; i++) { + cut[i] = i; + for (int j = 0; j <= i; j++) { + if (c[i] == c[j] && (j + 1 > i - 1 || dp[j + 1][i - 1])) { + dp[j][i] = true; + if (j == 0) { + cut[i] = 0; + } else { + cut[i] = (cut[i] < cut[j - 1] + 1) ? cut[i] : cut[j - 1] + 1; + } + } + } + } + + return cut[n - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_133.java b/src/main/java/com/fishercoder/solutions/firstthousand/_133.java new file mode 100644 index 0000000000..6a133ca435 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_133.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +public class _133 { + + public static class Solution1 { + public Node cloneGraph(Node node) { + if (node == null) { + return node; + } + + Map map = new HashMap(); + Queue queue = new LinkedList(); + Node root = new Node(node.val); + map.put(root.val, root); + // remember to offer the original input node into the queue which contains all the + // information + queue.offer(node); + while (!queue.isEmpty()) { + Node curr = queue.poll(); + for (Node eachNode : curr.neighbors) { + if (!map.containsKey(eachNode.val)) { + map.put(eachNode.val, new Node(eachNode.val)); + queue.offer(eachNode); + } + map.get(curr.val).neighbors.add(map.get(eachNode.val)); + } + } + return root; + } + + public static class Node { + public int val; + public List neighbors; + + public Node() { + this.neighbors = new ArrayList<>(); + } + + public Node(int val) { + this.val = val; + this.neighbors = new ArrayList<>(); + } + + public Node(int val, List neighbors) { + this.val = val; + this.neighbors = neighbors; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_134.java b/src/main/java/com/fishercoder/solutions/firstthousand/_134.java new file mode 100644 index 0000000000..b0251bb93f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_134.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +/* +* 134. Gas Station +* +* There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. +You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). +You begin the journey with an empty tank at one of the gas stations. + +Return the starting gas station's index if you can travel around the circuit once, otherwise return -1. + +Note: +The solution is guaranteed to be unique. +*/ +public class _134 { + + public static class Solution1 { + /* Credit: https://discuss.leetcode.com/topic/5088/my-ac-is-o-1-space-o-n-running-time-solution-does-anybody-have-posted-this-solution */ + public int canCompleteCircuit(int[] gas, int[] cost) { + int start = gas.length - 1; + int end = 0; + int sum = gas[start] - cost[start]; + while (start > end) { + if (sum >= 0) { + sum += gas[end] - cost[end]; + end++; + } else { + start--; + sum += gas[start] - cost[start]; + } + } + return sum >= 0 ? start : -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_135.java b/src/main/java/com/fishercoder/solutions/firstthousand/_135.java new file mode 100644 index 0000000000..d7440aefc0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_135.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +/* +* 135. Candy + +There are N children standing in a line. Each child is assigned a rating value. + +You are giving candies to these children subjected to the following requirements: + +Each child must have at least one candy. +Children with a higher rating get more candies than their neighbors. +What is the minimum candies you must give? +*/ +public class _135 { + + public static class Solution1 { + public int candy(int[] ratings) { + int[] candy = new int[ratings.length]; + for (int i = 0; i < ratings.length; i++) { + candy[i] = 1; + } + + for (int i = 0; i < ratings.length - 1; i++) { + if (ratings[i] < ratings[i + 1]) { + candy[i + 1] = candy[i] + 1; + } + } + + for (int i = ratings.length - 1; i > 0; i--) { + if (ratings[i] < ratings[i - 1]) { + candy[i - 1] = Math.max(candy[i - 1], candy[i] + 1); + } + } + + int sum = 0; + for (int i : candy) { + sum += i; + } + + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_136.java b/src/main/java/com/fishercoder/solutions/firstthousand/_136.java new file mode 100644 index 0000000000..73a94ea90c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_136.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _136 { + + public static class Solution1 { + /* + * Approach 1: use set, since this problem explicitly states that every element appears twice + * and only one appears once so, we could safely remove the ones that are already in the set, + * O(n) time and O(n) space. HashTable approach works similarly like this one, but it could be + * more easily extend to follow-up questions. + */ + public int singleNumber(int[] nums) { + Set set = new HashSet(); + for (int i : nums) { + if (!set.add(i)) { + set.remove(i); + } + } + return set.iterator().next(); + } + } + + public static class Solution2 { + /* + * Approach 2: bit manipulation, use exclusive or ^ to solve this problem: we're using the trick + * here: every number ^ itself will become zero, so, the only remaining element will be the one + * that appeared only once. + */ + public int singleNumber(int[] nums) { + int res = 0; + for (int i : nums) { + res ^= i; + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_137.java b/src/main/java/com/fishercoder/solutions/firstthousand/_137.java new file mode 100644 index 0000000000..29b535fecf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_137.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _137 { + + public static class Solution1 { + public int singleNumber(int[] nums) { + Map map = new HashMap(); + for (int i : nums) { + map.put(i, map.getOrDefault(i, 0) + 1); + } + for (int key : map.keySet()) { + if (map.get(key) != 3) { + return key; + } + } + return 0; + } + } + + public static class Solution2 { + /* + * Credit: https://discuss.leetcode.com/topic/11877/detailed-explanation-and-generalization-of-the-bitwise-operation-method-for-single-numbers/2 + */ + public int singleNumber(int[] nums) { + int counter1 = 0; + int counter2 = 0; + int mask = 0; + for (int num : nums) { + counter2 ^= counter1 & num; + counter1 ^= num; + mask = ~(counter1 & counter2); + counter1 &= mask; + counter2 &= mask; + } + return counter1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_138.java b/src/main/java/com/fishercoder/solutions/firstthousand/_138.java new file mode 100644 index 0000000000..9462f8a4f8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_138.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _138 { + public static class Solution1 { + public Node copyRandomList(Node head) { + /*Key is the original nodes, value is the new nodes we're deep copying to.*/ + Map map = new HashMap(); + Node node = head; + + // loop for the first time: copy the node themselves with only labels + while (node != null) { + map.put(node, new Node(node.val)); + node = node.next; + } + + // loop for the second time: copy random and next pointers + node = head; + while (node != null) { + map.get(node).next = map.get(node.next); + map.get(node).random = map.get(node.random); + node = node.next; + } + + return map.get(head); + } + + // Definition for singly-linked list with a random pointer. + class Node { + int val; + + Node next; + Node random; + + Node(int x) { + this.val = x; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_139.java b/src/main/java/com/fishercoder/solutions/firstthousand/_139.java new file mode 100644 index 0000000000..24bcb6d8db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_139.java @@ -0,0 +1,87 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.List; + +public class _139 { + + public static class Solution1 { + /* + * this solution takes between 7 and 8 ms to finish on LeetCode + * beats around 38% to 48% submissions as of 6/27/2020 + */ + public boolean wordBreak(String s, List wordDict) { + int n = s.length(); + boolean[] dp = new boolean[n + 1]; + dp[0] = true; + for (int i = 1; i <= n; i++) { + for (int j = 0; j < i; j++) { + if (dp[j] && wordDict.contains(s.substring(j, i))) { + dp[i] = true; + break; + } + } + } + return dp[n]; + } + } + + public static class Solution2 { + /* + * Added pruning based on max word length. + * this solution takes between 2 and 3 ms to finish on LeetCode + * this beats 94.53% submissions as of 6/27/2020 + */ + public boolean wordBreak(String s, List wordDict) { + int maxLen = Integer.MIN_VALUE; + for (String word : wordDict) { + maxLen = (word.length() > maxLen) ? word.length() : maxLen; + } + + int n = s.length(); + boolean[] dp = new boolean[n + 1]; + dp[0] = true; + for (int i = 1; i <= n; i++) { + for (int j = 0; j < i; j++) { + if ((i - j) > maxLen) { + continue; + } + if (dp[j] && wordDict.contains(s.substring(j, i))) { + dp[i] = true; + break; + } + } + } + return dp[n]; + } + } + + public static class Solution3 { + /* + * Added pruning, plus start from the end to check. + * This solution takes 1 ms to finish on LeetCode + * This beats 99.02% submissions as of 6/27/2020. + */ + public boolean wordBreak(String s, List wordDict) { + int maxLen = Integer.MIN_VALUE; + for (String word : wordDict) { + maxLen = (word.length() > maxLen) ? word.length() : maxLen; + } + + int n = s.length(); + boolean[] dp = new boolean[n + 1]; + dp[0] = true; + for (int i = 1; i <= n; i++) { + for (int lastWordLength = 1; + lastWordLength <= i && lastWordLength <= maxLen; + lastWordLength++) { + if (dp[i - lastWordLength] + && wordDict.contains(s.substring(i - lastWordLength, i))) { + dp[i] = true; + break; + } + } + } + return dp[n]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_14.java b/src/main/java/com/fishercoder/solutions/firstthousand/_14.java similarity index 91% rename from src/main/java/com/fishercoder/solutions/_14.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_14.java index 419d47c550..a1f98ac302 100644 --- a/src/main/java/com/fishercoder/solutions/_14.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_14.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _14 { public static class Solution1 { - //horizontal scan + // horizontal scan public String longestCommonPrefix(String[] strs) { if (strs.length == 0) { return ""; @@ -22,7 +22,7 @@ public String longestCommonPrefix(String[] strs) { } public static class Solution2 { - //vertical scan + // vertical scan public String longestCommonPrefix(String[] strs) { if (strs.length == 0) { return ""; diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_140.java b/src/main/java/com/fishercoder/solutions/firstthousand/_140.java new file mode 100644 index 0000000000..4d982323cd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_140.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _140 { + public static class Solution1 { + public List wordBreak(String s, List wordDict) { + return dfs(s, wordDict, new HashMap<>()); + } + + List dfs(String s, List wordDict, Map> map) { + if (map.containsKey(s)) { + return map.get(s); + } + + List result = new ArrayList<>(); + if (s.length() == 0) { + result.add(""); + return result; + } + + for (String word : wordDict) { + if (s.startsWith(word)) { + List subList = dfs(s.substring(word.length()), wordDict, map); + for (String sub : subList) { + result.add(word + (sub.length() == 0 ? "" : " ") + sub); + } + } + } + map.put(s, result); + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_141.java b/src/main/java/com/fishercoder/solutions/firstthousand/_141.java new file mode 100644 index 0000000000..0ee479113a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_141.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.HashSet; +import java.util.Set; + +public class _141 { + + public static class Solution1 { + public boolean hasCycle(ListNode head) { + Set set = new HashSet(); + while (head != null) { + if (!set.add(head)) { + return true; + } + head = head.next; + } + return false; + } + } + + public static class Solution2 { + public boolean hasCycle(ListNode head) { + ListNode slow = head; + ListNode fast = head; + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + if (fast == slow) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_142.java b/src/main/java/com/fishercoder/solutions/firstthousand/_142.java new file mode 100644 index 0000000000..e3a1691cde --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_142.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.HashSet; +import java.util.Set; + +public class _142 { + + public static class Solution1 { + public ListNode detectCycle(ListNode head) { + Set seen = new HashSet<>(); + while (head != null) { + if (!seen.add(head)) { + return head; + } + head = head.next; + } + return null; + } + } + + public static class Solution2 { + /* + * This comment explains it really well for this solution: + * https://leetcode.com/problems/linked-list-cycle-ii/discuss/44774/Java-O(1)-space-solution-with-detailed-explanation./44281 + * + * When fast and slow meet for the first time at point P, fast travelled (a + b + c + b) + * and slow travelled (a + b), and we know fast travels twice fast as slow, so we have: + * a + b + c + b = 2*(a + b), this gives us a == c; + * so at point P, we start a new slow2 pointer from the head, when both slow and slow2 travelled distance a, they must meet + * at cycle entrance point Q. + */ + public ListNode detectCycle(ListNode head) { + ListNode slow = head; + ListNode fast = head; + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + if (slow == fast) { + ListNode slow2 = head; + while (slow2 != slow) { + slow = slow.next; + slow2 = slow2.next; + } + return slow; + } + } + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_143.java b/src/main/java/com/fishercoder/solutions/firstthousand/_143.java new file mode 100644 index 0000000000..455f0e5bc2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_143.java @@ -0,0 +1,93 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _143 { + public static class Solution1 { + public void reorderList(ListNode head) { + if (head == null || head.next == null) { + return; + } + /* first we use two pointers to separate this list into two parts */ + ListNode slowNode = head; + ListNode fastNode = head; + while (fastNode.next != null) { + fastNode = fastNode.next; + if (fastNode.next != null) { + fastNode = fastNode.next; + } else { + break; + } + slowNode = slowNode.next; + } + // two sublist heads + ListNode head1 = head; + ListNode head2 = slowNode.next; + // detach the two sublists; + slowNode.next = null; + + // reverse the second sublist + ListNode cur = head2; + ListNode post = cur.next; + cur.next = null; + while (post != null) { + ListNode temp = post.next; + post.next = cur; + cur = post; + post = temp; + } + head2 = cur; // the new head of the reversed sublist + + // merge the two sublists as required + ListNode p = head1; + ListNode q = head2; + while (q != null) { + ListNode temp1 = p.next; + ListNode temp2 = q.next; + p.next = q; + q.next = temp1; + p = temp1; + q = temp2; + } + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/25/2021, although not super efficient in time complexity, + * since I keep going through the rest of the list to the end at each iteration, it's accepted on LeetCode. + */ + public void reorderList(ListNode head) { + int len = getLen(head); + if (len <= 2) { + return; + } + ListNode curr = head; + for (int i = 0; i < len / 2; i++) { + ListNode tmp = curr; + ListNode newHead = curr.next; + while (tmp.next.next != null) { + tmp = tmp.next; + } + if (tmp == curr) { + break; + } + ListNode tail = tmp.next; + tmp.next = null; + curr.next = tail; + tail.next = newHead; + curr = newHead; + } + } + + private int getLen(ListNode head) { + int len = 0; + ListNode tmp = head; + while (tmp != null) { + tmp = tmp.next; + len++; + } + return len; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_144.java b/src/main/java/com/fishercoder/solutions/firstthousand/_144.java new file mode 100644 index 0000000000..16a1534e4e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_144.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class _144 { + public static class Solution1 { + public List preorderTraversal(TreeNode root) { + List list = new ArrayList<>(); + Stack stack = new Stack<>(); + stack.push(root); + while (!stack.isEmpty()) { + TreeNode curr = stack.pop(); + if (curr != null) { + list.add(curr.val); + stack.push(curr.right); + stack.push(curr.left); + } + } + return list; + } + } + + public static class Solution2 { + public List preorderTraversal(TreeNode root) { + return pre(root, new ArrayList()); + } + + List pre(TreeNode root, List list) { + if (root == null) { + return list; + } + list.add(root.val); + pre(root.left, list); + pre(root.right, list); + return list; + } + } + + public static class Solution3 { + public List preorderTraversal(TreeNode root) { + List list = new ArrayList<>(); + Stack stack = new Stack<>(); + while (!stack.isEmpty() || root != null) { + while (root != null) { + list.add(root.val); + stack.push(root); + root = root.left; + } + root = stack.pop(); + root = root.right; + } + return list; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_145.java b/src/main/java/com/fishercoder/solutions/firstthousand/_145.java new file mode 100644 index 0000000000..999f8f934d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_145.java @@ -0,0 +1,87 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Stack; + +public class _145 { + public static class Solution1 { + /* + * A tricky/hacky one: Modify the code for pre-order traversal + * so that it becomes root->right->left, + * and then reverse the result to get left->right->root. + */ + public List postorderTraversal(TreeNode root) { + List result = new ArrayList(); + if (root == null) { + return result; + } + Stack stack = new Stack(); + stack.push(root); + while (!stack.isEmpty()) { + root = stack.pop(); + result.add(root.val); + if (root.left != null) { + stack.push(root.left); + } + if (root.right != null) { + stack.push(root.right); + } + } + Collections.reverse(result); + return result; + } + } + + public static class Solution2 { + /* + * Or use a LinkedList and add values to the head, then no reverse is needed. + * the linked list contents get added like this: + *

+ * root + * right, root + * left, right, root + */ + public List postorderTraversal(TreeNode root) { + List list = new LinkedList<>(); + if (root == null) { + return list; + } + Stack stack = new Stack<>(); + stack.push(root); + while (!stack.isEmpty()) { + TreeNode curr = stack.pop(); + list.add(0, curr.val); + if (curr.left != null) { + stack.push(curr.left); + } + if (curr.right != null) { + stack.push(curr.right); + } + } + return list; + } + } + + public static class Solution3 { + /* + * recursive solution is trivial. + */ + public List postorderTraversal(TreeNode root) { + return post(root, new ArrayList()); + } + + List post(TreeNode root, List result) { + if (root == null) { + return result; + } + post(root.left, result); + post(root.right, result); + result.add(root.val); + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_146.java b/src/main/java/com/fishercoder/solutions/firstthousand/_146.java new file mode 100644 index 0000000000..51d3e563a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_146.java @@ -0,0 +1,149 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +public class _146 { + public class Solution1 { + public class LRUCache { + /* + * The shortest implementation is to use LinkedHashMap: + * specify a size of the LinkedHashMap; + * override the removeEldestEntry method when its size exceeds max size: + * https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html#removeEldestEntry-java.util.Map.Entry- + * in the constructor, set the last boolean variable to be true: it means the ordering mode, + * if we set it to be true, it means in access order, false, means it's in insertion order: + * https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html#LinkedHashMap-int-float-boolean- + */ + + private Map cache; + private final int max; + + public LRUCache(int capacity) { + max = capacity; + cache = + new LinkedHashMap(capacity, 1.0f, true) { + public boolean removeEldestEntry(Map.Entry eldest) { + return cache.size() > max; + } + }; + } + + public int get(int key) { + return cache.getOrDefault(key, -1); + } + + public void put(int key, int value) { + cache.put(key, value); + } + } + } + + public class Solution2 { + public class LRUCache { + /* + * The more verbose solution is to implement a doubly linked list yourself plus a map. + * It's very straightforward to implement this, key notes here: https://docs.google.com/spreadsheets/d/1anN6L5OLhUFd1ANtqDdYY6tz2Ao2H1GESfpDiCfeWyM/edit#gid=0 + * (search for the URL of this problem to find it.) + */ + private class Node { + int key; + int value; + + LRUCache.Node prev; + LRUCache.Node next; + + Node(int k, int v) { + this.key = k; + this.value = v; + } + + Node() { + this.key = 0; + this.value = 0; + } + } + + private int capacity; + private int count; + private LRUCache.Node head; + private LRUCache.Node tail; + private Map map; + + // ATTN: the value should be Node type! This is the whole point of having a class called + // Node! + + public LRUCache(int capacity) { + this.capacity = capacity; + this.count = + 0; // we need a count to keep track of the number of elements in the cache + // so + // that we know when to evict the LRU one from the cache + this.map = new HashMap(); + head = new LRUCache.Node(); + tail = new LRUCache.Node(); + head.next = tail; + tail.prev = head; + } + + public int get(int key) { + LRUCache.Node node = map.get(key); + // HashMap allows value to be null, this is superior to HashTable! + if (node == null) { + return -1; + } else { + /*Do two operations: this makes the process more clear: + * remove the old node first, and then + * just add the node again. + * This will guarantee that this node will be at the latest position: + * the most recently used position.*/ + remove(node); + add(node); + + return node.value; + } + } + + public void put(int key, int value) { + LRUCache.Node node = map.get(key); + if (node == null) { + node = new LRUCache.Node(key, value); + map.put(key, node); + add(node); + count++; + + if (count > capacity) { + /* ATTN: It's tail.prev, not tail, because tail is always an invalid node, it + doesn't contain anything, it's always the tail.prev that is the last node in the + cache*/ + LRUCache.Node toDelete = tail.prev; + map.remove(toDelete.key); + remove(toDelete); + count--; + } + } else { + remove(node); + node.value = value; + add(node); + } + } + + private void remove(LRUCache.Node node) { + LRUCache.Node next = node.next; + LRUCache.Node prev = node.prev; + prev.next = next; + next.prev = prev; + } + + private void add(LRUCache.Node node) { + // ATTN: we'll always add the node into the first position: head.next!!!! + LRUCache.Node next = head.next; + head.next = node; + node.next = next; + node.prev = head; + next.prev = node; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_147.java b/src/main/java/com/fishercoder/solutions/firstthousand/_147.java new file mode 100644 index 0000000000..87aed10285 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_147.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.List; + +public class _147 { + + public static class Solution1 { + public ListNode insertionSortList(ListNode head) { + ListNode temp = head; + List list = new ArrayList<>(); + while (temp != null) { + list.add(temp.val); + temp = temp.next; + } + Integer[] nums = list.toArray(new Integer[list.size()]); + for (int i = 1; i < list.size(); i++) { + for (int j = i - 1; j >= 0; j--) { + if (nums[j] > nums[j + 1]) { + int tempNum = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tempNum; + } + } + } + ListNode newHead = head; + for (int i = 0; i < nums.length; i++) { + newHead.val = nums[i]; + newHead = newHead.next; + } + return head; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_148.java b/src/main/java/com/fishercoder/solutions/firstthousand/_148.java new file mode 100644 index 0000000000..661a0178ab --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_148.java @@ -0,0 +1,225 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _148 { + + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/18100/java-merge-sort-solution + * But this is not using constant space. + */ + public ListNode sortList(ListNode head) { + if (head == null || head.next == null) { + return head; + } + + // Step 1: split the list into halves + ListNode prev = null; + ListNode slow = head; + ListNode fast = head; + while (fast != null && fast.next != null) { + prev = slow; + fast = fast.next.next; + slow = slow.next; + } + prev.next = null; + + // step 2: sort each half + ListNode l1 = sortList(head); + ListNode l2 = sortList(slow); + + // step 3: merge the two halves + return merge(l1, l2); + } + + private ListNode merge(ListNode l1, ListNode l2) { + ListNode result = new ListNode(0); + ListNode tmp = result; + + while (l1 != null && l2 != null) { + if (l1.val < l2.val) { + tmp.next = l1; + l1 = l1.next; + } else { + tmp.next = l2; + l2 = l2.next; + } + tmp = tmp.next; + } + + if (l1 != null) { + tmp.next = l1; + } + if (l2 != null) { + tmp.next = l2; + } + return result.next; + } + } + + public static class Solution2 { + ListNode tail = new ListNode(0); + ListNode nextSubList = new ListNode(0); + + public ListNode sortList(ListNode head) { + if (head == null || head.next == null) { + return head; + } + int n = getCount(head); + ListNode start = head; + ListNode dummyHead = new ListNode(0); + for (int size = 1; size < n; size = size * 2) { + tail = dummyHead; + while (start != null) { + if (start.next == null) { + tail.next = start; + break; + } + ListNode mid = split(start, size); + merge(start, mid); + start = nextSubList; + } + start = dummyHead.next; + } + return dummyHead.next; + } + + ListNode split(ListNode start, int size) { + ListNode midPrev = start; + ListNode end = start.next; + // use fast and slow approach to find middle and end of second linked list + for (int index = 1; + index < size && (midPrev.next != null || end.next != null); + index++) { + if (end.next != null) { + end = (end.next.next != null) ? end.next.next : end.next; + } + if (midPrev.next != null) { + midPrev = midPrev.next; + } + } + ListNode mid = midPrev.next; + midPrev.next = null; + nextSubList = end.next; + end.next = null; + // return the start of second linked list + return mid; + } + + void merge(ListNode list1, ListNode list2) { + ListNode dummyHead = new ListNode(0); + ListNode newTail = dummyHead; + while (list1 != null && list2 != null) { + if (list1.val < list2.val) { + newTail.next = list1; + list1 = list1.next; + newTail = newTail.next; + } else { + newTail.next = list2; + list2 = list2.next; + newTail = newTail.next; + } + } + newTail.next = (list1 != null) ? list1 : list2; + // traverse till the end of merged list to get the newTail + while (newTail.next != null) { + newTail = newTail.next; + } + // link the old tail with the head of merged list + tail.next = dummyHead.next; + // update the old tail to the new tail of merged list + tail = newTail; + } + + int getCount(ListNode head) { + int cnt = 0; + ListNode ptr = head; + while (ptr != null) { + ptr = ptr.next; + cnt++; + } + return cnt; + } + } + + public static class Solution3 { + /* + * Credit: https://leetcode.com/problems/sort-list/solution/ top down approach. + */ + public ListNode sortList(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode mid = getMid(head); + ListNode left = sortList(head); + ListNode right = sortList(mid); + return mergeList(left, right); + } + + private ListNode mergeList(ListNode left, ListNode right) { + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + while (left != null && right != null) { + if (left.val < right.val) { + tmp.next = left; + left = left.next; + } else { + tmp.next = right; + right = right.next; + } + tmp = tmp.next; + } + if (left != null) { + tmp.next = left; + } else if (right != null) { + tmp.next = right; + } + return pre.next; + } + + private ListNode getMid(ListNode head) { + /*The key/trick is in this method: + * it directly uses this head to iterate, so that we could use this top down recursive approach. + * If we assign head to slow and fast pointers, then this algorithm will run into StackOverflow exception. + * + * This is an absolutely amazing method!*/ + ListNode midPrev = null; + while (head != null && head.next != null) { + midPrev = (midPrev == null) ? head : midPrev.next; + head = head.next.next; + } + ListNode mid = midPrev.next; + midPrev.next = null; // this is the key, otherwise, StackOverflow exception will occur. + return mid; + } + } + + public static class Solution4 { + /*This is the most naive, using O(n) extra memory, O(nlogn) time.*/ + public ListNode sortList(ListNode head) { + if (head == null) { + return head; + } + List list = new ArrayList<>(); + ListNode tmp = head; + while (tmp != null) { + list.add(tmp.val); + tmp = tmp.next; + } + Collections.sort(list); + ListNode pre = new ListNode(-1); + ListNode newHead = new ListNode(list.get(0)); + pre.next = newHead; + for (int i = 1; i < list.size(); i++) { + ListNode next = new ListNode(list.get(i)); + newHead.next = next; + newHead = newHead.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_149.java b/src/main/java/com/fishercoder/solutions/firstthousand/_149.java new file mode 100644 index 0000000000..b97c6962e8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_149.java @@ -0,0 +1,44 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _149 { + + /* + * credits: https://leetcode.com/problems/max-points-on-a-line/discuss/328269/A-Java-solution-with-my-understanding + */ + public static class Solution1 { + public int maxPoints(int[][] points) { + if (points.length < 3) { + return points.length; + } + int max = 0; + Map map = new HashMap<>(); + for (int i = 0; i < points.length; i++) { + int dup = 1; + map.clear(); + for (int j = i + 1; j < points.length; j++) { + int dx = points[j][0] - points[i][0]; + int dy = points[j][1] - points[i][1]; + if (dx == 0 && dy == 0) { + dup++; + } else { + int gcd = getGcd(dx, dy); + long slope = ((long) (dy / gcd) << 32) + (dx / gcd); + map.put(slope, map.getOrDefault(slope, 0) + 1); + } + } + max = Math.max(max, dup); + for (Map.Entry entry : map.entrySet()) { + max = Math.max(max, entry.getValue() + dup); + } + } + return max; + } + + int getGcd(int a, int b) { + return b == 0 ? a : getGcd(b, a % b); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_15.java b/src/main/java/com/fishercoder/solutions/firstthousand/_15.java new file mode 100644 index 0000000000..11aa2e1e9e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_15.java @@ -0,0 +1,44 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _15 { + public static class Solution1 { + public List> threeSum(int[] nums) { + Arrays.sort(nums); + + List> result = new ArrayList<>(); + for (int left = 0; left < nums.length - 2; left++) { + if (left >= 1 && nums[left] == nums[left - 1]) { + continue; + } + int mid = left + 1; + int right = nums.length - 1; + while (mid < right) { + int sum = nums[left] + nums[mid] + nums[right]; + if (sum == 0) { + result.add(Arrays.asList(nums[left], nums[mid], nums[right])); + + while (mid < right && nums[mid] == nums[mid + 1]) { + mid++; + } + + while (mid < right && nums[right] == nums[right - 1]) { + right--; + } + // these two lines are critical and easy to forget, if so, it'll TLE + mid++; + right--; + } else if (sum > 0) { + right--; + } else { + mid++; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_150.java b/src/main/java/com/fishercoder/solutions/firstthousand/_150.java new file mode 100644 index 0000000000..0650657a89 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_150.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.Stack; + +public class _150 { + + public static class Solution1 { + public int evalRPN(String[] tokens) { + Set operators = new HashSet<>(Arrays.asList("+", "-", "*", "/")); + Stack stack = new Stack<>(); + for (String token : tokens) { + if (operators.contains(token)) { + int secondNum = stack.pop(); + int firstNum = stack.pop(); + int result; + if (token.equals("+")) { + result = firstNum + secondNum; + } else if (token.equals("-")) { + result = firstNum - secondNum; + } else if (token.equals("*")) { + result = firstNum * secondNum; + } else { + result = firstNum / secondNum; + } + stack.push(result); + } else { + int num = Integer.parseInt(token); + stack.push(num); + } + } + return stack.pop(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_151.java b/src/main/java/com/fishercoder/solutions/firstthousand/_151.java new file mode 100644 index 0000000000..2e8e248d5a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_151.java @@ -0,0 +1,105 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class _151 { + public static class Solution1 { + public String reverseWords(String s) { + s.trim(); + if (s == null || s.length() == 0) { + return ""; + } + String[] words = s.split(" "); + if (words == null || words.length == 0) { + return ""; + } + Deque stack = new ArrayDeque<>(); + for (String word : words) { + if (!word.equals("")) { + stack.offer(word); + } + } + StringBuilder stringBuilder = new StringBuilder(); + while (!stack.isEmpty()) { + stringBuilder.append(stack.pollLast()).append(" "); + } + return stringBuilder.substring(0, stringBuilder.length() - 1); + } + } + + public static class Solution2 { + public String reverseWords(String s) { + int len = s.length(); + int i = 0; + int j = 0; + String result = ""; + while (i < len) { + + // index i keeps track of the spaces and ignores them if found + while (i < len && s.charAt(i) == ' ') { + i++; + } + if (i == len) { + break; + } + j = i + 1; + + // index j keeps track of non-space characters and gives index of the first + // occurrence of space after a non-space character + while (j < len && s.charAt(j) != ' ') { + j++; + } + // word found + String word = s.substring(i, j); + if (result.length() == 0) { + result = word; + } else { + result = word + " " + result; + } + i = j + 1; + } + return result; + } + } + + public static class Solution3 { + public String reverseWords(String s) { + s = s.trim(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == ' ' && sb.length() > 0 && sb.charAt(sb.length() - 1) == ' ') { + continue; + } else { + sb.append(s.charAt(i)); + } + } + int left = 0; + int right = sb.length() - 1; + while (left < right) { + char tmp = sb.charAt(left); + sb.setCharAt(left, sb.charAt(right)); + sb.setCharAt(right, tmp); + left++; + right--; + } + int boundary = 0; + while (boundary < sb.length()) { + left = boundary; + while (boundary < sb.length() && sb.charAt(boundary) != ' ') { + boundary++; + } + right = boundary - 1; + while (left < right) { + char tmp = sb.charAt(left); + sb.setCharAt(left, sb.charAt(right)); + sb.setCharAt(right, tmp); + left++; + right--; + } + boundary++; + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_152.java b/src/main/java/com/fishercoder/solutions/firstthousand/_152.java new file mode 100644 index 0000000000..ec9c907955 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_152.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.firstthousand; + +public class _152 { + public static class Solution1 { + public int maxProduct(int[] nums) { + int pos = nums[0]; + int neg = nums[0]; + int max = nums[0]; + for (int i = 1; i < nums.length; i++) { + int temp = pos; + pos = Math.max(nums[i], nums[i] * ((nums[i] >= 0) ? pos : neg)); + neg = Math.min(nums[i], nums[i] * ((nums[i] >= 0) ? neg : temp)); + max = Math.max(max, pos); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_153.java b/src/main/java/com/fishercoder/solutions/firstthousand/_153.java new file mode 100644 index 0000000000..952d9458ee --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_153.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +public class _153 { + public static class Solution1 { + /* + * My completely original solution on 10/23/2021. + * Again, using a pen and paper to visualize your thought process, to draw out all the possible cases helps a lot! + */ + public int findMin(int[] nums) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (mid == left || mid == right) { + // this is to avoid infinite loop + break; + } + if (nums[mid] > nums[left] && nums[mid] > nums[right]) { + left = mid + 1; + } else if (nums[mid] < nums[right] && nums[mid] > nums[left]) { + right = mid - 1; + } else if (nums[mid] < nums[left] && nums[mid] < nums[right]) { + right = mid; + } + } + return right >= 0 ? (nums[left] < nums[right] ? nums[left] : nums[right]) : nums[left]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_154.java b/src/main/java/com/fishercoder/solutions/firstthousand/_154.java new file mode 100644 index 0000000000..71a04859b5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_154.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +public class _154 { + public static class Solution1 { + /* + * My completely original solution on 10/23/2021. + * Again, using a pen and paper to visualize all possible cases helps a great deal! + */ + public int findMin(int[] nums) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + while (left + 1 < right && nums[left] == nums[left + 1]) { + left++; + } + while (right - 1 > left && nums[right - 1] == nums[right]) { + right--; + } + int mid = left + (right - left) / 2; + if (mid == left || mid == right) { + break; + } + if (nums[mid] < nums[right]) { + right = mid; + } else if (nums[mid] < nums[left]) { + left = mid; + } else if (nums[mid] > nums[right]) { + left = mid + 1; + } + } + return right >= 0 && nums[right] < nums[left] ? nums[right] : nums[left]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_155.java b/src/main/java/com/fishercoder/solutions/firstthousand/_155.java new file mode 100644 index 0000000000..5afab6579b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_155.java @@ -0,0 +1,85 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.Stack; + +public class _155 { + public static class Solution1 { + public static class MinStack { + private Stack stack; + private int min; + + public MinStack() { + stack = new Stack(); + min = Integer.MAX_VALUE; + } + + public void push(int x) { + if (x <= min) { + /*All the trick happens here, we push the second minimum number onto the stack before we push the newer one, + * this way, when popping, we could always get the next minimum one in constant time.*/ + stack.push(min); + min = x; + } + stack.push(x); + } + + public void pop() { + /*if the value on the top of the stack happens to be the current minimum, we'll pop twice and change + * the current min value to be the last min value */ + if (min == stack.pop()) { + min = stack.pop(); + } + } + + public int top() { + return stack.peek(); + } + + public int getMin() { + return min; + } + } + } + + public static class Solution2 { + /* + * We could store a pair onto the stack: the first element in the pair is the value itself, + * the second element in the pair is the current minimum element so far seen on the stack. + */ + public static class MinStack { + Deque stack; + + public MinStack() { + stack = new LinkedList<>(); + } + + public void push(int val) { + if (!stack.isEmpty()) { + int[] last = stack.peekLast(); + int currentMin = last[1]; + if (val <= currentMin) { + stack.addLast(new int[] {val, val}); + } else { + stack.addLast(new int[] {val, currentMin}); + } + } else { + stack.addLast(new int[] {val, val}); + } + } + + public void pop() { + stack.pollLast(); + } + + public int top() { + return stack.peekLast()[0]; + } + + public int getMin() { + return stack.peekLast()[1]; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_156.java b/src/main/java/com/fishercoder/solutions/firstthousand/_156.java similarity index 95% rename from src/main/java/com/fishercoder/solutions/_156.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_156.java index 9a50d8f0b1..36831b55c5 100644 --- a/src/main/java/com/fishercoder/solutions/_156.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_156.java @@ -1,8 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; -/** +/* * 156. Binary Tree Upside Down * * Given a binary tree where all the right nodes are either leaf nodes with a sibling diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_157.java b/src/main/java/com/fishercoder/solutions/firstthousand/_157.java new file mode 100644 index 0000000000..6232504903 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_157.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _157 { + public static class Solution1 { + public int read(char[] buf, int n) { + int index = 0; + int next = 0; + char[] buffer = new char[4]; + while (index < n && (next = read4(buffer)) != 0) { + for (int i = 0; i < next && index < n; index++, i++) { + buf[index] = buffer[i]; + } + } + return index; + } + + private int read4(char[] buffer) { + // this is a dummy method to make it compile + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_158.java b/src/main/java/com/fishercoder/solutions/firstthousand/_158.java new file mode 100644 index 0000000000..f4809a998e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_158.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +public class _158 { + + public static class Solution1 { + /* + * @param buf Destination buffer + * @param n Maximum number of characters to read + * @return The number of characters read + */ + private int buffPtr = 0; + private int buffCnt = 0; + private char[] buff = new char[4]; + + public int read(char[] buf, int n) { + int ptr = 0; + while (ptr < n) { + if (buffPtr == 0) { + buffCnt = read4(buff); + } + if (buffCnt == 0) { + break; + } + while (ptr < n && buffPtr < buffCnt) { + buf[ptr++] = buff[buffPtr++]; + } + if (buffPtr >= buffCnt) { + buffPtr = 0; + } + } + return ptr; + } + + // This is a fake method to make IDE happy. + private int read4(char[] buff) { + return 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_159.java b/src/main/java/com/fishercoder/solutions/firstthousand/_159.java new file mode 100644 index 0000000000..e42da87b40 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_159.java @@ -0,0 +1,71 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _159 { + public static class Solution1 { + public int lengthOfLongestSubstringTwoDistinct(String s) { + if (s.length() < 1) { + return 0; + } + Map index = new HashMap<>(); + int lo = 0; + int hi = 0; + int maxLength = 0; + while (hi < s.length()) { + if (index.size() <= 2) { + char c = s.charAt(hi); + index.put(c, hi); + hi++; + } + if (index.size() > 2) { + int leftMost = s.length(); + for (int i : index.values()) { + leftMost = Math.min(leftMost, i); + } + char c = s.charAt(leftMost); + index.remove(c); + lo = leftMost + 1; + } + maxLength = Math.max(maxLength, hi - lo); + } + return maxLength; + } + } + + public static class Solution2 { + /* + * My completely original solution, classic sliding window problem: + * use two pointers, one keeps moving towards the right to expand; + * the other moves only when we are no longer meeting the requirement, i.e. shrinks. + */ + public int lengthOfLongestSubstringTwoDistinct(String s) { + int distinct = 0; + int ans = 0; + int left = 0; + int right = 0; + int[] counts = new int[256]; + while (right < s.length()) { + char c1 = s.charAt(right); + if (counts[c1] == 0) { + distinct++; + } + counts[c1]++; + right++; + if (distinct <= 2) { + ans = Math.max(ans, right - left); + } + while (distinct > 2) { + char c2 = s.charAt(left); + counts[c2]--; + if (counts[c2] == 0) { + distinct--; + } + left++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_16.java b/src/main/java/com/fishercoder/solutions/firstthousand/_16.java new file mode 100644 index 0000000000..c7f66e48f9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_16.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _16 { + + public static class Solution1 { + public int threeSumClosest(int[] nums, int target) { + Arrays.sort(nums); + int sum = nums[0] + nums[1] + nums[2]; + for (int i = 0; i < nums.length - 2; i++) { + int left = i + 1; + int right = nums.length - 1; + while (left < right) { + int thisSum = nums[i] + nums[left] + nums[right]; + if (Math.abs(thisSum - target) < Math.abs(sum - target)) { + sum = thisSum; + if (sum == target) { + return sum; + } + } else if (target > thisSum) { + left++; + } else { + right--; + } + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_160.java b/src/main/java/com/fishercoder/solutions/firstthousand/_160.java new file mode 100644 index 0000000000..0f23f90f1b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_160.java @@ -0,0 +1,92 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.HashSet; +import java.util.Set; + +public class _160 { + + public static class Solution1 { + /* + * Time: O(max(m, n)) + * Space: O(1) + */ + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + int lenA = findLen(headA); + int lenB = findLen(headB); + /*align headA and headB to the same starting point and then move together until we find the intersection point*/ + while (lenA < lenB) { + headB = headB.next; + lenB--; + } + + while (lenB < lenA) { + headA = headA.next; + lenA--; + } + + while (headA != headB) { + headA = headA.next; + headB = headB.next; + } + + return headA; + } + + private int findLen(ListNode head) { + int len = 0; + while (head != null) { + head = head.next; + len++; + } + return len; + } + } + + public static class Solution2 { + /* + * Most optimal solution: + * O(m+n) time + * O(1) space + * credit: https://discuss.leetcode.com/topic/28067/java-solution-without-knowing-the-difference-in-len + */ + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + if (headA == null || headB == null) { + return null; + } + + ListNode a = headA; + ListNode b = headB; + + /*if a and b have different lengths, then it will stop the loop after second iteration*/ + while (a != b) { + /*for the first iteration, it'll just reset the pointer to the head of another linkedlist*/ + a = a == null ? headB : a.next; + b = b == null ? headA : b.next; + } + return a; + } + } + + public static class Solution3 { + /* + * O(m+n) time + * O(Math.max(m, n)) space + */ + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + Set set = new HashSet<>(); + while (headA != null) { + set.add(headA); + headA = headA.next; + } + + while (headB != null) { + if (set.contains(headB)) { + return headB; + } + headB = headB.next; + } + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_161.java b/src/main/java/com/fishercoder/solutions/firstthousand/_161.java new file mode 100644 index 0000000000..54244bacf6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_161.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +public class _161 { + public static class Solution1 { + public boolean isOneEditDistance(String s, String t) { + char[] schar = s.toCharArray(); + char[] tchar = t.toCharArray(); + + if (Math.abs(s.length() - t.length()) == 1) { + char[] longer = (s.length() > t.length()) ? schar : tchar; + char[] shorter = (longer == schar) ? tchar : schar; + + int diffCnt = 0; + int i = 0; + int j = 0; + for (; i < shorter.length && j < longer.length; ) { + if (longer[j] != shorter[i]) { + diffCnt++; + j++; + } else { + i++; + j++; + } + } + return diffCnt == 1 || diffCnt == 0; + // it could be the last char of the longer is the different one, in that case, + // diffCnt remains to be zero + } else if (s.length() == t.length()) { + int diffCnt = 0; + for (int i = 0; i < s.length(); i++) { + if (schar[i] != tchar[i]) { + diffCnt++; + } + if (diffCnt > 1) { + return false; + } + } + return diffCnt == 1; + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_162.java b/src/main/java/com/fishercoder/solutions/firstthousand/_162.java new file mode 100644 index 0000000000..f1f571cea3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_162.java @@ -0,0 +1,62 @@ +package com.fishercoder.solutions.firstthousand; + +public class _162 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/find-peak-element/solutions/1290642/intuition-behind-conditions-complete-explanation-diagram-binary-search/ + * Time: O(logn) + *

+ * draw three cases with three examples, it's pretty self-explanatory: + * case 1: + * mid + * 1, 3, 2 + * case 2: + * mid + * 1, 2, 3 + * so peak should be on the right side, so code is: left = mid + 1 + * case 3: + * mid + * 3, 2, 1 + * so peak should be on the left side, so code is: right = mid - 1; + */ + public int findPeakElement(int[] nums) { + if (nums == null || nums.length <= 1 || nums[0] > nums[1]) { + return 0; + } + if (nums[nums.length - 1] > nums[nums.length - 2]) { + return nums.length - 1; + } + int left = 1; + int right = nums.length - 2; + while (left <= right) { + int mid = left + (right - left) / 2; + if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) { + return mid; + } else if (nums[mid] < nums[mid - 1]) { + right = mid - 1; + } else if (nums[mid] < nums[mid + 1]) { + left = mid + 1; + } + } + return -1; + } + } + + public static class Solution2 { + /* + * My original O(n) solution. + */ + public int findPeakElement(int[] nums) { + for (int i = 1; i < nums.length; i++) { + if (nums[i] > nums[i - 1] && i + 1 < nums.length && nums[i] > nums[i + 1]) { + return i; + } + if (i == nums.length - 1 && nums[i] > nums[i - 1]) { + return i; + } + } + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_163.java b/src/main/java/com/fishercoder/solutions/firstthousand/_163.java new file mode 100644 index 0000000000..d8910747a9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_163.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _163 { + public static class Solution1 { + public List> findMissingRanges(int[] nums, int lower, int upper) { + List> missingRanges = new ArrayList<>(); + if (nums == null || nums.length == 0) { + missingRanges.add(Arrays.asList(lower, upper)); + return missingRanges; + } + // check for missing numbers between lower and nums[0] + if (lower < nums[0]) { + missingRanges.add(Arrays.asList(lower, nums[0] - 1)); + } + // check for missing numbers between nums + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] + 1 == nums[i + 1]) { + continue; + } + missingRanges.add(Arrays.asList(nums[i] + 1, nums[i + 1] - 1)); + } + // check for any missing numbers between nums[n - 1] and upper + if (nums[nums.length - 1] < upper) { + missingRanges.add(Arrays.asList(nums[nums.length - 1] + 1, upper)); + } + return missingRanges; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_164.java b/src/main/java/com/fishercoder/solutions/firstthousand/_164.java new file mode 100644 index 0000000000..48c1abc19f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_164.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +/* + * 164. Maximum Gap + * + * Given an unsorted array, find the maximum difference between the successive elements in its sorted form. + * Return 0 if the array contains less than 2 elements. + * + * Example 1: + * Input: [3,6,9,1] + * Output: 3 + * Explanation: The sorted form of the array is [1,3,6,9], either + * (3,6) or (6,9) has the maximum difference 3. + * + * Example 2: + * Input: [10] + * Output: 0 + * Explanation: The array contains less than 2 elements, therefore return 0. + * + * Note: + * You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range. + * Try to solve it in linear time/space. + */ +public class _164 { + public static class Solution1 { + /* brute force solution */ + public int maximumGap(int[] nums) { + if (nums.length < 2) { + return 0; + } + + Arrays.sort(nums); + int max = Integer.MIN_VALUE; + for (int i = 1; i < nums.length; ) { + while (i < nums.length && nums[i] == nums[i - 1]) { + i++; + } + if (i == nums.length) { + i--; + max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max; + break; + } else { + max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max; + } + if (nums[i] != nums[i - 1]) { + i++; + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_165.java b/src/main/java/com/fishercoder/solutions/firstthousand/_165.java new file mode 100644 index 0000000000..f8814716a9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_165.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.firstthousand; + +public class _165 { + public static class Solution1 { + public int compareVersion(String version1, String version2) { + String[] v1s = + version1.split( + "\\."); // escaping it is very important! Otherwise, it's not going to + // work as expected! + String[] v2s = version2.split("\\."); + int len = (v1s.length < v2s.length) ? v2s.length : v1s.length; + for (int i = 0; i < len; i++) { + if (v1s.length == i) { + while (i < len) { + if (Integer.parseInt(v2s[i]) > 0) { + return -1; + } + i++; + } + } else if (v2s.length == i) { + while (i < len) { + if (Integer.parseInt(v1s[i]) > 0) { + return 1; + } + i++; + } + } else { + if (Integer.parseInt(v1s[i]) > Integer.parseInt(v2s[i])) { + return 1; + } else if (Integer.parseInt(v2s[i]) > Integer.parseInt(v1s[i])) { + return -1; + } + } + } + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_166.java b/src/main/java/com/fishercoder/solutions/firstthousand/_166.java new file mode 100644 index 0000000000..6f688f81b9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_166.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +/* +* 166. Fraction to Recurring Decimal +* +* Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. + +If the fractional part is repeating, enclose the repeating part in parentheses. + +For example, + +Given numerator = 1, denominator = 2, return "0.5". +Given numerator = 2, denominator = 1, return "2". +Given numerator = 2, denominator = 3, return "0.(6)". + +*/ +public class _166 { + public static class Solution1 { + /* credit: https://discuss.leetcode.com/topic/33311/simple-and-short-solution-in-java */ + public String fractionToDecimal(int numerator, int denominator) { + String sign = + (numerator >= 0 && denominator >= 0) || (numerator < 0 && denominator < 0) + ? "" + : "-"; + if (numerator == 0) { + return "0"; + } + long num = Math.abs((long) numerator); + long deno = Math.abs((long) denominator); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(sign); + long integral = Math.abs(num / deno); + stringBuilder.append(integral); + if (numerator % denominator == 0) { + return stringBuilder.toString(); + } else { + stringBuilder.append("."); + } + long remainder = num % deno; + + Map map = new HashMap<>(); + while (!map.containsKey(remainder)) { + map.put(remainder, stringBuilder.length()); + long n = remainder * 10 / deno; + remainder = remainder * 10 % deno; + if (remainder != 0 || (remainder == 0 && !map.containsKey(remainder))) { + stringBuilder.append(n); + } + } + if (remainder != 0) { + stringBuilder.insert(map.get(remainder), "("); + stringBuilder.append(")"); + } + return stringBuilder.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_167.java b/src/main/java/com/fishercoder/solutions/firstthousand/_167.java new file mode 100644 index 0000000000..192ce26899 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_167.java @@ -0,0 +1,85 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _167 { + public static class Solution1 { + /* + * This is an amazing solution! + * Time: O(logn) + */ + public int[] twoSum(int[] numbers, int target) { + int left = 0; + int right = numbers.length - 1; + while (left < right) { + long sum = numbers[left] + numbers[right]; + if (sum > target) { + right--; + } else if (sum < target) { + left++; + } else { + return new int[] {left + 1, right + 1}; + } + } + return new int[] {-1, -1}; + } + } + + public static class Solution2 { + /* + * Time: O(nlogn) + */ + public int[] twoSum(int[] numbers, int target) { + for (int i = 0; i < numbers.length - 1; i++) { + int index = + exists( + Arrays.copyOfRange(numbers, i + 1, numbers.length), + target - numbers[i]); + if (index >= 0) { + return new int[] {i + 1, index + 2 + i}; + } + } + return null; + } + + private int exists(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) { + return mid; + } else if (nums[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return nums[left] == target ? left : (right >= 0 && nums[right] == target) ? right : -1; + } + } + + public static class Solution3 { + /* + * Time: O(nlogn) + */ + public int[] twoSum(int[] numbers, int target) { + for (int i = 0; i < numbers.length - 1; i++) { + int[] ans = new int[2]; + ans[0] = i + 1; + int index = + Arrays.binarySearch( + Arrays.copyOfRange(numbers, i, numbers.length), + target - numbers[i]); + if (index > 0) { + ans[1] = index + 1 + i; + return ans; + } else if (index == 0) { + ans[1] = index + 2 + i; + return ans; + } + } + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_168.java b/src/main/java/com/fishercoder/solutions/firstthousand/_168.java new file mode 100644 index 0000000000..01fe68662b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_168.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +/* +* +* 168. Excel Sheet Column Title + +Given a positive integer, return its corresponding column title as appear in an Excel sheet. + +For example: + +1 -> A +2 -> B +3 -> C +... +26 -> Z +27 -> AA +28 -> AB +... + +Example 1: + +Input: 1 +Output: "A" + +Example 2: + +Input: 28 +Output: "AB" + +Example 3: + +Input: 701 +Output: "ZY" + +*/ +public class _168 { + public static class Solution1 { + public String convertToTitle(int n) { + /*Get the right most digit first, move to the left, e.g. when n = 28, we get 'B' first, then we get 'A'.*/ + StringBuilder sb = new StringBuilder(); + while (n != 0) { + int temp = (n - 1) % 26; + sb.append((char) (temp + 65)); + n = (n - 1) / 26; + } + return sb.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_169.java b/src/main/java/com/fishercoder/solutions/firstthousand/_169.java new file mode 100644 index 0000000000..66e9a86fcb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_169.java @@ -0,0 +1,77 @@ +package com.fishercoder.solutions.firstthousand; + +public class _169 { + public static class Solution1 { + /* + * Moore Voting Algorithm + * How to understand this: + * 1. For a number to qualify as a majority element, it needs to occur more than 1/2 times, which + * means there are a max of only one such element in any given array. + * 2. E.g. given this array [1,2,3,1,1,1], two of the 1s will be balanced off by 2 and 3, still there are two 1s remaining in the end + * which is the majority element + */ + public int majorityElement(int[] nums) { + int count = 1; + int majority = nums[0]; + for (int i = 1; i < nums.length; i++) { + if (count == 0) { + count++; + majority = nums[i]; + } else if (nums[i] == majority) { + count++; + } else { + count--; + } + } + return majority; + } + } + + public static class Solution2 { + // bit manipulation + public int majorityElement(int[] nums) { + int[] bit = new int[32]; // because an integer is 32 bits, so we use an array of 32 long + for (int num : nums) { + for (int i = 0; i < 32; i++) { + if ((num >> (31 - i) & 1) == 1) { + bit[i]++; // this is to compute each number's ones frequency + } + } + } + int res = 0; + // this below for loop is to construct the majority element: since every bit of this + // element would have appeared more than n/2 times + for (int i = 0; i < 32; i++) { + bit[i] = + bit[i] > nums.length / 2 + ? 1 + : 0; // we get rid of those that bits that are not part of the + // majority number + res += bit[i] * (1 << (31 - i)); + } + return res; + } + } + + public static class Solution3 { + /* + * I'm glad to have come up with this idea myself on 10/12/2021. + */ + public int majorityElement(int[] nums) { + int count = 1; + int candidate = nums[0]; + for (int i = 1; i < nums.length; i++) { + if (nums[i] != candidate) { + count--; + if (count < 0) { + candidate = nums[i]; + count = 1; + } + } else { + count++; + } + } + return candidate; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_17.java b/src/main/java/com/fishercoder/solutions/firstthousand/_17.java new file mode 100644 index 0000000000..cbd8bbc480 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_17.java @@ -0,0 +1,99 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _17 { + + public static class Solution1 { + public List letterCombinations(String digits) { + List result = new ArrayList(); + if (digits.length() == 0) { + return result; + } + + String[] digits2Letters = + new String[] {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + + result.add( + ""); // this line is important, otherwise result is empty and Java will default + // it to an empty String + for (int i = 0; i < digits.length(); i++) { + result = combine(digits2Letters[digits.charAt(i) - '0'], result); + } + + return result; + } + + List combine(String letters, List result) { + List newResult = new ArrayList(); + + for (int i = 0; i < letters.length(); i++) { + // the order of the two for loops doesn't matter, you could swap them and it still + // works. + for (String str : result) { + newResult.add(str + letters.charAt(i)); + } + } + return newResult; + } + } + + public static class Solution2 { + /* + * It's recommended to use recursion to solve this problem, I got this feedback from a Meta interviewer on 6/17/2024 during the mock interview. + * My completely original solution on 10/11/2021, no backtracking involved. + */ + public List letterCombinations(String digits) { + List ans = new ArrayList<>(); + if (digits.length() == 0 || digits.equals("")) { + return ans; + } + String[] options = + new String[] {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + ans.add(""); + return recursion(ans, options, digits, 0); + } + + private List recursion( + List ans, String[] options, String digits, int index) { + if (index >= digits.length()) { + return ans; + } + List newAns = new ArrayList<>(); + String candidates = options[digits.charAt(index) - '0']; + for (String str : ans) { + for (int i = 0; i < candidates.length(); i++) { + newAns.add(str + candidates.charAt(i)); + } + } + return recursion(newAns, options, digits, index + 1); + } + } + + public static class Solution3 { + /* + * My completely original solution on 5/9/2022, no backtracking involved or helper method involved. + */ + public List letterCombinations(String digits) { + List ans = new ArrayList<>(); + if (digits.equals("")) { + return ans; + } + String[] buttons = + new String[] {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + ans.add(""); + for (char c : digits.toCharArray()) { + String button = buttons[Integer.parseInt(c + "")]; + List newList = new ArrayList<>(); + for (String str : ans) { + for (char b : button.toCharArray()) { + newList.add(str + b); + } + } + ans = newList; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_170.java b/src/main/java/com/fishercoder/solutions/firstthousand/_170.java new file mode 100644 index 0000000000..181d52adb9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_170.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _170 { + public static class Solution1 { + class TwoSum { + + private Map map; + private List list; + + /* + * Initialize your data structure here. + */ + public TwoSum() { + map = new HashMap(); + list = new ArrayList(); + } + + // Add the number to an internal data structure. + public void add(int number) { + list.add(number); + map.put(number, map.getOrDefault(number, 0) + 1); + } + + // Find if there exists any pair of numbers which sum is equal to the value. + public boolean find(int value) { + for (int i = 0; i < list.size(); i++) { + int val1 = list.get(i); + int val2 = value - val1; + if (map.containsKey(val2)) { + if (val1 == val2) { + if (map.get(val2) > 1) { + return true; + } + } else { + return true; + } + } + } + return false; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_171.java b/src/main/java/com/fishercoder/solutions/firstthousand/_171.java new file mode 100644 index 0000000000..93d8878d03 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_171.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.firstthousand; + +public class _171 { + + public static class Solution1 { + public int titleToNumber(String s) { + char[] c = s.toCharArray(); + int result = 0; + for (int i = s.length() - 1; i >= 0; i--) { + result += + (c[i] - 64) + * ((int) + Math.pow( + 26, + s.length() - i - 1)); // The ASCII value of A is 65 + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_172.java b/src/main/java/com/fishercoder/solutions/firstthousand/_172.java similarity index 92% rename from src/main/java/com/fishercoder/solutions/_172.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_172.java index 77019242a8..2b0af27ddc 100644 --- a/src/main/java/com/fishercoder/solutions/_172.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_172.java @@ -1,6 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; -/** +/* * 172. Factorial Trailing Zeroes * * Given an integer n, return the number of trailing zeroes in n!. diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_173.java b/src/main/java/com/fishercoder/solutions/firstthousand/_173.java new file mode 100644 index 0000000000..128e5bcba4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_173.java @@ -0,0 +1,106 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.Deque; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Stack; + +public class _173 { + + public static class Solution1 { + + public static class BSTIterator { + private Queue queue; + + public BSTIterator(TreeNode root) { + queue = new LinkedList<>(); + if (root != null) { + dfs(root, queue); + } + } + + private void dfs(TreeNode root, Queue q) { + if (root.left != null) { + dfs(root.left, q); + } + q.offer(root.val); + if (root.right != null) { + dfs(root.right, q); + } + } + + public boolean hasNext() { + return !queue.isEmpty(); + } + + public int next() { + return queue.poll(); + } + } + } + + public static class Solution2 { + public static class BSTIterator { + /* + * This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we + * push all its right nodes into the stack if there are any. + * This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge + * since h could be much smaller than n. Cheers! + */ + + private Stack stack; + + public BSTIterator(TreeNode root) { + stack = new Stack(); + pushToStack(root, stack); + } + + private void pushToStack(TreeNode root, Stack stack) { + while (root != null) { + stack.push(root); + root = root.left; + } + } + + public boolean hasNext() { + return !stack.isEmpty(); + } + + public int next() { + TreeNode curr = stack.pop(); + pushToStack(curr.right, stack); + return curr.val; + } + } + } + + public static class Solution3 { + /* + * credit: https://leetcode.com/problems/binary-search-tree-iterator/discuss/52647/Nice-Comparison-(and-short-Solution + */ + public static class BSTIterator { + Deque stack; + TreeNode visit; + + public BSTIterator(TreeNode root) { + stack = new LinkedList<>(); + visit = root; + } + + public int next() { + while (visit != null) { + stack.addLast(visit); + visit = visit.left; + } + TreeNode next = stack.pollLast(); + visit = next.right; + return next.val; + } + + public boolean hasNext() { + return visit != null && !stack.isEmpty(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_174.java b/src/main/java/com/fishercoder/solutions/firstthousand/_174.java new file mode 100644 index 0000000000..3bde9a0cf8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_174.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; + +public class _174 { + + public static class Solution1 { + /* + * This problem should fill the dp matrix from bottom right. + */ + public int calculateMinimumHP(int[][] dungeon) { + if (dungeon == null || dungeon.length == 0) { + return 0; + } + + int height = dungeon.length; + int width = dungeon[0].length; + int[][] dp = new int[height][width]; + dp[height - 1][width - 1] = + (dungeon[height - 1][width - 1] > 0) ? 1 : 1 - dungeon[height - 1][width - 1]; + + // fill the last column + for (int i = height - 2; i >= 0; i--) { + int temp = dp[i + 1][width - 1] - dungeon[i][width - 1]; + dp[i][width - 1] = Math.max(1, temp); + } + + // fill the last row + for (int j = width - 2; j >= 0; j--) { + int temp = dp[height - 1][j + 1] - dungeon[height - 1][j]; + dp[height - 1][j] = Math.max(temp, 1); + } + + for (int i = height - 2; i >= 0; i--) { + for (int j = width - 2; j >= 0; j--) { + int down = Math.max(1, dp[i + 1][j] - dungeon[i][j]); + int right = Math.max(1, dp[i][j + 1] - dungeon[i][j]); + dp[i][j] = Math.min(down, right); + } + } + + CommonUtils.printMatrix(dp); + return dp[0][0]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_179.java b/src/main/java/com/fishercoder/solutions/firstthousand/_179.java new file mode 100644 index 0000000000..a02fdcde75 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_179.java @@ -0,0 +1,90 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.*; + +public class _179 { + + public static class Solution1 { + public String largestNumber(int[] num) { + if (num.length == 0) { + return ""; + } + if (num.length == 1) { + return Integer.toString(num[0]); + } + String[] str = new String[num.length]; + for (int i = 0; i < num.length; i++) { + str[i] = Integer.toString(num[i]); + } + Arrays.sort(str, new StringComparator()); + StringBuilder sb = new StringBuilder(""); + for (int i = num.length - 1; i >= 0; i--) { + sb.append(str[i]); + } + if (sb.charAt(0) == '0') { + return "0"; + } + return sb.toString(); + } + + class StringComparator implements Comparator { + public int compare(String s1, String s2) { + if (s1.length() == 0 && s2.length() == 0) { + return 0; + } + if (s2.length() == 0) { + return 1; + } + if (s1.length() == 0) { + return -1; + } + for (int i = 0; i < s1.length() && i < s2.length(); i++) { + if (s1.charAt(i) > s2.charAt(i)) { + return 1; + } else if (s1.charAt(i) < s2.charAt(i)) { + return -1; + } + } + if (s1.length() == s2.length()) { + return 0; + } + if (s1.length() > s2.length()) { + if (s1.charAt(0) < s1.charAt(s2.length())) { + return 1; + } else if (s1.charAt(0) > s1.charAt(s2.length())) { + return -1; + } else { + return compare(s1.substring(s2.length()), s2); + } + } else { + if (s2.charAt(0) < s2.charAt(s1.length())) { + return -1; + } else if (s2.charAt(0) > s2.charAt(s1.length())) { + return 1; + } else { + return compare(s1, s2.substring(s1.length())); + } + } + } + } + } + + public static class Solution2 { + /* + * My completely original solution on 11/29/2021: + * we'll just sort these numbers as if they are strings. + */ + public String largestNumber(int[] nums) { + List strings = new ArrayList<>(); + for (int num : nums) { + strings.add(Integer.toString(num)); + } + Collections.sort(strings, (a, b) -> (b + a).compareToIgnoreCase(a + b)); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < strings.size(); i++) { + sb.append(strings.get(i)); + } + return sb.charAt(0) == '0' ? "0" : sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_18.java b/src/main/java/com/fishercoder/solutions/firstthousand/_18.java new file mode 100644 index 0000000000..ae1f794fe7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_18.java @@ -0,0 +1,75 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _18 { + + public static class Solution1 { + public List> fourSum(int[] nums, int target) { + List> result = new ArrayList(); + if (nums == null || nums.length == 0) { + return result; + } + Arrays.sort(nums); + for (int i = 0; i < nums.length - 3; i++) { + if (i > 0 && nums[i - 1] == nums[i]) { + continue; + } + for (int j = i + 1; j < nums.length - 2; j++) { + if (j > i + 1 && nums[j - 1] == nums[j]) { + continue; + } + int left = j + 1; + int right = nums.length - 1; + while (left < right) { + int sum = nums[i] + nums[j] + nums[left] + nums[right]; + if (sum == target) { + result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); + while (left + 1 < right && nums[left] == nums[left + 1]) { + left++; + } + while (right - 1 > left && nums[right] == nums[right - 1]) { + right--; + } + left++; + right--; + } else if (sum > target) { + right--; + } else { + left++; + } + } + } + } + return result; + } + } + + public static class Solution2 { + public List> fourSum(int[] nums, int target) { + Arrays.sort(nums); + Set> set = new HashSet<>(); + for (int i = 0; i < nums.length - 3; i++) { + for (int j = i + 1; j < nums.length - 2; j++) { + for (int k = j + 1; k < nums.length - 1; k++) { + for (int p = k + 1; p < nums.length; p++) { + int sum = nums[i] + nums[j] + nums[k] + nums[p]; + if (sum == target) { + set.add(Arrays.asList(nums[i], nums[j], nums[k], nums[p])); + } + } + } + } + } + List> result = new ArrayList<>(); + for (List each : set) { + result.add(each); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_186.java b/src/main/java/com/fishercoder/solutions/firstthousand/_186.java new file mode 100644 index 0000000000..3673c4d900 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_186.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.firstthousand; + +public class _186 { + public static class Solution1 { + public void reverseWords(char[] s) { + // Three steps to reverse + // 1, reverse the whole sentence + reverse(s, 0, s.length - 1); + // 2, reverse each word + int start = 0; + for (int i = 0; i < s.length; i++) { + if (s[i] == ' ') { + reverse(s, start, i - 1); + start = i + 1; + } + } + // 3, reverse the last word, if there is only one word this will solve the corner case + reverse(s, start, s.length - 1); + } + + private void reverse(char[] s, int start, int end) { + while (start < end) { + char temp = s[start]; + s[start++] = s[end]; + s[end--] = temp; + } + } + } + + public static class Solution2 { + public void reverseWords(char[] s) { + reverse(s, 0, s.length); + for (int i = 0; i < s.length; i++) { + int start = i; + while (i < s.length && s[i] != ' ') { + i++; + } + reverse(s, start, i); + } + } + + private void reverse(char[] chars, int start, int end) { + int left = start; + int right = end - 1; + while (left < right) { + char tmp = chars[left]; + chars[left] = chars[right]; + chars[right] = tmp; + left++; + right--; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_187.java b/src/main/java/com/fishercoder/solutions/firstthousand/_187.java new file mode 100644 index 0000000000..a2ab4a8db8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_187.java @@ -0,0 +1,69 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class _187 { + public static class Solution1 { + public List findRepeatedDnaSequences(String s) { + Map map = new HashMap(); + for (int i = 0; i < s.length() - 9; i++) { + String sequence = s.substring(i, i + 10); + map.put(sequence, map.getOrDefault(sequence, 0) + 1); + } + List repeatedSequences = new ArrayList<>(); + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() > 1) { + repeatedSequences.add(entry.getKey()); + } + } + return repeatedSequences; + } + } + + public static class Solution2 { + /* + * Use Rolling Hash/Rabin-Karp algorithm to significantly speed up the search. + *

+ * Rolling Hash/Rabin-Karp algorithm: + * Instead of comparing the entire string to the other, we compare only the hash after adding the incoming character + * and removing the outgoing character, this could be done in constant time. + * Back to this problem, since there are only 4 characters, we only need 2 bits to represent each character: + * 00 -> A + * 01 -> C + * 10 -> G + * 11 -> T + * so for a DNA sequence that is 10 character long, a total of 10 * 2 = 20 bits is good enough, this is much smaller than + * an Integer (32-bit) in most modern programming languages, so using one integer could well represent one DNA sequence. + * Thus we could do bit manipulation to implement the removal of the outgoing character and the addition of the incoming character. + *

+ * <<= 2 will shift the integer to the left, i.e. removing the outgoing character; + * |= val will add the incoming character. + */ + public List findRepeatedDnaSequences(String s) { + Set seen1stTime = new HashSet<>(); + Set seen2ndTime = new HashSet<>(); + List ans = new ArrayList<>(); + char[] map = new char[26]; + map['A' - 'A'] = 0; + map['C' - 'A'] = 1; + map['G' - 'A'] = 2; + map['T' - 'A'] = 3; + for (int i = 0; i < s.length() - 9; i++) { + int hash = 0; + for (int j = i; j < i + 10; j++) { + hash <<= 2; + hash |= map[s.charAt(j) - 'A']; + } + if (!seen1stTime.add(hash) && seen2ndTime.add(hash)) { + ans.add(s.substring(i, i + 10)); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_188.java b/src/main/java/com/fishercoder/solutions/firstthousand/_188.java new file mode 100644 index 0000000000..ba48f056b0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_188.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +public class _188 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/8984/a-concise-dp-solution-in-java + */ + public int maxProfit(int k, int[] prices) { + int len = prices.length; + if (k >= len / 2) { + return quickSolve(prices); + } + + int[][] t = new int[k + 1][len]; + for (int i = 1; i <= k; i++) { + int tmpMax = -prices[0]; + for (int j = 1; j < len; j++) { + t[i][j] = Math.max(t[i][j - 1], prices[j] + tmpMax); + tmpMax = Math.max(tmpMax, t[i - 1][j - 1] - prices[j]); + } + } + return t[k][len - 1]; + } + + private int quickSolve(int[] prices) { + int len = prices.length; + int profit = 0; + for (int i = 1; i < len; i++) { + // as long as there is a price gap, we gain a profit. + if (prices[i] > prices[i - 1]) { + profit += prices[i] - prices[i - 1]; + } + } + return profit; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_189.java b/src/main/java/com/fishercoder/solutions/firstthousand/_189.java new file mode 100644 index 0000000000..3bb28f59de --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_189.java @@ -0,0 +1,89 @@ +package com.fishercoder.solutions.firstthousand; + +public class _189 { + + public static class Solution1 { + /* + * O(n*k) time + * O(1) space + */ + public void rotate(int[] nums, int k) { + int tmp; + for (int i = 0; i < k; i++) { + tmp = nums[nums.length - 1]; + for (int j = nums.length - 1; j > 0; j--) { + nums[j] = nums[j - 1]; + } + nums[0] = tmp; + } + } + } + + public static class Solution2 { + /* + * using an extra array of the same size to copy it + * O(n) time + * O(n) space + */ + public void rotate(int[] nums, int k) { + int len = nums.length; + int[] tmp = new int[len]; + for (int i = 0; i < len; i++) { + tmp[(i + k) % len] = nums[i]; + } + for (int i = 0; i < len; i++) { + nums[i] = tmp[i]; + } + } + } + + public static class Solution3 { + /* + * reverse three times + * O(n) time + * O(1) space + */ + public void rotate(int[] nums, int k) { + int len = nums.length; + k %= len; + reverse(nums, 0, len - 1); + reverse(nums, 0, k - 1); + reverse(nums, k, len - 1); + } + + private void reverse(int[] nums, int start, int end) { + while (start < end) { + int tmp = nums[start]; + nums[start] = nums[end]; + nums[end] = tmp; + start++; + end--; + } + } + } + + public static class Solution4 { + /* + * O(n) time + * O(1) space + * The most optimal, we can safely ignore all the above three solutions... :) + * Credit: https://leetcode.com/problems/rotate-array/solution/ Approach #3 + */ + public void rotate(int[] nums, int k) { + k = k % nums.length; + int count = 0; + for (int start = 0; count < nums.length; start++) { + int current = start; + int prev = nums[start]; + do { + int nextIndex = (current + k) % nums.length; + int tmp = nums[nextIndex]; + nums[nextIndex] = prev; + prev = tmp; + current = nextIndex; + count++; + } while (start != current); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_19.java b/src/main/java/com/fishercoder/solutions/firstthousand/_19.java new file mode 100644 index 0000000000..0afc091a7e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_19.java @@ -0,0 +1,103 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _19 { + + public static class Solution1 { + /* + * Naive/most straightforward approach: + * go through the list, find its total length, then go through the list a second time: + * this time, pause at the delta point, then assign its next.next pointer to next. + * This approach has to traverse the list twice, not one-pass. + */ + public ListNode removeNthFromEnd(ListNode head, int n) { + ListNode temp = head; + int len = 0; + while (temp != null) { + temp = temp.next; + len++; + } + if (n == len) { + return head.next; + } + + temp = head; + int cut = len - n; + while (cut-- > 1) { + temp = temp.next; + } + if (temp.next != null) { + temp.next = temp.next.next; + return head; + } + return null; + } + } + + public static class Solution2 { + public ListNode removeNthFromEnd(ListNode head, int n) { + // this approach uses two pointers, fast moves first for n nodes, when fast reaches n, + // then we start to move slow + // then, when fast reaches null, slow reaches the point where the node should be + // deleted. + ListNode dummy = new ListNode(-1); + dummy.next = head; + ListNode slow = head; + ListNode fast = head; + int tempN = n; + while (tempN-- > 0) { + fast = fast.next; + } + + if (fast == null) { + if (n > 0) { + // this is for cases like this: [1,2] 2 or [1,2,3,4] 4, namely, remove the head + // of + // the list and return the second node from the original list + dummy.next = dummy.next.next; + } + return dummy.next; + } + + fast = fast.next; // we'll have to move fast pointer one node forward before moving the + // two together, this way, + // when fast reaches null, slow will be at the previous node to the node that should be + // deleted, thus, we can change the next pointer easily + + while (fast != null) { + fast = fast.next; + slow = slow.next; + } + + if (slow.next != null) { + slow.next = slow.next.next; + } + return dummy.next; + } + } + + public static class Solution3 { + // a more concise version using the same idea + // i.e. sliding window + // Time: O(n) + // Space: O(1) + public ListNode removeNthFromEnd(ListNode head, int n) { + ListNode pre = new ListNode(-1); + pre.next = head; + ListNode slow = pre; + ListNode fast = pre; + while (fast.next != null) { + if (n <= 0) { + slow = slow.next; + } + fast = fast.next; + n--; + } + if (slow.next != null) { + slow.next = slow.next.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_190.java b/src/main/java/com/fishercoder/solutions/firstthousand/_190.java new file mode 100644 index 0000000000..06618ed5ae --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_190.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.firstthousand; + +public class _190 { + /*delimiting the binary string into 4 bits array will make it easier to see/visualize: + * original binary format: + * 0000,0010,1001,0100,0001,1110,1001,1100, + * after reversing, becomes: + * 0011,1001,0111,1000,0010,1001,0100,0000 + * The most right side digit shifted to the most left side, the 2nd right side digit shifted to the 2nd left side, so forth..*/ + + /* + * This post: http://stackoverflow.com/questions/2811319/difference-between-and + * gives a good explanation between logical right shift: ">>>" and arithmetic right shift: ">>". + * Basically, ">>" preserves the most left bit and treats it as the sign for this number, + * e.g. -2 represented in 8 bits is 11111110, thus -2 >> 1 will become 11111111, i.e. -1 + * notice its sign bit (the most left one bit) is preserved + * However, logical right shift ">>>" doesn't care about the first bit on the most left, + * it simply shifts every bit to the right. + * e.g. -2 >>> 1 would become 1111111111111111111111111111111, i.e. 2147483647 + */ + + public static class Solution1 { + // you need treat n as an unsigned value + public int reverseBits(int n) { + int res = 0; + for (int i = 0; i < 32; i++) { + res += n & 1; // get the most right bit each time + n >>>= 1; // do UN-signed right shift by 1 each time + // n >>= 1;//this line works as well on LeetCode OJ, choosing either one works + if (i < 31) { + res <<= 1; // shift this number to the left by 1 each time, so that eventually, + // this number is reversed + } + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_191.java b/src/main/java/com/fishercoder/solutions/firstthousand/_191.java new file mode 100644 index 0000000000..f20cf2a3fd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_191.java @@ -0,0 +1,67 @@ +package com.fishercoder.solutions.firstthousand; + +public class _191 { + + public static class Solution1 { + /* + * Doing bitwise AND operation between n and n-1 will always flip the least significant 1 bit in n to zero + * example run for the above editorial solution: input 5, n will be 5&4 and becomes 4, + * then in the next run, n will become 4&3 which is 0, thus exit the while loop. + * + * e.g. 5 in binary format: 101 + * 4 in binary format: 100 + * so 5 AND 4 becomes 100, it's the least significant 1-bit in n always maps to a 0-bit in n - 1 + * so n ANDs with n - 1, will just set the least significant 1-bit in n to be zero and all other bits remain unchanged + */ + public int hammingWeight(int n) { + int bits = 0; + while (n != 0) { + bits++; + n &= (n - 1); + } + return bits; + } + } + + public static class Solution2 { + public int hammingWeight(int n) { + int bits = 0; + int mask = 1; + for (int i = 0; i < 32; i++) { + if ((n & mask) != 0) { + bits++; + } + mask <<= 1; + } + return bits; + } + } + + public static class Solution3 { + public int hammingWeight(int n) { + int bits = 0; + for (int i = 0; i < 32; i++) { + if ((n & 1) == 1) { + bits++; + } + if (n == 0) { + return bits; + } + /*must use unsigned right shift operator since the problem says this is an unsigned value*/ + n >>>= 1; + } + return bits; + } + } + + public static class Solution4 { + public int hammingWeight(int n) { + int bits = 0; + for (int i = 0; i < 32; i++) { + bits += n & 1; + n >>>= 1; + } + return bits; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_198.java b/src/main/java/com/fishercoder/solutions/firstthousand/_198.java new file mode 100644 index 0000000000..a7150aa09a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_198.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _198 { + public static class Solution1 { + public int rob(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + if (nums.length == 1) { + return nums[0]; + } + int[] dp = new int[nums.length]; + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + for (int i = 2; i < nums.length; i++) { + dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); + } + return dp[nums.length - 1]; + } + } + + public static class Solution2 { + /* + * recursion + memoization, basically the same as the above solution1 + * credit: https://leetcode.com/problems/house-robber/solution/ + */ + int[] memo; + + public int rob(int[] nums) { + memo = new int[nums.length]; + Arrays.fill(memo, -1); + return robFrom(0, nums); + } + + private int robFrom(int start, int[] nums) { + if (start >= nums.length) { + return 0; + } + if (this.memo[start] != -1) { + return this.memo[start]; + } + this.memo[start] = + Math.max(robFrom(start + 1, nums), robFrom(start + 2, nums) + nums[start]); + return this.memo[start]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_199.java b/src/main/java/com/fishercoder/solutions/firstthousand/_199.java new file mode 100644 index 0000000000..d97a41525b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_199.java @@ -0,0 +1,64 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _199 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/binary-tree-right-side-view/discuss/56012/My-simple-accepted-solution(JAVA) + */ + public List rightSideView(TreeNode root) { + List result = new ArrayList<>(); + rightView(root, result, 0); + return result; + } + + void rightView(TreeNode curr, List result, int currDepth) { + if (curr == null) { + return; + } + if (currDepth == result.size()) { + result.add(curr.val); + } + // go through right side first as we want right side view, so as soon as we find it, + // then we won't use the one from left side + rightView(curr.right, result, currDepth + 1); + rightView(curr.left, result, currDepth + 1); + } + } + + public static class Solution2 { + /* + * BFS the tree + */ + public List rightSideView(TreeNode root) { + List result = new ArrayList<>(); + if (root == null) { + return result; + } + Queue q = new LinkedList<>(); + q.offer(root); + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = q.poll(); + if (i == size - 1) { + result.add(curr.val); + } + if (curr.left != null) { + q.offer(curr.left); + } + if (curr.right != null) { + q.offer(curr.right); + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_2.java b/src/main/java/com/fishercoder/solutions/firstthousand/_2.java new file mode 100644 index 0000000000..1729c9a9a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_2.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _2 { + public static class Solution1 { + /* + * My completely original solution on 10/24/2021. + */ + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + int carry = 0; + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + while (l1 != null || l2 != null) { + int sum = 0; + if (l1 != null) { + sum += l1.val; + l1 = l1.next; + } + if (l2 != null) { + sum += l2.val; + l2 = l2.next; + } + sum += carry; + tmp.next = new ListNode(sum % 10); + tmp = tmp.next; + carry = sum / 10; + } + if (carry != 0) { + tmp.next = new ListNode(carry); + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_20.java b/src/main/java/com/fishercoder/solutions/firstthousand/_20.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_20.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_20.java index 87752aa669..da3f88feb4 100644 --- a/src/main/java/com/fishercoder/solutions/_20.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_20.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.Stack; @@ -29,7 +29,7 @@ public boolean isValid(String s) { } public static class Solution2 { - /** + /* * A more concise solution: * credit: https://leetcode.com/problems/valid-parentheses/discuss/9178/Short-java-solution * */ diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_200.java b/src/main/java/com/fishercoder/solutions/firstthousand/_200.java new file mode 100644 index 0000000000..5c867bfdc6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_200.java @@ -0,0 +1,114 @@ +package com.fishercoder.solutions.firstthousand; + +public class _200 { + + public static class Solution1 { + + /* + * DFS solution, note: this modifies the input. + */ + public int numIslands(char[][] grid) { + if (grid == null || grid.length == 0) { + return 0; + } + int count = 0; + int m = grid.length; + int n = grid[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == '1') { + count++; + dfs(grid, i, j, m, n); + } + } + } + return count; + } + + void dfs(char[][] grid, int i, int j, int m, int n) { + if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') { + return; + } + grid[i][j] = '0'; + dfs(grid, i + 1, j, m, n); + dfs(grid, i, j + 1, m, n); + dfs(grid, i - 1, j, m, n); + dfs(grid, i, j - 1, m, n); + } + } + + public static class Solution2 { + + class UnionFind { + int count; + int m; + int n; + int[] ids; + + public UnionFind(char[][] grid) { + m = grid.length; + n = grid[0].length; + ids = new int[m * n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == '1') { + // at initialization, we count each '1' as one island, later, during + // traversal, we'll union them during which we'll dedup the number of + // islands. + count++; + } + ids[i * n + j] = i * n + j; + } + } + } + + public void union(int i, int j) { + int x = find(i); + int y = find(j); + if (x != y) { + /* + * This means when these two nodes should be unioned, however, so far, + * they have not, i.e. they have different ids, + * so we'll have to unify them by assigning one's ids to the other, or vice versa. + * */ + ids[x] = y; // ids[y] = x; //also works + count--; // since now these two islands are unified/merged, we'll decrement the + // count by one + } + } + + public int find(int i) { + if (i != ids[i]) { + ids[i] = find((ids[i])); + } + return ids[i]; + } + } + + public int numIslands(char[][] grid) { + if (grid == null || grid.length == 0 || grid[0].length == 0) { + return 0; + } + int[] dirs = new int[] {0, 1, 0, -1, 0}; + UnionFind uf = new UnionFind(grid); + int m = grid.length; + int n = grid[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == '1') { + for (int k = 0; k < 4; k++) { + int x = i + dirs[k]; + int y = j + dirs[k + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1') { + int id1 = i * n + j; + int id2 = x * n + y; + uf.union(id1, id2); + } + } + } + } + } + return uf.count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_201.java b/src/main/java/com/fishercoder/solutions/firstthousand/_201.java new file mode 100644 index 0000000000..f28ce32145 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_201.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _201 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/28538/java-python-easy-solution-with-explanation + * Bitwise AND operation within range actually turns out to be doing some operations with just these two boundary numbers: m and n + * e.g. 5 and 7, in binary, they are 101 and 111, the result for [5,7] is 5&6&7 which is 101&110&111 + * this we can understand it to be shifting the digits of m and n from left to right until they become the same, then we pad that number with zeroes on the right side + *

+ * A more visual explanation here: https://leetcode.com/problems/bitwise-and-of-numbers-range/solution/ + */ + public int rangeBitwiseAnd(int left, int right) { + int shift = 0; + while (left != right) { + left >>= 1; + right >>= 1; + shift++; + } + return right << shift; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_202.java b/src/main/java/com/fishercoder/solutions/firstthousand/_202.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_202.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_202.java index c1304e0d02..d82a285f5d 100644 --- a/src/main/java/com/fishercoder/solutions/_202.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_202.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.HashSet; import java.util.Set; -/** +/* * 202. Happy Number * * Write an algorithm to determine if a number is "happy". diff --git a/src/main/java/com/fishercoder/solutions/_203.java b/src/main/java/com/fishercoder/solutions/firstthousand/_203.java similarity index 81% rename from src/main/java/com/fishercoder/solutions/_203.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_203.java index 57a0c52b18..eabd53c7a9 100644 --- a/src/main/java/com/fishercoder/solutions/_203.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_203.java @@ -1,44 +1,35 @@ -package com.fishercoder.solutions; - -import com.fishercoder.common.classes.ListNode; - -/** - * 203. Remove Linked List Elements - * - * Remove all elements from a linked list of integers that have value val. - * - * Example - * Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 - * Return: 1 --> 2 --> 3 --> 4 --> 5 - */ -public class _203 { - public static class Solution1 { - /** - * This is a very good question to test your understanding of pointers/memory/addresses, although it's marked as EASY. - * All the three nodes: dummy, curr and prev are indispensable. - * - * 1. Eventually, we should return dummy.next as the final result. - * 2. we assign head to curr, dummy to prev - * 3. and then we use prev and curr to traverse through the list and do the work - * 4. each time, we only move one node forward, so we don't need another while loop inside the while loop - * 5. KEY: if(curr.val == val), then curr is the node we want to remove, so, we'll assign curr.next to prev.next, thus, prev won't have that node - * else, we'll keep moving prev forward, so, just do prev = prev.next - * but, for both cases, we'll also move curr forward, so we put curr = curr.next in the outside. - */ - public ListNode removeElements(ListNode head, int val) { - ListNode dummy = new ListNode(-1); - dummy.next = head; - ListNode curr = head; - ListNode prev = dummy; - while (curr != null) { - if (curr.val == val) { - prev.next = curr.next; - } else { - prev = prev.next; - } - curr = curr.next; - } - return dummy.next; - } - } -} \ No newline at end of file +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _203 { + public static class Solution1 { + /* + * This is a very good question to test your understanding of pointers/memory/addresses, although it's marked as EASY. + * All the three nodes: dummy, curr and prev are indispensable. + *

+ * 1. Eventually, we should return dummy.next as the final result. + * 2. we assign head to curr, dummy to prev + * 3. and then we use prev and curr to traverse through the list and do the work + * 4. each time, we only move one node forward, so we don't need another while loop inside the while loop + * 5. KEY: if(curr.val == val), then curr is the node we want to remove, so, we'll assign curr.next to prev.next, thus, prev won't have that node + * else, we'll keep moving prev forward, so, just do prev = prev.next + * but, for both cases, we'll also move curr forward, so we put curr = curr.next in the outside. + */ + public ListNode removeElements(ListNode head, int val) { + ListNode dummy = new ListNode(-1); + dummy.next = head; + ListNode curr = head; + ListNode prev = dummy; + while (curr != null) { + if (curr.val == val) { + prev.next = curr.next; + } else { + prev = prev.next; + } + curr = curr.next; + } + return dummy.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_204.java b/src/main/java/com/fishercoder/solutions/firstthousand/_204.java new file mode 100644 index 0000000000..840d4d6b76 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_204.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _204 { + public static class Solution1 { + /* + * Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + * This is an ancient algorithm to efficiently count the number of primes up to a given point: + * it does so iteratively by marking composite (i.e. not prime) the multiples of each prime, + * starting from 2, then all remaining ones are prime. + */ + public int countPrimes(int n) { + if (n <= 1) { + return 0; + } + boolean[] isPrime = new boolean[n]; + Arrays.fill(isPrime, true); + isPrime[0] = false; + isPrime[1] = false; + int count = 0; + for (int i = 2; i < n; i++) { + if (isPrime[i]) { + count++; + for (int j = 2; i * j < n; j++) { + isPrime[i * j] = false; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_205.java b/src/main/java/com/fishercoder/solutions/firstthousand/_205.java new file mode 100644 index 0000000000..f5edbf5e71 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_205.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _205 { + /* + * space should be O(1) since it only has alphabetic letters which are capped at 52. + */ + + public static class Solution1 { + public boolean isIsomorphic(String s, String t) { + if (s == null || s.length() == 0) { + return (t == null || t.length() == 0); + } + if (t == null || t.length() == 0) { + return (s == null || s.length() == 0); + } + char[] schar = s.toCharArray(); + char[] tchar = t.toCharArray(); + Map map = new HashMap(); + if (s.length() != t.length()) { + return false; + } + for (int i = 0; i < s.length(); i++) { + if (map.containsKey(schar[i])) { + if (map.get(schar[i]) != tchar[i]) { + return false; + } + } else { + if (map.containsValue(tchar[i])) { + return false; // this line is necessary for this case: ("ab", "aa") + } + map.put(schar[i], tchar[i]); + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_206.java b/src/main/java/com/fishercoder/solutions/firstthousand/_206.java new file mode 100644 index 0000000000..2c53b7cc13 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_206.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _206 { + + public static class Solution1 { + public ListNode reverseList(ListNode head) { + ListNode newHead = null; + while (head != null) { + ListNode next = head.next; + head.next = newHead; + newHead = head; + head = next; + } + return newHead; + } + } + + public static class Solution2 { + public ListNode reverseList(ListNode head) { + return reverse(head, null); + } + + ListNode reverse(ListNode head, ListNode newHead) { + if (head == null) { + return newHead; + } + ListNode next = head.next; + head.next = newHead; + newHead = head; + head = next; + return reverse(head, newHead); + } + } + + public static class Solution3 { + + /* + * I feel like using the variable called prev makes more sense to me on 10/25/2021 + * since it's literally a previous node when we start reversing from the head of the list. + * Again, using a pen and paper to visualize the steps helps a lot to convert thoughts into code. + */ + public ListNode reverseList(ListNode head) { + ListNode prev = null; + while (head != null) { + ListNode next = head.next; + head.next = prev; + prev = head; + head = next; + } + return prev; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_207.java b/src/main/java/com/fishercoder/solutions/firstthousand/_207.java new file mode 100644 index 0000000000..4d9d887ed6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_207.java @@ -0,0 +1,212 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Set; + +/* + * 207. Course Schedule + *

+ * There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. + * You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. + * For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. + * Return true if you can finish all courses. Otherwise, return false. + *

+ * Example 1: + * Input: numCourses = 2, prerequisites = [[1,0]] + * Output: true + * Explanation: There are a total of 2 courses to take. + * To take course 1 you should have finished course 0. So it is possible. + *

+ * Example 2: + * Input: numCourses = 2, prerequisites = [[1,0],[0,1]] + * Output: false + * Explanation: There are a total of 2 courses to take. + * To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible. + *

+ * Constraints: + * 1 <= numCourses <= 2000 + * 0 <= prerequisites.length <= 5000 + * prerequisites[i].length == 2 + * 0 <= ai, bi < numCourses + * All the pairs prerequisites[i] are unique. + */ +public class _207 { + + public static class Solution1 { + /* + * Kahn's algorithm for topological sorting + */ + public boolean canFinish(int numCourses, int[][] prerequisites) { + int[] indegree = new int[numCourses]; + for (int[] prereq : prerequisites) { + indegree[prereq[0]]++; + } + Set zeroDegree = new HashSet(); + for (int i = 0; i < numCourses; i++) { + if (indegree[i] == 0) { + zeroDegree.add(i); + } + } + if (zeroDegree.isEmpty()) { + return false; + } + + while (!zeroDegree.isEmpty()) { + Iterator it = zeroDegree.iterator(); + int course = it.next(); + zeroDegree.remove(course); + for (int[] pre : prerequisites) { + if (pre[1] == course) { + indegree[pre[0]]--; + if (indegree[pre[0]] == 0) { + zeroDegree.add(pre[0]); + } + } + } + } + + for (int i : indegree) { + if (i != 0) { + return false; + } + } + return true; + } + } + + public static class Solution2 { + /* + * BFS + */ + public boolean canFinish(int numCourses, int[][] prerequisites) { + int[] indegree = new int[numCourses]; + for (int[] pre : prerequisites) { + indegree[pre[0]]++; + } + Queue queue = new LinkedList(); + for (int i = 0; i < numCourses; i++) { + if (indegree[i] == 0) { + queue.offer(i); + } + } + if (queue.isEmpty()) { + return false; + } + while (!queue.isEmpty()) { + int course = queue.poll(); + for (int[] pre : prerequisites) { + if (pre[1] == course) { + indegree[pre[0]]--; + if (indegree[pre[0]] == 0) { + queue.offer(pre[0]); + } + } + } + } + for (int degree : indegree) { + if (degree != 0) { + return false; + } + } + return true; + } + } + + public static class Solution3 { + /* + * DFS, the fastest method in all, with the help of a cache and also converted edges into adjacency list, + * although theoretically, all these three methods' time complexity are: O(V+E) + */ + public boolean canFinish(int numCourses, int[][] prerequisites) { + List> courseList = new ArrayList<>(); + for (int i = 0; i < numCourses; i++) { + courseList.add(new ArrayList<>()); + } + for (int[] pre : prerequisites) { + courseList.get(pre[1]).add(pre[0]); + } + int[] visited = new int[numCourses]; + // visit each course using DFS + for (int i = 0; i < numCourses; i++) { + if (!dfs(i, courseList, visited)) { + return false; + } + } + return true; + } + + private boolean dfs(int course, List> courseList, int[] visited) { + visited[course] = 1; // mark as temporarily visited + List coursesCanBeTaken = courseList.get(course); + for (int i = 0; i < coursesCanBeTaken.size(); i++) { + int courseToTake = coursesCanBeTaken.get(i); + if (visited[courseToTake] == 1) { + return false; + } + if (visited[courseToTake] == 0) { + if (!dfs(courseToTake, courseList, visited)) { + return false; + } + } + } + visited[course] = 2; // mark it as completely done. + return true; + } + } + + public static class Solution4 { + /* + * This is also Kahn's algorithm, but builds an adjacency list (unncessary for this problem) + * which is often times very helpful in other graph problems, doing it here as a practice: + * it's a very practical template: + * 1. an array of list type to hold all adjacency lists; + * 2. an array of integers to hold indegree for each node; + * 3. several for loops: + * first for-loop: initialize the adjacency list; + * second for-loop: go through all prerequisites to build both adjacency list and indegree array; + * third for-loop: find all nodes that have indegree == zero and add them into the queue; + * 4. start a while loop as long as q is not empty: + * 4.1: poll a node from the q, this node has indegree == zero; + * 4.2: go through all adjacent nodes of this node, decrement ecah of their indegree by one; + * 4.3: if any of their indegree == zero, add that adjacent node into the q + * 5. in the end, all nodes' indegree should be zero, otherwise, it's not a valid topological sortably graph. + */ + public boolean canFinish(int numCourses, int[][] prerequisites) { + List[] adjList = new ArrayList[numCourses]; + for (int i = 0; i < numCourses; i++) { + adjList[i] = new ArrayList<>(); + } + int[] indegree = new int[numCourses]; + for (int[] pre : prerequisites) { + indegree[pre[1]]++; + adjList[pre[0]].add(pre[1]); + } + Queue q = new LinkedList<>(); + for (int i = 0; i < numCourses; i++) { + if (indegree[i] == 0) { + q.offer(i); + } + } + while (!q.isEmpty()) { + Integer curr = q.poll(); + for (int v : adjList[curr]) { + indegree[v]--; + if (indegree[v] == 0) { + q.offer(v); + } + } + } + for (int i : indegree) { + if (i != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_208.java b/src/main/java/com/fishercoder/solutions/firstthousand/_208.java new file mode 100644 index 0000000000..5b3e8b22a9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_208.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +public class _208 { + public static class Solution1 { + public static class TrieNode { + boolean isWord; + TrieNode[] children = new TrieNode[26]; + } + + public static class Trie { + private TrieNode root; + + public Trie() { + root = new TrieNode(); // initialize root to be an empty char, this is a common + // practice as how Wiki defines Trie data structure as well + } + + // Inserts a word into the trie. + public void insert(String word) { + TrieNode node = root; + for (int i = 0; i < word.length(); i++) { + if (node.children[word.charAt(i) - 'a'] == null) { + node.children[word.charAt(i) - 'a'] = new TrieNode(); + } + node = node.children[word.charAt(i) - 'a']; + } + node.isWord = true; + } + + // Returns if the word is in the trie. + public boolean search(String word) { + TrieNode node = root; + for (int i = 0; i < word.length(); i++) { + if (node.children[word.charAt(i) - 'a'] == null) { + return false; + } + node = node.children[word.charAt(i) - 'a']; + } + return node.isWord; + } + + // Returns if there is any word in the trie + // that starts with the given prefix. + public boolean startsWith(String prefix) { + TrieNode node = root; + for (int i = 0; i < prefix.length(); i++) { + if (node.children[prefix.charAt(i) - 'a'] == null) { + return false; + } + node = node.children[prefix.charAt(i) - 'a']; + } + return true; + } + } + + // Your Trie object will be instantiated and called as such: + // Trie trie = new Trie(); + // trie.insert("somestring"); + // trie.search("key"); + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_209.java b/src/main/java/com/fishercoder/solutions/firstthousand/_209.java new file mode 100644 index 0000000000..74479f66bd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_209.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +public class _209 { + + public static class Solution1 { + /* + * A classic sliding window problem/solution. + */ + public int minSubArrayLen(int target, int[] nums) { + int left = 0; + int right = 0; + int sum = 0; + int ans = Integer.MAX_VALUE; + while (right < nums.length) { + sum += nums[right]; + while (sum >= target) { + ans = Math.min(ans, right - left + 1); + sum -= nums[left]; + left++; + } + right++; + } + return ans == Integer.MAX_VALUE ? 0 : ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_21.java b/src/main/java/com/fishercoder/solutions/firstthousand/_21.java similarity index 94% rename from src/main/java/com/fishercoder/solutions/_21.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_21.java index 675b508aeb..ac5c1c479c 100644 --- a/src/main/java/com/fishercoder/solutions/_21.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_21.java @@ -1,10 +1,10 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.ListNode; public class _21 { public static class Solution1 { - /** + /* * recursive solution * Time: O(m+n) * Space: O(m+n) @@ -27,7 +27,7 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) { } public static class Solution2 { - /** + /* * iterative solution * Time: O(m+n) * Space: O(1) @@ -51,5 +51,4 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) { return pre.next; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_210.java b/src/main/java/com/fishercoder/solutions/firstthousand/_210.java new file mode 100644 index 0000000000..a8c2ec28bb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_210.java @@ -0,0 +1,88 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +public class _210 { + public static class Solution1 { + public int[] findOrder(int numCourses, int[][] prerequisites) { + int[] indegree = new int[numCourses]; + Map> adjacencyList = new HashMap<>(); + for (int[] pre : prerequisites) { + List list = adjacencyList.getOrDefault(pre[1], new ArrayList<>()); + list.add(pre[0]); + adjacencyList.put(pre[1], list); + indegree[pre[0]]++; + } + Queue q = new LinkedList<>(); + for (int i = 0; i < numCourses; i++) { + if (indegree[i] == 0) { + q.offer(i); + } + } + int[] order = new int[numCourses]; + int i = 0; + while (!q.isEmpty()) { + Integer course = q.poll(); + order[i++] = course; + if (adjacencyList.containsKey(course)) { + for (int neighbor : adjacencyList.get(course)) { + indegree[neighbor]--; + if (indegree[neighbor] == 0) { + q.offer(neighbor); + } + } + } + } + if (i == numCourses) { + return order; + } + return new int[] {}; + } + } + + public static class Solution2 { + /* + * Instead of using a map, we can use an array of list type, it turned out to be even faster on LeetCode. + */ + public int[] findOrder(int numCourses, int[][] prerequisites) { + List[] adjList = new ArrayList[numCourses]; + for (int i = 0; i < numCourses; i++) { + adjList[i] = new ArrayList<>(); + } + int[] indegree = new int[numCourses]; + for (int[] pre : prerequisites) { + indegree[pre[0]]++; + adjList[pre[1]].add(pre[0]); + } + Queue q = new LinkedList<>(); + for (int i = 0; i < indegree.length; i++) { + if (indegree[i] == 0) { + q.offer(i); + } + } + int index = 0; + int[] order = new int[numCourses]; + while (!q.isEmpty()) { + Integer curr = q.poll(); + order[index++] = curr; + // NOTE: we only need to go through adjList[curr] here now, instead of going through + // all prerequisites again now. + for (int v : adjList[curr]) { + indegree[v]--; + if (indegree[v] == 0) { + q.offer(v); + } + } + } + if (index == numCourses) { + return order; + } + return new int[] {}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_211.java b/src/main/java/com/fishercoder/solutions/firstthousand/_211.java new file mode 100644 index 0000000000..1f2e92d36f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_211.java @@ -0,0 +1,86 @@ +package com.fishercoder.solutions.firstthousand; + +public class _211 { + public static class Solution1 { + public static class WordDictionary { + WordNode root; + + /* + * Initialize your data structure here. + */ + public WordDictionary() { + root = new WordNode(); + } + + public void addWord(String word) { + char[] chars = word.toCharArray(); + addWord(chars, 0, root); + } + + private void addWord(char[] chars, int index, WordNode parent) { + char c = chars[index]; + int idx = c - 'a'; + WordNode node = parent.children[idx]; + if (node == null) { + node = new WordNode(); + parent.children[idx] = node; + } + if (chars.length == index + 1) { + node.isLeaf = true; + return; + } + addWord(chars, ++index, node); + } + + public boolean search(String word) { + return search(word.toCharArray(), 0, root); + } + + /* + * This is also a beautifully designed recursive function. + */ + private boolean search(char[] chars, int index, WordNode parent) { + if (index == chars.length) { + if (parent.isLeaf) { + return true; + } + return false; + } + WordNode[] childNodes = parent.children; + char c = chars[index]; + if (c == '.') { + for (int i = 0; i < childNodes.length; i++) { + WordNode n = childNodes[i]; + if (n != null) { + boolean b = search(chars, index + 1, n); + if (b) { + return true; + } + } + } + return false; + } + WordNode node = childNodes[c - 'a']; + if (node == null) { + return false; + } + return search(chars, ++index, node); + } + + /* + * This is a cool/standard design for a Trie node class. + */ + private class WordNode { + boolean isLeaf; + WordNode[] children = new WordNode[26]; + } + } + + /* + * Your WordDictionary object will be instantiated and called as such: + * WordDictionary obj = new WordDictionary(); + * obj.addWord(word); + * boolean param_2 = obj.search(word); + */ + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_212.java b/src/main/java/com/fishercoder/solutions/firstthousand/_212.java new file mode 100644 index 0000000000..313ba3c8b5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_212.java @@ -0,0 +1,116 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +public class _212 { + + public static class Solution1 { + public List findWords(char[][] board, String[] words) { + TrieNode root = buildTrie(words); + List result = new ArrayList(); + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + dfs(root, board, i, j, result); + } + } + return result; + } + + private void dfs(TrieNode root, char[][] board, int i, int j, List result) { + char tmp = board[i][j]; + + if (tmp == '#' || root.children[tmp - 'a'] == null) { + return; + } + + if (root.children[tmp - 'a'].word != null) { + result.add(root.children[tmp - 'a'].word); + root.children[tmp - 'a'].word = null; // de-duplicate + } + board[i][j] = '#'; // mark it as visited to avoid cycles + if (i > 0) { + dfs(root.children[tmp - 'a'], board, i - 1, j, result); + } + if (j > 0) { + dfs(root.children[tmp - 'a'], board, i, j - 1, result); + } + if (i + 1 < board.length) { + dfs(root.children[tmp - 'a'], board, i + 1, j, result); + } + if (j + 1 < board[0].length) { + dfs(root.children[tmp - 'a'], board, i, j + 1, result); + } + + // backtracking + board[i][j] = tmp; + } + + private TrieNode root; + + class TrieNode { + String word; + TrieNode[] children = new TrieNode[26]; + } + + private TrieNode buildTrie(String[] words) { + TrieNode root = new TrieNode(); + for (String word : words) { + char[] chars = word.toCharArray(); + TrieNode temp = root; + for (char c : chars) { + if (temp.children[c - 'a'] == null) { + temp.children[c - 'a'] = new TrieNode(); + } + temp = temp.children[c - 'a']; + } + temp.word = word; + } + return root; + } + } + + public static class Solution2 { + public List findWords(char[][] board, String[] words) { + List result; + HashSet set = new HashSet(); + for (String word : words) { + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + if (board[i][j] == word.charAt(0) && search(board, i, j, 0, word)) { + set.add(word); + } + } + } + } + result = new ArrayList<>(set); + return result; + } + + private boolean search(char[][] board, int i, int j, int index, String word) { + if (index == word.length()) { + return true; + } + + if (i < 0 + || i >= board.length + || j < 0 + || j >= board[0].length + || board[i][j] != word.charAt(index)) { + return false; + } + + char temp = board[i][j]; + board[i][j] = ' '; + + boolean foundWord = + search(board, i + 1, j, index + 1, word) + || search(board, i - 1, j, index + 1, word) + || search(board, i, j + 1, index + 1, word) + || search(board, i, j - 1, index + 1, word); + board[i][j] = temp; + return foundWord; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_213.java b/src/main/java/com/fishercoder/solutions/firstthousand/_213.java new file mode 100644 index 0000000000..a5718957e7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_213.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +public class _213 { + public static class Solution1 { + /* + * Basically there are two ways to rob the houses: one is to rob from 0 to n - 1, the other is to rob from 1 to n, and then take the max from these two. + * Time: O(n) + * Space: O(n) + */ + public int rob(int[] nums) { + if (nums.length == 1) { + return nums[0]; + } + return Math.max(rob(nums, 0, nums.length - 1), rob(nums, 1, nums.length)); + } + + private int rob(int[] nums, int start, int end) { + int[] dp = new int[nums.length]; + dp[start] = nums[start]; + if (end - start <= 1) { + return nums[start]; + } + dp[start + 1] = Math.max(nums[start], nums[start + 1]); + for (int i = start + 2; i < end; i++) { + dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); + } + return dp[end - 1]; + } + } + + public static class Solution2 { + /* + * This solution is identical to the above solution 1, the only difference is that instead of using an extra array, we use only two extra variables here to reduce the space complexity to O(1). + * Time: O(n) + * Space: O(1) + *

+ * Just draw out how this rotation works on a piece of paper, it'll be easy to figure out how first, second and tmp variables keep shifting towards the right of the array. + */ + public int rob(int[] nums) { + if (nums.length == 1) { + return nums[0]; + } + return Math.max(rob(nums, 0, nums.length - 1), rob(nums, 1, nums.length)); + } + + private int rob(int[] nums, int start, int end) { + int first = nums[start]; + if (end - start <= 1) { + return first; + } + int second = Math.max(nums[start], nums[start + 1]); + int tmp; + for (int i = start + 2; i < end; i++) { + tmp = Math.max(second, first + nums[i]); + first = second; + second = tmp; + } + return Math.max(first, second); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_214.java b/src/main/java/com/fishercoder/solutions/firstthousand/_214.java new file mode 100644 index 0000000000..7f59b1d07e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_214.java @@ -0,0 +1,56 @@ +package com.fishercoder.solutions.firstthousand; + +public class _214 { + + public static class Solution1 { + /*credit: https://discuss.leetcode.com/topic/27261/clean-kmp-solution-with-super-detailed-explanation*/ + /* + * TODO: read it explanation and understand KMP completely. + */ + public String shortestPalindrome(String s) { + String temp = s + "#" + new StringBuilder(s).reverse().toString(); + int[] table = getTable(temp); + // get the maximum palin part in s starts from 0 + return new StringBuilder(s.substring(table[table.length - 1])).reverse().toString() + s; + } + + public int[] getTable(String s) { + // get lookup table + int[] table = new int[s.length()]; + + // pointer that points to matched char in prefix part + int index = 0; + // skip index 0, we will not match a string with itself + for (int i = 1; i < s.length(); i++) { + if (s.charAt(index) == s.charAt(i)) { + // we can extend match in prefix and postfix + table[i] = table[i - 1] + 1; + index++; + } else { + // match failed, we try to match a shorter substring + + // by assigning index to table[i-1], we will shorten the match string length, + // and jump to the + // prefix part that we used to match postfix ended at i - 1 + index = table[i - 1]; + + while (index > 0 && s.charAt(index) != s.charAt(i)) { + // we will try to shorten the match string length until we revert to the + // beginning of match (index 1) + index = table[index - 1]; + } + + // when we are here may either found a match char or we reach the boundary and + // still no luck + // so we need check char match + if (s.charAt(index) == s.charAt(i)) { + // if match, then extend one char + index++; + } + table[i] = index; + } + } + return table; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_215.java b/src/main/java/com/fishercoder/solutions/firstthousand/_215.java similarity index 83% rename from src/main/java/com/fishercoder/solutions/_215.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_215.java index 5c8611bb2c..81f19d9f0e 100644 --- a/src/main/java/com/fishercoder/solutions/_215.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_215.java @@ -1,21 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.Arrays; import java.util.Collections; import java.util.PriorityQueue; -/** - * 215. Kth Largest Element in an Array - * - * Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. - - For example, - Given [3,2,1,5,6,4] and k = 2, return 5. - - Note: - You may assume k is always valid, 1 ≤ k ≤ array's length. - - */ public class _215 { public static class Solution1 { @@ -39,10 +27,11 @@ public int findKthLargest(int[] nums, int k) { } public static class Solution3 { - /**Quick Select algorithm + /* + * Quick Select algorithm * Time: O(n) in average, O(n^2) in worst case - * - * Reference: https://discuss.leetcode.com/topic/14611/java-quick-select*/ + * Reference: https://discuss.leetcode.com/topic/14611/java-quick-select + */ public int findKthLargest(int[] nums, int k) { int start = 0; int end = nums.length - 1; diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_216.java b/src/main/java/com/fishercoder/solutions/firstthousand/_216.java new file mode 100644 index 0000000000..e136d0050f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_216.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _216 { + + public static class Solution1 { + public List> combinationSum3(int k, int n) { + List> result = new ArrayList(); + int[] nums = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; + backtracking(k, n, nums, 0, new ArrayList(), result); + return result; + } + + void backtracking( + int k, + int n, + int[] nums, + int start, + List curr, + List> result) { + if (n > 0) { + for (int i = start; i < nums.length; i++) { + curr.add(nums[i]); + backtracking(k, n - nums[i], nums, i + 1, curr, result); + curr.remove(curr.size() - 1); + } + } else if (n == 0 && curr.size() == k) { + result.add(new ArrayList(curr)); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_217.java b/src/main/java/com/fishercoder/solutions/firstthousand/_217.java new file mode 100644 index 0000000000..5b9281c9a3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_217.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _217 { + public static class Solution1 { + public boolean containsDuplicate(int[] nums) { + if (nums == null || nums.length == 0) { + return false; + } + Set set = new HashSet(); + for (int i : nums) { + if (!set.add(i)) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_218.java b/src/main/java/com/fishercoder/solutions/firstthousand/_218.java new file mode 100644 index 0000000000..ab46e00ded --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_218.java @@ -0,0 +1,85 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.TreeMap; + +public class _218 { + + public static class Solution1 { + + class BuildingPoint implements Comparable { + int x; + boolean isStart; + int h; + + public BuildingPoint(int x, boolean isStart, int h) { + this.x = x; + this.h = h; + this.isStart = isStart; + } + + @Override + public int compareTo(BuildingPoint o) { + if (this.x != o.x) { + return this.x - o.x; + } else { + if (this.isStart && o.isStart) { + return o.h - this.h; + } else if (this.isStart && !o.isStart) { + return -this.h - o.h; + } else if (!this.isStart && !o.isStart) { + return this.h - o.h; + } else { + return this.h + o.h; + } + } + } + } + + public List> getSkyline(int[][] buildings) { + BuildingPoint[] bps = new BuildingPoint[buildings.length * 2]; + int index = 0; + for (int[] building : buildings) { + BuildingPoint bp1 = new BuildingPoint(building[0], true, building[2]); + BuildingPoint bp2 = new BuildingPoint(building[1], false, building[2]); + bps[index++] = bp1; + bps[index++] = bp2; + } + + // this is one key step: + Arrays.sort(bps); + + List> result = new ArrayList(); + TreeMap treeMap = new TreeMap(); + treeMap.put(0, 1); + int prevMaxH = 0; + for (BuildingPoint bp : bps) { + // if it's a starting point, we'll add it into the final result + if (bp.isStart) { + if (treeMap.containsKey(bp.h)) { + treeMap.put(bp.h, treeMap.get(bp.h) + 1); + } else { + treeMap.put(bp.h, 1); + } + } else if (!bp.isStart) { + // if it's an ending point, we'll decrement/remove this entry + if (treeMap.containsKey(bp.h) && treeMap.get(bp.h) > 1) { + treeMap.put(bp.h, treeMap.get(bp.h) - 1); + } else { + treeMap.remove(bp.h); + } + } + + int currMaxH = treeMap.lastKey(); + if (currMaxH != prevMaxH) { + result.add(Arrays.asList(bp.x, currMaxH)); + prevMaxH = currMaxH; + } + } + + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_219.java b/src/main/java/com/fishercoder/solutions/firstthousand/_219.java new file mode 100644 index 0000000000..e13908aca1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_219.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _219 { + + public static class Solution1 { + public boolean containsNearbyDuplicate(int[] nums, int k) { + Map map = new HashMap(); + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i])) { + if (i - map.get(nums[i]) <= k) { + return true; + } else { + map.put(nums[i], i); + } + } else { + map.put(nums[i], i); + } + } + return false; + } + } + + public static class Solution2 { + public boolean containsNearbyDuplicate(int[] nums, int k) { + Set set = new HashSet<>(); + for (int i = 0; i < nums.length; i++) { + if (!set.add(nums[i])) { + return true; + } + if (set.size() > k) { + set.remove(nums[i - k]); + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_22.java b/src/main/java/com/fishercoder/solutions/firstthousand/_22.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_22.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_22.java index c07b65b927..e8dbddec13 100644 --- a/src/main/java/com/fishercoder/solutions/_22.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_22.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.List; @@ -33,19 +33,19 @@ public List generateParenthesis(int n) { if (n == 0) { return result; } - helper(result, "", n, n); + backtrack(result, "", n, n); return result; } - void helper(List result, String par, int left, int right) { + void backtrack(List result, String parenthesis, int left, int right) { if (left > 0) { - helper(result, par + "(", left - 1, right); + backtrack(result, parenthesis + "(", left - 1, right); } if (right > left) { - helper(result, par + ")", left, right - 1); + backtrack(result, parenthesis + ")", left, right - 1); } if (right == 0) { - result.add(par); + result.add(parenthesis); } } } diff --git a/src/main/java/com/fishercoder/solutions/_220.java b/src/main/java/com/fishercoder/solutions/firstthousand/_220.java similarity index 85% rename from src/main/java/com/fishercoder/solutions/_220.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_220.java index 09487ee201..39194b7315 100644 --- a/src/main/java/com/fishercoder/solutions/_220.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_220.java @@ -1,25 +1,18 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.TreeSet; -/** - * 220. Contains Duplicate III - * - * Given an array of integers, find out whether there are two - * distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at - * most t and the difference between i and j is at most k. - */ public class _220 { public static class Solution1 { - /** + /* * TreeSet: per Java doc, is a NavigableSet implementation based on a TreeMap. The elements are ordered * using their natural ordering, or by a Comparator provided at set creation time, depending on * which constructor is used. This implementation provides guaranteed log(n) time cost for the * basic operations (add, remove and contains). */ - /** + /* * TreeSet turns out to be a super handy data structure for this problem. It implements Set * interface and keeps elements in sorted order, plus it has two very handy APIs: * https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#ceiling(E): Returns the @@ -37,7 +30,7 @@ public static class Solution1 { */ public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { - /**case to Long to avoid Integer overflow.*/ + /*case to Long to avoid Integer overflow.*/ TreeSet set = new TreeSet<>(); for (int i = 0; i < nums.length; ++i) { Long s = set.ceiling((long) nums[i]); diff --git a/src/main/java/com/fishercoder/solutions/_221.java b/src/main/java/com/fishercoder/solutions/firstthousand/_221.java similarity index 80% rename from src/main/java/com/fishercoder/solutions/_221.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_221.java index 785376b849..7cf4a9f14e 100644 --- a/src/main/java/com/fishercoder/solutions/_221.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_221.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _221 { public static class Solution1 { - /** + /* * The idea is pretty straightforward: use a 2d dp table to store the intermediate results */ public int maximalSquare(char[][] matrix) { @@ -22,7 +22,9 @@ public int maximalSquare(char[][] matrix) { if (matrix[i][j] == '0') { dp[i][j] = 0; } else { - dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1; + dp[i][j] = + Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + + 1; } } max = (max < dp[i][j]) ? dp[i][j] : max; diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_222.java b/src/main/java/com/fishercoder/solutions/firstthousand/_222.java new file mode 100644 index 0000000000..1e5a4c58a1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_222.java @@ -0,0 +1,107 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _222 { + + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/21317/accepted-easy-understand-java-solution/2 + */ + public int countNodes(TreeNode root) { + int leftH = getLeftHeight(root); + int rightH = getRightHeight(root); + if (leftH == rightH) { + return (1 << leftH) - 1; + } else { + return 1 + countNodes(root.left) + countNodes(root.right); + } + } + + private int getRightHeight(TreeNode root) { + int height = 0; + while (root != null) { + root = root.right; + height++; + } + return height; + } + + private int getLeftHeight(TreeNode root) { + int height = 0; + while (root != null) { + root = root.left; + height++; + } + return height; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/count-complete-tree-nodes/solution/ + */ + public int countNodes(TreeNode root) { + if (root == null) { + return 0; + } + int depth = getDepth(root); + if (depth == 0) { + return 1; + } + int left = 0; + int right = (int) Math.pow(2, depth) - 1; + // here the condition needs to be not bigger than right, instead of the typical strictly + // smaller than right. + while (left <= right) { + int mid = left + (right - left) / 2; + // this is to suppose the elements on the last level are numbered from 1 to + // Math.pow(2, d) - 1, we are using + // binary search here to find the right-most number + if (exists(root, mid, depth)) { + left = mid + 1; + } else { + right = mid - 1; + } + } + // number of all nodes equals all nodes in the previous level + all the nodes on the + // last level indicated by variable "left" + return (int) Math.pow(2, depth) - 1 + left; + } + + private boolean exists(TreeNode root, int target, int depth) { + /*An example complete tree in this algorithm: + * 1 + * / \ + * 2 3 + * / \ / + * 1 2 3 (we use 1, 2, 3 at this level to indicate how this program runs instead of 4, 5, 6) + * + * first, target is 1, we found 1 <= 1 (root), so we go to root.left, after going down to the last level (depth), + * we found this target exists: node != null, we return true from this function + * */ + int left = 0; + int right = (int) Math.pow(2, depth) - 1; + for (int i = 0; i < depth; i++) { + int mid = left + (right - left) / 2; + if (target <= mid) { + root = root.left; + right = mid; + } else { + root = root.right; + left = mid + 1; + } + } + return root != null; + } + + private int getDepth(TreeNode root) { + int depth = 0; + while (root.left != null) { + root = root.left; + depth++; + } + return depth; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_223.java b/src/main/java/com/fishercoder/solutions/firstthousand/_223.java new file mode 100644 index 0000000000..45d05d2186 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_223.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _223 { + + public static class Solution1 { + public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { + int areaA = (C - A) * (D - B); + int areaB = (G - E) * (H - F); + + int top = Math.min(D, H); + int bottom = Math.max(B, F); + int left = Math.max(A, E); + int right = Math.min(C, G); + + int overlap = 0; + if (top > bottom && right > left) { + overlap = (top - bottom) * (right - left); + } + return areaA + areaB - overlap; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_224.java b/src/main/java/com/fishercoder/solutions/firstthousand/_224.java new file mode 100644 index 0000000000..0862376826 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_224.java @@ -0,0 +1,163 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _224 { + + public static class Solution1 { + /* + * My complete original solution on 12/23/2021 + */ + public int calculate(String s) { + Deque stack = new LinkedList<>(); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == ' ') { + continue; + } else { + if (s.charAt(i) == '(' || s.charAt(i) == '+' || s.charAt(i) == '-') { + stack.addLast(s.charAt(i) + ""); + } else if (Character.isDigit(s.charAt(i))) { + int start = i; + while (i < s.length() && Character.isDigit(s.charAt(i))) { + i++; + } + stack.addLast(s.substring(start, i)); + i--; + } else if (s.charAt(i) == ')') { + int result = 0; + while (!stack.isEmpty() && !stack.peekLast().equals("(")) { + String numStr = stack.pollLast(); + int numInt = Integer.parseInt(numStr); + if (!stack.isEmpty() + && (stack.peekLast().equals("-") + || stack.peekLast().equals("+"))) { + String operator = stack.pollLast(); + if (operator.equals("+")) { + result += numInt; + } else if (operator.equals("-")) { + result -= numInt; + } + } else { + result += numInt; + if (!stack.isEmpty() && stack.peekLast().equals("(")) { + stack.pollLast(); + break; + } + } + } + if (!stack.isEmpty() && stack.peekLast().equals("(")) { + stack.pollLast(); + } + stack.addLast(result + ""); + } + } + } + int result = 0; + while (!stack.isEmpty() && stack.peekLast() != "(") { + String numStr = stack.pollLast(); + int numInt = Integer.parseInt(numStr); + if (!stack.isEmpty()) { + String operator = stack.pollLast(); + if (operator.equals("+")) { + result += numInt; + } else if (operator.equals("-")) { + result -= numInt; + } + } else { + result += numInt; + } + } + return !stack.isEmpty() ? Integer.parseInt(stack.peekLast()) + result : result; + } + } + + public static class Solution2 { + /* + * Simple and clean recursion solution, credit: https://leetcode.com/problems/basic-calculator/solutions/2344042/java-2ms-100-recursion-easy-to-understand/ + * Key points: + * 1. it uses a global variable called index to control which char to iterate on; + * 2. it passes the entire string s into recursive functions. + */ + int index; + + public int calculate(String s) { + index = 0; + return cal(s); + } + + private int cal(String s) { + int result = 0; + int num = 0; + int sign = 1; + while (index < s.length()) { + char c = s.charAt(index++); + if (c >= '0' && c <= '9') { + num = num * 10 + c - '0'; + } else if (c == '(') { + // this is the beginning of a new sub-problem, we let recursion do its job + num = cal(s); + } else if (c == ')') { + // this is the end of a problem/sub-problem, so we return + return result + sign * num; + } else if (c == '+' || c == '-') { + // now we know we finished reading one number and a new number has begun + result += sign * num; + num = 0; + sign = c == '-' ? -1 : 1; + } + } + return result + sign * num; + } + } + + public static class Solution3 { + /* + * A more elegant solution using stack and iterative approach, credit: https://leetcode.com/problems/basic-calculator/solutions/62361/iterative-java-solution-with-stack/ + * Key points: + * 1. use an integer to represent sign: 1 or -1, so it can be pushed onto a stack that's of Integer type; + */ + public int calculate(String s) { + Deque stack = new LinkedList<>(); + int result = 0; + int sign = 1; + int num = 0; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (Character.isDigit(c)) { + num = num * 10 + c - '0'; + } else if (c == '(') { + // we push the result onto the stack first, then sign + stack.addLast(result); + stack.addLast(sign); + + // reset them + sign = 1; + num = 0; + } else if (c == ')') { + // this means we reached the end of one parenthesis, so we compute result and + // reset num + result += num * sign; + num = 0; + + result *= stack.pollLast(); // this is the last sign we pushed onto the stack + result += stack.pollLast(); // this is the last number on the stack + } else if (c == '+') { + result += num * sign; + // reset below two variables + num = 0; + sign = 1; + } else if (c == '-') { + result -= num * sign; + // reset below two variables + num = 0; + sign = 1; + } + } + if (num != 0) { + result += num * sign; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_225.java b/src/main/java/com/fishercoder/solutions/firstthousand/_225.java new file mode 100644 index 0000000000..27ac46d40d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_225.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _225 { + + public static class Solution1 { + class MyStack { + + Queue q; + + public MyStack() { + q = new LinkedList(); + } + + // Push element x onto stack. + public void push(int x) { + q.offer(x); + for (int i = 1; i < q.size(); i++) { + q.offer(q.remove()); + } + } + + // Removes the element on top of the stack. + public int pop() { + return q.poll(); + } + + // Get the top element. + public int top() { + return q.peek(); + } + + // Return whether the stack is empty. + public boolean empty() { + return q.isEmpty(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_226.java b/src/main/java/com/fishercoder/solutions/firstthousand/_226.java new file mode 100644 index 0000000000..6c010501d6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_226.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; + +public class _226 { + + public static class Solution1 { + /* + * An iterative solution + */ + public TreeNode invertTree(TreeNode root) { + if (root == null || (root.left == null && root.right == null)) { + return root; + } + Queue q = new LinkedList(); + q.offer(root); + while (!q.isEmpty()) { + TreeNode curr = q.poll(); + TreeNode temp = curr.left; + curr.left = curr.right; + curr.right = temp; + if (curr.left != null) { + q.offer(curr.left); + } + if (curr.right != null) { + q.offer(curr.right); + } + } + return root; + } + } + + public static class Solution2 { + /* + * A recursive solution + */ + public TreeNode invertTree(TreeNode root) { + if (root == null || (root.left == null && root.right == null)) { + return root; + } + TreeNode temp = root.left; + root.left = root.right; + root.right = temp; + invertTree(root.left); + invertTree(root.right); + return root; + } + } + + public static class Solution3 { + /* + * A more concise version + */ + public TreeNode invertTree(TreeNode root) { + if (root == null) { + return null; + } + TreeNode temp = root.left; + root.left = invertTree(root.right); + root.right = invertTree(temp); + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_227.java b/src/main/java/com/fishercoder/solutions/firstthousand/_227.java new file mode 100644 index 0000000000..7e02d5bac7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_227.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class _227 { + + public static class Solution1 { + public int calculate(String s) { + if (s == null || s.length() == 0) { + return 0; + } + int len = s.length(); + Deque stack = new ArrayDeque<>(); + int num = 0; + char sign = '+'; + for (int i = 0; i < len; i++) { + if (Character.isDigit(s.charAt(i))) { + num = num * 10 + s.charAt(i) - '0'; + } + if ((!Character.isDigit(s.charAt(i))) && ' ' != s.charAt(i) || i == len - 1) { + if (sign == '+') { + stack.addLast(num); + } else if (sign == '-') { + stack.addLast(-num); + } else if (sign == '/') { + stack.addLast(stack.pollLast() / num); + } else if (sign == '*') { + stack.addLast(stack.pollLast() * num); + } + sign = s.charAt(i); + // reset num for the next integer + num = 0; + } + } + int result = 0; + while (!stack.isEmpty()) { + result += stack.poll(); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_228.java b/src/main/java/com/fishercoder/solutions/firstthousand/_228.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_228.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_228.java index c87d62bc03..80f0fed86e 100644 --- a/src/main/java/com/fishercoder/solutions/_228.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_228.java @@ -1,15 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.List; -/** - * 228. Summary Ranges - * - * Given a sorted integer array without duplicates, return the summary of its ranges. - * - * For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. - */ public class _228 { public static class Solution1 { @@ -30,5 +23,4 @@ public List summaryRanges(int[] nums) { return result; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_229.java b/src/main/java/com/fishercoder/solutions/firstthousand/_229.java new file mode 100644 index 0000000000..fe1cf279de --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_229.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _229 { + + public static class Solution1 { + /* + * Moore Voting algorithm: + * This is an extension of Majority Element I, instead of one one majority, there could be a max of two majority elements, + * so we'll just use two counters to do the job. + */ + public List majorityElement(int[] nums) { + List result = new ArrayList<>(); + if (nums == null || nums.length == 0) { + return result; + } + int count1 = 0; + int count2 = 0; + int candidate1 = 0; + int candidate2 = 1; + for (int num : nums) { + if (num == candidate1) { + count1++; + } else if (num == candidate2) { + count2++; + } else if (count1 == 0) { + candidate1 = num; + count1 = 1; + } else if (count2 == 0) { + candidate2 = num; + count2 = 1; + } else { + count1--; + count2--; + } + } + count1 = 0; + count2 = 0; + for (int num : nums) { + if (num == candidate1) { + count1++; + } else if (num == candidate2) { + count2++; + } + } + if (count1 > nums.length / 3) { + result.add(candidate1); + } + if (count2 > nums.length / 3) { + result.add(candidate2); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_23.java b/src/main/java/com/fishercoder/solutions/firstthousand/_23.java similarity index 80% rename from src/main/java/com/fishercoder/solutions/_23.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_23.java index 697f2f25dc..0b69bc0c02 100644 --- a/src/main/java/com/fishercoder/solutions/_23.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_23.java @@ -1,14 +1,12 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.ListNode; - -import java.util.Comparator; import java.util.PriorityQueue; public class _23 { public static class Solution1 { public ListNode mergeKLists(ListNode[] lists) { - PriorityQueue heap = new PriorityQueue((Comparator) (o1, o2) -> o1.val - o2.val); + PriorityQueue heap = new PriorityQueue<>((a, b) -> a.val - b.val); for (ListNode node : lists) { if (node != null) { @@ -21,13 +19,12 @@ public ListNode mergeKLists(ListNode[] lists) { while (!heap.isEmpty()) { ListNode curr = heap.poll(); temp.next = new ListNode(curr.val); + temp = temp.next; if (curr.next != null) { heap.offer(curr.next); } - temp = temp.next; } return pre.next; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_230.java b/src/main/java/com/fishercoder/solutions/firstthousand/_230.java new file mode 100644 index 0000000000..af63111224 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_230.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _230 { + + public static class Solution1 { + /* + * Inorder traversal gives the natural ordering of a BST, no need to sort. + */ + public int kthSmallest(TreeNode root, int k) { + List inorder = new ArrayList(); + dfs(root, inorder, k); + return inorder.get(k - 1); + } + + private void dfs(TreeNode root, List list, int k) { + if (root == null) { + return; + } + dfs(root.left, list, k); + list.add(root.val); + dfs(root.right, list, k); + if (list.size() >= (k - 1)) { + return; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_231.java b/src/main/java/com/fishercoder/solutions/firstthousand/_231.java new file mode 100644 index 0000000000..124bb12760 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_231.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.firstthousand; + +public class _231 { + public static class Solution1 { + public boolean isPowerOfTwo(int n) { + // after writing out the binary representation of some numbers: 1,2,4,8,16,32, you can + // easily figure out that + // every number that is power of two has only one bit that is 1 + // then we can apply that cool trick that we learned from {@link easy._191}: n&(n-1) + // which will clear the least significant bit in n to zero + return n > 0 && (n & (n - 1)) == 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_232.java b/src/main/java/com/fishercoder/solutions/firstthousand/_232.java new file mode 100644 index 0000000000..bbcf4387f6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_232.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Stack; + +public class _232 { + + public static class Solution1 { + /* + * This is amortized O(1) time for each operation, explanation: https://leetcode.com/problems/implement-queue-using-stacks/solution/ + */ + class MyQueue { + + Stack input = new Stack(); + Stack output = new Stack(); + + // Push element x to the back of queue. + public void push(int x) { + input.push(x); + } + + // Removes the element from in front of queue. + public int pop() { + peek(); + return output.pop(); + } + + // Get the front element. + public int peek() { + if (output.isEmpty()) { + while (!input.isEmpty()) { + output.push(input.pop()); + } + } + return output.peek(); + } + + // Return whether the queue is empty. + public boolean empty() { + return input.isEmpty() && output.isEmpty(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_233.java b/src/main/java/com/fishercoder/solutions/firstthousand/_233.java new file mode 100644 index 0000000000..b089030188 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_233.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.firstthousand; + +public class _233 { + public static class Solution1 { + public int countDigitOne(int n) { + int count = 0; + for (long k = 1; k <= n; k *= 10) { + long r = n / k; + long m = n % k; + // sum up the count of ones on every place k + count += (r + 8) / 10 * k + (r % 10 == 1 ? m + 1 : 0); + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_234.java b/src/main/java/com/fishercoder/solutions/firstthousand/_234.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_234.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_234.java index 1afae718e5..f88ddfc416 100644 --- a/src/main/java/com/fishercoder/solutions/_234.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_234.java @@ -1,24 +1,15 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.ListNode; - import java.util.ArrayList; import java.util.List; -/** - * 234. Palindrome Linked List - * - * Given a singly linked list, determine if it is a palindrome. - - Follow up: - Could you do it in O(n) time and O(1) space? - */ - public class _234 { public static class Solution1 { - /**O(n) time + /* + * O(n) time * O(1) space - * */ + */ public boolean isPalindrome(ListNode head) { if (head == null) { return true; @@ -56,9 +47,10 @@ private ListNode reverse(ListNode head) { } public static class Solution2 { - /**O(n) time + /* + * O(n) time * O(n) space - * */ + */ public boolean isPalindrome(ListNode head) { int len = 0; ListNode fast = head; @@ -87,4 +79,23 @@ public boolean isPalindrome(ListNode head) { } } + public static class Solution3 { + /* + * O(n) time + * O(n) space + */ + public boolean isPalindrome(ListNode head) { + List list = new ArrayList<>(); + while (head != null) { + list.add(head.val); + head = head.next; + } + for (int i = 0, j = list.size() - 1; i <= j; i++, j--) { + if (list.get(i) != list.get(j)) { + return false; + } + } + return true; + } + } } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_235.java b/src/main/java/com/fishercoder/solutions/firstthousand/_235.java new file mode 100644 index 0000000000..420212ae06 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_235.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _235 { + + public static class Solution1 { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root == null || p == root || q == root) { + return root; + } + if (root.val > p.val && root.val > q.val) { + return lowestCommonAncestor(root.left, p, q); + } else if (root.val < p.val && root.val < q.val) { + return lowestCommonAncestor(root.right, p, q); + } + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_236.java b/src/main/java/com/fishercoder/solutions/firstthousand/_236.java new file mode 100644 index 0000000000..8027147ec4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_236.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _236 { + public static class Solution1 { + + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root == null || root == p || root == q) { + return root; + } + TreeNode left = lowestCommonAncestor(root.left, p, q); + TreeNode right = lowestCommonAncestor(root.right, p, q); + if (left != null && right != null) { + return root; + } + return left != null ? left : right; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_237.java b/src/main/java/com/fishercoder/solutions/firstthousand/_237.java new file mode 100644 index 0000000000..3f5a34f760 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_237.java @@ -0,0 +1,13 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _237 { + + public static class Solution1 { + public void deleteNode(ListNode node) { + node.val = node.next.val; + node.next = node.next.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_238.java b/src/main/java/com/fishercoder/solutions/firstthousand/_238.java new file mode 100644 index 0000000000..cbac38fce5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_238.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.firstthousand; + +public class _238 { + + public static class Solution1 { + /* + * Very straightforward idea: iterate through the array twice: + * first time: get res[i] = res[i-1]*nums[i-1] + * second time: have a variable called right, which means all the numbers product to its right, then do + * res[i] *= right; + * right *= nums[i]; + * that's it. + * This could be very well illustrated with this example: [1,2,3,4] + */ + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + int[] result = new int[n]; + result[0] = 1; + for (int i = 1; i < n; i++) { + result[i] = result[i - 1] * nums[i - 1]; + } + int right = 1; + for (int i = n - 1; i >= 0; i--) { + result[i] *= right; + right *= nums[i]; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_239.java b/src/main/java/com/fishercoder/solutions/firstthousand/_239.java new file mode 100644 index 0000000000..63024e25e3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_239.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; + +public class _239 { + + public static class Solution1 { + public int[] maxSlidingWindow(int[] nums, int k) { + int n = nums.length; + if (n == 0) { + return nums; + } + int[] result = new int[n - k + 1]; + LinkedList dq = new LinkedList<>(); + for (int i = 0; i < n; i++) { + if (!dq.isEmpty() && dq.peek() < i - k + 1) { + dq.poll(); + } + while (!dq.isEmpty() && nums[i] >= nums[dq.peekLast()]) { + dq.pollLast(); + } + dq.offer(i); + if (i - k + 1 >= 0) { + result[i - k + 1] = nums[dq.peek()]; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_24.java b/src/main/java/com/fishercoder/solutions/firstthousand/_24.java new file mode 100644 index 0000000000..723a68ac35 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_24.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _24 { + public static class Solution1 { + /* + * Recursive solution. + */ + public ListNode swapPairs(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode second = head.next; + ListNode third = second.next; + second.next = head; + head.next = swapPairs(third); + return second; + } + } + + public static class Solution2 { + /* + * Iterative approach: + * My completely original on 10/24/2021. + */ + public ListNode swapPairs(ListNode head) { + ListNode pre = new ListNode(-1); + pre.next = head; + ListNode tmp = pre; + while (head != null) { + ListNode third; + ListNode first = head; + ListNode second = head.next; + if (second == null) { + break; + } else { + third = head.next.next; + second.next = first; + first.next = third; + tmp.next = second; + tmp = tmp.next.next; + } + head = third; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_240.java b/src/main/java/com/fishercoder/solutions/firstthousand/_240.java new file mode 100644 index 0000000000..8f13bde0db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_240.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +public class _240 { + + public static class Solution1 { + public boolean searchMatrix(int[][] matrix, int target) { + if (matrix == null || matrix.length == 0) { + return false; + } + int m = matrix.length; + int n = matrix[0].length; + int x = 0; + int y = n - 1; + while (x < m && y >= 0) { + if (target == matrix[x][y]) { + return true; + } else if (target > matrix[x][y]) { + x++; + } else { + y--; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_241.java b/src/main/java/com/fishercoder/solutions/firstthousand/_241.java new file mode 100644 index 0000000000..d879706d2b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_241.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.List; + +public class _241 { + public static class Solution1 { + /*Time: O(n * 4^n / n^(3/2)) ~= n * Catalan numbers = n * (C(2n, n) - C(2n, n - 1)), + due to the size of the results is Catalan numbers, + and every way of evaluation is the length of the string, + so the time complexity is at most n * Catalan numbers. + Space: O(n * 4^n / n^(3/2)), the cache size of lookup is at most n * Catalan numbers.*/ + + /* + * Credit: https://discuss.leetcode.com/topic/19901/a-recursive-java-solution-284-ms + */ + public List diffWaysToCompute(String input) { + List answer = new LinkedList<>(); + int len = input.length(); + for (int i = 0; i < len; i++) { + if (input.charAt(i) == '+' || input.charAt(i) == '-' || input.charAt(i) == '*') { + String part1 = input.substring(0, i); + String part2 = input.substring(i + 1); + List answer1 = diffWaysToCompute(part1); + List answer2 = diffWaysToCompute(part2); + for (int a1 : answer1) { + for (int a2 : answer2) { + int result = 0; + switch (input.charAt(i)) { + case '+': + result = a1 + a2; + break; + case '-': + result = a1 - a2; + break; + case '*': + result = a1 * a2; + break; + default: + break; + } + answer.add(result); + } + } + } + } + if (answer.size() == 0) { + answer.add(Integer.valueOf(input)); + } + return answer; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_242.java b/src/main/java/com/fishercoder/solutions/firstthousand/_242.java new file mode 100644 index 0000000000..a4951e468b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_242.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class _242 { + + public static class Solution1 { + public boolean isAnagram(String s, String t) { + char[] schar = s.toCharArray(); + char[] tchar = t.toCharArray(); + Arrays.sort(schar); + Arrays.sort(tchar); + return new String(schar).equals(new String(tchar)); + } + } + + public static class Solution2 { + public boolean isAnagram(String s, String t) { + if (s == null || t == null || s.length() != t.length()) { + return false; + } + int[] counts = new int[26]; + for (int i = 0; i < s.length(); i++) { + counts[s.charAt(i) - 'a']++; + counts[t.charAt(i) - 'a']--; + } + for (int i : counts) { + if (i != 0) { + return false; + } + } + return true; + } + } + + public static class Solution3 { + // to deal with unicode characters + public boolean isAnagram(String s, String t) { + Map map = new HashMap<>(); + for (int i = 0; i < s.length(); i++) { + map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1); + map.put(t.charAt(i), map.getOrDefault(t.charAt(i), 0) - 1); + } + for (char c : map.keySet()) { + if (map.get(c) != 0) { + return true; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_243.java b/src/main/java/com/fishercoder/solutions/firstthousand/_243.java new file mode 100644 index 0000000000..261d49abc0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_243.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _243 { + public static class Solution1 { + + public int shortestDistance(String[] words, String word1, String word2) { + int p = -1; + int q = -1; + int min = Integer.MAX_VALUE; + for (int i = 0; i < words.length; i++) { + if (words[i].equals(word1)) { + p = i; + } + if (words[i].equals(word2)) { + q = i; + } + if (p != -1 && q != -1) { + min = Math.min(min, Math.abs(p - q)); + } + } + return min; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_244.java b/src/main/java/com/fishercoder/solutions/firstthousand/_244.java new file mode 100644 index 0000000000..c7f858b2a6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_244.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _244 { + public static class Solution1 { + class WordDistance { + + private Map> map; + + public WordDistance(String[] words) { + map = new HashMap<>(); + for (int i = 0; i < words.length; i++) { + String w = words[i]; + if (map.containsKey(w)) { + map.get(w).add(i); + } else { + List list = new ArrayList<>(); + list.add(i); + map.put(w, list); + } + } + } + + public int shortest(String word1, String word2) { + List list1 = map.get(word1); + List list2 = map.get(word2); + int result = Integer.MAX_VALUE; + for (int i = 0, j = 0; i < list1.size() && j < list2.size(); ) { + int index1 = list1.get(i); + int index2 = list2.get(j); + if (index1 < index2) { + result = Math.min(result, index2 - index1); + i++; + } else { + result = Math.min(result, index1 - index2); + j++; + } + } + return result; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_245.java b/src/main/java/com/fishercoder/solutions/firstthousand/_245.java new file mode 100644 index 0000000000..2ee48be84d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_245.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +public class _245 { + + public static class Solution1 { + public int shortestWordDistance(String[] words, String word1, String word2) { + int p1 = -1; + int p2 = -1; + int min = Integer.MAX_VALUE; + for (int i = 0; i < words.length; i++) { + if (words[i].equals(word1)) { + if (word1.equals(word2)) { + if (p1 != -1 && i - p1 < min) { + min = i - p1; + } + p1 = i; + } else { + p1 = i; + if (p2 != -1 && p1 - p2 < min) { + min = p1 - p2; + } + } + } else if (words[i].equals(word2)) { + p2 = i; + if (p1 != -1 && p2 - p1 < min) { + min = p2 - p1; + } + } + } + return min; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_246.java b/src/main/java/com/fishercoder/solutions/firstthousand/_246.java similarity index 85% rename from src/main/java/com/fishercoder/solutions/_246.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_246.java index c6435dc076..93e0371ae4 100644 --- a/src/main/java/com/fishercoder/solutions/_246.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_246.java @@ -1,18 +1,10 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; -/** - * 246. Strobogrammatic Number - * - * A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). - * Write a function to determine if a number is strobogrammatic. The number is represented as a string. - * For example, the numbers "69", "88", and "818" are all strobogrammatic. - */ - public class _246 { public static class Solution1 { @@ -43,7 +35,6 @@ public boolean isStrobogrammatic(String num) { } } - public static class Solution2 { public boolean isStrobogrammatic(String num) { Set set = new HashSet(); diff --git a/src/main/java/com/fishercoder/solutions/_247.java b/src/main/java/com/fishercoder/solutions/firstthousand/_247.java similarity index 95% rename from src/main/java/com/fishercoder/solutions/_247.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_247.java index 4e19e46805..6d9b99ffd6 100644 --- a/src/main/java/com/fishercoder/solutions/_247.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_247.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/com/fishercoder/solutions/_248.java b/src/main/java/com/fishercoder/solutions/firstthousand/_248.java similarity index 84% rename from src/main/java/com/fishercoder/solutions/_248.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_248.java index 41eb6a1512..13793a9bb9 100644 --- a/src/main/java/com/fishercoder/solutions/_248.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_248.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.HashMap; import java.util.Map; @@ -6,14 +6,16 @@ public class _248 { public static class Solution1 { - /**Credit: https://discuss.leetcode.com/topic/31386/concise-java-solution - * - Construct char arrays from low.length() to high.length() - Add stro pairs from outside - When left > right, add eligible count - */ + /*Credit: https://discuss.leetcode.com/topic/31386/concise-java-solution + * + Construct char arrays from low.length() to high.length() + Add stro pairs from outside + When left > right, add eligible count + */ - private static final char[][] pairs = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}}; + private static final char[][] pairs = { + {'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'} + }; public int strobogrammaticInRange(String low, String high) { int[] count = {0}; @@ -24,7 +26,7 @@ public int strobogrammaticInRange(String low, String high) { return count[0]; } - public void dfs(String low, String high , char[] c, int left, int right, int[] count) { + public void dfs(String low, String high, char[] c, int left, int right, int[] count) { if (left > right) { String s = new String(c); if ((s.length() == low.length() && s.compareTo(low) < 0) diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_249.java b/src/main/java/com/fishercoder/solutions/firstthousand/_249.java new file mode 100644 index 0000000000..a9e8fdce11 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_249.java @@ -0,0 +1,44 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _249 { + + public static class Solution1 { + public List> groupStrings(String[] strings) { + + List> result = new ArrayList<>(); + Map> map = new HashMap<>(); + + for (String word : strings) { + // calculate the representative/key that's unique for the entire group + // i.e. if the two string belong to the same group, after shifting n times, they all + // will end up having the same key + // abc -> "2021" + // xyz -> "2021" + // acef -> "212324" + String key = ""; + int offset = word.charAt(0) - 'a'; + for (int i = 1; i < word.length(); i++) { + char c = word.charAt(i); + int offsetForThisChar = (c - offset + 26) % 26; + key += offsetForThisChar; + } + + if (!map.containsKey(key)) { + map.put(key, new ArrayList<>()); + } + map.get(key).add(word); + } + + for (List list : map.values()) { + result.add(list); + } + + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_25.java b/src/main/java/com/fishercoder/solutions/firstthousand/_25.java new file mode 100644 index 0000000000..3851463211 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_25.java @@ -0,0 +1,141 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _25 { + + /* + * We use recursion to go all the way until the end: when the number of nodes are smaller than k; + * then we start to reverse each group of k nodes from the end towards the start. + */ + public static class Solution1 { + public ListNode reverseKGroup(ListNode head, int k) { + ListNode curr = head; + int count = 0; + while (curr != null && count != k) { + // find the k+1 node + curr = curr.next; + count++; + } + + if (count == k) { + /*after this below recursive call finishes, it'll return head; + * then this returned "head" will become "curr", while the head + * in its previous callstack is the real head after this call. + * Setting up a break point will make all of this crystal clear.*/ + curr = reverseKGroup(curr, k); + + while (count-- > 0) { + ListNode temp = head.next; + head.next = curr; + curr = head; + head = temp; + } + head = curr; + } + return head; // we run out of nodes before we hit count == k, so we'll just directly + // return head in this case as well + } + } + + public static class Solution2 { + public ListNode reverseKGroup(ListNode head, int k) { + if (head == null || head.next == null || k == 1) { + return head; + } + + int n = 0; // number of nodes + + ListNode curr = head; + while (curr != null) { + n++; + curr = curr.next; + } + + ListNode prev = null; + ListNode next = null; + ListNode newHead = null; + ListNode tail1 = null; + ListNode tail2 = head; + + curr = head; + + while (n >= k) { + // reverse nodes in blocks of k + for (int i = 0; i < k; i++) { + // linked List reversal code + next = curr.next; + curr.next = prev; + prev = curr; + curr = next; + } + if (newHead == null) { + newHead = prev; + } + if (tail1 != null) { + tail1.next = prev; + } + tail2.next = curr; // when n is not multiple of k + tail1 = tail2; + tail2 = curr; + prev = null; + n -= k; + } + return newHead; + } + } + + public static class Solution3 { + /* + * My completely original solution on 10/25/2021. Beats 100% submissions on LeetCode in runtime. + * Again, using a pen and paper to visualize the process helps a lot! + * My helper function returns two nodes: reversed node head and the starting node for the next reversal. + */ + public ListNode reverseKGroup(ListNode head, int k) { + ListNode pre = new ListNode(-1); + pre.next = head; + ListNode tmp = pre; + ListNode curr = head; + ListNode[] result = new ListNode[] {null, curr}; + do { + result = reverseKGroupHelper(result[1], k); + if (result[0] == result[1]) { + tmp.next = result[0]; + return pre.next; + } else { + tmp.next = result[0]; + while (tmp.next != null) { + tmp = tmp.next; + } + } + } while (true); + } + + private ListNode[] reverseKGroupHelper(ListNode head, int k) { + int originalK = k; + ListNode tmp = head; + while (tmp != null) { + tmp = tmp.next; + k--; + } + if (k > 0) { + return new ListNode[] {head, head}; + } else { + tmp = head; + k = originalK; + ListNode prev = null; + while (tmp != null) { + ListNode next = tmp.next; + tmp.next = prev; + prev = tmp; + tmp = next; + k--; + if (k == 0) { + return new ListNode[] {prev, tmp}; + } + } + return null; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_250.java b/src/main/java/com/fishercoder/solutions/firstthousand/_250.java similarity index 94% rename from src/main/java/com/fishercoder/solutions/_250.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_250.java index 1426efb5e4..5febaa610a 100644 --- a/src/main/java/com/fishercoder/solutions/_250.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_250.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; diff --git a/src/main/java/com/fishercoder/solutions/_251.java b/src/main/java/com/fishercoder/solutions/firstthousand/_251.java similarity index 95% rename from src/main/java/com/fishercoder/solutions/_251.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_251.java index f8291d5331..f6daebc749 100644 --- a/src/main/java/com/fishercoder/solutions/_251.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_251.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.Iterator; import java.util.LinkedList; @@ -35,4 +35,4 @@ public boolean hasNext() { } } } -} \ No newline at end of file +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_252.java b/src/main/java/com/fishercoder/solutions/firstthousand/_252.java new file mode 100644 index 0000000000..2fc42386de --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_252.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.Comparator; + +public class _252 { + public static class Solution1 { + public boolean canAttendMeetings(int[][] intervals) { + if (intervals == null || intervals.length == 0) { + return true; + } + Arrays.sort(intervals, Comparator.comparingInt(a -> a[0])); + for (int i = 1; i < intervals.length; i++) { + if (intervals[i][0] < intervals[i - 1][1]) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_253.java b/src/main/java/com/fishercoder/solutions/firstthousand/_253.java new file mode 100644 index 0000000000..fe6647a680 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_253.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.PriorityQueue; + +public class _253 { + public static class Solution1 { + public int minMeetingRooms(int[][] intervals) { + if (intervals == null || intervals.length == 0) { + return 0; + } + Arrays.sort(intervals, (a, b) -> a[0] - b[0]); // Sort the intervals by start time + PriorityQueue heap = + new PriorityQueue<>( + intervals.length, + (a, b) -> a[1] - b[1]); // Use a min heap to track the minimum end + // time of merged intervals + heap.offer(intervals[0]); // start with the first meeting, put it to a meeting room + for (int i = 1; i < intervals.length; i++) { + // get the meeting room that finishes earliest + int[] last = heap.poll(); + if (intervals[i][0] >= last[1]) { + // if the current meeting starts right after + // there's no need for a new room, merge the interval + last[1] = intervals[i][1]; + } else { + // otherwise, this meeting needs a new room + heap.offer(intervals[i]); + } + // don't forget to put the meeting room back + heap.offer(last); + } + + return heap.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_254.java b/src/main/java/com/fishercoder/solutions/firstthousand/_254.java similarity index 94% rename from src/main/java/com/fishercoder/solutions/_254.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_254.java index 1e65173cd3..1a5b5d9f8b 100644 --- a/src/main/java/com/fishercoder/solutions/_254.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_254.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fishercoder/solutions/_255.java b/src/main/java/com/fishercoder/solutions/firstthousand/_255.java similarity index 91% rename from src/main/java/com/fishercoder/solutions/_255.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_255.java index 338e4ed7cf..039ea386e9 100644 --- a/src/main/java/com/fishercoder/solutions/_255.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_255.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.Stack; @@ -20,5 +20,4 @@ public boolean verifyPreorder(int[] preorder) { return true; } } - } diff --git a/src/main/java/com/fishercoder/solutions/_256.java b/src/main/java/com/fishercoder/solutions/firstthousand/_256.java similarity index 92% rename from src/main/java/com/fishercoder/solutions/_256.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_256.java index 83d24491a1..29a5510315 100644 --- a/src/main/java/com/fishercoder/solutions/_256.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_256.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _256 { diff --git a/src/main/java/com/fishercoder/solutions/_257.java b/src/main/java/com/fishercoder/solutions/firstthousand/_257.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_257.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_257.java index 1eb25f866b..c264d2b10c 100644 --- a/src/main/java/com/fishercoder/solutions/_257.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_257.java @@ -1,13 +1,12 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; - import java.util.ArrayList; import java.util.List; public class _257 { public static class Solution1 { - //a very typical/good question to test your recursion/dfs understanding. + // a very typical/good question to test your recursion/dfs understanding. public List binaryTreePaths_more_concise(TreeNode root) { List paths = new ArrayList<>(); if (root == null) { @@ -31,7 +30,7 @@ private void dfs(TreeNode root, List paths, String path) { } } } - + public static class Solution2 { public List binaryTreePaths(TreeNode root) { List paths = new ArrayList<>(); diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_258.java b/src/main/java/com/fishercoder/solutions/firstthousand/_258.java new file mode 100644 index 0000000000..efa175e622 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_258.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.firstthousand; + +public class _258 { + + public static class Solution1 { + // only three cases as the code shows + public int addDigits(int num) { + if (num == 0) { + return 0; + } + if (num % 9 == 0) { + return 9; + } + return num % 9; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_259.java b/src/main/java/com/fishercoder/solutions/firstthousand/_259.java similarity index 80% rename from src/main/java/com/fishercoder/solutions/_259.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_259.java index 085cec0532..ff7feeb769 100644 --- a/src/main/java/com/fishercoder/solutions/_259.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_259.java @@ -1,11 +1,11 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.Arrays; public class _259 { public static class Solution1 { - /** + /* * Basically, very similar to 3Sum, but the key is that you'll have to add result by (right-left), not just increment result by 1! */ public int threeSumSmaller(int[] nums, int target) { @@ -20,8 +20,8 @@ public int threeSumSmaller(int[] nums, int target) { while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum < target) { - result += right - left;//this line is key! - left++; + result += right - left; // this line is key! + left++; // then increment left to continue to see all possibilities } else { right--; } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_26.java b/src/main/java/com/fishercoder/solutions/firstthousand/_26.java new file mode 100644 index 0000000000..2750d8d869 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_26.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; + +public class _26 { + + public static class Solution1 { + /* + * Key: It doesn't matter what you leave beyond the returned length. + */ + public int removeDuplicates(int[] nums) { + int i = 0; + for (int j = 1; j < nums.length; j++) { + if (nums[i] != nums[j]) { + i++; + nums[i] = nums[j]; + } + } + return i + 1; + } + } + + public static class Solution2 { + /* + * My completely original solution on 2/2/2022. + */ + public int removeDuplicates(int[] nums) { + int left = 0; + int right = 1; + for (; right < nums.length; right++) { + while (right < nums.length && nums[right] == nums[right - 1]) { + right++; + } + if (right < nums.length) { + nums[++left] = nums[right]; + } + } + CommonUtils.printArray(nums); + return left + 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_260.java b/src/main/java/com/fishercoder/solutions/firstthousand/_260.java new file mode 100644 index 0000000000..76fe815199 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_260.java @@ -0,0 +1,62 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _260 { + + public static class Solution1 { + public int[] singleNumber(int[] nums) { + Map map = new HashMap(); + for (int i : nums) { + map.put(i, map.getOrDefault(i, 0) + 1); + } + + int[] res = new int[2]; + int index = 0; + for (int key : map.keySet()) { + if (map.get(key) == 1) { + res[index++] = key; + } + if (index == 2) { + break; + } + } + return res; + } + } + + public static class Solution2 { + /*Credit: https://discuss.leetcode.com/topic/21605/accepted-c-java-o-n-time-o-1-space-easy-solution-with-detail-explanations/2 + * + * some more explanation about this algorithm: + * two's complement: one number's two's complement number is computed as below: + * reverse all bits of this number and then add one: + * e.g. decimal number 2, in binary format: 0010 (4 bits) + * reversing every single bit becomes 1101, + * then add 1 to it, it becomes 1110 + * + * so + * num &= -num, in this case, 2 &= -2 becomes 2 + * */ + public int[] singleNumber(int[] nums) { + int diff = 0; + for (int num : nums) { + diff ^= num; + } + + // get least significant set bit + diff &= -diff; + + int[] result = new int[2]; + for (int num : nums) { + if ((num & diff) == 0) { + result[0] ^= num; + } else { + result[1] ^= num; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_261.java b/src/main/java/com/fishercoder/solutions/firstthousand/_261.java new file mode 100644 index 0000000000..a07f4f3e3e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_261.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +public class _261 { + + public static class Solution1 { + public boolean validTree(int n, int[][] edges) { + int[] nums = new int[n]; + for (int i = 0; i < n; i++) { + nums[i] = i; + } + + for (int i = 0; i < edges.length; i++) { + int x = find(nums, edges[i][0]); + int y = find(nums, edges[i][1]); + + if (x == y) { + return false; + } + + // union + nums[x] = y; + } + + return edges.length == n - 1; + } + + int find(int[] nums, int i) { + if (nums[i] == i) { + return i; + } + return find(nums, nums[i]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_263.java b/src/main/java/com/fishercoder/solutions/firstthousand/_263.java similarity index 79% rename from src/main/java/com/fishercoder/solutions/_263.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_263.java index f2feaf647e..15bebef291 100644 --- a/src/main/java/com/fishercoder/solutions/_263.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_263.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _263 { @@ -7,7 +7,7 @@ public boolean isUgly(int num) { if (num == 0) { return false; } - int[] divisors = new int[]{5, 3, 2}; + int[] divisors = new int[] {5, 3, 2}; for (int divisor : divisors) { while (num % divisor == 0) { num /= divisor; @@ -16,5 +16,4 @@ public boolean isUgly(int num) { return num == 1; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_264.java b/src/main/java/com/fishercoder/solutions/firstthousand/_264.java new file mode 100644 index 0000000000..af2cb4b553 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_264.java @@ -0,0 +1,88 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.TreeSet; + +public class _264 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/21791/o-n-java-solution + */ + public int nthUglyNumber(int n) { + int[] ugly = new int[n]; + ugly[0] = 1; + int index2 = 0; + int index3 = 0; + int index5 = 0; + int factor2 = 2; + int factor3 = 3; + int factor5 = 5; + for (int i = 1; i < n; i++) { + int min = Math.min(Math.min(factor2, factor3), factor5); + ugly[i] = min; + if (factor2 == min) { + factor2 = 2 * ugly[++index2]; + } + if (factor3 == min) { + factor3 = 3 * ugly[++index3]; + } + if (factor5 == min) { + factor5 = 5 * ugly[++index5]; + } + } + return ugly[n - 1]; + } + } + + public static class Solution2 { + /* + * My completely original solution on 11/7/2021. + * Although not super robust, as the input increases, I'll have to increase the times (variable n) on line 61 as some smaller numbers might appear later. + */ + public int nthUglyNumber(int n) { + TreeSet treeSet = new TreeSet<>(); + treeSet.add(1L); + int count = 1; + int polled = 0; + int[] primes = new int[] {2, 3, 5}; + while (!treeSet.isEmpty()) { + int size = treeSet.size(); + for (int i = 0; i < size; i++) { + Long curr = treeSet.pollFirst(); + polled++; + if (polled == n) { + return curr.intValue(); + } + for (int prime : primes) { + treeSet.add(prime * curr); + count++; + } + } + if (count >= n * 3) { + while (polled < n - 1) { + treeSet.pollFirst(); + polled++; + } + return treeSet.pollFirst().intValue(); + } + } + return -1; + } + } + + public static class Solution3 { + + public int nthUglyNumber(int n) { + TreeSet treeSet = new TreeSet<>(); + treeSet.add(1L); + long currentUgly = 0; + for (int i = 0; i < n; i++) { + currentUgly = treeSet.pollFirst(); + treeSet.add(currentUgly * 2); + treeSet.add(currentUgly * 3); + treeSet.add(currentUgly * 5); + } + return (int) currentUgly; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_265.java b/src/main/java/com/fishercoder/solutions/firstthousand/_265.java new file mode 100644 index 0000000000..bea7c69485 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_265.java @@ -0,0 +1,44 @@ +package com.fishercoder.solutions.firstthousand; + +public class _265 { + + public static class Solution1 { + public int minCostII(int[][] costs) { + if (costs == null || costs.length == 0) { + return 0; + } + + int n = costs.length; + int k = costs[0].length; + // min1 is the index of the 1st-smallest cost till previous house + // min2 is the index of the 2nd-smallest cost till previous house + int min1 = -1; + int min2 = -1; + + for (int i = 0; i < n; i++) { + int last1 = min1; + int last2 = min2; + min1 = -1; + min2 = -1; + + for (int j = 0; j < k; j++) { + if (j != last1) { + // current color j is different to last min1 + costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1]; + } else { + costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2]; + } + + // find the indices of 1st and 2nd smallest cost of painting current house i + if (min1 < 0 || costs[i][j] < costs[i][min1]) { + min2 = min1; + min1 = j; + } else if (min2 < 0 || costs[i][j] < costs[i][min2]) { + min2 = j; + } + } + } + return costs[n - 1][min1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_266.java b/src/main/java/com/fishercoder/solutions/firstthousand/_266.java similarity index 94% rename from src/main/java/com/fishercoder/solutions/_266.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_266.java index 1a9edf6451..863babe750 100644 --- a/src/main/java/com/fishercoder/solutions/_266.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_266.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.HashMap; import java.util.Map; @@ -29,5 +29,4 @@ public boolean canPermutePalindrome(String s) { return true; } } - } diff --git a/src/main/java/com/fishercoder/solutions/_267.java b/src/main/java/com/fishercoder/solutions/firstthousand/_267.java similarity index 90% rename from src/main/java/com/fishercoder/solutions/_267.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_267.java index 7aeff72dcc..8109ded20e 100644 --- a/src/main/java/com/fishercoder/solutions/_267.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_267.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.HashMap; @@ -48,8 +48,12 @@ public List generatePalindromes(String s) { } // generate all unique permutation from list - void getPerm(List list, String mid, boolean[] used, StringBuilder sb, - List res) { + void getPerm( + List list, + String mid, + boolean[] used, + StringBuilder sb, + List res) { if (sb.length() == list.size()) { // form the palindromic string res.add(sb.toString() + mid + sb.reverse().toString()); diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_268.java b/src/main/java/com/fishercoder/solutions/firstthousand/_268.java new file mode 100644 index 0000000000..1c1939a04b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_268.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +public class _268 { + + public static class Solution1 { + /* + * we could take advantage of the array indices + * then a number xor with itself is zero, so after we xor the entire array with all of its indices, the missing number will show up. + */ + public int missingNumber(int[] nums) { + int xor = 0; + int i = 0; + for (; i < nums.length; i++) { + xor ^= i ^ nums[i]; + } + return xor ^ i; + } + } + + public static class Solution2 { + public int missingNumber(int[] nums) { + int n = nums.length; + long sum = n + (n * n - n) / 2; // this is the formula to compute the sum for arithmetic + // sequence + for (int i = 0; i < nums.length; i++) { + sum -= nums[i]; + } + return (int) sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_269.java b/src/main/java/com/fishercoder/solutions/firstthousand/_269.java new file mode 100644 index 0000000000..e2234867be --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_269.java @@ -0,0 +1,85 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _269 { + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs + */ + public String alienOrder(String[] words) { + Map> charToSmallerCharsMap = + new HashMap<>(); // this map means all chars in the value set are after the char + // in the key + Map indegreeMap = + new HashMap<>(); // this map means how many chars are before this given char + StringBuilder result = new StringBuilder(); + if (words == null || words.length == 0) { + return result.toString(); + } + for (String s : words) { + for (char c : s.toCharArray()) { + // we go through each word, nothing to compare, so each char's degree should be + // zero + // only when we compare words[i] with words[i+1], we know the order of different + // chars + indegreeMap.put(c, 0); + } + } + for (int i = 0; i < words.length - 1; i++) { + String curr = words[i]; + String next = words[i + 1]; + if (curr.length() > next.length() && curr.startsWith(next)) { + return ""; + } + for (int j = 0; j < curr.length(); j++) { + char c1 = curr.charAt(j); + char c2 = next.charAt(j); + if (c1 != c2) { + Set set = + charToSmallerCharsMap.getOrDefault(c1, new HashSet<>()); + if (!set.contains(c2)) { + set.add(c2); + charToSmallerCharsMap.put(c1, set); + indegreeMap.put(c2, indegreeMap.get(c2) + 1); + } + // no longer need to continue iterating through either one of these two + // words and should not to, + // because the first two chars at the same position of these two words that + // differ decides the order + break; + } + } + } + Queue queue = new LinkedList<>(); + for (char c : indegreeMap.keySet()) { + if (indegreeMap.get(c) == 0) { + // this means no chars come before this char, so they should be at the head of + // this alien dictionary + queue.offer(c); + } + } + while (!queue.isEmpty()) { + char curr = queue.poll(); + result.append(curr); + if (charToSmallerCharsMap.containsKey(curr)) { + for (char c : charToSmallerCharsMap.get(curr)) { + indegreeMap.put(c, indegreeMap.get(c) - 1); + if (indegreeMap.get(c) == 0) { + queue.offer(c); + } + } + } + } + if (result.length() != indegreeMap.size()) { + return ""; + } + return result.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_27.java b/src/main/java/com/fishercoder/solutions/firstthousand/_27.java new file mode 100644 index 0000000000..a67f580fd6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_27.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.firstthousand; + +public class _27 { + + public static class Solution1 { + public int removeElement(int[] nums, int val) { + int i = 0; + for (int j = 0; j < nums.length; j++) { + if (nums[j] != val) { + nums[i++] = nums[j]; + } + } + return i; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_270.java b/src/main/java/com/fishercoder/solutions/firstthousand/_270.java new file mode 100644 index 0000000000..505f10746f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_270.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _270 { + + public static class Solution1 { + public int closestValue(TreeNode root, double target) { + int val; + int closest = root.val; + while (root != null) { + val = root.val; + closest = + Math.abs(val - target) < Math.abs(closest - target) + || (Math.abs(val - target) == Math.abs(closest - target) + && val < closest) + ? val + : closest; + root = target < root.val ? root.left : root.right; + } + return closest; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_271.java b/src/main/java/com/fishercoder/solutions/firstthousand/_271.java new file mode 100644 index 0000000000..ff3a65a618 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_271.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _271 { + public static class Solution1 { + // Encodes a list of strings to a single string. + public String encode(List strs) { + StringBuilder sb = new StringBuilder(); + for (String s : strs) { + sb.append(s.length()).append('/').append(s); + } + return sb.toString(); + } + + // Decodes a single string to a list of strings. + public List decode(String s) { + List result = new ArrayList<>(); + int i = 0; + while (i < s.length()) { + int slash = s.indexOf('/', i); + int size = Integer.valueOf(s.substring(i, slash)); + result.add(s.substring(slash + 1, slash + 1 + size)); + i = slash + size + 1; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_272.java b/src/main/java/com/fishercoder/solutions/firstthousand/_272.java new file mode 100644 index 0000000000..96e65b56d1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_272.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class _272 { + + public static class Solution1 { + public List closestKValues(TreeNode root, double target, int k) { + List res = new ArrayList(); + + Stack s1 = new Stack(); // predecessors + Stack s2 = new Stack(); // successors + + inorder(root, target, false, s1); + inorder(root, target, true, s2); + + while (k-- > 0) { + if (s1.isEmpty()) { + res.add(s2.pop()); + } else if (s2.isEmpty()) { + res.add(s1.pop()); + } else if (Math.abs(s1.peek() - target) < Math.abs(s2.peek() - target)) { + res.add(s1.pop()); + } else { + res.add(s2.pop()); + } + } + + return res; + } + + // inorder traversal + void inorder(TreeNode root, double target, boolean reverse, Stack stack) { + if (root == null) { + return; + } + + inorder(reverse ? root.right : root.left, target, reverse, stack); + // early terminate, no need to traverse the whole tree + if ((reverse && root.val <= target) || (!reverse && root.val > target)) { + return; + } + // track the value of current node + stack.push(root.val); + inorder(reverse ? root.left : root.right, target, reverse, stack); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_273.java b/src/main/java/com/fishercoder/solutions/firstthousand/_273.java new file mode 100644 index 0000000000..c4c57f6e4e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_273.java @@ -0,0 +1,69 @@ +package com.fishercoder.solutions.firstthousand; + +public class _273 { + + public static class Solution1 { + private String[] belowTen = + new String[] { + "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" + }; + private String[] belowTwenty = + new String[] { + "Ten", + "Eleven", + "Twelve", + "Thirteen", + "Fourteen", + "Fifteen", + "Sixteen", + "Seventeen", + "Eighteen", + "Nineteen" + }; + private String[] belowHundred = + new String[] { + "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" + }; + private String[] overThousand = new String[] {"Thousand", "Million", "Billion"}; + + public String numberToWords(int num) { + String result; + if (num == 0) { + return belowTen[num]; + } + + result = hundredHelper(num % 1000); + num = num / 1000; + int i = 0; + while (i < 3 && num > 0) { + if (num % 1000 > 0) { + result = hundredHelper(num % 1000) + overThousand[i] + " " + result; + } + num = num / 1000; + i++; + } + + return result.trim(); + } + + private String hundredHelper(int num) { + String nstr = ""; + if (num >= 100) { + nstr = belowTen[num / 100] + " Hundred "; + } + num = num % 100; + if (num >= 20) { + if (num % 10 != 0) { + nstr = nstr + belowHundred[num / 10 - 2] + " " + belowTen[num % 10] + " "; + } else { + nstr = nstr + belowHundred[num / 10 - 2] + " "; + } + } else if (num >= 10) { + nstr = nstr + belowTwenty[num % 10] + " "; + } else if (num > 0) { + nstr = nstr + belowTen[num] + " "; + } + return nstr; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_274.java b/src/main/java/com/fishercoder/solutions/firstthousand/_274.java new file mode 100644 index 0000000000..a6b72ed646 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_274.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _274 { + public static class Solution1 { + public int hIndex(int[] citations) { + if (citations == null || citations.length == 0) { + return 0; + } + + Arrays.sort(citations); + for (int i = 0; i < citations.length; i++) { + if (citations[i] >= citations.length - i) { + return citations.length - i; + } + } + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_275.java b/src/main/java/com/fishercoder/solutions/firstthousand/_275.java new file mode 100644 index 0000000000..65e49fc1b5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_275.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.firstthousand; + +public class _275 { + public static class Solution1 { + public int hIndex(int[] citations) { + int left = 0; + int len = citations.length; + int right = len - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (citations[mid] >= (len - mid)) { + right = mid - 1; + } else { + left = mid + 1; + } + } + return len - left; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_276.java b/src/main/java/com/fishercoder/solutions/firstthousand/_276.java new file mode 100644 index 0000000000..b37cd86389 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_276.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +public class _276 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/paint-fence/editorial/ + * 1. base case: dp[0] = k; dp[1] = k * k; + * 2. recurrence: dp[i] = dp[i - 1] * (k - 1) + dp[i - 2] * (k - 1) + * dp[i - 1] * (k - 1) means to use a different color than the (i-1)th post to paint ith post + * dp[i - 2] * (k - 1) means to use the same color as the (i - 1)th post, but different from (i - 2)th post to paint the ith post + */ + public int numWays(int n, int k) { + int[] dp = new int[n]; + dp[0] = k; + dp[1] = k * k; + for (int i = 2; i < n; i++) { + dp[i] = dp[i - 1] * (k - 1) + dp[i - 2] * (k - 1); + } + return dp[n - 1]; + } + } + + public static class Solution2 { + /* + * The above solution could be further optimized to use O(1) space. + */ + public int numWays(int n, int k) { + if (n == 0) { + return 0; + } else if (n == 1) { + return k; + } + int sameColorCnt = k; + int diffColorCnt = k * (k - 1); + for (int i = 2; i < n; i++) { + int temp = diffColorCnt; + diffColorCnt = (diffColorCnt + sameColorCnt) * (k - 1); + sameColorCnt = temp; + } + return sameColorCnt + diffColorCnt; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_277.java b/src/main/java/com/fishercoder/solutions/firstthousand/_277.java new file mode 100644 index 0000000000..2631eebf8f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_277.java @@ -0,0 +1,71 @@ +package com.fishercoder.solutions.firstthousand; + +public class _277 { + + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/find-the-celebrity/solution/ approach 2 + * 1. we narrow down the possible celebrity candidate to only one person + * 2. we check to make sure that every other person knows + * this candidate and this candidate doesn't know any one of them, otherwise return -1 + * + * We can think of this is a directed graph problem, a total of n vertices, the one vertex that has zero outgoing edges + * and n - 1 incoming edges is the celebrity. + */ + public int findCelebrity(int n) { + int candidate = 0; + for (int i = 1; i < n; i++) { + if (knows(candidate, i)) { + // this rules out the possibility that candidiate is a celebrity since he/she + // knows i + // so we update candidate to be i, because at least i doesn't know anybody yet. + candidate = i; + } + } + for (int i = 0; i < n; i++) { + if (i != candidate && (knows(candidate, i) || !knows(i, candidate))) { + return -1; + } + } + return candidate; + } + + // this is a mock-up method to make IDE happy.s + boolean knows(int i, int candidate) { + return false; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/21/2021, which turns out to match https://leetcode.com/problems/find-the-celebrity/solution/ Solution 1. + * Time: O(n^2) + * Space: O(1) + */ + public int findCelebrity(int n) { + for (int i = 0; i < n; i++) { + // check if i is the celebrity + int j = 0; + for (; j < n; j++) { + if (i != j) { + if (knows(i, j)) { + break; + } + if (!knows(j, i)) { + break; + } + } + } + if (j == n) { + return i; + } + } + return -1; + } + + // this is a mock-up method to make IDE happy.s + boolean knows(int i, int candidate) { + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_278.java b/src/main/java/com/fishercoder/solutions/firstthousand/_278.java new file mode 100644 index 0000000000..0c031ef2a0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_278.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _278 { + + public static class Solution1 { + public int firstBadVersion(int n) { + int left = 1; + int right = n; + while (left < right) { + int mid = left + (right - left) / 2; + if (isBadVersion(mid)) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + } + + private boolean isBadVersion(int left) { + // this is a fake method to make Eclipse happy + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_279.java b/src/main/java/com/fishercoder/solutions/firstthousand/_279.java new file mode 100644 index 0000000000..56e12c51fd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_279.java @@ -0,0 +1,70 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _279 { + public static class Solution1 { + public int numSquares(int n) { + int result = n; + int num = 2; + while (num * num <= n) { + int temp1 = n / (num * num); + int temp2 = n % (num * num); + result = Math.min(result, temp1 + numSquares(temp2)); + num++; + } + return result; + } + } + + public static class Solution2 { + // DP solution + public int numSquares(int n) { + int[] dp = new int[n + 1]; + Arrays.fill(dp, Integer.MAX_VALUE); + dp[0] = 0; + dp[1] = 1; + for (int i = 1; i <= n; i++) { + int min = Integer.MAX_VALUE; + int j = 1; + while (i - j * j >= 0) { + min = Math.min(min, dp[i - j * j] + 1); + j++; + } + dp[i] = min; + } + return dp[n]; + } + } + + public static class Solution3 { + /* + * My completely original DP solution on 10/14/2021. + *

+ * Again, once you use a pen and paper to visualize your thought process, the idea flows out very quickly. + * Thinking without a pen and paper is a waste of time in most cases! :) + */ + public int numSquares(int n) { + int[] dp = new int[n + 1]; + for (int i = 1; i <= n; i++) { + int x = (int) Math.sqrt(i); + if (Math.pow(x, 2) == i) { + dp[i] = 1; + } else { + dp[i] = i; + int left = 1; + int right = i - 1; + while (left < right) { + dp[i] = Math.min(dp[i], dp[left] + dp[right]); + left++; + right--; + } + if (left == right && i % left == 0) { + dp[i] = Math.min(dp[i], (i / left) * dp[left]); + } + } + } + return dp[n]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_28.java b/src/main/java/com/fishercoder/solutions/firstthousand/_28.java new file mode 100644 index 0000000000..f5b5c58cde --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_28.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.firstthousand; + +public class _28 { + + public static class Solution1 { + public int strStr(String haystack, String needle) { + if (haystack == null || needle == null || haystack.length() < needle.length()) { + return -1; + } + + for (int i = 0; i <= haystack.length() - needle.length(); i++) { + if (haystack.substring(i, i + needle.length()).equals(needle)) { + return i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_280.java b/src/main/java/com/fishercoder/solutions/firstthousand/_280.java new file mode 100644 index 0000000000..941cee003b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_280.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.firstthousand; + +public class _280 { + public static class Solution1 { + public void wiggleSort(int[] nums) { + for (int i = 1; i < nums.length; i++) { + if ((i % 2 == 0 && nums[i] > nums[i - 1]) + || (i % 2 == 1 && nums[i] < nums[i - 1])) { + swap(nums, i); + } + } + } + + void swap(int[] nums, int i) { + int temp = nums[i - 1]; + nums[i - 1] = nums[i]; + nums[i] = temp; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_281.java b/src/main/java/com/fishercoder/solutions/firstthousand/_281.java new file mode 100644 index 0000000000..61464519d0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_281.java @@ -0,0 +1,67 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _281 { + + public static class Solution1 { + public static class ZigzagIterator { + private Iterator i; + private Iterator j; + private Iterator tmp; + + public ZigzagIterator(List v1, List v2) { + i = v2.iterator(); + j = v1.iterator(); + } + + public int next() { + if (j.hasNext()) { + tmp = j; + j = i; + i = tmp; + } + return i.next(); + } + + public boolean hasNext() { + return i.hasNext() || j.hasNext(); + } + } + } + + public static class Solution2 { + public static class ZigzagIterator { + + Queue> queue; + + public ZigzagIterator(List v1, List v2) { + queue = new LinkedList<>(); + if (v1 != null && !v1.isEmpty()) { + Iterator iterator1 = v1.iterator(); + queue.offer(iterator1); + } + if (v2 != null && !v2.isEmpty()) { + Iterator iterator2 = v2.iterator(); + queue.offer(iterator2); + } + } + + public boolean hasNext() { + return !queue.isEmpty(); + } + + public int next() { + Iterator iterator = queue.poll(); + int next = iterator.next(); + if (iterator.hasNext()) { + queue.offer(iterator); + } + return next; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_282.java b/src/main/java/com/fishercoder/solutions/firstthousand/_282.java new file mode 100644 index 0000000000..96e7712825 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_282.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _282 { + + public static class Solution1 { + public List addOperators(String num, int target) { + List res = new ArrayList<>(); + StringBuilder sb = new StringBuilder(); + dfs(res, sb, num, 0, target, 0, 0); + return res; + } + + private void dfs( + List res, + StringBuilder sb, + String num, + int pos, + int target, + long prev, + long multi) { + if (pos == num.length()) { + if (target == prev) { + res.add(sb.toString()); + } + return; + } + for (int i = pos; i < num.length(); i++) { + if (num.charAt(pos) == '0' && i != pos) { + break; + } + long curr = Long.parseLong(num.substring(pos, i + 1)); + int len = sb.length(); + if (pos == 0) { + dfs(res, sb.append(curr), num, i + 1, target, curr, curr); + sb.setLength(len); + } else { + dfs(res, sb.append("+").append(curr), num, i + 1, target, prev + curr, curr); + sb.setLength(len); + + dfs(res, sb.append("-").append(curr), num, i + 1, target, prev - curr, -curr); + sb.setLength(len); + + dfs( + res, + sb.append("*").append(curr), + num, + i + 1, + target, + prev - multi + multi * curr, + multi * curr); + sb.setLength(len); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_283.java b/src/main/java/com/fishercoder/solutions/firstthousand/_283.java new file mode 100644 index 0000000000..adb93161de --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_283.java @@ -0,0 +1,80 @@ +package com.fishercoder.solutions.firstthousand; + +public class _283 { + public static class Solution1 { + public void moveZeroes(int[] nums) { + // keep the last non-zero index and keep overwriting it, then append zeroes to fill the + // end + int j = 0; + int i = 0; + for (; j < nums.length; j++) { + if (nums[j] != 0) { + nums[i++] = nums[j]; + } + } + for (; i < nums.length; i++) { + nums[i] = 0; + } + } + } + + public static class Solution2 { + public void moveZeroes(int[] nums) { + // this solution is the most optimal since it minimizes the number of operations + // the idea is to swap the non-zero element to the first zero number position + for (int i = 0, j = 0; i < nums.length && j < nums.length; j++) { + if (nums[j] != 0) { + int temp = nums[i]; + nums[i++] = nums[j]; + nums[j] = temp; + } + } + } + } + + // then I came up with this solution and got it AC'ed! Cheers! + // basically, find the next non-zero number and swap it with the current zero number + // Apparently it's not the most optimal, since this is basically an O(n^2) solution, then I + // turned to Editorial solutions + public static class Solution3 { + public void moveZeroes(int[] nums) { + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] == 0) { + int j = i + 1; + while (j < nums.length && nums[j] == 0) { + j++; + } + if (j >= nums.length) { + return; + } else { + int temp = nums[j]; + nums[j] = nums[i]; + nums[i] = temp; + } + } + } + } + } + + public static class Solution4 { + /* + * I'm glad that I finally figured this one out completely on my own, this O(n) time, O(1) space solution. + */ + public void moveZeroes(int[] nums) { + int i = 0; // zero index + int j = 0; // non zero index + while (i < nums.length && j < nums.length) { + if (nums[j] != 0) { + if (i < j) { + nums[i] = nums[j]; + nums[j] = 0; + } + } + j++; + while (i < nums.length && nums[i] != 0) { + i++; + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_284.java b/src/main/java/com/fishercoder/solutions/firstthousand/_284.java new file mode 100644 index 0000000000..c8ad71832e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_284.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Queue; + +public class _284 { + public static class Solution1 { + public static class PeekingIterator implements Iterator { + + private Queue queue; + + public PeekingIterator(Iterator iterator) { + // initialize any member here. + queue = new LinkedList<>(); + while (iterator.hasNext()) { + queue.add(iterator.next()); + } + } + + // Returns the next element in the iteration without advancing the iterator. + public Integer peek() { + return queue.peek(); + } + + // hasNext() and next() should behave the same as in the Iterator interface. + // Override them if needed. + @Override + public Integer next() { + return queue.poll(); + } + + @Override + public boolean hasNext() { + return !queue.isEmpty(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_285.java b/src/main/java/com/fishercoder/solutions/firstthousand/_285.java new file mode 100644 index 0000000000..7ccd0be6ae --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_285.java @@ -0,0 +1,94 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public class _285 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/25698/java-python-solution-o-h-time-and-o-1-space-iterative + * The inorder traversal of a BST is the nodes in ascending order. + * To find a successor, you just need to find the smallest one that is larger than the given value since there are no duplicate values in a BST. + * It's just like the binary search in a sorted list. + *

+ * The time complexity should be O(h) where h is the depth of the result node. + * succ is a pointer that keeps the possible successor. + * Whenever you go left the current root is the new possible successor, otherwise it remains the same. + *

+ * Only in a balanced BST O(h) = O(log n). In the worst case h can be as large as n. + */ + public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { + TreeNode successor = null; + while (root != null) { + if (p.val < root.val) { + successor = root; + root = root.left; + } else { + root = root.right; + } + } + return successor; + } + } + + public static class Solution2 { + public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { + TreeMap map = new TreeMap<>(); + inorderTraversal(root, map); + Iterator> iterator = map.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + if (entry.getValue() == p) { + if (iterator.hasNext()) { + return iterator.next().getValue(); + } else { + return null; + } + } + } + return null; + } + + private void inorderTraversal(TreeNode root, TreeMap map) { + if (root == null) { + return; + } + inorderTraversal(root.left, map); + map.put(root.val, root); + inorderTraversal(root.right, map); + return; + } + } + + public static class Solution3 { + public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { + List inorder = new ArrayList<>(); + dfs(root, inorder); + for (int i = 0; i < inorder.size() - 1; i++) { + if (inorder.get(i) == p) { + return inorder.get(i + 1); + } + } + return null; + } + + private List dfs(TreeNode root, List inorder) { + if (root == null) { + return inorder; + } + if (root.left != null) { + dfs(root.left, inorder); + } + inorder.add(root); + if (root.right != null) { + dfs(root.right, inorder); + } + return inorder; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_286.java b/src/main/java/com/fishercoder/solutions/firstthousand/_286.java new file mode 100644 index 0000000000..7728765f48 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_286.java @@ -0,0 +1,74 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _286 { + public static class Solution1 { + + int[] dirs = new int[] {0, 1, 0, -1, 0}; + + public void wallsAndGates(int[][] rooms) { + if (rooms == null || rooms.length == 0 || rooms[0].length == 0) { + return; + } + int m = rooms.length; + int n = rooms[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (rooms[i][j] == 0) { + bfs(rooms, i, j, m, n); + } + } + } + } + + void bfs(int[][] rooms, int i, int j, int m, int n) { + for (int k = 0; k < 4; k++) { + int x = dirs[k] + i; + int y = dirs[k + 1] + j; + if (x >= 0 && y >= 0 && x < m && y < n && rooms[x][y] > rooms[i][j] + 1) { + rooms[x][y] = rooms[i][j] + 1; + bfs(rooms, x, y, m, n); + } + } + } + } + + public static class Solution2 { + + // push all gates into the queue first, and then put all its neighbours into the queue with + // one distance to the gate, then continue to push the rest of the nodes into the queue, and + // put all their neighbours into the queue with the nodes' value plus one until the queue is + // empty + int[] dirs = new int[] {0, 1, 0, -1, 0}; + + public void wallsAndGates(int[][] rooms) { + if (rooms == null || rooms.length == 0 || rooms[0].length == 0) { + return; + } + int m = rooms.length; + int n = rooms[0].length; + Queue queue = new LinkedList(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (rooms[i][j] == 0) { + queue.offer(new int[] {i, j}); + } + } + } + + while (!queue.isEmpty()) { + int[] curr = queue.poll(); + for (int k = 0; k < 4; k++) { + int x = curr[0] + dirs[k]; + int y = curr[1] + dirs[k + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && rooms[x][y] == Integer.MAX_VALUE) { + rooms[x][y] = rooms[curr[0]][curr[1]] + 1; + queue.offer(new int[] {x, y}); + } + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_287.java b/src/main/java/com/fishercoder/solutions/firstthousand/_287.java new file mode 100644 index 0000000000..449352bfde --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_287.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _287 { + + public static class Solution1 { + /* + * no-brainer, used O(n) space + */ + public int findDuplicate(int[] nums) { + Set set = new HashSet<>(); + int dup = 0; + for (int i = 0; i < nums.length; i++) { + if (!set.add(nums[i])) { + dup = nums[i]; + break; + } + } + return dup; + } + } + + public static class Solution2 { + /* + * O(1) space + */ + public int findDuplicate(int[] nums) { + int slow = 0; + int fast = 0; + int finder = 0; + while (true) { + slow = nums[slow]; + fast = nums[nums[fast]]; + + if (slow == fast) { + break; + } + } + + while (true) { + slow = nums[slow]; + finder = nums[finder]; + if (slow == finder) { + return slow; + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_288.java b/src/main/java/com/fishercoder/solutions/firstthousand/_288.java new file mode 100644 index 0000000000..32d8201841 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_288.java @@ -0,0 +1,94 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class _288 { + public static class Solution1 { + + public class ValidWordAbbr { + private Map dict; + + public ValidWordAbbr(String[] dictionary) { + dict = new HashMap(); + for (String word : dictionary) { + String key = + word.length() <= 2 + ? word + : (word.charAt(0) + + String.valueOf(word.length() - 2) + + word.charAt(word.length() - 1)); + if (dict.containsKey(key) && !dict.get(key).equals(word)) { + dict.put(key, ""); + } else { + dict.put(key, word); + } + } + } + + public boolean isUnique(String word) { + String key = + word.length() <= 2 + ? word + : (word.charAt(0) + + String.valueOf(word.length() - 2) + + word.charAt(word.length() - 1)); + if (!dict.containsKey(key)) { + return true; + } else { + return dict.get(key) != "" && dict.get(key).equals(word); + } + } + } + } + + public static class Solution2 { + public class ValidWordAbbr { + + private Map> dict; + + public ValidWordAbbr(String[] dictionary) { + dict = new HashMap(); + for (String word : dictionary) { + String key = + word.length() <= 2 + ? word + : (word.charAt(0) + + String.valueOf(word.length() - 2) + + word.charAt(word.length() - 1)); + if (dict.containsKey(key)) { + Set set = dict.get(key); + set.add(word); + dict.put(key, set); + } else { + Set set = new HashSet(); + set.add(word); + dict.put(key, set); + } + } + } + + public boolean isUnique(String word) { + String key = + word.length() <= 2 + ? word + : (word.charAt(0) + + String.valueOf(word.length() - 2) + + word.charAt(word.length() - 1)); + if (!dict.containsKey(key)) { + return true; + } else { + Set set = dict.get(key); + if (set.size() != 1) { + return false; + } + Iterator it = set.iterator(); + return it.next().equals(word); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_289.java b/src/main/java/com/fishercoder/solutions/firstthousand/_289.java new file mode 100644 index 0000000000..6f3b581502 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_289.java @@ -0,0 +1,120 @@ +package com.fishercoder.solutions.firstthousand; + +public class _289 { + public static class Solution1 { + /* + * Time: O(m*n) + * Space: O(m*n) + */ + public void gameOfLife(int[][] board) { + int height = board.length; + int width = board[0].length; + int[][] next = new int[height][width]; + int[][] directions = { + {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1} + }; + + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + int liveCellsCount = 0; + // count all its live cells + + for (int[] dir : directions) { + int x = i + dir[0]; + int y = j + dir[1]; + if (x >= 0 && y >= 0 && x < height && y < width && board[x][y] == 1) { + liveCellsCount++; + } + } + + if (board[i][j] == 1) { + if (liveCellsCount <= 3 && liveCellsCount >= 2) { + next[i][j] = 1; + } + } else if (board[i][j] == 0) { + if (liveCellsCount == 3) { + next[i][j] = 1; + } + } + } + } + + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + board[i][j] = next[i][j]; + } + } + } + } + + public static class Solution2 { + /* + * Time: O(m*n) + * Space: O(1) + *

+ * we use different numbers to represent its previous state so that we can change in place without losing track of its previous state + * we define: + * live to dead is 1 -> -1 + * dead to live is 0 -> 2 + * live to live is 1 -> 1 + *

+ * Credit: Solution 2 from https://leetcode.com/problems/game-of-life/solution/ + */ + public void gameOfLife(int[][] board) { + // Neighbors array to find 8 neighboring cells for a given cell + int[] neighbors = {0, 1, -1}; + + int rows = board.length; + int cols = board[0].length; + + // Iterate through board cell by cell. + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++) { + + // For each cell count the number of live neighbors. + int liveNeighbors = 0; + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + + if (!(neighbors[i] == 0 && neighbors[j] == 0)) { + int r = (row + neighbors[i]); + int c = (col + neighbors[j]); + + // Check the validity of the neighboring cell. + // and whether it was originally a live cell. + if ((r < rows && r >= 0) + && (c < cols && c >= 0) + && (Math.abs(board[r][c]) == 1)) { + liveNeighbors += 1; + } + } + } + } + + // Rule 1 or Rule 3 + if ((board[row][col] == 1) && (liveNeighbors < 2 || liveNeighbors > 3)) { + // -1 signifies the cell is now dead but originally was live. + board[row][col] = -1; + } + // Rule 4 + if (board[row][col] == 0 && liveNeighbors == 3) { + // 2 signifies the cell is now live but was originally dead. + board[row][col] = 2; + } + } + } + + // Get the final representation for the newly updated board. + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++) { + if (board[row][col] > 0) { + board[row][col] = 1; + } else { + board[row][col] = 0; + } + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_29.java b/src/main/java/com/fishercoder/solutions/firstthousand/_29.java new file mode 100644 index 0000000000..10a7e677cc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_29.java @@ -0,0 +1,90 @@ +package com.fishercoder.solutions.firstthousand; + +public class _29 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/divide-two-integers/solution/ solution 1 + *

+ * Key notes: + * 1. dividend = Integer.MAX_VALUE and divisor = -1 is a special case which will be handled separately; + * 2. because within the given range, [-2_31 to 2_31 - 1], every positive integer could be mapped to a corresponding negative integer while the opposite is not true + * because of the smallest number: Integer.MIN_VALUE = -2147483648 doesn't have one (Integer.MAX_VALUE is 2147483647). So we'll turn both dividend and divisor into negative numbers to do the operation; + * 3. division, in its essence, is subtraction multiple times until it cannot be subtracted any more + *

+ * Time: O(n) + * Space: O(1) + */ + public int divide(int dividend, int divisor) { + if (dividend == Integer.MIN_VALUE && divisor == -1) { + return Integer.MAX_VALUE; + } + int negativeCount = 0; + if (dividend < 0) { + negativeCount++; + } else { + dividend = -dividend; + } + if (divisor < 0) { + negativeCount++; + } else { + divisor = -divisor; + } + + int quotient = 0; + while (dividend <= divisor) { + dividend -= divisor; + quotient++; + } + if (negativeCount == 1) { + quotient = -quotient; + } + return quotient; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/divide-two-integers/solution/ solution 2 + *

+ * 1. exponetial growth to check to speed up + * 2. we still turn all numbers into negatives because negatives are a superset of all numbers in the positives. + *

+ * Time: O(log2n) + * Space: O(1) + */ + private static final int HALF_INT_MIN = Integer.MIN_VALUE / 2; + + public int divide(int dividend, int divisor) { + if (dividend == Integer.MIN_VALUE && divisor == -1) { + return Integer.MAX_VALUE; + } + int negativeCount = 0; + if (dividend < 0) { + negativeCount++; + } else { + dividend = -dividend; + } + if (divisor < 0) { + negativeCount++; + } else { + divisor = -divisor; + } + int quotient = 0; + while (dividend <= divisor) { + int powerOfTwo = -1; + int value = divisor; + while (value >= HALF_INT_MIN && value + value >= dividend) { + value += value; + powerOfTwo += powerOfTwo; + } + quotient += powerOfTwo; + dividend -= value; + } + if (negativeCount != 1) { + quotient = -quotient; + } + return quotient; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_290.java b/src/main/java/com/fishercoder/solutions/firstthousand/_290.java new file mode 100644 index 0000000000..db46f8df94 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_290.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _290 { + + public static class Solution1 { + public boolean wordPattern(String pattern, String str) { + String[] words = str.split(" "); + char[] patterns = pattern.toCharArray(); + Map map = new HashMap(); + if (patterns.length != words.length) { + return false; + } + for (int i = 0; i < patterns.length; i++) { + if (map.containsKey(patterns[i])) { + if (!map.get(patterns[i]).equals(words[i])) { + return false; + } + } else { + if (map.containsValue(words[i])) { + return false; // this is for this case: "abba", "dog dog dog dog" + } + map.put(patterns[i], words[i]); + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_291.java b/src/main/java/com/fishercoder/solutions/firstthousand/_291.java new file mode 100644 index 0000000000..53659c35bd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_291.java @@ -0,0 +1,79 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _291 { + + public static class Solution1 { + /* + * We can try recursively: + * say pattern is "abab", str is "redblueredblue" + * first we try if "a" matches with "r", "b" matches with "e", we find it's not, so we try to see if "b" matches "ed", and so on ... + * then eventually, we find this pattern: + * "a" matches "red" + * "b" matches "blue" + * then we'll just finish the str check based on this pattern + */ + public boolean wordPatternMatch(String pattern, String str) { + Map map = new HashMap(); + Set set = new HashSet(); + return isMatch(str, 0, pattern, 0, map, set); + } + + private boolean isMatch( + String str, + int i, + String pattern, + int j, + Map map, + Set set) { + // base case + if (i == str.length() && j == pattern.length()) { + return true; + } + if (i == str.length() || j == pattern.length()) { + return false; + } + + char c = pattern.charAt(j); + + if (map.containsKey(c)) { + String s = map.get(c); + + // check to see if we can use s to match str.substring(i, i + s.length()) + if (!str.startsWith(s, i)) { + return false; + } + + // if it's match, great, then let's check the rest + return isMatch(str, i + s.length(), pattern, j + 1, map, set); + } + + for (int k = i; k < str.length(); k++) { + String p = str.substring(i, k + 1); + + if (set.contains(p)) { + continue; + } + + map.put(c, p); + set.add(p); + + // continue to match the rest + if (isMatch(str, k + 1, pattern, j + 1, map, set)) { + return true; + } + + // backtracking + map.remove(c); + set.remove(p); + } + + // we've tried everything, but still no luck + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_292.java b/src/main/java/com/fishercoder/solutions/firstthousand/_292.java new file mode 100644 index 0000000000..e5d4a66c4f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_292.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.firstthousand; + +public class _292 { + + public static class Solution1 { + /* + * 1. If there are only 1 or 2 or 3 stones, you could always win by taking 1 or 2 or 3 stones; + * 2. If there are 4 stones, you could never win because no matter you tak 1 or 2 or 3 stones, you could never take the 4th one; + * 3. If there are 5 or 6 or 7 stones, you could always win because no matter how your opponent works, you'll always get the last one; + * 4. Then we could deduce that as long as the number is not divisible by 4, you could always win. + */ + + public boolean canWinNim(int n) { + return n % 4 != 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_293.java b/src/main/java/com/fishercoder/solutions/firstthousand/_293.java new file mode 100644 index 0000000000..af3cffc599 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_293.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _293 { + + public static class Solutoin1 { + public List generatePossibleNextMoves(String s) { + List result = new ArrayList<>(); + for (int i = 1; i < s.length(); i++) { + if (s.charAt(i) == '+' && s.charAt(i - 1) == '+') { + result.add(s.substring(0, i - 1) + "--" + s.substring(i + 1)); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_294.java b/src/main/java/com/fishercoder/solutions/firstthousand/_294.java new file mode 100644 index 0000000000..f8d30cdc7d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_294.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _294 { + + public static class Solution1 { + public boolean canWin(String s) { + List res = new ArrayList<>(); + char[] charArray = s.toCharArray(); + for (int i = 0; i < s.length() - 1; i++) { + if (charArray[i] == '+' && charArray[i + 1] == '+') { + // change these two bits to '-' + charArray[i] = '-'; + charArray[i + 1] = '-'; + res.add(String.valueOf(charArray)); + // change these two bits back to '+' for its next move + charArray[i] = '+'; + charArray[i + 1] = '+'; + } + } + /*The above part is the same of Flip Game I. + * The only added part is the following piece of logic (so-called backtracking.)*/ + for (String str : res) { + if (!canWin(str)) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_295.java b/src/main/java/com/fishercoder/solutions/firstthousand/_295.java new file mode 100644 index 0000000000..af6185769a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_295.java @@ -0,0 +1,123 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Collections; +import java.util.PriorityQueue; +import java.util.Queue; + +public class _295 { + /* + * A few key points for both following solutions: + *

+ * 1. always keep one queue one element more than the other if the number is odd, offer into that one + * first, then poll from that queue and offer into the other queue, then check whether that queue is smaller + * in size than the other, if so, poll one from the other queue and offer it into this queue + *

+ * 2. only need to check whether this bigger queue size is greater than the other queue when returning. + */ + + public static class Solution1 { + public static class MedianFinder { + private Queue large; + private Queue small; + + public MedianFinder() { + large = new PriorityQueue<>(); + small = new PriorityQueue<>(Collections.reverseOrder()); + } + + // Adds a number into the data structure. + public void addNum(int num) { + large.offer((long) num); + small.offer(large.poll()); + if (large.size() < small.size()) { + large.offer(small.poll()); + } + } + + // Returns the median of current data stream + public double findMedian() { + if (large.size() > small.size()) { + return large.peek(); + } + return (large.peek() + small.peek()) / 2.0; + } + } + } + + public static class Solution2 { + public static class MedianFinder { + /* + * credit: https://discuss.leetcode.com/topic/27521/short-simple-java-c-python-o-log-n-o-1 + * The idea is for sure to use two heaps, one is max heap, one is min heap, we always let the max heap have one more element + * than min heap if the total number of elements is not even. + * we could always get the median in O(1) time. + * 1. use Long type to avoid overflow + * 2. negate the numbers for small heap to save the effort for writing a reverse comparator, brilliant! + */ + + private Queue large; + private Queue small; + + /* + * initialize your data structure here. + */ + public MedianFinder() { + large = new PriorityQueue<>(); + small = new PriorityQueue<>(); + } + + // Adds a number into the data structure. + public void addNum(int num) { + large.offer((long) num); + small.offer(-large.poll()); + if (large.size() < small.size()) { + large.offer(-small.poll()); + } + } + + // Returns the median of current data stream + public double findMedian() { + if (large.size() > small.size()) { + return large.peek(); + } + return (large.peek() - small.peek()) / 2.0; + } + } + } + + public static class Solution3 { + public static class MedianFinder { + /* + * The same as Solution2, but not using negation for minHeap. + */ + + private Queue maxHeap; + private Queue minHeap; + + /* + * initialize your data structure here. + */ + public MedianFinder() { + maxHeap = new PriorityQueue<>(); + minHeap = new PriorityQueue<>((a, b) -> (int) (b - a)); + } + + // Adds a number into the data structure. + public void addNum(int num) { + maxHeap.offer((long) num); + minHeap.offer(maxHeap.poll()); + if (maxHeap.size() < minHeap.size()) { + maxHeap.offer(minHeap.poll()); + } + } + + // Returns the median of current data stream + public double findMedian() { + if (maxHeap.size() > minHeap.size()) { + return maxHeap.peek(); + } + return (maxHeap.peek() + minHeap.peek()) / 2.0; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_296.java b/src/main/java/com/fishercoder/solutions/firstthousand/_296.java new file mode 100644 index 0000000000..dd059f3ddd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_296.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _296 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/best-meeting-point/solution/ Approach 3 + */ + public int minTotalDistance(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + List rows = new ArrayList<>(); + List cols = new ArrayList<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + rows.add(i); + cols.add(j); + } + } + } + int rowMedian = rows.get(rows.size() / 2); + Collections.sort(cols); + int colMedian = cols.get(cols.size() / 2); + return minDistance1D(rows, rowMedian) + minDistance1D(cols, colMedian); + } + + private int minDistance1D(List points, int median) { + int distance = 0; + for (int i : points) { + distance += Math.abs(i - median); + } + return distance; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_297.java b/src/main/java/com/fishercoder/solutions/firstthousand/_297.java new file mode 100644 index 0000000000..e6ac2232a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_297.java @@ -0,0 +1,67 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; + +public class _297 { + + public static class Solution1 { + /* + * The idea is very straightforward: + * use "#" as the terminator, do BFS, level order traversal to store all nodes values into a StringBuilder. + * When deserializing, also use a queue: pop the root into the queue first, then use a for loop to construct each node, + * then eventually just return the root. + */ + + // Encodes a tree to a single string. + public String serialize(TreeNode root) { + if (root == null) { + return ""; + } + + StringBuilder sb = new StringBuilder(); + Queue queue = new LinkedList(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (curr == null) { + sb.append("# "); + continue; + } + sb.append(curr.val); + sb.append(" "); + queue.offer(curr.left); + queue.offer(curr.right); + } + } + return sb.toString(); + } + + // Decodes your encoded data to tree. + public TreeNode deserialize(String data) { + if (data == null || data.isEmpty()) { + return null; + } + + String[] nodes = data.split(" "); + TreeNode root = new TreeNode(Integer.parseInt(nodes[0])); + Queue queue = new LinkedList(); + queue.offer(root); + for (int i = 1; i < nodes.length; i++) { + TreeNode curr = queue.poll(); + if (!nodes[i].equals("#")) { + curr.left = new TreeNode(Integer.parseInt(nodes[i])); + queue.offer(curr.left); + } + if (!nodes[++i].equals("#")) { + curr.right = new TreeNode(Integer.parseInt(nodes[i])); + queue.offer(curr.right); + } + } + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_298.java b/src/main/java/com/fishercoder/solutions/firstthousand/_298.java new file mode 100644 index 0000000000..d37498501d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_298.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _298 { + + public static class Solution1 { + private int max = 1; + + public int longestConsecutive(TreeNode root) { + if (root == null) { + return 0; + } + dfs(root, 0, root.val); + return max; + } + + private void dfs(TreeNode root, int curr, int target) { + if (root == null) { + return; + } + if (root.val == target) { + curr++; + } else { + curr = 1; + } + max = Math.max(max, curr); + dfs(root.left, curr, root.val + 1); + dfs(root.right, curr, root.val + 1); + } + } + + public static class Solution2 { + /* + * This is a better solution since it doesn't involve a global variable. + */ + public int longestConsecutive(TreeNode root) { + return dfs(root, 0, root.val); + } + + private int dfs(TreeNode root, int curr, int target) { + if (root == null) { + return 0; + } + if (root.val == target) { + curr++; + } else { + curr = 1; + } + int left = dfs(root.left, curr, root.val + 1); + int right = dfs(root.right, curr, root.val + 1); + return Math.max(curr, Math.max(left, right)); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_299.java b/src/main/java/com/fishercoder/solutions/firstthousand/_299.java new file mode 100644 index 0000000000..6eaf8c6b87 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_299.java @@ -0,0 +1,64 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _299 { + public static class Solution1 { + public String getHint(String secret, String guess) { + int[] secretCows = new int[10]; + int[] guessCows = new int[10]; + int bulls = 0; + for (int i = 0; i < secret.length(); i++) { + if (guess.charAt(i) == secret.charAt(i)) { + bulls++; + } else { + secretCows[Character.getNumericValue(secret.charAt(i))]++; + guessCows[Character.getNumericValue(guess.charAt(i))]++; + } + } + int cows = 0; + for (int i = 0; i < 10; i++) { + cows += Math.min(secretCows[i], guessCows[i]); + } + return bulls + "A" + cows + "B"; + } + } + + public static class Solution2 { + /* + * My completely original solution on 12/24/2021. + */ + public String getHint(String secret, String guess) { + int bulls = 0; + int cows = 0; + boolean[] bulled = new boolean[secret.length()]; + Map map = new HashMap<>(); + for (int i = 0; i < secret.length(); i++) { + if (secret.charAt(i) == guess.charAt(i)) { + bulled[i] = true; + bulls++; + } + } + for (int i = 0; i < secret.length(); i++) { + if (!bulled[i]) { + int num = Character.getNumericValue(secret.charAt(i)); + map.put(num, map.getOrDefault(num, 0) + 1); + } + } + for (int i = 0; i < secret.length(); i++) { + if (!bulled[i]) { + int num = Character.getNumericValue(guess.charAt(i)); + if (map.getOrDefault(num, 0) > 1) { + map.put(num, map.get(num) - 1); + cows++; + } else if (map.getOrDefault(num, 0) == 1) { + map.remove(num); + cows++; + } + } + } + return bulls + "A" + cows + "B"; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_3.java b/src/main/java/com/fishercoder/solutions/firstthousand/_3.java new file mode 100644 index 0000000000..0fe50a228a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_3.java @@ -0,0 +1,164 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _3 { + + public static class Solution1 { + public int lengthOfLongestSubstring(String s) { + int result = 0; + Map map = new HashMap(); + for (int i = 0, j = i; j < s.length(); ) { + if (!map.containsKey(s.charAt(j)) + || (map.containsKey(s.charAt(j)) && map.get(s.charAt(j)) == 0)) { + map.put(s.charAt(j), 1); + result = Math.max(j - i + 1, result); + j++; + } else { + map.put(s.charAt(i), map.get(s.charAt(i)) - 1); + i++; + } + } + return result; + } + } + + public static class Solution2 { + /* + * Sliding Window + * O(n) time + * O(min(m,n)) or O(k) space + */ + public int lengthOfLongestSubstring(String s) { + int n = s.length(); + Set set = new HashSet<>(); + int result = 0; + int i = 0; + int j = 0; + while (i < n && j < n) { + /*Try to extend the range i, j*/ + if (!set.contains(s.charAt(j))) { + set.add(s.charAt(j++)); + result = Math.max(result, j - i); + } else { + set.remove(s.charAt(i++)); + } + } + return result; + } + } + + public static class Solution3 { + /* + * Sliding Window + * O(n) time + * O(n) space + */ + public int lengthOfLongestSubstring(String s) { + if (s.length() == 0) { + return 0; + } + int max = 0; + Map map = new HashMap<>(); + /*Try to extend the range (i, j)*/ + for (int i = 0, j = 0; i < s.length(); i++) { + if (map.containsKey(s.charAt(i))) { + j = Math.max(j, map.get(s.charAt(i)) + 1); + } + map.put(s.charAt(i), i); + max = Math.max(max, i + 1 - j); + } + return max; + } + } + + public static class Solution4 { + /* + * Sliding Window Optimized + * O(n) time + * O(n) space + */ + public int lengthOfLongestSubstring(String s) { + if (s.length() == 0) { + return 0; + } + int max = 0; + int[] index = new int[128]; + /*Try to extend the range (i, j)*/ + for (int i = 0, j = 0; j < s.length(); j++) { + i = Math.max(index[s.charAt(j)], i); + max = Math.max(max, j - i + 1); + index[s.charAt(j)] = j + 1; + } + return max; + } + } + + public static class Solution5 { + /* + * Sliding Window, my completely original idea on 9/17/2021. + * Basically, keep moving the left boundary towards the right and keep updating the result along the way. + * O(n) time + * O(n) space + */ + public int lengthOfLongestSubstring(String s) { + int startIndex = 0; + int longest = 0; + Map map = new HashMap<>(); + for (int i = 0; i < s.length(); i++) { + if (map.containsKey(s.charAt(i))) { + Integer removedIndex = map.get(s.charAt(i)); + if (removedIndex >= startIndex) { + startIndex = removedIndex + 1; + } + } + map.put(s.charAt(i), i); + longest = Math.max(longest, i - startIndex + 1); + } + return longest; + } + } + + public static class Solution6 { + /* + * Sliding Window, my completely original idea on 10/20/2021. Although less efficient then Solution5, it follows a generic template without any manipulation. + * Basically, keep moving the left boundary towards the right and keep updating the result along the way. + * O(n) time + * O(n) space + */ + + public int lengthOfLongestSubstring(String s) { + int left = 0; + int right = 0; + int ans = 0; + Map map = new HashMap<>(); + while (right < s.length()) { + map.put(s.charAt(right), map.getOrDefault(s.charAt(right), 0) + 1); + right++; + if (allUnique(map)) { + ans = Math.max(ans, right - left); + } + while (!allUnique(map)) { + map.put(s.charAt(left), map.get(s.charAt(left)) - 1); + if (map.get(s.charAt(left)) == 0) { + map.remove(s.charAt(left)); + } + left++; + } + } + return ans; + } + + private boolean allUnique(Map map) { + for (char key : map.keySet()) { + if (map.get(key) > 1) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_30.java b/src/main/java/com/fishercoder/solutions/firstthousand/_30.java new file mode 100644 index 0000000000..83d08aee31 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_30.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _30 { + + public static class Solution1 { + /*TODO: this one is not AC'ed. fix this one.*/ + public List findSubstring(String s, String[] words) { + Map map = new HashMap<>(); + for (String word : words) { + map.put(word, 1); + } + List result = new ArrayList<>(); + int startIndex = 0; + int wordLen = words.length; + for (int i = 0; i < s.length(); i++) { + startIndex = i; + Map clone = new HashMap<>(map); + int matchedWord = 0; + for (int j = i + 1; j < s.length(); j++) { + String word = s.substring(i, j); + if (clone.containsKey(word) && clone.get(word) == 1) { + clone.put(word, 0); + i = j; + matchedWord++; + } + if (matchedWord == wordLen) { + boolean all = true; + for (String key : clone.keySet()) { + if (clone.get(key) != 0) { + all = false; + break; + } + } + if (all) { + result.add(startIndex); + } + matchedWord = 0; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_300.java b/src/main/java/com/fishercoder/solutions/firstthousand/_300.java new file mode 100644 index 0000000000..99ec8e614f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_300.java @@ -0,0 +1,124 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _300 { + + public static class Solution1 { + /* + * brute force: + * Time: O(2^n), size of recursion tree will be: 2^n + * Space: O(n^2) + * will result in Time Limit Exceeded exception. + *

+ * The idea is straightforward: we'll iterate through each number, check to see if its next neighbor is smaller or bigger than itself, + * if bigger, then we'll take it, if not, we'll not take it. + */ + public int lengthOfLIS(int[] nums) { + return recursion(nums, Integer.MIN_VALUE, 0); + } + + private int recursion(int[] nums, int prev, int curr) { + if (curr == nums.length) { + return 0; + } + int taken = 0; + if (nums[curr] > prev) { + taken = 1 + recursion(nums, nums[curr], curr + 1); + } + int notTaken = recursion(nums, prev, curr + 1); + return Math.max(taken, notTaken); + } + } + + public static class Solution2 { + /* + * This is an iteration on the previous solution, we use a 2-d array to memoize the previously calculated result + * Time: O(n^2) + * Space: O(n^2) + */ + public int lengthOfLIS(int[] nums) { + int len = nums.length; + int[][] memo = new int[len + 1][len]; + for (int[] m : memo) { + Arrays.fill(m, -1); + } + return recusionWithMemo(nums, -1, 0, memo); + } + + private int recusionWithMemo(int[] nums, int prevIndex, int currIndex, int[][] memo) { + if (currIndex == nums.length) { + return 0; + } + if (memo[prevIndex + 1][currIndex] >= 0) { + // because we initialize all elements in memo to be -1, + // so if it's not -1, then it means we have computed this value before, + // we'll just return it and this way it avoid duplicate recursion + return memo[prevIndex + 1][currIndex]; + } + int taken = 0; + if (prevIndex < 0 || nums[currIndex] > nums[prevIndex]) { + taken = 1 + recusionWithMemo(nums, currIndex, currIndex + 1, memo); + } + int notTaken = recusionWithMemo(nums, prevIndex, currIndex + 1, memo); + memo[prevIndex + 1][currIndex] = Math.max(taken, notTaken); + return memo[prevIndex + 1][currIndex]; + } + } + + public static class Solution3 { + /* + * DP solution, credit: https://leetcode.com/problems/longest-increasing-subsequence/editorial/ + * Time: O(n^2) + * Space: O(n) + */ + public int lengthOfLIS(int[] nums) { + if (nums.length == 0) { + return 0; + } + int[] dp = new int[nums.length]; + Arrays.fill(dp, 1); + for (int i = 1; i < nums.length; i++) { + for (int j = 0; j < i; j++) { + if (nums[i] > nums[j]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + int ans = 1; + for (int val : dp) { + ans = Math.max(ans, val); + } + return ans; + } + } + + public static class Solution4 { + /* + * use binary search. + * Time: O(nlogn) + * Space: O(n) + *

+ * The reason we can use binary search here is all numbers we put into dp array are sorted. + * Arrays.binarySearch() method returns index of the search key, + * if it is contained in the array, else it returns (-(insertion point) - 1). + * The insertion point is the point at which the key would be inserted into the array: + * the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. + */ + public int lengthOfLIS(int[] nums) { + int[] dp = new int[nums.length]; + int len = 0; + for (int num : nums) { + int index = Arrays.binarySearch(dp, 0, len, num); + if (index < 0) { + index = -(index + 1); + } + dp[index] = num; + if (index == len) { + len++; + } + } + return len; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_301.java b/src/main/java/com/fishercoder/solutions/firstthousand/_301.java new file mode 100644 index 0000000000..850bc07f41 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_301.java @@ -0,0 +1,73 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Set; + +public class _301 { + + public static class Solution1 { + + public List removeInvalidParentheses(String s) { + List result = new ArrayList<>(); + if (s == null) { + return result; + } + + Set visited = new HashSet(); + Queue q = new LinkedList(); + + q.offer(s); + visited.add(s); + + boolean found = false; + + while (!q.isEmpty()) { + String curr = q.poll(); + if (isValid(curr)) { + found = true; + result.add(curr); + } + + if (found) { + continue; // this means if the initial input is already a valid one, we'll just + // directly return it and there's actually only one valid result + } + + for (int i = 0; i < curr.length(); i++) { + if (curr.charAt(i) != '(' && curr.charAt(i) != ')') { + continue; // this is to rule out those non-parentheses characters + } + + String next = curr.substring(0, i) + curr.substring(i + 1); + if (!visited.contains(next)) { + q.offer(next); + visited.add(next); + } + } + } + return result; + } + + private boolean isValid(String str) { + char[] chars = str.toCharArray(); + int count = 0; + for (int i = 0; i < chars.length; i++) { + char c = chars[i]; + if (c == '(') { + count++; + } + if (c == ')') { + count--; + if (count == -1) { + return false; + } + } + } + return count == 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_302.java b/src/main/java/com/fishercoder/solutions/firstthousand/_302.java new file mode 100644 index 0000000000..fe785dfa1d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_302.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.firstthousand; + +public class _302 { + public static class Solution1 { + private char[][] image; + + public int minArea(char[][] iImage, int x, int y) { + image = iImage; + int m = image.length; + int n = image[0].length; + int left = searchColumns(0, y, 0, m, true); + int right = searchColumns(y + 1, n, 0, m, false); + int top = searchRows(0, x, left, right, true); + int bottom = searchRows(x + 1, m, left, right, false); + return (right - left) * (bottom - top); + } + + private int searchColumns(int i, int j, int top, int bottom, boolean opt) { + while (i != j) { + int k = top; + int mid = (i + j) / 2; + while (k < bottom && image[k][mid] == '0') { + ++k; + } + if (k < bottom == opt) { + j = mid; + } else { + i = mid + 1; + } + } + return i; + } + + private int searchRows(int i, int j, int left, int right, boolean opt) { + while (i != j) { + int k = left; + int mid = (i + j) / 2; + while (k < right && image[mid][k] == '0') { + ++k; + } + if (k < right == opt) { + j = mid; + } else { + i = mid + 1; + } + } + return i; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_303.java b/src/main/java/com/fishercoder/solutions/firstthousand/_303.java new file mode 100644 index 0000000000..a570cbe995 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_303.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _303 { + public static class NumArray { + int[] sums; + + public NumArray(int[] nums) { + sums = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + if (i == 0) { + sums[i] = nums[i]; + } else { + sums[i] = sums[i - 1] + nums[i]; + } + } + } + + public int sumRange(int i, int j) { + if (i == 0) { + return sums[j]; + } + return sums[j] - sums[i - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_304.java b/src/main/java/com/fishercoder/solutions/firstthousand/_304.java new file mode 100644 index 0000000000..b4a2922bc8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_304.java @@ -0,0 +1,75 @@ +package com.fishercoder.solutions.firstthousand; + +/* + * 304. Range Sum Query 2D - Immutable + * Given a 2D matrix matrix, handle multiple queries of the following type: + *

+ * Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). + * Implement the NumMatrix class: + *

+ * NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix. + * int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). + * You must design an algorithm where sumRegion works on O(1) time complexity. + *

+ * Example 1: + * Input + * ["NumMatrix", "sumRegion", "sumRegion", "sumRegion"] + * [[[[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]], [2, 1, 4, 3], [1, 1, 2, 2], [1, 2, 2, 4]] + * Output + * [null, 8, 11, 12] + *

+ * Explanation + * NumMatrix numMatrix = new NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]); + * numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle) + * numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle) + * numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle) + *

+ * Constraints: + * m == matrix.length + * n == matrix[i].length + * 1 <= m, n <= 200 + * -104 <= matrix[i][j] <= 104 + * 0 <= row1 <= row2 < m + * 0 <= col1 <= col2 < n + * At most 104 calls will be made to sumRegion. + */ +public class _304 { + + public static class Solution1 { + public static class NumMatrix { + int[][] total; + + public NumMatrix(int[][] matrix) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return; + } + + /*The dimensions of this total matrix is actually 1 bigger than the given matrix to make the index mapping easier*/ + int m = matrix.length; + int n = matrix[0].length; + total = new int[m + 1][n + 1]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + total[i + 1][j + 1] = + matrix[i][j] + total[i + 1][j] + total[i][j + 1] - total[i][j]; + } + } + } + + public int sumRegion(int row1, int col1, int row2, int col2) { + // since we deduct both total[row2 + 1][col1] and total[row1][col2 + 1], this means + // we deducted their shared area twice + // which is total[row1][col1] + return total[row2 + 1][col2 + 1] + - total[row2 + 1][col1] + - total[row1][col2 + 1] + + total[row1][col1]; + } + } + } + /* + * Your NumMatrix object will be instantiated and called as such: + * NumMatrix obj = new NumMatrix(matrix); + * int param_1 = obj.sumRegion(row1,col1,row2,col2); + */ +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_305.java b/src/main/java/com/fishercoder/solutions/firstthousand/_305.java new file mode 100644 index 0000000000..e451606bec --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_305.java @@ -0,0 +1,76 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _305 { + public static class Solution1 { + + public int find(int[] father, int id) { + int tf = father[id]; + while (tf != father[tf]) { + tf = father[tf]; + } + int cur = id; + int tmp; + while (father[cur] != tf) { + tmp = father[cur]; + father[cur] = tf; + cur = tmp; + } + return tf; + } + + public void union(int[] father, int[] sz, int id1, int id2) { + int tf1 = find(father, id1); + int tf2 = find(father, id2); + if (tf1 != tf2) { + if (sz[tf1] > sz[tf2]) { + father[tf2] = tf1; + sz[tf1] += sz[tf2]; + } else { + father[tf1] = tf2; + sz[tf2] += sz[tf1]; + } + } + } + + public List numIslands2(int m, int n, int[][] positions) { + if (m == 0 || n == 0) { + return new ArrayList<>(); + } + ArrayList res = new ArrayList(); + int[] father = new int[m * n]; + for (int i = 0; i < father.length; i++) { + father[i] = -1; + } + int[] sz = new int[m * n]; + int[] dr = {0, 0, -1, 1}; + int[] dc = {-1, 1, 0, 0}; + int r; + int c; + int nr; + int nc; + int count = 0; + for (int i = 0; i < positions.length; i++) { + r = positions[i][0]; + c = positions[i][1]; + count++; + father[r * n + c] = r * n + c; + sz[r * n + c] = 1; + for (int j = 0; j < 4; j++) { + nr = r + dr[j]; + nc = c + dc[j]; + if (nr >= 0 && nr < m && nc >= 0 && nc < n && father[nr * n + nc] != -1) { + if (find(father, r * n + c) != find(father, nr * n + nc)) { + count--; + union(father, sz, r * n + c, nr * n + nc); + } + } + } + res.add(count); + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_306.java b/src/main/java/com/fishercoder/solutions/firstthousand/_306.java new file mode 100644 index 0000000000..99cfae52d5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_306.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +public class _306 { + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/29856/java-recursive-and-iterative-solutions/2 + */ + public boolean isAdditiveNumber(String num) { + int n = num.length(); + for (int i = 1; i <= n / 2; ++i) { + for (int j = 1; Math.max(j, i) <= n - i - j; ++j) { + if (isValid(i, j, num)) { + return true; + } + } + } + return false; + } + + private boolean isValid(int i, int j, String num) { + if (num.charAt(0) == '0' && i > 1) { + return false; + } + if (num.charAt(i) == '0' && j > 1) { + return false; + } + String sum; + Long x1 = Long.parseLong(num.substring(0, i)); + Long x2 = Long.parseLong(num.substring(i, i + j)); + for (int start = i + j; start != num.length(); start += sum.length()) { + x2 = x2 + x1; + x1 = x2 - x1; + sum = x2.toString(); + if (!num.startsWith(sum, start)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_307.java b/src/main/java/com/fishercoder/solutions/firstthousand/_307.java new file mode 100644 index 0000000000..4e4459566b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_307.java @@ -0,0 +1,84 @@ +package com.fishercoder.solutions.firstthousand; + +public class _307 { + + public static class Solution1 { + class NumArray { + class SegmentTreeNode { + SegmentTreeNode left; + SegmentTreeNode right; + int start; + int end; + int sum; + + public SegmentTreeNode(int start, int end) { + this.start = start; + this.end = end; + this.left = null; + this.right = null; + this.sum = 0; + } + } + + private SegmentTreeNode root = null; + + public NumArray(int[] nums) { + root = buildSegmentTree(nums, 0, nums.length - 1); + } + + SegmentTreeNode buildSegmentTree(int[] nums, int start, int end) { + if (start > end) { + return null; + } else { + SegmentTreeNode root = new SegmentTreeNode(start, end); + if (start == end) { + root.sum = nums[start]; + } else { + int mid = start + (end - start) / 2; + root.left = buildSegmentTree(nums, start, mid); + root.right = buildSegmentTree(nums, mid + 1, end); + root.sum = root.left.sum + root.right.sum; + } + return root; + } + } + + void update(int i, int val) { + update(root, i, val); + } + + void update(SegmentTreeNode root, int pos, int val) { + if (root.start == root.end) { + root.sum = val; + } else { + int mid = root.start + (root.end - root.start) / 2; + if (pos <= mid) { + update(root.left, pos, val); + } else { + update(root.right, pos, val); + } + root.sum = root.left.sum + root.right.sum; + } + } + + public int sumRange(int i, int j) { + return sumRange(root, i, j); + } + + int sumRange(SegmentTreeNode root, int start, int end) { + if (root.end == end && root.start == start) { + return root.sum; + } else { + int mid = root.start + (root.end - root.start) / 2; + if (end <= mid) { + return sumRange(root.left, start, end); + } else if (start >= mid + 1) { + return sumRange(root.right, start, end); + } else { + return sumRange(root.right, mid + 1, end) + sumRange(root.left, start, mid); + } + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_308.java b/src/main/java/com/fishercoder/solutions/firstthousand/_308.java new file mode 100644 index 0000000000..53ec2ab1f0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_308.java @@ -0,0 +1,67 @@ +package com.fishercoder.solutions.firstthousand; + +public class _308 { + public static class Solution1 { + public class NumMatrix { + int[][] nums; + int[][] tree; + int height; + int width; + + public NumMatrix(int[][] matrix) { + if (matrix.length == 0 || matrix[0].length == 0) { + return; + } + height = matrix.length; + width = matrix[0].length; + this.nums = new int[height][width]; + this.tree = new int[height + 1][width + 1]; + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + update(i, j, matrix[i][j]); + } + } + } + + public void update(int rowIndex, int colIndex, int newVal) { + if (height == 0 || width == 0) { + return; + } + int delta = newVal - nums[rowIndex][colIndex]; + nums[rowIndex][colIndex] = newVal; + for (int i = rowIndex + 1; i <= height; i += i & (-i)) { + for (int j = colIndex + 1; j <= width; j += j & (-j)) { + tree[i][j] += delta; // just use its previous value plus delta is good + } + } + } + + public int sumRegion(int row1, int col1, int row2, int col2) { + if (height == 0 || width == 0) { + return 0; + } + return sum(row2 + 1, col2 + 1) + + sum(row1, col1) + - sum(row1, col2 + 1) + - sum(row2 + 1, col1); + } + + private int sum(int row, int col) { + int sum = 0; + for (int i = row; i > 0; i -= i & (-i)) { + for (int j = col; j > 0; j -= j & (-j)) { + sum += tree[i][j]; + } + } + return sum; + } + } + + /* + * Your NumMatrix object will be instantiated and called as such: + * NumMatrix obj = new NumMatrix(matrix); + * obj.update(row,col,val); + * int param_2 = obj.sumRegion(row1,col1,row2,col2); + */ + } +} diff --git a/src/main/java/com/fishercoder/solutions/_309.java b/src/main/java/com/fishercoder/solutions/firstthousand/_309.java similarity index 86% rename from src/main/java/com/fishercoder/solutions/_309.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_309.java index 6c6914ec95..162e7e3588 100644 --- a/src/main/java/com/fishercoder/solutions/_309.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_309.java @@ -1,23 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; -/** - * 309. Best Time to Buy and Sell Stock with Cooldown - * - * Say you have an array for which the ith element is the price of a given stock on day i. - - Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: - - You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). - After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) - Example: - - prices = [1, 2, 3, 0, 2] - maxProfit = 3 - transactions = [buy, sell, cooldown, buy, sell] - */ public class _309 { public static class Solution1 { - /** + /* * The series of problems are typical dp. The key for dp is to find the variables to * represent the states and deduce the transition function. * @@ -81,8 +66,8 @@ public int maxProfit(int[] prices) { } public static class Solution2 { - /**Surprisingly, this solution is even much faster than the one above provided by the author.*/ - /** + /*Surprisingly, this solution is even much faster than the one above provided by the author.*/ + /* * Here I share my no brainer weapon when it comes to this kind of problems. * * 1. Define States diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_31.java b/src/main/java/com/fishercoder/solutions/firstthousand/_31.java new file mode 100644 index 0000000000..efcf24d1be --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_31.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +public class _31 { + public static class Solution1 { + /* + * Leetcode has a very good article to illustrate this problem and with animation: + * https://leetcode.com/articles/next-permutation/ + * 1. if the array is already in decrementing order, then there's no next larger permutation possible. + * 2. if not, start from the end of the array, find the first pair of numbers that break the decrementing order + * 3. then from that index going to the right again, find the element that is closest bigger than this number, swap them + * 4. reverse the right half of this array after this index to make it sorted increasingly so that it's the next permutation + *

+ * Use this nums as an example: 1,2,5,4,3 + * 1. we first found such an adjacent pair: 2 and 5 that breaks the decrementing order; + * 2. then starting from the right side again, we find a number that's bigger than 2, so that is 3, swap them, the array becomes: 1,3,5,4,2 + * 3. reverse this sub-array: 5,4,2 since they are guaranteed to be decreasing order, so reversing them will give us the next permutation: 1,3,2,4,5 + */ + + public void nextPermutation(int[] nums) { + int i = nums.length - 2; + while (i >= 0 && nums[i] >= nums[i + 1]) { + i--; + } + if (i >= 0) { + int j = nums.length - 1; + while (j >= 0 && nums[i] >= nums[j]) { + j--; + } + + swap(nums, i, j); + } + + reverse(nums, i + 1); + } + + private void reverse(int[] nums, int start) { + int end = nums.length - 1; + while (start <= end) { + swap(nums, start++, end--); + } + } + + private void swap(int[] nums, int i, int j) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_310.java b/src/main/java/com/fishercoder/solutions/firstthousand/_310.java new file mode 100644 index 0000000000..568fc20a7f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_310.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _310 { + + public static class Solution1 { + public List findMinHeightTrees(int n, int[][] edges) { + if (n == 1) { + return Collections.singletonList(0); + } + + List> adj = new ArrayList<>(n); + for (int i = 0; i < n; ++i) { + adj.add(new HashSet<>()); + } + for (int[] edge : edges) { + adj.get(edge[0]).add(edge[1]); + adj.get(edge[1]).add(edge[0]); + } + + List leaves = new ArrayList<>(); + for (int i = 0; i < n; ++i) { + if (adj.get(i).size() == 1) { + leaves.add(i); + } + } + + while (n > 2) { + n -= leaves.size(); + List newLeaves = new ArrayList<>(); + for (int i : leaves) { + int j = adj.get(i).iterator().next(); + adj.get(j).remove(i); + if (adj.get(j).size() == 1) { + newLeaves.add(j); + } + } + leaves = newLeaves; + } + return leaves; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_311.java b/src/main/java/com/fishercoder/solutions/firstthousand/_311.java new file mode 100644 index 0000000000..001e0a82dc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_311.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _311 { + + public static class Solution1 { + public int[][] multiply(int[][] A, int[][] B) { + int m = A.length; + int n = A[0].length; + int p = B[0].length; + int[][] C = new int[m][p]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (A[i][j] != 0) { + for (int k = 0; k < p; k++) { + if (B[j][k] != 0) { + C[i][k] += A[i][j] * B[j][k]; + } + } + } + } + } + return C; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_312.java b/src/main/java/com/fishercoder/solutions/firstthousand/_312.java new file mode 100644 index 0000000000..5ad6ee1b8d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_312.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.firstthousand; + +public class _312 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/burst-balloons/discuss/76228/Share-some-analysis-and-explanations + *

+ * Divide and conquer with memoization + */ + public int maxCoins(int[] nums) { + int[] input = new int[nums.length + 2]; + int n = 1; + for (int x : nums) { + if (x > 0) { + input[n++] = x; + } + } + input[0] = 1; + input[n++] = 1; + + int[][] memo = new int[n][n]; + return burst(memo, input, 0, n - 1); + } + + private int burst(int[][] memo, int[] nums, int left, int right) { + if (left + 1 == right) { + return 0; + } + if (memo[left][right] > 0) { + return memo[left][right]; + } + int ans = 0; + for (int i = left + 1; i < right; i++) { + ans = + Math.max( + ans, + nums[left] * nums[i] * nums[right] + + burst(memo, nums, left, i) + + burst(memo, nums, i, right)); + } + memo[left][right] = ans; + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_313.java b/src/main/java/com/fishercoder/solutions/firstthousand/_313.java new file mode 100644 index 0000000000..37b1551649 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_313.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.firstthousand; + +public class _313 { + + public static class Solution1 { + public int nthSuperUglyNumber(int n, int[] primes) { + int[] ret = new int[n]; + ret[0] = 1; + + int[] indexes = new int[primes.length]; + + for (int i = 1; i < n; i++) { + ret[i] = Integer.MAX_VALUE; + + for (int j = 0; j < primes.length; j++) { + ret[i] = Math.min(ret[i], primes[j] * ret[indexes[j]]); + } + + for (int j = 0; j < indexes.length; j++) { + if (ret[i] == primes[j] * ret[indexes[j]]) { + indexes[j]++; + } + } + } + return ret[n - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_314.java b/src/main/java/com/fishercoder/solutions/firstthousand/_314.java new file mode 100644 index 0000000000..d30ea0ea48 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_314.java @@ -0,0 +1,143 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.TreeMap; + +public class _314 { + public static class Solution1 { + public List> verticalOrder(TreeNode root) { + List> result = new ArrayList(); + if (root == null) { + return result; + } + Queue bfsQ = new LinkedList(); + Queue indexQ = new LinkedList(); + TreeMap> map = new TreeMap(); + bfsQ.offer(root); + indexQ.offer( + 0); // we set the root as index 0, left will be negative, right will be positive + while (!bfsQ.isEmpty()) { + int qSize = bfsQ.size(); + for (int i = 0; i < qSize; i++) { + TreeNode curr = bfsQ.poll(); + int index = indexQ.poll(); + if (map.containsKey(index)) { + map.get(index).add(curr.val); + } else if (!map.containsKey(index)) { + List list = new ArrayList(); + list.add(curr.val); + map.put(index, list); + } + if (curr.left != null) { + bfsQ.offer(curr.left); + indexQ.offer(index - 1); + } + if (curr.right != null) { + bfsQ.offer(curr.right); + indexQ.offer(index + 1); + } + } + } + for (int i : map.keySet()) { + result.add(map.get(i)); + } + return result; + } + } + + public static class Solution2 { + public List> verticalOrder(TreeNode root) { + List> result = new ArrayList(); + if (root == null) { + return result; + } + Queue bfsQ = new LinkedList(); + Queue indexQ = new LinkedList(); + HashMap> map = new HashMap(); + bfsQ.offer(root); + indexQ.offer( + 0); // we set the root as index 0, left will be negative, right will be positive + int min = 0; + int max = 0; + while (!bfsQ.isEmpty()) { + int qSize = bfsQ.size(); + for (int i = 0; i < qSize; i++) { + TreeNode curr = bfsQ.poll(); + int index = indexQ.poll(); + if (map.containsKey(index)) { + map.get(index).add(curr.val); + } else if (!map.containsKey(index)) { + List list = new ArrayList(); + list.add(curr.val); + map.put(index, list); + } + if (curr.left != null) { + bfsQ.offer(curr.left); + indexQ.offer(index - 1); + min = Math.min(min, index - 1); + } + if (curr.right != null) { + bfsQ.offer(curr.right); + indexQ.offer(index + 1); + max = Math.max(max, index + 1); + } + } + } + for (int i = min; i <= max; i++) { + result.add(map.get(i)); + } + return result; + } + } + + public static class Solution3 { + public List> verticalOrder(TreeNode root) { + if (root == null) { + return new ArrayList<>(); + } + TreeMap> map = new TreeMap<>(); + Queue queue = new LinkedList<>(); + queue.offer(new NodeWithIndex(root, 0)); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + NodeWithIndex nodeWithIndex = queue.poll(); + List thisList = + map.getOrDefault(nodeWithIndex.index, new ArrayList<>()); + thisList.add(nodeWithIndex.node.val); + map.put(nodeWithIndex.index, thisList); + if (nodeWithIndex.node.left != null) { + queue.offer( + new NodeWithIndex( + nodeWithIndex.node.left, nodeWithIndex.index - 1)); + } + if (nodeWithIndex.node.right != null) { + queue.offer( + new NodeWithIndex( + nodeWithIndex.node.right, nodeWithIndex.index + 1)); + } + } + } + List> result = new ArrayList<>(); + for (int index : map.keySet()) { + result.add(map.get(index)); + } + return result; + } + + class NodeWithIndex { + TreeNode node; + int index; + + public NodeWithIndex(TreeNode node, int index) { + this.node = node; + this.index = index; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_315.java b/src/main/java/com/fishercoder/solutions/firstthousand/_315.java new file mode 100644 index 0000000000..7a504a0035 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_315.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.List; + +public class _315 { + + public static class Solution1 { + class Node { + int val; + int sum; + int dup = 1; + Node left; + Node right; + + public Node(int v, int s) { + this.val = v; + this.sum = s; + } + } + + public List countSmaller(int[] nums) { + Integer[] ans = new Integer[nums.length]; + Node root = null; + for (int i = nums.length - 1; i >= 0; i--) { + root = insertNode(nums[i], root, i, 0, ans); + } + return Arrays.asList(ans); + } + + Node insertNode(int val, Node node, int i, int prevSum, Integer[] ans) { + if (node == null) { + node = new Node(val, 0); + ans[i] = prevSum; + } else if (val == node.val) { + node.dup += 1; + ans[i] = prevSum + node.sum; + } else if (val > node.val) { + node.right = insertNode(val, node.right, i, prevSum + node.sum + node.dup, ans); + } else { + node.sum += 1; + node.left = insertNode(val, node.left, i, prevSum, ans); + } + + return node; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_316.java b/src/main/java/com/fishercoder/solutions/firstthousand/_316.java new file mode 100644 index 0000000000..7942e9d313 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_316.java @@ -0,0 +1,77 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class _316 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/32259/java-solution-using-stack-with-comments/2 + */ + public String removeDuplicateLetters(String s) { + int[] res = new int[26]; // will contain number of occurences of character (i+'a') + boolean[] visited = + new boolean + [26]; // will contain if character (i+'a') is present in current result + // Stack + char[] ch = s.toCharArray(); + for (char c : ch) { // count number of occurences of character + res[c - 'a']++; + } + Deque st = new ArrayDeque<>(); // answer stack + int index; + for (char c : ch) { + index = c - 'a'; + res[index]--; // decrement number of characters remaining in the string to be + // analysed + if (visited[index]) { + // if character is already present in stack, dont bother + continue; + } + // if current character is smaller than last character in stack which occurs later + // in the string again + // it can be removed and added later e.g stack = bc remaining string abc then a can + // pop b and then c + while (!st.isEmpty() && c < st.peek() && res[st.peek() - 'a'] != 0) { + visited[st.pop() - 'a'] = false; + } + st.push(c); // add current character and mark it as visited + visited[index] = true; + } + + StringBuilder sb = new StringBuilder(); + // pop character from stack and build answer string from back + while (!st.isEmpty()) { + sb.insert(0, st.pop()); + } + return sb.toString(); + } + } + + public static class Solution2 { + /* + * Credit: https://discuss.leetcode.com/topic/31404/a-short-o-n-recursive-greedy-solution + */ + public String removeDuplicateLetters(String s) { + int[] count = new int[26]; + int pos = 0; // the position for the smallest s[i] + for (int i = 0; i < s.length(); i++) { + count[s.charAt(i) - 'a']++; + } + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) < s.charAt(pos)) { + pos = i; + } + count[s.charAt(i) - 'a']--; + if (count[s.charAt(i) - 'a'] == 0) { + break; + } + } + return s.length() == 0 + ? "" + : s.charAt(pos) + + removeDuplicateLetters( + s.substring(pos + 1).replaceAll("" + s.charAt(pos), "")); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_317.java b/src/main/java/com/fishercoder/solutions/firstthousand/_317.java new file mode 100644 index 0000000000..9f7aa20a87 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_317.java @@ -0,0 +1,69 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _317 { + public static class Solution1 { + public int shortestDistance(int[][] grid) { + int m = grid.length; + if (m == 0) { + return -1; + } + int n = grid[0].length; + int[][] reach = new int[m][n]; + int[][] distance = new int[m][n]; + int[] shift = + new int[] { + 0, 1, 0, -1, 0 + }; // how these five elements is ordered is important since it denotes the + // neighbor of the current node + int numBuilding = 0; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + numBuilding++; + int dist = 1; + boolean[][] visited = new boolean[m][n]; + + Queue q = new LinkedList(); + q.offer(new int[] {i, j}); + while (!q.isEmpty()) { + int size = q.size(); + for (int l = 0; l < size; l++) { + int[] current = q.poll(); + for (int k = 0; k < 4; k++) { + int nextRow = current[0] + shift[k]; + int nextCol = current[1] + shift[k + 1]; + if (nextRow >= 0 + && nextRow < m + && nextCol >= 0 + && nextCol < n + && !visited[nextRow][nextCol] + && grid[nextRow][nextCol] == 0) { + distance[nextRow][nextCol] += dist; + visited[nextRow][nextCol] = true; + reach[nextRow][nextCol]++; + q.offer(new int[] {nextRow, nextCol}); + } + } + } + dist++; + } + } + } + } + + int result = Integer.MAX_VALUE; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 0 && reach[i][j] == numBuilding && distance[i][j] < result) { + result = distance[i][j]; + } + } + } + return result == Integer.MAX_VALUE ? -1 : result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_318.java b/src/main/java/com/fishercoder/solutions/firstthousand/_318.java new file mode 100644 index 0000000000..b94057f94b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_318.java @@ -0,0 +1,81 @@ +package com.fishercoder.solutions.firstthousand; + +public class _318 { + public static class Solution1 { + // Inspired by this awesome post: + // https://discuss.leetcode.com/topic/35539/java-easy-version-to-understand + // Idea: this question states that all words consisted of lower case (total only 26 unique + // chars), + // this is a big hint that we could use integer (total 32 bits) to represent each char + // values[i] means how many unique characters this string words[i] has + public int maxProduct(String[] words) { + if (words == null || words.length == 0) { + return 0; + } + int len = words.length; + int[] values = new int[len]; + for (int i = 0; i < words.length; i++) { + String word = words[i]; + for (int j = 0; j < words[i].length(); j++) { + values[i] |= + 1 << (word.charAt(j) - 'a'); // the reason for left shift by this number + // "word.charAt(j) -'a'" is for 'a', otherwise + // 'a' - 'a' will be zero and 'a' will be missed + // out. + } + } + int maxProduct = 0; + for (int i = 0; i < words.length; i++) { + for (int j = 0; j < words.length; j++) { + // check if values[i] AND values[j] equals to zero, this means they share NO + // common chars + if ((values[i] & values[j]) == 0 + && words[i].length() * words[j].length() > maxProduct) { + maxProduct = words[i].length() * words[j].length(); + } + } + } + return maxProduct; + } + } + + public static void main(String... strings) { + _318 test = new _318(); + String[] words = new String[] {"abcw", "baz", "foo", "bar", "xtfn", "abcdef"}; + + // The following is to understand what does left shift by 1 mean: + // the tricky part is to understand how it's written for me: + // "x << y" means left shift x by y bits + // left shift is equivalent to multiplication of powers of 2, so "4 << 1" equals to " 4 * + // 2^1" + // similarly, "4 << 3" equals to "4 * 2^3" which equals "4 * 8" + String sample = "f"; + int bits = 0; + int shiftLeftByHowMany = 0; + int shiftLeftResult = 0; + for (int j = 0; j < sample.length(); j++) { + shiftLeftByHowMany = sample.charAt(j) - 'a'; + shiftLeftResult = 1 << shiftLeftByHowMany; + bits |= 1 << (sample.charAt(j) - 'a'); + // this means shift left 1 by "sample.charAt(j) -'a'" bits + System.out.println( + "nonShiftLeft = " + + shiftLeftByHowMany + + "\tnonShiftLeft binary form is: " + + Integer.toBinaryString(shiftLeftByHowMany) + + "\nshiftLeft = " + + shiftLeftResult + + "\tshiftLeft binary form is: " + + Integer.toBinaryString(shiftLeftResult) + + "\nbits = " + + bits + + "\tbits binary form is: " + + Integer.toBinaryString(bits)); + System.out.println(shiftLeftResult == (1 * Math.pow(2, shiftLeftByHowMany))); + } + + // similarly, right shift is written like this: "x >> y", means shift x by y bits + // 4 >> 3 equals 4 * 2^3, see below: + System.out.println(4 * 8 == (4 * Math.pow(2, 3))); + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_319.java b/src/main/java/com/fishercoder/solutions/firstthousand/_319.java new file mode 100644 index 0000000000..9f46873bae --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_319.java @@ -0,0 +1,13 @@ +package com.fishercoder.solutions.firstthousand; + +public class _319 { + + public static class Solution1 { + public int bulbSwitch(int n) { + if (n < 2) { + return n; + } + return (int) Math.sqrt(n); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_32.java b/src/main/java/com/fishercoder/solutions/firstthousand/_32.java new file mode 100644 index 0000000000..a6a851171a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_32.java @@ -0,0 +1,72 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Stack; + +public class _32 { + public static class Solution1 { + public int longestValidParentheses(String s) { + int result = 0; + Stack stack = new Stack(); + stack.push(-1); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == '(') { + stack.push(i); + } else { + stack.pop(); + if (stack.isEmpty()) { + stack.push(i); + } else { + result = Math.max(result, i - stack.peek()); + } + } + } + return result; + } + } + + public static class Solution2 { + /* + * my lengthy but original solution on 4/5/2021, the idea is to convert the valid parenthesis pairs into numbers and push them onto a stack. + */ + public int longestValidParentheses(String s) { + Stack stack = new Stack<>(); + int longest = 0; + for (char c : s.toCharArray()) { + if (c == '(') { + stack.push(c + ""); + } else { + if (stack.isEmpty()) { + continue; + } else { + if (stack.peek().equals("(")) { + stack.pop(); + stack.push("2"); + } else { + int sum = 0; + while (!stack.isEmpty() && !stack.peek().equals("(")) { + sum += Integer.parseInt(stack.pop()); + } + if (!stack.isEmpty()) { + stack.pop(); // pop off the open paren + sum += 2; + stack.push("" + sum); + } + longest = Math.max(longest, sum); + } + } + } + } + int result = 0; + while (!stack.isEmpty()) { + if (stack.peek().equals("(")) { + stack.pop(); + result = 0; + } else { + result += Integer.parseInt(stack.pop()); + longest = Math.max(result, longest); + } + } + return longest; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_320.java b/src/main/java/com/fishercoder/solutions/firstthousand/_320.java new file mode 100644 index 0000000000..995e13b1ca --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_320.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _320 { + + public static class Solution1 { + public List generateAbbreviations(String word) { + List result = new ArrayList<>(); + backtrack(word, result, 0, "", 0); + return result; + } + + private void backtrack( + String word, List result, int position, String current, int count) { + if (position == word.length()) { + if (count > 0) { + current += count; + } + result.add(current); + } else { + backtrack(word, result, position + 1, current, count + 1); + backtrack( + word, + result, + position + 1, + current + (count > 0 ? count : "") + word.charAt(position), + 0); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_321.java b/src/main/java/com/fishercoder/solutions/firstthousand/_321.java new file mode 100644 index 0000000000..f1d78fbd5e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_321.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +public class _321 { + public static class Solution1 { + public int[] maxNumber(int[] nums1, int[] nums2, int k) { + int n = nums1.length; + int m = nums2.length; + int[] ans = new int[k]; + for (int i = Math.max(0, k - m); i <= k && i <= n; ++i) { + // what is this and why? + int[] candidate = merge(maxArray(nums1, i), maxArray(nums2, k - i), k); + if (greater(candidate, 0, ans, 0)) { + ans = candidate; + } + } + return ans; + } + + private boolean greater(int[] nums1, int i, int[] nums2, int j) { + while (i < nums1.length && j < nums2.length && nums1[i] == nums2[j]) { + i++; + j++; + } + return j == nums2.length || (i < nums1.length && nums1[i] > nums2[j]); + } + + private int[] merge(int[] nums1, int[] nums2, int k) { + int[] ans = new int[k]; + for (int i = 0, j = 0, r = 0; r < k; r++) { + ans[r] = greater(nums1, i, nums2, j) ? nums1[i++] : nums2[j++]; + } + return ans; + } + + private int[] maxArray(int[] nums, int k) { + int n = nums.length; + int[] ans = new int[k]; + for (int i = 0, j = 0; i < n; i++) { + while (n - i + j > k && j > 0 && ans[j - 1] < nums[i]) { + j--; + } + if (j < k) { + ans[j++] = nums[i]; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_322.java b/src/main/java/com/fishercoder/solutions/firstthousand/_322.java new file mode 100644 index 0000000000..d5573e7365 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_322.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +public class _322 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/coin-change-2/discuss/99212/Knapsack-problem-Java-solution-with-thinking-process-O(nm)-Time-and-O(m)-Space + */ + public int coinChange(int[] coins, int amount) { + if (amount < 1) { + return 0; + } + return coinChange(coins, amount, new int[amount]); + } + + private int coinChange(int[] coins, int rem, int[] count) { + if (rem < 0) { + return -1; + } + if (rem == 0) { + return 0; + } + if (count[rem - 1] != 0) { + return count[rem - 1]; + } + int min = Integer.MAX_VALUE; + for (int coin : coins) { + int res = coinChange(coins, rem - coin, count); + if (res >= 0 && res < min) { + min = 1 + res; + } + } + count[rem - 1] = (min == Integer.MAX_VALUE) ? -1 : min; + return count[rem - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_323.java b/src/main/java/com/fishercoder/solutions/firstthousand/_323.java new file mode 100644 index 0000000000..6adc6e7562 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_323.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _323 { + public static class Solution1 { + + public int countComponents(int n, int[][] edges) { + if (n <= 1) { + return n; + } + + List> adList = new ArrayList<>(); + for (int i = 0; i < n; i++) { + adList.add(new ArrayList<>()); + } + + for (int[] edge : edges) { + adList.get(edge[0]).add(edge[1]); + adList.get(edge[1]).add(edge[0]); + } + + for (List list : adList) { + for (int i : list) { + System.out.print(i + ", "); + } + System.out.println(); + } + + boolean[] visited = new boolean[n]; + int count = 0; + for (int i = 0; i < n; i++) { + if (!visited[i]) { + count++; + Queue q = new LinkedList<>(); + q.offer(i); + while (!q.isEmpty()) { + int index = q.poll(); + visited[index] = true; + for (int j : adList.get(index)) { + if (!visited[j]) { + q.offer(j); + } + } + } + } + } + + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_324.java b/src/main/java/com/fishercoder/solutions/firstthousand/_324.java new file mode 100644 index 0000000000..063e556d40 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_324.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Collections; +import java.util.PriorityQueue; + +public class _324 { + + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/41464/step-by-step-explanation-of-index-mapping-in-java + */ + public void wiggleSort(int[] nums) { + int median = findKthLargest(nums, (nums.length + 1) / 2); + int n = nums.length; + + int left = 0; + int i = 0; + int right = n - 1; + + while (i <= right) { + + if (nums[newIndex(i, n)] > median) { + swap(nums, newIndex(left++, n), newIndex(i++, n)); + } else if (nums[newIndex(i, n)] < median) { + swap(nums, newIndex(right--, n), newIndex(i, n)); + } else { + i++; + } + } + } + + private int findKthLargest(int[] nums, int k) { + PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); + for (int i : nums) { + maxHeap.offer(i); + } + while (k-- > 1) { + maxHeap.poll(); + } + return maxHeap.poll(); + } + + private void swap(int[] nums, int i, int j) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + } + + private int newIndex(int index, int n) { + return (1 + 2 * index) % (n | 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_325.java b/src/main/java/com/fishercoder/solutions/firstthousand/_325.java new file mode 100644 index 0000000000..20d8210f4f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_325.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _325 { + + public static class Solution1 { + /* + * 1. This is a beautiful and classic solution that combines prefix sum and hashmap for quick search; + * 2. This actually covers all possible cases and could find the maximum array size + */ + public int maxSubArrayLen(int[] nums, int k) { + Map map = new HashMap(); + int sum = 0; + int max = 0; + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; + if (sum == k) { + max = i + 1; + } else if (map.containsKey(sum - k)) { + max = Math.max(max, i - map.get(sum - k)); + } + if (!map.containsKey(sum)) { + map.put(sum, i); + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_326.java b/src/main/java/com/fishercoder/solutions/firstthousand/_326.java new file mode 100644 index 0000000000..adb1731eab --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_326.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +public class _326 { + public static class Solution1 { + // regular method that has a loop + public boolean isPowerOfThree(int n) { + if (n < 3 && n != 1) { + return false; + } + while (n != 1) { + if (n % 3 != 0) { + return false; + } + n /= 3; + } + return true; + } + } + + public static class Solution2 { + // find the max possible integer that is a power of 3, then do modulor with this number + public boolean isPowerOfThree(int n) { + return (n > 0 && 1162261467 % n == 0); + } + } + + public static class Solution3 { + // Editorial solution: it's pretty elegant to use base conversion which can be easily + // extended to any radix k + // Idea: for a number in base 10, if it's power of 10, then it must be in this format: 10, + // 100, 1000... with a leading one and all trailing zeros + // similarly, if a number is power of 3, then in its base 3 format, it must be in this + // format as well: 10, 100, 1000, 1000... + // some Java built-in function could help us along the way: + public boolean isPowerOfThree(int n) { + return Integer.toString(n, 3).matches("^10*$"); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_327.java b/src/main/java/com/fishercoder/solutions/firstthousand/_327.java similarity index 77% rename from src/main/java/com/fishercoder/solutions/_327.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_327.java index ea018b4f61..bc468af68f 100644 --- a/src/main/java/com/fishercoder/solutions/_327.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_327.java @@ -1,22 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; -/** - * 327. Count of Range Sum - * - * Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. - * Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive. - - Note: - A naive algorithm of O(n2) is trivial. You MUST do better than that. - - Example: - Given nums = [-2, 5, -1], lower = -2, upper = 2, Return 3. - The three ranges are : [0, 0], [2, 2], [0, 2] and their respective sums are: -2, -1, 2. - */ public class _327 { public static class Solution1 { - /** + /* * Time: O(n^2) * This results in TLE on Leetcode by the last test case. */ @@ -60,7 +47,9 @@ private int countWhileMergeSort(long[] sums, int start, int end, int lower, int return 0; } int mid = (start + end) / 2; - int count = countWhileMergeSort(sums, start, mid, lower, upper) + countWhileMergeSort(sums, mid, end, lower, upper); + int count = + countWhileMergeSort(sums, start, mid, lower, upper) + + countWhileMergeSort(sums, mid, end, lower, upper); int j = mid; int k = mid; int t = mid; @@ -82,5 +71,4 @@ private int countWhileMergeSort(long[] sums, int start, int end, int lower, int return count; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_328.java b/src/main/java/com/fishercoder/solutions/firstthousand/_328.java new file mode 100644 index 0000000000..8d6ff8fb30 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_328.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _328 { + + public static class Solution1 { + public ListNode oddEvenList(ListNode head) { + if (head != null) { + ListNode odd = head; + ListNode even = head.next; + ListNode evenHead = even; + + while (even != null && even.next != null) { + odd.next = odd.next.next; + even.next = even.next.next; + odd = odd.next; + even = even.next; + } + + odd.next = evenHead; + } + return head; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_329.java b/src/main/java/com/fishercoder/solutions/firstthousand/_329.java new file mode 100644 index 0000000000..0a9faa305a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_329.java @@ -0,0 +1,44 @@ +package com.fishercoder.solutions.firstthousand; + +public class _329 { + + public static class Solution1 { + final int[] dirs = new int[] {0, 1, 0, -1, 0}; + + public int longestIncreasingPath(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + int max = 0; + int[][] cache = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + int len = dfs(matrix, i, j, cache); + max = Math.max(len, max); + } + } + return max; + } + + int dfs(int[][] matrix, int row, int col, int[][] cache) { + if (cache[row][col] != 0) { + return cache[row][col]; + } + int max = 1; + for (int i = 0; i < dirs.length - 1; i++) { + int nextRow = row + dirs[i]; + int nextCol = col + dirs[i + 1]; + if (nextRow < 0 + || nextRow >= matrix.length + || nextCol < 0 + || nextCol >= matrix[0].length + || matrix[nextRow][nextCol] <= matrix[row][col]) { + continue; + } + int len = 1 + dfs(matrix, nextRow, nextCol, cache); + max = Math.max(max, len); + } + cache[row][col] = max; + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_33.java b/src/main/java/com/fishercoder/solutions/firstthousand/_33.java new file mode 100644 index 0000000000..596798e13b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_33.java @@ -0,0 +1,77 @@ +package com.fishercoder.solutions.firstthousand; + +/* + * 33. Search in Rotated Sorted Array + *

+ * There is an integer array nums sorted in ascending order (with distinct values). + * Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) + * such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). + * For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. + * Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. + *

+ * You must write an algorithm with O(log n) runtime complexity. + *

+ * Example 1: + * Input: nums = [4,5,6,7,0,1,2], target = 0 + * Output: 4 + *

+ * Example 2: + * Input: nums = [4,5,6,7,0,1,2], target = 3 + * Output: -1 + *

+ * Example 3: + * Input: nums = [1], target = 0 + * Output: -1 + *

+ * Constraints: + * 1 <= nums.length <= 5000 + * -104 <= nums[i] <= 104 + * All values of nums are unique. + * nums is an ascending array that is possibly rotated. + * -104 <= target <= 104 + */ +public class _33 { + + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/search-in-rotated-sorted-array/editorial/ + * Approach 3 says it very well. + */ + public int search(int[] nums, int target) { + if (nums == null || nums.length == 0) { + return -1; + } + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = (left + right) / 2; + if (nums[mid] == target) { + return mid; + } + + if (nums[left] <= nums[mid]) { + // this is for this case: [4, 5, 6, 7, 0, 1, 2], target = 4 + // this means that the left sub-array is sorted + if (target >= nums[left] && target < nums[mid]) { + // in this case, if target exists, in must be in this left sorted sub-array + right = mid - 1; + } else { + // otherwise, it's in the other half + // e.g. this case: [4, 5, 6, 7, 0, 1, 2], target = 2 + left = mid + 1; + } + } else { + // this is for this case: [8, 9, 2, 3, 4], target = 9 + // this means the right sub-array is sorted and the left sub-array is rotated at + // some pivot + if (target > nums[mid] && target <= nums[right]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + } + return nums[left] == target ? left : -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_330.java b/src/main/java/com/fishercoder/solutions/firstthousand/_330.java new file mode 100644 index 0000000000..6006d08e77 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_330.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _330 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/articles/patching-array/ and https://discuss.leetcode.com/topic/35494/solution-explanation/2 + *

+ * Let miss be the smallest sum in [0,n] that we might be missing. Meaning we already know we + * can build all sums in [0,miss). Then if we have a number num <= miss in the given array, we + * can add it to those smaller sums to build all sums in [0,miss+num). If we don't, then we must + * add such a number to the array, and it's best to add miss itself, to maximize the reach. + *

+ * Example: Let's say the input is nums = [1, 2, 4, 13, 43] and n = 100. We need to ensure that + * all sums in the range [1,100] are possible. Using the given numbers 1, 2 and 4, we can + * already build all sums from 0 to 7, i.e., the range [0,8). But we can't build the sum 8, and + * the next given number (13) is too large. So we insert 8 into the array. Then we can build all + * sums in [0,16). Do we need to insert 16 into the array? No! We can already build the sum 3, + * and adding the given 13 gives us sum 16. We can also add the 13 to the other sums, extending + * our range to [0,29). And so on. The given 43 is too large to help with sum 29, so we must + * insert 29 into our array. This extends our range to [0,58). But then the 43 becomes useful + * and expands our range to [0,101). At which point we're done. + */ + + public int minPatches(int[] nums, int n) { + long misses = 1; // use long to avoid integer addition overflow + int patches = 0; + int i = 0; + while (misses <= n) { + if (i < nums.length && nums[i] <= misses) { // miss is covered + misses += nums[i++]; + } else { // patch miss to the array + misses += misses; + patches++; // increase the answer + } + } + return patches; + } + + public List findPatches(int[] nums, int n) { + long misses = 1; // use long to avoid integer addition overflow + List patches = new ArrayList<>(); + int i = 0; + while (misses <= n) { + if (i < nums.length && nums[i] <= misses) { // miss is covered + misses += nums[i++]; + } else { // patch miss to the array + patches.add((int) misses); // increase the answer + misses += misses; + } + } + return patches; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_331.java b/src/main/java/com/fishercoder/solutions/firstthousand/_331.java new file mode 100644 index 0000000000..f97d83a085 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_331.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class _331 { + + public static class Solution1 { + public boolean isValidSerialization(String preorder) { + /*Idea: we keep inserting the string into the stack, if it's a number, we just push it onto the stack; + * if it's a "#", we see if the top of the stack is a "#" or not, + * 1. if it's a "#", we pop it and keep popping numbers from the stack, + * 2. if it's not a "#", we push the "#" onto the stack*/ + if (preorder == null || preorder.length() == 0) { + return false; + } + String[] pre = preorder.split(","); + Deque stack = new ArrayDeque<>(); + for (int i = 0; i < pre.length; i++) { + while (pre[i].equals("#") && !stack.isEmpty() && stack.peekLast().equals("#")) { + stack.pollLast(); + if (stack.isEmpty()) { + return false; + } + stack.pollLast(); + } + stack.addLast(pre[i]); + } + return stack.size() == 1 && stack.peekLast().equals("#"); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_332.java b/src/main/java/com/fishercoder/solutions/firstthousand/_332.java new file mode 100644 index 0000000000..e3874890ec --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_332.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; + +public class _332 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/36383/share-my-solution + */ + public List findItinerary(List> tickets) { + Map> flights = new HashMap<>(); + LinkedList path = new LinkedList<>(); + for (List ticket : tickets) { + flights.putIfAbsent(ticket.get(0), new PriorityQueue<>()); + flights.get(ticket.get(0)).add(ticket.get(1)); + } + dfs("JFK", flights, path); + return path; + } + + public void dfs( + String departure, Map> flights, LinkedList path) { + PriorityQueue arrivals = flights.get(departure); + while (arrivals != null && !arrivals.isEmpty()) { + dfs(arrivals.poll(), flights, path); + } + path.addFirst(departure); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_333.java b/src/main/java/com/fishercoder/solutions/firstthousand/_333.java new file mode 100644 index 0000000000..90d11f8632 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_333.java @@ -0,0 +1,99 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _333 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/36995/share-my-o-n-java-code-with-brief-explanation-and-comments + */ + class Result { // (size, rangeLower, rangeUpper) -- size of current tree, range of current + // tree [rangeLower, rangeUpper] + int size; + int lower; + int upper; + + Result(int size, int lower, int upper) { + this.size = size; + this.lower = lower; + this.upper = upper; + } + } + + int max = 0; + + public int largestBSTSubtree(TreeNode root) { + if (root == null) { + return 0; + } + traverse(root); + return max; + } + + private Result traverse(TreeNode root) { + if (root == null) { + return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE); + } + Result left = traverse(root.left); + Result right = traverse(root.right); + if (left.size == -1 + || right.size == -1 + || root.val <= left.upper + || root.val >= right.lower) { + return new Result(-1, 0, 0); + } + int size = left.size + 1 + right.size; + max = Math.max(size, max); + return new Result( + size, Math.min(left.lower, root.val), Math.max(right.upper, root.val)); + } + } + + public int largestBSTSubtree(TreeNode root) { + if (root == null) { + return 0; + } + if (isBST(root)) { + return getNodes(root); + } + return Math.max(find(root.left), find(root.right)); + } + + int find(TreeNode root) { + if (isBST(root)) { + return getNodes(root); + } + return Math.max(find(root.left), find(root.right)); + } + + int getNodes(TreeNode root) { + if (root == null) { + return 0; + } + return dfsCount(root); + } + + int dfsCount(TreeNode root) { + if (root == null) { + return 0; + } + return dfsCount(root.left) + dfsCount(root.right) + 1; + } + + boolean isBST(TreeNode root) { + if (root == null) { + return true; + } + return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE); + } + + boolean dfs(TreeNode root, long min, long max) { + if (root == null) { + return true; + } + if (root.val <= min || root.val >= max) { + return false; + } + return dfs(root.left, min, root.val) && dfs(root.right, root.val, max); + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_334.java b/src/main/java/com/fishercoder/solutions/firstthousand/_334.java new file mode 100644 index 0000000000..b2f96cfd88 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_334.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.firstthousand; + +public class _334 { + public static class Solution1 { + /* + * Time: O(n^2) + * Space: O(1) + */ + public boolean increasingTriplet(int[] nums) { + if (nums == null || nums.length == 0) { + return false; + } + int small = nums[0]; + int medium = Integer.MAX_VALUE; + for (int i = 1; i < nums.length; i++) { + small = Math.min(small, nums[i - 1]); + if (nums[i] > small) { + medium = Math.min(medium, nums[i]); + for (int j = i + 1; j < nums.length; j++) { + if (nums[j] > nums[i] || nums[j] > medium) { + return true; + } + } + } + } + return false; + } + } + + public static class Solution2 { + /* + * Time: O(n) + * Space: O(1) + * + * Idea: we can keep updating these two numbers: + * firstSmallestNumber and secondSmallestNumber + * until we found one that's bigger than both + */ + public boolean increasingTriplet(int[] nums) { + int firstSmallestNumber = Integer.MAX_VALUE; + int secondSmallestNumber = Integer.MAX_VALUE; + for (int num : nums) { + if (num <= firstSmallestNumber) { + firstSmallestNumber = num; + } else if (num <= secondSmallestNumber) { + secondSmallestNumber = num; + } else { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_335.java b/src/main/java/com/fishercoder/solutions/firstthousand/_335.java new file mode 100644 index 0000000000..318e0410dc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_335.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +public class _335 { + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/38014/java-oms-with-explanation/2 + */ + public boolean isSelfCrossing(int[] x) { + int l = x.length; + if (l <= 3) { + return false; + } + + for (int i = 3; i < l; i++) { + if (x[i] >= x[i - 2] && x[i - 1] <= x[i - 3]) { + return true; // Fourth line crosses first line and onward + } + if (i >= 4) { + if (x[i - 1] == x[i - 3] && x[i] + x[i - 4] >= x[i - 2]) { + return true; // Fifth line meets first line and onward + } + } + if (i >= 5) { + if (x[i - 2] - x[i - 4] >= 0 + && x[i] >= x[i - 2] - x[i - 4] + && x[i - 1] >= x[i - 3] - x[i - 5] + && x[i - 1] <= x[i - 3]) { + return true; // Sixth line crosses first line and onward + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_336.java b/src/main/java/com/fishercoder/solutions/firstthousand/_336.java new file mode 100644 index 0000000000..6a792d41e4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_336.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _336 { + + public static class Solution1 { + public List> palindromePairs(String[] words) { + List> pairs = new ArrayList(); + Map map = new HashMap(); + for (int i = 0; i < words.length; i++) { + map.put(words[i], i); + } + + for (int i = 0; i < words.length; i++) { + int l = 0; + int r = 0; + while (l <= r) { + String s = words[i].substring(l, r); + Integer j = map.get(new StringBuilder(s).reverse().toString()); + if (j != null + && j != i + && isPalindrome( + words[i].substring( + l == 0 ? r : 0, l == 0 ? words[i].length() : l))) { + pairs.add( + Arrays.asList( + l == 0 ? new Integer[] {i, j} : new Integer[] {j, i})); + } + if (r < words[i].length()) { + r++; + } else { + l++; + } + } + } + return pairs; + } + + private boolean isPalindrome(String s) { + for (int i = 0; i < s.length() / 2; i++) { + if (s.charAt(i) != s.charAt(s.length() - 1 - i)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_337.java b/src/main/java/com/fishercoder/solutions/firstthousand/_337.java new file mode 100644 index 0000000000..29d4fe666a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_337.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.Map; + +public class _337 { + public static class Solution1 { + // simple recursion without caching: 1189 ms + public int rob(TreeNode root) { + if (root == null) { + return 0; + } + + int val = 0; + if (root.left != null) { + val += rob(root.left.left) + rob(root.left.right); + } + if (root.right != null) { + val += rob(root.right.left) + rob(root.right.right); + } + val = Math.max(val + root.val, rob(root.left) + rob(root.right)); + return val; + } + } + + public static class Solution2 { + // same idea, but with caching via a hashmap: 8 ms + public int rob(TreeNode root) { + Map map = new HashMap<>(); + return getMaxValue(root, map); + } + + private int getMaxValue(TreeNode root, Map map) { + if (root == null) { + return 0; + } + if (map.containsKey(root)) { + return map.get(root); + } + + int val = 0; + if (root.left != null) { + val += getMaxValue(root.left.left, map) + getMaxValue(root.left.right, map); + } + if (root.right != null) { + val += getMaxValue(root.right.left, map) + getMaxValue(root.right.right, map); + } + int max = + Math.max( + root.val + val, + getMaxValue(root.left, map) + getMaxValue(root.right, map)); + map.put(root, max); + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_338.java b/src/main/java/com/fishercoder/solutions/firstthousand/_338.java new file mode 100644 index 0000000000..aa540153f3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_338.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +public class _338 { + public static class Solution1 { + // use the most regular method to get it AC'ed first + public int[] countBits(int num) { + int[] ones = new int[num + 1]; + for (int i = 0; i <= num; i++) { + ones[i] = countOnes(i); + } + return ones; + } + + private int countOnes(int i) { + int ones = 0; + while (i != 0) { + ones++; + i &= (i - 1); + } + return ones; + } + } + + private class Solution2 { + /* + * lixx2100's post is cool:https://discuss.leetcode.com/topic/40162/three-line-java-solution + * An easy recurrence for this problem is f[i] = f[i / 2] + i % 2 + * and then we'll use bit manipulation to express the above recursion function + * right shift by 1 means to divide by 2 + * AND with 1 means to modulo 2 + * this is so cool! + */ + public int[] countBits(int num) { + int[] ones = new int[num + 1]; + for (int i = 1; i <= num; i++) { + ones[i] = ones[i >> 1] + (i & 1); + } + return ones; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_339.java b/src/main/java/com/fishercoder/solutions/firstthousand/_339.java new file mode 100644 index 0000000000..96eb673606 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_339.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.NestedInteger; +import java.util.List; + +public class _339 { + public static class Solution1 { + private int sum = 0; + + public int depthSum(List nestedList) { + return dfs(nestedList, 1); + } + + private int dfs(List nestedList, int depth) { + for (NestedInteger ni : nestedList) { + if (ni.isInteger()) { + sum += depth * ni.getInteger(); + } else { + dfs(ni.getList(), depth + 1); + } + } + return sum; + } + } + + public static class Solution2 { + public int depthSum(List nestedList) { + return dfs(nestedList, 1); + } + + private int dfs(List nestedList, int depth) { + int sum = 0; + for (NestedInteger ni : nestedList) { + sum += ni.isInteger() ? depth * ni.getInteger() : dfs(ni.getList(), depth + 1); + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_34.java b/src/main/java/com/fishercoder/solutions/firstthousand/_34.java new file mode 100644 index 0000000000..0bf13ea76f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_34.java @@ -0,0 +1,141 @@ +package com.fishercoder.solutions.firstthousand; + +public class _34 { + + public static class Solution1 { + public int[] searchRange(int[] nums, int target) { + int[] range = new int[2]; + range[0] = -1; + range[1] = -1; + if (nums == null || nums.length == 0) { + return range; + } + int start = 0; + int end = nums.length - 1; + while (start + 1 < end) { + int mid = start + (end - start) / 2; + if (nums[mid] == target) { + int left = mid; + while (left - 1 >= 0 && nums[left] == nums[left - 1]) { + left--; + } + range[0] = left; + int right = mid; + while (right + 1 < nums.length && nums[right] == nums[right + 1]) { + right++; + } + range[1] = right; + break; + } else if (nums[mid] < target) { + start = mid; + } else { + end = mid; + } + } + + if (nums[start] == target) { + range[0] = start; + while (start + 1 < nums.length && nums[start] == nums[start + 1]) { + start++; + } + range[1] = start; + } else if (nums[end] == target) { + range[1] = end; + while (end - 1 >= 0 && nums[end] == nums[end - 1]) { + end--; + } + range[0] = end; + } + return range; + } + } + + public static class Solution2 { + public int[] searchRange(int[] nums, int target) { + int[] result = new int[] {-1, -1}; + if (nums == null || nums.length == 0) { + return result; + } + if (nums[0] > target) { + return result; + } + if (nums[nums.length - 1] < target) { + return result; + } + int left = 0; + int right = nums.length - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) { + while (mid - 1 >= 0 && nums[mid] == nums[mid - 1]) { + mid--; + } + result[0] = mid; + while (mid + 1 < nums.length && nums[mid] == nums[mid + 1]) { + mid++; + } + result[1] = mid; + return result; + } else if (nums[mid] > target) { + right = mid - 1; + } else { + left = mid + 1; + } + } + return result; + } + } + + public static class Solution3 { + /* + * My completely original solution on 1/15/2022. A great practice to solidify binary search basics. + */ + public int[] searchRange(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + int[] ans = new int[] {-1, -1}; + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] > target) { + right = mid - 1; + } else if (nums[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + ans[0] = mid; + ans[1] = mid; + } + } + if (left < nums.length + && nums[left] != target + && right > 0 + && nums[right] != target + && right + 1 < nums.length + && nums[right + 1] != target) { + return ans; + } + if (left < nums.length && nums[left] == target) { + ans[0] = left; + ans[1] = left; + } + right = nums.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] < target) { + left = mid + 1; + } else if (nums[mid] > target) { + right = mid - 1; + } else { + ans[1] = mid; + left = mid + 1; + } + } + if (right >= 0 && nums[right] == target) { + ans[1] = right; + } else if (left < nums.length && nums[left] == target) { + ans[1] = left; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_340.java b/src/main/java/com/fishercoder/solutions/firstthousand/_340.java new file mode 100644 index 0000000000..efb5f11dde --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_340.java @@ -0,0 +1,88 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _340 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/41671/15-lines-java-solution-using-slide-window + */ + public int lengthOfLongestSubstringKDistinct(String s, int k) { + int[] count = new int[256]; + int left = 0; + int result = 0; + int num = 0; + for (int right = 0; right < s.length(); right++) { + if (count[s.charAt(right)]++ == 0) { + num++; + } + if (num > k) { + while (--count[s.charAt(left++)] > 0) {} + num--; + } + result = Math.max(result, right - left + 1); + } + return result; + } + } + + public static class Solution2 { + /* + * This is a more generic solution for any characters, not limited to ASCII characters. + */ + public int lengthOfLongestSubstringKDistinct(String s, int k) { + Map map = new HashMap<>(); + int longest = 0; + int left = 0; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + map.put(c, map.getOrDefault(c, 0) + 1); + while (map.size() > k) { + char leftChar = s.charAt(left); + if (map.containsKey(leftChar)) { + map.put(leftChar, map.get(leftChar) - 1); + if (map.get(leftChar) == 0) { + map.remove(leftChar); + } + } + left++; + } + longest = Math.max(longest, i - left + 1); + } + return longest; + } + } + + public static class Solution3 { + /* + * My original solution on 10/20/2021, a very generic sliding window template. + */ + public int lengthOfLongestSubstringKDistinct(String s, int k) { + int left = 0; + int right = 0; + int ans = 0; + int[] count = new int[256]; + int distinctCount = 0; + while (right < s.length()) { + if (count[s.charAt(right)] == 0) { + distinctCount++; + } + count[s.charAt(right)]++; + right++; + if (distinctCount <= k) { + ans = Math.max(ans, right - left); + } + while (distinctCount > k) { + count[s.charAt(left)]--; + if (count[s.charAt(left)] == 0) { + distinctCount--; + } + left++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_341.java b/src/main/java/com/fishercoder/solutions/firstthousand/_341.java new file mode 100644 index 0000000000..7136735920 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_341.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.NestedInteger; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _341 { + + public static class Solution1 { + public static class NestedIterator implements Iterator { + + private Queue flattenedList; + + public NestedIterator(List nestedList) { + flattenedList = new LinkedList<>(); + constructList(nestedList); + } + + private void constructList(List nestedList) { + for (NestedInteger nestedInteger : nestedList) { + if (nestedInteger.isInteger()) { + flattenedList.add(nestedInteger.getInteger()); + } else { + constructList(nestedInteger.getList()); + } + } + } + + @Override + public Integer next() { + return flattenedList.poll(); + } + + @Override + public boolean hasNext() { + return !flattenedList.isEmpty(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_342.java b/src/main/java/com/fishercoder/solutions/firstthousand/_342.java new file mode 100644 index 0000000000..844df59828 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_342.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +public class _342 { + public static class Solution1 { + // Just dive a little bit deeper, you can realize that another important feature of a number + // that is power of four is that its only single one bit must appear on the odd position, + // and power of two won't meet this requirement + // decimal number 8 has binary format: 0000-0000-0000-0000-0000-0000-0000-1000 + // decimal number 16 has binary format: 0000-0000-0000-0000-0000-0000-0001-0000 + // hex number 0x55555555 has binary format: 1010-1010-1010-1010-1010-1010-1010-1010 + // thus, doing AND with 0x55555 will check if the only one bit is located on the odd + // position, thus ruling out those that are power of 2 but not power of 4 + public boolean isPowerOfFour(int num) { + return (num > 0 && 1073741824 % num == 0 && (num & 0x55555555) != 0); + } + } + + public static class Solution2 { + public boolean isPowerOfFour(int num) { + // ^ means to match the beginning of a line + // $ means to match the end of a line + // * means zero or more of the preceding character + return Integer.toString(num, 4).matches("^10*$"); + } + } + + public static class Solution3 { + // a regular loop method to make it AC'ed + public boolean isPowerOfFour(int num) { + if (num < 4 && num != 1) { + return false; + } + while (num != 1) { + if (num % 4 != 0) { + return false; + } + num /= 4; + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_343.java b/src/main/java/com/fishercoder/solutions/firstthousand/_343.java new file mode 100644 index 0000000000..1291e5bd57 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_343.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.firstthousand; + +public class _343 { + public static class Solution1 { + public int integerBreak(int n) { + if (n == 2) { + return 1; + } else if (n == 3) { + return 2; + } else if (n % 3 == 0) { + return (int) Math.pow(3, n / 3); + } else if (n % 3 == 1) { + return 2 * 2 * (int) Math.pow(3, (n - 4) / 3); + } else { + return 2 * (int) Math.pow(3, n / 3); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_344.java b/src/main/java/com/fishercoder/solutions/firstthousand/_344.java new file mode 100644 index 0000000000..9142598b06 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_344.java @@ -0,0 +1,13 @@ +package com.fishercoder.solutions.firstthousand; + +public class _344 { + public static class Solution1 { + public void reverseString(char[] s) { + for (int left = 0, right = s.length - 1; left < right; left++, right--) { + char tmp = s[left]; + s[left] = s[right]; + s[right] = tmp; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_345.java b/src/main/java/com/fishercoder/solutions/firstthousand/_345.java new file mode 100644 index 0000000000..906c3956fa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_345.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _345 { + public static class Solution1 { + public String reverseVowels(String s) { + StringBuilder sb = new StringBuilder(s); + Set vowels = + new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')); + // use two pointers approach would be the fastest + int i = 0; + int j = s.length() - 1; + while (i < j) { + char left = s.charAt(i); + char right = s.charAt(j); + while (i < j && !vowels.contains(left)) { + i++; + left = s.charAt(i); + } + while (i < j && !vowels.contains(right)) { + j--; + right = s.charAt(j); + } + char temp = left; + sb.setCharAt(i, right); + sb.setCharAt(j, temp); + i++; + j--; + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_346.java b/src/main/java/com/fishercoder/solutions/firstthousand/_346.java new file mode 100644 index 0000000000..595f00d4a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_346.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _346 { + + public static class Solution1 { + class MovingAverage { + + private Deque q; + private Long sum; + private int max; + + /* + * Initialize your data structure here. + */ + public MovingAverage(int size) { + q = new LinkedList(); + sum = 0L; + max = size; + } + + public double next(int val) { + if (q.size() >= max) { + int first = q.pollFirst(); + sum -= first; + } + sum += val; + q.offer(val); + return (double) sum / q.size(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_347.java b/src/main/java/com/fishercoder/solutions/firstthousand/_347.java new file mode 100644 index 0000000000..3253ac6563 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_347.java @@ -0,0 +1,80 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.*; +import java.util.Map.Entry; + +public class _347 { + + public static class Solution1 { + /* + * Bucket sort: + * Use buckets to hold numbers of the same frequency, some buckets might be empty while the rest might have more than one element. + * This editorial explains it well enough: https://leetcode.com/problems/top-k-frequent-elements/editorial/ starting from 08'55". + *

+ * This is the most optimal solution. + * Time: O(n) + * Space: O(n) + */ + public int[] topKFrequent(int[] nums, int k) { + Map map = new HashMap(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + // use nums.length + 1, so that we can directly use the frequency as the index for this + // array + // how this buckets look like is: buckets[1] holds numbers that have frequency one, + // buckets[2] holds numbers that have frequency two, etc. + // so, the numbers that have the highest frequencies are on the right-most side. + List[] bucket = new ArrayList[nums.length + 1]; + for (Entry entry : map.entrySet()) { + int freq = entry.getValue(); + if (bucket[freq] == null) { + bucket[freq] = new ArrayList(); + } + bucket[freq].add(entry.getKey()); + } + int[] result = new int[k]; + for (int i = bucket.length - 1, l = 0; i >= 0 && l < k; i--) { + if (bucket[i] != null) { + for (int j = 0; j < bucket[i].size(); j++) { + result[l++] = (int) bucket[i].get(j); + } + } + } + return result; + } + } + + public static class Solution2 { + /* + * Use hashtable and heap. + * Time: O(nlogn) + * Space: O(n) + */ + public int[] topKFrequent(int[] nums, int k) { + // construct the frequency map first, and then iterate through the map + // and put them into the heap, this is O(n) + Map map = new HashMap(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + + // build heap, this is O(nlogn) + Queue> heap = + new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue()); + for (Entry entry : map.entrySet()) { + heap.offer(entry); + } + + List result = new ArrayList(); + while (k-- > 0) { + result.add(heap.poll().getKey()); + } + int[] arr = new int[result.size()]; + for (int i = 0; i < arr.length; i++) { + arr[i] = result.get(i); + } + return arr; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_348.java b/src/main/java/com/fishercoder/solutions/firstthousand/_348.java new file mode 100644 index 0000000000..dc348b2e78 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_348.java @@ -0,0 +1,80 @@ +package com.fishercoder.solutions.firstthousand; + +public class _348 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/44548/java-o-1-solution-easy-to-understand + *

+ * Key: in order to win a TicTacToe, you must have the entire row or column, thus, we don't need + * to keep track of the entire n^2 board. We only need to keep a count for each row and column. + * If at any time, a row or column matches the size of the board, then that player has won. + *

+ * We use 1 to denote a move for Player 1, -1 for Player 2. + */ + public static class TicTacToe { + + private int diagonal; + /* + * This is diagonal: + * |X| | | + * | |X| | + * | | |X| + * So, its condition is always like this: if (row == + * col) + */ + + private int antidiagonal; + /* + * This is antidiagonal: + * | | |X| + * | |X| | + * |X| | | + * So, its condition is always like this: if + * (col == size - row - 1) + */ + private int[] rows; + private int[] cols; + + /* + * Initialize your data structure here. + */ + public TicTacToe(int n) { + rows = new int[n]; + cols = new int[n]; + } + + /* + * Player {player} makes a move at ({row}, {col}). + * + * @param row The row of the board. + * @param col The column of the board. + * @param player The player, can be either 1 or 2. + * @return The current winning condition, can be either: 0: No one wins. 1: Player 1 wins. 2: + * Player 2 wins. + */ + public int move(int row, int col, int player) { + int toAdd = player == 1 ? 1 : -1; + + rows[row] += toAdd; + cols[col] += toAdd; + int size = rows.length; + + if (row == col) { + diagonal += toAdd; + } + if (col == (size - row - 1)) { + antidiagonal += toAdd; + } + + if (Math.abs(rows[row]) == size + || Math.abs(cols[col]) == size + || Math.abs(antidiagonal) == size + || Math.abs(diagonal) == size) { + return player; + } + + return 0; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_349.java b/src/main/java/com/fishercoder/solutions/firstthousand/_349.java new file mode 100644 index 0000000000..3c231f3def --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_349.java @@ -0,0 +1,90 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +public class _349 { + + public static class Solution1 { + // credit: https://leetcode.com/articles/intersection-of-two-arrays/ + // Time: O(m+n) on average, O(m*n) in worse case + // Space: O(m+n) + public int[] intersection(int[] nums1, int[] nums2) { + Set set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet()); + Set set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet()); + set1.retainAll(set2); + int[] intersection = new int[set1.size()]; + int i = 0; + for (int num : set1) { + intersection[i++] = num; + } + return intersection; + } + } + + public static class Solution2 { + // Time: O(nlgn) + public int[] intersection(int[] nums1, int[] nums2) { + Arrays.sort(nums2); + Set intersect = new HashSet(); + for (int i : nums1) { + if (binarySearch(i, nums2)) { + intersect.add(i); + } + } + int[] result = new int[intersect.size()]; + int i = 0; + for (int num : intersect) { + result[i++] = num; + } + return result; + } + + private boolean binarySearch(int i, int[] nums) { + int left = 0; + int right = nums.length - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (nums[mid] == i) { + return true; + } else if (nums[mid] > i) { + right = mid - 1; + } else { + left = mid + 1; + } + } + return false; + } + } + + public static class Solution3 { + // use two pointers + // credit: + // https://leetcode.com/problems/intersection-of-two-arrays/discuss/81969/Three-Java-Solutions + public int[] intersection(int[] nums1, int[] nums2) { + Arrays.sort(nums1); + Arrays.sort(nums2); + int i = 0; + int j = 0; + Set set = new HashSet<>(); + while (i < nums1.length && j < nums2.length) { + if (nums1[i] == nums2[j]) { + set.add(nums1[i++]); + j++; + } else if (nums1[i] < nums2[j]) { + i++; + } else { + j++; + } + } + int[] intersection = new int[set.size()]; + int k = 0; + for (int num : set) { + intersection[k++] = num; + } + return intersection; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_35.java b/src/main/java/com/fishercoder/solutions/firstthousand/_35.java new file mode 100644 index 0000000000..c1571735ca --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_35.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _35 { + + public static class Solution1 { + public int searchInsert(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) { + return mid; + } else if (nums[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return nums[left] >= target ? left : left + 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_350.java b/src/main/java/com/fishercoder/solutions/firstthousand/_350.java new file mode 100644 index 0000000000..963116cb5e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_350.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _350 { + public static class Solution1 { + public int[] intersect(int[] nums1, int[] nums2) { + Map map = new HashMap(); + for (int i : nums1) { + map.put(i, map.getOrDefault(i, 0) + 1); + } + + List list = new ArrayList(); + for (int i : nums2) { + if (map.containsKey(i) && map.get(i) > 0) { + list.add(i); + map.put(i, map.get(i) - 1); + } + } + return list.stream().mapToInt(i -> i).toArray(); + } + } + + public static class Solution2 { + public int[] intersect(int[] nums1, int[] nums2) { + Arrays.sort(nums1); + Arrays.sort(nums2); + int i = 0; + int j = 0; + List list = new ArrayList<>(); + while (i < nums1.length && j < nums2.length) { + if (nums1[i] == nums2[j]) { + list.add(nums1[i]); + i++; + j++; + } else if (nums1[i] < nums2[j]) { + i++; + } else { + j++; + } + } + return list.stream().mapToInt(k -> k).toArray(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_351.java b/src/main/java/com/fishercoder/solutions/firstthousand/_351.java new file mode 100644 index 0000000000..5f2e31c99f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_351.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.firstthousand; + +public class _351 { + + public static class Solution1 { + private int[][] jumps; + private boolean[] visited; + + public int numberOfPatterns(int m, int n) { + jumps = new int[10][10]; + jumps[1][3] = jumps[3][1] = 2; + jumps[4][6] = jumps[6][4] = 5; + jumps[7][9] = jumps[9][7] = 8; + jumps[1][7] = jumps[7][1] = 4; + jumps[2][8] = jumps[8][2] = jumps[1][9] = jumps[9][1] = 5; + jumps[9][3] = jumps[3][9] = 6; + jumps[3][7] = jumps[7][3] = 5; + visited = new boolean[10]; + int count = 0; + count += + dfs(1, 1, 0, m, n) + * 4; // 1,3,7,9 are symmetric, so we only need to use 1 to do it once + // and then multiply the result by 4 + count += + dfs(2, 1, 0, m, n) + * 4; // 2,4,6,8 are symmetric, so we only need to use 1 to do it once + // and then multiply the result by 4 + count += dfs(5, 1, 0, m, n); + return count; + } + + private int dfs(int num, int len, int count, int m, int n) { + if (len >= m) { + count++; + } + len++; + if (len > n) { + return count; + } + visited[num] = true; + for (int next = 1; next <= 9; next++) { + int jump = jumps[num][next]; + if (!visited[next] && (jump == 0 || visited[jump])) { + count = dfs(next, len, count, m, n); + } + } + visited[num] = false; // backtracking + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_352.java b/src/main/java/com/fishercoder/solutions/firstthousand/_352.java new file mode 100644 index 0000000000..7af3e74ce9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_352.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.Interval; +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _352 { + + public static class Solution1 { + public static class SummaryRanges { + + /* + * Use treemap, key is the start of the interval. + */ + TreeMap treeMap; + + /* + * Initialize your data structure here. + */ + public SummaryRanges() { + treeMap = new TreeMap<>(); + } + + public void addNum(int val) { + if (treeMap.containsKey(val)) { + return; + } + Integer lower = treeMap.lowerKey(val); + Integer higher = treeMap.higherKey(val); + if (lower != null + && higher != null + && treeMap.get(lower).end + 1 == val + && higher == val + 1) { + treeMap.get(lower).end = treeMap.get(higher).end; + treeMap.remove(higher); + } else if (lower != null && treeMap.get(lower).end + 1 >= val) { + treeMap.get(lower).end = Math.max(treeMap.get(lower).end, val); + } else if (higher != null && higher == val + 1) { + treeMap.put(val, new Interval(val, treeMap.get(higher).end)); + treeMap.remove(higher); + } else { + treeMap.put(val, new Interval(val, val)); + } + } + + public List getIntervals() { + return new ArrayList<>(treeMap.values()); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_353.java b/src/main/java/com/fishercoder/solutions/firstthousand/_353.java new file mode 100644 index 0000000000..cc3b8f5441 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_353.java @@ -0,0 +1,106 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Set; + +public class _353 { + public class SnakeGame { + private Set + set; // Use a set to hold all occupied points for the snake body, this is for easy + // access for the case of eating its own body + private Deque + body; // use a queue to hold all occupied points for the snake body as well, this is + // for easy access to update the tail + int[][] food; + int score; + int foodIndex; + int width; + int height; + + /* + * Initialize your data structure here. + * + * @param width - screen width + * @param height - screen height + * @param food - A list of food positions + * E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. + */ + public SnakeGame(int width, int height, int[][] food) { + this.set = new HashSet(); + set.add(0); // initially at [0][0] + this.body = new LinkedList(); + body.offerLast(0); + this.food = food; + this.width = width; + this.height = height; + } + + /* + * Moves the snake. + * + * @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down + * @return The game's score after the move. Return -1 if game over. + * Game over when snake crosses the screen boundary or bites its body. + */ + public int move(String direction) { + if (score == -1) { + return -1; + } + + // compute head + int rowHead = body.peekFirst() / width; + int colHead = body.peekFirst() % width; + switch (direction) { + case "U": + rowHead--; + break; + case "D": + rowHead++; + break; + case "L": + colHead--; + break; + default: + colHead++; + } + int newHead = rowHead * width + colHead; + + set.remove(body.peekLast()); // we'll remove the tail from set for now to see if it + // hits its tail + // if it hits the boundary + if (set.contains(newHead) + || rowHead < 0 + || colHead < 0 + || rowHead >= height + || colHead >= width) { + return score = -1; + } + + // add head for the following two normal cases: + set.add(newHead); + body.offerFirst(newHead); + + // normal eat case: keep tail, add head + if (foodIndex < food.length + && rowHead == food[foodIndex][0] + && colHead == food[foodIndex][1]) { + set.add(body.peekLast()); // old tail does not change, so add it back to set + // since we removed it earlier + foodIndex++; + return ++score; + } + + // normal move case without eating: move head and remove tail + body.pollLast(); + return score; + } + } + + /* + * Your SnakeGame object will be instantiated and called as such: + * SnakeGame obj = new SnakeGame(width, height, food); + * int param_1 = obj.move(direction); + */ +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_354.java b/src/main/java/com/fishercoder/solutions/firstthousand/_354.java new file mode 100644 index 0000000000..351d67e8d1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_354.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _354 { + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/47469/java-nlogn-solution-with-explanation + */ + public int maxEnvelopes(int[][] envelopes) { + if (envelopes == null + || envelopes.length == 0 + || envelopes[0].length == 0 + || envelopes[0].length != 2) { + return 0; + } + Arrays.sort( + envelopes, + (int[] a, int[] b) -> { + if (a[0] == b[0]) { + return b[1] - a[1]; + } else { + return a[0] - b[0]; + } + }); + int[] dp = new int[envelopes.length]; + int len = 0; + for (int[] envelope : envelopes) { + int index = Arrays.binarySearch(dp, 0, len, envelope[1]); + if (index < 0) { + index = -(index + 1); + } + dp[index] = envelope[1]; + if (index == len) { + len++; + } + } + return len; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_355.java b/src/main/java/com/fishercoder/solutions/firstthousand/_355.java similarity index 75% rename from src/main/java/com/fishercoder/solutions/_355.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_355.java index 399dd6c255..a3b032e659 100644 --- a/src/main/java/com/fishercoder/solutions/_355.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_355.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.HashMap; import java.util.HashSet; @@ -8,47 +8,10 @@ import java.util.PriorityQueue; import java.util.Set; -/** - * 355. Design Twitter - * - * Design a simplified version of Twitter where users can post tweets, - * follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods: - postTweet(userId, tweetId): Compose a new tweet. - getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. - follow(followerId, followeeId): Follower follows a followee. - unfollow(followerId, followeeId): Follower unfollows a followee. - - Example: - - Twitter twitter = new Twitter(); - - // User 1 posts a new tweet (userId = 5). - twitter.postTweet(1, 5); - - // User 1's news feed should return a list with 1 tweet userId -> [5]. - twitter.getNewsFeed(1); - - // User 1 follows user 2. - twitter.follow(1, 2); - - // User 2 posts a new tweet (userId = 6). - twitter.postTweet(2, 6); - - // User 1's news feed should return a list with 2 tweet ids -> [6, 5]. - // Tweet userId 6 should precede tweet userId 5 because it is posted after tweet userId 5. - twitter.getNewsFeed(1); - - // User 1 unfollows user 2. - twitter.unfollow(1, 2); - - // User 1's news feed should return a list with 1 tweet userId -> [5], - // since user 1 is no longer following user 2. - twitter.getNewsFeed(1); - */ public class _355 { public static class Solution1 { - /** + /* * reference: https://discuss.leetcode.com/topic/48100/java-oo-design-with-most-efficient-function-getnewsfeed */ public static class Twitter { @@ -60,9 +23,12 @@ class Tweet { public int time; public int id; public Tweet next; - /**have a pointer, + + /* + * have a pointer, * so we could be more memory efficient when retrieving tweets, - * think about merging k sorted lists*/ + * think about merging k sorted lists + */ public Tweet(int id) { this.id = id; @@ -71,7 +37,7 @@ public Tweet(int id) { } } - /** + /* * the meat part of this OO design problem, * have a User object itself, * have follow() and unfollow() method embedded inside it @@ -84,7 +50,7 @@ class User { public User(int id) { this.id = id; followed = new HashSet<>(); - followed.add(id);//follow oneself first + followed.add(id); // follow oneself first this.tweetHead = null; } @@ -97,25 +63,25 @@ public void unfollow(int followeeId) { } public void postTweet(int tweetId) { - //every time we post, we prepend it to the head of the tweet + // every time we post, we prepend it to the head of the tweet Tweet head = new Tweet(tweetId); head.next = tweetHead; - tweetHead = head;//don't forget to overwrite tweetHead with the new head + tweetHead = head; // don't forget to overwrite tweetHead with the new head } } - /** + /* * Initialize your data structure here. */ public Twitter() { map = new HashMap(); } - /** + /* * Compose a new tweet. */ public void postTweet(int userId, int tweetId) { - /**update oneself newsFeed first and also all of his followers' newsFeed*/ + /*update oneself newsFeed first and also all of his followers' newsFeed*/ if (!map.containsKey(userId)) { User user = new User(userId); map.put(userId, user); @@ -123,7 +89,7 @@ public void postTweet(int userId, int tweetId) { map.get(userId).postTweet(tweetId); } - /** + /* * Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */ public List getNewsFeed(int userId) { @@ -132,10 +98,11 @@ public List getNewsFeed(int userId) { return newsFeed; } Set users = map.get(userId).followed; - PriorityQueue heap = new PriorityQueue<>(users.size(), (a, b) -> b.time - a.time); + PriorityQueue heap = + new PriorityQueue<>(users.size(), (a, b) -> b.time - a.time); for (int user : users) { Tweet tweet = map.get(user).tweetHead; - //it's super important to check null before putting into the heap + // it's super important to check null before putting into the heap if (tweet != null) { heap.offer(tweet); } @@ -154,7 +121,7 @@ public List getNewsFeed(int userId) { return newsFeed; } - /** + /* * Follower follows a followee. If the operation is invalid, it should be a no-op. */ public void follow(int followerId, int followeeId) { @@ -171,7 +138,7 @@ public void follow(int followerId, int followeeId) { map.get(followerId).follow(followeeId); } - /** + /* * Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ public void unfollow(int followerId, int followeeId) { @@ -180,7 +147,7 @@ public void unfollow(int followerId, int followeeId) { } map.get(followerId).unfollow(followeeId); } - /** + /* * Your Twitter object will be instantiated and called as such: * Twitter obj = new Twitter(); * obj.postTweet(userId,tweetId); @@ -222,7 +189,6 @@ public void follow(int followeeId) { public void unfollow(int followeeId) { followed.remove(followeeId); } - } private class Tweet { @@ -237,13 +203,17 @@ public Tweet(int id) { } } - /** Initialize your data structure here. */ + /* + * Initialize your data structure here. + */ public Twitter() { map = new HashMap<>(); timestamp = 0; } - /** Compose a new tweet. */ + /* + * Compose a new tweet. + */ public void postTweet(int userId, int tweetId) { if (!map.containsKey(userId)) { User user = new User(userId); @@ -252,7 +222,9 @@ public void postTweet(int userId, int tweetId) { map.get(userId).postTweet(tweetId); } - /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */ + /* + * Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. + */ public List getNewsFeed(int userId) { List result = new LinkedList<>(); if (!map.containsKey(userId)) { @@ -282,7 +254,9 @@ public List getNewsFeed(int userId) { return result; } - /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ + /* + * Follower follows a followee. If the operation is invalid, it should be a no-op. + */ public void follow(int followerId, int followeeId) { if (!map.containsKey(followerId)) { map.put(followerId, new User(followerId)); @@ -293,7 +267,9 @@ public void follow(int followerId, int followeeId) { map.get(followerId).follow(followeeId); } - /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ + /* + * Follower unfollows a followee. If the operation is invalid, it should be a no-op. + */ public void unfollow(int followerId, int followeeId) { if (!map.containsKey(followerId) || followeeId == followerId) { return; diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_356.java b/src/main/java/com/fishercoder/solutions/firstthousand/_356.java new file mode 100644 index 0000000000..400f57c9bf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_356.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _356 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/48172/simple-java-hashset-solution + */ + public boolean isReflected(int[][] points) { + int max = Integer.MIN_VALUE; + int min = Integer.MAX_VALUE; + Set set = new HashSet<>(); + for (int[] point : points) { + max = Math.max(max, point[0]); + min = Math.min(min, point[0]); + String str = point[0] + "a" + point[1]; + set.add(str); + } + int sum = max + min; + for (int[] p : points) { + String str = (sum - p[0]) + "a" + p[1]; + if (!set.contains(str)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_357.java b/src/main/java/com/fishercoder/solutions/firstthousand/_357.java new file mode 100644 index 0000000000..beabbbc11d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_357.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +public class _357 { + + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/47983/java-dp-o-1-solution Following the hint. + * Let f(n) = count of number with unique digits of length n. f(1) = 10. (0, 1, 2, 3, ...., 9) + * f(2) = 9 * 9. Because for each number i from 1, ..., 9, we can pick j to form a 2-digit + * number ij and there are 9 numbers that are different from i for j to choose from. f(3) = f(2) + * * 8 = 9 * 9 * 8. Because for each number with unique digits of length 2, say ij, we can pick + * k to form a 3 digit number ijk and there are 8 numbers that are different from i and j for k + * to choose from. Similarly f(4) = f(3) * 7 = 9 * 9 * 8 * 7.... ... f(10) = 9 * 9 * 8 * 7 * 6 * + * ... * 1 f(11) = 0 = f(12) = f(13).... any number with length > 10 couldn't be unique digits + * number. The problem is asking for numbers from 0 to 10^n. Hence return f(1) + f(2) + .. + + * f(n) As @4acreg suggests, There are only 11 different ans. You can create a lookup table for + * it. This problem is O(1) in essence. + */ + public int countNumbersWithUniqueDigits(int n) { + if (n == 0) { + return 1; + } + int res = 10; + int uniqueDigits = 9; + int availableNumber = 9; + while (n-- > 1 && availableNumber > 0) { + uniqueDigits = uniqueDigits * availableNumber; + res += uniqueDigits; + availableNumber--; + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_358.java b/src/main/java/com/fishercoder/solutions/firstthousand/_358.java new file mode 100644 index 0000000000..e7dfc22107 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_358.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Queue; + +public class _358 { + + public static class Solution1 { + public String rearrangeString(String s, int k) { + Map count = new HashMap<>(); + for (char c : s.toCharArray()) { + count.put(c, count.getOrDefault(c, 0) + 1); + } + + PriorityQueue> heap = + new PriorityQueue<>((a, b) -> b.getValue() - a.getValue()); + heap.addAll(count.entrySet()); + + Queue> waitQueue = new LinkedList<>(); + + StringBuilder stringBuilder = new StringBuilder(); + while (!heap.isEmpty()) { + Map.Entry entry = heap.poll(); + stringBuilder.append(entry.getKey()); + entry.setValue(entry.getValue() - 1); + waitQueue.offer(entry); + if (waitQueue.size() < k) { + continue; // there's only k-1 chars in the waitHeap, not full yet + } + Map.Entry front = waitQueue.poll(); + if (front.getValue() > 0) { + heap.offer(front); + } + } + + return stringBuilder.length() == s.length() ? stringBuilder.toString() : ""; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_359.java b/src/main/java/com/fishercoder/solutions/firstthousand/_359.java new file mode 100644 index 0000000000..d723e833ca --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_359.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _359 { + + public static class Solution1 { + class Logger { + + private Map map; + + public Logger() { + map = new HashMap<>(); + } + + public boolean shouldPrintMessage(int timestamp, String message) { + if (!map.containsKey(message)) { + map.put(message, timestamp); + return true; + } else { + if (timestamp - map.get(message) < 10) { + return false; + } else { + map.put(message, timestamp); + return true; + } + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_36.java b/src/main/java/com/fishercoder/solutions/firstthousand/_36.java similarity index 98% rename from src/main/java/com/fishercoder/solutions/_36.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_36.java index 6f2f69bf01..72bd8569ce 100644 --- a/src/main/java/com/fishercoder/solutions/_36.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_36.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _36 { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_360.java b/src/main/java/com/fishercoder/solutions/firstthousand/_360.java new file mode 100644 index 0000000000..dd5400eab2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_360.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _360 { + + public static class Solution1 { + // credit: + // https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution + // in sum, only two cases: when a >= 0 or when a < 0, this simplifies logic + public int[] sortTransformedArray(int[] nums, int a, int b, int c) { + int n = nums.length; + int[] sorted = new int[n]; + int i = 0; + int j = n - 1; + int index = a >= 0 ? n - 1 : 0; + while (i <= j) { + if (a >= 0) { + sorted[index--] = + function(nums[i], a, b, c) >= function(nums[j], a, b, c) + ? function(nums[i++], a, b, c) + : function(nums[j--], a, b, c); + } else { + sorted[index++] = + function(nums[i], a, b, c) >= function(nums[j], a, b, c) + ? function(nums[j--], a, b, c) + : function(nums[i++], a, b, c); + } + } + return sorted; + } + + private int function(int num, int a, int b, int c) { + return a * (num * num) + b * num + c; + } + } + + public static class Solution2 { + public int[] sortTransformedArray(int[] nums, int a, int b, int c) { + int[] result = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + result[i] = function(nums[i], a, b, c); + } + Arrays.sort(result); + return result; + } + + private int function(int num, int a, int b, int c) { + return a * (num * num) + b * num + c; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_361.java b/src/main/java/com/fishercoder/solutions/firstthousand/_361.java new file mode 100644 index 0000000000..8486415d5d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_361.java @@ -0,0 +1,74 @@ +package com.fishercoder.solutions.firstthousand; + +public class _361 { + + public static class Solution1 { + public int maxKilledEnemies(char[][] grid) { + int m = grid.length; + if (grid == null || m == 0) { + return 0; + } + int n = grid[0].length; + + int[][] max = new int[m][n]; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + + if (grid[i][j] == '0') { + int count = 0; + + // count all possible hits in its upward direction + for (int k = j - 1; k >= 0; k--) { + if (grid[i][k] == 'E') { + count++; + } else if (grid[i][k] == 'W') { + break; + } + } + + // count all possible hits in its downward direction + for (int k = j + 1; k < n; k++) { + if (grid[i][k] == 'E') { + count++; + } else if (grid[i][k] == 'W') { + break; + } + } + + // count all possible hits in its right direction + for (int k = i + 1; k < m; k++) { + if (grid[k][j] == 'E') { + count++; + } else if (grid[k][j] == 'W') { + break; + } + } + + // count all possible hits in its left direction + for (int k = i - 1; k >= 0; k--) { + if (grid[k][j] == 'E') { + count++; + } else if (grid[k][j] == 'W') { + break; + } + } + + max[i][j] = count; + } + } + } + + int result = 0; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (max[i][j] > result) { + result = max[i][j]; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_362.java b/src/main/java/com/fishercoder/solutions/firstthousand/_362.java new file mode 100644 index 0000000000..21655e4e18 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_362.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +public class _362 { + + public static class Solution1 { + public static class HitCounter { + /* + * Reference: https://discuss.leetcode.com/topic/48758/super-easy-design-o-1-hit-o-s-gethits-no-fancy-data-structure-is-needed, + * I added one more field k to make it more generic. + * It basically maintains a window of size 300, use modular to update the index. + */ + private int[] times; + private int[] hits; + private int k; + + public HitCounter() { + k = 300; + times = new int[k]; + hits = new int[k]; + } + + public void hit(int timestamp) { + int index = timestamp % k; + if (times[index] != timestamp) { + times[index] = timestamp; + hits[index] = 1; + } else { + hits[index]++; + } + } + + public int getHits(int timestamp) { + int total = 0; + for (int i = 0; i < k; i++) { + if (timestamp - times[i] < k) { + total += hits[i]; + } + } + return total; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_363.java b/src/main/java/com/fishercoder/solutions/firstthousand/_363.java new file mode 100644 index 0000000000..19c96723cf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_363.java @@ -0,0 +1,44 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.TreeSet; + +public class _363 { + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/48854/java-binary-search-solution-time-complexity-min-m-n-2-max-m-n-log-max-m-n + */ + public int maxSumSubmatrix(int[][] matrix, int k) { + int row = matrix.length; + if (row == 0) { + return 0; + } + int col = matrix[0].length; + int m = Math.min(row, col); + int n = Math.max(row, col); + // indicating sum up in every row or every column + boolean colIsBig = col > row; + int res = Integer.MIN_VALUE; + for (int i = 0; i < m; i++) { + int[] array = new int[n]; + // sum from row j to row i + for (int j = i; j >= 0; j--) { + int val = 0; + TreeSet set = new TreeSet<>(); + set.add(0); + // traverse every column/row and sum up + for (int p = 0; p < n; p++) { + array[p] = array[p] + (colIsBig ? matrix[j][p] : matrix[p][j]); + val = val + array[p]; + // use TreeMap to binary search previous sum to get possible result + Integer subres = set.ceiling(val - k); + if (null != subres) { + res = Math.max(res, val - subres); + } + set.add(val); + } + } + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_364.java b/src/main/java/com/fishercoder/solutions/firstthousand/_364.java new file mode 100644 index 0000000000..3e93854efb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_364.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.NestedInteger; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _364 { + + public static class Solution1 { + public int depthSumInverse(List nestedList) { + Queue q = new LinkedList<>(); + for (NestedInteger next : nestedList) { + q.offer(next); + } + int prev = 0; + int total = 0; + + while (!q.isEmpty()) { + int size = q.size(); + int levelSum = 0; + for (int i = 0; i < size; i++) { + NestedInteger next = q.poll(); + if (next.isInteger()) { + levelSum += next.getInteger(); + } else { + List list = next.getList(); + for (NestedInteger n : list) { + q.offer(n); + } + } + } + prev += levelSum; + total += prev; + } + return total; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_365.java b/src/main/java/com/fishercoder/solutions/firstthousand/_365.java new file mode 100644 index 0000000000..50d3242aa0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_365.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _365 { + + public static class Solution1 { + public boolean canMeasureWater(int x, int y, int z) { + if (x + y < z) { + return false; + } + if (x == z || y == z || x + y == z) { + return true; + } + return z % gcd(x, y) == 0; + } + + int gcd(int x, int y) { + while (y != 0) { + int temp = y; + y = x % y; + x = temp; + } + return x; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_366.java b/src/main/java/com/fishercoder/solutions/firstthousand/_366.java new file mode 100644 index 0000000000..778b8d1d05 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_366.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _366 { + + public static class Solution1 { + List> result = new ArrayList<>(); + + public List> findLeaves(TreeNode root) { + dfs(root); + return result; + } + + int dfs(TreeNode root) { + if (root == null) { + return 0; + } + int level = Math.max(dfs(root.left), dfs(root.right)) + 1; + if (result.size() < level) { + result.add(new ArrayList<>()); + } + result.get(level - 1).add(root.val); + return level; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_367.java b/src/main/java/com/fishercoder/solutions/firstthousand/_367.java similarity index 86% rename from src/main/java/com/fishercoder/solutions/_367.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_367.java index a050117cba..3d6c1dbcc3 100644 --- a/src/main/java/com/fishercoder/solutions/_367.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_367.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _367 { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_368.java b/src/main/java/com/fishercoder/solutions/firstthousand/_368.java new file mode 100644 index 0000000000..8807cf16ae --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_368.java @@ -0,0 +1,79 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _368 { + + public static class Solution1 { + /* + * DP solution, credit: https://leetcode.com/problems/largest-divisible-subset/solution/ Solution 1 + */ + public List largestDivisibleSubset(int[] nums) { + List> lists = new ArrayList<>(); + Arrays.sort(nums); + int len = nums.length; + if (len == 0) { + return new ArrayList<>(); + } + for (int i = 0; i < len; i++) { + lists.add(new ArrayList<>()); + } + for (int i = 0; i < len; i++) { + List maxSubset = new ArrayList<>(); + for (int k = 0; k < i; k++) { + if (nums[i] % nums[k] == 0 && maxSubset.size() < lists.get(k).size()) { + maxSubset = lists.get(k); + } + } + lists.get(i).addAll(maxSubset); + lists.get(i).add(nums[i]); + } + List ans = new ArrayList<>(); + for (List list : lists) { + if (list.size() > ans.size()) { + ans.clear(); + ans.addAll(list); + } + } + return ans; + } + } + + public static class Solution2 { + /* + * Credit: https://discuss.leetcode.com/topic/49652/classic-dp-solution-similar-to-lis-o-n-2 + */ + public List largestDivisibleSubset(int[] nums) { + int len = nums.length; + int[] count = new int[len]; + int[] pre = new int[len]; + Arrays.sort(nums); + int max = 0; + int index = -1; + for (int i = 0; i < len; i++) { + count[i] = 1; + pre[i] = -1; + for (int j = i - 1; j >= 0; j--) { + if (nums[i] % nums[j] == 0) { + if (1 + count[j] > count[i]) { + count[i] = count[j] + 1; + pre[i] = j; + } + } + } + if (count[i] > max) { + max = count[i]; + index = i; + } + } + List res = new ArrayList<>(); + while (index != -1) { + res.add(nums[index]); + index = pre[index]; + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_369.java b/src/main/java/com/fishercoder/solutions/firstthousand/_369.java new file mode 100644 index 0000000000..7b472fb55d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_369.java @@ -0,0 +1,89 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _369 { + + public static class Solution1 { + public ListNode plusOne(ListNode head) { + // get the length of the list and take out the value of each node and store them into an + // array + ListNode temp = head; + int len = 0; + while (temp != null) { + len++; + temp = temp.next; + } + + int[] nums = new int[len]; + temp = head; + int j = 0; + while (temp != null) { + nums[j++] = temp.val; + temp = temp.next; + } + + // plus one into this array: nums + for (int i = len - 1; i >= 0; i--) { + if (nums[i] != 9) { + nums[i]++; + break; + } else { + nums[i] = 0; + } + } + + // still assuming the first value in the list should not be zero as it's representing a + // valid number, although it's in a list + ListNode pre = new ListNode(-1); + if (nums[0] == 0) { + // in this case, let's just construct a new linked list and return: only first node + // value is 1, all the rest is 0 + ListNode newHead = new ListNode(1); + ListNode result = newHead; + int count = 0; + while (count++ < len) { + newHead.next = new ListNode(0); + newHead = newHead.next; + } + return result; + } else { + pre.next = head; + for (int i = 0; i < len; i++) { + head.val = nums[i]; + head = head.next; + } + return pre.next; + } + } + } + + public static class Solution2 { + + public ListNode plusOne(ListNode head) { + ListNode dummyNode = new ListNode(0); + dummyNode.next = head; + + ListNode notNineNode = dummyNode; + + // find the right most node value != 9 + while (head != null) { + if (head.val != 9) { + notNineNode = head; + } + head = head.next; + } + + // increase the rightmost node value to 1 + notNineNode.val++; + notNineNode = notNineNode.next; + + // set all the following node values with 9 to 0 + while (notNineNode != null) { + notNineNode.val = 0; + notNineNode = notNineNode.next; + } + return dummyNode.val != 0 ? dummyNode : dummyNode.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_37.java b/src/main/java/com/fishercoder/solutions/firstthousand/_37.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_37.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_37.java index 0ece61333d..8eaeb80caa 100644 --- a/src/main/java/com/fishercoder/solutions/_37.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_37.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _37 { @@ -15,14 +15,14 @@ private boolean solve(char[][] board) { for (int j = 0; j < board[0].length; j++) { if (board[i][j] == '.') { for (char c = '1'; c <= '9'; c++) { - //try 1 to 9 + // try 1 to 9 if (isValid(board, i, j, c)) { board[i][j] = c; if (solve(board)) { return true; } else { - board[i][j] = '.';//recover it to be '.' + board[i][j] = '.'; // recover it to be '.' } } } @@ -36,17 +36,17 @@ private boolean solve(char[][] board) { private boolean isValid(char[][] board, int row, int col, char c) { for (int i = 0; i < 9; i++) { if (board[i][col] != '.' && board[i][col] == c) { - return false;//check row + return false; // check row } if (board[row][i] != '.' && board[row][i] == c) { - return false;//check column + return false; // check column } - if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] != '.' && board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) { - return false; //check 3*3 block + if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] != '.' + && board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) { + return false; // check 3*3 block } } return true; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_370.java b/src/main/java/com/fishercoder/solutions/firstthousand/_370.java new file mode 100644 index 0000000000..e4931ad074 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_370.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.firstthousand; + +public class _370 { + public static class Solution1 { + public int[] getModifiedArray(int length, int[][] updates) { + int[] nums = new int[length]; + int k = updates.length; + for (int i = 0; i < k; i++) { + int start = updates[i][0]; + int end = updates[i][1]; + int inc = updates[i][2]; + nums[start] += inc; + if (end < length - 1) { + nums[end + 1] -= inc; + } + } + + int sum = 0; + for (int i = 0; i < length; i++) { + sum += nums[i]; + nums[i] = sum; + } + return nums; + } + } + + public static class Solution2 { + public int[] getModifiedArray(int length, int[][] updates) { + int[] result = new int[length]; + for (int[] update : updates) { + for (int i = update[0]; i <= update[1]; i++) { + result[i] += update[2]; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_371.java b/src/main/java/com/fishercoder/solutions/firstthousand/_371.java new file mode 100644 index 0000000000..6e2ce1600a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_371.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.firstthousand; + +public class _371 { + + public static class Solution1 { + /* + * reference: http://stackoverflow.com/questions/9070937/adding-two-numbers-without-operator-clarification + */ + public int getSum(int a, int b) { + if (b == 0) { + return a; + } + int sum = a ^ b; + int carry = (a & b) << 1; + return getSum(sum, carry); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_372.java b/src/main/java/com/fishercoder/solutions/firstthousand/_372.java new file mode 100644 index 0000000000..a456a9aaf1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_372.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +public class _372 { + public static class Solution1 { + public int superPow(int a, int[] b) { + if (a % 1337 == 0) { + return 0; + } + int p = 0; + for (int i : b) { + p = (p * 10 + i) % 1140; + } + if (p == 0) { + p += 1140; + } + return power(a, p, 1337); + } + + private int power(int a, int n, int mod) { + a %= mod; + int result = 1; + while (n != 0) { + if ((n & 1) != 0) { + result = result * a % mod; + } + a = a * a % mod; + n >>= 1; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_373.java b/src/main/java/com/fishercoder/solutions/firstthousand/_373.java new file mode 100644 index 0000000000..b55fbe297f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_373.java @@ -0,0 +1,65 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.PriorityQueue; + +public class _373 { + + public static class Solution1 { + + int[][] dirs = {{0, 1}, {1, 0}, {1, 1}}; + + public List> kSmallestPairs(int[] nums1, int[] nums2, int k) { + + List> result = new ArrayList<>(); + + // EDGE CASE + if (nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0) { + return result; + } + + // visited array + boolean[][] visited = new boolean[nums1.length][nums2.length]; + + // Min Heap + PriorityQueue pq = + new PriorityQueue<>( + (a, b) -> { + return (a[0] + a[1]) - (b[0] + b[1]); + }); + + int[] temp = new int[] {nums1[0], nums2[0], 0, 0}; + pq.add(temp); + visited[0][0] = true; + + while (!pq.isEmpty()) { + int[] arr = pq.poll(); + List ls = new ArrayList<>(); + ls.add(arr[0]); + ls.add(arr[1]); + result.add(ls); + + if (result.size() == k) { + break; + } + int i = arr[2]; + int j = arr[3]; + + for (int[] dir : dirs) { + int dx = i + dir[0]; + int dy = j + dir[1]; + if (dx >= 0 + && dx < nums1.length + && dy >= 0 + && dy < nums2.length + && !visited[dx][dy]) { + pq.add(new int[] {nums1[dx], nums2[dy], dx, dy}); + visited[dx][dy] = true; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_374.java b/src/main/java/com/fishercoder/solutions/firstthousand/_374.java new file mode 100644 index 0000000000..145a33428f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_374.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +public class _374 { + public static class Solution1 { + /* + * The core problem/trouble to solve this problem is to figure out the problem description: this + * API: guess(int num) means to take your guess num and let you know if your guessed num is + * bigger or smaller than the answer. That's why if num > target, it returns -1 which means the + * target is smaller than your guess!!! + */ + + public int guessNumber(int n) { + int left = 1; + int right = n; + while (left < right) { + int mid = left + (right - left) / 2; + int g = guess(mid); + if (g == 0) { + return mid; + } else if (g > 0) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return guess(left) == 0 ? left : right; + } + + /* + * This is a fake guess method that I wrote just to make the compiler happy, + * 7 is just a completely random value. + */ + private int guess(int num) { + if (num > 7) { + return -1; + } else if (num < 7) { + return 1; + } else { + return 0; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_375.java b/src/main/java/com/fishercoder/solutions/firstthousand/_375.java new file mode 100644 index 0000000000..8cf66b388b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_375.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +public class _375 { + public static class Solution1 { + public int getMoneyAmount(int n) { + int[][] table = new int[n + 1][n + 1]; + return dp(table, 1, n); + } + + private int dp(int[][] table, int s, int e) { + if (s >= e) { + return 0; + } + if (table[s][e] != 0) { + return table[s][e]; + } + int res = Integer.MAX_VALUE; + for (int i = s; i <= e; i++) { + int temp = i + Math.max(dp(table, s, i - 1), dp(table, i + 1, e)); + res = Math.min(res, temp); + } + table[s][e] = res; + return res; + } + } + + public static class Solution2 { + public int getMoneyAmount(int n) { + if (n == 1) { + return 0; + } + int[][] dp = new int[n + 1][n + 1]; + for (int x = 1; x < n; x++) { + for (int i = 0; i + x <= n; i++) { + int j = i + x; + dp[i][j] = Integer.MAX_VALUE; + for (int k = i; k <= j; k++) { + dp[i][j] = + Math.min( + dp[i][j], + k + + Math.max( + k - 1 >= i ? dp[i][k - 1] : 0, + j >= k + 1 ? dp[k + 1][j] : 0)); + } + } + } + for (int i = 0; i < n + 1; i++) { + for (int j = 0; j < n + 1; j++) { + System.out.print(dp[i][j] + " "); + } + System.out.println(); + } + return dp[1][n]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_376.java b/src/main/java/com/fishercoder/solutions/firstthousand/_376.java new file mode 100644 index 0000000000..f93fa4ad0f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_376.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +public class _376 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/wiggle-subsequence/discuss/84843/Easy-understanding-DP-solution-with-O(n)-Java-version + *

+ * For every position in the array, there are only three possible statuses for it. + *

+ * up position, it means nums[i] > nums[i-1] + * down position, it means nums[i] < nums[i-1] + * equals to position, nums[i] == nums[i-1] + * So we can use two arrays up[] and down[] to record the max wiggle sequence length so far at index i. + * If nums[i] > nums[i-1], that means it wiggles up. the element before it must be a down position. so up[i] = down[i-1] + 1; down[i] keeps the same with before. + * If nums[i] < nums[i-1], that means it wiggles down. the element before it must be a up position. so down[i] = up[i-1] + 1; up[i] keeps the same with before. + * If nums[i] == nums[i-1], that means it will not change anything becasue it didn't wiggle at all. so both down[i] and up[i] keep the same. + */ + public int wiggleMaxLength(int[] nums) { + int len = nums.length; + int[] up = new int[len]; + int[] down = new int[len]; + up[0] = 1; + down[0] = 1; + for (int i = 1; i < len; i++) { + if (nums[i] > nums[i - 1]) { + up[i] = down[i - 1] + 1; + down[i] = down[i - 1]; + } else if (nums[i] < nums[i - 1]) { + down[i] = up[i - 1] + 1; + up[i] = up[i - 1]; + } else { + up[i] = up[i - 1]; + down[i] = down[i - 1]; + } + } + return Math.max(down[len - 1], up[len - 1]); + } + } + + public static class Solution2 { + public int wiggleMaxLength(int[] nums) { + if (nums.length < 2) { + return nums.length; + } + int prevDiff = nums[1] - nums[0]; + int count = (prevDiff != 0) ? 2 : 1; + for (int i = 2; i < nums.length; i++) { + int diff = nums[i] - nums[i - 1]; + /*ATTN: prevDiff could be zero. e.g. [3,3,3,2,5] + * but diff needs to be exactly greater than zero*/ + if ((prevDiff <= 0 && diff > 0) || (prevDiff >= 0) && diff < 0) { + count++; + prevDiff = diff; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_377.java b/src/main/java/com/fishercoder/solutions/firstthousand/_377.java new file mode 100644 index 0000000000..868b724c9e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_377.java @@ -0,0 +1,124 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _377 { + + public static class Solution1 { + /* + * this normal backtracking recursive solution will end up in MLE by this testcase: [4,2,1], 32 + */ + public int combinationSum4(int[] nums, int target) { + List> result = new ArrayList(); + Arrays.sort(nums); + backtracking(nums, target, new ArrayList(), result); + return result.size(); + } + + private void backtracking( + int[] nums, int target, List list, List> result) { + if (target == 0) { + result.add(new ArrayList(list)); + } else if (target > 0) { + for (int i = 0; i < nums.length; i++) { + list.add(nums[i]); + backtracking(nums, target - nums[i], list, result); + list.remove(list.size() - 1); + } + } + } + } + + public static class Solution2 { + /* + * Since we don't need to get all of the combinations, instead, + * we only need to get the possible count, I can use only a count instead of "List> result" + * However, it also ended up in TLE by this testcase: [1,2,3], 32 + */ + public static int count = 0; + + public int combinationSum4(int[] nums, int target) { + Arrays.sort(nums); + backtracking(nums, target, new ArrayList()); + return count; + } + + private void backtracking(int[] nums, int target, List list) { + if (target == 0) { + count++; + } else if (target > 0) { + for (int i = 0; i < nums.length; i++) { + list.add(nums[i]); + backtracking(nums, target - nums[i], list); + list.remove(list.size() - 1); + } + } + } + } + + public static class Solution3 { + /* + * Time: O(n^2) + * Space: O(n) + *

+ * Since this question doesn't require to return all the combination result, instead, it just wants one number, we could use DP + * the idea is similar to Climbing Stairs. + *

+ * The idea is very clear as the code speaks for itself: + * It's easy to find the routine + * dp[0] = 0; + * dp[1] = 1; + * ... + *

+ * Reference: https://discuss.leetcode.com/topic/52186/my-3ms-java-dp-solution + */ + public int combinationSum4(int[] nums, int target) { + Arrays.sort(nums); + int[] dp = new int[target + 1]; + for (int i = 1; i < dp.length; i++) { + for (int num : nums) { + if (num > i) { + break; + } else if (num == i) { + dp[i]++; + } else { + dp[i] += dp[i - num]; + } + } + } + return dp[target]; + } + } + + public static class Solution4 { + /* + * Time: O(n) + * Space: O(n) + *

+ * Reference: https://discuss.leetcode.com/topic/52255/java-recursion-solution-using-hashmap-as-memory + */ + Map map = new HashMap<>(); + + public int combinationSum4(int[] nums, int target) { + if (nums == null || nums.length == 0 || target < 0) { + return 0; + } + if (target == 0) { + return 1; + } + if (map.containsKey(target)) { + return map.get(target); + } + int count = 0; + for (int num : nums) { + count += combinationSum4(nums, target - num); + } + map.put(target, count); + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_378.java b/src/main/java/com/fishercoder/solutions/firstthousand/_378.java new file mode 100644 index 0000000000..03191f2373 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_378.java @@ -0,0 +1,76 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.PriorityQueue; + +public class _378 { + public static class Solution1 { + /* + * brute force made it AC'ed, extreme test case needed for OJ + */ + public int kthSmallest(int[][] matrix, int k) { + List list = new ArrayList(); + int n = matrix.length; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + list.add(matrix[i][j]); + } + } + Collections.sort(list); + return list.get(k - 1); + } + } + + public static class Solution2 { + /* + * use heap data structure + */ + public int kthSmallest(int[][] matrix, int k) { + PriorityQueue heap = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0])); + for (int i = 0; i < Math.min(matrix.length, k); i++) { + // we store value, rowIndex, colIndex as an array into this heap + heap.offer(new int[] {matrix[i][0], i, 0}); + } + while (k-- > 1) { + int[] min = heap.poll(); + if (min[2] + 1 < matrix[min[1]].length) { + heap.offer(new int[] {matrix[min[1]][min[2] + 1], min[1], min[2] + 1}); + } + } + return heap.poll()[0]; + } + } + + public static class Solution3 { + /* + * Binary Search : The idea is to pick a mid number, then compare it with the elements in each row, we start form + * end of row util we find the element is less than the mid, the left side element is all less than mid; keep tracking elements + * that less than mid and compare with k, then update the k. + */ + public int kthSmallest(int[][] matrix, int k) { + int row = matrix.length - 1; + int col = matrix[0].length - 1; + int lo = matrix[0][0]; + int hi = matrix[row][col]; + while (lo < hi) { + int mid = lo + (hi - lo) / 2; + int count = 0; + int j = col; + for (int i = 0; i <= row; i++) { + while (j >= 0 && matrix[i][j] > mid) { + j--; + } + count += (j + 1); + } + if (count < k) { + lo = mid + 1; + } else { + hi = mid; + } + } + return lo; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_379.java b/src/main/java/com/fishercoder/solutions/firstthousand/_379.java new file mode 100644 index 0000000000..1ee4fbf714 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_379.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; + +public class _379 { + + public static class Solution1 { + private class PhoneDirectory { + private Queue phoneDir; + private Set used; + + /* + * Initialize your data structure here + * + * @param maxNumbers - The maximum numbers that can be stored in the phone directory. + */ + public PhoneDirectory(int maxNumbers) { + phoneDir = new LinkedList(); + int number = 0; + while (maxNumbers-- > 0) { + phoneDir.add(number++); + } + used = new HashSet(); + } + + /* + * Provide a number which is not assigned to anyone. + * + * @return - Return an available number. Return -1 if none is available. + */ + public int get() { + if (phoneDir.peek() == null) { + return -1; + } + int newNumber = phoneDir.poll(); + used.add(newNumber); + return newNumber; + } + + /* + * Check if a number is available or not. + */ + public boolean check(int number) { + return !used.contains(number); + } + + /* + * Recycle or release a number. + */ + public void release(int number) { + if (used.remove(number)) { + phoneDir.add(number); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_38.java b/src/main/java/com/fishercoder/solutions/firstthousand/_38.java similarity index 94% rename from src/main/java/com/fishercoder/solutions/_38.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_38.java index 800cb6a10e..b5b38923ed 100644 --- a/src/main/java/com/fishercoder/solutions/_38.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_38.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _38 { public static class Solution1 { @@ -27,5 +27,4 @@ public String countAndSay(int n) { return curr.toString(); } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_380.java b/src/main/java/com/fishercoder/solutions/firstthousand/_380.java new file mode 100644 index 0000000000..307273fdf3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_380.java @@ -0,0 +1,65 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; + +public class _380 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/insert-delete-getrandom-o1/discuss/85401/Java-solution-using-a-HashMap-and-an-ArrayList-along-with-a-follow-up.-(131-ms) + * 1. use an arraylist and a hashmap; + * 2. you always insert at the end of the arraylist and put the index/position of this new item into the map; + * 3. for deletion, always delete the last item in the arraylist so it's O(1) time, if the last item in the arraylist is not the item we want to delete, we swap it first before we delete + */ + public static class RandomizedSet { + Map map; + List list; + Random random; + + public RandomizedSet() { + map = new HashMap<>(); + random = new Random(); + list = new ArrayList<>(); + } + + public boolean insert(int val) { + if (map.containsKey(val)) { + return false; + } + map.put(val, list.size()); + list.add(val); + return true; + } + + public boolean remove(int val) { + if (!map.containsKey(val)) { + return false; + } else { + int removeIndex = map.get(val); + if (removeIndex != list.size() - 1) { + // if it's not the last element, then we need to swap it with the last + // element so that this operation is also O(1) + int lastElement = list.get(list.size() - 1); + // using set() API is another key here which gives us O(1), + // using add() is not only wrong, i.e. it adds an element at this position + // and shifts all elements on the right of this element to the right, so + // leading to O(n) time + list.set(removeIndex, lastElement); + map.put(lastElement, removeIndex); + } + map.remove(val); + list.remove(list.size() - 1); + return true; + } + } + + public int getRandom() { + return list.get(random.nextInt(list.size())); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_381.java b/src/main/java/com/fishercoder/solutions/firstthousand/_381.java new file mode 100644 index 0000000000..f97b0aeada --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_381.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +public class _381 { + public static class Solution1 { + /* + * This is a natural extension to the solution from https://leetcode.com/problems/insert-delete-getrandom-o1 + * You only need to change the value type of the hashmap to be a set instead of Integer to hold all indexes for this value ever inserted. + */ + + public static class RandomizedCollection { + List list; + Map> map; + Random random; + + public RandomizedCollection() { + this.list = new ArrayList<>(); + this.map = new HashMap<>(); + this.random = new Random(); + } + + public boolean insert(int val) { + Set indexSet = map.getOrDefault(val, new LinkedHashSet<>()); + indexSet.add(list.size()); + map.put(val, indexSet); + list.add(val); + return indexSet.size() <= 1; + } + + public boolean remove(int val) { + if (!map.containsKey(val) || map.get(val).size() == 0) { + return false; + } else { + int removeIndex = map.get(val).iterator().next(); + map.get(val).remove(removeIndex); + int lastElement = list.get(list.size() - 1); + list.set(removeIndex, lastElement); + map.get(lastElement).add(removeIndex); + map.get(lastElement).remove(list.size() - 1); + list.remove(list.size() - 1); + return true; + } + } + + public int getRandom() { + return list.get(random.nextInt(list.size())); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_382.java b/src/main/java/com/fishercoder/solutions/firstthousand/_382.java new file mode 100644 index 0000000000..cf05f98660 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_382.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +public class _382 { + + public static class Solution { + private Map map; + private Random rand; + + /* + * @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at least one node. + */ + public Solution(ListNode head) { + map = new HashMap(); + rand = new Random(); + int i = 0; + while (head != null) { + map.put(i++, head); + head = head.next; + } + } + + /* + * Returns a random node's value. + */ + public int getRandom() { + return map.get(rand.nextInt(map.size())).val; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_383.java b/src/main/java/com/fishercoder/solutions/firstthousand/_383.java new file mode 100644 index 0000000000..242f64872a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_383.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +public class _383 { + + public static class Solution1 { + public boolean canConstruct(String ransomNote, String magazine) { + char[] mchars = magazine.toCharArray(); + int[] mcnt = new int[256]; + for (int i = 0; i < mchars.length; i++) { + mcnt[mchars[i] - 'a']++; + } + + char[] rchars = ransomNote.toCharArray(); + for (int i = 0; i < rchars.length; i++) { + if (mcnt[rchars[i] - 'a'] <= 0) { + return false; + } + mcnt[rchars[i] - 'a']--; + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_384.java b/src/main/java/com/fishercoder/solutions/firstthousand/_384.java new file mode 100644 index 0000000000..6ea0748551 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_384.java @@ -0,0 +1,117 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Random; + +public class _384 { + + public static class Solution1 { + // Note as of 7/20/2021: This solution ends in TLE on LeetCode now. + // Note: the problem states that this is a set without duplicates which makes building all + // combinations easier + + private List> combinations; + private int[] original; + private Random random; + + public Solution1(int[] nums) { + original = nums; + random = new Random(); + combinations = buildAllComb(nums); + } + + // insert next value into all possible positions, I wrote this method myself, of course it + // could be simplified to not use a queue + // but it just naturally came into my mind that I used a queue + private List> buildAllComb(int[] nums) { + List> result = new ArrayList(); + if (nums == null || nums.length == 0) { + return result; + } + + List list = new ArrayList(); + list.add(nums[0]); + Queue> q = new LinkedList(); + q.offer(list); + for (int i = 1; i < nums.length; i++) { + int qSize = q.size(); + for (int k = 0; k < qSize; k++) { + List currList = q.poll(); + for (int j = 0; j <= currList.size(); j++) { + List newL = new ArrayList(currList); + newL.add(j, nums[i]); + q.offer(newL); + } + } + } + while (!q.isEmpty()) { + result.add(q.poll()); + } + return result; + } + + /* + * Resets the array to its original configuration and return it. + */ + public int[] reset() { + return original; + } + + /* + * Returns a random shuffling of the array. + */ + public int[] shuffle() { + if (original == null || original.length == 0) { + return original; + } + int randomIndex = random.nextInt(combinations.size()); + List list = combinations.get(randomIndex); + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/shuffle-an-array/discuss/85958/First-Accepted-Solution-Java + */ + private int[] nums; + private Random random; + + public Solution2(int[] nums) { + this.nums = nums; + this.random = new Random(); + } + + /* + * Resets the array to its original configuration and return it. + */ + public int[] reset() { + return this.nums; + } + + /* + * Returns a random shuffling of the array. + */ + public int[] shuffle() { + int[] shuffled = this.nums.clone(); + for (int i = 1; i < nums.length; i++) { + int j = random.nextInt(i + 1); + swap(shuffled, i, j); + } + return shuffled; + } + + private void swap(int[] shuffled, int i, int j) { + int tmp = shuffled[i]; + shuffled[i] = shuffled[j]; + shuffled[j] = tmp; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_385.java b/src/main/java/com/fishercoder/solutions/firstthousand/_385.java new file mode 100644 index 0000000000..f89d71fe28 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_385.java @@ -0,0 +1,105 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.NestedInteger; +import java.util.Stack; + +public class _385 { + + public static class Solution1 { + // Lessons: ask the interviewer to clarify the input, for this question, the input could be + // "324", "[324]", they are different + // the former should return a nested integer with one single integer, the latter should + // return a nested integer with a list + + // Idea: + // if it's '[', we just construct a new nested integer and push it onto the stack + // if it's a number, we parse the whole number and add to the previous nested integer object + // if it's ',', we'll just continue; + // if it's ']', we'll just pop one nested integer from the working stack and assign it to + // the result + + public NestedInteger deserialize(String s) { + if (s == null || s.isEmpty() || s.length() == 0) { + return new NestedInteger(); + } + Stack workStack = new Stack<>(); + NestedInteger result = null; + StringBuilder sb = new StringBuilder(); + int i = 0; + // if it's just a single number, then we'll just return a nested integer with one + // integer + if (s.charAt(i) != '[') { + sb.setLength(0); + while (i < s.length() + && ((Character.getNumericValue(s.charAt(i)) < 10 + && Character.getNumericValue(s.charAt(i)) >= 0) + || s.charAt(i) == '-')) { + sb.append(s.charAt(i)); + i++; + } + int num = Integer.parseInt(sb.toString()); + return new NestedInteger(num); + } else { + // all other cases, we'll return a nested integer with a list + while (i < s.length()) { + if (s.charAt(i) == '[') { + NestedInteger ni = new NestedInteger(); + // we'll put this one into its last one if there's one on the workStack + if (!workStack.isEmpty()) { + NestedInteger lastNi = workStack.pop(); + lastNi.add(ni); + workStack.push(lastNi); // then push it back + } + workStack.push(ni); + i++; + } else if (s.charAt(i) == ',') { + i++; + } else if (s.charAt(i) == ']') { + NestedInteger completedNi = workStack.pop(); + result = completedNi; + i++; + } else { + // then it must be a number + sb.setLength(0); + while (i < s.length() + && ((Character.getNumericValue(s.charAt(i)) < 10 + && Character.getNumericValue(s.charAt(i)) >= 0) + || s.charAt(i) == '-')) { + sb.append(s.charAt(i)); + i++; + } + int num = Integer.parseInt(sb.toString()); + NestedInteger ni = null; + if (!workStack.isEmpty()) { + ni = workStack.pop(); + } else { + ni = new NestedInteger(); + } + // case 1: if this one contains one integer + if (ni.isInteger()) { + // we'll add it to this ni + ni.add(new NestedInteger(num)); + } else if (ni.getList() != null && ni.getList().size() != 0) { + // case 2: if this one contains a nested integer + // we'll get the last nested integer and add this one to it + ni.add(new NestedInteger(num)); + } else { + // case 3: if this is an empty nested integer + if (i > 0) { + ni.add(new NestedInteger(num)); + } else { + ni.setInteger(num); + } + } + workStack.push(ni); + if (i == s.length()) { + return ni; // this is for test cases like this: "324", there's no '[' or + // ']' + } + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_386.java b/src/main/java/com/fishercoder/solutions/firstthousand/_386.java new file mode 100644 index 0000000000..2a39fb174e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_386.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _386 { + public static class Solution1 { + // Radix sort doesn't apply here! Don't confuse yourself! + + // rewrote their solution from Python to + // Java:https://discuss.leetcode.com/topic/54986/python-memory-limit-exceeded-for-problem-386/17 + public List lexicalOrder(int n) { + List result = new ArrayList(); + int i = 1; + while (true) { + result.add(i); + if (i * 10 <= n) { + i *= 10; + } else { + while (i % 10 == 9 || i == n) { + i /= 10; + } + if (i == 0) { + return result; + } + i++; + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_387.java b/src/main/java/com/fishercoder/solutions/firstthousand/_387.java new file mode 100644 index 0000000000..6dd555643e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_387.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.firstthousand; + +public class _387 { + public static class Solution1 { + public static int firstUniqChar(String s) { + int[] freq = new int[26]; + for (int i = 0; i < s.length(); i++) { + freq[s.charAt(i) - 'a']++; + } + for (int i = 0; i < s.length(); i++) { + if (freq[s.charAt(i) - 'a'] == 1) { + return i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_388.java b/src/main/java/com/fishercoder/solutions/firstthousand/_388.java new file mode 100644 index 0000000000..4f13b0c000 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_388.java @@ -0,0 +1,74 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Stack; + +public class _388 { + public static class Solution1 { + public int lengthLongestPath(String input) { + Stack stack = new Stack(); + int longestLen = 0; + int currDirLen = 0; + int i = 0; + int currLevel = 0; + int nextLevel = 0; + boolean isFile = false; + Character period = '.'; + Character space = ' '; + while (i < input.length()) { + currLevel = nextLevel; + int currStrLen = 0; + while (i < input.length() + && (Character.isLetterOrDigit(input.charAt(i)) + || period.equals(input.charAt(i)) + || space.equals(input.charAt(i)))) { + if (period.equals(input.charAt(i))) { + isFile = true; + } + i++; + currStrLen++; + } + if (isFile) { + longestLen = Math.max(longestLen, currDirLen + currStrLen); + } else { + currDirLen += currStrLen + 1; + stack.push(currStrLen + 1); + } + + nextLevel = 0; + i = i + 1; // increment one to let it pass "\n" and start from "\t" + while (i < input.length() - 1 && input.substring(i, i + 1).equals("\t")) { + nextLevel++; + i = i + 1; + } + + if (nextLevel < currLevel) { + int j = 0; + if (isFile) { + while (!stack.isEmpty() && j < (currLevel - nextLevel)) { + currDirLen -= stack.pop(); + j++; + } + } else { + while (!stack.isEmpty() && j <= (currLevel - nextLevel)) { + currDirLen -= stack.pop(); + j++; + } + } + } else if (nextLevel == currLevel) { + if (!isFile && !stack.isEmpty()) { + currDirLen -= stack.pop(); + } + } + + if (nextLevel == 0) { + currDirLen = 0; + stack.clear(); + } + + isFile = false; + } + + return longestLen; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_389.java b/src/main/java/com/fishercoder/solutions/firstthousand/_389.java new file mode 100644 index 0000000000..d5ede8bbc9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_389.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _389 { + public static class Solution1 { + public char findTheDifference(String s, String t) { + int[] counts = new int[128]; + char[] schars = s.toCharArray(); + char[] tchars = t.toCharArray(); + for (int i = 0; i < schars.length; i++) { + counts[schars[i]]++; + } + for (int i = 0; i < tchars.length; i++) { + counts[tchars[i]]--; + } + char result = 'a'; + for (int i = 0; i < 128; i++) { + if (counts[i] != 0) { + result = (char) i; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_39.java b/src/main/java/com/fishercoder/solutions/firstthousand/_39.java similarity index 75% rename from src/main/java/com/fishercoder/solutions/_39.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_39.java index 74197c45a8..c203a41c2d 100644 --- a/src/main/java/com/fishercoder/solutions/_39.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_39.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.Arrays; @@ -14,11 +14,16 @@ public List> combinationSum(int[] candidates, int target) { return result; } - void backtracking(int[] candidates, int target, int start, List curr, List> result) { + void backtracking( + int[] candidates, + int target, + int start, + List curr, + List> result) { if (target > 0) { for (int i = start; i < candidates.length; i++) { if (candidates[i] > target) { - continue;//pruning + break; // pruning } curr.add(candidates[i]); backtracking(candidates, target - candidates[i], i, curr, result); diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_390.java b/src/main/java/com/fishercoder/solutions/firstthousand/_390.java new file mode 100644 index 0000000000..092c653703 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_390.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +public class _390 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/55870/share-my-solutions-for-contest-2 instead of + * literally removing half of the elements in each scan, this solution is just moving the + * pointer to point to next start position So brilliant! + */ + public int lastRemaining(int n) { + int remaining = n; + int start = 1; + int step = 2; + boolean forward = true; + while (remaining > 1) { + remaining /= 2; + if (forward) { + start = start + step * remaining - step / 2; + } else { + start = start - step * remaining + step / 2; + } + step *= 2; + forward = !forward; + } + return start; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_391.java b/src/main/java/com/fishercoder/solutions/firstthousand/_391.java new file mode 100644 index 0000000000..e70d696201 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_391.java @@ -0,0 +1,62 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _391 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java + */ + public boolean isRectangleCover(int[][] rectangles) { + if (rectangles.length == 0 || rectangles[0].length == 0) { + return false; + } + + int x1 = Integer.MAX_VALUE; + int x2 = Integer.MIN_VALUE; + int y1 = Integer.MAX_VALUE; + int y2 = Integer.MIN_VALUE; + + Set set = new HashSet<>(); + int area = 0; + + for (int[] rect : rectangles) { + x1 = Math.min(rect[0], x1); + y1 = Math.min(rect[1], y1); + x2 = Math.max(rect[2], x2); + y2 = Math.max(rect[3], y2); + + area += (rect[2] - rect[0]) * (rect[3] - rect[1]); + + String s1 = rect[0] + " " + rect[1]; + String s2 = rect[0] + " " + rect[3]; + String s3 = rect[2] + " " + rect[3]; + String s4 = rect[2] + " " + rect[1]; + + if (!set.add(s1)) { + set.remove(s1); + } + if (!set.add(s2)) { + set.remove(s2); + } + if (!set.add(s3)) { + set.remove(s3); + } + if (!set.add(s4)) { + set.remove(s4); + } + } + + if (!set.contains(x1 + " " + y1) + || !set.contains(x1 + " " + y2) + || !set.contains(x2 + " " + y1) + || !set.contains(x2 + " " + y2) + || set.size() != 4) { + return false; + } + + return area == (x2 - x1) * (y2 - y1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_392.java b/src/main/java/com/fishercoder/solutions/firstthousand/_392.java new file mode 100644 index 0000000000..2fa995a0ba --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_392.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _392 { + + public static class Solution1 { + public boolean isSubsequence(String s, String t) { + int left = 0; + for (int i = 0; i < s.length(); i++) { + boolean foundI = false; + int j = left; + for (; j < t.length(); j++) { + if (s.charAt(i) == t.charAt(j)) { + left = j + 1; + foundI = true; + break; + } + } + if (j == t.length() && !foundI) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_393.java b/src/main/java/com/fishercoder/solutions/firstthousand/_393.java new file mode 100644 index 0000000000..e3932ae76a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_393.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +public class _393 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/58338/bit-manipulation-java-6ms/4 + */ + public boolean validUtf8(int[] data) { + int count = 0; + for (int d : data) { + if (count == 0) { + if ((d >> 5) == 0b110) { + count = 1; + } else if ((d >> 4) == 0b1110) { + count = 2; + } else if ((d >> 3) == 0b11110) { + count = 3; + } else if ((d >> 7) == 1) { + return false; + } + } else { + if ((d >> 6) != 0b10) { + return false; + } else { + count--; + } + } + } + return count == 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_394.java b/src/main/java/com/fishercoder/solutions/firstthousand/_394.java new file mode 100644 index 0000000000..97ff83fbd6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_394.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Stack; + +public class _394 { + + public static class Solution1 { + public String decodeString(String s) { + Stack count = new Stack<>(); + Stack str = new Stack<>(); + + int idx = 0; + str.push(""); + + while (idx < s.length()) { + if (s.charAt(idx) >= '0' && s.charAt(idx) <= '9') { + int start = idx; + while (s.charAt(idx + 1) >= '0' && s.charAt(idx + 1) <= '9') { + idx++; + } + count.push(Integer.parseInt(s.substring(start, idx + 1))); + } else if (s.charAt(idx) == '[') { + str.push(""); + } else if (s.charAt(idx) == ']') { + String st = str.pop(); + StringBuilder sb = new StringBuilder(); + int n = count.pop(); + for (int j = 0; j < n; j++) { + sb.append(st); + } + str.push(str.pop() + sb.toString()); + } else { + str.push(str.pop() + s.charAt(idx)); + } + + idx++; + } + + return str.pop(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_395.java b/src/main/java/com/fishercoder/solutions/firstthousand/_395.java new file mode 100644 index 0000000000..c4496c045a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_395.java @@ -0,0 +1,102 @@ +package com.fishercoder.solutions.firstthousand; + +public class _395 { + public static class Solution1 { + /* + * Reference: https://discuss.leetcode.com/topic/57372/java-divide-and-conquer-recursion-solution + */ + public int longestSubstring(String s, int k) { + return findLongestSubstring(s.toCharArray(), 0, s.length(), k); + } + + int findLongestSubstring(char[] chars, int start, int end, int k) { + /*Base case 1 of 2*/ + if (end - start < k) { + return 0; + } + int[] count = new int[26]; + for (int i = start; i < end; i++) { + int index = chars[i] - 'a'; + count[index]++; + } + + /*For every character in the above frequency table*/ + for (int i = 0; i < 26; i++) { + if (count[i] < k && count[i] > 0) { + for (int j = start; j < end; j++) { + if (chars[j] == i + 'a') { + int left = findLongestSubstring(chars, start, j, k); + int right = findLongestSubstring(chars, j + 1, end, k); + return Math.max(left, right); + } + } + } + } + /*Base case 2 of 2: + * when any characters in this substring has repeated at least k times, then this entire substring is a valid answer*/ + return end - start; + } + } + + public static class Solution2 { + /* + * classic sliding window approach. + * credit: https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/170010/Java-O(n)-Solution-with-Detailed-Explanation-Sliding-Window/774350 + */ + public int longestSubstring(String s, int k) { + int res = 0; + for (int numUniqueTarget = 1; numUniqueTarget <= 26; numUniqueTarget++) { + res = Math.max(res, slidingWindowHelper(s, k, numUniqueTarget)); + } + return res; + } + + // sliding window template + private int slidingWindowHelper(String s, int k, int numUniqueTarget) { + int[] map = new int[26]; + int start = 0; + int end = 0; + int res = 0; + int uniqueLetterCount = 0; + int numNoLessThanK = 0; + while (end < s.length()) { + char c1 = s.charAt(end); + if (map[c1 - 'a'] == 0) { + // we increment this when we include a new letter into our sliding window + uniqueLetterCount++; + } + map[c1 - 'a']++; + if (map[c1 - 'a'] == k) { + // we increment this number when we find a letter's frequency reaches k + numNoLessThanK++; + } + end++; + + while (uniqueLetterCount > numUniqueTarget) { + // as long as the counter (the number of qualified letters) is greater than our + // target number, + // we can move the left pointer to the right, + // this keeps the interval within our sliding window always valid + char c2 = s.charAt(start); + if (map[c2 - 'a'] == k) { + // we decrement this numNoLessThanK when we find this letter's frequency + // equals + // to k because we'll move past this letter, i.e. our sliding window no + // longer includes it + numNoLessThanK--; + } + map[c2 - 'a']--; + if (map[c2 - 'a'] == 0) { + uniqueLetterCount--; + } + start++; + } + + if (uniqueLetterCount == numNoLessThanK) { + res = Math.max(res, end - start); + } + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_396.java b/src/main/java/com/fishercoder/solutions/firstthousand/_396.java new file mode 100644 index 0000000000..d7c2e3a217 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_396.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +public class _396 { + public static class Solution1 { + /* + * F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1] + */ + public int maxRotateFunction(int[] A) { + if (A == null || A.length == 0) { + return 0; + } + int[] F = new int[A.length]; + int[] B = A; + int max = Integer.MIN_VALUE; + for (int i = 0; i < A.length; i++) { + F[i] = compute(B); + max = Math.max(max, F[i]); + B = rotate(B); + } + return max; + } + + private int compute(int[] b) { + int sum = 0; + for (int i = 0; i < b.length; i++) { + sum += i * b[i]; + } + return sum; + } + + private int[] rotate(int[] a) { + int first = a[0]; + for (int i = 1; i < a.length; i++) { + a[i - 1] = a[i]; + } + a[a.length - 1] = first; + return a; + } + } + + public static class Solution2 { + /* + * Reference: https://discuss.leetcode.com/topic/58459/java-o-n-solution-with-explanation + */ + public int maxRotateFunction(int[] A) { + int allSum = 0; + int len = A.length; + int F = 0; + for (int i = 0; i < len; i++) { + F += i * A[i]; + allSum += A[i]; + } + int max = F; + for (int i = len - 1; i >= 1; i--) { + F = F + allSum - len * A[i]; + max = Math.max(F, max); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_397.java b/src/main/java/com/fishercoder/solutions/firstthousand/_397.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_397.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_397.java index 38d183ae56..e4d89ee8e1 100644 --- a/src/main/java/com/fishercoder/solutions/_397.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_397.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.HashSet; import java.util.Iterator; @@ -6,37 +6,6 @@ import java.util.Queue; import java.util.Set; -/** - * 397. Integer Replacement - * - * Given a positive integer n and you can do operations as follow: - - If n is even, replace n with n/2. - If n is odd, you can replace n with either n + 1 or n - 1. - What is the minimum number of replacements needed for n to become 1? - - Example 1: - - Input: - 8 - - Output: - 3 - - Explanation: - 8 -> 4 -> 2 -> 1 - Example 2: - - Input: - 7 - - Output: - 4 - - Explanation: - 7 -> 8 -> 4 -> 2 -> 1 - or - 7 -> 6 -> 3 -> 2 -> 1*/ public class _397 { public static class Solution1 { @@ -80,5 +49,4 @@ public int integerReplacement(int n) { return (int) min; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_398.java b/src/main/java/com/fishercoder/solutions/firstthousand/_398.java new file mode 100644 index 0000000000..5c9bdaca09 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_398.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.*; + +public class _398 { + + // TODO: use reservoir sampling to solve it again + // reservoir sampling: the size of the dataset is unknow before hand + + public static class Solution { + Map> map; + Random random; + + public Solution(int[] nums) { + map = new HashMap<>(); + random = new Random(); + for (int i = 0; i < nums.length; i++) { + List list = map.getOrDefault(nums[i], new ArrayList<>()); + list.add(i); + map.put(nums[i], list); + } + } + + public int pick(int target) { + List list = map.get(target); + return list.get(random.nextInt(list.size())); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_399.java b/src/main/java/com/fishercoder/solutions/firstthousand/_399.java new file mode 100644 index 0000000000..54b9896e04 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_399.java @@ -0,0 +1,72 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _399 { + + public static class Solution1 { + /* + * Credit: https://medium.com/@null00/leetcode-evaluate-division-52a0158488c1 + */ + private Map root; + private Map rate; + + public double[] calcEquation( + List> equations, double[] values, List> queries) { + root = new HashMap<>(); + rate = new HashMap<>(); + int n = equations.size(); + for (int i = 0; i < n; ++i) { + String X = equations.get(i).get(0); + String Y = equations.get(i).get(1); + root.put(X, X); + root.put(Y, Y); + rate.put(X, 1.0); + rate.put(Y, 1.0); + } + + for (int i = 0; i < n; ++i) { + String X = equations.get(i).get(0); + String Y = equations.get(i).get(1); + union(X, Y, values[i]); + } + + double[] result = new double[queries.size()]; + for (int i = 0; i < queries.size(); ++i) { + String X = queries.get(i).get(0); + String Y = queries.get(i).get(1); + if (!root.containsKey(X) || !root.containsKey(Y)) { + result[i] = -1; + continue; + } + + String rootx = findRoot(X, X, 1.0); + String rooty = findRoot(Y, Y, 1.0); + result[i] = rootx.equals(rooty) ? rate.get(X) / rate.get(Y) : -1.0; + } + + return result; + } + + private void union(String X, String Y, double v) { + String rootx = findRoot(X, X, 1.0); + String rooty = findRoot(Y, Y, 1.0); + root.put(rootx, rooty); + double r1 = rate.get(X); + double r2 = rate.get(Y); + rate.put(rootx, v * r2 / r1); + } + + private String findRoot(String originalX, String X, double r) { + if (root.get(X).equals(X)) { + root.put(originalX, X); + rate.put(originalX, r * rate.get(X)); + return X; + } + + return findRoot(originalX, root.get(X), r * rate.get(X)); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_4.java b/src/main/java/com/fishercoder/solutions/firstthousand/_4.java similarity index 79% rename from src/main/java/com/fishercoder/solutions/_4.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_4.java index a93ac5ef4a..0486009667 100644 --- a/src/main/java/com/fishercoder/solutions/_4.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_4.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import static java.lang.Math.max; import static java.lang.Math.min; @@ -6,15 +6,15 @@ public class _4 { public static class Solution1 { - /**credit: https://discuss.leetcode.com/topic/28602/concise-java-solution-based-on-binary-search - * - * The key point of this problem is to ignore half part of A and B each step recursively by comparing the median of remaining A and B: + /*credit: https://discuss.leetcode.com/topic/28602/concise-java-solution-based-on-binary-search + * + * The key point of this problem is to ignore half part of A and B each step recursively by comparing the median of remaining A and B: - if (aMid < bMid) Keep [aRight + bLeft] - else Keep [bRight + aLeft] + if (aMid < bMid) Keep [aRight + bLeft] + else Keep [bRight + aLeft] - As the following: time=O(log(m + n)) - */ + As the following: time=O(log(m + n)) + */ public double findMedianSortedArrays(int[] A, int[] B) { int m = A.length; int n = B.length; @@ -44,15 +44,15 @@ public double getkth(int[] A, int aStart, int[] B, int bStart, int k) { } if (aMid < bMid) { - return getkth(A, aStart + k / 2, B, bStart, k - k / 2);// Check: aRight + bLeft + return getkth(A, aStart + k / 2, B, bStart, k - k / 2); // Check: aRight + bLeft } else { - return getkth(A, aStart, B, bStart + k / 2, k - k / 2);// Check: bRight + aLeft + return getkth(A, aStart, B, bStart + k / 2, k - k / 2); // Check: bRight + aLeft } } } public static class Solution2 { - /** + /* * Reference: https://leetcode.com/discuss/28843/my-accepted-java-solution: * Basic Idea is very similar to K-selection. it's easier to understand if you imagine this to be chopping off the last K elements from a total of len(A) + len(B) elements, * where K = (len(A) + len(B))/2. @@ -62,7 +62,9 @@ public static class Solution2 { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int K = nums1.length + nums2.length; if (K % 2 == 0) { - return (findMedianSortedArrays(nums1, nums2, (K - K / 2)) + findMedianSortedArrays(nums1, nums2, (K - (K / 2 + 1)))) / 2; + return (findMedianSortedArrays(nums1, nums2, (K - K / 2)) + + findMedianSortedArrays(nums1, nums2, (K - (K / 2 + 1)))) + / 2; } else { return findMedianSortedArrays(nums1, nums2, K - (K / 2 + 1)); } @@ -83,7 +85,10 @@ public double findMedianSortedArrays(int[] A, int[] B, int K) { midA = highA - chopA; midB = highB - chopB; - if (A[midA] < B[midB]) { // here A[0 .. midA] < B[midB], and we know that B[0 .. midB-1] < B[midB], so B[midB..highB] can not possibly be within the first (len(A) + len(B) - K) elements, and can be safely removed. + if (A[midA] < B[midB]) { // here A[0 .. midA] < B[midB], and we know that B[0 .. + // midB-1] < B[midB], so B[midB..highB] can not possibly be + // within the first (len(A) + len(B) - K) elements, and can + // be safely removed. highB = midB; K = K - chopB; } else { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_40.java b/src/main/java/com/fishercoder/solutions/firstthousand/_40.java new file mode 100644 index 0000000000..ae96c3b145 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_40.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _40 { + + public static class Solution1 { + public List> combinationSum2(int[] candidates, int target) { + List> result = new ArrayList(); + Arrays.sort(candidates); + backtracking(candidates, 0, result, target, new ArrayList()); + return result; + } + + void backtracking( + int[] candidates, + int start, + List> result, + int target, + List curr) { + if (target > 0) { + for (int i = start; i < candidates.length; i++) { + if (candidates[i] > target + || (i > start && candidates[i - 1] == candidates[i])) { + continue; + } + curr.add(candidates[i]); + backtracking(candidates, i + 1, result, target - candidates[i], curr); + curr.remove(curr.size() - 1); + } + } else if (target == 0) { + result.add(new ArrayList(curr)); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_400.java b/src/main/java/com/fishercoder/solutions/firstthousand/_400.java new file mode 100644 index 0000000000..0a6a3ea3f8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_400.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +public class _400 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/59314/java-solution: + *

+ * 1. find the length of the number where the nth digit is from 2. find the actual number where + * the nth digit is from 3. find the nth digit and return + */ + public int findNthDigit(int n) { + int len = 1; + long count = 9; + int start = 1; + + while (n > len * count) { + n -= len * count; + len += 1; + count *= 10; + start *= 10; + } + + start += (n - 1) / len; + String s = Integer.toString(start); + return Character.getNumericValue(s.charAt((n - 1) % len)); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_401.java b/src/main/java/com/fishercoder/solutions/firstthousand/_401.java new file mode 100644 index 0000000000..f64b8e3efd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_401.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _401 { + + public static class Solution1 { + public List readBinaryWatch(int num) { + List times = new ArrayList<>(); + for (int h = 0; h < 12; h++) { + for (int m = 0; m < 60; m++) { + if (Integer.bitCount(h * 64 + m) == num) { + times.add( + String.format( + "%d:%02d", + h, m)); // %02 means to pad this two-digit decimal number on + // the left with zeroes + } + } + } + return times; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_402.java b/src/main/java/com/fishercoder/solutions/firstthousand/_402.java new file mode 100644 index 0000000000..4b82189126 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_402.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.firstthousand; + +public class _402 { + public static class Solution1 { + + /* + * credit: https://discuss.leetcode.com/topic/59412/a-greedy-method-using-stack-o-n-time-and-o-n-space + */ + public String removeKdigits(String num, int k) { + int digits = num.length() - k; + char[] stack = new char[num.length()]; + int top = 0; + + for (int i = 0; i < num.length(); i++) { + char c = num.charAt(i); + while (top > 0 && stack[top - 1] > c && k > 0) { + top--; + k--; + } + stack[top++] = c; + } + + int index = 0; + while (index < digits && stack[index] == '0') { + index++; + } + return index == digits ? "0" : new String(stack, index, digits - index); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_403.java b/src/main/java/com/fishercoder/solutions/firstthousand/_403.java new file mode 100644 index 0000000000..4ab6a71c27 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_403.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _403 { + + public static class Solution1 { + /* + * Reference: https://discuss.leetcode.com/topic/59903/very-easy-to-understand-java-solution-with-explanations/2 + * and https://leetcode.com/articles/frog-jump/#approach-5-using-dynamic-programmingaccepted + */ + public boolean canCross(int[] stones) { + if (stones.length == 0) { + return true; + } + Map> map = new HashMap<>(stones.length); + map.put(0, new HashSet<>()); + map.get(0).add(1); + for (int i = 1; i < stones.length; i++) { + map.put(stones[i], new HashSet<>()); + } + + for (int i = 0; i < stones.length; i++) { + int stone = stones[i]; + for (int step : map.get(stone)) { + int reach = step + stone; + if (reach == stones[stones.length - 1]) { + return true; + } + Set set = map.get(reach); + if (set != null) { + set.add(step); + if (step - 1 > 0) { + set.add(step - 1); + } + set.add(step + 1); + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_404.java b/src/main/java/com/fishercoder/solutions/firstthousand/_404.java new file mode 100644 index 0000000000..82379ea59b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_404.java @@ -0,0 +1,84 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; + +public class _404 { + public static class Solution1 { + public int sumOfLeftLeaves(TreeNode root) { + int result = 0; + if (root == null) { + return result; + } + return dfs(root, result, false); + } + + private int dfs(TreeNode root, int result, boolean left) { + if (root.left == null && root.right == null && left) { + result += root.val; + return result; + } + int leftResult = 0; + if (root.left != null) { + left = true; + leftResult = dfs(root.left, result, left); + } + int rightResult = 0; + if (root.right != null) { + left = false; + rightResult = dfs(root.right, result, left); + } + return leftResult + rightResult; + } + } + + public static class Solution2 { + + public int sumOfLeftLeaves(TreeNode root) { + int sum = 0; + if (root == null) { + return sum; + } + if (root.left != null) { + if (root.left.left == null && root.left.right == null) { + sum += root.left.val; + } else { + sum += sumOfLeftLeaves(root.left); + } + } + if (root.right != null) { + sum += sumOfLeftLeaves(root.right); + } + return sum; + } + } + + public static class Solution3 { + /* + * My completely original solution on 11/4/2021. + */ + public int sumOfLeftLeaves(TreeNode root) { + Queue queue = new LinkedList<>(); + queue.offer(root); + int level = 0; + int sum = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (curr == null) { + continue; + } + if (level > 0 && curr.left == null && curr.right == null && i % 2 != 1) { + sum += curr.val; + } + queue.offer(curr.left); + queue.offer(curr.right); + } + level++; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_405.java b/src/main/java/com/fishercoder/solutions/firstthousand/_405.java new file mode 100644 index 0000000000..b4828f5a38 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_405.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.firstthousand; + +public class _405 { + + public static class Solution1 { + public String toHex(int num) { + char[] hexChars = + new char[] { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', + 'f' + }; + String result = ""; + while (num != 0) { + result = hexChars[(num & 15)] + result; + num >>>= 4; + } + return result.isEmpty() ? "0" : result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_406.java b/src/main/java/com/fishercoder/solutions/firstthousand/_406.java new file mode 100644 index 0000000000..6c07d50e5a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_406.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; + +public class _406 { + + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/60437/java-solution-using-priorityqueue-and-linkedlist + */ + public int[][] reconstructQueue(int[][] people) { + Arrays.sort( + people, + new Comparator() { + public int compare(int[] p1, int[] p2) { + return p1[0] != p2[0] + ? Integer.compare(p2[0], p1[0]) + : Integer.compare(p1[1], p2[1]); + } + }); + List list = new LinkedList(); + for (int[] ppl : people) { + list.add(ppl[1], ppl); + } + return list.toArray(new int[people.length][]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_407.java b/src/main/java/com/fishercoder/solutions/firstthousand/_407.java new file mode 100644 index 0000000000..56bef30b84 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_407.java @@ -0,0 +1,70 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.PriorityQueue; + +public class _407 { + public static class Solution1 { + /* + * Reference: https://discuss.leetcode.com/topic/60418/java-solution-using-priorityqueue + */ + public class Cell { + int row; + int col; + int height; + + public Cell(int row, int col, int height) { + this.row = row; + this.col = col; + this.height = height; + } + } + + public int trapRainWater(int[][] heights) { + if (heights == null || heights.length == 0 || heights[0].length == 0) { + return 0; + } + + PriorityQueue queue = new PriorityQueue<>(1, (a, b) -> a.height - b.height); + + int m = heights.length; + int n = heights[0].length; + boolean[][] visited = new boolean[m][n]; + + // Initially, add all the Cells which are on borders to the queue. + for (int i = 0; i < m; i++) { + visited[i][0] = true; + visited[i][n - 1] = true; + queue.offer(new Cell(i, 0, heights[i][0])); + queue.offer(new Cell(i, n - 1, heights[i][n - 1])); + } + + for (int i = 0; i < n; i++) { + visited[0][i] = true; + visited[m - 1][i] = true; + queue.offer(new Cell(0, i, heights[0][i])); + queue.offer(new Cell(m - 1, i, heights[m - 1][i])); + } + + // from the borders, pick the shortest cell visited and check its neighbors: + // if the neighbor is shorter, collect the water it can trap and update its height as + // its height plus the water trapped + // add all its neighbors to the queue. + int[][] dirs = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + int res = 0; + while (!queue.isEmpty()) { + Cell cell = queue.poll(); + for (int[] dir : dirs) { + int row = cell.row + dir[0]; + int col = cell.col + dir[1]; + if (row >= 0 && row < m && col >= 0 && col < n && !visited[row][col]) { + visited[row][col] = true; + res += Math.max(0, cell.height - heights[row][col]); + queue.offer(new Cell(row, col, Math.max(heights[row][col], cell.height))); + } + } + } + + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_408.java b/src/main/java/com/fishercoder/solutions/firstthousand/_408.java new file mode 100644 index 0000000000..9608f28278 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_408.java @@ -0,0 +1,102 @@ +package com.fishercoder.solutions.firstthousand; + +public class _408 { + + public static class Solution1 { + public boolean validWordAbbreviation(String word, String abbr) { + if (abbr.length() > word.length()) { + return false; + } else { + char[] abbrChars = abbr.toCharArray(); + char[] wordChars = word.toCharArray(); + if (abbr.length() == word.length()) { + boolean prevDigit = false; + for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++, j++) { + if (Character.isDigit(abbrChars[i]) && !prevDigit) { + prevDigit = true; + if (Character.getNumericValue(abbrChars[i]) != 1) { + return false; + } + } else if (Character.isDigit(abbrChars[i]) && prevDigit) { + return false; + } else if (abbrChars[i] != wordChars[j]) { + return false; + } else if (prevDigit) { + prevDigit = false; + } + } + return true; + } else { + StringBuilder stringBuilder = new StringBuilder(); + boolean firstDigit = true; + for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++) { + while (i < abbrChars.length && Character.isDigit(abbrChars[i])) { + if (firstDigit && Character.getNumericValue(abbrChars[i]) == 0) { + return false; + } + stringBuilder.append(abbrChars[i]); + i++; + firstDigit = false; + } + firstDigit = true; + if (!stringBuilder.toString().isEmpty()) { + int number = Integer.valueOf(stringBuilder.toString()); + j += number; + stringBuilder.setLength(0); + } + if ((i >= abbrChars.length && j < wordChars.length) + || (i < abbrChars.length && j >= wordChars.length)) { + return false; + } + if (i < abbrChars.length + && j < wordChars.length + && abbrChars[i] != wordChars[j]) { + return false; + } + if (j > wordChars.length && i <= abbrChars.length) { + return false; + } + j++; + } + return true; + } + } + } + } + + public static class Solution2 { + public boolean validWordAbbreviation(String word, String abbr) { + int aLen = abbr.length(); + int wLen = word.length(); + if (aLen > wLen) { + return false; + } + int i = 0; + int j = 0; + while (i < wLen && j < aLen) { + if (word.charAt(i) == abbr.charAt(j)) { + i++; + j++; + continue; + } + + // now the two chars don't match, then the char in abbr should be a valid digit: 0 < + // x <= 9 + if (abbr.charAt(j) == '0' || !Character.isDigit(abbr.charAt(j))) { + return false; + } + + // now we count the number of letters that are abbreviated, i.e. get the number from + // abbr before next letter shows up in abbr + int num = 0; + while (j < aLen && Character.isDigit(abbr.charAt(j))) { + num = num * 10 + (abbr.charAt(j) - '0'); + j++; + } + + i += num; + } + return i == wLen && j == aLen; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_409.java b/src/main/java/com/fishercoder/solutions/firstthousand/_409.java new file mode 100644 index 0000000000..07316ec1dd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_409.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _409 { + public static class Solution1 { + public int longestPalindrome(String s) { + int[] counts = new int[56]; + for (char c : s.toCharArray()) { + if (Character.isUpperCase(c)) { + counts[c - 'A' + 33]++; + } else { + counts[c - 'a']++; + } + } + boolean hasOdd = false; + int len = 0; + for (int i = 0; i < 56; i++) { + if (counts[i] % 2 != 0) { + hasOdd = true; + if (counts[i] > 1) { + len += counts[i] - 1; + } + } else { + len += counts[i]; + } + } + return hasOdd ? len + 1 : len; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/14/2021. + */ + public int longestPalindrome(String s) { + Map map = new HashMap<>(); + for (char c : s.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + int ans = 0; + boolean hasOdd = false; + for (char key : map.keySet()) { + if (map.getOrDefault(key, 0) % 2 == 0) { + ans += map.getOrDefault(key, 0); + } else { + hasOdd = true; + if (map.containsKey(key)) { + ans += map.getOrDefault(key, 0) - 1; + } + } + } + if (hasOdd) { + ans++; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_41.java b/src/main/java/com/fishercoder/solutions/firstthousand/_41.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_41.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_41.java index 41e2662135..e0851220ee 100644 --- a/src/main/java/com/fishercoder/solutions/_41.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_41.java @@ -1,17 +1,19 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _41 { public static class Solution1 { - /** + /* * Time: O(n) Space: O(1) * Idea: put every number in its right position, e.g. put 5 in nums[4]. */ public int firstMissingPositive(int[] nums) { int i = 0; while (i < nums.length) { - if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.length && nums[i] != nums[nums[i] - - 1]) { + if (nums[i] > 0 + && nums[i] != i + 1 + && nums[i] - 1 < nums.length + && nums[i] != nums[nums[i] - 1]) { swap(nums, i, nums[i] - 1); } else { i++; diff --git a/src/main/java/com/fishercoder/solutions/_410.java b/src/main/java/com/fishercoder/solutions/firstthousand/_410.java similarity index 77% rename from src/main/java/com/fishercoder/solutions/_410.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_410.java index 573e5d7c2f..e1ff2241f8 100644 --- a/src/main/java/com/fishercoder/solutions/_410.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_410.java @@ -1,42 +1,15 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; -/** - * 410. Split Array Largest Sum - * - * Given an array which consists of non-negative integers and an integer m, - * you can split the array into m non-empty continuous subarrays. - * Write an algorithm to minimize the largest sum among these m subarrays. - - Note: - If n is the length of array, assume the following constraints are satisfied: - - 1 ≤ n ≤ 1000 - 1 ≤ m ≤ min(50, n) - - Examples: - - Input: - nums = [7,2,5,10,8] - m = 2 - - Output: - 18 - - Explanation: - There are four ways to split nums into two subarrays. - The best way is to split it into [7,2,5] and [10,8], - where the largest sum among the two subarrays is only 18. - */ public class _410 { public static class Solution1 { - /** + /* * credit: https://discuss.leetcode.com/topic/61324/clear-explanation-8ms-binary-search-java - * + *

* The answer is between maximum value of input array numbers and sum of those numbers. Use * binary search to approach the correct answer. We have l = max number of array; r = sum of all * numbers in the array; Every time we do mid = (l + r) / 2; - * + *

* Use greedy to narrow down left and right boundaries in binary search. 3.1 Cut the array from * left. 3.2 Try our best to make sure that the sum of numbers between each two cuts (inclusive) * is large enough but still less than mid. 3.3 We'll end up with two results: either we can @@ -60,7 +33,7 @@ public int splitArray(int[] nums, int m) { if (m == 1) { return (int) sum; } - //binary search + // binary search long l = max; long r = sum; while (l <= r) { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_411.java b/src/main/java/com/fishercoder/solutions/firstthousand/_411.java new file mode 100644 index 0000000000..a4260110d9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_411.java @@ -0,0 +1,102 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _411 { + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/61346/trie-bruteforce + */ + class Trie { + Trie[] children = new Trie[26]; + boolean isWord = false; + } + + Trie root = new Trie(); + List abbrs; + + public String minAbbreviation(String target, String[] dictionary) { + for (String s : dictionary) { + addTrie(s); + } + + for (int i = 0; i < target.length(); i++) { + abbrs = new ArrayList<>(); + abbrGenerator(target, 0, "", 0, i + 1); + for (String s : abbrs) { + if (search(s, root, 0, 0) == false) { + return s; + } + } + } + return ""; + } + + public void addTrie(String s) { + Trie cur = root; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (cur.children[c - 'a'] == null) { + cur.children[c - 'a'] = new Trie(); + } + cur = cur.children[c - 'a']; + } + cur.isWord = true; + } + + public boolean search(String target, Trie root, int i, int loop) { + if (root == null) { + return false; + } + + if (loop != 0) { + for (int a = 0; a < 26; a++) { + if (search(target, root.children[a], i, loop - 1)) { + return true; + } + } + return false; + } + if (i == target.length()) { + if (root.isWord) { + return true; + } + return false; + } + if (Character.isDigit(target.charAt(i))) { + int tmp = 0; + while (i < target.length() && Character.isDigit(target.charAt(i))) { + tmp = tmp * 10 + target.charAt(i) - '0'; + i++; + } + return search(target, root, i, tmp); + } else { + return search(target, root.children[target.charAt(i) - 'a'], i + 1, 0); + } + } + + public void abbrGenerator(String target, int i, String tmp, int abbr, int num) { + if (i == target.length()) { + if (num == 0 && abbr == 0) { + abbrs.add(tmp); + } + if (num == 1 && abbr != 0) { + abbrs.add(tmp + abbr); + } + return; + } + if (num <= 0) { + return; + } + char cur = target.charAt(i); + abbrGenerator( + target, + i + 1, + abbr == 0 ? tmp + cur : tmp + abbr + cur, + 0, + abbr == 0 ? num - 1 : num - 2); + abbrGenerator(target, i + 1, tmp, abbr + 1, num); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_412.java b/src/main/java/com/fishercoder/solutions/firstthousand/_412.java new file mode 100644 index 0000000000..b79783ff4c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_412.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _412 { + + public static class Solution1 { + public List fizzBuzz(int n) { + List result = new ArrayList(); + for (int i = 1; i <= n; i++) { + if (i % 3 == 0 && i % 5 == 0) { + result.add("FizzBuzz"); + } else if (i % 3 == 0) { + result.add("Fizz"); + } else if (i % 5 == 0) { + result.add("Buzz"); + } else { + result.add(Integer.toString(i)); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_413.java b/src/main/java/com/fishercoder/solutions/firstthousand/_413.java new file mode 100644 index 0000000000..2ccd4b5a9f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_413.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +public class _413 { + + public static class Solution1 { + // credit: https://discuss.leetcode.com/topic/62884/2ms-java-o-n-time-o-1-space-solution + public int numberOfArithmeticSlices(int[] A) { + int sum = 0; + int len = 2; + for (int i = 2; i < A.length; i++) { + if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) { + len++; + } else { + if (len > 2) { + sum += calculateSlices(len); + } + len = 2; // reset it to 2 + } + } + if (len > 2) { + sum += calculateSlices(len); + } + return sum; + } + + int calculateSlices(int len) { + return (len - 1) * (len - 2) / 2; + } + } + + class Solution2 { + // credit: https://discuss.leetcode.com/topic/63302/simple-java-solution-9-lines-2ms + public int numberOfArithmeticSlices(int[] A) { + int sum = 0; + int curr = 0; + for (int i = 2; i < A.length; i++) { + if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) { + curr++; + sum += curr; + } else { + curr = 0; + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_414.java b/src/main/java/com/fishercoder/solutions/firstthousand/_414.java new file mode 100644 index 0000000000..4b8e463e6d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_414.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.firstthousand; + +public class _414 { + + public static class Solution1 { + public int thirdMax(int[] nums) { + long max1 = Long.MIN_VALUE; + long max2 = Long.MIN_VALUE; + long max3 = Long.MIN_VALUE; + for (int i : nums) { + max1 = Math.max(max1, i); + } + for (int i : nums) { + if (i == max1) { + continue; + } + max2 = Math.max(max2, i); + } + for (int i : nums) { + if (i == max1 || i == max2) { + continue; + } + max3 = Math.max(max3, i); + } + return (int) (max3 == Long.MIN_VALUE ? max1 : max3); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_415.java b/src/main/java/com/fishercoder/solutions/firstthousand/_415.java new file mode 100644 index 0000000000..d7a49e4bf2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_415.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.firstthousand; + +public class _415 { + + public static class Solution1 { + /* + * My completely original solution on 10/14/2021. + */ + public String addStrings(String num1, String num2) { + StringBuilder sb = new StringBuilder(); + int i = num1.length() - 1; + int j = num2.length() - 1; + int carry = 0; + while (i >= 0 || j >= 0) { + int sum = carry; + if (i >= 0) { + sum += Character.getNumericValue(num1.charAt(i)); + } + if (j >= 0) { + sum += Character.getNumericValue(num2.charAt(j)); + } + sb.append(sum % 10); + carry = sum / 10; + i--; + j--; + } + if (carry > 0) { + sb.append(carry); + } + return sb.reverse().toString(); + } + } + + public static class Solution2 { + /* + * This is an optimized version of Solution1, on LeetCode, this version beats 100% while Solution1 beats only 67%. + */ + public String addStrings(String num1, String num2) { + StringBuilder sb = new StringBuilder(); + int i = num1.length() - 1; + int j = num2.length() - 1; + int carry = 0; + while (i >= 0 || j >= 0 || carry > 0) { + int sum = carry; + if (i >= 0) { + sum += num1.charAt(i) - '0'; + } + if (j >= 0) { + sum += num2.charAt(j) - '0'; + } + sb.append(sum % 10); + carry = sum / 10; + i--; + j--; + } + return sb.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_416.java b/src/main/java/com/fishercoder/solutions/firstthousand/_416.java new file mode 100644 index 0000000000..20fe577d8e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_416.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _416 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/67539/0-1-knapsack-detailed-explanation + */ + public boolean canPartition(int[] nums) { + int sum = 0; + for (int num : nums) { + sum += num; + } + + if ((sum & 1) == 1) { + return false; + } + + sum /= 2; + + int n = nums.length; + boolean[][] dp = new boolean[n + 1][sum + 1]; + for (int i = 0; i < dp.length; i++) { + Arrays.fill(dp[i], false); + } + + dp[0][0] = true; + + for (int i = 1; i < n + 1; i++) { + dp[i][0] = true; + } + + for (int j = 1; j < sum + 1; j++) { + dp[0][j] = false; + } + + for (int i = 1; i < n + 1; i++) { + for (int j = 1; j < sum + 1; j++) { + dp[i][j] = dp[i - 1][j]; + if (j >= nums[i - 1]) { + dp[i][j] = (dp[i][j] || dp[i - 1][j - nums[i - 1]]); + } + } + } + + return dp[n][sum]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_417.java b/src/main/java/com/fishercoder/solutions/firstthousand/_417.java new file mode 100644 index 0000000000..4902a0c165 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_417.java @@ -0,0 +1,134 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _417 { + public static class Solution1 { + /* + * Credit: looked at this post: https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean + *

+ * One typical trick to work on 2d grid problems is to go through the border and put proper ones into a queue if using BFS. + */ + public List pacificAtlantic(int[][] matrix) { + + List result = new ArrayList(); + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return result; + } + + int m = matrix.length; + int n = matrix[0].length; + boolean[][] pacific = new boolean[m][n]; + boolean[][] atlantic = new boolean[m][n]; + + for (int i = 0; i < m; i++) { + dfs(matrix, pacific, Integer.MIN_VALUE, i, 0); + dfs(matrix, atlantic, Integer.MIN_VALUE, i, n - 1); + } + + for (int i = 0; i < n; i++) { + dfs(matrix, pacific, Integer.MIN_VALUE, 0, i); + dfs(matrix, atlantic, Integer.MIN_VALUE, m - 1, i); + } + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (pacific[i][j] && atlantic[i][j]) { + result.add(new int[] {i, j}); + } + } + } + + return result; + } + + void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y) { + int m = matrix.length; + int n = matrix[0].length; + if (x < 0 || y < 0 || x >= m || y >= n || matrix[x][y] < height || visited[x][y]) { + return; + } + visited[x][y] = true; + dfs(matrix, visited, matrix[x][y], x + 1, y); + dfs(matrix, visited, matrix[x][y], x, y + 1); + dfs(matrix, visited, matrix[x][y], x - 1, y); + dfs(matrix, visited, matrix[x][y], x, y - 1); + } + } + + public static class Solution2 { + /*This is my original solution on 3/25/2021, although brute force, it works.*/ + public List> pacificAtlantic(int[][] matrix) { + List> result = new ArrayList<>(); + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return result; + } + int m = matrix.length; + int n = matrix[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + boolean[] touchesPacificAndAtlantic = new boolean[2]; + update(i, j, m, n, touchesPacificAndAtlantic); + Queue queue = new LinkedList<>(); + boolean[][] visited = new boolean[m][n]; + visited[i][j] = true; + queue.offer(new int[] {i, j}); + if (bfs(matrix, m, n, touchesPacificAndAtlantic, queue, visited)) { + result.add(new ArrayList<>(Arrays.asList(i, j))); + } + } + } + return result; + } + + private void update(int i, int j, int m, int n, boolean[] touchesPacificAndAtlantic) { + if (i == 0 || j == 0) { + touchesPacificAndAtlantic[0] = true; + } + if (i == m - 1 || j == n - 1) { + touchesPacificAndAtlantic[1] = true; + } + } + + private boolean bfs( + int[][] matrix, + int m, + int n, + boolean[] touchesPacificAndAtlantic, + Queue queue, + boolean[][] visited) { + if (touchesPacificAndAtlantic[0] && touchesPacificAndAtlantic[1]) { + return true; + } + int[] directions = new int[] {0, 1, 0, -1, 0}; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int k = 0; k < size; k++) { + int[] curr = queue.poll(); + for (int p = 0; p < directions.length - 1; p++) { + int newx = curr[0] + directions[p]; + int newy = curr[1] + directions[p + 1]; + if (newx >= 0 + && newx < m + && newy >= 0 + && newy < n + && matrix[newx][newy] <= matrix[curr[0]][curr[1]] + && !visited[newx][newy]) { + visited[newx][newy] = true; + queue.offer(new int[] {newx, newy}); + update(newx, newy, m, n, touchesPacificAndAtlantic); + if (touchesPacificAndAtlantic[0] && touchesPacificAndAtlantic[1]) { + return true; + } + } + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_418.java b/src/main/java/com/fishercoder/solutions/firstthousand/_418.java new file mode 100644 index 0000000000..53fccb0b61 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_418.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +public class _418 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/62455/21ms-18-lines-java-solution + *

+ * 1. String s = String.join(" ", sentence) + " " ;. This line gives us a formatted sentence to be put to our screen. + * 2. start is the counter for how many valid characters from s have been put to our screen. + * 3. if (s.charAt(start % l) == ' ') is the situation that we don't need an extra space for current row. The current row could be successfully fitted. So that we need to increase our counter by using start++. + * 4. The else is the situation, which the next word can't fit to current row. So that we need to remove extra characters from next word. + * 5. start / s.length() is (# of valid characters) / our formatted sentence. + */ + public int wordsTyping(String[] sentence, int rows, int cols) { + String s = String.join(" ", sentence) + " "; + int start = 0; + int l = s.length(); + for (int i = 0; i < rows; i++) { + start += cols; + if (s.charAt(start % l) == ' ') { + start++; + } else { + while (start > 0 && s.charAt((start - 1) % l) != ' ') { + start--; + } + } + } + return start / s.length(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_419.java b/src/main/java/com/fishercoder/solutions/firstthousand/_419.java new file mode 100644 index 0000000000..5a1f8bb1e4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_419.java @@ -0,0 +1,82 @@ +package com.fishercoder.solutions.firstthousand; + +public class _419 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/62970/simple-java-solution, + *

+ * This solution does NOT modify original input. + * Basically, it only counts the top-left one while ignoring all other parts of one battleship, + * using the top-left one as a representative for one battle. + * This is achieved by counting cells that don't have 'X' to the left and above them. + */ + public int countBattleships(char[][] board) { + if (board == null || board.length == 0) { + return 0; + } + int count = 0; + int m = board.length; + int n = board[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == '.') { + continue; // if it can pass this line, then board[i][j] must be 'X' + } + if (j > 0 && board[i][j - 1] == 'X') { + continue; // then we check if its left is 'X' + } + if (i > 0 && board[i - 1][j] == 'X') { + continue; // also check if its top is 'X' + } + count++; + } + } + return count; + } + } + + public static class Solution2 { + /* + * My original solution, actually modified the input. I just undo it at the end. + */ + public int countBattleships(char[][] board) { + if (board == null || board.length == 0) { + return 0; + } + int result = 0; + int m = board.length; + int n = board[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == 'X') { + result++; + dfs(board, i, j, m, n); + } + } + } + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == '#') { + board[i][j] = 'X'; + } + } + } + return result; + } + + private void dfs(char[][] board, int x, int y, int m, int n) { + if (x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'X') { + return; + } + if (board[x][y] == 'X') { + board[x][y] = '#'; + } + dfs(board, x + 1, y, m, n); + dfs(board, x, y + 1, m, n); + dfs(board, x - 1, y, m, n); + dfs(board, x, y - 1, m, n); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_42.java b/src/main/java/com/fishercoder/solutions/firstthousand/_42.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_42.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_42.java index 9ddd3453c0..d759c0f263 100644 --- a/src/main/java/com/fishercoder/solutions/_42.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_42.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _42 { public static class Solution1 { - /** + /* * O(n) time and O(1) space, awesome! * * 1. first scan to find the max height index @@ -50,4 +50,4 @@ public int trap(int[] height) { return water; } } -} \ No newline at end of file +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_420.java b/src/main/java/com/fishercoder/solutions/firstthousand/_420.java new file mode 100644 index 0000000000..cdf837a719 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_420.java @@ -0,0 +1,71 @@ +package com.fishercoder.solutions.firstthousand; + +public class _420 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/63854/o-n-java-solution-by-analyzing-changes-allowed-to-fix-each-condition + */ + public int strongPasswordChecker(String s) { + int res = 0; + int a = 1; + int A = 1; + int d = 1; + char[] carr = s.toCharArray(); + int[] arr = new int[carr.length]; + + for (int i = 0; i < arr.length; ) { + if (Character.isLowerCase(carr[i])) { + a = 0; + } + if (Character.isUpperCase(carr[i])) { + A = 0; + } + if (Character.isDigit(carr[i])) { + d = 0; + } + + int j = i; + while (i < carr.length && carr[i] == carr[j]) { + i++; + } + arr[j] = i - j; + } + + int totalMissing = (a + A + d); + + if (arr.length < 6) { + res += totalMissing + Math.max(0, 6 - (arr.length + totalMissing)); + } else { + int overLen = Math.max(arr.length - 20, 0); + int leftOver = 0; + res += overLen; + + for (int k = 1; k < 3; k++) { + for (int i = 0; i < arr.length && overLen > 0; i++) { + if (arr[i] < 3 || arr[i] % 3 != (k - 1)) { + continue; + } + arr[i] -= Math.min(overLen, k); + overLen -= k; + } + } + + for (int i = 0; i < arr.length; i++) { + if (arr[i] >= 3 && overLen > 0) { + int need = arr[i] - 2; + arr[i] -= overLen; + overLen -= need; + } + + if (arr[i] >= 3) { + leftOver += arr[i] / 3; + } + } + + res += Math.max(totalMissing, leftOver); + } + + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_421.java b/src/main/java/com/fishercoder/solutions/firstthousand/_421.java new file mode 100644 index 0000000000..8b5ac84b61 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_421.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _421 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/63213/java-o-n-solution-using-bit-manipulation-and-hashmap/7 + *

+ * Note: comment out those system.out.println statements before submitting on LeetCode, otherwise TLE. + */ + public int findMaximumXOR(int[] nums) { + int max = 0; + int mask = 0; + for (int i = 31; i >= 0; i--) { + mask |= (1 << i); // the mask will grow like this: 100...000, 110...000, + // 111...000 to 111...111, each time, we only get the most + // left part of all numbers in the given array + System.out.println("mask = " + Integer.toBinaryString(mask)); + Set set = new HashSet<>(); + for (int num : nums) { + System.out.println("num = " + Integer.toBinaryString(num)); + set.add(num & mask); + System.out.println("mask & num = " + Integer.toBinaryString(mask & num)); + } + + int candidate = max | (1 << i); + System.out.println("candidate = " + Integer.toBinaryString(candidate)); + /*Reason behind this: if a ^ prefix = candidate, then a ^ candidate = prefix, also prefix ^ candidate = a + * in this below code: we use this one: prefix ^ candidate = a*/ + for (int prefix : set) { + System.out.println( + "candidate ^ prefix = " + Integer.toBinaryString(candidate ^ prefix)); + if (set.contains(candidate ^ prefix)) { + max = candidate; + } + } + System.out.println("max = " + max); + System.out.println("i = " + i); + System.out.println("==============================================="); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_422.java b/src/main/java/com/fishercoder/solutions/firstthousand/_422.java new file mode 100644 index 0000000000..fc885ec566 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_422.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.List; + +public class _422 { + + public static class Solution1 { + public boolean validWordSquare(List words) { + for (int i = 0; i < words.size(); i++) { + String word = words.get(i); + for (int j = 0; j < word.length(); j++) { + if (j >= words.size()) { + return false; + } + if (i >= words.get(j).length()) { + return false; + } + if (word.charAt(j) != words.get(j).charAt(i)) { + return false; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_423.java b/src/main/java/com/fishercoder/solutions/firstthousand/_423.java new file mode 100644 index 0000000000..d7b5e4701e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_423.java @@ -0,0 +1,80 @@ +package com.fishercoder.solutions.firstthousand; + +public class _423 { + + public static class Solution1 { + public String originalDigits(String s) { + /*we can use one char as a representative to uniquely stand for one number, + * for some numbers that we cannot find a unique representive, we can dedup. + * e.g. for number one, if we use 'o' as its representive, then 'o' also exists in numbers 2, 4 and 0, so + * we need to dedupe the 'o' in those numbers. + * Also, the order to dedupe matters: + * we'll have to dedupe for counts[3], counts[5], counts[7] first before we dedupe counts[1] and counts[9].*/ + int[] counts = new int[10]; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == 'o') { + counts[1]++; // 2,4,0 + } + if (s.charAt(i) == 'w') { + counts[2]++; + } + if (s.charAt(i) == 'h') { + counts[3]++; // 8 + } + if (s.charAt(i) == 'u') { + counts[4]++; + } + if (s.charAt(i) == 'f') { + counts[5]++; // 4 + } + if (s.charAt(i) == 'x') { + counts[6]++; + } + if (s.charAt(i) == 'v') { + counts[7]++; // 5 + } + if (s.charAt(i) == 'g') { + counts[8]++; + } + if (s.charAt(i) == 'i') { + counts[9]++; // 5,6,8 + } + if (s.charAt(i) == 'z') { + counts[0]++; + } + } + + counts[3] -= counts[8]; + counts[5] -= counts[4]; + counts[7] -= counts[5]; + counts[1] = counts[1] - (counts[2] + counts[4] + counts[0]); + counts[9] = counts[9] - (counts[5] + counts[6] + counts[8]); + + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < 10; i++) { + for (int j = 0; j < counts[i]; j++) { + stringBuilder.append(i); + } + } + return stringBuilder.toString(); + } + } + + public static class Solution2 { + /*My original idea on 3/28/2021, similar to the above idea in Solution1: + * + * we can use signal characters to sort these unsorted characters out + 1. z must be mapping to zero; 0 + 2. x -> six; 6 + 3. w -> two; 2 + 4. u -> four; 4 + 5. g -> eight; 8 + 6. only two digits have f: five and four, four is represented by the letter u, so the remaining f must form five; 5 + 7. only two digits have v: seven and five, five is done based on rule 6, so the remaining v must form seven; 7 + 8. only two digits have h: three and eight, eight is done based on rule 5, so the remaining h must form three; 3 + 9. four digits could have o: zero, one, two and four, since all the latter 3 digits have been done already, so the remaining o must form one; 1 + 10. all the rest of the unmapped characters must be able to form a multiple of nine; 9 + 11. then all 10 digits are sorted out + */ + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_424.java b/src/main/java/com/fishercoder/solutions/firstthousand/_424.java new file mode 100644 index 0000000000..67f06907b8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_424.java @@ -0,0 +1,70 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _424 { + + public static class Solution1 { + // credit: + // https://discuss.leetcode.com/topic/63494/java-12-lines-o-n-sliding-window-solution-with-explanation + public int characterReplacement(String s, int k) { + int len = s.length(); + int[] count = new int[26]; + int start = 0; + int maxCount = 0; + int maxLength = 0; + for (int end = 0; end < len; end++) { + maxCount = Math.max(maxCount, ++count[s.charAt(end) - 'A']); + while (end - start + 1 - maxCount > k) { + count[s.charAt(start) - 'A']--; + start++; + } + maxLength = Math.max(maxLength, end - start + 1); + } + return maxLength; + } + } + + public static class Solution2 { + /* + * My original solution using Sliding Window technique: + * I try to use each character as the possible candidate to find all solutions and compare. + */ + public int characterReplacement(String s, int k) { + Set set = new HashSet<>(); + for (char c : s.toCharArray()) { + set.add(c); + } + int ans = 0; + for (char c : set) { + ans = Math.max(ans, slidingWindow(c, s, k)); + } + return ans; + } + + private int slidingWindow(char c, String s, int k) { + int left = 0; + int right = 0; + int ans = 0; + while (right < s.length()) { + if (s.charAt(right) != c) { + if (k > 0) { + k--; + right++; + } else { + while (left < s.length() && s.charAt(left) == c) { + left++; + } + left++; + k++; + } + } else { + right++; + } + ans = Math.max(ans, right - left); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_425.java b/src/main/java/com/fishercoder/solutions/firstthousand/_425.java new file mode 100644 index 0000000000..519ab61bc8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_425.java @@ -0,0 +1,93 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _425 { + + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/63516/explained-my-java-solution-using-trie-126ms-16-16/2 + */ + + class TrieNode { + List startWith; + TrieNode[] children; + + TrieNode() { + startWith = new ArrayList<>(); + children = new TrieNode[26]; + } + } + + class Trie { + TrieNode root; + + Trie(String[] words) { + root = new TrieNode(); + for (String word : words) { + TrieNode cur = root; + for (char ch : word.toCharArray()) { + int index = ch - 'a'; + if (cur.children[index] == null) { + cur.children[index] = new TrieNode(); + } + cur.children[index].startWith.add(word); + cur = cur.children[index]; + } + } + } + + List findByPrefix(String prefix) { + List ans = new ArrayList<>(); + TrieNode cur = root; + for (char ch : prefix.toCharArray()) { + int index = ch - 'a'; + if (cur.children[index] == null) { + return ans; + } + + cur = cur.children[index]; + } + ans.addAll(cur.startWith); + return ans; + } + } + + public List> wordSquares(String[] words) { + List> ans = new ArrayList<>(); + if (words == null || words.length == 0) { + return ans; + } + int len = words[0].length(); + Trie trie = new Trie(words); + List ansBuilder = new ArrayList<>(); + for (String w : words) { + ansBuilder.add(w); + search(len, trie, ans, ansBuilder); + ansBuilder.remove(ansBuilder.size() - 1); + } + + return ans; + } + + private void search(int len, Trie trie, List> ans, List ansBuilder) { + if (ansBuilder.size() == len) { + ans.add(new ArrayList<>(ansBuilder)); + return; + } + + int idx = ansBuilder.size(); + StringBuilder prefixBuilder = new StringBuilder(); + for (String s : ansBuilder) { + prefixBuilder.append(s.charAt(idx)); + } + List startWith = trie.findByPrefix(prefixBuilder.toString()); + for (String sw : startWith) { + ansBuilder.add(sw); + search(len, trie, ans, ansBuilder); + ansBuilder.remove(ansBuilder.size() - 1); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_426.java b/src/main/java/com/fishercoder/solutions/firstthousand/_426.java new file mode 100644 index 0000000000..706a319465 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_426.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.firstthousand; + +public class _426 { + + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/solutions/1494821/simplest-java-solution-with-explanation-inorder-traversal-in-place-no-dummy-node-needed/ + */ + Node head; + Node tail; + + public Node treeToDoublyList(Node root) { + if (root == null) { + return null; + } + dfs(root); + tail.right = head; + head.left = tail; + return head; + } + + private void dfs(Node node) { + if (node == null) { + return; + } + // we traverse all the way to the most bottom left leaf node first + dfs(node.left); + // we only need to assign head once, i.e. when it's not assigned, we'll assign the most + // bottom left leaf node to head + if (head == null) { + head = node; + } + // if the tail is already assigned, which should be all of the cases except when it's + // just finding the most bottom left leaf + // attach current node to tail's right, assign tail to current node's left + if (tail != null) { + tail.right = node; + node.left = tail; + } + // then always assign the current node to be the new tail + tail = node; + dfs(node.right); + } + } + + public static class Node { + public int val; + public Node left; + public Node right; + + public Node() {} + + public Node(int val) { + this.val = val; + } + + public Node(int val, Node left, Node right) { + this.val = val; + this.left = left; + this.right = right; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_427.java b/src/main/java/com/fishercoder/solutions/firstthousand/_427.java new file mode 100644 index 0000000000..f18963e61a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_427.java @@ -0,0 +1,111 @@ +package com.fishercoder.solutions.firstthousand; + +public class _427 { + static class Node { + public boolean val; + public boolean isLeaf; + public Node topLeft; + public Node topRight; + public Node bottomLeft; + public Node bottomRight; + + public Node() { + this.val = false; + this.isLeaf = false; + this.topLeft = null; + this.topRight = null; + this.bottomLeft = null; + this.bottomRight = null; + } + + public Node(boolean val, boolean isLeaf) { + this.val = val; + this.isLeaf = isLeaf; + this.topLeft = null; + this.topRight = null; + this.bottomLeft = null; + this.bottomRight = null; + } + + public Node( + boolean val, + boolean isLeaf, + Node topLeft, + Node topRight, + Node bottomLeft, + Node bottomRight) { + this.val = val; + this.isLeaf = isLeaf; + this.topLeft = topLeft; + this.topRight = topRight; + this.bottomLeft = bottomLeft; + this.bottomRight = bottomRight; + } + } + + public static class Solution1 { + public Node construct(int[][] grid) { + return recurse(grid, 0, 0, grid.length); + } + + private Node recurse(int[][] grid, int row, int col, int limit) { + if (allTheSameValue(grid, row, col, limit)) { + return new Node(grid[row][col] == 1, true); + } else { + Node root = new Node(false, false); + // top left + root.topLeft = recurse(grid, row, col, limit / 2); + // top right + root.topRight = recurse(grid, row, col + limit / 2, limit / 2); + // bottom left + root.bottomLeft = recurse(grid, row + limit / 2, col, limit / 2); + // bottom right + root.bottomRight = recurse(grid, row + limit / 2, col + limit / 2, limit / 2); + return root; + } + } + + private boolean allTheSameValue(int[][] grid, int row, int col, int limit) { + int val = grid[row][col]; + for (int i = row; i < row + limit; i++) { + for (int j = col; j < col + limit; j++) { + if (val != grid[i][j]) { + return false; + } + } + } + return true; + } + } + + public static class Solution2 { + public Node construct(int[][] grid) { + return recurse(grid, 0, 0, grid.length); + } + + private Node recurse(int[][] grid, int row, int col, int limit) { + if (limit == 1) { + return new Node(grid[row][col] == 1, true); + } + Node topLeft = recurse(grid, row, col, limit / 2); + Node topRgith = recurse(grid, row, col + limit / 2, limit / 2); + Node bottomLeft = recurse(grid, row + limit / 2, col, limit / 2); + Node bottomRight = recurse(grid, row + limit / 2, col + limit / 2, limit / 2); + if (topLeft.isLeaf + && topRgith.isLeaf + && bottomLeft.isLeaf + && bottomRight.isLeaf + && topLeft.val == topRgith.val + && topLeft.val == bottomLeft.val + && topLeft.val == bottomRight.val) { + return new Node(topLeft.val, true); + } + Node root = new Node(grid[row][col] == 1, false); + root.topLeft = topLeft; + root.topRight = topRgith; + root.bottomLeft = bottomLeft; + root.bottomRight = bottomRight; + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_429.java b/src/main/java/com/fishercoder/solutions/firstthousand/_429.java new file mode 100644 index 0000000000..d35ccdd81a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_429.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.Node; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _429 { + public static class Solution1 { + public List> levelOrder(Node root) { + List> result = new ArrayList<>(); + if (root == null) { + return result; + } + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + List level = new ArrayList<>(); + for (int i = 0; i < size; i++) { + Node currentNode = queue.poll(); + if (currentNode != null) { + level.add(currentNode.val); + for (Node child : currentNode.children) { + queue.offer(child); + } + } + } + result.add(level); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_43.java b/src/main/java/com/fishercoder/solutions/firstthousand/_43.java new file mode 100644 index 0000000000..60430fc020 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_43.java @@ -0,0 +1,125 @@ +package com.fishercoder.solutions.firstthousand; + +public class _43 { + + public static class Solution1 { + /* + * Inspired by https://discuss.leetcode.com/topic/30508/easiest-java-solution-with-graph-explanation + * Basically, the rule we can find is that products of each two digits will land in this position in the final product: + * i+j and i+j+1 + */ + public String multiply(String num1, String num2) { + if (isZero(num1) || isZero(num2)) { + return "0"; + } + int[] a1 = new int[num1.length()]; + int[] a2 = new int[num2.length()]; + int[] product = new int[num1.length() + num2.length()]; + + for (int i = a1.length - 1; i >= 0; i--) { + for (int j = a2.length - 1; j >= 0; j--) { + int thisProduct = + Character.getNumericValue(num1.charAt(i)) + * Character.getNumericValue(num2.charAt(j)); + product[i + j + 1] += thisProduct % 10; + if (product[i + j + 1] >= 10) { + product[i + j + 1] %= 10; + product[i + j]++; + } + product[i + j] += thisProduct / 10; + if (product[i + j] >= 10) { + product[i + j] %= 10; + product[i + j - 1]++; + } + } + } + + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < product.length; i++) { + if (i == 0 && product[i] == 0) { + continue; + } + stringBuilder.append(product[i]); + } + return stringBuilder.toString(); + } + + private boolean isZero(String num) { + for (char c : num.toCharArray()) { + if (c != '0') { + return false; + } + } + return true; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/14/2021. + * + * Gist: just use string instead of integers for times variable, otherwise guaranteed to overflow/underflow! + * Also: using a pen and paper to visualize how this works out helps a great deal! + */ + public String multiply(String num1, String num2) { + String previous = ""; + String j = ""; + for (int i = num2.length() - 1; i >= 0; i--, j += "0") { + String intermediate = + multiplyBySingleDigit(num1, Character.getNumericValue(num2.charAt(i)), j); + String result = add(intermediate, previous); + previous = result; + } + int i = 0; + for (; i < previous.length(); i++) { + if (previous.charAt(i) != '0') { + break; + } + } + return i == previous.length() ? "0" : previous.substring(i); + } + + private String add(String num1, String num2) { + int i = num1.length() - 1; + int j = num2.length() - 1; + int carry = 0; + StringBuilder sb = new StringBuilder(); + while (i >= 0 || j >= 0) { + int sum = carry; + if (i >= 0) { + sum += Character.getNumericValue(num1.charAt(i)); + } + if (j >= 0) { + sum += Character.getNumericValue(num2.charAt(j)); + } + sb.append(sum % 10); + carry = sum / 10; + i--; + j--; + } + if (carry > 0) { + sb.append(carry); + } + return sb.reverse().toString(); + } + + private String multiplyBySingleDigit(String num, int multiplier, String times) { + if (multiplier == 0) { + return "0"; + } + StringBuilder sb = new StringBuilder(); + int carry = 0; + for (int i = num.length() - 1; i >= 0; i--) { + int val = Character.getNumericValue(num.charAt(i)); + int product = val * multiplier; + product += carry; + sb.append(product % 10); + carry = product / 10; + } + if (carry > 0) { + sb.append(carry); + } + return sb.reverse() + times; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_430.java b/src/main/java/com/fishercoder/solutions/firstthousand/_430.java new file mode 100644 index 0000000000..54f0069125 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_430.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.firstthousand; + +public class _430 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/solution/ + */ + public Node flatten(Node head) { + if (head == null) { + return null; + } + Node pre = new Node(-1, null, head, null); + dfs(pre, head); + pre.next.prev = null; + return pre.next; + } + + private Node dfs(Node prev, Node curr) { + if (curr == null) { + return prev; + } + curr.prev = prev; + prev.next = curr; + + Node next = curr.next; + Node tail = dfs(curr, curr.child); + curr.child = null; + return dfs(tail, next); + } + } + + public static class Node { + public int val; + public Node prev; + public Node next; + public Node child; + + public Node(int val, Node prev, Node next, Node child) { + this.val = val; + this.prev = prev; + this.next = next; + this.child = child; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_432.java b/src/main/java/com/fishercoder/solutions/firstthousand/_432.java similarity index 85% rename from src/main/java/com/fishercoder/solutions/_432.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_432.java index b296343d68..93f56c038e 100644 --- a/src/main/java/com/fishercoder/solutions/_432.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_432.java @@ -1,27 +1,15 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; -/** - * 432. All O`one Data Structure - * Implement a data structure supporting the following operations: - - 1. Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string. - 2. Dec(Key) - If Key's value is 1, remove it from the data structure. Otherwise decrements an existing key by 1. If the key does not exist, this function does nothing. Key is guaranteed to be a non-empty string. - 3. GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string "". - 4. GetMinKey() - Returns one of the keys with minimal value. If no element exists, return an empty string "". - - Challenge: Perform all these in O(1) time complexity. - - */ public class _432 { public static class Solution1 { - /** + /* * credit: https://discuss.leetcode.com/topic/65634/java-ac-all-strict-o-1-not-average-o-1-easy-to-read/2 */ class AllOne { @@ -46,7 +34,7 @@ public Bucket(int cnt) { } } - /** + /* * Initialize your data structure here. */ public AllOne() { @@ -58,7 +46,7 @@ public AllOne() { keyCountMap = new HashMap<>(); } - /** + /* * Inserts a new key with value 1. Or increments an existing key by 1. */ public void inc(String key) { @@ -74,7 +62,7 @@ public void inc(String key) { } } - /** + /* * Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ public void dec(String key) { @@ -89,14 +77,14 @@ public void dec(String key) { } } - /** + /* * Returns one of the keys with maximal value. */ public String getMaxKey() { return tail.pre == head ? "" : (String) tail.pre.keySet.iterator().next(); } - /** + /* * Returns one of the keys with Minimal value. */ public String getMinKey() { @@ -148,7 +136,7 @@ private void addBucketAfter(Bucket newBucket, Bucket preBucket) { } } -/** +/* * Your AllOne object will be instantiated and called as such: * AllOne obj = new AllOne(); * obj.inc(key); diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_433.java b/src/main/java/com/fishercoder/solutions/firstthousand/_433.java new file mode 100644 index 0000000000..d6ceb35505 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_433.java @@ -0,0 +1,56 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; + +public class _433 { + public static class Solution1 { + /* + * My completely original solution, BFS. + */ + public int minMutation(String startGene, String endGene, String[] bank) { + boolean found = false; + for (String b : bank) { + if (b.equals(endGene)) { + found = true; + } + } + if (!found) { + return -1; + } + Queue q = new LinkedList<>(); + q.offer(startGene); + int mutations = 0; + Set used = new HashSet<>(); + used.add(startGene); + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + String curr = q.poll(); + if (curr.equals(endGene)) { + return mutations; + } + for (String candidate : bank) { + if (oneDiff(curr, candidate) && used.add(candidate)) { + q.offer(candidate); + } + } + } + mutations++; + } + return -1; + } + + private boolean oneDiff(String word1, String word2) { + int diffChars = 0; + for (int i = 0; i < word1.length(); i++) { + if (word1.charAt(i) != word2.charAt(i)) { + diffChars++; + } + } + return diffChars == 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_434.java b/src/main/java/com/fishercoder/solutions/firstthousand/_434.java new file mode 100644 index 0000000000..15f92217e4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_434.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +public class _434 { + + public static class Solution1 { + public int countSegments(String s) { + if (s == null || s.isEmpty()) { + return 0; + } + String[] segments = s.split(" "); + int count = 0; + for (String seg : segments) { + if (seg.equals("")) { + continue; + } + count++; + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_435.java b/src/main/java/com/fishercoder/solutions/firstthousand/_435.java new file mode 100644 index 0000000000..1f19e078b7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_435.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _435 { + + public static class Solution1 { + /* + * credit:: https://discuss.leetcode.com/topic/65828/java-solution-with-clear-explain + * and https://discuss.leetcode.com/topic/65594/java-least-is-most + * Sort the intervals by their end time, if equal, then sort by their start time. + * Then merge based on ending time. + * + * This comment is inspiring as of why sorting by end time could guarantee the correct answer: https://leetcode.com/problems/non-overlapping-intervals/discuss/91713/Java:-Least-is-Most/96271 + * In my words: if the earliest finished interval is not included, it's more likely that others (with later finish time) will overlap with others. + */ + public int eraseOverlapIntervals(int[][] intervals) { + Arrays.sort(intervals, (a, b) -> a[1] != b[1] ? a[1] - b[1] : a[0] - b[0]); + int erasures = 0; + int end = intervals[0][1]; + for (int i = 1; i < intervals.length; i++) { + if (intervals[i][0] < end) { + erasures++; + } else { + end = intervals[i][1]; + } + } + return erasures; + } + } + + public static class Solution2 { + /* + * This is sorting my starting time, the key here is that we'll want to update end time when an erasure is needed: + * we use the smaller end time instead of the bigger one which is more likely to overlap with others. + */ + public int eraseOverlapIntervals(int[][] intervals) { + Arrays.sort(intervals, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); + int erasures = 0; + int end = intervals[0][1]; + for (int i = 1; i < intervals.length; i++) { + if (intervals[i][0] < end) { + erasures++; + end = Math.min(end, intervals[i][1]); + } else { + end = intervals[i][1]; + } + } + return erasures; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_436.java b/src/main/java/com/fishercoder/solutions/firstthousand/_436.java new file mode 100644 index 0000000000..6c4f22a4f1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_436.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.TreeMap; + +public class _436 { + + public static class Solution1 { + public int[] findRightInterval(int[][] intervals) { + TreeMap map = new TreeMap<>(); + int[] res = new int[intervals.length]; + for (int i = 0; i < intervals.length; i++) { + map.put(intervals[i][0], i); + } + for (int i = 0; i < intervals.length; i++) { + Integer key = map.ceilingKey(intervals[i][intervals[i].length - 1]); + res[i] = key != null ? map.get(key) : -1; + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_437.java b/src/main/java/com/fishercoder/solutions/firstthousand/_437.java new file mode 100644 index 0000000000..98cd7a9919 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_437.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _437 { + + public static class Solution1 { + public int pathSum(TreeNode root, int sum) { + if (root == null) { + return 0; + } + return pathSumFrom(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); + } + + private int pathSumFrom(TreeNode root, int sum) { + if (root == null) { + return 0; + } + return (root.val == sum ? 1 : 0) + + pathSumFrom(root.left, sum - root.val) + + pathSumFrom(root.right, sum - root.val); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_438.java b/src/main/java/com/fishercoder/solutions/firstthousand/_438.java new file mode 100644 index 0000000000..fa5f407d99 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_438.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _438 { + + public static class Solution1 { + /* + * Sliding Window + */ + public List findAnagrams(String s, String p) { + List result = new ArrayList(); + int[] hash = new int[26]; + for (char c : p.toCharArray()) { + hash[c - 'a']++; + } + int start = 0; + int end = 0; + int count = p.length(); + while (end < s.length()) { + if (hash[s.charAt(end) - 'a'] > 0) { + count--; + } + hash[s.charAt(end) - 'a']--; + end++; + + if (count == 0) { + result.add(start); + } + + if ((end - start) == p.length()) { + if (hash[s.charAt(start) - 'a'] >= 0) { + count++; + } + hash[s.charAt(start) - 'a']++; + start++; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_439.java b/src/main/java/com/fishercoder/solutions/firstthousand/_439.java new file mode 100644 index 0000000000..c3a14d82a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_439.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class _439 { + + public static class Solution1 { + /* + * Below is my original solution, but looking at Discuss, a more concise way is to use just one + * stack, process it from right to left, example: https://discuss.leetcode.com/topic/64409/very-easy-1-pass-stack-solution-in-java-no-string-concat + */ + + public String parseTernary(String expression) { + Deque stack = new ArrayDeque<>(); + Deque tmpStack = new ArrayDeque<>(); + for (char c : expression.toCharArray()) { + stack.addFirst(c); + } + while (!stack.isEmpty()) { + if (stack.peek() != '?') { + tmpStack.addFirst(stack.pollFirst()); + } else { + char char1 = tmpStack.removeFirst(); + tmpStack.removeFirst(); // remove ':' + char char2 = tmpStack.removeFirst(); + stack.removeFirst(); // remove '?' + char judge = stack.removeFirst(); + tmpStack.addFirst(judge == 'T' ? char1 : char2); + while (!tmpStack.isEmpty()) { + stack.addFirst(tmpStack.pollFirst()); + } + } + if (stack.size() == 1) { + break; + } + } + return Character.toString(stack.removeFirst()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_44.java b/src/main/java/com/fishercoder/solutions/firstthousand/_44.java similarity index 95% rename from src/main/java/com/fishercoder/solutions/_44.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_44.java index c7695340ad..b1dbe521e5 100644 --- a/src/main/java/com/fishercoder/solutions/_44.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_44.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _44 { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_440.java b/src/main/java/com/fishercoder/solutions/firstthousand/_440.java new file mode 100644 index 0000000000..606a5460e3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_440.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +public class _440 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/64624/concise-easy-to-understand-java-5ms-solution-with-explaination/2 + */ + public int findKthNumber(int n, int k) { + int curr = 1; + k = k - 1; + while (k > 0) { + int steps = calSteps(n, curr, curr + 1); + if (steps <= k) { + curr += 1; + k -= steps; + } else { + curr *= 10; + k -= 1; + } + } + return curr; + } + + // use long in case of overflow + public int calSteps(int n, long n1, long n2) { + int steps = 0; + while (n1 <= n) { + steps += Math.min(n + 1, n2) - n1; + n1 *= 10; + n2 *= 10; + } + return steps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_441.java b/src/main/java/com/fishercoder/solutions/firstthousand/_441.java new file mode 100644 index 0000000000..431cbbbbd1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_441.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _441 { + + public static class Solution1 { + public int arrangeCoins(int n) { + if (n < 2) { + return n; + } + int row = 0; + int count = 0; + long sum = 0; + while (sum < n) { + row += 1; + sum += row; + count++; + } + if (sum == n) { + return count; + } + return count - 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_442.java b/src/main/java/com/fishercoder/solutions/firstthousand/_442.java new file mode 100644 index 0000000000..87c5df7992 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_442.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _442 { + + public static class Solution1 { + // O(n) space + // O(n) time + public List findDuplicates(int[] nums) { + Set set = new HashSet(); + List result = new ArrayList(); + for (int i : nums) { + if (!set.add(i)) { + result.add(i); + } + } + return result; + } + } + + public static class Solution2 { + /* + * O(1) space + * O(n) time + *

+ * This approach makes full use of what the problem states: all the integers of nums are in the range [1, n], + * this implies that for any value x in this array, x - 1 must be a valid index in this array + * thus, nums[x - 1] is a valid element in this array. + *

+ * So the solution was born: + * we could mark one element as seen/visited before by negating it, + * so when we encounter this same number again, i.e. the number is negative, + * we know it appeared before, so we add it to the result + * and then negate this number back. + */ + public List findDuplicates(int[] nums) { + List result = new ArrayList(); + for (int i = 0; i < nums.length; i++) { + int index = Math.abs(nums[i]) - 1; + if (nums[index] < 0) { + result.add(Math.abs(index + 1)); + } + nums[index] = -nums[index]; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_443.java b/src/main/java/com/fishercoder/solutions/firstthousand/_443.java new file mode 100644 index 0000000000..8b2b487f44 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_443.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +public class _443 { + public static class Solution1 { + /* + * This is breaking the rules, it's not in-place. + */ + public int compress(char[] chars) { + if (chars == null || chars.length == 0) { + return 0; + } + StringBuilder sb = new StringBuilder(); + int count = 1; + char prev = chars[0]; + for (int i = 1; i < chars.length; i++) { + if (chars[i] == prev) { + count++; + } else { + if (count > 1) { + sb.append(prev); + sb.append(count); + } else if (count == 1) { + sb.append(prev); + } + prev = chars[i]; + count = 1; + } + } + sb.append(prev); + if (count > 1) { + sb.append(count); + } + int i = 0; + for (char c : sb.toString().toCharArray()) { + chars[i++] = c; + } + return sb.length(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_444.java b/src/main/java/com/fishercoder/solutions/firstthousand/_444.java new file mode 100644 index 0000000000..d6b83b2722 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_444.java @@ -0,0 +1,71 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _444 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/65948/java-solution-using-bfs-topological-sort + */ + public boolean sequenceReconstruction(int[] org, List> seqs) { + Map> map = new HashMap<>(); + Map indegree = new HashMap<>(); + for (List seq : seqs) { + if (seq.size() == 1) { + if (!map.containsKey(seq.get(0))) { + map.put(seq.get(0), new HashSet<>()); + indegree.put(seq.get(0), 0); + } + } else { + for (int i = 0; i < seq.size() - 1; i++) { + if (!map.containsKey(seq.get(i))) { + map.put(seq.get(i), new HashSet<>()); + indegree.put(seq.get(i), 0); + } + + if (!map.containsKey(seq.get(i + 1))) { + map.put(seq.get(i + 1), new HashSet<>()); + indegree.put(seq.get(i + 1), 0); + } + + if (map.get(seq.get(i)).add(seq.get(i + 1))) { + indegree.put(seq.get(i + 1), indegree.get(seq.get(i + 1)) + 1); + } + } + } + } + + Queue queue = new LinkedList<>(); + for (Integer key : indegree.keySet()) { + if (indegree.get(key) == 0) { + queue.offer(key); + } + } + + int index = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + if (size > 1) { + return false; + } + int curr = queue.poll(); + if (index == org.length || curr != org[index++]) { + return false; + } + for (int next : map.get(curr)) { + indegree.put(next, indegree.get(next) - 1); + if (indegree.get(next) == 0) { + queue.offer(next); + } + } + } + return index == org.length && index == map.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_445.java b/src/main/java/com/fishercoder/solutions/firstthousand/_445.java similarity index 84% rename from src/main/java/com/fishercoder/solutions/_445.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_445.java index 7b432c5041..87e6d89745 100644 --- a/src/main/java/com/fishercoder/solutions/_445.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_445.java @@ -1,27 +1,10 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.ListNode; - import java.util.ArrayDeque; import java.util.Deque; import java.util.Stack; -/** - * 445. Add Two Numbers II - * - * You are given two non-empty linked lists representing two non-negative integers. - * The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. - - You may assume the two numbers do not contain any leading zero, except the number 0 itself. - - Follow up: - What if you cannot modify the input lists? In other words, reversing the lists is not allowed. - - Example: - - Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) - Output: 7 -> 8 -> 0 -> 7 - */ public class _445 { public static class Solution1 { @@ -58,7 +41,6 @@ private Deque popIntoStack(ListNode head) { } } - public static class Solution2 { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Stack stack1 = popOntoStack(l1); diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_446.java b/src/main/java/com/fishercoder/solutions/firstthousand/_446.java new file mode 100644 index 0000000000..43b20633db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_446.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _446 { + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution + */ + public int numberOfArithmeticSlices(int[] A) { + int res = 0; + Map[] map = new Map[A.length]; + + for (int i = 0; i < A.length; i++) { + map[i] = new HashMap<>(i); + + for (int j = 0; j < i; j++) { + long diff = (long) A[i] - A[j]; + if (diff <= Integer.MIN_VALUE || diff > Integer.MAX_VALUE) { + continue; + } + + int d = (int) diff; + int c1 = map[i].getOrDefault(d, 0); + int c2 = map[j].getOrDefault(d, 0); + res += c2; + map[i].put(d, c1 + c2 + 1); + } + } + + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_447.java b/src/main/java/com/fishercoder/solutions/firstthousand/_447.java new file mode 100644 index 0000000000..1623024160 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_447.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _447 { + + public static class Solution1 { + /* + * Looked at these two posts: https://discuss.leetcode.com/topic/66587/clean-java-solution-o-n-2-166ms and + * https://discuss.leetcode.com/topic/66521/share-my-straightforward-solution-with-hashmap-o-n-2, basically, + * have a HashMap, key is the distance, value is the number of points that are this distance apart to this point. + * Note: we clear up this map every time after we traverse one point with the rest of the other points. + *

+ * Time complexity: O(n^2) + * Space complexity: O(n) + */ + + public int numberOfBoomerangs(int[][] points) { + int result = 0; + if (points == null || points.length == 0 || points[0].length == 0) { + return result; + } + int totalPts = points.length; + Map map = new HashMap(); + for (int i = 0; i < totalPts; i++) { + for (int j = 0; j < totalPts; j++) { + if (i == j) { + continue; + } + long d = calcDistance(points[i], points[j]); + map.put(d, map.getOrDefault(d, 0) + 1); + } + + for (int val : map.values()) { + result += val * (val - 1); + } + map.clear(); + } + return result; + } + + private long calcDistance(int[] p1, int[] p2) { + long x = p2[0] - p1[0]; + long y = p2[1] - p1[1]; + return x * x + y * y; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_448.java b/src/main/java/com/fishercoder/solutions/firstthousand/_448.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_448.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_448.java index 9d8980396f..d685712a31 100644 --- a/src/main/java/com/fishercoder/solutions/_448.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_448.java @@ -1,30 +1,14 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -/** - * 448. Find All Numbers Disappeared in an Array - * - * Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. - * Find all the elements of [1, n] inclusive that do not appear in this array. - * Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space. - - Example: - - Input: - [4,3,2,7,8,2,3,1] - - Output: - [5,6] - - */ public class _448 { public static class Solution1 { - /** + /* * O(n) space * O(n) time */ @@ -60,7 +44,7 @@ public List findDisappearedNumbers(int[] nums) { } public static class Solution2 { - /** + /* * O(1) space * O(n) time */ @@ -82,5 +66,4 @@ public List findDisappearedNumbers(int[] nums) { return result; } } - -} \ No newline at end of file +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_449.java b/src/main/java/com/fishercoder/solutions/firstthousand/_449.java new file mode 100644 index 0000000000..6124dec31f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_449.java @@ -0,0 +1,198 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Queue; + +public class _449 { + + public static class Solution1 { + /* + * Preorder + * Reference: https://discuss.leetcode.com/topic/97922/pre-or-post-order-with-only-keeping-one-bound-beat-98-and-95 + */ + + // Encodes a tree to a single string. + public String serialize(TreeNode root) { + if (root == null) { + return null; + } + StringBuilder stringBuilder = new StringBuilder(); + return serialize(root, stringBuilder); + } + + private String serialize(TreeNode root, StringBuilder stringBuilder) { + if (root == null) { + return null; + } + stringBuilder.append(root.val).append(" "); + serialize(root.left, stringBuilder); + serialize(root.right, stringBuilder); + return stringBuilder.toString(); + } + + // Decodes your encoded data to tree. + public TreeNode deserialize(String data) { + if (data == null || data.length() == 0) { + return null; + } + String[] values = data.split(" "); + int[] index = + new int[] {0}; /*TODO: Why must use an int array, instead of just an int?*/ + return deserialize(values, index, Integer.MAX_VALUE); + } + + private TreeNode deserialize(String[] values, int[] index, int maxValue) { + if (index[0] >= values.length || Integer.valueOf(values[index[0]]) >= maxValue) { + return null; + } + TreeNode root = new TreeNode(Integer.valueOf(values[index[0]++])); + root.left = deserialize(values, index, root.val); + root.right = deserialize(values, index, maxValue); + return root; + } + } + + public static class Solution2 { + /* + * Postorder + * Reference: https://discuss.leetcode.com/topic/97922/pre-or-post-order-with-only-keeping-one-bound-beat-98-and-95 + */ + + public String serialize(TreeNode root) { + if (root == null) { + return null; + } + StringBuilder sb = new StringBuilder(); + return serialize(root, sb); + } + + private String serialize(TreeNode root, StringBuilder sb) { + if (root == null) { + return null; + } + serialize(root.left, sb); + serialize(root.right, sb); + sb.append(root.val).append(" "); + return sb.toString(); + } + + // Decodes your encoded data to tree. + public TreeNode deserialize(String data) { + if (data == null || data.length() == 0) { + return null; + } + String[] values = data.split(" "); + int[] index = + new int[] { + values.length - 1 + }; /*TODO: This is not just one element any more like in the preorder solution above*/ + return deserialize(values, index, Integer.MIN_VALUE); + } + + private TreeNode deserialize(String[] values, int[] index, int minValue) { + if (index[0] < 0 || Integer.valueOf(values[index[0]]) < minValue) { + return null; + } + TreeNode root = new TreeNode(Integer.valueOf(values[index[0]--])); + root.right = deserialize(values, index, root.val); + root.left = deserialize(values, index, minValue); + return root; + } + } + + public static class Solution3 { + /* + * This is a generic solution that applies to both BT and BST. And also the easiest to follow. + */ + + // Encodes a tree to a single string. + public String serialize(TreeNode root) { + Queue queue = new LinkedList<>(); + StringBuilder stringBuilder = new StringBuilder(); + if (root == null) { + return stringBuilder.toString(); + } + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (curr == null) { + stringBuilder.append("# "); + } else { + stringBuilder.append(curr.val + " "); + queue.offer(curr.left); + queue.offer(curr.right); + } + } + } + return stringBuilder.toString(); + } + + // Decodes your encoded data to tree. + public TreeNode deserialize(String data) { + if (data == null || data.length() == 0) { + return null; + } + String[] nodes = data.split(" "); + TreeNode root = new TreeNode(Integer.valueOf(nodes[0])); + Queue queue = new LinkedList<>(); + queue.offer(root); + for (int i = 1; i < nodes.length; i++) { + TreeNode curr = queue.poll(); + if (!nodes[i].equals("#")) { + curr.left = new TreeNode(Integer.valueOf(nodes[i])); + queue.offer(curr.left); + } + if (!nodes[++i].equals("#")) { + curr.right = new TreeNode(Integer.valueOf(nodes[i])); + queue.offer(curr.right); + } + } + return root; + } + } + + public static class Solution4 { + private static final String NULL_SYMBOL = "X"; + private static final String DELIMITER = ","; + + // Encodes a tree to a single string. + public String serialize(TreeNode root) { + + // If we have a null symbol, encode it to NULL_SYMBOL + if (root == null) { + return NULL_SYMBOL + DELIMITER; + } + + String leftSubtree = serialize(root.left); + String rightSubtree = serialize(root.right); + + return root.val + DELIMITER + leftSubtree + rightSubtree; + } + + // Decodes your encoded data to tree. + public TreeNode deserialize(String data) { + + Queue nodesLeftToSerialize = new LinkedList<>(); + nodesLeftToSerialize.addAll(Arrays.asList(data.split(DELIMITER))); + return deserializeHelper(nodesLeftToSerialize); + } + + private TreeNode deserializeHelper(Queue nodesLeft) { + + // remove the node + String nodeLeftToSerialize = nodesLeft.poll(); + // base case + if (nodeLeftToSerialize.equals(NULL_SYMBOL)) { + return null; + } + TreeNode newNode = new TreeNode(Integer.valueOf(nodeLeftToSerialize)); + newNode.left = deserializeHelper(nodesLeft); + newNode.right = deserializeHelper(nodesLeft); + return newNode; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_45.java b/src/main/java/com/fishercoder/solutions/firstthousand/_45.java new file mode 100644 index 0000000000..9e9feac678 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_45.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _45 { + public static class Solution1 { + public int jump(int[] nums) { + int jumps = 0; + int lastJumpMax = 0; + int currentJumpMax = 0; + for (int i = 0; i < nums.length - 1; i++) { + currentJumpMax = Math.max(currentJumpMax, i + nums[i]); + if (i == lastJumpMax) { + jumps++; + lastJumpMax = currentJumpMax; + } + if (lastJumpMax >= nums.length) { + return jumps; + } + } + return jumps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_450.java b/src/main/java/com/fishercoder/solutions/firstthousand/_450.java new file mode 100644 index 0000000000..3a7f667849 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_450.java @@ -0,0 +1,154 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _450 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/65792/recursive-easy-to-understand-java-solution + * Steps: + * 1. Recursively find the node that has the same value as the key, while setting the left/right nodes equal to the returned subtree + * 2. Once the node is found, have to handle the below 4 cases + * a. node doesn't have left or right - return null + * b. node only has left subtree- return the left subtree + * c. node only has right subtree- return the right subtree + * d. node has both left and right - find the minimum value in the right subtree, set that value to the currently found node, then recursively delete the minimum value in the right subtree + */ + public TreeNode deleteNode(TreeNode root, int key) { + if (root == null) { + return root; + } + if (root.val > key) { + root.left = deleteNode(root.left, key); + } else if (root.val < key) { + root.right = deleteNode(root.right, key); + } else { + if (root.left == null) { + return root.right; + } else if (root.right == null) { + return root.left; + } + + TreeNode minNode = getMin(root.right); + root.val = minNode.val; + root.right = deleteNode(root.right, root.val); + } + return root; + } + + private TreeNode getMin(TreeNode node) { + while (node.left != null) { + node = node.left; + } + return node; + } + } + + public static class Solution2 { + /* + * My original, but brute force solution, time complexity: O(n) instead of O(h) + */ + public TreeNode deleteNode(TreeNode root, int key) { + List list = new ArrayList<>(); + dfs(root, key, list); + return formBst(list, 0, list.size() - 1); + } + + private TreeNode formBst(List list, int left, int right) { + if (left > right) { + return null; + } + int mid = left + (right - left) / 2; + TreeNode root = new TreeNode(list.get(mid)); + root.left = formBst(list, left, mid - 1); + root.right = formBst(list, mid + 1, right); + return root; + } + + private List dfs(TreeNode root, int key, List list) { + if (root == null) { + return list; + } + dfs(root.left, key, list); + if (root.val != key) { + list.add(root.val); + } + dfs(root.right, key, list); + return list; + } + } + + public static class Solution3 { + /* + * credit: https://leetcode.com/problems/delete-node-in-a-bst/solution/ + * + * Again, using a pen and paper to visualize helps a lot. + * Putting a BST into an inorder traversal array helps a lot to understand: + * + * The successor of a node is always: go the right once, and then go to the left as many times as possible, + * because the successor is the next smallest element that is larger than the current one: so going to the right one time + * makes sure that we are finding the larger one, and then keeping going to the left makes sure that we'll find the smallest one. + * + * The predecessor of a node is always: go the left once, and then go the right as many times as possible, + * because the predecessor is the largest element that is smaller than the current one: so going to the left once makes it smaller than the current node, + * then keeping going to the right makes sure that we are getting the largest element. + */ + public TreeNode deleteNode(TreeNode root, int key) { + if (root == null) { + return null; + } + if (root.val < key) { + // delete from the right subtree + root.right = deleteNode(root.right, key); + } else if (root.val > key) { + // delete from the left subtree + root.left = deleteNode(root.left, key); + } else { + // delete this current node, three cases: + if (root.left == null && root.right == null) { + // case 1: if this is a leaf + root = null; + } else if (root.right != null) { + // case 2: has a right child, regardless whether it has left children or not, + // this is because we want to traverse the tree only once, so we'll want to keep + // going down the tree + root.val = + findSuccessor( + root); // we find the value of the successor and assign it to + // current root.val + root.right = + deleteNode( + root.right, + root.val); // and then we delete this successor's value in the + // right subtree as it's been moved up + } else if (root.left != null) { + // case 3: this node is not a leaf and no right child, but has a left child + // That means that its successor is somewhere upper in the tree but we don't + // want to go back. + // Let's use the predecessor here which is somewhere lower in the left subtree. + root.val = findPredecessor(root); + root.left = deleteNode(root.left, root.val); + } + } + return root; + } + + private int findPredecessor(TreeNode root) { + root = root.left; + while (root.right != null) { + root = root.right; + } + return root.val; + } + + private int findSuccessor(TreeNode root) { + root = root.right; + while (root.left != null) { + root = root.left; + } + return root.val; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_451.java b/src/main/java/com/fishercoder/solutions/firstthousand/_451.java new file mode 100644 index 0000000000..03354eba8a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_451.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public class _451 { + + public static class Solution1 { + public String frequencySort(String s) { + Map map = new HashMap(); + for (char c : s.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + List> list = new ArrayList<>(map.entrySet()); + Collections.sort(list, (o1, o2) -> (o2.getValue() - o1.getValue())); + StringBuilder stringBuilder = new StringBuilder(); + for (Map.Entry entry : list) { + for (int i = 0; i < entry.getValue(); i++) { + stringBuilder.append(entry.getKey()); + } + } + return stringBuilder.toString(); + } + } + + public static class Solution2 { + public String frequencySort(String s) { + Map map = new HashMap<>(); + for (char c : s.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + TreeMap> reverseMap = + new TreeMap<>(Collections.reverseOrder()); + for (char c : map.keySet()) { + int freq = map.get(c); + if (!reverseMap.containsKey(freq)) { + reverseMap.put(freq, new ArrayList<>()); + } + reverseMap.get(freq).add(c); + } + StringBuilder sb = new StringBuilder(); + for (int freq : reverseMap.keySet()) { + List list = reverseMap.get(freq); + for (char c : list) { + for (int i = 0; i < freq; i++) { + sb.append(c); + } + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_452.java b/src/main/java/com/fishercoder/solutions/firstthousand/_452.java new file mode 100644 index 0000000000..95a60e5835 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_452.java @@ -0,0 +1,90 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _452 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/66579/java-greedy-soution/6 + */ + public int findMinArrowShots(int[][] points) { + if (points == null || points.length == 0) { + return 0; + } + // sort points based on their end point. + Arrays.sort(points, (p1, p2) -> Integer.compare(p1[1], p2[1])); + int currentEnd = points[0][1]; + int count = 1; + for (int[] p : points) { + // if the point starts after currentEnd, it means this balloons not been burst. + // Then we shot the balloon in its end point. Otherwise, means this balloon has been + // burst, then ignore it. + if (p[0] > currentEnd) { + count++; + currentEnd = p[1]; + } else { + continue; + } + } + return count; + } + } + + public static class Solution2 { + /* + * I'm glad to have come up with this solution on my own on 10/13/2021: + * we'll have to sort the balloons by its ending points, a counter case to this is below: + * {{0, 6}, {0, 9}, {7, 8}} + * if we sort by starting points, then it becomes: {0, 6}, {0, 9}, {7, 8}, this way, if we shoot 9, {0, 6} won't be burst + * however, if we sort by ending points, then it becomes: {0, 6}, {7, 8}, {0, 9}, + * then we shoot at 6, then at 8, this gives us the result of bursting all balloons. + */ + public int findMinArrowShots(int[][] points) { + Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1])); + int minArrows = 1; + long end = points[0][1]; + for (int i = 1; i < points.length; i++) { + if (points[i][0] <= end) { + continue; + } else { + minArrows++; + end = points[i][1]; + } + } + return minArrows; + } + } + + public static class Solution3 { + /* + * Another approach of mine: completely original. + * 1. Sort the points by start first, if tie, sort by end, both ascendingly. + * 2. While checking, we'll keep updating the ending to be the smaller one so that we don't possibly miss to burst a balloon. See test case 4 for this class. + */ + + public int findMinArrowShots(int[][] points) { + int arrowShots = 0; + Arrays.sort( + points, + (a, b) -> + a[0] != b[0] + ? Integer.compare(a[0], b[0]) + : Integer.compare(a[1], b[1])); + for (int i = 0; i < points.length; ) { + int end = points[i][1]; + int j = i + 1; + for (; j < points.length; j++) { + if (points[j][0] <= end) { + end = Math.min(end, points[j][1]); + } else { + break; + } + } + i = j; + arrowShots++; + } + return arrowShots; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_453.java b/src/main/java/com/fishercoder/solutions/firstthousand/_453.java new file mode 100644 index 0000000000..2c73cc1819 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_453.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _453 { + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/66557/java-o-n-solution-short + * i.e. Add 1 to n-1 elements basically equals to subtracting 1 from one element. So the easiest way + * to make all elements in this array equal is to make all of them equal to the minimum element. + */ + public int minMoves(int[] nums) { + if (nums.length == 0) { + return 0; + } + int min = nums[0]; + for (int n : nums) { + min = Math.min(min, n); + } + int res = 0; + for (int n : nums) { + res += n - min; + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_454.java b/src/main/java/com/fishercoder/solutions/firstthousand/_454.java new file mode 100644 index 0000000000..7366622e55 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_454.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _454 { + public static class Solution1 { + public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + Map map = new HashMap(); + int result = 0; + int len = nums1.length; + for (int i = 0; i < len; i++) { + for (int j = 0; j < len; j++) { + int sum = nums1[i] + nums2[j]; + map.put(sum, map.getOrDefault(sum, 0) + 1); + } + } + + for (int i = 0; i < len; i++) { + for (int j = 0; j < len; j++) { + int sum = -(nums3[i] + nums4[j]); + if (map.containsKey(sum)) { + result += map.get(sum); + } + } + } + + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_455.java b/src/main/java/com/fishercoder/solutions/firstthousand/_455.java new file mode 100644 index 0000000000..3cb560a65c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_455.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _455 { + + public static class Solution1 { + public int findContentChildren(int[] g, int[] s) { + Arrays.sort(g); + Arrays.sort(s); + + int result = 0; + for (int i = 0, j = 0; i < g.length && j < s.length; ) { + if (s[j] >= g[i]) { + result++; + i++; + } + j++; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_456.java b/src/main/java/com/fishercoder/solutions/firstthousand/_456.java new file mode 100644 index 0000000000..8c1e42dae7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_456.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _456 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/67881/single-pass-c-o-n-space-and-time-solution-8-lines-with-detailed-explanation + * It scans only once, this is the power of using correct data structure. + * It goes from the right to the left. + * It keeps pushing elements into the stack, + * but it also keeps poping elements out of the stack as long as the current element is bigger than this number. + */ + public boolean find132pattern(int[] nums) { + Deque stack = new LinkedList<>(); + + int s3 = Integer.MIN_VALUE; + for (int i = nums.length - 1; i >= 0; i--) { + if (nums[i] < s3) { + return true; + } else { + while (!stack.isEmpty() && nums[i] > stack.peek()) { + s3 = Math.max(s3, stack.pop()); + } + } + stack.push(nums[i]); + } + + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_457.java b/src/main/java/com/fishercoder/solutions/firstthousand/_457.java new file mode 100644 index 0000000000..484c82bc61 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_457.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.firstthousand; + +public class _457 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/66894/java-slow-fast-pointer-solution + */ + public boolean circularArrayLoop(int[] nums) { + int n = nums.length; + for (int i = 0; i < n; i++) { + if (nums[i] == 0) { + continue; + } + // slow/fast pointer + int j = i; + int k = getIndex(i, nums); + while (nums[k] * nums[i] > 0 && nums[getIndex(k, nums)] * nums[i] > 0) { + if (j == k) { + // check for loop with only one element + if (j == getIndex(j, nums)) { + break; + } + return true; + } + j = getIndex(j, nums); + k = getIndex(getIndex(k, nums), nums); + } + // loop not found, set all element along the way to 0 + j = i; + int val = nums[i]; + while (nums[j] * val > 0) { + int next = getIndex(j, nums); + nums[j] = 0; + j = next; + } + } + return false; + } + + public int getIndex(int i, int[] nums) { + int n = nums.length; + return i + nums[i] >= 0 ? (i + nums[i]) % n : n + ((i + nums[i]) % n); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_458.java b/src/main/java/com/fishercoder/solutions/firstthousand/_458.java new file mode 100644 index 0000000000..c7ef33dc88 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_458.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.firstthousand; + +public class _458 { + + public static class Solution1 { + public int poorPigs(int buckets, int minutesToDie, int minutesToTest) { + if (buckets-- == 1) { + return 0; + } + int base = minutesToTest / minutesToDie + 1; + int count = 0; + while (buckets > 0) { + buckets /= base; + count++; + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_459.java b/src/main/java/com/fishercoder/solutions/firstthousand/_459.java new file mode 100644 index 0000000000..750e0feb66 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_459.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.firstthousand; + +public class _459 { + + public static class Solution1 { + public boolean repeatedSubstringPattern(String s) { + int len = s.length(); + for (int i = len / 2; i >= 1; i--) { + String pattern = s.substring(0, i); + if (len % i == 0) { + String formedString = formStringByCopying(pattern, len / i); + if (formedString.equals(s)) { + return true; + } + } + } + return false; + } + + private String formStringByCopying(String pattern, int k) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < k; i++) { + sb.append(pattern); + } + return sb.toString(); + } + } + + public static class Solution2 { + /* + * credit: https://discuss.leetcode.com/topic/68089/repeated-substring-pattern-simple-java-solution-using-kmp + */ + public static boolean repeatedSubstringPattern(String str) { + // build the KMP pattern. + int n = str.length(); + int cur = 0; + int j = 1; + int[] pattern = new int[n]; + pattern[0] = 0; + + while (j < n) { + if (str.charAt(cur) == str.charAt(j)) { + pattern[j++] = ++cur; + } else { + if (cur == 0) { + pattern[j++] = 0; + } else { + cur = pattern[cur - 1]; // start from beginning of current matching pattern. + } + } + } + + return (pattern[n - 1] > 0 && n % (n - pattern[n - 1]) == 0); + } + } + + public static class Solution3 { + public static boolean repeatedSubstringPattern(String str) { + String s = str + str; + return s.substring(1, s.length() - 1).contains(str); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_46.java b/src/main/java/com/fishercoder/solutions/firstthousand/_46.java new file mode 100644 index 0000000000..f2c22bb05f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_46.java @@ -0,0 +1,99 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _46 { + public static class Solution1 { + public List> permute(int[] nums) { + List> result = new ArrayList(); + result.add(new ArrayList<>()); + return recurse(nums, 0, result); + } + + private List> recurse(int[] nums, int index, List> result) { + if (index == nums.length) { + return result; + } + List> newResult = new ArrayList<>(); + for (List list : result) { + for (int i = 0; i <= list.size(); i++) { + List newList = new ArrayList<>(list); + newList.add(i, nums[index]); + newResult.add(newList); + } + } + return recurse(nums, index + 1, newResult); + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/11/2021, although a little verbose and super as efficient as the above one. + * Basically, insert the next unused number into all possible positions in the currently formed list, + * as soon as the size of this list equals nums.length, add this new permutation into the result; + * I'll have to use a HashSet to filter out duplicates. + */ + public List> permute(int[] nums) { + Set> ans = new HashSet<>(); + boolean[] used = new boolean[nums.length]; + backtracking(new ArrayList<>(), nums, ans, used); + List> result = new ArrayList<>(); + for (List list : ans) { + result.add(list); + } + return result; + } + + private void backtracking( + List list, int[] nums, Set> ans, boolean[] used) { + if (list.size() == nums.length) { + ans.add(new ArrayList<>(list)); + return; + } + for (int i = 0; i <= list.size(); i++) { + for (int j = 0; j < nums.length; j++) { + if (!used[j]) { + used[j] = true; + list.add(i, nums[j]); + backtracking(list, nums, ans, used); + used[j] = false; + list.remove(i); + } + } + } + } + } + + public static class Solution3 { + /* + * A more efficient version of backtracking. + */ + public List> permute(int[] nums) { + List> ans = new ArrayList<>(); + boolean[] used = new boolean[nums.length]; + return backtracking(ans, used, new ArrayList<>(), nums); + } + + private List> backtracking( + List> ans, boolean[] used, List list, int[] nums) { + if (list.size() == nums.length) { + ans.add(new ArrayList<>(list)); + return ans; + } + for (int i = 0; i < nums.length; i++) { + if (used[i]) { + continue; + } + used[i] = true; + list.add(nums[i]); + backtracking(ans, used, list, nums); + used[i] = false; + list.remove(list.size() - 1); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_460.java b/src/main/java/com/fishercoder/solutions/firstthousand/_460.java new file mode 100644 index 0000000000..bf0331925d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_460.java @@ -0,0 +1,101 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.LinkedHashSet; + +public class _460 { + public static class Solution1 { + /* + * Wikipedia: The simplest method to employ an LFU algorithm is to assign a counter to every + * block that is loaded into the cache. Each time a reference is made to that block the counter + * is increased by one. When the cache reaches capacity and has a new block waiting to be + * inserted the system will search for the block with the lowest counter and remove it from the + * cache. + * Policy to handle frequency ties: based on timestamp, the entries that get set into cache earlier will be evicted first. + */ + + public static class LFUCache { + /* + * credit: https://discuss.leetcode.com/topic/69737/java-o-1-very-easy-solution-using-3-hashmaps-and-linkedhashset/2 + */ + + HashMap keyToValue; + /* + * key is the key, value is the value + */ + HashMap keyToCount; + /* + * key is the key, value if the count of the key/value pair + */ + HashMap> countToLRUKeys; + /* + * key is count, value is a set of keys that have the same key, but keeps insertion order + */ + int cap; + int minimumCount; + + public LFUCache(int capacity) { + this.minimumCount = -1; + this.cap = capacity; + this.keyToValue = new HashMap<>(); + this.keyToCount = new HashMap<>(); + this.countToLRUKeys = new HashMap<>(); + this.countToLRUKeys.put(1, new LinkedHashSet<>()); + } + + public int get(int key) { + if (!keyToValue.containsKey(key)) { + return -1; + } + int count = keyToCount.get(key); + keyToCount.put(key, count + 1); + countToLRUKeys.get(count).remove(key); + + if (count == minimumCount && countToLRUKeys.get(count).size() == 0) { + /*This means this key's count equals to current minimumCount + * AND + * this count doesn't have any entries in the cache. + * So, we'll increment minimumCount by 1 to get the next LFU cache entry + * when we need to evict.*/ + minimumCount++; + } + + if (!countToLRUKeys.containsKey(count + 1)) { + countToLRUKeys.put(count + 1, new LinkedHashSet<>()); + } + countToLRUKeys.get(count + 1).add(key); + + return keyToValue.get(key); + } + + public void put(int key, int value) { + if (cap <= 0) { + return; + } + + if (keyToValue.containsKey(key)) { + /*If the key is already in the cache, we can simply overwrite this entry; + * then call get(key) which will do the update work.*/ + keyToValue.put(key, value); + get(key); + return; + } + + /*If the key is not in the cache, we'll check the size first, evict the LFU entry first, + * then insert this one into cache.*/ + if (keyToValue.size() >= cap) { + int evit = countToLRUKeys.get(minimumCount).iterator().next(); + countToLRUKeys.get(minimumCount).remove(evit); + keyToValue.remove(evit); + } + keyToValue.put(key, value); + keyToCount.put(key, 1); + countToLRUKeys + .get(1) + .add( + key); /*Because we put this key/value into cache for the first time, so its count is 1*/ + minimumCount = 1; /*For the same above reason, minimumCount is of course 1*/ + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_461.java b/src/main/java/com/fishercoder/solutions/firstthousand/_461.java new file mode 100644 index 0000000000..3196b3c13c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_461.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +public class _461 { + public static class Solution1 { + public int hammingDistance(int x, int y) { + int n = x ^ y; + int count = 0; + while (n != 0) { + count++; + n &= (n - 1); + } + return count; + } + } + + public static class Solution2 { + public int hammingDistance(int x, int y) { + int ans = 0; + for (int i = 0; i < 32; i++) { + ans += (x & 1) ^ (y & 1); + x >>= 1; + y >>= 1; + } + return ans; + } + } + + public static class Solution3 { + public int hammingDistance(int x, int y) { + return Integer.bitCount(x ^ y); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_462.java b/src/main/java/com/fishercoder/solutions/firstthousand/_462.java new file mode 100644 index 0000000000..04bb806629 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_462.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _462 { + + public static class Solution1 { + public int minMoves2(int[] nums) { + /*sort this array, find the median of this array as the pivot*/ + Arrays.sort(nums); + int result = 0; + int result1 = 0; + if (nums.length % 2 != 0) { + int median = nums[nums.length / 2]; + for (int n : nums) { + result += Math.abs(n - median); + } + return result; + } else { + int median1 = nums[nums.length / 2]; + for (int n : nums) { + result1 += Math.abs(n - median1); + } + int result2 = 0; + int median2 = nums[nums.length / 2 - 1]; + for (int n : nums) { + result2 += Math.abs(n - median2); + } + result1 = Math.min(result1, result2); + return result1; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_463.java b/src/main/java/com/fishercoder/solutions/firstthousand/_463.java new file mode 100644 index 0000000000..76fc04d526 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_463.java @@ -0,0 +1,79 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _463 { + + public static class Solution1 { + /* + * Inspired by this post: https://discuss.leetcode.com/topic/68983/java-9-line-solution-add-4-for-each-land-and-remove-2-for-each-internal-edge + * 1. we increment the count by 4 whenever we encounter an island + * 2. also, we check in two directions: island's left and island's top, we only check these two directions, + * see if this island has any island neighbors, if so, we'll deduct two from it. + */ + public int islandPerimeter(int[][] grid) { + int count = 0; + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == 1) { + count += 4; + if (i > 0 && grid[i - 1][j] == 1) { + count -= 2; + } + if (j > 0 && grid[i][j - 1] == 1) { + count -= 2; + } + } + } + } + return count; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/4/2021: + * Count the number of island neighbors that each island has, then reduce this number from four and add it to the result. + */ + public int islandPerimeter(int[][] grid) { + int perimeter = 0; + int m = grid.length; + int n = grid[0].length; + boolean[][] visited = new boolean[m][n]; + int[] directions = new int[] {0, 1, 0, -1, 0}; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + Queue queue = new LinkedList<>(); + queue.offer(new int[] {i, j}); + while (!queue.isEmpty()) { + int[] curr = queue.poll(); + if (!visited[curr[0]][curr[1]]) { + visited[curr[0]][curr[1]] = true; + int neighborCount = 0; + for (int k = 0; k < directions.length - 1; k++) { + int newX = curr[0] + directions[k]; + int newY = curr[1] + directions[k + 1]; + if (newX >= 0 + && newX < m + && newY >= 0 + && newY < n + && grid[newX][newY] == 1) { + neighborCount++; + if (!visited[newX][newY]) { + queue.offer(new int[] {newX, newY}); + } + } + } + perimeter += 4 - neighborCount; + } + } + return perimeter; + } + } + } + return perimeter; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_464.java b/src/main/java/com/fishercoder/solutions/firstthousand/_464.java new file mode 100644 index 0000000000..76fcf632bf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_464.java @@ -0,0 +1,65 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _464 { + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/68896/java-solution-using-hashmap-with-detailed-explanation + */ + + Map map; + boolean[] used; + + public boolean canIWin(int maxChoosableInteger, int desiredTotal) { + int sum = (1 + maxChoosableInteger) * maxChoosableInteger / 2; + if (sum < desiredTotal) { + return false; + } + if (desiredTotal <= 0) { + return true; + } + + map = new HashMap(); + used = new boolean[maxChoosableInteger + 1]; + return helper(desiredTotal); + } + + public boolean helper(int desiredTotal) { + if (desiredTotal <= 0) { + return false; + } + int key = format(used); + if (!map.containsKey(key)) { + // try every unchosen number as next step + for (int i = 1; i < used.length; i++) { + if (!used[i]) { + used[i] = true; + // check whether this lead to a win (i.e. the other player lose) + if (!helper(desiredTotal - i)) { + map.put(key, true); + used[i] = false; + return true; + } + used[i] = false; + } + } + map.put(key, false); + } + return map.get(key); + } + + // transfer boolean[] to an Integer + public int format(boolean[] used) { + int num = 0; + for (boolean b : used) { + num <<= 1; + if (b) { + num |= 1; + } + } + return num; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_465.java b/src/main/java/com/fishercoder/solutions/firstthousand/_465.java new file mode 100644 index 0000000000..294e51c1c3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_465.java @@ -0,0 +1,71 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Stack; + +public class _465 { + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/68948/easy-java-solution-with-explanation + */ + public int minTransfers(int[][] transactions) { + if (transactions == null || transactions.length == 0) { + return 0; + } + Map acc = new HashMap<>(); + for (int i = 0; i < transactions.length; i++) { + int id1 = transactions[i][0]; + int id2 = transactions[i][1]; + int m = transactions[i][2]; + acc.put(id1, acc.getOrDefault(id1, 0) - m); + acc.put(id2, acc.getOrDefault(id2, 0) + m); + } + List negs = new ArrayList<>(); + List poss = new ArrayList<>(); + for (Integer key : acc.keySet()) { + int m = acc.get(key); + if (m == 0) { + continue; + } + if (m < 0) { + negs.add(-m); + } else { + poss.add(m); + } + } + int ans = Integer.MAX_VALUE; + Stack stNeg = new Stack<>(); + Stack stPos = new Stack<>(); + for (int i = 0; i < 1000; i++) { + for (Integer num : negs) { + stNeg.push(num); + } + for (Integer num : poss) { + stPos.push(num); + } + int cur = 0; + while (!stNeg.isEmpty()) { + int n = stNeg.pop(); + int p = stPos.pop(); + cur++; + if (n == p) { + continue; + } + if (n > p) { + stNeg.push(n - p); + } else { + stPos.push(p - n); + } + } + ans = Math.min(ans, cur); + Collections.shuffle(negs); + Collections.shuffle(poss); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_466.java b/src/main/java/com/fishercoder/solutions/firstthousand/_466.java new file mode 100644 index 0000000000..506f50718f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_466.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +public class _466 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/70707/ugly-java-brute-force-solution-but-accepted-1088ms + */ + public int getMaxRepetitions(String s1, int n1, String s2, int n2) { + char[] s1chars = s1.toCharArray(); + char[] s2chars = s2.toCharArray(); + int i = 0; + int j = 0; + int count1 = 0; + int count2 = 0; + while (count1 < n1) { + if (s1chars[i] == s2chars[j]) { + j++; + if (j == s2.length()) { + j = 0; + count2++; + } + } + i++; + if (i == s1.length()) { + i = 0; + count1++; + } + } + return count2 / n2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_467.java b/src/main/java/com/fishercoder/solutions/firstthousand/_467.java new file mode 100644 index 0000000000..70afc4ee1b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_467.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +public class _467 { + public static class Solution1 { + /* + * A naive solution would definitely result in TLE. + * Since the pattern keeps repeating, DP is the way to go. + * Because the input consists merely of lowercase English letters, we could maintain an array of size 26, + * keep updating this array, counting the substrings that end with this letter, keep updating it with the largest one possible. + *

+ * Inspired by this: https://discuss.leetcode.com/topic/70658/concise-java-solution-using-dp + */ + public int findSubstringInWraproundString(String p) { + if (p == null || p.isEmpty()) { + return 0; + } + if (p.length() < 2) { + return p.length(); + } + int count = 0; + int[] dp = new int[26]; + dp[p.charAt(0) - 'a'] = 1; + int len = 1; + for (int i = 1; i < p.length(); i++) { + if (p.charAt(i) - 1 == p.charAt(i - 1) + || (p.charAt(i) == 'a' && p.charAt(i - 1) == 'z')) { + len++; + } else { + len = 1; + } + dp[p.charAt(i) - 'a'] = Math.max(len, dp[p.charAt(i) - 'a']); + } + + for (int i : dp) { + count += i; + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_468.java b/src/main/java/com/fishercoder/solutions/firstthousand/_468.java new file mode 100644 index 0000000000..7b50054583 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_468.java @@ -0,0 +1,111 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _468 { + + public static class Solution1 { + + static final String NEITHER = "Neither"; + + public String validIPAddress(String IP) { + if (IP.contains(".")) { + return isValidIPv4(IP); + } else if (IP.contains(":")) { + return isValidIPv6(IP); + } else { + return NEITHER; + } + } + + private String isValidIPv6(String IP) { + if (getDelimiterCount(IP, ':') != 7) { + return NEITHER; + } + String[] bytes = IP.split("\\:"); + if (bytes.length != 8) { + return NEITHER; + } + for (int i = 0; i < 8; i++) { + if (hasInvalidIPV6Char(bytes[i])) { + return NEITHER; + } + try { + if (bytes[i].length() > 4) { + return NEITHER; + } + int intNum = Integer.parseInt(bytes[i], 16); + if (intNum < 0) { + return NEITHER; + } + if (i == 0 && intNum != 0 && bytes[i].charAt(0) == '0') { + return NEITHER; + } + } catch (Exception e) { + return NEITHER; + } + } + return "IPv6"; + } + + private String isValidIPv4(String IP) { + if (getDelimiterCount(IP, '.') != 3) { + return NEITHER; + } + String[] bytes = IP.split("\\."); + if (bytes.length != 4) { + return NEITHER; + } + for (String num : bytes) { + try { + int intNum = Integer.parseInt(num); + if (intNum > 255 || intNum < 0) { + return NEITHER; + } + if (intNum != 0) { + for (int i = 0; i < num.length(); i++) { + if (num.charAt(i) == '0') { + return NEITHER; + } else { + break; + } + } + } else if (intNum == 0) { + if (num.length() != 1) { + return NEITHER; + } + } + } catch (Exception e) { + return NEITHER; + } + } + return "IPv4"; + } + + private boolean hasInvalidIPV6Char(String str) { + Set set = + new HashSet<>( + Arrays.asList( + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', + 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F')); + for (char c : str.toCharArray()) { + if (!set.contains(c)) { + return true; + } + } + return false; + } + + private int getDelimiterCount(String ip, char delimiter) { + int count = 0; + for (char c : ip.toCharArray()) { + if (c == delimiter) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_469.java b/src/main/java/com/fishercoder/solutions/firstthousand/_469.java new file mode 100644 index 0000000000..9a6ee7736a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_469.java @@ -0,0 +1,64 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.List; + +public class _469 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/70706/beyond-my-knowledge-java-solution-with-in-line-explanation + */ + public boolean isConvex(List> points) { + // For each set of three adjacent points A, B, C, find the cross product AB · BC. If the + // sign of + // all the cross products is the same, the angles are all positive or negative + // (depending on the + // order in which we visit them) so the polygon is convex. + boolean gotNegative = false; + boolean gotPositive = false; + int numPoints = points.size(); + int B; + int C; + for (int A = 0; A < numPoints; A++) { + // Trick to calc the last 3 points: n - 1, 0 and 1. + B = (A + 1) % numPoints; + C = (B + 1) % numPoints; + + int crossProduct = + crossProductLength( + points.get(A).get(0), points.get(A).get(1), + points.get(B).get(0), points.get(B).get(1), + points.get(C).get(0), points.get(C).get(1)); + if (crossProduct < 0) { + gotNegative = true; + } else if (crossProduct > 0) { + gotPositive = true; + } + if (gotNegative && gotPositive) { + return false; + } + } + + // If we got this far, the polygon is convex. + return true; + } + + // Return the cross product AB x BC. + // The cross product is a vector perpendicular to AB and BC having length |AB| * |BC| * + // Sin(theta) and + // with direction given by the right-hand rule. For two vectors in the X-Y plane, the result + // is a + // vector with X and Y components 0 so the Z component gives the vector's length and + // direction. + private int crossProductLength(int Ax, int Ay, int Bx, int By, int Cx, int Cy) { + // Get the vectors' coordinates. + int BAx = Ax - Bx; + int BAy = Ay - By; + int BCx = Cx - Bx; + int BCy = Cy - By; + + // Calculate the Z coordinate of the cross product. + return (BAx * BCy - BAy * BCx); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_47.java b/src/main/java/com/fishercoder/solutions/firstthousand/_47.java new file mode 100644 index 0000000000..dc337a1a8e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_47.java @@ -0,0 +1,82 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _47 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/31445/really-easy-java-solution-much-easier-than-the-solutions-with-very-high-vote + */ + public List> permuteUnique(int[] nums) { + List> result = new ArrayList(); + if (nums == null || nums.length == 0) { + return result; + } + boolean[] used = new boolean[nums.length]; + Arrays.sort(nums); // this sorting is critical for the correctness of this backtracking + // algorithm as we compare the two adjacent neighbors to filter out + // possible duplicate permutations + backtracking(nums, used, new ArrayList(), result); + return result; + } + + private void backtracking( + int[] nums, boolean[] used, List list, List> result) { + if (list.size() == nums.length) { + result.add(new ArrayList(list)); + return; + } + for (int i = 0; i < nums.length; i++) { + if (used[i]) { + continue; + } + if (i > 0 && nums[i - 1] == nums[i] && used[i - 1]) { + /* + * For this line, both !used[i-1] and used[i-1] will AC. + * It is because the first one makes sure when duplicates are selected, the order is ascending (index from small to large). + * However, the second one means the descending order. + * But without this used[i - 1] or !used[i - 1] will not yield a correct result as the program will not yield a correct result. + */ + continue; + } + used[i] = true; + list.add(nums[i]); + backtracking(nums, used, list, result); + used[i] = false; + list.remove(list.size() - 1); + } + } + } + + public static class Solution2 { + public List> permuteUnique(int[] nums) { + Set> set = new HashSet<>(); + set.add(new ArrayList<>()); + set = recurse(nums, set, 0); + List> res = new ArrayList<>(); + for (List list : set) { + res.add(list); + } + return res; + } + + private Set> recurse(int[] nums, Set> set, int pos) { + if (pos == nums.length) { + return set; + } + Set> newSet = new HashSet<>(); + for (List list : set) { + for (int i = 0; i <= list.size(); i++) { + List newList = new ArrayList<>(list); + newList.add(i, nums[pos]); + newSet.add(newList); + } + } + return recurse(nums, newSet, pos + 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_471.java b/src/main/java/com/fishercoder/solutions/firstthousand/_471.java new file mode 100644 index 0000000000..d38dc2bfac --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_471.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.firstthousand; + +public class _471 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/71963/accepted-solution-in-java + */ + public String encode(String s) { + String[][] dp = new String[s.length()][s.length()]; + + for (int l = 0; l < s.length(); l++) { + for (int i = 0; i < s.length() - l; i++) { + int j = i + l; + String substr = s.substring(i, j + 1); + // Checking if string length < 5. In that case, we know that encoding will not + // help. + if (j - i < 4) { + dp[i][j] = substr; + } else { + dp[i][j] = substr; + // Loop for trying all results that we get after dividing the strings into 2 + // and combine the results of 2 substrings + for (int k = i; k < j; k++) { + if ((dp[i][k] + dp[k + 1][j]).length() < dp[i][j].length()) { + dp[i][j] = dp[i][k] + dp[k + 1][j]; + } + } + + // Loop for checking if string can itself found some pattern in it which + // could be repeated. + for (int k = 0; k < substr.length(); k++) { + String repeatStr = substr.substring(0, k + 1); + if (repeatStr != null + && substr.length() % repeatStr.length() == 0 + && substr.replaceAll(repeatStr, "").length() == 0) { + String ss = + substr.length() / repeatStr.length() + + "[" + + dp[i][i + k] + + "]"; + if (ss.length() < dp[i][j].length()) { + dp[i][j] = ss; + } + } + } + } + } + } + return dp[0][s.length() - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_472.java b/src/main/java/com/fishercoder/solutions/firstthousand/_472.java new file mode 100644 index 0000000000..fa1f3f8421 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_472.java @@ -0,0 +1,156 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _472 { + + public static class Solution1 { + private TrieNode root; + private int maxWordLen; + + public List findAllConcatenatedWordsInADict(String[] words) { + ResultType result = buildTrie(words); + root = result.root; + maxWordLen = result.maxWordLen; + + List validConcatenatedWords = new ArrayList(); + for (String word : words) { + if (word == null || word.length() == 0) { + continue; + } + remove( + word, + root); /* every word is comprised of every word itself, thus this word itself needs to be removed first for checking it*/ + int n = word.length(); + boolean[] dp = new boolean[n + 1]; + dp[0] = true; + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= i && j <= maxWordLen; j++) { + if (!dp[i - j]) { + continue; + } + + String subWord = word.substring(i - j, i); + if (contains(subWord, root)) { + dp[i] = true; + break; + } + } + } + + if (dp[n]) { + validConcatenatedWords.add(word); + } + undoRemove(word, root); + } + return validConcatenatedWords; + } + + public ResultType buildTrie(String[] words) { + ResultType result = new ResultType(); + + TrieNode root = new TrieNode(); + int maxWordLen = 0; + + for (String word : words) { + maxWordLen = Math.max(maxWordLen, word.length()); + char[] chars = word.toCharArray(); + TrieNode node = root; + for (int i = 0; i < chars.length; i++) { + char c = chars[i]; + if (node.children[c - 'a'] == null) { + node.children[c - 'a'] = new TrieNode(); + } + node = node.children[c - 'a']; + } + node.isWord = true; + } + + result.root = root; + result.maxWordLen = maxWordLen; + return result; + } + + public class ResultType { + int maxWordLen; + TrieNode root; + } + + // Returns true if the word is in the trie. + public boolean contains(String word, TrieNode root) { + TrieNode node = root; + for (int i = 0; i < word.length(); i++) { + if (node.children[word.charAt(i) - 'a'] == null) { + return false; + } + node = node.children[word.charAt(i) - 'a']; + } + return node.isWord; + } + + // mark that word on + public void undoRemove(String word, TrieNode root) { + TrieNode node = root; + for (int i = 0; i < word.length(); i++) { + node = node.children[word.charAt(i) - 'a']; + } + node.isWord = true; + } + + // mark that word off, we are not really deleting that word + public void remove(String word, TrieNode root) { + TrieNode node = root; + for (int i = 0; i < word.length(); i++) { + node = node.children[word.charAt(i) - 'a']; + } + node.isWord = false; + } + + class TrieNode { + boolean isWord; + TrieNode[] children = new TrieNode[26]; + + public TrieNode() {} + } + } + + public static class Solution2 { + public List findAllConcatenatedWordsInADict(String[] words) { + List result = new ArrayList<>(); + Set preWords = new HashSet<>(); + /*Words could only be formed by other words that are shorter than itself, so we sort them based on their lengths first.*/ + Arrays.sort(words, (s1, s2) -> s1.length() - s2.length()); + + for (int i = 0; i < words.length; i++) { + if (canForm(words[i], preWords)) { + result.add(words[i]); + } + preWords.add(words[i]); + } + + return result; + } + + boolean canForm(String word, Set dict) { + if (dict.isEmpty()) { + return false; + } + boolean[] dp = new boolean[word.length() + 1]; + dp[0] = true; + for (int i = 1; i <= word.length(); i++) { + for (int j = 0; j < i; j++) { + if (dp[j] && dict.contains(word.substring(j, i))) { + dp[i] = true; + break; + } + } + } + return dp[word.length()]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_473.java b/src/main/java/com/fishercoder/solutions/firstthousand/_473.java new file mode 100644 index 0000000000..7f39f75407 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_473.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _473 { + + public static class Solution1 { + /* + * Partially inspired by: https://discuss.leetcode.com/topic/72107/java-dfs-solution-with-explanation/2 + * One hidden requirement: you'll have to use up all of the given matchsticks, nothing could be left behind. + */ + public boolean makesquare(int[] nums) { + if (nums == null || nums.length < 4) { + return false; + } + Arrays.sort(nums); + reverse(nums); + int sum = 0; + for (int i : nums) { + sum += i; + } + if (sum % 4 != 0) { + return false; + } + + return dfs(nums, new int[4], 0, sum / 4); + } + + private boolean dfs(int[] nums, int[] sums, int index, int target) { + if (index == nums.length) { + if (sums[0] == target && sums[1] == target && sums[2] == target) { + return true; + } + return false; + } + for (int i = 0; i < 4; i++) { + if (sums[i] + nums[index] > target) { + continue; + } + sums[i] += nums[index]; + if (dfs(nums, sums, index + 1, target)) { + return true; + } + sums[i] -= nums[index]; + } + return false; + } + + private void reverse(int[] nums) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int tmp = nums[left]; + nums[left] = nums[right]; + nums[right] = tmp; + left++; + right--; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_474.java b/src/main/java/com/fishercoder/solutions/firstthousand/_474.java new file mode 100644 index 0000000000..2020f65af1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_474.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +public class _474 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/ones-and-zeroes/discuss/95811/Java-Iterative-DP-Solution-O(mn)-Space and + * https://leetcode.com/problems/ones-and-zeroes/discuss/95811/Java-Iterative-DP-Solution-O(mn)-Space/100352 + *

+ * The problem can be interpreted as: + * What's the max number of str can we pick from strs with limitation of m "0"s and n "1"s. + * + * Thus we can define dp[i][j] as it stands for max number of str can we pick from strs with limitation of i "0"s and j "1"s. + * + * For each str, assume it has a "0"s and b "1"s, we update the dp array iteratively + * and set dp[i][j] = Math.max(dp[i][j], dp[i - a][j - b] + 1). + * So at the end, dp[m][n] is the answer. + */ + public int findMaxForm(String[] strs, int m, int n) { + int[][] dp = new int[m + 1][n + 1]; + for (String str : strs) { + int[] count = count(str); + for (int i = m; i >= count[0]; i--) { + for (int j = n; j >= count[1]; j--) { + dp[i][j] = Math.max(dp[i][j], dp[i - count[0]][j - count[1]] + 1); + } + } + } + return dp[m][n]; + } + + private int[] count(String str) { + int[] res = new int[2]; + for (int i = 0; i < str.length(); i++) { + res[str.charAt(i) - '0']++; + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_475.java b/src/main/java/com/fishercoder/solutions/firstthousand/_475.java new file mode 100644 index 0000000000..78b751c27c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_475.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _475 { + + public static class Solution1 { + // credit: + // https://discuss.leetcode.com/topic/71460/short-and-clean-java-binary-search-solution + public int findRadius(int[] houses, int[] heaters) { + Arrays.sort(heaters); + int radius = Integer.MIN_VALUE; + for (int house : houses) { + int index = Arrays.binarySearch(heaters, house); + if (index < 0) { + index = ~index; + } + int distance1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE; + int distance2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE; + + radius = Math.max(radius, Math.min(distance1, distance2)); + } + return radius; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_476.java b/src/main/java/com/fishercoder/solutions/firstthousand/_476.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_476.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_476.java index 571b7e6512..22b1ccc149 100644 --- a/src/main/java/com/fishercoder/solutions/_476.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_476.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _476 { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_477.java b/src/main/java/com/fishercoder/solutions/firstthousand/_477.java new file mode 100644 index 0000000000..8245642a58 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_477.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +public class _477 { + public static class Solution1 { + public int totalHammingDistance(int[] nums) { + int r = 0; + for (int i = 0; i < 32; i++) { + int one = 0; + int zero = 0; + int bit = 1 << i; + for (int n : nums) { + if ((n & bit) != 0) { + one++; + } else { + zero++; + } + } + r += one * zero; + } + return r; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_478.java b/src/main/java/com/fishercoder/solutions/firstthousand/_478.java new file mode 100644 index 0000000000..29e8def87d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_478.java @@ -0,0 +1,64 @@ +package com.fishercoder.solutions.firstthousand; + +public class _478 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/154037/Polar-Coordinates-10-lines + * and + * https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/155650/Explanation-with-Graphs-why-using-Math.sqrt() + */ + double radius; + double xCenter; + double yCenter; + + public Solution1(double radius, double xCenter, double yCenter) { + this.radius = radius; + this.xCenter = xCenter; + this.yCenter = yCenter; + } + + public double[] randPoint() { + double len = Math.sqrt(Math.random()) * radius; + double degree = Math.random() * 2 * Math.PI; + double x = xCenter + len * Math.cos(degree); + double y = yCenter + len * Math.sin(degree); + return new double[] {x, y}; + } + } + + public static class Solution2 { + /* + * How to know one point is within a circle: + * https://www.geeksforgeeks.org/find-if-a-point-lies-inside-or-on-circle/ + */ + double top; + double bottom; + double left; + double right; + double rad; + double xCenter; + double yCenter; + + public Solution2(double radius, double xCenter, double yCenter) { + this.rad = radius; + this.top = yCenter + radius; + this.bottom = yCenter - radius; + this.left = xCenter - radius; + this.right = xCenter + radius; + this.xCenter = xCenter; + this.yCenter = yCenter; + } + + public double[] randPoint() { + double[] result = new double[2]; + result[0] = (Math.random() * (right - left)) + left; + result[1] = (Math.random() * (top - bottom)) + bottom; + while (Math.pow(result[0] - xCenter, 2.0) + Math.pow(result[1] - yCenter, 2.0) + > Math.pow(rad, 2.0)) { + result[0] = (Math.random() * (right - left)) + left; + result[1] = (Math.random() * (top - bottom)) + bottom; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_479.java b/src/main/java/com/fishercoder/solutions/firstthousand/_479.java similarity index 81% rename from src/main/java/com/fishercoder/solutions/_479.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_479.java index c506e37ff1..dc565e06fd 100644 --- a/src/main/java/com/fishercoder/solutions/_479.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_479.java @@ -1,23 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; -/** - * 479. Largest Palindrome Product - * - * Find the largest palindrome made from the product of two n-digit numbers. - Since the result could be very large, you should return the largest palindrome mod 1337. - - Example: - Input: 2 - Output: 987 - - Explanation: 99 x 91 = 9009, 9009 % 1337 = 987 - - Note: - The range of n is [1,8]. - */ public class _479 { public static class Solution1 { - /** + /* * credit: https://discuss.leetcode.com/topic/74125/java-solution-using-assumed-max-palindrom */ public int largestPalindrome(int n) { @@ -45,7 +30,8 @@ public int largestPalindrome(int n) { // here i and palindrom/i forms the two factor of assumed palindrom for (long i = upperBound; upperBound > lowerBound; i--) { - // if n= 3 none of the factor of palindrom can be more than 999 or less than square root of assumed palindrom + // if n= 3 none of the factor of palindrom can be more than 999 or less than + // square root of assumed palindrom if (palindrom / i > maxNumber || i * i < palindrom) { break; } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_48.java b/src/main/java/com/fishercoder/solutions/firstthousand/_48.java new file mode 100644 index 0000000000..75c10565f5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_48.java @@ -0,0 +1,123 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; + +public class _48 { + + /* + * Note: this is an n*n matrix, in other words, it's a square, this makes it easier as well. + */ + + public static class Solution1 { + // Time: O(n^2) + // Space: O(1) + public void rotate(int[][] matrix) { + /*First swap the elements on the diagonal, then reverse each row: + * 1, 2, 3 1, 4, 7 7, 4, 1 + * 4, 5, 6 becomes 2, 5, 8 becomes 8, 5, 2 + * 7, 8, 9 3, 6, 9 9, 6, 3 + **/ + int m = matrix.length; + for (int i = 0; i < m; i++) { + for (int j = i; j < m; j++) { + /*ATTN: j starts from i, so that the diagonal changes with itself, results in no change.*/ + int tmp = matrix[i][j]; + matrix[i][j] = matrix[j][i]; + matrix[j][i] = tmp; + } + } + CommonUtils.print2DIntArray(matrix); + + /*then reverse*/ + for (int i = 0; i < m; i++) { + int left = 0; + int right = m - 1; + while (left < right) { + int tmp = matrix[i][left]; + matrix[i][left] = matrix[i][right]; + matrix[i][right] = tmp; + left++; + right--; + } + } + } + } + + public static class Solution2 { + /* + * First swap the rows bottom up, then swap the element on the diagonal: + *

+ * 1, 2, 3 7, 8, 9 7, 4, 1 + * 4, 5, 6 becomes 4, 5, 6 becomes 8, 5, 2 + * 7, 8, 9 1, 2, 3 9, 6, 3 + *

+ * Time: O(n^2) + * Space: O(1) + */ + public void rotate(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + int top = 0; + int bottom = n - 1; + while (top < bottom) { + int[] tmp = matrix[top]; + matrix[top] = matrix[bottom]; + matrix[bottom] = tmp; + top++; + bottom--; + } + + for (int i = 0; i < m; i++) { + for (int j = i + 1; j < n; j++) { + int tmp = matrix[i][j]; + matrix[i][j] = matrix[j][i]; + matrix[j][i] = tmp; + } + } + } + } + + public static class Solution3 { + /* + * You only need to rotate the top right quarter, + * with this example: + * {1, 2, 3, 4}, + * {5, 6, 7, 8}, + * {9, 10, 11, 12}, + * {13, 14, 15, 16} + * + * top will only be: + * 1, 2, 3, + * 6 + * + * then this entire matrix is rotated. As they'll drive to ratate the corresponding three elements in the matrix. + * + * Another cool trick that this solution takes advantage is that because it's a square matrix, + * meaning number of rows equals to the number of columns, we could do swap like this, + * if it's a rectangular, below method won't work and will throw ArrayIndexOutOfBoundsException. + * + */ + public void rotate(int[][] matrix) { + int n = matrix.length; + for (int i = 0; i < n / 2; i++) { + for (int j = i; j < n - i - 1; j++) { + // save the top left + int top = matrix[i][j]; + System.out.println("top = " + top); + + // move bottom left to top left + matrix[i][j] = matrix[n - 1 - j][i]; + + // move bottom right to bottom left + matrix[n - 1 - j][i] = matrix[n - i - 1][n - 1 - j]; + + // move top right to bottom right + matrix[n - i - 1][n - 1 - j] = matrix[j][n - i - 1]; + + // move top left to top right + matrix[j][n - i - 1] = top; + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_480.java b/src/main/java/com/fishercoder/solutions/firstthousand/_480.java new file mode 100644 index 0000000000..5c7c9523d4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_480.java @@ -0,0 +1,76 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Collections; +import java.util.PriorityQueue; + +public class _480 { + public static class Solution1 { + + /* + * You cannot simply use minus sign '-' to denote the descending order, because e.g. 3 and -3 might both exist in this array, + * so we'll have to use the original numbers themselves to store in the heaps. + */ + private PriorityQueue minHeap = new PriorityQueue<>(); + private PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); + + public double[] medianSlidingWindow(int[] nums, int k) { + int n = nums.length - k + 1; + if (n <= 0) { + return new double[0]; + } + double[] result = new double[n]; + + for (int i = 0; i <= nums.length; i++) { + if (i >= k) { + result[i - k] = getMedian(); + remove(nums[i - k]); + } + if (i < nums.length) { + add(nums[i]); + } + } + + return result; + } + + private double getMedian() { + if (maxHeap.isEmpty() && minHeap.isEmpty()) { + return 0; + } + + if (maxHeap.size() == minHeap.size()) { + return ((double) maxHeap.peek() + (double) minHeap.peek()) / 2.0; + } else { + return (double) minHeap.peek(); + } + } + + private void remove(int num) { + if (num < getMedian()) { + maxHeap.remove(num); + } else { + minHeap.remove(num); + } + if (maxHeap.size() > minHeap.size()) { + minHeap.add(maxHeap.poll()); + } + if (minHeap.size() - maxHeap.size() > 1) { + maxHeap.add(minHeap.poll()); + } + } + + private void add(int num) { + if (num < getMedian()) { + maxHeap.add(num); + } else { + minHeap.add(num); + } + if (maxHeap.size() > minHeap.size()) { + minHeap.add(maxHeap.poll()); + } + if (minHeap.size() - maxHeap.size() > 1) { + maxHeap.add(minHeap.poll()); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_481.java b/src/main/java/com/fishercoder/solutions/firstthousand/_481.java new file mode 100644 index 0000000000..360c317e0d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_481.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +public class _481 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/74917/simple-java-solution-using-one-array-and-two-pointers + * Algorithm: + *

+ * 1. Create an int array a and initialize the first 3 elements with 1, 2, 2. + * 2. Create two pointers head and tail. head points to the number which will be used to generate new numbers. + * tail points to the next empty position to put the new number. Then keep generating new numbers until tail >= n. + * 3. Need to create the array 1 element more than n to avoid overflow because the last round head might points to a number 2. + * 4. A trick to flip number back and forth between 1 and 2: num = num ^ 3 + */ + public int magicalString(int n) { + if (n <= 0) { + return 0; + } + if (n <= 3) { + return 1; + } + + int[] a = new int[n + 1]; + a[0] = 1; + a[1] = 2; + a[2] = 2; + + int head = 2; + int tail = 3; + int num = 1; + int result = 1; + + while (tail < n) { + for (int i = 0; i < a[head]; i++) { + a[tail] = num; + if (num == 1 && tail < n) { + result++; + } + tail++; + } + num = num ^ 3; + head++; + } + + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_482.java b/src/main/java/com/fishercoder/solutions/firstthousand/_482.java new file mode 100644 index 0000000000..50aebb9f45 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_482.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +public class _482 { + public static class Solution1 { + public String licenseKeyFormatting(String S, int K) { + StringBuilder stringBuilder = new StringBuilder(); + char[] SChars = S.toCharArray(); + for (int i = S.length() - 1, j = 0; i >= 0; ) { + if (j < K) { + if (SChars[i] != '-') { + if (SChars[i] >= 'a' && SChars[i] <= 'z') { + stringBuilder.append(Character.toUpperCase(SChars[i])); + } else { + stringBuilder.append(SChars[i]); + } + j++; + } + i--; + } else if (j == K) { + j = 0; + stringBuilder.append('-'); + } + } + if (stringBuilder.length() > 1 + && stringBuilder.substring(stringBuilder.length() - 1).equals("-")) { + return stringBuilder.reverse().substring(1); + } + return stringBuilder.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_483.java b/src/main/java/com/fishercoder/solutions/firstthousand/_483.java new file mode 100644 index 0000000000..e1f73ea7c3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_483.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +import java.math.BigInteger; + +public class _483 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/82130/java-solution-with-hand-writing-explain + */ + public String smallestGoodBase(String n) { + long nn = Long.parseLong(n); + long res = 0; + for (int k = 60; k >= 2; k--) { + long start = 2; + long end = nn; + while (start < end) { + long m = start + (end - start) / 2; + + BigInteger left = BigInteger.valueOf(m); + left = left.pow(k).subtract(BigInteger.ONE); + BigInteger right = + BigInteger.valueOf(nn) + .multiply(BigInteger.valueOf(m).subtract(BigInteger.ONE)); + int cmr = left.compareTo(right); + if (cmr == 0) { + res = m; + break; + } else if (cmr < 0) { + start = m + 1; + } else { + end = m; + } + } + + if (res != 0) { + break; + } + } + return "" + res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_484.java b/src/main/java/com/fishercoder/solutions/firstthousand/_484.java new file mode 100644 index 0000000000..ccecf545de --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_484.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.firstthousand; + +public class _484 { + public static class Solution1 { + + /* + * credit:https://discuss.leetcode.com/topic/76221/java-o-n-clean-solution-easy-to-understand + *

+ * For example, given IDIIDD we start with sorted sequence 1234567 + * Then for each k continuous D starting at index i we need to reverse [i, i+k] portion of the sorted sequence. + *

+ * e.g. + * IDIIDD + *

+ * 1234567 // sorted + * 1324765 // answer + */ + public int[] findPermutation(String s) { + int[] result = new int[s.length() + 1]; + for (int i = 0; i <= s.length(); i++) { + result[i] = i + 1; + } + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == 'D') { + int left = i; + while (i < s.length() && s.charAt(i) == 'D') { + i++; + } + reverse(result, left, i); + } + } + return result; + } + + private void reverse(int[] result, int left, int i) { + while (left < i) { + int temp = result[left]; + result[left] = result[i]; + result[i] = temp; + left++; + i--; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_485.java b/src/main/java/com/fishercoder/solutions/firstthousand/_485.java new file mode 100644 index 0000000000..f9fe998bee --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_485.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +public class _485 { + public static class Solution1 { + public int findMaxConsecutiveOnes(int[] nums) { + int maxLen = 0; + for (int i = 0; i < nums.length; i++) { + int currentLen = 0; + while (i < nums.length && nums[i] == 1) { + i++; + currentLen++; + } + maxLen = Math.max(maxLen, currentLen); + } + return maxLen; + } + } + + public static class Solution2 { + /* + * Apply the sliding window template, i.e. two pointer technique. + */ + public int findMaxConsecutiveOnes(int[] nums) { + int left = 0; + int right = 0; + int ans = 0; + while (right < nums.length) { + while (right < nums.length && nums[right] == 1) { + right++; + } + ans = Math.max(ans, right - left); + while (right < nums.length && nums[right] != 1) { + right++; + } + left = right; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_486.java b/src/main/java/com/fishercoder/solutions/firstthousand/_486.java new file mode 100644 index 0000000000..cfccceeb5b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_486.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +public class _486 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/76312/java-1-line-recursion-solution + * Explanation + * So assuming the sum of the array it SUM, so eventually player1 and player2 will split the SUM between themselves. + * For player1 to win, he/she has to get more than what player2 gets. + * If we think from the prospective of one player, then what he/she gains each time is a plus, + * while, what the other player gains each time is a minus. Eventually if player1 can have a >0 total, player1 can win. + *

+ * Helper function simulate this process. In each round: + * if e==s, there is no choice but have to select nums[s] + * otherwise, this current player has 2 options: + * --> nums[s]-helper(nums,s+1,e): this player select the front item, leaving the other player a choice from s+1 to e + * --> nums[e]-helper(nums,s,e-1): this player select the tail item, leaving the other player a choice from s to e-1 + * Then take the max of these two options as this player's selection, return it. + */ + public boolean predictTheWinner(int[] nums) { + return helper(nums, 0, nums.length - 1, new Integer[nums.length][nums.length]) >= 0; + } + + private int helper(int[] nums, int start, int end, Integer[][] mem) { + if (mem[start][end] == null) { + mem[start][end] = + start == end + ? nums[end] + : Math.max( + nums[end] - helper(nums, start, end - 1, mem), + nums[start] - helper(nums, start + 1, end, mem)); + } + return mem[start][end]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_487.java b/src/main/java/com/fishercoder/solutions/firstthousand/_487.java new file mode 100644 index 0000000000..42648fcab2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_487.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.firstthousand; + +public class _487 { + + public static class Solution1 { + /* + * I implemented this on my own after a quick read from https://leetcode.com/problems/max-consecutive-ones-ii/solution/ + */ + public static int findMaxConsecutiveOnes(int[] nums) { + int left = 0; + int right = 0; + int zeroes = 0; + int ans = 0; + while (right < nums.length) { + if (nums[right] == 0) { + zeroes++; + } + if (zeroes <= 1) { + ans = Math.max(ans, right - left + 1); + } else { + while (left < nums.length && zeroes > 1) { + if (nums[left] == 0) { + zeroes--; + } + left++; + } + } + right++; + } + return ans; + } + } + + public static class Solution2 { + /* + * This is a more generic solution adapted from https://leetcode.com/problems/max-consecutive-ones-iii/, just set k = 1 here. + */ + public static int findMaxConsecutiveOnes(int[] nums) { + int len = nums.length; + int left = 0; + int right = 0; + int ans = 0; + int k = 1; + for (; right < len; right++) { + if (nums[right] == 0) { + k--; + } + while (k < 0) { + if (nums[left] == 0) { + k++; + } + left++; + } + ans = Math.max(ans, right - left + 1); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_488.java b/src/main/java/com/fishercoder/solutions/firstthousand/_488.java new file mode 100644 index 0000000000..4d1f23877f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_488.java @@ -0,0 +1,67 @@ +package com.fishercoder.solutions.firstthousand; + +public class _488 { + + public static class Solution1 { + + /* + * credit: https://discuss.leetcode.com/topic/79820/short-java-solution-beats-98 + * Two layer of recursion, pretty cool! + */ + + int maxcount = + 6; // the max balls you need will not exceed 6 since "The number of balls in your + + // hand won't exceed 5" + + public int findMinStep(String board, String hand) { + int[] handCount = new int[26]; + for (int i = 0; i < hand.length(); ++i) { + ++handCount[hand.charAt(i) - 'A']; + } + int result = dfs(board + "#", handCount); // append a "#" to avoid special process while + // j==board.length, make the code shorter. + return result == maxcount ? -1 : result; + } + + private int dfs(String s, int[] handCount) { + s = removeConsecutive(s); + if (s.equals("#")) { + return 0; + } + int result = maxcount; + int need = 0; + for (int i = 0, j = 0; j < s.length(); ++j) { + if (s.charAt(j) == s.charAt(i)) { + continue; + } + need = 3 - (j - i); // balls need to remove current consecutive balls. + if (handCount[s.charAt(i) - 'A'] >= need) { + handCount[s.charAt(i) - 'A'] -= need; + result = + Math.min( + result, + need + dfs(s.substring(0, i) + s.substring(j), handCount)); + handCount[s.charAt(i) - 'A'] += need; + } + i = j; + } + return result; + } + + // remove consecutive balls longer than 3 + private String removeConsecutive(String board) { + for (int i = 0, j = 0; j < board.length(); ++j) { + if (board.charAt(j) == board.charAt(i)) { + continue; + } + if (j - i >= 3) { + return removeConsecutive(board.substring(0, i) + board.substring(j)); + } else { + i = j; + } + } + return board; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_49.java b/src/main/java/com/fishercoder/solutions/firstthousand/_49.java new file mode 100644 index 0000000000..39dade76d0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_49.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _49 { + + public static class Solution1 { + /* + * Time: O(n*k*logk) where n is the # of strings in the given input and k is the maximum length of each string + */ + public List> groupAnagrams(String[] strs) { + Map> map = new HashMap<>(); + for (String word : strs) { + char[] c = word.toCharArray(); + Arrays.sort(c); + String key = new String(c); + if (!map.containsKey(key)) { + map.put(key, new ArrayList<>()); + } + map.get(key).add(word); + } + return new ArrayList<>(map.values()); + } + } + + public static class Solution2 { + /* + * This is an improvement to the above solution in terms of time complexity. + * Time: O(n*k) where n is the # of strings in the given input and k is the maximum length of each string + */ + public List> groupAnagrams(String[] strs) { + Map> map = new HashMap<>(); + for (String word : strs) { + int[] count = new int[26]; + for (char c : word.toCharArray()) { + count[c - 'a']++; + } + StringBuilder sb = new StringBuilder(); + for (int c : count) { + sb.append(c); + sb.append("$"); + } + if (!map.containsKey(sb.toString())) { + map.put(sb.toString(), new ArrayList<>()); + } + map.get(sb.toString()).add(word); + } + return new ArrayList<>(map.values()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_490.java b/src/main/java/com/fishercoder/solutions/firstthousand/_490.java new file mode 100644 index 0000000000..94313474ca --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_490.java @@ -0,0 +1,62 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _490 { + /* + * BFS: the key part of this algorithm is that: this is a ball that won't stop rolling until it hits a wall. + * This means we'll have to store all the empty spaces where the ball was forced to stop into the queue, these are + * the only places where the next starting points could be. + * Then we use BFS to traverse through all four directions of each starting point. + *

+ * Also, another point to note is: it must be a stop point for the ball. + */ + public boolean hasPath(int[][] maze, int[] start, int[] destination) { + if (start[0] == destination[0] && destination[0] == destination[1]) { + return true; + } + final int[] directions = new int[] {-1, 0, 1, 0, -1}; + Queue queue = new LinkedList<>(); + queue.offer(new Point(start[0], start[1])); + int m = maze.length; + int n = maze[0].length; + boolean[][] visited = new boolean[m][n]; + visited[start[0]][start[1]] = true; + + while (!queue.isEmpty()) { + Point curr = queue.poll(); + int x = curr.x; + int y = curr.y; // keep the original value + for (int i = 0; i < directions.length - 1; i++) { + int xx = x; + int yy = y; // use temp variables to move + while (xx >= 0 && yy >= 0 && xx < m && yy < n && maze[xx][yy] == 0) { + xx += directions[i]; + yy += directions[i + 1]; + } + xx -= directions[i]; + yy -= directions[i + 1]; + if (visited[xx][yy]) { + continue; + } + visited[xx][yy] = true; + if (destination[0] == xx && destination[1] == yy) { + return true; + } + queue.offer(new Point(xx, yy)); + } + } + return false; + } + + class Point { + int x; + int y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_491.java b/src/main/java/com/fishercoder/solutions/firstthousand/_491.java new file mode 100644 index 0000000000..ffdf1f6630 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_491.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _491 { + + public static class Solution1 { + public List> findSubsequences(int[] nums) { + if (nums == null || nums.length == 1) { + return new ArrayList<>(); + } + Set> answer = new HashSet<>(); + List list = new ArrayList<>(); + return new ArrayList<>(backtracking(nums, 0, list, answer)); + } + + private Set> backtracking( + int[] nums, int start, List currList, Set> answer) { + if (currList.size() >= 2) { + answer.add(new ArrayList<>(currList)); + } + for (int i = start; i < nums.length; i++) { + if (currList.size() == 0 || currList.get(currList.size() - 1) <= nums[i]) { + currList.add(nums[i]); + backtracking(nums, i + 1, currList, answer); + currList.remove(currList.size() - 1); + } + } + return answer; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_492.java b/src/main/java/com/fishercoder/solutions/firstthousand/_492.java new file mode 100644 index 0000000000..2c2930b94b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_492.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _492 { + + public static class Solution1 { + public int[] constructRectangle(int area) { + int i = 0; + int j = area; + int[] result = new int[2]; + while (i <= j) { + long product = i * j; + if (product == area) { + result[0] = j--; + result[1] = i++; + } else if (product > area) { + j--; + } else { + i++; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_493.java b/src/main/java/com/fishercoder/solutions/firstthousand/_493.java new file mode 100644 index 0000000000..3e7dec3dc2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_493.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _493 { + + public static class Solution1 { + + /* + * reference: https://discuss.leetcode.com/topic/78933/very-short-and-clear-mergesort-bst-java-solutions + */ + public int reversePairs(int[] nums) { + return mergeSort(nums, 0, nums.length - 1); + } + + private int mergeSort(int[] nums, int start, int end) { + if (start >= end) { + return 0; + } + int mid = start + (end - start) / 2; + int cnt = mergeSort(nums, start, mid) + mergeSort(nums, mid + 1, end); + for (int i = start, j = mid + 1; i <= mid; i++) { + /*it has to be 2.0 instead of 2, otherwise it's going to stack overflow, i.e. test3 is going to fail*/ + while (j <= end && nums[i] > nums[j] * 2.0) { + j++; + } + cnt += j - (mid + 1); + } + Arrays.sort(nums, start, end + 1); + return cnt; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_494.java b/src/main/java/com/fishercoder/solutions/firstthousand/_494.java new file mode 100644 index 0000000000..e36f6caa49 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_494.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +public class _494 { + + public static class Solution1 { + public int findTargetSumWays(int[] nums, int S) { + return find(0, nums, S); + } + + private int find(int p, int[] nums, int sum) { + if (p == nums.length) { + if (sum == 0) { + return 1; + } else { + return 0; + } + } + return find(p + 1, nums, sum + nums[p]) + find(p + 1, nums, sum - nums[p]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_495.java b/src/main/java/com/fishercoder/solutions/firstthousand/_495.java new file mode 100644 index 0000000000..f285843a0d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_495.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _495 { + + public static class Solution1 { + public int findPoisonedDuration(int[] timeSeries, int duration) { + if (timeSeries == null || timeSeries.length == 0 || duration == 0) { + return 0; + } + int totalDuration = 0; + for (int i = 0; i < timeSeries.length - 1; i++) { + if ((timeSeries[i + 1] - timeSeries[i]) >= duration) { + totalDuration += duration; + } else { + totalDuration += (timeSeries[i + 1] - timeSeries[i]); + } + } + totalDuration += duration; // plus the last one duration + return totalDuration; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_496.java b/src/main/java/com/fishercoder/solutions/firstthousand/_496.java new file mode 100644 index 0000000000..db94ed98c1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_496.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +public class _496 { + + public static class Solution1 { + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + int[] ans = new int[nums1.length]; + Map map = new HashMap<>(); + for (int i = 0; i < nums2.length; i++) { + map.put(nums2[i], i); + } + for (int i = 0; i < nums1.length; i++) { + int start = map.get(nums1[i]); + for (int j = start + 1; j < nums2.length; j++) { + if (nums2[j] > nums1[i]) { + ans[i] = nums2[j]; + break; + } + } + if (ans[i] == 0) { + ans[i] = -1; + } + } + return ans; + } + } + + public static class Solution2 { + /* + * Use monotonic stack, using a pen and paper to help visualize this is a great help! + */ + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + Deque stack = new LinkedList<>(); + Map map = new HashMap(); + for (int i = 0; i < nums2.length; i++) { + while (!stack.isEmpty() && nums2[i] > stack.peekLast()) { + map.put(stack.pollLast(), nums2[i]); + } + stack.addLast(nums2[i]); + } + + while (!stack.isEmpty()) { + map.put(stack.pollLast(), -1); + } + + int[] result = new int[nums1.length]; + for (int i = 0; i < nums1.length; i++) { + result[i] = map.get(nums1[i]); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_498.java b/src/main/java/com/fishercoder/solutions/firstthousand/_498.java new file mode 100644 index 0000000000..90c005a474 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_498.java @@ -0,0 +1,100 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _498 { + + public static class Solutoin1 { + /* + * Reference: https://discuss.leetcode.com/topic/77865/concise-java-solution/2 + * Just keep walking the matrix, when hitting the four borders (top, bottom, left or right), + * change directions and keep walking: + *

+ * if out of bottom border (i >= m), then i = m - 1, j += 2, change walk direction; + * if out of top border (i < 0), then i = 0, change walk direction; + * if out of left border (j < 0), then j = 0, change walk direction; + * if out of right border (j >= n), then j = n - 1, i += 2, change walk direction. + */ + public int[] findDiagonalOrder(int[][] mat) { + if (mat == null || mat.length == 0) { + return new int[0]; + } + int m = mat.length; + int n = mat[0].length; + int[] result = new int[m * n]; + // {-1,1} goes from top left to bottom right + // {1,-1} goes from top right to bottom left + int[][] dirs = new int[][] {{-1, 1}, {1, -1}}; + int i = 0; + int j = 0; + int d = 0; + for (int k = 0; k < m * n; ) { + result[k++] = mat[i][j]; + i += dirs[d][0]; + j += dirs[d][1]; + + if (i >= m) { + i = m - 1; + j += 2; + d = 1 - d; + } + if (j >= n) { + j = n - 1; + i += 2; + d = 1 - d; + } + if (i < 0) { + i = 0; + d = 1 - d; + } + if (j < 0) { + j = 0; + d = 1 - d; + } + } + return result; + } + } + + public static class Solutoin2 { + public int[] findDiagonalOrder(int[][] matrix) { + if (matrix == null || matrix.length == 0) { + return new int[0]; + } + List> diagonals = new ArrayList<>(); + int maxRow = matrix.length; + int maxCol = matrix[0].length; + int maxDiagonal = maxRow + maxCol - 1; + for (int diagonalIndex = 0; diagonalIndex < maxDiagonal; diagonalIndex++) { + int curRowIdx = (diagonalIndex < maxCol) ? 0 : (diagonalIndex - maxCol + 1); + int curColIdx = (diagonalIndex < maxCol) ? diagonalIndex : (maxCol - 1); + List diagonal = new ArrayList<>(); + while (curRowIdx >= 0 + && curRowIdx < maxRow + && curColIdx >= 0 + && curColIdx < maxCol) { + int diagonalElement = matrix[curRowIdx][curColIdx]; + diagonal.add(diagonalElement); + curRowIdx++; + curColIdx--; + } + diagonals.add(diagonal); + } + int[] result = new int[maxRow * maxCol]; + int resultIdx = 0; + for (int i = 0; i < diagonals.size(); i++) { + List diagonal = diagonals.get(i); + if (i % 2 == 0) { + Collections.reverse(diagonal); + } + for (int j = 0; j < diagonal.size(); j++) { + result[resultIdx] = diagonal.get(j); + resultIdx++; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_499.java b/src/main/java/com/fishercoder/solutions/firstthousand/_499.java new file mode 100644 index 0000000000..57f0c6fec9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_499.java @@ -0,0 +1,87 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.PriorityQueue; +import java.util.Queue; + +public class _499 { + public static class Solutoin1 { + /* + * credit: https://discuss.leetcode.com/topic/77474/similar-to-the-maze-ii-easy-understanding-java-bfs-solution + */ + + public String findShortestWay(int[][] maze, int[] ball, int[] hole) { + + final int[] directions = new int[] {-1, 0, 1, 0, -1}; + Queue heap = new PriorityQueue<>(); + heap.offer(new Point(ball[0], ball[1], 0, "")); + int m = maze.length; + int n = maze[0].length; + Point[][] points = new Point[m][n]; + for (int i = 0; i < m * n; i++) { + points[i / n][i % n] = new Point(i / n, i % n); // initialize the length array + } + String[] ds = new String[] {"u", "r", "d", "l"}; + while (!heap.isEmpty()) { + Point curr = heap.poll(); + if (points[curr.x][curr.y].compareTo(curr) <= 0) { + continue; // if we have already found a shorter route + } + points[curr.x][curr.y] = curr; + for (int i = 0; i < directions.length - 1; i++) { + int x = curr.x; + int y = curr.y; + int distance = curr.distance; // use temp variables to move + // we need below while loop to find only "stop" points that could be put into + // the queue + while (x >= 0 + && y >= 0 + && x < m + && y < n + && maze[x][y] == 0 + && (x != hole[0] || y != hole[1])) { + x += directions[i]; + y += directions[i + 1]; + distance++; + } + if (x != hole[0] || y != hole[1]) { + x -= directions[i]; + y -= directions[i + 1]; + distance--; + } + heap.offer(new Point(x, y, distance, curr.path + ds[i])); + } + } + return points[hole[0]][hole[1]].distance == Integer.MAX_VALUE + ? "impossible" + : points[hole[0]][hole[1]].path; + } + + class Point implements Comparable { + int x; + int y; + int distance; + String path; + + public Point(int x, int y) { + this.x = x; + this.y = y; + this.distance = Integer.MAX_VALUE; + this.path = ""; + } + + public Point(int x, int y, int distance, String path) { + this.x = x; + this.y = y; + this.distance = distance; + this.path = path; + } + + @Override + public int compareTo(Point o) { + return (this.distance == o.distance) + ? this.path.compareTo(o.path) + : this.distance - o.distance; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_5.java b/src/main/java/com/fishercoder/solutions/firstthousand/_5.java new file mode 100644 index 0000000000..0449c53c17 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_5.java @@ -0,0 +1,99 @@ +package com.fishercoder.solutions.firstthousand; + +public class _5 { + + public static class Solution1 { + private int low; + private int maxLen; + + public String longestPalindrome(String s) { + int len = s.length(); + if (len < 2) { + return s; + } + + for (int i = 0; i < len - 1; i++) { + extendPalindrome( + s, i, i); // assume odd length, try to extend Palindrome as possible + extendPalindrome(s, i, i + 1); // assume even length. + } + return s.substring(low, low + maxLen); + } + + private void extendPalindrome(String s, int left, int right) { + while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { + left--; + right++; + } + if (maxLen < right - left - 1) { + low = left + 1; + maxLen = right - left - 1; + } + } + } + + public static class Solution2 { + /* + * Same sliding window idea, but without using global variables. + * Credit: https://leetcode.com/problems/longest-palindromic-substring/solution/ + */ + public String longestPalindrome(String s) { + int start = 0; + int end = 0; + for (int i = 0; i < s.length(); i++) { + int len1 = expand(s, i, i); + int len2 = expand(s, i, i + 1); + int len = Math.max(len1, len2); + if (len > end - start) { + start = i - (len - 1) / 2; + end = i + len / 2; + } + } + return s.substring(start, end + 1); + } + + private int expand(String s, int left, int right) { + int l = left; + int r = right; + while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) { + l--; + r++; + } + return r - l - 1; + } + } + + public static class Solution3 { + /* + * My own implementation using the same idea. + */ + public String longestPalindrome(String s) { + String ans = ""; + int maxLen = 0; + for (int i = 0; i < s.length(); i++) { + int[] pair = expand(s, i, i); + if (pair[1] - pair[0] + 1 > maxLen) { + maxLen = pair[1] - pair[0] + 1; + ans = s.substring(pair[0], pair[1] + 1); + } + pair = expand(s, i, i + 1); + if (pair[1] - pair[0] + 1 > maxLen) { + maxLen = pair[1] - pair[0] + 1; + ans = s.substring(pair[0], pair[1] + 1); + } + } + return ans; + } + + private int[] expand(String s, int l, int r) { + int[] pair = new int[2]; + while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) { + pair[0] = l; + pair[1] = r; + l--; + r++; + } + return pair; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_50.java b/src/main/java/com/fishercoder/solutions/firstthousand/_50.java new file mode 100644 index 0000000000..7a8b5343fb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_50.java @@ -0,0 +1,74 @@ +package com.fishercoder.solutions.firstthousand; + +public class _50 { + + public static class Solution1 { + /* + * Time: O(logn) + * Space: O(logn) + */ + public double myPow(double x, int n) { + long N = n; + if (N < 0) { + x = 1 / x; + N = -N; + } + return fastPow(x, N); + } + + private double fastPow(double x, long n) { + if (n == 0) { + return 1.0; + } + double half = fastPow(x, n / 2); + if (n % 2 == 0) { + return half * half; + } else { + return half * half * x; + } + } + } + + public static class Solution2 { + /* + * Time: O(logn) + * Space: O(1) + */ + public double myPow(double x, int n) { + long N = n; + if (N < 0) { + x = 1 / x; + N = -N; + } + double answer = 1; + double currentProduct = x; + for (long i = N; i > 0; i /= 2) { + if (i % 2 == 1) { + answer = answer * currentProduct; + } + currentProduct *= currentProduct; + } + return answer; + } + } + + public static class Solution3 { + /* + * credit: https://leetcode.com/problems/powx-n/solutions/19546/short-and-easy-to-understand-solution/comments/162293 + */ + public double myPow(double x, int n) { + if (n == 0) { + return 1; + } + if (n < 0) { + // this is to avoid integer overflow + return 1 / x * myPow(1 / x, -(n + 1)); + } + if (n % 2 == 0) { + return myPow(x * x, n / 2); + } else { + return x * myPow(x * x, n / 2); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_500.java b/src/main/java/com/fishercoder/solutions/firstthousand/_500.java new file mode 100644 index 0000000000..ee4ecba7a2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_500.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _500 { + + public String[] findWords(String[] words) { + final Set row1 = + new HashSet<>(Arrays.asList('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p')); + final Set row2 = + new HashSet<>(Arrays.asList('a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l')); + final Set row3 = new HashSet<>(Arrays.asList('z', 'x', 'c', 'v', 'b', 'n', 'm')); + final List> setList = Arrays.asList(row1, row2, row3); + List wordList = new ArrayList<>(); + for (String word : words) { + String lowerCaseWord = word.toLowerCase(); + boolean can = true; + for (Set row : setList) { + for (char c : lowerCaseWord.toCharArray()) { + if (!row.contains(c)) { + can = false; + break; + } + } + if (can) { + wordList.add(word); + break; + } + can = true; + } + } + String[] result = new String[wordList.size()]; + for (int i = 0; i < wordList.size(); i++) { + result[i] = wordList.get(i); + } + return result; + } +} diff --git a/src/main/java/com/fishercoder/solutions/_501.java b/src/main/java/com/fishercoder/solutions/firstthousand/_501.java similarity index 88% rename from src/main/java/com/fishercoder/solutions/_501.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_501.java index 9a8554d226..5509681b3f 100644 --- a/src/main/java/com/fishercoder/solutions/_501.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_501.java @@ -1,7 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; - import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; @@ -9,27 +8,11 @@ import java.util.Map; import java.util.Queue; -/** - * 501. Find Mode in Binary Search Tree - * Given a binary search tree with duplicates. You have to find all the mode(s) in given binary tree. - - For example: - Given binary tree [1,null,2,2], - 1 - \ - 2 - / - 2 - return [2]. - - Note: If a tree has more than one mode, you can return them in any order. - - */ public class _501 { public static class Solution1 { public int[] findMode(TreeNode root) { - int[] result = new int[]{}; + int[] result = new int[] {}; Map map = new HashMap(); if (root == null) { return result; @@ -105,5 +88,4 @@ private void dfs(TreeNode root, Map map) { dfs(root.right, map); } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_502.java b/src/main/java/com/fishercoder/solutions/firstthousand/_502.java new file mode 100644 index 0000000000..546a3dba33 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_502.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.PriorityQueue; + +public class _502 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues + */ + public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) { + PriorityQueue capitalHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]); + PriorityQueue profitHeap = new PriorityQueue<>((a, b) -> b[1] - a[1]); + for (int i = 0; i < Profits.length; i++) { + capitalHeap.add(new int[] {Capital[i], Profits[i]}); + } + while (k-- > 0) { + while (!capitalHeap.isEmpty() && capitalHeap.peek()[0] <= W) { + profitHeap.add(capitalHeap.poll()); + } + if (profitHeap.isEmpty()) { + break; + } + W += profitHeap.poll()[1]; + } + return W; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_503.java b/src/main/java/com/fishercoder/solutions/firstthousand/_503.java new file mode 100644 index 0000000000..e701d408cc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_503.java @@ -0,0 +1,56 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Stack; + +public class _503 { + + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/77881/typical-ways-to-solve-circular-array-problems-java-solution + * Note: we store INDEX into the stack, reversely, the larger index put at the bottom of the stack, the smaller index at the top + */ + public int[] nextGreaterElements(int[] nums) { + if (nums == null || nums.length == 0) { + return nums; + } + int len = nums.length; + Stack stack = new Stack<>(); + for (int i = len - 1; i >= 0; i--) { + stack.push(i); + // push all indexes into the stack reversely + } + int[] result = new int[len]; + for (int i = len - 1; i >= 0; i--) { + result[i] = -1; + // initialize it to be -1 in case we cannot find its next greater element in the + // array + while (!stack.isEmpty() && (nums[stack.peek()] <= nums[i])) { + stack.pop(); + } + if (!stack.isEmpty()) { + result[i] = nums[stack.peek()]; + } + stack.push(i); + } + return result; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/articles/next-greater-element-ii/ + */ + public int[] nextGreaterElements(int[] nums) { + int[] result = new int[nums.length]; + Stack stack = new Stack<>(); + for (int i = nums.length * 2 - 1; i >= 0; i--) { + while (!stack.isEmpty() && nums[stack.peek()] <= nums[i % nums.length]) { + stack.pop(); + } + result[i % nums.length] = stack.isEmpty() ? -1 : nums[stack.peek()]; + stack.push(i % nums.length); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_504.java b/src/main/java/com/fishercoder/solutions/firstthousand/_504.java new file mode 100644 index 0000000000..1aa8e77f17 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_504.java @@ -0,0 +1,10 @@ +package com.fishercoder.solutions.firstthousand; + +public class _504 { + + public static class Solution1 { + public String convertToBase7(int num) { + return String.valueOf(Integer.toString(num, 7)); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_505.java b/src/main/java/com/fishercoder/solutions/firstthousand/_505.java new file mode 100644 index 0000000000..0875f62e47 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_505.java @@ -0,0 +1,65 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _505 { + + public static class Solution1 { + /* + * The difference between II and I of this problem: + * the extra array is not boolean type any more, but int type, and it's recording the length of each point to start point. + */ + public int shortestDistance(int[][] maze, int[] start, int[] destination) { + + final int[] directions = new int[] {-1, 0, 1, 0, -1}; + Queue queue = new LinkedList<>(); + queue.offer(new Point(start[0], start[1], 0)); + int m = maze.length; + int n = maze[0].length; + int[][] length = new int[m][n]; + for (int i = 0; i < m * n; i++) { + length[i / n][i % n] = Integer.MAX_VALUE; // initialize the length array + } + + while (!queue.isEmpty()) { + Point curr = queue.poll(); + if (length[curr.x][curr.y] <= curr.distance) { + continue; + } + length[curr.x][curr.y] = curr.distance; + for (int i = 0; i < directions.length - 1; i++) { + int x = curr.x; + int y = curr.y; + int distance = curr.distance; // use temp variables to move + // we need below while loop to find only "stop" points that could be put into + // the queue + while (x >= 0 && y >= 0 && x < m && y < n && maze[x][y] == 0) { + x += directions[i]; + y += directions[i + 1]; + distance++; + } + x -= directions[i]; + y -= directions[i + 1]; + distance--; + queue.offer(new Point(x, y, distance)); + } + } + return length[destination[0]][destination[1]] == Integer.MAX_VALUE + ? -1 + : length[destination[0]][destination[1]]; + } + + class Point { + int x; + int y; + int distance; + + public Point(int x, int y, int distance) { + this.x = x; + this.y = y; + this.distance = distance; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_506.java b/src/main/java/com/fishercoder/solutions/firstthousand/_506.java new file mode 100644 index 0000000000..2006cacb3c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_506.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class _506 { + + public static class Solution1 { + public String[] findRelativeRanks(int[] nums) { + int[] tmp = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + tmp[i] = nums[i]; + } + Arrays.sort(tmp); + Map rankMap = new HashMap(); + int len = nums.length; + for (int i = len - 1; i >= 0; i--) { + if (i == len - 1) { + rankMap.put(tmp[i], "Gold Medal"); + } else if (i == len - 2) { + rankMap.put(tmp[i], "Silver Medal"); + } else if (i == len - 3) { + rankMap.put(tmp[i], "Bronze Medal"); + } else { + rankMap.put(tmp[i], String.valueOf(len - i)); + } + } + String[] result = new String[len]; + for (int i = 0; i < len; i++) { + result[i] = rankMap.get(nums[i]); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_507.java b/src/main/java/com/fishercoder/solutions/firstthousand/_507.java new file mode 100644 index 0000000000..229365917b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_507.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +public class _507 { + + public static class Solution1 { + public boolean checkPerfectNumber(int num) { + if (num == 1) { + return false; + } + int sum = 0; + for (int i = 2; i <= Math.sqrt(num); i++) { + if (num % i == 0) { + sum += i; + if (i != num / i) { + sum += num / i; + } + } + } + sum++; + return sum == num; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_508.java b/src/main/java/com/fishercoder/solutions/firstthousand/_508.java new file mode 100644 index 0000000000..55d9ae90ce --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_508.java @@ -0,0 +1,154 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class _508 { + + public static class Solution1 { + // my purely original but verbose solution + public int[] findFrequentTreeSum(TreeNode root) { + if (root == null) { + return new int[] {}; + } + + Map map = new HashMap(); + postOrder(root, map); + + Map frequencyMap = new HashMap<>(); + for (Map.Entry entry : map.entrySet()) { + frequencyMap.put( + (Integer) entry.getValue(), + frequencyMap.getOrDefault(entry.getValue(), 0) + 1); + } + + List> list = new LinkedList<>(frequencyMap.entrySet()); + Collections.sort(list, (o1, o2) -> (o2.getValue()).compareTo(o1.getValue())); + + int mostFrequency = list.get(0).getValue(); + List topFrequencyList = new ArrayList<>(); + topFrequencyList.add(list.get(0).getKey()); + int i = 1; + while (i < list.size() && list.get(i).getValue() == mostFrequency) { + topFrequencyList.add(list.get(i).getKey()); + i++; + } + + int[] result = new int[topFrequencyList.size()]; + for (int j = 0; j < topFrequencyList.size(); j++) { + result[j] = topFrequencyList.get(j); + } + + return result; + } + + private int postOrder(TreeNode root, Map map) { + int left = 0; + int right = 0; + if (root.left != null) { + left = postOrder(root.left, map); + } + if (root.right != null) { + right = postOrder(root.right, map); + } + if (root.left == null && root.right == null) { + map.put(root, root.val); + return root.val; + } + int sum = left + right + root.val; + map.put(root, sum); + return sum; + } + } + + public static class Solution2 { + // my 2nd purely original but verbose solution + public int[] findFrequentTreeSum(TreeNode root) { + Map map = new HashMap<>(); + dfs(root, map); + List> entryList = new ArrayList<>(map.entrySet()); + Collections.sort(entryList, (a, b) -> b.getValue() - a.getValue()); + List list = new ArrayList<>(); + for (int i = 0; i < entryList.size(); i++) { + if (list.size() == 0) { + list.add(entryList.get(i).getKey()); + } else { + if (map.get(list.get(0)) == entryList.get(i).getValue()) { + list.add(entryList.get(i).getKey()); + } else { + break; + } + } + } + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + + private int dfs(TreeNode root, Map map) { + if (root == null) { + return 0; + } + if (root.left == null && root.right == null) { + map.put(root.val, map.getOrDefault(root.val, 0) + 1); + return root.val; + } + int leftVal = 0; + if (root.left != null) { + leftVal = dfs(root.left, map); + } + int rightVal = 0; + if (root.right != null) { + rightVal = dfs(root.right, map); + } + int subtreeSum = leftVal + rightVal + root.val; + map.put(subtreeSum, map.getOrDefault(subtreeSum, 0) + 1); + return subtreeSum; + } + } + + public static class Solution3 { + /* + * Use post-order traversal for this problem as it needs to process subtree first before processing the root. + */ + Map map = new HashMap<>(); + + public int[] findFrequentTreeSum(TreeNode root) { + postOrder(root); + int mostFreq = -1; + for (Map.Entry entry : map.entrySet()) { + mostFreq = Math.max(mostFreq, entry.getValue()); + } + List list = new ArrayList<>(); + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() == mostFreq) { + list.add(entry.getKey()); + } + } + return list.stream().mapToInt(integer -> integer).toArray(); + } + + private int postOrder(TreeNode root) { + if (root == null) { + return 0; + } + int leftSum = postOrder(root.left); + int rightSum = postOrder(root.right); + int subtreeSum = leftSum + rightSum + root.val; + map.put(subtreeSum, map.getOrDefault(subtreeSum, 0) + 1); + return subtreeSum; + } + } + + // a more concise and space-efficient solution: + // https://discuss.leetcode.com/topic/77775/verbose-java-solution-postorder-traverse-hashmap-18ms + // the key difference between the above post and my original solution is that it's using + // Frequency as the key of the HashMap +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_509.java b/src/main/java/com/fishercoder/solutions/firstthousand/_509.java new file mode 100644 index 0000000000..14e2402aff --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_509.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _509 { + public static class Solution1 { + /* + * Time: O(n) + * Space: O(n) + */ + public int fib(int N) { + List list = new ArrayList<>(); + list.add(0); + list.add(1); + for (int i = 2; i <= N; i++) { + list.add(list.get(i - 1) + list.get(i - 2)); + } + return list.get(N); + } + } + + public static class Solution2 { + /* + * Time: O(n) + * Space: O(n) + */ + public int fib(int N) { + if (N < 2) { + return N; + } + int[] fibNums = new int[N + 1]; + fibNums[0] = 0; + fibNums[1] = 1; + for (int i = 2; i <= N; i++) { + fibNums[i] = fibNums[i - 1] + fibNums[i - 2]; + } + return fibNums[N]; + } + } + + public static class Solution3 { + /* + * Time: O(n) + * Space: O(1) + */ + public int fib(int N) { + if (N < 2) { + return N; + } + int a = 0; + int b = 1; + while (N-- > 1) { + int sum = a + b; + a = b; + b = sum; + } + return b; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_51.java b/src/main/java/com/fishercoder/solutions/firstthousand/_51.java similarity index 97% rename from src/main/java/com/fishercoder/solutions/_51.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_51.java index 0348751286..ee50d15ad2 100644 --- a/src/main/java/com/fishercoder/solutions/_51.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_51.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_513.java b/src/main/java/com/fishercoder/solutions/firstthousand/_513.java new file mode 100644 index 0000000000..639320114d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_513.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; + +public class _513 { + + public static class Solution1 { + public int findBottomLeftValue(TreeNode root) { + Queue queue = new LinkedList<>(); + queue.offer(root); + TreeNode leftMost = root; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (i == 0) { + leftMost = curr; + } + if (curr.left != null) { + queue.offer(curr.left); + } + if (curr.right != null) { + queue.offer(curr.right); + } + } + } + return leftMost.val; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_514.java b/src/main/java/com/fishercoder/solutions/firstthousand/_514.java new file mode 100644 index 0000000000..ed85d8b1db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_514.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _514 { + + public static class Solution1 { + public int findRotateSteps(String ring, String key) { + int n = ring.length(); + int m = key.length(); + int[][] dp = new int[m + 1][n]; + for (int i = m - 1; i >= 0; i--) { + for (int j = 0; j < n; j++) { + dp[i][j] = Integer.MAX_VALUE; + for (int k = 0; k < n; k++) { + if (ring.charAt(k) == key.charAt(i)) { + int diff = Math.abs(j - k); + int step = Math.min(diff, n - diff); + dp[i][j] = Math.min(dp[i][j], step + dp[i + 1][k]); + } + } + } + } + return dp[0][0] + m; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_515.java b/src/main/java/com/fishercoder/solutions/firstthousand/_515.java similarity index 87% rename from src/main/java/com/fishercoder/solutions/_515.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_515.java index 2bd64cd110..99e844aa6b 100644 --- a/src/main/java/com/fishercoder/solutions/_515.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_515.java @@ -1,28 +1,11 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; - import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; -/** - * 515. Find Largest Value in Each Tree Row - * - * You need to find the largest value in each row of a binary tree. - - Example: - Input: - - 1 - / \ - 3 2 - / \ \ - 5 3 9 - - Output: [1, 3, 9] - */ public class _515 { public static class Solution1 { @@ -59,7 +42,6 @@ public List largestValues(TreeNode root) { } dfs(root, res, 0); return res; - } public void dfs(TreeNode root, List res, int level) { @@ -74,5 +56,4 @@ public void dfs(TreeNode root, List res, int level) { dfs(root.right, res, level + 1); } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_516.java b/src/main/java/com/fishercoder/solutions/firstthousand/_516.java new file mode 100644 index 0000000000..014aa3629b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_516.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.firstthousand; + +public class _516 { + + public static class Solution1 { + /* + * Inspired by https://discuss.leetcode.com/topic/78603/straight-forward-java-dp-solution + * dp[i][j] means the longest palindromic subsequence's length of substring(i, j) + * so, in the end, we return dp[0][s.length() - 1] which means the longest palindromic subsequence + * of this whole string. + */ + public int longestPalindromeSubseq(String s) { + int[][] dp = new int[s.length()][s.length()]; + for (int i = s.length() - 1; i >= 0; i--) { + dp[i][i] = 1; // initialization + for (int j = i + 1; j < s.length(); j++) { + if (s.charAt(i) == s.charAt(j)) { + dp[i][j] = dp[i + 1][j - 1] + 2; + } else { + dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]); + } + } + } + return dp[0][s.length() - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_517.java b/src/main/java/com/fishercoder/solutions/firstthousand/_517.java new file mode 100644 index 0000000000..8e0676d97f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_517.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +public class _517 { + public static class Solution1 { + /* + * Reference: https://discuss.leetcode.com/topic/79938/super-short-easy-java-o-n-solution + */ + public int findMinMoves(int[] machines) { + int total = 0; + for (int i : machines) { + total += i; + } + if (total % machines.length != 0) { + return -1; + } + int avg = total / machines.length; + int cnt = 0; + int max = 0; + for (int load : machines) { + cnt += load - avg; // load-avg is "gain/lose" + max = Math.max(Math.max(max, Math.abs(cnt)), load - avg); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_518.java b/src/main/java/com/fishercoder/solutions/firstthousand/_518.java new file mode 100644 index 0000000000..a474a7bbf9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_518.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.firstthousand; + +public class _518 { + public static class Solution1 { + public int change(int amount, int[] coins) { + int[] ways = new int[amount + 1]; + ways[0] = 1; + for (int denom : coins) { + for (int i = 0; i < ways.length; i++) { + if (denom <= i) { + ways[i] += ways[i - denom]; + } + } + } + return ways[amount]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_519.java b/src/main/java/com/fishercoder/solutions/firstthousand/_519.java new file mode 100644 index 0000000000..a7a070c15a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_519.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +public class _519 { + public static class Solution { + + private int m; + private int n; + private Set flipped; + private Random random; + + public Solution(int m, int n) { + this.m = m; + this.n = n; + this.random = new Random(); + this.flipped = new HashSet<>(); + } + + public int[] flip() { + int i = random.nextInt(m); + int j = random.nextInt(n); + while (!flipped.add(i * n + j)) { + i = random.nextInt(m); + j = random.nextInt(n); + } + return new int[] {i, j}; + } + + public void reset() { + this.flipped = new HashSet<>(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_52.java b/src/main/java/com/fishercoder/solutions/firstthousand/_52.java similarity index 85% rename from src/main/java/com/fishercoder/solutions/_52.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_52.java index 91c9b140e6..3e6f189f5b 100644 --- a/src/main/java/com/fishercoder/solutions/_52.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_52.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _52 { public static class Solution1 { - /** + /* * credit: https://discuss.leetcode.com/topic/29626/easiest-java-solution-1ms-98-22 */ int count = 0; @@ -16,8 +16,8 @@ public int totalNQueens(int n) { return count; } - private void backtracking(int row, boolean[] cols, boolean[] diagnol, boolean[] antiDiagnol, - int n) { + private void backtracking( + int row, boolean[] cols, boolean[] diagnol, boolean[] antiDiagnol, int n) { if (row == n) { count++; } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_520.java b/src/main/java/com/fishercoder/solutions/firstthousand/_520.java new file mode 100644 index 0000000000..796def71ee --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_520.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +public class _520 { + public static class Solution1 { + public boolean detectCapitalUse(String word) { + char[] words = word.toCharArray(); + boolean firstLetterCap = false; + if (Character.isUpperCase(words[0])) { + firstLetterCap = true; + } + + if (firstLetterCap) { + if (words.length >= 2) { + int i = 2; + if (Character.isUpperCase(words[1])) { + // then all following must be all uppercase + while (i < words.length) { + if (!Character.isUpperCase(words[i])) { + return false; + } + i++; + } + return true; + } else { + // then all following must be all lowercase + while (i < words.length) { + if (!Character.isLowerCase(words[i])) { + return false; + } + i++; + } + return true; + } + } + return true; + } else { + // then all following must be all lowercase + int i = 1; + while (i < words.length) { + if (!Character.isLowerCase(words[i])) { + return false; + } + i++; + } + return true; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_521.java b/src/main/java/com/fishercoder/solutions/firstthousand/_521.java new file mode 100644 index 0000000000..5e7b32d237 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_521.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.firstthousand; + +public class _521 { + public static class Solution1 { + /* + * The gotcha point of this question is: + * 1. if a and b are identical, then there will be no common subsequence, return -1 + * 2. else if a and b are of equal length, then any one of them will be a subsequence of the other string + * 3. else if a and b are of different length, then the longer one is a required subsequence because the longer string cannot be a subsequence of the shorter one + * Or in other words, when a.length() != b.length(), no subsequence of b will be equal to a, so return Math.max(a.length(), b.length()) + */ + public int findLUSlength(String a, String b) { + if (a.equals(b)) { + return -1; + } + return Math.max(a.length(), b.length()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_522.java b/src/main/java/com/fishercoder/solutions/firstthousand/_522.java new file mode 100644 index 0000000000..8cc4749d13 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_522.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.Comparator; + +public class _522 { + + public static class Solution1 { + /* + * Idea: if there's such a LUS there in the list, it must be one of the strings in the given list, + * so we'll just go through the list and check if one string is NOT subsequence of any others, if so, return it, otherwise, return -1 + */ + public int findLUSlength(String[] strs) { + Arrays.sort( + strs, + new Comparator() { + @Override + public int compare(String o1, String o2) { + return o2.length() - o1.length(); + } + }); + + for (int i = 0; i < strs.length; i++) { + boolean found = true; + for (int j = 0; j < strs.length; j++) { + if (i == j) { + continue; + } else if (isSubsequence(strs[i], strs[j])) { + found = false; + break; + } + } + if (found) { + return strs[i].length(); + } + } + return -1; + } + + private boolean isSubsequence(String a, String b) { + int i = 0; + for (int j = 0; i < a.length() && j < b.length(); j++) { + if (a.charAt(i) == b.charAt(j)) { + i++; + } + } + return i == a.length(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_523.java b/src/main/java/com/fishercoder/solutions/firstthousand/_523.java new file mode 100644 index 0000000000..8f776f126f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_523.java @@ -0,0 +1,84 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _523 { + + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/80793/java-o-n-time-o-k-space/20 + * "The reason we use modulo is: + * (a+(n*x))%x is same as (a%x) + * e.g. in case of the array [23,2,6,4,7] and k = 6 the running sum is [23,25,31,35,42] + * and the remainders are [5,1,1,5,0]. + * We got reminder 5 at index 0 and at index 3. + * That means, in between these two indexes we must have added a number which is multiple of the k. + * Hope this clarifies your doubt :)" + */ + public boolean checkSubarraySum(int[] nums, int k) { + Map map = new HashMap<>(); + map.put( + 0, + -1); // this line is critical to mark the beginning of the prefix sum, so that + // next time, when we encounter a running sum of zero, we know that's the + // answer, see test case 11 + int sum = 0; + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; + if (k != 0) { + /*Because if k == 0, sum %= k will throw ArithmeticException.*/ + sum %= k; + } + Integer prev = map.get(sum); + if (prev != null) { + if (i - prev > 1) { + /*This makes sure that it has length at least 2*/ + return true; + } + } else { + map.put(sum, i); + } + } + return false; + } + } + + public static class Solution2 { + /* + * O(n^2), this will time out on LeetCode. + */ + public boolean checkSubarraySum(int[] nums, int k) { + if (nums == null || nums.length == 0) { + return false; + } + + // Two continuous zeroes will form a subarray of length 2 with sum 0, 0*k = 0 will + // always be true + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] == 0 && nums[i + 1] == 0) { + return true; + } + } + + // then k cannot be zero any more + if (k == 0 || nums.length < 2) { + return false; + } + + int[] preSums = new int[nums.length + 1]; + for (int i = 1; i <= nums.length; i++) { + preSums[i] = preSums[i - 1] + nums[i - 1]; + } + + for (int i = 1; i <= nums.length; i++) { + for (int j = 0; j < i - 1; j++) { + if ((preSums[i] - preSums[j]) % k == 0) { + return true; + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_524.java b/src/main/java/com/fishercoder/solutions/firstthousand/_524.java new file mode 100644 index 0000000000..b71c4f3773 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_524.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Collections; +import java.util.List; + +public class _524 { + + public static class Solution1 { + public String findLongestWord(String s, List d) { + Collections.sort( + d, + (a, b) -> a.length() == b.length() ? a.compareTo(b) : b.length() - a.length()); + for (String dictWord : d) { + int i = 0; + for (char c : s.toCharArray()) { + if (i < dictWord.length() && dictWord.charAt(i) == c) { + i++; + } + } + if (i == dictWord.length()) { + return dictWord; + } + } + return ""; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_525.java b/src/main/java/com/fishercoder/solutions/firstthousand/_525.java new file mode 100644 index 0000000000..8298344ac1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_525.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _525 { + + public static class Solution1 { + // credit: https://leetcode.com/articles/contiguous-array/#approach-3-using-hashmap-accepted + public int findMaxLength(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + int count = 0; + int max = 0; + Map map = new HashMap(); + map.put( + 0, + -1); // initialize the map, which means at index zero, the length of contiguous + // subarray is -1 + for (int i = 0; i < nums.length; i++) { + count += nums[i] == 1 ? 1 : -1; + if (map.containsKey(count)) { + max = Math.max(i - map.get(count), max); + } else { + map.put( + count, + i); // only when the map does not have this key, we put it in the map, + // this avoids overwriting the map + // also, it helps us keep the most leftside value in the map to help us compute + // the longest contigous array length + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_526.java b/src/main/java/com/fishercoder/solutions/firstthousand/_526.java new file mode 100644 index 0000000000..2b5bd3bd0f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_526.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +public class _526 { + public static class Solution1 { + /* + * A good post to look at: https://discuss.leetcode.com/topic/79916/java-solution-backtracking + * and there's a generic template afterwards for backtracking problems + */ + + int count = 0; + + public int countArrangement(int N) { + backtracking(N, new int[N + 1], 1); + return count; + } + + private void backtracking(int N, int[] used, int pos) { + if (pos > N) { + count++; + return; + } + + for (int i = 1; i <= N; i++) { + if (used[i] == 0 && (i % pos == 0 || pos % i == 0)) { + used[i] = 1; + backtracking(N, used, pos + 1); + used[i] = 0; + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_527.java b/src/main/java/com/fishercoder/solutions/firstthousand/_527.java new file mode 100644 index 0000000000..226dc3e1a5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_527.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +public class _527 { + + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/82613/really-simple-and-straightforward-java-solution + */ + public List wordsAbbreviation(List dict) { + int len = dict.size(); + String[] ans = new String[len]; + int[] prefix = new int[len]; + for (int i = 0; i < len; i++) { + prefix[i] = 1; + ans[i] = abbreviate(dict.get(i), 1); // make abbreviation for each string + } + for (int i = 0; i < len; i++) { + while (true) { + HashSet set = new HashSet<>(); + for (int j = i + 1; j < len; j++) { + if (ans[j].equals(ans[i])) { + set.add(j); // check all strings with the same abbreviation + } + } + if (set.isEmpty()) { + break; + } + set.add(i); + for (int k : set) { + ans[k] = abbreviate(dict.get(k), ++prefix[k]); // increase the prefix + } + } + } + return Arrays.asList(ans); + } + + private String abbreviate(String word, int k) { + if (k + 2 >= word.length()) { + return word; + } + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(word.substring(0, k)); + stringBuilder.append(word.length() - 1 - k); + stringBuilder.append(word.substring(word.length() - 1)); + return stringBuilder.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_528.java b/src/main/java/com/fishercoder/solutions/firstthousand/_528.java new file mode 100644 index 0000000000..3cf73398a2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_528.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Random; + +public class _528 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/random-pick-with-weight/editorial/ + *

+ * Mental gymnastics (which is explained step by step in the above link): + * 1. picture this to be a ball throwing onto a line from the starting point (0,0); + * 2. where this ball is going to land on the line is a probability problem, i.e. an offset to the starting point (0,0); + * 3. we can use prefix sums array to simulate this line (each value in the array is positive as it represents the weight/probability of it being picked, so cannot be zero or negative); + * 4. we can use Random() to generate a random number called index bounded by the last value of the prefix sums array, i.e. the biggest offset possible; + * 5. then we can use binary search to find where this random number: index, would fit in the prefix sums array. + */ + Random random; + int[] prefixSums; + + public Solution1(int[] w) { + this.random = new Random(); + this.prefixSums = new int[w.length]; + int prefixSum = 0; + for (int i = 0; i < w.length; ++i) { + prefixSum += w[i]; + prefixSums[i] = prefixSum; + } + } + + public int pickIndex() { + int len = prefixSums.length; + int idx = random.nextInt(prefixSums[len - 1]) + 1; + int left = 0; + int right = len - 1; + // search position + while (left < right) { + int mid = left + (right - left) / 2; + if (prefixSums[mid] == idx) { + return mid; + } else if (prefixSums[mid] < idx) { + left = mid + 1; + } else { + right = mid; + } + } + return left; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_529.java b/src/main/java/com/fishercoder/solutions/firstthousand/_529.java new file mode 100644 index 0000000000..f1c2bf466c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_529.java @@ -0,0 +1,72 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _529 { + public static class Solution1 { + public char[][] updateBoard(char[][] board, int[] click) { + int m = board.length; + int n = board[0].length; + Queue queue = new LinkedList(); + queue.offer(click); + while (!queue.isEmpty()) { + int[] curr = queue.poll(); + int currRow = curr[0]; + int currCol = curr[1]; + if (board[currRow][currCol] == 'M') { + board[currRow][currCol] = 'X'; + } else { + /*checks all eight neighbors of this curr cell, count all mines, this includes 'X' and 'M' */ + int count = 0; + for (int i = -1; i < 2; i++) { + for (int j = -1; j < 2; j++) { + if (i == 0 && j == 0) { + // this is the curr cell itself, so we skip + continue; + } + int nextRow = currRow + i; + int nextCol = currCol + j; + if (nextRow >= m || nextRow < 0 || nextCol >= n || nextCol < 0) { + continue; + } + if (board[nextRow][nextCol] == 'M' || board[nextRow][nextCol] == 'X') { + count++; + } + } + } + + if (count > 0) { + /*There are mines around this cell, so update it with the number of mines*/ + board[currRow][currCol] = (char) (count + '0'); + } else { + /*There is no mines around this cell, so update it to be 'B'*/ + board[currRow][currCol] = 'B'; + + /*then we'll also check all of its eight surrounding cells, if it's 'E'. we'll also update it to be 'B' and offer it into the queue + * Only when we know this is a 'B', we'll offer into the queue, so below check could only happen here, not in the previous nested for loop.*/ + for (int i = -1; i < 2; i++) { + for (int j = -1; j < 2; j++) { + if (i == 0 && j == 0) { + continue; + } + int nextRow = currRow + i; + int nextCol = currCol + j; + if (nextRow >= m || nextRow < 0 || nextCol >= n || nextCol < 0) { + continue; + } + if (board[nextRow][nextCol] == 'E') { + /*we offer 'E' cells into the queue*/ + queue.offer(new int[] {nextRow, nextCol}); + /*then update this cell to be 'B' */ + board[nextRow][nextCol] = 'B'; + } + } + } + } + } + } + return board; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_53.java b/src/main/java/com/fishercoder/solutions/firstthousand/_53.java new file mode 100644 index 0000000000..03adcca526 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_53.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _53 { + + public static class Solution1 { + /* + * Kadane's algorithm. + *

+ * It boils down to: + * whether it's worth to include the current element into the sum or not. + *

+ * It'll be very straightforward once you draw this array [-2,1,-3,4,-1,2,1,-5,4] out on a paper and walk through it. + */ + public int maxSubArray(int[] nums) { + int globalMax = nums[0]; + int currentMax = nums[0]; + for (int i = 1; i < nums.length; i++) { + currentMax = Math.max(nums[i], currentMax + nums[i]); + globalMax = Math.max(currentMax, globalMax); + } + return globalMax; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_530.java b/src/main/java/com/fishercoder/solutions/firstthousand/_530.java new file mode 100644 index 0000000000..1d16fc4374 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_530.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.Iterator; +import java.util.TreeSet; + +public class _530 { + + public static class Solution1 { + public int getMinimumDifference(TreeNode root) { + TreeSet treeset = new TreeSet<>(); + treeset.add(root.val); + dfs(root, treeset); + int diff = Integer.MAX_VALUE; + Iterator iterator = treeset.iterator(); + int prev = iterator.next(); + while (iterator.hasNext()) { + int current = iterator.next(); + diff = Math.min(diff, Math.abs(current - prev)); + prev = current; + } + return diff; + } + + private void dfs(TreeNode root, TreeSet treeset) { + if (root.left != null) { + treeset.add(root.left.val); + dfs(root.left, treeset); + } + if (root.right != null) { + treeset.add(root.right.val); + dfs(root.right, treeset); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_531.java b/src/main/java/com/fishercoder/solutions/firstthousand/_531.java new file mode 100644 index 0000000000..083c28b306 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_531.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +public class _531 { + + public static class Solution1 { + public int findLonelyPixel(char[][] picture) { + int m = picture.length; + int n = picture[0].length; + int count = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (picture[i][j] == 'B' && isLonely(i, j, picture, m, n)) { + count++; + } + } + } + return count; + } + + private boolean isLonely(int row, int col, char[][] picture, int m, int n) { + for (int i = 0; i < m; i++) { + if (i != row) { + if (picture[i][col] == 'B') { + return false; + } + } + } + + for (int j = 0; j < n; j++) { + if (j != col) { + if (picture[row][j] == 'B') { + return false; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_532.java b/src/main/java/com/fishercoder/solutions/firstthousand/_532.java new file mode 100644 index 0000000000..c8bec96018 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_532.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _532 { + + public static class Solution1 { + public int findPairs(int[] nums, int k) { + if (nums == null || nums.length == 0 || k < 0) { + return 0; + } + + Map map = new HashMap(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + + int answer = 0; + for (int key : map.keySet()) { + if (k == 0) { + if (map.get(key) >= 2) { + answer++; + } + } else { + if (map.containsKey(key + k)) { + answer++; + } + } + } + return answer; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_533.java b/src/main/java/com/fishercoder/solutions/firstthousand/_533.java new file mode 100644 index 0000000000..2d26da30f9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_533.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _533 { + public static class Solution1 { + /* + * Credit: https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/5 + * This program is very well designed to do things: + * 1. it scans the entire matrix once, but does two things in this scan: + * first it records an array of cols that keeps count of how many 'B' are there in each column; + * second, at the end of traversing each column, it puts this entire row as the key into a HashMap + * when there N number of 'B's in this row. + * 2. then we could go through the HashMap keyset: + * if one row has N number of 'B's, we go through this row's each column to see if any element in this row is 'B' and also that element's column has N 'B's + */ + public int findBlackPixel(char[][] picture, int N) { + if (picture == null || picture.length == 0 || picture[0].length == 0) { + return 0; + } + int m = picture.length; + int n = picture[0].length; + int[] cols = new int[n]; + Map map = new HashMap<>(); + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < m; i++) { + int count = 0; + for (int j = 0; j < n; j++) { + if (picture[i][j] == 'B') { + count++; + cols[j]++; + } + stringBuilder.append(picture[i][j]); + } + if (count == N) { + /*we use this entire row string as key for the map*/ + map.put( + stringBuilder.toString(), + map.getOrDefault(stringBuilder.toString(), 0) + 1); + } + stringBuilder.setLength(0); + } + + int answer = 0; + for (String key : map.keySet()) { + if (map.get(key) != N) { + continue; + } + for (int i = 0; i < n; i++) { + if (key.charAt(i) == 'B' && cols[i] == N) { + answer += N; + } + } + } + return answer; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_535.java b/src/main/java/com/fishercoder/solutions/firstthousand/_535.java new file mode 100644 index 0000000000..b4d702ba14 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_535.java @@ -0,0 +1,125 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +public class _535 { + + public static class Solution1 { + /* + * Simple counter approach + * Analysis: + * The range of URLs that can be decoded is limited by the range of Integer. + * If excessively large number of URLs have to be encoded, after the range of Integer is exceeded, + * integer overflow could lead to overwriting previous URL's encodings. + * The length of the URL isn't necessary shorter than incoming URL. + * One potential security issue with this problem is that it's very easy to predict the next code generated, + * since this pattern is very easy to be detected. + */ + public class Codec { + int i = 0; + Map map = new HashMap(); + public static final String PREFIX = "http://tinyurl/"; + + public String encode(String longUrl) { + map.put(i, longUrl); + return PREFIX + i++; + } + + public String decode(String shortUrl) { + return map.get(Integer.parseInt(shortUrl.substring(PREFIX.length()))); + } + } + } + + public static class Solution2 { + /* + * Use Java built-in HashCode + * Analysis: + * hashCode() does NOT generate unique codes for different strings, collision might happen. + * As the number of URLs increase, the probability of getting collision increases which leads to failure. + */ + public class Codec { + Map map = new HashMap<>(); + public static final String PREFIX = "http://tinyurl/"; + + // Encodes a URL to a shortened URL. + public String encode(String longUrl) { + /*I don't need to create a local variable to cache longUrl.hashCode() + * since Java's String cache it already. :) Look at its source code.*/ + map.put(longUrl.hashCode(), longUrl); + return PREFIX + longUrl.hashCode(); + } + + // Decodes a shortened URL to its original URL. + public String decode(String shortUrl) { + return map.get(Integer.parseInt(shortUrl.substring(PREFIX.length()))); + } + } + } + + public static class Solution3 { + /* + * Use a random number + */ + Map map = new HashMap<>(); + Random random = new Random(); + public static final String PREFIX = "http://tinyurl/"; + + // Encodes a URL to a shortened URL. + public String encode(String longUrl) { + int num = random.nextInt(Integer.MAX_VALUE); + while (map.containsKey(num)) { + num = random.nextInt(Integer.MAX_VALUE); + } + map.put(num, longUrl); + return PREFIX + num; + } + + // Decodes a shortened URL to its original URL. + public String decode(String shortUrl) { + return map.get(Integer.parseInt(shortUrl.substring(PREFIX.length()))); + } + } + + public static class Solution4 { + /* + * Use a random but fixed length encoding + * Analysis: + * 1. This is the most optimal solution so far. + * 2. The number of URLs that can be encoded can be as big as Math.pow((10 + 26*2), FIXED_LENGTH), in below code, FIXED_LENGTH = 7 + * 3. The length of the shortened URL is fixed at a certain length, which could be a significant reduce for large URLs + * 4. The performance of this scheme is pretty good, due to much smaller probability of encountering collision + * 5. Predicting pattern/encoding isn't possible in this case since random numbers are used. + */ + Map map = new HashMap<>(); + public static final String PREFIX = "http://tinyurl/"; + public static final int FIXED_LENGTH = 7; + Random random = new Random(); + String alphabet = "0123456789abcdefghijklmnopgrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + // Encodes a URL to a shortened URL. + public String encode(String longUrl) { + String shortKey = getRandomFixedLengthKey(); + while (map.containsKey(shortKey)) { + shortKey = getRandomFixedLengthKey(); + } + map.put(shortKey, longUrl); + return PREFIX + shortKey; + } + + private String getRandomFixedLengthKey() { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < FIXED_LENGTH; i++) { + stringBuilder.append(alphabet.charAt(random.nextInt(alphabet.length()))); + } + return stringBuilder.toString(); + } + + // Decodes a shortened URL to its original URL. + public String decode(String shortUrl) { + return map.get(shortUrl.substring(PREFIX.length())); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_536.java b/src/main/java/com/fishercoder/solutions/firstthousand/_536.java new file mode 100644 index 0000000000..0c91bb95fe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_536.java @@ -0,0 +1,74 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayDeque; +import java.util.Deque; + +public class _536 { + + public static class Solution1 { + /* + * recursive solution + */ + public TreeNode str2tree(String s) { + if (s.equals("")) { + return null; + } + int firstParen = s.indexOf("("); + int val = + firstParen == -1 + ? Integer.parseInt(s) + : Integer.parseInt(s.substring(0, firstParen)); + TreeNode cur = new TreeNode(val); + if (firstParen == -1) { + return cur; + } + int start = firstParen; + int leftParenCount = 0; + for (int i = start; i < s.length(); i++) { + if (s.charAt(i) == '(') { + leftParenCount++; + } else if (s.charAt(i) == ')') { + leftParenCount--; + } + if (leftParenCount == 0 && start == firstParen) { + cur.left = str2tree(s.substring(start + 1, i)); + start = i + 1; + } else if (leftParenCount == 0) { + cur.right = str2tree(s.substring(start + 1, i)); + } + } + return cur; + } + } + + public static class Solution2 { + /* + * iterative solution + */ + public TreeNode str2tree(String s) { + Deque stack = new ArrayDeque<>(); + for (int i = 0, j = i; i < s.length(); i++, j = i) { + char c = s.charAt(i); + if (c == ')') { + stack.pop(); + } else if (c >= '0' && c <= '9' || c == '-') { + while (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') { + i++; + } + TreeNode curr = new TreeNode(Integer.valueOf(s.substring(j, i + 1))); + if (!stack.isEmpty()) { + TreeNode parent = stack.peek(); + if (parent.left != null) { + parent.right = curr; + } else { + parent.left = curr; + } + } + stack.push(curr); + } + } + return stack.isEmpty() ? null : stack.peek(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_537.java b/src/main/java/com/fishercoder/solutions/firstthousand/_537.java new file mode 100644 index 0000000000..aa9c6e276c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_537.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.stream.Stream; + +public class _537 { + + public static class Solution1 { + public String complexNumberMultiply(String a, String b) { + String[] part1And2 = a.split("\\+"); + String[] part3And4 = b.split("\\+"); + String product1 = + String.valueOf( + Integer.parseInt(part1And2[0]) + * Integer.parseInt( + part3And4[0])); // this is real number multiplication + String product2 = multiply(part1And2[0], part3And4[1]); + String product3 = multiply(part3And4[0], part1And2[1]); + String product4 = multiplyTwoIs(part3And4[1], part1And2[1]); + String twoISum = sumTwoI(product2, product3); + String numberValue = + String.valueOf(Integer.valueOf(product1) + Integer.valueOf(product4)); + return numberValue + "+" + twoISum; + } + + private String sumTwoI(String product2, String product3) { + int number2 = Integer.parseInt(product2.substring(0, product2.length() - 1)); + int number3 = Integer.parseInt(product3.substring(0, product3.length() - 1)); + return String.valueOf(number2 + number3) + "i"; + } + + private String multiplyTwoIs(String p, String q) { + int number1 = Integer.parseInt(p.substring(0, p.length() - 1)); + int number2 = Integer.parseInt(q.substring(0, q.length() - 1)); + int numberProduct = number1 * number2; + return String.valueOf(-numberProduct); + } + + private String multiply(String p, String withI) { + int numberPart = Integer.parseInt(withI.substring(0, withI.length() - 1)); + return String.valueOf(numberPart * Integer.valueOf(p)) + "i"; + } + } + + public static class Solution2 { + /* + * (a + bi) * (c + di) could become (ac - bd) + (ad + bc)*i + * Thus, we have the following function + */ + public String complexNumberMultiply(String a, String b) { + int[] coefficients1 = Stream.of(a.split("\\+|i")).mapToInt(Integer::parseInt).toArray(); + int[] coefficients2 = Stream.of(b.split("\\+|i")).mapToInt(Integer::parseInt).toArray(); + return (coefficients1[0] * coefficients2[0] - coefficients1[1] * coefficients2[1]) + + "+" + + (coefficients1[0] * coefficients2[1] + + coefficients1[1] * coefficients2[0] + + "i"); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_538.java b/src/main/java/com/fishercoder/solutions/firstthousand/_538.java similarity index 78% rename from src/main/java/com/fishercoder/solutions/_538.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_538.java index 16e4b21cb1..ed513a488e 100644 --- a/src/main/java/com/fishercoder/solutions/_538.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_538.java @@ -1,35 +1,15 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; - import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.TreeMap; -/** - * 538. Convert BST to Greater Tree - * - * Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed - * to the original key plus sum of all keys greater than the original key in BST. - - Example: - - Input: The root of a Binary Search Tree like this: - 5 - / \ - 2 13 - - Output: The root of a Greater Tree like this: - 18 - / \ - 20 13 - */ - public class _538 { public static class Solution1 { - /** + /* * Traverse in this order: right -> root -> left */ public TreeNode convertBST(TreeNode root) { @@ -47,7 +27,7 @@ private int dfs(TreeNode root, int val) { } public static class Solution2 { - //This solution is generic for both BST and regular binary trees + // This solution is generic for both BST and regular binary trees public TreeNode convertBST(TreeNode root) { if (root == null) { return root; @@ -68,7 +48,8 @@ public TreeNode convertBST(TreeNode root) { return generateResultRoot(root, treeMap, result); } - private TreeNode generateResultRoot(TreeNode root, TreeMap treeMap, TreeNode result) { + private TreeNode generateResultRoot( + TreeNode root, TreeMap treeMap, TreeNode result) { if (root != null) { result.val = treeMap.get(root.val) + root.val; } @@ -95,5 +76,4 @@ private void putNodeToList(List list, TreeNode root) { } } } - -} \ No newline at end of file +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_539.java b/src/main/java/com/fishercoder/solutions/firstthousand/_539.java new file mode 100644 index 0000000000..6a0acb9b39 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_539.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.List; + +public class _539 { + + public static class Soluiton1 { + + public int findMinDifference(List timePoints) { + /*there are in total 24*60 = 1440 possible time points*/ + final int ALL_POSSIBLE_TIMEPOINTS = 1440; + boolean[] allTimePoints = new boolean[ALL_POSSIBLE_TIMEPOINTS]; + for (String eachTime : timePoints) { + String[] timeParts = eachTime.split(":"); + int hour = Integer.valueOf(timeParts[0]); + int minute = Integer.valueOf(timeParts[1]); + int value = hour * 60 + minute; + if (allTimePoints[value]) { + return 0; + } + allTimePoints[value] = true; + } + + int min = Integer.MAX_VALUE; + int prev = 0; + int first = Integer.MAX_VALUE; + int last = Integer.MIN_VALUE; + for (int i = 0; i < ALL_POSSIBLE_TIMEPOINTS; i++) { + if (allTimePoints[i]) { + if (first != Integer.MAX_VALUE) { + min = Math.min(min, i - prev); + } + first = Math.min(first, i); + last = Math.max(last, i); + prev = i; + } + } + min = Math.min(min, (ALL_POSSIBLE_TIMEPOINTS - last + first)); + return min; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_54.java b/src/main/java/com/fishercoder/solutions/firstthousand/_54.java new file mode 100644 index 0000000000..ae4f167ebc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_54.java @@ -0,0 +1,95 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _54 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/spiral-matrix/discuss/20599/Super-Simple-and-Easy-to-Understand-Solution/185257 + */ + public List spiralOrder(int[][] matrix) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return new ArrayList<>(); + } + int m = matrix.length; + int n = matrix[0].length; + List result = new ArrayList(); + int left = 0; + int right = n - 1; + int top = 0; + int bottom = m - 1; + while (result.size() < m * n) { + for (int j = left; j <= right && result.size() < m * n; j++) { + result.add(matrix[top][j]); + } + top++; + for (int i = top; i <= bottom && result.size() < m * n; i++) { + result.add(matrix[i][right]); + } + right--; + for (int j = right; j >= left && result.size() < m * n; j--) { + result.add(matrix[bottom][j]); + } + bottom--; + for (int i = bottom; i >= top && result.size() < m * n; i--) { + result.add(matrix[i][left]); + } + left++; + } + return result; + } + } + + public static class Solution2 { + /* + * My completely original solution on 12/29/2021. + */ + public List spiralOrder(int[][] matrix) { + List ans = new ArrayList<>(); + int m = matrix.length; + int n = matrix[0].length; + int direction = 0; + int total = m * n; + int j = 0; + int i = 0; + int lowerRow = 0; + int lowerCol = 0; + while (ans.size() < total) { + for (; i < m && i >= lowerRow && j < n && j >= lowerCol; ) { + ans.add(matrix[i][j]); + if (direction == 0) { // east + j++; + } else if (direction == 1) { // south + i++; + } else if (direction == 2) { // west + j--; + } else { + i--; // north + } + } + if (direction == 0) { + i++; + j--; + } else if (direction == 1) { + j--; + i--; + } else if (direction == 2) { + j++; + i--; + lowerRow++; + m--; + } else { + i++; + j++; + lowerCol++; + n--; + } + direction++; + direction %= 4; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_540.java b/src/main/java/com/fishercoder/solutions/firstthousand/_540.java new file mode 100644 index 0000000000..08c58a3649 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_540.java @@ -0,0 +1,65 @@ +package com.fishercoder.solutions.firstthousand; + +public class _540 { + public static class Solution1 { + public int singleNonDuplicate(int[] nums) { + int result = 0; + for (int i = 0; i < nums.length; i++) { + result ^= nums[i]; + } + return result; + } + } + + public static class Solution2 { + public int singleNonDuplicate(int[] nums) { + int start = 0; + int end = nums.length - 1; + while (start < end) { + int mid = start + (end - start) / 2; + if (mid + 1 < nums.length + && nums[mid] != nums[mid + 1] + && mid - 1 >= 0 + && nums[mid] != nums[mid - 1]) { + return nums[mid]; + } else if (mid + 1 < nums.length && nums[mid] == nums[mid + 1] && mid % 2 == 0) { + start = mid + 1; + } else if (mid - 1 >= 0 && nums[mid] == nums[mid - 1] && mid % 2 == 1) { + start = mid + 1; + } else { + end = mid - 1; + } + } + return nums[start]; + } + } + + public static class Solution3 { + public int singleNonDuplicate(int[] nums) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] != nums[mid + 1] && nums[mid] != nums[mid - 1]) { + return nums[mid]; + } + if (nums[mid] != nums[mid + 1]) { + if ((right - mid) % 2 == 0) { + if (right != mid) { + right = mid; + } else { + return nums[mid]; + } + } else { + left = mid + 1; + } + } else if ((right - mid) % 2 == 0 && right != mid) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return nums[left]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_541.java b/src/main/java/com/fishercoder/solutions/firstthousand/_541.java new file mode 100644 index 0000000000..f2b795d0d4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_541.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _541 { + + public static class Solution1 { + public String reverseStr(String s, int k) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < s.length(); i = i + 2 * k) { + if (s.length() >= (i + k)) { + stringBuilder.append(new StringBuilder(s.substring(i, i + k)).reverse()); + } else { + stringBuilder.append(new StringBuilder(s.substring(i)).reverse()); + break; + } + if ((i + 2 * k) <= s.length()) { + stringBuilder.append(s.substring(i + k, i + 2 * k)); + } else { + stringBuilder.append(s.substring(i + k)); + } + } + return stringBuilder.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_542.java b/src/main/java/com/fishercoder/solutions/firstthousand/_542.java new file mode 100644 index 0000000000..6b152280e8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_542.java @@ -0,0 +1,96 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.Queue; + +public class _542 { + + public static class Solution1 { + public int[][] updateMatrix(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + int[][] ans = new int[m][n]; + Deque deque = new LinkedList<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (mat[i][j] == 0) { + deque.offer(new int[] {i, j}); + } else { + ans[i][j] = m * n; + } + } + } + int[] directions = new int[] {0, 1, 0, -1, 0}; + while (!deque.isEmpty()) { + int[] curr = deque.poll(); + for (int i = 0; i < directions.length - 1; i++) { + int nextX = directions[i] + curr[0]; + int nextY = directions[i + 1] + curr[1]; + if (nextX >= 0 + && nextX < m + && nextY >= 0 + && nextY < n + && ans[nextX][nextY] > ans[curr[0]][curr[1]] + 1) { + deque.offer(new int[] {nextX, nextY}); + ans[nextX][nextY] = ans[curr[0]][curr[1]] + 1; + } + } + } + return ans; + } + } + + public static class Solution2 { + /* + * A silly, but working solution. Apparently, the above BFS approach is a smarter version of this one. + */ + public int[][] updateMatrix(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + int[][] ans = new int[m][n]; + Queue queue = new LinkedList<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (mat[i][j] == 0) { + queue.offer(new int[] {i, j}); + } else { + ans[i][j] = m * n; + } + } + } + while (!queue.isEmpty()) { + int[] curr = queue.poll(); + for (int i = curr[0] + 1, j = curr[1]; i < m && mat[i][j] != 0; i++) { + ans[i][j] = Math.min(ans[i][j], i - curr[0]); + } + for (int i = curr[0] - 1, j = curr[1]; i >= 0 && mat[i][j] != 0; i--) { + ans[i][j] = Math.min(ans[i][j], curr[0] - i); + } + for (int j = curr[1] + 1, i = curr[0]; j < n && mat[i][j] != 0; j++) { + ans[i][j] = Math.min(ans[i][j], j - curr[1]); + } + for (int j = curr[1] - 1, i = curr[0]; j >= 0 && mat[i][j] != 0; j--) { + ans[i][j] = Math.min(ans[i][j], curr[1] - j); + } + } + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (i + 1 < m && ans[i + 1][j] >= 1) { + ans[i][j] = Math.min(ans[i][j], ans[i + 1][j] + 1); + } + if (i - 1 >= 0 && ans[i - 1][j] >= 1) { + ans[i][j] = Math.min(ans[i][j], ans[i - 1][j] + 1); + } + if (j + 1 < n && ans[i][j + 1] >= 1) { + ans[i][j] = Math.min(ans[i][j], ans[i][j + 1] + 1); + } + if (j - 1 >= 0 && ans[i][j - 1] >= 1) { + ans[i][j] = Math.min(ans[i][j], ans[i][j - 1] + 1); + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_543.java b/src/main/java/com/fishercoder/solutions/firstthousand/_543.java new file mode 100644 index 0000000000..4d18752d56 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_543.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _543 { + + public static class Solution1 { + /* + * A great observation of this problem is that the longest path must exist between two leaf nodes, + * since it's easy to prove its opposite is not the longest by simply adding one to reach its leaf node. + * + *

+ * This is a very great problem for practicing recursion: + * 1. What dfs() returns is the max height it should pick from either its left or right subtree, that's + * what the int return type stands for; + * 2. And during the recursion, we can keep updating the global variable: "diameter"; + * 3. When computing length/height of a subtree, we should take the max of its left and right, then plus one + * and left height should be like this + * int left = dfs(root.left); + * instead of dfs(root.left) + 1; + * we'll only plus one at the end + */ + int diameter = 0; + + public int diameterOfBinaryTree(TreeNode root) { + dfs(root); + return diameter; + } + + private int dfs(TreeNode root) { + if (root == null) { + return 0; + } + int leftPath = dfs(root.left); + int rightPath = dfs(root.right); + diameter = Math.max(diameter, leftPath + rightPath); + return Math.max(leftPath, rightPath) + 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_544.java b/src/main/java/com/fishercoder/solutions/firstthousand/_544.java new file mode 100644 index 0000000000..07f461523d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_544.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _544 { + + public String findContestMatch(int n) { + List pairs = new ArrayList<>(); + int left = 1; + int right = n; + while (left < right) { + pairs.add("(" + left + "," + right + ")"); + left++; + right--; + } + if (n == 2) { + return pairs.get(0); + } + return generateFinal(pairs, n / 2); + } + + private String generateFinal(List pairs, int n) { + if (n > 2) { + int size = pairs.size(); + int left = 0; + int right = size - 1; + List newPairs = new ArrayList<>(); + while (left < right) { + String newPair = "(" + pairs.get(left) + "," + pairs.get(right) + ")"; + newPairs.add(newPair); + left++; + right--; + } + return generateFinal(newPairs, n / 2); + } + return "(" + pairs.get(0) + "," + pairs.get(1) + ")"; + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_545.java b/src/main/java/com/fishercoder/solutions/firstthousand/_545.java new file mode 100644 index 0000000000..b0767909c2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_545.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _545 { + public static class Solution1 { + public List boundaryOfBinaryTree(TreeNode root) { + List nodes = new ArrayList<>(); + if (root == null) { + return nodes; + } + + nodes.add(root.val); + leftBoundary(root.left, nodes); + addLeaves(root.left, nodes); + addLeaves(root.right, nodes); + rightBoundary(root.right, nodes); + return nodes; + } + + public void leftBoundary(TreeNode root, List nodes) { + if (root == null || (root.left == null && root.right == null)) { + /*we don't want to add any LEAVES in leftBoundary and rightBoundary functions either, + * that's why we have the later condition in the if branch.*/ + return; + } + nodes.add(root.val); // add BEFORE child visit + if (root.left == null) { + leftBoundary(root.right, nodes); + } else { + leftBoundary(root.left, nodes); + } + } + + public void rightBoundary(TreeNode root, List nodes) { + if (root == null || (root.right == null && root.left == null)) { + return; + } + if (root.right == null) { + rightBoundary(root.left, nodes); + } else { + rightBoundary(root.right, nodes); + } + nodes.add(root.val); // add AFTER child visit(reverse) + } + + public void addLeaves(TreeNode root, List nodes) { + if (root == null) { + return; + } + if (root.left == null && root.right == null) { + nodes.add(root.val); + return; + } + addLeaves(root.left, nodes); + addLeaves(root.right, nodes); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_546.java b/src/main/java/com/fishercoder/solutions/firstthousand/_546.java new file mode 100644 index 0000000000..f2489d3237 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_546.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +public class _546 { + public static class Solution1 { + /* + * credit: https://leetcode.com/articles/remove-boxes/#approach-2-using-dp-with-memorizationaccepted + *

+ * For an entry in dp[l][r][k], l represents the starting index of the subarray, + * r represents the ending index of the subarray + * and k represents the number of elements similar to the r​th element + * following it which can be combined to obtain the point information to be stored in dp[l][r][k]. + */ + public int removeBoxes(int[] boxes) { + int[][][] dp = new int[100][100][100]; + return calculatePoints(boxes, dp, 0, boxes.length - 1, 0); + } + + public int calculatePoints(int[] boxes, int[][][] dp, int l, int r, int k) { + if (l > r) { + return 0; + } + if (dp[l][r][k] != 0) { + return dp[l][r][k]; + } + while (r > l && boxes[r] == boxes[r - 1]) { + r--; + k++; + } + dp[l][r][k] = calculatePoints(boxes, dp, l, r - 1, 0) + (k + 1) * (k + 1); + for (int i = l; i < r; i++) { + if (boxes[i] == boxes[r]) { + dp[l][r][k] = + Math.max( + dp[l][r][k], + calculatePoints(boxes, dp, l, i, k + 1) + + calculatePoints(boxes, dp, i + 1, r - 1, 0)); + } + } + return dp[l][r][k]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_547.java b/src/main/java/com/fishercoder/solutions/firstthousand/_547.java new file mode 100644 index 0000000000..2259d0cef5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_547.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.firstthousand; + +public class _547 { + + public static class Solution1 { + public int findCircleNum(int[][] isConnected) { + if (isConnected == null || isConnected.length == 0 || isConnected[0].length == 0) { + return 0; + } + int m = isConnected.length; // number of rows in this matrix + UnionFind unionFind = new UnionFind(m); + for (int i = 0; i < m; i++) { + for (int j = i + 1; j < m; j++) { + if (isConnected[i][j] == 1) { + unionFind.union(i, j); + } + } + } + return unionFind.count; + } + + static class UnionFind { + int count; + int[] root; + + public UnionFind(int m) { + root = new int[m]; + for (int i = 0; i < m; i++) { + root[i] = i; + } + count = m; + } + + public void union(int i, int j) { + int x = find(root, i); + int y = find(root, j); + // at this point, x and y should equal, if not, then we should union them into the + // same value + if (x != y) { + count--; + root[x] = y; // path compression, i.e. union + } + } + + public int find(int[] ids, int i) { + if (ids[i] == i) { + // this is the base case, so nothing to recurse on + return i; + } + return find(ids, ids[i]); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_548.java b/src/main/java/com/fishercoder/solutions/firstthousand/_548.java new file mode 100644 index 0000000000..d48e40be1b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_548.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _548 { + + public static class Solution1 { + public boolean splitArray(int[] nums) { + int len = nums.length; + if (len < 7) { + return false; + } + int[] sum = new int[len]; + sum[0] = nums[0]; + for (int i = 1; i < len; i++) { + sum[i] = sum[i - 1] + nums[i]; + } + for (int j = 3; j < len - 3; j++) { + Set set = new HashSet<>(); + for (int i = 1; i < j - 1; i++) { + if (sum[i - 1] == sum[j - 1] - sum[i]) { + /*this is sum(0, i-1) and sum(i+1, j-1)*/ + set.add(sum[i - 1]); + } + } + for (int k = j + 2; k < len - 1; k++) { + if (sum[k - 1] - sum[j] == sum[len - 1] - sum[k] + && set.contains(sum[k - 1] - sum[j])) { + /*this is sum(j+1, k-1) and sum(k+1, len-1)*/ + return true; + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_549.java b/src/main/java/com/fishercoder/solutions/firstthousand/_549.java new file mode 100644 index 0000000000..3f700d907b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_549.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _549 { + + public static class Solution1 { + int max = 0; + + public int longestConsecutive(TreeNode root) { + longestPath(root); + return max; + } + + private int[] longestPath(TreeNode root) { + if (root == null) { + return new int[] {0, 0}; + } + int increasing = 1; + int decreasing = 1; + if (root.left != null) { + int[] left = longestPath(root.left); + if (root.val == root.left.val + 1) { + decreasing = left[1] + 1; + } else if (root.val == root.left.val - 1) { + increasing = left[0] + 1; + } + } + + if (root.right != null) { + int[] right = longestPath(root.right); + if (root.val == root.right.val + 1) { + decreasing = Math.max(right[1] + 1, decreasing); + } else if (root.val == root.right.val - 1) { + increasing = Math.max(right[0] + 1, increasing); + } + } + + max = Math.max(max, decreasing + increasing - 1); + return new int[] {increasing, decreasing}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_55.java b/src/main/java/com/fishercoder/solutions/firstthousand/_55.java new file mode 100644 index 0000000000..9d4a5aa104 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_55.java @@ -0,0 +1,103 @@ +package com.fishercoder.solutions.firstthousand; + +public class _55 { + + public static class Solution1 { + /* + * My very original but lengthy solution. + */ + public boolean canJump(int[] nums) { + int furthestReach = nums[0]; + if (furthestReach >= nums.length - 1) { + return true; + } + int i = 1; + for (; i < nums.length; ) { + int newFurthestReach = -1; + while (i <= furthestReach) { + newFurthestReach = Math.max(newFurthestReach, nums[i] + i); + if (newFurthestReach >= nums.length) { + return true; + } + i++; + } + if (newFurthestReach <= furthestReach) { + return false; + } else if (newFurthestReach >= nums.length - 1) { + return true; + } else { + furthestReach = newFurthestReach; + } + } + return false; + } + } + + public static class Solution2 { + /* + * The same idea as mine above, but much more concise. + * Credit: https://leetcode.com/problems/jump-game/discuss/20917/Linear-and-simple-solution-in-C%2B%2B + */ + public boolean canJump(int[] nums) { + int i = 0; + for (int reach = 0; i < nums.length && i <= reach; i++) { + reach = Math.max(reach, nums[i] + i); + } + return i >= nums.length; + } + } + + public static class Solution3 { + /* + * Top-down DP. + * Credit: https://leetcode.com/problems/jump-game/solution/ approach 2 + *

+ * Specifically, for this problem, my very own Solution1 and the above Solution2 run much faster than this DP solution, likely due to this is top-down, there's stack overhead. + * But just use this problem to practice DP. + *

+ * The reason it's called top-down is that it's filling the dp array from the right to the left if you set break points and step through this. + */ + public boolean canJump(int[] nums) { + int[] dp = new int[nums.length]; + // 0 means unknown, 1 means reachable, 2 means unreachable + dp[nums.length - 1] = 1; + return canJumpFrom(0, nums, dp); + } + + private boolean canJumpFrom(int index, int[] nums, int[] dp) { + if (dp[index] != 0) { + return dp[index] == 1; + } + int furthestReach = Math.min(index + nums[index], nums.length - 1); + for (int i = index + 1; i <= furthestReach; i++) { + if (canJumpFrom(i, nums, dp)) { + dp[i] = 1; + return true; + } + } + dp[index] = 2; + return false; + } + } + + public static class Solution4 { + /* + * This is bottom-up DP. + */ + public boolean canJump(int[] nums) { + int[] dp = new int[nums.length]; + // 0 means unknown, 1 means reachable, 2 means unreachable + dp[nums.length - 1] = 1; + for (int i = nums.length - 2; i >= 0; i--) { + int furthestReach = Math.min(nums[i] + i, nums.length - 1); + for (int j = i + 1; j <= furthestReach; j++) { + if (dp[j] == 1) { + dp[i] = 1; + break; + } + } + } + return dp[0] == 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_551.java b/src/main/java/com/fishercoder/solutions/firstthousand/_551.java new file mode 100644 index 0000000000..ed105699cb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_551.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +public class _551 { + + public static class Solution1 { + public boolean checkRecord(String s) { + int aCount = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == 'A') { + aCount++; + if (aCount > 1) { + return false; + } + } else if (s.charAt(i) == 'L') { + int continuousLCount = 0; + while (i < s.length() && s.charAt(i) == 'L') { + i++; + continuousLCount++; + if (continuousLCount > 2) { + return false; + } + } + i--; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_552.java b/src/main/java/com/fishercoder/solutions/firstthousand/_552.java new file mode 100644 index 0000000000..f293394c7a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_552.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +public class _552 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/86526/improving-the-runtime-from-o-n-to-o-log-n + */ + public int checkRecord(int n) { + final int MOD = 1000000007; + int[][][] f = new int[n + 1][2][3]; + + f[0] = new int[][] {{1, 1, 1}, {1, 1, 1}}; + for (int i = 1; i <= n; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < 3; k++) { + int val = f[i - 1][j][2]; // ...P + if (j > 0) { + val = (val + f[i - 1][j - 1][2]) % MOD; // ...A + } + if (k > 0) { + val = (val + f[i - 1][j][k - 1]) % MOD; // ...L + } + f[i][j][k] = val; + } + } + } + return f[n][1][2]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_553.java b/src/main/java/com/fishercoder/solutions/firstthousand/_553.java new file mode 100644 index 0000000000..1f7c6882a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_553.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.StringJoiner; + +public class _553 { + public static class Solution1 { + /* + * Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_553.java + */ + public String optimalDivision(int[] nums) { + /*https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html: + * StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. + * The String "[George:Sally:Fred]" may be constructed as follows: + StringJoiner sj = new StringJoiner(":", "[", "]"); + sj.add("George").add("Sally").add("Fred"); + String desiredString = sj.toString();*/ + + if (nums.length == 1) { + return "" + nums[0]; + } + if (nums.length == 2) { + return nums[0] + "/" + nums[1]; + } + + /*Tricky one: the solution is fixed: always wrap the one from the second until the last. + * Another important thing to note that such way could work is that: + * the prerequisite is: Elements will be in range [2,1000], so no elements are smaller than 1.*/ + StringJoiner stringJoiner = new StringJoiner("/"); + for (int i = 1; i < nums.length; i++) { + stringJoiner.add("" + nums[i]); + } + return String.format("%d/(%s)", nums[0], stringJoiner.toString()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_554.java b/src/main/java/com/fishercoder/solutions/firstthousand/_554.java new file mode 100644 index 0000000000..c1ca25b1aa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_554.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _554 { + public static class Solution1 { + /* + * credit: https://leetcode.com/articles/brick-wall/ + *

+ * we make use of a HashMap + * map which is used to store entries in the form: + * (sum,count). Here, + * sum refers to the cumulative sum of the bricks' widths encountered in the current row, and + * count refers to the number of times the corresponding sum is obtained. Thus, + * sum in a way, represents the positions of the bricks's boundaries relative to the leftmost boundary. + *

+ * This is done based on the following observation: + * We will never obtain the same value of sum twice while traversing over a particular row. + * Thus, if the sum value is repeated while traversing over the rows, it means some row's brick boundary coincides with some previous row's brick boundary. + * This fact is accounted for by incrementing the corresponding count value. + * But, for every row, we consider the sum only upto the second last brick, since the last boundary isn't a valid boundary for the solution. + */ + + public int leastBricks(List> wall) { + Map map = new HashMap(); + for (List row : wall) { + int sum = 0; + for (int i = 0; i < row.size() - 1; i++) { + // NOTE: i < row.size()-1 + sum += row.get(i); + if (map.containsKey(sum)) { + map.put(sum, map.get(sum) + 1); + } else { + map.put(sum, 1); + } + } + } + int result = wall.size(); + for (int key : map.keySet()) { + result = Math.min(result, wall.size() - map.get(key)); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_555.java b/src/main/java/com/fishercoder/solutions/firstthousand/_555.java new file mode 100644 index 0000000000..c557b8120a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_555.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +public class _555 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/86477/neat-java-solution + * and article: https://leetcode.com/articles/split-assembled-strings/#approach-3-optimized-solution-accepted + */ + public String splitLoopedString(String[] strs) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < strs.length; i++) { + sb.setLength(0); + String reverse = sb.append(strs[i]).reverse().toString(); + if (strs[i].compareTo(reverse) < 0) { + strs[i] = reverse; + } + } + String result = ""; + for (int i = 0; i < strs.length; i++) { + sb.setLength(0); + String reverse = sb.append(strs[i]).reverse().toString(); + for (String str : new String[] {strs[i], reverse}) { + for (int k = 0; k < str.length(); k++) { + sb.setLength(0); + sb.append(str.substring(k)); + for (int j = i + 1; j < strs.length; j++) { + sb.append(strs[j]); + } + for (int j = 0; j < i; j++) { + sb.append(strs[j]); + } + sb.append(str.substring(0, k)); + if (sb.toString().compareTo(result) > 0) { + result = sb.toString(); + } + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_556.java b/src/main/java/com/fishercoder/solutions/firstthousand/_556.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_556.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_556.java index 8d1ed5c049..f611de716f 100644 --- a/src/main/java/com/fishercoder/solutions/_556.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_556.java @@ -1,22 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; -/** - * 556. Next Greater Element III - * - * Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing - * in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1. - * - * Example 1: - * Input: 12 - * Output: 21 - * - * Example 2: - * Input: 21 - * Output: -1 - */ public class _556 { public static class Solution1 { - /** + /* * credit: https://discuss.leetcode.com/topic/85759/this-problem-is-the-same-to-next-permutation-algorithm-only and https://discuss.leetcode.com/topic/85755/java-solution-like-next-permutation-problem-o-n */ @@ -58,5 +44,4 @@ private void swap(char[] a, int i, int j) { a[j] = temp; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_557.java b/src/main/java/com/fishercoder/solutions/firstthousand/_557.java new file mode 100644 index 0000000000..326b2fe892 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_557.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.firstthousand; + +public class _557 { + + public static class Solution1 { + public String reverseWords(String s) { + StringBuilder stringBuilder = new StringBuilder(); + StringBuilder sbUtils = new StringBuilder(); + for (String word : s.split(" ")) { + sbUtils.setLength(0); + sbUtils.append(word); + stringBuilder.append(sbUtils.reverse().toString()); + stringBuilder.append(" "); + } + stringBuilder.setLength(stringBuilder.length() - 1); + return stringBuilder.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_559.java b/src/main/java/com/fishercoder/solutions/firstthousand/_559.java new file mode 100644 index 0000000000..7b6a719356 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_559.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.Node; +import java.util.ArrayList; +import java.util.List; + +public class _559 { + public static class Solution1 { + public int maxDepth(Node root) { + int maxDepth = 0; + if (root == null) { + return maxDepth; + } + List> allPaths = new ArrayList<>(); + List currentPath = new ArrayList<>(); + dfs(root, currentPath, allPaths); + for (List path : allPaths) { + maxDepth = Math.max(path.size(), maxDepth); + } + return maxDepth; + } + + private void dfs(Node root, List currentPath, List> allPaths) { + if (root == null) { + allPaths.add(new ArrayList<>(currentPath)); + } + if (root.children != null && !root.children.isEmpty()) { + currentPath.add(root.val); + for (Node child : root.children) { + dfs(child, new ArrayList<>(currentPath), allPaths); + } + } + if (root.children == null || root.children.isEmpty()) { + currentPath.add(root.val); + allPaths.add(new ArrayList<>(currentPath)); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_56.java b/src/main/java/com/fishercoder/solutions/firstthousand/_56.java new file mode 100644 index 0000000000..42ad3f330f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_56.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _56 { + + public static class Solution1 { + /* + * My completely original solution on 10/12/2021. + */ + public int[][] merge(int[][] intervals) { + List list = new ArrayList<>(); + Arrays.sort( + intervals, + (a, b) -> + a[0] != b[0] + ? Integer.compare(a[0], b[0]) + : Integer.compare( + b[1], a[1])); // to avoid integer subtraction overflow + for (int i = 0; i < intervals.length; i++) { + int start = intervals[i][0]; + int end = intervals[i][1]; + while (i + 1 < intervals.length && intervals[i + 1][0] <= end) { + end = Math.max(intervals[i + 1][1], end); + i++; + } + list.add(new int[] {start, end}); + } + return list.toArray(new int[list.size()][2]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_560.java b/src/main/java/com/fishercoder/solutions/firstthousand/_560.java new file mode 100644 index 0000000000..babf5698c8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_560.java @@ -0,0 +1,64 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _560 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/87850/java-solution-presum-hashmap + * We know the key to solve this problem is SUM[i, j]. + * So if we know SUM[0, i - 1] and SUM[0, j], + * then we can easily get SUM[i, j] via (SUM[0, j] - SUM[0, i-1]). + * To achieve this, we just need to go through the array, + * calculate the current sum and save number of all seen PreSum to a HashMap. + *

+ * Time complexity: O(n); + * Space complexity: O(n). + */ + public int subarraySum(int[] nums, int k) { + Map preSumFrequencyMap = new HashMap(); + int sum = 0; + int count = 0; + preSumFrequencyMap.put(0, 1); + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; + if (preSumFrequencyMap.containsKey(sum - k)) { + count += preSumFrequencyMap.get(sum - k); + } + preSumFrequencyMap.put(sum, preSumFrequencyMap.getOrDefault(sum, 0) + 1); + } + return count; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/14/2021. + * Again, using a pen and paper to visualize your thought process just clears out all ambiguities. + *

+ * Time: O(n^2) + * Space: O(n) + */ + public int subarraySum(int[] nums, int k) { + int count = 0; + int[] prefixSum = new int[nums.length]; + prefixSum[0] = nums[0]; + for (int i = 1; i < nums.length; i++) { + prefixSum[i] = prefixSum[i - 1] + nums[i]; + } + for (int i = 0; i < nums.length; i++) { + if (prefixSum[i] == k) { + count++; + } + for (int j = 0; j < i; j++) { + if (prefixSum[i] - prefixSum[j] == k) { + count++; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_561.java b/src/main/java/com/fishercoder/solutions/firstthousand/_561.java new file mode 100644 index 0000000000..fcf61bd7f1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_561.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _561 { + + public static class Solution1 { + public int arrayPairSum(int[] nums) { + Arrays.sort(nums); + int sum = 0; + for (int i = 0; i < nums.length; i += 2) { + sum += nums[i]; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_562.java b/src/main/java/com/fishercoder/solutions/firstthousand/_562.java new file mode 100644 index 0000000000..19932c035a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_562.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.firstthousand; + +public class _562 { + + public static class Solution1 { + public int longestLine(int[][] M) { + if (M == null || M.length == 0) { + return 0; + } + int[][] directions = + new int[][] { + {-1, 0}, + {-1, 1}, + {0, 1}, + {1, 1}, + {1, 0}, + {1, -1}, + {0, -1}, + {-1, -1}, + }; + int longestLine = 0; + int m = M.length; + int n = M[0].length; + int[][][] cache = new int[m][n][8]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (M[i][j] == 1) { + for (int k = 0; k < directions.length; k++) { + int nextI = i + directions[k][0]; + int nextJ = j + directions[k][1]; + int thisLine = 1; + if (nextI >= 0 + && nextI < m + && nextJ >= 0 + && nextJ < n + && cache[nextI][nextJ][k] != 0) { + thisLine += cache[nextI][nextJ][k]; + cache[i][j][k] = thisLine; + } else { + while (nextI >= 0 + && nextI < m + && nextJ >= 0 + && nextJ < n + && M[nextI][nextJ] == 1) { + thisLine++; + cache[i][j][k] = thisLine; + nextI += directions[k][0]; + nextJ += directions[k][1]; + } + } + longestLine = Math.max(longestLine, thisLine); + } + } + } + } + return longestLine; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_563.java b/src/main/java/com/fishercoder/solutions/firstthousand/_563.java new file mode 100644 index 0000000000..33eae0aee8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_563.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _563 { + public static class Solution1 { + + int tilt = 0; + + public int findTilt(TreeNode root) { + findTiltDfs(root); + return tilt; + } + + public int findTiltDfs(TreeNode root) { + if (root == null) { + return 0; + } + int leftTilt = 0; + if (root.left != null) { + leftTilt = findTiltDfs(root.left); + } + int rightTilt = 0; + if (root.right != null) { + rightTilt = findTiltDfs(root.right); + } + if (root.left == null && root.right == null) { + return root.val; + } + tilt += Math.abs(leftTilt - rightTilt); + return leftTilt + rightTilt + root.val; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_564.java b/src/main/java/com/fishercoder/solutions/firstthousand/_564.java new file mode 100644 index 0000000000..acdd1434f5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_564.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.firstthousand; + +public class _564 { + public static class Solution1 { + + public String nearestPalindromic(String n) { + if (n.length() >= 2 && allNine(n)) { + String s = "1"; + for (int i = 0; i < n.length() - 1; i++) { + s += "0"; + } + s += "1"; + return s; + } + boolean isOdd = (n.length() % 2 != 0); + String left = n.substring(0, (n.length() + 1) / 2); + long[] increment = {-1, 0, +1}; + String ret = n; + long minDiff = Long.MAX_VALUE; + for (long i : increment) { + String s = getPalindrom(Long.toString(Long.parseLong(left) + i), isOdd); + if (n.length() >= 2 && (s.length() != n.length() || Long.parseLong(s) == 0)) { + s = ""; + for (int j = 0; j < n.length() - 1; j++) { + s += "9"; + } + } + long diff = + s.equals(n) + ? Long.MAX_VALUE + : Math.abs(Long.parseLong(s) - Long.parseLong(n)); + if (diff < minDiff) { + minDiff = diff; + ret = s; + } + } + return ret; + } + + private String getPalindrom(String s, boolean isOdd) { + String right = new StringBuilder(s).reverse().toString(); + return isOdd ? s.substring(0, s.length() - 1) + right : s + right; + } + + private boolean allNine(String s) { + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) != '9') { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_565.java b/src/main/java/com/fishercoder/solutions/firstthousand/_565.java new file mode 100644 index 0000000000..26f04a6c29 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_565.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _565 { + + public static class Solution1 { + public int arrayNesting(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + boolean[] visited = new boolean[nums.length]; + int answer = 0; + for (int i : nums) { + int count = 0; + int j = i; + while (j >= 0 && j < nums.length && !visited[j]) { + count++; + visited[j] = true; + j = nums[j]; + } + answer = Math.max(answer, count); + } + return answer; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_566.java b/src/main/java/com/fishercoder/solutions/firstthousand/_566.java new file mode 100644 index 0000000000..10808187a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_566.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +public class _566 { + + public static class Solution1 { + public int[][] matrixReshape(int[][] mat, int r, int c) { + if (mat == null || mat.length == 0) { + return mat; + } + int m = mat.length; + int n = mat[0].length; + if (r * c > m * n) { + return mat; + } + int k = 0; + int[][] result = new int[r][c]; + for (int i = 0; i < r; i++) { + for (int j = 0; j < c; j++, k++) { + result[i][j] = mat[k / n][k % n]; + } + } + return result; + } + } + + public static class Solution2 { + public int[][] matrixReshape(int[][] mat, int r, int c) { + int m = mat.length; + int n = mat[0].length; + if (m * n != r * c) { + return mat; + } + int[][] reshapedMat = new int[r][c]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + int index = i * n + j; + reshapedMat[index / c][index % c] = mat[i][j]; + } + } + return reshapedMat; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_567.java b/src/main/java/com/fishercoder/solutions/firstthousand/_567.java new file mode 100644 index 0000000000..c4f37e9fda --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_567.java @@ -0,0 +1,88 @@ +package com.fishercoder.solutions.firstthousand; + +public class _567 { + + public static class Solution1 { + /* + * credit: sliding window: https://discuss.leetcode.com/topic/87845/java-solution-sliding-window + */ + public boolean checkInclusion(String s1, String s2) { + int len1 = s1.length(); + int len2 = s2.length(); + if (len1 > len2) { + return false; + } + + int[] count = new int[26]; + for (int i = 0; i < len1; i++) { + count[s1.charAt(i) - 'a']++; + count[s2.charAt(i) - 'a']--; + } + + if (allZeroes(count)) { + return true; + } + + for (int i = len1; i < len2; i++) { + count[s2.charAt(i) - 'a']--; + count[s2.charAt(i - len1) - 'a']++; + if (allZeroes(count)) { + return true; + } + } + + return false; + } + + private boolean allZeroes(int[] count) { + for (int i : count) { + if (i != 0) { + return false; + } + } + return true; + } + } + + public static class Solution2 { + /* + * A classic sliding window problem. + * I came up with below solution independently on 9/17/2021. + *

+ * A few pointers that led me to the sliding window approach: + * 1. if it's a valid permutation, the substring from S2 must have equal length as of s1; + * 2. I don't want to repeatedly calculate each and every possible substring of s2, if s1 is really long, this could mean lots of redundant calculation. + * So sliding window to the rescue! + */ + public boolean checkInclusion(String s1, String s2) { + if (s1.length() > s2.length()) { + return false; + } + int[] count = new int[26]; + for (char c : s1.toCharArray()) { + count[c - 'a']++; + } + for (int i = 0; i < s1.length(); i++) { + count[s2.charAt(i) - 'a']--; + } + for (int i = s1.length(), j = 0; i < s2.length(); i++, j++) { + if (isPermutation(count)) { + return true; + } else { + count[s2.charAt(j) - 'a']++; + count[s2.charAt(i) - 'a']--; + } + } + return isPermutation(count); + } + + private boolean isPermutation(int[] count) { + for (int c : count) { + if (c != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_568.java b/src/main/java/com/fishercoder/solutions/firstthousand/_568.java new file mode 100644 index 0000000000..459b79084b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_568.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _568 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/articles/maximum-vacation-days/#approach-2-using-dfs-with-memoization-accepted + */ + public int maxVacationDays(int[][] flights, int[][] days) { + int[][] memo = new int[flights.length][days[0].length]; + for (int[] l : memo) { + Arrays.fill(l, Integer.MIN_VALUE); + } + return dfs(flights, days, 0, 0, memo); + } + + public int dfs(int[][] flights, int[][] days, int curCity, int weekno, int[][] memo) { + if (weekno == days[0].length) { + return 0; + } + if (memo[curCity][weekno] != Integer.MIN_VALUE) { + return memo[curCity][weekno]; + } + int maxvac = 0; + for (int i = 0; i < flights.length; i++) { + if (flights[curCity][i] == 1 || i == curCity) { + int vac = days[i][weekno] + dfs(flights, days, i, weekno + 1, memo); + maxvac = Math.max(maxvac, vac); + } + } + memo[curCity][weekno] = maxvac; + return maxvac; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_57.java b/src/main/java/com/fishercoder/solutions/firstthousand/_57.java new file mode 100644 index 0000000000..73253db338 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_57.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _57 { + + public static class Solution1 { + public int[][] insert(int[][] intervals, int[] newInterval) { + List list = new ArrayList<>(); + int i = 0; + // add all the intervals ending before newInterval starts + while (i < intervals.length && intervals[i][1] < newInterval[0]) { + list.add(intervals[i++]); + } + // merge all overlapping intervals to one considering newInterval + while (i < intervals.length && intervals[i][0] <= newInterval[1]) { + newInterval = + new int[] { + Math.min(newInterval[0], intervals[i][0]), + Math.max(newInterval[1], intervals[i][1]) + }; + i++; + } + list.add(newInterval); + // add all the rest + while (i < intervals.length) { + list.add(intervals[i++]); + } + return list.toArray(new int[list.size()][]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_572.java b/src/main/java/com/fishercoder/solutions/firstthousand/_572.java new file mode 100644 index 0000000000..71b1b2406d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_572.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _572 { + + public static class Solution1 { + public boolean isSubtree(TreeNode root, TreeNode subRoot) { + if (root == null) { + return false; + } + if (same(root, subRoot)) { + return true; + } + return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot); + } + + private boolean same(TreeNode s, TreeNode t) { + if (s == null || t == null) { + return s == t; + } + if (s.val != t.val) { + return false; + } + return same(s.left, t.left) && same(s.right, t.right); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_573.java b/src/main/java/com/fishercoder/solutions/firstthousand/_573.java new file mode 100644 index 0000000000..d7cedaff0f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_573.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +public class _573 { + + public static class Solution1 { + + /* + * reference: https://leetcode.com/articles/squirrel-simulation + *

+ * 1. The order in which to pick the nuts does not matter except the first one + * because for all the other nuts, the squirrel needs to travel back and forth. + *

+ * 2. For the first nut to be picked, there's some distance we might be able to save, what is this distance? + * It is the difference between the squirrel's original starting point to the first nut and that the distance from this + * first nut to the tree. + * This is because, only for the first nut, the squirrel does NOT need to travel back and forth, it only needs to travel from + * its starting position to the nut position and then return to the tree. + *

+ * 3. For the rest of all other nuts, the squirrel always needs to go back and forth. + *

+ * 4. So how can we find the first nut to go to so that we could have the maximum saved distance? + * We iterate through all of the nuts and keep updating the savedDist as below: + */ + public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) { + int totalDist = 0; + int savedDist = Integer.MIN_VALUE; + for (int[] nut : nuts) { + totalDist += + (getDist(nut, tree) * 2); // it needs to travel back and forth, so times two + savedDist = Math.max(savedDist, getDist(nut, tree) - getDist(nut, squirrel)); + } + return totalDist - savedDist; + } + + private int getDist(int[] pointA, int[] pointB) { + return Math.abs(pointA[0] - pointB[0]) + Math.abs(pointA[1] - pointB[1]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_575.java b/src/main/java/com/fishercoder/solutions/firstthousand/_575.java new file mode 100644 index 0000000000..317c8bd230 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_575.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _575 { + public static class Solution1 { + public int distributeCandies(int[] candyType) { + Set map = new HashSet<>(); + for (int candy : candyType) { + map.add(candy); + } + return Math.min(map.size(), candyType.length / 2); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_576.java b/src/main/java/com/fishercoder/solutions/firstthousand/_576.java new file mode 100644 index 0000000000..f2c8c54763 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_576.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +public class _576 { + public static class Solution1 { + /* + * reference: https://leetcode.com/articles/out-of-boundary-paths/#approach-2-recursion-with-memoization-accepted + */ + public int findPaths(int m, int n, int N, int x, int y) { + int M = 1000000000 + 7; + int[][] dp = new int[m][n]; + dp[x][y] = 1; + int count = 0; + for (int moves = 1; moves <= N; moves++) { + int[][] temp = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (i == m - 1) { + count = (count + dp[i][j]) % M; + } + if (j == n - 1) { + count = (count + dp[i][j]) % M; + } + if (i == 0) { + count = (count + dp[i][j]) % M; + } + if (j == 0) { + count = (count + dp[i][j]) % M; + } + temp[i][j] = + (((i > 0 ? dp[i - 1][j] : 0) + (i < m - 1 ? dp[i + 1][j] : 0)) % M + + ((j > 0 ? dp[i][j - 1] : 0) + + (j < n - 1 ? dp[i][j + 1] : 0)) + % M) + % M; + } + } + dp = temp; + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_58.java b/src/main/java/com/fishercoder/solutions/firstthousand/_58.java similarity index 89% rename from src/main/java/com/fishercoder/solutions/_58.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_58.java index 89206a7d0f..a4112f1576 100644 --- a/src/main/java/com/fishercoder/solutions/_58.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_58.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _58 { @@ -15,5 +15,4 @@ public int lengthOfLastWord(String s) { return s.length() - n - 1; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_581.java b/src/main/java/com/fishercoder/solutions/firstthousand/_581.java new file mode 100644 index 0000000000..c2bebfe522 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_581.java @@ -0,0 +1,89 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _581 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/89282/java-o-n-time-o-1-space + * Use start and end to keep track of the minimum subarray nums[start...end] which must be sorted for the entire array nums. + * If start < end < 0 at the end of the for loop, then the array is already fully sorted. + *

+ * Time: O(n) + * Space: O(1) + */ + public int findUnsortedSubarray(int[] nums) { + int n = nums.length; + int start = -1; + int end = -2; + int min = nums[n - 1]; + int max = nums[0]; + for (int i = 1; i < n; i++) { + max = Math.max(max, nums[i]); + min = Math.min(min, nums[n - 1 - i]); + if (nums[i] < max) { + end = i; + } + if (nums[n - 1 - i] > min) { + start = n - 1 - i; + } + } + return end - start + 1; + } + } + + public static class Solution2 { + /* + * Time: O(n) + * Space: O(1) + *

+ * This is just an alternative way of writing Solution1, credit: https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/103057/Java-O(n)-Time-O(1)-Space/106306 + * + * But still, initializing end to be -2 is an art to take care of the corner case as in Solution1. + */ + public int findUnsortedSubarray(int[] nums) { + int end = -2; + int max = Integer.MIN_VALUE; + // go from left to right, find the number that is smaller than the max number on its + // left side, that should be the end index because it needs to be sorted + for (int i = 0; i < nums.length; i++) { + max = Math.max(max, nums[i]); + if (nums[i] < max) { + end = i; + } + } + int start = -1; + int min = Integer.MAX_VALUE; + // go from right to left, find the number that is bigger than the min number on its + // right, that should be the beginning index + for (int i = nums.length - 1; i >= 0; i--) { + min = Math.min(min, nums[i]); + if (nums[i] > min) { + start = i; + } + } + return end - start + 1; + } + } + + public static class Solution3 { + /* + * Time: O(nlogn) + * Space: O(n) + */ + public int findUnsortedSubarray(int[] nums) { + int[] clones = nums.clone(); + Arrays.sort(clones); + int start = nums.length; + int end = 0; + for (int i = 0; i < nums.length; i++) { + if (clones[i] != nums[i]) { + start = Math.min(start, i); + end = Math.max(end, i); + } + } + return (end - start > 0) ? end - start + 1 : 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_582.java b/src/main/java/com/fishercoder/solutions/firstthousand/_582.java new file mode 100644 index 0000000000..0f2e9c7ec4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_582.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class _582 { + + public static class Solution1 { + public List killProcess(List pid, List ppid, int kill) { + Map> map = new HashMap<>(); + for (int i = 0; i < pid.size(); i++) { + map.putIfAbsent(ppid.get(i), new LinkedList<>()); + map.get(ppid.get(i)).add(pid.get(i)); + } + List result = new LinkedList<>(); + Deque stack = new ArrayDeque<>(); + stack.offer(kill); + while (!stack.isEmpty()) { + int curr = stack.poll(); + result.add(curr); + List list = map.get(curr); + if (list != null) { + stack.addAll(list); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_583.java b/src/main/java/com/fishercoder/solutions/firstthousand/_583.java new file mode 100644 index 0000000000..1073275de0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_583.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _583 { + + public static class Solution1 { + + public int minDistance(String word1, String word2) { + int m = word1.length(); + int n = word2.length(); + int[][] dp = new int[m + 1][n + 1]; + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + dp[i][j] = + word1.charAt(i - 1) == word2.charAt(j - 1) + ? dp[i - 1][j - 1] + 1 + : Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + return m + n - 2 * dp[m][n]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_587.java b/src/main/java/com/fishercoder/solutions/firstthousand/_587.java new file mode 100644 index 0000000000..5f19f127b4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_587.java @@ -0,0 +1,88 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.Point; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _587 { + public static class Solution1 { + + /* + * credit: https://discuss.leetcode.com/topic/89323/java-solution-convex-hull-algorithm-gift-wrapping-aka-jarvis-march + * There are couple of ways to solve Convex Hull problem. https://en.wikipedia.org/wiki/Convex_hull_algorithms + * The following code implements Gift wrapping aka Jarvis march algorithm + * https://en.wikipedia.org/wiki/Gift_wrapping_algorithm and + * also added logic to handle case of multiple Points in a line + * because original Jarvis march algorithm assumes no three points are collinear. + * It also uses knowledge in this problem https://leetcode.com/problems/convex-polygon. + * Disscussion: https://discuss.leetcode.com/topic/70706/beyond-my-knowledge-java-solution-with-in-line-explanation + */ + public List outerTrees(Point[] points) { + Set result = new HashSet<>(); + + // Find the leftmost point + Point first = points[0]; + int firstIndex = 0; + for (int i = 1; i < points.length; i++) { + if (points[i].x < first.x) { + first = points[i]; + firstIndex = i; + } + } + result.add(first); + + Point cur = first; + int curIndex = firstIndex; + do { + Point next = points[0]; + int nextIndex = 0; + for (int i = 1; i < points.length; i++) { + if (i == curIndex) { + continue; + } + int cross = crossProductLength(cur, points[i], next); + if (nextIndex == curIndex + || cross > 0 + // Handle collinear points + || (cross == 0 && distance(points[i], cur) > distance(next, cur))) { + next = points[i]; + nextIndex = i; + } + } + // Handle collinear points + for (int i = 0; i < points.length; i++) { + if (i == curIndex) { + continue; + } + int cross = crossProductLength(cur, points[i], next); + if (cross == 0) { + result.add(points[i]); + } + } + + cur = next; + curIndex = nextIndex; + + } while (curIndex != firstIndex); + + return new ArrayList<>(result); + } + + private int crossProductLength(Point A, Point B, Point C) { + // Get the vectors' coordinates. + int BAx = A.x - B.x; + int BAy = A.y - B.y; + int BCx = C.x - B.x; + int BCy = C.y - B.y; + + // Calculate the Z coordinate of the cross product. + return (BAx * BCy - BAy * BCx); + } + + private int distance(Point p1, Point p2) { + return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_588.java b/src/main/java/com/fishercoder/solutions/firstthousand/_588.java new file mode 100644 index 0000000000..40619534a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_588.java @@ -0,0 +1,83 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _588 { + public static class Solution1 { + /* + * Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_588.java + */ + public static class FileSystem { + + class TrieNode { + boolean isFile = false; + Map map; + String name; + StringBuilder stringBuilder; + + TrieNode(String name) { + this.name = name; + map = new HashMap<>(); + stringBuilder = new StringBuilder(); + } + } + + TrieNode root = new TrieNode(""); + + TrieNode dfs(String path) { + TrieNode node = root; + for (String eachPath : path.split("/")) { + if (eachPath.isEmpty()) { + continue; + } + if (!node.map.containsKey(eachPath)) { + node.map.put(eachPath, new TrieNode(eachPath)); + } + node = node.map.get(eachPath); + } + return node; + } + + public FileSystem() {} + + public List ls(String path) { + TrieNode node = dfs(path); + if (node.isFile) { + return Arrays.asList(node.name); + } else { + List files = new ArrayList(node.map.keySet()); + Collections.sort(files); + return files; + } + } + + public void mkdir(String path) { + dfs(path); + } + + public void addContentToFile(String filePath, String content) { + TrieNode node = dfs(filePath); + node.isFile = true; + node.stringBuilder.append(content); + } + + public String readContentFromFile(String filePath) { + return dfs(filePath).stringBuilder.toString(); + } + } + } + + /* + * Your FileSystem object will be instantiated and called as such: + * FileSystem obj = new FileSystem(); + * List param_1 = obj.ls(path); + * obj.mkdir(path); + * obj.addContentToFile(filePath,content); + * String param_4 = obj.readContentFromFile(filePath); + */ +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_589.java b/src/main/java/com/fishercoder/solutions/firstthousand/_589.java new file mode 100644 index 0000000000..64b058ed44 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_589.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.Node; +import java.util.ArrayList; +import java.util.List; + +public class _589 { + public static class Solution1 { + public List preorder(Node root) { + List result = new ArrayList<>(); + if (root == null) { + return result; + } + dfs(root, result); + return result; + } + + private void dfs(Node root, List result) { + if (root == null) { + return; + } + result.add(root.val); + if (root.children.size() > 0) { + for (Node child : root.children) { + dfs(child, result); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_59.java b/src/main/java/com/fishercoder/solutions/firstthousand/_59.java new file mode 100644 index 0000000000..2aa90a1de5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_59.java @@ -0,0 +1,106 @@ +package com.fishercoder.solutions.firstthousand; + +public class _59 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/spiral-matrix-ii/discuss/22289/My-Super-Simple-Solution.-Can-be-used-for-both-Spiral-Matrix-I-and-II/21907 + */ + public int[][] generateMatrix(int n) { + int[][] matrix = new int[n][n]; + if (n == 0) { + return matrix; + } + + int value = 1; + int top = 0; + int bottom = n - 1; + int left = 0; + int right = n - 1; + while (left <= right && top <= bottom) { + for (int j = left; j <= right; j++) { + matrix[top][j] = value++; + } + top++; + for (int i = top; i <= bottom; i++) { + matrix[i][right] = value++; + } + right--; + for (int j = right; j >= left; j--) { + matrix[bottom][j] = value++; + } + bottom--; + for (int i = bottom; i >= top; i--) { + matrix[i][left] = value++; + } + left++; + } + return matrix; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/12/2021. + */ + public int[][] generateMatrix(int n) { + int direction = 0; + int[][] matrix = new int[n][n]; + int num = 1; + int i = 0; + int j = 0; + int eastBoundary = n; + int southBoundary = n; + int westBoundary = 0; + int northBoundary = 0; + int limit = n * n; + while (num <= limit) { + if (direction % 4 == 0) { + // 0 means going east + for (; j < eastBoundary && num <= limit; j++) { + matrix[i][j] = num; + num++; + } + j--; + direction++; + eastBoundary--; + i++; + } + if (direction % 4 == 1) { + // 1 means going south + for (; i < southBoundary && num <= limit; i++) { + matrix[i][j] = num; + num++; + } + i--; + direction++; + southBoundary--; + j--; + } + if (direction % 4 == 2) { + // 2 means going west + for (; j >= westBoundary && num <= limit; j--) { + matrix[i][j] = num; + num++; + } + j++; + direction++; + westBoundary++; + i--; + } + if (direction % 4 == 3) { + // 3 means going north + for (; i > northBoundary && num <= limit; i--) { + matrix[i][j] = num; + num++; + } + i++; + direction++; + northBoundary++; + j++; + } + } + return matrix; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_590.java b/src/main/java/com/fishercoder/solutions/firstthousand/_590.java new file mode 100644 index 0000000000..ada5b98eb8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_590.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.Node; +import java.util.ArrayList; +import java.util.List; + +public class _590 { + public static class Solution1 { + public List postorder(Node root) { + return post(root, new ArrayList<>()); + } + + private List post(Node root, List list) { + if (root == null) { + return list; + } + for (Node child : root.children) { + post(child, list); + } + list.add(root.val); + return list; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_591.java b/src/main/java/com/fishercoder/solutions/firstthousand/_591.java new file mode 100644 index 0000000000..c7061129c0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_591.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class _591 { + + public static class Solution1 { + + /* + * Credit: https://discuss.leetcode.com/topic/91300/java-solution-use-startswith-and-indexof + */ + public boolean isValid(String code) { + Deque stack = new ArrayDeque<>(); + for (int i = 0; i < code.length(); ) { + if (i > 0 && stack.isEmpty()) { + return false; + } + if (code.startsWith("", j); + if (i < 0) { + return false; + } + i += 3; // "]]>" length is 3 + } else if (code.startsWith("", j); + if (i < 0 || i == j || i - j > 9) { + return false; + } + for (int k = j; k < i; k++) { + if (!Character.isUpperCase(code.charAt(k))) { + return false; + } + } + String s = code.substring(j, i++); + if (stack.isEmpty() || !stack.pop().equals(s)) { + return false; + } + } else if (code.startsWith("<", i)) { + int j = i + 1; + i = code.indexOf(">", j); + if (i < 0 || i == j || i - j > 9) { + return false; + } + for (int k = j; k < i; k++) { + if (!Character.isUpperCase(code.charAt(k))) { + return false; + } + } + String s = code.substring(j, i++); + stack.push(s); + } else { + i++; + } + } + return stack.isEmpty(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_592.java b/src/main/java/com/fishercoder/solutions/firstthousand/_592.java new file mode 100644 index 0000000000..6f3b33059c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_592.java @@ -0,0 +1,65 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _592 { + + public static class Solution1 { + + /* + * Credit: https://discuss.leetcode.com/topic/89993/java-solution-fraction-addition-and-gcd + */ + public String fractionAddition(String expression) { + List nums = new ArrayList<>(); + int i = 0; + int j = 0; + while (j <= expression.length()) { + if (j == expression.length() + || j != i && (expression.charAt(j) == '-' || expression.charAt(j) == '+')) { + if (expression.charAt(i) == '+') { + nums.add(expression.substring(i + 1, j)); + } else { + nums.add(expression.substring(i, j)); + } + i = j; + } + j++; + } + + String result = "0/1"; + for (String frac : nums) { + result = add(result, frac); + } + return result; + } + + private String add(String result, String frac) { + String[] frac1 = frac.split("/"); + String[] frac2 = result.split("/"); + int n1 = Integer.parseInt(frac1[0]); + int d1 = Integer.parseInt(frac1[1]); + int n2 = Integer.parseInt(frac2[0]); + int d2 = Integer.parseInt(frac2[1]); + int numerator = n1 * d2 + n2 * d1; + int denominator = d1 * d2; + if (numerator == 0) { + return "0/1"; + } + + boolean negative = numerator * denominator < 0; + numerator = Math.abs(numerator); + denominator = Math.abs(denominator); + int gcd = getGCD(numerator, denominator); + + return (negative ? "-" : "") + (numerator / gcd) + "/" + (denominator / gcd); + } + + private int getGCD(int a, int b) { + if (a == 0 || b == 0) { + return a + b; + } + return getGCD(b, a % b); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_593.java b/src/main/java/com/fishercoder/solutions/firstthousand/_593.java similarity index 75% rename from src/main/java/com/fishercoder/solutions/_593.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_593.java index c9984f6bc6..1d60b40c83 100644 --- a/src/main/java/com/fishercoder/solutions/_593.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_593.java @@ -1,32 +1,11 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -/** - * 593. Valid Square - * - * Given the coordinates of four points in 2D space, return whether the four points could construct a square. - - The coordinate (x,y) of a point is represented by an integer array with two integers. - - Example: - Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] - Output: True - Note: - All the input integers are in the range [-10000, 10000]. - A valid square has four equal sides with positive length and four equal angles (90-degree angles). - Input points have no order. - - */ public class _593 { public static class Solution1 { - /** - * Note: I don't need to use backtracking to find all permutations, this is an overkill. - * This is the most easy one: https://leetcode.com/articles/kill-process-2/#approach-3-checking-every-case-accepted - */ - public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) { List input = new ArrayList<>(Arrays.asList(p1, p2, p3, p4)); List> allPermuations = getAllPermutations(input); @@ -45,7 +24,8 @@ private List> getAllPermutations(List input) { return backTracking(result, input, 0); } - private List> backTracking(List> result, List input, int pos) { + private List> backTracking( + List> result, List input, int pos) { if (pos == input.size()) { return result; } @@ -90,5 +70,4 @@ public boolean isRightAngle(int[] p1, int[] p2, int[] p3) { return degree % 45 == 0; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_594.java b/src/main/java/com/fishercoder/solutions/firstthousand/_594.java new file mode 100644 index 0000000000..bf2bb9ce3f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_594.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _594 { + public static class Solution1 { + public int findLHS(int[] nums) { + Map map = new HashMap<>(); + for (int i : nums) { + map.put(i, map.getOrDefault(i, 0) + 1); + } + int max = 0; + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i] + 1)) { + max = Math.max(max, map.get(nums[i]) + map.get(nums[i] + 1)); + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_598.java b/src/main/java/com/fishercoder/solutions/firstthousand/_598.java new file mode 100644 index 0000000000..9124358ffc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_598.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.firstthousand; + +public class _598 { + + public static class Solution1 { + /* + * Since the incrementing starts from zero to op[0] and op[1], we only need to find the range that has the most overlaps. + * Thus we keep finding the minimum of both x and y. + */ + public int maxCount(int m, int n, int[][] ops) { + int x = m; + int y = n; + for (int[] op : ops) { + x = Math.min(x, op[0]); + y = Math.min(y, op[1]); + } + return x * y; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_599.java b/src/main/java/com/fishercoder/solutions/firstthousand/_599.java new file mode 100644 index 0000000000..083eb58446 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_599.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _599 { + public static class Solution1 { + public String[] findRestaurant(String[] list1, String[] list2) { + if (list1 == null || list2 == null) { + return new String[0]; + } + Map map1 = putIntoMap(list1); + Map map2 = putIntoMap(list2); + int leastIndexSum = Integer.MAX_VALUE; + List resultList = new ArrayList<>(); + for (String key1 : map1.keySet()) { + if (map2.containsKey(key1)) { + int indexSum = map1.get(key1) + map2.get(key1); + if (indexSum < leastIndexSum) { + resultList.clear(); + resultList.add(key1); + leastIndexSum = indexSum; + } else if (indexSum == leastIndexSum) { + resultList.add(key1); + } + } + } + String[] result = new String[resultList.size()]; + for (int i = 0; i < resultList.size(); i++) { + result[i] = resultList.get(i); + } + return result; + } + + private Map putIntoMap(String[] list) { + Map map = new HashMap<>(); + for (int i = 0; i < list.length; i++) { + map.put(list[i], i); + } + return map; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_6.java b/src/main/java/com/fishercoder/solutions/firstthousand/_6.java new file mode 100644 index 0000000000..e964832733 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_6.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +public class _6 { + public static class Solution1 { + public String convert(String s, int numRows) { + StringBuilder[] sb = new StringBuilder[numRows]; + char[] c = s.toCharArray(); + int len = s.length(); + for (int i = 0; i < numRows; i++) { + sb[i] = new StringBuilder(); // this is an important step to initialize it + } + int i = 0; + while (i < len) { + for (int index = 0; index < numRows && i < len; index++) { + sb[index].append(c[i++]); // vertically down + } + + for (int index = numRows - 2; index >= 1 && i < len; index--) { + /*Why it should start from numRows - 2? Think of the example when numRows = 3 + the starting point of obliquely going up is 1, which is numRows-2.*/ + sb[index].append(c[i++]); // obliquely up + } + } + + for (i = 1; i < numRows; i++) { + sb[0].append(sb[i]); + } + return sb[0].toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_60.java b/src/main/java/com/fishercoder/solutions/firstthousand/_60.java similarity index 86% rename from src/main/java/com/fishercoder/solutions/_60.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_60.java index d4080b38c9..5aa01bbbcc 100644 --- a/src/main/java/com/fishercoder/solutions/_60.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_60.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _60 { @@ -15,7 +15,7 @@ public String getPermutation(int n, int k) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { permcount = permcount / (n - i); - int idx = k / permcount;// the index that this position should + int idx = k / permcount; // the index that this position should // choose sb.append(nums[idx]); // left shift nums[] by one bit diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_600.java b/src/main/java/com/fishercoder/solutions/firstthousand/_600.java new file mode 100644 index 0000000000..fbc9578bc3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_600.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +public class _600 { + + public static class Solution1 { + /* + * Credit: https://leetcode.com/articles/non-negative-integers-without-consecutive-ones/#approach-3-using-bit-manipulation-accepted + */ + public int findIntegers(int num) { + int[] f = new int[32]; + f[0] = 1; + f[1] = 2; + for (int i = 2; i < f.length; i++) { + f[i] = f[i - 1] + f[i - 2]; + } + int i = 30; + int sum = 0; + int prevBit = 0; + while (i >= 0) { + if ((num & (1 << i)) != 0) { + sum += f[i]; + if (prevBit == 1) { + sum--; + break; + } + prevBit = 1; + } else { + prevBit = 0; + } + i--; + } + return sum + 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_604.java b/src/main/java/com/fishercoder/solutions/firstthousand/_604.java new file mode 100644 index 0000000000..49ea03e847 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_604.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class _604 { + public static class Solution1 { + public static class StringIterator { + + Deque deque; + + public StringIterator(String compressedString) { + deque = new ArrayDeque<>(); + int len = compressedString.length(); + int i = 0; + while (i < len) { + int j = i + 1; + while (j < len && Character.isDigit(compressedString.charAt(j))) { + j++; + } + deque.addLast( + new int[] { + compressedString.charAt(i) - 'A', + Integer.parseInt(compressedString.substring(i + 1, j)) + }); + i = j; + } + } + + public char next() { + if (deque.isEmpty()) { + return ' '; + } + int[] top = deque.peek(); + top[1]--; + if (top[1] == 0) { + deque.pollFirst(); + } + return (char) ('A' + top[0]); + } + + public boolean hasNext() { + return !deque.isEmpty(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_605.java b/src/main/java/com/fishercoder/solutions/firstthousand/_605.java new file mode 100644 index 0000000000..5f5b915312 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_605.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +public class _605 { + + public static class Solution1 { + public boolean canPlaceFlowers(int[] flowerbed, int n) { + int count = 0; + int i = 0; + while (i < flowerbed.length) { + if (flowerbed[i] == 0 + && (i == 0 || flowerbed[i - 1] == 0) + && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) { + count++; + flowerbed[i] = 1; + } + if (count >= n) { + return true; + } + i++; + } + if (count >= n) { + return true; + } + return false; + } + } + + public static class Solution2 { + public boolean canPlaceFlowers(int[] flowerbed, int n) { + int len = flowerbed.length; + if (len == 1) { + if ((flowerbed[0] == 0 && n <= 1) || n == 0) { + return true; + } + return false; + } + if (flowerbed[0] == 0 && flowerbed[1] == 0) { + flowerbed[0] = 1; + n--; + } + for (int i = 1; i < len - 1; i++) { + if (flowerbed[i] == 0 && flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) { + n--; + // modify the input, discuss this with interviwer, if not allowed, then have a + // copy of this input and modify copy + flowerbed[i] = 1; + } + if (n <= 0) { + return true; + } + } + if (len >= 2 && flowerbed[len - 2] == 0 && flowerbed[len - 1] == 0) { + n--; + } + if (n <= 0) { + return true; + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_606.java b/src/main/java/com/fishercoder/solutions/firstthousand/_606.java new file mode 100644 index 0000000000..0649bebafb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_606.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _606 { + + public static class Solution1 { + public String tree2str(TreeNode t) { + if (t == null) { + return ""; + } + String result = "" + t.val; + String left = tree2str(t.left); + String right = tree2str(t.right); + if (left.equals("") && right.equals("")) { + return result; + } + if (left.equals("")) { + return result + "()(" + right + ")"; + } + if (right.equals("")) { + return result + "(" + left + ")"; + } + return result + "(" + left + ")(" + right + ")"; + } + } + + public static class Solution2 { + public String tree2str(TreeNode t) { + if (t == null) { + return ""; + } + StringBuilder sb = new StringBuilder(); + preorder(t, sb); + return sb.toString(); + } + + private void preorder(TreeNode root, StringBuilder sb) { + if (root == null) { + return; + } + sb.append(root.val); + if (root.left != null) { + sb.append("("); + preorder(root.left, sb); + sb.append(")"); + } + if (root.right != null) { + if (root.left == null) { + sb.append("()"); + } + sb.append("("); + preorder(root.right, sb); + sb.append(")"); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_609.java b/src/main/java/com/fishercoder/solutions/firstthousand/_609.java new file mode 100644 index 0000000000..2ff5ec8791 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_609.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _609 { + + public static class Solution1 { + public List> findDuplicate(String[] paths) { + Map> contentMap = new HashMap<>(); + for (String path : paths) { + String[] contents = path.split(" "); + List list = new ArrayList<>(); + for (int i = 1; i < contents.length; i++) { + list.add(contents[i]); + int start = contents[i].indexOf('('); + int end = contents[i].indexOf(')'); + String content = contents[i].substring(start + 1, end); + if (!contentMap.containsKey(content)) { + contentMap.put(content, new ArrayList<>()); + } + List dupFiles = contentMap.get(content); + dupFiles.add(contents[0] + "/" + contents[i].substring(0, start)); + } + } + List> result = new ArrayList<>(); + for (String key : contentMap.keySet()) { + if (contentMap.get(key).size() > 1) { + result.add(contentMap.get(key)); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_61.java b/src/main/java/com/fishercoder/solutions/firstthousand/_61.java similarity index 81% rename from src/main/java/com/fishercoder/solutions/_61.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_61.java index 5b63811a90..1aa6b1a609 100644 --- a/src/main/java/com/fishercoder/solutions/_61.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_61.java @@ -1,11 +1,11 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.ListNode; public class _61 { public static class Solution1 { - /** + /* * credit: https://discuss.leetcode.com/topic/26364/clean-java-solution-with-brief-explanation * link the tail of the linked list to the head to form a circle, then count to find the pint and cut it */ @@ -19,12 +19,12 @@ public ListNode rotateRight(ListNode head, int k) { copyHead = copyHead.next; len++; } - copyHead.next = head;//link the tail and head to make it a circle + copyHead.next = head; // link the tail and head to make it a circle for (int i = len - k % len; i > 1; i--) { head = head.next; } copyHead = head.next; - head.next = null;//break the circle + head.next = null; // break the circle return copyHead; } } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_611.java b/src/main/java/com/fishercoder/solutions/firstthousand/_611.java new file mode 100644 index 0000000000..f052b5616d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_611.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _611 { + public static class Solution1 { + /* + * Rule: among three sides, we need to find whether the longest of the three is smaller than the sum of the two shorter one. + * If so, then these three could form a valid triangle. + */ + + public int triangleNumber(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + Arrays.sort(nums); + int triplets = 0; + for (int i = 2; i < nums.length; i++) { + int left = 0; + int right = i - 1; + while (left < right) { + if (nums[i] < nums[left] + nums[right]) { + triplets += (right - left); + right--; + } else { + left++; + } + } + } + return triplets; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_616.java b/src/main/java/com/fishercoder/solutions/firstthousand/_616.java new file mode 100644 index 0000000000..1ca551e7e2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_616.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +public class _616 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/92112/java-solution-boolean-array + */ + public String addBoldTag(String s, String[] dict) { + boolean[] shouldBold = new boolean[s.length()]; + for (int i = 0, end = 0; i < s.length(); i++) { + for (String word : dict) { + if (s.startsWith(word, i)) { + end = Math.max(end, i + word.length()); + } + } + shouldBold[i] = end > i; + } + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + if (!shouldBold[i]) { + stringBuilder.append(s.charAt(i)); + continue; + } + int j = i; + while (j < s.length() && shouldBold[j]) { + j++; + } + stringBuilder.append("" + s.substring(i, j) + ""); + i = j - 1; + } + return stringBuilder.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_617.java b/src/main/java/com/fishercoder/solutions/firstthousand/_617.java new file mode 100644 index 0000000000..90e704eb70 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_617.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _617 { + + public static class Solution1 { + public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { + if (root1 == null) { + return root2; + } + if (root2 == null) { + return root1; + } + TreeNode mergedNode = new TreeNode(root1.val + root2.val); + mergedNode.left = mergeTrees(root1.left, root2.left); + mergedNode.right = mergeTrees(root1.right, root2.right); + return mergedNode; + } + } + + public static class Solution2 { + /* + * My completely original solution on 9/20/2021, no new extra nodes created. + */ + public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { + if (root1 == null) { + return root2; + } else if (root2 == null) { + return root1; + } + root1.val += root2.val; + root1.left = mergeTrees(root1.left, root2.left); + root1.right = mergeTrees(root1.right, root2.right); + return root1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_62.java b/src/main/java/com/fishercoder/solutions/firstthousand/_62.java similarity index 92% rename from src/main/java/com/fishercoder/solutions/_62.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_62.java index 07165861c0..34420349c0 100644 --- a/src/main/java/com/fishercoder/solutions/_62.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_62.java @@ -1,8 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _62 { public static class Solution1 { - /** + /* * Another typical DP question, use a 2d array: the first row and the first column need to be * initialized to be 1 since there's only one way to reach every position in the first row and * the first column: either from left or top. diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_621.java b/src/main/java/com/fishercoder/solutions/firstthousand/_621.java new file mode 100644 index 0000000000..ad8e9cab2b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_621.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; + +public class _621 { + + public static class Solution1 { + + public int leastInterval(char[] tasks, int n) { + Map map = new HashMap<>(); + for (char c : tasks) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b.total - a.total); + for (Map.Entry entry : map.entrySet()) { + maxHeap.offer(new Task(entry.getValue(), entry.getKey())); + } + int times = 0; + while (!maxHeap.isEmpty()) { + int i = 0; + List temp = new ArrayList<>(); + while (i <= n) { + if (!maxHeap.isEmpty()) { + if (maxHeap.peek().total > 1) { + Task curr = maxHeap.poll(); + temp.add(new Task(curr.total - 1, curr.character)); + } else { + maxHeap.poll(); + } + } + times++; + if (maxHeap.isEmpty() && temp.size() == 0) { + break; + } + i++; + } + for (Task task : temp) { + maxHeap.offer(task); + } + } + return times; + } + + class Task { + int total; + char character; + + public Task(int total, char character) { + this.total = total; + this.character = character; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_622.java b/src/main/java/com/fishercoder/solutions/firstthousand/_622.java new file mode 100644 index 0000000000..7508b2c388 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_622.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _622 { + public static class Solution1 { + public static class MyCircularQueue { + + int[] arr; + int rearIndex; // this one points to the rear of the queue and could grow to 3000 + // which is the max calls that this problem is bound to + int size; // this is the max size of this circule queue + int frontIndex; + + public MyCircularQueue(int k) { + arr = new int[k]; + Arrays.fill(arr, -1); + size = k; + rearIndex = 0; + frontIndex = 0; + } + + public boolean enQueue(int value) { + if (arr[rearIndex % size] == -1) { + arr[rearIndex % size] = value; + rearIndex++; + return true; + } else { + return false; + } + } + + public boolean deQueue() { + if (arr[frontIndex % size] != -1) { + arr[frontIndex % size] = -1; + frontIndex++; + return true; + } else { + return false; + } + } + + public int Front() { + return arr[frontIndex % size]; + } + + public int Rear() { + return arr[(rearIndex - 1) % size]; + } + + public boolean isEmpty() { + return rearIndex == frontIndex; + } + + public boolean isFull() { + return Math.abs(rearIndex - frontIndex) == size; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_623.java b/src/main/java/com/fishercoder/solutions/firstthousand/_623.java new file mode 100644 index 0000000000..49e8fd8f0c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_623.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _623 { + + public static class Solution1 { + public TreeNode addOneRow(TreeNode root, int v, int d) { + if (d == 1) { + TreeNode newRoot = new TreeNode(v); + newRoot.left = root; + return newRoot; + } else { + dfs(root, v, d); + return root; + } + } + + private void dfs(TreeNode root, int v, int d) { + if (root == null) { + return; + } + if (d == 2) { + TreeNode newLeft = new TreeNode(v); + TreeNode newRight = new TreeNode(v); + newLeft.left = root.left; + newRight.right = root.right; + root.left = newLeft; + root.right = newRight; + } else { + dfs(root.left, v, d - 1); + dfs(root.right, v, d - 1); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_624.java b/src/main/java/com/fishercoder/solutions/firstthousand/_624.java new file mode 100644 index 0000000000..23c392c8d4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_624.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _624 { + + public static class Solution1 { + public int maxDistance(List> arrays) { + List max = new ArrayList<>(); + for (List array : arrays) { + max.add(array.get(array.size() - 1)); + } + Collections.sort(max); + int ans = Integer.MIN_VALUE; + for (List array : arrays) { + int big = + array.get(array.size() - 1) == max.get(max.size() - 1) + ? max.get(max.size() - 2) + : max.get(max.size() - 1); + ans = Math.max(ans, big - array.get(0)); + } + return ans; + } + } + + public static class Solution2 { + public int maxDistance(List> arrays) { + int min = arrays.get(0).get(0); + int max = arrays.get(0).get(arrays.get(0).size() - 1); + int ans = 0; + for (int i = 1; i < arrays.size(); i++) { + List curr = arrays.get(i); + ans = Math.max(ans, Math.abs(max - curr.get(0))); + ans = Math.max(ans, Math.abs(curr.get(curr.size() - 1) - min)); + max = Math.max(max, curr.get(curr.size() - 1)); + min = Math.min(min, curr.get(0)); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_625.java b/src/main/java/com/fishercoder/solutions/firstthousand/_625.java new file mode 100644 index 0000000000..f07420f37e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_625.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _625 { + + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/92854/java-solution-result-array + * and https://leetcode.com/articles/minimum-factorization/#approach-3-using-factorizationaccepted + */ + public int smallestFactorization(int a) { + // case 1: a < 10 + if (a < 10) { + return a; + } + + // case 2: start with 9 and try every possible digit + List resultArray = new ArrayList<>(); + for (int i = 9; i > 1; i--) { + // if current digit divides a, then store all occurences of current digit in res + while (a % i == 0) { + a = a / i; + resultArray.add(i); + } + } + + // if a could not be broken in form of digits, return 0 + if (a != 0) { + return 0; + } + + // get the result from the result array in reverse order + long result = 0; + for (int i = resultArray.size() - 1; i >= 0; i--) { + result = result * 10 + resultArray.get(i); + if (result > Integer.MAX_VALUE) { + return 0; + } + } + return (int) result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_628.java b/src/main/java/com/fishercoder/solutions/firstthousand/_628.java new file mode 100644 index 0000000000..71a5979634 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_628.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _628 { + + public static class Solution1 { + public int maximumProduct(int[] nums) { + Arrays.sort(nums); + int product = 1; + if (nums.length >= 3) { + for (int i = nums.length - 1; i >= nums.length - 3; i--) { + product *= nums[i]; + } + int anotherProduct = nums[0] * nums[1] * nums[nums.length - 1]; + product = Math.max(product, anotherProduct); + } else { + for (int i = 0; i < nums.length; i++) { + product *= nums[i]; + } + } + return product; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_629.java b/src/main/java/com/fishercoder/solutions/firstthousand/_629.java new file mode 100644 index 0000000000..c8aac6a7f8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_629.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +public class _629 { + + public static class Solution1 { + + /* + * reference: https://leetcode.com/articles/k-inverse-pairs-array/#approach-5-another-optimized-dynamic-programming-approachaccepted + * and + * https://discuss.leetcode.com/topic/93815/java-dp-o-nk-solution + */ + public int kInversePairs(int n, int k) { + int mod = 1000000007; + if (k > n * (n - 1) / 2 || k < 0) { + return 0; + } + if (k == 0 || k == n * (n - 1) / 2) { + return 1; + } + long[][] dp = new long[n + 1][k + 1]; + dp[2][0] = 1; + dp[2][1] = 1; + for (int i = 3; i <= n; i++) { + dp[i][0] = 1; + for (int j = 1; j <= Math.min(k, i * (i - 1) / 2); j++) { + dp[i][j] = dp[i][j - 1] + dp[i - 1][j]; + if (j >= i) { + dp[i][j] -= dp[i - 1][j - i]; + } + dp[i][j] = (dp[i][j] + mod) % mod; + } + } + return (int) dp[n][k]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_63.java b/src/main/java/com/fishercoder/solutions/firstthousand/_63.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_63.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_63.java index 198bfbdcd6..87264499c1 100644 --- a/src/main/java/com/fishercoder/solutions/_63.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_63.java @@ -1,8 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _63 { public static class Solution1 { - /** + /* * Idea: grid[i][j] has to be set to zero if obstacleGrid[i][j] == 1, otherwise, we can get * dp[i][j] from its top and left dp. */ diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_630.java b/src/main/java/com/fishercoder/solutions/firstthousand/_630.java new file mode 100644 index 0000000000..231956dd53 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_630.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.PriorityQueue; + +public class _630 { + public static class Solution1 { + /* + * Reference: https://discuss.leetcode.com/topic/93790/short-java-code-using-priorityqueue + * Sort by finish date!!! This is greedy! We should take those classes that finish early first. + */ + public int scheduleCourse(int[][] courses) { + Arrays.sort(courses, (a, b) -> a[1] - b[1]); + int day = 0; + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); + for (int[] course : courses) { + day += course[0]; + maxHeap.offer(course[0]); + if (day > course[1]) { + day -= maxHeap.poll(); // drop the previous courses that took the most time + } + } + return maxHeap.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_631.java b/src/main/java/com/fishercoder/solutions/firstthousand/_631.java new file mode 100644 index 0000000000..9ad9a9f976 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_631.java @@ -0,0 +1,117 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Stack; + +public class _631 { + + public static class Solution1 { + /* + * Credit: https://leetcode.com/articles/design-excel-sum-formula/#approach-1-using-topological-sortaccepted + */ + public static class Excel { + + Formula[][] formulas; + + class Formula { + Formula(HashMap c, int v) { + val = v; + cells = c; + } + + HashMap cells; + int val; + } + + Stack stack = new Stack<>(); + + public Excel(int H, char W) { + formulas = new Formula[H][(W - 'A') + 1]; + } + + public int get(int r, char c) { + if (formulas[r - 1][c - 'A'] == null) { + return 0; + } + return formulas[r - 1][c - 'A'].val; + } + + public void set(int r, char c, int v) { + formulas[r - 1][c - 'A'] = new Formula(new HashMap(), v); + topologicalSort(r - 1, c - 'A'); + execute_stack(); + } + + public int sum(int r, char c, String[] strs) { + HashMap cells = convert(strs); + int summ = calculate_sum(r - 1, c - 'A', cells); + set(r, c, summ); + formulas[r - 1][c - 'A'] = new Formula(cells, summ); + return summ; + } + + public void topologicalSort(int r, int c) { + for (int i = 0; i < formulas.length; i++) { + for (int j = 0; j < formulas[0].length; j++) { + if (formulas[i][j] != null + && formulas[i][j].cells.containsKey( + "" + (char) ('A' + c) + (r + 1))) { + topologicalSort(i, j); + } + } + } + stack.push(new int[] {r, c}); + } + + public void execute_stack() { + while (!stack.isEmpty()) { + int[] top = stack.pop(); + if (formulas[top[0]][top[1]].cells.size() > 0) { + calculate_sum(top[0], top[1], formulas[top[0]][top[1]].cells); + } + } + } + + public HashMap convert(String[] strs) { + HashMap res = new HashMap<>(); + for (String st : strs) { + if (st.indexOf(":") < 0) { + res.put(st, res.getOrDefault(st, 0) + 1); + } else { + String[] cells = st.split(":"); + int si = Integer.parseInt(cells[0].substring(1)); + int ei = Integer.parseInt(cells[1].substring(1)); + char sj = cells[0].charAt(0); + char ej = cells[1].charAt(0); + for (int i = si; i <= ei; i++) { + for (char j = sj; j <= ej; j++) { + res.put("" + j + i, res.getOrDefault("" + j + i, 0) + 1); + } + } + } + } + return res; + } + + public int calculate_sum(int r, int c, HashMap cells) { + int sum = 0; + for (String s : cells.keySet()) { + int x = Integer.parseInt(s.substring(1)) - 1; + int y = s.charAt(0) - 'A'; + sum += (formulas[x][y] != null ? formulas[x][y].val : 0) * cells.get(s); + } + formulas[r][c] = new Formula(cells, sum); + return sum; + } + } + } + + /* + * Your Excel object will be instantiated and called as such: + * Excel obj = new Excel(H, W); + * obj.set(r,c,v); + * int param_2 = obj.get(r,c); + * int param_3 = obj.sum(r,c,strs); + */ + +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_632.java b/src/main/java/com/fishercoder/solutions/firstthousand/_632.java new file mode 100644 index 0000000000..dbb48b38a0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_632.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.List; +import java.util.PriorityQueue; + +public class _632 { + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/94445/java-code-using-priorityqueue-similar-to-merge-k-array/2 + */ + public int[] smallestRange(List> nums) { + PriorityQueue minHeap = new PriorityQueue<>(nums.size(), (a, b) -> a[0] - b[0]); + /*int[] array consists of three numbers: value; which list in nums; index of value in this list*/ + + int max = nums.get(0).get(0); + for (int i = 0; i < nums.size(); i++) { + minHeap.offer(new int[] {nums.get(i).get(0), i, 0}); + max = Math.max(max, nums.get(i).get(0)); + } + int minRange = Integer.MAX_VALUE; + int start = -1; + while (minHeap.size() == nums.size()) { + int[] curr = minHeap.poll(); + if (max - curr[0] < minRange) { + minRange = max - curr[0]; + start = curr[0]; + } + if (curr[2] + 1 < nums.get(curr[1]).size()) { + curr[0] = nums.get(curr[1]).get(curr[2] + 1); + curr[2]++; + minHeap.offer(curr); + max = Math.max(max, curr[0]); + } + } + return new int[] {start, start + minRange}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_633.java b/src/main/java/com/fishercoder/solutions/firstthousand/_633.java new file mode 100644 index 0000000000..1a3259c91d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_633.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _633 { + public static class Solution1 { + public boolean judgeSquareSum(int c) { + if (c < 0) { + return false; + } + int left = 0; + int right = (int) (Math.sqrt(c)); + while (left <= right) { + int curr = left * left + right * right; + if (curr > c) { + right--; + } else if (curr < c) { + left++; + } else { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_634.java b/src/main/java/com/fishercoder/solutions/firstthousand/_634.java new file mode 100644 index 0000000000..26866b4608 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_634.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.firstthousand; + +public class _634 { + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/94442/java-5-lines-o-1-space-solution + * and https://leetcode.com/articles/find-derangements/#approach-5-using-formula-accepted + */ + private static final int M = 1000000007; + + public int findDerangement(int n) { + long ans = 1; + for (int i = 1; i <= n; i++) { + ans = (i * ans % M + (i % 2 == 0 ? 1 : -1)) % M; + } + return (int) ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_635.java b/src/main/java/com/fishercoder/solutions/firstthousand/_635.java new file mode 100644 index 0000000000..6b6f0fa0a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_635.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class _635 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/94449/concise-java-solution + */ + public static class LogSystem { + + /* + * These indices denote and string endings of timestamps of different granularity, i.e. + * timestamp[1] in timestamps: "2017:01:01:22:59:59" + * -> 2017: 4, 01: 7, 01: 10, 22: 13, 59: 16, 59: 19 + */ + + List timestamps; + List units; + int[] indices; + + public LogSystem() { + timestamps = new LinkedList<>(); + units = Arrays.asList("Year", "Month", "Day", "Hour", "Minute", "Second"); + indices = new int[] {4, 7, 10, 13, 16, 19}; + } + + public void put(int id, String timestamp) { + timestamps.add(new String[] {Integer.toString(id), timestamp}); + } + + public List retrieve(String s, String e, String gra) { + List res = new LinkedList<>(); + int index = units.indexOf(gra); + int stringEnd = indices[index]; + for (String[] timestamp : timestamps) { + if (timestamp[1].substring(0, stringEnd).compareTo(s.substring(0, stringEnd)) + >= 0 + && timestamp[1] + .substring(0, stringEnd) + .compareTo(e.substring(0, stringEnd)) + <= 0) { + res.add(Integer.parseInt(timestamp[0])); + } + } + return res; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_636.java b/src/main/java/com/fishercoder/solutions/firstthousand/_636.java new file mode 100644 index 0000000000..0b8e3342f0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_636.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +public class _636 { + public static class Solution1 { + public int[] exclusiveTime(int n, List logs) { + /*Stack is the way to go: + * 1. we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp, + * 2. we'll pop this logId only when we encounter this logId's end timestamp. + * 3. Meanwhile, we keep a counter called prevTime, + * 4. whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/ + Deque stack = new LinkedList<>(); + int[] result = new int[n]; + int prevTime = 0; + for (String log : logs) { + String[] parts = log.split(":"); + if (!stack.isEmpty()) { + result[stack.peekLast()] += Integer.parseInt(parts[2]) - prevTime; + } + prevTime = Integer.parseInt(parts[2]); + if (parts[1].equals("start")) { + stack.addLast(Integer.parseInt(parts[0])); + } else { + // remember to have result plus 1, i.e. when a task starts at 2, ends at 5, it + // should be 5 -2 + 1 = 4 + prevTime++; + result[stack.pollLast()]++; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_637.java b/src/main/java/com/fishercoder/solutions/firstthousand/_637.java new file mode 100644 index 0000000000..0962c32b27 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_637.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _637 { + public static class Solution1 { + public List averageOfLevels(TreeNode root) { + List result = new ArrayList<>(); + if (root == null) { + return result; + } + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + double sum = 0.0; + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (curr != null) { + sum += curr.val; + } + if (curr.left != null) { + queue.offer(curr.left); + } + if (curr.right != null) { + queue.offer(curr.right); + } + } + result.add(sum / size); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_638.java b/src/main/java/com/fishercoder/solutions/firstthousand/_638.java new file mode 100644 index 0000000000..bb45a6c156 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_638.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _638 { + public static class Solution1 { + /* + * reference: https://leetcode.com/articles/shopping-offers/#approach-1-using-recursion-accepted + */ + public int shoppingOffers( + List price, List> special, List needs) { + return shopping(price, special, needs, 0); + } + + public int shopping( + List price, List> special, List needs, int i) { + if (i == special.size()) { + return dot(needs, price); + } + ArrayList clone = new ArrayList(needs); + int j = 0; + for (; j < special.get(i).size() - 1; j++) { + int diff = clone.get(j) - special.get(i).get(j); + if (diff < 0) { + break; + } + clone.set(j, diff); + } + if (j == special.get(i).size() - 1) { + return Math.min( + special.get(i).get(j) + shopping(price, special, clone, i), + shopping(price, special, needs, i + 1)); + } else { + return shopping(price, special, needs, i + 1); + } + } + + public int dot(List a, List b) { + int sum = 0; + for (int i = 0; i < a.size(); i++) { + sum += a.get(i) * b.get(i); + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_639.java b/src/main/java/com/fishercoder/solutions/firstthousand/_639.java new file mode 100644 index 0000000000..db98560d1d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_639.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.firstthousand; + +public class _639 { + public static class Solution1 { + /* + * reference: https://leetcode.com/articles/decode-ways-ii/#approach-2-dynamic-programming-accepted + */ + int m = 1000000007; + + public int numDecodings(String s) { + long[] dp = new long[s.length() + 1]; + dp[0] = 1; + dp[1] = s.charAt(0) == '*' ? 9 : s.charAt(0) == '0' ? 0 : 1; + for (int i = 1; i < s.length(); i++) { + if (s.charAt(i) == '*') { + dp[i + 1] = 9 * dp[i]; + if (s.charAt(i - 1) == '1') { + dp[i + 1] = (dp[i + 1] + 9 * dp[i - 1]) % m; + } else if (s.charAt(i - 1) == '2') { + dp[i + 1] = (dp[i + 1] + 6 * dp[i - 1]) % m; + } else if (s.charAt(i - 1) == '*') { + dp[i + 1] = (dp[i + 1] + 15 * dp[i - 1]) % m; + } + } else { + dp[i + 1] = s.charAt(i) != '0' ? dp[i] : 0; + if (s.charAt(i - 1) == '1') { + dp[i + 1] = (dp[i + 1] + dp[i - 1]) % m; + } else if (s.charAt(i - 1) == '2' && s.charAt(i) <= '6') { + dp[i + 1] = (dp[i + 1] + dp[i - 1]) % m; + } else if (s.charAt(i - 1) == '*') { + dp[i + 1] = (dp[i + 1] + (s.charAt(i) <= '6' ? 2 : 1) * dp[i - 1]) % m; + } + } + } + return (int) dp[s.length()]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_64.java b/src/main/java/com/fishercoder/solutions/firstthousand/_64.java similarity index 94% rename from src/main/java/com/fishercoder/solutions/_64.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_64.java index 4f324b31f0..3386f3229c 100644 --- a/src/main/java/com/fishercoder/solutions/_64.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_64.java @@ -1,8 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _64 { public static class Solution1 { - /** + /* * Same idea as _70: have to initialize the first row and the first column and start the for * loop from i==1 and j==1 for the rest of the matrix. */ diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_640.java b/src/main/java/com/fishercoder/solutions/firstthousand/_640.java new file mode 100644 index 0000000000..cccd90d256 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_640.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +public class _640 { + public static class Solution1 { + /* + * Reference: https://discuss.leetcode.com/topic/95203/concise-java-solution/7 + */ + public String solveEquation(String equation) { + String[] parts = equation.split("="); + int[] left = evaluate(parts[0]); + int[] right = evaluate(parts[1]); + if (left[0] == right[0] && left[1] == right[1]) { + return "Infinite solutions"; + } else if (left[0] == right[0]) { + return "No solution"; + } + return "x=" + (right[1] - left[1]) / (left[0] - right[0]); + } + + private int[] evaluate(String part) { + int[] result = + new int[2]; // result[0] is the coefficient for x, result[1] is the coefficient + // for constants + String[] tokens = + part.split( + "(?=[+-])"); // ()for match group; ?= for match and include in res; [+-] + // means + or -; + for (String token : tokens) { + if (token.equals("+x") || token.equals("x")) { + result[0]++; + } else if (token.equals("-x")) { + result[0]--; + } else if (token.contains("x")) { + result[0] += Integer.parseInt(token.substring(0, token.length() - 1)); + } else { + result[1] += Integer.parseInt(token); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_642.java b/src/main/java/com/fishercoder/solutions/firstthousand/_642.java new file mode 100644 index 0000000000..e5edcfe8bb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_642.java @@ -0,0 +1,73 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class _642 { + public static class Solution1 { + + /* + * reference: https://discuss.leetcode.com/topic/96150/java-solution-trie-and-priorityqueue/3 + */ + public class AutocompleteSystem { + + Map map; + List> answers; + StringBuilder stringBuilder; + + public AutocompleteSystem(String[] sentences, int[] times) { + map = new HashMap<>(); + answers = new ArrayList<>(); + stringBuilder = new StringBuilder(); + + for (int i = 0; i < sentences.length; i++) { + map.put(sentences[i], map.getOrDefault(sentences[i], 0) + times[i]); + } + } + + public List input(char c) { + List result = new ArrayList<>(); + if (c == '#') { + map.put( + stringBuilder.toString(), + map.getOrDefault(stringBuilder.toString(), 0) + 1); + stringBuilder.setLength(0); + answers + .clear(); /*The user has finished typing, so we'll clean answers to get ready for next search*/ + } else { + stringBuilder.append(c); + /*when its length is 1, we find all the prefix that is a match and put them into answers, + * then for the rest, we'll just remove those that are not match with the prefix any more, we do this logic in else branch*/ + if (stringBuilder.length() == 1) { + for (Map.Entry entry : map.entrySet()) { + if (entry.getKey().startsWith(stringBuilder.toString())) { + answers.add(entry); + } + } + Collections.sort( + answers, + (a, b) -> + a.getValue() == b.getValue() + ? a.getKey().compareTo(b.getKey()) + : b.getValue() - a.getValue()); + } else { + for (Iterator> iterator = answers.iterator(); + iterator.hasNext(); ) { + if (!iterator.next().getKey().startsWith(stringBuilder.toString())) { + iterator.remove(); + } + } + } + for (int i = 0; i < 3 && i < answers.size(); i++) { + result.add(answers.get(i).getKey()); + } + } + return result; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_643.java b/src/main/java/com/fishercoder/solutions/firstthousand/_643.java new file mode 100644 index 0000000000..bfc8fb04de --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_643.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +public class _643 { + + public static class Solution1 { + public double findMaxAverage(int[] nums, int k) { + double sum = 0; + double maxAve = Integer.MIN_VALUE; + for (int i = 0; i < nums.length; i++) { + if (k <= i) { + sum -= nums[i - k]; + } + sum += nums[i]; + if ((i + 1) >= k) { + maxAve = Math.max(maxAve, sum / k); + } + } + return maxAve; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_644.java b/src/main/java/com/fishercoder/solutions/firstthousand/_644.java new file mode 100644 index 0000000000..7c9d47ad94 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_644.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.firstthousand; + +public class _644 { + /*reference: https://leetcode.com/articles/maximum-average-subarray-ii/#approach-2-using-binary-search-accepted + * https://discuss.leetcode.com/topic/96123/java-solution-o-nlogm-binary-search-the-answer/13*/ + + /* + * To understand the idea behind this method, let's look at the following points. + * Firstly, we know that the value of the average could lie between the range (min, max)(min,max). + * Here, minmin and maxmax refer to the minimum and the maximum values out of the given numsnums array. + * This is because, the average can't be lesser than the minimum value and can't be larger than the maximum value. + * But, in this case, we need to find the maximum average of a subarray with atleast kk elements. + * The idea in this method is to try to approximate(guess) the solution and to try to find if this solution really exists. + * If it exists, we can continue trying to approximate the solution even to a further precise value, + * but choosing a larger number as the next approximation. + * But, if the initial guess is wrong, and the initial maximum average value(guessed) isn't possible, + * we need to try with a smaller number as the next approximate. + *

+ * Now, instead of doing the guesses randomly, we can make use of Binary Search. + * With minmin and maxmax as the initial numbers to begin with, + * we can find out the midmid of these two numbers given by (min+max)/2(min+max)/2. + * Now, we need to find if a subarray with length greater than or equal to kk is possible with an average sum greater than this midmid value. + */ + public double findMaxAverage(int[] nums, int k) { + double l = -10000; + double r = 10000; + while (r - l > 10e-7) { + double mid = (l + r) / 2; + if (getMaxSubbaraySumOfSizeK(nums, k, mid) >= 0) { + l = mid; + } else { + r = mid; + } + } + return (l + r) / 2; + } + + private double getMaxSubbaraySumOfSizeK(int[] nums, int k, double mid) { + double sum = 0.0; + for (int i = 0; i <= k - 1; i++) { + sum += nums[i] - mid; + } + double maxSum = sum; + double prev = nums[0] - mid; + for (int i = k; i < nums.length; i++) { + sum = sum - nums[i - k] + nums[i]; + maxSum = Math.max(maxSum, Math.max(sum, sum + prev)); + prev = Math.max(nums[i - k + 1], nums[i - k + 1] + prev) - mid; + } + return maxSum; + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_645.java b/src/main/java/com/fishercoder/solutions/firstthousand/_645.java new file mode 100644 index 0000000000..dc9217dfdf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_645.java @@ -0,0 +1,68 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _645 { + public static class Solution1 { + public int[] findErrorNums(int[] nums) { + int[] result = new int[2]; + Arrays.sort(nums); + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i + 1] == nums[i]) { + result[0] = nums[i]; + } + } + long temp = 0; + for (int i = 0; i < nums.length; i++) { + temp += i + 1; + temp -= nums[i]; + } + temp += result[0]; + result[1] = (int) temp; + return result; + } + } + + public static class Solution2 { + public int[] findErrorNums(int[] nums) { + Set set = new HashSet(); + boolean[] has = new boolean[nums.length]; + int dup = -1; + for (int i = 0; i < nums.length; i++) { + has[nums[i] - 1] = true; + if (!set.add(nums[i])) { + dup = nums[i]; + } + } + int dup2 = -1; + for (int i = 0; i < has.length; i++) { + if (!has[i]) { + dup2 = i + 1; + } + } + return new int[] {dup, dup2}; + } + } + + public static class Solution3 { + public int[] findErrorNums(int[] nums) { + int[] arr = new int[nums.length]; + Set set = new HashSet<>(); + int[] result = new int[2]; + for (int i = 0; i < nums.length; i++) { + arr[i] = i + 1; + if (!set.add(nums[i])) { + result[0] = nums[i]; + } + } + for (int i = 0; i < nums.length; i++) { + if (!set.contains(i + 1)) { + result[1] = i + 1; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_646.java b/src/main/java/com/fishercoder/solutions/firstthousand/_646.java new file mode 100644 index 0000000000..8c9c4e0dfa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_646.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _646 { + /* + * Although this problem could be solved using DP, greedy is more efficient in both time and space complexity. + */ + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/maximum-length-of-pair-chain/editorial/ + */ + public int findLongestChain(int[][] pairs) { + // sort by the second element + Arrays.sort(pairs, (a, b) -> a[1] - b[1]); + int ans = 0; + int prev = Integer.MIN_VALUE; + for (int[] pair : pairs) { + if (pair[0] > prev) { + ans++; + prev = pair[1]; + } + } + return ans; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/maximum-length-of-pair-chain/discuss/105623/Java-Very-Simple-without-DP + */ + public int findLongestChain(int[][] pairs) { + // sort by the first element + Arrays.sort(pairs, (a, b) -> a[0] - b[0]); + int len = 0; + int pre = Integer.MIN_VALUE; + for (int[] pair : pairs) { + if (pair[0] > pre) { + // no overlap + len++; + // so we need to update the previous number to be the end of current pair: + // pair[1] + pre = pair[1]; + } else if (pair[1] < pre) { + // overlap but with a smaller second number + // since we want to find the maximum possible chain, so we update pre to be this + // smaller number + // this means we decided to adopt this pair to be in this chain and give up the + // previous one + // this logic can be seen clearly in test3 for this class + pre = pair[1]; + } + } + return len; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_647.java b/src/main/java/com/fishercoder/solutions/firstthousand/_647.java new file mode 100644 index 0000000000..17ba043966 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_647.java @@ -0,0 +1,56 @@ +package com.fishercoder.solutions.firstthousand; + +public class _647 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/96819/java-solution-8-lines-extendpalindrome + */ + public int countSubstrings(String s) { + int count = 0; + for (int i = 0; i < s.length(); i++) { + count += extendPalindrome(s, i, i); // odd length + count += extendPalindrome(s, i, i + 1); // even length + } + return count; + } + + private int extendPalindrome(String s, int left, int right) { + int count = 0; + while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { + count++; + left--; + right++; + } + return count; + } + } + + public static class Solution2 { + /* + * Simple brute force solution is accepted as well, although not ideal in terms of time complexity: O(n^2) + */ + public int countSubstrings(String s) { + int result = 0; + for (int i = 0; i < s.length(); i++) { + for (int j = i + 1; j <= s.length(); j++) { + if (isPal(s.substring(i, j))) { + result++; + } + } + } + return result; + } + + private boolean isPal(String str) { + int i = 0; + int j = str.length() - 1; + while (i < j) { + if (str.charAt(i++) != str.charAt(j--)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_648.java b/src/main/java/com/fishercoder/solutions/firstthousand/_648.java new file mode 100644 index 0000000000..5a3203c618 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_648.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.List; + +public class _648 { + public static class Solution1 { + public String replaceWords(List dict, String sentence) { + String[] tokens = sentence.split(" "); + TrieNode trie = buildTrie(dict); + return replaceWords(tokens, trie); + } + + private String replaceWords(String[] tokens, TrieNode root) { + StringBuilder stringBuilder = new StringBuilder(); + for (String token : tokens) { + stringBuilder.append(getShortestReplacement(token, root)); + stringBuilder.append(" "); + } + return stringBuilder.substring(0, stringBuilder.length() - 1); + } + + private String getShortestReplacement(String token, final TrieNode root) { + TrieNode temp = root; + StringBuilder stringBuilder = new StringBuilder(); + for (char c : token.toCharArray()) { + stringBuilder.append(c); + if (temp.children[c - 'a'] != null) { + if (temp.children[c - 'a'].isWord) { + return stringBuilder.toString(); + } + temp = temp.children[c - 'a']; + } else { + return token; + } + } + return token; + } + + private TrieNode buildTrie(List dict) { + TrieNode root = new TrieNode(' '); + for (String word : dict) { + TrieNode temp = root; + for (char c : word.toCharArray()) { + if (temp.children[c - 'a'] == null) { + temp.children[c - 'a'] = new TrieNode(c); + } + temp = temp.children[c - 'a']; + } + temp.isWord = true; + } + return root; + } + + public class TrieNode { + char val; + TrieNode[] children; + boolean isWord; + + public TrieNode(char val) { + this.val = val; + this.children = new TrieNode[26]; + this.isWord = false; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_649.java b/src/main/java/com/fishercoder/solutions/firstthousand/_649.java new file mode 100644 index 0000000000..9ca12a0ef7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_649.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _649 { + + public static class Solution1 { + public String predictPartyVictory(String senate) { + Queue radiantQ = new LinkedList<>(); + Queue direQ = new LinkedList<>(); + int len = senate.length(); + for (int i = 0; i < len; i++) { + if (senate.charAt(i) == 'R') { + radiantQ.offer(i); + } else { + direQ.offer(i); + } + } + while (!radiantQ.isEmpty() && !direQ.isEmpty()) { + int radiantIndex = radiantQ.poll(); + int direIndex = direQ.poll(); + if (radiantIndex < direIndex) { + /*Radiant will ban Dire in this case, so we'll add radiant index back to the queue plus n*/ + radiantQ.offer(radiantIndex + len); + } else { + direQ.offer(direIndex + len); + } + } + return radiantQ.isEmpty() ? "Dire" : "Radiant"; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_65.java b/src/main/java/com/fishercoder/solutions/firstthousand/_65.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_65.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_65.java index 0d60a2ba54..a88d813181 100644 --- a/src/main/java/com/fishercoder/solutions/_65.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_65.java @@ -1,7 +1,7 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _65 { - /** + /* * credit: https://discuss.leetcode.com/topic/9490/clear-java-solution-with-ifs */ public static class Solution1 { @@ -41,7 +41,7 @@ public boolean isNumber(String s) { } public static class Solution2 { - /** + /* * credit: https://discuss.leetcode.com/topic/2973/java-solution-with-one-line */ public boolean isNumber(String s) { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_650.java b/src/main/java/com/fishercoder/solutions/firstthousand/_650.java new file mode 100644 index 0000000000..d53725397e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_650.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _650 { + + public static class Solution1 { + public int minSteps(int n) { + int[] dp = new int[n + 1]; + for (int i = 2; i <= n; i++) { + dp[i] = i; // we assign i to dp[i] first, because for a lot of cases, e.g. for most + // cases when i is odd, its min steps is i itself, if it's not, we can + // overwrite it later + for (int j = i - 1; j > 1; j--) { + // traverse backwards, whenever it's divisible by j, we'll update dp[i] because + // it's guaranteed to be smaller when j is smaller. + if (i % j == 0) { + dp[i] = dp[j] + (i / j); + break; + } + } + } + return dp[n]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_651.java b/src/main/java/com/fishercoder/solutions/firstthousand/_651.java new file mode 100644 index 0000000000..b79ef58e5d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_651.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +public class _651 { + + public static class Solution1 { + /* + * Minimum needs to be more than 3 A's in a row, otherwise "Ctrl A, Ctrl C, Ctrl V" will make fewer A's than directly + * copying A's with the equal number of steps. + * E.g. when n == 5, + * if we do 5 this: A, A, Ctrl A, Ctrl C, Ctrl V, => this will result in only AAAA (4 A's) + * while if we do A, A, A, A, A, => this will result in AAAAA (5 A's) + * So, at a minimum, we need to have 3 A's, then it's worth to do "Ctrl A, Ctrl C, Ctrl V".. + * That's why we have j = 3 in the inner for loop below. + */ + public int maxA(int N) { + int[] dp = new int[N + 1]; + for (int i = 1; i <= N; i++) { + dp[i] = i; + for (int j = 3; j < i; j++) { + dp[i] = Math.max(dp[i], dp[i - j] * (j - 1)); + } + } + return dp[N]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_652.java b/src/main/java/com/fishercoder/solutions/firstthousand/_652.java new file mode 100644 index 0000000000..57dcbca1fd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_652.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; + +public class _652 { + public static class Solution1 { + + /*credit: https://discuss.leetcode.com/topic/97584/java-concise-postorder-traversal-solution*/ + + /* + * You don't actually need to check if every other tree is a duplicate of current node, + * just when you go through each node, you'll see whether there's already one in the map, + * since map.containsKey() checks this TreeNode. + */ + public List findDuplicateSubtrees(TreeNode root) { + List res = new LinkedList<>(); + postorder(root, new HashMap<>(), res); + return res; + } + + private String postorder(TreeNode curr, HashMap map, List res) { + if (curr == null) { + return "#"; + } + String serial = + curr.val + + "," + + postorder(curr.left, map, res) + + "," + + postorder(curr.right, map, res); + if (map.getOrDefault(serial, 0) == 1) { + res.add(curr); + } + map.put(serial, map.getOrDefault(serial, 0) + 1); + return serial; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_653.java b/src/main/java/com/fishercoder/solutions/firstthousand/_653.java new file mode 100644 index 0000000000..b1694793e4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_653.java @@ -0,0 +1,56 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _653 { + + public static class Solution1 { + public boolean findTarget(TreeNode root, int k) { + if (root == null) { + return false; + } + List list = new ArrayList<>(); + dfs(root, list); + for (int i = 0; i < list.size() - 1; i++) { + for (int j = i + 1; j < list.size(); j++) { + if (list.get(i) + list.get(j) == k) { + return true; + } + } + } + return false; + } + + private void dfs(TreeNode root, List list) { + list.add(root.val); + if (root.left != null) { + dfs(root.left, list); + } + if (root.right != null) { + dfs(root.right, list); + } + } + } + + public static class Solution2 { + public boolean findTarget(TreeNode root, int k) { + return dfs(root, new HashSet(), k); + } + + private boolean dfs(TreeNode root, Set set, int k) { + if (root == null) { + return false; + } + + if (set.contains(k - root.val)) { + return true; + } + set.add(root.val); + return dfs(root.left, set, k) || dfs(root.right, set, k); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_654.java b/src/main/java/com/fishercoder/solutions/firstthousand/_654.java new file mode 100644 index 0000000000..86afaaed2f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_654.java @@ -0,0 +1,93 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _654 { + + public static class Solution1 { + /* + * Completely my original solution: + *

+ * As the problem states, I always broke the array into two halves and make notes + * of current max node, then in the recursive call, we can recursively search + * from its left part to construct its left subtree and its right part to construct its + * right subtree. + */ + public TreeNode constructMaximumBinaryTree(int[] nums) { + int max = Integer.MIN_VALUE; + int maxIndex = -1; + for (int i = 0; i < nums.length; i++) { + if (nums[i] > max) { + max = nums[i]; + maxIndex = i; + } + } + TreeNode root = new TreeNode(max); + return constructMaxTree(root, maxIndex, nums, 0, nums.length - 1); + } + + private TreeNode constructMaxTree( + TreeNode root, int rootIndex, int[] nums, int start, int end) { + if (rootIndex > start) { + int max = Integer.MIN_VALUE; + int maxIndex = -1; + for (int i = start; i < rootIndex; i++) { + if (max < nums[i]) { + max = nums[i]; + maxIndex = i; + } + } + root.left = + constructMaxTree(new TreeNode(max), maxIndex, nums, start, rootIndex - 1); + } + if (rootIndex < end) { + int max = Integer.MIN_VALUE; + int maxIndex = -1; + for (int i = rootIndex + 1; i <= end; i++) { + if (max < nums[i]) { + max = nums[i]; + maxIndex = i; + } + } + root.right = + constructMaxTree(new TreeNode(max), maxIndex, nums, rootIndex + 1, end); + } + return root; + } + } + + public static class Solution2 { + /* + * Completely my original solution as well, but more concise. + */ + public TreeNode constructMaximumBinaryTree(int[] nums) { + if (nums == null || nums.length == 0) { + return null; + } + return construct(nums, 0, nums.length - 1); + } + + TreeNode construct(int[] nums, int start, int end) { + if (start > end) { + return null; + } + int[] maxArray = findMax(nums, start, end); + TreeNode root = new TreeNode(maxArray[0]); + root.left = construct(nums, start, maxArray[1] - 1); + root.right = construct(nums, maxArray[1] + 1, end); + return root; + } + + int[] findMax(int[] nums, int start, int end) { + int max = nums[start]; + int maxIndex = start; + for (int i = start + 1; i <= end; i++) { + if (max < nums[i]) { + maxIndex = i; + max = nums[i]; + } + } + return new int[] {max, maxIndex}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_655.java b/src/main/java/com/fishercoder/solutions/firstthousand/_655.java new file mode 100644 index 0000000000..3749cafbfe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_655.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +public class _655 { + + public static class Solution1 { + + /* + * Reference: https://discuss.leetcode.com/topic/98381/java-recursive-solution + * and https://leetcode.com/articles/print-binary-tree/ + */ + public List> printTree(TreeNode root) { + List> result = new LinkedList<>(); + int height = root == null ? 1 : getHeight(root); + int rows = height; + int columns = (int) (Math.pow(2, height) - 1); + List row = new ArrayList<>(); + for (int i = 0; i < columns; i++) { + row.add(""); + } + for (int i = 0; i < rows; i++) { + result.add(new ArrayList<>(row)); + } + populateResult(root, result, 0, rows, 0, columns - 1); + return result; + } + + private void populateResult( + TreeNode root, List> result, int row, int totalRows, int i, int j) { + if (row == totalRows || root == null) { + return; + } + result.get(row).set((i + j) / 2, Integer.toString(root.val)); + populateResult(root.left, result, row + 1, totalRows, i, (i + j) / 2 - 1); + populateResult(root.right, result, row + 1, totalRows, (i + j) / 2 + 1, j); + } + + private int getHeight(TreeNode root) { + if (root == null) { + return 0; + } + return 1 + Math.max(getHeight(root.left), getHeight(root.right)); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_656.java b/src/main/java/com/fishercoder/solutions/firstthousand/_656.java new file mode 100644 index 0000000000..90e84c2dc0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_656.java @@ -0,0 +1,44 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _656 { + public static class Solution1 { + + /* + * Time: O(n*B) + * Reference: https://leetcode.com/articles/coin-path/#approach-3-using-dynamic-programming-accepted + */ + public List cheapestJump(int[] A, int B) { + int[] next = new int[A.length]; + long[] dp = new long[A.length]; + Arrays.fill(next, -1); + List res = new ArrayList(); + for (int i = A.length - 2; i >= 0; i--) { + long minCost = Integer.MAX_VALUE; + for (int j = i + 1; j <= i + B && j < A.length; j++) { + if (A[j] >= 0) { + long cost = A[i] + dp[j]; + if (cost < minCost) { + minCost = cost; + next[i] = j; + } + } + } + dp[i] = minCost; + } + int i; + for (i = 0; i < A.length && next[i] > 0; i = next[i]) { + res.add(i + 1); + } + if (i == A.length - 1 && A[i] >= 0) { + res.add(A.length); + } else { + return new ArrayList<>(); + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_657.java b/src/main/java/com/fishercoder/solutions/firstthousand/_657.java new file mode 100644 index 0000000000..8af96ac1d2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_657.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _657 { + public static class Solution1 { + public boolean judgeCircle(String moves) { + int hori = 0; + int verti = 0; + for (char c : moves.toCharArray()) { + if (c == 'U') { + verti++; + } else if (c == 'D') { + verti--; + } else if (c == 'L') { + hori--; + } else if (c == 'R') { + hori++; + } + } + return verti == 0 && hori == 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_658.java b/src/main/java/com/fishercoder/solutions/firstthousand/_658.java new file mode 100644 index 0000000000..a505ef775e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_658.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _658 { + public static class Solution1 { + public List findClosestElements(List arr, int k, int x) { + List result = new ArrayList<>(); + int index = findInsertPosition(arr, x); + int right = 0; + if (arr.get(index) == x) { + result.add(arr.get(index)); + right = index + 1; + k--; + } else { + right = index; + } + int left = index - 1; + while (k-- > 0) { + if (left >= 0 && right < arr.size()) { + if (x - arr.get(left) <= arr.get(right) - x) { + result.add(0, arr.get(left--)); + } else { + result.add(result.size(), arr.get(right++)); + } + } else if (left >= 0) { + result.add(0, arr.get(left--)); + } else if (right < arr.size()) { + result.add(result.size(), arr.get(right++)); + } + } + return result; + } + + private int findInsertPosition(List arr, int x) { + if (arr == null || arr.size() == 0) { + return 0; + } + int len = arr.size(); + for (int i = 0; i < len; i++) { + if (arr.get(0) > x) { + return 0; + } else if (arr.get(len - 1) < x) { + return len; + } else if (arr.get(i) < x && arr.get(i + 1) > x) { + return i + 1; + } else if (arr.get(i) == x) { + return i; + } + } + return len; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_659.java b/src/main/java/com/fishercoder/solutions/firstthousand/_659.java new file mode 100644 index 0000000000..41bbbcaaa5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_659.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _659 { + + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/99187/java-o-n-time-o-n-space + */ + public boolean isPossible(int[] nums) { + Map freqMap = new HashMap<>(); + for (int i : nums) { + freqMap.put(i, freqMap.getOrDefault(i, 0) + 1); + } + Map appendFreqMap = new HashMap<>(); + for (int i : nums) { + if (freqMap.get(i) == 0) { + continue; + } else if (appendFreqMap.getOrDefault(i, 0) > 0) { + appendFreqMap.put(i, appendFreqMap.get(i) - 1); + appendFreqMap.put(i + 1, appendFreqMap.getOrDefault(i + 1, 0) + 1); + } else if (freqMap.getOrDefault(i + 1, 0) > 0 + && freqMap.getOrDefault(i + 2, 0) > 0) { + freqMap.put(i + 1, freqMap.get(i + 1) - 1); + freqMap.put(i + 2, freqMap.get(i + 2) - 1); + appendFreqMap.put(i + 3, appendFreqMap.getOrDefault(i + 3, 0) + 1); + } else { + return false; + } + freqMap.put(i, freqMap.get(i) - 1); + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_66.java b/src/main/java/com/fishercoder/solutions/firstthousand/_66.java new file mode 100644 index 0000000000..c866392753 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_66.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +public class _66 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/plus-one/discuss/24082/My-Simple-Java-Solution + */ + public int[] plusOne(int[] digits) { + int len = digits.length; + for (int i = len - 1; i >= 0; i--) { + if (digits[i] < 9) { + digits[i]++; + return digits; + } + digits[i] = 0; + } + int[] newNumber = new int[len + 1]; + newNumber[0] = 1; + return newNumber; + } + } + + public static class Solution2 { + public int[] plusOne(int[] digits) { + int len = digits.length; + for (int i = len - 1; i >= 0; i--) { + if (digits[i] == 9) { + digits[i] = 0; + } else { + digits[i]++; + return digits; + } + } + int[] newNumber = new int[len + 1]; + newNumber[0] = 1; + return newNumber; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_660.java b/src/main/java/com/fishercoder/solutions/firstthousand/_660.java new file mode 100644 index 0000000000..aae272cc8c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_660.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +public class _660 { + + public static class Solution1 { + public int newInteger(int n) { + return Integer.parseInt(Integer.toString(n, 9)); + } + } + + public static class Solution2 { + public int newInteger(int n) { + int result = 0; + int base = 1; + while (n > 0) { + result += n % 9 * base; + n /= 9; + base *= 10; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_661.java b/src/main/java/com/fishercoder/solutions/firstthousand/_661.java new file mode 100644 index 0000000000..4c8441add2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_661.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +public class _661 { + public static class Solution1 { + public int[][] imageSmoother(int[][] M) { + if (M == null || M.length == 0) { + return M; + } + int m = M.length; + int n = M[0].length; + int[][] result = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + bfs(M, i, j, result, m, n); + } + } + return result; + } + + private void bfs(int[][] M, int i, int j, int[][] result, int m, int n) { + int sum = M[i][j]; + int denominator = 1; + if (j + 1 < n) { + sum += M[i][j + 1]; + denominator++; + } + if (i + 1 < m && j + 1 < n) { + sum += M[i + 1][j + 1]; + denominator++; + } + if (i + 1 < m) { + sum += M[i + 1][j]; + denominator++; + } + if (i + 1 < m && j - 1 >= 0) { + sum += M[i + 1][j - 1]; + denominator++; + } + if (j - 1 >= 0) { + sum += M[i][j - 1]; + denominator++; + } + if (i - 1 >= 0 && j - 1 >= 0) { + sum += M[i - 1][j - 1]; + denominator++; + } + if (i - 1 >= 0) { + sum += M[i - 1][j]; + denominator++; + } + if (i - 1 >= 0 && j + 1 < n) { + sum += M[i - 1][j + 1]; + denominator++; + } + result[i][j] = sum / denominator; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_662.java b/src/main/java/com/fishercoder/solutions/firstthousand/_662.java new file mode 100644 index 0000000000..8bb0856fe2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_662.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +public class _662 { + public static class Solution1 { + /* + * Use a map to store the node to value map, + * we use root as index 1, then its left child is 2*i-1 and right child is 2*i + */ + public int widthOfBinaryTree(TreeNode root) { + if (root == null) { + return 0; + } + + Queue> queue = new LinkedList<>(); + queue.offer(new AbstractMap.SimpleEntry<>(root, 1)); + int max = 1; + while (!queue.isEmpty()) { + int size = queue.size(); + List> list = new ArrayList<>(); + for (int i = 0; i < size; i++) { + Map.Entry curr = queue.poll(); + if (curr.getKey().left != null) { + Map.Entry newEntry = + new AbstractMap.SimpleEntry<>( + curr.getKey().left, curr.getValue() * 2 - 1); + queue.offer(newEntry); + list.add(newEntry); + } + if (curr.getKey().right != null) { + Map.Entry newEntry = + new AbstractMap.SimpleEntry<>( + curr.getKey().right, curr.getValue() * 2); + queue.offer(newEntry); + list.add(newEntry); + } + } + if (list.size() > 1) { + max = + Math.max( + list.get(list.size() - 1).getValue() + - list.get(0).getValue() + + 1, + max); + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_663.java b/src/main/java/com/fishercoder/solutions/firstthousand/_663.java new file mode 100644 index 0000000000..dbacba9459 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_663.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.Map; + +public class _663 { + public static class Solution1 { + /* + * The idea is that we use a map to store the sum of each node, then in the end, + * we check if any node has a sum that is exactly half of total sum. + */ + public boolean checkEqualTree(TreeNode root) { + Map map = new HashMap<>(); + int totalSum = sumForEachNode(root, map); + if (totalSum % 2 != 0 || map.size() < 2) { + return false; + } + for (TreeNode key : map.keySet()) { + if (map.get(key) == totalSum / 2) { + return true; + } + } + return false; + } + + private int sumForEachNode(TreeNode root, Map map) { + if (root == null) { + return 0; + } + if (root.left == null && root.right == null) { + map.put(root, root.val); + return root.val; + } + int leftVal = 0; + if (root.left != null) { + leftVal = sumForEachNode(root.left, map); + } + int rightVal = 0; + if (root.right != null) { + rightVal = sumForEachNode(root.right, map); + } + int val = root.val + leftVal + rightVal; + map.put(root, val); + return val; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_664.java b/src/main/java/com/fishercoder/solutions/firstthousand/_664.java new file mode 100644 index 0000000000..91ed02c4d3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_664.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +public class _664 { + public static class Solution1 { + /* + * reference: https://discuss.leetcode.com/topic/100137/java-solution-dp + */ + public int strangePrinter(String s) { + int n = s.length(); + if (n == 0) { + return 0; + } + + int[][] dp = new int[101][101]; + for (int i = 0; i < n; i++) { + dp[i][i] = 1; + } + + for (int i = 1; i < n; i++) { + for (int j = 0; j < n - i; j++) { + dp[j][j + i] = i + 1; + for (int k = j + 1; k <= j + i; k++) { + int temp = dp[j][k - 1] + dp[k][j + i]; + if (s.charAt(k - 1) == s.charAt(j + i)) { + temp--; + } + dp[j][j + i] = Math.min(dp[j][j + i], temp); + } + } + } + return dp[0][n - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_665.java b/src/main/java/com/fishercoder/solutions/firstthousand/_665.java similarity index 75% rename from src/main/java/com/fishercoder/solutions/_665.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_665.java index d91b17965b..362d860cc3 100644 --- a/src/main/java/com/fishercoder/solutions/_665.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_665.java @@ -1,26 +1,5 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; -/** - * 665. Non-decreasing Array - * - * Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element. - - We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n). - - Example 1: - - Input: [4,2,3] - Output: True - Explanation: You could modify the first 4 to 1 to get a non-decreasing array. - - Example 2: - - Input: [4,2,1] - Output: False - Explanation: You can't get a non-decreasing array by modify at most one element. - - Note: The n belongs to [1, 10,000]. - */ public class _665 { public static class Solution1 { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_666.java b/src/main/java/com/fishercoder/solutions/firstthousand/_666.java new file mode 100644 index 0000000000..877d111f7c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_666.java @@ -0,0 +1,168 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.Map; + +public class _666 { + public static class Solution1 { + /* + * OMG, since it's no larger than depth 5, I've got a hardcoded solution here.... + * By "harcoded", I mean the constructTree() method. + */ + public int totalSum = 0; + + public int pathSum(int[] nums) { + TreeNode root = constructTree(nums); + if (root == null) { + return 0; + } + computePathSum(root, 0); + return totalSum; + } + + private void computePathSum(TreeNode root, int pathSum) { + pathSum += root.val; + if (root.left != null) { + computePathSum(root.left, pathSum); + } + if (root.right != null) { + computePathSum(root.right, pathSum); + } + if (root.left == null && root.right == null) { + totalSum += pathSum; + } + // pathSum -= root.val; + /*this line is not necessary as I'm passing pathSum as a local variable around, so it's always updated + it's AC'ed with or without this line*/ + } + + private TreeNode constructTree(int[] nums) { + if (nums == null || nums.length == 0) { + return null; + } + TreeNode root = + new TreeNode(Integer.parseInt(Integer.toString(nums[0]).substring(2, 3))); + // depth 2 + for (int i = 1; i < nums.length; i++) { + if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) { + root.left = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) { + root.right = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } + } + + // depth 3 + for (int i = 2; i < nums.length; i++) { + if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) { + root.left.left = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) { + root.left.right = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) { + root.right.left = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) { + root.right.right = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } + } + + // depth 4 + for (int i = 3; i < nums.length; i++) { + if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) { + root.left.left.left = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) { + root.left.left.right = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) { + root.left.right.left = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) { + root.left.right.right = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 5) { + root.right.left.left = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 6) { + root.right.left.right = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 7) { + root.right.right.left = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 + && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 8) { + root.right.right.right = + new TreeNode( + Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); + } + } + + return root; + } + } + + public static class Solution2 { + public int totalSum = 0; + + public int pathSum(int[] nums) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + int key = nums[i] / 10; + int value = nums[i] % 10; + map.put(key, value); + } + dfs(nums[0] / 10, 0, map); + return totalSum; + } + + private void dfs(int node, int preSum, Map map) { + int level = node / 10; + int pos = node % 10; + int leftChild = (level + 1) * 10 + pos * 2 - 1; + int rightChild = (level + 1) * 10 + pos * 2; + int currSum = preSum + map.get(node); + if (!map.containsKey(leftChild) && !map.containsKey(rightChild)) { + totalSum += currSum; + return; + } + + if (map.containsKey(leftChild)) { + dfs(leftChild, currSum, map); + } + if (map.containsKey(rightChild)) { + dfs(rightChild, currSum, map); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_667.java b/src/main/java/com/fishercoder/solutions/firstthousand/_667.java new file mode 100644 index 0000000000..6d89ec67ec --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_667.java @@ -0,0 +1,68 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _667 { + + public static class Solutoin1 { + /* + * inspired by this post: https://leetcode.com/problems/beautiful-arrangement-ii/discuss/1154683/Short-and-Simple-Solution-or-Multiple-Approaches-Explained-with-Examples-! and implemented it on my own + */ + public int[] constructArray(int n, int k) { + List list = new ArrayList<>(); + int maxSoFar = 1; + list.add(1); + boolean plus = true; + while (k > 0) { + if (plus) { + plus = false; + int num = list.get(list.size() - 1) + k; + maxSoFar = Math.max(maxSoFar, num); + list.add(num); + } else { + plus = true; + list.add(list.get(list.size() - 1) - k); + } + k--; + } + for (int start = maxSoFar + 1; start <= n; start++) { + list.add(start); + } + int[] result = new int[n]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + } + + public static class Solutoin2 { + /* + * This is a very smart solution: + * First, we can see that the max value k could reach is n-1 which + * comes from a sequence like this: + * when n = 8, k = 5, one possible sequence is: + * 1, 8, 2, 7, 3, 4, 5, 6 + * absolute diffs are: + * 7, 6, 5, 4, 1, 1, 1 + * so, there are total 5 distinct integers. + *

+ * So, we can just form such a sequence by putting the first part first and + * decrement k along the way, when k becomes 1, we just put the rest numbers in order. + */ + public int[] constructArray(int n, int k) { + int[] result = new int[n]; + int left = 1; + int right = n; + for (int i = 0; i < n && left <= right; i++) { + if (k > 1) { + result[i] = k-- % 2 != 0 ? left++ : right--; + } else { + result[i] = k % 2 != 0 ? left++ : right--; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_668.java b/src/main/java/com/fishercoder/solutions/firstthousand/_668.java new file mode 100644 index 0000000000..ee00d447c9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_668.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.PriorityQueue; + +public class _668 { + public static class Solution1 { + /* + * This brute force approach resulted in + * TLE on Leetcode and + * OOM error by _668test.test3() when running in my localhost: + * java.lang.OutOfMemoryError: Java heap space + * at java.util.Arrays.copyOf(Arrays.java:3210) + * at java.util.Arrays.copyOf(Arrays.java:3181) + * at java.util.PriorityQueue.grow(PriorityQueue.java:300) + * at java.util.PriorityQueue.offer(PriorityQueue.java:339) + */ + public int findKthNumber(int m, int n, int k) { + PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a - b); + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + minHeap.offer(i * j); + } + } + while (k-- > 1) { + minHeap.poll(); + } + return minHeap.peek(); + } + } + + public static class Solution2 { + /* + * reference: https://discuss.leetcode.com/topic/101132/java-solution-binary-search + */ + public int findKthNumber(int m, int n, int k) { + int low = 1; + int high = m * n + 1; + while (low < high) { + int mid = low + (high - low) / 2; + int c = count(mid, m, n); + if (c >= k) { + high = mid; + } else { + low = mid + 1; + } + } + return high; + } + + int count(int v, int m, int n) { + int count = 0; + for (int i = 1; i <= m; i++) { + int temp = Math.min(v / i, n); + count += temp; + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_669.java b/src/main/java/com/fishercoder/solutions/firstthousand/_669.java new file mode 100644 index 0000000000..0a9f4ef26d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_669.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _669 { + public static class Solution1 { + public TreeNode trimBST(TreeNode root, int L, int R) { + if (root == null) { + return root; + } + + if (root.val > R) { + return trimBST(root.left, L, R); + } + + if (root.val < L) { + return trimBST(root.right, L, R); + } + + root.left = trimBST(root.left, L, R); + root.right = trimBST(root.right, L, R); + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_67.java b/src/main/java/com/fishercoder/solutions/firstthousand/_67.java similarity index 95% rename from src/main/java/com/fishercoder/solutions/_67.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_67.java index 456d1dd21d..0ecb54e9a5 100644 --- a/src/main/java/com/fishercoder/solutions/_67.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_67.java @@ -1,8 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _67 { public static class Solution1 { - /** + /* * credit: https://discuss.leetcode.com/topic/13698/short-ac-solution-in-java-with-explanation * 1. use StringBuilder.reverse() function! Nice! * 2. if a numeric number is represented/stored in String, how to get its value: use Character.getNumericValue(s.charAt(i)) diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_670.java b/src/main/java/com/fishercoder/solutions/firstthousand/_670.java new file mode 100644 index 0000000000..9731578a60 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_670.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +public class _670 { + public static class Solution1 { + public int maximumSwap(int num) { + String numStr = String.valueOf(num); + int max = num; + for (int i = 0; i < numStr.length() - 1; i++) { + for (int j = i + 1; j < numStr.length(); j++) { + if (numStr.charAt(i) < numStr.charAt(j)) { + StringBuilder sb = new StringBuilder(numStr); + sb.replace(i, i + 1, String.valueOf(numStr.charAt(j))); + sb.replace(j, j + 1, String.valueOf(numStr.charAt(i))); + max = Math.max(max, Integer.parseInt(sb.toString())); + } + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_671.java b/src/main/java/com/fishercoder/solutions/firstthousand/_671.java new file mode 100644 index 0000000000..956dc94007 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_671.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.TreeSet; + +public class _671 { + public static class Solution1 { + public int findSecondMinimumValue(TreeNode root) { + TreeSet set = new TreeSet<>(); + dfs(root, set); + if (set.size() < 2) { + return -1; + } + set.pollFirst(); + return set.pollFirst(); + } + + private void dfs(TreeNode root, TreeSet set) { + if (root == null) { + return; + } + set.add(root.val); + dfs(root.left, set); + dfs(root.right, set); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_672.java b/src/main/java/com/fishercoder/solutions/firstthousand/_672.java new file mode 100644 index 0000000000..6ef6ee109d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_672.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; + +public class _672 { + public static class Solution1 { + public int flipLights(int n, int m) { + if (m < 1) { + return 1; + } + int[][] dp = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (j == 0) { + dp[i][j] = 2; + } else if (i == 0 && j == 1) { + dp[i][j] = 3; + } else if (i == 0) { + dp[i][j] = 4; + } else if (i == 1 && j == 1) { + dp[i][j] = 4; + } else if (i == 1 && j > 1) { + dp[i][j] = 7; + } else if (j == 1) { + dp[i][j] = 4; + } else if (i == 1) { + dp[i][j] = 7; + } else { + dp[i][j] = 8; + } + } + } + CommonUtils.print2DIntArray(dp); + return dp[m - 1][n - 1]; + } + } + + public static class Solution2 { + public int flipLights(int n, int m) { + if (n == 1 && m > 0) { + return 2; + } else if (n == 2 && m == 1) { + return 3; + } else if ((n > 2 && m == 1) || (n == 2 && m > 1)) { + return 4; + } else if (n > 2 && m == 2) { + return 7; + } else if (n > 2 && m > 2) { + return 8; + } else { + return 1; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_673.java b/src/main/java/com/fishercoder/solutions/firstthousand/_673.java new file mode 100644 index 0000000000..9931eb1189 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_673.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +public class _673 { + public static class Solution1 { + /* + * Reference: https://discuss.leetcode.com/topic/103020/java-c-simple-dp-solution-with-explanation + */ + public int findNumberOfLIS(int[] nums) { + int n = nums.length; + int[] cnt = new int[n]; + int[] len = new int[n]; + int max = 0; + int count = 0; + for (int i = 0; i < n; i++) { + len[i] = cnt[i] = 1; + for (int j = 0; j < i; j++) { + if (nums[i] > nums[j]) { + if (len[i] == len[j] + 1) { + cnt[i] += cnt[j]; + } + if (len[i] < len[j] + 1) { + len[i] = len[j] + 1; + cnt[i] = cnt[j]; + } + } + } + + if (max == len[i]) { + count += cnt[i]; + } + + if (len[i] > max) { + max = len[i]; + count = cnt[i]; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_674.java b/src/main/java/com/fishercoder/solutions/firstthousand/_674.java new file mode 100644 index 0000000000..124c0ffc4a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_674.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _674 { + public static class Solution1 { + public int findLengthOfLCIS(int[] nums) { + int longest = 0; + for (int i = 0; i < nums.length; i++) { + int len = 1; + for (int j = i + 1; j < nums.length; j++) { + if (nums[j - 1] < nums[j]) { + len++; + continue; + } else { + break; + } + } + longest = Math.max(longest, len); + } + return longest; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_675.java b/src/main/java/com/fishercoder/solutions/firstthousand/_675.java new file mode 100644 index 0000000000..ed953b8ab3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_675.java @@ -0,0 +1,97 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.List; +import java.util.PriorityQueue; +import java.util.Queue; + +public class _675 { + public static class Solution1 { + public int cutOffTree(List> forest) { + if (forest == null + || forest.isEmpty() + || forest.size() == 0 + || forest.get(0).get(0) == 0) { + return -1; + } + int m = forest.size(); + int n = forest.get(0).size(); + /*cut trees in ascending order*/ + PriorityQueue heap = new PriorityQueue<>((a, b) -> a.height - b.height); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (forest.get(i).get(j) > 1) { + /*This is important: we'll add trees that are only taller than 1!!!*/ + heap.offer(new Tree(i, j, forest.get(i).get(j))); + } + } + } + + int sum = 0; + Tree start = new Tree(); + while (!heap.isEmpty()) { + Tree curr = heap.poll(); + int step = bfs(forest, curr, start, m, n); + if (step == -1) { + return -1; + } + sum += step; + start = curr; + } + return sum; + } + + private int bfs(List> forest, Tree target, Tree start, int m, int n) { + int[] dirs = new int[] {0, 1, 0, -1, 0}; + boolean[][] visited = new boolean[m][n]; + Queue queue = new LinkedList<>(); + queue.offer(start); + visited[start.x][start.y] = true; + int step = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int k = 0; k < size; k++) { + Tree tree = queue.poll(); + if (tree.x == target.x && tree.y == target.y) { + return step; + } + + for (int i = 0; i < 4; i++) { + int nextX = tree.x + dirs[i]; + int nextY = tree.y + dirs[i + 1]; + if (nextX < 0 + || nextY < 0 + || nextX >= m + || nextY >= n + || visited[nextX][nextY] + || forest.get(nextX).get(nextY) == 0) { + continue; + } + queue.offer(new Tree(nextX, nextY, forest.get(nextX).get(nextY))); + visited[nextX][nextY] = true; + } + } + step++; + } + return -1; + } + + class Tree { + int x; + int y; + int height; + + public Tree(int x, int y, int height) { + this.x = x; + this.y = y; + this.height = height; + } + + public Tree() { + this.x = 0; + this.y = 0; + this.height = 0; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_676.java b/src/main/java/com/fishercoder/solutions/firstthousand/_676.java new file mode 100644 index 0000000000..75355388b3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_676.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _676 { + + public static class Solution1 { + public static class MagicDictionary { + + Set wordSet; + + /* + * Initialize your data structure here. + */ + public MagicDictionary() { + wordSet = new HashSet<>(); + } + + /* + * Build a dictionary through a list of words + */ + public void buildDict(String[] dict) { + for (String word : dict) { + wordSet.add(word); + } + } + + /* + * Returns if there is any word in the trie that equals to the given word after modifying exactly one character + */ + public boolean search(String word) { + for (String candidate : wordSet) { + if (modifyOneChar(word, candidate)) { + return true; + } + } + return false; + } + + private boolean modifyOneChar(String word, String candidate) { + if (word.length() != candidate.length()) { + return false; + } + int diff = 0; + for (int i = 0; i < word.length(); i++) { + if (word.charAt(i) != candidate.charAt(i)) { + diff++; + } + if (diff > 1) { + return false; + } + } + return diff == 1; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_677.java b/src/main/java/com/fishercoder/solutions/firstthousand/_677.java new file mode 100644 index 0000000000..6c090932fc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_677.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _677 { + public static class Solution1 { + + public static class MapSum { + + Map map; + + /* + * Initialize your data structure here. + */ + public MapSum() { + map = new HashMap<>(); + } + + public void insert(String key, int val) { + map.put(key, val); + } + + public int sum(String prefix) { + int sum = 0; + for (String key : map.keySet()) { + if (key.startsWith(prefix)) { + sum += map.get(key); + } + } + return sum; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_678.java b/src/main/java/com/fishercoder/solutions/firstthousand/_678.java similarity index 77% rename from src/main/java/com/fishercoder/solutions/_678.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_678.java index 1562038cbf..299f14a46f 100644 --- a/src/main/java/com/fishercoder/solutions/_678.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_678.java @@ -1,37 +1,10 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.Stack; -/** - * 678. Valid Parenthesis String - * - * Given a string containing only three types of characters: '(', ')' and '*', - * write a function to check whether this string is valid. We define the validity of a string by these rules: - - 1. Any left parenthesis '(' must have a corresponding right parenthesis ')'. - 2. Any right parenthesis ')' must have a corresponding left parenthesis '('. - 3. Left parenthesis '(' must go before the corresponding right parenthesis ')'. - 4. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. - 5. An empty string is also valid. - - Example 1: - Input: "()" - Output: True - - Example 2: - Input: "(*)" - Output: True - - Example 3: - Input: "(*))" - Output: True - - Note: - The string size will be in the range [1, 100]. - */ public class _678 { public static class Solution1 { - /** + /* * This solution is correct, but will result in TLE by test4 */ public boolean checkValidString(String s) { @@ -89,7 +62,7 @@ private boolean isValid(String s, int start, int cnt) { } cnt--; } else if (c == '*') { - /**Extra caution: start should be i+1, not start+1 !*/ + /*Extra caution: start should be i+1, not start+1 !*/ return isValid(s, i + 1, cnt + 1) || isValid(s, i + 1, cnt - 1) || isValid(s, i + 1, cnt); @@ -100,7 +73,7 @@ private boolean isValid(String s, int start, int cnt) { } public static class Solution3 { - /** + /* * Greedy solution: * 1. Let lo mean the lowest possible open left paren * 2. Let hi mean the possibilities of highest possible open left paren, so as long as s.charAt(i) != ')', it's possible to be a '(', so we'll increment hi by 1 diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_679.java b/src/main/java/com/fishercoder/solutions/firstthousand/_679.java new file mode 100644 index 0000000000..77b0617d24 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_679.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.stream.IntStream; + +public class _679 { + public static class Solution1 { + /* + * Since there are only 4 cards and only 4 operations, we can iterate through all possible combinations, there's a total of 9216 possibilities: + * 1. we pick two out of four cards, with order (since order matters for division), 4 * 3 = 12, then pick one of four operations: 12 * 4 = 48; + * 2. then we pick two from these three numbers: 12 * 4 * 3 * 4 * 2 = 1152 + * 3. then we pick the remaining two: 1152 * 2 * 4 = 9216 (with order and out of 4 operations) + */ + public boolean judgePoint24(int[] nums) { + return dfs(IntStream.of(nums).mapToDouble(num -> num).toArray()); + } + + private boolean dfs(double[] nums) { + if (nums.length == 1) { + return Math.abs(nums[0] - 24) < 1e-8; // 1e-8 means 0.000000001, i.e. 10^(-8) + } + + for (int i = 0; i < nums.length; i++) { + for (int j = 0; j < nums.length; j++) { + if (i != j) { + int len = 0; + double[] a = new double[nums.length - 1]; + for (int k = 0; k < nums.length; k++) { + if (k != i && k != j) { + a[len++] = nums[k]; + } + } + + a[len] = nums[i] + nums[j]; + if (dfs(a)) { + return true; + } + + a[len] = nums[i] - nums[j]; + if (dfs(a)) { + return true; + } + + a[len] = nums[i] * nums[j]; + if (dfs(a)) { + return true; + } + + if (nums[j] > 1e-8) { + a[len] = nums[i] / nums[j]; + if (dfs(a)) { + return true; + } + } + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_68.java b/src/main/java/com/fishercoder/solutions/firstthousand/_68.java similarity index 97% rename from src/main/java/com/fishercoder/solutions/_68.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_68.java index 5b1e34dfde..20a9fbd2eb 100644 --- a/src/main/java/com/fishercoder/solutions/_68.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_68.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.List; @@ -57,5 +57,4 @@ public List fullJustify(String[] words, int L) { return result; } } - } diff --git a/src/main/java/com/fishercoder/solutions/_680.java b/src/main/java/com/fishercoder/solutions/firstthousand/_680.java similarity index 80% rename from src/main/java/com/fishercoder/solutions/_680.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_680.java index 3be4fadc45..16048321fa 100644 --- a/src/main/java/com/fishercoder/solutions/_680.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_680.java @@ -1,23 +1,5 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; -/** - * 680. Valid Palindrome II - * - * Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. - - Example 1: - Input: "aba" - Output: True - - Example 2: - Input: "abca" - Output: True - Explanation: You could delete the character 'c'. - - Note: - The string will only contain lowercase characters a-z. The maximum length of the string is 50000. - - */ public class _680 { public static class Solution1 { public boolean validPalindrome(String s) { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_681.java b/src/main/java/com/fishercoder/solutions/firstthousand/_681.java new file mode 100644 index 0000000000..e133a7596b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_681.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _681 { + public static class Solution1 { + public String nextClosestTime(String time) { + int cur = 60 * Integer.parseInt(time.substring(0, 2)); + cur += Integer.parseInt(time.substring(3)); + Set allowed = new HashSet(); + for (char c : time.toCharArray()) { + if (c != ':') { + allowed.add(c - '0'); + } + } + + while (true) { + cur = (cur + 1) % (24 * 60); + int[] digits = + new int[] {cur / 60 / 10, cur / 60 % 10, cur % 60 / 10, cur % 60 % 10}; + search: + { + for (int d : digits) { + if (!allowed.contains(d)) { + break search; + } + } + return String.format("%02d:%02d", cur / 60, cur % 60); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_682.java b/src/main/java/com/fishercoder/solutions/firstthousand/_682.java new file mode 100644 index 0000000000..7f591494d2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_682.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Stack; + +public class _682 { + + public static class Solution1 { + public int calPoints(String[] ops) { + Stack stack = new Stack<>(); + int sum = 0; + int firstLast = Integer.MIN_VALUE; + int secondLast = Integer.MIN_VALUE; + for (String op : ops) { + if (op.equals("+")) { + if (!stack.isEmpty()) { + firstLast = stack.pop(); + } + if (!stack.isEmpty()) { + secondLast = stack.pop(); + } + int thisRoundPoints = firstLast + secondLast; + + if (secondLast != Integer.MIN_VALUE) { + stack.push(secondLast); + } + if (firstLast != Integer.MIN_VALUE) { + stack.push(firstLast); + } + stack.push(thisRoundPoints); + sum += thisRoundPoints; + + firstLast = Integer.MIN_VALUE; + secondLast = Integer.MIN_VALUE; + } else if (op.equals("D")) { + if (!stack.isEmpty()) { + int thisRoundPoints = stack.peek() * 2; + stack.push(thisRoundPoints); + sum += thisRoundPoints; + } + } else if (op.equals("C")) { + if (!stack.isEmpty()) { + int removedData = stack.pop(); + sum -= removedData; + } + } else { + Integer val = Integer.parseInt(op); + sum += val; + stack.push(val); + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_683.java b/src/main/java/com/fishercoder/solutions/firstthousand/_683.java new file mode 100644 index 0000000000..61dbefe092 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_683.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +public class _683 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/104771/java-c-simple-o-n-solution + */ + public int kEmptySlots(int[] flowers, int k) { + int[] days = new int[flowers.length]; + for (int i = 0; i < flowers.length; i++) { + days[flowers[i] - 1] = i + 1; + } + int left = 0; + int right = k + 1; + int result = Integer.MAX_VALUE; + for (int i = 0; right < flowers.length; i++) { + if (days[i] < days[left] || days[i] <= days[right]) { + if (i == right) { + result = Math.min(result, Math.max(days[left], days[right])); + } + left = i; + right = k + 1 + i; + } + } + return result == Integer.MAX_VALUE ? -1 : result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_684.java b/src/main/java/com/fishercoder/solutions/firstthousand/_684.java new file mode 100644 index 0000000000..08c7177571 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_684.java @@ -0,0 +1,133 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _684 { + + public static class Solution1 { + /* + * This is my original solution. A little verbose. + */ + class UnionFind { + int[] ids; + Set nodes; + Set visitedNodes; + int[] redundantConn; + int m; + int n; + + public UnionFind(int[][] edges) { + m = edges.length; + n = edges[0].length; + nodes = new HashSet<>(); + visitedNodes = new HashSet<>(); + redundantConn = new int[] {}; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + nodes.add(edges[i][j]); + } + } + ids = new int[nodes.size()]; + for (int i = 0; i < ids.length; i++) { + ids[i] = i + 1; + } + } + + public int[] union(int[] edge) { + int x = find(edge[0] - 1); + int y = find(edge[1] - 1); + + if (x == y && visitedNodes.contains(edge[0]) && visitedNodes.contains(edge[1])) { + redundantConn = edge; + } + + if (x != y) { + if (x < y) { + ids[y] = x + 1; + } else { + ids[x] = y + 1; + } + } + + visitedNodes.add(edge[0]); + visitedNodes.add(edge[1]); + return redundantConn; + } + + private int find(int id) { + if (isTree()) { + return ids[id]; + } + if ((id + 1) != ids[id]) { + return find(ids[id] - 1); + } + return id; + } + + private boolean isTree() { + Set set = new HashSet<>(); + for (int i : ids) { + set.add(i); + } + return set.size() == 1; + } + } + + public int[] findRedundantConnection(int[][] edges) { + UnionFind unionFind = new UnionFind(edges); + int[] result = new int[] {}; + for (int[] edge : edges) { + result = unionFind.union(edge); + } + return result; + } + } + + public static class Solution2 { + /* + * DFS, credit: https://leetcode.com/problems/redundant-connection/editorial/ + * 1. we build the graph one edge at a time, each time, we add both edge[0] to the neighbors of edge[1] and vice versa since this is an un-directed graph; + * 2. as soon as we encounter an edge that can connect to each other, it must be the redundant one. + * In other words, we first check if this new edge is needed or not based on the current existing graph: + * if the two nodes connected by this edge is already connected, then this edge is redundant. + */ + private static final int MAX_VERTICES = 1000; + + public int[] findRedundantConnection(int[][] edges) { + List[] graph = new ArrayList[MAX_VERTICES + 1]; + for (int i = 0; i < graph.length; i++) { + graph[i] = new ArrayList<>(); + } + Set visited = new HashSet<>(); + for (int[] edge : edges) { + visited.clear(); + if (!graph[edge[0]].isEmpty() + && !graph[edge[1]].isEmpty() + && canConnect(edge[0], edge[1], graph, visited)) { + return edge; + } + graph[edge[0]].add(edge[1]); + graph[edge[1]].add(edge[0]); + } + return null; + } + + private boolean canConnect( + int source, int target, List[] graph, Set visited) { + if (visited.add(source)) { + if (source == target) { + return true; + } + for (int v : graph[target]) { + if (canConnect(source, v, graph, visited)) { + return true; + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_685.java b/src/main/java/com/fishercoder/solutions/firstthousand/_685.java similarity index 75% rename from src/main/java/com/fishercoder/solutions/_685.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_685.java index 18c2c34d9b..9c2dbdf7ea 100644 --- a/src/main/java/com/fishercoder/solutions/_685.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_685.java @@ -1,47 +1,13 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; -/** - * 685. Redundant Connection II - * - * In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are - * descendants of this node, plus every node has exactly one parents, except for the root node which has no parents. - * The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1, 2, ..., N), - * with one additional directed edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed. - * The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] that - * represents a directed edge connecting nodes u and v, where u is a parents of child v. - * Return an edge that can be removed so that the resulting graph is a rooted tree of N nodes. - * If there are multiple answers, return the answer that occurs last in the given 2D-array. - - Example 1: - Input: [[1,2], [1,3], [2,3]] - Output: [2,3] - Explanation: The given directed graph will be like this: - 1 - / \ - v v - 2-->3 - - Example 2: - Input: [[1,2], [2,3], [3,4], [4,1], [1,5]] - Output: [4,1] - Explanation: The given directed graph will be like this: - 5 <- 1 -> 2 - ^ | - | v - 4 <- 3 - - Note: - The size of the input 2D-array will be between 3 and 1000. - Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array. - */ public class _685 { public static class Solution1 { - /** + /* * My original solution, failed by _685Test.test3 */ class UnionFind { @@ -55,7 +21,7 @@ class UnionFind { int n; class LinkedNode { - List parents;//at most, there's one node that has two parents + List parents; // at most, there's one node that has two parents int val; public LinkedNode(int val) { @@ -85,7 +51,7 @@ public UnionFind(int[][] edges) { nodes = new HashSet<>(); visitedLinkedNodes = new ArrayList<>(); visitedValues = new HashSet<>(); - redundantConn = new int[]{}; + redundantConn = new int[] {}; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { nodes.add(edges[i][j]); @@ -116,11 +82,15 @@ public void union(int[] edge) { public int[] findRedundantDirectedConnection() { int index = hasTwoParents(); if (index != -1) { - List parents = visitedLinkedNodes.get(index).parents;//parents size is fixed, only 2 + List parents = + visitedLinkedNodes.get(index).parents; // parents size is fixed, only 2 int[] result = new int[2]; for (int i = 0; i < parents.size(); i++) { if (hasCycle(visitedLinkedNodes.get(index), parents.get(i))) { - result = new int[]{parents.get(i).val, visitedLinkedNodes.get(index).val}; + result = + new int[] { + parents.get(i).val, visitedLinkedNodes.get(index).val + }; break; } } @@ -164,7 +134,7 @@ private int hasTwoParents() { public int[] findRedundantDirectedConnection(int[][] edges) { UnionFind unionFind = new UnionFind(edges); - /**two cases: + /*two cases: * 1. the entire edges are just one directed loop, in this case, just return the last edge, see test2 in _685Test.java * 2. there's one directed loop, but one node of the loop has two parents, in this case, what we'll need to do * is just to return the edge in this loop that points to the child that has two parents, see test1 in _685Test.java @@ -179,7 +149,9 @@ public int[] findRedundantDirectedConnection(int[][] edges) { } public static class Solution2 { - /**credit: https://discuss.leetcode.com/topic/105108/c-java-union-find-with-explanation-o-n*/ + /* + * credit: https://discuss.leetcode.com/topic/105108/c-java-union-find-with-explanation-o-n + */ public int[] findRedundantDirectedConnection(int[][] edges) { int[] can1 = {-1, -1}; int[] can2 = {-1, -1}; @@ -188,8 +160,8 @@ public int[] findRedundantDirectedConnection(int[][] edges) { if (parent[edges[i][1]] == 0) { parent[edges[i][1]] = edges[i][0]; } else { - can2 = new int[]{edges[i][0], edges[i][1]}; - can1 = new int[]{parent[edges[i][1]], edges[i][1]}; + can2 = new int[] {edges[i][0], edges[i][1]}; + can1 = new int[] {parent[edges[i][1]], edges[i][1]}; edges[i][1] = 0; } } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_686.java b/src/main/java/com/fishercoder/solutions/firstthousand/_686.java new file mode 100644 index 0000000000..d4b8cd23cb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_686.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _686 { + public static class Solution1 { + public int repeatedStringMatch(String A, String B) { + Set set = new HashSet<>(); + for (char c : A.toCharArray()) { + set.add(c); + } + for (char c : B.toCharArray()) { + if (!set.contains(c)) { + return -1; + } + } + StringBuilder stringBuilder = new StringBuilder(A); + for (int i = 0; i < B.length(); i++) { + if (stringBuilder.toString().contains(B)) { + return i + 1; + } + stringBuilder.append(A); + } + return -1; + } + } + + public static class Solution2 { + /* + * Time: O(N(N+M)) + * Space: O(N + M) + */ + public int repeatedStringMatch(String A, String B) { + int count = 1; + StringBuilder sb = new StringBuilder(A); + for (; sb.length() < B.length(); count++) { + sb.append(A); + } + if (sb.indexOf(B) >= 0) { + return count; + } + sb.append(A); + if (sb.indexOf(B) >= 0) { + return count + 1; + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_687.java b/src/main/java/com/fishercoder/solutions/firstthousand/_687.java new file mode 100644 index 0000000000..6db64c03af --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_687.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _687 { + public static class Solution1 { + /* + * Use a one element array to pass in and out is a common technique for handling tree questions. + */ + public int longestUnivaluePath(TreeNode root) { + int[] result = new int[1]; + if (root != null) { + dfs(root, result); + } + return result[0]; + } + + // calculate longest univalue path from root to leaves + // In addition, the maximum univalue path cross the root node is calculated and then global + // maximum is udpated. + private int dfs(TreeNode root, int[] result) { + int leftPath = root.left == null ? 0 : dfs(root.left, result); + int rightPath = root.right == null ? 0 : dfs(root.right, result); + int leftResult = (root.left != null && root.left.val == root.val) ? leftPath + 1 : 0; + int rightResult = + (root.right != null && root.right.val == root.val) ? rightPath + 1 : 0; + result[0] = Math.max(result[0], leftResult + rightResult); + return Math.max(leftResult, rightResult); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_688.java b/src/main/java/com/fishercoder/solutions/firstthousand/_688.java new file mode 100644 index 0000000000..fbfb7613e0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_688.java @@ -0,0 +1,79 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Queue; + +public class _688 { + + public static class Solution1 { + /* + * This BFS solution results in TLE on Leetcode. + */ + public double knightProbability(int N, int K, int r, int c) { + int[][] directions = { + {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1} + }; + Queue queue = new LinkedList<>(); + queue.offer(new int[] {r, c}); + int level = K; + while (level-- > 0) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + int[] curr = queue.poll(); + for (int[] direction : directions) { + int x = curr[0] + direction[0]; + int y = curr[1] + direction[1]; + if (x >= 0 && x < N && y >= 0 && y < N) { + queue.offer(new int[] {x, y}); + } + } + } + } + double prob = queue.size(); + for (int i = 0; i < K; i++) { + prob /= 8; + } + return prob; + } + } + + public static class Solution2 { + /* + * Let f[r][c][k] mean the probability that the knight is still on board after k steps, + * we can deduce a recursion from its k-1 steps + * In addition, instead of using a 3-d array, we can only keep the most recent two layers, + * i.e. using only two 2-d arrays. + */ + public double knightProbability(int N, int K, int r, int c) { + int[][] directions = { + {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1} + }; + double[][] dp0 = new double[N][N]; + for (double[] row : dp0) { + Arrays.fill(row, 1); + } + for (int k = 0; k < K; k++) { + double[][] dp1 = new double[N][N]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + for (int l = 0; l < directions.length; l++) { + int[] direction = directions[l]; + int x = i + direction[0]; + int y = j + direction[1]; + if (x >= 0 && y >= 0 && x < N && y < N) { + dp1[i][j] += dp0[x][y]; + } + } + } + } + dp0 = dp1; + } + return dp0[r][c] / Math.pow(8, K); + } + } + + public static void main(String... args) { + System.out.println((double) 2 / 8); + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_689.java b/src/main/java/com/fishercoder/solutions/firstthousand/_689.java new file mode 100644 index 0000000000..daef283d17 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_689.java @@ -0,0 +1,70 @@ +package com.fishercoder.solutions.firstthousand; + +public class _689 { + public static class Solution1 { + /* + * we basically need to find the interval (i, i+k-1) as the middle interval, where k <= i <= n-2k + * then this interval (0, i-1) will be the left interval + * the interval (i+k, n-1) will be the right interval. + *

+ * Please pay special attention to the variable name I use here: this `k` is not a random one, it's the `k` + * from the passed in parameter. + *

+ * Credit: https://discuss.leetcode.com/topic/105577/c-java-dp-with-explanation-o-n/ + */ + public int[] maxSumOfThreeSubarrays(int[] nums, int k) { + if (nums == null || nums.length == 0) { + return new int[] {}; + } + int n = nums.length; + int[] sums = new int[n + 1]; + for (int i = 0; i < n; i++) { + sums[i + 1] = sums[i] + nums[i]; + } + + int[] leftMax = new int[n]; + for (int i = k, total = sums[k] - sums[0]; i < n; i++) { + if (sums[i + 1] - sums[i + 1 - k] > total) { + leftMax[i] = i + 1 - k; + total = sums[i + 1] - sums[i + 1 - k]; + } else { + leftMax[i] = leftMax[i - 1]; + } + } + + int[] rightMax = new int[n]; + rightMax[n - k] = n - k; + for (int i = n - k - 1, total = sums[n] - sums[n - k]; i >= 0; i--) { + if (sums[i + k] - sums[i] >= total) { + rightMax[i] = i; + total = sums[i + k] - sums[i]; + } else { + rightMax[i] = rightMax[i + 1]; + } + } + + // try to find all possible middle intervals + int[] result = new int[3]; + int max = 0; + for (int i = k; i <= n - 2 * k; i++) { + int left = leftMax[i - 1]; + int right = rightMax[i + k]; + int total = + (sums[i + k] - sums[i]) + + (sums[left + k] - sums[left]) + + (sums[right + k] - sums[right]); + if (total > max) { + max = total; + result[0] = left; + result[1] = i; + result[2] = right; + } + } + return result; + } + } + + public static class Solution2 { + /*reference: https://leetcode.com/articles/maximum-sum-of-3-non-overlapping-intervals*/ + } +} diff --git a/src/main/java/com/fishercoder/solutions/_69.java b/src/main/java/com/fishercoder/solutions/firstthousand/_69.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_69.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_69.java index 5d7019fcab..f321356d8d 100644 --- a/src/main/java/com/fishercoder/solutions/_69.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_69.java @@ -1,8 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _69 { public static class Solution1 { - /** + /* * A few key points: * 1. all variable use long type, otherwise overflow, just cast to int before returning * 2. left start from 0, not 1 diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_690.java b/src/main/java/com/fishercoder/solutions/firstthousand/_690.java new file mode 100644 index 0000000000..cbcbd7b1e6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_690.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.Employee; +import java.util.List; +import java.util.stream.Collectors; + +public class _690 { + + public static class Solution1 { + + int total = 0; + + public int getImportance(List employees, int id) { + Employee manager = + employees.stream().filter(e -> e.id == id).collect(Collectors.toList()).get(0); + total += manager.importance; + manager.subordinates.forEach(subId -> getImportance(employees, subId)); + + /*The above line is equivalent to below for loop*/ + // for (int subId : manager.subordinates) { + // getImportance(employees, subId); + // } + + return total; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_691.java b/src/main/java/com/fishercoder/solutions/firstthousand/_691.java new file mode 100644 index 0000000000..0fe15b4036 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_691.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _691 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/106273/c-java-python-dp-memoization-with-optimization-29-ms-c/2 + */ + public int minStickers(String[] stickers, String target) { + int m = stickers.length; + int[][] mp = new int[m][26]; + Map dp = new HashMap<>(); + for (int i = 0; i < m; i++) { + for (char c : stickers[i].toCharArray()) { + mp[i][c - 'a']++; + } + } + dp.put("", 0); + return helper(dp, mp, target); + } + + private int helper(Map dp, int[][] mp, String target) { + if (dp.containsKey(target)) { + return dp.get(target); + } + int ans = Integer.MAX_VALUE; + int n = mp.length; + int[] tar = new int[26]; + for (char c : target.toCharArray()) { + tar[c - 'a']++; + } + // try every sticker + for (int i = 0; i < n; i++) { + // optimization + if (mp[i][target.charAt(0) - 'a'] == 0) { + continue; + } + StringBuilder sb = new StringBuilder(); + // apply a sticker on every character a-z + for (int j = 0; j < 26; j++) { + if (tar[j] > 0) { + for (int k = 0; k < Math.max(0, tar[j] - mp[i][j]); k++) { + sb.append((char) ('a' + j)); + } + } + } + String s = sb.toString(); + int tmp = helper(dp, mp, s); + if (tmp != -1) { + ans = Math.min(ans, 1 + tmp); + } + } + dp.put(target, ans == Integer.MAX_VALUE ? -1 : ans); + return dp.get(target); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_692.java b/src/main/java/com/fishercoder/solutions/firstthousand/_692.java new file mode 100644 index 0000000000..a92883293a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_692.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; + +public class _692 { + + public static class Solution1 { + /* + * O(n) extra space + * O(nlogk) time + */ + public List topKFrequent(String[] words, int k) { + Map map = new HashMap<>(); + for (String word : words) { + map.put(word, map.getOrDefault(word, 0) + 1); + } + + SortedSet> sortedset = + new TreeSet<>( + (e1, e2) -> { + if (e1.getValue() != e2.getValue()) { + return e2.getValue() - e1.getValue(); + } else { + return e1.getKey().compareToIgnoreCase(e2.getKey()); + } + }); + sortedset.addAll(map.entrySet()); + + List result = new ArrayList<>(); + Iterator> iterator = sortedset.iterator(); + while (iterator.hasNext() && k-- > 0) { + result.add(iterator.next().getKey()); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_693.java b/src/main/java/com/fishercoder/solutions/firstthousand/_693.java new file mode 100644 index 0000000000..f9320541bb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_693.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +public class _693 { + public static class Solution1 { + public boolean hasAlternatingBits(int n) { + String binaryStr = Integer.toBinaryString(n); + for (int i = 1; i < binaryStr.length(); i++) { + if (binaryStr.charAt(i - 1) == binaryStr.charAt(i)) { + return false; + } + } + return true; + } + } + + public static class Solution2 { + public boolean hasAlternatingBits(int n) { + return Integer.bitCount(((n >> 1) ^ n) + 1) == 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_694.java b/src/main/java/com/fishercoder/solutions/firstthousand/_694.java new file mode 100644 index 0000000000..4de2652eb9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_694.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _694 { + public static class Solution1 { + int[][] directions = + new int[][] { + {0, 1}, + {1, 0}, + {0, -1}, + {-1, 0} + }; + + public int numDistinctIslands(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + Set>> uniqueShapeIslands = new HashSet<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + List> island = new ArrayList<>(); + if (dfs(i, j, i, j, grid, m, n, island)) { + uniqueShapeIslands.add(island); + } + } + } + return uniqueShapeIslands.size(); + } + + private boolean dfs( + int i0, + int j0, + int i, + int j, + int[][] grid, + int m, + int n, + List> island) { + if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] <= 0) { + return false; + } + island.add(Arrays.asList(i - i0, j - j0)); + grid[i][j] *= -1; + for (int k = 0; k < 4; k++) { + dfs(i0, j0, i + directions[k][0], j + directions[k][1], grid, m, n, island); + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_695.java b/src/main/java/com/fishercoder/solutions/firstthousand/_695.java new file mode 100644 index 0000000000..f01b1811b7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_695.java @@ -0,0 +1,85 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _695 { + + public static class Solution1 { + public int maxAreaOfIsland(int[][] grid) { + if (grid == null || grid.length == 0) { + return 0; + } + int m = grid.length; + int n = grid[0].length; + int max = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + int area = dfs(grid, i, j, m, n, 0); + max = Math.max(area, max); + } + } + } + return max; + } + + int dfs(int[][] grid, int i, int j, int m, int n, int area) { + if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) { + return area; + } + grid[i][j] = 0; + area++; + area = dfs(grid, i + 1, j, m, n, area); + area = dfs(grid, i, j + 1, m, n, area); + area = dfs(grid, i - 1, j, m, n, area); + area = dfs(grid, i, j - 1, m, n, area); + return area; + } + } + + public static class Solution2 { + public int maxAreaOfIsland(int[][] grid) { + int maxArea = 0; + int m = grid.length; + int n = grid[0].length; + int[] directions = new int[] {0, 1, 0, -1, 0}; + boolean[][] visited = new boolean[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1 && !visited[i][j]) { + Queue queue = new LinkedList<>(); + queue.offer(new int[] {i, j}); + int area = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int k = 0; k < size; k++) { + int[] curr = queue.poll(); + if (!visited[curr[0]][curr[1]]) { + area++; + visited[curr[0]][curr[1]] = true; + } + for (int p = 0; p < directions.length - 1; p++) { + int newX = curr[0] + directions[p]; + int newY = curr[1] + directions[p + 1]; + if (newX >= 0 + && newX < m + && newY >= 0 + && newY < n + && !visited[newX][newY] + && grid[newX][newY] == 1) { + visited[newX][newY] = true; + area++; + queue.offer(new int[] {newX, newY}); + } + } + } + } + maxArea = Math.max(maxArea, area); + } + } + } + return maxArea; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_696.java b/src/main/java/com/fishercoder/solutions/firstthousand/_696.java new file mode 100644 index 0000000000..25115dd474 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_696.java @@ -0,0 +1,56 @@ +package com.fishercoder.solutions.firstthousand; + +public class _696 { + public static class Solution1 { + public int countBinarySubstrings(String s) { + int n = s.length(); + /*a[i][0] denotes from most left up to i (inclusive), how many consecutive 0's + * a[i][1] denotes from most left up to i (inclusive), how many consecutive 1's*/ + int[][] a = new int[n][2]; + /*a[i][0] denotes from i (inclusive) to the most right, how many consecutive 0's + * b[i][0] denotes from i (inclusive) to the most right, how many consecutive 1's*/ + int[][] b = new int[n][2]; + for (int i = 0; i < n; i++) { + if (s.charAt(i) == '0') { + a[i][0] = 1 + (i - 1 >= 0 ? a[i - 1][0] : 0); + } else { + a[i][1] = 1 + (i - 1 >= 0 ? a[i - 1][1] : 0); + } + } + for (int i = n - 1; i >= 0; i--) { + if (s.charAt(i) == '0') { + b[i][0] = 1 + (i + 1 < n ? b[i + 1][0] : 0); + } else { + b[i][1] = 1 + (i + 1 < n ? b[i + 1][1] : 0); + } + } + long ans = 0; + for (int i = 0; i + 1 < n; i++) { + ans += Math.min(a[i][0], b[i + 1][1]); + ans += Math.min(a[i][1], b[i + 1][0]); + } + return (int) ans; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/count-binary-substrings/discuss/1172553/JS-Python-Java-C%2B%2B-or-Easy-Rolling-Count-Solution-w-Explanation + */ + public int countBinarySubstrings(String s) { + int curr = 1; + int prev = 0; + int ans = 0; + for (int i = 1; i < s.length(); i++) { + if (s.charAt(i) == s.charAt(i - 1)) { + curr++; + } else { + ans += Math.min(curr, prev); + prev = curr; + curr = 1; + } + } + return ans + Math.min(curr, prev); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_697.java b/src/main/java/com/fishercoder/solutions/firstthousand/_697.java new file mode 100644 index 0000000000..5503bce792 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_697.java @@ -0,0 +1,105 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.*; + +public class _697 { + public static class Solution1 { + public int findShortestSubArray(int[] nums) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i])) { + map.put(nums[i], map.get(nums[i]) + 1); + } else { + map.put(nums[i], 1); + } + } + int degree = -1; + for (int key : map.keySet()) { + degree = Math.max(degree, map.get(key)); + } + List candidateNums = new ArrayList(); + for (int key : map.keySet()) { + if (map.get(key) == degree) { + candidateNums.add(key); + } + } + int shortest = Integer.MAX_VALUE; + for (int candidate : candidateNums) { + shortest = Math.min(shortest, findLength(nums, candidate)); + } + return shortest; + } + + int findLength(int[] arr, int candidate) { + int firstAppearance = Integer.MAX_VALUE; + for (int i = 0; i < arr.length; i++) { + if (arr[i] == candidate) { + firstAppearance = i; + break; + } + } + int lastAppearance = arr.length - 1; + for (int i = arr.length - 1; i > firstAppearance; i--) { + if (arr[i] == candidate) { + lastAppearance = i; + break; + } + } + return (lastAppearance - firstAppearance) + 1; + } + } + + public static class Solution2 { + public int findShortestSubArray(int[] nums) { + Map count = new HashMap<>(); + Map left = new HashMap<>(); + Map right = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + count.put(nums[i], count.getOrDefault(nums[i], 0) + 1); + if (!left.containsKey(nums[i])) { + left.put(nums[i], i); + } + right.put(nums[i], i); + } + + int result = nums.length; + int degree = Collections.max(count.values()); + for (int num : count.keySet()) { + if (count.get(num) == degree) { + result = Math.min(result, right.get(num) - left.get(num) + 1); + } + } + return result; + } + } + + public static class Solution3 { + public int findShortestSubArray(int[] nums) { + Map frequencyMap = new HashMap<>(); + Map> numberToIndicesMap = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + frequencyMap.put(nums[i], frequencyMap.getOrDefault(nums[i], 0) + 1); + List indices = numberToIndicesMap.getOrDefault(nums[i], new ArrayList<>()); + indices.add(i); + numberToIndicesMap.put(nums[i], indices); + } + int degree = 0; + Set numbersThatOccurTheMost = new HashSet<>(); + for (Map.Entry entry : frequencyMap.entrySet()) { + degree = Math.max(degree, entry.getValue()); + } + for (Map.Entry entry : frequencyMap.entrySet()) { + if (entry.getValue() == degree) { + numbersThatOccurTheMost.add(entry.getKey()); + } + } + int result = nums.length; + for (int num : numbersThatOccurTheMost) { + List indices = numberToIndicesMap.get(num); + result = Math.min(result, indices.get(indices.size() - 1) - indices.get(0) + 1); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_698.java b/src/main/java/com/fishercoder/solutions/firstthousand/_698.java new file mode 100644 index 0000000000..d75f436758 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_698.java @@ -0,0 +1,106 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _698 { + + public static class Solution1 { + public boolean canPartitionKSubsets(int[] nums, int k) { + long sum = 0; + for (int num : nums) { + sum += num; + } + if (sum % k != 0) { + return false; + } + int equalSum = (int) (sum / k); + boolean[] visited = new boolean[nums.length]; + return canPartition(nums, visited, 0, k, 0, 0, equalSum); + } + + private boolean canPartition( + int[] nums, + boolean[] visited, + int startIndex, + int k, + int currSum, + int currNum, + int target) { + if (k == 1) { + return true; + } + if (currSum == target && currNum > 0) { + /*Everytime when we get currSum == target, we'll start from index 0 and look up the numbers that are not used yet + * and try to find another sum that could equal to target*/ + return canPartition(nums, visited, 0, k - 1, 0, 0, target); + } + for (int i = startIndex; i < nums.length; i++) { + if (!visited[i]) { + visited[i] = true; + if (canPartition( + nums, visited, i + 1, k, currSum + nums[i], currNum++, target)) { + return true; + } + visited[i] = false; + } + } + return false; + } + } + + public static class Solution2 { + /* + * I'm glad that I figured out below solution completely on my own on 9/30/2021. + * Backtracking is so beautiful! + *

+ * Although not super concise, and I've added a sorting step, it's completely original. + */ + public boolean canPartitionKSubsets(int[] nums, int k) { + Arrays.sort(nums); + long sum = 0L; + for (int num : nums) { + sum += num; + } + int ave = (int) sum / k; + if (sum % k != 0) { + return false; + } + boolean[] used = new boolean[nums.length]; + int found = 0; + for (int i = nums.length - 1; i >= 0; i--) { + if (!used[i]) { + used[i] = true; + found += recursive(nums, used, ave, nums[i], i); + } + } + return k == found; + } + + private int recursive( + int[] nums, boolean[] used, int targetSum, int currSum, int currIndex) { + if (currSum == targetSum) { + return 1; + } else if (currSum > targetSum) { + used[currIndex] = false; + return 0; + } else { + if (currIndex > 0) { + for (int i = currIndex; i > 0; i--) { + if (!used[i - 1]) { + used[i - 1] = true; + int found = + recursive(nums, used, targetSum, currSum + nums[i - 1], i - 1); + if (found == 1) { + return found; + } + used[i - 1] = + false; // this is the backtracking step: reset this number to be + // available if not found + } + } + } + return 0; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_699.java b/src/main/java/com/fishercoder/solutions/firstthousand/_699.java new file mode 100644 index 0000000000..644b0dec26 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_699.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _699 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/107107/easy-understood-o-n-2-solution-with-explanation + */ + public List fallingSquares(int[][] positions) { + List intervals = new ArrayList<>(); + List result = new ArrayList<>(); + int height = 0; + for (int[] position : positions) { + Interval curr = + new Interval(position[0], position[0] + position[1] - 1, position[1]); + height = Math.max(height, getHeight(intervals, curr)); + result.add(height); + } + return result; + } + + private int getHeight(List intervals, Interval curr) { + int preMaxHeight = 0; + for (Interval interval : intervals) { + if (interval.end < curr.start || interval.start > curr.end) { + continue; + } + preMaxHeight = Math.max(preMaxHeight, interval.height); + } + curr.height += preMaxHeight; + intervals.add(curr); + return curr.height; + } + + class Interval { + int start; + int end; + int height; + + public Interval(int start, int end, int height) { + this.start = start; + this.end = end; + this.height = height; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_7.java b/src/main/java/com/fishercoder/solutions/firstthousand/_7.java similarity index 82% rename from src/main/java/com/fishercoder/solutions/_7.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_7.java index 8e04b4f62a..637f915593 100644 --- a/src/main/java/com/fishercoder/solutions/_7.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_7.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _7 { public static class Solution1 { - /**This solution is NOT meeting the problem requirement actually: + /*This solution is NOT meeting the problem requirement actually: * Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1].*/ public int reverse(int x) { long result = 0; @@ -21,7 +21,7 @@ public int reverse(int x) { } public static class Solution2 { - /**credit: https://leetcode.com/problems/reverse-integer/discuss/4060/My-accepted-15-lines-of-code-for-Java*/ + /*credit: https://leetcode.com/problems/reverse-integer/discuss/4060/My-accepted-15-lines-of-code-for-Java*/ public int reverse(int x) { int result = 0; while (x != 0) { @@ -36,4 +36,4 @@ public int reverse(int x) { return result; } } -} \ No newline at end of file +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_70.java b/src/main/java/com/fishercoder/solutions/firstthousand/_70.java new file mode 100644 index 0000000000..276bceb452 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_70.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +public class _70 { + public static class Solution1 { + /* + * O(n) time + * O(n) space + */ + public int climbStairs(int n) { + if (n == 1) { + return n; + } + int[] dp = new int[n + 1]; + dp[1] = 1; + dp[2] = 2; + for (int i = 3; i <= n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n]; + } + } + + public static class Solution2 { + /* + * O(n) time + * O(1) space + */ + public int climbStairs(int n) { + if (n == 1) { + return n; + } + int secondLastStep = 1; + int lastStep = 2; + for (int i = 3; i <= n; i++) { + int tmp = lastStep; + lastStep = secondLastStep + lastStep; + secondLastStep = tmp; + } + return lastStep; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_700.java b/src/main/java/com/fishercoder/solutions/firstthousand/_700.java new file mode 100644 index 0000000000..d1d079f002 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_700.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _700 { + public static class Solution1 { + public TreeNode searchBST(TreeNode root, int val) { + if (root == null) { + return null; + } else if (root.val == val) { + return root; + } else if (root.val > val) { + return searchBST(root.left, val); + } else { + return searchBST(root.right, val); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_701.java b/src/main/java/com/fishercoder/solutions/firstthousand/_701.java new file mode 100644 index 0000000000..d177cb1f4a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_701.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _701 { + public static class Solution1 { + public TreeNode insertIntoBST(TreeNode root, int val) { + if (root == null) { + return new TreeNode(val); + } + if (root.val < val) { + root.right = insertIntoBST(root.right, val); + } else { + root.left = insertIntoBST(root.left, val); + } + return root; + } + } + + public static class Solution2 { + public TreeNode insertIntoBST(TreeNode root, int val) { + List list = new ArrayList<>(); + inorder(root, list); + int insertionPoint = binarySearch(list, val); + list.add(insertionPoint, val); + return formBST(list); + } + + private TreeNode formBST(List list) { + if (list.isEmpty()) { + return null; + } + TreeNode newRoot = new TreeNode(list.get(list.size() / 2)); + newRoot.left = formBST(list.subList(0, list.size() / 2)); + newRoot.right = formBST(list.subList(list.size() / 2 + 1, list.size())); + return newRoot; + } + + private int binarySearch(List list, int target) { + int left = 0; + int right = list.size() - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (list.get(mid) < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return left == right + ? list.get(left) >= target ? left : left + 1 + : left; // here's the most tricky/easy to get buggy part! + } + + private void inorder(TreeNode root, List list) { + if (root == null) { + return; + } + inorder(root.left, list); + list.add(root.val); + inorder(root.right, list); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_703.java b/src/main/java/com/fishercoder/solutions/firstthousand/_703.java new file mode 100644 index 0000000000..d3fe57f5c5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_703.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.PriorityQueue; + +public class _703 { + public static class Solution1 { + public static class KthLargest { + PriorityQueue minHeap; + int maxK; + + public KthLargest(int k, int[] nums) { + minHeap = new PriorityQueue<>(); + for (int num : nums) { + minHeap.offer(num); + if (minHeap.size() > k) { + minHeap.poll(); + } + } + maxK = k; + } + + public int add(int val) { + if (minHeap.size() < maxK || minHeap.peek() < val) { + minHeap.offer(val); + if (minHeap.size() > maxK) { + minHeap.poll(); + } + } + return minHeap.peek(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_704.java b/src/main/java/com/fishercoder/solutions/firstthousand/_704.java new file mode 100644 index 0000000000..72f6311105 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_704.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.firstthousand; + +public class _704 { + public static class Solution1 { + public int search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + if (target < nums[left] || target > nums[right]) { + return -1; + } + if (nums[left] == target) { + return left; + } else if (nums[right] == target) { + return right; + } + while (left <= right) { + int mid = left + (right - left) / 2; + if (target == nums[mid]) { + return mid; + } else if (target > nums[mid]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return -1; + } + } + + public static class Solution2 { + public int search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) { + return mid; + } else if (nums[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + if (left <= nums.length && nums[left] == target) { + return left; + } + if (right >= 0 && nums[right] == target) { + return right; + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_705.java b/src/main/java/com/fishercoder/solutions/firstthousand/_705.java new file mode 100644 index 0000000000..097838f053 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_705.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _705 { + public static class Solution1 { + class MyHashSet { + Map map; + + /* + * Initialize your data structure here. + */ + public MyHashSet() { + map = new HashMap<>(); + } + + public void add(int key) { + map.put(key, 0); + } + + public void remove(int key) { + if (map.containsKey(key)) { + map.remove(key); + } + } + + /* + * Returns true if this set contains the specified element + */ + public boolean contains(int key) { + return map.containsKey(key); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_706.java b/src/main/java/com/fishercoder/solutions/firstthousand/_706.java new file mode 100644 index 0000000000..de74c2bd73 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_706.java @@ -0,0 +1,195 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _706 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/design-hashmap/discuss/152746/Java-Solution + */ + class MyHashMap { + + final ListNode[] nodes = new ListNode[10000]; + + public void put(int key, int value) { + int i = idx(key); + if (nodes[i] == null) { + nodes[i] = new ListNode(-1, -1); + } + ListNode prev = find(nodes[i], key); + if (prev.next == null) { + prev.next = new ListNode(key, value); + } else { + prev.next.val = value; + } + } + + public int get(int key) { + int i = idx(key); + if (nodes[i] == null) { + return -1; + } + ListNode node = find(nodes[i], key); + return node.next == null ? -1 : node.next.val; + } + + public void remove(int key) { + int i = idx(key); + if (nodes[i] == null) { + return; + } + ListNode prev = find(nodes[i], key); + if (prev.next == null) { + return; + } + prev.next = prev.next.next; + } + + int idx(int key) { + return Integer.hashCode(key) % nodes.length; + } + + ListNode find(ListNode bucket, int key) { + ListNode node = bucket; + ListNode prev = null; + while (node != null && node.key != key) { + prev = node; + node = node.next; + } + return prev; + } + + class ListNode { + int key; + int val; + ListNode next; + + ListNode(int key, int val) { + this.key = key; + this.val = val; + } + } + } + } + + public static class Solution2 { + + public static class MyHashMap { + /* + * Considering the given constraints for this problem on LeetCode, load factors and resizing/rehashing are not considered. Thus an EASY problem. + *

+ * inspired by: https://leetcode.com/problems/design-hashmap/discuss/225312/hashmaparraylinkedlistcollision + */ + class Node { + /* + * We need to have both key and val in this ListNode because all values input key are hashed to the same bucket, so we need its original key + * to be a differentiator within this bucket. + */ + int val; + int key; + Node next; + + public Node(int key, int val) { + this.key = key; + this.val = val; + } + } + + Node[] nodes; + int size = 1000000; + + /* + * Initialize your data structure here. + */ + public MyHashMap() { + nodes = new Node[size]; + } + + /* + * value will always be non-negative. + */ + public void put(int key, int value) { + int index = getHashedKey(key); + Node head = nodes[index]; + Node tmp = head; + while (tmp != null) { + if (tmp.key == key) { + tmp.val = value; + return; + } + tmp = tmp.next; + } + Node newHead = new Node(key, value); + newHead.next = head; + nodes[index] = newHead; + } + + private int getHashedKey(int key) { + return Integer.hashCode(key) % size; + } + + /* + * Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key + */ + public int get(int key) { + Node head = nodes[getHashedKey(key)]; + Node tmp = head; + while (tmp != null && tmp.key != key) { + tmp = tmp.next; + } + if (tmp == null) { + return -1; + } + if (tmp.key == key) { + return tmp.val; + } + return -1; + } + + /* + * Removes the mapping of the specified value key if this map contains a mapping for the key + */ + public void remove(int key) { + /*We can just set the value of this key to -1 since the constraint for this problem is that all values are >= 0*/ + Node node = nodes[getHashedKey(key)]; + Node tmp = node; + Node pre = new Node(-1, -1); + pre.next = tmp; + while (tmp != null) { + if (tmp.key == key) { + tmp.val = -1; + return; + } + tmp = tmp.next; + } + } + } + } + + public static class Solution3 { + /* + * My completely original, but hacky and cheaty solution to take full advantage of the problem constraints. + */ + public static class MyHashMap { + + int[] map; + + public MyHashMap() { + map = new int[1000001]; + Arrays.fill(map, -1); + } + + public void put(int key, int value) { + map[key] = value; + } + + public int get(int key) { + return map[key]; + } + + public void remove(int key) { + map[key] = -1; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_708.java b/src/main/java/com/fishercoder/solutions/firstthousand/_708.java new file mode 100644 index 0000000000..9cf28e8920 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_708.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.firstthousand; + +public class _708 { + static class Node { + public int val; + public Node next; + + public Node(int val, Node next) { + this.val = val; + this.next = next; + } + + public Node(int val) { + this.val = val; + } + + public Node() {} + } + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list/solutions/995676/java-concise-o-n/ + */ + public Node insert(Node head, int insertVal) { + Node insertNode = new Node(insertVal); + if (head == null) { + insertNode.next = insertNode; + return insertNode; + } + Node node = head; + while (node.next != head) { + if (node.val <= node.next.val) { + // increasing + if (node.val <= insertVal && insertVal <= node.next.val) { + // this is the place to insert + break; + } + } else { + // transition point, higher value to lower value + if (node.val < insertVal || insertVal < node.next.val) { + break; + } + } + node = node.next; + } + // insert this new node + insertNode.next = node.next; + node.next = insertNode; + + return head; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_709.java b/src/main/java/com/fishercoder/solutions/firstthousand/_709.java new file mode 100644 index 0000000000..442b0ec373 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_709.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _709 { + public static class Solution1 { + public String toLowerCase(String s) { + return s.toLowerCase(); + } + } + + public static class Solution2 { + public String toLowerCase(String s) { + Map map = new HashMap<>(); + String upper = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + String lower = new String("abcdefghijklmnopqrstuvwxyz"); + for (int i = 0; i < upper.length(); i++) { + map.put(upper.charAt(i), lower.charAt(i)); + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + if (map.containsKey(s.charAt(i))) { + sb.append(map.get(s.charAt(i))); + } else { + sb.append(s.charAt(i)); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_71.java b/src/main/java/com/fishercoder/solutions/firstthousand/_71.java new file mode 100644 index 0000000000..0f7152fa54 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_71.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.Deque; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Set; + +public class _71 { + + public static class Solution1 { + /* + * For LinkedList class in Java, if you use pop(), then you'll have to use push() as its corresponding method to remove from the "top" of the stack, using pollLast() will not work. + */ + public String simplifyPath(String path) { + Deque stack = new LinkedList<>(); + Set skipSet = new HashSet<>(Arrays.asList("..", ".", "")); + for (String dir : path.split("/")) { + if (dir.equals("..") && !stack.isEmpty()) { + stack.pop(); + } else if (!skipSet.contains(dir)) { + stack.push(dir); + } + } + String result = ""; + for (String dir : stack) { + result = "/" + dir + result; + } + return result.isEmpty() ? "/" : result; + } + } + + public static class Solution2 { + /* + * This solution doesn't vary too much from the above one, except in that it's using pollLast() and addLast() instead of pop() and push(). + * Key notes: + * if using pollLast, then it must be consistent across all calls, including peekLast() and addLast(), cannot mix with pop() and push(), otherwise, unexpected/undesired results will happen. + */ + public String simplifyPath(String path) { + Set skip = new HashSet(Arrays.asList("..", ".", "")); + Deque stack = new LinkedList(); + String[] directories = path.split("/"); + for (String dir : directories) { + if (dir.equals("..") && !stack.isEmpty()) { + stack.pollLast(); + } else if (!skip.contains(dir)) { + stack.addLast(dir); + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append("/"); + sb.append(stack.pollFirst()); + } + return sb.length() == 0 ? "/" : sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_712.java b/src/main/java/com/fishercoder/solutions/firstthousand/_712.java new file mode 100644 index 0000000000..9444c4c3d8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_712.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +public class _712 { + public static class Solution1 { + // credit: https://leetcode.com/articles/minimum-ascii-delete-sum-for-two-strings/ + public int minimumDeleteSum(String s1, String s2) { + int[][] dp = new int[s1.length() + 1][s2.length() + 1]; + + for (int i = s1.length() - 1; i >= 0; i--) { + dp[i][s2.length()] = dp[i + 1][s2.length()] + s1.codePointAt(i); + } + + for (int j = s2.length() - 1; j >= 0; j--) { + dp[s1.length()][j] = dp[s1.length()][j + 1] + s2.codePointAt(j); + } + + for (int i = s1.length() - 1; i >= 0; i--) { + for (int j = s2.length() - 1; j >= 0; j--) { + if (s1.charAt(i) == s2.charAt(j)) { + dp[i][j] = dp[i + 1][j + 1]; + } else { + dp[i][j] = + Math.min( + dp[i + 1][j] + s1.codePointAt(i), + dp[i][j + 1] + s2.codePointAt(j)); + } + } + } + + return dp[0][0]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_713.java b/src/main/java/com/fishercoder/solutions/firstthousand/_713.java new file mode 100644 index 0000000000..0b60cb320c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_713.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _713 { + public static class Solution1 { + /* + * Classic two pointer technique, in this problem: + * the right pointer keeps moving forward and the left pointer moves forward when the current product >= k + */ + public int numSubarrayProductLessThanK(int[] nums, int k) { + if (k < 2) { + return 0; + } + int result = 0; + int product = 1; + for (int left = 0, right = 0; right < nums.length; right++) { + product *= nums[right]; + while (left < nums.length && product >= k) { + product /= nums[left++]; + } + result += right - left + 1; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_714.java b/src/main/java/com/fishercoder/solutions/firstthousand/_714.java new file mode 100644 index 0000000000..2599bc5051 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_714.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +public class _714 { + public static class Solution1 { + /* + * O(n) time + * O(n) space + * credit: https://discuss.leetcode.com/topic/108009/java-c-clean-code-dp-greedy + */ + public int maxProfit(int[] prices, int fee) { + int n = prices.length; + if (n < 2) { + return 0; + } + int[] hold = new int[n]; + int[] sell = new int[n]; + hold[0] = -prices[0]; + for (int i = 1; i < prices.length; i++) { + hold[i] = Math.max(hold[i - 1], sell[i - 1] - prices[i]); + sell[i] = Math.max(sell[i - 1], hold[i - 1] - fee + prices[i]); + } + return sell[n - 1]; + } + } + + public static class Solution2 { + /* + * O(n) time + * O(1) space + * credit: https://leetcode.com/articles/best-time-to-buy-and-sell-stock-with-transaction-fee/ + *

+ * cash: the max profit we could have if we did not have a share of stock in hand + * hold: the max profit we could have if we hold one share of stack in hand + *

+ * to transition from the i-th day to the i+1 th day, we have two options: + * 1. sell our stock: cash = Math.max(cash, hold + prices[i] - fee) + * 2. buy a stock: hold = Math.max(hold, cash - prices[i]) + */ + public int maxProfit(int[] prices, int fee) { + int cash = 0; + int hold = -prices[0]; + for (int i = 1; i < prices.length; i++) { + cash = + Math.max( + cash, + hold + prices[i] + - fee); // this means to sell the stock: gain the current + // ith day's price and pay the transaction fee + hold = + Math.max( + hold, + cash - prices[i]); // this means to buy in this stock on the ith + // day's price. + } + return cash; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_716.java b/src/main/java/com/fishercoder/solutions/firstthousand/_716.java new file mode 100644 index 0000000000..6d26f502af --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_716.java @@ -0,0 +1,252 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Deque; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Stack; +import java.util.TreeMap; + +public class _716 { + public static class Solution1 { + /* + * This is O(n) for popMax() and pop() while O(1) for the other three operations which is UN-acceptable during an interview! + * We need to do better than O(n) time complexity in order to ace the interview! + * But O(1) is impossible, so let's aim for O(logn). + */ + public static class MaxStack { + + private int max; + private Stack stack; + + /* + * initialize your data structure here. + */ + public MaxStack() { + max = Integer.MIN_VALUE; + stack = new Stack<>(); + } + + public void push(int x) { + if (x > max) { + max = x; + } + stack.push(x); + } + + public int pop() { + if (stack.peek() == max) { + int result = stack.pop(); + max = findMax(); + return result; + } else { + return stack.pop(); + } + } + + private int findMax() { + if (!stack.isEmpty()) { + Iterator iterator = stack.iterator(); + int max = stack.peek(); + while (iterator.hasNext()) { + max = Math.max(max, iterator.next()); + } + return max; + } else { + max = Integer.MIN_VALUE; + return max; + } + } + + public int top() { + return stack.peek(); + } + + public int peekMax() { + return max; + } + + public int popMax() { + Stack tmp = new Stack<>(); + int result = 0; + while (!stack.isEmpty()) { + if (stack.peek() != max) { + tmp.push(stack.pop()); + } else { + result = stack.pop(); + break; + } + } + while (!tmp.isEmpty()) { + stack.push(tmp.pop()); + } + max = findMax(); + return result; + } + } + } + + public static class Solution2 { + /* + * Use a treemap and a doubly linked list to achieve O(logn) time complexity. + */ + + static class Node { + int val; + Node prev; + Node next; + + public Node(int val) { + this.val = val; + } + } + + static class DoublyLinkedList { + Node head; + Node tail; + + public DoublyLinkedList() { + head = new Node(0); + tail = new Node(0); + head.next = tail; + tail.prev = head; + } + + public Node add(int val) { + /*For this doubly linked list, we always add it to the end of the list*/ + Node x = new Node(val); + x.next = tail; + x.prev = tail.prev; + tail.prev.next = x; + tail.prev = tail.prev.next; + return x; + } + + public int pop() { + /*for pop(), we always pop one from the tail of the doubly linked list*/ + return unlink(tail.prev).val; + } + + public Node unlink(Node node) { + node.prev.next = node.next; + node.next.prev = node.prev; + return node; + } + + public int peek() { + return tail.prev.val; + } + } + + public static class MaxStack { + TreeMap> treeMap; + /* + * the reason we have a list of nodes as treemap's value is because one value could be pushed + * multiple times into this MaxStack and we want to keep track of all of them. + */ + DoublyLinkedList doublyLinkedList; + + /* + * initialize your data structure here. + */ + public MaxStack() { + treeMap = new TreeMap(); + doublyLinkedList = new DoublyLinkedList(); + } + + public void push(int x) { + Node node = doublyLinkedList.add(x); + if (!treeMap.containsKey(x)) { + treeMap.put(x, new ArrayList<>()); + } + treeMap.get(x).add(node); + } + + public int pop() { + int val = doublyLinkedList.pop(); + List nodes = treeMap.get(val); + nodes.remove(nodes.size() - 1); + if (nodes.isEmpty()) { + treeMap.remove(val); + } + return val; + } + + public int top() { + return doublyLinkedList.peek(); + } + + public int peekMax() { + return treeMap.lastKey(); + } + + public int popMax() { + int max = treeMap.lastKey(); + List nodes = treeMap.get(max); + Node node = nodes.remove(nodes.size() - 1); + doublyLinkedList.unlink(node); + if (nodes.isEmpty()) { + treeMap.remove(max); + } + return max; + } + } + } + + public static class Solution3 { + /* + * My completely original solution on 10/25/2021. + * popMax() takes O(n) time, all other operations take O(1) time. + */ + + public static class MaxStack { + + Deque stack; + Deque tmp; + + public MaxStack() { + stack = new LinkedList<>(); + tmp = new LinkedList<>(); + } + + public void push(int x) { + if (stack.isEmpty()) { + stack.addLast(new int[] {x, x}); + } else { + int[] last = stack.peekLast(); + stack.addLast(new int[] {x, Math.max(last[1], x)}); + } + } + + public int pop() { + return stack.pollLast()[0]; + } + + public int top() { + return stack.peekLast()[0]; + } + + public int peekMax() { + return stack.peekLast()[1]; + } + + public int popMax() { + tmp.clear(); + while (stack.peekLast()[0] != stack.peekLast()[1]) { + tmp.addLast(stack.pollLast()); + } + int[] max = stack.pollLast(); + while (!tmp.isEmpty()) { + int[] curr = tmp.pollLast(); + if (!stack.isEmpty()) { + stack.addLast(new int[] {curr[0], Math.max(curr[0], stack.peekLast()[1])}); + } else { + stack.addLast(new int[] {curr[0], curr[0]}); + } + } + return max[0]; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_717.java b/src/main/java/com/fishercoder/solutions/firstthousand/_717.java new file mode 100644 index 0000000000..6e5d21b379 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_717.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.firstthousand; + +public class _717 { + public static class Solution1 { + public boolean isOneBitCharacter(int[] bits) { + int n = bits.length; + int i = 0; + while (i < n - 1) { + if (bits[i] == 0) { + i++; + } else { + i += 2; + } + } + return i == n - 1 ? true : false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_718.java b/src/main/java/com/fishercoder/solutions/firstthousand/_718.java new file mode 100644 index 0000000000..214c0e564b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_718.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _718 { + public static class Solution1 { + /* + * This brute force idea results in TLE. + */ + public int findLength(int[] nums1, int[] nums2) { + int[] shorter = nums1.length < nums2.length ? nums1 : nums2; + int[] longer = nums1.length < nums2.length ? nums2 : nums1; + int maxLen = 0; + for (int i = 0; i < shorter.length - 1; i++) { + for (int j = shorter.length; j >= 0 && j > i && j - i > maxLen; j--) { + int[] candidate = Arrays.copyOfRange(shorter, i, j); + if (candidate.length > maxLen && isSubarray(candidate, longer)) { + maxLen = candidate.length; + } + } + } + return maxLen; + } + + private boolean isSubarray(int[] candidate, int[] array) { + for (int i = 0; i <= array.length - candidate.length; i++) { + if (Arrays.equals(candidate, Arrays.copyOfRange(array, i, i + candidate.length))) { + return true; + } + } + return false; + } + } + + public static class Solution2 { + /* + * DP approach: + * credit: https://leetcode.com/problems/maximum-length-of-repeated-subarray/solution/ + * 1. we initialize a 2D matrix as the cash to hold values, initially, all are zeroes, + * the number of rows of this matrix is the length of nums1 plus one and the number of columns is the length of nums2 plus one; + * 2. Since a common subarray of A and B must start at some A[i] and B[j], let dp[i][j] be the longest common prefix of A[i:] and B[j:]. + * Whenever A[i] == B[j], we know dp[i][j] = dp[i+1][j+1] + 1. Also, the answer is max(dp[i][j]) over all i, j. + * + * We can perform bottom-up dynamic programming to find the answer based on this recurrence. + * Our loop invariant is that the answer is already calculated correctly and stored in dp for any larger i, j. + */ + public int findLength(int[] nums1, int[] nums2) { + if (nums1 == null || nums2 == null) { + return 0; + } + int m = nums1.length; + int n = nums2.length; + int max = 0; + int[][] dp = new int[m + 1][n + 1]; + for (int i = m - 1; i >= 0; i--) { + for (int j = n - 1; j >= 0; j--) { + if (nums1[i] == nums2[j]) { + dp[i][j] = dp[i + 1][j + 1] + 1; + max = Math.max(max, dp[i][j]); + } + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_719.java b/src/main/java/com/fishercoder/solutions/firstthousand/_719.java new file mode 100644 index 0000000000..5432a77425 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_719.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.PriorityQueue; + +public class _719 { + public static class Solution1 { + /* + * This brute force solution results in TLE of course. + */ + public int smallestDistancePair(int[] nums, int k) { + if (nums == null || nums.length == 0) { + return 0; + } + PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a - b); + for (int i = 0; i < nums.length - 1; i++) { + for (int j = i + 1; j < nums.length; j++) { + minHeap.offer(Math.abs(nums[j] - nums[i])); + } + } + + int result = 0; + while (k-- > 0) { + result = minHeap.poll(); + } + return result; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/articles/find-k-th-smallest-pair-distance/#approach-3-binary-search-sliding-window-accepted + */ + public int smallestDistancePair(int[] nums, int k) { + Arrays.sort(nums); + int lo = 0; + int hi = nums[nums.length - 1] - nums[0]; + while (lo < hi) { + int mi = (lo + hi) / 2; + int count = 0; + int left = 0; + for (int right = 0; right < nums.length; ++right) { + while (nums[right] - nums[left] > mi) { + left++; + } + count += right - left; + } + // count = number of pairs with distance <= mi + if (count >= k) { + hi = mi; + } else { + lo = mi + 1; + } + } + return lo; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_72.java b/src/main/java/com/fishercoder/solutions/firstthousand/_72.java new file mode 100644 index 0000000000..9505162ac6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_72.java @@ -0,0 +1,87 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _72 { + + public static class Solution1 { + public int minDistance(String word1, String word2) { + int m = word1.length(); + int n = word2.length(); + if (m == 0) { + return n; + } + if (n == 0) { + return m; + } + char[] str1 = word1.toCharArray(); + char[] str2 = word2.toCharArray(); + + int[][] table = new int[m + 1][n + 1]; + for (int i = 0; i < m + 1; i++) { + table[i][0] = i; + } + for (int j = 0; j < n + 1; j++) { + table[0][j] = j; + } + for (int i = 1; i < m + 1; i++) { + for (int j = 1; j < n + 1; j++) { + int cost = 0; + if (str1[i - 1] != str2[j - 1]) { + cost = 1; + } + table[i][j] = + Math.min( + Math.min(table[i - 1][j] + 1, table[i][j - 1] + 1), + table[i - 1][j - 1] + cost); + } + } + return table[m][n]; + } + } + + public static class Solution2 { + public int minDistance(String word1, String word2) { + // using levenshtein Distance to find minimum transformation operations required from + // word 1 to word 2 + int[][] dp = new int[word1.length()][word2.length()]; + // fill the dp array with -1 value + for (int[] rows : dp) { + Arrays.fill(rows, -1); + } + return levenshteinDistance(word1, word1.length() - 1, word2, word2.length() - 1, dp); + } + + private int levenshteinDistance( + String s1, int s1Index, String s2, int s2Index, int[][] dp) { + + if (s1Index < 0) { // when s1 is "" perform all insertions to get s1 to s2 + return s2Index + 1; + } else if (s2Index < 0) { // when s2 is "" perform all deletions from s1 + return s1Index + 1; + } + + // base condition when dp array is filled, return the distance + if (dp[s1Index][s2Index] != -1) { + return dp[s1Index][s2Index]; + } + + if (s1.charAt(s1Index) == s2.charAt(s2Index)) { + // Characters match, no edit distance to be calculated + dp[s1Index][s2Index] = levenshteinDistance(s1, s1Index - 1, s2, s2Index - 1, dp); + } else { + // When there is a character mismatch, perform operations + + // Insertion + int insertValue = levenshteinDistance(s1, s1Index, s2, s2Index - 1, dp); + int deleteValue = levenshteinDistance(s1, s1Index - 1, s2, s2Index, dp); + int substituteValue = levenshteinDistance(s1, s1Index - 1, s2, s2Index - 1, dp); + + /* Now we need to take the minimum of the 3 operations to find the edit distance and add 1 to the min cost action denoting that an action is performed */ + dp[s1Index][s2Index] = + 1 + Math.min(insertValue, Math.min(deleteValue, substituteValue)); + } + return dp[s1Index][s2Index]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_720.java b/src/main/java/com/fishercoder/solutions/firstthousand/_720.java new file mode 100644 index 0000000000..99fd1fade4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_720.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.firstthousand; + +public class _720 { + public static class Solution1 { + public String longestWord(String[] words) { + TrieNode root = buildTrie(words); + return findLongestWord(root, words); + } + + private String findLongestWord(TrieNode root, String[] words) { + String longestWord = ""; + for (String word : words) { + if (longestWord.length() > word.length() + || (longestWord.length() == word.length() + && (longestWord.compareToIgnoreCase(word) < 0))) { + continue; + } + TrieNode tmp = root; + boolean validWord = true; + for (char c : word.toCharArray()) { + if (tmp.children[c - 'a'] != null) { + tmp = tmp.children[c - 'a']; + if (!tmp.isWord) { + validWord = false; + break; + } + } + } + if (validWord) { + longestWord = word; + } + } + return longestWord; + } + + private TrieNode buildTrie(String[] words) { + TrieNode root = new TrieNode(' '); + for (String word : words) { + TrieNode tmp = root; + for (char c : word.toCharArray()) { + if (tmp.children[c - 'a'] == null) { + tmp.children[c - 'a'] = new TrieNode(c); + } + tmp = tmp.children[c - 'a']; + } + tmp.isWord = true; + } + return root; + } + + class TrieNode { + char val; + boolean isWord; + TrieNode[] children; + + public TrieNode(char val) { + this.val = val; + this.isWord = false; + this.children = new TrieNode[26]; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_721.java b/src/main/java/com/fishercoder/solutions/firstthousand/_721.java new file mode 100644 index 0000000000..89d17417a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_721.java @@ -0,0 +1,133 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; + +public class _721 { + + public static class Solution1 { + /* + * credit: https://leetcode.com/articles/accounts-merge/#approach-1-depth-first-search-accepted + *

+ * Time Complexity: O(∑ai*logai) where a​i is the length of accounts[i]. + * Without the log factor, this is the complexity to build the graph and search for each component. The log factor is for sorting each component at the end. + * Space Complexity: O(∑ai) the space used by the graph and search. + * . + */ + public List> accountsMerge(List> accounts) { + Map emailToName = new HashMap(); + Map> graph = new HashMap(); + for (List account : accounts) { + String name = ""; + for (String email : account) { + if (name == "") { + name = email; + continue; + } + graph.computeIfAbsent(email, x -> new ArrayList<>()).add(account.get(1)); + graph.computeIfAbsent(account.get(1), x -> new ArrayList<>()).add(email); + emailToName.put(email, name); + } + } + + Set seen = new HashSet(); + List> ans = new ArrayList(); + for (String email : graph.keySet()) { + if (!seen.contains(email)) { + seen.add(email); + Stack stack = new Stack(); + stack.push(email); + List component = new ArrayList(); + while (!stack.empty()) { + String node = stack.pop(); + component.add(node); + for (String nei : graph.get(node)) { + if (!seen.contains(nei)) { + seen.add(nei); + stack.push(nei); + } + } + } + Collections.sort(component); + component.add(0, emailToName.get(email)); + ans.add(component); + } + } + return ans; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/articles/accounts-merge/#approach-2-union-find-accepted + * DSU stands for Disjoint Set Union: https://en.wikipedia.org/wiki/Disjoint-set_data_structure, a.k.a Union Find data structure. + *

+ * Time complexity: O(nlogn) + * Space complexity: O(n) + */ + public List> accountsMerge(List> accounts) { + UnionFind uf = new UnionFind(); + Map emailToName = new HashMap<>(); + Map emailToId = new HashMap<>(); + int id = 0; + for (List account : accounts) { + String name = ""; + for (String email : account) { + if (name.equals("")) { + name = email; + continue; + } + emailToName.put(email, name); + if (!emailToId.containsKey(email)) { + emailToId.put(email, id++); + } + uf.union(emailToId.get(account.get(1)), emailToId.get(email)); + } + } + + Map> map = new HashMap<>(); + for (String email : emailToName.keySet()) { + // find the index of this email first: use this email's ID to find its parent in the + // Union Find + int index = uf.find(emailToId.get(email)); + map.computeIfAbsent(index, x -> new ArrayList()).add(email); + } + for (List component : map.values()) { + Collections.sort(component); + // this is to add name to the head of the list + component.add(0, emailToName.get(component.get(0))); + } + return new ArrayList<>(map.values()); + } + + class UnionFind { + int[] parent; + int size = 10001; + + public UnionFind() { + parent = new int[size]; + for (int i = 0; i < size; i++) { + parent[i] = i; + } + } + + public int find(int x) { + if (parent[x] != x) { + parent[x] = find(parent[x]); + } + return parent[x]; + } + + public void union(int x, int y) { + parent[find(x)] = + find(y); // can be written as parent[find(y)] = find(x); they are equivalent + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_722.java b/src/main/java/com/fishercoder/solutions/firstthousand/_722.java new file mode 100644 index 0000000000..5aead62955 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_722.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _722 { + public static class Solution1 { + /* + * My completely original solution. + * Not a hard one, just some corner cases to consider. + */ + public List removeComments(String[] source) { + List result = new ArrayList<>(); + StringBuilder sb = new StringBuilder(); + boolean possiblyMultilineComment = false; + for (String line : source) { + for (int i = 0; i < line.length(); i++) { + if (line.charAt(i) == '/' && i + 1 < line.length()) { + // adding (&& !possiblyMultilineComment) is for cases like test 6 + if (line.charAt(i + 1) == '*' && !possiblyMultilineComment) { + // but cannot break here, because this might be a single line comment, + // so we need to find the closing "*/" + possiblyMultilineComment = true; + // increment i by one to bypass this '*' + i++; + } else if (line.charAt(i + 1) == '/') { + // this is a single line comment, remove + if (!possiblyMultilineComment) { + // only at this time, we know this is not part of a possibly + // multiline comment, + // then we can safely break out, see test case 4 + break; + } + } else if (!possiblyMultilineComment) { + sb.append(line.charAt(i)); + } + } else if (line.charAt(i) == '*' + && i + 1 < line.length() + && line.charAt(i + 1) == '/' + && possiblyMultilineComment) { + // this is the end of the multiline comment + possiblyMultilineComment = false; + // increment i by one to bypass the closing '/' + i++; + } else if (!possiblyMultilineComment) { + sb.append(line.charAt(i)); + } + } + if (sb.length() > 0 && !possiblyMultilineComment) { + result.add(sb.toString()); + sb.setLength(0); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_723.java b/src/main/java/com/fishercoder/solutions/firstthousand/_723.java new file mode 100644 index 0000000000..c8024d4337 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_723.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.firstthousand; + +public class _723 { + public static class Solution1 { + /* + * credit: https://leetcode.com/articles/candy-crush/ + */ + public int[][] candyCrush(int[][] board) { + int row = board.length; + int col = board[0].length; + boolean todo = false; + for (int i = 0; i < row; i++) { + for (int j = 0; j < col - 2; j++) { + int v = Math.abs(board[i][j]); + if (v != 0 + && v == Math.abs(board[i][j + 1]) + && v == Math.abs(board[i][j + 2])) { + // mark all of them to be negative + board[i][j] = board[i][j + 1] = board[i][j + 2] = -v; + todo = true; + } + } + } + + for (int i = 0; i < row - 2; i++) { + for (int j = 0; j < col; j++) { + int v = Math.abs(board[i][j]); + if (v != 0 + && v == Math.abs(board[i + 1][j]) + && v == Math.abs(board[i + 2][j])) { + board[i + 1][j] = board[i + 2][j] = board[i][j] = -v; + todo = true; + } + } + } + + for (int j = 0; j < col; j++) { + int wr = row - 1; + for (int i = row - 1; i >= 0; i--) { + if (board[i][j] > 0) { + board[wr--][j] = board[i][j]; + } + } + while (wr >= 0) { + board[wr--][j] = 0; + } + } + + return todo ? candyCrush(board) : board; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_724.java b/src/main/java/com/fishercoder/solutions/firstthousand/_724.java new file mode 100644 index 0000000000..ccbe4bb5d8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_724.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +public class _724 { + public static class Solution1 { + /* + * Space: O(n) + * Time: O(n) + */ + public int pivotIndex(int[] nums) { + if (nums == null || nums.length == 0) { + return -1; + } + int[] sums = new int[nums.length]; + sums[0] = nums[0]; + for (int i = 1; i < nums.length; i++) { + sums[i] = sums[i - 1] + nums[i]; + } + for (int i = 0; i < nums.length; i++) { + if (i == 0 && 0 == sums[nums.length - 1] - sums[i] + || (i > 0 && sums[i - 1] == sums[nums.length - 1] - sums[i])) { + return i; + } + } + return -1; + } + } + + public static class Solution2 { + /* + * Space: O(1) + * Time: O(n) + */ + public int pivotIndex(int[] nums) { + int total = 0; + for (int num : nums) { + total += num; + } + int sum = 0; + for (int i = 0; i < nums.length; sum += nums[i++]) { + if (sum * 2 == total - nums[i]) { + return i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_725.java b/src/main/java/com/fishercoder/solutions/firstthousand/_725.java new file mode 100644 index 0000000000..9efe0002be --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_725.java @@ -0,0 +1,78 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _725 { + public static class Solution1 { + /* + * My very original solution, but verbose. + */ + public ListNode[] splitListToParts(ListNode head, int k) { + int len = getLength(head); + int aveSize = len / k; + int extra = len % k; + ListNode[] result = new ListNode[k]; + for (int i = 0; i < k; i++) { + result[i] = head; + int aveSizeTmp = aveSize; + aveSizeTmp += extra-- > 0 ? 1 : 0; + int aveSizeTmp2 = aveSizeTmp; + while (aveSizeTmp-- > 0) { + head = head.next; + } + if (result[i] != null) { + ListNode tmp = result[i]; + while (tmp.next != null && aveSizeTmp2-- > 1) { + tmp = tmp.next; + } + tmp.next = null; + } + } + return result; + } + + private int getLength(ListNode root) { + int len = 0; + ListNode tmp = root; + while (tmp != null) { + len++; + tmp = tmp.next; + } + return len; + } + } + + public static class Solution2 { + /* + * More concise version + */ + public ListNode[] splitListToParts(ListNode head, int k) { + int len = getLength(head); + int aveSize = len / k; + int extra = len % k; + ListNode[] result = new ListNode[k]; + ListNode prev = null; + for (int i = 0; i < k; i++, extra--) { + result[i] = head; + for (int j = 0; j < aveSize + (extra > 0 ? 1 : 0); j++) { + prev = head; + head = head.next; + } + if (prev != null) { + prev.next = null; + } + } + return result; + } + + private int getLength(ListNode root) { + int len = 0; + ListNode tmp = root; + while (tmp != null) { + len++; + tmp = tmp.next; + } + return len; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_726.java b/src/main/java/com/fishercoder/solutions/firstthousand/_726.java new file mode 100644 index 0000000000..32f03cb10c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_726.java @@ -0,0 +1,110 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Deque; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class _726 { + public static class Solution1 { + /* + * My completely original solution: + * 1. use a stack; + * 2. whenever we encounter the open paren, we push it into the top of the stack; + * 3. whenever we encounter an uppercase, we check to get its full atom letters, + * then check to get the number after it if there's any, then form a pair objet and push onto the stack; + * 4. whenever we encounter the closed paren, we check if there's any number after it, + * then poll all items on top of the stack off onto a new/temp stack until we encounter the corresponding open paren, + * then add these items from the temp stack back into the original/main stack; + */ + public String countOfAtoms(String formula) { + Deque stack = new LinkedList<>(); + for (int i = 0; i < formula.length(); i++) { + char curr = formula.charAt(i); + if (curr == '(') { + stack.addLast(new Pair("(", 1)); + } else if (Character.isUpperCase(curr)) { + StringBuilder sb = new StringBuilder(curr + ""); + i++; + while (i < formula.length() && Character.isLowerCase(formula.charAt(i))) { + sb.append(formula.charAt(i++)); + } + if (i < formula.length()) { + if (Character.isUpperCase(formula.charAt(i)) + || formula.charAt(i) == '(' + || formula.charAt(i) == ')') { + // no numbers + stack.addLast(new Pair(sb.toString(), 1)); + i--; + } else { + // there are numbers + StringBuilder numberSb = new StringBuilder(); + while (i < formula.length() && Character.isDigit(formula.charAt(i))) { + numberSb.append(formula.charAt(i++)); + } + i--; + stack.addLast( + new Pair(sb.toString(), Integer.parseInt(numberSb.toString()))); + } + } else { + stack.addLast(new Pair(sb.toString(), 1)); + } + } else if (curr == ')') { + i++; + StringBuilder sb = new StringBuilder(); + while (i < formula.length() && Character.isDigit(formula.charAt(i))) { + sb.append(formula.charAt(i)); + i++; + } + i--; + int number = 1; + if (sb.length() > 0) { + number = Integer.parseInt(sb.toString()); + } + Deque stack2 = new LinkedList<>(); + while (!stack.isEmpty() && !stack.peekLast().atom.equals("(")) { + Pair pair = stack.pollLast(); + stack2.addLast(new Pair(pair.atom, pair.count * number)); + } + stack.pollLast(); // poll "(" off of the stack + while (!stack2.isEmpty()) { + stack.addLast(stack2.pollLast()); + } + } + } + List list = new ArrayList<>(); + while (!stack.isEmpty()) { + list.add(stack.pollLast()); + } + // now merge the same atoms + Map map = new HashMap<>(); + for (Pair pair : list) { + map.put(pair.atom, map.getOrDefault(pair.atom, 0) + pair.count); + } + // now add the merged atoms into the list again before sorting them + list.clear(); + for (Map.Entry entry : map.entrySet()) { + list.add(new Pair(entry.getKey(), entry.getValue())); + } + Collections.sort(list, (a, b) -> a.atom.compareToIgnoreCase(b.atom)); + StringBuilder sb = new StringBuilder(); + for (Pair pair : list) { + sb.append(pair.atom + (pair.count == 1 ? "" : pair.count)); + } + return sb.toString(); + } + + class Pair { + String atom; + int count; + + public Pair(String atom, int count) { + this.atom = atom; + this.count = count; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_727.java b/src/main/java/com/fishercoder/solutions/firstthousand/_727.java new file mode 100644 index 0000000000..cf10f049d3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_727.java @@ -0,0 +1,62 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _727 { + public static class Solution1 { + /* + * This naive brute force results in TLE. + */ + public String minWindow(String S, String T) { + String result = S; + for (int i = 0; i < S.length(); i++) { + for (int j = i + T.length(); j <= S.length(); j++) { + String sub = S.substring(i, j); + if (sub.length() < result.length() && isSubsequence(T, sub)) { + result = sub; + } + } + } + return result.equals(S) ? "" : result; + } + + private boolean isSubsequence(String T, String sub) { + int i = 0; + for (int j = 0; i < T.length() && j < sub.length(); j++) { + if (T.charAt(i) == sub.charAt(j)) { + i++; + } + } + return i == T.length(); + } + } + + public static class Solution2 { + /* + * credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_727.java + */ + public String minWindow(String S, String T) { + int[][] dp = new int[S.length() + 1][T.length() + 1]; + int INFINITY = 1 << 29; + Arrays.fill(dp[0], INFINITY); + dp[0][0] = 0; + for (int i = 1; i <= S.length(); i++) { + for (int j = 1; j <= T.length(); j++) { + dp[i][j] = dp[i - 1][j] + 1; + if (S.charAt(i - 1) == T.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } + } + } + int ans = INFINITY; + int tail = -1; + for (int i = 1; i <= S.length(); i++) { + if (dp[i][T.length()] < ans) { + ans = dp[i][T.length()]; + tail = i; + } + } + return ans == INFINITY ? "" : S.substring(tail - ans, tail); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_728.java b/src/main/java/com/fishercoder/solutions/firstthousand/_728.java new file mode 100644 index 0000000000..63f9d0a663 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_728.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _728 { + public static class Solution1 { + public List selfDividingNumbers(int left, int right) { + List result = new ArrayList<>(); + for (int num = left; num <= right; num++) { + if (isSelfDividing(num)) { + result.add(num); + } + } + return result; + } + + private boolean isSelfDividing(int num) { + int tmp = num; + while (tmp != 0) { + int digit = tmp % 10; + if (digit == 0 || num % digit != 0) { + return false; + } + tmp /= 10; + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_729.java b/src/main/java/com/fishercoder/solutions/firstthousand/_729.java new file mode 100644 index 0000000000..8a689fa516 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_729.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _729 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/111205/java-8-liner-treemap + */ + public static class MyCalendar { + TreeMap calendar; + + public MyCalendar() { + calendar = new TreeMap<>(); + } + + public boolean book(int start, int end) { + Integer floorKey = calendar.floorKey(start); + if (floorKey != null && calendar.get(floorKey) > start) { + return false; + } + Integer ceilingKey = calendar.ceilingKey(start); + if (ceilingKey != null && ceilingKey < end) { + return false; + } + calendar.put(start, end); + return true; + } + } + } + + public static class Solution2 { + public class MyCalendar { + List calendar; + + MyCalendar() { + calendar = new ArrayList(); + } + + public boolean book(int start, int end) { + for (int[] event : calendar) { + if (event[0] < end && start < event[1]) { + return false; + } + } + calendar.add(new int[] {start, end}); + return true; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_73.java b/src/main/java/com/fishercoder/solutions/firstthousand/_73.java similarity index 94% rename from src/main/java/com/fishercoder/solutions/_73.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_73.java index ce44200d91..bef960ce43 100644 --- a/src/main/java/com/fishercoder/solutions/_73.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_73.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _73 { public static class Solution1 { - /** + /* * Space: O(m*n) */ public void setZeroes(int[][] matrix) { @@ -36,7 +36,7 @@ public void setZeroes(int[][] matrix) { } public static class Solution2 { - /** + /* * Space: O(m+n) */ public void setZeroes(int[][] matrix) { @@ -72,7 +72,7 @@ public void setZeroes(int[][] matrix) { } public static class Solution3 { - /** + /* * Space: O(1) */ public void setZeroes(int[][] matrix) { @@ -121,7 +121,7 @@ public void setZeroes(int[][] matrix) { } public static class Solution4 { - /** + /* * Space: O(1) * credit: https://leetcode.com/problems/set-matrix-zeroes/discuss/26014/Any-shorter-O(1)-space-solution */ @@ -129,7 +129,7 @@ public void setZeroes(int[][] matrix) { int col0 = 1; int m = matrix.length; int n = matrix[0].length; - /**the first iteration (first nested for loop) is to check from top row to bottom row: + /*the first iteration (first nested for loop) is to check from top row to bottom row: * keep the first column state into variable col0; * then starting from the second column, check all the rest of the columns and mark its top cell and its most-left cell if it * s a zero.*/ @@ -146,7 +146,7 @@ public void setZeroes(int[][] matrix) { } } - /**the second iteration (second nested for loop) is to check from bottom row to the top row + /*the second iteration (second nested for loop) is to check from bottom row to the top row * from the right-most column to the second left-most column: as long as its left-most column cell or its top row cell is zero, then set that cell to be zero * at last, check col0 variable, if it's zero, mark that row cell as zero*/ for (int i = m - 1; i >= 0; i--) { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_733.java b/src/main/java/com/fishercoder/solutions/firstthousand/_733.java new file mode 100644 index 0000000000..4ba173b90a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_733.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _733 { + public static class Solution1 { + public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { + int[] directions = new int[] {0, 1, 0, -1, 0}; + int m = image.length; + int n = image[0].length; + int originalValue = image[sr][sc]; + image[sr][sc] = newColor; + + boolean[][] visited = new boolean[m][n]; + + Queue queue = new LinkedList<>(); + queue.offer(new int[] {sr, sc}); + while (!queue.isEmpty()) { + int[] curr = queue.poll(); + visited[curr[0]][curr[1]] = true; + for (int i = 0; i < directions.length - 1; i++) { + int nextR = curr[0] + directions[i]; + int nextC = curr[1] + directions[i + 1]; + if (nextR < 0 + || nextC < 0 + || nextR >= m + || nextC >= n + || image[nextR][nextC] != originalValue + || visited[nextR][nextC]) { + continue; + } + image[nextR][nextC] = newColor; + queue.offer(new int[] {nextR, nextC}); + } + } + return image; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_734.java b/src/main/java/com/fishercoder/solutions/firstthousand/_734.java new file mode 100644 index 0000000000..87cd6a93c3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_734.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +public class _734 { + public static class Solution1 { + public boolean areSentencesSimilar(String[] words1, String[] words2, String[][] pairs) { + if (words1.length != words2.length) { + return false; + } + for (int i = 0; i < words1.length; i++) { + if (!isSimilar(words1[i], words2[i], pairs)) { + return false; + } + } + return true; + } + + private boolean isSimilar(String word1, String word2, String[][] pairs) { + if (word1.equals(word2)) { + return true; + } + for (int i = 0; i < pairs.length; i++) { + String[] pair = pairs[i]; + if (pair[0].equals(word1) && pair[1].equals(word2)) { + return true; + } + if (pair[0].equals(word2) && pair[1].equals(word1)) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_735.java b/src/main/java/com/fishercoder/solutions/firstthousand/_735.java new file mode 100644 index 0000000000..b5af200a7c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_735.java @@ -0,0 +1,163 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _735 { + public static class Solution1 { + public int[] asteroidCollision(int[] asteroids) { + Deque stack = new LinkedList<>(); + for (int i = 0; i < asteroids.length; i++) { + if (!stack.isEmpty() && stack.peek() > 0 && asteroids[i] < 0) { + if (Math.abs(stack.peek()) < Math.abs(asteroids[i])) { + stack.pop(); + stack.push(asteroids[i]); + collide(stack); + } else if (Math.abs(stack.peek()) == Math.abs(asteroids[i])) { + stack.pop(); + } + } else { + stack.push(asteroids[i]); + } + } + int[] result = new int[stack.size()]; + int i = stack.size(); + while (!stack.isEmpty()) { + result[--i] = stack.pop(); + } + return result; + } + + private void collide(Deque stack) { + do { + Integer top = stack.pop(); + if (!stack.isEmpty() && stack.peek() * top < 0) { + if (stack.peek() < Math.abs(top)) { + stack.pop(); + stack.push(top); + } else if (stack.peek() == Math.abs(top)) { + stack.pop(); + break; + } else { + break; + } + } else if (stack.isEmpty() || stack.peek() * top > 0) { + stack.push(top); + break; + } + } while (!stack.isEmpty()); + } + } + + public static class Solution2 { + /* + * My completely original solution on 11/5/2021. + */ + public int[] asteroidCollision(int[] asteroids) { + Deque stack = new LinkedList<>(); + for (int a : asteroids) { + if (a > 0) { + stack.addLast(a); + } else { + if (!stack.isEmpty() && stack.peekLast() > 0) { + if (stack.peekLast() > Math.abs(a)) { + continue; + } else if (stack.peekLast() == Math.abs(a)) { + stack.pollLast(); + } else { + while (!stack.isEmpty() + && stack.peekLast() > 0 + && stack.peekLast() < Math.abs(a)) { + stack.pollLast(); + } + if (!stack.isEmpty() + && stack.peekLast() > 0 + && stack.peekLast() == Math.abs(a)) { + stack.pollLast(); + continue; + } else if (stack.isEmpty() || stack.peekLast() < 0) { + stack.addLast(a); + } + } + } else { + stack.addLast(a); + } + } + } + int[] ans = new int[stack.size()]; + for (int i = stack.size() - 1; i >= 0; i--) { + ans[i] = stack.pollLast(); + } + return ans; + } + } + + public static class Solution3 { + /* + * My completely original solution on 1/14/2022. + */ + public int[] asteroidCollision(int[] asteroids) { + Deque stack = new LinkedList<>(); + for (int i = 0; i < asteroids.length; i++) { + int a = asteroids[i]; + if (a > 0) { + stack.addLast(a); + } else { + if (!stack.isEmpty() && stack.peekLast() > 0) { + if (stack.peekLast() > Math.abs(a)) { + continue; + } else if (stack.peekLast() == Math.abs(a)) { + stack.pollLast(); + } else { + stack.pollLast(); + i--; + } + } else { + stack.addLast(a); + } + } + } + int[] ans = new int[stack.size()]; + for (int i = ans.length - 1; i >= 0; i--) { + ans[i] = stack.pollLast(); + } + return ans; + } + } + + public static class Solution4 { + /* + * My completely original solution on 7/19/2024. + */ + public int[] asteroidCollision(int[] asteroids) { + Deque stack = new LinkedList<>(); + for (int asteroid : asteroids) { + if (asteroid < 0 && !stack.isEmpty() && stack.peekLast() > 0) { + boolean bothRemoved = false; + while (!stack.isEmpty() + && stack.peekLast() > 0 + && stack.peekLast() <= -asteroid) { + if (stack.peekLast() == -asteroid) { + bothRemoved = true; + stack.pollLast(); + break; + } else if (stack.peekLast() < -asteroid) { + stack.pollLast(); + } + } + if ((stack.isEmpty() || stack.peekLast() < 0) && !bothRemoved) { + stack.addLast(asteroid); + } + } else { + stack.addLast(asteroid); + } + } + int[] result = new int[stack.size()]; + int i = 0; + while (!stack.isEmpty()) { + result[i++] = stack.pollFirst(); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_737.java b/src/main/java/com/fishercoder/solutions/firstthousand/_737.java new file mode 100644 index 0000000000..4d234ed801 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_737.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _737 { + public static class Solution1 { + public boolean areSentencesSimilarTwo(String[] words1, String[] words2, String[][] pairs) { + if (words1.length != words2.length) { + return false; + } + + Map map = new HashMap<>(); + for (String[] pair : pairs) { + if (!map.containsKey(pair[0])) { + map.put(pair[0], pair[0]); + } + if (!map.containsKey(pair[1])) { + map.put(pair[1], pair[1]); + } + + setParent(map, pair[0], pair[1]); + } + + for (int i = 0; i < words1.length; i++) { + String parent1 = getParent(map, words1[i]); + String parent2 = getParent(map, words2[i]); + if (!parent1.equals(parent2)) { + return false; + } + } + return true; + } + + private void setParent(Map map, String word1, String word2) { + String parent1 = getParent(map, word1); + String parent2 = getParent(map, word2); + map.put(parent1, parent2); + } + + private String getParent(Map map, String word) { + if (!map.containsKey(word)) { + return word; + } + while (!word.equals(map.get(word))) { + word = map.get(word); + } + return word; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_738.java b/src/main/java/com/fishercoder/solutions/firstthousand/_738.java new file mode 100644 index 0000000000..4636d5767e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_738.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.firstthousand; + +public class _738 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/112808/simple-python-solution-w-explanation/2 + */ + public int monotoneIncreasingDigits(int N) { + String s = Integer.toString(N); + int index = -1; + for (int i = s.length() - 2; i >= 0; i--) { + if (s.charAt(i) > s.charAt(i + 1) + || (index != -1 && s.charAt(index) == s.charAt(i))) { + index = i; + } + } + return index == -1 ? N : N - Integer.parseInt(s.substring(index + 1, s.length())) - 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_739.java b/src/main/java/com/fishercoder/solutions/firstthousand/_739.java new file mode 100644 index 0000000000..ba4bd47c13 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_739.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _739 { + + public static class Solution1 { + public int[] dailyTemperatures(int[] temperatures) { + if (temperatures == null || temperatures.length == 0) { + return temperatures; + } + int[] result = new int[temperatures.length]; + for (int i = 0; i < temperatures.length; i++) { + for (int j = i + 1; j < temperatures.length; j++) { + if (temperatures[j] > temperatures[i]) { + result[i] = j - i; + break; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_74.java b/src/main/java/com/fishercoder/solutions/firstthousand/_74.java similarity index 88% rename from src/main/java/com/fishercoder/solutions/_74.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_74.java index a3650ea74a..8fc9b51a0a 100644 --- a/src/main/java/com/fishercoder/solutions/_74.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_74.java @@ -1,10 +1,11 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _74 { public static class Solution1 { public boolean searchMatrix(int[][] matrix, int target) { - if (matrix == null || matrix.length == 0 + if (matrix == null + || matrix.length == 0 || matrix[0].length == 0 || matrix[0][0] > target || matrix[matrix.length - 1][matrix[0].length - 1] < target) { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_740.java b/src/main/java/com/fishercoder/solutions/firstthousand/_740.java new file mode 100644 index 0000000000..e78295a9c1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_740.java @@ -0,0 +1,95 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _740 { + public static class Solution1 { + /* + * Since the number is within range [1, 10000], we can build another array: + * each number in the array denotes the total sum of this number that appears in this array + * and + * use the numbers themselves in the indices of another array + *

+ * credit: https://leetcode.com/problems/delete-and-earn/discuss/109895/JavaC++-Clean-Code-with-Explanation + *

+ * Notes: + * 1. In essence, this is the same as House Robber: https://leetcode.com/problems/house-robber/ + * 2. We are adding the number itself into values, instead of its frequency because we will directly use this value to compute the result + */ + public int deleteAndEarn(int[] nums) { + int n = 10001; + int[] values = new int[n]; + for (int num : nums) { + values[num] += num; + } + + int take = 0; + int skip = 0; + for (int i = 0; i < n; i++) { + int takeI = skip + values[i]; + int skipI = Math.max(skip, take); + take = takeI; + skip = skipI; + } + return Math.max(take, skip); + } + } + + public static class Solution2 { + /* + * A simplified version using treemap instead of an array, credit: https://leetcode.com/problems/delete-and-earn/discuss/109895/JavaC++-Clean-Code-with-Explanation/111626 + */ + public int deleteAndEarn(int[] nums) { + TreeMap treeMap = new TreeMap<>(); + for (int num : nums) { + treeMap.put(num, treeMap.getOrDefault(num, 0) + num); + } + int prev = 0; + int curr = 0; + for (int key : treeMap.keySet()) { + if (!treeMap.containsKey(key - 1)) { + prev = curr; + curr += treeMap.get(key); + } else { + int tmp = Math.max(prev + treeMap.get(key), curr); + prev = curr; + curr = tmp; + } + } + return curr; + } + } + + public static class Solution3 { + // use DP, this is basically the same code as + // https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_3186.java + // except here it's current - 1, in the above it's current - 2 + public int deleteAndEarn(int[] nums) { + TreeMap treeMap = new TreeMap<>(); + for (int num : nums) { + treeMap.put(num, treeMap.getOrDefault(num, 0) + 1); + } + List sortedList = new ArrayList<>(treeMap.keySet()); + int[] dp = new int[sortedList.size()]; + dp[0] = sortedList.get(0) * treeMap.get(sortedList.get(0)); + for (int i = 1; i < sortedList.size(); i++) { + int current = sortedList.get(i); + int currentTotal = current * treeMap.get(current); + int j = i - 1; + // we keep going to the left of the sorted list until we find a value that's not in + // the range of current - 1 if possible + while (j >= 0 && sortedList.get(j) >= current - 1) { + j--; + } + if (j >= 0) { + dp[i] = Math.max(dp[i - 1], currentTotal + dp[j]); + } else { + dp[i] = Math.max(dp[i - 1], currentTotal); + } + } + return dp[dp.length - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_742.java b/src/main/java/com/fishercoder/solutions/firstthousand/_742.java new file mode 100644 index 0000000000..f047f83b48 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_742.java @@ -0,0 +1,77 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _742 { + public static class Solution1 { + public int findClosestLeaf(TreeNode root, int k) { + Map> graph = new HashMap<>(); + Set leaves = new HashSet<>(); + buildGraph(root, graph, null, leaves); + if (leaves.contains(k)) { + return k; + } + // Now we can do a BFS traversal + Queue queue = new LinkedList<>(); + Set directNeighbors = graph.get(k); + Set visited = + new HashSet<>(); // use a visited set to prevent cycles and not adding the + // target node itself + visited.add(k); + for (int node : directNeighbors) { + queue.offer(node); + visited.add(node); + } + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + int curr = queue.poll(); + if (leaves.contains(curr)) { + return curr; + } + Set nextNodes = graph.get(curr); + for (int next : nextNodes) { + if (!visited.contains(next)) { + queue.offer(next); + visited.add(next); + } + } + } + } + return root.val; + } + + private void buildGraph( + TreeNode root, + Map> map, + TreeNode parent, + Set leaves) { + if (root == null) { + return; + } + if (!map.containsKey(root.val)) { + map.put(root.val, new HashSet<>()); + } + if (root.left != null) { + map.get(root.val).add(root.left.val); + } + if (root.right != null) { + map.get(root.val).add(root.right.val); + } + if (parent != null) { + map.get(root.val).add(parent.val); + } + if (root.left == null && root.right == null) { + leaves.add(root.val); + } + buildGraph(root.left, map, root, leaves); + buildGraph(root.right, map, root, leaves); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_743.java b/src/main/java/com/fishercoder/solutions/firstthousand/_743.java similarity index 77% rename from src/main/java/com/fishercoder/solutions/_743.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_743.java index 46bc1f43b3..081bcfc71b 100644 --- a/src/main/java/com/fishercoder/solutions/_743.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_743.java @@ -1,20 +1,4 @@ -package com.fishercoder.solutions; - -/** - * 743. Network Delay Time - * - * There are N network nodes, labelled 1 to N. - * - * Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the target node, and w is the time it takes for a signal to travel from source to target. - * - * Now, we send a signal from a certain node K. How long will it take for all nodes to receive the signal? If it is impossible, return -1. - * - * Note: - * * N will be in the range [1, 100]. - * * K will be in the range [1, N]. - * * The length of times will be in the range [1, 6000]. - * * All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 0 <= w <= 100. - */ +package com.fishercoder.solutions.firstthousand; public class _743 { public static class Solution1 { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_744.java b/src/main/java/com/fishercoder/solutions/firstthousand/_744.java new file mode 100644 index 0000000000..488e01e492 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_744.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.firstthousand; + +public class _744 { + public static class Solution1 { + public char nextGreatestLetter(char[] letters, char target) { + if (letters[0] > target) { + return letters[0]; + } + int left = 0; + int right = letters.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (letters[mid] > target) { + while (letters[mid] > target) { + mid--; + } + return letters[++mid]; + } else { + left = mid + 1; + } + } + if (right < letters.length && letters[right] > target) { + return letters[right]; + } + return letters[0]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_746.java b/src/main/java/com/fishercoder/solutions/firstthousand/_746.java new file mode 100644 index 0000000000..4c1b45a2a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_746.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.firstthousand; + +public class _746 { + public static class Solution1 { + public int minCostClimbingStairs(int[] cost) { + int[] dp = new int[cost.length]; + dp[0] = cost[0]; + dp[1] = cost[1]; + for (int i = 2; i < cost.length; i++) { + dp[i] = cost[i] + Math.min(dp[i - 1], dp[i - 2]); + } + return Math.min(dp[cost.length - 1], dp[cost.length - 2]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_747.java b/src/main/java/com/fishercoder/solutions/firstthousand/_747.java new file mode 100644 index 0000000000..4b1812b37a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_747.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class _747 { + + public static class Solution1 { + public int dominantIndex(int[] nums) { + Map map = new HashMap<>(); + int max; + int secondMax; + for (int i = 0; i < nums.length; i++) { + map.put(nums[i], i); + } + Arrays.sort(nums); + max = nums[nums.length - 1]; + secondMax = nums[nums.length - 2]; + if (max >= 2 * secondMax) { + return map.get(max); + } else { + return -1; + } + } + } + + public static class Solution2 { + public int dominantIndex(int[] nums) { + int max = Integer.MIN_VALUE; + int maxIndex = -1; + for (int i = 0; i < nums.length; i++) { + if (nums[i] > max) { + max = nums[i]; + maxIndex = i; + } + } + for (int i = 0; i < nums.length; i++) { + if (nums[i] * 2 > max && i != maxIndex) { + return -1; + } + } + return maxIndex; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_748.java b/src/main/java/com/fishercoder/solutions/firstthousand/_748.java new file mode 100644 index 0000000000..c941de550a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_748.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +public class _748 { + + public static class Solution1 { + public String shortestCompletingWord(String licensePlate, String[] words) { + int[] counts = new int[26]; + for (char c : licensePlate.toCharArray()) { + if (Character.isAlphabetic(c)) { + counts[Character.toLowerCase(c) - 'a']++; + } + } + String result = ""; + for (String word : words) { + if (isComplete(word, counts)) { + if (result.equals("")) { + result = word; + } else if (word.length() < result.length()) { + result = word; + } + } + } + return result; + } + + private boolean isComplete(String word, int[] counts) { + int[] tmp = counts.clone(); + for (char c : word.toCharArray()) { + if (tmp[c - 'a'] > 0) { + tmp[c - 'a']--; + } + } + for (int i : tmp) { + if (i != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_749.java b/src/main/java/com/fishercoder/solutions/firstthousand/_749.java new file mode 100644 index 0000000000..f4c2bfd8b7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_749.java @@ -0,0 +1,10 @@ +package com.fishercoder.solutions.firstthousand; + +public class _749 { + public static class Solution1 { + // TODO: implement it + public int containVirus(int[][] grid) { + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_75.java b/src/main/java/com/fishercoder/solutions/firstthousand/_75.java new file mode 100644 index 0000000000..79f1d98ffe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_75.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +public class _75 { + + public static class Solution1 { + public void sortColors(int[] nums) { + int zeroIndex = 0; + int twoIndex = nums.length - 1; + for (int i = 0; i <= twoIndex; ) { + if (nums[i] == 0 && i > zeroIndex) { + swap(nums, i, zeroIndex++); + } else if (nums[i] == 2 && i < twoIndex) { + swap(nums, i, twoIndex--); + } else { + i++; + } + } + } + + void swap(int[] nums, int m, int n) { + int temp = nums[m]; + nums[m] = nums[n]; + nums[n] = temp; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_750.java b/src/main/java/com/fishercoder/solutions/firstthousand/_750.java new file mode 100644 index 0000000000..302ba8f6d7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_750.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.firstthousand; + +public class _750 { + public static class Solution1 { + public int countCornerRectangles(int[][] grid) { + if (grid == null || grid.length < 2) { + return 0; + } + int m = grid.length; + int n = grid[0].length; + int count = 0; + for (int i = 0; i < m - 1; i++) { + for (int j = 0; j < n - 1; j++) { + if (grid[i][j] == 1) { + for (int jNext = j + 1; jNext < n; jNext++) { + if (grid[i][jNext] == 1) { + for (int iNext = i + 1; iNext < m; iNext++) { + if (grid[iNext][j] == 1 && grid[iNext][jNext] == 1) { + count++; + } + } + } + } + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_751.java b/src/main/java/com/fishercoder/solutions/firstthousand/_751.java new file mode 100644 index 0000000000..b5de66f685 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_751.java @@ -0,0 +1,12 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.List; + +public class _751 { + public static class Solution1 { + public List ipToCIDR(String ip, int n) { + // TODO: implement it + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_752.java b/src/main/java/com/fishercoder/solutions/firstthousand/_752.java new file mode 100644 index 0000000000..9cab3ea634 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_752.java @@ -0,0 +1,103 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.*; + +public class _752 { + public static class Solution1 { + public int openLock(String[] deadends, String target) { + // Map the next slot digit for each current slot digit. + Map nextSlot = + new HashMap() { + { + put('0', '1'); + put('1', '2'); + put('2', '3'); + put('3', '4'); + put('4', '5'); + put('5', '6'); + put('6', '7'); + put('7', '8'); + put('8', '9'); + put('9', '0'); + } + }; + // Map the previous slot digit for each current slot digit. + Map prevSlot = + new HashMap() { + { + put('0', '9'); + put('1', '0'); + put('2', '1'); + put('3', '2'); + put('4', '3'); + put('5', '4'); + put('6', '5'); + put('7', '6'); + put('8', '7'); + put('9', '8'); + } + }; + + // Set to store visited and dead-end combinations. + Set visited = new HashSet<>(Arrays.asList(deadends)); + // Queue to store combinations generated after each turn. + Queue q = new LinkedList<>(); + + // Count the number of wheel turns made. + int turns = 0; + + // If the starting combination is also a dead-end, + // then we can't move from the starting combination. + if (visited.contains("0000")) { + return -1; + } + + // Start with the initial combination '0000'. + q.add("0000"); + visited.add("0000"); + + while (!q.isEmpty()) { + // Explore all the combinations of the current level. + int currLevelNodesCount = q.size(); + for (int i = 0; i < currLevelNodesCount; i++) { + // Get the current combination from the front of the queue. + String curr = q.poll(); + + // If the current combination matches the target, + // return the number of turns/level. + if (curr.equals(target)) { + return turns; + } + + // Explore all possible new combinations by turning each wheel in both + // directions. + for (int j = 0; j < curr.length(); j += 1) { + // Generate the new combination by turning the wheel to the next digit. + StringBuilder newCombination = new StringBuilder(curr); + newCombination.setCharAt(j, nextSlot.get(newCombination.charAt(j))); + // If the new combination is not a dead-end and was never visited, + // add it to the queue and mark it as visited. + if (!visited.contains(newCombination.toString())) { + q.add(newCombination.toString()); + visited.add(newCombination.toString()); + } + + // Generate the new combination by turning the wheel to the previous digit. + newCombination = new StringBuilder(curr); + newCombination.setCharAt(j, prevSlot.get(newCombination.charAt(j))); + // If the new combination is not a dead-end and is never visited, + // add it to the queue and mark it as visited. + if (!visited.contains(newCombination.toString())) { + q.add(newCombination.toString()); + visited.add(newCombination.toString()); + } + } + } + // We will visit next-level combinations. + turns++; + } + // We never reached the target combination. + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_754.java b/src/main/java/com/fishercoder/solutions/firstthousand/_754.java new file mode 100644 index 0000000000..03ed903377 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_754.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +public class _754 { + public static class Solution1 { + /* + * Two case: + * 1. go to the right, and reach the goal exactly. + * 2. go over the goal by several steps: + * by even number, then you can choose one of the steps that went right to go back to the left (the step is half of what you went over) + * by odd number, then you keep going until you are over by an even number. + */ + public int reachNumber(int target) { + int absTarget = Math.abs(target); + int steps = 1; + int sum = 0; + while (sum < absTarget || (sum - absTarget) % 2 == 1) { + sum += steps; + steps++; + } + return steps - 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_755.java b/src/main/java/com/fishercoder/solutions/firstthousand/_755.java new file mode 100644 index 0000000000..d25d88f3ab --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_755.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +public class _755 { + public static class Solution1 { + public int[] pourWater(int[] heights, int V, int K) { + int index; + while (V > 0) { + index = K; + for (int i = K - 1; i >= 0; i--) { + if (heights[i] > heights[index]) { + break; + } else if (heights[i] < heights[index]) { + index = i; + } + } + if (index != K) { + heights[index]++; + V--; + continue; + } + + for (int i = K + 1; i < heights.length; i++) { + if (heights[i] > heights[index]) { + break; + } else if (heights[i] < heights[index]) { + index = i; + } + } + heights[index]++; + V--; + } + return heights; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_756.java b/src/main/java/com/fishercoder/solutions/firstthousand/_756.java new file mode 100644 index 0000000000..91152e5538 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_756.java @@ -0,0 +1,62 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _756 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/116042/java-solution-map-backtracking + */ + public boolean pyramidTransition(String bottom, List allowed) { + Map> map = new HashMap<>(); + for (String s : allowed) { + String key = s.substring(0, 2); + if (!map.containsKey(key)) { + map.put(key, new ArrayList<>()); + } + map.get(key).add(s.substring(2)); + } + + return helper(bottom, map); + } + + private boolean helper(String bottom, Map> map) { + if (bottom.length() == 1) { + return true; + } + for (int i = 0; i < bottom.length() - 1; i++) { + if (!map.containsKey(bottom.substring(i, i + 2))) { + return false; + } + } + List ls = new ArrayList<>(); + getList(bottom, 0, new StringBuilder(), ls, map); + for (String s : ls) { + if (helper(s, map)) { + return true; + } + } + return false; + } + + private void getList( + String bottom, + int idx, + StringBuilder sb, + List ls, + Map> map) { + if (idx == bottom.length() - 1) { + ls.add(sb.toString()); + return; + } + for (String s : map.get(bottom.substring(idx, idx + 2))) { + sb.append(s); + getList(bottom, idx + 1, sb, ls, map); + sb.deleteCharAt(sb.length() - 1); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_757.java b/src/main/java/com/fishercoder/solutions/firstthousand/_757.java new file mode 100644 index 0000000000..697b730cbc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_757.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +/* + * Approach: Sort the intervals in the ascending order of end range. + * In case if the end range of any 2 intervals match, + * sort those intervals based on the descending order of start range + * e.g. intervals = [[1, 3], [1, 4], [2, 5], [3, 5]] + * After sorting, intervals[] becomes = [[1,3], [1,4], [3,5],[2,5]] + * The reason for sorting based on descending order of start range is to get minimum possible size of S that intersect with A of atleast size 2 + */ +public class _757 { + public static class Solution { + public int intersectionSizeTwo(int[][] intervals) { + Arrays.sort(intervals, (a, b) -> a[1] == b[1] ? b[0] - a[0] : a[1] - b[1]); + int count = 0; + int startTime = Integer.MIN_VALUE; + int endTime = Integer.MIN_VALUE; + + for (int[] interval : intervals) { + if (startTime >= interval[0]) { + continue; + } else if (endTime >= interval[0]) { + startTime = endTime; + endTime = interval[1]; + count += 1; + } else { + startTime = interval[1] - 1; + endTime = interval[1]; + count += 2; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_758.java b/src/main/java/com/fishercoder/solutions/firstthousand/_758.java new file mode 100644 index 0000000000..730d4c6138 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_758.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +public class _758 { + public static class Solution1 { + /* + * Interestingly, this problem is exactly the same as 616, using 616's code could get it AC'ed. + */ + public String boldWords(String[] words, String S) { + boolean[] shouldBold = new boolean[S.length()]; + for (int i = 0, end = 0; i < S.length(); i++) { + for (String word : words) { + if (S.startsWith(word, i)) { + end = Math.max(end, i + word.length()); + } + } + shouldBold[i] = end > i; + } + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < S.length(); i++) { + if (!shouldBold[i]) { + stringBuilder.append(S.charAt(i)); + continue; + } + int j = i; + while (j < S.length() && shouldBold[j]) { + j++; + } + stringBuilder.append("" + S.substring(i, j) + ""); + i = j - 1; + } + return stringBuilder.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_76.java b/src/main/java/com/fishercoder/solutions/firstthousand/_76.java new file mode 100644 index 0000000000..6de76ad06a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_76.java @@ -0,0 +1,85 @@ +package com.fishercoder.solutions.firstthousand; + +public class _76 { + + public static class Solution1 { + public String minWindow(String s, String t) { + int[] counts = new int[256]; + for (char c : t.toCharArray()) { + counts[c]++; + } + + int start = 0; + int end = 0; + int minStart = 0; + int minLen = Integer.MAX_VALUE; + int counter = t.length(); + while (end < s.length()) { + if (counts[s.charAt(end)] > 0) { + counter--; + } + + counts[s.charAt(end)]--; + end++; + + while (counter == 0) { + if (end - start < minLen) { + minStart = start; + minLen = end - start; + } + counts[s.charAt(start)]++; + if (counts[s.charAt(start)] > 0) { + counter++; + } + start++; + } + } + + if (minLen == Integer.MAX_VALUE) { + return ""; + } + return s.substring(minStart, minStart + minLen); + } + } + + public static class Solution2 { + /* + * I implemented below solution on my own following the hints on LeetCode. + * In comparison, Solution1 is more optimized and runs faster. + */ + public String minWindow(String s, String t) { + if (t.length() > s.length()) { + return ""; + } + int[] tCount = new int[256]; + for (int i = 0; i < t.length(); i++) { + tCount[t.charAt(i)]++; + } + int left = 0; + int right = 0; + int[] sCount = new int[256]; + String ans = ""; + while (right < s.length()) { + sCount[s.charAt(right)]++; + while (isValid(sCount, tCount)) { + if (right - left < ans.length() || ans.equals("")) { + ans = s.substring(left, right + 1); + } + sCount[s.charAt(left)]--; + left++; + } + right++; + } + return ans; + } + + private boolean isValid(int[] sCount, int[] tCount) { + for (int i = 0; i < sCount.length; i++) { + if (sCount[i] < tCount[i]) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_760.java b/src/main/java/com/fishercoder/solutions/firstthousand/_760.java new file mode 100644 index 0000000000..18e34df390 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_760.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.firstthousand; + +public class _760 { + public static class Solution1 { + public int[] anagramMappings(int[] A, int[] B) { + int[] result = new int[A.length]; + for (int i = 0; i < A.length; i++) { + for (int j = 0; j < B.length; j++) { + if (A[i] == B[j]) { + result[i] = j; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_762.java b/src/main/java/com/fishercoder/solutions/firstthousand/_762.java new file mode 100644 index 0000000000..3b1b969943 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_762.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +public class _762 { + public static class Solution1 { + public int countPrimeSetBits(int L, int R) { + int count = 0; + for (int i = L; i <= R; i++) { + if (hasPrimeNumberSetBits(i)) { + count++; + } + } + return count; + } + + private boolean hasPrimeNumberSetBits(int num) { + int k = getSetBits(num); + if (k <= 1) { + return false; + } + for (int i = 2; i * i <= k; i++) { + if (k % i == 0) { + return false; + } + } + return true; + } + + private int getSetBits(int n) { + int bits = 0; + while (n != 0) { + bits++; + n &= (n - 1); + } + return bits; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_763.java b/src/main/java/com/fishercoder/solutions/firstthousand/_763.java new file mode 100644 index 0000000000..a1f60562e1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_763.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _763 { + + public static class Solution1 { + public List partitionLabels(String s) { + List result = new ArrayList<>(); + int[] last = new int[26]; + /*This is the key step: + * we find the last occurrence of each letter and record them in last[]*/ + for (int i = 0; i < s.length(); i++) { + last[s.charAt(i) - 'a'] = i; + } + /*record the last end index of the current substring*/ + int end = 0; + int start = 0; + for (int i = 0; i < s.length(); i++) { + end = Math.max(end, last[s.charAt(i) - 'a']); + if (end == i) { + result.add(end - start + 1); + start = end + 1; + } + } + return result; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/14/2021. + * + * Again, using a pen and paper to visualize how this works, + * from the left to the right of the given string helps + * sort out the algorithm greatly and clears up any ambiguities! + */ + public List partitionLabels(String s) { + List ans = new ArrayList<>(); + Map lastIndexMap = new HashMap<>(); + for (int i = 0; i < s.length(); i++) { + lastIndexMap.put(s.charAt(i), i); + } + for (int i = 0; i < s.length(); i++) { + int boundary = i; + int start = i; + do { + int lastIndex = lastIndexMap.get(s.charAt(i)); + boundary = Math.max(lastIndex, boundary); + i++; + } while (i < boundary); + if (i > boundary) { + i--; + } + ans.add(boundary - start + 1); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_764.java b/src/main/java/com/fishercoder/solutions/firstthousand/_764.java new file mode 100644 index 0000000000..94c30b86e0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_764.java @@ -0,0 +1,139 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _764 { + public static class Solution1 { + /* + * Dp + *

+ * Time: O(N^2) + * Space: O(N^2) + * Credit: https://leetcode.com/articles/largest-plus-sign/ + */ + public int orderOfLargestPlusSign(int N, int[][] mines) { + Set banned = new HashSet<>(); + for (int[] mine : mines) { + banned.add(mine[0] * N + mine[1]); + } + + int[][] dp = new int[N][N]; + + for (int row = 0; row < N; row++) { + int count = 0; + for (int col = 0; col < N; col++) { + count = banned.contains(row * N + col) ? 0 : count + 1; + dp[row][col] = count; + } + + count = 0; + for (int col = N - 1; col >= 0; col--) { + count = banned.contains(row * N + col) ? 0 : count + 1; + dp[row][col] = Math.min(dp[row][col], count); + } + } + + int result = 0; + for (int col = 0; col < N; col++) { + int count = 0; + for (int row = 0; row < N; row++) { + count = banned.contains(row * N + col) ? 0 : count + 1; + dp[row][col] = Math.min(dp[row][col], count); + } + + count = 0; + for (int row = N - 1; row >= 0; row--) { + count = banned.contains(row * N + col) ? 0 : count + 1; + dp[row][col] = Math.min(dp[row][col], count); + result = Math.max(result, dp[row][col]); + } + } + return result; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/largest-plus-sign/discuss/113314/JavaC%2B%2BPython-O(N2)-solution-using-only-one-grid-matrix + */ + public int orderOfLargestPlusSign(int n, int[][] mines) { + int[][] grid = new int[n][n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + grid[i][j] = n; + } + } + for (int i = 0; i < mines.length; i++) { + grid[mines[i][0]][mines[i][1]] = 0; + } + for (int i = 0; i < n; i++) { + for (int j = 0, k = n - 1, l = 0, r = 0, u = 0, d = 0; j < n; j++, k--) { + grid[i][j] = + Math.min( + grid[i][j], + l = (grid[i][j] == 0 ? 0 : l + 1)); // left direction + grid[i][k] = + Math.min( + grid[i][k], + r = (grid[i][k] == 0 ? 0 : r + 1)); // right direction + grid[j][i] = Math.min(grid[j][i], u = (grid[j][i] == 0 ? 0 : u + 1)); // upwards + grid[k][i] = + Math.min(grid[k][i], d = (grid[k][i] == 0 ? 0 : d + 1)); // downwards + } + } + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + result = Math.max(result, grid[i][j]); + } + } + return result; + } + + /* + * break the above into FOUR separate loops to go over four directions for easier understanding + */ + public int orderOfLargestPlusSign_initialVersion(int n, int[][] mines) { + int[][] grid = new int[n][n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + grid[i][j] = n; + } + } + for (int i = 0; i < mines.length; i++) { + grid[mines[i][0]][mines[i][1]] = 0; + } + for (int i = 0; i < n; i++) { + for (int j = 0, l = 0; j < n; j++) { + grid[i][j] = Math.min(grid[i][j], l = (grid[i][j] == 0 ? 0 : l + 1)); + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0, k = n - 1, r = 0; j < n; j++, k--) { + grid[i][k] = Math.min(grid[i][k], r = (grid[i][k] == 0 ? 0 : r + 1)); + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0, k = n - 1, u = 0; j < n; j++, k--) { + grid[j][i] = Math.min(grid[j][i], u = (grid[j][i] == 0 ? 0 : u + 1)); + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0, k = n - 1, d = 0; j < n; j++, k--) { + grid[k][i] = Math.min(grid[k][i], d = (grid[k][i] == 0 ? 0 : d + 1)); + } + } + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + result = Math.max(result, grid[i][j]); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_765.java b/src/main/java/com/fishercoder/solutions/firstthousand/_765.java new file mode 100644 index 0000000000..7f043013d9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_765.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +public class _765 { + public static class Solution1 { + public int minSwapsCouples(int[] row) { + int swaps = 0; + for (int i = 0; i < row.length - 1; i += 2) { + int coupleValue = row[i] % 2 == 0 ? row[i] + 1 : row[i] - 1; + if (row[i + 1] != coupleValue) { + swaps++; + int coupleIndex = findIndex(row, coupleValue); + swap(row, coupleIndex, i + 1); + } + } + return swaps; + } + + private void swap(int[] row, int i, int j) { + int tmp = row[i]; + row[i] = row[j]; + row[j] = tmp; + } + + private int findIndex(int[] row, int value) { + for (int i = 0; i < row.length; i++) { + if (row[i] == value) { + return i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_766.java b/src/main/java/com/fishercoder/solutions/firstthousand/_766.java new file mode 100644 index 0000000000..241d1f7bad --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_766.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.firstthousand; + +public class _766 { + public static class Solution1 { + public boolean isToeplitzMatrix(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + if (matrix[i][j] != matrix[i - 1][j - 1]) { + return false; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_767.java b/src/main/java/com/fishercoder/solutions/firstthousand/_767.java new file mode 100644 index 0000000000..121c4c27c0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_767.java @@ -0,0 +1,107 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; + +public class _767 { + public static class Solution1 { + public String reorganizeString(String S) { + Map map = new HashMap<>(); + for (char c : S.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + int len = S.length(); + for (char c : map.keySet()) { + if ((len % 2 == 0 && map.get(c) > len / 2) + || (len % 2 != 0 && map.get(c) >= len / 2 + 2)) { + return ""; + } + } + PriorityQueue queue = new PriorityQueue<>((a, b) -> b.count - a.count); + for (char c : map.keySet()) { + queue.offer(new CustChar(c, map.get(c))); + } + + StringBuilder sb = new StringBuilder(); + while (!queue.isEmpty()) { + CustChar curr = queue.poll(); + char c = curr.c; + if (sb.length() > 0 && sb.charAt(sb.length() - 1) != c) { + sb.append(c); + if (curr.count > 1) { + queue.offer(new CustChar(c, curr.count - 1)); + } + } else if (sb.length() == 0) { + sb.append(c); + if (curr.count > 1) { + queue.offer(new CustChar(c, curr.count - 1)); + } + } else if (sb.length() > 0 && sb.charAt(sb.length() - 1) == c && !queue.isEmpty()) { + CustChar next = queue.poll(); + sb.append(next.c); + if (next.count > 1) { + queue.offer(new CustChar(next.c, next.count - 1)); + } + queue.offer(curr); + } + } + return sb.toString(); + } + + class CustChar { + Character c; + int count; + + public CustChar(Character c, int count) { + this.c = c; + this.count = count; + } + } + } + + public static class Solution2 { + /* + * My completely original solution on 12/24/2021. + */ + public String reorganizeString(String s) { + Map map = new HashMap<>(); + for (char c : s.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b.count - a.count); + for (char c : map.keySet()) { + maxHeap.add(new Tuple(c, map.get(c))); + } + StringBuilder sb = new StringBuilder("1"); + while (!maxHeap.isEmpty()) { + PriorityQueue tmp = new PriorityQueue<>((a, b) -> b.count - a.count); + Tuple curr = maxHeap.poll(); + while (sb.length() != 0 + && sb.charAt(sb.length() - 1) == curr.c + && !maxHeap.isEmpty()) { + tmp.offer(curr); + curr = maxHeap.poll(); + } + if (curr.c != sb.charAt(sb.length() - 1)) { + sb.append(curr.c); + } + maxHeap.addAll(tmp); + if (curr.count > 1) { + maxHeap.offer(new Tuple(curr.c, curr.count - 1)); + } + } + return sb.substring(1).length() != s.length() ? "" : sb.substring(1); + } + + class Tuple { + char c; + int count; + + public Tuple(char c, int count) { + this.c = c; + this.count = count; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_769.java b/src/main/java/com/fishercoder/solutions/firstthousand/_769.java new file mode 100644 index 0000000000..853e6f4510 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_769.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +public class _769 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/max-chunks-to-make-sorted/discuss/113520/Java-solution-left-max-and-right-min. + */ + public int maxChunksToSorted(int[] arr) { + int len = arr.length; + + int[] maxOfLeft = new int[len]; + maxOfLeft[0] = arr[0]; + for (int i = 1; i < len; i++) { + maxOfLeft[i] = Math.max(arr[i], maxOfLeft[i - 1]); + } + + int[] minOfRight = new int[len]; + minOfRight[len - 1] = arr[len - 1]; + for (int i = len - 2; i >= 0; i--) { + minOfRight[i] = Math.min(minOfRight[i + 1], arr[i]); + } + + int result = 0; + for (int i = 0; i < len - 1; i++) { + if (maxOfLeft[i] <= minOfRight[i + 1]) { + result++; + } + } + return result + 1; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/articles/max-chunks-to-make-sorted-i/ + */ + public int maxChunksToSorted(int[] arr) { + int ans = 0; + int max = 0; + for (int i = 0; i < arr.length; ++i) { + max = Math.max(max, arr[i]); + if (max == i) { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_77.java b/src/main/java/com/fishercoder/solutions/firstthousand/_77.java new file mode 100644 index 0000000000..24ee370c9c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_77.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _77 { + + public static class Solution1 { + /* + * I'm glad that I worked this one out completely on my own on 10/11/2021! Enjoy the beauty of backtracking! + */ + public List> combine(int n, int k) { + List> ans = new ArrayList<>(); + for (int num = 1; num <= n - k + 1; num++) { + List list = new ArrayList<>(); + list.add(num); + backtracking(list, k - 1, num + 1, n, ans); + } + return ans; + } + + private void backtracking( + List list, int k, int start, int limit, List> ans) { + if (k == 0) { + ans.add(new ArrayList<>(list)); + return; + } + for (int num = start; num <= limit; num++) { + list.add(num); + backtracking(list, k - 1, num + 1, limit, ans); + list.remove(list.size() - 1); + } + } + } + + public static class Solution2 { + /* + * My completely own solution on 1/24/2022. + */ + public List> combine(int n, int k) { + List> ans = new ArrayList<>(); + int[] nums = new int[n]; + for (int i = 1; i <= n; i++) { + nums[i - 1] = i; + } + backtrack(ans, nums, k, new ArrayList<>(), 0); + return ans; + } + + private void backtrack( + List> ans, int[] nums, int k, List curr, int start) { + if (curr.size() == k) { + ans.add(new ArrayList<>(curr)); + } else if (curr.size() < k) { + for (int i = start; i < nums.length; i++) { + curr.add(nums[i]); + backtrack(ans, nums, k, curr, i + 1); + curr.remove(curr.size() - 1); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_771.java b/src/main/java/com/fishercoder/solutions/firstthousand/_771.java new file mode 100644 index 0000000000..ad11fdbdba --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_771.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _771 { + public static class Solution1 { + public int numJewelsInStones(String J, String S) { + Set set = new HashSet<>(); + for (char c : J.toCharArray()) { + set.add(c); + } + int count = 0; + for (char c : S.toCharArray()) { + if (set.contains(c)) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_773.java b/src/main/java/com/fishercoder/solutions/firstthousand/_773.java new file mode 100644 index 0000000000..cb2489701f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_773.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; + +public class _773 { + public static class Solution1 { + public int slidingPuzzle(int[][] board) { + String target = "123450"; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 3; j++) { + sb.append(board[i][j]); + } + } + Queue q = new LinkedList<>(); + String start = sb.toString(); + q.offer(start); + Set visited = new HashSet<>(); + visited.add(start); + // since there are only 6 cells, we just use 0 through 5 to represent the positions: + // 0, 1, 2 + // 3, 4, 5 + // the swap positions, go from left to right, top to bottom + // swap[index] means the possible positions to swap when '0' is at position index + int[][] swap = + new int[][] { + {1, 3}, + {0, 4, 2}, + {1, 5}, + {0, 4}, + {3, 1, 5}, + {2, 4} + }; + int level = 0; + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + String curr = q.poll(); + if (curr.equals(target)) { + return level; + } + int index = curr.indexOf('0'); + for (int swapIndex : swap[index]) { + sb.setLength(0); + sb.append(curr); + + // swap + sb.setCharAt(index, curr.charAt(swapIndex)); + sb.setCharAt(swapIndex, '0'); + + String path = sb.toString(); + if (!visited.add(path)) { + continue; + } + q.offer(path); + } + } + level++; + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_775.java b/src/main/java/com/fishercoder/solutions/firstthousand/_775.java new file mode 100644 index 0000000000..818d21059f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_775.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.firstthousand; + +public class _775 { + /* + * credit: https://leetcode.com/problems/global-and-local-inversions/solution/ + */ + public static class Solution1 { + /* + * 1. a local inversion is also a global inversion; + * 2. we only need to check if a this input has any non-local inversion, i.e. global inversions that are not local inversions + * because local inversion is a subset of global inversions. + *

+ * This one will result in TLE with a time complexity of O(n^2). + */ + public boolean isIdealPermutation(int[] A) { + for (int i = 0; i < A.length; i++) { + for (int j = i + 2; j < A.length; j++) { + if (A[i] > A[j]) { + return false; + } + } + } + return true; + } + } + + public static class Solution2 { + /* + * from the above solution, we can tell that if we can find the minimum of A[j] where j >= i + 2, then we could quickly return false, so two steps: + * 1. remembering minimum + * 2. scanning from right to left + *

+ * Time: O(n) + */ + public boolean isIdealPermutation(int[] A) { + int min = A.length; + for (int i = A.length - 1; i >= 2; i--) { + min = Math.min(min, A[i]); + if (A[i - 2] > min) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_776.java b/src/main/java/com/fishercoder/solutions/firstthousand/_776.java new file mode 100644 index 0000000000..0edd106869 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_776.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _776 { + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/119481/recursive-java-solution + */ + public TreeNode[] splitBST(TreeNode root, int V) { + TreeNode small = new TreeNode(0); + TreeNode big = new TreeNode(0); + split(root, V, small, big); + return new TreeNode[] {small.right, big.left}; + } + + private void split(TreeNode root, int v, TreeNode small, TreeNode big) { + if (root == null) { + return; + } + if (root.val <= v) { + small.right = root; + TreeNode right = root.right; + root.right = null; + split(right, v, root, big); + } else { + big.left = root; + TreeNode left = root.left; + root.left = null; + split(left, v, small, root); + } + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/articles/split-bst/ + */ + public TreeNode[] splitBST(TreeNode root, int V) { + if (root == null) { + return new TreeNode[] {null, null}; + } else if (root.val <= V) { + TreeNode[] result = splitBST(root.right, V); + root.right = result[0]; + result[0] = root; + return result; + } else { + TreeNode[] result = splitBST(root.left, V); + root.left = result[1]; + result[1] = root; + return result; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_777.java b/src/main/java/com/fishercoder/solutions/firstthousand/_777.java new file mode 100644 index 0000000000..70cde023ab --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_777.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +public class _777 { + public static class Solution1 { + public boolean canTransform(String start, String end) { + StringBuilder sb = new StringBuilder(); + for (char c : start.toCharArray()) { + if (c != 'X') { + sb.append(c); + } + } + String cleanedStart = sb.toString(); + sb.setLength(0); + for (char c : end.toCharArray()) { + if (c != 'X') { + sb.append(c); + } + } + String cleandEnd = sb.toString(); + if (!cleanedStart.equals(cleandEnd)) { + return false; + } + + // check R from left, check on the start string for R first + // whenever count becomes negative, this means we encounter an R in a more left position + // in end string than start string + // since R could only be moved to the right in the start string, there's no way that + // start string could be shifted to match end string + // test11 illustrates this well + int count = 0; + for (int i = 0; i < start.length(); i++) { + if (start.charAt(i) == 'R') { + count++; + } + if (end.charAt(i) == 'R') { + count--; + } + if (count < 0) { + return false; + } + } + // check L from left, but check on the end string first, + // this means if L is in a more left index in start string than end string, it's + // impossible for start to match end + // test12 illustrates this case well + count = 0; + for (int i = 0; i < end.length(); i++) { + if (end.charAt(i) == 'L') { + count++; + } + if (start.charAt(i) == 'L') { + count--; + } + if (count < 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_779.java b/src/main/java/com/fishercoder/solutions/firstthousand/_779.java new file mode 100644 index 0000000000..40a07fba6a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_779.java @@ -0,0 +1,44 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _779 { + public static class Solution1 { + /* + * Time: O(2^n) + * Space: O(2^n) + * This will result int TLE. + */ + public int kthGrammar(int N, int K) { + List> lists = new ArrayList<>(); + lists.add(Arrays.asList(0)); + for (int i = 1; i <= N; i++) { + List curr = new ArrayList<>(); + List prev = lists.get(i - 1); + for (int j = 0; j < prev.size(); j++) { + if (prev.get(j) == 0) { + curr.add(0); + curr.add(1); + } else { + curr.add(1); + curr.add(0); + } + } + lists.add(curr); + } + return lists.get(N).get(K - 1); + } + } + + public static class Solution2 { + /* + * Time: O(logn) + * Space: O(1) + */ + public int kthGrammar(int N, int K) { + return Integer.bitCount(K - 1) % 2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_78.java b/src/main/java/com/fishercoder/solutions/firstthousand/_78.java similarity index 87% rename from src/main/java/com/fishercoder/solutions/_78.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_78.java index d6c28946c7..4c390af42a 100644 --- a/src/main/java/com/fishercoder/solutions/_78.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_78.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.List; @@ -14,7 +14,8 @@ public List> subsets(int[] nums) { result.add(new ArrayList()); for (int i = 0; i < nums.length; i++) { List> temp = new ArrayList(); - //you'll have to create a new one here, otherwise, it'll throw ConcurrentModificationException. + // you'll have to create a new one here, otherwise, it'll throw + // ConcurrentModificationException. for (List list : result) { List newList = new ArrayList(list); newList.add(nums[i]); @@ -27,7 +28,7 @@ public List> subsets(int[] nums) { } public static class Solution2 { - /** + /* * This is the most straightforward solution and easy to follow. */ public List> subsets(int[] nums) { @@ -47,7 +48,7 @@ void backtracking(List> result, List list, int[] nums, in } public static class Solution3 { - /** + /* * This is just a slight modification of Solution2, pay close to attention to notice the difference between them. */ public List> subsets(int[] nums) { @@ -58,7 +59,8 @@ public List> subsets(int[] nums) { return result; } - private void backtracking(List> result, List list, int[] nums, int start) { + private void backtracking( + List> result, List list, int[] nums, int start) { for (int i = start; i < nums.length; i++) { list.add(nums[i]); result.add(new ArrayList<>(list)); diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_781.java b/src/main/java/com/fishercoder/solutions/firstthousand/_781.java new file mode 100644 index 0000000000..4d5b29188c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_781.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _781 { + public static class Solution1 { + public int numRabbits(int[] answers) { + Map map = new HashMap<>(); + int rabbits = 0; + for (int rabbitType : answers) { + if (map.containsKey(rabbitType)) { + int count = map.get(rabbitType); + count--; + if (count == 0) { + map.remove(rabbitType); + } else { + map.put(rabbitType, count); + } + } else { + rabbits += rabbitType; + rabbits++; + if (rabbitType != 0) { + map.put(rabbitType, rabbitType); + } + } + } + return rabbits; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_783.java b/src/main/java/com/fishercoder/solutions/firstthousand/_783.java new file mode 100644 index 0000000000..c22e0f7e0c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_783.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _783 { + public static class Solution1 { + public int minDiffInBST(TreeNode root) { + List inorder = new ArrayList<>(); + inorder(root, inorder); + return findMinDiff(inorder); + } + + private int findMinDiff(List inorder) { + int minDiff = Integer.MAX_VALUE; + for (int i = 1; i < inorder.size(); i++) { + minDiff = Math.min(minDiff, inorder.get(i) - inorder.get(i - 1)); + } + return minDiff; + } + + private void inorder(TreeNode root, List inorder) { + if (root == null) { + return; + } + inorder(root.left, inorder); + inorder.add(root.val); + inorder(root.right, inorder); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_784.java b/src/main/java/com/fishercoder/solutions/firstthousand/_784.java new file mode 100644 index 0000000000..06a3b1e643 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_784.java @@ -0,0 +1,80 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _784 { + public static class Solution1 { + public List letterCasePermutation(String S) { + Set result = new HashSet<>(); + result.add(S); + for (int i = 0; i < S.length(); i++) { + if (Character.isAlphabetic(S.charAt(i))) { + Set newResult = new HashSet<>(); + for (String word : result) { + if (Character.isUpperCase(word.charAt(i))) { + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < i; j++) { + sb.append(word.charAt(j)); + } + sb.append(Character.toLowerCase(word.charAt(i))); + for (int j = i + 1; j < word.length(); j++) { + sb.append(word.charAt(j)); + } + newResult.add(sb.toString()); + } else { + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < i; j++) { + sb.append(word.charAt(j)); + } + sb.append(Character.toUpperCase(word.charAt(i))); + for (int j = i + 1; j < word.length(); j++) { + sb.append(word.charAt(j)); + } + newResult.add(sb.toString()); + } + } + result.addAll(newResult); + } + } + return new ArrayList<>(result); + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/11/2021. + */ + public List letterCasePermutation(String s) { + List ans = new ArrayList<>(); + ans.add(""); + return recursion(s, ans, 0); + } + + private List recursion(String s, List ans, int start) { + if (start >= s.length()) { + return ans; + } + List newList = new ArrayList<>(); + int index = start; + while (start < s.length() && Character.isDigit(s.charAt(start))) { + start++; + } + String digits = ""; + if (start > index) { + digits = s.substring(index, start); + } + for (String str : ans) { + if (start < s.length()) { + newList.add(str + digits + Character.toUpperCase(s.charAt(start))); + newList.add(str + digits + Character.toLowerCase(s.charAt(start))); + } else { + newList.add(str + digits); + } + } + return recursion(s, newList, start + 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_785.java b/src/main/java/com/fishercoder/solutions/firstthousand/_785.java new file mode 100644 index 0000000000..14c54f4210 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_785.java @@ -0,0 +1,79 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Stack; + +public class _785 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/is-graph-bipartite/discuss/115503/java-BFS + */ + public boolean isBipartite(int[][] graph) { + int[] visited = new int[graph.length]; + // BFS + // 0 means never encountered before, 1 means we put this node into set A, 2 means we put + // this node into set B + for (int i = 0; i < graph.length; i++) { + if (graph[i].length != 0 && visited[i] == 0) { + visited[i] = 1; + Queue queue = new LinkedList<>(); + queue.offer(i); + while (!queue.isEmpty()) { + int current = queue.poll(); + for (int node : graph[current]) { + if (visited[node] == 0) { + // if the current node is in set A (1), then we put its neighbor in + // set B (2), otherwise set A (1) + visited[node] = (visited[current] == 1) ? 2 : 1; + queue.offer(node); + } else { + if (visited[node] == visited[current]) { + return false; + } + } + } + } + } + } + return true; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/is-graph-bipartite/solution/ + *

+ * Let red indicate set A and blue indicate set B, if the graph is a bipartite, + * we should be able to greedily color this graph: for each node, if we color it red, then color all of its neighbors blue, etc. + */ + public boolean isBipartite(int[][] graph) { + // 0 means uncolored, 1 means red and 2 means blue + int[] colors = new int[graph.length]; + for (int start = 0; start < graph.length; start++) { + if (colors[start] == 0) { + Stack stack = new Stack<>(); + stack.push(start); + colors[start] = 1; // color it to be red + + while (!stack.isEmpty()) { + Integer curr = stack.pop(); + for (int neighbor : graph[curr]) { + if (colors[neighbor] == 0) { + stack.push(neighbor); + // if the current node is red (1), then we color it to be blue (2), + // otherwise red (1) + colors[neighbor] = (colors[curr] == 1) ? 2 : 1; + } else if (colors[neighbor] == colors[curr]) { + // this means the two connected nodes have the same color, so this + // is not a bipartite + return false; + } + } + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_788.java b/src/main/java/com/fishercoder/solutions/firstthousand/_788.java new file mode 100644 index 0000000000..45d4606226 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_788.java @@ -0,0 +1,86 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _788 { + public static class Solution1 { + /* + * My very original, but non-DP solution. + */ + public int rotatedDigits(int n) { + int count = 0; + Map map = new HashMap<>(); + map.put('0', "0"); + map.put('1', "1"); + map.put('8', "8"); + map.put('2', "5"); + map.put('5', "2"); + map.put('6', "9"); + map.put('9', "6"); + for (int i = 1; i <= n; i++) { + if (isRotatedNumber(i, map)) { + count++; + } + } + return count; + } + + private boolean isRotatedNumber(int num, Map map) { + String originalNum = String.valueOf(num); + StringBuilder sb = new StringBuilder(); + for (char c : String.valueOf(num).toCharArray()) { + if (!map.containsKey(c)) { + return false; + } else { + sb.append(map.get(c)); + } + } + return !originalNum.equals(sb.toString()); + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/rotated-digits/discuss/117975/Java-dp-solution-9ms + * dp[i] = 0 means invalid; + * dp[i] = 1 means valid but the same; + * dp[i] = 2 means valid and different. + */ + public int rotatedDigits(int n) { + int[] dp = new int[n + 1]; + int count = 0; + for (int num = 0; num <= n; num++) { + if (num < 10) { + if (num == 0 || num == 1 || num == 8) { + dp[num] = 1; + } else if (num == 2 || num == 5 || num == 6 || num == 9) { + count++; + dp[num] = 2; + } + } else { + /*Here's the key/beauty of this DP solution: + * we could keep checking each number by reusing the previous number we worked on, + * basically, always break a bigger number into two parts: a number that's its right most digit and everything else, e.g. + * num = 12 -> 1 and 2, so we check dp[1] and dp[2] to know if 12 could be rotated to a valid number, + * num = 123 -> 12 and 3, so we check dp[12] and dp[3] to know if 123 could be rotated to a valid number. + * and so on. + * */ + int a = dp[num / 10]; + int b = dp[num % 10]; + if (a == 1 && b == 1) { + // we first check if both are valid and the same, if that's the case, then + // we mark it as 1 + dp[num] = 1; + } else if (a >= 1 && b >= 1) { + // then only in this case, either a or b is greater than 1, it's a valid and + // different number + dp[num] = 2; + count++; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_789.java b/src/main/java/com/fishercoder/solutions/firstthousand/_789.java new file mode 100644 index 0000000000..5a0512ef02 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_789.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +public class _789 { + + public static class Solution { + public boolean escapeGhosts(int[][] ghosts, int[] target) { + int[] currPos = {0, 0}; + int selfDist = getDist(currPos, target); + + for (int[] ghost : ghosts) { + int ghostDist = getDist(ghost, target); + if (ghostDist <= selfDist) { + return false; + } + } + return true; + } + + private int getDist(int[] p1, int[] p2) { + return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_79.java b/src/main/java/com/fishercoder/solutions/firstthousand/_79.java new file mode 100644 index 0000000000..76d0217f75 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_79.java @@ -0,0 +1,158 @@ +package com.fishercoder.solutions.firstthousand; + +public class _79 { + + public static class Solution1 { + // credit: https://discuss.leetcode.com/topic/21142/my-java-solution + + boolean[][] visited; + + public boolean exist(char[][] board, String word) { + int m = board.length; + int n = board[0].length; + visited = new boolean[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (word.charAt(0) == board[i][j] && search(board, word, i, j, 0)) { + return true; + } + } + } + return false; + } + + boolean search(char[][] board, String word, int i, int j, int pos) { + if (pos == word.length()) { + return true; + } + if (i < 0 + || j < 0 + || i >= board.length + || j >= board[0].length + || word.charAt(pos) != board[i][j] + || visited[i][j]) { + return false; + } + visited[i][j] = true; + if (search(board, word, i + 1, j, pos + 1) + || search(board, word, i - 1, j, pos + 1) + || search(board, word, i, j + 1, pos + 1) + || search(board, word, i, j - 1, pos + 1)) { + return true; + } + + visited[i][j] = false; + return false; + } + } + + // O(1) space solution + public static class Solution2 { + public boolean exist(char[][] board, String word) { + // do DFS traversal + int row = board.length; + int col = board[0].length; + + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + if (board[i][j] == word.charAt(0) && search(board, i, j, word, 0) == true) { + return true; + } + } + } + return false; + } + + private boolean search(char[][] board, int i, int j, String word, int index) { + if (index == word.length() - 1) { + return true; + } + + // store the visited char in a temp variable + char temp = board[i][j]; + board[i][j] = ' '; + if (i > 0 + && board[i - 1][j] == word.charAt(index + 1) + && search(board, i - 1, j, word, index + 1) == true) { + return true; + } + if (i < board.length - 1 + && board[i + 1][j] == word.charAt(index + 1) + && search(board, i + 1, j, word, index + 1) == true) { + return true; + } + + if (j > 0 + && board[i][j - 1] == word.charAt(index + 1) + && search(board, i, j - 1, word, index + 1) == true) { + return true; + } + + if (j < board[0].length - 1 + && board[i][j + 1] == word.charAt(index + 1) + && search(board, i, j + 1, word, index + 1) == true) { + return true; + } + + board[i][j] = temp; + return false; + } + } + + public static class Solution3 { + /* + * I came up with below solution completely independently on 10/7/2021, although space complexity is O(m*n) instead of constant. + */ + public boolean exist(char[][] board, String word) { + int m = board.length; + int n = board[0].length; + boolean[][] visited = new boolean[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == word.charAt(0)) { + visited[i][j] = true; + if (existByDfs(board, i, j, word.substring(1), visited, m, n)) { + return true; + } + // backtracking + visited[i][j] = false; + } + } + } + return false; + } + + int[] directions = new int[] {0, 1, 0, -1, 0}; + + private boolean existByDfs( + char[][] board, + int startI, + int startJ, + String word, + boolean[][] visited, + int m, + int n) { + if (word.equals("")) { + return true; + } + for (int i = 0; i < directions.length - 1; i++) { + int nextX = startI + directions[i]; + int nextY = startJ + directions[i + 1]; + if (nextX >= 0 + && nextX < m + && nextY >= 0 + && nextY < n + && !visited[nextX][nextY] + && board[nextX][nextY] == word.charAt(0)) { + visited[nextX][nextY] = true; + if (existByDfs(board, nextX, nextY, word.substring(1), visited, m, n)) { + return true; + } + // backtracking + visited[nextX][nextY] = false; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_791.java b/src/main/java/com/fishercoder/solutions/firstthousand/_791.java new file mode 100644 index 0000000000..5755ee185b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_791.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _791 { + public static class Solution1 { + public String customSortString(String order, String s) { + Map map = new HashMap<>(); + for (char c : s.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + StringBuilder sb = new StringBuilder(); + for (char c : order.toCharArray()) { + if (map.containsKey(c)) { + int count = map.get(c); + while (count-- > 0) { + sb.append(c); + } + map.remove(c); + } + } + for (char c : map.keySet()) { + int count = map.get(c); + while (count-- > 0) { + sb.append(c); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_792.java b/src/main/java/com/fishercoder/solutions/firstthousand/_792.java new file mode 100644 index 0000000000..7cbef6d3f0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_792.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _792 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/number-of-matching-subsequences/discuss/1290406/C%2B%2BJavaPython-Next-Letter-Pointers-Picture-explain-O(N-%2B-S) + */ + public int numMatchingSubseq(String s, String[] words) { + List[] buckets = new ArrayList[26]; + for (int i = 0; i < buckets.length; i++) { + buckets[i] = new ArrayList<>(); + } + for (String word : words) { + char start = word.charAt(0); + buckets[start - 'a'].add(new Node(word, 0)); + } + int result = 0; + for (char c : s.toCharArray()) { + List currBucket = buckets[c - 'a']; + buckets[c - 'a'] = new ArrayList<>(); + for (Node node : currBucket) { + node.index++; + if (node.index == node.word.length()) { + result++; + } else { + char start = node.word.charAt(node.index); + buckets[start - 'a'].add(node); + } + } + } + return result; + } + + private class Node { + String word; + int index; + + public Node(String word, int index) { + this.word = word; + this.index = index; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_796.java b/src/main/java/com/fishercoder/solutions/firstthousand/_796.java new file mode 100644 index 0000000000..b1a6015dcd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_796.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.firstthousand; + +public class _796 { + public static class Solution1 { + public boolean rotateString(String A, String B) { + if (A.length() != B.length()) { + return false; + } + for (int i = 0; i < A.length(); i++) { + if ((A.substring(i) + A.substring(0, i)).equals(B)) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_799.java b/src/main/java/com/fishercoder/solutions/firstthousand/_799.java new file mode 100644 index 0000000000..3ba703fec1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_799.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.firstthousand; + +public class _799 { + public static class Solution1 { + public double champagneTower(int poured, int queryRow, int queryGlass) { + double[][] dp = new double[101][101]; + dp[0][0] = poured; + for (int row = 0; row <= queryRow; row++) { + for (int col = 0; col <= row; col++) { + double quantity = (dp[row][col] - 1.0) / 2.0; + if (quantity > 0) { + dp[row + 1][col] += quantity; + dp[row + 1][col + 1] += quantity; + } + } + } + return Math.min(dp[queryRow][queryGlass], 1.0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_8.java b/src/main/java/com/fishercoder/solutions/firstthousand/_8.java new file mode 100644 index 0000000000..e3994726b8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_8.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +public class _8 { + + public static class Solution1 { + /* + * four corner cases: + * 1. discards all leading zeroes + * 2. sign of the number + * 3. overflow + * 4. invalid input + */ + public int myAtoi(String s) { + int pointer = 0; + int result = 0; + while (pointer < s.length() && Character.isWhitespace(s.charAt(pointer))) { + pointer++; + } + if (pointer == s.length()) { + return 0; + } + boolean negativeFlag = (s.charAt(pointer) == '-'); + if (s.charAt(pointer) == '+' || s.charAt(pointer) == '-') { + pointer++; + } + for (; pointer < s.length(); pointer++) { + if (s.charAt(pointer) > '9' || s.charAt(pointer) < '0') { + break; + } else { + int digit = s.charAt(pointer) - '0'; + if (!negativeFlag && result > (Integer.MAX_VALUE - digit) / 10) { + return Integer.MAX_VALUE; + } else if (negativeFlag && result < (Integer.MIN_VALUE + digit) / 10) { + return Integer.MIN_VALUE; + } + result = result * 10 + (negativeFlag ? -digit : digit); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_80.java b/src/main/java/com/fishercoder/solutions/firstthousand/_80.java similarity index 77% rename from src/main/java/com/fishercoder/solutions/_80.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_80.java index 63ae7c6cdb..44e96c4d70 100644 --- a/src/main/java/com/fishercoder/solutions/_80.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_80.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.List; @@ -9,16 +9,9 @@ public static class Solution1 { public int removeDuplicates(int[] nums) { int counter = 0; int len = nums.length; - if (len == 0) { - return 0; + if (len < 3) { + return len; } - if (len == 1) { - return 1; - } - if (len == 2) { - return 2; - } - List a = new ArrayList(); a.add(nums[0]); a.add(nums[1]); @@ -37,5 +30,4 @@ public int removeDuplicates(int[] nums) { return counter; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_800.java b/src/main/java/com/fishercoder/solutions/firstthousand/_800.java new file mode 100644 index 0000000000..7160359877 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_800.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _800 { + public static class Solution1 { + public String similarRGB(String color) { + List allShortHandCombinations = computeAllShorthandCombinations(); + int minSimilarity = Integer.MIN_VALUE; + String result = ""; + for (String candidate : allShortHandCombinations) { + int similarity = computeSimilarity(candidate, color); + if (similarity > minSimilarity) { + result = candidate; + minSimilarity = similarity; + } + } + return result; + } + + private int computeSimilarity(String candidate, String color) { + return -(Integer.parseInt(candidate.substring(1, 3), 16) + - Integer.parseInt(color.substring(1, 3), 16)) + * (Integer.parseInt(candidate.substring(1, 3), 16) + - Integer.parseInt(color.substring(1, 3), 16)) + - (Integer.parseInt(candidate.substring(3, 5), 16) + - Integer.parseInt(color.substring(3, 5), 16)) + * (Integer.parseInt(candidate.substring(3, 5), 16) + - Integer.parseInt(color.substring(3, 5), 16)) + - (Integer.parseInt(candidate.substring(5, 7), 16) + - Integer.parseInt(color.substring(5, 7), 16)) + * (Integer.parseInt(candidate.substring(5, 7), 16) + - Integer.parseInt(color.substring(5, 7), 16)); + } + + private List computeAllShorthandCombinations() { + List result = new ArrayList<>(); + List hexNumber = + new ArrayList<>( + Arrays.asList( + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', + 'd', 'e', 'f')); + for (int i = 0; i < hexNumber.size(); i++) { + for (int j = 0; j < hexNumber.size(); j++) { + for (int k = 0; k < hexNumber.size(); k++) { + StringBuilder sb = new StringBuilder(); + sb.append("#"); + sb.append(hexNumber.get(i)); + sb.append(hexNumber.get(i)); + sb.append(hexNumber.get(j)); + sb.append(hexNumber.get(j)); + sb.append(hexNumber.get(k)); + sb.append(hexNumber.get(k)); + result.add(sb.toString()); + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_802.java b/src/main/java/com/fishercoder/solutions/firstthousand/_802.java new file mode 100644 index 0000000000..467e19e249 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_802.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _802 { + public static class Solution1 { + /* + * This is a variation of the templated topological sort in that it doesn't use indegree array, instead, it uses an outdegree array. + *

+ * For topological sort, it usually makes sense to just keep an array of elements since it's a graph of n nodes, + * we always need to take care each and every one of the nodes, no skipping any, so using an array could let you access each node by its index/name directly. + */ + public List eventualSafeNodes(int[][] graph) { + int n = graph.length; + List[] adjList = new ArrayList[n]; + for (int i = 0; i < n; i++) { + adjList[i] = new ArrayList<>(); + } + int[] outdegree = new int[n]; + for (int i = 0; i < n; i++) { + for (int g : graph[i]) { + adjList[g].add(i); + outdegree[i]++; + } + } + Queue q = new LinkedList<>(); + for (int i = 0; i < n; i++) { + if (outdegree[i] == 0) { + q.offer(i); + } + } + boolean[] safe = new boolean[n]; + while (!q.isEmpty()) { + Integer curr = q.poll(); + safe[curr] = true; + for (int v : adjList[curr]) { + outdegree[v]--; + if (outdegree[v] == 0) { + q.offer(v); + } + } + } + List result = new ArrayList<>(); + for (int i = 0; i < n; i++) { + if (safe[i]) { + result.add(i); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_804.java b/src/main/java/com/fishercoder/solutions/firstthousand/_804.java new file mode 100644 index 0000000000..920f28bc0b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_804.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _804 { + public static class Solution1 { + public int uniqueMorseRepresentations(String[] words) { + String[] morseCodes = + new String[] { + ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", + "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", + "...-", ".--", "-..-", "-.--", "--.." + }; + Set concatenation = new HashSet<>(); + StringBuilder sb = new StringBuilder(); + for (String word : words) { + sb.setLength(0); + for (char c : word.toCharArray()) { + sb.append(morseCodes[c - 'a']); + } + concatenation.add(sb.toString()); + } + return concatenation.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_806.java b/src/main/java/com/fishercoder/solutions/firstthousand/_806.java new file mode 100644 index 0000000000..de0e9f6f3f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_806.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _806 { + public static class Solution1 { + public int[] numberOfLines(int[] widths, String S) { + int numOfLines = 1; + int offsetInCurrentLine = 0; + for (char c : S.toCharArray()) { + if (offsetInCurrentLine + widths[c - 'a'] < 100) { + offsetInCurrentLine += widths[c - 'a']; + } else if (offsetInCurrentLine + widths[c - 'a'] == 100) { + numOfLines++; + offsetInCurrentLine = 0; + } else { + numOfLines++; + offsetInCurrentLine = widths[c - 'a']; + } + } + return new int[] {numOfLines, offsetInCurrentLine}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_807.java b/src/main/java/com/fishercoder/solutions/firstthousand/_807.java new file mode 100644 index 0000000000..c6bbb413bc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_807.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.firstthousand; + +public class _807 { + public static class Solution1 { + public int maxIncreaseKeepingSkyline(int[][] grid) { + int size = grid.length; + int[] horizontalLimits = new int[size]; + int[] verticalLimits = new int[size]; + for (int i = 0; i < size; i++) { + int horizontalLimit = grid[i][0]; + for (int j = 1; j < size; j++) { + horizontalLimit = Math.max(horizontalLimit, grid[i][j]); + } + horizontalLimits[i] = horizontalLimit; + } + for (int j = 0; j < size; j++) { + int verticalLimit = grid[0][j]; + for (int i = 1; i < size; i++) { + verticalLimit = Math.max(verticalLimit, grid[i][j]); + } + verticalLimits[j] = verticalLimit; + } + int increases = 0; + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + if (grid[i][j] != horizontalLimits[i] && grid[i][j] != verticalLimits[j]) { + increases += Math.min(horizontalLimits[i], verticalLimits[j]) - grid[i][j]; + } + } + } + return increases; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_809.java b/src/main/java/com/fishercoder/solutions/firstthousand/_809.java new file mode 100644 index 0000000000..cd85c68957 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_809.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.firstthousand; + +public class _809 { + public static class Solution1 { + public int expressiveWords(String S, String[] words) { + int ans = 0; + for (String w : words) { + if (check(S, w)) { + ans++; + } + } + return ans; + } + + private boolean check(String S, String w) { + int i = 0; + int j = 0; + /* Logic is to check whether character at same index of S and w are same + if same, + 1. Find the consecutive number of occurrences of the char in S (say len1) and w ( say len2) + 2. If len1 == len 2 , move to the next char in S and w + 3. If len1 >= 3 and len2 < len1, means we can make the char in w stretchy to match len1 + 4. else, return false, because it's not possible to stretch the char in w + */ + while (i < S.length() && j < w.length()) { + char ch1 = S.charAt(i); + char ch2 = w.charAt(j); + + int len1 = getLen(S, i); + int len2 = getLen(w, j); + if (ch1 == ch2) { + if (len1 == len2) { + i = i + len1; + j = j + len2; + } else if (len1 >= 3 && len2 < len1) { + i = i + len1; + j = j + len2; + } else { + return false; + } + } else { + return false; + } + } + return i == S.length() && j == w.length(); + } + + private int getLen(String value, int i) { + i = i + 1; + int count = 1; + for (int j = i; j < value.length(); j++) { + if (value.charAt(j) == value.charAt(i - 1)) { + count++; + } else { + break; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_81.java b/src/main/java/com/fishercoder/solutions/firstthousand/_81.java new file mode 100644 index 0000000000..2c9213dbbc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_81.java @@ -0,0 +1,70 @@ +package com.fishercoder.solutions.firstthousand; + +/* + * 81. Search in Rotated Sorted Array II + *

+ * There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values). + * Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is + * [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4]. + * Given the array nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums. + * You must decrease the overall operation steps as much as possible. + *

+ * Example 1: + * Input: nums = [2,5,6,0,0,1,2], target = 0 + * Output: true + *

+ * Example 2: + * Input: nums = [2,5,6,0,0,1,2], target = 3 + * Output: false + *

+ * Constraints: + * 1 <= nums.length <= 5000 + * -104 <= nums[i] <= 104 + * nums is guaranteed to be rotated at some pivot. + * -104 <= target <= 104 + * Follow up: This problem is similar to Search in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why? + */ +public class _81 { + public static class Solution1 { + public boolean search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + + // check each num so we will check left == right + // We always get a sorted part and a half part + // we can check sorted part to decide where to go next + while (left <= right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) { + return true; + } + + if (nums[left] < nums[mid]) { + // if left part is sorted + if (target < nums[left] || target > nums[mid]) { + // target is in rotated part + left = mid + 1; + } else { + right = mid - 1; + } + } else if (nums[left] > nums[mid]) { + // right part is sorted + if (target < nums[mid] || target > nums[right]) { + // target is in rotated part + right = mid - 1; + } else { + left = mid + 1; + } + } else { + // duplicates, we know nums[mid] != target, so nums[left] != target + // based on current information, we can only move left pointer to skip one cell + // thus in the worst case, we would have target: 2, and array like 11111111, + // then + // the running time would be O(n) + left++; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_811.java b/src/main/java/com/fishercoder/solutions/firstthousand/_811.java new file mode 100644 index 0000000000..b8da068f02 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_811.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _811 { + public static class Solution1 { + public List subdomainVisits(String[] cpdomains) { + Map map = new HashMap<>(); + for (String each : cpdomains) { + String[] pair = each.split(" "); + String[] subDomains = pair[1].split("\\."); + StringBuilder sb = new StringBuilder(); + for (int i = subDomains.length - 1; i >= 0; i--) { + if (i < subDomains.length - 1) { + sb.insert(0, "."); + } + sb.insert(0, subDomains[i]); + map.put( + sb.toString(), + map.getOrDefault(sb.toString(), 0) + Integer.parseInt(pair[0])); + } + } + List result = new ArrayList<>(); + for (String key : map.keySet()) { + result.add(map.get(key) + " " + key); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_812.java b/src/main/java/com/fishercoder/solutions/firstthousand/_812.java new file mode 100644 index 0000000000..f8d5564970 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_812.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.firstthousand; + +public class _812 { + public static class Solution1 { + /* + * reference: https://www.mathopenref.com/coordtrianglearea.html + */ + public double largestTriangleArea(int[][] points) { + double largestArea = 0.0; + for (int i = 0; i < points.length - 2; i++) { + for (int j = i + 1; j < points.length - 1; j++) { + for (int k = j + 1; k < points.length; k++) { + double area = + Math.abs( + points[i][0] * (points[j][1] - points[k][1]) + + points[j][0] + * (points[k][1] - points[i][1]) + + points[k][0] + * (points[i][1] - points[j][1])) + / 2.0; + largestArea = Math.max(largestArea, area); + } + } + } + return largestArea; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_814.java b/src/main/java/com/fishercoder/solutions/firstthousand/_814.java new file mode 100644 index 0000000000..f95a14f4f7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_814.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _814 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/binary-tree-pruning/discuss/122730/C%2B%2BJavaPython-Self-Explaining-Solution-and-2-lines + */ + public TreeNode pruneTree(TreeNode root) { + if (root == null) { + return root; + } + root.left = pruneTree(root.left); + root.right = pruneTree(root.right); + if (root.left == null && root.right == null && root.val == 0) { + return null; + } + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_816.java b/src/main/java/com/fishercoder/solutions/firstthousand/_816.java new file mode 100644 index 0000000000..2357234a6b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_816.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _816 { + public static class Solution1 { + public List ambiguousCoordinates(String s) { + s = s.substring(1, s.length() - 1); + List list = new ArrayList<>(); + for (int i = 1; i < s.length(); i++) { + String x = s.substring(0, i); + List xs = findAllPossibilities(x); + String y = s.substring(i); + List ys = findAllPossibilities(y); + for (String j : xs) { + for (String k : ys) { + list.add("(" + j + ", " + k + ")"); + } + } + } + return list; + } + + private List findAllPossibilities(String str) { + List result = new ArrayList<>(); + if (str.length() == 1) { + result.add(str); + return result; + } else { + for (int i = 1; i < str.length(); i++) { + String integerPart = str.substring(0, i); + String floatPart = str.substring(i); + if (integerPart.length() > 1 + && integerPart.charAt(0) != '0' + && floatPart.charAt(floatPart.length() - 1) != '0') { + result.add(integerPart + "." + floatPart); + } else if (integerPart.length() == 1 + && floatPart.charAt(floatPart.length() - 1) != '0') { + result.add(integerPart + "." + floatPart); + } + } + if (str.charAt(0) != '0') { + result.add(str); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_819.java b/src/main/java/com/fishercoder/solutions/firstthousand/_819.java new file mode 100644 index 0000000000..8a6c1c087c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_819.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _819 { + public static class Solution1 { + public String mostCommonWord(String paragraph, String[] banned) { + Set bannedSet = new HashSet(Arrays.asList(banned)); + String[] words = paragraph.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+"); + Map map = new HashMap<>(); + Arrays.stream(words) + .filter(word -> !bannedSet.contains(word)) + .forEach(word -> map.put(word, map.getOrDefault(word, 0) + 1)); + String result = ""; + int freq = 0; + for (String key : map.keySet()) { + if (map.get(key) > freq) { + result = key; + freq = map.get(key); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_82.java b/src/main/java/com/fishercoder/solutions/firstthousand/_82.java new file mode 100644 index 0000000000..eef715a9d1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_82.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _82 { + public static class Solution1 { + public ListNode deleteDuplicates(ListNode head) { + ListNode pre = new ListNode(-1); + pre.next = head; + ListNode tmp = pre; + while (head != null) { + while (head.next != null && head.val == head.next.val) { + head = head.next; + } + if (tmp.next == head) { + tmp = tmp.next; + } else { + tmp.next = head.next; + } + head = head.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_820.java b/src/main/java/com/fishercoder/solutions/firstthousand/_820.java new file mode 100644 index 0000000000..07788c5a04 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_820.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _820 { + public static class Solution1 { + public int minimumLengthEncoding(String[] words) { + Arrays.sort(words, (a, b) -> a.length() - b.length()); + boolean[] removed = new boolean[words.length]; + for (int j = words.length - 2; j >= 0; j--) { + for (int i = j + 1; i < words.length; i++) { + if (!removed[i]) { + if (words[i].substring(words[i].length() - words[j].length()) + .equals(words[j])) { + removed[j] = true; + break; + } + } + } + } + int len = 0; + for (int i = 0; i < words.length; i++) { + if (!removed[i]) { + len += words[i].length(); + len++; + } + } + return len; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_821.java b/src/main/java/com/fishercoder/solutions/firstthousand/_821.java new file mode 100644 index 0000000000..07fdc0b9c6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_821.java @@ -0,0 +1,62 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.TreeSet; + +public class _821 { + + public static class Solution1 { + public int[] shortestToChar(String S, char C) { + int[] result = new int[S.length()]; + TreeSet cIndices = new TreeSet(); + for (int i = 0; i < S.length(); i++) { + if (S.charAt(i) == C) { + cIndices.add(i); + } + } + for (int i = 0; i < S.length(); i++) { + int leftDist = Integer.MAX_VALUE; + if (cIndices.floor(i) != null) { + leftDist = Math.abs(cIndices.floor(i) - i); + } + int rightDist = Integer.MAX_VALUE; + if (cIndices.ceiling(i) != null) { + rightDist = Math.abs(cIndices.ceiling(i) - i); + } + result[i] = Math.min(leftDist, rightDist); + } + return result; + } + } + + public static class Solution2 { + public int[] shortestToChar(String s, char c) { + int[] result = new int[s.length()]; + Arrays.fill(result, Integer.MAX_VALUE); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == c) { + result[i] = 0; + } + } + for (int i = 0; i < s.length(); i++) { + if (result[i] != 0) { + int j = i - 1; + while (j >= 0 && result[j] != 0) { + j--; + } + if (j >= 0) { + result[i] = i - j; + } + j = i + 1; + while (j < s.length() && result[j] != 0) { + j++; + } + if (j < s.length()) { + result[i] = Math.min(result[i], j - i); + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_823.java b/src/main/java/com/fishercoder/solutions/firstthousand/_823.java new file mode 100644 index 0000000000..ff3ed17760 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_823.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class _823 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/binary-trees-with-factors/discuss/126277/Concise-Java-solution-using-HashMap-with-detailed-explanation.-Easily-understand!!! + */ + private static final long MOD = 1000000007L; + + public int numFactoredBinaryTrees(int[] arr) { + Arrays.sort(arr); + Map map = new HashMap<>(); + long count = 1; + map.put(arr[0], count); + for (int i = 1; i < arr.length; i++) { + count = 1; + for (int key : map.keySet()) { + if (arr[i] % key == 0 && map.containsKey(arr[i] / key)) { + count += map.get(key) * map.get(arr[i] / key); + } + } + map.put(arr[i], count); + } + long sum = 0; + for (int key : map.keySet()) { + sum = (sum + map.get(key)) % MOD; + } + return (int) sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_824.java b/src/main/java/com/fishercoder/solutions/firstthousand/_824.java new file mode 100644 index 0000000000..441c3160c7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_824.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _824 { + + public static class Solution1 { + public String toGoatLatin(String S) { + StringBuilder sb = new StringBuilder(); + Set vowels = + new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')); + String[] words = S.split(" "); + for (int i = 0; i < words.length; i++) { + if (vowels.contains(words[i].charAt(0))) { + String newWord = words[i] + "ma"; + int j = i + 1; + while (j-- > 0) { + newWord += 'a'; + } + sb.append(newWord); + sb.append(" "); + } else { + StringBuilder subSb = new StringBuilder(words[i].substring(1)); + subSb.append(words[i].charAt(0)); + subSb.append("ma"); + int j = i + 1; + while (j-- > 0) { + subSb.append("a"); + } + sb.append(subSb.toString()); + sb.append(" "); + } + } + return sb.substring(0, sb.length() - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_826.java b/src/main/java/com/fishercoder/solutions/firstthousand/_826.java new file mode 100644 index 0000000000..50a51386fd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_826.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _826 { + public static class Solution1 { + public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { + List jobs = new ArrayList<>(); + for (int i = 0; i < difficulty.length; i++) { + jobs.add(new int[] {difficulty[i], profit[i]}); + } + // sort by difficulty level + Collections.sort(jobs, (a, b) -> a[0] - b[0]); + + // update the profit values: because a later (with more difficult) job must be able to + // handle a prior job, so we take the more profitable one + // this makes this jobs list in non-decreasing order in both dimensions + for (int i = 0; i < jobs.size() - 1; i++) { + jobs.get(i + 1)[1] = Math.max(jobs.get(i)[1], jobs.get(i + 1)[1]); + } + + int maxProfit = 0; + for (int ability : worker) { + maxProfit += binarySearch(ability, jobs); + } + return maxProfit; + } + + private int binarySearch(int ability, List jobs) { + int left = 0; + int right = jobs.size() - 1; + int maxProfit = 0; + // it's important to use <= here so we don't miss a possible element + while (left <= right) { + int mid = left + (right - left) / 2; + if (ability >= jobs.get(mid)[0]) { + maxProfit = Math.max(jobs.get(mid)[1], maxProfit); + left = mid + 1; + } else { + right = mid - 1; + } + } + return maxProfit; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_83.java b/src/main/java/com/fishercoder/solutions/firstthousand/_83.java similarity index 84% rename from src/main/java/com/fishercoder/solutions/_83.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_83.java index a3428e7c6f..1f1e87e5b6 100644 --- a/src/main/java/com/fishercoder/solutions/_83.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_83.java @@ -1,19 +1,19 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.ListNode; public class _83 { public static class Solution1 { public ListNode deleteDuplicates(ListNode head) { - ListNode ret = new ListNode(-1); - ret.next = head; + ListNode pre = new ListNode(-1); + pre.next = head; while (head != null) { while (head.next != null && head.next.val == head.val) { head.next = head.next.next; } head = head.next; } - return ret.next; + return pre.next; } } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_830.java b/src/main/java/com/fishercoder/solutions/firstthousand/_830.java new file mode 100644 index 0000000000..681696712c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_830.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _830 { + public static class Solution1 { + public List> largeGroupPositions(String S) { + List> result = new ArrayList<>(); + char[] chars = S.toCharArray(); + for (int i = 0; i < chars.length; ) { + char first = chars[i]; + int j = i + 1; + while (j < chars.length && first == chars[j]) { + j++; + } + if ((j - i) >= 3) { + result.add(Arrays.asList(i, j - 1)); + } + i = j; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_832.java b/src/main/java/com/fishercoder/solutions/firstthousand/_832.java new file mode 100644 index 0000000000..7fd4cd103f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_832.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +public class _832 { + public static class Solution1 { + public int[][] flipAndInvertImage(int[][] A) { + int m = A.length; + int n = A[0].length; + int[][] result = new int[m][n]; + for (int i = 0; i < m; i++) { + int[] flipped = (reverse(A[i])); + result[i] = invert(flipped); + } + return result; + } + + private int[] invert(int[] flipped) { + int[] result = new int[flipped.length]; + for (int i = 0; i < flipped.length; i++) { + if (flipped[i] == 0) { + result[i] = 1; + } else { + result[i] = 0; + } + } + return result; + } + + private int[] reverse(int[] nums) { + for (int i = 0, j = nums.length - 1; i < j; i++, j--) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + } + return nums; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_836.java b/src/main/java/com/fishercoder/solutions/firstthousand/_836.java new file mode 100644 index 0000000000..e476c8d1d0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_836.java @@ -0,0 +1,12 @@ +package com.fishercoder.solutions.firstthousand; + +public class _836 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/rectangle-overlap/discuss/132340/C%2B%2BJavaPython-1-line-Solution-1D-to-2D + */ + public boolean isRectangleOverlap(int[] rec1, int[] rec2) { + return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_838.java b/src/main/java/com/fishercoder/solutions/firstthousand/_838.java new file mode 100644 index 0000000000..8685e0b30b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_838.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _838 { + public static class Solution1 { + public String pushDominoes(String dominoes) { + StringBuilder currentSb = new StringBuilder(dominoes); + Set visited = new HashSet<>(); + visited.add(dominoes); + do { + StringBuilder newSb = new StringBuilder(); + for (int i = 0; i < currentSb.length(); i++) { + if (currentSb.charAt(i) == 'L') { + newSb.append('L'); + if (i == 1 && currentSb.charAt(i - 1) == '.') { + newSb.replace(i - 1, i, "L"); + } else if (i > 1 + && currentSb.charAt(i - 1) == '.' + && currentSb.charAt(i - 2) != 'R') { + newSb.replace(i - 1, i, "L"); + } + } else if (currentSb.charAt(i) == 'R') { + newSb.append('R'); + if (i == currentSb.length() - 2 && currentSb.charAt(i + 1) == '.') { + newSb.replace(i + 1, i + 2, "R"); + i++; + } else if (i < currentSb.length() - 2 + && currentSb.charAt(i + 1) == '.' + && currentSb.charAt(i + 2) != 'L') { + newSb.replace(i + 1, i + 2, "R"); + i++; + } + } else { + newSb.append('.'); + } + } + currentSb.setLength(0); + currentSb = newSb; + } while (visited.add(currentSb.toString())); + return currentSb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_84.java b/src/main/java/com/fishercoder/solutions/firstthousand/_84.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_84.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_84.java index 5b2e7e72e6..c379d5cd40 100644 --- a/src/main/java/com/fishercoder/solutions/_84.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_84.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.Stack; @@ -6,7 +6,7 @@ public class _84 { public static class Solution1 { - /** + /* * credit: https://leetcode.com/articles/largest-rectangle-histogram/#approach-5-using-stack-accepted * and https://discuss.leetcode.com/topic/7599/o-n-stack-based-java-solution */ @@ -27,5 +27,4 @@ public int largestRectangleArea(int[] heights) { return maxArea; } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_840.java b/src/main/java/com/fishercoder/solutions/firstthousand/_840.java new file mode 100644 index 0000000000..077d74b699 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_840.java @@ -0,0 +1,56 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _840 { + public static class Solution1 { + public int numMagicSquaresInside(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + int count = 0; + for (int i = 0; i < m - 2; i++) { + for (int j = 0; j < n - 2; j++) { + Set set = new HashSet<>(); + int sum = grid[i][j] + grid[i][j + 1] + grid[i][j + 2]; + if (isValid(grid, i, j, set, sum)) { + count++; + } + } + } + return count; + } + + private boolean isValid(int[][] grid, int i, int j, Set set, int sum) { + return sum == grid[i + 1][j] + grid[i + 1][j + 1] + grid[i + 1][j + 2] + && sum == grid[i + 2][j] + grid[i + 2][j + 1] + grid[i + 2][j + 2] + && sum == grid[i][j] + grid[i + 1][j] + grid[i + 2][j] + && sum == grid[i][j + 1] + grid[i + 1][j + 1] + grid[i + 2][j + 1] + && sum == grid[i][j + 2] + grid[i + 1][j + 2] + grid[i + 2][j + 2] + && sum == grid[i][j] + grid[i + 1][j + 1] + grid[i + 2][j + 2] + && sum == grid[i][j + 2] + grid[i + 1][j + 1] + grid[i + 2][j] + && set.add(grid[i][j]) + && isLegit(grid[i][j]) + && set.add(grid[i][j + 1]) + && isLegit(grid[i][j + 1]) + && set.add(grid[i][j + 2]) + && isLegit(grid[i][j + 2]) + && set.add(grid[i + 1][j]) + && isLegit(grid[i + 1][j]) + && set.add(grid[i + 1][j + 1]) + && isLegit(grid[i + 1][j + 1]) + && set.add(grid[i + 1][j + 2]) + && isLegit(grid[i + 1][j + 2]) + && set.add(grid[i + 2][j]) + && isLegit(grid[i + 2][j]) + && set.add(grid[i + 2][j + 1]) + && isLegit(grid[i + 2][j + 1]) + && set.add(grid[i + 2][j + 2]) + && isLegit(grid[i + 2][j + 2]); + } + + private boolean isLegit(int num) { + return num <= 9 && num >= 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_841.java b/src/main/java/com/fishercoder/solutions/firstthousand/_841.java new file mode 100644 index 0000000000..48c5502708 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_841.java @@ -0,0 +1,95 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Set; +import java.util.TreeSet; + +public class _841 { + public static class Solution1 { + public boolean canVisitAllRooms(List> rooms) { + Set unvisitedRooms = new HashSet<>(); + for (int i = 1; i < rooms.size(); i++) { + unvisitedRooms.add(i); + } + List keys = rooms.get(0); + Queue queue = new LinkedList<>(); + for (int key : keys) { + queue.offer(key); + } + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + int roomIndex = queue.poll(); + unvisitedRooms.remove(roomIndex); + for (int j = 0; j < rooms.get(roomIndex).size(); j++) { + Integer nextRoom = rooms.get(roomIndex).get(j); + if (unvisitedRooms.contains(nextRoom) && !queue.contains(nextRoom)) { + queue.offer(nextRoom); + } + } + } + } + return unvisitedRooms.isEmpty(); + } + } + + public static class Solution2 { + public boolean canVisitAllRooms(List> rooms) { + TreeSet treeSet = new TreeSet<>(); + Set visited = new HashSet<>(); + visited.add(0); + treeSet.addAll(rooms.get(0)); + while (!treeSet.isEmpty()) { + Integer key = treeSet.pollFirst(); + if (!visited.add(key)) { + continue; + } + if (visited.size() == rooms.size()) { + return true; + } + treeSet.addAll(rooms.get(key)); + } + return visited.size() == rooms.size(); + } + } + + public static class Solution3 { + /* + * My completely original recursive solution. + */ + public boolean canVisitAllRooms(List> rooms) { + Set visited = new HashSet<>(); + visited.add(0); + Set keys = new HashSet<>(); + keys.addAll(rooms.get(0)); + return dfs(rooms, visited, keys); + } + + private boolean dfs(List> rooms, Set visited, Set keys) { + if (visited.size() == rooms.size()) { + return true; + } + Set newKeys = new HashSet<>(); + for (int key : keys) { + if (!visited.contains(key)) { + visited.add(key); + if (!rooms.get(key).isEmpty()) { + newKeys.addAll(rooms.get(key)); + } + } + } + if (visited.size() == rooms.size()) { + return true; + } + if (newKeys.size() == 0) { + return false; + } + keys.addAll(newKeys); + dfs(rooms, visited, keys); + return visited.size() == rooms.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_844.java b/src/main/java/com/fishercoder/solutions/firstthousand/_844.java new file mode 100644 index 0000000000..c02c6dd017 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_844.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _844 { + public static class Solution1 { + public boolean backspaceCompare(String S, String T) { + String processedS = process(S); + String processedT = process(T); + return processedS.equals(processedT); + } + + private String process(String str) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < str.length(); i++) { + if (str.charAt(i) == '#') { + if (sb.length() > 0) { + sb.deleteCharAt(sb.length() - 1); + } + } else { + sb.append(str.charAt(i)); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_847.java b/src/main/java/com/fishercoder/solutions/firstthousand/_847.java new file mode 100644 index 0000000000..9c02f0773d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_847.java @@ -0,0 +1,10 @@ +package com.fishercoder.solutions.firstthousand; + +public class _847 { + public static class Solution1 { + public int shortestPathLength(int[][] graph) { + // TODO: implement this + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_848.java b/src/main/java/com/fishercoder/solutions/firstthousand/_848.java new file mode 100644 index 0000000000..22098c9fb7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_848.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.firstthousand; + +public class _848 { + public static class Solution1 { + public String shiftingLetters(String s, int[] shifts) { + long[] preSums = + new long[shifts.length]; // use long type to avoid integer addition overflow + for (int i = shifts.length - 1; i >= 0; i--) { + if (i < shifts.length - 1) { + preSums[i] = preSums[i + 1] + shifts[i]; + } else { + preSums[i] = shifts[i]; + } + preSums[i] %= 26; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + int newChar = s.charAt(i) + (int) preSums[i] % 26; + if (newChar > 122) { + sb.append((char) (newChar - 122 + 96)); + } else { + sb.append((char) (newChar)); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_849.java b/src/main/java/com/fishercoder/solutions/firstthousand/_849.java new file mode 100644 index 0000000000..9d9fa299d6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_849.java @@ -0,0 +1,81 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.TreeSet; + +public class _849 { + public static class Solution1 { + int maxDist = 0; + + public int maxDistToClosest(int[] seats) { + for (int i = 0; i < seats.length; i++) { + if (seats[i] == 0) { + extend(seats, i); + } + } + return maxDist; + } + + private void extend(int[] seats, int position) { + int left = position - 1; + int right = position + 1; + int leftMinDistance = 1; + while (left >= 0) { + if (seats[left] == 0) { + leftMinDistance++; + left--; + } else { + break; + } + } + int rightMinDistance = 1; + while (right < seats.length) { + if (seats[right] == 0) { + rightMinDistance++; + right++; + } else { + break; + } + } + int maxReach = 0; + if (position == 0) { + maxReach = rightMinDistance; + } else if (position == seats.length - 1) { + maxReach = leftMinDistance; + } else { + maxReach = Math.min(leftMinDistance, rightMinDistance); + } + maxDist = Math.max(maxDist, maxReach); + } + } + + public static class Solution2 { + /* + * my completely original solution on 9/13/2021. + */ + public int maxDistToClosest(int[] seats) { + int maxDistance = 0; + TreeSet treeMap = new TreeSet<>(); + for (int i = 0; i < seats.length; i++) { + if (seats[i] == 1) { + treeMap.add(i); + } + } + for (int i = 0; i < seats.length; i++) { + if (seats[i] == 0) { + Integer leftNeighbor = treeMap.floor(i); + Integer rightNeighbor = treeMap.ceiling(i); + if (leftNeighbor != null && rightNeighbor != null) { + maxDistance = + Math.max( + maxDistance, Math.min(i - leftNeighbor, rightNeighbor - i)); + } else if (leftNeighbor == null) { + maxDistance = Math.max(maxDistance, rightNeighbor - i); + } else { + maxDistance = Math.max(maxDistance, i - leftNeighbor); + } + } + } + return maxDistance; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_85.java b/src/main/java/com/fishercoder/solutions/firstthousand/_85.java similarity index 84% rename from src/main/java/com/fishercoder/solutions/_85.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_85.java index 22339ff2e2..947d48938e 100644 --- a/src/main/java/com/fishercoder/solutions/_85.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_85.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.Arrays; @@ -21,7 +21,7 @@ public int maximalRectangle(char[][] matrix) { int currLeft = 0; int currRight = n; - //compute height, this can be achieved from either side + // compute height, this can be achieved from either side for (int j = 0; j < n; j++) { if (matrix[i][j] == '1') { height[j]++; @@ -30,7 +30,7 @@ public int maximalRectangle(char[][] matrix) { } } - //compute left, from left to right + // compute left, from left to right for (int j = 0; j < n; j++) { if (matrix[i][j] == '1') { left[j] = Math.max(left[j], currLeft); @@ -40,7 +40,7 @@ public int maximalRectangle(char[][] matrix) { } } - //compute right, from right to left + // compute right, from right to left for (int j = n - 1; j >= 0; j--) { if (matrix[i][j] == '1') { right[j] = Math.min(right[j], currRight); @@ -50,7 +50,7 @@ public int maximalRectangle(char[][] matrix) { } } - //compute rectangle area, this can be achieved from either side + // compute rectangle area, this can be achieved from either side for (int j = 0; j < n; j++) { maxA = Math.max(maxA, (right[j] - left[j]) * height[j]); } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_851.java b/src/main/java/com/fishercoder/solutions/firstthousand/_851.java new file mode 100644 index 0000000000..5a8b9cccf4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_851.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _851 { + public static class Solution1 { + /* + * My completely original solution. Practice does make perfect! + * Topological sort template does work well for this: + * 1. make variable names as descriptive as possible to help sort out your logic; + * 2. initializing an array of size n for topological sort problems of n nodes is pretty handy; + * 3. it's either indegree or outdegree, and each time, we process it, we decrement the degree by one, in this case, we also check the quietness; + * 4. overwrite the value for quiet[v] each time it needs to be updated so that next time around, it's not going to use outdated quietness value + */ + public int[] loudAndRich(int[][] richer, int[] quiet) { + List[] adjList = new ArrayList[quiet.length]; + for (int i = 0; i < quiet.length; i++) { + adjList[i] = new ArrayList<>(); + } + int[] indegree = new int[quiet.length]; + if (richer.length != 0 && richer[0].length != 0) { + for (int[] rich : richer) { + indegree[rich[1]]++; + adjList[rich[0]].add(rich[1]); + } + } + Queue q = new LinkedList<>(); + int[] result = new int[quiet.length]; + for (int i = 0; i < quiet.length; i++) { + if (indegree[i] == 0) { + q.offer(i); + } + result[i] = i; + } + while (!q.isEmpty()) { + int curr = q.poll(); + for (int v : adjList[curr]) { + int quietnessForLessRichPerson = quiet[v]; + int quietnessForRicherPerson = quiet[result[curr]]; + if (quietnessForRicherPerson < quietnessForLessRichPerson) { + result[v] = result[curr]; + // remember to update the quietness value for this node as well + quiet[v] = quiet[curr]; + } + indegree[v]--; + if (indegree[v] == 0) { + q.offer(v); + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_852.java b/src/main/java/com/fishercoder/solutions/firstthousand/_852.java new file mode 100644 index 0000000000..b35505948d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_852.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.firstthousand; + +public class _852 { + public static class Solution1 { + public int peakIndexInMountainArray(int[] A) { + for (int i = 1; i < A.length - 1; i++) { + if (A[i] > A[i + 1]) { + return i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_856.java b/src/main/java/com/fishercoder/solutions/firstthousand/_856.java new file mode 100644 index 0000000000..18f821326c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_856.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Stack; + +public class _856 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/score-of-parentheses/discuss/141763/Java-solution-using-Stack + */ + public int scoreOfParentheses(String S) { + Stack stack = new Stack<>(); + for (int i = 0; i < S.length(); i++) { + if (S.charAt(i) == '(') { + stack.push(-1); // we use -1 to indicate this is a left paren '(' + } else { + int curr = 0; + while (stack.peek() != -1) { + curr += stack.pop(); + } + stack.pop(); // this is to push the '(' off of the stack + stack.push(curr == 0 ? 1 : curr * 2); + } + } + int score = 0; + while (!stack.isEmpty()) { + score += stack.pop(); + } + return score; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_859.java b/src/main/java/com/fishercoder/solutions/firstthousand/_859.java new file mode 100644 index 0000000000..900c4f5120 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_859.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _859 { + public static class Solution1 { + public boolean buddyStrings(String A, String B) { + if (A.length() != B.length()) { + return false; + } + Character c1 = null; + Character c2 = null; + Set set = new HashSet<>(); + int count = 0; + for (int i = 0; i < A.length(); i++) { + if (A.charAt(i) != B.charAt(i)) { + if (count > 2) { + return false; + } + if (c1 == null) { + c1 = B.charAt(i); + c2 = A.charAt(i); + count++; + continue; + } + if (c1 != A.charAt(i) || c2 != B.charAt(i)) { + return false; + } + count++; + } + set.add(A.charAt(i)); + } + return count == 2 || (count == 0 && set.size() < A.length()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_86.java b/src/main/java/com/fishercoder/solutions/firstthousand/_86.java new file mode 100644 index 0000000000..a701f9d59e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_86.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.List; + +public class _86 { + public static class Solution1 { + public ListNode partition(ListNode head, int x) { + if (head == null || head.next == null) { + return head; + } + ListNode left = new ListNode(0); + ListNode right = new ListNode(0); + ListNode less = left; + ListNode greater = right; + while (head != null) { + if (head.val < x) { + less.next = head; + less = less.next; + } else { + greater.next = head; + greater = greater.next; + } + head = head.next; + } + greater.next = null; + less.next = right.next; + return left.next; + } + } + + public static class Solution2 { + public ListNode partition(ListNode head, int x) { + List first = new ArrayList<>(); + List last = new ArrayList<>(); + while (head != null) { + if (head.val < x) { + first.add(head.val); + } else { + last.add(head.val); + } + head = head.next; + } + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + int i = 0; + int j = 0; + while (i < first.size() || j < last.size()) { + if (i < first.size()) { + tmp.next = new ListNode(first.get(i++)); + } else if (j < last.size()) { + tmp.next = new ListNode(last.get(j++)); + } + tmp = tmp.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_860.java b/src/main/java/com/fishercoder/solutions/firstthousand/_860.java new file mode 100644 index 0000000000..b519c6246a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_860.java @@ -0,0 +1,90 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _860 { + public static class Solution1 { + public boolean lemonadeChange(int[] bills) { + Map map = new HashMap<>(); + for (int bill : bills) { + if (bill == 5) { + map.put(5, map.getOrDefault(5, 0) + 1); + } else if (bill == 10) { + if (!map.containsKey(5)) { + return false; + } else { + map.put(5, map.get(5) - 1); + if (map.get(5) == 0) { + map.remove(5); + } + map.put(10, map.getOrDefault(10, 0) + 1); + } + } else { + if (!map.containsKey(5)) { + return false; + } else { + if (!map.containsKey(10)) { + if (!map.containsKey(5) || map.get(5) < 3) { + return false; + } else { + map.put(5, map.get(5) - 3); + if (map.get(5) == 0) { + map.remove(5); + } + } + } else { + if (!map.containsKey(5)) { + return false; + } else { + map.put(5, map.get(5) - 1); + if (map.get(5) == 0) { + map.remove(5); + } + map.put(10, map.get(10) - 1); + if (map.get(10) == 0) { + map.remove(10); + } + } + } + } + map.put(20, map.getOrDefault(20, 0) + 1); + } + } + return true; + } + } + + public static class Solution2 { + /* + * My original solution on 8/14/2024. + * You only need to keep track of the number of $5 and $10 bills at hand. + */ + + public boolean lemonadeChange(int[] bills) { + int[] changes = new int[2]; // 5 and 10 + for (int bill : bills) { + if (bill == 5) { + changes[0]++; + } else if (bill == 10) { + if (changes[0] <= 0) { + return false; + } else { + changes[0]--; + } + changes[1]++; + } else if (bill == 20) { + if (changes[1] > 0 && changes[0] > 0) { + changes[1]--; + changes[0]--; + } else if (changes[0] > 2) { + changes[0] -= 3; + } else { + return false; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_861.java b/src/main/java/com/fishercoder/solutions/firstthousand/_861.java new file mode 100644 index 0000000000..a3b5c28974 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_861.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.firstthousand; + +public class _861 { + public static class Solution1 { + /* + * We can simply apply greedy methodology here. + * 1. we check if the left most digits are ones or not, if it's a zero, + * then we'll just flip this entire row, reason being the left most digit carries the biggest weight when interpreting this binary row/number; + * 2. after step #1, we'll check column wise starting from the second column, + * we'll count the number of ones in each column, if the number of ones in each column is less than or equal to half of the column length, + * then flipping this column would make a bigger number + */ + public int matrixScore(int[][] A) { + int m = A.length; + int n = A[0].length; + for (int i = 0; i < m; i++) { + if (A[i][0] == 0) { + for (int j = 0; j < n; j++) { + if (A[i][j] == 0) { + A[i][j] = 1; + } else { + A[i][j] = 0; + } + } + } + } + for (int j = 1; j < n; j++) { + int ones = 0; + for (int i = 0; i < m; i++) { + if (A[i][j] == 1) { + ones++; + } + } + if (ones <= m / 2) { + for (int i = 0; i < m; i++) { + if (A[i][j] == 1) { + A[i][j] = 0; + } else { + A[i][j] = 1; + } + } + } + } + int result = 0; + for (int i = 0; i < m; i++) { + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < n; j++) { + sb.append(A[i][j]); + } + result += Integer.parseInt(sb.toString(), 2); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_863.java b/src/main/java/com/fishercoder/solutions/firstthousand/_863.java new file mode 100644 index 0000000000..2072b0b422 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_863.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.*; + +public class _863 { + public static class Solution1 { + /* + * Since it's asking for distance k, a.k.a shortest distance, BFS should be the way to go. + * For this particular problem: we'll do BFS twice: + * 1st time: we build a child to parent mapping, in binary tree, there's only parent to children mapping, so we'll need to establish this child to parent link; + * 2nd time: we push the target node into the queue, traverse all its neighbors (children and parent), + * push them into the queue and decrement k by one, until k becomes zero, remaining elements in the queue are the answer. + */ + public List distanceK(TreeNode root, TreeNode target, int k) { + Map childToParentMap = new HashMap<>(); + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (curr.left != null) { + childToParentMap.put(curr.left.val, curr); + queue.offer(curr.left); + } + if (curr.right != null) { + childToParentMap.put(curr.right.val, curr); + queue.offer(curr.right); + } + } + } + queue.offer(target); + Set visited = new HashSet<>(); + while (k > 0 && !queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + visited.add(curr.val); + if (curr.left != null && !visited.contains(curr.left.val)) { + queue.offer(curr.left); + } + if (curr.right != null && !visited.contains(curr.right.val)) { + queue.offer(curr.right); + } + if (childToParentMap.containsKey(curr.val) + && !visited.contains(childToParentMap.get(curr.val).val)) { + queue.offer(childToParentMap.get(curr.val)); + } + } + k--; + } + List list = new ArrayList<>(); + while (!queue.isEmpty()) { + list.add(queue.poll().val); + } + return list; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_867.java b/src/main/java/com/fishercoder/solutions/firstthousand/_867.java new file mode 100644 index 0000000000..156faf6086 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_867.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.firstthousand; + +public class _867 { + public static class Solution1 { + public int[][] transpose(int[][] matrix) { + int[][] result = new int[matrix[0].length][matrix.length]; + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + result[j][i] = matrix[i][j]; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_868.java b/src/main/java/com/fishercoder/solutions/firstthousand/_868.java new file mode 100644 index 0000000000..a57f7db151 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_868.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _868 { + public static class Solution1 { + public int binaryGap(int N) { + String bin = Integer.toBinaryString(N); + List oneIndexes = new ArrayList<>(); + for (int i = 0; i < bin.length(); i++) { + if (bin.charAt(i) == '1') { + oneIndexes.add(i); + } + } + int maxGap = 0; + for (int i = 0; i < oneIndexes.size() - 1; i++) { + maxGap = Math.max(oneIndexes.get(i + 1) - oneIndexes.get(i), maxGap); + } + return maxGap; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_87.java b/src/main/java/com/fishercoder/solutions/firstthousand/_87.java similarity index 80% rename from src/main/java/com/fishercoder/solutions/_87.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_87.java index 9d89f5ef35..59201883e6 100644 --- a/src/main/java/com/fishercoder/solutions/_87.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_87.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _87 { public static class Solution1 { - /** + /* * credit: https://discuss.leetcode.com/topic/19158/accepted-java-solution */ public boolean isScramble(String s1, String s2) { @@ -27,12 +27,12 @@ public boolean isScramble(String s1, String s2) { } for (int i = 1; i < s1.length(); i++) { - if (isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble( - s1.substring(i), s2.substring(i))) { + if (isScramble(s1.substring(0, i), s2.substring(0, i)) + && isScramble(s1.substring(i), s2.substring(i))) { return true; } - if (isScramble(s1.substring(0, i), s2.substring(s2.length() - i)) && isScramble( - s1.substring(i), s2.substring(0, s2.length() - i))) { + if (isScramble(s1.substring(0, i), s2.substring(s2.length() - i)) + && isScramble(s1.substring(i), s2.substring(0, s2.length() - i))) { return true; } } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_870.java b/src/main/java/com/fishercoder/solutions/firstthousand/_870.java new file mode 100644 index 0000000000..8f2f9fc6ca --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_870.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +public class _870 { + public static class Solution1 { + public int[] advantageCount(int[] A, int[] B) { + int[] result = new int[A.length]; + Arrays.sort(A); + boolean[] used = new boolean[A.length]; + for (int i = 0; i < A.length; i++) { + result[i] = findSmallestAdvantage(A, used, B[i]); + } + List unused = new ArrayList<>(); + for (int i = 0; i < A.length; i++) { + if (!used[i]) { + unused.add(A[i]); + } + } + Iterator iterator = unused.iterator(); + for (int i = 0; i < A.length; i++) { + if (result[i] == -1) { + result[i] = iterator.next(); + } + } + return result; + } + + private int findSmallestAdvantage(int[] A, boolean[] used, int target) { + for (int i = 0; i < A.length; i++) { + if (!used[i] && A[i] > target) { + used[i] = true; + return A[i]; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_872.java b/src/main/java/com/fishercoder/solutions/firstthousand/_872.java new file mode 100644 index 0000000000..44deaff476 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_872.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _872 { + public static class Solution1 { + public boolean leafSimilar(TreeNode root1, TreeNode root2) { + List leaves1 = new ArrayList<>(); + List leaves2 = new ArrayList<>(); + preorder(root1, leaves1); + preorder(root2, leaves2); + return leaves1.equals(leaves2); + } + + private void preorder(TreeNode root, List leaves) { + if (root == null) { + return; + } + if (root.left == null && root.right == null) { + leaves.add(root.val); + } + preorder(root.left, leaves); + preorder(root.right, leaves); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_875.java b/src/main/java/com/fishercoder/solutions/firstthousand/_875.java new file mode 100644 index 0000000000..b0f25d6cd0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_875.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +public class _875 { + public static class Solution1 { + public int minEatingSpeed(int[] piles, int h) { + long left = Long.MAX_VALUE; + long right = Long.MIN_VALUE; + for (int pile : piles) { + left = Math.min(left, pile); + right = Math.max(right, pile); + } + left /= h; + while (left + 1 < right) { + long mid = left + (right - left) / 2; + if (possibleSpeed((int) mid, piles, h)) { + right = mid; + } else { + left = mid; + } + } + return possibleSpeed((int) left, piles, h) ? (int) left : (int) (left + 1); + } + + private boolean possibleSpeed(int speed, int[] piles, int hour) { + if (speed <= 0) { + return false; + } + long usedHours = 0; + for (int pile : piles) { + if (pile <= speed) { + usedHours++; + } else { + usedHours += pile / speed; + pile %= speed; + usedHours += pile > 0 ? 1 : 0; + } + } + return usedHours <= hour; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_876.java b/src/main/java/com/fishercoder/solutions/firstthousand/_876.java new file mode 100644 index 0000000000..da9152c05e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_876.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _876 { + public static class Solution1 { + public ListNode middleNode(ListNode head) { + ListNode fast = head; + ListNode slow = head; + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + } + return slow; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_877.java b/src/main/java/com/fishercoder/solutions/firstthousand/_877.java new file mode 100644 index 0000000000..27758b1dc3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_877.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _877 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/stone-game/discuss/154660/Java-This-is-minimax-%2B-dp-(fully-detailed-explanation-%2B-generalization-%2B-easy-understand-code) + *

+ * Suppose the ID for Alex is 1, that for Lee is 0 + * Alex wants to maximize the score to win while Lee wants to minimize the score to win. + * Each time, each player has two options to pick, we'll use recursion to find the most optimal choice for each of them. + */ + public boolean stoneGame(int[] piles) { + int len = piles.length; + int[][][] dp = new int[len + 1][len + 1][2]; + for (int[][] arr : dp) { + for (int[] num : arr) { + Arrays.fill(num, -1); + } + } + return recursion(dp, 0, len - 1, 1, piles) > 0; + } + + private int recursion(int[][][] dp, int left, int right, int identifier, int[] piles) { + if (left > right) { + return 0; + } + if (dp[left][right][identifier] != -1) { + return dp[left][right][identifier]; + } + int next = Math.abs(identifier - 1); + if (identifier == 1) { + dp[left][right][identifier] = + Math.max( + piles[left] + recursion(dp, left + 1, right, next, piles), + piles[right] + recursion(dp, left, right - 1, next, piles)); + } else { + dp[left][right][identifier] = + Math.min( + -piles[left] + recursion(dp, left + 1, right, next, piles), + -piles[right] + recursion(dp, left, right - 1, next, piles)); + } + return dp[left][right][identifier]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_88.java b/src/main/java/com/fishercoder/solutions/firstthousand/_88.java similarity index 75% rename from src/main/java/com/fishercoder/solutions/_88.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_88.java index c514539fb1..031b235f0b 100644 --- a/src/main/java/com/fishercoder/solutions/_88.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_88.java @@ -1,8 +1,11 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _88 { public static class Solution1 { + /* + * The key to reach this optimal solution is: start from the right side instead of the left. + */ public void merge(int[] nums1, int m, int[] nums2, int n) { int i = m - 1; int j = n - 1; diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_880.java b/src/main/java/com/fishercoder/solutions/firstthousand/_880.java new file mode 100644 index 0000000000..c8e41d8345 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_880.java @@ -0,0 +1,10 @@ +package com.fishercoder.solutions.firstthousand; + +public class _880 { + public static class Solution1 { + public String decodeAtIndex(String S, int K) { + // TODO: implement it + return ""; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_881.java b/src/main/java/com/fishercoder/solutions/firstthousand/_881.java new file mode 100644 index 0000000000..7e0704d032 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_881.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _881 { + public static class Solution1 { + public int numRescueBoats(int[] people, int limit) { + TreeMap map = new TreeMap<>(); + for (int w : people) { + map.put(w, map.getOrDefault(w, 0) + 1); + } + int boats = 0; + List uniqWeights = new ArrayList(map.keySet()); + int left = 0; + int right = uniqWeights.size() - 1; + while (left < right) { + int heavierWeight = uniqWeights.get(right); + int lighterWeight = uniqWeights.get(left); + if (heavierWeight + lighterWeight <= limit) { + int pairs = Math.min(map.get(heavierWeight), map.get(lighterWeight)); + boats += pairs; + if (map.get(heavierWeight) == pairs && map.get(lighterWeight) == pairs) { + map.remove(heavierWeight); + map.remove(lighterWeight); + left++; + right--; + } else if (map.get(heavierWeight) == pairs) { + map.remove(heavierWeight); + map.put(lighterWeight, map.get(lighterWeight) - pairs); + right--; + } else { + map.remove(lighterWeight); + map.put(heavierWeight, map.get(heavierWeight) - pairs); + left++; + } + } else { + boats += map.get(heavierWeight); + map.remove(heavierWeight); + right--; + } + } + if (!map.isEmpty()) { + int weight = uniqWeights.get(left); + int remainingPeople = map.get(weight); + if (remainingPeople == 1) { + boats++; + } else { + if (weight * 2 <= limit) { + boats += (remainingPeople / 2 + ((remainingPeople % 2 == 0) ? 0 : 1)); + } else { + boats += remainingPeople; + } + } + } + return boats; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_883.java b/src/main/java/com/fishercoder/solutions/firstthousand/_883.java new file mode 100644 index 0000000000..f046c0fd60 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_883.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +public class _883 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/projection-area-of-3d-shapes/discuss/156726/C%2B%2BJavaPython-Straight-Forward-One-Pass + */ + public int projectionArea(int[][] grid) { + int n = grid.length; + int area = 0; + for (int i = 0; i < n; i++) { + int x = 0; + int y = 0; + for (int j = 0; j < n; j++) { + x = Math.max(x, grid[i][j]); + y = Math.max(x, grid[j][i]); + if (grid[i][j] > 0) { + area++; + } + } + area += x + y; + } + return area; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_884.java b/src/main/java/com/fishercoder/solutions/firstthousand/_884.java new file mode 100644 index 0000000000..903888a5af --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_884.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _884 { + public static class Solution1 { + public String[] uncommonFromSentences(String A, String B) { + Map map = new HashMap<>(); + for (String word : A.split(" ")) { + map.put(word, map.getOrDefault(word, 0) + 1); + } + + for (String word : B.split(" ")) { + map.put(word, map.getOrDefault(word, 0) + 1); + } + List result = new ArrayList<>(); + for (String key : map.keySet()) { + if (map.get(key) == 1) { + result.add(key); + } + } + String[] strs = new String[result.size()]; + result.toArray(strs); + return strs; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_885.java b/src/main/java/com/fishercoder/solutions/firstthousand/_885.java new file mode 100644 index 0000000000..37d24310e5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_885.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.firstthousand; + +public class _885 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/spiral-matrix-iii/discuss/158977/Java-15-lines-concise-solution-with-comments + */ + public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) { + int[] directions = new int[] {0, 1, 0, -1, 0}; + int[][] result = new int[rows * cols][2]; + int i = 0; + result[i++] = new int[] {rStart, cStart}; + int len = 0; + int d = 0; + while (i < rows * cols) { + if (d == 0 || d == 2) { + // plus one when moving east or west + len++; + } + for (int k = 0; k < len; k++) { + rStart += directions[d]; + cStart += directions[d + 1]; + if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) { + result[i++] = new int[] {rStart, cStart}; + } + } + d = (d + 1) % 4; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_888.java b/src/main/java/com/fishercoder/solutions/firstthousand/_888.java new file mode 100644 index 0000000000..4eee87019b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_888.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; + +public class _888 { + public static class Solution1 { + public int[] fairCandySwap(int[] A, int[] B) { + int aSum = 0; + int bSum = 0; + int diff = 0; + int[] ans = new int[2]; + for (int bar : A) { + aSum += bar; + } + for (int bar : B) { + bSum += bar; + } + diff = aSum - bSum; + HashSet set = new HashSet<>(); + for (int bar : A) { + set.add(bar); + } + for (int bar : B) { + if (set.contains(bar + diff / 2)) { + ans[0] = bar + diff / 2; + ans[1] = bar; + break; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_89.java b/src/main/java/com/fishercoder/solutions/firstthousand/_89.java new file mode 100644 index 0000000000..02aff4af3f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_89.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _89 { + + public static class Solution1 { + public List grayCode(int n) { + List result = new ArrayList(); + for (int i = 0; i < (1 << n); i++) { + result.add(i ^ (i >> 1)); + } + return result; + } + } + + public static class Solution2 { + public List grayCode(int n) { + List result = new ArrayList(); + for (int i = 0; i < Math.pow(2, n); i++) { + result.add(i ^ (i >> 1)); + } + return result; + } + } + + public static void main(String... args) { + System.out.println( + "-----------------------------------------------------------------------------------------"); + System.out.println( + "How to understand i << n? It means n to the power of two, see below. So we have an equivalent solution, which is solution2."); + System.out.println("1 << 2: " + (1 << 2)); + System.out.println("1 << 3: " + (1 << 3)); + System.out.println("1 << 4: " + (1 << 4)); + System.out.println("1 << 5: " + (1 << 5)); + System.out.println("1 << 6: " + (1 << 6)); + System.out.println( + "-----------------------------------------------------------------------------------------"); + System.out.println( + "How to understand i >> 1? It means to shift the number i to the right by 1 bit, see below"); + System.out.println("0 >> 1: " + (0 >> 1)); + System.out.println("1 >> 1: " + (1 >> 1)); + System.out.println("2 >> 1: " + (2 >> 1)); + System.out.println("3 >> 1: " + (3 >> 1)); + System.out.println("4 >> 1: " + (4 >> 1)); + System.out.println("5 >> 1: " + (5 >> 1)); + System.out.println("6 >> 1: " + (6 >> 1)); + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_890.java b/src/main/java/com/fishercoder/solutions/firstthousand/_890.java new file mode 100644 index 0000000000..744f6624f0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_890.java @@ -0,0 +1,72 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class _890 { + public static class Solution1 { + public List findAndReplacePattern(String[] words, String pattern) { + List result = new ArrayList<>(); + for (String word : words) { + Map map = new HashMap<>(); + Set set = new HashSet<>(); + boolean match = true; + for (int i = 0; i < pattern.length(); i++) { + if (map.containsKey(pattern.charAt(i))) { + if (word.charAt(i) != map.get(pattern.charAt(i))) { + match = false; + break; + } + } else { + map.put(pattern.charAt(i), word.charAt(i)); + if (!set.add(word.charAt(i))) { + match = false; + } + } + } + if (match) { + result.add(word); + } + } + return result; + } + } + + public static class Solution2 { + public List findAndReplacePattern(String[] words, String pattern) { + List result = new ArrayList<>(); + for (String word : words) { + if (matches(word, pattern)) { + result.add(word); + } + } + return result; + } + + private boolean matches(String word, String pattern) { + Map map1 = new HashMap<>(); // word -> p + Map map2 = new HashMap<>(); // p -> word + for (int i = 0; i < pattern.length(); i++) { + if (!map1.containsKey(word.charAt(i))) { + map1.put(word.charAt(i), pattern.charAt(i)); + } + if (map1.containsKey(word.charAt(i)) + && map1.get(word.charAt(i)) != pattern.charAt(i)) { + return false; + } + if (!map2.containsKey(pattern.charAt(i))) { + map2.put(pattern.charAt(i), word.charAt(i)); + } + if (map2.containsKey(pattern.charAt(i)) + && map2.get(pattern.charAt(i)) != word.charAt(i)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_892.java b/src/main/java/com/fishercoder/solutions/firstthousand/_892.java new file mode 100644 index 0000000000..17c992ac13 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_892.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.firstthousand; + +public class _892 { + public static class Solution1 { + /* + * It's the way that you approach a problem like this matters. This is why we practice LeetCode - train your thought process, i.e. how do you approach a seemingly complex problem. + *

+ * Inspired by: https://leetcode.com/problems/surface-area-of-3d-shapes/discuss/163414/C%2B%2BJava1-line-Python-Minus-Hidden-Area + *

+ * Idea is: + * 1. Each tower's surface is 4*height + 2, because each tower has 6 surfaces: 4 standing ones and 1 on the top and 1 on the bottom; + * 2. For the adjacent areas between two towers, we can deduct the overlapped area by using the smaller area * 2; + * 3. How to achieve #2, for each tower, we can its against rightside neighbor, and then check against its bottom neighbor, this will cover all. + * Of course, the three for loops could be combined into one, I put it like this for easier understanding. + */ + public int surfaceArea(int[][] grid) { + int area = 0; + int m = grid.length; + int n = grid[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] != 0) { + area += (grid[i][j] * 4 + 2); + } + } + } + // check its right side neighbors + for (int i = 0; i < m; i++) { + for (int j = 0; j < n - 1; j++) { + if (grid[i][j] != 0 && grid[i][j + 1] != 0) { + area -= 2 * Math.min(grid[i][j], grid[i][j + 1]); + } + } + } + // check its downside neighbors + for (int i = 0; i < m - 1; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] != 0 && grid[i + 1][j] != 0) { + area -= 2 * Math.min(grid[i][j], grid[i + 1][j]); + } + } + } + return area; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_893.java b/src/main/java/com/fishercoder/solutions/firstthousand/_893.java new file mode 100644 index 0000000000..4e63033b6c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_893.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +public class _893 { + + public static class Solution1 { + /* + * my original solution, a bit lengthy: + * generate a unique signaure as key for each equivelant group and sum them up + */ + public int numSpecialEquivGroups(String[] A) { + return Arrays.stream(A).map(this::getCommonKey).collect(Collectors.toSet()).size(); + } + + private String getCommonKey(String word) { + char[] oddIndexed = new char[word.length() / 2]; + char[] evenIndexed = new char[word.length() / 2 + (word.length() % 2 == 0 ? 0 : 1)]; + char[] array = word.toCharArray(); + for (int i = 0; i < array.length - 1; i += 2) { + evenIndexed[i / 2] = array[i]; + oddIndexed[i / 2] = array[i + 1]; + } + if (word.length() % 2 != 0) { + evenIndexed[evenIndexed.length - 1] = array[array.length - 1]; + } + Arrays.sort(oddIndexed); + Arrays.sort(evenIndexed); + return new StringBuffer() + .append(new String(evenIndexed)) + .append(new String(oddIndexed)) + .toString(); + } + } + + public static class Solution2 { + /* + * more concise solution: https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163413/Java-Concise-Set-Solution + * but somehow a bit slower than mine: 12 ms vs 7ms + * I guess due to the problem constraint and this: "1 <= A[i].length <= 20" to have made this problem simpler + */ + public int numSpecialEquivGroups(String[] A) { + Set set = new HashSet<>(); + for (String str : A) { + int[] odd = new int[26]; + int[] even = new int[26]; + for (int i = 0; i < str.length(); i++) { + if (i % 2 == 0) { + even[str.charAt(i) - 'a']++; + } else { + odd[str.charAt(i) - 'a']++; + } + } + String key = Arrays.toString(even) + Arrays.toString(odd); + set.add(key); + } + return set.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_895.java b/src/main/java/com/fishercoder/solutions/firstthousand/_895.java new file mode 100644 index 0000000000..d6c7d00983 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_895.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; + +public class _895 { + public static class Solution1 { + public static class FreqStack { + int counter; + PriorityQueue maxHeap; + Map map; + + public FreqStack() { + maxHeap = new PriorityQueue<>((a, b) -> a[1] != b[1] ? b[1] - a[1] : b[2] - a[2]); + map = new HashMap<>(); + } + + public void push(int x) { + map.put(x, map.getOrDefault(x, 0) + 1); + maxHeap.offer(new int[] {x, map.get(x), counter++}); + } + + public int pop() { + int[] top = maxHeap.poll(); + map.put(top[0], map.get(top[0]) - 1); + return top[0]; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_896.java b/src/main/java/com/fishercoder/solutions/firstthousand/_896.java new file mode 100644 index 0000000000..1292d67060 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_896.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +public class _896 { + public static class Solution1 { + public boolean isMonotonic(int[] nums) { + int i = 0; + // check if it's increasing + for (; i < nums.length - 1; i++) { + if (nums[i] > nums[i + 1]) { + break; + } + } + if (i == nums.length - 1) { + return true; + } + i = 0; + // check if it's decreasing + for (; i < nums.length - 1; i++) { + if (nums[i] < nums[i + 1]) { + break; + } + } + return i == nums.length - 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_897.java b/src/main/java/com/fishercoder/solutions/firstthousand/_897.java new file mode 100644 index 0000000000..8f28fc25ad --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_897.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _897 { + public static class Solution1 { + public TreeNode increasingBST(TreeNode root) { + List inorderList = new ArrayList<>(); + inorderTraversal(root, inorderList); + return constructTree(inorderList); + } + + private TreeNode constructTree(List inorderList) { + if (inorderList.isEmpty() || inorderList.size() == 0) { + return null; + } + TreeNode root = new TreeNode(inorderList.get(0)); + TreeNode tmp = root; + for (int i = 1; i < inorderList.size(); i++) { + tmp.right = new TreeNode(inorderList.get(i)); + tmp = tmp.right; + } + return root; + } + + private void inorderTraversal(TreeNode root, List inorderList) { + if (root == null) { + return; + } + if (root.left != null) { + inorderTraversal(root.left, inorderList); + } + inorderList.add(root.val); + if (root.right != null) { + inorderTraversal(root.right, inorderList); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_9.java b/src/main/java/com/fishercoder/solutions/firstthousand/_9.java similarity index 79% rename from src/main/java/com/fishercoder/solutions/_9.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_9.java index aa5d924f8c..73195729ae 100644 --- a/src/main/java/com/fishercoder/solutions/_9.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_9.java @@ -1,7 +1,7 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _9 { - /**credit: https://discuss.leetcode.com/topic/8090/9-line-accepted-java-code-without-the-need-of-handling-overflow + /*credit: https://discuss.leetcode.com/topic/8090/9-line-accepted-java-code-without-the-need-of-handling-overflow * reversing only half and then compare if they're equal.*/ public static class Solution1 { public boolean isPalindrome(int x) { @@ -22,5 +22,4 @@ public boolean isPalindrome(int x) { return (x == reversed || x == reversed / 10); } } - } diff --git a/src/main/java/com/fishercoder/solutions/_90.java b/src/main/java/com/fishercoder/solutions/firstthousand/_90.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_90.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_90.java index 481a600c07..031658a3ae 100644 --- a/src/main/java/com/fishercoder/solutions/_90.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_90.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.Arrays; @@ -65,7 +65,8 @@ public List> subsetsWithDup(int[] nums) { return result; } - private void backtracking(int[] nums, int start, List> result, List list) { + private void backtracking( + int[] nums, int start, List> result, List list) { for (int i = start; i < nums.length; i++) { if (i > start && nums[i] == nums[i - 1]) { continue; diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_900.java b/src/main/java/com/fishercoder/solutions/firstthousand/_900.java new file mode 100644 index 0000000000..2c1fba7a02 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_900.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +public class _900 { + public static class Solution1 { + public static class RLEIterator { + + int index; + int[] array; + + public RLEIterator(int[] A) { + index = 0; + array = A; + } + + public int next(int n) { + int lastElement = -1; + while (n > 0 && index < array.length) { + if (array[index] > n) { + array[index] -= n; + lastElement = array[index + 1]; + break; + } else if (array[index] == n) { + array[index] = 0; + lastElement = array[index + 1]; + index += 2; + break; + } else { + n -= array[index]; + index += 2; + } + } + return lastElement; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_901.java b/src/main/java/com/fishercoder/solutions/firstthousand/_901.java new file mode 100644 index 0000000000..0cebda0e45 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_901.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Stack; + +public class _901 { + public static class Solution1 { + public static class StockSpanner { + Stack stack; + + public StockSpanner() { + stack = new Stack<>(); + } + + public int next(int price) { + int result = 1; + while (!stack.isEmpty() && stack.peek()[0] <= price) { + result += stack.pop()[1]; + } + stack.push(new int[] {price, result}); + return result; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_904.java b/src/main/java/com/fishercoder/solutions/firstthousand/_904.java new file mode 100644 index 0000000000..de833cfc6b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_904.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _904 { + public static class Solution1 { + /* + * This is a two-pointer solution, a.k.a sliding window + */ + public int totalFruit(int[] fruits) { + int maxFruits = 0; + Set set = new HashSet<>(); + int startIndex = 0; + for (int i = 0; i < fruits.length; i++) { + if (set.size() < 2 || set.contains(fruits[i])) { + // either one of the two cases, we keep adding fruits[i] into the set and expand + // i to the right + } else { + // in other cases, we know there's a 3rd type of fruit we just encountered, + // so keep the 2nd type of fruit as lastOne, go backwards, + // find the first 1st type of fruit as j, set startIndex = j + 1, + // remove the 1st type of fruit from the set and break + int lastOne = fruits[i - 1]; + for (int j = i - 2; j >= 0; j--) { + if (fruits[j] != lastOne) { + startIndex = j + 1; + set.remove(fruits[j]); + break; + } + } + } + set.add(fruits[i]); + maxFruits = Math.max(maxFruits, i - startIndex + 1); + } + return maxFruits; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_905.java b/src/main/java/com/fishercoder/solutions/firstthousand/_905.java new file mode 100644 index 0000000000..e04590d8c6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_905.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +public class _905 { + public static class Solution1 { + public int[] sortArrayByParity(int[] A) { + for (int i = 0, j = A.length - 1; i < j; i++) { + if (A[i] % 2 == 0) { + continue; + } else { + while (j > i && A[j] % 2 != 0) { + j--; + } + swap(A, i, j); + } + } + return A; + } + + private void swap(int[] A, int i, int j) { + int tmp = A[i]; + A[i] = A[j]; + A[j] = tmp; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_908.java b/src/main/java/com/fishercoder/solutions/firstthousand/_908.java new file mode 100644 index 0000000000..ddb8ffdbdd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_908.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _908 { + public static class Solution1 { + public int smallestRangeI(int[] A, int K) { + Arrays.sort(A); + int smallestPlus = A[0] + K; + int biggestMinus = A[A.length - 1] - K; + int diff = biggestMinus - smallestPlus; + if (diff > 0) { + return diff; + } else { + return 0; + } + } + } + + public static class Solution2 { + public int smallestRangeI(int[] A, int K) { + int min = A[0]; + int max = A[0]; + + for (int k : A) { + min = Math.min(min, k); + max = Math.max(max, k); + } + + return Math.max(max - min - 2 * K, 0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_91.java b/src/main/java/com/fishercoder/solutions/firstthousand/_91.java new file mode 100644 index 0000000000..a2aa585a4b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_91.java @@ -0,0 +1,73 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _91 { + /* + * Credit: https://discuss.leetcode.com/topic/35840/java-clean-dp-solution-with-explanation + * I used a dp array of size n + 1 to save subproblem solutions. + * dp[0] means an empty string will have one way to decode, + * dp[1] means the way to decode a string of size 1. + * I then check one digit and two digit combination and save the results along the way. + * In the end, dp[n] will be the end result. + */ + + public static class Solution1 { + public int numDecodings(String s) { + if (s == null || s.length() == 0) { + return 0; + } + int[] dp = new int[s.length() + 1]; + dp[0] = 1; + dp[1] = (s.charAt(0) != '0') ? 1 : 0; + for (int i = 2; i <= s.length(); i++) { + int first = Integer.valueOf(s.substring(i - 1, i)); + int second = Integer.valueOf(s.substring(i - 2, i)); + if (first > 0 && first <= 9) { + dp[i] += dp[i - 1]; + } + if (second >= 10 && second <= 26) { + dp[i] += dp[i - 2]; + } + } + return dp[s.length()]; + } + } + + public static class Solution2 { + /*credit: https://leetcode.com/problems/decode-ways/solution/ + * Approach 1: Recursive Approach with Memoization + * + * The actual code goes from the right most character to the left side to build out the dp cache map. + * And this HashMap uses index as its key instead of a substring. + * */ + + public int numDecodings(String s) { + return dp(new HashMap<>(), s, 0); + } + + private int dp(Map cache, String s, int index) { + if (cache.containsKey(index)) { + return cache.get(index); + } + if (index == s.length()) { + // this means we reached the end of the string, so return 1 as success + return 1; + } + if (s.charAt(index) == '0') { + // this means this string cannot be decoded + return 0; + } + if (index == s.length() - 1) { + return 1; + } + int ways = dp(cache, s, index + 1); + if (Integer.parseInt(s.substring(index, index + 2)) <= 26) { + ways += dp(cache, s, index + 2); + } + cache.put(index, ways); + return cache.get(index); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_912.java b/src/main/java/com/fishercoder/solutions/firstthousand/_912.java new file mode 100644 index 0000000000..43b60a2644 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_912.java @@ -0,0 +1,62 @@ +package com.fishercoder.solutions.firstthousand; + +public class _912 { + public static class Solution1 { + /* + * Implementation of MergeSort which is a stable sort, unlike QuickSort which isn't. + */ + public int[] sortArray(int[] nums) { + // use a helper function to take in two additional parameters for the ease of recursion + return sort(nums, 0, nums.length - 1); + } + + // this is the recursive function + private int[] sort(int[] nums, int left, int right) { + // this condition keeps dividing the array until nums becomes one individual item and + // then it goes back to the call stack + if (left < right) { + int mid = left + (right - left) / 2; + sort(nums, left, mid); + sort(nums, mid + 1, right); + merge(nums, left, mid, right); + } + return nums; + } + + private void merge(int[] nums, int left, int mid, int right) { + int leftSize = mid - left + 1; + int rightSize = right - mid; + // use two temp array to copy the original values in the input before we overwrite them + int[] leftHalf = new int[leftSize]; + int[] rightHalf = new int[rightSize]; + for (int i = 0; i < leftSize; i++) { + // this index is key: it should be nums[left + i] as it should start from left + // instead of zero + leftHalf[i] = nums[left + i]; + } + for (int i = 0; i < rightSize; i++) { + // similarly, this index is key as well: it should be nums[mid + i + 1] instead of + // starting from zero + rightHalf[i] = nums[mid + i + 1]; + } + int i = 0; + int j = 0; + // again, this index k = left is key, it should start from left instead of 0 + int k = left; + while (i < leftSize && j < rightSize) { + if (leftHalf[i] < rightHalf[j]) { + nums[k++] = leftHalf[i++]; + } else { + nums[k++] = rightHalf[j++]; + } + } + + while (i < leftSize) { + nums[k++] = leftHalf[i++]; + } + while (j < rightSize) { + nums[k++] = rightHalf[j++]; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_914.java b/src/main/java/com/fishercoder/solutions/firstthousand/_914.java new file mode 100644 index 0000000000..bcecebb55e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_914.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _914 { + public static class Solution1 { + public boolean hasGroupsSizeX(int[] deck) { + // Size too small for partitions + if (deck.length < 2) { + return false; + } + + // Track repetitions of values in deck array + Map mapReps = new HashMap<>(); + for (int card : deck) { + if (!mapReps.containsKey(card)) { + mapReps.put(card, 1); + } else { + mapReps.put(card, mapReps.get(card) + 1); + } + } + + // Create array of map values + int num = 0; + int[] arrReps = new int[mapReps.size()]; + for (Map.Entry e : mapReps.entrySet()) { + arrReps[num++] = e.getValue(); + } + + // Find greatest common denominator + num = arrGCD(arrReps, arrReps.length); + + // If gcd of all repetitions is greater than 1, it's partitionable. + return num > 1; + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } + + private int arrGCD(int[] arr, int n) { + int result = arr[0]; + for (int i = 1; i < n; i++) { + result = gcd(arr[i], result); + } + + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_917.java b/src/main/java/com/fishercoder/solutions/firstthousand/_917.java new file mode 100644 index 0000000000..9fe9067f5d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_917.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +public class _917 { + public static class Solution1 { + public String reverseOnlyLetters(String S) { + char[] array = S.toCharArray(); + for (int i = 0, j = array.length - 1; i < j; ) { + if (Character.isLetter(array[i]) && Character.isLetter(array[j])) { + char temp = array[i]; + array[i++] = array[j]; + array[j--] = temp; + } else if (Character.isLetter(array[i])) { + j--; + } else if (Character.isLetter(array[j])) { + i++; + } else { + i++; + j--; + } + } + return new String(array); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_918.java b/src/main/java/com/fishercoder/solutions/firstthousand/_918.java new file mode 100644 index 0000000000..1c8c247089 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_918.java @@ -0,0 +1,83 @@ +package com.fishercoder.solutions.firstthousand; + +public class _918 { + public static class Solution1 { + /* + * This is my original solution, but results in TLE on LeetCode. + * Time: O(n^2) + */ + public int maxSubarraySumCircular(int[] nums) { + int[] prefixSums; + int maxSum = Integer.MIN_VALUE; + for (int i = 0; i < nums.length; i++) { + prefixSums = new int[nums.length]; + for (int j = i, k = 0; j < (i + nums.length); j++) { + if (k == 0) { + prefixSums[k] = nums[(j + nums.length) % nums.length]; + } else { + prefixSums[k] = prefixSums[k - 1] + nums[(j + nums.length) % nums.length]; + } + maxSum = Math.max(maxSum, prefixSums[k++]); + } + } + return maxSum; + } + } + + public static class Solution2 { + /* + * Credit: https://leetcode.com/problems/maximum-sum-circular-subarray/discuss/178422/One-Pass + * Think of two cases: + * 1. the max comes from the contiguous part of the original array + * 2. the max comes from connecting the tail part and the head part of the original array. + * See graph from the above link. + *

+ * Time: O(n) + *

+ * This is a follow-up from https://leetcode.com/problems/maximum-subarray/ which is solved by Kadane's algorithm. + */ + public int maxSubarraySumCircular(int[] nums) { + int currMax = 0; + int globalMax = nums[0]; + int currMin = 0; + int globalMin = nums[0]; + int total = 0; + for (int i = 0; i < nums.length; i++) { + currMax = Math.max(nums[i], currMax + nums[i]); + globalMax = Math.max(globalMax, currMax); + currMin = Math.min(currMin + nums[i], nums[i]); + globalMin = Math.min(currMin, globalMin); + total += nums[i]; + } + return globalMax > 0 ? Math.max(globalMax, total - globalMin) : globalMax; + } + } + + public static class Solution3 { + /* + * Credit: https://leetcode.com/problems/maximum-sum-circular-subarray/discuss/633058/Java-or-C%2B%2B-or-Python3-or-With-detailed-explanation-or-O(N)-time-or-O(1) + * This one is similar to the above Solution2, but only slightly differs in that it starts from i = 1 instead of i = 0 + * And it listed out a few examples to help illustrate why this algorithm makes sense. + * Which I think is easier to make sense of. + *

+ * Time: O(n) + *

+ * This is a follow-up from https://leetcode.com/problems/maximum-subarray/ which is solved by Kadane's algorithm. + */ + public int maxSubarraySumCircular(int[] nums) { + int currMax = nums[0]; + int globalMax = nums[0]; + int currMin = nums[0]; + int globalMin = nums[0]; + int total = nums[0]; + for (int i = 1; i < nums.length; i++) { + currMax = Math.max(nums[i], currMax + nums[i]); + globalMax = Math.max(globalMax, currMax); + currMin = Math.min(currMin + nums[i], nums[i]); + globalMin = Math.min(currMin, globalMin); + total += nums[i]; + } + return globalMax > 0 ? Math.max(globalMax, total - globalMin) : globalMax; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_919.java b/src/main/java/com/fishercoder/solutions/firstthousand/_919.java new file mode 100644 index 0000000000..9d18f99b5c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_919.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; + +public class _919 { + public static class Solution1 { + /* + * My completely original solution. + * Beats 98.11% submissions. + */ + public static class CBTInserter { + private Map indexMap; + private int index; + private TreeNode root; + + public CBTInserter(TreeNode root) { + this.indexMap = new HashMap<>(); + this.index = 1; + this.root = root; + Queue q = new LinkedList<>(); + q.offer(root); + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = q.poll(); + indexMap.put(index++, curr); + if (curr.left != null) { + q.offer(curr.left); + } + if (curr.right != null) { + q.offer(curr.right); + } + } + } + } + + public int insert(int val) { + int parentIndex = index / 2; + TreeNode parentNode = indexMap.get(parentIndex); + TreeNode childNode = new TreeNode(val); + if (index % 2 == 0) { + parentNode.left = childNode; + } else { + parentNode.right = childNode; + } + indexMap.put(index++, childNode); + indexMap.put(parentIndex, parentNode); + return parentNode.val; + } + + public TreeNode get_root() { + return root; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_92.java b/src/main/java/com/fishercoder/solutions/firstthousand/_92.java new file mode 100644 index 0000000000..672aadc031 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_92.java @@ -0,0 +1,75 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _92 { + + public static class Solution1 { + /* + * credit: https://discuss.leetcode.com/topic/8976/simple-java-solution-with-clear-explanation + */ + public ListNode reverseBetween(ListNode head, int m, int n) { + // use four nodes, pre, start, then, dummy + // just reverse the nodes along the way + ListNode dummy = new ListNode(-1); + dummy.next = head; + ListNode pre = dummy; + for (int i = 0; i < m - 1; i++) { + pre = pre.next; + } + + ListNode start = + pre.next; // start is the node prior to reversing, in the given example, + // start is node with value 1 + ListNode then = + start.next; // then is the node that we'll start to reverse, in the given + // example, it's 2 + + for (int i = 0; i < n - m; i++) { + // pay special attention to this for loop, it's assigning then.next to start.next, + // it + // didn't initialize a new node + // this does exactly what I desired to do, but I just didn't figure out how to + // implement + // it, thumbs up to the OP! + start.next = then.next; + then.next = pre.next; + pre.next = then; + then = start.next; + } + return dummy.next; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/25/2021. + */ + public ListNode reverseBetween(ListNode head, int left, int right) { + ListNode pre = new ListNode(-1); + pre.next = head; + ListNode tmp = pre; + right -= left; + while (left-- > 1 && tmp != null) { + tmp = tmp.next; + } + ListNode intersection = tmp; + ListNode reverseHead = tmp.next; + ListNode prev = null; + right++; + int original = right; + while (right-- > 0 && reverseHead != null) { + ListNode next = reverseHead.next; + reverseHead.next = prev; + prev = reverseHead; + reverseHead = next; + } + intersection.next = prev; + while (original-- > 0) { + intersection = intersection.next; + } + intersection.next = reverseHead; + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_921.java b/src/main/java/com/fishercoder/solutions/firstthousand/_921.java new file mode 100644 index 0000000000..78e0aeb844 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_921.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _921 { + public static class Solution1 { + public int minAddToMakeValid(String s) { + Deque stack = new LinkedList<>(); + for (char c : s.toCharArray()) { + if (c == ')') { + if (!stack.isEmpty() && stack.peekLast() == '(') { + stack.pollLast(); + } else { + stack.addLast(c); + } + } else { + stack.addLast(c); + } + } + return stack.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_922.java b/src/main/java/com/fishercoder/solutions/firstthousand/_922.java new file mode 100644 index 0000000000..bf4aa491e8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_922.java @@ -0,0 +1,93 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _922 { + public static class Solution1 { + /* + * Space: O(n) + * Time: O(n) + */ + public int[] sortArrayByParityII(int[] nums) { + List odds = new ArrayList<>(); + List evens = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 2 == 0) { + evens.add(nums[i]); + } else { + odds.add(nums[i]); + } + } + int oddIndex = 0; + int evenIndex = 0; + for (int i = 0; i < nums.length; i++) { + if (i % 2 == 0) { + nums[i] = evens.get(evenIndex++); + } else { + nums[i] = odds.get(oddIndex++); + } + } + return nums; + } + } + + public static class Solution2 { + /* + * Space: O(1) + * Time: O(n^2) + */ + public int[] sortArrayByParityII(int[] nums) { + for (int i = 0; i < nums.length; i++) { + if (i % 2 == 0 && nums[i] % 2 != 0) { + for (int j = i + 1; j < nums.length; j++) { + if (j % 2 != 0 && nums[j] % 2 == 0) { + int tmp = nums[j]; + nums[j] = nums[i]; + nums[i] = tmp; + break; + } + } + } else if (i % 2 != 0 && nums[i] % 2 == 0) { + for (int j = i + 1; j < nums.length; j++) { + if (j % 2 == 0 && nums[j] % 2 != 0) { + int tmp = nums[j]; + nums[j] = nums[i]; + nums[i] = tmp; + break; + } + } + } + } + return nums; + } + } + + public static class Solution3 { + /* + * This is the most efficient solution: one implicit condition is that: + * we start with index zero for i, so we look for nums[i] that is not an even number to be swapped with; + * we start with index one for j, so we look for nums[j] that is not an odd number to be swapped with. + * Time: O(n) + * Space: O(1) + */ + public int[] sortArrayByParityII(int[] nums) { + for (int i = 0, j = 1; i < nums.length - 1 && j < nums.length; ) { + if (nums[i] % 2 != 0 && nums[j] % 2 == 0) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + i += 2; + j += 2; + } + while (i < nums.length - 1 && nums[i] % 2 == 0) { + i += 2; + } + while (j < nums.length && nums[j] % 2 != 0) { + j += 2; + } + } + return nums; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_923.java b/src/main/java/com/fishercoder/solutions/firstthousand/_923.java new file mode 100644 index 0000000000..f4dd26f2a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_923.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _923 { + public static class Solution1 { + public int threeSumMulti(int[] A, int target) { + int MOD = 1000000007; + Map map = new HashMap<>(); + for (int num : A) { + map.put(num, map.getOrDefault(num, 0L) + 1); + } + long result = 0; + for (int num1 : map.keySet()) { + for (int num2 : map.keySet()) { + int num3 = target - num1 - num2; + if (map.containsKey(num3)) { + Long count1 = map.get(num1); + Long count2 = map.get(num2); + Long count3 = map.get(num3); + if (num1 == num2 && num1 == num3) { + result = (result + (count1 * (count1 - 1) * (count1 - 2) / 6)) % MOD; + } else if (num1 == num2 && num1 != num3) { + result = (result + (count1 * (count1 - 1) / 2) * count3) % MOD; + } else if (num1 < num2 && num2 < num3) { + result = (result + count1 * count2 * count3) % MOD; + } + } + } + } + return (int) result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_925.java b/src/main/java/com/fishercoder/solutions/firstthousand/_925.java new file mode 100644 index 0000000000..d16c2be76d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_925.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.firstthousand; + +public class _925 { + public static class Solution1 { + public boolean isLongPressedName(String name, String typed) { + int i = 0; + for (int j = 0; i < name.length() && j < typed.length(); i++) { + if (name.charAt(i) != typed.charAt(j)) { + return false; + } else if (i < name.length() - 1 && name.charAt(i) != name.charAt(i + 1)) { + j++; + while (j < typed.length() && name.charAt(i) == typed.charAt(j)) { + j++; + } + } else { + j++; + } + } + return i == name.length(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_929.java b/src/main/java/com/fishercoder/solutions/firstthousand/_929.java new file mode 100644 index 0000000000..6cfaabc419 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_929.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _929 { + public static class Solution1 { + public int numUniqueEmails(String[] emails) { + Map> map = new HashMap<>(); + for (String email : emails) { + String[] parts = email.split("@"); + if (!map.containsKey(parts[1])) { + map.put(parts[1], new HashSet<>()); + } + String filteredLocalName = parts[0].substring(0, parts[0].indexOf('+')); + filteredLocalName = filteredLocalName.replace(".", ""); + map.get(parts[1]).add(filteredLocalName); + } + return map.keySet().stream().mapToInt(key -> map.get(key).size()).sum(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_93.java b/src/main/java/com/fishercoder/solutions/firstthousand/_93.java similarity index 97% rename from src/main/java/com/fishercoder/solutions/_93.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_93.java index bd4e91efae..c6bf715168 100644 --- a/src/main/java/com/fishercoder/solutions/_93.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_93.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_931.java b/src/main/java/com/fishercoder/solutions/firstthousand/_931.java new file mode 100644 index 0000000000..7ed6013e2d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_931.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.firstthousand; + +public class _931 { + public static class Solution1 { + public int minFallingPathSum(int[][] A) { + int size = A.length; + int[][] dp = new int[size][size]; + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + if (i == 0) { + dp[i][j] = A[i][j]; + } else { + int lastRow = dp[i - 1][j]; + if (j - 1 >= 0) { + lastRow = Math.min(dp[i - 1][j - 1], lastRow); + } + if (j + 1 < size) { + lastRow = Math.min(dp[i - 1][j + 1], lastRow); + } + dp[i][j] = lastRow + A[i][j]; + } + } + } + int minSum = Integer.MAX_VALUE; + for (int i = 0; i < size; i++) { + minSum = Math.min(minSum, dp[size - 1][i]); + } + return minSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_933.java b/src/main/java/com/fishercoder/solutions/firstthousand/_933.java new file mode 100644 index 0000000000..651a061153 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_933.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _933 { + public static class Solution1 { + public static class RecentCounter { + + Deque deque; + + public RecentCounter() { + deque = new LinkedList<>(); + } + + public int ping(int t) { + while (!deque.isEmpty() && t - deque.getFirst() > 3000) { + deque.removeFirst(); + } + deque.addLast(t); + return deque.size(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_934.java b/src/main/java/com/fishercoder/solutions/firstthousand/_934.java new file mode 100644 index 0000000000..dbe31578e1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_934.java @@ -0,0 +1,82 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _934 { + public static class Solution1 { + /* + * Time: O(m*n) + * Space: O(m*n) + */ + public int shortestBridge(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + Queue q1 = new LinkedList<>(); + Queue q2 = new LinkedList<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + q1.offer(new int[] {i, j}); + q2.offer(new int[] {i, j}); + grid[i][j] = + 2; // we mark this one as 2 and all its connected islands to be 2 as + // well using BFS below + // once we find the first land, we break and start BFS to find all remaining + // lands that are connected to this one as island A + break; + } + } + if (!q1.isEmpty()) { + break; + } + } + int[] dirs = new int[] {0, 1, 0, -1, 0}; + while (!q1.isEmpty()) { + int size = q1.size(); + for (int i = 0; i < size; i++) { + int[] curr = q1.poll(); + for (int j = 0; j < dirs.length - 1; j++) { + int nextx = curr[0] + dirs[j]; + int nexty = curr[1] + dirs[j + 1]; + if (nextx >= 0 + && nextx < m + && nexty >= 0 + && nexty < n + && grid[nextx][nexty] == 1) { + grid[nextx][nexty] = 2; + q1.offer(new int[] {nextx, nexty}); + q2.offer(new int[] {nextx, nexty}); + } + } + } + } + // now with the above BFS done, we've discovered all island lands that should be island + // A + // then we go through q2 to check for shortest distance to island B + int distance = 0; + while (!q2.isEmpty()) { + int size = q2.size(); + for (int i = 0; i < size; i++) { + int[] curr = q2.poll(); + for (int j = 0; j < dirs.length - 1; j++) { + int nextx = curr[0] + dirs[j]; + int nexty = curr[1] + dirs[j + 1]; + if (nextx >= 0 && nextx < m && nexty >= 0 && nexty < n) { + if (grid[nextx][nexty] == 1) { + return distance; + } else if (grid[nextx][nexty] == 0) { + q2.offer(new int[] {nextx, nexty}); + grid[nextx][nexty] = + -1; // this is important to mark it as visited, otherwise + // we'll go into infinite loop and TLE + } + } + } + } + distance++; + } + return distance; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_935.java b/src/main/java/com/fishercoder/solutions/firstthousand/_935.java new file mode 100644 index 0000000000..9c47c666dc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_935.java @@ -0,0 +1,68 @@ +package com.fishercoder.solutions.firstthousand; + +public class _935 { + /* + * The intuition is to calculate the number of ways + * we can reach a key k after i hops, based on the number of ways we can reach keys x after i-1 hops + * s.t. the knight can move from x to k in one move + * For example, + * We can reach 6 in 3 ways after 1 hop (1 -> 6, 7 -> 6 or 0 -> 6) + * We can reach 8 in 2 ways after 1 hop (1 -> 8 or 3 -> 8) + * Thus, we can reach 1 in 5 ways after 2 hops: + * . 1. 1 -> 6 -> 1 + * . 2. 7 -> 6 -> 1 + * . 3. 0 -> 6 -> 1 + * 4. 1 -> 8 -> 1 + * 5. 3 -> 8 -> 1 + */ + public static class Solution1 { + private static final int MOD = 1000_000_007; + + // whereFromHere[i] is an array of keys that can be reached from the ith digit + private static final int[][] whereFromHere = { + {4, 6}, + {6, 8}, + {7, 9}, + {4, 8}, // 0, 1, 2, 3 + {3, 9, 0}, + {}, + {1, 7, 0}, // 4, 5, 6 + {2, 6}, + {1, 3}, + {2, 4} // 7, 8, 9 + }; + + public int knightDialer(int N) { + // a[i] is the number of ways we can end up on the ith digit + // The initial array is for N = 1, i.e. for 0 hops. + long[] a = new long[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + + // Simulate N - 1 hops + for (int i = 0; i < N - 1; ++i) { + long[] tmp = new long[10]; + + // For each digit + for (int j = 0; j < 10; j++) { + // Which other digits can we reach? + for (int k : whereFromHere[j]) { + tmp[j] = (tmp[j] + a[k]) % MOD; + } + } + + // Sanity checks based on symmetry of the keypad + assert tmp[1] == tmp[3]; + assert tmp[4] == tmp[6]; + assert tmp[7] == tmp[9]; + + a = tmp; + } + + long ans = 0; + for (long k : a) { + ans = (ans + k) % MOD; + } + + return (int) ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_936.java b/src/main/java/com/fishercoder/solutions/firstthousand/_936.java new file mode 100644 index 0000000000..1743542a80 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_936.java @@ -0,0 +1,64 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _936 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/stamping-the-sequence/discuss/201546/12ms-Java-Solution-Beats-100 + *

+ * Think reversely! + * How to change target to ****?! + */ + public int[] movesToStamp(String stamp, String target) { + List moves = new ArrayList<>(); + char[] s = stamp.toCharArray(); + char[] t = target.toCharArray(); + int stars = 0; + boolean[] visited = new boolean[target.length()]; + while (stars < target.length()) { + boolean doneReplace = false; + for (int i = 0; i <= target.length() - stamp.length(); i++) { + if (!visited[i] && canReplace(t, i, s)) { + stars = doReplace(t, i, s, stars); + doneReplace = true; + visited[i] = true; + moves.add(i); + if (stars == t.length) { + break; + } + } + } + if (!doneReplace) { + return new int[0]; + } + } + + int[] result = new int[moves.size()]; + for (int i = 0; i < moves.size(); i++) { + result[i] = moves.get(moves.size() - i - 1); + } + return result; + } + + private boolean canReplace(char[] t, int i, char[] s) { + for (int j = 0; j < s.length; j++) { + if (t[i + j] != '*' && t[i + j] != s[j]) { + return false; + } + } + return true; + } + + private int doReplace(char[] t, int i, char[] s, int stars) { + for (int j = 0; j < s.length; j++) { + if (t[i + j] != '*') { + t[i + j] = '*'; + stars++; + } + } + return stars; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_937.java b/src/main/java/com/fishercoder/solutions/firstthousand/_937.java new file mode 100644 index 0000000000..1a89f820db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_937.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _937 { + public static class Solution1 { + public String[] reorderLogFiles(String[] logs) { + TreeMap letterLogMap = new TreeMap<>(); + List digitLogList = new ArrayList<>(); + for (String log : logs) { + int firstSpaceIndex = log.indexOf(' '); + String id = log.substring(0, firstSpaceIndex); + if (Character.isAlphabetic(log.charAt(firstSpaceIndex + 1))) { + String key = log.substring(firstSpaceIndex + 1) + id; + letterLogMap.put(key, log); + } else { + digitLogList.add(log); + } + } + String[] reorderedLogs = new String[logs.length]; + int i = 0; + for (String key : letterLogMap.keySet()) { + reorderedLogs[i++] = letterLogMap.get(key); + } + for (String log : digitLogList) { + reorderedLogs[i++] = log; + } + return reorderedLogs; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_938.java b/src/main/java/com/fishercoder/solutions/firstthousand/_938.java new file mode 100644 index 0000000000..55a9ad3400 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_938.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _938 { + public static class Solution1 { + public int rangeSumBST(TreeNode root, int low, int high) { + if (root == null) { + return 0; + } + List list = new ArrayList<>(); + dfs(root, low, high, list); + return list.stream().mapToInt(num -> num).sum(); + } + + private void dfs(TreeNode root, int low, int high, List list) { + if (root == null) { + return; + } + if (root.val <= high && root.val >= low) { + list.add(root.val); + } + if (root.val > low) { + dfs(root.left, low, high, list); + } + if (root.val < high) { + dfs(root.right, low, high, list); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_94.java b/src/main/java/com/fishercoder/solutions/firstthousand/_94.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_94.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_94.java index 19b2ec5d2a..c0a71832cd 100644 --- a/src/main/java/com/fishercoder/solutions/_94.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_94.java @@ -1,7 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; - import java.util.ArrayList; import java.util.List; import java.util.Stack; @@ -24,7 +23,7 @@ List inorder(TreeNode root, List result) { } public static class Solution2 { - //iterative approach + // iterative approach public List inorderTraversal(TreeNode root) { List result = new ArrayList(); Stack stack = new Stack(); diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_941.java b/src/main/java/com/fishercoder/solutions/firstthousand/_941.java new file mode 100644 index 0000000000..77cd63429b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_941.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.firstthousand; + +public class _941 { + public static class Solution1 { + public boolean validMountainArray(int[] arr) { + int i = 0; + for (; i < arr.length - 1; i++) { + if (arr[i] < arr[i + 1]) { + continue; + } else if (arr[i] == arr[i + 1]) { + return false; + } else { + break; + } + } + if (i == 0 || i >= arr.length - 1) { + return false; + } + for (; i < arr.length - 1; i++) { + if (arr[i] > arr[i + 1]) { + continue; + } else { + return false; + } + } + return i == arr.length - 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_942.java b/src/main/java/com/fishercoder/solutions/firstthousand/_942.java new file mode 100644 index 0000000000..9ad108cff7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_942.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Queue; + +public class _942 { + public static class Solution1 { + public int[] diStringMatch(String S) { + Queue deque = new ArrayDeque<>(); + for (int i = 0; i <= S.length(); i++) { + deque.add(i); + } + int[] result = new int[S.length() + 1]; + for (int i = 0; i <= S.length(); i++) { + if (i == S.length()) { + result[i] = ((ArrayDeque) deque).pollLast(); + } else if (S.charAt(i) == 'I') { + result[i] = ((ArrayDeque) deque).pollFirst(); + } else { + result[i] = ((ArrayDeque) deque).pollLast(); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_944.java b/src/main/java/com/fishercoder/solutions/firstthousand/_944.java new file mode 100644 index 0000000000..53d32bc63a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_944.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +public class _944 { + public static class Solution1 { + public int minDeletionSize(String[] A) { + if (A == null || A.length == 0) { + return 0; + } + int deletion = 0; + for (int i = 0; i < A[0].length(); i++) { + for (int j = 0; j < A.length - 1; j++) { + if (A[j].charAt(i) > A[j + 1].charAt(i)) { + deletion++; + break; + } + } + } + return deletion; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_945.java b/src/main/java/com/fishercoder/solutions/firstthousand/_945.java new file mode 100644 index 0000000000..a2c270cff1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_945.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _945 { + public static class Solution1 { + public int minIncrementForUnique(int[] nums) { + Arrays.sort(nums); + int minIncs = 0; + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i + 1] <= nums[i]) { + minIncs += nums[i] - nums[i + 1] + 1; + nums[i + 1] = nums[i] + 1; + } + } + return minIncs; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_946.java b/src/main/java/com/fishercoder/solutions/firstthousand/_946.java new file mode 100644 index 0000000000..521cf9213c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_946.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Stack; + +public class _946 { + public static class Solution1 { + public boolean validateStackSequences(int[] pushed, int[] popped) { + if (pushed == null || popped == null || pushed.length == 0 || popped.length == 0) { + return true; + } + Stack stack = new Stack<>(); + stack.push(pushed[0]); + int i = 1; + int j = 0; + while (!stack.isEmpty() || j < popped.length) { + if (j < popped.length && !stack.isEmpty() && stack.peek() == popped[j]) { + stack.pop(); + j++; + } else { + if (i < pushed.length) { + stack.push(pushed[i++]); + } else { + return stack.isEmpty(); + } + } + } + return stack.isEmpty(); + } + } + + public static class Solution2 { + public boolean validateStackSequences(int[] pushed, int[] popped) { + Stack stack = new Stack<>(); + int i = 0; + int j = 0; + int len = pushed.length; + while (i < len) { + if (pushed[i] == popped[j]) { + i++; + j++; + } else if (!stack.isEmpty() && stack.peek() == popped[j]) { + stack.pop(); + j++; + } else { + stack.push(pushed[i++]); + } + } + while (j < len) { + if (!stack.isEmpty() && stack.peek() != popped[j++]) { + return false; + } else { + stack.pop(); + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_95.java b/src/main/java/com/fishercoder/solutions/firstthousand/_95.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_95.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_95.java index 500118231d..3ae7cee755 100644 --- a/src/main/java/com/fishercoder/solutions/_95.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_95.java @@ -1,7 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; - import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_950.java b/src/main/java/com/fishercoder/solutions/firstthousand/_950.java new file mode 100644 index 0000000000..9793e99258 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_950.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Deque; + +public class _950 { + public static class Solution1 { + public int[] deckRevealedIncreasing(int[] deck) { + Arrays.sort(deck); + Deque deque = new ArrayDeque<>(); + for (int i = deck.length - 1; i >= 0; i--) { + if (i != deck.length - 1) { + deque.addFirst(deque.pollLast()); + } + deque.addFirst(deck[i]); + } + int[] result = new int[deck.length]; + int i = 0; + while (!deque.isEmpty()) { + result[i++] = deque.pollFirst(); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_951.java b/src/main/java/com/fishercoder/solutions/firstthousand/_951.java new file mode 100644 index 0000000000..5a45c8b75e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_951.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _951 { + public static class Solution1 { + public boolean flipEquiv(TreeNode root1, TreeNode root2) { + if (root1 == null && root2 == null) { + return true; + } + if (root1 == null || root2 == null) { + return false; + } + + if (root1.val != root2.val) { + return false; + } + + return ((flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right)) + || (flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left))); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_953.java b/src/main/java/com/fishercoder/solutions/firstthousand/_953.java new file mode 100644 index 0000000000..2fed2f046b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_953.java @@ -0,0 +1,72 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class _953 { + public static class Solution1 { + public boolean isAlienSorted(String[] words, String order) { + if (words.length == 1) { + return true; + } + + Map map = new HashMap<>(); + for (int i = 0; i < order.length(); i++) { + map.put(order.charAt(i), i); + } + + for (int i = 0; i < words.length - 1; i++) { + String firstWord = words[i]; + String secondWord = words[i + 1]; + if (!sorted(firstWord, secondWord, map)) { + return false; + } + } + return true; + } + + private boolean sorted(String firstWord, String secondWord, Map map) { + for (int i = 0; i < Math.min(firstWord.length(), secondWord.length()); i++) { + if (firstWord.charAt(i) != secondWord.charAt(i)) { + return map.get(firstWord.charAt(i)) <= map.get(secondWord.charAt(i)); + } + } + return firstWord.length() <= secondWord.length(); + } + } + + public static class Solution2 { + /* + * Solution1 above is actually consistently faster than this Solution2 on LeetCode. + */ + public boolean isAlienSorted(String[] words, String order) { + String[] copy = Arrays.copyOf(words, words.length); + Arrays.sort( + words, + (o1, o2) -> { + int pos1 = 0; + int pos2 = 0; + for (int i = 0; i < Math.min(o1.length(), o2.length()); i++) { + pos1 = order.indexOf(o1.charAt(i)); + pos2 = order.indexOf(o2.charAt(i)); + if (pos1 != pos2) { + break; + } + } + + if (pos1 == pos2 && o1.length() != o2.length()) { + return o1.length() - o2.length(); + } + + return pos1 - pos2; + }); + for (int i = 0; i < words.length; i++) { + if (!copy[i].equals(words[i])) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_954.java b/src/main/java/com/fishercoder/solutions/firstthousand/_954.java similarity index 94% rename from src/main/java/com/fishercoder/solutions/_954.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_954.java index 9da8058e0d..c8e2af4e15 100644 --- a/src/main/java/com/fishercoder/solutions/_954.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_954.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.Arrays; import java.util.Comparator; @@ -7,7 +7,7 @@ public class _954 { public static class Solution1 { - /** + /* * credit: https://leetcode.com/problems/array-of-doubled-pairs/solution/ */ public boolean canReorderDoubled(int[] A) { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_957.java b/src/main/java/com/fishercoder/solutions/firstthousand/_957.java new file mode 100644 index 0000000000..106d3c4b2a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_957.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _957 { + public static class Solution1 { + public int[] prisonAfterNDays(int[] cells, int N) { + Set prisonStates = new HashSet<>(); + boolean hasCycle = false; + int times = 0; + for (int i = 0; i < N; i++) { + int[] next = getNextDay(cells); + String nextDayState = Arrays.toString(next); + if (prisonStates.contains(nextDayState)) { + hasCycle = true; + break; + } else { + prisonStates.add(nextDayState); + times++; + } + cells = next; + } + if (hasCycle) { + N %= times; + for (int i = 0; i < N; i++) { + cells = getNextDay(cells); + } + } + return cells; + } + + private int[] getNextDay(int[] cells) { + int[] nextDay = new int[cells.length]; + for (int i = 0; i < cells.length; i++) { + if (i == 0 || i == cells.length - 1) { + nextDay[i] = 0; + } else { + nextDay[i] = (cells[i - 1] == cells[i + 1]) ? 1 : 0; + } + } + return nextDay; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_958.java b/src/main/java/com/fishercoder/solutions/firstthousand/_958.java new file mode 100644 index 0000000000..d9a4ee6c3b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_958.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; + +public class _958 { + public static class Solution1 { + public boolean isCompleteTree(TreeNode root) { + Queue queue = new LinkedList<>(); + queue.offer(root); + boolean shouldHaveNoMoreChildren = false; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (shouldHaveNoMoreChildren && (curr.left != null || curr.right != null)) { + return false; + } + if (curr.left == null && curr.right != null) { + return false; + } + if (curr.left != null) { + queue.offer(curr.left); + } + if (curr.right == null) { + shouldHaveNoMoreChildren = true; + } else { + queue.offer(curr.right); + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_96.java b/src/main/java/com/fishercoder/solutions/firstthousand/_96.java similarity index 89% rename from src/main/java/com/fishercoder/solutions/_96.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_96.java index aeb75f6e38..fdde561e95 100644 --- a/src/main/java/com/fishercoder/solutions/_96.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_96.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; public class _96 { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_961.java b/src/main/java/com/fishercoder/solutions/firstthousand/_961.java new file mode 100644 index 0000000000..77b491ebaf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_961.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _961 { + public static class Solution1 { + public int repeatedNTimes(int[] A) { + Set set = new HashSet<>(); + for (int num : A) { + if (!set.add(num)) { + return num; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_965.java b/src/main/java/com/fishercoder/solutions/firstthousand/_965.java new file mode 100644 index 0000000000..fb0f1dbb73 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_965.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _965 { + public static class Solution1 { + public boolean isUnivalTree(TreeNode root) { + if (root == null) { + return true; + } + return dfs(root, root.val); + } + + private boolean dfs(TreeNode root, int value) { + if (root == null) { + return true; + } + if (root.val != value) { + return false; + } + return dfs(root.left, value) && dfs(root.right, value); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_966.java b/src/main/java/com/fishercoder/solutions/firstthousand/_966.java new file mode 100644 index 0000000000..cc837c9a37 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_966.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _966 { + + public static class Solution1 { + public String[] spellchecker(String[] wordlist, String[] queries) { + Map caseMap = new HashMap<>(); + Set set = new HashSet<>(); + + // Case Part Mapping + for (String word : wordlist) { + if (!caseMap.containsKey(word.toLowerCase())) { + caseMap.put(word.toLowerCase(), word); + } + set.add(word); + } + + // Vowel Part Mapping + Map vowelMap = new HashMap<>(); + for (String word : wordlist) { + String genericVal = makeGenericVowel(word); + if (!vowelMap.containsKey(genericVal)) { + vowelMap.put(genericVal, word); + } + } + + String[] ans = new String[queries.length]; + + for (int i = 0; i < queries.length; i++) { + if (set.contains(queries[i])) { + ans[i] = queries[i]; + } else if (caseMap.containsKey(queries[i].toLowerCase())) { + ans[i] = caseMap.get(queries[i].toLowerCase()); + } else if (vowelMap.containsKey(makeGenericVowel(queries[i]))) { + ans[i] = vowelMap.get(makeGenericVowel(queries[i])); + } else { + ans[i] = ""; + } + } + return ans; + } + + private String makeGenericVowel(String s) { + String vowel = "aeiou"; + char[] ch = s.toLowerCase().toCharArray(); + for (int i = 0; i < ch.length; i++) { + if (vowel.indexOf(ch[i]) != -1) { + ch[i] = '#'; + } + } + return String.valueOf(ch); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_97.java b/src/main/java/com/fishercoder/solutions/firstthousand/_97.java new file mode 100644 index 0000000000..34353dcfee --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_97.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.firstthousand; + +public class _97 { + public static class Solution1 { + public boolean isInterleave(String s1, String s2, String s3) { + int m = s1.length(); + int n = s2.length(); + if (m + n != s3.length()) { + return false; + } + + boolean[][] dp = new boolean[m + 1][n + 1]; + + dp[0][0] = true; + + for (int i = 0; i < m; i++) { + if (s1.charAt(i) == s3.charAt(i)) { + dp[i + 1][0] = true; + } else { + // if one char fails, that means it breaks, the rest of the chars won't matter + // any more. + // Mian and I found one missing test case on Lintcode: ["b", "aabccc", + // "aabbbcb"] + // if we don't break, here, Lintcode could still accept this code, but Leetcode + // fails it. + break; + } + } + + for (int j = 0; j < n; j++) { + if (s2.charAt(j) == s3.charAt(j)) { + dp[0][j + 1] = true; + } else { + break; + } + } + + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + int k = i + j - 1; + dp[i][j] = + (s1.charAt(i - 1) == s3.charAt(k) && dp[i - 1][j]) + || (s2.charAt(j - 1) == s3.charAt(k) && dp[i][j - 1]); + } + } + + return dp[m][n]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_970.java b/src/main/java/com/fishercoder/solutions/firstthousand/_970.java new file mode 100644 index 0000000000..a22f15de78 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_970.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _970 { + public static class Solution1 { + public List powerfulIntegers(int x, int y, int bound) { + int powerInteger; + Set set = new HashSet<>(); + for (int i = 0; i <= bound; i++) { + for (int j = 0; j <= bound; j++) { + powerInteger = (int) (Math.pow(x, i) + Math.pow(y, j)); + if (powerInteger <= bound) { + set.add(powerInteger); + } else { + break; + } + if (y == 1) { + break; + } + } + if (x == 1) { + break; + } + } + return new ArrayList<>(set); + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/powerful-integers/discuss/214212/JavaC%2B%2BPython-Brute-Force + */ + public List powerfulIntegers(int x, int y, int bound) { + Set result = new HashSet<>(); + for (int i = 1; i < bound; i *= x > 1 ? x : bound + 1) { + for (int j = 1; i + j <= bound; j *= y > 1 ? y : bound + 1) { + result.add(i + j); + } + } + return new ArrayList<>(result); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_973.java b/src/main/java/com/fishercoder/solutions/firstthousand/_973.java new file mode 100644 index 0000000000..4c96ae2ed4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_973.java @@ -0,0 +1,69 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.PriorityQueue; + +public class _973 { + + public static class Solution1 { + public int[][] kClosest(int[][] points, int k) { + int[][] ans = new int[k][2]; + + PriorityQueue pq = + new PriorityQueue<>( + (o1, o2) -> { + double dist1 = getDistance(o1); + double dist2 = getDistance(o2); + + if (dist1 > dist2) { + return 1; + } else if (dist1 < dist2) { + return -1; + } else { + return 0; + } + }); + + for (int[] point : points) { + pq.add(point); + } + + for (int i = 0; i < k; i++) { + ans[i] = pq.poll(); + } + + return ans; + } + + private double getDistance(int[] point) { + return Math.sqrt(Math.pow(point[0], 2) + Math.pow(point[1], 2)); + } + } + + public static class Solution2 { + public int[][] kClosest(int[][] points, int k) { + PriorityQueue maxHeap = + new PriorityQueue<>( + (a, b) -> (b[0] * b[0] + b[1] * b[1]) - (a[0] * a[0] + a[1] * a[1])); + for (int[] point : points) { + long distance = (long) point[0] * point[0] + point[1] * point[1]; + if (maxHeap.size() < k) { + maxHeap.offer(point); + } else { + int[] peek = maxHeap.peek(); + long peekedDistance = (long) peek[0] * peek[0] + peek[1] * peek[1]; + if (peekedDistance > distance) { + // this is an optimization so that the space complexity is limited to O(k) + maxHeap.poll(); + maxHeap.offer(point); + } + } + } + int[][] result = new int[k][2]; + for (int i = 0; i < k; i++) { + int[] point = maxHeap.poll(); + result[i] = point; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_974.java b/src/main/java/com/fishercoder/solutions/firstthousand/_974.java similarity index 75% rename from src/main/java/com/fishercoder/solutions/_974.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_974.java index b935ac29ba..07c67d899e 100644 --- a/src/main/java/com/fishercoder/solutions/_974.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_974.java @@ -1,15 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import java.util.HashMap; import java.util.Map; -/** - * 974. Subarray Sums Divisible by K - * - * Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum - * divisible by K. - * */ - public class _974 { public static class Solution1 { public int subarraysDivByK(int[] A, int K) { diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_976.java b/src/main/java/com/fishercoder/solutions/firstthousand/_976.java new file mode 100644 index 0000000000..e234179958 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_976.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _976 { + + public static class Solution1 { + public int largestPerimeter(int[] A) { + Arrays.sort(A); + int n = A.length; + + for (int i = n - 1; i > 1; i--) { + if (A[i] < A[i - 1] + A[i - 2]) { + return A[i] + A[i - 1] + A[i - 2]; + } + } + + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_977.java b/src/main/java/com/fishercoder/solutions/firstthousand/_977.java new file mode 100644 index 0000000000..f68fd56ac8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_977.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _977 { + public static class Solution1 { + /* + * O(nlogn) solution + */ + public int[] sortedSquares(int[] nums) { + int[] result = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + result[i] = (int) Math.pow(nums[i], 2); + } + Arrays.sort(result); + return result; + } + } + + public static class Solution2 { + /* + * O(n) solution + */ + public int[] sortedSquares(int[] nums) { + int[] ans = new int[nums.length]; + for (int i = nums.length - 1, left = 0, right = nums.length - 1; + i < nums.length && left <= right; + i--) { + if (Math.abs(nums[left]) < Math.abs(nums[right])) { + ans[i] = nums[right] * nums[right]; + right--; + } else { + ans[i] = nums[left] * nums[left]; + left++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_979.java b/src/main/java/com/fishercoder/solutions/firstthousand/_979.java new file mode 100644 index 0000000000..0ce650bbd5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_979.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _979 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/distribute-coins-in-binary-tree/discuss/221930/JavaC%2B%2BPython-Recursive-Solution + */ + int moves = 0; + + public int distributeCoins(TreeNode root) { + dfs(root); + return moves; + } + + int dfs(TreeNode root) { + if (root == null) { + return 0; + } + int left = dfs(root.left); + int right = dfs(root.right); + moves += Math.abs(left) + Math.abs(right); + return root.val + left + right - 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_98.java b/src/main/java/com/fishercoder/solutions/firstthousand/_98.java similarity index 92% rename from src/main/java/com/fishercoder/solutions/_98.java rename to src/main/java/com/fishercoder/solutions/firstthousand/_98.java index 3e2f39ec46..e9c8c7f2e7 100644 --- a/src/main/java/com/fishercoder/solutions/_98.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_98.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.firstthousand; import com.fishercoder.common.classes.TreeNode; @@ -20,5 +20,4 @@ boolean valid(TreeNode root, Integer min, Integer max) { return valid(root.left, min, root.val) && valid(root.right, root.val, max); } } - } diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_980.java b/src/main/java/com/fishercoder/solutions/firstthousand/_980.java new file mode 100644 index 0000000000..8b60e62622 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_980.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.firstthousand; + +public class _980 { + public static class Solution1 { + + int[] directions = new int[] {0, 1, 0, -1, 0}; + int paths = 0; + + public int uniquePathsIII(int[][] grid) { + int[] start = findStart(grid); + int m = grid.length; + int n = grid[0].length; + boolean[][] visited = new boolean[m][n]; + visited[start[0]][start[1]] = true; + return backtracking(grid, m, n, visited, start); + } + + private int backtracking(int[][] grid, int m, int n, boolean[][] visited, int[] start) { + for (int i = 0; i < directions.length - 1; i++) { + int nextX = directions[i] + start[0]; + int nextY = directions[i + 1] + start[1]; + if (nextX >= 0 + && nextX < m + && nextY >= 0 + && nextY < n + && grid[nextX][nextY] != -1 + && !visited[nextX][nextY]) { + if (grid[nextX][nextY] == 2) { + if (allZeroesVisited(visited, grid)) { + paths++; + return paths; + } else { + continue; + } + } + visited[nextX][nextY] = true; + backtracking(grid, m, n, visited, new int[] {nextX, nextY}); + visited[nextX][nextY] = false; + } + } + return paths; + } + + private boolean allZeroesVisited(boolean[][] visited, int[][] grid) { + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == 0 && !visited[i][j]) { + return false; + } + } + } + return true; + } + + private int[] findStart(int[][] grid) { + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == 1) { + return new int[] {i, j}; + } + } + } + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_981.java b/src/main/java/com/fishercoder/solutions/firstthousand/_981.java new file mode 100644 index 0000000000..0f122afc52 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_981.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; + +public class _981 { + public static class Solution1 { + + public static class TimeMap { + + Map> map; + + /* + * Initialize your data structure here. + */ + public TimeMap() { + this.map = new HashMap<>(); + } + + public void set(String key, String value, int timestamp) { + if (!map.containsKey(key)) { + map.put(key, new TreeMap<>()); + } + TreeMap timestampMap = map.get(key); + timestampMap.put(timestamp, value); + } + + public String get(String key, int timestamp) { + TreeMap timestampMap = map.get(key); + Integer prevTimestamp = timestampMap.floorKey(timestamp); + if (prevTimestamp == null) { + return ""; + } else { + return timestampMap.get(prevTimestamp); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_985.java b/src/main/java/com/fishercoder/solutions/firstthousand/_985.java new file mode 100644 index 0000000000..6515895d3b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_985.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.Arrays; + +public class _985 { + + public static class Solution1 { + + public int[] sumEvenAfterQueries(int[] A, int[][] queries) { + int[] result = new int[A.length]; + for (int i = 0; i < A.length; i++) { + int col = queries[i][1]; + A[col] = A[col] + queries[i][0]; + result[i] = computeEvenSum(A); + } + return result; + } + + private int computeEvenSum(int[] A) { + int sum = Arrays.stream(A).filter(num -> num % 2 == 0).sum(); + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_986.java b/src/main/java/com/fishercoder/solutions/firstthousand/_986.java new file mode 100644 index 0000000000..2a975981c2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_986.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _986 { + public static class Solution1 { + public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { + int i = 0; + int j = 0; + List list = new ArrayList<>(); + while (i < firstList.length && j < secondList.length) { + int start = Math.max(firstList[i][0], secondList[j][0]); + int end = Math.min(firstList[i][1], secondList[j][1]); + if (start <= end) { + list.add(new int[] {start, end}); + } + if (end == firstList[i][1]) { + i++; + } else { + j++; + } + } + return list.toArray(new int[list.size()][2]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_987.java b/src/main/java/com/fishercoder/solutions/firstthousand/_987.java new file mode 100644 index 0000000000..6a3b436e17 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_987.java @@ -0,0 +1,107 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.*; + +public class _987 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/discuss/231148/Java-TreeMap-Solution + */ + public List> verticalTraversal(TreeNode root) { + TreeMap>> map = new TreeMap<>(); + dfs(root, 0, 0, map); + List> list = new ArrayList<>(); + for (TreeMap> yMap : map.values()) { + list.add(new ArrayList<>()); + for (PriorityQueue nodes : yMap.values()) { + while (!nodes.isEmpty()) { + list.get(list.size() - 1).add(nodes.poll()); + } + } + } + return list; + } + + private void dfs( + TreeNode root, + int x, + int y, + TreeMap>> map) { + if (root == null) { + return; + } + if (!map.containsKey(x)) { + map.put(x, new TreeMap<>()); + } + if (!map.get(x).containsKey(y)) { + map.get(x).put(y, new PriorityQueue<>()); + } + map.get(x).get(y).offer(root.val); + dfs(root.left, x - 1, y + 1, map); + dfs(root.right, x + 1, y + 1, map); + } + } + + public static class Solution2 { + + /* + * My completely original solution on 6/13/2024. + */ + public List> verticalTraversal(TreeNode root) { + TreeMap> map = new TreeMap<>(); + Queue q = new LinkedList<>(); + q.offer(new NodeWithCoords(root, 0, 0)); + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + NodeWithCoords curr = q.poll(); + int col = curr.col; + int row = curr.row; + List list = map.getOrDefault(col, new ArrayList<>()); + list.add(curr); + map.put(col, list); + if (curr.node.left != null) { + q.offer(new NodeWithCoords(curr.node.left, row + 1, col - 1)); + } + if (curr.node.right != null) { + q.offer(new NodeWithCoords(curr.node.right, row + 1, col + 1)); + } + } + } + List> result = new ArrayList<>(); + for (Integer key : map.keySet()) { + List list = map.get(key); + Collections.sort( + list, + (a, b) -> { + if (a.row != b.row) { + return a.row - b.row; + } else if (a.col != b.col) { + return a.col - b.col; + } else { + return a.node.val - b.node.val; + } + }); + List intList = new ArrayList<>(); + for (NodeWithCoords nodeWithCoords : list) { + intList.add(nodeWithCoords.node.val); + } + result.add(intList); + } + return result; + } + + class NodeWithCoords { + TreeNode node; + int row; + int col; + + public NodeWithCoords(TreeNode node, int row, int col) { + this.node = node; + this.row = row; + this.col = col; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_988.java b/src/main/java/com/fishercoder/solutions/firstthousand/_988.java new file mode 100644 index 0000000000..2c8030696f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_988.java @@ -0,0 +1,70 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _988 { + public static class Solution1 { + public String smallestFromLeaf(TreeNode root) { + final List paths = new ArrayList<>(); + Map map = new HashMap<>(); + map.put(0, 'a'); + map.put(1, 'b'); + map.put(2, 'c'); + map.put(3, 'd'); + map.put(4, 'e'); + map.put(5, 'f'); + map.put(6, 'g'); + map.put(7, 'h'); + map.put(8, 'i'); + map.put(9, 'j'); + map.put(10, 'k'); + map.put(11, 'l'); + map.put(12, 'm'); + map.put(13, 'n'); + map.put(14, 'o'); + map.put(15, 'p'); + map.put(16, 'q'); + map.put(17, 'r'); + map.put(18, 's'); + map.put(19, 't'); + map.put(20, 'u'); + map.put(21, 'v'); + map.put(22, 'w'); + map.put(23, 'x'); + map.put(24, 'y'); + map.put(25, 'z'); + dfs(root, "", paths, map); + return findSmallest(paths); + } + + private String findSmallest(List paths) { + List reversed = new ArrayList<>(); + for (String path : paths) { + StringBuilder sb = new StringBuilder(); + sb.append(path); + reversed.add(sb.reverse().toString()); + } + Collections.sort(reversed); + return reversed.get(0); + } + + private void dfs( + TreeNode root, String path, List paths, Map map) { + if (root == null) { + return; + } + path += map.get(root.val); + if (root.left == null && root.right == null) { + paths.add(path); + } + dfs(root.left, path, paths, map); + dfs(root.right, path, paths, map); + path = path.substring(0, path.length() - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_989.java b/src/main/java/com/fishercoder/solutions/firstthousand/_989.java new file mode 100644 index 0000000000..ff775e3bae --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_989.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _989 { + public static class Solution1 { + public List addToArrayForm(int[] A, int K) { + List kDigitsReversed = new ArrayList<>(); + int divisor = 10; + while (K != 0) { + kDigitsReversed.add(K % divisor); + K /= 10; + } + List result = new ArrayList<>(); + int prevFlow = 0; + for (int i = A.length - 1, j = 0; i >= 0 || j < kDigitsReversed.size(); i--, j++) { + int sum; + if (i >= 0 && j < kDigitsReversed.size()) { + sum = A[i] + kDigitsReversed.get(j); + } else if (i >= 0) { + sum = A[i]; + } else { + sum = kDigitsReversed.get(j); + } + int flow = 0; + if (prevFlow != 0) { + sum += prevFlow; + } + if (sum > 9) { + flow = 1; + } + sum %= 10; + prevFlow = flow; + result.add(sum); + } + if (prevFlow != 0) { + result.add(prevFlow); + } + Collections.reverse(result); + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_99.java b/src/main/java/com/fishercoder/solutions/firstthousand/_99.java new file mode 100644 index 0000000000..a980eaa720 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_99.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _99 { + public static class Solution1 { + TreeNode firstElement = null; + TreeNode secondElement = null; + + TreeNode prevElement = new TreeNode(Integer.MIN_VALUE); + + public void recoverTree(TreeNode root) { + traverseTree(root); + + // swap the two elements + int temp = firstElement.val; + firstElement.val = secondElement.val; + secondElement.val = temp; + } + + private void traverseTree(TreeNode root) { + if (root == null) { + return; + } + + traverseTree(root.left); + + // prevElement means the one previous to the current root, refer to in-order traversal, + // previous element must be smaller than the current root + // if it's bigger, then we find the first element, thus we store it in the variable + // called firstElement + if (firstElement == null && prevElement.val >= root.val) { + firstElement = prevElement; + } + + if (firstElement != null && prevElement.val >= root.val) { + secondElement = root; + } + + // this is the last step in the "do some business logic", so we'll always to have update + // the previous node to be the current root before it traverses the right subtree + // since the current root will be the new previous node for the right subtree. + prevElement = root; + + traverseTree(root.right); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_991.java b/src/main/java/com/fishercoder/solutions/firstthousand/_991.java new file mode 100644 index 0000000000..0f321596be --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_991.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.firstthousand; + +public class _991 { + public static class Solution1 { + public int brokenCalc(int X, int Y) { + int ops = 0; + while (Y > X) { + ops++; + if (Y % 2 != 0) { + Y++; + } else { + Y /= 2; + } + } + return ops + X - Y; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_993.java b/src/main/java/com/fishercoder/solutions/firstthousand/_993.java new file mode 100644 index 0000000000..259bf97249 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_993.java @@ -0,0 +1,80 @@ +package com.fishercoder.solutions.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _993 { + public static class Solution1 { + public boolean isCousins(TreeNode root, int x, int y) { + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode current = queue.poll(); + if (current.left != null) { + queue.offer(current.left); + } + if (current.right != null) { + queue.offer(current.right); + } + if (current.left != null && current.right != null) { + if (current.left.val == x && current.right.val == y + || current.left.val == y && current.right.val == x) { + return false; + } + } + } + if (checkQueue(queue, x, y)) { + return true; + } + } + return false; + } + + private boolean checkQueue(Queue queue, int x, int y) { + Set set = new HashSet<>(); + Queue tmp = new LinkedList<>(queue); + while (!tmp.isEmpty()) { + set.add(tmp.poll().val); + } + return set.contains(x) && set.contains(y); + } + } + + public static class Solution2 { + public boolean isCousins(TreeNode root, int x, int y) { + Queue queue = new LinkedList<>(); + queue.offer(root); + Map childToParentMap = new HashMap<>(); + while (!queue.isEmpty()) { + int size = queue.size(); + childToParentMap.clear(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (curr.left != null) { + queue.offer(curr.left); + childToParentMap.put(curr.left.val, curr.val); + } + if (curr.right != null) { + queue.offer(curr.right); + childToParentMap.put(curr.right.val, curr.val); + } + } + if (childToParentMap.containsKey(x) + && childToParentMap.containsKey(y) + && childToParentMap.get(x) != childToParentMap.get(y)) { + return true; + } else if (childToParentMap.containsKey(x) || childToParentMap.containsKey(y)) { + return false; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_994.java b/src/main/java/com/fishercoder/solutions/firstthousand/_994.java new file mode 100644 index 0000000000..a9d04deae9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_994.java @@ -0,0 +1,154 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; + +public class _994 { + public static class Solution1 { + int[] directions = new int[] {0, 1, 0, -1, 0}; + + public int orangesRotting(int[][] grid) { + Queue rottens = new LinkedList<>(); + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == 2) { + rottens.add(new int[] {i, j}); + } + } + } + int times = 0; + while (!rottens.isEmpty()) { + int size = rottens.size(); + boolean counted = false; + for (int k = 0; k < size; k++) { + int[] rotten = rottens.poll(); + for (int i = 0; i < 4; i++) { + int x = rotten[0] + directions[i]; + int y = rotten[1] + directions[i + 1]; + if (x >= 0 + && x < grid.length + && y >= 0 + && y < grid[0].length + && grid[x][y] == 1) { + grid[x][y] = 2; + if (!counted) { + times++; + } + counted = true; + rottens.add(new int[] {x, y}); + } + } + } + } + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == 1) { + return -1; + } + } + } + return times; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/11/2021. + */ + public int orangesRotting(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + Queue queue = new LinkedList<>(); + Set fresh = new HashSet<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + fresh.add(i * n + j); + } else if (grid[i][j] == 2) { + queue.offer(new int[] {i, j}); + } + } + } + int time = 0; + int[] directions = new int[] {0, 1, 0, -1, 0}; + while (!queue.isEmpty() && !fresh.isEmpty()) { + int size = queue.size(); + time++; + for (int i = 0; i < size; i++) { + int[] curr = queue.poll(); + for (int k = 0; k < directions.length - 1; k++) { + int nextX = curr[0] + directions[k]; + int nextY = curr[1] + directions[k + 1]; + if (nextX >= 0 + && nextX < m + && nextY >= 0 + && nextY < n + && grid[nextX][nextY] == 1) { + fresh.remove(nextX * n + nextY); + if (fresh.isEmpty()) { + return time; + } + grid[nextX][nextY] = 2; + queue.offer(new int[] {nextX, nextY}); + } + } + } + } + return fresh.isEmpty() ? time : -1; + } + } + + public static class Solution3 { + /* + * My original solution on 10/29/2021. + */ + public int orangesRotting(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + int freshOranges = 0; + Queue queue = new LinkedList<>(); + boolean[][] visited = new boolean[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 2) { + queue.offer(new int[] {i, j}); + visited[i][j] = true; + } else if (grid[i][j] == 1) { + freshOranges++; + } + } + } + int mins = 0; + int[] directions = new int[] {0, 1, 0, -1, 0}; + while (!queue.isEmpty()) { + int size = queue.size(); + boolean hasOneToRot = false; + for (int i = 0; i < size; i++) { + int[] curr = queue.poll(); + for (int j = 0; j < directions.length - 1; j++) { + int newx = directions[j] + curr[0]; + int newy = directions[j + 1] + curr[1]; + if (newx >= 0 + && newx < m + && newy >= 0 + && newy < n + && grid[newx][newy] == 1 + && !visited[newx][newy]) { + freshOranges--; + grid[newx][newy] = 2; + visited[newx][newy] = true; + queue.offer(new int[] {newx, newy}); + hasOneToRot = true; + } + } + } + if (hasOneToRot) { + mins++; + } + } + return freshOranges == 0 ? mins : -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_997.java b/src/main/java/com/fishercoder/solutions/firstthousand/_997.java new file mode 100644 index 0000000000..396311856c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_997.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.firstthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _997 { + public static class Solution1 { + public int findJudge(int n, int[][] trust) { + int[] trustPoints = new int[n]; + Set trustOthers = new HashSet<>(); + for (int[] eachTrust : trust) { + trustPoints[eachTrust[1] - 1]++; + trustOthers.add(eachTrust[0]); + } + int judge = -1; + for (int i = 0; i < trustPoints.length; i++) { + if (trustPoints[i] == n - 1 && !trustOthers.contains(i + 1)) { + judge = i + 1; + } + } + return judge; + } + } + + public static class Solution2 { + /* + * Credit: https://leetcode.com/problems/find-the-town-judge/solution/ solution 2 + * Also, note: is it possible to have more than one town judges? + * No! It's impossible! If it's possible, suppose there are two town judges, then both of them have to be trusted by everyone else which includes the other judge. + * Otherwise, we'd have a town judge not trusted by everybody. + */ + public int findJudge(int n, int[][] trust) { + if (trust.length < n - 1) { + return -1; + } + int[] trustScores = new int[n]; + for (int[] t : trust) { + trustScores[t[1] - 1]++; + trustScores[t[0] - 1]--; + } + for (int i = 0; i < n; i++) { + if (trustScores[i] == n - 1) { + return i + 1; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_999.java b/src/main/java/com/fishercoder/solutions/firstthousand/_999.java new file mode 100644 index 0000000000..c4078bbca5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_999.java @@ -0,0 +1,81 @@ +package com.fishercoder.solutions.firstthousand; + +public class _999 { + public static class Solution1 { + int[] directions = new int[] {0, 1, 0, -1, 0}; + + public int numRookCaptures(char[][] board) { + int m = board.length; + int n = board[0].length; + int rowR = -1; + int colR = -1; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == 'R') { + rowR = i; + colR = j; + break; + } + } + } + int count = 0; + for (int i = 0; i < 4; i++) { + int neighborRow = rowR + directions[i]; + int neighborCol = colR + directions[i + 1]; + if (neighborRow >= 0 + && neighborRow < m + && neighborCol >= 0 + && neighborCol < n + && board[neighborRow][neighborCol] != 'B') { + if (directions[i] == 0 && directions[i + 1] == 1) { + while (neighborCol < n) { + if (board[neighborRow][neighborCol] == 'p') { + count++; + break; + } else if (board[neighborRow][neighborCol] == 'B') { + break; + } else { + neighborCol++; + } + } + } else if (directions[i] == 1 && directions[i + 1] == 0) { + while (neighborRow < m) { + if (board[neighborRow][neighborCol] == 'p') { + count++; + break; + } else if (board[neighborRow][neighborCol] == 'B') { + break; + } else { + neighborRow++; + } + } + } else if (directions[i] == 0 && directions[i + 1] == -1) { + while (neighborCol >= 0) { + if (board[neighborRow][neighborCol] == 'p') { + count++; + break; + } else if (board[neighborRow][neighborCol] == 'B') { + break; + } else { + neighborCol--; + } + } + } else { + while (neighborRow >= 0) { + if (board[neighborRow][neighborCol] == 'p') { + count++; + break; + } else if (board[neighborRow][neighborCol] == 'B') { + break; + } else { + neighborRow--; + } + } + } + } + } + + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3000.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3000.java new file mode 100644 index 0000000000..d2584c3e2e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3000.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3000 { + public static class Solution1 { + public int areaOfMaxDiagonal(int[][] dimensions) { + int ans = 0; + double maxDiagonal = 0.0; + for (int i = 0; i < dimensions.length; i++) { + int length = dimensions[i][0]; + int width = dimensions[i][1]; + double diagonal = Math.sqrt(length * length + width * width); + if (diagonal > maxDiagonal) { + maxDiagonal = diagonal; + ans = length * width; + } else if (diagonal == maxDiagonal && length * width > ans) { + ans = length * width; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3004.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3004.java new file mode 100644 index 0000000000..b72065466d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3004.java @@ -0,0 +1,81 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _3004 { + public static class Solution1 { + /* + * My completely original solution. + * Practice makes perfect! + * Post-order traversal is the way to go since we need to process all children first before processing any particular node. + */ + class ColoredTreeNode { + int val; + int color; + List children; + boolean allSubtreeSameColor; + int totalChildrenCount; + + public ColoredTreeNode(int val, int color) { + this.val = val; + this.color = color; + this.children = new ArrayList<>(); + this.allSubtreeSameColor = + true; // initialize to be true until it's built/proven to be false + this.totalChildrenCount = 1; // count itself as its own child + } + } + + int maxSize = 0; + + public int maximumSubtreeSize(int[][] edges, int[] colors) { + if (edges == null || edges.length == 0 || edges[0].length == 0) { + return colors.length > 0 ? 1 : 0; + } + ColoredTreeNode root = buildTree(edges, colors); + int totalNodeCount = postOrder(root); + if (root.allSubtreeSameColor) { + return totalNodeCount; + } + return maxSize; + } + + private int postOrder(ColoredTreeNode root) { + if (root == null) { + return 0; + } + int totalChildrenCount = 1; // count itself as a child + for (ColoredTreeNode child : root.children) { + int count = postOrder(child); + totalChildrenCount += count; + if (root.color != child.color || !child.allSubtreeSameColor) { + root.allSubtreeSameColor = false; + } + } + root.totalChildrenCount = totalChildrenCount; + if (root.allSubtreeSameColor) { + maxSize = Math.max(maxSize, root.totalChildrenCount); + } + return totalChildrenCount; + } + + private ColoredTreeNode buildTree(int[][] edges, int[] colors) { + Map map = new HashMap<>(); + for (int i = 0; i < edges.length; i++) { + ColoredTreeNode parent = + map.getOrDefault( + edges[i][0], new ColoredTreeNode(edges[i][0], colors[edges[i][0]])); + ColoredTreeNode child = + map.getOrDefault( + edges[i][1], new ColoredTreeNode(edges[i][1], colors[edges[i][1]])); + parent.children.add(child); + map.put(edges[i][0], parent); + map.put(edges[i][1], child); + } + return map.get(0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3005.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3005.java new file mode 100644 index 0000000000..b20987bb28 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3005.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; + +public class _3005 { + public static class Solution1 { + public int maxFrequencyElements(int[] nums) { + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + int maxFreq = 0; + for (int key : map.keySet()) { + if (map.get(key) > maxFreq) { + maxFreq = map.get(key); + } + } + int result = 0; + for (int key : map.keySet()) { + if (map.get(key) == maxFreq) { + result += map.get(key); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3006.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3006.java new file mode 100644 index 0000000000..906686977d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3006.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.List; + +public class _3006 { + public static class Solution1 { + public List beautifulIndices(String s, String a, String b, int k) { + List aIndices = new ArrayList<>(); + List bIndices = new ArrayList<>(); + for (int i = 0; i < s.length(); i++) { + if ((i + a.length()) <= s.length() && s.substring(i, i + a.length()).equals(a)) { + aIndices.add(i); + } + if ((i + b.length()) <= s.length() && s.substring(i, i + b.length()).equals(b)) { + bIndices.add(i); + } + } + List result = new ArrayList<>(); + for (int aIndex : aIndices) { + for (int bIndex : bIndices) { + if (Math.abs(aIndex - bIndex) <= k) { + result.add(aIndex); + break; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3010.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3010.java new file mode 100644 index 0000000000..ed04e5ba94 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3010.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.PriorityQueue; + +public class _3010 { + public static class Solution1 { + public int minimumCost(int[] nums) { + PriorityQueue minHeap = new PriorityQueue<>(); + for (int i = 1; i < nums.length; i++) { + minHeap.offer(nums[i]); + } + return nums[0] + minHeap.poll() + minHeap.poll(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3014.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3014.java new file mode 100644 index 0000000000..5a9dd17a37 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3014.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3014 { + public static class Solution1 { + + public int minimumPushes(String word) { + int keyCount = 8; + int times = 1; + int pushes = 0; + int len = word.length(); + while (len > 0) { + if (len < keyCount) { + pushes += times * len; + return pushes; + } else { + pushes += times * keyCount; + len -= keyCount; + times++; + } + } + return pushes; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java new file mode 100644 index 0000000000..88baa0a05f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; + +public class _3016 { + public static class Solution1 { + public int minimumPushes(String word) { + Map map = new HashMap<>(); + for (char c : word.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + PriorityQueue> maxHeap = + new PriorityQueue<>((a, b) -> b.getValue() - a.getValue()); + for (Map.Entry entry : map.entrySet()) { + maxHeap.offer(entry); + } + int[] possibleSets = new int[] {1, 2, 3, 4}; + int digitsLength = 8; // a total of 8 digits that can be assigned + Map assigned = new HashMap<>(); + for (int j = 0; j < possibleSets.length && !maxHeap.isEmpty(); j++) { + for (int i = 0; i < digitsLength && !maxHeap.isEmpty(); i++) { + Map.Entry curr = maxHeap.poll(); + assigned.put(curr.getKey(), possibleSets[j]); + } + } + int ans = 0; + for (Map.Entry entry : map.entrySet()) { + ans += entry.getValue() * assigned.get(entry.getKey()); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3019.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3019.java new file mode 100644 index 0000000000..11f470e7b4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3019.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3019 { + public static class Solution1 { + public int countKeyChanges(String s) { + int ans = 0; + for (int i = 1; i < s.length(); i++) { + if (Character.toLowerCase(s.charAt(i - 1)) != Character.toLowerCase(s.charAt(i))) { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3024.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3024.java new file mode 100644 index 0000000000..e892849491 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3024.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3024 { + public static class Solution1 { + public String triangleType(int[] nums) { + if (nums[0] == nums[1] && nums[1] == nums[2]) { + return "equilateral"; + } else { + if (!validTriangle(nums)) { + return "none"; + } else if (nums[0] == nums[1] || nums[0] == nums[2] || nums[1] == nums[2]) { + return "isosceles"; + } else { + return "scalene"; + } + } + } + + private boolean validTriangle(int[] nums) { + return nums[0] + nums[1] > nums[2] + && nums[1] + nums[2] > nums[0] + && nums[0] + nums[2] > nums[1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3028.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3028.java new file mode 100644 index 0000000000..107bfc4008 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3028.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3028 { + public static class Solution1 { + public int returnToBoundaryCount(int[] nums) { + int ans = 0; + int pos = 0; + for (int i = 0; i < nums.length; i++) { + pos += nums[i]; + if (pos == 0) { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3032.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3032.java new file mode 100644 index 0000000000..ecb042c9c9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3032.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashSet; +import java.util.Set; + +public class _3032 { + public static class Solution1 { + public int numberCount(int a, int b) { + int ans = 0; + for (int num = a; num <= b; num++) { + if (isUniqeDigits(num)) { + ans++; + } + } + return ans; + } + + private boolean isUniqeDigits(int num) { + Set seen = new HashSet<>(); + while (num != 0) { + if (!seen.add(num % 10)) { + return false; + } + num /= 10; + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3033.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3033.java new file mode 100644 index 0000000000..3d52ad0b7e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3033.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3033 { + public static class Solution1 { + public int[][] modifiedMatrix(int[][] matrix) { + if (matrix == null || matrix.length == 0) { + return matrix; + } + int m = matrix.length; + int n = matrix[0].length; + int[][] answer = new int[m][n]; + for (int j = 0; j < n; j++) { + int max = -1; + for (int i = 0; i < m; i++) { + max = Math.max(max, matrix[i][j]); + answer[i][j] = matrix[i][j]; + } + for (int i = 0; i < m; i++) { + if (matrix[i][j] == -1) { + answer[i][j] = max; + } + } + } + return answer; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3038.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3038.java new file mode 100644 index 0000000000..13527eaf1c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3038.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3038 { + public static class Solution1 { + public int maxOperations(int[] nums) { + int maxOps = 0; + if (nums == null || nums.length < 2) { + return maxOps; + } + maxOps++; + int sum = nums[0] + nums[1]; + for (int i = 2; i < nums.length - 1; i += 2) { + if (nums[i] + nums[i + 1] == sum) { + maxOps++; + } else { + break; + } + } + return maxOps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3042.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3042.java new file mode 100644 index 0000000000..b6911c7a15 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3042.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3042 { + public static class Solution1 { + public int countPrefixSuffixPairs(String[] words) { + int pairs = 0; + for (int i = 0; i < words.length - 1; i++) { + for (int j = i + 1; j < words.length; j++) { + if (isPrefixAndSuffix(words[i], words[j])) { + pairs++; + } + } + } + return pairs; + } + + private boolean isPrefixAndSuffix(String word1, String word2) { + if (word1.length() > word2.length()) { + return false; + } + return word2.startsWith(word1) && word2.endsWith(word1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3046.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3046.java new file mode 100644 index 0000000000..82059228d1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3046.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; + +public class _3046 { + public static class Solution1 { + public boolean isPossibleToSplit(int[] nums) { + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + if (map.get(num) > 2) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3062.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3062.java new file mode 100644 index 0000000000..bf180d3bed --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3062.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.fourththousand; + +import com.fishercoder.common.classes.ListNode; + +public class _3062 { + public static class Solution1 { + public String gameResult(ListNode head) { + int oddPoints = 0; + int evenPoints = 0; + ListNode even = head; + ListNode odd = head.next; + while (odd != null && even != null) { + if (even.val > odd.val) { + evenPoints++; + } else { + oddPoints++; + } + if (even.next != null && odd.next != null) { + even = even.next.next; + odd = odd.next.next; + } else { + break; + } + } + return evenPoints > oddPoints ? "Even" : evenPoints == oddPoints ? "Tie" : "Odd"; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3063.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3063.java new file mode 100644 index 0000000000..06f183f572 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3063.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.fourththousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.HashMap; +import java.util.Map; + +public class _3063 { + public static class Solution1 { + public ListNode frequenciesOfElements(ListNode head) { + Map map = new HashMap<>(); + while (head != null) { + map.put(head.val, map.getOrDefault(head.val, 0) + 1); + head = head.next; + } + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + for (Map.Entry entry : map.entrySet()) { + tmp.next = new ListNode(entry.getValue()); + tmp = tmp.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3065.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3065.java new file mode 100644 index 0000000000..04c742ed78 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3065.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.PriorityQueue; + +public class _3065 { + public static class Solution1 { + public int minOperations(int[] nums, int k) { + PriorityQueue minHeap = new PriorityQueue<>(); + for (int num : nums) { + minHeap.offer(num); + } + int ops = 0; + while (!minHeap.isEmpty() && minHeap.peek() < k) { + minHeap.poll(); + ops++; + } + return ops; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3069.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3069.java new file mode 100644 index 0000000000..8f26a3e7dc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3069.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.List; + +public class _3069 { + public static class Solution1 { + public int[] resultArray(int[] nums) { + int[] ans = new int[nums.length]; + List list1 = new ArrayList<>(); + list1.add(nums[0]); + List list2 = new ArrayList<>(); + list2.add(nums[1]); + for (int i = 2; i < nums.length; i++) { + if (list1.get(list1.size() - 1) > list2.get(list2.size() - 1)) { + list1.add(nums[i]); + } else { + list2.add(nums[i]); + } + } + int i = 0; + for (int j = 0; j < list1.size(); j++) { + ans[i++] = list1.get(j); + } + for (int j = 0; j < list2.size(); j++) { + ans[i++] = list2.get(j); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3074.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3074.java new file mode 100644 index 0000000000..25813a6d64 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3074.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Arrays; + +public class _3074 { + public static class Solution1 { + public int minimumBoxes(int[] apple, int[] capacity) { + int apples = 0; + for (int app : apple) { + apples += app; + } + Arrays.sort(capacity); + int boxes = 0; + for (int i = capacity.length - 1; i >= 0; i--) { + boxes++; + apples -= capacity[i]; + if (apples <= 0) { + return boxes; + } + } + return boxes; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3079.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3079.java new file mode 100644 index 0000000000..a8090a4d60 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3079.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3079 { + public static class Solution1 { + public int sumOfEncryptedInt(int[] nums) { + int sum = 0; + for (int num : nums) { + sum += encrypt(num); + } + return sum; + } + + private int encrypt(int num) { + int max = 0; + int digits = 0; + while (num != 0) { + max = Math.max(max, num % 10); + num /= 10; + digits++; + } + int ans = 0; + int base = 1; + while (digits > 0) { + ans += base * max; + digits--; + base *= 10; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3083.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3083.java new file mode 100644 index 0000000000..b0668303ee --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3083.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3083 { + public static class Solution1 { + public boolean isSubstringPresent(String s) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length() - 1; i++) { + sb.append(s.charAt(i + 1)); + sb.append(s.charAt(i)); + if (s.indexOf(sb.toString()) != -1) { + return true; + } + sb.setLength(0); + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3090.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3090.java new file mode 100644 index 0000000000..6bcaaf1454 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3090.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3090 { + public static class Solution1 { + public int maximumLengthSubstring(String s) { + int max = 0; + int[] count = new int[26]; + for (int left = 0, right = 0; right < s.length() && left < s.length(); ) { + if (qualified(count, s.charAt(right))) { + max = Math.max(max, right - left + 1); + right++; + } else { + count[s.charAt(left) - 'a']--; + left++; + } + } + return max; + } + + private boolean qualified(int[] count, char charAt) { + count[charAt - 'a']++; + for (int c : count) { + if (c > 2) { + count[charAt - 'a']--; + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3095.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3095.java new file mode 100644 index 0000000000..e20dbe09d3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3095.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3095 { + public static class Solution1 { + public int minimumSubarrayLength(int[] nums, int k) { + int min = Integer.MAX_VALUE; + for (int i = 0; i < nums.length; i++) { + if (nums[i] >= k) { + return 1; + } + int or = nums[i]; + for (int j = i + 1; j < nums.length; j++) { + or |= nums[j]; + if (or >= k) { + min = Math.min(min, j - i + 1); + } + } + } + return min == Integer.MAX_VALUE ? -1 : min; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3099.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3099.java new file mode 100644 index 0000000000..9f3f49da8b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3099.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3099 { + public static class Solution1 { + public int sumOfTheDigitsOfHarshadNumber(int x) { + int sum = 0; + int tmp = x; + while (tmp != 0) { + sum += tmp % 10; + tmp /= 10; + } + return x % sum == 0 ? sum : -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3110.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3110.java new file mode 100644 index 0000000000..c97f42933a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3110.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3110 { + public static class Solution1 { + public int scoreOfString(String s) { + int score = 0; + for (int i = 0; i < s.length() - 1; i++) { + int asciiVal1 = s.charAt(i); + int asciiVal2 = s.charAt(i + 1); + score += Math.abs(asciiVal1 - asciiVal2); + } + return score; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3112.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3112.java new file mode 100644 index 0000000000..0ca5a2104f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3112.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.PriorityQueue; + +public class _3112 { + public static class Solution1 { + /* + * My completely original solution: Dijkstra's algorithm! + */ + public int[] minimumTime(int n, int[][] edges, int[] disappear) { + List[] graph = new ArrayList[n]; + for (int i = 0; i < n; i++) { + graph[i] = new ArrayList<>(); + } + for (int[] edge : edges) { + graph[edge[0]].add(new int[] {edge[1], edge[2]}); + graph[edge[1]].add(new int[] {edge[0], edge[2]}); + } + int[] ans = new int[n]; + int[] shortestTimes = new int[disappear.length]; + Arrays.fill(shortestTimes, Integer.MAX_VALUE); + shortestTimes[0] = 0; + dijkstra(graph, disappear, shortestTimes); + for (int target = 1; target < n; target++) { + if (shortestTimes[target] == Integer.MAX_VALUE + || shortestTimes[target] >= disappear[target]) { + ans[target] = -1; + } else { + ans[target] = shortestTimes[target]; + } + } + return ans; + } + + private void dijkstra(List[] graph, int[] disappear, int[] shortestTimes) { + PriorityQueue q = new PriorityQueue<>((a, b) -> a[1] - b[1]); + q.offer(new int[] {0, 0}); + while (!q.isEmpty()) { + int[] curr = q.poll(); + int currNode = curr[0]; + int currCost = curr[1]; + if (currCost > shortestTimes[currNode]) { + continue; + } + for (int[] neighbor : graph[currNode]) { + int neighborNode = neighbor[0]; + int neighborCost = neighbor[1]; + if (neighborCost + currCost < shortestTimes[neighborNode] + && neighborCost + currCost < disappear[neighborNode]) { + shortestTimes[neighborNode] = neighborCost + currCost; + q.offer(new int[] {neighborNode, shortestTimes[neighborNode]}); + } + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3114.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3114.java new file mode 100644 index 0000000000..b091c0053a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3114.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3114 { + public static class Solution1 { + public String findLatestTime(String s) { + char[] arr = s.toCharArray(); + if (arr[0] == '?') { + if (arr[1] == '?' || arr[1] == '0' || arr[1] == '1') { + arr[0] = '1'; + } else { + arr[0] = '0'; + } + } + if (arr[1] == '?') { + if (arr[0] == '1') { + arr[1] = '1'; + } else { + arr[1] = '9'; + } + } + if (arr[3] == '?') { + arr[3] = '5'; + } + if (arr[4] == '?') { + arr[4] = '9'; + } + return new String(arr); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3120.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3120.java new file mode 100644 index 0000000000..d71c131474 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3120.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3120 { + public static class Solution1 { + public int numberOfSpecialChars(String word) { + int count = 0; + int[] lower = new int[26]; + int[] upper = new int[26]; + for (char c : word.toCharArray()) { + if (Character.isLowerCase(c)) { + lower[c - 'a']++; + } else { + upper[c - 'A']++; + } + } + for (int i = 0; i < 26; i++) { + if (lower[i] != 0 && upper[i] != 0) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3127.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3127.java new file mode 100644 index 0000000000..191fe6a6f9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3127.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3127 { + public static class Solution1 { + public boolean canMakeSquare(char[][] grid) { + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + if (isPossible(grid, i, j, 'B') || isPossible(grid, i, j, 'W')) { + return true; + } + } + } + return false; + } + + private boolean isPossible(char[][] grid, int startI, int startJ, char color) { + int count = 0; + for (int i = startI; i < startI + 2; i++) { + for (int j = startJ; j < startJ + 2; j++) { + if (grid[i][j] == color) { + count++; + } + } + } + return count >= 3; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3131.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3131.java new file mode 100644 index 0000000000..6d6fc7b2ca --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3131.java @@ -0,0 +1,13 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Arrays; + +public class _3131 { + public static class Solution1 { + public int addedInteger(int[] nums1, int[] nums2) { + Arrays.sort(nums1); + Arrays.sort(nums2); + return nums2[0] - nums1[0]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3136.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3136.java new file mode 100644 index 0000000000..c5fdc62b7f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3136.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _3136 { + public static class Solution1 { + public boolean isValid(String word) { + if (word.length() < 3) { + return false; + } + Set vowels = + new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')); + boolean containsVowel = false; + boolean containsConsonant = false; + for (char c : word.toCharArray()) { + if (vowels.contains(c)) { + containsVowel = true; + } else if (Character.isAlphabetic(c)) { + containsConsonant = true; + } else if (!Character.isDigit(c)) { + return false; + } + } + return containsVowel && containsConsonant; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3142.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3142.java new file mode 100644 index 0000000000..bc83eeab1e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3142.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3142 { + public static class Solution1 { + public boolean satisfiesConditions(int[][] grid) { + for (int i = 0; i < grid.length - 1; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] != grid[i + 1][j]) { + return false; + } + } + } + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length - 1; j++) { + if (grid[i][j] == grid[i][j + 1]) { + return false; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3146.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3146.java new file mode 100644 index 0000000000..a4571d55dd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3146.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; + +public class _3146 { + public static class Solution1 { + public int findPermutationDifference(String s, String t) { + Map map = new HashMap<>(); + for (int i = 0; i < s.length(); i++) { + map.put(s.charAt(i), i); + } + int sum = 0; + for (int i = 0; i < t.length(); i++) { + sum += Math.abs(map.get(t.charAt(i)) - i); + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3151.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3151.java new file mode 100644 index 0000000000..3a839a23e0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3151.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3151 { + public static class Solution1 { + public boolean isArraySpecial(int[] nums) { + for (int i = 1; i < nums.length; i++) { + if (nums[i - 1] % 2 == nums[i] % 2) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3157.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3157.java new file mode 100644 index 0000000000..30ae1265af --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3157.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.fourththousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; +import java.util.TreeMap; + +public class _3157 { + public static class Solution1 { + public int minimumLevel(TreeNode root) { + Queue q = new LinkedList<>(); + q.offer(root); + TreeMap treeMap = new TreeMap<>(); + int level = 1; + while (!q.isEmpty()) { + int size = q.size(); + long sum = 0L; + for (int i = 0; i < size; i++) { + TreeNode curr = q.poll(); + sum += curr.val; + if (curr.left != null) { + q.offer(curr.left); + } + if (curr.right != null) { + q.offer(curr.right); + } + } + if (!treeMap.containsKey(sum)) { + treeMap.put(sum, level); + } + level++; + } + return treeMap.firstEntry().getValue(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3158.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3158.java new file mode 100644 index 0000000000..a234b5e594 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3158.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashSet; +import java.util.Set; + +public class _3158 { + public static class Solution1 { + public int duplicateNumbersXOR(int[] nums) { + int ans = 0; + Set met = new HashSet<>(); + for (int num : nums) { + if (!met.add(num)) { + ans ^= num; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3162.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3162.java new file mode 100644 index 0000000000..79466473be --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3162.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3162 { + public static class Solution1 { + public int numberOfPairs(int[] nums1, int[] nums2, int k) { + int count = 0; + for (int i = 0; i < nums1.length; i++) { + for (int j = 0; j < nums2.length; j++) { + if (nums1[i] % (nums2[j] * k) == 0) { + count++; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3164.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3164.java new file mode 100644 index 0000000000..1fbe56dd22 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3164.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; + +public class _3164 { + public static class Solution1 { + public long numberOfPairs(int[] nums1, int[] nums2, int k) { + long count = 0; + Map map = new HashMap<>(); + for (int num : nums2) { + int product = num * k; + map.put(product, map.getOrDefault(product, 0) + 1); + } + for (int num : nums1) { + for (int j = 1; j * j <= num; j++) { + if (num % j == 0) { + if (map.containsKey(j)) { + count += map.get(j); + } + int division = num / j; + if (j != division && map.containsKey(division)) { + count += map.get(division); + } + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3168.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3168.java new file mode 100644 index 0000000000..e0e9423f62 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3168.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3168 { + public static class Solution1 { + public int minimumChairs(String s) { + int seated = 0; + int seats = 0; + for (char c : s.toCharArray()) { + if (c == 'E') { + seated++; + } else { + seated--; + } + seats = Math.max(seated, seats); + } + return seats; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3173.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3173.java new file mode 100644 index 0000000000..3a72052263 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3173.java @@ -0,0 +1,13 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3173 { + public static class Solution1 { + public int[] orArray(int[] nums) { + int[] result = new int[nums.length - 1]; + for (int i = 1; i < nums.length; i++) { + result[i - 1] = nums[i] | nums[i - 1]; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3174.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3174.java new file mode 100644 index 0000000000..3bd302297d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3174.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _3174 { + public static class Solution1 { + public String clearDigits(String s) { + Deque stack = new LinkedList<>(); + for (char c : s.toCharArray()) { + if (Character.isDigit(c)) { + if (!stack.isEmpty()) { + Deque temp = new LinkedList<>(); + while (!stack.isEmpty() && Character.isDigit(stack.peekLast())) { + temp.addLast(stack.pollLast()); + } + if (!stack.isEmpty() && !Character.isDigit(stack.peekLast())) { + stack.pollLast(); + while (!temp.isEmpty()) { + stack.addLast(temp.pollLast()); + } + } + } + } else { + stack.addLast(c); + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append(stack.pollLast()); + } + return sb.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3175.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3175.java new file mode 100644 index 0000000000..f7a69eeec2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3175.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _3175 { + public static class Solution1 { + public int findWinningPlayer(int[] skills, int k) { + Deque q = new LinkedList<>(); + int highestSkill = 0; + for (int i = 0; i < skills.length; i++) { + q.offer(new int[] {i, skills[i], 0}); + highestSkill = Math.max(highestSkill, skills[i]); + } + int count = 0; + while (true) { + int[] first = q.pollFirst(); + if (first[1] == highestSkill) { + // if the highest skill stands at the head of the queue, then it'll keep + // standing there + // so it's guaranteed that it'll be the winner + return first[0]; + } + int[] second = q.pollFirst(); + if (first[2] >= k) { + return first[0]; + } + if (first[1] > second[1]) { + first[2]++; + q.addLast(second); + q.addFirst(first); + } else { + second[2]++; + q.addFirst(second); + q.addLast(first); + } + count++; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3178.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3178.java new file mode 100644 index 0000000000..dc3f822044 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3178.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3178 { + public static class Solution1 { + public int numberOfChild(int n, int k) { + // decrement by 1 to make it easier to do math so it becomes o to n - 1 + n--; + int roundTrips = k / n; + int remainingSteps = k % n; + if (roundTrips % 2 == 0) { + // this means it's forward direction + return remainingSteps; + } else { + // this means it's reverse direction + return n - remainingSteps; + } + } + } + + public static class Solution2 { + /* + * Also, my completely original solution, much more elegant and efficient. + */ + public int numberOfChild(int n, int k) { + // n - 1 is the number of steps is takes to finish from one end to the other + // 2 * (n - 1) is the whole round trip, so after this, it's back to the starting point + // so we only need to handle the modulo remainder of 2 * (n - 1) + k = k % ((n - 1) * 2); + if (k < n) { + // in this case, we can directly return k + return k; + } else { + // in this case, it's in the reverse direction, we deduct the number of steps needed + // to finish the forward direction first + k -= n - 1; + // then return the correct child index + return n - k - 1; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3184.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3184.java new file mode 100644 index 0000000000..c2b145b398 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3184.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3184 { + public static class Solution1 { + public int countCompleteDayPairs(int[] hours) { + int count = 0; + for (int i = 0; i < hours.length - 1; i++) { + for (int j = i + 1; j < hours.length; j++) { + if ((hours[i] + hours[j]) % 24 == 0) { + count++; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3185.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3185.java new file mode 100644 index 0000000000..91b5959956 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3185.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3185 { + public static class Solution1 { + public long countCompleteDayPairs(int[] hours) { + int[] remainderCounts = new int[24]; + long pairs = 0L; + for (int hour : hours) { + int remainder = hour % 24; + int complement = (24 - remainder) % 24; + pairs += remainderCounts[complement]; + remainderCounts[remainder]++; + } + return pairs; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3186.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3186.java new file mode 100644 index 0000000000..bf8de241a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3186.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _3186 { + public static class Solution1 { + public long maximumTotalDamage(int[] power) { + TreeMap treeMap = new TreeMap<>(); + for (int p : power) { + treeMap.put(p, treeMap.getOrDefault(p, 0) + 1); + } + List sortedList = new ArrayList<>(treeMap.keySet()); + long[] dp = new long[sortedList.size()]; + dp[0] = (long) sortedList.get(0) * treeMap.get(sortedList.get(0)); + for (int i = 1; i < sortedList.size(); i++) { + int currentPower = sortedList.get(i); + long currentDamage = (long) currentPower * treeMap.get(currentPower); + // from i - 1, all the way to the left of this sorted list, check to find the + // nearest valid power + // using this test case: new int[]{7, 1, 6, 3}, would easily illustrate this idea + // dp[i] holds the maximum possible damage for up to sortedList[i] + int j = i - 1; + while (j >= 0 && sortedList.get(j) >= currentPower - 2) { + j--; + } + if (j >= 0) { + dp[i] = Math.max(dp[i - 1], currentDamage + dp[j]); + } else { + dp[i] = Math.max(dp[i - 1], currentDamage); + } + } + return dp[dp.length - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3189.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3189.java new file mode 100644 index 0000000000..10ad868271 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3189.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Arrays; + +public class _3189 { + public static class Solution1 { + /* + * Greedy is the way to go for this problem. + */ + public int minMoves(int[][] rooks) { + Arrays.sort(rooks, (a, b) -> a[0] - b[0]); + int len = rooks.length; + int moves = 0; + for (int i = 0; i < len; i++) { + int[] rook = rooks[i]; + // move each rook to row i + moves += Math.abs(rook[0] - i); + } + Arrays.sort(rooks, (a, b) -> a[1] - b[1]); + for (int i = 0; i < len; i++) { + int[] rook = rooks[i]; + // move each rook to its column i + moves += Math.abs(rook[1] - i); + } + return moves; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3190.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3190.java new file mode 100644 index 0000000000..b6cb8707ab --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3190.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3190 { + public static class Solution1 { + public int minimumOperations(int[] nums) { + int ops = 0; + for (int num : nums) { + if (num % 3 != 0) { + ops++; + } + } + return ops; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3191.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3191.java new file mode 100644 index 0000000000..e06e226fd9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3191.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3191 { + public static class Solution1 { + public int minOperations(int[] nums) { + int ops = 0; + for (int i = 0; i < nums.length - 2; i++) { + if (nums[i] == 0) { + ops++; + flipThree(nums, i); + } + } + if (allOnes(nums)) { + return ops; + } + return -1; + } + + private boolean allOnes(int[] nums) { + for (int num : nums) { + if (num == 0) { + return false; + } + } + return true; + } + + private void flipThree(int[] nums, int start) { + for (int i = start; i <= start + 2; i++) { + if (nums[i] == 0) { + nums[i] = 1; + } else { + nums[i] = 0; + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3192.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3192.java new file mode 100644 index 0000000000..b69e9878ea --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3192.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3192 { + public static class Solution1 { + /* + * 1. Go from left to right; + * 2. The only way to flip the entire array to be 1s is to change each nums[i] = 0 to nums[i] = 1 whenever we encounter a 0; + * 3. if we flip each number twice, it's back to its original number, so we only need to keep track of how many times each number is flipped instead of actually flipping the number; + * 4. keep the original array intact, check two conditions: + * nums[i] is 0 or 1 + * ops is odd or even + */ + public int minOperations(int[] nums) { + int ops = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] == 0 && ops % 2 == 0) { + // this means after an even number of flipping, this number is (originally) a + // zero, so we need to flip it and all the numbers to its right + ops++; + } + if (nums[i] == 1 && ops % 2 == 1) { + // this means after an odd number of flipping prior to reaching this number and + // this number is a one, so it should have been flipped to become a zero, so we + // need to flip it + ops++; + } + } + return ops; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3194.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3194.java new file mode 100644 index 0000000000..f14dfbdf8a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3194.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Arrays; +import java.util.PriorityQueue; + +public class _3194 { + public static class Solution1 { + public double minimumAverage(int[] nums) { + PriorityQueue minHeap = new PriorityQueue<>(); + Arrays.sort(nums); + int left = 0; + int right = nums.length - 1; + while (left < right) { + double ave = ((double) nums[left] + nums[right]) / 2; + minHeap.offer(ave); + left++; + right--; + } + return minHeap.poll(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3195.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3195.java new file mode 100644 index 0000000000..66fd61670f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3195.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3195 { + public static class Solution1 { + /* + * My completely original solution: + * 1. project all 1's to each of the four sides; + * 2. use four variables to denote four corners + */ + public int minimumArea(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + int top = m - 1; + int bottom = 0; + int left = n - 1; + int right = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + bottom = Math.max(i, bottom); + right = Math.max(j, right); + top = Math.min(top, i); + left = Math.min(j, left); + } + } + } + return (right - left + 1) * (bottom - top + 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3196.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3196.java new file mode 100644 index 0000000000..0042aec59d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3196.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3196 { + public static class Solution1 { + /* + * I knew it's a DP problem, I was close to figuring out the recurrence relationship. + *

+ * Credit: https://leetcode.com/problems/maximize-total-cost-of-alternating-subarrays/solutions/5355138/dynamic-programming-and-space-optimized-beats-100-easy-to-understand/ + */ + public long maximumTotalCost(int[] nums) { + int len = nums.length; + long add = nums[0]; + long subtract = nums[0]; + for (int i = 1; i < len; i++) { + long newAdd = Math.max(add, subtract) + nums[i]; + long newSubtract = add - nums[i]; + + add = newAdd; + subtract = newSubtract; + } + return Math.max(add, subtract); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3199.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3199.java new file mode 100644 index 0000000000..274969f1c9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3199.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3199 { + public static class Solution1 { + public int tripletCount(int[] a, int[] b, int[] c) { + int count = 0; + for (int i = 0; i < a.length; i++) { + for (int j = 0; j < b.length; j++) { + for (int k = 0; k < c.length; k++) { + int xor = a[i] ^ b[j] ^ c[k]; + if (evenSetBits(xor)) { + count++; + } + } + } + } + return count; + } + + private boolean evenSetBits(int num) { + int bits = 0; + // this is the idea of calculating hamming weight: + // https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_191.java#L16_L23 + while (num != 0) { + bits++; + num &= num - 1; + } + return bits % 2 == 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3200.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3200.java new file mode 100644 index 0000000000..75cd1445a3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3200.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3200 { + public static class Solution1 { + public int maxHeightOfTriangle(int red, int blue) { + return Math.max(getHeight(red, blue), getHeight(blue, red)); + } + + private int getHeight(int first, int second) { + int height = 1; + boolean useFirst = true; + while (first > 0 || second > 0) { + if (useFirst) { + if (first >= height) { + first -= height; + } else { + break; + } + } else { + if (second >= height) { + second -= height; + } else { + break; + } + } + height++; + useFirst = !useFirst; + } + return height - 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3206.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3206.java new file mode 100644 index 0000000000..f26e242429 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3206.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3206 { + public static class Solution1 { + public int numberOfAlternatingGroups(int[] colors) { + int result = 0; + int len = colors.length; + for (int i = 0; i < len; i++) { + if (i == 0) { + if (colors[i] != colors[len - 1] && colors[i] != colors[i + 1]) { + result++; + } + } else if (i < len - 1) { + if (colors[i] != colors[i - 1] && colors[i] != colors[i + 1]) { + result++; + } + } else { + if (colors[i] != colors[i - 1] && colors[i] != colors[0]) { + result++; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3208.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3208.java new file mode 100644 index 0000000000..3be269ce96 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3208.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3208 { + public static class Solution1 { + /* + * My completely original solution: + * we just keep looking for the possible k alternating groups, if it encounters the same color, then set i to that pointer and restart. + */ + public int numberOfAlternatingGroups(int[] colors, int k) { + int len = colors.length; + int groups = 0; + int i = 0; + for (; i < len; i++) { + int j = i + 1; + for (; j < len + k - 1; j++) { + if (colors[j % len] == colors[(j - 1) % len]) { + break; + } + if (j - i + 1 >= k) { + groups++; + } + } + i = j - 1; + } + return groups; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3210.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3210.java new file mode 100644 index 0000000000..f7728e994e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3210.java @@ -0,0 +1,13 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3210 { + public static class Solution1 { + public String getEncryptedString(String s, int k) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + sb.append(s.charAt((i + k) % s.length())); + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3211.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3211.java new file mode 100644 index 0000000000..dc97fca560 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3211.java @@ -0,0 +1,67 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _3211 { + public static class Solution1 { + public List validStrings(int n) { + List result = new ArrayList<>(); + for (int i = n / 2; i <= n; i++) { + List combinations = generateCombinations(i, n - i); + for (String s : combinations) { + if (noAdjacentZeroes(s)) { + result.add(s); + } + } + } + return result; + } + + private boolean noAdjacentZeroes(String s) { + for (int i = 0; i < s.length() - 1; i++) { + if (s.charAt(i) == '0' && s.charAt(i + 1) == '0') { + return false; + } + } + return true; + } + + private List generateCombinations(int ones, int zeroes) { + int[] nums = new int[ones + zeroes]; + int i = 0; + while (ones-- > 0) { + nums[i++] = 1; + } + return permuteUnique(nums); + } + + private List permuteUnique(int[] nums) { + Set set = new HashSet<>(); + set.add(""); + set = recurse(nums, set, 0); + List list = new ArrayList<>(); + for (String s : set) { + list.add(s); + } + return list; + } + + private Set recurse(int[] nums, Set set, int pos) { + if (pos == nums.length) { + return set; + } + Set newSet = new HashSet<>(); + for (String s : set) { + for (int i = 0; i <= s.length(); i++) { + StringBuilder sb = new StringBuilder(s); + sb.insert(i, nums[pos]); + newSet.add(sb.toString()); + } + } + return recurse(nums, newSet, pos + 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3212.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3212.java new file mode 100644 index 0000000000..5a172d7948 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3212.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3212 { + public static class Solution1 { + /* + * My completely original solution: (although it could be further optimized.) + * use a 3-d array, dp[i][j][0] means the number of x's and dp[i][j][1] means the number of y's startring from (0,0) all the way to (i,j) + * then how to compute prefix sum: + * I used two steps in sequence: + * first: I calculate the number of x's and y's for each row; + * second: I sum up both x's and y's from its previous row with its current row + */ + public int numberOfSubmatrices(char[][] grid) { + int count = 0; + int m = grid.length; + int n = grid[0].length; + int[][][] dp = new int[m][n][2]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 'X') { + dp[i][j][0]++; + for (int k = j + 1; k < n; k++) { + dp[i][k][0]++; + } + } else if (grid[i][j] == 'Y') { + dp[i][j][1]++; + for (int k = j + 1; k < n; k++) { + dp[i][k][1]++; + } + } + } + } + for (int i = 1; i < m; i++) { + for (int j = 0; j < n; j++) { + dp[i][j][0] += dp[i - 1][j][0]; + dp[i][j][1] += dp[i - 1][j][1]; + } + } + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (dp[i][j][0] != 0 && dp[i][j][0] == dp[i][j][1]) { + count++; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3216.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3216.java new file mode 100644 index 0000000000..827e151dfc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3216.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3216 { + public static class Solution1 { + public String getSmallestString(String s) { + for (int i = 0; i < s.length() - 1; i++) { + if (s.charAt(i) > s.charAt(i + 1) && sameParity(s.charAt(i), s.charAt(i + 1))) { + char[] charArray = s.toCharArray(); + char tmp = charArray[i]; + charArray[i] = charArray[i + 1]; + charArray[i + 1] = tmp; + return new String(charArray); + } + } + return s; + } + + private boolean sameParity(char c1, char c2) { + int num1 = Integer.parseInt(c1 + ""); + int num2 = Integer.parseInt(c2 + ""); + if (num2 % 2 == 0 && num1 % 2 == 0 || (num2 % 2 == 1 && num1 % 2 == 1)) { + return true; + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3217.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3217.java new file mode 100644 index 0000000000..1ead3cd257 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3217.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.fourththousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _3217 { + public static class Solution1 { + public ListNode modifiedList(int[] nums, ListNode head) { + Set set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + ListNode tmp = head; + List list = new ArrayList<>(); + while (tmp != null) { + if (!set.contains(tmp.val)) { + list.add(tmp.val); + } + tmp = tmp.next; + } + if (list.size() == 0) { + return null; + } + ListNode pre = new ListNode(-1); + ListNode tmp2 = new ListNode(list.get(0)); + pre.next = tmp2; + for (int i = 1; i < list.size(); i++) { + tmp2.next = new ListNode(list.get(i)); + tmp2 = tmp2.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3218.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3218.java new file mode 100644 index 0000000000..88e362100c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3218.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.PriorityQueue; + +public class _3218 { + public static class Solution1 { + /* + * My completely original solution. + */ + public int minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) { + PriorityQueue maxHeapHorizontal = new PriorityQueue<>((a, b) -> b - a); + for (int cut : horizontalCut) { + maxHeapHorizontal.offer(cut); + } + PriorityQueue maxHeapVertical = new PriorityQueue<>((a, b) -> b - a); + for (int cut : verticalCut) { + maxHeapVertical.offer(cut); + } + int verticalParts = 1; + int horizontalParts = 1; + int cost = 0; + while (!maxHeapHorizontal.isEmpty() || !maxHeapVertical.isEmpty()) { + Integer curr; + if (!maxHeapHorizontal.isEmpty() && !maxHeapVertical.isEmpty()) { + if (maxHeapHorizontal.peek() > maxHeapVertical.peek()) { + curr = maxHeapHorizontal.poll(); + cost += curr * verticalParts; + horizontalParts++; + } else { + curr = maxHeapVertical.poll(); + cost += curr * horizontalParts; + verticalParts++; + } + } else if (!maxHeapHorizontal.isEmpty()) { + curr = maxHeapHorizontal.poll(); + cost += curr * verticalParts; + horizontalParts++; + } else { + curr = maxHeapVertical.poll(); + cost += curr * horizontalParts; + verticalParts++; + } + } + return cost; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3219.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3219.java new file mode 100644 index 0000000000..63fda748c3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3219.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.PriorityQueue; + +public class _3219 { + public static class Solution1 { + /* + * My completely original solution. + */ + public long minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) { + PriorityQueue maxHeapHorizontal = new PriorityQueue<>((a, b) -> b - a); + for (int cut : horizontalCut) { + maxHeapHorizontal.offer(cut); + } + PriorityQueue maxHeapVertical = new PriorityQueue<>((a, b) -> b - a); + for (int cut : verticalCut) { + maxHeapVertical.offer(cut); + } + int verticalParts = 1; + int horizontalParts = 1; + long cost = 0L; + while (!maxHeapHorizontal.isEmpty() || !maxHeapVertical.isEmpty()) { + Integer curr; + if (!maxHeapHorizontal.isEmpty() && !maxHeapVertical.isEmpty()) { + if (maxHeapHorizontal.peek() > maxHeapVertical.peek()) { + curr = maxHeapHorizontal.poll(); + cost += curr * verticalParts; + horizontalParts++; + } else { + curr = maxHeapVertical.poll(); + cost += curr * horizontalParts; + verticalParts++; + } + } else if (!maxHeapHorizontal.isEmpty()) { + curr = maxHeapHorizontal.poll(); + cost += curr * verticalParts; + horizontalParts++; + } else { + curr = maxHeapVertical.poll(); + cost += curr * horizontalParts; + verticalParts++; + } + } + return cost; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3222.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3222.java new file mode 100644 index 0000000000..7b663eb348 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3222.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3222 { + public static class Solution1 { + public String losingPlayer(int x, int y) { + boolean bobsTurn = true; + while (x >= 1 && y >= 4) { + x--; + y -= 4; + bobsTurn = !bobsTurn; + } + return !bobsTurn ? "Alice" : "Bob"; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3223.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3223.java new file mode 100644 index 0000000000..ec55b1de0c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3223.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Deque; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +public class _3223 { + public static class Solution1 { + public int minimumLength(String s) { + Map map = new HashMap<>(); + Deque stack = new LinkedList<>(); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (!map.containsKey(c) || map.get(c) < 2) { + map.put(c, map.getOrDefault(c, 0) + 1); + stack.addLast(c); + } else { + Deque tmpStack = new LinkedList<>(); + int removedCount = 0; + while (!stack.isEmpty()) { + Character last = stack.pollLast(); + if (last == c) { + map.put(c, map.get(c) - 1); + if (map.get(c) == 0) { + map.remove(c); + } + removedCount++; + if (removedCount == 2) { + break; + } + } else { + tmpStack.addLast(last); + } + } + while (!tmpStack.isEmpty()) { + stack.addLast(tmpStack.pollLast()); + } + stack.addLast(c); + map.put(c, map.getOrDefault(c, 0) + 1); + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append(stack.pollFirst()); + } + return sb.toString().length(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3224.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3224.java new file mode 100644 index 0000000000..a977a9ed1f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3224.java @@ -0,0 +1,68 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _3224 { + public static class Solution1 { + /* + * My completely original solution during the contest. + */ + public int minChanges(int[] nums, int k) { + // compute the frequency of each diff + Map map = new HashMap<>(); + for (int i = 0; i < nums.length / 2; i++) { + int diff = Math.abs(nums[nums.length - i - 1] - nums[i]); + map.put(diff, map.getOrDefault(diff, 0) + 1); + } + List list = new ArrayList<>(); + for (Map.Entry entry : map.entrySet()) { + list.add(new int[] {entry.getKey(), entry.getValue()}); + } + // sort them by their frequency + Collections.sort(list, (a, b) -> b[1] - a[1]); + List modes = new ArrayList<>(); + modes.add(list.get(0)); + int i = 1; + // in case there are ties (same frequency, different mode values) + while (i < list.size() && list.get(i)[1] == list.get(0)[1]) { + modes.add(list.get(i++)); + } + // we'll take the second most frequent mode as well, otherwise, test case 4 won't pass + if (i < list.size()) { + modes.add(list.get(i)); + } + int minChanges = nums.length; + for (int[] mode : modes) { + minChanges = Math.min(minChanges, computeChanges(mode[0], nums, k)); + } + return minChanges; + } + + private int computeChanges(int mode, int[] nums, int k) { + int changes = 0; + for (int i = 0; i < nums.length / 2; i++) { + int diff = Math.abs(nums[nums.length - i - 1] - nums[i]); + if (diff != mode) { + if (nums[nums.length - i - 1] > nums[i]) { + if (k - nums[i] >= mode || nums[nums.length - i - 1] >= mode) { + changes++; + } else { + changes += 2; + } + } else { + if (k - nums[nums.length - i - 1] >= mode || nums[i] >= mode) { + changes++; + } else { + changes += 2; + } + } + } + } + return changes; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3226.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3226.java new file mode 100644 index 0000000000..d8dfcf95c7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3226.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3226 { + public static class Solution1 { + public int minChanges(int n, int k) { + if (n == k) { + return 0; + } + String nBin = Integer.toBinaryString(n); + String kBin = Integer.toBinaryString(k); + if (nBin.length() > kBin.length()) { + StringBuilder sb = new StringBuilder(kBin); + sb.reverse(); + while (nBin.length() > sb.length()) { + sb.append("0"); + } + kBin = sb.reverse().toString(); + } + if (nBin.length() != kBin.length()) { + return -1; + } + int minChanges = 0; + for (int i = nBin.length() - 1; i >= 0; i--) { + if (nBin.charAt(i) != kBin.charAt(i)) { + if (nBin.charAt(i) == '1') { + minChanges++; + } else { + return -1; + } + } + } + return minChanges; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3228.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3228.java new file mode 100644 index 0000000000..c2427332b2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3228.java @@ -0,0 +1,72 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3228 { + public static class Solution1 { + /* + * This is literal simulation and swap the 1s and 0s, but ended up in TLE, so you'll have to do better. + */ + public int maxOperations(String s) { + char[] arr = s.toCharArray(); + int len = arr.length; + int maxOps = 0; + int oneIndex = 0; + int zeroIndex = 0; + while (oneIndex < len && arr[oneIndex] == '0') { + oneIndex++; + } + // now we found the first one, then we'll have to find the last one in case there's a + // consecutive group of 1's + int firstOneOccurrence = oneIndex; + while (oneIndex < len && zeroIndex < len) { + while (oneIndex < len && arr[oneIndex] == '1') { + oneIndex++; + } + oneIndex--; + + zeroIndex = oneIndex; + while (zeroIndex < len && arr[zeroIndex] == '1') { + zeroIndex++; + } + // likewise, we need to find the last occurrence of 0 in case there's a group of + // consecutive 0's + while (zeroIndex < len && arr[zeroIndex] == '0') { + zeroIndex++; + } + if (zeroIndex >= len && arr[zeroIndex - 1] == '1') { + return maxOps; + } + int nextBeginOneIndex = zeroIndex; + zeroIndex--; + + int ops = 0; + do { + int[] swappedIndex = swap(arr, zeroIndex, oneIndex); + oneIndex = swappedIndex[0]; + zeroIndex = swappedIndex[1]; + ops++; + } while (oneIndex >= firstOneOccurrence); + maxOps += ops; + firstOneOccurrence = zeroIndex + 1; + oneIndex = nextBeginOneIndex; + } + return maxOps; + } + + private int[] swap(char[] arr, int zeroIndex, int oneIndex) { + char tmp = arr[zeroIndex]; + arr[zeroIndex] = arr[oneIndex]; + arr[oneIndex] = tmp; + return new int[] {oneIndex - 1, zeroIndex - 1}; + } + } + + public static class Solution2 { + /* + * TODO: finish this. + */ + public int maxOperations(String s) { + int maxOps = 0; + return maxOps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3232.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3232.java new file mode 100644 index 0000000000..a472167d8a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3232.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3232 { + public static class Solution1 { + public boolean canAliceWin(int[] nums) { + int aliceScore = 0; + int bobScore = 0; + // alice single digit, bob double digits + for (int num : nums) { + if (num > 9) { + bobScore += num; + } else { + aliceScore += num; + } + } + if (aliceScore > bobScore) { + return true; + } + // now alice double, bob the rest + aliceScore = 0; + bobScore = 0; + for (int num : nums) { + if (num > 9) { + aliceScore += num; + } else { + bobScore += num; + } + } + if (aliceScore > bobScore) { + return true; + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3233.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3233.java new file mode 100644 index 0000000000..00707d1b8c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3233.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Arrays; + +public class _3233 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/find-the-count-of-numbers-which-are-not-special/solutions/5546339/sieve-of-eratosthenes/ + * In order for a number to be special, it must be a square of a prime number; + * so we use sieve algorithm to find all prime numbers up to Math.sqrt(r); + * since Math.sqrt() method rounds down, we'll use Math.sqrt(r) + 1 as the bound for isPrime array + * and check if the number is within the range of [l, r] later. + */ + public int nonSpecialCount(int l, int r) { + int limit = (int) Math.sqrt(r); + boolean[] isPrime = new boolean[limit + 1]; + Arrays.fill(isPrime, true); + isPrime[0] = false; + isPrime[1] = false; + for (int i = 2; i * i < isPrime.length; i++) { + if (isPrime[i]) { + // below for loop is key to construct isPrime[] array: + // we start j from i * i, as long as j is within boundary, we increase j by i + // each time + // i.e. if i = 2, j starts from 4, then 6, 8, 10, 12 + // if i = 3, j starts from 9, then 12, 15, 18, 21 + for (int j = i * i; j < isPrime.length; j += i) { + isPrime[j] = false; + } + } + } + + // now count special numbers + int special = 0; + for (int i = Math.max(2, (int) Math.sqrt(l)); i < isPrime.length; i++) { + if (isPrime[i]) { + int square = i * i; + if (square <= r && square >= l) { + special++; + } + } + } + // total number of numbers in this range + int totalCount = r - l + 1; + // minus the special ones + return totalCount - special; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3234.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3234.java new file mode 100644 index 0000000000..4d1f5d52d3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3234.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3234 { + public static class Solution1 { + /* + * Sliding window. + * credit: https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/solutions/5547005/sliding-window-java-o-sqrt-of-n-n/ + * The idea is: + * 1. we fix the number of zeroes in each iteration, then the number of ones is zeroes * zeroes; + * 2. now we operate the sliding window. + */ + public int numberOfSubstrings(String s) { + int ans = 0; + for (int zeroes = 0; zeroes * zeroes < s.length(); zeroes++) { + int[] count = new int[2]; + int lastPos = -1; + // end keeps moving to the right in each iteration + for (int start = 0, end = 0; end < s.length(); end++) { + count[s.charAt(end) - '0']++; + while (start < end) { + if (s.charAt(start) == '0' && count[0] > zeroes) { + // this means we have more zeroes than we want, so we'll move start to + // the right by one + count[0]--; + lastPos = start; + } else if (s.charAt(start) == '1' && (count[1] - 1) >= (zeroes * zeroes)) { + // this means the current start position is '1' and after excluding it, + // the window is still a valid dominant one + count[1]--; + } else { + break; + } + start++; + } + if (count[0] == zeroes && count[1] >= zeroes * zeroes) { + ans += (start - lastPos); + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3237.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3237.java new file mode 100644 index 0000000000..49975f0ae4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3237.java @@ -0,0 +1,79 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; + +public class _3237 { + public static class Solution1 { + /* + * My completely original solution, very natural to think of doubly linked list + hashmap. + * Whenever a window is chosen (iterating on in the queries array), that window will be put onto the head of the list, + * all other windows will be pushed to the right by one position. + */ + public int[] simulationResult(int[] windows, int[] queries) { + Map map = new HashMap<>(); + DoublyLinkedListNode pre = buildList(windows, map); + for (int q : queries) { + moveToHead(q, pre, map); + } + return backToArray(pre, windows.length); + } + + private int[] backToArray(DoublyLinkedListNode pre, int length) { + DoublyLinkedListNode tmp = pre; + int[] ans = new int[length]; + for (int i = 0; i < length; i++) { + ans[i] = tmp.next.val; + tmp = tmp.next; + } + return ans; + } + + private void moveToHead( + int q, DoublyLinkedListNode headPrev, Map map) { + DoublyLinkedListNode node = map.get(q); + // if this window is already at the head, then we don't need to do anything + if (headPrev.next == node) { + return; + } + // get this node's next and prev pointers + DoublyLinkedListNode next = node.next; + DoublyLinkedListNode prev = node.prev; + // connect it's next to its previous' next, essentially cutting the current node out of + // the chain + prev.next = next; + // in case this is tail, we don't need to re-assign its next pointer + if (next != null) { + next.prev = prev; + } + DoublyLinkedListNode oldHead = headPrev.next; + headPrev.next = node; + node.next = oldHead; + oldHead.prev = node; + } + + private DoublyLinkedListNode buildList( + int[] windows, Map map) { + DoublyLinkedListNode pre = new DoublyLinkedListNode(-1); + DoublyLinkedListNode tmp = pre; + for (int i = 0; i < windows.length; i++) { + DoublyLinkedListNode next = new DoublyLinkedListNode(windows[i]); + next.prev = tmp; + tmp.next = next; + map.put(windows[i], next); + tmp = tmp.next; + } + return pre; + } + + public static class DoublyLinkedListNode { + DoublyLinkedListNode prev; + DoublyLinkedListNode next; + int val; + + public DoublyLinkedListNode(int val) { + this.val = val; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3238.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3238.java new file mode 100644 index 0000000000..eee88cfc7d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3238.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; + +public class _3238 { + public static class Solution1 { + public int winningPlayerCount(int n, int[][] pick) { + int winners = 0; + Map map = new HashMap<>(); + for (int[] p : pick) { + int player = p[0]; + int color = p[1]; + int[] colors = map.getOrDefault(player, new int[11]); + colors[color]++; + map.put(player, colors); + } + for (Map.Entry entry : map.entrySet()) { + int player = entry.getKey(); + int[] colors = entry.getValue(); + for (int c : colors) { + if (c >= player + 1) { + winners++; + break; + } + } + } + return winners; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java new file mode 100644 index 0000000000..c927608166 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3239 { + public static class Solution1 { + public int minFlips(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + int ans = m * n; + // try rows first + int flips = 0; + for (int i = 0; i < m; i++) { + for (int left = 0, right = n - 1; left < right; left++, right--) { + if (grid[i][left] != grid[i][right]) { + flips++; + } + } + } + ans = Math.min(ans, flips); + flips = 0; + // try columns now + for (int j = 0; j < n; j++) { + for (int top = 0, bottom = m - 1; top < bottom; top++, bottom--) { + if (grid[top][j] != grid[bottom][j]) { + flips++; + } + } + } + ans = Math.min(flips, ans); + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3240.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3240.java new file mode 100644 index 0000000000..9d9b414e63 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3240.java @@ -0,0 +1,78 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3240 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-ii/solutions/5580937/java-o-m-n/ + */ + public int minFlips(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + int ans = 0; + for (int i = 0; i < m / 2; i++) { + for (int j = 0; j < n / 2; j++) { + int cnt = 0; + cnt += grid[i][j]; + cnt += grid[m - i - 1][j]; + cnt += grid[i][n - j - 1]; + cnt += grid[m - i - 1][n - j - 1]; + ans += Math.min(cnt, 4 - cnt); + } + } + int diff = 0; + int p0 = 0; + int p1 = 0; + // process if there's odd number of rows + if (m % 2 == 1) { + for (int j = 0; j < n / 2; j++) { + if (grid[m / 2][j] != grid[m / 2][n - j - 1]) { + diff++; + } else { + if (grid[m / 2][j] == 0) { + p0++; + } else { + p1++; + } + } + } + } + // process if there's odd number of columns + if (n % 2 == 1) { + for (int i = 0; i < m / 2; i++) { + if (grid[i][n / 2] != grid[m - i - 1][n / 2]) { + diff++; + } else { + if (grid[i][n / 2] == 0) { + p0++; + } else { + p1++; + } + } + } + } + + if (m % 2 == 1 && n % 2 == 1) { + if (grid[m / 2][n / 2] == 1) { + ans++; + } + } + + int ans1; + if (diff % 2 == p1 % 2) { + ans1 = diff; + } else { + if (diff % 2 == 0) { + if (diff == 0) { + ans1 = 2; + } else { + ans1 = diff; + } + } else { + ans1 = diff; + } + } + + return ans + ans1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3241.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3241.java new file mode 100644 index 0000000000..b7c8bd10ba --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3241.java @@ -0,0 +1,64 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.PriorityQueue; + +public class _3241 { + public static class Solution1 { + /* + * This is my original solution during the contest, it's correct but not efficient enough, so got TLE on LeetCode. + * TODO: figure out a more efficient approach. + */ + public int[] timeTaken(int[][] edges) { + int[] times = new int[edges.length + 1]; + List[] graph = new ArrayList[edges.length + 1]; + for (int i = 0; i < edges.length + 1; i++) { + graph[i] = new ArrayList<>(); + } + for (int[] edge : edges) { + graph[edge[0]].add(edge[1]); + graph[edge[1]].add(edge[0]); + } + for (int i = 0; i < edges.length + 1; i++) { + times[i] = markAllNodes(graph, i); + } + return times; + } + + private int markAllNodes(List[] graph, int startNode) { + PriorityQueue q = new PriorityQueue<>((a, b) -> a[1] - b[1]); + q.offer(new int[] {startNode, 0}); + int[] shortestTime = new int[graph.length]; + Arrays.fill(shortestTime, Integer.MAX_VALUE); + shortestTime[startNode] = 0; + int maxTime = -1; + while (!q.isEmpty()) { + int[] curr = q.poll(); + int currNode = curr[0]; + int currTime = curr[1]; + if (currTime > shortestTime[currNode]) { + continue; + } + maxTime = shortestTime[currNode]; + for (int neighbor : graph[currNode]) { + if (neighbor % 2 == 0) { + if (currTime + 2 < shortestTime[neighbor]) { + shortestTime[neighbor] = currTime + 2; + maxTime = Math.max(maxTime, shortestTime[neighbor]); + q.offer(new int[] {neighbor, shortestTime[neighbor]}); + } + } else { + if (currTime + 1 < shortestTime[neighbor]) { + shortestTime[neighbor] = currTime + 1; + maxTime = Math.max(maxTime, shortestTime[neighbor]); + q.offer(new int[] {neighbor, shortestTime[neighbor]}); + } + } + } + } + return maxTime; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3242.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3242.java new file mode 100644 index 0000000000..c263c79138 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3242.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; + +public class _3242 { + public static class Solution1 { + class neighborSum { + int[][] matrix; + int m; + int n; + Map map; + + public neighborSum(int[][] grid) { + this.matrix = grid; + this.m = grid.length; + this.n = grid[0].length; + this.map = new HashMap<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + map.put(grid[i][j], new int[] {i, j}); + } + } + } + + public int adjacentSum(int value) { + int[] dirs = new int[] {0, 1, 0, -1, 0}; + int[] pos = this.map.get(value); + int sum = 0; + for (int i = 0; i < dirs.length - 1; i++) { + int nextx = pos[0] + dirs[i]; + int nexty = pos[1] + dirs[i + 1]; + if (nextx >= 0 && nextx < m && nexty >= 0 && nexty < n) { + sum += matrix[nextx][nexty]; + } + } + return sum; + } + + public int diagonalSum(int value) { + int[][] dirs = + new int[][] { + {-1, 1}, + {1, 1}, + {1, -1}, + {-1, -1} + }; + int[] pos = this.map.get(value); + int sum = 0; + for (int[] dir : dirs) { + int nextx = pos[0] + dir[0]; + int nexty = pos[1] + dir[1]; + if (nextx >= 0 && nextx < m && nexty >= 0 && nexty < n) { + sum += matrix[nextx][nexty]; + } + } + return sum; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3243.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3243.java new file mode 100644 index 0000000000..6186521e0d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3243.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _3243 { + public static class Solution1 { + public int[] shortestDistanceAfterQueries(int n, int[][] queries) { + List[] graph = new ArrayList[n]; + for (int i = 0; i < n; i++) { + graph[i] = new ArrayList<>(); + if (i + 1 < n) { + graph[i].add(i + 1); + } + } + int[] ans = new int[queries.length]; + for (int i = 0; i < queries.length; i++) { + graph[queries[i][0]].add(queries[i][1]); + ans[i] = bfs(graph, n)[n - 1]; + } + return ans; + } + + private int[] bfs(List[] graph, int n) { + int[] shortest = new int[n]; + Arrays.fill(shortest, -1); + shortest[0] = 0; + Queue q = new LinkedList<>(); + q.offer(0); + while (!q.isEmpty()) { + int curr = q.poll(); + for (int next : graph[curr]) { + if (shortest[next] == -1) { + shortest[next] = shortest[curr] + 1; + q.offer(next); + } + } + } + return shortest; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3248.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3248.java new file mode 100644 index 0000000000..aa36934245 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3248.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.List; + +public class _3248 { + public static class Solution1 { + public int finalPositionOfSnake(int n, List commands) { + int[] pos = new int[2]; + for (String command : commands) { + if (command.equals("RIGHT")) { + pos[1]++; + } else if (command.equals("DOWN")) { + pos[0]++; + } else if (command.equals("UP")) { + pos[0]--; + } else if (command.equals("LEFT")) { + pos[1]--; + } + } + return pos[0] * n + pos[1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3249.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3249.java new file mode 100644 index 0000000000..6632806861 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3249.java @@ -0,0 +1,113 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _3249 { + public static class Solution1 { + /* + * My completely original solution during the contest. + */ + class TreeNode { + int val; + List children; + int totalChildrenCount; + + public TreeNode(int val) { + this.val = val; + this.children = new ArrayList<>(); + this.totalChildrenCount = 1; // count itself as its own child + } + } + + int goodNodes = 0; + + public int countGoodNodes(int[][] edges) { + if (edges == null || edges.length == 0 || edges[0].length == 0) { + return 0; + } + TreeNode root = buildTree(edges); + postOrder(root); + dfs(root); + return goodNodes; + } + + private void dfs(TreeNode root) { + if (root == null || root.children.isEmpty()) { + return; + } + int size = root.children.get(0).totalChildrenCount; + if (size == 0) { + return; + } + boolean possiblyGoodNode = true; + for (TreeNode child : root.children) { + if (child.totalChildrenCount != size) { + possiblyGoodNode = false; + break; + } + } + if (possiblyGoodNode) { + goodNodes++; + } + for (TreeNode child : root.children) { + dfs(child); + } + } + + private int postOrder(TreeNode root) { + if (root == null) { + return 0; + } + if (root.children.isEmpty()) { + goodNodes++; + return 1; + } + int totalChildrenCount = 1; + for (TreeNode child : root.children) { + int count = postOrder(child); + totalChildrenCount += count; + } + root.totalChildrenCount = totalChildrenCount; + return totalChildrenCount; + } + + private TreeNode buildTree(int[][] edges) { + Map map = new HashMap<>(); + for (int i = 0; i < edges.length; i++) { + if (edges[i][0] == 0 || edges[i][1] == 0) { + if (edges[i][0] == 0) { + TreeNode parent = map.getOrDefault(edges[i][0], new TreeNode(edges[i][0])); + TreeNode child = map.getOrDefault(edges[i][1], new TreeNode(edges[i][1])); + parent.children.add(child); + map.put(edges[i][0], parent); + map.put(edges[i][1], child); + } else { + TreeNode parent = map.getOrDefault(edges[i][1], new TreeNode(edges[i][1])); + TreeNode child = map.getOrDefault(edges[i][0], new TreeNode(edges[i][0])); + parent.children.add(child); + map.put(edges[i][1], parent); + map.put(edges[i][0], child); + } + } else { + if (map.containsKey(edges[i][0])) { + TreeNode parent = map.get(edges[i][0]); + TreeNode child = map.getOrDefault(edges[i][1], new TreeNode(edges[i][1])); + parent.children.add(child); + map.put(edges[i][0], parent); + map.put(edges[i][1], child); + } else if (map.containsKey(edges[i][1])) { + TreeNode parent = map.get(edges[i][1]); + TreeNode child = map.getOrDefault(edges[i][0], new TreeNode(edges[i][0])); + parent.children.add(child); + map.put(edges[i][1], parent); + map.put(edges[i][0], child); + } + } + } + return map.get(0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3254.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3254.java new file mode 100644 index 0000000000..267092eb04 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3254.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Arrays; + +public class _3254 { + public static class Solution1 { + public int[] resultsArray(int[] nums, int k) { + int n = nums.length; + int[] ans = new int[n - k + 1]; + boolean sorted; + if (k == 1) { + return nums; + } + Arrays.fill(ans, -1); + for (int i = 0; i <= n - k; i++) { + sorted = true; + for (int j = i + 1; j < i + k; j++) { + if (nums[j] != nums[j - 1] + 1) { + sorted = false; + break; + } + } + if (sorted) { + ans[i] = nums[i + k - 1]; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3258.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3258.java new file mode 100644 index 0000000000..1e380c1f88 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3258.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3258 { + public static class Solution1 { + public int countKConstraintSubstrings(String s, int k) { + int ans = 0; + for (int i = 0; i < s.length(); i++) { + for (int j = i + 1; j <= s.length(); j++) { + String candidate = s.substring(i, j); + if (meetConstraints(candidate, k)) { + ans++; + } + } + } + return ans; + } + + private boolean meetConstraints(String candidate, int k) { + int ones = 0; + int zeroes = 0; + for (char c : candidate.toCharArray()) { + if (c == '0') { + zeroes++; + } else { + ones++; + } + } + return ones <= k || zeroes <= k; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3263.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3263.java new file mode 100644 index 0000000000..c0eeb6cb89 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3263.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.List; + +public class _3263 { + class Node { + public int val; + public Node prev; + public Node next; + } + + public static class Solution1 { + public int[] toArray(Node head) { + List list = new ArrayList<>(); + while (head != null) { + list.add(head.val); + head = head.next; + } + return list.stream().mapToInt(integer -> integer).toArray(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3264.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3264.java new file mode 100644 index 0000000000..2002939c68 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3264.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3264 { + public static class Solution1 { + public int[] getFinalState(int[] nums, int k, int multiplier) { + while (k-- > 0) { + int index = findFirstSmallestIndex(nums); + nums[index] *= multiplier; + } + return nums; + } + + private int findFirstSmallestIndex(int[] nums) { + int index = 0; + int min = nums[index]; + for (int i = 1; i < nums.length; i++) { + if (nums[i] < min) { + min = nums[i]; + index = i; + } + } + return index; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3270.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3270.java new file mode 100644 index 0000000000..7db28148fd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3270.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3270 { + public static class Solution1 { + public int generateKey(int num1, int num2, int num3) { + String[] padded = new String[3]; + padded[0] = String.format("%04d", num1); + padded[1] = String.format("%04d", num2); + padded[2] = String.format("%04d", num3); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < padded[0].length(); i++) { + sb.append( + Math.min( + Character.getNumericValue(padded[0].charAt(i)), + Math.min( + Character.getNumericValue(padded[1].charAt(i)), + Character.getNumericValue(padded[2].charAt(i))))); + } + return Integer.parseInt(sb.toString()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3274.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3274.java new file mode 100644 index 0000000000..9882484b81 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3274.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashSet; +import java.util.Set; + +public class _3274 { + public static class Solution1 { + public boolean checkTwoChessboards(String coordinate1, String coordinate2) { + return isBlack(coordinate2) == isBlack(coordinate1); + } + + private boolean isBlack(String coordinate) { + Set blackColsWithOddRows = new HashSet<>(); + blackColsWithOddRows.add('a'); + blackColsWithOddRows.add('c'); + blackColsWithOddRows.add('e'); + blackColsWithOddRows.add('g'); + if (blackColsWithOddRows.contains(coordinate.charAt(0))) { + return Character.getNumericValue(coordinate.charAt(1)) % 2 == 1; + } else { + return Character.getNumericValue(coordinate.charAt(1)) % 2 == 0; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3280.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3280.java new file mode 100644 index 0000000000..0c358a3ed0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3280.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3280 { + public static class Solution1 { + public String convertDateToBinary(String date) { + String[] parts = date.split("-"); + StringBuilder sb = new StringBuilder(); + for (String part : parts) { + sb.append(Integer.toBinaryString(Integer.parseInt(part))); + sb.append("-"); + } + sb.setLength(sb.length() - 1); + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3285.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3285.java new file mode 100644 index 0000000000..4f0dcd259a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3285.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.List; + +public class _3285 { + public static class Solution1 { + public List stableMountains(int[] height, int threshold) { + List ans = new ArrayList<>(); + for (int i = 1; i < height.length; i++) { + if (height[i - 1] > threshold) { + ans.add(i); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3289.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3289.java new file mode 100644 index 0000000000..68fb8dbaba --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3289.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashSet; +import java.util.Set; + +public class _3289 { + public static class Solution1 { + public int[] getSneakyNumbers(int[] nums) { + int[] ans = new int[2]; + Set set = new HashSet<>(); + int i = 0; + for (int num : nums) { + if (!set.add(num)) { + ans[i++] = num; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3300.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3300.java new file mode 100644 index 0000000000..26eb91e953 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3300.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3300 { + public static class Solution1 { + public int minElement(int[] nums) { + int min = Integer.MAX_VALUE; + for (int num : nums) { + min = Math.min(min, findSum(num)); + } + return min; + } + + private int findSum(int num) { + int sum = 0; + while (num != 0) { + sum += num % 10; + num /= 10; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3304.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3304.java new file mode 100644 index 0000000000..89ff1b1607 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3304.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3304 { + public static class Solution1 { + public char kthCharacter(int k) { + StringBuilder sb = new StringBuilder("a"); + while (sb.length() <= k) { + int n = sb.length(); + for (int i = 0; i < n; i++) { + sb.append((char) (sb.charAt(i) + 1)); + } + } + return sb.charAt(k - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3314.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3314.java new file mode 100644 index 0000000000..22724af407 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3314.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.List; + +public class _3314 { + public static class Solution1 { + public int[] minBitwiseArray(List nums) { + int[] ans = new int[nums.size()]; + for (int i = 0; i < nums.size(); i++) { + boolean found = false; + for (int j = 1; j < nums.get(i); j++) { + if ((j | (j + 1)) == nums.get(i)) { + ans[i] = j; + found = true; + break; + } + } + if (!found) { + ans[i] = -1; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3318.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3318.java new file mode 100644 index 0000000000..e0ba94d826 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3318.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; + +public class _3318 { + public static class Solution1 { + public int[] findXSum(int[] nums, int k, int x) { + PriorityQueue maxHeap = + new PriorityQueue<>( + (a, b) -> + a[1] != b[1] + ? b[1] - a[1] + : b[0] - a[0]); // a[0] is the number itself, a[1] + // is the frequency + Map map = new HashMap<>(); + int i = 0; + for (; i < k; i++) { + int[] a = map.getOrDefault(nums[i], new int[2]); + a[0] = nums[i]; + a[1]++; + map.put(nums[i], a); + } + maxHeap.addAll(map.values()); + int[] ans = new int[nums.length - k + 1]; + for (int j = i - 1, p = 0; j < nums.length; ) { + ans[p++] = computeTopX(new PriorityQueue<>(maxHeap), x); + + j++; + if (j >= nums.length) { + break; + } + int[] a = map.getOrDefault(nums[j], new int[2]); + a[0] = nums[j]; + a[1]++; + map.put(nums[j], a); + + a = map.getOrDefault(nums[j - k], new int[2]); + a[0] = nums[j - k]; + a[1]--; + map.put(nums[j - k], a); + + maxHeap.clear(); + maxHeap.addAll(map.values()); + } + return ans; + } + + private int computeTopX(PriorityQueue maxHeap, int x) { + int sum = 0; + while (!maxHeap.isEmpty() && x-- > 0) { + int[] a = maxHeap.poll(); + sum += a[0] * a[1]; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3324.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3324.java new file mode 100644 index 0000000000..5353903d26 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3324.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.ArrayList; +import java.util.List; + +public class _3324 { + public static class Solution1 { + public List stringSequence(String target) { + List ans = new ArrayList<>(); + StringBuilder sb = new StringBuilder(); + for (char c : target.toCharArray()) { + char candidate = 'a'; + boolean firstTime = true; + do { + if (firstTime) { + firstTime = false; + sb.append(candidate); + } else { + sb.setLength(sb.length() - 1); + candidate = (char) (candidate + 1); + sb.append(candidate); + } + ans.add(sb.toString()); + } while (c != candidate); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3330.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3330.java new file mode 100644 index 0000000000..2370c7360c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3330.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3330 { + public static class Solution1 { + public int possibleStringCount(String word) { + int ans = 1; + for (int i = 1; i < word.length(); i++) { + if (word.charAt(i) == word.charAt(i - 1)) { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3340.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3340.java new file mode 100644 index 0000000000..276ff833e7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3340.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3340 { + public static class Solution1 { + public boolean isBalanced(String num) { + int oddSum = 0; + int evenSum = 0; + for (int i = 0; i < num.length(); i++) { + if (i % 2 == 0) { + evenSum += Character.getNumericValue(num.charAt(i)); + } else { + oddSum += Character.getNumericValue(num.charAt(i)); + } + } + return oddSum == evenSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3345.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3345.java new file mode 100644 index 0000000000..933a1bb667 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3345.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3345 { + public static class Solution1 { + public int smallestNumber(int n, int t) { + for (int num = n; ; num++) { + int digitSum = getDigitsProduct(num); + if (digitSum % t == 0) { + return num; + } + } + } + + private int getDigitsProduct(int num) { + int copy = num; + int product = 1; + while (copy != 0) { + product *= copy % 10; + copy /= 10; + } + return product; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3349.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3349.java new file mode 100644 index 0000000000..323a90d055 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3349.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.List; + +public class _3349 { + public static class Solution1 { + public boolean hasIncreasingSubarrays(List nums, int k) { + for (int i = 0; i < nums.size(); i++) { + int count = 1; + int j = i; + boolean possible = true; + for (; j + 1 < nums.size() && count++ < k; j++) { + if (nums.get(j + 1) <= nums.get(j)) { + possible = false; + break; + } + } + boolean possibleAgain = true; + j++; + if (possible) { + count = 1; + for (; j + 1 < nums.size() && count++ < k; j++) { + if (nums.get(j + 1) <= nums.get(j)) { + possibleAgain = false; + break; + } + } + if (count < k) { + possibleAgain = false; + } + if (possibleAgain) { + return true; + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3353.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3353.java new file mode 100644 index 0000000000..247f0dd169 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3353.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3353 { + public static class Solution1 { + public int minOperations(int[] nums) { + int minOps = 0; + int delta = 0; + int target = nums[nums.length - 1]; + for (int i = nums.length - 2; i >= 0; i--) { + nums[i] += delta; + if (nums[i] != target) { + delta += target - nums[i]; + minOps++; + } + } + return minOps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3354.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3354.java new file mode 100644 index 0000000000..87dcdad837 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3354.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.Arrays; + +public class _3354 { + public static class Solution1 { + public int countValidSelections(int[] nums) { + int count = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] == 0) { + if (isValidWithMoveDirection(nums, i, true)) { + count++; + } + if (isValidWithMoveDirection(nums, i, false)) { + count++; + } + } + } + return count; + } + + private boolean isValidWithMoveDirection(int[] nums, int index, boolean moveLeft) { + int[] copy = Arrays.copyOf(nums, nums.length); + while (index >= 0 && index < nums.length) { + if (moveLeft) { + if (copy[index] > 0) { + copy[index]--; + moveLeft = !moveLeft; + index++; + } else { + index--; + } + } else { + if (copy[index] > 0) { + copy[index]--; + moveLeft = !moveLeft; + index--; + } else { + index++; + } + } + } + for (int num : copy) { + if (num != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3360.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3360.java new file mode 100644 index 0000000000..34fe2f0328 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3360.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3360 { + public static class Solution1 { + public boolean canAliceWin(int n) { + int turns = 0; + int removeCount = 10; + while (n >= removeCount) { + n -= removeCount; + removeCount--; + turns++; + } + return turns % 2 != 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3364.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3364.java new file mode 100644 index 0000000000..d81bec1551 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3364.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.List; + +public class _3364 { + public static class Solution1 { + public int minimumSumSubarray(List nums, int l, int r) { + int minSum = Integer.MAX_VALUE; + for (int len = l; len <= r; len++) { + int sum = findSmallestSum(nums, len); + if (sum > 0) { + minSum = Math.min(minSum, sum); + } + } + return minSum == Integer.MAX_VALUE ? -1 : minSum; + } + + private int findSmallestSum(List nums, int len) { + int sum = 0; + int i = 0; + for (; i < len; i++) { + sum += nums.get(i); + } + int minSum = Integer.MAX_VALUE; + if (sum > 0) { + minSum = sum; + } + for (; i < nums.size(); i++) { + sum -= nums.get(i - len); + sum += nums.get(i); + if (sum > 0) { + minSum = Math.min(minSum, sum); + } + } + return minSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3370.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3370.java new file mode 100644 index 0000000000..6e00f12a9a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3370.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3370 { + public static class Solution1 { + public int smallestNumber(int n) { + for (int num = n; ; num++) { + if (allSetBits(num)) { + return num; + } + } + } + + private boolean allSetBits(int num) { + String binaryStr = Integer.toBinaryString(num); + for (char c : binaryStr.toCharArray()) { + if (c != '1') { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3375.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3375.java new file mode 100644 index 0000000000..f46a30b5b0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3375.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.TreeMap; + +public class _3375 { + public static class Solution1 { + public int minOperations(int[] nums, int k) { + TreeMap map = new TreeMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + if (map.firstKey() < k) { + return -1; + } + if (map.firstKey() == k) { + return map.size() - 1; + } + return map.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3379.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3379.java new file mode 100644 index 0000000000..d3087915f2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3379.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3379 { + public static class Solution1 { + public int[] constructTransformedArray(int[] nums) { + int[] result = new int[nums.length]; + int len = nums.length; + for (int i = 0; i < len; i++) { + if (nums[i] > 0) { + int moves = nums[i] % len; + result[i] = nums[(i + moves) % len]; + } else if (nums[i] < 0) { + if (i + nums[i] >= 0) { + result[i] = nums[i + nums[i]]; + } else { + int moves = Math.abs(nums[i]) % len; + result[i] = nums[(len + (i - moves)) % len]; + } + } else { + result[i] = nums[i]; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3386.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3386.java new file mode 100644 index 0000000000..530e18ea6a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3386.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; + +public class _3386 { + public static class Solution1 { + public int buttonWithLongestTime(int[][] events) { + Map map = new HashMap<>(); + int ans = events[0][0]; + map.put(events[0][0], events[0][1]); + for (int i = 1; i < events.length; i++) { + int duration = events[i][1] - events[i - 1][1]; + if (map.getOrDefault(events[i][0], 0) < duration) { + map.put(events[i][0], duration); + } + } + int maxDuration = events[0][1]; + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() > maxDuration) { + ans = entry.getKey(); + maxDuration = entry.getValue(); + } else if (entry.getValue() == maxDuration) { + if (entry.getKey() < ans) { + ans = entry.getKey(); + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3392.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3392.java new file mode 100644 index 0000000000..12dfbad125 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3392.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3392 { + public static class Solution1 { + public int countSubarrays(int[] nums) { + int count = 0; + for (int i = 0; i < nums.length - 2; i++) { + if ((nums[i] + nums[i + 2]) * 2 == nums[i + 1]) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3396.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3396.java new file mode 100644 index 0000000000..9d7f727fe9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3396.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashMap; +import java.util.Map; + +public class _3396 { + public static class Solution1 { + public int minimumOperations(int[] nums) { + int ops = 0; + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + int i = 0; + while (!allDistinct(map)) { + ops++; + int target = i + 3; + for (; i < target && i < nums.length; i++) { + map.put(nums[i], map.get(nums[i]) - 1); + } + } + return ops; + } + + private boolean allDistinct(Map map) { + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() > 1) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3402.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3402.java new file mode 100644 index 0000000000..88e07e77ea --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3402.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3402 { + public static class Solution1 { + public int minimumOperations(int[][] grid) { + int ops = 0; + for (int j = 0; j < grid[0].length; j++) { + for (int i = 1; i < grid.length; i++) { + if (grid[i][j] <= grid[i - 1][j]) { + ops += grid[i - 1][j] - grid[i][j] + 1; + grid[i][j] = grid[i - 1][j] + 1; + } + } + } + return ops; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3471.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3471.java new file mode 100644 index 0000000000..9926ed7f16 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3471.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.fourththousand; + +import java.util.HashSet; +import java.util.Set; +import java.util.TreeMap; + +public class _3471 { + public static class Solution1 { + public int largestInteger(int[] nums, int k) { + TreeMap map = new TreeMap<>(); + for (int num : nums) { + map.put(num, 0); + } + for (int i = 0; i <= nums.length - k; i++) { + Set set = new HashSet<>(); + for (int j = i; j < i + k; j++) { + if (set.add(nums[j])) { + map.put(nums[j], map.getOrDefault(nums[j], 0) + 1); + } + } + } + int ans = -1; + for (int key : map.keySet()) { + if (map.get(key) == 1) { + ans = key; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3477.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3477.java new file mode 100644 index 0000000000..f2054202fa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3477.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3477 { + public static class Solution1 { + public int numOfUnplacedFruits(int[] fruits, int[] baskets) { + for (int i = 0; i < fruits.length; i++) { + for (int j = 0; j < baskets.length; j++) { + if (fruits[i] <= baskets[j]) { + baskets[j] = -1; + fruits[i] = -1; + break; + } + } + } + int count = 0; + for (int i = 0; i < fruits.length; i++) { + if (fruits[i] > -1) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3491.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3491.java new file mode 100644 index 0000000000..fecbad9937 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3491.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3491 { + public static class Solution1 { + public boolean phonePrefix(String[] numbers) { + for (int i = 0; i < numbers.length; i++) { + for (int j = 0; j < numbers.length; j++) { + if (i != j && numbers[i].startsWith(numbers[j])) { + return false; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/fourththousand/_3502.java b/src/main/java/com/fishercoder/solutions/fourththousand/_3502.java new file mode 100644 index 0000000000..98c94c7726 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/fourththousand/_3502.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.fourththousand; + +public class _3502 { + public static class Solution1 { + public int[] minCosts(int[] cost) { + int[] res = new int[cost.length]; + int min = cost[0]; + for (int i = 0; i < cost.length; i++) { + if (cost[i] < min) { + min = cost[i]; + } + res[i] = min; + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1002.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1002.java new file mode 100644 index 0000000000..db229aa962 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1002.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1002 { + public static class Solution1 { + public List commonChars(String[] A) { + int[][] charCount = new int[A.length][26]; + for (int i = 0; i < A.length; i++) { + for (char c : A[i].toCharArray()) { + charCount[i][c - 'a']++; + } + } + List result = new ArrayList<>(); + for (int i = 0; i < 26; i++) { + while (charCount[0][i] != 0) { + char c = (char) (i + 'a'); + boolean valid = true; + charCount[0][i]--; + for (int j = 1; j < A.length; j++) { + if (charCount[j][i] == 0) { + valid = false; + break; + } else { + charCount[j][i]--; + } + } + if (!valid) { + break; + } else { + result.add("" + c); + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1003.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1003.java new file mode 100644 index 0000000000..23320be518 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1003.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class _1003 { + public static class Solution1 { + public boolean isValid(String S) { + Deque stack = new ArrayDeque<>(); + + for (char c : S.toCharArray()) { + if (c == 'c') { + if (stack.isEmpty() || stack.pop() != 'b') { + return false; + } + if (stack.isEmpty() || stack.pop() != 'a') { + return false; + } + } else { + stack.push(c); + } + } + + return stack.isEmpty(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1004.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1004.java new file mode 100644 index 0000000000..b231074a22 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1004.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1004 { + public static class Solution1 { + /* + * Two pointer technique, a.k.a sliding window. + */ + public int longestOnes(int[] nums, int k) { + int result = 0; + int left = 0; + for (int right = 0; right < nums.length; right++) { + if (nums[right] == 0) { + k--; + } + while (k < 0) { + // in this case, we'll move the left pointer to the right + if (nums[left] == 0) { + k++; + } + left++; + } + result = Math.max(result, right - left + 1); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1005.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1005.java new file mode 100644 index 0000000000..8806176fa7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1005.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.PriorityQueue; + +public class _1005 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/discuss/252228/A-very-simple-java-solution + */ + public int largestSumAfterKNegations(int[] A, int K) { + PriorityQueue minHeap = new PriorityQueue<>(); + for (int num : A) { + minHeap.offer(num); + } + while (K-- > 0) { + minHeap.offer(-minHeap.poll()); + } + int sum = 0; + while (!minHeap.isEmpty()) { + sum += minHeap.poll(); + } + return sum; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/discuss/252254/JavaC%2B%2BPython-Sort + */ + public int largestSumAfterKNegations(int[] A, int K) { + Arrays.sort(A); + for (int i = 0; i < A.length && K > 0 && A[i] < 0; i++, K--) { + A[i] = -A[i]; + } + int sum = 0; + int min = Integer.MAX_VALUE; + for (int i = 0; i < A.length; i++) { + min = Math.min(min, A[i]); + sum += A[i]; + } + return K % 2 == 0 ? sum : sum - min * 2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1008.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1008.java new file mode 100644 index 0000000000..128e97fd45 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1008.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1008 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/discuss/252232/JavaC%2B%2BPython-O(N)-Solution + */ + int i = 0; + + public TreeNode bstFromPreorder(int[] preorder) { + return bstFromPreorder(preorder, Integer.MAX_VALUE); + } + + private TreeNode bstFromPreorder(int[] preorder, int bound) { + if (i == preorder.length || preorder[i] > bound) { + return null; + } + TreeNode root = new TreeNode(preorder[i++]); + root.left = bstFromPreorder(preorder, root.val); + root.right = bstFromPreorder(preorder, bound); + return root; + } + } + + public static class Solution2 { + /* + * I'm happy to have come up with this solution completely on my own on 10/13/2021.Enjoy the beauty of recursion! + */ + public TreeNode bstFromPreorder(int[] preorder) { + return bstFromPreorder(preorder, 0, preorder.length); + } + + private TreeNode bstFromPreorder(int[] preorder, int start, int end) { + if (start >= end) { + return null; + } + TreeNode root = new TreeNode(preorder[start]); + int i = start + 1; + for (; i < end; i++) { + if (preorder[i] > preorder[start]) { + break; + } + } + root.left = bstFromPreorder(preorder, start + 1, i); + root.right = bstFromPreorder(preorder, i, end); + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1009.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1009.java new file mode 100644 index 0000000000..f2ab5346f6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1009.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1009 { + public static class Solution1 { + public int bitwiseComplement(int N) { + if (N == 0) { + return 1; + } + List list = new ArrayList<>(); + while (N != 0) { + list.add(N & 1); + N >>= 1; + } + int result = 0; + int exp = list.size() - 1; + for (int i = list.size() - 1; i >= 0; i--) { + if (list.get(i) == 0) { + result += Math.pow(2, exp); + } + exp--; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1010.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1010.java new file mode 100644 index 0000000000..ff23768cf6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1010.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1010 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/discuss/256726/Java-O(n)-code-w-comment-similar-to-Two-Sum + *

+ * Think of Problem 1: Two Sum + * Assume target is 60, each item in time % 60. + * Then this problem becomes very similar to Problem 1. + */ + public int numPairsDivisibleBy60(int[] time) { + int result = 0; + Map map = new HashMap<>(); + for (int t : time) { + int d = (60 - t % 60) % 60; + if (map.containsKey(d)) { + result += map.get(d); + } + map.put(t % 60, map.getOrDefault(t % 60, 0) + 1); + } + return result; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/solution/ + */ + public int numPairsDivisibleBy60(int[] time) { + int[] remainders = new int[60]; + int ans = 0; + for (int t : time) { + if (t % 60 == 0) { + ans += remainders[0]; + } else { + ans += remainders[60 - t % 60]; + } + remainders[t % 60]++; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1011.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1011.java new file mode 100644 index 0000000000..43f6bc4ee1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1011.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1011 { + public static class Solution1 { + public int daysToShip(int[] weights, int capacity) { + int days = 0; + int currentShip = 0; + + for (int k : weights) { + if (currentShip + k > capacity) { + currentShip = 0; + days += 1; + } + currentShip += k; + } + + return days + 1; + } + + public int shipWithinDays(int[] weights, int D) { + + int sum = 0; + int max = 0; + + for (int k : weights) { + sum += k; + max = Math.max(max, k); + } + + // Minimum possible capacity needs to be as much as the heaviest package + int lower = max; + // Maximum possible capacity is the total weight of all packages + int upper = sum; + + if (daysToShip(weights, lower) <= D) { + return lower; + } + + // Guess is for capacity + int currentGuess; + int bestGuess = -1; + + // Binary search + while (lower <= upper) { + currentGuess = (upper + lower) / 2; + if (daysToShip(weights, currentGuess) <= D) { + bestGuess = currentGuess; + upper = currentGuess - 1; + } else { + lower = currentGuess + 1; + } + } + + return bestGuess; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1013.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1013.java new file mode 100644 index 0000000000..88afe169b0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1013.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1013 { + public static class Solution1 { + public boolean canThreePartsEqualSum(int[] A) { + int sum = 0; + for (int i = 0; i < A.length; i++) { + sum += A[i]; + } + if (sum % 3 != 0) { + return false; + } + int equalSum = sum / 3; + int left = 0; + int leftSum = 0; + while (left < A.length - 2 && leftSum != equalSum) { + leftSum += A[left++]; + } + if (left > A.length - 2 || leftSum != equalSum) { + return false; + } + + int right = A.length - 1; + int rightSum = 0; + while (right > left && rightSum != equalSum) { + rightSum += A[right--]; + } + if (right < left || rightSum != equalSum) { + return false; + } + int middleSum = 0; + for (int i = left; i <= right; i++) { + middleSum += A[i]; + } + return middleSum == equalSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1014.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1014.java new file mode 100644 index 0000000000..ea8d6623a5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1014.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1014 { + public static class Solution1 { + public int maxScoreSightseeingPair(int[] A) { + int bestPrevious = A[0]; + int maxSum = 0; + + for (int i = 1; i < A.length; ++i) { + maxSum = Math.max(maxSum, bestPrevious + A[i] - i); + bestPrevious = Math.max(bestPrevious, A[i] + i); + } + + return maxSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1018.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1018.java new file mode 100644 index 0000000000..8915e98de0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1018.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1018 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/266051/Java-beats-100 + */ + public List prefixesDivBy5(int[] A) { + List result = new ArrayList<>(A.length); + int remainder = 0; + for (int a : A) { + remainder = ((remainder << 1) + a) % 5; + result.add(remainder == 0); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1019.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1019.java new file mode 100644 index 0000000000..02deda209c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1019.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _1019 { + public static class Solution1 { + public int[] nextLargerNodes(ListNode head) { + int len = findLength(head); + int[] result = new int[len]; + ListNode tmp = head; + int i = 0; + while (tmp != null) { + result[i++] = findNextLarger(tmp.val, tmp); + tmp = tmp.next; + } + return result; + } + + private int findNextLarger(int val, ListNode head) { + ListNode tmp = head.next; + while (tmp != null) { + if (tmp.val > val) { + return tmp.val; + } + tmp = tmp.next; + } + return 0; + } + + private int findLength(ListNode head) { + ListNode tmp = head; + int count = 0; + while (tmp != null) { + tmp = tmp.next; + count++; + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1020.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1020.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_1020.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1020.java index 28f71fd087..5eea39991d 100644 --- a/src/main/java/com/fishercoder/solutions/_1020.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1020.java @@ -1,14 +1,4 @@ -package com.fishercoder.solutions; - -/** - * 1020. Number of Enclaves - * - * Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land) - * - * A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid. - * - * Return the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves. - */ +package com.fishercoder.solutions.secondthousand; public class _1020 { public static class Solution1 { diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1021.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1021.java new file mode 100644 index 0000000000..bd0444f60b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1021.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1021 { + public static class Solution1 { + public String removeOuterParentheses(String S) { + List primitives = new ArrayList<>(); + for (int i = 1; i < S.length(); i++) { + int initialI = i - 1; + int left = 1; + while (i < S.length() && left > 0) { + if (S.charAt(i) == '(') { + left++; + } else { + left--; + } + i++; + } + primitives.add(S.substring(initialI, i)); + } + StringBuilder sb = new StringBuilder(); + for (String primitive : primitives) { + sb.append(primitive.substring(1, primitive.length() - 1)); + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1022.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1022.java new file mode 100644 index 0000000000..2af350c676 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1022.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _1022 { + public static class Solution1 { + public int sumRootToLeaf(TreeNode root) { + List> paths = new ArrayList<>(); + dfs(root, paths, new ArrayList<>()); + int sum = 0; + for (List list : paths) { + int num = 0; + for (int i : list) { + num = (num << 1) + i; + } + sum += num; + } + return sum; + } + + private void dfs(TreeNode root, List> paths, List path) { + path.add(root.val); + if (root.left != null) { + dfs(root.left, paths, path); + path.remove(path.size() - 1); + } + if (root.right != null) { + dfs(root.right, paths, path); + path.remove(path.size() - 1); + } + if (root.left == null && root.right == null) { + paths.add(new ArrayList<>(path)); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1024.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1024.java new file mode 100644 index 0000000000..d1b06aa392 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1024.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1024 { + public static class Solution1 { + /* + * Greedy + * Time: O(nlogn) where n is the number of clips + * Space: O(1) + */ + public int videoStitching(int[][] clips, int time) { + Arrays.sort(clips, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); + int count = 0; + int covered = 0; + for (int i = 0, start = 0; start < time; count++, start = covered) { + for (; i < clips.length && clips[i][0] <= start; i++) { + covered = Math.max(covered, clips[i][1]); + } + if (start == covered) { + return -1; + } + } + return count; + } + } + + public static class Solution2 { + /* + * DP + * Time: ? + * Space: ? + */ + public int videoStitching(int[][] clips, int time) { + // TODO: implement it. + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1025.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1025.java new file mode 100644 index 0000000000..818857e870 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1025.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1025 { + public static class Solution1 { + /* + * After writing out a few examples, beginning from n = 1, up to n = 5, the logic flows out naturally: + * 1. when N deduced to 1, whoever plays now loses because no integers exist between 0 and 1; + * 2. when N deduced to 2, whoever plays now wins because he/she will pick one and the next player is left with nothing to play; + * 3. all numbers N will eventually be deduced to either 1 or 2 depending on whether its odd or even. + */ + public boolean divisorGame(int N) { + return N % 2 == 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1026.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1026.java new file mode 100644 index 0000000000..31da38d4c0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1026.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1026 { + public static class Solution1 { + /* + * My completely original solution on 12/31/2021. + */ + int maxDiff = 0; + + public int maxAncestorDiff(TreeNode root) { + dfs(root); + return maxDiff; + } + + private void dfs(TreeNode root) { + if (root == null) { + return; + } + int[] minmax = new int[] {root.val, root.val}; + findMinMax(root, minmax); + maxDiff = + Math.max( + maxDiff, + Math.max( + Math.abs(root.val - minmax[0]), + Math.abs(minmax[1] - root.val))); + dfs(root.left); + dfs(root.right); + } + + private void findMinMax(TreeNode root, int[] minmax) { + if (root == null) { + return; + } + if (root.left != null) { + minmax[0] = Math.min(root.left.val, minmax[0]); + minmax[1] = Math.max(root.left.val, minmax[1]); + } + if (root.right != null) { + minmax[0] = Math.min(root.right.val, minmax[0]); + minmax[1] = Math.max(root.right.val, minmax[1]); + } + findMinMax(root.left, minmax); + findMinMax(root.right, minmax); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1029.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1029.java new file mode 100644 index 0000000000..d6cec8e531 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1029.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1029 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/two-city-scheduling/discuss/280173/Java-4-lines-intuitive-solution + * and + * https://leetcode.com/problems/two-city-scheduling/discuss/278771/Java-sort-solution + */ + public int twoCitySchedCost(int[][] costs) { + Arrays.sort(costs, (a, b) -> (a[0] - a[1] - (b[0] - b[1]))); + int cost = 0; + for (int i = 0; i < costs.length; i++) { + if (i < costs.length / 2) { + cost += costs[i][0]; + } else { + cost += costs[i][1]; + } + } + return cost; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1030.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1030.java new file mode 100644 index 0000000000..745967c340 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1030.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _1030 { + public static class Solution1 { + public int[][] allCellsDistOrder(int R, int C, int r0, int c0) { + int[][] result = new int[R * C][2]; + Queue queue = new LinkedList<>(); + queue.offer(new int[] {r0, c0}); + boolean[][] visited = new boolean[R][C]; + int i = 0; + while (!queue.isEmpty()) { + int[] cell = queue.poll(); + int row = cell[0]; + int col = cell[1]; + + if (row < 0 || row >= R || col < 0 || col >= C || visited[row][col]) { + continue; + } + + visited[row][col] = true; + result[i++] = new int[] {row, col}; + queue.offer(new int[] {row, col + 1}); + queue.offer(new int[] {row + 1, col}); + queue.offer(new int[] {row - 1, col}); + queue.offer(new int[] {row, col - 1}); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1033.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1033.java new file mode 100644 index 0000000000..c6ac8942af --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1033.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1033 { + public static class Solution1 { + private int minMoves(int x, int y, int z) { + // already consecutive integers, nothing to be done + if (x + 1 == y && y + 1 == z) { + return 0; + } + // one of the following (sample) cases: + // 1, 2, 8 (8 -> 3) + // 1, 7, 8 (1 -> 6) + // 1, 3, 8 (8 -> 2) + // 1, 6, 8 (1 -> 7) + if (y - x <= 2 || z - y <= 2) { + return 1; + } + + // move z to y + 1, x to y - 1 + return 2; + } + + private int maxMoves(int x, int y, int z) { + return z - x - 2; + } + + public int[] numMovesStones(int a, int b, int c) { + int[] t = {a, b, c}; + Arrays.sort(t); + + int min = minMoves(t[0], t[1], t[2]); + int max = maxMoves(t[0], t[1], t[2]); + + return new int[] {min, max}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1034.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1034.java new file mode 100644 index 0000000000..05e45e12cb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1034.java @@ -0,0 +1,77 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1034 { + public static class Solution1 { + /* + * My completely original solution. + */ + int[] dirs = new int[] {0, 1, 0, -1, 0}; + + public int[][] colorBorder(int[][] grid, int row, int col, int color) { + int m = grid.length; + int n = grid[0].length; + boolean[][] visited = new boolean[m][n]; + visited[row][col] = true; + // copy the input as the final output so that we keep the input intact during dfs, + // otherwise, it'll lead to incorrect result like in test case 3 + int[][] result = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + result[i][j] = grid[i][j]; + } + } + return dfs(grid, row, col, color, m, n, grid[row][col], visited, result); + } + + private int[][] dfs( + int[][] grid, + int row, + int col, + int color, + int m, + int n, + int originalColor, + boolean[][] visited, + int[][] result) { + if (row == 0 + || col == 0 + || row == m - 1 + || col == n - 1 + || neighborDiffColor(row, col, grid, originalColor, m, n)) { + result[row][col] = color; + } + for (int i = 0; i < dirs.length - 1; i++) { + int nextRow = dirs[i] + row; + int nextCol = dirs[i + 1] + col; + if (nextRow >= 0 + && nextRow < m + && nextCol >= 0 + && nextCol < n + && grid[nextRow][nextCol] == originalColor + && !visited[nextRow][nextCol]) { + visited[nextRow][nextCol] = true; + dfs(grid, nextRow, nextCol, color, m, n, originalColor, visited, result); + } + } + return result; + } + + private boolean neighborDiffColor( + int row, int col, int[][] grid, int originalColor, int m, int n) { + // if any of the four neighbors has a different color, we consider this cell as a + // boarding cell as well as it's a boarder to this connected component + for (int i = 0; i < dirs.length - 1; i++) { + int nextRow = row + dirs[i]; + int nextCol = col + dirs[i + 1]; + if (nextRow >= 0 + && nextCol >= 0 + && nextRow < m + && nextCol < n + && grid[nextRow][nextCol] != originalColor) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1037.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1037.java new file mode 100644 index 0000000000..9b9d3d4c4a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1037.java @@ -0,0 +1,10 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1037 { + public static class Solution1 { + public boolean isBoomerang(int[][] points) { + return (points[1][1] - points[0][1]) * (points[2][0] - points[0][0]) + != (points[2][1] - points[0][1]) * (points[1][0] - points[0][0]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1038.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1038.java new file mode 100644 index 0000000000..1aa5ad5cf9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1038.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1038 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/discuss/286725/JavaC%2B%2BPython-Revered-Inorder-Traversal + */ + int greaterSum = 0; + + public TreeNode bstToGst(TreeNode root) { + if (root.right != null) { + bstToGst(root.right); + } + greaterSum = root.val = greaterSum + root.val; + if (root.left != null) { + bstToGst(root.left); + } + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1043.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1043.java new file mode 100644 index 0000000000..e1466a06d2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1043.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1043 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/partition-array-for-maximum-sum/discuss/290863/JavaC%2B%2BPython-DP + */ + public int maxSumAfterPartitioning(int[] A, int K) { + int N = A.length; + int[] dp = new int[N]; + for (int i = 0; i < N; i++) { + int curMax = 0; + for (int k = 1; k <= K && i - k + 1 >= 0; k++) { + curMax = Math.max(curMax, A[i - k + 1]); + dp[i] = Math.max(dp[i], (i >= k ? dp[i - k] : 0) + curMax * k); + } + } + return dp[N - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1046.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1046.java new file mode 100644 index 0000000000..5d3216c9c3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1046.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1046 { + public static class Solution1 { + public int lastStoneWeight(int[] stones) { + PriorityQueue heap = new PriorityQueue<>((a, b) -> b - a); + for (int stone : stones) { + heap.offer(stone); + } + while (!heap.isEmpty()) { + if (heap.size() >= 2) { + int one = heap.poll(); + int two = heap.poll(); + int diff = one - two; + heap.offer(diff); + } else { + return heap.poll(); + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1047.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1047.java new file mode 100644 index 0000000000..94fc94d4c9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1047.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _1047 { + public static class Solution1 { + public String removeDuplicates(String S) { + StringBuilder sb = new StringBuilder(S); + for (int i = 0; i < S.length() - 1; i++) { + if (S.charAt(i) == S.charAt(i + 1)) { + return removeDuplicates(S.substring(0, i) + S.substring(i + 2)); + } + } + return sb.toString(); + } + } + + public static class Solution2 { + public String removeDuplicates(String s) { + Deque stack = new LinkedList<>(); + for (char c : s.toCharArray()) { + if (!stack.isEmpty() && stack.peekLast() == c) { + stack.pollLast(); + } else { + stack.addLast(c); + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append(stack.pollLast()); + } + return sb.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1049.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1049.java new file mode 100644 index 0000000000..756b80be7c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1049.java @@ -0,0 +1,10 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1049 { + public static class Solution1 { + public int lastStoneWeightII(int[] stones) { + // TODO: implement it + return 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1051.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1051.java new file mode 100644 index 0000000000..ddc2cde46e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1051.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1051 { + + public static class Solution1 { + public int heightChecker(int[] heights) { + int[] originals = Arrays.copyOf(heights, heights.length); + Arrays.sort(heights); + int count = 0; + for (int i = 0; i < originals.length; i++) { + if (heights[i] != originals[i]) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1055.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1055.java new file mode 100644 index 0000000000..76592d95b4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1055.java @@ -0,0 +1,11 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.stream.IntStream; + +public class _1055 { + public static class Solution1 { + public int fixedPoint(int[] A) { + return IntStream.range(0, A.length).filter(i -> A[i] == i).findFirst().orElse(-1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1056.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1056.java new file mode 100644 index 0000000000..d83060d049 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1056.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1056 { + public static class Solution1 { + Map map = + new HashMap() { + { + put(0, 0); + put(1, 1); + put(8, 8); + put(6, 9); + put(9, 6); + } + }; + + public boolean confusingNumber(int N) { + if (N == 0) { + return false; + } + int newNumber = 0; + int originalN = N; + while (N != 0) { + newNumber *= 10; + int digit = N % 10; + if (!map.containsKey(digit)) { + return false; + } + digit = map.get(digit); + newNumber += digit; + N /= 10; + } + return newNumber != originalN; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1057.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1057.java new file mode 100644 index 0000000000..0d32f5d4f6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1057.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; +import java.util.TreeMap; + +public class _1057 { + public static class Solution1 { + public int[] assignBikes(int[][] workers, int[][] bikes) { + int w = workers.length; + int b = bikes.length; + TreeMap> treeMap = new TreeMap<>(); + for (int i = 0; i < w; i++) { + for (int j = 0; j < b; j++) { + int distance = + Math.abs(workers[i][0] - bikes[j][0]) + + Math.abs(workers[i][1] - bikes[j][1]); + if (!treeMap.containsKey(distance)) { + treeMap.put( + distance, + new PriorityQueue<>( + (x, y) -> x[0] == y[0] ? x[1] - y[1] : x[0] - y[0])); + } + treeMap.get(distance).add(new int[] {i, j}); + } + } + int[] ans = new int[w]; + for (int i = 0; i < w; i++) { + ans[i] = -1; + } + boolean[] assigned = new boolean[b]; + int workersHaveBikes = 0; + for (int dist : treeMap.keySet()) { + PriorityQueue workerBikePairs = treeMap.get(dist); + while (!workerBikePairs.isEmpty()) { + int[] workerBikePair = workerBikePairs.poll(); + if (ans[workerBikePair[0]] == -1 && !assigned[workerBikePair[1]]) { + assigned[workerBikePair[1]] = true; + ans[workerBikePair[0]] = workerBikePair[1]; + workersHaveBikes++; + } + } + if (workersHaveBikes == w) { + return ans; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1059.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1059.java new file mode 100644 index 0000000000..cbcd5d172a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1059.java @@ -0,0 +1,62 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1059 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/all-paths-from-source-lead-to-destination/editorial/ + * A very powerful algorithm, three colors to DFS a tree/graph. + */ + enum Color { + WHITE, + GRAY, + BLACK + } + + public boolean leadsToDestination(int n, int[][] edges, int source, int destination) { + List[] graph = buildGraph(n, edges); + Color[] colors = new Color[n]; + for (int i = 0; i < n; i++) { + colors[i] = Color.WHITE; + } + return leadsToDest(graph, colors, source, destination); + } + + private boolean leadsToDest( + List[] graph, Color[] colors, int node, int destination) { + // if it's not WHITE, then it should be BLACK, otherwise, there's a circle + if (colors[node] != Color.WHITE) { + return colors[node] == Color.BLACK; + } + // if this is a leaf node, then it should be destination, otherwise, it's a dead end and + // we return false + if (graph[node].size() == 0) { + return node == destination; + } + + // now, we start processing this node and mark it as GRAY + colors[node] = Color.GRAY; + for (int neighbor : graph[node]) { + if (!leadsToDest(graph, colors, neighbor, destination)) { + return false; + } + } + // recursive processing is done, we mark it as BLACK + colors[node] = Color.BLACK; + return true; + } + + private static List[] buildGraph(int n, int[][] edges) { + List[] graph = new ArrayList[n]; + for (int i = 0; i < n; i++) { + graph[i] = new ArrayList<>(); + } + for (int[] edge : edges) { + graph[edge[0]].add(edge[1]); + } + return graph; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1060.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1060.java new file mode 100644 index 0000000000..dc1a52f885 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1060.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1060 { + public static class Solution1 { + // Time: O(n) + // This is to calculate the number of missing elements in between each two numbers from left + // to right + public int missingElement(int[] nums, int k) { + int missing; + for (int i = 1; i < nums.length; i++) { + missing = nums[i] - nums[i - 1] - 1; + if (missing >= k) { + return nums[i - 1] + k; + } + k -= missing; + } + return nums[nums.length - 1] + k; + } + } + + public static class Solution2 { + // Time: O(logn) + // credit: https://leetcode.com/problems/missing-element-in-sorted-array/editorial/ + // We use binary search here, instead of focusing on the missing elements between two + // adjacent numbers, + // we can focus on the number of missing elements between any two numbers: nums[0] and + // nums[i] + // e.g. given this array: 4, 7, 9, 10, 14, i = 2; + // if nothing is missing, the elements should be 4,5,6,7,8,9, in other words, + // the total number of elements should be nums[2] - nums[0] + 1 = 9 - 4 + 1 = 6 + // however, in reality, there's only i - 0 + 1 = 2 - 0 + 1 = 3 elements, so we are missing 6 + // - 3 = 3 elements, they are 5,6,8 + // so the formula became: (nums[i] - nums[0] + 1) - (i - 0 + 1) = nums[i] - nums[0] - i + public int missingElement(int[] nums, int k) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = + right + - (right - left) + / 2; // has to be written this way, otherwise, infinite + // loop, since we assign mid to left instead of mid + 1 + // to left, although mathematically, it's equivalent to + // left + (right - left) / 2, integer division rounds + // off in Java + if (nums[mid] - nums[0] - mid < k) { + left = mid; + } else { + right = mid - 1; + } + } + return nums[0] + k + left; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1061.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1061.java new file mode 100644 index 0000000000..65364bd1c3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1061.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1061 { + public static class Solution1 { + public String smallestEquivalentString(String s1, String s2, String baseStr) { + UnionFind unionFind = new UnionFind(); + for (int i = 0; i < s1.length(); i++) { + unionFind.union(s1.charAt(i), s2.charAt(i)); + } + StringBuilder sb = new StringBuilder(); + for (char c : baseStr.toCharArray()) { + sb.append((char) (unionFind.find(c) + 'a')); + } + return sb.toString(); + } + + class UnionFind { + int[] ids; + + public UnionFind() { + this.ids = new int[26]; + for (int i = 0; i < ids.length; i++) { + ids[i] = i; + } + } + + public void union(char a, char b) { + int x = find(a); + int y = find(b); + if (x < y) { + ids[y] = x; + } else { + ids[x] = y; + } + } + + public int find(char x) { + while (x - 'a' != ids[x - 'a']) { + ids[x - 'a'] = ids[ids[x - 'a']]; + x = (char) (ids[x - 'a'] + 'a'); + } + return x - 'a'; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1062.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1062.java new file mode 100644 index 0000000000..2208384b41 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1062.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _1062 { + public static class Solution1 { + /* + * My completely original solution, although kind of brute-force, on 1/20/2022. + * Two pointer technique: + * j starts from the right, i starts from the left, + * as soon as we are able to find a repeated substring, we return. + */ + public int longestRepeatingSubstring(String s) { + Set seen = new HashSet<>(); + for (int j = s.length() - 1; j > 0; j--) { + for (int i = 0; i + j <= s.length(); i++) { + if (!seen.add(s.substring(i, i + j))) { + return j; + } + } + seen.clear(); + } + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1065.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1065.java new file mode 100644 index 0000000000..fdd4604789 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1065.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class _1065 { + public static class Solution1 { + public int[][] indexPairs(String text, String[] words) { + List> lists = new ArrayList<>(); + for (String word : words) { + lists.addAll(findAllMatchsForThisWord(word, text)); + } + if (lists.isEmpty()) { + return new int[][] {}; + } + Collections.sort( + lists, + (o1, o2) -> { + if (o1.get(0) > o2.get(0)) { + return 1; + } else if (o1.get(0) < o2.get(0)) { + return -1; + } else { + if (o1.get(1) > o2.get(1)) { + return 1; + } else { + return -1; + } + } + }); + int[][] result = new int[lists.size()][lists.get(0).size()]; + for (int i = 0; i < lists.size(); i++) { + result[i][0] = lists.get(i).get(0); + result[i][1] = lists.get(i).get(1); + } + return result; + } + + private List> findAllMatchsForThisWord(String word, String text) { + List> lists = new ArrayList<>(); + for (int i = 0; i <= text.length() - word.length(); i++) { + if (text.substring(i, i + word.length()).equals(word)) { + lists.add(Arrays.asList(i, i + word.length() - 1)); + } + } + return lists; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1066.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1066.java new file mode 100644 index 0000000000..91d7461480 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1066.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1066 { + public static class Solution1 { + int minSum = Integer.MAX_VALUE; + + public int assignBikes(int[][] workers, int[][] bikes) { + backtracking(workers, bikes, 0, new boolean[bikes.length], 0); + return minSum; + } + + private void backtracking( + int[][] workers, + int[][] bikes, + int workersIndex, + boolean[] bikesAssigned, + int currentSum) { + if (workersIndex >= workers.length) { + minSum = Math.min(minSum, currentSum); + return; + } + + if (currentSum > minSum) { + return; + } + + for (int j = 0; j < bikes.length; j++) { + if (!bikesAssigned[j]) { + bikesAssigned[j] = true; + backtracking( + workers, + bikes, + workersIndex + 1, + bikesAssigned, + currentSum + dist(workers[workersIndex], bikes[j])); + bikesAssigned[j] = false; + } + } + } + + private int dist(int[] worker, int[] bike) { + return Math.abs(worker[0] - bike[0]) + Math.abs(worker[1] - bike[1]); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1071.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1071.java similarity index 78% rename from src/main/java/com/fishercoder/solutions/_1071.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1071.java index f50667edb6..f46ac8f71a 100644 --- a/src/main/java/com/fishercoder/solutions/_1071.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1071.java @@ -1,29 +1,5 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; -/** - * 1071. Greatest Common Divisor of Strings - * - * For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concatenated with itself 1 or more times) - * Return the largest string X such that X divides str1 and X divides str2. - * - * Example 1: - * Input: str1 = "ABCABC", str2 = "ABC" - * Output: "ABC" - * - * Example 2: - * Input: str1 = "ABABAB", str2 = "ABAB" - * Output: "AB" - * - * Example 3: - * Input: str1 = "LEET", str2 = "CODE" - * Output: "" - * - * Note: - * - * 1 <= str1.length <= 1000 - * 1 <= str2.length <= 1000 - * str1[i] and str2[i] are English uppercase letters. - * */ public class _1071 { public static class Solution1 { public String gcdOfStrings(String str1, String str2) { diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1078.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1078.java new file mode 100644 index 0000000000..d38b9f4ac1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1078.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class _1078 { + public static class Solution1 { + public String[] findOcurrences(String text, String first, String second) { + String[] words = text.split(" "); + return IntStream.range(0, words.length - 2) + .filter(i -> words[i].equals(first) && words[i + 1].equals(second)) + .mapToObj(i -> words[i + 2]) + .collect(Collectors.toList()) + .stream() + .toArray(String[]::new); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1079.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1079.java new file mode 100644 index 0000000000..cf5cc3fd4c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1079.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.IntStream; + +public class _1079 { + public static class Solution1 { + public int numTilePossibilities(String tiles) { + char[] chars = tiles.toCharArray(); + Arrays.sort(chars); + boolean[] used = new boolean[chars.length]; + StringBuilder sb = new StringBuilder(); + List result = new ArrayList<>(); + dfs(chars, used, sb, result); + return result.size(); + } + + private void dfs(char[] chars, boolean[] used, StringBuilder sb, List result) { + if (sb.length() != 0) { + result.add(sb.toString()); + } + IntStream.range(0, chars.length) + .filter(i -> !used[i]) + .filter(i -> i <= 0 || chars[i - 1] != chars[i] || used[i - 1]) + .forEach( + i -> { + used[i] = true; + sb.append(chars[i]); + dfs(chars, used, sb, result); + used[i] = false; + sb.deleteCharAt(sb.length() - 1); + }); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1080.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1080.java new file mode 100644 index 0000000000..ffb944242e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1080.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1080 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/solutions/1340243/concise-dfs-solution-cpp-and-java-0ms/ + * DFS does this very cleanly. + */ + public TreeNode sufficientSubset(TreeNode root, int limit) { + return dfs(root, limit, 0); + } + + private TreeNode dfs(TreeNode root, int limit, int sumThusFar) { + if (root == null) { + return null; + } + if (root.left == null && root.right == null) { + return root.val + sumThusFar < limit ? null : root; + } + root.left = dfs(root.left, limit, sumThusFar + root.val); + root.right = dfs(root.right, limit, sumThusFar + root.val); + return root.left == null && root.right == null ? null : root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1085.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1085.java new file mode 100644 index 0000000000..dbaf44ae3f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1085.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1085 { + public static class Solution1 { + public int sumOfDigits(int[] A) { + int smallestNumber = A[0]; + for (int i = 1; i < A.length; i++) { + smallestNumber = Math.min(smallestNumber, A[i]); + } + int sum = 0; + while (smallestNumber > 0) { + sum += smallestNumber % 10; + smallestNumber /= 10; + } + return sum % 2 == 0 ? 1 : 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1086.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1086.java new file mode 100644 index 0000000000..f7b1ca5150 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1086.java @@ -0,0 +1,67 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.PriorityQueue; +import java.util.TreeMap; + +public class _1086 { + public static class Solution1 { + public int[][] highFive(int[][] items) { + TreeMap> map = new TreeMap<>(); + for (int[] studentToScore : items) { + if (map.containsKey(studentToScore[0])) { + map.get(studentToScore[0]).add(studentToScore[1]); + } else { + List list = new ArrayList<>(); + list.add(studentToScore[1]); + map.put(studentToScore[0], list); + } + } + int[][] result = new int[map.size()][2]; + for (int id : map.keySet()) { + List scores = map.get(id); + Collections.sort(scores); + int sum = 0; + for (int i = scores.size() - 1; i >= scores.size() - 5 && i >= 0; i--) { + sum += scores.get(i); + } + result[id - 1][0] = id; + result[id - 1][1] = sum / 5; + } + return result; + } + } + + public static class Solution2 { + public int[][] highFive(int[][] items) { + TreeMap> treeMap = new TreeMap<>(); + for (int[] studentToScores : items) { + if (treeMap.containsKey(studentToScores[0])) { + PriorityQueue maxHeap = treeMap.get(studentToScores[0]); + maxHeap.offer(studentToScores[1]); + if (maxHeap.size() > 5) { + maxHeap.poll(); + } + treeMap.put(studentToScores[0], maxHeap); + } else { + PriorityQueue maxHeap = new PriorityQueue<>(); + maxHeap.offer(studentToScores[1]); + treeMap.put(studentToScores[0], maxHeap); + } + } + int[][] result = new int[treeMap.size()][2]; + for (int id : treeMap.keySet()) { + result[id - 1][0] = id; + int sum = 0; + PriorityQueue maxHeap = treeMap.get(id); + while (!maxHeap.isEmpty()) { + sum += maxHeap.poll(); + } + result[id - 1][1] = sum / 5; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1087.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1087.java new file mode 100644 index 0000000000..568981ef4b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1087.java @@ -0,0 +1,101 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class _1087 { + public static class Solution1 { + public String[] expand(String s) { + List letters = parse(s); + List result = backtracking(letters, 0, new StringBuilder(), new ArrayList<>()); + String[] r = result.stream().toArray(String[]::new); + Arrays.sort(r); + return r; + } + + private List backtracking( + List letters, int start, StringBuilder sb, List result) { + if (start >= letters.size()) { + result.add(sb.toString()); + return result; + } + char[] chars = letters.get(start); + for (int i = 0; i < chars.length; i++) { + sb.append(chars[i]); + backtracking(letters, start + 1, sb, result); + sb.setLength(sb.length() - 1); + } + return result; + } + + private List parse(String s) { + List result = new ArrayList<>(); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == '{') { + int start = ++i; + while (i < s.length() && s.charAt(i) != '}') { + i++; + } + String[] strings = s.substring(start, i).split(","); + char[] chars = new char[strings.length]; + for (int j = 0; j < strings.length; j++) { + chars[j] = strings[j].charAt(0); + } + result.add(chars); + } else { + char[] chars = new char[1]; + chars[0] = s.charAt(i); + result.add(chars); + } + } + return result; + } + } + + public static class Solution2 { + /* + * My completely original solution on 1/17/2022. + */ + public String[] expand(String s) { + List list = new ArrayList<>(); + list.add(""); + for (int i = 0; i < s.length(); i++) { + List newList = new ArrayList<>(); + if (s.charAt(i) == '{') { + int j = i + 1; + while (s.charAt(j) != '}') { + j++; + } + String s2 = s.substring(i + 1, j); + String[] chars = s2.split("\\,"); + for (String c : chars) { + for (String sb : list) { + sb += c; + newList.add(sb); + } + } + i = j; + } else { + for (String sb : list) { + sb += s.charAt(i); + newList.add(sb); + } + } + list.clear(); + list.addAll(newList); + } + List res = new ArrayList<>(); + for (String sb : list) { + res.add(sb); + } + Collections.sort(res); + String[] ans = new String[res.size()]; + for (int i = 0; i < res.size(); i++) { + ans[i] = res.get(i); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1089.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1089.java new file mode 100644 index 0000000000..b25b2ff7a3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1089.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1089 { + public static class Solution1 { + public void duplicateZeros(int[] arr) { + for (int i = 0; i < arr.length - 1; i++) { + if (arr[i] == 0) { + duplicateZeros(arr, i + 1); + i++; + } + } + } + + private void duplicateZeros(int[] arr, int zeroIndex) { + int tmp = arr[zeroIndex]; + arr[zeroIndex] = 0; + for (int i = zeroIndex + 1; i < arr.length; i++) { + int next = arr[i]; + arr[i] = tmp; + tmp = next; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1090.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1090.java new file mode 100644 index 0000000000..b65c2f8ad0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1090.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class _1090 { + public static class Solution1 { + public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) { + int[][] tuple = new int[values.length][2]; + for (int i = 0; i < values.length; i++) { + tuple[i][0] = values[i]; + tuple[i][1] = labels[i]; + } + Arrays.sort(tuple, (a, b) -> b[0] - a[0]); + Map labelUsedCountMap = new HashMap<>(); + int sum = 0; + int numbersUsed = 0; + for (int i = 0; i < values.length; i++) { + int val = tuple[i][0]; + int usedCount = labelUsedCountMap.getOrDefault(tuple[i][1], 0); + if (usedCount >= useLimit) { + continue; + } else { + sum += val; + numbersUsed++; + labelUsedCountMap.put(tuple[i][1], usedCount + 1); + } + if (numbersUsed >= numWanted) { + break; + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1091.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1091.java new file mode 100644 index 0000000000..d300c0f87c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1091.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _1091 { + public static class Solution1 { + // you can count in the normal four directions first, then count the diagonal ones to form + // this array + int[] directions = new int[] {0, 1, 1, 0, -1, 1, -1, -1, 0}; + + public int shortestPathBinaryMatrix(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + if (grid[0][0] == 1 || grid[m - 1][n - 1] == 1) { + return -1; + } + int minPath = 0; + Queue queue = new LinkedList<>(); + queue.offer(new int[] {0, 0}); + boolean[][] visited = new boolean[m][n]; + visited[0][0] = true; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + int[] curr = queue.poll(); + if (curr[0] == m - 1 && curr[1] == n - 1) { + return minPath + 1; + } + for (int j = 0; j < directions.length - 1; j++) { + int newx = directions[j] + curr[0]; + int newy = directions[j + 1] + curr[1]; + if (newx >= 0 + && newx < n + && newy >= 0 + && newy < n + && !visited[newx][newy] + && grid[newx][newy] == 0) { + queue.offer(new int[] {newx, newy}); + visited[newx][newy] = true; + } + } + } + minPath++; + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1094.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1094.java new file mode 100644 index 0000000000..cd6cad4a31 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1094.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.PriorityQueue; + +public class _1094 { + public static class Solution1 { + public boolean carPooling(int[][] trips, int capacity) { + Arrays.sort(trips, (a, b) -> a[1] - b[1]); + PriorityQueue heap = new PriorityQueue<>((a, b) -> a[1] - b[1]); + for (int[] trip : trips) { + int startTime = trip[1]; + int endTime = trip[2]; + while (!heap.isEmpty() && heap.peek()[1] <= startTime) { + int[] curr = heap.poll(); + capacity += curr[0]; + } + int peopleCnt = trip[0]; + capacity -= peopleCnt; + if (capacity < 0) { + return false; + } + heap.offer(new int[] {peopleCnt, endTime}); + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1099.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1099.java new file mode 100644 index 0000000000..8562dacefa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1099.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1099 { + public static class Solution1 { + /* + * Time: O(n^2) + * Space: O(1) + */ + public int twoSumLessThanK(int[] nums, int k) { + int maxSum = Integer.MIN_VALUE; + for (int i = 0; i < nums.length - 1; i++) { + for (int j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] < k) { + maxSum = Math.max(maxSum, nums[i] + nums[j]); + } + } + } + return maxSum == Integer.MIN_VALUE ? -1 : maxSum; + } + } + + public static class Solution2 { + /* + * Time: O(nlogn) + * Space: O(1) + */ + public int twoSumLessThanK(int[] nums, int k) { + Arrays.sort(nums); + int left = 0; + int right = nums.length - 1; + int sum = Integer.MIN_VALUE; + while (left < right) { + int newSum = nums[left] + nums[right]; + if (newSum < k && newSum > sum) { + sum = newSum; + } else if (newSum >= k) { + right--; + } else { + left++; + } + } + return sum == Integer.MIN_VALUE ? -1 : sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1100.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1100.java new file mode 100644 index 0000000000..01e63b1300 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1100.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _1100 { + public static class Solution1 { + public int numKLenSubstrNoRepeats(String S, int K) { + int count = 0; + Set set = new HashSet<>(); + for (int i = 0; i <= S.length() - K; i++) { + String string = S.substring(i, i + K); + boolean invalid = false; + for (char c : string.toCharArray()) { + if (!set.add(c)) { + invalid = true; + break; + } + } + count += invalid ? 0 : 1; + set.clear(); + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1103.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1103.java new file mode 100644 index 0000000000..834dd0c1e5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1103.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1103 { + public static class Solution1 { + public int[] distributeCandies(int candies, int numPeople) { + Map map = new HashMap<>(); + int candy = 1; + while (candies > 0) { + for (int person = 1; person <= numPeople && candies > 0; person++, candy++) { + if (candies < candy) { + map.put(person, map.getOrDefault(person, 0) + candies); + candies -= candy; + break; + } else { + map.put(person, map.getOrDefault(person, 0) + candy); + candies -= candy; + } + } + } + int[] result = new int[numPeople]; + for (int i = 1; i <= numPeople; i++) { + if (map.containsKey(i)) { + result[i - 1] = map.get(i); + } else { + result[i - 1] = 0; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1104.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1104.java similarity index 83% rename from src/main/java/com/fishercoder/solutions/_1104.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1104.java index 9d0a641f90..4d7e56dc63 100644 --- a/src/main/java/com/fishercoder/solutions/_1104.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1104.java @@ -1,9 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.CommonUtils; import com.fishercoder.common.utils.TreeUtils; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -12,29 +11,11 @@ import java.util.List; import java.util.Queue; -/** - * 1104. Path In Zigzag Labelled Binary Tree - * - * In an infinite binary tree where every node has two children, the nodes are labelled in row order. - * In the odd numbered rows (ie., the first, third, fifth,...), the labelling is left to right, - * while in the even numbered rows (second, fourth, sixth,...), the labelling is right to left. - * - * Given the label of a node in this tree, return the labels in the path from the root of the tree to the node with that label. - * - * Example 1: - * Input: label = 14 - * Output: [1,3,4,14] - * - * Example 2: - * Input: label = 26 - * Output: [1,2,6,10,26] - * - * Constraints: - * 1 <= label <= 10^6 - * */ public class _1104 { public static class Solution1 { - /**This brute force solution is correct but results in TLE on LeetCode.*/ + /* + * This brute force solution is correct but results in TLE on LeetCode. + */ public List pathInZigZagTree(int label) { Deque deque = buildZigZagOrderList(label); CommonUtils.printDeque(deque); @@ -101,7 +82,9 @@ private Deque buildZigZagOrderList(int label) { } public static class Solution2 { - /**We'll directly compute the index of its parent, it'll be much faster this way.*/ + /* + * We'll directly compute the index of its parent, it'll be much faster this way. + */ public List pathInZigZagTree(int label) { List> lists = buildZigZagOrderList(label); List result = new ArrayList<>(); diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1105.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1105.java new file mode 100644 index 0000000000..f5909801e1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1105.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1105 { + public static class Solution1 { + /* + * Bottom up DP: + * 1. we place the books sequentially, for each book, there are only two options: + * place it on a new level (this will maximize the height of the shelf); + * or + * place it on the previous level if its remaining width still fits the current book's width + *

+ * How it's implemented below is: + * we always place the new book onto a new level to maximize its height, + * then we try to move previous books onto this new level as long as the width could accommodate, + * during this process, we minimize the height for dp[i]. + */ + public int minHeightShelves(int[][] books, int shelfWidth) { + // dp[i] means the minimum shelf height after placing all books up to and excluding book + // i + int[] dp = new int[books.length + 1]; + dp[0] = 0; + dp[1] = books[0][1]; + int len = books.length; + for (int i = 2; i <= len; i++) { + // suppose we put this book on a new level + int remainingShelfWidth = shelfWidth - books[i - 1][0]; + int maxHeight = books[i - 1][1]; + dp[i] = books[i - 1][1] + dp[i - 1]; + + // now we calculate the height if previous books are placed onto this new level to + // try to minimize dp[i] + for (int j = i - 1; j > 0 && remainingShelfWidth - books[j - 1][0] >= 0; j--) { + maxHeight = Math.max(maxHeight, books[j - 1][1]); + remainingShelfWidth -= books[j - 1][0]; + dp[i] = Math.min(dp[i], maxHeight + dp[j - 1]); + } + } + return dp[len]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1108.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1108.java new file mode 100644 index 0000000000..bab73f0699 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1108.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1108 { + public static class Solution1 { + public String defangIPaddr(String address) { + // String.replaceAll() takes in a regex which needs to be escaped + return address.replaceAll("\\.", "\\[\\.\\]"); + } + } + + public static class Solution2 { + public String defangIPaddr(String address) { + // String.replace() takes in a string which does NOT need to be escaped + return address.replace(".", "[.]"); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1110.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1110.java new file mode 100644 index 0000000000..79792d3977 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1110.java @@ -0,0 +1,173 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Set; + +public class _1110 { + public static class Solution1 { + public List delNodes(TreeNode root, int[] toDelete) { + Queue queue = new LinkedList<>(); + queue.offer(root); + for (int d : toDelete) { + delete(d, queue); + } + List result = new ArrayList<>(); + while (!queue.isEmpty()) { + result.add(queue.poll()); + } + return result; + } + + private void delete(int toDelete, Queue queue) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (delete(curr, toDelete, queue)) { + if (toDelete != curr.val) { + queue.offer(curr); + } + break; + } else { + queue.offer(curr); + } + } + } + + private boolean delete(TreeNode curr, int toDelete, Queue queue) { + if (curr == null) { + return false; + } else { + if (curr.val == toDelete) { + if (curr.left != null) { + queue.offer(curr.left); + } + if (curr.right != null) { + queue.offer(curr.right); + } + return true; + } else if (curr.left != null && curr.left.val == toDelete) { + if (curr.left.left != null) { + queue.offer(curr.left.left); + } + if (curr.left.right != null) { + queue.offer(curr.left.right); + } + curr.left = null; + return true; + } else if (curr.right != null && curr.right.val == toDelete) { + if (curr.right.left != null) { + queue.offer(curr.right.left); + } + if (curr.right.right != null) { + queue.offer(curr.right.right); + } + curr.right = null; + return true; + } + return delete(curr.left, toDelete, queue) || delete(curr.right, toDelete, queue); + } + } + } + + public static class Solution2 { + // use BFS + public List delNodes(TreeNode root, int[] toDelete) { + Set deleteSet = new HashSet<>(); + for (int d : toDelete) { + deleteSet.add(d); + } + Queue q = new LinkedList<>(); + q.offer(root); + List forest = new ArrayList<>(); + while (!q.isEmpty()) { + TreeNode curr = q.poll(); + + // process left child if any + if (curr.left != null) { + // add it into the q first because we need to process it any ways as it might + // have children that might not need to be deleted + q.offer(curr.left); + if (deleteSet.contains(curr.left.val)) { + curr.left = null; + } + } + + // process right child if any + if (curr.right != null) { + q.offer(curr.right); + if (deleteSet.contains(curr.right.val)) { + curr.right = null; + } + } + + // process this curr node: if it needs to be deleted, then add its non-null children + // into forest as we checked its children + // and we know they do not need to be deleted at this point + if (deleteSet.contains(curr.val)) { + if (curr.left != null) { + forest.add(curr.left); + } + if (curr.right != null) { + forest.add(curr.right); + } + } + // we don't add curr into forest here, otherwise there might be duplicate as we + // might have added them as their parent's child already + } + // at this point, only root might be missing, so we check root + if (!deleteSet.contains(root.val)) { + forest.add(root); + } + return forest; + } + } + + public static class Solution3 { + // use DFS/Post-order traversal + // key to recognize to apply post-order traversal: we need to handle subtree/children first + // before handling the root. + // it is in this case, handle children first in case children do not need to be removed and + // the parent needs to be removed, + // so we avoid the case of prematurely removing the parent before handling its children + // credit: https://leetcode.com/problems/delete-nodes-and-return-forest/editorial/ + public List delNodes(TreeNode root, int[] toDelete) { + List forest = new ArrayList<>(); + if (root == null) { + return forest; + } + Set deleteSet = new HashSet<>(); + for (int d : toDelete) { + deleteSet.add(d); + } + root = postOrder(root, deleteSet, forest); + if (root != null) { + forest.add(root); + } + return forest; + } + + private TreeNode postOrder(TreeNode root, Set deleteSet, List forest) { + if (root == null) { + return null; + } + root.left = postOrder(root.left, deleteSet, forest); + root.right = postOrder(root.right, deleteSet, forest); + if (deleteSet.contains(root.val)) { + if (root.left != null) { + forest.add(root.left); + } + if (root.right != null) { + forest.add(root.right); + } + // return null to its parent to delete the current node + return null; + } + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1114.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1114.java new file mode 100644 index 0000000000..0ef0821d61 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1114.java @@ -0,0 +1,44 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1114 { + public static class Solution1 { + static class Foo { + + private static volatile boolean onePrinted; + private static volatile boolean twoPrinted; + + public Foo() { + onePrinted = false; + twoPrinted = false; + } + + public synchronized void first(Runnable printFirst) throws InterruptedException { + + // printFirst.run() outputs "first". Do not change or remove this line. + printFirst.run(); + onePrinted = true; + notifyAll(); + } + + public synchronized void second(Runnable printSecond) throws InterruptedException { + while (!onePrinted) { + wait(); + } + + // printSecond.run() outputs "second". Do not change or remove this line. + printSecond.run(); + twoPrinted = true; + notifyAll(); + } + + public synchronized void third(Runnable printThird) throws InterruptedException { + while (!twoPrinted) { + wait(); + } + + // printThird.run() outputs "third". Do not change or remove this line. + printThird.run(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1118.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1118.java new file mode 100644 index 0000000000..d653f170ae --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1118.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1118 { + public static class Solution1 { + public int numberOfDays(int Y, int M) { + int[] map = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if (isLeapYear(Y) && M == 2) { + return 29; + } else { + return map[M]; + } + } + + private boolean isLeapYear(int year) { + return year % 4 == 0 && ((year % 100 == 0 && year % 400 == 0) || year % 100 != 0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1119.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1119.java new file mode 100644 index 0000000000..369e539651 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1119.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _1119 { + public static class Solution1 { + public String removeVowels(String S) { + Set vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u')); + StringBuilder stringBuilder = new StringBuilder(); + for (char c : S.toCharArray()) { + if (!vowels.contains(c)) { + stringBuilder.append(c); + } + } + return stringBuilder.toString(); + } + } + + public static class Solution2 { + public String removeVowels(String S) { + return S.replaceAll("[aeiou]", ""); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1120.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1120.java new file mode 100644 index 0000000000..c05a30e8e2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1120.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1120 { + public static class Solution1 { + /* + * Almost identical idea to https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree + * When it comes to subtree, or, you need to process subtrees first before processing root, post-order traversal/recursion is handy. + */ + double maxAve; + + public double maximumAverageSubtree(TreeNode root) { + postOrder(root); + return maxAve; + } + + private int[] postOrder(TreeNode root) { + if (root == null) { + return new int[2]; + } + int[] left = postOrder(root.left); + int[] right = postOrder(root.right); + int nodeSum = left[0] + right[0] + root.val; + int nodeCount = left[1] + right[1] + 1; + double ave = ((double) nodeSum / nodeCount); + maxAve = Math.max(ave, maxAve); + return new int[] {nodeSum, nodeCount}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1122.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1122.java new file mode 100644 index 0000000000..f103a38c34 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1122.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.TreeMap; + +public class _1122 { + public static class Solution1 { + public int[] relativeSortArray(int[] arr1, int[] arr2) { + TreeMap map = new TreeMap<>(); + Arrays.stream(arr1).forEach(num -> map.put(num, map.getOrDefault(num, 0) + 1)); + int[] result = new int[arr1.length]; + int i = 0; + for (int num : arr2) { + int count = map.get(num); + while (count-- > 0) { + result[i++] = num; + } + map.remove(num); + } + for (int key : map.keySet()) { + int count = map.get(key); + while (count-- > 0) { + result[i++] = key; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1123.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1123.java new file mode 100644 index 0000000000..5bcd2c9189 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1123.java @@ -0,0 +1,12 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1123 { + public static class Solution1 { + // TODO: implement it + public TreeNode lcaDeepestLeaves(TreeNode root) { + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1128.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1128.java new file mode 100644 index 0000000000..1fd4313600 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1128.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1128 { + public static class Solution1 { + public int numEquivDominoPairs(int[][] dominoes) { + int m = dominoes.length; + Map map = new HashMap<>(); + int count = 0; + for (int i = 0; i < m; i++) { + int smaller = Math.min(dominoes[i][0], dominoes[i][1]); + int bigger = Math.max(dominoes[i][0], dominoes[i][1]); + int key = smaller * 10 + bigger; + count += map.getOrDefault(key, 0); + map.put(key, map.getOrDefault(key, 0) + 1); + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1133.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1133.java new file mode 100644 index 0000000000..55eba0b7a3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1133.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.TreeMap; + +public class _1133 { + public static class Solution1 { + public int largestUniqueNumber(int[] A) { + TreeMap treeMap = new TreeMap<>((a, b) -> b - a); + for (int num : A) { + treeMap.put(num, treeMap.getOrDefault(num, 0) + 1); + } + for (int key : treeMap.keySet()) { + if (treeMap.get(key) == 1) { + return key; + } + } + return -1; + } + } + + public static class Solution2 { + public int largestUniqueNumber(int[] A) { + int[] count = new int[1001]; + for (int num : A) { + count[num]++; + } + for (int i = 1000; i >= 0; i--) { + if (count[i] == 1) { + return i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1134.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1134.java new file mode 100644 index 0000000000..5e7057aae6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1134.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1134 { + public static class Solution1 { + public boolean isArmstrong(int N) { + int numOfDigits = 0; + int copyN = N; + while (copyN > 0) { + copyN /= 10; + numOfDigits++; + } + int sum = 0; + copyN = N; + while (N > 0) { + int digit = N % 10; + sum += Math.pow(digit, numOfDigits); + N /= 10; + } + return sum == copyN; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1136.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1136.java new file mode 100644 index 0000000000..f4285b276a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1136.java @@ -0,0 +1,97 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _1136 { + public static class Solution1 { + public int minimumSemesters(int n, int[][] relations) { + Map> indegree = new HashMap<>(); + for (int[] rel : relations) { + if (!indegree.containsKey(rel[1])) { + indegree.put(rel[1], new HashSet<>()); + } + Set prereqs = indegree.get(rel[1]); + prereqs.add(rel[0]); + } + Queue queue = new LinkedList<>(); + Set taken = new HashSet<>(); + for (int i = 1; i <= n; i++) { + if (!indegree.containsKey(i)) { + queue.offer(i); + taken.add(i); + } + } + int minSemesters = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + minSemesters++; + for (int i = 0; i < size; i++) { + Integer curr = queue.poll(); + for (int key : indegree.keySet()) { + Set prereqs = indegree.get(key); + if (prereqs.contains(curr)) { + prereqs.remove(curr); + if (prereqs.size() == 0) { + queue.offer(key); + taken.add(key); + } + } + } + } + } + return taken.size() != n ? -1 : minSemesters; + } + } + + public static class Solution2 { + /* + * A straightforward one to practice topological sort (template). + * Use an indegree/outdegree array and an array of list type. + */ + + public int minimumSemesters(int n, int[][] relations) { + List[] adjList = new ArrayList[n + 1]; + for (int i = 1; i <= n; i++) { + adjList[i] = new ArrayList<>(); + } + int[] indegree = new int[n + 1]; + for (int[] rel : relations) { + indegree[rel[1]]++; + adjList[rel[0]].add(rel[1]); + } + Queue q = new LinkedList<>(); + for (int i = 1; i <= n; i++) { + if (indegree[i] == 0) { + q.offer(i); + } + } + int semesters = 0; + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + Integer curr = q.poll(); + for (int v : adjList[curr]) { + indegree[v]--; + if (indegree[v] == 0) { + q.offer(v); + } + } + } + semesters++; + } + for (int i = 1; i <= n; i++) { + if (indegree[i] != 0) { + return -1; + } + } + return semesters; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1137.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1137.java new file mode 100644 index 0000000000..b0322c5f73 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1137.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.stream.IntStream; + +public class _1137 { + public static class Solution1 { + public int tribonacci(int n) { + if (n <= 1) { + return n; + } + int[] numbers = new int[n + 1]; + numbers[0] = 0; + numbers[1] = 1; + numbers[2] = 1; + IntStream.rangeClosed(3, n) + .forEach(i -> numbers[i] = numbers[i - 1] + numbers[i - 2] + numbers[i - 3]); + return numbers[n]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1138.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1138.java new file mode 100644 index 0000000000..f69ce7a123 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1138.java @@ -0,0 +1,73 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1138 { + public static class Solution1 { + public String alphabetBoardPath(String target) { + Map map = initMap(); + StringBuilder sb = new StringBuilder(); + int[] currPos = new int[2]; + for (char c : target.toCharArray()) { + moveToDest(currPos, map.get(c), sb); + } + return sb.toString(); + } + + private void moveToDest(int[] currPos, int[] dest, StringBuilder sb) { + int currRow = currPos[0]; + int currCol = currPos[1]; + int destRow = dest[0]; + int destCol = dest[1]; + while (currRow != destRow || currCol != destCol) { + if (currRow < destRow) { + while (currRow < destRow) { + if (currCol != 0 && currRow == 4) { + break; + } + sb.append("D"); + currRow++; + } + currPos[0] = currRow; + } + if (currRow > destRow) { + while (currRow > destRow) { + sb.append("U"); + currRow--; + } + currPos[0] = currRow; + } + if (currCol < destCol) { + while (currCol < destCol) { + sb.append("R"); + currCol++; + } + currPos[1] = currCol; + } + if (currCol > destCol) { + while (currCol > destCol) { + sb.append("L"); + currCol--; + } + currPos[1] = currCol; + } + } + sb.append("!"); + return; + } + + private Map initMap() { + Map map = new HashMap<>(); + int row; + int col; + int number = 0; + for (char c = 'a'; c <= 'z'; c++, number++) { + row = number / 5; + col = number % 5; + map.put(c, new int[] {row, col}); + } + return map; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1143.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1143.java new file mode 100644 index 0000000000..aa81d3a643 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1143.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1143 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/longest-common-subsequence/solution/ + *

+ * Recall that there are two different techniques we can use to implement a dynamic programming solution; memoization and tabulation. + *

+ * Memoization is where we add caching to a function (that has no side effects). In dynamic programming, it is typically used on recursive functions for a top-down solution that starts with the initial problem and then recursively calls itself to solve smaller problems. + * Tabulation uses a table to keep track of subproblem results and works in a bottom-up manner: solving the smallest subproblems before the large ones, in an iterative manner. Often, people use the words "tabulation" and "dynamic programming" interchangeably. + *

+ * For most people, it's easiest to start by coming up with a recursive brute-force solution and then adding memoization to it. After that, they then figure out how to convert it into an (often more desired) bottom-up tabulated algorithm. + */ + public int longestCommonSubsequence(String text1, String text2) { + if (text1 == null || text1.length() == 0 || text2 == null || text2.length() == 0) { + return 0; + } + int m = text1.length(); + int n = text2.length(); + int[][] dp = new int[m + 1][n + 1]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + dp[i][j] = -1; + } + } + return topDownRecursiveSolve(dp, 0, 0, text1, text2); + } + + private int topDownRecursiveSolve(int[][] dp, int i, int j, String text1, String text2) { + if (dp[i][j] != -1) { + return dp[i][j]; + } + // option1: we don't include text1.charAt(i) in the optimal solution + int option1 = topDownRecursiveSolve(dp, i + 1, j, text1, text2); + // option2: we do include text1.charAt(i) in the optimal solution as long as a match in + // text2 at or after j does exist + int firstOccurence = text2.indexOf(text1.charAt(i), j); + int option2 = 0; + if (firstOccurence != -1) { + option2 = 1 + topDownRecursiveSolve(dp, i + 1, firstOccurence + 1, text1, text2); + } + dp[i][j] = Math.max(option1, option2); + return dp[i][j]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1145.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1145.java new file mode 100644 index 0000000000..d1303828bd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1145.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1145 { + public static class Solution1 { + public boolean btreeGameWinningMove(TreeNode root, int n, int x) { + if (root == null) { + return false; + } + + if (root.val == x) { + // 3 possible paths to block, left, right, parent + int leftCount = countNodes(root.left); + int rightCount = countNodes(root.right); + int parent = n - (leftCount + rightCount + 1); + + // possible to win if no. of nodes in 1 path is > than sum of nodes in the other 2 + // paths + return parent > (leftCount + rightCount) + || leftCount > (parent + rightCount) + || rightCount > (parent + leftCount); + } + return btreeGameWinningMove(root.left, n, x) || btreeGameWinningMove(root.right, n, x); + } + + private int countNodes(TreeNode root) { + if (root == null) { + return 0; + } + return countNodes(root.left) + countNodes(root.right) + 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1146.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1146.java new file mode 100644 index 0000000000..955930521c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1146.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.*; + +public class _1146 { + public static class Solution1 { + public static class SnapshotArray { + TreeMap[] + snapshots; // using this data structure is much more efficient in terms of + // storage, esp. if snap() calls happen frequently + int snapId; + + public SnapshotArray(int length) { + snapshots = new TreeMap[length]; + snapId = 0; + for (int i = 0; i < length; i++) { + snapshots[i] = new TreeMap<>(); + snapshots[i].put(0, 0); + } + } + + public void set(int index, int val) { + snapshots[index].put(snapId, val); + } + + public int snap() { + return snapId++; + } + + public int get(int index, int snapId) { + return snapshots[index].floorEntry(snapId).getValue(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1150.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1150.java new file mode 100644 index 0000000000..8d3f7a5796 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1150.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1150 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/discuss/358130/Java-just-one-binary-search-O(logN))-0ms-beats-100 + */ + public boolean isMajorityElement(int[] nums, int target) { + int firstIndex = findFirstOccur(nums, target); + int plusHalfIndex = firstIndex + nums.length / 2; + return plusHalfIndex < nums.length && nums[plusHalfIndex] == target; + } + + private int findFirstOccur(int[] nums, int target) { + int left = 0; + int right = nums.length; + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] < target) { + left = mid + 1; + } else if (nums[mid] >= target) { + right = mid; + } + } + return left; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1151.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1151.java new file mode 100644 index 0000000000..4a6d0b5279 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1151.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1151 { + public static class Solution1 { + /* + * My completely original solution on 11/4/2021. + * Typical sliding window problem/solution + */ + public int minSwaps(int[] data) { + int oneCount = 0; + for (int d : data) { + oneCount += d; + } + if (oneCount <= 1) { + return 0; + } + int left = 0; + int right = 0; + int ones = 0; + int zeroes = 0; + int minSwaps = data.length; + while (right < data.length) { + if (data[right] == 1) { + ones++; + } else { + zeroes++; + } + if (ones + zeroes == oneCount) { + minSwaps = Math.min(minSwaps, zeroes); + if (data[left] == 1) { + ones--; + } else { + zeroes--; + } + left++; + } + right++; + } + return minSwaps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1152.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1152.java new file mode 100644 index 0000000000..5327e470d2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1152.java @@ -0,0 +1,81 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +public class _1152 { + public static class Solution1 { + public List mostVisitedPattern( + String[] username, int[] timestamp, String[] website) { + Map> userToSiteMap = new HashMap<>(); + for (int i = 0; i < username.length; i++) { + if (!userToSiteMap.containsKey(username[i])) { + userToSiteMap.put(username[i], new TreeMap<>()); + } + TreeMap timeToSiteMap = userToSiteMap.get(username[i]); + timeToSiteMap.put(timestamp[i], website[i]); + userToSiteMap.put(username[i], timeToSiteMap); + } + Map sequenceCountMap = new HashMap<>(); + for (String user : userToSiteMap.keySet()) { + TreeMap timeToSiteMap = userToSiteMap.get(user); + if (timeToSiteMap.size() < 3) { + continue; + } else { + List times = new ArrayList<>(); + for (int time : timeToSiteMap.keySet()) { + times.add(time); + } + List allSequences = formAllSequences(times, timeToSiteMap); + Set encounteredSequence = new HashSet<>(); + for (String sequence : allSequences) { + if (encounteredSequence.add(sequence)) { + sequenceCountMap.put( + sequence, sequenceCountMap.getOrDefault(sequence, 0) + 1); + } + } + } + } + List mostVisitedPattern = new ArrayList<>(); + int count = 0; + String result = null; + for (String sequence : sequenceCountMap.keySet()) { + if (sequenceCountMap.get(sequence) > count) { + result = sequence; + count = sequenceCountMap.get(sequence); + } else if (count == sequenceCountMap.get(sequence)) { + if (sequence.compareTo(result) < 0) { + result = sequence; + } + } + } + for (String site : result.split("->")) { + mostVisitedPattern.add(site); + } + return mostVisitedPattern; + } + + private List formAllSequences( + List times, TreeMap timeToSiteMap) { + List result = new ArrayList<>(); + for (int i = 0; i < times.size() - 2; i++) { + for (int j = i + 1; j < times.size() - 1; j++) { + for (int k = j + 1; k < times.size(); k++) { + result.add( + timeToSiteMap.get(times.get(i)) + + "->" + + timeToSiteMap.get(times.get(j)) + + "->" + + timeToSiteMap.get(times.get(k))); + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1154.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1154.java new file mode 100644 index 0000000000..ed56ec74c6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1154.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Calendar; + +public class _1154 { + public static class Solution1 { + Calendar cal = Calendar.getInstance(); + + public int dayOfYear(String date) { + int year = Integer.parseInt(date.substring(0, 4)); + int month = Integer.parseInt(date.substring(5, 7)); + int day = Integer.parseInt(date.substring(8, 10)); + int thirtyDays = 30; + int thirtyOneDays = 31; + if (month == 1) { + return day; + } else if (month == 2) { + return day + thirtyOneDays; + } else { + int daysInFeb = isLeapYear(year) ? 29 : 28; + if (month == 3) { + return thirtyOneDays + daysInFeb + day; + } else if (month == 4) { + return 2 * thirtyOneDays + daysInFeb + day; + } else if (month == 5) { + return 2 * thirtyOneDays + daysInFeb + day + thirtyDays; + } else if (month == 6) { + return 3 * thirtyOneDays + daysInFeb + day + thirtyDays; + } else if (month == 7) { + return 3 * thirtyOneDays + daysInFeb + day + 2 * thirtyDays; + } else if (month == 8) { + return 4 * thirtyOneDays + daysInFeb + day + 2 * thirtyDays; + } else if (month == 9) { + return 5 * thirtyOneDays + daysInFeb + day + 2 * thirtyDays; + } else if (month == 10) { + return 5 * thirtyOneDays + daysInFeb + day + 3 * thirtyDays; + } else if (month == 11) { + return 6 * thirtyOneDays + daysInFeb + day + 3 * thirtyDays; + } else { + return 6 * thirtyOneDays + daysInFeb + day + 4 * thirtyDays; + } + } + } + + private boolean isLeapYear(int year) { + cal.set(Calendar.YEAR, year); + return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1160.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1160.java new file mode 100644 index 0000000000..f2d1e24eb6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1160.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1160 { + public static class Solution1 { + public int countCharacters(String[] words, String chars) { + int length = 0; + Map map = new HashMap<>(); + for (char c : chars.toCharArray()) { + int count = map.getOrDefault(c, 0); + map.put(c, count + 1); + } + for (String word : words) { + if (canForm(word, map)) { + length += word.length(); + } + } + return length; + } + + private boolean canForm(String word, final Map map) { + Map tmp = new HashMap<>(map); + for (Character c : word.toCharArray()) { + if (tmp.containsKey(c) && tmp.get(c) > 0) { + int count = tmp.get(c); + tmp.put(c, count - 1); + } else { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1161.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1161.java new file mode 100644 index 0000000000..3af9eac591 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1161.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; +import java.util.TreeMap; + +public class _1161 { + public static class Solution1 { + public int maxLevelSum(TreeNode root) { + if (root == null) { + return 0; + } + Queue q = new LinkedList<>(); + q.offer(root); + TreeMap treeMap = new TreeMap<>((a, b) -> b - a); + int level = 1; + while (!q.isEmpty()) { + int size = q.size(); + int sum = 0; + for (int i = 0; i < size; i++) { + TreeNode curr = q.poll(); + sum += curr.val; + if (curr.left != null) { + q.offer(curr.left); + } + if (curr.right != null) { + q.offer(curr.right); + } + } + treeMap.put(sum, level); + level++; + } + return treeMap.firstEntry().getValue(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1165.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1165.java new file mode 100644 index 0000000000..3b4f98ea80 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1165.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1165 { + public static class Solution1 { + public int calculateTime(String keyboard, String word) { + int time = 0; + int fromIndex = 0; + for (char c : word.toCharArray()) { + int fingerMoves = Math.abs(fromIndex - keyboard.indexOf(c)); + fromIndex = keyboard.indexOf(c); + time += fingerMoves; + } + return time; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1170.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1170.java similarity index 76% rename from src/main/java/com/fishercoder/solutions/_1170.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1170.java index 1a8e0ce33c..c4f1092011 100644 --- a/src/main/java/com/fishercoder/solutions/_1170.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1170.java @@ -1,40 +1,14 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.Arrays; -/** - * 1170. Compare Strings by Frequency of the Smallest Character - * - * Let's define a function f(s) over a non-empty string s, - * which calculates the frequency of the smallest character in s. - * For example, if s = "dcce" then f(s) = 2 because the smallest character is "c" and its frequency is 2. - * Now, given string arrays queries and words, - * return an integer array answer, - * where each answer[i] is the number of words such that f(queries[i]) < f(W), where W is a word in words. - * - * Example 1: - * Input: queries = ["cbd"], words = ["zaaaz"] - * Output: [1] - * Explanation: On the first query we have f("cbd") = 1, f("zaaaz") = 3 so f("cbd") < f("zaaaz"). - * - * Example 2: - * Input: queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"] - * Output: [1,2] - * Explanation: On the first query only f("bbb") < f("aaaa"). On the second query both f("aaa") and f("aaaa") are both > f("cc"). - * - * Constraints: - * 1 <= queries.length <= 2000 - * 1 <= words.length <= 2000 - * 1 <= queries[i].length, words[i].length <= 10 - * queries[i][j], words[i][j] are English lowercase letters. - * */ public class _1170 { public static class Solution1 { - /** + /* * Use simple iteration when finding counts * Time: O(n^m) where m is the size of queries and n is the size of words * Space: O(max(m, n) where m is the size of queries and n is the size of words) - * */ + */ public int[] numSmallerByFrequency(String[] queries, String[] words) { int[] queriesMinFrequecies = new int[queries.length]; for (int i = 0; i < queries.length; i++) { @@ -83,11 +57,11 @@ private int computeLowestFrequency(String string) { } public static class Solution2 { - /** + /* * Use binary search when finding counts * Time: O(n^logn) where m is the size of queries and n is the size of words * Space: O(max(m, n) where m is the size of queries and n is the size of words) - * */ + */ public int[] numSmallerByFrequency(String[] queries, String[] words) { int[] queriesMinFrequecies = new int[queries.length]; for (int i = 0; i < queries.length; i++) { diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1171.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1171.java new file mode 100644 index 0000000000..71cd6e3cba --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1171.java @@ -0,0 +1,108 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.*; + +public class _1171 { + public static class Solution1 { + /* + * I keep shrinking the array whenever I found there's a range of sum that equals to zero + * until the size of the list doesn't change any more. + * This is probably not super efficient, but accepted on LeetCode. + */ + public ListNode removeZeroSumSublists(ListNode head) { + List list = convertToList(head); + int size; + do { + size = list.size(); + list = shrinkList(list); + } while (list.size() != size); + return recoverLinkedList(list); + } + + private ListNode recoverLinkedList(List list) { + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + for (int i = 0; i < list.size(); i++) { + tmp.next = new ListNode(list.get(i)); + tmp = tmp.next; + } + return pre.next; + } + + private List convertToList(ListNode head) { + List list = new ArrayList<>(); + while (head != null) { + if (head.val != 0) { + // if it's zero, we'll just ignore it, this can help us take care of the zero + // values + list.add(head.val); + } + head = head.next; + } + return list; + } + + private List shrinkList(List list) { + for (int i = 0; i < list.size(); i++) { + int start = i; + List preSumList = new ArrayList<>(); + for (int k = 0; k < start; k++) { + preSumList.add(0); + } + preSumList.add(list.get(i)); + for (int k = i; k < list.size(); k++) { + if (k > start) { + Integer sum = preSumList.get(k - 1) + list.get(k); + if (sum == 0) { + List shrinkedList = new ArrayList<>(); + for (int j = 0; j < start; j++) { + shrinkedList.add(list.get(j)); + } + for (int j = k + 1; j < list.size(); j++) { + shrinkedList.add(list.get(j)); + } + return shrinkedList; + } else { + preSumList.add(sum); + } + } + } + } + return list; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/discuss/366337/Java-Iterative-and-Recursive-solution + * this post explains it all + * key of the hashmap is the prefix sum of all the nodes we've gone so far + * value of the hashmap is the corresponding linked list node + */ + public ListNode removeZeroSumSublists(ListNode head) { + ListNode pre = new ListNode(-1); + ListNode curr = pre; + pre.next = head; + Map map = new HashMap<>(); + int preSum = 0; + while (curr != null) { + preSum += curr.val; + if (map.containsKey(preSum)) { + curr = map.get(preSum).next; + int key = preSum + curr.val; + while (key != preSum) { + map.remove(key); + curr = curr.next; + key += curr.val; + } + map.get(preSum).next = curr.next; + } else { + map.put(preSum, curr); + } + curr = curr.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1175.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1175.java new file mode 100644 index 0000000000..f956497154 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1175.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.secondthousand; + +import java.math.BigInteger; +import java.util.Arrays; + +public class _1175 { + public static class Solution1 { + // credit: + // https://leetcode.com/problems/prime-arrangements/discuss/371884/Simple-Java-With-comment-sieve_of_eratosthenes + static int MOD = 1000000007; + + public static int numPrimeArrangements(int n) { + int numberOfPrimes = generatePrimes(n); + BigInteger x = factorial(numberOfPrimes); + BigInteger y = factorial(n - numberOfPrimes); + return x.multiply(y).mod(BigInteger.valueOf(MOD)).intValue(); + } + + public static BigInteger factorial(int n) { + BigInteger fac = BigInteger.ONE; + for (int i = 2; i <= n; i++) { + fac = fac.multiply(BigInteger.valueOf(i)); + } + return fac.mod(BigInteger.valueOf(MOD)); + } + + public static int generatePrimes(int n) { + boolean[] prime = new boolean[n + 1]; + Arrays.fill(prime, 2, n + 1, true); + for (int i = 2; i * i <= n; i++) { + if (prime[i]) { + for (int j = i * i; j <= n; j += i) { + prime[j] = false; + } + } + } + int count = 0; + for (int i = 0; i < prime.length; i++) { + if (prime[i]) { + count++; + } + } + + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1176.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1176.java new file mode 100644 index 0000000000..70fb54b710 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1176.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1176 { + public static class Solution1 { + public int dietPlanPerformance(int[] calories, int k, int lower, int upper) { + int sum = 0; + int points = 0; + for (int i = 0, j = 0; i <= calories.length - k && j < calories.length; j++) { + sum += calories[j]; + if (j - i > k - 1) { + sum -= calories[i++]; + } + if (j - i == k - 1) { + if (sum > upper) { + points++; + } else if (sum < lower) { + points--; + } + } + } + return points; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1180.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1180.java new file mode 100644 index 0000000000..cd57b387b7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1180.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1180 { + public static class Solution1 { + public int countLetters(String S) { + int count = 0; + for (int i = 0, j = 1; j < S.length() && i <= j; ) { + while (j < S.length() && S.charAt(i) == S.charAt(j)) { + j++; + } + count += countTimes(S.substring(i, j)); + i += S.substring(i, j).length(); + } + return count; + } + + private int countTimes(String str) { + int len = str.length(); + int times = 0; + while (len > 0) { + times += len--; + } + return times; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1182.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1182.java new file mode 100644 index 0000000000..5f555131fe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1182.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1182 { + public static class Solution1 { + public List shortestDistanceColor(int[] colors, int[][] queries) { + Map> map = buildMap(colors); + List result = new ArrayList<>(); + for (int[] query : queries) { + result.add(binarySearch(query[0], map.get(query[1]))); + } + return result; + } + + private Integer binarySearch(int index, List indices) { + if (indices == null) { + return -1; + } + int left = 0; + int right = indices.size() - 1; + int minDistance = Integer.MAX_VALUE; + while (left <= right) { + int mid = left + (right - left) / 2; + if (indices.get(mid) == index) { + return 0; + } else if (indices.get(mid) > index) { + minDistance = Math.min(minDistance, indices.get(mid) - index); + right = mid - 1; + } else { + minDistance = Math.min(minDistance, index - indices.get(mid)); + left = mid + 1; + } + } + return minDistance; + } + + private Map> buildMap(int[] colors) { + Map> map = new HashMap<>(); + for (int i = 0; i < colors.length; i++) { + if (!map.containsKey(colors[i])) { + map.put(colors[i], new ArrayList<>()); + } + map.get(colors[i]).add(i); + } + return map; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1184.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1184.java new file mode 100644 index 0000000000..cf0825f755 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1184.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1184 { + public static class Solution1 { + public int distanceBetweenBusStops(int[] distance, int start, int destination) { + if (start > destination) { + int tmp = start; + start = destination; + destination = tmp; + } + int clockwise = 0; + for (int i = start; i < destination; i++) { + clockwise += distance[i]; + } + int counterClockwise = 0; + for (int i = destination; i < distance.length; i++) { + counterClockwise += distance[i]; + } + for (int i = 0; i < start; i++) { + counterClockwise += distance[i]; + } + + return Math.min(clockwise, counterClockwise); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1185.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1185.java new file mode 100644 index 0000000000..e19b6a8b53 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1185.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1185 { + public static class Solution1 { + /* + * Time: O(1) + * Space: O(1) + *

+ * Plain and simple algorithm: + * based on the fact that 1/1/1971 is a Friday and calculate the given day. + */ + public String dayOfTheWeek(int day, int month, int year) { + String[] daysInTheWeek = + new String[] { + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" + }; + int[] daysInTheMonth = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int numberOfDays = 0; + for (int i = 1971; i < year; i++) { + numberOfDays += i % 4 == 0 ? 366 : 365; + } + if (year % 4 == 0) { + daysInTheMonth[1] = 29; + } + for (int i = 0; i < month - 1; i++) { + numberOfDays += daysInTheMonth[i]; + } + numberOfDays += day - 1; + return daysInTheWeek[(numberOfDays + 5) % 7]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1186.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1186.java new file mode 100644 index 0000000000..85de820652 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1186.java @@ -0,0 +1,10 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1186 { + public static class Solution1 { + public int maximumSum(int[] arr) { + // TODO: implement it + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1189.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1189.java new file mode 100644 index 0000000000..22447b2b77 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1189.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1189 { + public static class Solution1 { + public int maxNumberOfBalloons(String text) { + int[] counts = new int[26]; + for (char c : text.toCharArray()) { + counts[c - 'a']++; + } + return Math.min( + counts[0], + Math.min( + counts[1], + Math.min(counts[11] / 2, Math.min(counts[14] / 2, counts[13])))); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1190.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1190.java new file mode 100644 index 0000000000..0d321bc2ed --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1190.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Stack; + +public class _1190 { + public static class Solution1 { + public String reverseParentheses(String s) { + Stack stack = new Stack<>(); + Queue queue = new LinkedList<>(); + for (char c : s.toCharArray()) { + if (c != ')') { + stack.push(c); + } else { + while (!stack.isEmpty() && stack.peek() != '(') { + queue.offer(stack.pop()); + } + if (!stack.isEmpty()) { + stack.pop(); // pop off the open paren + } + while (!queue.isEmpty()) { + stack.push(queue.poll()); + } + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append(stack.pop()); + } + return sb.reverse().toString(); + } + } + + public static class Solution2 { + + public static String reverseParentheses(String s) { + Deque stack = new LinkedList<>(); + for (char c : s.toCharArray()) { + if (c == '(' || Character.isAlphabetic(c)) { + stack.addLast(c + ""); + } else { + StringBuilder innerSb = new StringBuilder(); + while (!stack.isEmpty()) { + if (stack.peekLast().equals("(")) { + stack.pollLast(); + break; + } else { + innerSb.append(stack.pollLast()); + } + } + stack.addLast(innerSb.reverse().toString()); + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append(stack.pollLast()); + } + return sb.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1196.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1196.java new file mode 100644 index 0000000000..23b58d66ba --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1196.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1196 { + public static class Solution1 { + public int maxNumberOfApples(int[] arr) { + Arrays.sort(arr); + int sum = 0; + int i = 0; + for (; i < arr.length; i++) { + sum += arr[i]; + if (sum > 5000) { + break; + } + } + return i; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1197.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1197.java new file mode 100644 index 0000000000..c2160d9032 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1197.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; + +public class _1197 { + public static class Solution1 { + /* + * My completely original solution. + */ + public int minKnightMoves(int x, int y) { + int boundary = 600; // this is from the constraints of this problem: -300 <= x, y <= 300 + Queue q = new LinkedList<>(); + q.offer(new int[] {0, 0}); + int moves = 0; + int[][] dirs = + new int[][] { + {-2, 1}, + {-1, 2}, + {1, 2}, + {2, 1}, + {2, -1}, + {1, -2}, + {-1, -2}, + {-2, -1} + }; + Set visited = new HashSet<>(); + visited.add(0); + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + int[] curr = q.poll(); + if (curr[0] == x && curr[1] == y) { + return moves; + } + for (int[] dir : dirs) { + int nextx = dir[0] + curr[0]; + int nexty = dir[1] + curr[1]; + if (visited.add(nexty * boundary + nextx)) { + // formula: col * size of matrix + row, is a common way to project a 2D + // matrix onto 1D array + q.offer(new int[] {nextx, nexty}); + } + } + } + moves++; + } + return moves; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1198.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1198.java new file mode 100644 index 0000000000..622b097b35 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1198.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1198 { + public static class Solution1 { + public int smallestCommonElement(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + for (int j = 0; j < n; j++) { + int minCommon = mat[0][j]; + // we'll start from the second row + int i = 1; + for (; i < m; i++) { + if (thisRowHasThisNumber(mat[i], minCommon)) { + continue; + } else { + break; + } + } + if (i == m) { + return minCommon; + } + } + return -1; + } + + private boolean thisRowHasThisNumber(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (target == nums[mid]) { + return true; + } else if (target > nums[mid]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1200.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1200.java new file mode 100644 index 0000000000..19a6b9ca67 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1200.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _1200 { + public static class Solution1 { + /* + * Time: O(nlogn) due to sorting + * Space: O(k) where k is the distinct number of differences between two numbers in the given array + */ + public List> minimumAbsDifference(int[] arr) { + Arrays.sort(arr); + int minimumDiff = arr[1] - arr[0]; + List result = new ArrayList(); + result.add(Arrays.asList(arr[0], arr[1])); + for (int i = 1; i < arr.length - 1; i++) { + int diff = arr[i + 1] - arr[i]; + if (minimumDiff == diff) { + result.add(Arrays.asList(arr[i], arr[i + 1])); + } else if (minimumDiff > diff) { + minimumDiff = diff; + result = new ArrayList(); + result.add(Arrays.asList(arr[i], arr[i + 1])); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1207.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1207.java new file mode 100644 index 0000000000..93c357963f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1207.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _1207 { + public static class Solution1 { + public boolean uniqueOccurrences(int[] arr) { + Map map = new HashMap<>(); + Arrays.stream(arr) + .forEach( + num -> { + map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1); + }); + Set set = new HashSet<>(); + return map.keySet().stream() + .mapToInt(key -> key) + .allMatch(key -> set.add(map.get(key))); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1209.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1209.java new file mode 100644 index 0000000000..e4302e8269 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1209.java @@ -0,0 +1,155 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.Stack; + +public class _1209 { + public static class Solution1 { + public String removeDuplicates(String s, int k) { + Stack stack = new Stack<>(); + char c = s.charAt(0); + stack.push(c); + int count = 1; + for (int i = 1; i < s.length(); i++) { + if (s.charAt(i) == c) { + count++; + stack.push(s.charAt(i)); + if (count == k) { + while (!stack.isEmpty() && stack.peek() == c) { + stack.pop(); + } + count = 0; + if (!stack.isEmpty()) { + c = stack.peek(); + while (!stack.isEmpty() && c == stack.peek()) { + count++; + stack.pop(); + } + int tmp = count; + while (tmp-- > 0) { + stack.push(c); + } + } + } + } else { + c = s.charAt(i); + stack.push(s.charAt(i)); + count = 1; + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append(stack.pop()); + } + return sb.reverse().toString(); + } + } + + public static class Solution2 { + public String removeDuplicates(String s, int k) { + StringBuilder sb = new StringBuilder(); + int dupCount = 0; + for (int i = 0; i < s.length(); i++) { + if (sb.length() != 0 && sb.charAt(sb.length() - 1) == s.charAt(i)) { + dupCount++; + } else { + dupCount = 1; + } + sb.append(s.charAt(i)); + if (dupCount == k) { + sb.setLength(sb.length() - k); + if (i + 1 < s.length()) { + dupCount = 0; + for (int j = sb.length() - 1; j >= 0; j--) { + if (sb.charAt(j) == s.charAt(i + 1)) { + dupCount++; + } else { + break; + } + } + } + } + } + return sb.toString(); + } + } + + public static class Solution3 { + /* + * My completely original solution on 1/6/2021. + */ + class CharCount { + char c; + int count; + + public CharCount(char c, int count) { + this.c = c; + this.count = count; + } + } + + public String removeDuplicates(String s, int k) { + Deque stack = new LinkedList<>(); + for (char c : s.toCharArray()) { + if (stack.isEmpty()) { + stack.addLast(new CharCount(c, 1)); + } else { + if (stack.peekLast().c == c && stack.peekLast().count + 1 == k) { + stack.pollLast(); + } else if (stack.peekLast().c == c) { + stack.addLast(new CharCount(c, stack.pollLast().count + 1)); + } else { + stack.addLast(new CharCount(c, 1)); + } + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + CharCount pair = stack.pollLast(); + int count = pair.count; + while (count-- > 0) { + sb.append(pair.c); + } + } + return sb.reverse().toString(); + } + } + + public static class Solution4 { + // my completely original solution on 6/19/2024 + public String removeDuplicates(String s, int k) { + Deque stack = new LinkedList<>(); + for (char c : s.toCharArray()) { + if (!stack.isEmpty() && stack.peekLast().c == c) { + Pair pair = stack.pollLast(); + pair.count = pair.count + 1; + if (pair.count < k) { + stack.addLast(pair); + } + } else { + stack.addLast(new Pair(c, 1)); + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + Pair pair = stack.pollLast(); + int count = pair.count; + while (count-- > 0) { + sb.append(pair.c); + } + } + return sb.reverse().toString(); + } + + class Pair { + char c; + int count; + + public Pair(char c, int count) { + this.c = c; + this.count = count; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1213.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1213.java new file mode 100644 index 0000000000..2b37c22f4c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1213.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1213 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/intersection-of-three-sorted-arrays/discuss/397603/Simple-Java-solution-beats-100 + */ + public List arraysIntersection(int[] arr1, int[] arr2, int[] arr3) { + List result = new ArrayList(); + int i = 0; + int j = 0; + int k = 0; + while (i < arr1.length && j < arr2.length && k < arr3.length) { + if (arr1[i] == arr2[j] && arr1[i] == arr3[k]) { + result.add(arr1[i]); + i++; + j++; + k++; + } else if (arr1[i] < arr2[j]) { + i++; + } else if (arr2[j] < arr3[k]) { + j++; + } else { + k++; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1214.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1214.java new file mode 100644 index 0000000000..cab9ade29b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1214.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _1214 { + public static class Solution1 { + public boolean twoSumBSTs(TreeNode root1, TreeNode root2, int target) { + if (root1 == null || root2 == null) { + return false; + } + List inorder1 = inorderDfs(root1, new ArrayList<>()); + List inorder2 = inorderDfs(root2, new ArrayList<>()); + return findTwoSum(inorder1, inorder2, target); + } + + private boolean findTwoSum(List sorted1, List sorted2, int target) { + for (int i = 0; i < sorted1.size(); i++) { + if (exists(sorted2, target - sorted1.get(i))) { + return true; + } + } + return false; + } + + private boolean exists(List sorted, int target) { + int left = 0; + int right = sorted.size() - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (sorted.get(mid) == target) { + return true; + } else if (sorted.get(mid) < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return false; + } + + private List inorderDfs(TreeNode root, List list) { + if (root == null) { + return list; + } + if (root.left != null) { + list = inorderDfs(root.left, list); + } + list.add(root.val); + if (root.right != null) { + list = inorderDfs(root.right, list); + } + return list; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1217.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1217.java new file mode 100644 index 0000000000..9487e11a8d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1217.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1217 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/play-with-chips/discuss/398239/C%2B%2B-3-lines + */ + public int minCostToMoveChips(int[] position) { + int chipsAtOddPosition = 0; + int chipsAtEvenPosition = 0; + for (int i = 0; i < position.length; i++) { + if (position[i] % 2 == 0) { + chipsAtEvenPosition++; + } else { + chipsAtOddPosition++; + } + } + return chipsAtEvenPosition > chipsAtOddPosition + ? chipsAtOddPosition + : chipsAtEvenPosition; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1219.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1219.java new file mode 100644 index 0000000000..54da88416f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1219.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _1219 { + public static class Solution1 { + public int getMaximumGold(int[][] grid) { + Queue queue = new LinkedList<>(); + int m = grid.length; + int n = grid[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] > 0) { + queue.offer(new int[] {i, j}); + } + } + } + int maxGold = 0; + while (!queue.isEmpty()) { + int[] start = queue.poll(); + boolean[][] visited = new boolean[m][n]; + visited[start[0]][start[1]] = true; + maxGold = + Math.max( + maxGold, + backtracking(grid, start, grid[start[0]][start[1]], visited)); + } + return maxGold; + } + + int[] directions = new int[] {0, 1, 0, -1, 0}; + + private int backtracking(int[][] grid, int[] start, int gold, boolean[][] visited) { + int max = gold; + for (int i = 0; i < directions.length - 1; i++) { + int nextX = start[0] + directions[i]; + int nextY = start[1] + directions[i + 1]; + if (nextX >= 0 + && nextX < grid.length + && nextY >= 0 + && nextY < grid[0].length + && !visited[nextX][nextY] + && grid[nextX][nextY] > 0) { + visited[nextX][nextY] = true; + max = + Math.max( + max, + backtracking( + grid, + new int[] {nextX, nextY}, + gold + grid[nextX][nextY], + visited)); + visited[nextX][nextY] = false; + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1221.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1221.java new file mode 100644 index 0000000000..9b047f9737 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1221.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1221 { + public static class Solution1 { + public int balancedStringSplit(String s) { + int i = 0; + int balancedCount = 0; + int lCount = 0; + int rCount = 0; + while (i < s.length()) { + if (s.charAt(i) == 'L') { + lCount++; + } else { + rCount++; + } + i++; + if (lCount != 0 && lCount == rCount) { + lCount = 0; + rCount = 0; + balancedCount++; + } + } + return balancedCount; + } + } + + public static class Solution2 { + public int balancedStringSplit(String s) { + int count = 0; + int result = 0; + int i = 0; + while (i < s.length()) { + if (s.charAt(i) == 'L') { + count++; + } else { + count--; + } + if (count == 0) { + result++; + } + i++; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1228.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1228.java new file mode 100644 index 0000000000..af33a9fea4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1228.java @@ -0,0 +1,80 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1228 { + public static class Solution1 { + /* + * A super verbose and inefficient but working way... + */ + public int missingNumber(int[] arr) { + Arrays.sort(arr); + Map> map = new HashMap<>(); + for (int i = 0; i < arr.length - 1; i++) { + int diff = arr[i + 1] - arr[i]; + List list = map.getOrDefault(diff, new ArrayList<>()); + list.add(i); + map.put(diff, list); + } + int smallDiff = arr[arr.length - 1]; + int bigDiff = 0; + for (int key : map.keySet()) { + smallDiff = Math.min(smallDiff, key); + bigDiff = Math.max(bigDiff, key); + } + return arr[map.get(bigDiff).get(0)] + smallDiff; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/missing-number-in-arithmetic-progression/discuss/408474/JavaC%2B%2BPython-Arithmetic-Sum-and-Binary-Search + */ + public int missingNumber(int[] arr) { + int min = arr[0]; + int max = arr[0]; + int sum = 0; + for (int num : arr) { + max = Math.max(max, num); + min = Math.min(min, num); + sum += num; + } + return (max + min) * (arr.length + 1) / 2 - sum; + } + } + + public static class Solution3 { + public int missingNumber(int[] arr) { + if (arr[1] < arr[0]) { + reverse(arr); + } + int diff = -1; + for (int i = 0; i < arr.length - 1; i++) { + if (i == 0) { + diff = arr[i + 1] - arr[i]; + } else if (arr[i + 1] - arr[i] > diff) { + return arr[i] + diff; + } else if (arr[i + 1] - arr[i] < diff) { + return arr[i] - (arr[i + 1] - arr[i]); + } + } + return arr[arr.length - 1]; + } + + private void reverse(int[] arr) { + int i = 0; + int j = arr.length - 1; + while (i < j) { + int tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + i++; + j--; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1230.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1230.java new file mode 100644 index 0000000000..3e879096c2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1230.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1230 { + public static class Solution1 { + public double probabilityOfHeads(double[] prob, int target) { + int n = prob.length; + // initialize a 2-D array, column size should be target + 1 + // dp[i][j] means the probability of getting j heads using the first i coins + // so dp[n][target] is the answer where n is the number of coins + double[][] dp = new double[n + 1][target + 1]; + dp[0][0] = 1; + for (int i = 1; i <= n; i++) { + dp[i][0] = dp[i - 1][0] * (1 - prob[i - 1]); + for (int j = 1; j <= target && j <= i; j++) { + dp[i][j] = dp[i - 1][j - 1] * prob[i - 1] + dp[i - 1][j] * (1 - prob[i - 1]); + } + } + return dp[n][target]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1232.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1232.java new file mode 100644 index 0000000000..c9da10f177 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1232.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1232 { + public static class Solution1 { + /* + * To check if they share the same slope, we use this formula: + *

+ * check whether (y4 - y3)/(x4- x3) equals to (y2 - y1)/(x2 - x1) + * considering denominator could be zero, we'll change it to use multiplication instead of division, + * thus it becomes + * check whether (y4 - y3)*(x2 - x1) equals (x4 - x3)*(y2 - y1) + */ + public boolean checkStraightLine(int[][] coordinates) { + for (int i = 2; i < coordinates.length - 1; i++) { + if ((coordinates[1][0] - coordinates[0][0]) + * (coordinates[i + 1][1] - coordinates[i][1]) + != (coordinates[1][1] - coordinates[0][1]) + * (coordinates[i + 1][0] - coordinates[i][0])) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1233.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1233.java new file mode 100644 index 0000000000..b20a9907f0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1233.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _1233 { + public static class Solution1 { + public List removeSubfolders(String[] folder) { + Arrays.sort(folder); + List ans = new ArrayList<>(); + Set set = new HashSet<>(); + for (int i = 0; i < folder.length; i++) { + String[] parts = folder[i].split("/"); + StringBuilder sb = new StringBuilder(); + boolean isSubFolder = false; + for (int j = 0; j < parts.length; j++) { + sb.append(parts[j]); + if (set.contains(sb.toString())) { + isSubFolder = true; + break; + } + sb.append("/"); + } + if (!isSubFolder) { + sb.setLength(sb.length() - 1); + ans.add(sb.toString()); + set.add(sb.toString()); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1237.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1237.java new file mode 100644 index 0000000000..86255fc1a8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1237.java @@ -0,0 +1,88 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _1237 { + + // This is the custom function interface. + // You should not implement it, or speculate about its implementation + abstract class CustomFunction { + // Returns f(x, y) for any given positive integers x and y. + // Note that f(x, y) is increasing with respect to both x and y. + // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1) + public abstract int f(int x, int y); + } + + public static class Solution1 { + /* + * Time: O(x*y) + * Space: O(1) + */ + public List> findSolution(CustomFunction customfunction, int z) { + List> result = new ArrayList<>(); + for (int x = 1; x <= 1000; x++) { + for (int y = 1; y <= 1000; y++) { + if (customfunction.f(x, y) == z) { + result.add(Arrays.asList(x, y)); + } + } + } + return result; + } + } + + public static class Solution2 { + /* + * linear search + *

+ * Time: O(x + y) + * Space: O(1) + */ + public List> findSolution(CustomFunction customfunction, int z) { + List> result = new ArrayList<>(); + int x = 1; + int y = 1000; + while (x < 1001 && y > 0) { + int functionResult = customfunction.f(x, y); + if (functionResult < z) { + x++; + } else if (functionResult > z) { + y--; + } else { + result.add(Arrays.asList(x++, y--)); + } + } + return result; + } + } + + public static class Solution3 { + /* + * binary search + *

+ * Time: O(xlogy) + * Space: O(1) + */ + public List> findSolution(CustomFunction customfunction, int z) { + List> result = new ArrayList<>(); + for (int x = 1; x <= 1000; x++) { + int left = 1; + int right = 1001; + while (left < right) { + int y = (left + right) / 2; + if (customfunction.f(x, y) < z) { + left = y + 1; + } else { + right = y; + } + } + if (customfunction.f(x, left) == z) { + result.add(Arrays.asList(x, left)); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1242.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1242.java new file mode 100644 index 0000000000..7740d2ed72 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1242.java @@ -0,0 +1,82 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.*; +import java.util.concurrent.*; + +public class _1242 { + public interface HtmlParser { + public List getUrls(String url); + } + + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/web-crawler-multithreaded/solutions/699006/java-blockingqueue-executorservice/ + */ + public List crawl(String startUrl, HtmlParser htmlParser) { + String targetHostName = getHostName(startUrl); + List result = new ArrayList<>(); + BlockingQueue queue = new LinkedBlockingQueue<>(); + queue.offer(startUrl); + Set visited = new HashSet<>(); + + Queue tasks = new LinkedList<>(); + // create a thread pool to crawling the URLs + ExecutorService executorService = + Executors.newFixedThreadPool( + 4, + r -> { + Thread t = new Thread(r); + // LeetCode doesn't allow executor.shutdown(), so use daemon threads + // to let the program shutdown, otherwise TLE. + t.setDaemon(true); + return t; + }); + + while (true) { + String url = queue.poll(); + if (url != null) { + if (getHostName(url).equals(targetHostName) && visited.add(url)) { + result.add(url); + tasks.add( + executorService.submit( + () -> { + List urls = htmlParser.getUrls(url); + for (String u : urls) { + queue.offer(u); + } + })); + } + } else { + if (!tasks.isEmpty()) { + // wait for the next task to complete which might add new URLs into the + // queue + Future nextTask = tasks.poll(); + try { + nextTask.get(); + } catch (InterruptedException | ExecutionException e) { + } + } else { + // exit when all tasks are completed. + break; + } + } + } + + return result; + } + + private String getHostName(String url) { + url = url.substring("http://".length()); + String[] parts = url.split("/"); + return parts[0]; + } + } + + public static void main(String... args) { + Solution1 solution1 = new Solution1(); + System.out.println(solution1.getHostName("http://news.yahoo.com")); + System.out.println(solution1.getHostName("http://news.yahoo.com/news")); + System.out.println(solution1.getHostName("http://news.yahoo.com/us")); + System.out.println(solution1.getHostName("http://news.yahoo.com")); + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1243.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1243.java new file mode 100644 index 0000000000..925f0a5ac4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1243.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class _1243 { + public static class Solution1 { + public List transformArray(int[] arr) { + int[] copy; + do { + copy = Arrays.copyOf(arr, arr.length); + for (int i = 1; i < arr.length - 1; i++) { + if (copy[i] < copy[i - 1] && copy[i] < copy[i + 1]) { + arr[i]++; + } else if (copy[i] > copy[i - 1] && copy[i] > copy[i + 1]) { + arr[i]--; + } + } + } while (!Arrays.equals(copy, arr)); + return Arrays.stream(arr).boxed().collect(Collectors.toList()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1248.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1248.java new file mode 100644 index 0000000000..f594e4ef24 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1248.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1248 { + public static class Solution1 { + public int numberOfSubarrays(int[] nums, int k) { + for (int i = 0; i < nums.length; i++) { + for (int j = 0; j < nums.length; j++) { + // TODO: implement it + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1249.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1249.java new file mode 100644 index 0000000000..1795133648 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1249.java @@ -0,0 +1,85 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _1249 { + public static class Solution1 { + public String minRemoveToMakeValid(String s) { + Deque stack = new LinkedList<>(); + int leftParen = 0; + int rightParen = 0; + for (char c : s.toCharArray()) { + if (c == '(') { + stack.push(c); + leftParen++; + } else if (c == ')') { + if (leftParen > rightParen) { + stack.push(c); + rightParen++; + } + } else { + stack.push(c); + } + } + StringBuilder sb = new StringBuilder(); + int diff = leftParen - rightParen; + while (!stack.isEmpty()) { + if (stack.peek() == '(') { + if (diff-- > 0) { + stack.pop(); + } else { + sb.append(stack.pop()); + } + } else { + sb.append(stack.pop()); + } + } + return sb.reverse().toString(); + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/26/2021. + */ + public String minRemoveToMakeValid(String s) { + Deque stack = new LinkedList<>(); + int left = 0; + int right = 0; + for (char c : s.toCharArray()) { + if (c == '(') { + stack.addLast(c + ""); + left++; + } else if (c == ')') { + if (left <= right) { + continue; + } else { + right++; + stack.addLast(c + ""); + } + } else { + stack.addLast(c + ""); + } + } + left = 0; + right = 0; + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + String str = stack.pollLast(); + if (str.equals(")")) { + right++; + sb.append(str); + } else if (str.equals("(")) { + if (right > left) { + sb.append(str); + left++; + } + } else { + sb.append(str); + } + } + return sb.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1252.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1252.java new file mode 100644 index 0000000000..649b00fa73 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1252.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1252 { + public static class Solution1 { + /* + * Time: O(m*n + k) where k is the length of indices + * Space: O(m*n) + */ + public int oddCells(int n, int m, int[][] indices) { + int[][] matrix = new int[n][m]; + for (int i = 0; i < indices.length; i++) { + addOneToRow(matrix, indices[i][0]); + addOneToColumn(matrix, indices[i][1]); + } + int oddNumberCount = 0; + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (matrix[i][j] % 2 != 0) { + oddNumberCount++; + } + } + } + return oddNumberCount; + } + + private void addOneToColumn(int[][] matrix, int columnIndex) { + for (int i = 0; i < matrix.length; i++) { + matrix[i][columnIndex] += 1; + } + } + + private void addOneToRow(int[][] matrix, int rowIndex) { + for (int j = 0; j < matrix[0].length; j++) { + matrix[rowIndex][j] += 1; + } + } + } + + public static class Solution2 { + /* + * Time: O(m*n + k) where k is the length of indices + * Space: O(m + n) + */ + public int oddCells(int n, int m, int[][] indices) { + boolean[] row = new boolean[n]; + boolean[] column = new boolean[m]; + for (int[] index : indices) { + row[index[0]] ^= true; + column[index[1]] ^= true; + } + int oddNumberCount = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + oddNumberCount += row[i] ^ column[j] ? 1 : 0; + } + } + return oddNumberCount; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1254.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1254.java new file mode 100644 index 0000000000..4bfc4aa281 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1254.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _1254 { + public static class Solution1 { + /* + * BFS each cell in the grid with a visited matrix to avoid infinite loop. + */ + public int closedIsland(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + boolean[][] visited = new boolean[m][n]; + int count = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 0 && !visited[i][j] && bfs(i, j, m, n, grid, visited)) { + count++; + } + } + } + return count; + } + + private boolean bfs(int x, int y, int m, int n, int[][] grid, boolean[][] visited) { + int[] dirs = new int[] {0, 1, 0, -1, 0}; + Queue q = new LinkedList<>(); + q.offer(new int[] {x, y}); + boolean isClosed = true; + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + int[] curr = q.poll(); + for (int j = 0; j < dirs.length - 1; j++) { + int newx = dirs[j] + curr[0]; + int newy = dirs[j + 1] + curr[1]; + if (newx < 0 || newx >= m || newy < 0 || newy >= n) { + // this means that (x,y) is a boundary cell + isClosed = false; + } else if (!visited[newx][newy] && grid[newx][newy] == 0) { + visited[newx][newy] = true; + q.offer(new int[] {newx, newy}); + } + } + } + } + return isClosed; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1257.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1257.java new file mode 100644 index 0000000000..295314202f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1257.java @@ -0,0 +1,56 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1257 { + public static class Solution1 { + public String findSmallestRegion( + List> regions, String region1, String region2) { + Map childToParent = new HashMap<>(); + for (List region : regions) { + for (int i = 1; i < region.size(); i++) { + String parent = region.get(0); + String child = region.get(i); + childToParent.put(child, parent); + } + } + List path1 = findPath(childToParent, region1); + List path2 = findPath(childToParent, region2); + int i = path1.size() - 1; + int j = path2.size() - 1; + for (; i >= 0 && j >= 0; ) { + if (path1.get(i).equals(path2.get(j))) { + i--; + j--; + } else { + if (i == path1.size() - 1) { + return path1.get(i); + } else { + return path1.get(i + 1); + } + } + } + if (i < 0) { + return path1.get(0); + } else { + return path2.get(0); + } + } + + private List findPath(Map childToParent, String leaf) { + List path = new ArrayList<>(); + do { + path.add(leaf); + if (childToParent.containsKey(leaf)) { + leaf = childToParent.get(leaf); + } else { + break; + } + } while (true); + return path; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1258.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1258.java new file mode 100644 index 0000000000..21f4e8e59b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1258.java @@ -0,0 +1,74 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class _1258 { + public static class Solution1 { + public List generateSentences(List> synonyms, String text) { + String[] words = text.split(" "); + Map> map = buildSynonymDict(words, synonyms); + Set result = new HashSet<>(); + result.add(text); + for (int i = 0; i < words.length; i++) { + List list = new ArrayList<>(); + for (String next : result) { + list.add(next); + list.addAll(findAllSynonymForThisWord(next, i, map)); + } + result.addAll(list); + } + List list = new ArrayList<>(); + list.addAll(result); + Collections.sort(list); + return list; + } + + private List findAllSynonymForThisWord( + String sentence, int i, Map> map) { + String[] words = sentence.split(" "); + List list = new ArrayList<>(); + Set synonyms = map.get(words[i]); + for (String s : synonyms) { + words[i] = s; + list.add(formWord(words)); + } + return list; + } + + private String formWord(String[] words) { + StringBuilder sb = new StringBuilder(); + for (String word : words) { + sb.append(word); + sb.append(" "); + } + return sb.substring(0, sb.length() - 1); + } + + private Map> buildSynonymDict( + String[] words, List> synonyms) { + Map> map = new HashMap<>(); + for (String key : words) { + if (!map.containsKey(key)) { + map.put(key, new HashSet<>()); + } + map.get(key).add(key); + } + for (List list : synonyms) { + for (String key : map.keySet()) { + if (map.get(key).contains(list.get(0))) { + map.get(key).add(list.get(1)); + } else if (map.get(key).contains(list.get(1))) { + map.get(key).add(list.get(0)); + } + } + } + return map; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1260.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1260.java new file mode 100644 index 0000000000..ccbeefec5c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1260.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +public class _1260 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/shift-2d-grid/discuss/431102/JavaPython-3-simple-code-using-mod + */ + public List> shiftGrid(int[][] grid, int k) { + int m = grid.length; + int n = grid[0].length; + int totalNumbers = m * n; + int start = totalNumbers - k % totalNumbers; + LinkedList> result = new LinkedList<>(); + for (int i = start; i < totalNumbers + start; i++) { + int moveIndex = i % totalNumbers; + int moveRow = moveIndex / n; + int moveColumn = moveIndex % n; + if ((i - start) % n == 0) { + result.add(new ArrayList<>()); + } + result.peekLast().add(grid[moveRow][moveColumn]); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1261.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1261.java new file mode 100644 index 0000000000..c533edcdfa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1261.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1261 { + public static class Solution1 { + class FindElements { + + TreeNode recoveredRoot; + + public FindElements(TreeNode root) { + recoveredRoot = new TreeNode(0); + recoveredRoot = recoverTree(root, recoveredRoot); + } + + private TreeNode recoverTree(TreeNode root, TreeNode recoveredRoot) { + if (root == null) { + return recoveredRoot; + } + if (root.left != null) { + recoveredRoot.left = new TreeNode(recoveredRoot.val * 2 + 1); + } + if (root.right != null) { + recoveredRoot.right = new TreeNode(recoveredRoot.val * 2 + 2); + } + recoverTree(root.left, recoveredRoot.left); + recoverTree(root.right, recoveredRoot.right); + return recoveredRoot; + } + + public boolean find(int target) { + return find(recoveredRoot, target); + } + + private boolean find(TreeNode root, int target) { + if (root == null) { + return false; + } else if (root.val == target) { + return true; + } + return find(root.left, target) || find(root.right, target); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1265.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1265.java new file mode 100644 index 0000000000..5d78d0435a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1265.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Stack; + +public class _1265 { + + interface ImmutableListNode { + ImmutableListNode getNext(); + + void printValue(); + } + + public static class Solution1 { + public void printLinkedListInReverse(ImmutableListNode head) { + Stack stack = new Stack(); + while (head != null) { + stack.add(head); + head = head.getNext(); + } + while (!stack.isEmpty()) { + head = stack.pop(); + head.printValue(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1266.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1266.java new file mode 100644 index 0000000000..1502d08457 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1266.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1266 { + public static class Solution1 { + /* + * Time: O(n) + * Space: O(1) + *

+ * credit: https://leetcode.com/problems/minimum-time-visiting-all-points/discuss/436142/Sum-of-Chebyshev-distance-between-two-consecutive-points + */ + public int minTimeToVisitAllPoints(int[][] points) { + int minTime = 0; + for (int i = 0; i < points.length - 1; i++) { + minTime += chebyshevDistance(points[i], points[i + 1]); + } + return minTime; + } + + private int chebyshevDistance(int[] pointA, int[] pointB) { + return Math.max(Math.abs(pointA[0] - pointB[0]), Math.abs(pointA[1] - pointB[1])); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1267.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1267.java new file mode 100644 index 0000000000..400a362945 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1267.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1267 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/count-servers-that-communicate/discuss/436188/Java-or-Clean-And-Simple-or-Beats-100 + */ + public int countServers(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + int[] rowCount = new int[m]; + int[] columnCount = new int[n]; + int total = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + rowCount[i]++; + columnCount[j]++; + total++; + } + } + } + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1 && rowCount[i] == 1 && columnCount[j] == 1) { + total--; + } + } + } + return total; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1268.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1268.java new file mode 100644 index 0000000000..e7fea19a68 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1268.java @@ -0,0 +1,162 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1268 { + public static class Solution1 { + public List> suggestedProducts(String[] products, String searchWord) { + TrieNode root = buildTrie(products); + List> result = new ArrayList<>(); + for (int i = 1; i <= searchWord.length(); i++) { + result.add(findTopThreeMatches(root, searchWord.substring(0, i))); + } + return result; + } + + private List findTopThreeMatches(TrieNode root, String searchTerm) { + List result = new ArrayList<>(); + TrieNode node = root; + for (char c : searchTerm.toCharArray()) { + if (node.children[c - 'a'] == null) { + return result; + } else { + node = node.children[c - 'a']; + } + } + if (node.isWord) { + result.add(searchTerm); + } + for (TrieNode child : node.children) { + if (child != null) { + List thisResult = dfs(child, searchTerm, new ArrayList<>()); + result.addAll(thisResult); + if (result.size() >= 3) { + return result.subList(0, 3); + } + } + } + return result; + } + + private List dfs(TrieNode node, String substring, List result) { + if (node.isWord) { + result.add(substring + node.c); + if (result.size() >= 3) { + return result; + } + } + for (TrieNode child : node.children) { + if (child != null) { + dfs(child, substring + node.c, result); + } + } + return result; + } + + private TrieNode buildTrie(String[] products) { + TrieNode root = new TrieNode(' '); + for (String pro : products) { + insert(pro, root); + } + return root; + } + + private void insert(String word, TrieNode root) { + TrieNode node = root; + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + if (node.children[c - 'a'] == null) { + node.children[c - 'a'] = new TrieNode(c); + } + node = node.children[c - 'a']; + } + node.isWord = true; + } + + class TrieNode { + TrieNode[] children; + boolean isWord; + char c; + + public TrieNode(char c) { + this.c = c; + this.children = new TrieNode[26]; + } + } + } + + public static class Solution2 { + public List> suggestedProducts(String[] products, String searchWord) { + TrieNode root = buildTrie(products); + List> result = new ArrayList<>(); + for (int i = 1; i <= searchWord.length(); i++) { + String searchTerm = searchWord.substring(0, i); + TrieNode tmp = root; + List searchResult = new ArrayList<>(); + for (int j = 0; j < searchTerm.length(); j++) { + char c = searchTerm.charAt(j); + if (tmp.children[c - 'a'] == null) { + break; + } else { + tmp = tmp.children[c - 'a']; + } + if (j == searchTerm.length() - 1) { + searchResult.addAll(findAllWords(tmp, searchTerm)); + } + } + result.add(searchResult.size() > 3 ? searchResult.subList(0, 3) : searchResult); + } + return result; + } + + private List findAllWords(TrieNode trieNode, String prefix) { + List result = new ArrayList<>(); + if (trieNode.isWord) { + result.add(prefix); + if (result.size() > 3) { + return result; + } + } + for (TrieNode node : trieNode.children) { + if (node != null) { + result.addAll(findAllWords(node, prefix + node.val)); + if (result.size() > 3) { + return result; + } + } + } + return result; + } + + private TrieNode buildTrie(String[] words) { + TrieNode root = new TrieNode(' '); + for (String word : words) { + TrieNode tmp = root; + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + if (tmp.children[c - 'a'] == null) { + tmp.children[c - 'a'] = new TrieNode(c); + } + tmp = tmp.children[c - 'a']; + if (i == word.length() - 1) { + tmp.isWord = true; + } + } + } + return root; + } + + public class TrieNode { + TrieNode[] children; + char val; + boolean isWord; + + public TrieNode(char val) { + this.children = new TrieNode[26]; + this.val = val; + this.isWord = false; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1271.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1271.java new file mode 100644 index 0000000000..3eee4f8c4c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1271.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _1271 { + public static class Solution1 { + public String toHexspeak(String num) { + long numInt = Long.parseLong(num); + String hexString = Long.toHexString(numInt); + StringBuilder sb = new StringBuilder(); + Set set = + new HashSet<>( + Arrays.asList( + 'A', 'B', 'C', 'D', 'E', 'F', '1', '0', 'a', 'b', 'c', 'd', 'e', + 'f')); + for (char c : hexString.toCharArray()) { + if (!set.contains(c)) { + return "ERROR"; + } else if (c == '1') { + sb.append("I"); + } else if (c == '0') { + sb.append("O"); + } else { + sb.append(Character.toUpperCase(c)); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1273.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1273.java new file mode 100644 index 0000000000..58fdd13598 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1273.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1273 { + public static class Solution1 { + public int deleteTreeNodes(int nodes, int[] parent, int[] value) { + int len = parent.length; + for (int i = len - 1; i > 0; ) { + int sum = 0; + int parentIndex = parent[i]; + while (i > 0 && parent[i] == parentIndex) { + sum += value[i--]; + } + // we'll reset the value to be the newly computed sum of this node and all of its + // children + value[parentIndex] = value[parentIndex] + sum; + } + // then we'll reset this node's children to be zero if this node's computed sum is zero + for (int i = 0; i < value.length; i++) { + if (value[i] == 0) { + for (int j = 0; j < parent.length; j++) { + if (parent[j] == i) { + value[j] = 0; + } + } + } + } + return (int) Arrays.stream(value).filter(i -> i != 0).count(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1275.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1275.java new file mode 100644 index 0000000000..02d2cf56fa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1275.java @@ -0,0 +1,65 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1275 { + public static class Solution1 { + public String tictactoe(int[][] moves) { + String[][] board = new String[3][3]; + for (int i = 0; i < moves.length; i++) { + if (i % 2 == 0) { + board[moves[i][0]][moves[i][1]] = "X"; + } else { + board[moves[i][0]][moves[i][1]] = "O"; + } + if (i > 3) { + if (!wins(board).equals("")) { + return wins(board); + } + } + } + return moves.length == 9 ? "Draw" : "Pending"; + } + + private String wins(String[][] board) { + // check rows + for (int i = 0; i < 3; i++) { + if (board[i][0] == null) { + break; + } + String str = board[i][0]; + if (str.equals(board[i][1]) && str.equals(board[i][2])) { + return getWinner(str); + } + } + + // check columns + for (int j = 0; j < 3; j++) { + if (board[0][j] == null) { + break; + } + String str = board[0][j]; + if (str.equals(board[1][j]) && str.equals(board[2][j])) { + return getWinner(str); + } + } + + // check diagonals + if (board[1][1] == null) { + return ""; + } + String str = board[1][1]; + if (str.equals(board[0][0]) && str.equals(board[2][2]) + || (str.equals(board[0][2]) && str.equals(board[2][0]))) { + return getWinner(str); + } + return ""; + } + + private String getWinner(String str) { + if (str.equals("X")) { + return "A"; + } else { + return "B"; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1277.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1277.java new file mode 100644 index 0000000000..20d1bd0e00 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1277.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1277 { + public static class Solution1 { + /* + * In-place solution. + * credit: https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441306/Python-DP-solution + */ + public int countSquares(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + int count = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (matrix[i][j] > 0 && i > 0 && j > 0) { + matrix[i][j] = + Math.min( + matrix[i - 1][j - 1], + Math.min(matrix[i - 1][j], matrix[i][j - 1])) + + 1; + } + count += matrix[i][j]; + } + } + return count; + } + } + + public static class Solution2 { + /* + * Use m*n extra space solution. + * credit: https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441312/Java-Simple-DP-solution + */ + public int countSquares(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + int[][] dp = new int[m][n]; + for (int i = 0; i < m; i++) { + if (matrix[i][0] == 1) { + dp[i][0] = 1; + } + } + for (int j = 0; j < n; j++) { + if (matrix[0][j] == 1) { + dp[0][j] = 1; + } + } + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + if (matrix[i][j] == 1) { + dp[i][j] = + Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + + 1; + } + } + } + int count = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + count += dp[i][j]; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1281.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1281.java new file mode 100644 index 0000000000..df9c7946ec --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1281.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1281 { + public static class Solution1 { + public int subtractProductAndSum(int n) { + int sum = 0; + int product = 1; + while (n > 0) { + int digit = n % 10; + n /= 10; + sum += digit; + product *= digit; + } + return product - sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1282.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1282.java new file mode 100644 index 0000000000..bba47dc15b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1282.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1282 { + public static class Solution1 { + public List> groupThePeople(int[] groupSizes) { + Map> map = new HashMap<>(); + for (int i = 0; i < groupSizes.length; i++) { + List list = map.getOrDefault(groupSizes[i], new ArrayList<>()); + list.add(i); + map.put(groupSizes[i], list); + } + List> result = new ArrayList<>(); + for (int key : map.keySet()) { + List list = map.get(key); + int i = 0; + do { + result.add(list.subList(i, i + key)); + i += key; + } while (i + key <= list.size()); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1283.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1283.java new file mode 100644 index 0000000000..4413a895e6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1283.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1283 { + public static class Solution { + + public int smallestDivisor(int[] nums, int threshold) { + + int start = 1; + int result = 0; + int end = Integer.MAX_VALUE; + while (start <= end) { + int middle = start + (end - start) / 2; + if (isSumLessThanThreshold(middle, nums, threshold)) { + result = middle; + end = middle - 1; + } else { + start = middle + 1; + } + } + return result; + } + + private boolean isSumLessThanThreshold(int middle, int[] nums, int threshold) { + int sum = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] % middle == 0) { + sum += nums[i] / middle; + } else { + sum += nums[i] / middle + 1; + } + } + return sum <= threshold; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1286.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1286.java new file mode 100644 index 0000000000..8338d21eff --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1286.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1286 { + public static class Solution1 { + public static class CombinationIterator { + + List list; + int index; + int combinationLength; + boolean[] visited; + + public CombinationIterator(String characters, int combinationLength) { + this.index = 0; + this.list = new ArrayList<>(); + this.combinationLength = combinationLength; + this.visited = new boolean[characters.length()]; + buildAllCombinations(characters, 0, new StringBuilder(), visited); + } + + private void buildAllCombinations( + String characters, int start, StringBuilder sb, boolean[] visited) { + if (sb.length() == combinationLength) { + list.add(sb.toString()); + return; + } else { + for (int i = start; i < characters.length(); ) { + if (!visited[i]) { + sb.append(characters.charAt(i)); + visited[i] = true; + buildAllCombinations(characters, i++, sb, visited); + visited[i - 1] = false; + sb.setLength(sb.length() - 1); + } else { + i++; + } + } + } + } + + public String next() { + return list.get(index++); + } + + public boolean hasNext() { + return index < list.size(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1287.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1287.java new file mode 100644 index 0000000000..00d22dd53c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1287.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1287 { + public static class Solution1 { + public int findSpecialInteger(int[] arr) { + int quarter = arr.length / 4; + for (int i = 0; i < arr.length - quarter; i++) { + if (arr[i] == arr[i + quarter]) { + return arr[i]; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1289.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1289.java new file mode 100644 index 0000000000..831a04090f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1289.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1289 { + public static class Solution1 { + public int minFallingPathSum(int[][] arr) { + int size = arr.length; + int[][] dp = new int[size][size]; + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + if (i == 0) { + dp[i][j] = arr[i][j]; + } else { + int previousRowMin = Integer.MAX_VALUE; + for (int k = 0; k < size; k++) { + if (k != j) { + previousRowMin = Math.min(dp[i - 1][k], previousRowMin); + } + } + dp[i][j] = arr[i][j] + previousRowMin; + } + } + } + int result = dp[size - 1][size - 1]; + for (int i = 0; i < size; i++) { + result = Math.min(result, dp[size - 1][i]); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1290.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1290.java new file mode 100644 index 0000000000..5fe1f0f198 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1290.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _1290 { + public static class Solution1 { + public int getDecimalValue(ListNode head) { + StringBuilder sb = new StringBuilder(); + while (head != null) { + sb.append(head.val); + head = head.next; + } + return Integer.parseInt(sb.toString(), 2); + } + } + + public static class Solution2 { + public int getDecimalValue(ListNode head) { + int sum = 0; + while (head != null) { + sum *= 2; + sum += head.val; + head = head.next; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1291.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1291.java new file mode 100644 index 0000000000..a2c414c39f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1291.java @@ -0,0 +1,12 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.List; + +public class _1291 { + public static class Solution1 { + public List sequentialDigits(int low, int high) { + // TODO: implement it + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1295.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1295.java new file mode 100644 index 0000000000..4a22819476 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1295.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1295 { + public static class Solution1 { + public int findNumbers(int[] nums) { + int count = 0; + for (int num : nums) { + if (String.valueOf(num).length() % 2 == 0) { + count++; + } + } + return count; + } + } + + public static class Solution2 { + public int findNumbers(int[] nums) { + return (int) + Arrays.stream(nums) + .filter(num -> String.valueOf(num).length() % 2 == 0) + .count(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1296.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1296.java new file mode 100644 index 0000000000..b52aed0c1c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1296.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.TreeMap; + +public class _1296 { + public static class Solution1 { + public boolean isPossibleDivide(int[] nums, int k) { + if (nums.length % k != 0) { + return false; + } + TreeMap treeMap = new TreeMap(); + int min = nums[0]; + for (int num : nums) { + treeMap.put(num, treeMap.getOrDefault(num, 0) + 1); + min = Math.min(min, num); + } + while (!treeMap.isEmpty()) { + if (!isConsecutiveK(treeMap, min, k)) { + return false; + } + min = findNextMin(treeMap); + if (min == Integer.MIN_VALUE) { + break; + } + } + return true; + } + + private int findNextMin(TreeMap treeMap) { + return treeMap.isEmpty() + ? Integer.MIN_VALUE + : treeMap.entrySet().iterator().next().getKey(); + } + + private boolean isConsecutiveK(TreeMap treeMap, int min, int k) { + int count = treeMap.get(min); + treeMap.remove(min); + for (int i = 1; i < k; i++) { + int key = min + i; + if (!treeMap.containsKey(key) || treeMap.get(key) < count) { + return false; + } else if (treeMap.get(key) > count) { + treeMap.put(key, treeMap.get(key) - count); + } else { + treeMap.remove(key); + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1297.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1297.java new file mode 100644 index 0000000000..d67b5ac37b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1297.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _1297 { + public static class Solution1 { + public int maxFreq(String s, int maxLetters, int minSize, int maxSize) { + int maxFreq = 0; + for (int size = minSize; size <= maxSize; size++) { + maxFreq = Math.max(maxFreq, getMaxFreqWithThisSize(s, maxLetters, size)); + } + return maxFreq; + } + + private int getMaxFreqWithThisSize(String str, int maxLetters, int size) { + Map map = new HashMap<>(); + for (int i = 0; i <= str.length() - size; i++) { + String substring = str.substring(i, i + size); + Set set = new HashSet<>(); + for (int j = 0; j < substring.length(); j++) { + set.add(substring.charAt(j)); + if (set.size() > maxLetters) { + break; + } + } + if (set.size() <= maxLetters) { + map.put(substring, map.getOrDefault(substring, 0) + 1); + } + } + int max = 0; + for (String key : map.keySet()) { + max = Math.max(max, map.get(key)); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1299.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1299.java new file mode 100644 index 0000000000..115b441afb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1299.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1299 { + public static class Solution1 { + public int[] replaceElements(int[] arr) { + PriorityQueue heap = new PriorityQueue<>((a, b) -> b - a); + for (int num : arr) { + heap.offer(num); + } + int[] result = new int[arr.length]; + for (int i = 0; i < arr.length - 1; i++) { + heap.remove(arr[i]); + result[i] = heap.peek(); + } + result[arr.length - 1] = -1; + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1300.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1300.java new file mode 100644 index 0000000000..68b839f395 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1300.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1300 { + public static class Solution1 { + public int findBestValue(int[] arr, int target) { + int ave = target / arr.length; + int max = arr[0]; + int min = arr[0]; + for (int i = 1; i < arr.length; i++) { + min = Math.min(min, arr[i]); + max = Math.max(max, arr[i]); + } + if (ave >= max) { + return max; + } + // if ave is the best value, what's the difference to target? + int closetDiff = findClosestDiffIfReplaceWithVal(arr, ave, target); + int bestValue = ave; + // extend candidate towards the right to see how close the sum could be to target + int candidateOnTheRight = ave; + while (candidateOnTheRight <= max) { + int thisOne = findClosestDiffIfReplaceWithVal(arr, ++candidateOnTheRight, target); + if (thisOne >= closetDiff) { + break; + } else { + closetDiff = thisOne; + bestValue = candidateOnTheRight; + } + } + + // extend candidate towards the left to see how close the sum could be to target + int candidateOnTheLeft = ave; + while (candidateOnTheLeft >= min) { + int thisOne = findClosestDiffIfReplaceWithVal(arr, --candidateOnTheLeft, target); + if (thisOne >= closetDiff) { + break; + } else { + closetDiff = thisOne; + bestValue = candidateOnTheLeft; + } + } + return bestValue; + } + + private int findClosestDiffIfReplaceWithVal(int[] arr, int replaceValue, int target) { + int sum = 0; + for (int i = 0; i < arr.length; i++) { + if (arr[i] > replaceValue) { + sum += replaceValue; + } else { + sum += arr[i]; + } + } + return Math.abs(sum - target); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1302.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1302.java new file mode 100644 index 0000000000..b0a9c6ee59 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1302.java @@ -0,0 +1,68 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; + +public class _1302 { + public static class Solution1 { + public int deepestLeavesSum(TreeNode root) { + int depth = maxDepth(root); + return bfs(root, depth); + } + + private int bfs(TreeNode root, int depth) { + Queue queue = new LinkedList<>(); + queue.offer(root); + int currentLevel = 0; + int sum = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + currentLevel++; + for (int i = 0; i < size; i++) { + TreeNode currNode = queue.poll(); + if (currentLevel == depth) { + sum += currNode.val; + } + if (currNode.left != null) { + queue.offer(currNode.left); + } + if (currNode.right != null) { + queue.offer(currNode.right); + } + } + } + return sum; + } + + private int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } + } + + public static class Solution2 { + public int deepestLeavesSum(TreeNode root) { + Queue queue = new LinkedList<>(); + queue.offer(root); + int sum = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + sum = 0; + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + sum += curr.val; + if (curr.left != null) { + queue.offer(curr.left); + } + if (curr.right != null) { + queue.offer(curr.right); + } + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1304.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1304.java new file mode 100644 index 0000000000..6fc1322123 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1304.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1304 { + public static class Solution1 { + public int[] sumZero(int n) { + int[] result = new int[n]; + int start = -n / 2; + for (int i = 0; i < n / 2; i++) { + result[i] = start++; + } + if (n % 2 == 0) { + start++; + } + for (int i = n / 2; i < n; i++) { + result[i] = start++; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1305.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1305.java new file mode 100644 index 0000000000..57757a7313 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1305.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _1305 { + public static class Solution1 { + public List getAllElements(TreeNode root1, TreeNode root2) { + List list1 = getAllNodes(root1); + List list2 = getAllNodes(root2); + List merged = new ArrayList<>(); + merged.addAll(list1); + merged.addAll(list2); + Collections.sort(merged); + return merged; + } + + private List getAllNodes(TreeNode root) { + List list = new ArrayList<>(); + return inorder(root, list); + } + + List inorder(TreeNode root, List result) { + if (root == null) { + return result; + } + inorder(root.left, result); + result.add(root.val); + return inorder(root.right, result); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1309.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1309.java new file mode 100644 index 0000000000..ad76db7b9c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1309.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1309 { + public static class Solution1 { + // TODO: very silly solution, optimze it + public String freqAlphabets(String s) { + Map map = new HashMap<>(); + map.put("1", "a"); + map.put("2", "b"); + map.put("3", "c"); + map.put("4", "d"); + map.put("5", "e"); + map.put("6", "f"); + map.put("7", "g"); + map.put("8", "h"); + map.put("9", "i"); + map.put("10#", "j"); + map.put("11#", "k"); + map.put("12#", "l"); + map.put("13#", "m"); + map.put("14#", "n"); + map.put("15#", "o"); + map.put("16#", "p"); + map.put("17#", "q"); + map.put("18#", "r"); + map.put("19#", "s"); + map.put("20#", "t"); + map.put("21#", "u"); + map.put("22#", "v"); + map.put("23#", "w"); + map.put("24#", "x"); + map.put("25#", "y"); + map.put("26#", "z"); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); ) { + if (Integer.parseInt("" + s.charAt(i)) == 1 + && i + 1 < s.length() + && i + 2 < s.length() + && s.charAt(i + 2) == '#') { + sb.append(map.get(s.substring(i, i + 3))); + i += 3; + } else if (Integer.parseInt("" + s.charAt(i)) == 2 + && i + 1 < s.length() + && i + 2 < s.length() + && s.charAt(i + 2) == '#') { + sb.append(map.get(s.substring(i, i + 3))); + i += 3; + } else { + sb.append(map.get("" + s.charAt(i))); + i++; + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1313.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1313.java new file mode 100644 index 0000000000..afc29169c5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1313.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1313 { + public static class Solution1 { + public int[] decompressRLElist(int[] nums) { + List list = new ArrayList<>(); + for (int i = 0; i < nums.length - 1; i += 2) { + int count = nums[i]; + int value = nums[i + 1]; + while (count-- > 0) { + list.add(value); + } + } + return list.stream().mapToInt(integer -> integer).toArray(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1314.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1314.java new file mode 100644 index 0000000000..b02b15e487 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1314.java @@ -0,0 +1,87 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1314 { + public static class Solution1 { + /* + * This is a brute force solution without using prefix sum. i.e. lots of repeated computation. + */ + public int[][] matrixBlockSum(int[][] mat, int k) { + int m = mat.length; + int n = mat[0].length; + int[][] answer = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + List iRange = findRange(i, k, m); + List jRange = findRange(j, k, n); + int sum = 0; + for (int ii = 0; ii < iRange.size(); ii++) { + for (int jj = 0; jj < jRange.size(); jj++) { + sum += mat[iRange.get(ii)][jRange.get(jj)]; + } + } + answer[i][j] = sum; + } + } + return answer; + } + + private List findRange(int iOrJ, int k, int upper) { + int min = (iOrJ - k) < 0 ? 0 : (iOrJ - k); + int max = (iOrJ + k) >= upper ? (upper - 1) : (iOrJ + k); + List range = new ArrayList<>(); + for (int i = min; i <= max; i++) { + range.add(i); + } + return range; + } + } + + public static class Solution2 { + /* + * This is using prefix sum, much more efficient and saves a lot of repeated computation, + * built on top of this: https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_304.java + */ + public int[][] matrixBlockSum(int[][] mat, int k) { + int m = mat.length; + int n = mat[0].length; + int[][] prefixSum = new int[m + 1][n + 1]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + // because we add prefixSum[i + 1][j] and prefixSum[i][j + 1], this means we + // added their shared area twice, so we'll deduct it once: prefixSum[i][j] + prefixSum[i + 1][j + 1] = + mat[i][j] + prefixSum[i + 1][j] + prefixSum[i][j + 1] - prefixSum[i][j]; + } + } + int[][] result = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + int[] range = findRange(i, j, k, m, n); + int row1 = range[0]; + int col1 = range[2]; + int row2 = range[1]; + int col2 = range[3]; + // because we deducted prefixSum[row2 + 1][col1] and prefixSum[row1][col2 + 1], + // we deducted the shared area prefixSum[row1][col1] twice, so we added it back + result[i][j] = + prefixSum[row2 + 1][col2 + 1] + - prefixSum[row2 + 1][col1] + - prefixSum[row1][col2 + 1] + + prefixSum[row1][col1]; + } + } + return result; + } + + private int[] findRange(int i, int j, int k, int m, int n) { + int rowMin = i - k < 0 ? 0 : i - k; + int rowMax = i + k < m ? i + k : m - 1; + int colMin = j - k < 0 ? 0 : j - k; + int colMax = j + k < n ? j + k : n - 1; + return new int[] {rowMin, rowMax, colMin, colMax}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1315.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1315.java new file mode 100644 index 0000000000..c84bec3ae9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1315.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1315 { + public static class Solution1 { + public int sumEvenGrandparent(TreeNode root) { + if (root == null) { + return 0; + } + return dfs(root, root.left, 0) + dfs(root, root.right, 0); + } + + private int dfs(TreeNode grandparent, TreeNode parent, int sum) { + if (grandparent == null || parent == null) { + return sum; + } + if (grandparent.val % 2 == 0 && parent.left != null) { + sum += parent.left.val; + } + if (grandparent.val % 2 == 0 && parent.right != null) { + sum += parent.right.val; + } + sum = dfs(parent, parent.left, sum); + sum = dfs(parent, parent.right, sum); + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1317.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1317.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_1317.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1317.java index e3b43e956c..8ea7aa8aaf 100644 --- a/src/main/java/com/fishercoder/solutions/_1317.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1317.java @@ -1,6 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; -/** +/* * 1317. Convert Integer to the Sum of Two No-Zero Integers * * Given an integer n. No-Zero integer is a positive integer which doesn't contain any 0 in its decimal representation. @@ -41,7 +41,7 @@ public int[] getNoZeroIntegers(int n) { int right = n - 1; while (left <= right) { if (noZero(left) && noZero(right)) { - return new int[]{left, right}; + return new int[] {left, right}; } else { left++; right--; diff --git a/src/main/java/com/fishercoder/solutions/_1323.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1323.java similarity index 82% rename from src/main/java/com/fishercoder/solutions/_1323.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1323.java index d49813c062..4726ccfc67 100644 --- a/src/main/java/com/fishercoder/solutions/_1323.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1323.java @@ -1,8 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.stream.IntStream; -/** +/* * 1323. Maximum 69 Number * * Given a positive integer num consisting only of digits 6 and 9. @@ -36,7 +36,10 @@ public class _1323 { public static class Solution1 { public int maximum69Number(int num) { char[] chars = Integer.toString(num).toCharArray(); - IntStream.range(0, chars.length).filter(i -> chars[i] == '6').findFirst().ifPresent(i -> chars[i] = '9'); + IntStream.range(0, chars.length) + .filter(i -> chars[i] == '6') + .findFirst() + .ifPresent(i -> chars[i] = '9'); return Integer.parseInt(new String(chars)); } } diff --git a/src/main/java/com/fishercoder/solutions/_1324.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1324.java similarity index 97% rename from src/main/java/com/fishercoder/solutions/_1324.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1324.java index 4c6eb574fd..2030720c17 100644 --- a/src/main/java/com/fishercoder/solutions/_1324.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1324.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.List; -/** +/* * 1324. Print Words Vertically * * Given a string s. Return all the words vertically in the same order in which they appear in s. diff --git a/src/main/java/com/fishercoder/solutions/_1325.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1325.java similarity index 88% rename from src/main/java/com/fishercoder/solutions/_1325.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1325.java index cf0ee5cf28..fcd3e180f5 100644 --- a/src/main/java/com/fishercoder/solutions/_1325.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1325.java @@ -1,8 +1,8 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import com.fishercoder.common.classes.TreeNode; -/** +/* * 1325. Delete Leaves With a Given Value * * Given a binary tree root and an integer target, delete all the leaf nodes with value target. @@ -66,7 +66,7 @@ * */ public class _1325 { public static class Solution1 { - /** + /* * my original but verbose solution */ public TreeNode removeLeafNodes(TreeNode root, int target) { @@ -84,10 +84,16 @@ private TreeNode removeLeafNodes(int target, TreeNode root) { root = null; return root; } - if (root.left != null && root.left.val == target && root.left.left == null && root.left.right == null) { + if (root.left != null + && root.left.val == target + && root.left.left == null + && root.left.right == null) { root.left = null; } - if (root.right != null && root.right.val == target && root.right.left == null && root.right.right == null) { + if (root.right != null + && root.right.val == target + && root.right.left == null + && root.right.right == null) { root.right = null; } removeLeafNodes(target, root.left); @@ -107,7 +113,7 @@ private boolean hasTargetLeafNodes(TreeNode root, int target) { } public static class Solution2 { - /**A much more concise and efficient solution.*/ + /*A much more concise and efficient solution.*/ public TreeNode removeLeafNodes(TreeNode root, int target) { if (root == null) { return root; diff --git a/src/main/java/com/fishercoder/solutions/_1329.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1329.java similarity index 97% rename from src/main/java/com/fishercoder/solutions/_1329.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1329.java index 6d1471eb99..42394c83e4 100644 --- a/src/main/java/com/fishercoder/solutions/_1329.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1329.java @@ -1,10 +1,10 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.Collections; import java.util.List; -/** +/* * 1329. Sort the Matrix Diagonally * * Given a m * n matrix mat of integers, diff --git a/src/main/java/com/fishercoder/solutions/_1331.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1331.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_1331.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1331.java index 689c9a9d10..c008344979 100644 --- a/src/main/java/com/fishercoder/solutions/_1331.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1331.java @@ -1,10 +1,10 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.HashMap; import java.util.Map; import java.util.TreeSet; -/** +/* * 1331. Rank Transform of an Array * * Given an array of integers arr, replace each element with its rank. diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1332.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1332.java new file mode 100644 index 0000000000..2369bb9b63 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1332.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1332 { + public static class Solution1 { + /* + * Notice: there are only two characters in the given string: 'a' and 'b' + */ + public int removePalindromeSub(String s) { + if (s.isEmpty()) { + return 0; + } + if (s.equals((new StringBuilder(s)).reverse().toString())) { + return 1; + } + return 2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1333.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1333.java similarity index 90% rename from src/main/java/com/fishercoder/solutions/_1333.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1333.java index cc7b8a4543..20002b62a3 100644 --- a/src/main/java/com/fishercoder/solutions/_1333.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1333.java @@ -1,11 +1,11 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; -/** +/* * 1333. Filter Restaurants by Vegan-Friendly, Price and Distance * * Given the array restaurants where restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]. @@ -49,11 +49,13 @@ * */ public class _1333 { public static class Solution1 { - public List filterRestaurants(int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) { + public List filterRestaurants( + int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) { List list = new ArrayList<>(); for (int[] restaurant : restaurants) { if (((veganFriendly == 1 && restaurant[2] == 1) || veganFriendly == 0) - && restaurant[3] <= maxPrice && restaurant[4] <= maxDistance) { + && restaurant[3] <= maxPrice + && restaurant[4] <= maxDistance) { list.add(restaurant); } } diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1334.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1334.java new file mode 100644 index 0000000000..168db640c2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1334.java @@ -0,0 +1,82 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.PriorityQueue; + +public class _1334 { + public static class Solution1 { + /* + * Dijkstra's algorithm to find the shortest path from each node to all possibly reachable nodes within limit. + * Dijkstra's algorithm applies to weights are non-negative problems. + * Keys to implement Dijkstra's algorithm: + * 1. use an array to hold the shortest distance to each node for each node; + * 2. initially, only the starting node distance is zero, all other nodes' distances to be infinity; + * 3. use a PriorityQueue to poll out the next node that has the shortest distance and scan through all its neighbors, + * if the cost can be updated, then put it into the priority queue (this is a critical key to implement Dijkstra!) + * Only when this node's code could be updated/shortened, we'll put it into the priority queue/minHeap, + * so that next time, it'll be polled and processed based on priority order + */ + public int findTheCity(int n, int[][] edges, int distanceThreshold) { + List> graph = new ArrayList(); + int[][] shortestPaths = new int[n][n]; + for (int i = 0; i < n; i++) { + graph.add(new ArrayList<>()); + } + for (int[] edge : edges) { + int source = edge[0]; + int dest = edge[1]; + int weight = edge[2]; + graph.get(source).add(new int[] {dest, weight}); + graph.get(dest).add(new int[] {source, weight}); + } + for (int i = 0; i < n; i++) { + dijkstraAlgo(graph, i, shortestPaths[i]); + } + return findCityWithFewestReachableCities(shortestPaths, distanceThreshold); + } + + private int findCityWithFewestReachableCities( + int[][] shortestPaths, int distanceThreshold) { + int ans = 0; + int fewestReachable = shortestPaths.length; + for (int i = 0; i < shortestPaths.length; i++) { + int reachable = 0; + for (int j = 0; j < shortestPaths[0].length; j++) { + if (i != j && shortestPaths[i][j] <= distanceThreshold) { + reachable++; + } + } + if (reachable <= fewestReachable) { + fewestReachable = reachable; + ans = i; + } + } + return ans; + } + + private void dijkstraAlgo(List> graph, int startCity, int[] shortestPath) { + Arrays.fill(shortestPath, Integer.MAX_VALUE); + shortestPath[startCity] = 0; + PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]); + minHeap.offer(new int[] {startCity, 0}); + while (!minHeap.isEmpty()) { + int[] curr = minHeap.poll(); + int currCity = curr[0]; + int currCost = curr[1]; + if (currCost > shortestPath[currCity]) { + continue; + } + for (int[] neighbor : graph.get(currCity)) { + int neighborCity = neighbor[0]; + int neighborCost = neighbor[1]; + if (currCost + neighborCost < shortestPath[neighborCity]) { + shortestPath[neighborCity] = currCost + neighborCost; + minHeap.offer(new int[] {neighborCity, shortestPath[neighborCity]}); + } + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1337.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1337.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_1337.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1337.java index 9dd59f5cfa..e569dc72c5 100644 --- a/src/main/java/com/fishercoder/solutions/_1337.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1337.java @@ -1,6 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; -/** +/* * 1337. Remove Palindromic Subsequences * * Given a string s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subsequence from s. diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1338.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1338.java new file mode 100644 index 0000000000..00ae9befc9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1338.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1338 { + public static class Solution1 { + public int minSetSize(int[] arr) { + Map map = new HashMap<>(); + for (int i : arr) { + map.put(i, map.getOrDefault(i, 0) + 1); + } + List list = new ArrayList<>(); + for (int k : map.keySet()) { + list.add(map.get(k)); + } + Collections.sort(list, Collections.reverseOrder()); + int sum = 0; + int i = 0; + while (sum < arr.length / 2) { + sum += list.get(i++); + } + return i--; + } + } + + public static class Solution2 { + public int minSetSize(int[] arr) { + Map map = new HashMap<>(); + for (int num : arr) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + List list = new ArrayList<>(); + for (int key : map.keySet()) { + list.add(new int[] {map.get(key), key}); + } + Collections.sort(list, (a, b) -> b[0] - a[0]); + int minSet = 0; + int count = 0; + for (int[] pair : list) { + count += pair[0]; + minSet++; + if (count >= arr.length / 2) { + return minSet; + } + } + return minSet; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1339.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1339.java new file mode 100644 index 0000000000..ef18cf1771 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1339.java @@ -0,0 +1,89 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class _1339 { + public static class Solution1 { + public int maxProduct(TreeNode root) { + Set set = new HashSet<>(); + long total = postOrder(root, set); + long result = 0L; + for (long sum : set) { + result = Math.max(result, sum * (total - sum)); + } + return (int) (result % 1000000007); + } + + private long postOrder(TreeNode root, Set set) { + if (root == null) { + return 0; + } + long leftSum = postOrder(root.left, set); + long rightSum = postOrder(root.right, set); + long sum = root.val + leftSum + rightSum; + set.add(sum); + return sum; + } + } + + public static class Solution2 { + /* + * My completely original solution, but much more verbose and uses more extra space. + */ + public int maxProduct(TreeNode root) { + Map sumMap = new HashMap<>(); + long totalSum = postOrderBuildSumMap(root, sumMap); + sumMap.put(root, totalSum); + List productList = new ArrayList<>(); + postOrderBuildProductList(root, sumMap, productList, sumMap.get(root)); + long result = 0L; + double modulo = Math.pow(10, 9) + 7; + for (long[] p : productList) { + long product = p[0] * p[1]; + result = Math.max(result, product); + } + return (int) (result % modulo); + } + + private void postOrderBuildProductList( + TreeNode root, Map sumMap, List productList, Long total) { + if (root == null) { + return; + } + if (root.left == null && root.right == null) { + return; + } + postOrderBuildProductList(root.left, sumMap, productList, total); + postOrderBuildProductList(root.right, sumMap, productList, total); + if (root.left != null) { + // suppose we cut off left subtree now + long leftSum = sumMap.get(root.left); + long remainder = total - leftSum; + productList.add(new long[] {leftSum, remainder}); + } + if (root.right != null) { + // suppose we cut off right subtree now + long rightSum = sumMap.get(root.right); + long remainder = total - rightSum; + productList.add(new long[] {rightSum, remainder}); + } + } + + private long postOrderBuildSumMap(TreeNode root, Map sumMap) { + if (root == null) { + return 0L; + } + long leftSum = postOrderBuildSumMap(root.left, sumMap); + long rightSum = postOrderBuildSumMap(root.right, sumMap); + long sum = leftSum + rightSum + root.val; + sumMap.put(root, sum); + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1341.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1341.java new file mode 100644 index 0000000000..107234d5a5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1341.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _1341 { + public static class Solution1 { + public int[] kWeakestRows(int[][] mat, int k) { + List list = new ArrayList<>(); + for (int i = 0; i < mat.length; i++) { + int soldiers = 0; + for (int j = 0; j < mat[0].length; j++) { + if (mat[i][j] == 1) { + soldiers++; + } else { + break; + } + } + list.add(new int[] {i, soldiers}); + } + Collections.sort(list, (a, b) -> a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]); + int[] result = new int[k]; + int i = 0; + while (i < k) { + result[i] = list.get(i++)[0]; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1342.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1342.java new file mode 100644 index 0000000000..7a92bfa74c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1342.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1342 { + public static class Solution1 { + public int numberOfSteps(int num) { + int steps = 0; + while (num != 0) { + if (num % 2 == 0) { + num /= 2; + } else { + num--; + } + steps++; + } + return steps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1343.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1343.java new file mode 100644 index 0000000000..00bbbbc8bb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1343.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1343 { + public static class Solution1 { + public int numOfSubarrays(int[] arr, int k, int threshold) { + int sum = 0; + for (int i = 0; i < k - 1; i++) { + sum += arr[i]; + } + int count = 0; + for (int i = k - 1; i < arr.length; i++) { + sum += arr[i]; + if (i - k >= 0) { + sum -= arr[i - k]; + } + if (sum / k >= threshold) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1344.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1344.java new file mode 100644 index 0000000000..8585283d20 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1344.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1344 { + public static class Solution1 { + public double angleClock(int hour, int minutes) { + double minAngle = minutes * 360 / 60; + double hourAnglePart1 = hour != 12 ? (hour * 360) / 12 : 0; + double hourAnglePart2 = (double) (30 * minutes) / (double) 60; + double hourAngle = hourAnglePart1 + hourAnglePart2; + double preResult = Math.abs(minAngle - (hourAngle)); + return preResult > 180 ? 360 - preResult : preResult; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1345.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1345.java new file mode 100644 index 0000000000..de5115361a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1345.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +public class _1345 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/jump-game-iv/discuss/502699/JavaC%2B%2B-BFS-Solution-Clean-code-O(N) + */ + public int minJumps(int[] arr) { + Map> valueToIndices = new HashMap<>(); + for (int i = 0; i < arr.length; i++) { + valueToIndices.computeIfAbsent(arr[i], k -> new LinkedList<>()).add(i); + } + boolean[] visited = new boolean[arr.length]; + Queue indexQueue = new LinkedList<>(); + indexQueue.offer(0); + int steps = 0; + while (!indexQueue.isEmpty()) { + int size = indexQueue.size(); + for (int i = 0; i < size; i++) { + int index = indexQueue.poll(); + if (index == arr.length - 1) { + return steps; + } + List nextPossibleIndices = valueToIndices.get(arr[index]); + nextPossibleIndices.add(index - 1); + nextPossibleIndices.add(index + 1); + for (int next : nextPossibleIndices) { + if (next >= 0 && next < arr.length && !visited[next]) { + visited[next] = true; + indexQueue.offer(next); + } + } + nextPossibleIndices + .clear(); // this line is the key to this entire algorithm to avoid TLE, + // explanation: + // https://leetcode.com/problems/jump-game-iv/discuss/502699/JavaC++-BFS-Solution-Clean-code-O(N)/445620 + } + steps++; + } + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1346.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1346.java new file mode 100644 index 0000000000..30ade1cac3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1346.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1346 { + public static class Solution1 { + public boolean checkIfExist(int[] arr) { + for (int i = 0; i < arr.length; i++) { + for (int j = 0; j < arr.length; j++) { + if (i != j && (arr[i] * 2 == arr[j] || arr[i] == arr[j] * 2)) { + return true; + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1347.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1347.java new file mode 100644 index 0000000000..0d052565d5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1347.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1347 { + public static class Solution1 { + public int minSteps(String s, String t) { + int[] counts = new int[26]; + for (char c : s.toCharArray()) { + counts[c - 'a']++; + } + for (char c : t.toCharArray()) { + if (counts[c - 'a'] > 0) { + counts[c - 'a']--; + } + } + return Arrays.stream(counts).sum(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1348.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1348.java new file mode 100644 index 0000000000..e6e3c3d59c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1348.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public class _1348 { + public static class Solution1 { + public static class TweetCounts { + /* + * credit: https://leetcode.com/problems/tweet-counts-per-frequency/discuss/503453/Java-TreeMap-Accepted-Solution-Easy-Understand + */ + private Map> map; + + public TweetCounts() { + map = new HashMap<>(); + } + + public void recordTweet(String tweetName, int time) { + if (!map.containsKey(tweetName)) { + map.put(tweetName, new TreeMap<>()); + } + TreeMap tweetMap = map.get(tweetName); + tweetMap.put(time, tweetMap.getOrDefault(time, 0) + 1); + } + + public List getTweetCountsPerFrequency( + String freq, String tweetName, int startTime, int endTime) { + if (!map.containsKey(tweetName)) { + return null; + } + int interval; + if (freq.equals("minute")) { + interval = 60; + } else if (freq.equals("hour")) { + interval = 60 * 60; + } else { + interval = 60 * 60 * 24; + } + int size = ((endTime - startTime) / interval) + 1; + int[] buckets = new int[size]; + TreeMap tweetMap = map.get(tweetName); + for (Map.Entry entry : + tweetMap.subMap(startTime, endTime + 1).entrySet()) { + int index = (entry.getKey() - startTime) / interval; + buckets[index] += entry.getValue(); + } + List result = new ArrayList<>(); + for (int num : buckets) { + result.add(num); + } + return result; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1349.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1349.java new file mode 100644 index 0000000000..eccbf7d590 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1349.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1349 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/maximum-students-taking-exam/discuss/503686/A-simple-tutorial-on-this-bitmasking-problem + */ + public int maxStudents(char[][] seats) { + int m = seats.length; + int n = seats[0].length; + int[] validRows = new int[m]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + validRows[i] = (validRows[i] << 1) + (seats[i][j] == '.' ? 1 : 0); + } + } + int stateSize = 1 << n; // There are 2^n states for n columns in binary format + int[][] dp = new int[m][stateSize]; + for (int i = 0; i < m; i++) { + Arrays.fill(dp[i], -1); + } + int ans = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < stateSize; j++) { + if (((j & validRows[i]) == j) && ((j & (j << 1)) == 0)) { + if (i == 0) { + dp[i][j] = Integer.bitCount(j); + } else { + for (int k = 0; k < stateSize; k++) { + if (((k << 1) & j) == 0 + && ((j << 1) & k) == 0 + && dp[i - 1][k] != -1) { + dp[i][j] = + Math.max(dp[i][j], dp[i - 1][k] + Integer.bitCount(j)); + } + } + } + ans = Math.max(ans, dp[i][j]); + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1351.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1351.java new file mode 100644 index 0000000000..300fd08b1c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1351.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1351 { + public static class Solution1 { + public int countNegatives(int[][] grid) { + int count = 0; + for (int[] row : grid) { + for (int v : row) { + if (v < 0) { + count++; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1352.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1352.java new file mode 100644 index 0000000000..00ebc10514 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1352.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1352 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/product-of-the-last-k-numbers/discuss/510260/JavaC%2B%2BPython-Prefix-Product + */ + public static class ProductOfNumbers { + + List list; + + public ProductOfNumbers() { + add(0); + } + + public void add(int num) { + if (num > 0) { + list.add(list.get(list.size() - 1) * num); + } else { + list = new ArrayList<>(); + list.add(1); + } + } + + public int getProduct(int k) { + int size = list.size(); + return k >= size ? 0 : (list.get(size - 1) / list.get(size - k - 1)); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1353.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1353.java new file mode 100644 index 0000000000..fd1a64cb16 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1353.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.PriorityQueue; + +public class _1353 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/510263/JavaC%2B%2BPython-Priority-Queue + *

+ * 1. Sort events by start time, if ties, by end time; + * 2. From day 1 to day 100,000, we add all events that start on this day into a priorityqueue, + * also, we remove the events that closed on this day from the priorityqueue; + * 3. attend the event that ends on this day (earliest, i.e. greedy) and pop it out of the priorityqueue + */ + public int maxEvents(int[][] events) { + Arrays.sort(events, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); + PriorityQueue heap = new PriorityQueue<>(); + int maxEvents = 0; + int i = 0; + for (int day = 1; day <= 100000; day++) { + while (i < events.length && events[i][0] == day) { + heap.offer(events[i++][1]); + } + while (heap.size() > 0 && heap.peek() < day) { + heap.poll(); + } + if (heap.size() > 0) { + heap.poll(); + maxEvents++; + } + } + return maxEvents; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1354.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1354.java new file mode 100644 index 0000000000..f21a8e6596 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1354.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1354 { + public static class Solution1 { + /* + * Use % is the key here to avoid TLE, a good test case for this is [1,1000000000] + */ + public boolean isPossible(int[] target) { + long sum = 0; + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); + for (int i = 0; i < target.length; i++) { + maxHeap.offer(target[i]); + sum += target[i]; + } + while (maxHeap.peek() != 1) { + int max = maxHeap.poll(); + sum -= max; + if (max <= sum || sum < 1) { + return false; + } + max %= sum; + sum += max; + maxHeap.offer(max); + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1356.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1356.java new file mode 100644 index 0000000000..1f7d81ec7c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1356.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1356 { + public static class Solution1 { + public int[] sortByBits(int[] arr) { + Map> map = new HashMap<>(); + for (int num : arr) { + int count = Integer.bitCount(num); + if (!map.containsKey(count)) { + map.put(count, new ArrayList<>()); + } + map.get(count).add(num); + } + int[] result = new int[arr.length]; + int i = 0; + for (int count : map.keySet()) { + List list = map.get(count); + Collections.sort(list); + for (int num : list) { + result[i++] = num; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1357.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1357.java new file mode 100644 index 0000000000..6e12ae9c86 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1357.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1357 { + public static class Solution1 { + public static class Cashier { + + int customerCount; + int n; + Map productsToPrices; + int discount; + + public Cashier(int n, int discount, int[] products, int[] prices) { + this.n = n; + this.customerCount = 0; + this.discount = discount; + this.productsToPrices = new HashMap<>(); + for (int i = 0; i < products.length; i++) { + productsToPrices.put(products[i], prices[i]); + } + } + + public double getBill(int[] product, int[] amount) { + double totalPrice = 0; + for (int i = 0; i < product.length; i++) { + totalPrice += productsToPrices.get(product[i]) * amount[i]; + } + if (customerCount + 1 == n) { + // apply discount + totalPrice *= (double) (100 - discount) / 100; + customerCount = 0; + } else { + customerCount++; + } + return totalPrice; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1358.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1358.java new file mode 100644 index 0000000000..253be3a57c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1358.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1358 { + public static class Solution1 { + /* + * A classic sliding window problem, no dp or backtracking, just sliding window: use two pointers. + * my new favorite question! + */ + public int numberOfSubstrings(String s) { + int[] counts = new int[3]; + int i = 0; + int n = s.length(); + int result = 0; + for (int j = 0; j < n; j++) { + counts[s.charAt(j) - 'a']++; + while (counts[0] > 0 && counts[1] > 0 && counts[2] > 0) { + counts[s.charAt(i++) - 'a']--; + } + result += i; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1360.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1360.java new file mode 100644 index 0000000000..47c36457a0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1360.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1360 { + public static class Solution1 { + public int daysBetweenDates(String date1, String date2) { + String[] strings1 = date1.split("-"); + String[] strings2 = date2.split("-"); + return Math.abs( + julianDay( + Integer.parseInt(strings1[0]), + Integer.parseInt(strings1[1]), + Integer.parseInt(strings1[2])) + - julianDay( + Integer.parseInt(strings2[0]), + Integer.parseInt(strings2[1]), + Integer.parseInt(strings2[2]))); + } + + public int julianDay(int year, int month, int day) { + int a = (14 - month) / 12; + int y = year + 4800 - a; + int m = month + 12 * a - 3; + int jdn = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045; + return jdn; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1361.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1361.java new file mode 100644 index 0000000000..75633f3c8c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1361.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1361 { + public static class Solution1 { + public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) { + int[] indegree = new int[n]; + for (int i = 0; i < n; i++) { + if (leftChild[i] >= 0) { + indegree[leftChild[i]]++; + } + if (rightChild[i] >= 0) { + indegree[rightChild[i]]++; + } + } + // only one node has in-degree = 0 + int indegreeZero = 0; + for (int num : indegree) { + if (num == 0) { + indegreeZero++; + if (indegreeZero > 1) { + return false; + } + } + // every other node's in-degree must be exactly equal to 1 + if (num > 1) { + return false; + } + } + // no bi-directional pointing + Map> map = new HashMap<>(); + for (int i = 0; i < n; i++) { + map.put(i, new ArrayList<>()); + map.get(i).add(leftChild[i]); + map.get(i).add(rightChild[i]); + } + for (int node : map.keySet()) { + List children = map.get(node); + for (int child : children) { + if (map.containsKey(child) && map.get(child).contains(node)) { + return false; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1362.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1362.java new file mode 100644 index 0000000000..cf695269cf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1362.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1362 { + public static class Solution1 { + public int[] closestDivisors(int num) { + int sqrt = (int) Math.sqrt(num); + int left = sqrt + 1; + int right = sqrt + 1; + long product = left * right; + while (product != (long) (num + 1) && product != (long) (num + 2)) { + if (product < (num + 1)) { + left++; + } else if (product > (num + 2)) { + right--; + } + product = left * right; + } + return new int[] {left, right}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1365.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1365.java new file mode 100644 index 0000000000..bf2d68b54c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1365.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _1365 { + public static class Solution1 { + public int[] smallerNumbersThanCurrent(int[] nums) { + int[] result = new int[nums.length]; + int[] tmp = Arrays.copyOf(nums, nums.length); + Arrays.sort(tmp); + List list = new ArrayList<>(); + for (int i : tmp) { + list.add(i); + } + for (int i = 0; i < nums.length; i++) { + result[i] = list.indexOf(nums[i]); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1366.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1366.java new file mode 100644 index 0000000000..48e0ff7acc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1366.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.Comparator; + +public class _1366 { + public static class Solution1 { + class Node { + int[] count = new int[26]; + char c; + + public Node(char c) { + this.c = c; + } + } + + public String rankTeams(String[] votes) { + Node[] nodes = new Node[26]; + for (int i = 0; i < 26; i++) { + nodes[i] = new Node((char) (i + 'A')); + } + for (String vote : votes) { + for (int i = 0; i < vote.length(); i++) { + nodes[vote.charAt(i) - 'A'].count[i]++; + } + } + Arrays.sort( + nodes, + new Comparator() { + @Override + public int compare(Node o1, Node o2) { + for (int i = 0; i < 26; i++) { + if (o1.count[i] != o2.count[i]) { + return o2.count[i] - o1.count[i]; + } + } + return o1.c - o2.c; + } + }); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < votes[0].length(); i++) { + sb.append(nodes[i].c); + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1367.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1367.java new file mode 100644 index 0000000000..048f5aed94 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1367.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _1367 { + public static class Solution1 { + List> paths = new ArrayList<>(); + + public boolean isSubPath(ListNode head, TreeNode root) { + List list = getList(head); + findAllPaths(root, new ArrayList<>()); + for (List path : paths) { + if (path.size() >= list.size()) { + if (find(list, path)) { + return true; + } + } + } + return false; + } + + private boolean find(List list, List path) { + int i = 0; + int j = 0; + for (; i <= path.size() - list.size(); i++) { + j = 0; + int tmpI = i; + while (j < list.size() && tmpI < path.size() && list.get(j) == path.get(tmpI)) { + tmpI++; + j++; + } + if (j >= list.size()) { + return true; + } + } + return j >= list.size(); + } + + private void findAllPaths(TreeNode root, List path) { + if (root == null) { + return; + } + path.add(root.val); + if (root.left == null && root.right == null) { + paths.add(new ArrayList<>(path)); + path.remove(path.size() - 1); + return; + } + findAllPaths(root.left, path); + findAllPaths(root.right, path); + path.remove(path.size() - 1); + } + + private List getList(ListNode head) { + List list = new ArrayList<>(); + while (head != null) { + list.add(head.val); + head = head.next; + } + return list; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1370.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1370.java new file mode 100644 index 0000000000..e9182bc086 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1370.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1370 { + public static class Solution1 { + public String sortString(String s) { + int[] count = new int[26]; + for (char c : s.toCharArray()) { + count[c - 'a']++; + } + StringBuilder sb = new StringBuilder(); + while (sb.length() < s.length()) { + for (int i = 0; i < count.length; i++) { + if (count[i] != 0) { + char character = (char) (i + 'a'); + sb.append(character); + count[i]--; + } + } + for (int i = 25; i >= 0; i--) { + if (count[i] > 0) { + char character = (char) (i + 'a'); + sb.append(character); + count[i]--; + } + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1371.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1371.java new file mode 100644 index 0000000000..143f187d1a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1371.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1371 { + public static class Solution1 { + public int findTheLongestSubstring(String s) { + int max = 0; + for (int i = 0; i < s.length(); i++) { + Map map = setupMap(); + if (s.length() - i < max) { + return max; + } + for (int j = i; j < s.length(); j++) { + Character b = s.charAt(j); + if (map.containsKey(b)) { + map.put(b, map.get(b) + 1); + } + if (allEven(map)) { + max = Math.max(max, j - i + 1); + } + } + } + return max; + } + + private Map setupMap() { + Map map = new HashMap<>(); + map.put('a', 0); + map.put('e', 0); + map.put('i', 0); + map.put('o', 0); + map.put('u', 0); + return map; + } + + private boolean allEven(Map map) { + for (char c : map.keySet()) { + if (map.get(c) % 2 != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1372.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1372.java new file mode 100644 index 0000000000..45c67680d5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1372.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1372 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/discuss/531808/Java-Recursion-Try-each-node-as-a-zigzag-root-then-return-valid-sum-to-parent + */ + int maxLength = 0; + + public int longestZigZag(TreeNode root) { + dfs(root, true); + return maxLength; + } + + private int dfs(TreeNode root, boolean isLeft) { + if (root == null) { + return 0; + } + int left = dfs(root.left, false); + int right = dfs(root.right, true); + maxLength = Math.max(maxLength, left); + maxLength = Math.max(maxLength, right); + return 1 + (isLeft ? left : right); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1373.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1373.java new file mode 100644 index 0000000000..d5dd1bb926 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1373.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1373 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/discuss/532021/Java-Post-Order + */ + public int maxSumBST(TreeNode root) { + return postOrder(root)[4]; + } + + /* + * result[0] means this tree is a BST + * result[1] means the sum of this tree + * result[2] means the left boundary + * result[3] means the right boundary + * result[4] means the global max sum + */ + private int[] postOrder(TreeNode root) { + if (root == null) { + return new int[] {1, 0, Integer.MAX_VALUE, Integer.MIN_VALUE, 0}; + } + int[] leftSide = postOrder(root.left); + int[] rightSide = postOrder(root.right); + int localMax = Math.max(leftSide[4], rightSide[4]); + if (leftSide[0] == 1 + && rightSide[0] == 1 + && root.val > leftSide[3] + && root.val < rightSide[2]) { + int sum = root.val + leftSide[1] + rightSide[1]; + return new int[] { + 1, + sum, + leftSide[2] == Integer.MAX_VALUE ? root.val : leftSide[2], + rightSide[3] == Integer.MIN_VALUE ? root.val : rightSide[3], + Math.max(localMax, sum) + }; + } else { + return new int[] {0, 0, 0, 0, localMax}; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1374.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1374.java new file mode 100644 index 0000000000..55a352c1de --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1374.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1374 { + public static class Solution1 { + public String generateTheString(int n) { + StringBuilder sb = new StringBuilder(); + if (n > 1 && n % 2 == 0) { + while (n-- > 1) { + sb.append("a"); + } + } else if (n > 1) { + while (n-- > 2) { + sb.append("a"); + } + sb.append("b"); + } + sb.append("z"); + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1375.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1375.java new file mode 100644 index 0000000000..fdc96f4670 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1375.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1375 { + public static class Solution1 { + public int numTimesAllBlue(int[] light) { + int blues = 0; + int[] status = new int[light.length]; // 0 means off, 1 means on + for (int i = 0; i < light.length; i++) { + status[light[i] - 1] = 1; + if (checkAllBlues(status, i)) { + blues++; + } + } + return blues; + } + + private boolean checkAllBlues(int[] status, int endIndex) { + for (int i = 0; i <= endIndex; i++) { + if (status[i] != 1) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1376.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1376.java new file mode 100644 index 0000000000..1289b0f07e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1376.java @@ -0,0 +1,72 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class _1376 { + public static class Solution1 { + int maxMinutes = 0; + + public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) { + boolean[] visited = new boolean[manager.length]; + Set managerIdSet = new HashSet<>(); + for (int id : manager) { + managerIdSet.add(id); + } + Set visitedEmployees = new HashSet<>(); + for (int i = 0; i < n; i++) { + visitedEmployees.add(i); + } + + Map> map = new HashMap<>(); + for (int i = 0; i < manager.length; i++) { + if (!map.containsKey(manager[i])) { + map.put(manager[i], new ArrayList<>()); + } + map.get(manager[i]).add(i); + } + backtracking(visited, headID, 0, informTime, managerIdSet, visitedEmployees, map); + return maxMinutes; + } + + private void backtracking( + boolean[] visited, + int managerId, + int currentMinutes, + int[] informTime, + Set managerIdSet, + Set visitedEmployees, + Map> map) { + if (visitedEmployees.contains(managerId)) { + visitedEmployees.remove(managerId); + } + if (!managerIdSet.contains(managerId)) { + maxMinutes = Math.max(currentMinutes, maxMinutes); + return; + } + if (visitedEmployees.isEmpty()) { + return; + } + visited[managerId] = true; + if (map.containsKey(managerId)) { + List suboridnates = map.get(managerId); + for (int subordinate : suboridnates) { + if (!visited[subordinate]) { + backtracking( + visited, + subordinate, + currentMinutes + informTime[managerId], + informTime, + managerIdSet, + visitedEmployees, + map); + } + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1377.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1377.java new file mode 100644 index 0000000000..a732f33fc4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1377.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _1377 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/frog-position-after-t-seconds/discuss/532505/Java-Straightforward-BFS-Clean-code-O(N) + */ + public double frogPosition(int n, int[][] edges, int t, int target) { + List[] graph = new ArrayList[n]; + for (int i = 0; i < n; i++) { + graph[i] = new ArrayList<>(); + } + for (int[] edge : edges) { + graph[edge[0] - 1].add(edge[1] - 1); + graph[edge[1] - 1].add(edge[0] - 1); + } + boolean[] visited = new boolean[n]; + visited[0] = true; + double[] probabilities = new double[n]; + probabilities[0] = 1f; + Queue queue = new LinkedList<>(); + queue.offer(0); + while (!queue.isEmpty() && t-- > 0) { + for (int i = queue.size(); i > 0; i--) { + int vertex = queue.poll(); + int nextVerticesCount = 0; + for (int next : graph[vertex]) { + if (!visited[next]) { + nextVerticesCount++; + } + } + for (int next : graph[vertex]) { + if (!visited[next]) { + visited[next] = true; + queue.offer(next); + probabilities[next] = probabilities[vertex] / nextVerticesCount; + } + } + if (nextVerticesCount > 0) { + probabilities[vertex] = 0; + } + } + } + return probabilities[target - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1379.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1379.java new file mode 100644 index 0000000000..9f64b37f9d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1379.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1379 { + public static class Solution1 { + public final TreeNode getTargetCopy( + final TreeNode original, final TreeNode cloned, final TreeNode target) { + if (original == null) { + return null; + } + if (original.val == target.val) { + return cloned; + } + TreeNode left = getTargetCopy(original.left, cloned.left, target); + if (left != null && left.val == target.val) { + return left; + } + return getTargetCopy(original.right, cloned.right, target); + } + } + + public static class Solution2 { + /* + * My completely original solution on 5/17/2022. + */ + public final TreeNode getTargetCopy( + final TreeNode original, final TreeNode cloned, final TreeNode target) { + if (original == null || cloned == null) { + return null; + } + if (original == target) { + return cloned; + } + TreeNode left = getTargetCopy(original.left, cloned.left, target); + if (left == null) { + return getTargetCopy(original.right, cloned.right, target); + } + return left; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1380.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1380.java new file mode 100644 index 0000000000..f09d464397 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1380.java @@ -0,0 +1,72 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1380 { + public static class Solution1 { + public List luckyNumbers(int[][] matrix) { + List result = new ArrayList<>(); + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (luckyInRow(matrix[i][j], matrix[i])) { + if (luckyInColumn(matrix[i][j], matrix, j)) { + result.add(matrix[i][j]); + } + } + } + } + return result; + } + + private boolean luckyInColumn(int number, int[][] matrix, int columnIndex) { + for (int i = 0; i < matrix.length; i++) { + if (number < matrix[i][columnIndex]) { + return false; + } + } + return true; + } + + private boolean luckyInRow(int number, int[] row) { + for (int num : row) { + if (number > num) { + return false; + } + } + return true; + } + } + + public static class Solution2 { + public List luckyNumbers(int[][] matrix) { + List rowMins = new ArrayList<>(); + for (int i = 0; i < matrix.length; i++) { + int j = 0; + int rowMin = matrix[i][j++]; + for (; j < matrix[0].length; j++) { + rowMin = Math.min(rowMin, matrix[i][j]); + } + rowMins.add(rowMin); + } + List colMaxs = new ArrayList<>(); + for (int j = 0; j < matrix[0].length; j++) { + int i = 0; + int colMax = matrix[i++][j]; + for (; i < matrix.length; i++) { + colMax = Math.max(colMax, matrix[i][j]); + } + colMaxs.add(colMax); + } + List result = new ArrayList<>(); + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (matrix[i][j] == rowMins.get(i) && matrix[i][j] == colMaxs.get(j)) { + result.add(matrix[i][j]); + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1381.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1381.java new file mode 100644 index 0000000000..14cf302a89 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1381.java @@ -0,0 +1,87 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1381 { + public static class Solution1 { + public static class CustomStack { + + List list; + int maxSize; + + public CustomStack(int maxSize) { + this.list = new ArrayList<>(); + this.maxSize = maxSize; + } + + public void push(int x) { + if (list.size() >= maxSize) { + return; + } else { + list.add(x); + } + } + + public int pop() { + if (!list.isEmpty()) { + return list.remove(list.size() - 1); + } else { + return -1; + } + } + + public void increment(int k, int val) { + for (int i = 0; i < k && i < list.size(); i++) { + list.set(i, list.get(i) + val); + } + } + } + } + + /* + * Implementation of Stack using Array + */ + public static class Solution2 { + public static class CustomStack { + private int top; + private int maxSize; + private int[] stack; + + public CustomStack(int maxSize) { + this.maxSize = maxSize; + this.stack = new int[maxSize]; + } + + public void push(int x) { + if (top == maxSize) { + return; + } + stack[top] = x; + top++; + } + + public int pop() { + if (top == 0) { + return -1; + } + int popValue = stack[top - 1]; + stack[top - 1] = 0; + top--; + return popValue; + } + + public void increment(int k, int val) { + if (top == 0 || k == 0) { + return; + } + for (int i = 0; i < k; i++) { + if (i == top) { + break; + } + stack[i] += val; + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1382.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1382.java new file mode 100644 index 0000000000..3722ca5c88 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1382.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _1382 { + public static class Solution1 { + public TreeNode balanceBST(TreeNode root) { + List inorder = inorder(root, new ArrayList<>()); + return dfs(inorder, 0, inorder.size() - 1); + } + + private List inorder(TreeNode root, List list) { + if (root == null) { + return list; + } + inorder(root.left, list); + list.add(root.val); + return inorder(root.right, list); + } + + private TreeNode dfs(List nums, int start, int end) { + if (end < start) { + return null; + } + int mid = (start + end) / 2; + TreeNode root = new TreeNode(nums.get(mid)); + root.left = dfs(nums, start, mid - 1); + root.right = dfs(nums, mid + 1, end); + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1385.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1385.java new file mode 100644 index 0000000000..253fcf5839 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1385.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1385 { + public static class Solution1 { + public int findTheDistanceValue(int[] arr1, int[] arr2, int d) { + int count = 0; + for (int i = 0; i < arr1.length; i++) { + int j = 0; + for (; j < arr2.length; j++) { + if (Math.abs(arr1[i] - arr2[j]) <= d) { + break; + } + } + if (j == arr2.length) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1386.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1386.java new file mode 100644 index 0000000000..6146b3f4d4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1386.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _1386 { + public static class Solution1 { + public int maxNumberOfFamilies(int n, int[][] reservedSeats) { + Map> map = new HashMap<>(); + for (int[] seat : reservedSeats) { + if (!map.containsKey(seat[0])) { + map.put(seat[0], new HashSet<>()); + } + map.get(seat[0]).add(seat[1]); + } + int count = (n - map.size()) * 2; + for (int key : map.keySet()) { + Set reservedOnes = map.get(key); + if (reservedOnes.size() > 6) { + continue; + } + if (!reservedOnes.contains(2) + && !reservedOnes.contains(3) + && !reservedOnes.contains(4) + && !reservedOnes.contains(5) + && !reservedOnes.contains(6) + && !reservedOnes.contains(7) + && !reservedOnes.contains(8) + && !reservedOnes.contains(9)) { + count += 2; + } else if (!reservedOnes.contains(4) + && !reservedOnes.contains(5) + && !reservedOnes.contains(6) + && !reservedOnes.contains(7)) { + count++; + } else if (!reservedOnes.contains(2) + && !reservedOnes.contains(3) + && !reservedOnes.contains(4) + && !reservedOnes.contains(5)) { + count++; + } else if (!reservedOnes.contains(6) + && !reservedOnes.contains(7) + && !reservedOnes.contains(8) + && !reservedOnes.contains(9)) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1387.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1387.java new file mode 100644 index 0000000000..9002d0e581 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1387.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _1387 { + public static class Solution1 { + public int getKth(int lo, int hi, int k) { + List power = new ArrayList<>(); + for (int i = lo; i <= hi; i++) { + power.add(new int[] {getSteps(i), i}); + } + Collections.sort(power, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); + return power.get(k - 1)[1]; + } + + private int getSteps(int number) { + int steps = 0; + while (number != 1) { + if (number % 2 == 0) { + number /= 2; + } else { + number = 3 * number + 1; + } + steps++; + } + return steps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1388.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1388.java new file mode 100644 index 0000000000..62c849786e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1388.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1388 { + public static class Solution1 { + public int maxSizeSlices(int[] slices) { + int n = slices.length; + int[] b = Arrays.copyOf(slices, 2 * n); + for (int i = 0; i < n; i++) { + b[i + n] = slices[i]; + } + int[][] dp = new int[2 * n][2 * n]; + for (int len = 3; len <= n; len += 3) { + for (int i = 0; i + len - 1 < 2 * n; i++) { + int j = i + len - 1; + for (int k = i + 3; k <= j - 2; k += 3) { + dp[i][j] = Math.max(dp[i][j], dp[i][k - 1] + dp[k][j]); + } + for (int k = i + 1; k < j; k += 3) { + dp[i][j] = + Math.max( + dp[i][j], + (i + 1 <= k - 1 ? dp[i + 1][k - 1] : 0) + + b[k] + + (k + 1 <= j - 1 ? dp[k + 1][j - 1] : 0)); + } + } + } + int ans = 0; + for (int i = 0; i < n; i++) { + ans = Math.max(ans, dp[i][i + n - 1]); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1389.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1389.java new file mode 100644 index 0000000000..c1e86f1c93 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1389.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1389 { + public static class Solution1 { + public int[] createTargetArray(int[] nums, int[] index) { + List list = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + list.add(index[i], nums[i]); + } + int[] target = new int[list.size()]; + for (int i = 0; i < target.length; i++) { + target[i] = list.get(i); + } + return target; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1390.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1390.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_1390.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1390.java index 487c1fe4ee..1f2f38f8e0 100644 --- a/src/main/java/com/fishercoder/solutions/_1390.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1390.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.List; -/** +/* * 1390. Four Divisors * * Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. diff --git a/src/main/java/com/fishercoder/solutions/_1392.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1392.java similarity index 82% rename from src/main/java/com/fishercoder/solutions/_1392.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1392.java index b701d4d03a..b6ef654c46 100644 --- a/src/main/java/com/fishercoder/solutions/_1392.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1392.java @@ -1,6 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; -/** +/* * 1392. Longest Happy Prefix * * A string is called a happy prefix if is a non-empty prefix which is also a suffix (excluding itself). @@ -31,14 +31,16 @@ * */ public class _1392 { public static class Solution1 { - /**credit: https://leetcode.com/problems/longest-happy-prefix/discuss/547446/C%2B%2BJava-Incremental-Hash-and-DP*/ + /*credit: https://leetcode.com/problems/longest-happy-prefix/discuss/547446/C%2B%2BJava-Incremental-Hash-and-DP*/ public String longestPrefix(String s) { int times = 2; long prefixHash = 0; long suffixHash = 0; long multiplier = 1; long len = 0; - long mod = 1000000007;//use some large prime as a modulo to avoid overflow errors, e.g. 10 ^ 9 + 7. + long mod = + 1000000007; // use some large prime as a modulo to avoid overflow errors, e.g. + // 10 ^ 9 + 7. for (int i = 0; i < s.length() - 1; i++) { prefixHash = (prefixHash * times + s.charAt(i)) % mod; suffixHash = (multiplier * s.charAt(s.length() - i - 1) + suffixHash) % mod; diff --git a/src/main/java/com/fishercoder/solutions/_1394.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1394.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_1394.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1394.java index db0bd2d63f..0dabdeba41 100644 --- a/src/main/java/com/fishercoder/solutions/_1394.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1394.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.HashMap; import java.util.Map; -/** +/* * 1394. Find Lucky Integer in an Array * * Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value. diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1395.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1395.java new file mode 100644 index 0000000000..a3dd808b8f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1395.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.secondthousand; + +/* + * 1395. Count Number of Teams + *

+ * There are n soldiers standing in a line. Each soldier is assigned a unique rating value. + * You have to form a team of 3 soldiers amongst them under the following rules: + * Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]). + * A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n). + * Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams). + *

+ * Example 1: + * Input: rating = [2,5,3,4,1] + * Output: 3 + * Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1). + *

+ * Example 2: + * Input: rating = [2,1,3] + * Output: 0 + * Explanation: We can't form any team given the conditions. + *

+ * Example 3: + * Input: rating = [1,2,3,4] + * Output: 4 + *

+ * Constraints: + * n == rating.length + * 1 <= n <= 200 + * 1 <= rating[i] <= 10^5 + */ +public class _1395 { + public static class Solution1 { + public int numTeams(int[] rating) { + int teams = 0; + for (int i = 0; i < rating.length - 2; i++) { + for (int j = i + 1; j < rating.length - 1; j++) { + if (rating[i] < rating[j]) { + for (int k = j + 1; k < rating.length; k++) { + if (rating[j] < rating[k]) { + teams++; + } + } + } else if (rating[i] > rating[j]) { + for (int k = j + 1; k < rating.length; k++) { + if (rating[j] > rating[k]) { + teams++; + } + } + } + } + } + return teams; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1396.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1396.java new file mode 100644 index 0000000000..fa01094524 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1396.java @@ -0,0 +1,64 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +public class _1396 { + public static class Solution1 { + public class UndergroundSystem { + + class StationAndTime { + String stationName; + int t; + + public StationAndTime(String stationName, int t) { + this.stationName = stationName; + this.t = t; + } + + public String getStation() { + return this.stationName; + } + + public int getTime() { + return this.t; + } + } + + Map averageTimeMap; + Map> travelerMap = new HashMap<>(); + + public UndergroundSystem() { + averageTimeMap = new HashMap<>(); + travelerMap = new HashMap<>(); + } + + public void checkIn(int id, String stationName, int t) { + if (!travelerMap.containsKey(id)) { + travelerMap.put(id, new LinkedList<>()); + } + travelerMap.get(id).add(new StationAndTime(stationName, t)); + } + + public void checkOut(int id, String stationName, int t) { + LinkedList list = travelerMap.get(id); + StationAndTime stationAndTime = list.getLast(); + String startToEndStation = stationAndTime.getStation() + "->" + stationName; + int duration = t - stationAndTime.getTime(); + if (!averageTimeMap.containsKey(startToEndStation)) { + averageTimeMap.put(startToEndStation, new double[] {duration, 1}); + } else { + double[] pair = averageTimeMap.get(startToEndStation); + double newAverage = + (double) (pair[0] * pair[1] + duration) / (double) (pair[1] + 1); + averageTimeMap.put(startToEndStation, new double[] {newAverage, pair[1] + 1}); + } + } + + public double getAverageTime(String startStation, String endStation) { + return averageTimeMap.get(startStation + "->" + endStation)[0]; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1399.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1399.java similarity index 97% rename from src/main/java/com/fishercoder/solutions/_1399.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1399.java index e8159263fd..8fd89e9ef4 100644 --- a/src/main/java/com/fishercoder/solutions/_1399.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1399.java @@ -1,11 +1,11 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -/** +/* * 1399. Count Largest Group * * Given an integer n. Each number from 1 to n is grouped according to the sum of its digits. diff --git a/src/main/java/com/fishercoder/solutions/_1400.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1400.java similarity index 97% rename from src/main/java/com/fishercoder/solutions/_1400.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1400.java index da0d17d230..f49730b90d 100644 --- a/src/main/java/com/fishercoder/solutions/_1400.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1400.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.HashMap; import java.util.Map; -/** +/* * 1400. Construct K Palindrome Strings * * Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s. diff --git a/src/main/java/com/fishercoder/solutions/_1401.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1401.java similarity index 92% rename from src/main/java/com/fishercoder/solutions/_1401.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1401.java index 7d9d6a5193..8b7c9d9859 100644 --- a/src/main/java/com/fishercoder/solutions/_1401.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1401.java @@ -1,6 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; -/** +/* * 1401. Circle and Rectangle Overlapping * * Given a circle represented as (radius, x_center, y_center) and an axis-aligned rectangle represented as (x1, y1, x2, y2), where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the rectangle. @@ -32,7 +32,8 @@ * */ public class _1401 { public static class Solution1 { - public boolean checkOverlap(int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) { + public boolean checkOverlap( + int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) { if (x1 <= xCenter && x2 >= xCenter && y1 <= yCenter && y2 >= yCenter) { return true; } diff --git a/src/main/java/com/fishercoder/solutions/_1403.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1403.java similarity index 86% rename from src/main/java/com/fishercoder/solutions/_1403.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1403.java index 21ae82e047..b6153ad281 100644 --- a/src/main/java/com/fishercoder/solutions/_1403.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1403.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.Arrays; @@ -6,7 +6,7 @@ import java.util.List; import java.util.stream.Collectors; -/** +/* * 1403. Minimum Subsequence in Non-Increasing Order * * Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence. @@ -35,10 +35,11 @@ public class _1403 { public static class Solution1 { public List minSubsequence(int[] nums) { - List list = Arrays.stream(nums) - .boxed() - .sorted(Collections.reverseOrder()) - .collect(Collectors.toCollection(() -> new ArrayList<>(nums.length))); + List list = + Arrays.stream(nums) + .boxed() + .sorted(Collections.reverseOrder()) + .collect(Collectors.toCollection(() -> new ArrayList<>(nums.length))); int sum = list.stream().mapToInt(num -> num).sum(); int minSum = 0; List result = new ArrayList<>(); diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1405.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1405.java new file mode 100644 index 0000000000..b160a3a004 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1405.java @@ -0,0 +1,71 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1405 { + public static class Solution1 { + public String longestDiverseString(int a, int b, int c) { + PriorityQueue maxHeap = new PriorityQueue<>((x, y) -> y.count - x.count); + if (a > 0) { + maxHeap.offer(new Tuple('a', a)); + } + if (b > 0) { + maxHeap.offer(new Tuple('b', b)); + } + if (c > 0) { + maxHeap.offer(new Tuple('c', c)); + } + StringBuilder sb = new StringBuilder(); + while (maxHeap.size() > 1) { + Tuple one = maxHeap.poll(); + if (one.count >= 2) { + sb.append(one.c); + sb.append(one.c); + one.count -= 2; + } else { + sb.append(one.c); + one.count--; + } + + Tuple two = maxHeap.poll(); + if (two.count >= 2 && one.count < two.count) { + sb.append(two.c); + sb.append(two.c); + two.count -= 2; + } else { + sb.append(two.c); + two.count--; + } + + // only after the above two poll() calls, then do below: + if (one.count > 0) { + maxHeap.offer(one); + } + if (two.count > 0) { + maxHeap.offer(two); + } + } + if (!maxHeap.isEmpty()) { + if (sb.length() == 0 || sb.charAt(sb.length() - 1) != maxHeap.peek().c) { + if (maxHeap.peek().count >= 2) { + sb.append(maxHeap.peek().c); + sb.append(maxHeap.peek().c); + } else { + sb.append(maxHeap.peek().c); + } + } + } + return sb.toString(); + } + + class Tuple { + char c; + int count; + + public Tuple(char c, int count) { + this.c = c; + this.count = count; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1408.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1408.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_1408.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1408.java index 423c0130b8..5fabff802d 100644 --- a/src/main/java/com/fishercoder/solutions/_1408.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1408.java @@ -1,11 +1,11 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; -/** +/* * 1408. String Matching in an Array * * Given an array of string words. Return all strings in words which is substring of another word in any order. diff --git a/src/main/java/com/fishercoder/solutions/_1409.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1409.java similarity index 97% rename from src/main/java/com/fishercoder/solutions/_1409.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1409.java index ea2bb7ffd0..22f63725a1 100644 --- a/src/main/java/com/fishercoder/solutions/_1409.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1409.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.HashMap; import java.util.Map; -/** +/* * 1409. Queries on a Permutation With Key * * Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: diff --git a/src/main/java/com/fishercoder/solutions/_1410.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1410.java similarity index 90% rename from src/main/java/com/fishercoder/solutions/_1410.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1410.java index cbbf4266a7..3af2863f3e 100644 --- a/src/main/java/com/fishercoder/solutions/_1410.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1410.java @@ -1,6 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; -/** +/* * 1410. HTML Entity Parser * * HTML entity parser is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself. @@ -51,10 +51,12 @@ public String entityParser(String text) { if (i + 7 <= text.length() && text.substring(i, i + 7).equals("⁄")) { sb.append("/"); i += 6; - } else if (i + 6 <= text.length() && text.substring(i, i + 6).equals(""")) { + } else if (i + 6 <= text.length() + && text.substring(i, i + 6).equals(""")) { sb.append("\""); i += 5; - } else if (i + 6 <= text.length() && text.substring(i, i + 6).equals("'")) { + } else if (i + 6 <= text.length() + && text.substring(i, i + 6).equals("'")) { sb.append("'"); i += 5; } else if (i + 5 <= text.length() && text.substring(i, i + 5).equals("&")) { diff --git a/src/main/java/com/fishercoder/solutions/_1413.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1413.java similarity index 96% rename from src/main/java/com/fishercoder/solutions/_1413.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1413.java index 2b8cb1da2b..a18616f492 100644 --- a/src/main/java/com/fishercoder/solutions/_1413.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1413.java @@ -1,6 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; -/** +/* * 1413. Minimum Value to Get Positive Step by Step Sum * * Given an array of integers nums, you start with an initial positive value startValue. diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1414.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1414.java new file mode 100644 index 0000000000..8c0f5dd3a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1414.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1414 { + public static class Solution1 { + /* + * My completely original solution, heap is not really necessary, a regular array/list works out as well. + */ + public int findMinFibonacciNumbers(int k) { + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); + int one = 1; + int two = 1; + maxHeap.offer(one); + maxHeap.offer(two); + int three = one + two; + while (three <= k) { + maxHeap.offer(three); + int tmp = two; + two = three; + one = tmp; + three = one + two; + } + int minNumbers = 0; + while (k > 0) { + if (maxHeap.contains(k)) { + minNumbers++; + return minNumbers; + } else { + while (maxHeap.peek() > k) { + maxHeap.poll(); + } + Integer max = maxHeap.poll(); + k -= max; + minNumbers++; + } + } + return minNumbers; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1415.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1415.java similarity index 95% rename from src/main/java/com/fishercoder/solutions/_1415.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1415.java index 7fc71a9379..6925060733 100644 --- a/src/main/java/com/fishercoder/solutions/_1415.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1415.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.List; -/** +/* * 1415. The k-th Lexicographical String of All Happy Strings of Length n * * A happy string is a string that: @@ -44,7 +44,7 @@ public class _1415 { public static class Solution1 { public String getHappyString(int n, int k) { - char[] chars = new char[]{'a', 'b', 'c'}; + char[] chars = new char[] {'a', 'b', 'c'}; List happyStrings = new ArrayList<>(); happyStrings.add(""); happyStrings = findAllHappyStrings(chars, n, happyStrings); diff --git a/src/main/java/com/fishercoder/solutions/_1417.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1417.java similarity index 98% rename from src/main/java/com/fishercoder/solutions/_1417.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1417.java index c44a525f7b..34b599cce0 100644 --- a/src/main/java/com/fishercoder/solutions/_1417.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1417.java @@ -1,9 +1,9 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.List; -/** +/* * 1417. Reformat The String * * Given alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits). diff --git a/src/main/java/com/fishercoder/solutions/_1418.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1418.java similarity index 98% rename from src/main/java/com/fishercoder/solutions/_1418.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1418.java index e6cefc431a..f067af1d60 100644 --- a/src/main/java/com/fishercoder/solutions/_1418.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1418.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.Collections; @@ -9,7 +9,7 @@ import java.util.Set; import java.util.TreeMap; -/** +/* * 1418. Display Table of Food Orders in a Restaurant * * Given the array orders, which represents the orders that customers have done in a restaurant. diff --git a/src/main/java/com/fishercoder/solutions/_1422.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1422.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_1422.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1422.java index 4cc94b6a81..209ed5b280 100644 --- a/src/main/java/com/fishercoder/solutions/_1422.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1422.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; public class _1422 { public static class Solution1 { diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1423.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1423.java new file mode 100644 index 0000000000..a018851f9b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1423.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1423 { + public static class Solution1 { + public int maxScore(int[] cardPoints, int k) { + int maxScore = 0; + if (cardPoints.length <= k) { + for (int point : cardPoints) { + maxScore += point; + } + return maxScore; + } + for (int i = 0; i < k; i++) { + maxScore += cardPoints[i]; + } + int runningSum = maxScore; + for (int i = cardPoints.length - 1, j = 1; i >= 0 && j <= k; i--, j++) { + runningSum = runningSum - cardPoints[k - j] + cardPoints[i]; + maxScore = Math.max(maxScore, runningSum); + } + return maxScore; + } + } + + public static class Solution2 { + /* + * My own implementation after looking at hints on LeetCode. + */ + public int maxScore(int[] cardPoints, int k) { + long sum = 0; + for (int i = 0; i < cardPoints.length; i++) { + sum += cardPoints[i]; + } + int windowSize = cardPoints.length - k; + if (windowSize == 0) { + return (int) sum; + } + long windowSum = 0; + int ans = 0; + for (int i = 0, j = i; + i < cardPoints.length - windowSize && j <= cardPoints.length + 1; ) { + if (j - i < windowSize) { + windowSum += cardPoints[j]; + j++; + } else if (j - i == windowSize) { + ans = (int) Math.max(ans, sum - windowSum); + windowSum += cardPoints[j]; + j++; + } else { + windowSum -= cardPoints[i]; + i++; + } + } + return (int) Math.max(ans, sum - windowSum); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1424.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1424.java new file mode 100644 index 0000000000..0b97b2b869 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1424.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.*; + +public class _1424 { + public static class Solution1 { + /* + * One key note: + * For all elements on the same diagonal, the sums of their row index and column index are equal. + * This is widely applicable to all matrix problems. + */ + public int[] findDiagonalOrder(List> nums) { + TreeMap> map = new TreeMap<>(); + for (int i = 0; i < nums.size(); i++) { + for (int j = 0; j < nums.get(i).size(); j++) { + int index = i + j; + List list = map.getOrDefault(index, new ArrayList<>()); + list.add(0, nums.get(i).get(j)); + map.put(index, list); + } + } + List list = new ArrayList<>(); + for (int index : map.keySet()) { + list.addAll(map.get(index)); + } + return list.stream().mapToInt(Integer -> Integer).toArray(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1426.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1426.java similarity index 88% rename from src/main/java/com/fishercoder/solutions/_1426.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1426.java index 95c1d7bc1c..fc9524f06e 100644 --- a/src/main/java/com/fishercoder/solutions/_1426.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1426.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.Arrays; import java.util.Set; diff --git a/src/main/java/com/fishercoder/solutions/_1427.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1427.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_1427.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1427.java index 253757d275..8434d70032 100644 --- a/src/main/java/com/fishercoder/solutions/_1427.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1427.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; public class _1427 { public static class Solution1 { diff --git a/src/main/java/com/fishercoder/solutions/_1428.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1428.java similarity index 92% rename from src/main/java/com/fishercoder/solutions/_1428.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1428.java index 8fa292ca11..65516b2d6e 100644 --- a/src/main/java/com/fishercoder/solutions/_1428.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1428.java @@ -1,7 +1,6 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import com.fishercoder.common.classes.BinaryMatrix; - import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -15,7 +14,7 @@ public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { List list = new ArrayList(); for (int i = 0; i < m; i++) { int leftMostColumn = binarySearch(i, binaryMatrix, n - 1); - list.add(new int[]{i, leftMostColumn}); + list.add(new int[] {i, leftMostColumn}); } Collections.sort(list, (a, b) -> a[1] - b[1]); return list.get(0)[1] == 101 ? -1 : list.get(0)[1]; diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1429.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1429.java new file mode 100644 index 0000000000..f170bce4fd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1429.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; + +public class _1429 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/first-unique-number/discuss/602698/Java-Easy-Set-O(1) + *

+ * LinkedHashSet is a handy data structure to help preserve the order of all unique elements. + */ + public static class FirstUnique { + + Set uniqSet; + Set nonUniqSet; + + public FirstUnique(int[] nums) { + uniqSet = new LinkedHashSet<>(); + nonUniqSet = new HashSet<>(); + for (int num : nums) { + add(num); + } + } + + public int showFirstUnique() { + if (!uniqSet.isEmpty()) { + return uniqSet.iterator().next(); + } + return -1; + } + + public void add(int value) { + if (uniqSet.contains(value)) { + uniqSet.remove(value); + nonUniqSet.add(value); + } + if (!nonUniqSet.contains(value)) { + uniqSet.add(value); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1431.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1431.java similarity index 92% rename from src/main/java/com/fishercoder/solutions/_1431.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1431.java index 782a46d0dc..2b3b01e0e6 100644 --- a/src/main/java/com/fishercoder/solutions/_1431.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1431.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/fishercoder/solutions/_1432.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1432.java similarity index 98% rename from src/main/java/com/fishercoder/solutions/_1432.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1432.java index c757da230f..86d3185209 100644 --- a/src/main/java/com/fishercoder/solutions/_1432.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1432.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; public class _1432 { public static class Solution1 { diff --git a/src/main/java/com/fishercoder/solutions/_1436.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1436.java similarity index 93% rename from src/main/java/com/fishercoder/solutions/_1436.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1436.java index 559921aa72..547aac18a7 100644 --- a/src/main/java/com/fishercoder/solutions/_1436.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1436.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.HashSet; import java.util.List; diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1437.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1437.java new file mode 100644 index 0000000000..8c19225a2d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1437.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1437 { + public static class Solution1 { + public boolean kLengthApart(int[] nums, int k) { + int lastOneIndex = nums[0] == 1 ? 0 : -1; + for (int i = 1; i < nums.length; i++) { + if (nums[i] == 1) { + if (i - lastOneIndex <= k) { + return false; + } else { + lastOneIndex = i; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1438.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1438.java new file mode 100644 index 0000000000..f904ad8195 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1438.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1438 { + public static class Solution1 { + /* + * My completely original solution on 1/19/2022. + */ + public int longestSubarray(int[] nums, int limit) { + int ans = 0; + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); + PriorityQueue minHeap = new PriorityQueue<>(); + for (int left = 0, right = 0; left < nums.length && right < nums.length; right++) { + if (ans == 0) { + ans = 1; + } + maxHeap.offer(nums[right]); + minHeap.offer(nums[right]); + if (!maxHeap.isEmpty() + && !minHeap.isEmpty() + && (maxHeap.peek() - minHeap.peek() <= limit)) { + ans = Math.max(ans, right - left + 1); + } else { + maxHeap.remove(nums[left]); + minHeap.remove(nums[left]); + left++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1439.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1439.java new file mode 100644 index 0000000000..58809e235a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1439.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.TreeSet; + +public class _1439 { + public static class Solution1 { + /* + * My completely own implementation after reading the hint on LeetCode: + * 1. We put the sum along with every single element's index in each row as an array into a TreeSet, let it sort by its sum ascendingly; + * each time we poll an element out of the set, we can find the next m smallest sums by moving each row index to the right by one. + * 2. priority queue doesn't help in this case unless used in combination with a set to filter out duplicates; + * 3. implement a customized comparator for treeset: if sum i.e. entry[0] doesn't equal, then it's not a duplicate, + * then we compare the rest of the elements in the array, as long as anyone of them differs at the same index, it's not a duplicate. + * + * Again, using a pen and paper to visualize my thought process helps a lot! + */ + public int kthSmallest(int[][] mat, int k) { + TreeSet treeSet = + new TreeSet<>( + new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + if (o1[0] != o2[0]) { + return o1[0] - o2[0]; + } else { + for (int i = 1; i < o1.length; i++) { + if (o1[i] != o2[i]) { + return o1[i] - o2[i]; + } + } + return 0; + } + } + }); + int m = mat.length; + int n = mat[0].length; + int sum = 0; + int[] entry = new int[m + 1]; + for (int i = 0; i < m; i++) { + sum += mat[i][0]; + } + entry[0] = sum; + treeSet.add(entry); + int count = 0; + while (count < k) { + int[] curr = treeSet.pollFirst(); + count++; + if (count == k) { + return curr[0]; + } + for (int i = 0; i < m; i++) { + int[] next = Arrays.copyOf(curr, curr.length); + if (curr[i + 1] + 1 < n) { + next[0] -= mat[i][curr[i + 1]]; + next[0] += mat[i][curr[i + 1] + 1]; + next[i + 1]++; + treeSet.add(next); + } + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_1441.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1441.java similarity index 94% rename from src/main/java/com/fishercoder/solutions/_1441.java rename to src/main/java/com/fishercoder/solutions/secondthousand/_1441.java index 5317208235..136312d69d 100644 --- a/src/main/java/com/fishercoder/solutions/_1441.java +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1441.java @@ -1,4 +1,4 @@ -package com.fishercoder.solutions; +package com.fishercoder.solutions.secondthousand; import java.util.ArrayList; import java.util.HashSet; diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1446.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1446.java new file mode 100644 index 0000000000..1d419f58d6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1446.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1446 { + public static class Solution1 { + public int maxPower(String s) { + int max = 0; + for (int i = 0; i < s.length(); i++) { + int start = i; + while (i + 1 < s.length() && s.charAt(i) == s.charAt(i + 1)) { + i++; + } + max = Math.max(max, i - start + 1); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1447.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1447.java new file mode 100644 index 0000000000..f55172ab23 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1447.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1447 { + public static class Solution1 { + public List simplifiedFractions(int n) { + List result = new ArrayList<>(); + for (int denominator = 2; denominator <= n; denominator++) { + for (int nominator = 1; nominator < denominator; nominator++) { + if (getGcd(nominator, denominator) == 1) { + result.add(nominator + "/" + denominator); + } + } + } + return result; + } + + private int getGcd(int a, int b) { + return b == 0 ? a : getGcd(b, a % b); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1448.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1448.java new file mode 100644 index 0000000000..b4f792ce96 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1448.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.Collections; +import java.util.PriorityQueue; + +public class _1448 { + public static class Solution1 { + int count; + + public int goodNodes(TreeNode root) { + dfs(root, new PriorityQueue<>(Collections.reverseOrder())); + return count; + } + + private void dfs(TreeNode root, PriorityQueue maxHeap) { + if (root == null) { + return; + } + maxHeap.offer(root.val); + if (root.val >= maxHeap.peek()) { + count++; + } + if (root.left != null) { + dfs(root.left, maxHeap); + } + if (root.right != null) { + dfs(root.right, maxHeap); + } + maxHeap.remove(root.val); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1450.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1450.java new file mode 100644 index 0000000000..db425d2b67 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1450.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1450 { + public static class Solution1 { + public int busyStudent(int[] startTime, int[] endTime, int queryTime) { + int count = 0; + for (int i = 0; i < startTime.length; i++) { + if (startTime[i] <= queryTime && endTime[i] >= queryTime) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1451.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1451.java new file mode 100644 index 0000000000..7d0ee5b031 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1451.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _1451 { + public static class Solution1 { + public String arrangeWords(String text) { + TreeMap> map = new TreeMap<>(); + String[] words = text.split(" "); + for (String word : words) { + int len = word.length(); + if (!map.containsKey(len)) { + map.put(len, new ArrayList<>()); + } + map.get(len).add(word.toLowerCase()); + } + StringBuilder sb = new StringBuilder(); + boolean first = true; + for (int len : map.keySet()) { + List strings = map.get(len); + for (String str : strings) { + if (first) { + str = Character.toUpperCase(str.charAt(0)) + str.substring(1); + first = false; + } + sb.append(str + " "); + } + } + return sb.substring(0, sb.length() - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1452.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1452.java new file mode 100644 index 0000000000..066d4ba178 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1452.java @@ -0,0 +1,74 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +public class _1452 { + public static class Solution1 { + public List peopleIndexes(List> favoriteCompanies) { + TreeMap map = + new TreeMap<>( + new Comparator() { + @Override + public int compare(String o1, String o2) { + int diffLength = o1.length() - o2.length(); + if (diffLength != 0) { + return diffLength; + } + return o1.compareTo(o2); + } + }); + Map> setMap = new HashMap<>(); + for (int i = 0; i < favoriteCompanies.size(); i++) { + List list = favoriteCompanies.get(i); + Collections.sort(list); + StringBuilder sb = new StringBuilder(); + Set set = new HashSet<>(); + for (String str : list) { + sb.append(str); + set.add(str); + } + map.put(sb.toString(), i); + setMap.put(sb.toString(), set); + } + List keys = new ArrayList<>(); + for (String key : map.keySet()) { + keys.add(key); + } + List result = new ArrayList<>(); + for (int i = 0; i < keys.size(); i++) { + boolean goodCandidate = true; + for (int j = i + 1; j < keys.size(); j++) { + if (keys.get(j).contains(keys.get(i))) { + goodCandidate = false; + break; + } + } + if (goodCandidate) { + Set smallerSet = setMap.get(keys.get(i)); + for (int j = 0; j < keys.size(); j++) { + if (j != i) { + Set biggerSet = setMap.get(keys.get(j)); + if (biggerSet.containsAll(smallerSet)) { + goodCandidate = false; + break; + } + } + } + } + if (goodCandidate) { + result.add(map.get(keys.get(i))); + } + } + Collections.sort(result); + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1455.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1455.java new file mode 100644 index 0000000000..b9b88bccfd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1455.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1455 { + public static class Solution1 { + public int isPrefixOfWord(String sentence, String searchWord) { + String[] words = sentence.split(" "); + for (int i = 0; i < words.length; i++) { + if (words[i].startsWith(searchWord)) { + return i + 1; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1456.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1456.java new file mode 100644 index 0000000000..47f5d3bb08 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1456.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _1456 { + public static class Solution1 { + public int maxVowels(String s, int k) { + Map vowels = new HashMap<>(); + Set set = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u')); + int left = 0; + for (; left < k; left++) { + if (set.contains(s.charAt(left))) { + vowels.put(s.charAt(left), vowels.getOrDefault(s.charAt(left), 0) + 1); + } + } + int max = 0; + for (char c : vowels.keySet()) { + max += vowels.get(c); + } + int right = left; + left = 0; + for (; right < s.length(); right++, left++) { + char leftChar = s.charAt(left); + if (set.contains(leftChar) + && vowels.containsKey(leftChar) + && vowels.get(leftChar) > 0) { + vowels.put(leftChar, vowels.get(leftChar) - 1); + } + char rightChar = s.charAt(right); + if (set.contains(rightChar)) { + vowels.put(rightChar, vowels.getOrDefault(rightChar, 0) + 1); + } + int thisMax = 0; + for (char c : vowels.keySet()) { + thisMax += vowels.get(c); + } + max = Math.max(max, thisMax); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1457.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1457.java new file mode 100644 index 0000000000..91273ff8a8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1457.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1457 { + public static class Solution1 { + public int pseudoPalindromicPaths(TreeNode root) { + List> allPaths = new ArrayList<>(); + List path = new ArrayList<>(); + dfs(root, path, allPaths); + int result = 0; + for (List thisPath : allPaths) { + Map count = findCount(thisPath); + int oddCount = 0; + for (int num : count.keySet()) { + if (count.get(num) % 2 != 0) { + oddCount++; + } + if (oddCount > 1) { + break; + } + } + if (oddCount <= 1) { + result++; + } + } + return result; + } + + private void dfs(TreeNode root, List path, List> allPaths) { + if (root.left == null && root.right == null) { + path.add(root.val); + allPaths.add(new ArrayList<>(path)); + return; + } + path.add(root.val); + if (root.left != null) { + dfs(root.left, path, allPaths); + path.remove(path.size() - 1); + } + if (root.right != null) { + dfs(root.right, path, allPaths); + path.remove(path.size() - 1); + } + } + + private Map findCount(List path) { + Map map = new HashMap<>(); + path.forEach(i -> map.put(i, map.getOrDefault(i, 0) + 1)); + return map; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1460.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1460.java new file mode 100644 index 0000000000..c919202d52 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1460.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1460 { + public static class Solution1 { + public boolean canBeEqual(int[] target, int[] arr) { + Map map = new HashMap<>(); + for (int num : target) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + for (int num : arr) { + map.put(num, map.getOrDefault(num, 0) - 1); + } + for (int key : map.keySet()) { + if (map.get(key) != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1461.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1461.java new file mode 100644 index 0000000000..1f41edbf3e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1461.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _1461 { + public static class Solution1 { + /* + * inspired by https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/660505/Java-4-lines-solution + */ + public boolean hasAllCodes(String s, int k) { + Set set = new HashSet<>(); + for (int i = 0; i <= s.length() - k; i++) { + set.add(s.substring(i, i + k)); + } + return set.size() == 1 << k; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1462.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1462.java new file mode 100644 index 0000000000..9cd3043a1c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1462.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1462 { + public static class Solution1 { + /* + * My completely original solution. + * DFS + part of topological sort (building the adjacency list) + */ + public List checkIfPrerequisite( + int numCourses, int[][] prerequisites, int[][] queries) { + List[] graph = new ArrayList[numCourses]; + for (int i = 0; i < numCourses; i++) { + graph[i] = new ArrayList<>(); + } + for (int[] pre : prerequisites) { + graph[pre[0]].add(pre[1]); + } + List result = new ArrayList<>(); + // this cache is essential to speed things up, otherwise TLE on LeetCode + Map cache = new HashMap<>(); + for (int[] query : queries) { + result.add(isPrereq(query[0], query[1], graph, cache)); + } + return result; + } + + private Boolean isPrereq( + int pre, int target, List[] graph, Map cache) { + if (pre == target) { + cache.put(pre + "-" + target, true); + return true; + } + if (cache.containsKey(pre + "-" + target)) { + return cache.get(pre + "-" + target); + } + for (int v : graph[pre]) { + if (isPrereq(v, target, graph, cache)) { + return true; + } + } + cache.put(pre + "-" + target, false); + return false; + } + } + + public static class Solution2 { + /*TODO: use BFS to solve this problem.*/ + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1464.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1464.java new file mode 100644 index 0000000000..a066ac8e77 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1464.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1464 { + public static class Solution1 { + public int maxProduct(int[] nums) { + int maxProduct = 0; + for (int i = 0; i < nums.length; i++) { + for (int j = 0; j < nums.length; j++) { + if (i != j) { + int product = (nums[i] - 1) * (nums[j] - 1); + maxProduct = Math.max(maxProduct, product); + } + } + } + return maxProduct; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1466.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1466.java new file mode 100644 index 0000000000..464a7ef4e5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1466.java @@ -0,0 +1,107 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.*; + +public class _1466 { + public static class Solution1 { + public int minReorder(int n, int[][] connections) { + // key is entering city, value is departure city + Map> map = new HashMap<>(); + Queue queue = new LinkedList<>(); + int minReorder = 0; + Set visited = new HashSet<>(); + for (int i = 0; i < n; i++) { + visited.add(i); + } + + // key is departure city, value is entering city + Map> reverseMap = new HashMap<>(); + for (int[] con : connections) { + if (!map.containsKey(con[1])) { + map.put(con[1], new HashSet<>()); + } + map.get(con[1]).add(con[0]); + + if (!reverseMap.containsKey(con[0])) { + reverseMap.put(con[0], new HashSet<>()); + } + reverseMap.get(con[0]).add(con[1]); + + // for all those directly connected to city 0, must be reordered if not yet + // and they are the start nodes of BFS + if (con[0] == 0) { + minReorder++; + queue.offer(con[1]); + visited.remove(con[1]); + visited.remove(0); + } + if (con[1] == 0) { + queue.offer(con[0]); + visited.remove(0); + } + } + while (!queue.isEmpty() || !visited.isEmpty()) { + int curr = queue.poll(); + visited.remove(curr); + if (map.containsKey(curr)) { + Set departureCityList = map.get(curr); + for (int city : departureCityList) { + if (visited.contains(city)) { + queue.offer(city); + } + } + } + if (reverseMap.containsKey(curr)) { + Set enteringCityList = reverseMap.get(curr); + for (int city : enteringCityList) { + if (visited.contains(city)) { + queue.offer(city); + minReorder++; + } + } + } + } + return minReorder; + } + } + + public static class Solution2 { + /* + * build an adjacency list and BFS + */ + public int minReorder(int n, int[][] connections) { + // int[] in the below map holds two integers, the first one means the node, the second + // one means the direction: + // 0 means it's pointing to the key, i.e. doesn't need to be flipped, + // 1 means it's the opposite direction, i.e. needs to be flipped + Map> adjList = new HashMap<>(); + for (int[] conn : connections) { + adjList.computeIfAbsent(conn[0], k -> new ArrayList<>()) + .add(new int[] {conn[1], 1}); + adjList.computeIfAbsent(conn[1], k -> new ArrayList<>()) + .add(new int[] {conn[0], 0}); + } + int count = 0; + Queue queue = new LinkedList<>(); + queue.offer(0); + boolean[] visited = new boolean[n]; + visited[0] = true; + while (!queue.isEmpty()) { + Integer curr = queue.poll(); + if (!adjList.containsKey(curr)) { + continue; + } + for (int[] next : adjList.get(curr)) { + int neighbor = next[0]; + int flip = next[1]; + if (!visited[neighbor]) { + count += flip; + visited[neighbor] = true; + queue.offer(neighbor); + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1469.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1469.java new file mode 100644 index 0000000000..412fb690e9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1469.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.List; + +public class _1469 { + public static class Solution1 { + public List getLonelyNodes(TreeNode root) { + List lonelyNodes = new ArrayList<>(); + dfs(root, lonelyNodes); + return lonelyNodes; + } + + private void dfs(TreeNode root, List lonelyNodes) { + if (root == null) { + return; + } + + if (root.left == null && root.right != null) { + lonelyNodes.add(root.right.val); + } + + if (root.left != null && root.right == null) { + lonelyNodes.add(root.left.val); + } + + dfs(root.left, lonelyNodes); + dfs(root.right, lonelyNodes); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1470.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1470.java new file mode 100644 index 0000000000..3e518afd9d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1470.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1470 { + public static class Solution1 { + public int[] shuffle(int[] nums, int n) { + int[] result = new int[nums.length]; + for (int i = 0, j = 0; i < n && j < 2 * n; i++, j++) { + result[j] = nums[i]; + result[++j] = nums[i + n]; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1471.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1471.java new file mode 100644 index 0000000000..eeceae59f4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1471.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.TreeMap; + +public class _1471 { + public static class Solution1 { + public int[] getStrongest(int[] arr, int k) { + Arrays.sort(arr); + int median = arr.length % 2 != 0 ? arr[arr.length / 2] : arr[arr.length / 2 - 1]; + TreeMap> treeMap = new TreeMap<>(Collections.reverseOrder()); + for (int num : arr) { + int diff = Math.abs(num - median); + if (!treeMap.containsKey(diff)) { + treeMap.put(diff, new ArrayList<>()); + } + treeMap.get(diff).add(num); + } + List sorted = new ArrayList<>(); + for (int key : treeMap.keySet()) { + List sort = treeMap.get(key); + Collections.sort(sort, Collections.reverseOrder()); + sorted.addAll(sort); + } + int[] result = new int[k]; + for (int i = 0; i < k; i++) { + result[i] = sorted.get(i); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1472.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1472.java new file mode 100644 index 0000000000..b1e94b8ace --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1472.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1472 { + public static class Solution1 { + public static class BrowserHistory { + + int curr; + List history; + + public BrowserHistory(String homepage) { + history = new ArrayList<>(); + history.add(homepage); + curr = 0; + } + + public void visit(String url) { + curr++; + history = history.subList(0, curr); + history.add(url); + } + + public String back(int steps) { + curr -= steps; + if (curr < 0) { + curr = 0; + } + return history.get(curr); + } + + public String forward(int steps) { + curr += steps; + if (history.size() <= curr) { + curr = history.size() - 1; + } + return history.get(curr); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1474.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1474.java new file mode 100644 index 0000000000..fa402520b6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1474.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _1474 { + public static class Solution1 { + public ListNode deleteNodes(ListNode head, int m, int n) { + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + while (head != null) { + int mCount = m; + while (head != null && mCount-- > 0) { + tmp.next = new ListNode(head.val); + head = head.next; + tmp = tmp.next; + } + int nCount = n; + while (head != null && nCount-- > 0) { + head = head.next; + } + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1475.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1475.java new file mode 100644 index 0000000000..c6cd366b8a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1475.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1475 { + public static class Solution1 { + public int[] finalPrices(int[] prices) { + int[] result = new int[prices.length]; + for (int i = 0; i < prices.length; i++) { + boolean foundDiscount = false; + for (int j = i + 1; j < prices.length; j++) { + if (prices[j] <= prices[i]) { + result[i] = prices[i] - prices[j]; + foundDiscount = true; + break; + } + } + if (!foundDiscount) { + result[i] = prices[i]; + } + } + result[prices.length - 1] = prices[prices.length - 1]; + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1476.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1476.java new file mode 100644 index 0000000000..b2ea378cff --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1476.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1476 { + public static class Solution1 { + public static class SubrectangleQueries { + int[][] matrix; + int m; + int n; + + public SubrectangleQueries(int[][] rectangle) { + m = rectangle.length; + n = rectangle[0].length; + matrix = rectangle; + } + + public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) { + for (int i = row1; i <= row2; i++) { + for (int j = col1; j <= col2; j++) { + matrix[i][j] = newValue; + } + } + } + + public int getValue(int row, int col) { + return matrix[row][col]; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1480.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1480.java new file mode 100644 index 0000000000..3d7bd67691 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1480.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1480 { + public static class Solution1 { + public int[] runningSum(int[] nums) { + int sum = 0; + int[] result = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; + result[i] = sum; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1481.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1481.java new file mode 100644 index 0000000000..be375531f0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1481.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +public class _1481 { + public static class Solution1 { + public int findLeastNumOfUniqueInts(int[] arr, int k) { + Map unSortedMap = new HashMap<>(); + for (int num : arr) { + unSortedMap.put(num, unSortedMap.getOrDefault(num, 0) + 1); + } + // LinkedHashMap preserve the ordering of elements in which they are inserted + LinkedHashMap sortedMap = new LinkedHashMap<>(); + + unSortedMap.entrySet().stream() + .sorted(Map.Entry.comparingByValue()) + .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue())); + int leastUniq = 0; + for (int key : sortedMap.keySet()) { + int count = sortedMap.get(key); + if (k >= count) { + k -= count; + } else { + leastUniq++; + } + } + return leastUniq; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1482.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1482.java new file mode 100644 index 0000000000..389deb9c46 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1482.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1482 { + public static class Solution1 { + /* + * https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/discuss/686316/JavaC%2B%2BPython-Binary-Search + */ + public int minDays(int[] bloomDay, int m, int k) { + int n = bloomDay.length; + int left = 1; + int right = Integer.MAX_VALUE; + if (m * k > n) { + return -1; + } + while (left < right) { + int mid = left + (right - left) / 2; + int flower = 0; + int bouq = 0; + for (int j = 0; j < n; j++) { + if (bloomDay[j] > mid) { + flower = 0; + } else if (++flower >= k) { + bouq++; + flower = 0; + } + } + if (bouq < m) { + left = mid + 1; + } else { + right = mid; + } + } + return left; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1485.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1485.java new file mode 100644 index 0000000000..b5d5d9f27c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1485.java @@ -0,0 +1,97 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1485 { + public static class Solution1 { + public NodeCopy copyRandomBinaryTree(Node root) { + if (root == null) { + return null; + } + Map map = new HashMap<>(); + map.put(root, new NodeCopy(root.val)); + dfs(root, map); + dfsConnect(root, map); + return map.get(root); + } + + private void dfsConnect(Node root, Map map) { + if (root == null) { + return; + } + NodeCopy copy = map.get(root); + if (root.left != null) { + copy.left = map.get(root.left); + } + if (root.right != null) { + copy.right = map.get(root.right); + } + if (root.random != null) { + copy.random = map.get(root.random); + } + map.put(root, copy); + dfsConnect(root.left, map); + dfsConnect(root.right, map); + } + + private void dfs(Node root, Map map) { + if (root == null) { + return; + } + NodeCopy copy = map.getOrDefault(root, new NodeCopy(root.val)); + if (root.left != null) { + copy.left = new NodeCopy(root.left.val); + } + if (root.right != null) { + copy.right = new NodeCopy(root.right.val); + } + if (root.random != null) { + copy.random = new NodeCopy(root.random.val); + } + map.put(root, copy); + dfs(root.left, map); + dfs(root.right, map); + } + } + + public static class Node { + int val; + public Node left; + public Node right; + public Node random; + + public Node() {} + + public Node(int val) { + this.val = val; + } + + public Node(int val, Node left, Node right, Node random) { + this.val = val; + this.left = left; + this.right = right; + this.random = random; + } + } + + public static class NodeCopy { + int val; + public NodeCopy left; + public NodeCopy right; + public NodeCopy random; + + public NodeCopy() {} + + public NodeCopy(int val) { + this.val = val; + } + + public NodeCopy(int val, NodeCopy left, NodeCopy right, NodeCopy random) { + this.val = val; + this.left = left; + this.right = right; + this.random = random; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1486.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1486.java new file mode 100644 index 0000000000..18a7b8cb7f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1486.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1486 { + public static class Solution1 { + public int xorOperation(int n, int start) { + int[] nums = new int[n]; + for (int i = 0; i < n; i++) { + nums[i] = start + 2 * i; + } + int result = 0; + for (int num : nums) { + result ^= num; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1487.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1487.java new file mode 100644 index 0000000000..2b02781422 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1487.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class _1487 { + public static class Solution1 { + public String[] getFolderNames(String[] names) { + String[] result = new String[names.length]; + Map> map = new HashMap<>(); + Set used = new HashSet<>(); + for (int i = 0; i < names.length; i++) { + if (!used.contains(names[i])) { + result[i] = names[i]; + map.put(names[i], new ArrayList<>()); + map.get(names[i]).add(names[i]); + used.add(names[i]); + } else { + List list = map.get(names[i]); + int count = list.size(); + String newName = names[i] + "(" + count + ")"; + while (used.contains(newName)) { + count++; + newName = names[i] + "(" + count + ")"; + } + result[i] = newName; + map.get(names[i]).add(newName); + used.add(newName); + + map.put(newName, new ArrayList<>()); + map.get(newName).add(newName); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1490.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1490.java new file mode 100644 index 0000000000..14b9389a40 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1490.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.Node; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; + +public class _1490 { + public static class Solution1 { + public Node cloneTree(Node root) { + if (root == null) { + return root; + } + Map map = new HashMap<>(); + map.put(root, new Node(root.val)); + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + Node curr = queue.poll(); + for (Node child : curr.children) { + Node childCopy = new Node(child.val); + if (!map.containsKey(child)) { + map.put(child, childCopy); + queue.offer(child); + } + map.get(curr).children.add(childCopy); + } + } + return map.get(root); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1491.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1491.java new file mode 100644 index 0000000000..73194f830c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1491.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1491 { + public static class Solution1 { + public double average(int[] salary) { + int max = salary[0]; + int min = salary[0]; + for (int i = 1; i < salary.length; i++) { + max = Math.max(max, salary[i]); + min = Math.min(min, salary[i]); + } + long total = 0; + int count = 0; + for (int i = 0; i < salary.length; i++) { + if (salary[i] != max && salary[i] != min) { + total += salary[i]; + count++; + } + } + return (double) total / count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1492.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1492.java new file mode 100644 index 0000000000..f949dad024 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1492.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1492 { + public static class Solution1 { + public int kthFactor(int n, int k) { + List list = new ArrayList<>(); + for (int i = 1; i <= n; i++) { + if (n % i == 0) { + list.add(i); + } + } + return list.size() >= k ? list.get(k - 1) : -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1493.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1493.java new file mode 100644 index 0000000000..e54a39e203 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1493.java @@ -0,0 +1,75 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1493 { + public static class Solution1 { + public int longestSubarray(int[] nums) { + List brackets = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + if (nums[i] == 1) { + int right = i + 1; + while (right < nums.length && nums[right] == 1) { + right++; + } + if (right < nums.length && nums[right] == 1) { + brackets.add(new int[] {i, right}); + } else { + brackets.add(new int[] {i, right - 1}); + } + i = right; + } + } + int longest = 0; + for (int[] bracket : brackets) { + if (bracket[1] == nums.length - 1 && bracket[0] == 0) { + return nums.length - 1; + } + longest = Math.max(bracket[1] - bracket[0] + 1, longest); + } + for (int i = 0; i < brackets.size() - 1; i++) { + int[] first = brackets.get(i); + int[] second = brackets.get(i + 1); + if (first[1] + 2 == second[0]) { + int connected = (first[1] - first[0] + 1) + (second[1] - second[0] + 1); + longest = Math.max(longest, connected); + } + } + return longest; + } + } + + public static class Solution2 { + /* + * Sliding window solution + * Credit: https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/discuss/708112/JavaC%2B%2BPython-Sliding-Window-at-most-one-0 + *

+ * we initialize k to be one, meaning we could have at most one zero in the sliding window + * i is the left pointer + * j is the right pointer + * when j encounters a zero, we'll decrement k by one; + * and once k becomes negative, that means we have more than one zeroes in the sliding window, + * so we'll have to move left pointer to the right until k becomes not negative; + * along this process, we use result to hold the max length of this sliding window + */ + public int longestSubarray(int[] nums) { + int i = 0; + int k = 1; + int result = 0; + for (int j = 0; j < nums.length; j++) { + if (nums[j] == 0) { + k--; + } + while (k < 0) { + if (nums[i] == 0) { + k++; + } + i++; + } + result = Math.max(result, j - i); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1496.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1496.java new file mode 100644 index 0000000000..bd2a69735a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1496.java @@ -0,0 +1,69 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Objects; +import java.util.Stack; + +public class _1496 { + public static class Solution1 { + public boolean isPathCrossing(String path) { + Stack visited = new Stack<>(); + visited.add(new Coord(0, 0)); + for (char c : path.toCharArray()) { + Coord last = visited.peek(); + if (c == 'N') { + Coord nextStep = new Coord(last.x, last.y + 1); + if (visited.contains(nextStep)) { + return true; + } + visited.add(nextStep); + } else if (c == 'S') { + Coord nextStep = new Coord(last.x, last.y - 1); + if (visited.contains(nextStep)) { + return true; + } + visited.add(nextStep); + } else if (c == 'E') { + Coord nextStep = new Coord(last.x - 1, last.y); + if (visited.contains(nextStep)) { + return true; + } + visited.add(nextStep); + } else if (c == 'W') { + Coord nextStep = new Coord(last.x + 1, last.y); + if (visited.contains(nextStep)) { + return true; + } + visited.add(nextStep); + } + } + return false; + } + + static class Coord { + int x; + int y; + + public Coord(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Coord coord = (Coord) o; + return x == coord.x && y == coord.y; + } + + @Override + public int hashCode() { + return Objects.hash(x, y); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1502.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1502.java new file mode 100644 index 0000000000..b8e6fb9852 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1502.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1502 { + public static class Solution1 { + public boolean canMakeArithmeticProgression(int[] arr) { + Arrays.sort(arr); + for (int i = 0; i < arr.length - 2; i++) { + if (arr[i + 1] - arr[i] != arr[i + 2] - arr[i + 1]) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1507.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1507.java new file mode 100644 index 0000000000..755ffef4f9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1507.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1507 { + public static class Solution1 { + public String reformatDate(String date) { + String[] dates = date.split(" "); + return dates[2] + "-" + getMonth(dates[1]) + "-" + getDay(dates[0]); + } + + private String getDay(String day) { + String formatedDay = day.substring(0, day.length() - 2); + if (formatedDay.length() == 1) { + return "0" + formatedDay; + } + return formatedDay; + } + + private String getMonth(String month) { + String result = ""; + switch (month) { + case "Jan": + result = "01"; + break; + case "Feb": + result = "02"; + break; + case "Mar": + result = "03"; + break; + case "Apr": + result = "04"; + break; + case "May": + result = "05"; + break; + case "Jun": + result = "06"; + break; + case "Jul": + result = "07"; + break; + case "Aug": + result = "08"; + break; + case "Sep": + result = "09"; + break; + case "Oct": + result = "10"; + break; + case "Nov": + result = "11"; + break; + case "Dec": + result = "12"; + break; + default: + result = ""; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1508.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1508.java new file mode 100644 index 0000000000..cbdec8f713 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1508.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _1508 { + public static class Solution1 { + public int rangeSum(int[] nums, int n, int left, int right) { + List list = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + long sum = nums[i]; + list.add(sum); + for (int j = i + 1; j < nums.length; j++) { + sum += nums[j]; + list.add(sum); + } + } + Collections.sort(list); + long result = 0; + for (int i = left - 1; i < right; i++) { + result += list.get(i); + } + return (int) result % 1000000007; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1509.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1509.java new file mode 100644 index 0000000000..547be11472 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1509.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1509 { + public static class Solution1 { + public int minDifference(int[] nums) { + if (nums.length <= 4) { + return 0; + } + Arrays.sort(nums); + int len = nums.length; + // try to change three biggest nums to smallest + int minDiff = Math.abs(nums[len - 4] - nums[0]); + + // now try to change the three smallest to biggest + minDiff = Math.min(minDiff, nums[len - 1] - nums[3]); + + // now try to change first two and last one + minDiff = Math.min(minDiff, nums[len - 2] - nums[2]); + + // now try to change first one and last two + minDiff = Math.min(minDiff, nums[len - 3] - nums[1]); + return minDiff; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1512.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1512.java new file mode 100644 index 0000000000..8e657ab1d4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1512.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1512 { + public static class Solution1 { + public int numIdenticalPairs(int[] nums) { + int count = 0; + for (int i = 0; i < nums.length; i++) { + for (int j = i + 1; j < nums.length; j++) { + if (nums[i] == nums[j]) { + count++; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1514.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1514.java new file mode 100644 index 0000000000..556b77efa6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1514.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; +import java.util.Queue; + +public class _1514 { + public static class Solution1 { + public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) { + List[] nodeToNodesList = new List[n]; + List[] nodeToProbabilitiesList = new List[n]; + for (int i = 0; i < n; i++) { + nodeToNodesList[i] = new ArrayList<>(); + nodeToProbabilitiesList[i] = new ArrayList<>(); + } + for (int i = 0; i < edges.length; i++) { + int u = edges[i][0]; + int v = edges[i][1]; + double w = succProb[i]; + nodeToNodesList[u].add(v); + nodeToProbabilitiesList[u].add(w); + + nodeToNodesList[v].add(u); + nodeToProbabilitiesList[v].add(w); + } + + double[] probabilities = new double[n]; + probabilities[start] = 1.0; + boolean[] visited = new boolean[n]; + Queue queue = new ArrayDeque<>(); + queue.add(start); + visited[start] = true; + while (!queue.isEmpty()) { + int u = queue.poll(); + visited[u] = false; + + for (int i = 0; i < nodeToNodesList[u].size(); i++) { + int v = nodeToNodesList[u].get(i); + double w = nodeToProbabilitiesList[u].get(i); + if (probabilities[u] * w > probabilities[v]) { + probabilities[v] = probabilities[u] * w; + if (!visited[v]) { + visited[v] = true; + queue.add(v); + } + } + } + } + + return probabilities[end]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1518.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1518.java new file mode 100644 index 0000000000..0f96952e6f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1518.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1518 { + public static class Solution1 { + public int numWaterBottles(int numBottles, int numExchange) { + int drank = numBottles; + int emptyBottles = numBottles; + while (emptyBottles >= numExchange) { + int exchangedBottles = emptyBottles / numExchange; + drank += exchangedBottles; + int unUsedEmptyBottles = emptyBottles % numExchange; + emptyBottles = exchangedBottles + unUsedEmptyBottles; + } + return drank; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1523.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1523.java new file mode 100644 index 0000000000..4116871d11 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1523.java @@ -0,0 +1,12 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1523 { + public static class Solution1 { + public int countOdds(int low, int high) { + if (low % 2 != 0 || high % 2 != 0) { + return (high - low) / 2 + 1; + } + return (high - low) / 2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1524.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1524.java new file mode 100644 index 0000000000..9bf5e37a18 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1524.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1524 { + public static class Solution1 { + /* + * This brute force solution will throw exceed time limit exceeded exception on LeetCode. + */ + public int numOfSubarrays(int[] arr) { + long oddCount = 0; + for (int i = 0; i < arr.length; i++) { + long subTotal = 0; + for (int j = i; j < arr.length; j++) { + subTotal += arr[j]; + if (subTotal % 2 != 0) { + oddCount++; + } + } + } + return (int) oddCount % 1000000007; + } + } + + public static class Solution2 { + public int numOfSubarrays(int[] arr) { + int oddSumCount = 0; + int evenSumCount = 1; + long result = 0; + int sum = 0; + for (int num : arr) { + sum += num; + if (sum % 2 == 0) { + result += oddSumCount; + } else { + result += evenSumCount; + } + if (sum % 2 == 0) { + evenSumCount++; + } else { + oddSumCount++; + } + result %= 1000000007; + } + return (int) result % 1000000007; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1525.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1525.java new file mode 100644 index 0000000000..eeb95961fe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1525.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1525 { + public static class Solution1 { + public int numSplits(String s) { + int goodSplits = 0; + int[] left = new int[26]; + int[] right = new int[26]; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + right[c - 'a']++; + } + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + left[c - 'a']++; + int distinctCharOnTheLeft = getDistinct(left); + right[c - 'a']--; + int distinctCharOnTheRight = getDistinct(right); + if (distinctCharOnTheLeft == distinctCharOnTheRight) { + goodSplits++; + } + } + return goodSplits; + } + + private int getDistinct(int[] count) { + int c = 0; + for (int i : count) { + if (i != 0) { + c++; + } + } + return c; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1526.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1526.java new file mode 100644 index 0000000000..ae5e0b628a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1526.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1526 { + public static class Solution1 { + /* + * This brute force solution results in TLE on LeetCode. + */ + public int minNumberOperations(int[] target) { + int ops = 0; + while (!allZero(target)) { + int i = 0; + while (target[i] == 0) { + i++; + } + for (; i < target.length && target[i] != 0; i++) { + target[i]--; + } + ops++; + } + return ops; + } + + private boolean allZero(int[] target) { + for (int i : target) { + if (i != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1528.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1528.java new file mode 100644 index 0000000000..48a59629b9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1528.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1528 { + public static class Solution1 { + public String restoreString(String s, int[] indices) { + char[] c = new char[s.length()]; + for (int i = 0; i < s.length(); i++) { + int index = findIndex(indices, i); + c[i] = s.charAt(index); + } + return new String(c); + } + + private static int findIndex(int[] indices, int i) { + for (int j = 0; j < indices.length; j++) { + if (indices[j] == i) { + return j; + } + } + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1530.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1530.java new file mode 100644 index 0000000000..f49da8f469 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1530.java @@ -0,0 +1,74 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _1530 { + public static class Solution1 { + public int countPairs(TreeNode root, int distance) { + Map childToParentMap = new HashMap<>(); + List leafNodes = new ArrayList(); + postOrderToFindAllLeavesAndBuildChildParentMap(root, childToParentMap, leafNodes); + int pairs = 0; + for (TreeNode leaf : leafNodes) { + pairs += bfsToPossibleLeaves(leaf, distance, childToParentMap); + } + return pairs / 2; + } + + private int bfsToPossibleLeaves( + TreeNode leaf, int distance, Map childToParentMap) { + Queue q = new LinkedList<>(); + q.offer(leaf); + Set visited = new HashSet<>(); + visited.add(leaf); + int count = 0; + while (!q.isEmpty() && distance >= 0) { + int size = q.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = q.poll(); + if (leaf != curr && curr.left == null && curr.right == null) { + count++; + } + if (curr.left != null && visited.add(curr.left)) { + q.offer(curr.left); + } + if (curr.right != null && visited.add(curr.right)) { + q.offer(curr.right); + } + if (childToParentMap.containsKey(curr) + && visited.add(childToParentMap.get(curr))) { + q.offer(childToParentMap.get(curr)); + } + } + distance--; + } + return count; + } + + private void postOrderToFindAllLeavesAndBuildChildParentMap( + TreeNode node, Map childToParentMap, List leafNodes) { + if (node == null) { + return; + } + if (node.left != null) { + childToParentMap.put(node.left, node); + } + if (node.right != null) { + childToParentMap.put(node.right, node); + } + postOrderToFindAllLeavesAndBuildChildParentMap(node.left, childToParentMap, leafNodes); + postOrderToFindAllLeavesAndBuildChildParentMap(node.right, childToParentMap, leafNodes); + if (node.left == null && node.right == null) { + leafNodes.add(node); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1534.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1534.java new file mode 100644 index 0000000000..f80c3463b9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1534.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1534 { + public static class Solution1 { + public int countGoodTriplets(int[] arr, int a, int b, int c) { + int count = 0; + for (int i = 0; i < arr.length - 2; i++) { + for (int j = i + 1; j < arr.length - 1; j++) { + for (int k = j + 1; k < arr.length; k++) { + if (Math.abs(arr[i] - arr[j]) <= a + && Math.abs(arr[j] - arr[k]) <= b + && Math.abs(arr[i] - arr[k]) <= c) { + count++; + } + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1535.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1535.java new file mode 100644 index 0000000000..7c98d618ae --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1535.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1535 { + public static class Solution1 { + public int getWinner(int[] arr, int k) { + int winner = arr[0]; + int winTimes = 0; + for (int i = 1; i < arr.length; i++) { + if (arr[i] > winner) { + winner = arr[i]; + winTimes = 1; + } else { + winTimes++; + } + + if (winTimes >= k) { + return winner; + } + } + return winner; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1539.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1539.java new file mode 100644 index 0000000000..e3b6b81045 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1539.java @@ -0,0 +1,99 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _1539 { + public static class Solution1 { + /* + * Space: O(n) + * Time: O(n) + */ + public int findKthPositive(int[] arr, int k) { + Set set = new HashSet<>(); + int max = 0; + for (int i : arr) { + set.add(i); + max = Math.max(max, i); + } + int missed = 0; + for (int i = 1; i <= max; i++) { + if (!set.contains(i)) { + missed++; + } + if (missed == k) { + return i; + } + } + while (missed++ < k) { + max++; + } + return max; + } + } + + public static class Solution2 { + /* + * Space: O(1) + * Time: O(n) + */ + public int findKthPositive(int[] arr, int k) { + int missed = 0; + for (int i = 0; i < arr.length; i++) { + if (i == 0) { + missed += arr[0] - 1; + if (missed >= k) { + return k; + } + } else { + missed += arr[i] - arr[i - 1] - 1; + if (missed >= k) { + missed -= arr[i] - arr[i - 1] - 1; + int result = arr[i - 1]; + while (missed++ < k) { + result++; + } + return result; + } + } + } + int result = arr[arr.length - 1]; + while (missed++ < k) { + result++; + } + return result; + } + } + + public static class Solution3 { + /* + * Use binary search: + * use an array without missing integers to illustrate: + * 1, 2, 3, 4, 5 + * 2, 3, 4, 7, 11 + * at index = 2, number of missing positive numbers: arr[index] - index - 1 + *

+ * Space: O(1) + * Time: O(logn) + * Credit: https://leetcode.com/problems/kth-missing-positive-number/editorial/ + */ + public int findKthPositive(int[] arr, int k) { + int left = 0; + int right = arr.length - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (arr[mid] - mid - 1 < k) { + left = mid + 1; + } else { + right = mid - 1; + } + } + // when it exits the above while loop, left = right + 1; + // the k-th missing number should be between arr[right] and arr[left] + // the number of integers missing before arr[right] is arr[right] - right - 1; + // so the number to return is: + // arr[right] + k - (arr[right] - right - 1) = k + right + 1 = k + left; + return left + k; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1541.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1541.java new file mode 100644 index 0000000000..e88ec9bb78 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1541.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Stack; + +public class _1541 { + public static class Solution1 { + public int minInsertions(String s) { + Stack stack = new Stack<>(); + int insertionsNeeded = 0; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '(') { + if (stack.isEmpty()) { + stack.add(c); + } else { + if (stack.peek() == ')') { + // in this case, we need to add one more ')' to get two consecutive + // right paren, then we could pop the one ')' and one '(' off the stack + insertionsNeeded++; + stack.pop(); + stack.pop(); + stack.add(c); + } else { + stack.add(c); + } + } + } else if (c == ')') { + if (stack.isEmpty()) { + // in this case, we need to add one '(' before we add this ')' onto this + // stack + insertionsNeeded++; + stack.add('('); + stack.add(c); + } else { + if (stack.peek() == ')') { + // in this case, we could pop the one ')' and one '(' off the stack + stack.pop(); + stack.pop(); + } else { + stack.add(c); + } + } + } + } + if (stack.isEmpty()) { + return insertionsNeeded; + } else { + while (!stack.isEmpty()) { + char pop = stack.pop(); + if (pop == '(') { + insertionsNeeded += 2; + } else { + insertionsNeeded++; + stack.pop(); + } + } + return insertionsNeeded; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1544.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1544.java new file mode 100644 index 0000000000..87ed442880 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1544.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Stack; + +public class _1544 { + public static class Solution1 { + public String makeGood(String s) { + Stack stack = new Stack<>(); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (stack.isEmpty()) { + stack.add(c); + } else { + if (Character.toLowerCase(stack.peek()) == Character.toLowerCase(c)) { + if ((Character.isLowerCase(stack.peek()) && Character.isUpperCase(c))) { + stack.pop(); + } else if (Character.isUpperCase(stack.peek()) + && Character.isLowerCase(c)) { + stack.pop(); + } else { + stack.add(c); + } + } else { + stack.add(c); + } + } + } + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append(stack.pop()); + } + return sb.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1545.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1545.java new file mode 100644 index 0000000000..a78541033d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1545.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1545 { + public static class Solution1 { + public char findKthBit(int n, int k) { + String[] strings = new String[n]; + strings[0] = "0"; + for (int i = 1; i < n; i++) { + strings[i] = formString(strings[i - 1]); + } + return strings[n - 1].charAt(k - 1); + } + + private String formString(String str) { + return str + "1" + reverse(invert(str)); + } + + private String reverse(String str) { + StringBuilder sb = new StringBuilder(); + return sb.append(str).reverse().toString(); + } + + private String invert(String str) { + StringBuilder sb = new StringBuilder(); + for (char c : str.toCharArray()) { + if (c == '1') { + sb.append('0'); + } else { + sb.append('1'); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1550.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1550.java new file mode 100644 index 0000000000..638d563b53 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1550.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1550 { + public static class Solution1 { + public boolean threeConsecutiveOdds(int[] arr) { + for (int i = 0; i < arr.length - 2; i++) { + if (arr[i] % 2 == 1 && arr[i + 1] % 2 == 1 && arr[i + 2] % 2 == 1) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1551.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1551.java new file mode 100644 index 0000000000..f6211fbebc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1551.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1551 { + public static class Solution1 { + public int minOperations(int n) { + int min = 1; + int max = 2 * (n - 1) + 1; + int equalNumber = (max + min) / 2; + int ops = 0; + for (int i = 0; i < n / 2; i++) { + ops += equalNumber - (2 * i + 1); + } + return ops; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1556.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1556.java new file mode 100644 index 0000000000..3348075e9a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1556.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1556 { + public static class Solution1 { + public String thousandSeparator(int n) { + String str = Integer.toString(n); + StringBuilder sb = new StringBuilder(); + for (int i = str.length() - 1, j = 1; i >= 0; i--, j++) { + sb.append(str.charAt(i)); + j++; + if (j % 3 == 0) { + sb.append("."); + } + } + String result = sb.reverse().toString(); + if (result.charAt(0) == '.') { + result = result.substring(1); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1557.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1557.java new file mode 100644 index 0000000000..bd2360b939 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1557.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1557 { + public static class Solution1 { + public List findSmallestSetOfVertices(int n, List> edges) { + int[] indegree = new int[n]; + for (List edge : edges) { + indegree[edge.get(1)]++; + } + List ans = new ArrayList<>(); + for (int i = 0; i < indegree.length; i++) { + if (indegree[i] == 0) { + ans.add(i); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1558.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1558.java new file mode 100644 index 0000000000..c9a3edc99b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1558.java @@ -0,0 +1,73 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1558 { + public static class Solution1 { + public int minOperations(int[] nums) { + int ops = 0; + while (!allZero(nums)) { + if (allEvenAndNonZeroes(nums)) { + nums = half(nums); + ops++; + } else if (hasOdds(nums)) { + int[] result = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 2 != 0) { + result[i] = nums[i] - 1; + ops++; + } else { + result[i] = nums[i]; + } + } + nums = result; + } else { + int[] result = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + if (nums[i] != 0) { + result[i] = nums[i] / 2; + } else { + result[i] = nums[i]; + } + } + nums = result; + ops++; + } + } + return ops; + } + + private boolean hasOdds(int[] nums) { + for (int i : nums) { + if (i % 2 != 0) { + return true; + } + } + return false; + } + + private int[] half(int[] nums) { + int[] result = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + result[i] = nums[i] / 2; + } + return result; + } + + private boolean allEvenAndNonZeroes(int[] nums) { + for (int i : nums) { + if (i % 2 != 0 || i == 0) { + return false; + } + } + return true; + } + + private boolean allZero(int[] nums) { + for (int i : nums) { + if (i != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1560.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1560.java new file mode 100644 index 0000000000..171d3fb6f2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1560.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1560 { + public static class Solution1 { + public List mostVisited(int n, int[] rounds) { + List result = new ArrayList<>(); + Map map = new HashMap<>(); + for (int i = 0; i < rounds.length - 1; i++) { + if (rounds[i] > rounds[i + 1]) { + if (i == 0) { + map.put(rounds[i], 1); + } + for (int j = rounds[i] + 1; j <= n; j++) { + map.put(j, map.getOrDefault(j, 0) + 1); + } + for (int j = 1; j <= rounds[i + 1]; j++) { + map.put(j, map.getOrDefault(j, 0) + 1); + } + } else { + int j; + if (i == 0) { + j = rounds[i]; + } else { + j = rounds[i] + 1; + } + for (; j <= rounds[i + 1]; j++) { + map.put(j, map.getOrDefault(j, 0) + 1); + } + } + } + + int mostVisitedCount = 0; + for (int key : map.keySet()) { + mostVisitedCount = Math.max(mostVisitedCount, map.get(key)); + } + for (int key : map.keySet()) { + if (map.get(key) == mostVisitedCount) { + result.add(key); + } + } + Collections.sort(result); + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1561.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1561.java new file mode 100644 index 0000000000..8095a05981 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1561.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1561 { + public static class Solution1 { + public int maxCoins(int[] piles) { + Arrays.sort(piles); + int j = 0; + int coins = 0; + for (int i = piles.length - 2; i > 0; i -= 2) { + coins += piles[i]; + if (++j == piles.length / 3) { + return coins; + } + } + return coins; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1566.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1566.java new file mode 100644 index 0000000000..8ed8aaedf7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1566.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1566 { + public static class Solution1 { + public boolean containsPattern(int[] arr, int m, int k) { + for (int i = 0; i < arr.length - m; i++) { + int[] pattern = Arrays.copyOfRange(arr, i, i + m); + int times = 1; + for (int j = i + m; j < arr.length; j += m) { + int[] candidate = Arrays.copyOfRange(arr, j, j + m); + if (Arrays.equals(pattern, candidate)) { + times++; + if (times == k) { + return true; + } + } else { + break; + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1567.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1567.java new file mode 100644 index 0000000000..31c4cdc23c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1567.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1567 { + public static class Solution1 { + public int getMaxLen(int[] nums) { + int max = 0; + for (int i = 0; i < nums.length; i++) { + if (nums.length - i <= max) { + return max; + } + if (nums[i] != 0) { + int negatives = nums[i] > 0 ? 0 : 1; + max = Math.max(max, nums[i] > 0 ? 1 : 0); + for (int j = i + 1; j < nums.length; j++) { + if (nums[j] < 0) { + negatives++; + } else if (nums[j] == 0) { + break; + } + if (negatives % 2 == 0) { + max = Math.max(max, j - i + 1); + } + } + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1570.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1570.java new file mode 100644 index 0000000000..660da14e7a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1570.java @@ -0,0 +1,92 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1570 { + public static class Solution1 { + /* + * This is a brute force but accepted solution. + */ + class SparseVector { + int[] vector; + + SparseVector(int[] nums) { + this.vector = nums; + } + + // Return the dotProduct of two sparse vectors + public int dotProduct(SparseVector vec) { + int[] incoming = vec.vector; + int dotProduct = 0; + for (int i = 0; i < vector.length; i++) { + dotProduct += incoming[i] * this.vector[i]; + } + return dotProduct; + } + } + } + + public static class Solution2 { + /* + * More optimal solution: + * 1. use a map to store only non-zero values to save space; + * 2. loop through the smaller list; + * 3. use binary search to find the corresponding index in the bigger list if it exists; + */ + class SparseVector { + private List indexAndNumList; + + SparseVector(int[] nums) { + this.indexAndNumList = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + if (nums[i] != 0) { + this.indexAndNumList.add(new int[] {i, nums[i]}); + } + } + } + + // Return the dotProduct of two sparse vectors + public int dotProduct(SparseVector vec) { + List incoming = vec.indexAndNumList; + if (incoming.size() < this.indexAndNumList.size()) { + return dotProduct(incoming, this.indexAndNumList); + } else { + return dotProduct(this.indexAndNumList, incoming); + } + } + + private int dotProduct(List smaller, List bigger) { + int product = 0; + for (int[] indexAndNum : smaller) { + int[] exists = binarySearch(bigger, indexAndNum[0]); + if (indexAndNum[0] == exists[0]) { + product += indexAndNum[1] * exists[1]; + } + } + return product; + } + + private int[] binarySearch(List indexAndNumList, int target) { + int left = 0; + int right = indexAndNumList.size() - 1; + int[] result = new int[] {-1, 0}; + if (indexAndNumList.get(right)[0] < target + || indexAndNumList.get(left)[0] > target) { + return result; + } + while (left <= right) { + int mid = left + (right - left) / 2; + if (indexAndNumList.get(mid)[0] == target) { + return indexAndNumList.get(mid); + } else if (indexAndNumList.get(mid)[0] > target) { + right = mid - 1; + } else { + left = mid + 1; + } + } + return new int[] {-1, 0}; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1572.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1572.java new file mode 100644 index 0000000000..1eef7fe013 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1572.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _1572 { + public static class Solution1 { + public int diagonalSum(int[][] mat) { + int m = mat.length; + Set added = new HashSet<>(); + int sum = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < m; j++) { + if (i == j) { + added.add(i * m + j); + sum += mat[i][j]; + } + } + } + for (int i = 0; i < m; i++) { + for (int j = m - 1; j >= 0; j--) { + if (i + j == m - 1 && added.add(i * m + j)) { + sum += mat[i][j]; + } + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1574.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1574.java new file mode 100644 index 0000000000..e6678fbc37 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1574.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1574 { + public static class Solution1 { + public int findLengthOfShortestSubarray(int[] arr) { + int left = 0; + while (left < arr.length - 1 && arr[left] <= arr[left + 1]) { + left++; + } + if (left == arr.length - 1) { + return 0; + } + int right = arr.length - 1; + while (right > left && arr[right] >= arr[right - 1]) { + right--; + } + if (right == 0) { + return arr.length - 1; + } + int result = Math.min(arr.length - left - 1, right); + int i = 0; + int j = right; + while (i <= left && j < arr.length) { + if (arr[j] >= arr[i]) { + result = Math.min(result, j - i - 1); + i++; + } else { + j++; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1576.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1576.java new file mode 100644 index 0000000000..cd159b97ac --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1576.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1576 { + public static class Solution1 { + /* + * Each char could have at most two neighbors, so we only need to toggle between three character candidates to avoid repetition. + */ + public String modifyString(String s) { + char[] arr = s.toCharArray(); + for (int i = 0; i < arr.length; i++) { + if (arr[i] == '?') { + for (int j = 0; j < 3; j++) { + if (i > 0 && arr[i - 1] == 'a' + j) { + continue; + } else if (i < arr.length - 1 && arr[i + 1] == 'a' + j) { + continue; + } else { + arr[i] = (char) ('a' + j); + break; + } + } + } + } + return String.valueOf(arr); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1577.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1577.java new file mode 100644 index 0000000000..cc3926a424 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1577.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1577 { + public static class Solution1 { + public int numTriplets(int[] nums1, int[] nums2) { + long result = 0; + for (long num : nums1) { + result += twoProduct(num * num, nums2); + } + for (long num : nums2) { + result += twoProduct(num * num, nums1); + } + return (int) result; + } + + private long twoProduct(long product, int[] nums) { + Map map = new HashMap<>(); + long count = 0; + for (long num : nums) { + if (product % num == 0) { + count += map.getOrDefault(product / num, 0L); + } + map.put(num, map.getOrDefault(num, 0L) + 1); + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1582.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1582.java new file mode 100644 index 0000000000..9947cd39f4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1582.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1582 { + public static class Solution1 { + public int numSpecial(int[][] mat) { + int count = 0; + for (int i = 0; i < mat.length; i++) { + for (int j = 0; j < mat[0].length; j++) { + if (mat[i][j] == 1 && isSpecial(mat, i, j)) { + count++; + } + } + } + return count; + } + + private boolean isSpecial(int[][] mat, int row, int col) { + for (int i = 0; i < mat.length; i++) { + if (i != row && mat[i][col] == 1) { + return false; + } + } + for (int j = 0; j < mat[0].length; j++) { + if (j != col && mat[row][j] == 1) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1583.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1583.java new file mode 100644 index 0000000000..761e81b89f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1583.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1583 { + public static class Solution1 { + public int unhappyFriends(int n, int[][] preferences, int[][] pairs) { + int unhappyFriends = 0; + Map assignedPair = new HashMap<>(); + for (int[] pair : pairs) { + assignedPair.put(pair[0], pair[1]); + assignedPair.put(pair[1], pair[0]); + } + for (int[] pair : pairs) { + if (isUnHappy(pair[1], pair[0], preferences, assignedPair)) { + unhappyFriends++; + } + if (isUnHappy(pair[0], pair[1], preferences, assignedPair)) { + unhappyFriends++; + } + } + return unhappyFriends; + } + + private boolean isUnHappy( + int self, + int assignedFriend, + int[][] preferences, + Map assignedPairs) { + int[] preference = preferences[self]; + int assignedFriendPreferenceIndex = findIndex(preference, assignedFriend); + for (int i = 0; i <= assignedFriendPreferenceIndex; i++) { + int preferredFriend = preference[i]; + int preferredFriendAssignedFriend = assignedPairs.get(preferredFriend); + if (preferredFriendAssignedFriend == self) { + return false; + } + int candidateAssignedFriendIndex = + findIndex(preferences[preferredFriend], preferredFriendAssignedFriend); + if (isPreferred(self, preferences[preferredFriend], candidateAssignedFriendIndex)) { + return true; + } + } + return false; + } + + private boolean isPreferred(int self, int[] preference, int boundary) { + for (int i = 0; i <= boundary; i++) { + if (self == preference[i]) { + return true; + } + } + return false; + } + + private int findIndex(int[] preference, int assignedFriend) { + for (int i = 0; i < preference.length; i++) { + if (preference[i] == assignedFriend) { + return i; + } + } + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1588.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1588.java new file mode 100644 index 0000000000..bc04c9fe8c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1588.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1588 { + public static class Solution1 { + public int sumOddLengthSubarrays(int[] arr) { + int sum = 0; + for (int i = 0; i < arr.length; i++) { + sum += arr[i]; + } + int length = 3; + while (length <= arr.length) { + for (int j = 0; j <= arr.length - length; j++) { + for (int i = j; i < j + length; i++) { + sum += arr[i]; + } + } + length += 2; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1592.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1592.java new file mode 100644 index 0000000000..1dc86f6d4e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1592.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1592 { + public static class Solution1 { + public String reorderSpaces(String text) { + int spaceCount = 0; + for (char c : text.toCharArray()) { + if (c == ' ') { + spaceCount++; + } + } + String[] words = text.trim().split("\\s+"); + if (words.length == 1) { + StringBuilder sb = new StringBuilder(words[0]); + for (int i = 0; i < spaceCount; i++) { + sb.append(" "); + } + return sb.toString(); + } + int trailingSpaces = spaceCount % (words.length - 1); + int newSpaces = spaceCount / (words.length - 1); + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < words.length; j++) { + sb.append(words[j]); + if (j < words.length - 1) { + for (int i = 0; i < newSpaces; i++) { + sb.append(" "); + } + } else { + for (int i = 0; i < trailingSpaces; i++) { + sb.append(" "); + } + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1598.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1598.java new file mode 100644 index 0000000000..0821b9962b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1598.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1598 { + public static class Solution1 { + public int minOperations(String[] logs) { + int steps = 0; + for (String log : logs) { + if (log.equals("../")) { + if (steps > 0) { + steps--; + } + } else if (log.equals("./")) { + // do nothing + } else { + steps++; + } + } + return steps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1600.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1600.java new file mode 100644 index 0000000000..8bb81fa222 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1600.java @@ -0,0 +1,72 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1600 { + public static class Solution1 { + /* + * My completely original solution: + * 1. use a tree structure to represent the king family; + * 2. then use preorder traversal to find the inheritance order; + * 3. use a map to quickly find the corresponding node in the tree based on the name since all names are distinct + */ + public static class Person { + List children; + String name; + boolean isAlive; + + public Person(String kingName) { + this.name = kingName; + this.children = new ArrayList<>(); + this.isAlive = true; + } + } + + public static class ThroneInheritance { + Person king; + Map map; + String kingName; + + public ThroneInheritance(String kingName) { + king = new Person(kingName); + map = new HashMap<>(); + this.kingName = kingName; + map.put(kingName, king); + } + + public void birth(String parentName, String childName) { + Person parentNode = map.getOrDefault(parentName, new Person(parentName)); + Person child = new Person(childName); + parentNode.children.add(child); + map.put(parentName, parentNode); + map.put(childName, child); + } + + public void death(String name) { + Person deadPerson = map.get(name); + deadPerson.isAlive = false; + map.put(name, deadPerson); + } + + public List getInheritanceOrder() { + return preorder(map.get(this.kingName), new ArrayList<>()); + } + + private List preorder(Person person, List list) { + if (person == null) { + return list; + } + if (person.isAlive) { + list.add(person.name); + } + for (Person child : person.children) { + preorder(child, list); + } + return list; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1601.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1601.java new file mode 100644 index 0000000000..9be6d475f5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1601.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1601 { + public static class Solution1 { + int max = 0; + + public int maximumRequests(int n, int[][] requests) { + helper(requests, 0, new int[n], 0); + return max; + } + + private void helper(int[][] requests, int index, int[] count, int num) { + // Traverse all n buildings to see if they are all 0. (means balanced) + if (index == requests.length) { + for (int i : count) { + if (0 != i) { + return; + } + } + max = Math.max(max, num); + return; + } + // Choose this request + count[requests[index][0]]++; + count[requests[index][1]]--; + helper(requests, index + 1, count, num + 1); + count[requests[index][0]]--; + count[requests[index][1]]++; + + // Not Choose the request + helper(requests, index + 1, count, num); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1602.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1602.java new file mode 100644 index 0000000000..a5fada96fe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1602.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.LinkedList; +import java.util.Queue; + +public class _1602 { + public static class Solution1 { + public TreeNode findNearestRightNode(TreeNode root, TreeNode u) { + Queue q = new LinkedList<>(); + q.offer(root); + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = q.poll(); + if (curr == u) { + if (i == size - 1) { + return null; + } else { + return q.poll(); + } + } + if (curr.left != null) { + q.offer(curr.left); + } + if (curr.right != null) { + q.offer(curr.right); + } + } + } + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1603.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1603.java new file mode 100644 index 0000000000..e50ad272bd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1603.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1603 { + public static class Solution1 { + class ParkingSystem { + int[] slots = new int[3]; + + public ParkingSystem(int big, int medium, int small) { + slots[0] = big; + slots[1] = medium; + slots[2] = small; + } + + public boolean addCar(int carType) { + if (carType == 1) { + if (slots[0] > 0) { + slots[0]--; + return true; + } else { + return false; + } + } else if (carType == 2) { + if (slots[1] > 0) { + slots[1]--; + return true; + } else { + return false; + } + } else { + if (slots[2] > 0) { + slots[2]--; + return true; + } else { + return false; + } + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1604.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1604.java new file mode 100644 index 0000000000..f806a02c2b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1604.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1604 { + public static class Solution1 { + public List alertNames(String[] keyName, String[] keyTime) { + Map> map = new HashMap<>(); + for (int i = 0; i < keyName.length; i++) { + if (!map.containsKey(keyName[i])) { + map.put(keyName[i], new ArrayList<>()); + } + map.get(keyName[i]).add(keyTime[i]); + } + List people = new ArrayList<>(); + for (String person : map.keySet()) { + List times = map.get(person); + List minutes = new ArrayList<>(); + for (String time : times) { + String[] hourAndMin = time.split(":"); + Integer minute = + Integer.parseInt(hourAndMin[0]) * 60 + Integer.parseInt(hourAndMin[1]); + minutes.add(minute); + } + Collections.sort(minutes); + for (int i = 0; i < minutes.size() - 2; i++) { + if (minutes.get(i + 2) - minutes.get(i) <= 60) { + people.add(person); + break; + } + } + } + Collections.sort(people); + return people; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1605.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1605.java new file mode 100644 index 0000000000..c24de19be0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1605.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1605 { + public static class Solution1 { + /* + * My completely original solution: + * 1. sort out your logic with a pen and paper first, greedy algorithm should be the way to go; + * 2. each time, take out the minimum value from both rowSet and colSet, put that entire value onto the result grid, + * then deduct that value from the other set if they are not equal, put it back into the minHeap, repeat until both minHeaps are empty; + */ + public int[][] restoreMatrix(int[] rowSum, int[] colSum) { + // form two minHeaps, use their values to sort + PriorityQueue rowMinHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]); + for (int i = 0; i < rowSum.length; i++) { + rowMinHeap.offer(new int[] {i, rowSum[i]}); + } + PriorityQueue colMinHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]); + for (int j = 0; j < colSum.length; j++) { + colMinHeap.offer(new int[] {j, colSum[j]}); + } + + int[][] result = new int[rowSum.length][colSum.length]; + while (!colMinHeap.isEmpty() && !rowMinHeap.isEmpty()) { + int[] minRow = rowMinHeap.poll(); + int[] minCol = colMinHeap.poll(); + if (minRow[1] < minCol[1]) { + result[minRow[0]][minCol[0]] = minRow[1]; + colMinHeap.offer(new int[] {minCol[0], minCol[1] - minRow[1]}); + } else if (minRow[1] > minCol[1]) { + result[minRow[0]][minCol[0]] = minCol[1]; + rowMinHeap.offer(new int[] {minRow[0], minRow[1] - minCol[1]}); + } else { + // the min values from row and col are equal + result[minRow[0]][minCol[0]] = minCol[1]; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1608.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1608.java new file mode 100644 index 0000000000..5e6bfbe9ea --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1608.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1608 { + public static class Solution1 { + public int specialArray(int[] nums) { + Arrays.sort(nums); + int max = nums[nums.length - 1]; + for (int x = 1; x <= max; x++) { + int found = 0; + int i = nums.length - 1; + while (i >= 0 && nums[i] >= x) { + i--; + found++; + } + if (found == x) { + return x; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1609.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1609.java new file mode 100644 index 0000000000..8f8f9a5227 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1609.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _1609 { + public static class Solution1 { + public boolean isEvenOddTree(TreeNode root) { + Queue queue = new LinkedList<>(); + queue.offer(root); + int level = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + List list = new ArrayList<>(); + for (int i = 0; i < size; i++) { + TreeNode node = queue.poll(); + list.add(node.val); + if (node.left != null) { + queue.add(node.left); + } + if (node.right != null) { + queue.add(node.right); + } + } + if (level % 2 == 0) { + for (int i = 0; i < list.size(); i++) { + if (list.get(i) % 2 == 0) { + return false; + } + if (i < list.size() - 1 && list.get(i) >= list.get(i + 1)) { + return false; + } + } + } else { + for (int i = 0; i < list.size(); i++) { + if (list.get(i) % 2 != 0) { + return false; + } + if (i < list.size() - 1 && list.get(i) <= list.get(i + 1)) { + return false; + } + } + } + level++; + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1614.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1614.java new file mode 100644 index 0000000000..89b5f93e78 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1614.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Stack; + +public class _1614 { + public static class Solution1 { + public int maxDepth(String s) { + Stack stack = new Stack<>(); + int maxDepth = 0; + for (char c : s.toCharArray()) { + if (c == '(') { + stack.push(c); + } else if (c == ')') { + maxDepth = Math.max(maxDepth, stack.size()); + stack.pop(); + } + } + return maxDepth; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1619.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1619.java new file mode 100644 index 0000000000..e9d0ea64c6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1619.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1619 { + public static class Solution1 { + public double trimMean(int[] arr) { + Arrays.sort(arr); + int n = arr.length; + long sum = 0; + for (int i = (int) Math.round(n * 0.05); i < (n - n * 0.05); i++) { + sum += arr[i]; + } + return sum / (n - n * 0.1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1620.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1620.java new file mode 100644 index 0000000000..da4738ee24 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1620.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1620 { + public static class Solution1 { + public static int[] bestCoordinate(int[][] towers, int radius) { + int maxSignal = 0; + int[] best = new int[2]; + for (int i = 0; i < towers.length; i++) { + int thisQuality = 0; + for (int j = 0; j < towers.length; j++) { + double distance = + Math.sqrt( + (towers[i][0] - towers[j][0]) * (towers[i][0] - towers[j][0]) + + (towers[i][1] - towers[j][1]) + * (towers[i][1] - towers[j][1])); + if (distance <= radius) { + thisQuality += Math.floor(towers[j][2] / (1 + distance)); + } + } + if (thisQuality > maxSignal) { + maxSignal = thisQuality; + best[0] = towers[i][0]; + best[1] = towers[i][1]; + } else if (thisQuality == maxSignal) { + if (towers[i][0] < best[0]) { + best[0] = towers[i][0]; + best[1] = towers[i][1]; + } else if (towers[i][0] == best[0] && towers[i][1] < best[1]) { + best[0] = towers[i][0]; + best[1] = towers[i][1]; + } + } + } + return best; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1624.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1624.java new file mode 100644 index 0000000000..9391ee4da9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1624.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1624 { + public static class Solution1 { + public int maxLengthBetweenEqualCharacters(String s) { + int maxLen = -1; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + int lastIndex = s.lastIndexOf(c); + if (lastIndex != i) { + maxLen = Math.max(maxLen, Math.abs(lastIndex - i - 1)); + } + } + return maxLen; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1625.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1625.java new file mode 100644 index 0000000000..befb2beae3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1625.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; + +public class _1625 { + public static class Solution1 { + public String findLexSmallestString(String s, int a, int b) { + Queue queue = new LinkedList<>(); + Set seen = new HashSet<>(); + queue.offer(s); + String smallest = s; + while (!queue.isEmpty()) { + String current = queue.poll(); + // add + char[] c = current.toCharArray(); + for (int i = 1; i < c.length; i++) { + if (i % 2 == 1) { + c[i] = (char) (((Integer.parseInt(String.valueOf(c[i])) + a) % 10) + '0'); + } + } + String next = new String(c); + if (smallest.compareTo(next) > 0) { + smallest = next; + } + if (seen.add(next)) { + queue.add(next); + } + // rotate + next = next.substring(next.length() - b) + next.substring(0, next.length() - b); + if (seen.add(next)) { + queue.add(next); + } + if (smallest.compareTo(next) > 0) { + smallest = next; + } + } + return smallest; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1626.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1626.java new file mode 100644 index 0000000000..ffffc6c65e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1626.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1626 { + public static class Solution1 { + public int bestTeamScore(int[] scores, int[] ages) { + int len = scores.length; + int[][] players = new int[len][2]; + for (int i = 0; i < len; i++) { + players[i][0] = ages[i]; + players[i][1] = scores[i]; + } + // sort by age first, if tie, then sort by scores + Arrays.sort(players, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); + + // dp array is the max possible score up to this age, i.e. dp[2] means the max score up + // to from age 0 up to age 2 + int[] dp = new int[len]; + dp[0] = players[0][1]; + for (int i = 1; i < len; i++) { + int maxScoreUpToAgeI = + players[i][1]; // this is the max score possible on this age i alone + for (int j = 0; j < i; j++) { + // then we try to find all possible scores from the min age up to this age i + if (players[i][1] >= players[j][1]) { + maxScoreUpToAgeI = Math.max(maxScoreUpToAgeI, dp[j] + players[i][1]); + } + } + dp[i] = maxScoreUpToAgeI; + } + int bestScore = 0; + for (int score : dp) { + bestScore = Math.max(bestScore, score); + } + return bestScore; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1628.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1628.java new file mode 100644 index 0000000000..e287bcf5d9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1628.java @@ -0,0 +1,83 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +public class _1628 { + public static class Solution1 { + /* + * Being able to think of resorting to Stack data structure is the key here to a straightforward solution. + */ + + public abstract static class Node { + public abstract int evaluate(); + + public abstract List print(Node node, List list); + + // define your fields here + Node left; + Node right; + String val; + } + + static class NodeImpl extends Node { + + @Override + public int evaluate() { + return dfs(this); + } + + private int dfs(Node node) { + if (node == null) { + return 0; + } + if (node.left == null && node.right == null) { + return Integer.parseInt(node.val); + } + String op = node.val; + int left = dfs(node.left); + int right = dfs(node.right); + if (op.equals("+")) { + return left + right; + } else if (op.equals("-")) { + return left - right; + } else if (op.equals("*")) { + return left * right; + } else { + return left / right; + } + } + + public NodeImpl(String val) { + this.val = val; + } + + @Override + public List print(Node node, List list) { + if (node == null) { + return list; + } + print(node.left, list); + list.add(node.val); + print(node.right, list); + return list; + } + } + + public static class TreeBuilder { + public Node buildTree(String[] postfix) { + Deque stack = new LinkedList<>(); + for (String str : postfix) { + Node node = new NodeImpl(str); + if (!Character.isDigit(str.charAt(0))) { + node.right = stack.pollLast(); + node.left = stack.pollLast(); + } + stack.addLast(node); + } + return stack.pop(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1629.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1629.java new file mode 100644 index 0000000000..32d4bed2c8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1629.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1629 { + public static class Solution1 { + public char slowestKey(int[] releaseTimes, String keysPressed) { + Map map = new HashMap<>(); + for (int i = 0; i < releaseTimes.length; i++) { + char c = keysPressed.charAt(i); + int duration; + if (i == 0) { + duration = releaseTimes[i]; + } else { + duration = releaseTimes[i] - releaseTimes[i - 1]; + } + if (!map.containsKey(c)) { + map.put(c, duration); + } else { + int val = map.get(c); + if (duration > val) { + map.put(c, duration); + } + } + } + Map> map2 = new HashMap<>(); + for (char c : map.keySet()) { + int duration = map.get(c); + if (!map2.containsKey(duration)) { + map2.put(duration, new ArrayList<>()); + } + map2.get(duration).add(c); + } + int max = -1; + for (int duration : map2.keySet()) { + List chars = map2.get(duration); + Collections.sort(chars); + map2.put(duration, chars); + max = Math.max(max, duration); + } + return map2.get(max).get(map2.get(max).size() - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1630.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1630.java new file mode 100644 index 0000000000..9528fed952 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1630.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _1630 { + public static class Solution1 { + public List checkArithmeticSubarrays(int[] nums, int[] l, int[] r) { + List result = new ArrayList<>(); + for (int i = 0; i < l.length; i++) { + if (isArithmetic(nums, l[i], r[i])) { + result.add(true); + } else { + result.add(false); + } + } + return result; + } + + private boolean isArithmetic(int[] nums, int start, int end) { + List list = new ArrayList<>(); + for (int i = start; i <= end; i++) { + list.add(nums[i]); + } + Collections.sort(list); + for (int i = 1; i < list.size(); i++) { + if (list.get(i) - list.get(i - 1) != list.get(1) - list.get(0)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1636.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1636.java new file mode 100644 index 0000000000..998dad1239 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1636.java @@ -0,0 +1,69 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public class _1636 { + public static class Solution1 { + public int[] frequencySort(int[] nums) { + Map count = new HashMap<>(); + for (int num : nums) { + count.put(num, count.getOrDefault(num, 0) + 1); + } + TreeMap> map = new TreeMap<>(); + for (int num : count.keySet()) { + int freq = count.get(num); + if (!map.containsKey(freq)) { + map.put(freq, new ArrayList<>()); + } + List list = map.get(freq); + list.add(num); + map.put(freq, list); + } + int[] result = new int[nums.length]; + int i = 0; + for (int num : map.keySet()) { + List list = map.get(num); + Collections.sort(list, Collections.reverseOrder()); + int k = num; + for (int j = 0; j < list.size(); j++, k = num) { + while (k-- > 0) { + result[i++] = list.get(j); + } + } + } + return result; + } + } + + public static class Solution2 { + + public int[] frequencySort(int[] nums) { + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + int[][] pairs = new int[map.size()][2]; + int i = 0; + for (Map.Entry entry : map.entrySet()) { + pairs[i][0] = entry.getKey(); + pairs[i++][1] = entry.getValue(); + } + Arrays.sort(pairs, (a, b) -> a[1] != b[1] ? a[1] - b[1] : b[0] - a[0]); + int[] result = new int[nums.length]; + i = 0; + for (int[] pair : pairs) { + int count = pair[1]; + while (count-- > 0) { + result[i++] = pair[0]; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1637.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1637.java new file mode 100644 index 0000000000..89d83af24d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1637.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeSet; + +public class _1637 { + public static class Solution1 { + public int maxWidthOfVerticalArea(int[][] points) { + TreeSet treeSet = new TreeSet<>(); + for (int[] point : points) { + treeSet.add(point[0]); + } + int ans = 0; + List list = new ArrayList<>(); + for (int x : treeSet) { + list.add(x); + } + for (int i = 0; i < list.size() - 1; i++) { + ans = Math.max(ans, list.get(i + 1) - list.get(i)); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1640.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1640.java new file mode 100644 index 0000000000..40a60e70e6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1640.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1640 { + public static class Solution1 { + public boolean canFormArray(int[] arr, int[][] pieces) { + for (int[] piece : pieces) { + int first = piece[0]; + int index = findIndex(arr, first); + if (index == -1) { + return false; + } + int i = 0; + for (int j = index; i < piece.length && j < arr.length; i++, j++) { + if (arr[j] != piece[i]) { + return false; + } + } + if (i != piece.length) { + return false; + } + } + return true; + } + + private int findIndex(int[] arr, int key) { + for (int i = 0; i < arr.length; i++) { + if (arr[i] == key) { + return i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1641.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1641.java new file mode 100644 index 0000000000..3b204ab7e5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1641.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1641 { + public static class Solution1 { + /* + * I solved this problem using Math, no DP, recursion or backtracking techniques. + * Time: beat 100% submission consistently since it's O(n), essentialy it's O(1) because the contraints in the problem state: 1 <= n <= 50 + * After writing out from n = 1 to 3, we can see the pattern. + * Detailed reasoning to be seen in my YouTube video on my channel: https://youtu.be/gdH4yfgfwiU + */ + public int countVowelStrings(int n) { + if (n == 1) { + return 5; + } + int[] arr = new int[] {1, 1, 1, 1, 1}; + int sum = 5; + for (int i = 2; i <= n; i++) { + int[] copy = new int[5]; + for (int j = 0; j < arr.length; j++) { + if (j == 0) { + copy[j] = sum; + } else { + copy[j] = copy[j - 1] - arr[j - 1]; + } + } + arr = Arrays.copyOf(copy, 5); + sum = 0; + for (int j = 0; j < arr.length; j++) { + sum += arr[j]; + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1642.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1642.java new file mode 100644 index 0000000000..7065a6ed46 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1642.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1642 { + public static class Solution1 { + public int furthestBuilding(int[] heights, int bricks, int ladders) { + PriorityQueue minHeap = new PriorityQueue<>(); + int i = 0; + // we'll assume to use ladders for the first l jumps and adjust it afterwards + for (; i < heights.length - 1 && minHeap.size() < ladders; i++) { + int diff = heights[i + 1] - heights[i]; + if (diff > 0) { + minHeap.offer(diff); + } + } + // now ladders have been used up, we'll use bricks to jump + while (i < heights.length - 1) { + int diff = heights[i + 1] - heights[i]; + if (diff > 0) { + // we'll check if the last one that cost a ladder could actually be filled with + // some bricks + if (!minHeap.isEmpty() && minHeap.peek() < diff) { + bricks -= minHeap.poll(); + minHeap.offer(diff); + } else { + bricks -= diff; + } + if (bricks < 0) { + return i; + } + } + i++; + } + return i; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1644.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1644.java new file mode 100644 index 0000000000..851491a265 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1644.java @@ -0,0 +1,107 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1644 { + public static class Solution1 { + /* + * This is my not so elegant but original solution to get it accepted. + */ + boolean[] exists = new boolean[2]; + + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + exists(p, root, 0); + exists(q, root, 1); + if (!exists[0] || !exists[1]) { + return null; + } + return dfs(root, p, q); + } + + private void exists(TreeNode target, TreeNode root, int index) { + if (root == null) { + return; + } + if (target == root) { + exists[index] = true; + return; + } + if (!exists[index]) { + exists(target, root.left, index); + } + if (!exists[index]) { + exists(target, root.right, index); + } + } + + private TreeNode dfs(TreeNode root, TreeNode p, TreeNode q) { + if (root == null || p == root || q == root) { + return root; + } + TreeNode left = lowestCommonAncestor(root.left, p, q); + TreeNode right = lowestCommonAncestor(root.right, p, q); + if (left != null && right != null) { + return root; + } + return left != null ? left : right; + } + } + + public static class Solution2 { + /* + * This still checks nodes existence. + */ + int found = 0; + + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + TreeNode lca = lca(root, p, q); + return found == 2 ? lca : null; + } + + private TreeNode lca(TreeNode root, TreeNode p, TreeNode q) { + if (root == null) { + return null; + } + TreeNode left = lca(root.left, p, q); + TreeNode right = lca(root.right, p, q); + if (root == p || root == q) { + found++; + return root; + } + return (left != null && right != null) ? root : left != null ? left : right; + } + } + + public static class Solution3 { + /* + * Credit: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/solutions/944963/beat-96-recursion-without-count-easy-understanding/ + */ + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root == null || p == null || q == null) { + return null; + } + TreeNode result = findLCA(root, p, q); + if (result == p) { + // if p equals result, we'll check the existence of q in the subtree of p + return findLCA(p, q, q) != null ? result : null; + } else if (result == q) { + // if q equals result, we'll check the existence of p in the subtree of q + return findLCA(q, p, p) != null ? result : null; + } + // otherwise, it's this case: (p != result && q != result) || result == null + return result; + } + + private TreeNode findLCA(TreeNode root, TreeNode p, TreeNode q) { + if (root == null || p == root || q == root) { + return root; + } + TreeNode left = findLCA(root.left, p, q); + TreeNode right = findLCA(root.right, p, q); + if (left != null && right != null) { + return root; + } + return left != null ? left : right; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1646.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1646.java new file mode 100644 index 0000000000..e331a4dfe8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1646.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1646 { + public static class Solution1 { + public int getMaximumGenerated(int n) { + if (n == 0) { + return 0; + } + int[] nums = new int[n + 1]; + nums[0] = 0; + nums[1] = 1; + int max = 1; + for (int i = 1; i <= n / 2; i++) { + nums[(i * 2)] = nums[i]; + max = Math.max(max, nums[i]); + if ((i * 2) + 1 <= n) { + nums[(i * 2) + 1] = nums[i] + nums[i + 1]; + max = Math.max(max, nums[(i * 2) + 1]); + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1650.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1650.java new file mode 100644 index 0000000000..d4924752bc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1650.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _1650 { + class Node { + public int val; + public Node parent; + public Node left; + public Node right; + + public Node(int val) { + this.val = val; + } + } + + public static class Solution1 { + public Node lowestCommonAncestor(Node p, Node q) { + Node a = p; + Node b = q; + while (a != b) { + a = a == null ? p : a.parent; + b = b == null ? q : b.parent; + } + return a; + } + } + + public static class Solution2 { + public Node lowestCommonAncestor(Node p, Node q) { + Set set = new HashSet<>(); + while (p != null) { + set.add(p); + p = p.parent; + } + while (q != null) { + if (set.contains(q)) { + return q; + } + q = q.parent; + } + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1652.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1652.java new file mode 100644 index 0000000000..1323f27e75 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1652.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1652 { + public static class Solution1 { + public int[] decrypt(int[] code, int k) { + int[] result = new int[code.length]; + int len = code.length; + if (k == 0) { + for (int i = 0; i < code.length; i++) { + result[i] = 0; + } + } else if (k > 0) { + int kSum = 0; + for (int i = 1; i <= k; i++) { + kSum += code[i]; + } + result[0] = kSum; + for (int i = 1; i < len; i++) { + kSum -= code[i]; + kSum += code[(i + k) % len]; + result[i] = kSum; + } + } else { + int kSum = 0; + int kVal = Math.abs(k); + for (int i = len - 1; i >= len - kVal; i--) { + kSum += code[i]; + } + result[0] = kSum; + for (int i = 1; i < len; i++) { + kSum -= code[(len - kVal + i - 1) % len]; + kSum += code[i - 1]; + result[i] = kSum; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1653.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1653.java new file mode 100644 index 0000000000..d285aa5587 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1653.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _1653 { + public static class Solution1 { + public int minimumDeletions(String s) { + int[][] dp = new int[s.length()][2]; + int count = 0; + // we count the number of 'b's to the left of each index + for (int i = 0; i < s.length(); i++) { + dp[i][0] = count; + if (s.charAt(i) == 'b') { + count++; + } + } + count = 0; + // now we count the number of 'a's to the left of each index + for (int i = s.length() - 1; i >= 0; i--) { + dp[i][1] = count; + if (s.charAt(i) == 'a') { + count++; + } + } + int deletions = s.length(); + // we can balance the string by deleting all 'b's to the left and all 'a's to the right + // at each index + for (int i = 0; i < s.length(); i++) { + deletions = Math.min(deletions, dp[i][0] + dp[i][1]); + } + return deletions; + } + } + + public static class Solution2 { + /* + * use stack + * whenever we encounter a "ba" pair, we increase deletions count by one + */ + public int minimumDeletions(String s) { + Deque stack = new LinkedList<>(); + int deletions = 0; + for (char c : s.toCharArray()) { + if (!stack.isEmpty() && stack.peekLast() == 'b' && c == 'a') { + stack.pollLast(); + deletions++; + } else { + stack.addLast(c); + } + } + return deletions; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1656.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1656.java new file mode 100644 index 0000000000..168d88b6db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1656.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _1656 { + public static class Solution1 { + public static class OrderedStream { + TreeMap map; + int ptr; + int limit; + + public OrderedStream(int n) { + this.map = new TreeMap<>(); + this.ptr = 1; + this.limit = n; + } + + public List insert(int id, String value) { + map.put(id, value); + List result = new ArrayList<>(); + if (map.containsKey(ptr)) { + for (int key = ptr; key <= limit; key++) { + if (map.containsKey(key)) { + result.add(map.get(key)); + } else { + break; + } + } + int i = id; + while (map.containsKey(i)) { + i++; + } + ptr = i; + } + return result; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1657.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1657.java new file mode 100644 index 0000000000..f17563c4a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1657.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _1657 { + public static class Solution1 { + public boolean closeStrings(String word1, String word2) { + int[] counts1 = new int[26]; + int[] counts2 = new int[26]; + Set set1 = new HashSet<>(); + Set set2 = new HashSet<>(); + for (char c : word1.toCharArray()) { + counts1[c - 'a']++; + set1.add(c); + } + Arrays.sort(counts1); + for (char c : word2.toCharArray()) { + counts2[c - 'a']++; + set2.add(c); + } + Arrays.sort(counts2); + return set1.equals(set2) && Arrays.equals(counts1, counts2); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1658.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1658.java new file mode 100644 index 0000000000..7a04046934 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1658.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1658 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/936074/JavaPython-3-Sliding-window%3A-Longest-subarray-sum-to-the-target-sum(nums)-x. + */ + public int minOperations(int[] nums, int x) { + int sum = 0; + for (int n : nums) { + sum += n; + } + int target = sum - x; + int len = nums.length; + int size = Integer.MIN_VALUE; + for (int left = -1, right = 0, windowSum = 0; right < len; right++) { + windowSum += nums[right]; + while (left + 1 < len && windowSum > target) { + left++; + windowSum -= nums[left]; + } + if (windowSum == target) { + size = Math.max(size, right - left); + } + } + return size < 0 ? -1 : len - size; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1660.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1660.java new file mode 100644 index 0000000000..b5b9e7da66 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1660.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; + +public class _1660 { + public static class Solution1 { + /* + * First off, this problem description is confusing. + * Second, that aside, I learned a cool technique to pass TreeNode[]{node, nodeParent} into the queue + * so that you can easily reference one node's parent without building an additional hashmap. + * Third, there's no easy way to write unit tests for this problem... + */ + public TreeNode correctBinaryTree(TreeNode root) { + Queue q = new LinkedList<>(); + q.offer(new TreeNode[] {root, null}); + Set visited = new HashSet<>(); + visited.add(root.val); + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + TreeNode[] curr = q.poll(); + TreeNode node = curr[0]; + TreeNode nodeParent = curr[1]; + if (node.right != null && visited.contains(node.right.val)) { + if (nodeParent.left == node) { + nodeParent.left = null; + } else { + nodeParent.right = null; + } + return root; + } + if (node.left != null) { + q.offer(new TreeNode[] {node.left, node}); + visited.add(node.left.val); + } + if (node.right != null) { + q.offer(new TreeNode[] {node.right, node}); + visited.add(node.right.val); + } + } + } + return root; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1662.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1662.java new file mode 100644 index 0000000000..eac23fcd82 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1662.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1662 { + public static class Solution1 { + public boolean arrayStringsAreEqual(String[] word1, String[] word2) { + StringBuilder sb1 = new StringBuilder(); + for (String word : word1) { + sb1.append(word); + } + StringBuilder sb2 = new StringBuilder(); + for (String word : word2) { + sb2.append(word); + } + return sb1.toString().equals(sb2.toString()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1663.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1663.java new file mode 100644 index 0000000000..39d0bb3eb2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1663.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1663 { + public static class Solution1 { + public String getSmallestString(int n, int k) { + StringBuilder sb = new StringBuilder(); + int balance = k - n; + while (balance > 0) { + if (balance > 25) { + balance -= 25; + sb.append("z"); + n--; + } else { + char str = (char) ('a' + balance); + sb.append(str); + n--; + while (n > 0) { + sb.append("a"); + n--; + } + break; + } + } + return sb.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1668.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1668.java new file mode 100644 index 0000000000..4af45f5560 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1668.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1668 { + public static class Solution1 { + public int maxRepeating(String sequence, String word) { + StringBuilder sb = new StringBuilder(word); + int times = 0; + while (sequence.indexOf(sb.toString()) != -1) { + times++; + sb.append(word); + } + return times; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1669.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1669.java new file mode 100644 index 0000000000..738b36bc79 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1669.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.ListNode; + +public class _1669 { + public static class Solution1 { + public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) { + + ListNode pre = new ListNode(-1); + ListNode list1Temp = list1; + pre.next = list1Temp; + b -= a; + while (a > 1) { + list1Temp = list1Temp.next; + a--; + } + ListNode tail = list1Temp.next; + list1Temp.next = list2; + while (b > 0) { + tail = tail.next; + b--; + } + int length = 0; + ListNode temp2 = list2; + while (temp2 != null) { + temp2 = temp2.next; + length++; + } + while (length > 0) { + list1Temp = list1Temp.next; + length--; + } + list1Temp.next = tail.next; + return pre.next; + } + } + + public static class Solution2 { + public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) { + ListNode endList = list1; + ListNode startList = null; + + for (int i = 0; i < b; i++, endList = endList.next) { + if (i == a - 1) { + startList = endList; + } + } + // Connect the startList.next to list2 + startList.next = list2; + while (list2.next != null) { + list2 = list2.next; + } + list2.next = endList.next; + endList.next = null; + return list1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1670.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1670.java new file mode 100644 index 0000000000..bd520d3658 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1670.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1670 { + public static class Solution1 { + /* + * This is a brute force approach. + * TODO: use two Deques to implement a solution. + */ + public static class FrontMiddleBackQueue { + List list; + + public FrontMiddleBackQueue() { + list = new ArrayList<>(); + } + + public void pushFront(int val) { + list.add(0, val); + } + + public void pushMiddle(int val) { + list.add(list.size() / 2, val); + } + + public void pushBack(int val) { + list.add(val); + } + + public int popFront() { + if (list.size() > 0) { + return list.remove(0); + } + return -1; + } + + public int popMiddle() { + if (list.size() > 0) { + return list.remove( + list.size() % 2 == 0 ? list.size() / 2 - 1 : list.size() / 2); + } + return -1; + } + + public int popBack() { + if (list.size() > 0) { + return list.remove(list.size() - 1); + } + return -1; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1672.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1672.java new file mode 100644 index 0000000000..de01739fc1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1672.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1672 { + public static class Solution1 { + public int maximumWealth(int[][] accounts) { + int m = accounts.length; + int n = accounts[0].length; + int max = 0; + for (int i = 0; i < m; i++) { + int sum = 0; + for (int j = 0; j < n; j++) { + sum += accounts[i][j]; + } + max = Math.max(max, sum); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1673.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1673.java new file mode 100644 index 0000000000..44532c1c6a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1673.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Stack; + +public class _1673 { + public static class Solution1 { + public int[] mostCompetitive(int[] nums, int k) { + Stack stack = new Stack<>(); + for (int i = 0; i < nums.length; i++) { + while (!stack.isEmpty() + && nums[i] < stack.peek() + && nums.length - i + stack.size() > k) { + stack.pop(); + } + if (stack.size() < k) { + stack.push(nums[i]); + } + } + int[] result = new int[k]; + for (int i = k - 1; i >= 0; i--) { + result[i] = stack.pop(); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1675.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1675.java new file mode 100644 index 0000000000..06fb8943df --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1675.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.TreeSet; + +public class _1675 { + public static class Solution1 { + public int minimumDeviation(int[] nums) { + TreeSet treeSet = new TreeSet<>(); + for (int num : nums) { + if (num % 2 == 1) { + treeSet.add(num * 2); + } else { + treeSet.add(num); + } + } + int minDev = treeSet.last() - treeSet.first(); + while (treeSet.last() % 2 == 0) { + treeSet.add(treeSet.last() / 2); + treeSet.remove(treeSet.last()); + minDev = Math.min(minDev, treeSet.last() - treeSet.first()); + } + return minDev; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1676.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1676.java new file mode 100644 index 0000000000..7dba492077 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1676.java @@ -0,0 +1,67 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashSet; +import java.util.Set; + +public class _1676 { + public static class Solution1 { + /* + * Since there are conditions for this problem: all values in the tree and given nodes are unique, + * we could simply use a HashSet to track the number of nodes we've found so far during the traversal. + *

+ * credit: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iv/discuss/958833/java-100 + */ + TreeNode lca = null; + + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) { + Set target = new HashSet<>(); + for (TreeNode node : nodes) { + target.add(node.val); + } + dfs(root, target); + return lca; + } + + private int dfs(TreeNode root, Set target) { + if (root == null) { + return 0; + } + int leftCount = dfs(root.left, target); + int rightCount = dfs(root.right, target); + int foundCount = leftCount + rightCount; + if (target.contains(root.val)) { + foundCount++; + } + if (foundCount == target.size() && lca == null) { + lca = root; + } + return foundCount; + } + } + + public static class Solution2 { + /* + * Silly brute force way. + */ + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) { + TreeNode ans = nodes[0]; + for (int i = 1; i < nodes.length; i++) { + ans = lca(root, ans, nodes[i]); + } + return ans; + } + + private TreeNode lca(TreeNode root, TreeNode p, TreeNode q) { + if (root == null || root == p || root == q) { + return root; + } + TreeNode left = lca(root.left, p, q); + TreeNode right = lca(root.right, p, q); + if (left != null && right != null) { + return root; + } + return left != null ? left : right; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1678.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1678.java new file mode 100644 index 0000000000..b08658e11d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1678.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1678 { + public static class Solution1 { + public String interpret(String command) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < command.length(); i++) { + if (command.charAt(i) == 'G') { + sb.append("G"); + } else if (command.charAt(i) == '(' && command.charAt(i + 1) == ')') { + sb.append("o"); + i++; + } else if (command.charAt(i) == '(' && command.charAt(i + 1) == 'a') { + sb.append("al"); + i += 3; + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1679.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1679.java new file mode 100644 index 0000000000..2321333375 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1679.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1679 { + public static class Solution1 { + public int maxOperations(int[] nums, int k) { + Map map = new HashMap<>(); + int ops = 0; + for (int num : nums) { + if (map.containsKey(k - num)) { + map.put(k - num, map.get(k - num) - 1); + ops++; + if (map.get(k - num) == 0) { + map.remove(k - num); + } + } else { + map.put(num, map.getOrDefault(num, 0) + 1); + } + } + return ops; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1680.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1680.java new file mode 100644 index 0000000000..51d092d5ef --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1680.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1680 { + public static class Solution1 { + public int concatenatedBinary(int n) { + final int MOD = 1000000007; + int result = 0; + for (int i = 1; i <= n; i++) { + String binary = Integer.toBinaryString(i); + for (int j = 0; j < binary.length(); j++) { + result = (result * 2 + (binary.charAt(j) - '0')) % MOD; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1684.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1684.java new file mode 100644 index 0000000000..cdf8fde52c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1684.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _1684 { + public static class Solution1 { + public int countConsistentStrings(String allowed, String[] words) { + Set set = new HashSet<>(); + for (char c : allowed.toCharArray()) { + set.add(c); + } + int count = 0; + for (String word : words) { + boolean isConsistent = true; + for (char c : word.toCharArray()) { + if (!set.contains(c)) { + isConsistent = false; + break; + } + } + if (isConsistent) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1685.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1685.java new file mode 100644 index 0000000000..9689b5bdb3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1685.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1685 { + public static class Solution1 { + public int[] getSumAbsoluteDifferences(int[] nums) { + int len = nums.length; + int[] preSums = new int[len]; + for (int i = 1; i < len; i++) { + preSums[i] = preSums[i - 1] + nums[i - 1]; + } + int[] postSums = new int[len]; + for (int i = len - 2; i >= 0; i--) { + postSums[i] = postSums[i + 1] + nums[i + 1]; + } + int[] result = new int[len]; + for (int i = 0; i < len; i++) { + result[i] = nums[i] * i - preSums[i] + postSums[i] - nums[i] * (len - i - 1); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1686.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1686.java new file mode 100644 index 0000000000..ee553e30f9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1686.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1686 { + public static class Solution1 { + /* + * 1. The most optimal strategy for each player is take the stone with the currently max combined value instead of just his own max value + * because when they take the stone, it also removes the same stone from the other player, ending the best situation for them; + * 2. Both players would stick to the above strategy since it's the best for themselves and they are playing optimally. + */ + public int stoneGameVI(int[] aliceValues, int[] bobValues) { + PriorityQueue maxHeap = + new PriorityQueue<>((a, b) -> a[0] - b[0] == 0 ? a[1] - b[1] : b[0] - a[0]); + for (int i = 0; i < aliceValues.length; i++) { + maxHeap.offer(new int[] {aliceValues[i] + bobValues[i], i}); + } + int[] sums = new int[aliceValues.length]; + boolean aliceTurn = true; + while (!maxHeap.isEmpty()) { + int[] curr = maxHeap.poll(); + if (aliceTurn) { + aliceTurn = false; + sums[curr[1]] = aliceValues[curr[1]]; + } else { + aliceTurn = true; + sums[curr[1]] = -bobValues[curr[1]]; + } + } + int sum = 0; + for (int s : sums) { + sum += s; + } + if (sum == 0) { + return 0; + } else if (sum > 0) { + return 1; + } else { + return -1; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1688.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1688.java new file mode 100644 index 0000000000..e002318e5c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1688.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1688 { + public static class Solution1 { + public int numberOfMatches(int n) { + int matches = 0; + while (n > 1) { + if (n % 2 == 0) { + matches += n / 2; + n /= 2; + } else { + matches += (n - 1) / 2; + n = (n + 1) / 2; + } + } + return matches; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1689.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1689.java new file mode 100644 index 0000000000..657c8007a9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1689.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1689 { + public static class Solution1 { + public int minPartitions(String n) { + int max = 0; + for (char c : n.toCharArray()) { + int num = Integer.parseInt(c + ""); + max = Math.max(max, num); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1690.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1690.java new file mode 100644 index 0000000000..c25600cbca --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1690.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1690 { + public static class Solution1 { + + int[] stonesRef; + + int[] prepareSums; + + int[][] maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ = new int[1005][1005]; + + public int stoneGameVII(int[] stones) { + this.stonesRef = stones; + int totalStonesNumber = stones.length; + this.prepareSums = new int[totalStonesNumber + 1]; + for (int i = 1; i <= totalStonesNumber; i++) { + this.prepareSums[i] = this.prepareSums[i - 1] + stones[i - 1]; + } + for (int len = 1; len <= totalStonesNumber; len++) { + for (int i = 1; i + len - 1 <= totalStonesNumber; i++) { + int j = i + len - 1; + this.setMaxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ(i, j); + } + } + return maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[1][totalStonesNumber]; + } + + private void setMaxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ(int i, int j) { + if (j - i == 0) { + maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j] = 0; + } else if (j - i == 1) { + maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j] = + Math.max(stonesRef[i - 1], stonesRef[j - 1]); + } else { + maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j] = + Math.max( + this.sumOfTheStonesValueInPosIToJ(i + 1, j) + - maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i + 1][j], + this.sumOfTheStonesValueInPosIToJ(i, j - 1) + - maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j - 1]); + } + } + + private int sumOfTheStonesValueInPosIToJ(int i, int j) { + return this.prepareSums[j] - this.prepareSums[i - 1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1694.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1694.java new file mode 100644 index 0000000000..7697aed43f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1694.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1694 { + public static class Solution1 { + public String reformatNumber(String number) { + StringBuilder sb = new StringBuilder(); + for (char c : number.toCharArray()) { + if (Character.isDigit(c)) { + sb.append(c); + } + } + String cleaned = sb.toString(); + sb.setLength(0); + for (int i = 0; i < cleaned.length(); ) { + if (i + 4 == cleaned.length()) { + sb.append(cleaned.substring(i, i + 2)); + sb.append("-"); + sb.append(cleaned.substring(i + 2)); + break; + } else if (i + 3 <= cleaned.length()) { + sb.append(cleaned.substring(i, i + 3)); + sb.append("-"); + i += 3; + } else { + sb.append(cleaned.substring(i)); + break; + } + } + if (sb.charAt(sb.length() - 1) == '-') { + sb.setLength(sb.length() - 1); + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1695.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1695.java new file mode 100644 index 0000000000..9c90e0edad --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1695.java @@ -0,0 +1,56 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _1695 { + public static class Solution1 { + public int maximumUniqueSubarray(int[] nums) { + Set set = new HashSet<>(); + int maxSum = 0; + int runningSum = 0; + for (int right = 0, left = 0; right < nums.length; right++) { + if (set.add(nums[right])) { + runningSum += nums[right]; + maxSum = Math.max(maxSum, runningSum); + } else { + while (left < right && set.contains(nums[right])) { + set.remove(nums[left]); + runningSum -= nums[left]; + left++; + } + set.add(nums[right]); + runningSum += nums[right]; + } + } + return maxSum; + } + } + + public static class Solution2 { + /* + * My completely original solution on 10/202/2021. Classic sliding window solution. + */ + public int maximumUniqueSubarray(int[] nums) { + Map map = new HashMap<>(); + int sum = 0; + int start = 0; + int ans = 0; + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i])) { + Integer lastIndex = map.get(nums[i]); + while (start <= lastIndex) { + sum -= nums[start]; + start++; + } + } + sum += nums[i]; + map.put(nums[i], i); + ans = Math.max(ans, sum); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1700.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1700.java new file mode 100644 index 0000000000..2218d4f2f3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1700.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _1700 { + public static class Solution1 { + public int countStudents(int[] students, int[] sandwiches) { + Queue studentsQueue = new LinkedList<>(); + Queue sandwichesQueue = new LinkedList<>(); + for (int i = 0; i < sandwiches.length; i++) { + studentsQueue.add(students[i]); + sandwichesQueue.add(sandwiches[i]); + } + do { + if (!studentsQueue.isEmpty()) { + if (studentsQueue.peek() == sandwichesQueue.peek()) { + studentsQueue.poll(); + sandwichesQueue.poll(); + } else { + if (!studentsQueue.contains(sandwichesQueue.peek())) { + break; + } + studentsQueue.add(studentsQueue.poll()); + } + } + } while (!studentsQueue.isEmpty()); + return studentsQueue.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1701.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1701.java new file mode 100644 index 0000000000..d29b117030 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1701.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1701 { + public static class Solution1 { + /* + * A simple one-pass, just simulate what the problem describes. + */ + public double averageWaitingTime(int[][] customers) { + long totalWaitTime = customers[0][1]; + int chefFinishTime = customers[0][0] + customers[0][1]; + for (int i = 1; i < customers.length; i++) { + int arrival = customers[i][0]; + int prep = customers[i][1]; + if (chefFinishTime < arrival) { + chefFinishTime = arrival; + } + chefFinishTime += prep; + totalWaitTime += chefFinishTime - arrival; + } + return (double) totalWaitTime / customers.length; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1704.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1704.java new file mode 100644 index 0000000000..8bc1d59ae3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1704.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.IntStream; + +public class _1704 { + public static class Solution1 { + public boolean halvesAreAlike(String s) { + Set vowels = + new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')); + int firstHalfVowelsCount = + (int) + IntStream.range(0, s.length() / 2) + .filter(i -> vowels.contains(s.charAt(i))) + .count(); + int secondHalfVowelsCount = + (int) + IntStream.range(s.length() / 2, s.length()) + .filter(i -> vowels.contains(s.charAt(i))) + .count(); + return firstHalfVowelsCount == secondHalfVowelsCount; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1705.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1705.java new file mode 100644 index 0000000000..8f02c0db35 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1705.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1705 { + public static class Solution1 { + public int eatenApples(int[] apples, int[] days) { + /*we sort the heap by its expiration dates, we'll eat the earliest expiration apples first*/ + PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]); + int eatenApples = 0; + for (int i = 0; i < apples.length || !minHeap.isEmpty(); i++) { + if (i < apples.length) { + minHeap.offer(new int[] {i + days[i], apples[i]}); + } + while (!minHeap.isEmpty() && (minHeap.peek()[0] <= i || minHeap.peek()[1] <= 0)) { + minHeap.poll(); + } + if (!minHeap.isEmpty()) { + eatenApples++; + minHeap.peek()[1]--; + } + } + return eatenApples; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1708.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1708.java new file mode 100644 index 0000000000..e821888981 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1708.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1708 { + public static class Solution1 { + public int[] largestSubarray(int[] nums, int k) { + int max = nums[0]; + int beginIndex = 0; + for (int i = 1; i <= nums.length - k; i++) { + if (nums[i] > max) { + max = nums[i]; + beginIndex = i; + } + } + int[] result = new int[k]; + for (int i = beginIndex, j = 0; i < i + k && j < k; i++) { + result[j++] = nums[i]; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1710.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1710.java new file mode 100644 index 0000000000..c7c454b001 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1710.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1710 { + public static class Solution1 { + public int maximumUnits(int[][] boxTypes, int truckSize) { + Arrays.sort(boxTypes, (a, b) -> b[1] - a[1]); + int totalUnits = 0; + int loadedBoxes = 0; + for (int i = 0; i < boxTypes.length; i++) { + int number = boxTypes[i][0]; + while (loadedBoxes < truckSize && number > 0) { + totalUnits += boxTypes[i][1]; + number--; + loadedBoxes++; + } + } + return totalUnits; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1711.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1711.java new file mode 100644 index 0000000000..2807f8aaaf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1711.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1711 { + public static class Solution1 { + /* + * This is a very brilliant solution: + * 1. go through each number only once: for each number, we iterate through all possible power of twos, at max, there's only 21 due to the constraints of this problem; + * 2. since it's asking for the sum of two, we can check we have encountered the other number before using a hashmap + */ + + public int countPairs(int[] deliciousness) { + final long MODULAR = 1000000007; + Map map = new HashMap<>(); + long pairs = 0; + for (int del : deliciousness) { + int power = 1; + // we only need to go up to 21 since one of the constraints is: 0 <= + // deliciousness[i] <= 2 to the power of 20 + for (int j = 0; j < 22; j++) { + if (map.containsKey(power - del)) { + pairs += map.get(power - del); + } + power *= 2; + } + map.put(del, map.getOrDefault(del, 0) + 1); + } + return (int) (pairs % MODULAR); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1716.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1716.java new file mode 100644 index 0000000000..7ab458655c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1716.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1716 { + public static class Solution1 { + public int totalMoney(int n) { + int mondayMoney = 1; + int total = 0; + while (n > 0) { + int weekDays = 0; + int base = mondayMoney; + while (weekDays < 7 && n > 0) { + total += base; + base++; + weekDays++; + n--; + } + mondayMoney++; + } + return total; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1717.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1717.java new file mode 100644 index 0000000000..5bb943be11 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1717.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _1717 { + public static class Solution1 { + public int maximumGain(String s, int x, int y) { + int big = Math.max(x, y); + int small = big == x ? y : x; + char first = big == x ? 'a' : 'b'; + char second = first == 'a' ? 'b' : 'a'; + Deque stack1 = new LinkedList<>(); + int max = 0; + for (char c : s.toCharArray()) { + if (c == second && !stack1.isEmpty() && stack1.peekLast() == first) { + stack1.pollLast(); + max += big; + } else { + stack1.addLast(c); + } + } + Deque stack2 = new LinkedList<>(); + while (!stack1.isEmpty()) { + char c = stack1.pollLast(); + if (!stack2.isEmpty() && c == second && stack2.peekLast() == first) { + max += small; + stack2.pollLast(); + } else { + stack2.addLast(c); + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1718.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1718.java new file mode 100644 index 0000000000..acae688aad --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1718.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1718 { + public static class Solution1 { + public int[] constructDistancedSequence(int n) { + int[] result = new int[n * 2 - 1]; + boolean[] visited = new boolean[n + 1]; + backtracking(0, result, visited, n); + return result; + } + + private boolean backtracking(int index, int[] result, boolean[] visited, int n) { + if (index == result.length) { + return true; + } + if (result[index] != 0) { + return backtracking(index + 1, result, visited, n); + } else { + for (int i = n; i > 0; i--) { + if (visited[i]) { + continue; + } + visited[i] = true; + result[index] = i; + if (i == 1) { + if (backtracking(index + 1, result, visited, n)) { + return true; + } + } else if (index + i < result.length && result[index + i] == 0) { + result[i + index] = i; + if (backtracking(index + 1, result, visited, n)) { + return true; + } + result[index + i] = 0; + } + result[index] = 0; + visited[i] = false; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1720.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1720.java new file mode 100644 index 0000000000..2c13147533 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1720.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1720 { + public static class Solution1 { + public int[] decode(int[] encoded, int first) { + int[] arr = new int[encoded.length + 1]; + arr[0] = first; + for (int i = 0; i < encoded.length; i++) { + arr[i + 1] = encoded[i] ^ arr[i]; + } + return arr; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1721.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1721.java new file mode 100644 index 0000000000..9e86202da1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1721.java @@ -0,0 +1,108 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.List; + +public class _1721 { + public static class Solution1 { + public ListNode swapNodes(ListNode head, int k) { + List list = new ArrayList<>(); + ListNode tmp = head; + while (tmp != null) { + list.add(tmp.val); + tmp = tmp.next; + } + int first = list.get(k - 1); + int size = list.size(); + int second = list.get(size - k); + list.remove(k - 1); + list.add(k - 1, second); + list.remove(size - k); + list.add(size - k, first); + ListNode pre = new ListNode(-1); + tmp = pre; + for (int i = 0; i < list.size(); i++) { + pre.next = new ListNode(list.get(i)); + pre = pre.next; + } + return tmp.next; + } + } + + public static class Solution2 { + public ListNode swapNodes(ListNode head, int k) { + if (head == null || head.next == null) { + return head; + } + + // find length of list + int n = 0; + ListNode current = head; + while (current != null) { + current = current.next; + n++; + } + + int[] nums = new int[n]; + current = head; + int i = 0; + while (current != null) { + nums[i++] = current.val; + current = current.next; + } + int firstIndex; + int secondIndex; + firstIndex = k; + secondIndex = n - k; + int temp = nums[firstIndex - 1]; + nums[firstIndex - 1] = nums[secondIndex]; + nums[secondIndex] = temp; + ListNode dummy = new ListNode(-1); + current = dummy; + for (i = 0; i < n; i++) { + ListNode node = new ListNode(nums[i]); + current.next = node; + current = current.next; + } + return dummy.next; + } + } + + public static class Solution3 { + public ListNode swapNodes(ListNode head, int k) { + // O(n) linear time + /* + 1. Calculate length of linked list + 2. Initialize 3 ptrs, temp1 and temp2 used for pointing to nodes at k, (len - k + 1) + and temp3 used to iterate over the linked list + */ + int length = 0; + int secondIndex; + + ListNode temp1 = null; + ListNode temp2 = null; + ListNode temp3 = head; + while (temp3 != null) { + length++; + temp3 = temp3.next; + } + + secondIndex = length - k + 1; + temp3 = head; + for (int i = 1; i <= length; i++) { + if (i == k) { + temp1 = temp3; + } + if (i == secondIndex) { + temp2 = temp3; + } + temp3 = temp3.next; + } + int value = temp1.val; + temp1.val = temp2.val; + temp2.val = value; + return head; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1725.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1725.java new file mode 100644 index 0000000000..e22fca6142 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1725.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.TreeMap; + +public class _1725 { + public static class Solution1 { + public int countGoodRectangles(int[][] rectangles) { + TreeMap map = new TreeMap<>(); + for (int[] rec : rectangles) { + int min = Math.min(rec[0], rec[1]); + map.put(min, map.getOrDefault(min, 0) + 1); + } + return map.lastEntry().getValue(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1726.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1726.java new file mode 100644 index 0000000000..ca920391f8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1726.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1726 { + public static class Solution1 { + public int tupleSameProduct(int[] nums) { + Map map = new HashMap<>(); + int count = 0; + for (int i = 0; i < nums.length - 1; i++) { + for (int j = i + 1; j < nums.length; j++) { + int product = nums[i] * nums[j]; + count += 8 * map.getOrDefault(product, 0); + map.put(product, map.getOrDefault(product, 0) + 1); + } + } + return count; + } + } + + public static class Solution2 { + public int tupleSameProduct(int[] nums) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length - 1; i++) { + for (int j = i + 1; j < nums.length; j++) { + int product = nums[i] * nums[j]; + map.put(product, map.getOrDefault(product, 0) + 1); + } + } + int count = 0; + for (int key : map.keySet()) { + if (map.get(key) > 1) { + int freq = map.get(key); + count += (freq * (freq - 1) / 2) * 8; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1727.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1727.java new file mode 100644 index 0000000000..3072cc3524 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1727.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1727 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/largest-submatrix-with-rearrangements/discuss/1020682/Java-or-6ms-or-easy-understanding-with-comments-and-images + */ + public int largestSubmatrix(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + for (int i = 1; i < m; i++) { + for (int j = 0; j < n; j++) { + if (matrix[i][j] != 0) { + matrix[i][j] = matrix[i - 1][j] + 1; + } + } + } + int count = 0; + for (int i = 0; i < m; i++) { + Arrays.sort(matrix[i]); + for (int j = 1; j <= n; j++) { + count = Math.max(count, j * matrix[i][n - j]); + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1730.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1730.java new file mode 100644 index 0000000000..cabb871f9b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1730.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; + +public class _1730 { + public static class Solution1 { + public int getFood(char[][] grid) { + int m = grid.length; + int n = grid[0].length; + Queue q = new LinkedList<>(); + Set visited = new HashSet<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == '*') { + q.offer(new int[] {i, j}); + visited.add(i * n + j); + } + } + } + int[] dirs = new int[] {0, 1, 0, -1, 0}; + int steps = 0; + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + int[] curr = q.poll(); + for (int j = 0; j < dirs.length - 1; j++) { + int nextx = curr[0] + dirs[j]; + int nexty = curr[1] + dirs[j + 1]; + if (nextx >= 0 + && nextx < m + && nexty >= 0 + && nexty < n + && visited.add(nextx * n + nexty)) { + if (grid[nextx][nexty] == 'O') { + q.offer(new int[] {nextx, nexty}); + } else if (grid[nextx][nexty] == '#') { + return steps + 1; + } + } + } + } + steps++; + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1732.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1732.java new file mode 100644 index 0000000000..c9d4e4ce57 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1732.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1732 { + public static class Solution1 { + public int largestAltitude(int[] gain) { + int max = 0; + int[] altitudes = new int[gain.length + 1]; + for (int i = 0; i < gain.length; i++) { + altitudes[i + 1] = altitudes[i] + gain[i]; + max = Math.max(max, altitudes[i + 1]); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1733.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1733.java new file mode 100644 index 0000000000..2ffb006914 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1733.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _1733 { + public static class Solution1 { + public int minimumTeachings(int n, int[][] languages, int[][] friendships) { + Map> langMap = new HashMap<>(); + for (int i = 0; i < languages.length; i++) { + Set set = new HashSet<>(); + langMap.put(i + 1, set); + for (int lang : languages[i]) { + set.add(lang); + } + } + boolean[] canCommunicate = new boolean[friendships.length]; + for (int i = 1; i <= n; i++) { + for (int j = 0; j < friendships.length; j++) { + int friend1 = friendships[j][0]; + int friend2 = friendships[j][1]; + if (langMap.get(friend1).contains(i) && langMap.get(friend2).contains(i)) { + canCommunicate[j] = true; + } + } + } + int minTeach = friendships.length; + for (int i = 1; i <= n; i++) { + Set teach = new HashSet<>(); + for (int j = 0; j < friendships.length; j++) { + if (!canCommunicate[j]) { + int friend1 = friendships[j][0]; + int friend2 = friendships[j][1]; + if (!langMap.get(friend1).contains(i)) { + teach.add(friend1); + } + if (!langMap.get(friend2).contains(i)) { + teach.add(friend2); + } + } + } + minTeach = Math.min(minTeach, teach.size()); + } + return minTeach; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1736.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1736.java new file mode 100644 index 0000000000..985c4faa95 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1736.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1736 { + public static class Solution1 { + public String maximumTime(String time) { + StringBuilder sb = new StringBuilder(); + String[] strs = time.split(":"); + String hour = strs[0]; + String min = strs[1]; + if (hour.charAt(0) == '?') { + if (hour.charAt(1) == '?') { + sb.append("23"); + } else if (hour.charAt(1) > '3') { + sb.append("1"); + sb.append(hour.charAt(1)); + } else { + sb.append("2"); + sb.append(hour.charAt(1)); + } + } else if (hour.charAt(0) == '0' || hour.charAt(0) == '1') { + if (hour.charAt(1) == '?') { + sb.append(hour.charAt(0)); + sb.append("9"); + } else { + sb.append(hour); + } + } else if (hour.charAt(0) == '2') { + if (hour.charAt(1) == '?') { + sb.append("23"); + } else { + sb.append(hour); + } + } + sb.append(":"); + if (min.charAt(0) == '?') { + if (min.charAt(1) == '?') { + sb.append("59"); + } else { + sb.append("5"); + sb.append(min.charAt(1)); + } + return sb.toString(); + } + sb.append(min.charAt(0)); + if (min.charAt(1) == '?') { + sb.append("9"); + } else { + sb.append(min.charAt(1)); + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1740.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1740.java new file mode 100644 index 0000000000..035d237e7b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1740.java @@ -0,0 +1,76 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.*; + +public class _1740 { + public static class Solution1 { + /* + * My completely original solution on 6/30/2024. + */ + public int findDistance(TreeNode root, int p, int q) { + // dfs to find either p or q first, then add it into a queue, also form a child to + // parent mapping + Queue queue = new LinkedList<>(); + Map childToParent = new HashMap<>(); + dfs(root, p, q, queue, childToParent); + int target = queue.peek().val == p ? q : p; + int distance = 0; + Set visited = + new HashSet<>(); // this visited collection is often very essential to prevent + // infinite loop. + visited.add(queue.peek().val); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + if (curr == null) { + continue; + } + if (curr.val == target) { + return distance; + } + if (curr.left != null && visited.add(curr.left.val)) { + queue.offer(curr.left); + } + if (curr.right != null && visited.add(curr.right.val)) { + queue.offer(curr.right); + } + if (childToParent.containsKey(curr) + && visited.add(childToParent.get(curr).val)) { + queue.offer(childToParent.get(curr)); + } + } + distance++; + } + return distance; + } + + private void dfs( + TreeNode root, + int p, + int q, + Queue queue, + Map childToParent) { + if (root == null) { + return; + } + if (root.val == p || root.val == q) { + if (queue.isEmpty()) { + queue.offer(root); + } + } + + if (root.left != null) { + childToParent.put(root.left, root); + } + dfs(root.left, p, q, queue, childToParent); + + if (root.right != null) { + childToParent.put(root.right, root); + } + + dfs(root.right, p, q, queue, childToParent); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1742.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1742.java new file mode 100644 index 0000000000..1eaa3591db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1742.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1742 { + public static class Solution1 { + public int countBalls(int lowLimit, int highLimit) { + Map map = new HashMap<>(); + for (int limit = lowLimit, i = 0; limit <= highLimit; limit++, i++) { + int number = 0; + int tmp = limit; + do { + number += tmp % 10; + tmp /= 10; + } while (tmp != 0); + map.put(number, map.getOrDefault(number, 0) + 1); + } + int maxBalls = 0; + for (int c : map.keySet()) { + maxBalls = Math.max(maxBalls, map.get(c)); + } + return maxBalls; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1743.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1743.java new file mode 100644 index 0000000000..baa5cf663f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1743.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class _1743 { + public static class Solution1 { + public int[] restoreArray(int[][] adjacentPairs) { + Map map = new HashMap<>(); + Map> map2 = new HashMap<>(); + for (int[] pair : adjacentPairs) { + int num1 = pair[0]; + int num2 = pair[1]; + map.put(num1, map.getOrDefault(num1, 0) + 1); + map.put(num2, map.getOrDefault(num2, 0) + 1); + if (!map2.containsKey(num1)) { + map2.put(num1, new ArrayList<>()); + } + map2.get(num1).add(num2); + if (!map2.containsKey(num2)) { + map2.put(num2, new ArrayList<>()); + } + map2.get(num2).add(num1); + } + int first = Integer.MAX_VALUE; + for (int key : map.keySet()) { + if (map.get(key) == 1) { + first = key; + break; + } + } + int[] original = new int[map.size()]; + Set set = new HashSet<>(); + original[0] = first; + set.add(first); + for (int i = 1; i < map.size(); i++) { + int prev = original[i - 1]; + List connected = map2.get(prev); + for (int conn : connected) { + if (set.add(conn)) { + original[i] = conn; + break; + } + } + } + return original; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1745.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1745.java new file mode 100644 index 0000000000..09d8f787a1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1745.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1745 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/palindrome-partitioning-iv/discuss/1042910/Java-Detailed-Explanation-DP-O(N2) + * + * check whether substring(i, j) is a palindrome becomes checking whether substring(i + 1, j -1) is a palindrome + * + * How we build the dp array: + * start from the top right of this square matrix + */ + public boolean checkPartitioning(String s) { + int n = s.length(); + boolean[][] dp = new boolean[n][n]; + for (int i = n - 1; i >= 0; i--) { + for (int j = i; j < n; j++) { + if (s.charAt(i) == s.charAt(j)) { + dp[i][j] = (i + 1 <= j - 1) ? dp[i + 1][j - 1] : true; + } else { + dp[i][j] = false; + } + } + } + for (int i = 1; i < n - 1; i++) { + for (int j = i; j < n - 1; j++) { + if (dp[0][i - 1] && dp[i][j] && dp[j + 1][n - 1]) { + return true; + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1746.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1746.java new file mode 100644 index 0000000000..2e3c223f21 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1746.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1746 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/discuss/1049224/Java-O(n)-Time-O(n)-Space-DP-solution + */ + public int maxSumAfterOperation(int[] nums) { + int len = nums.length; + // dp[i][0] means the sum of all elements in the subarray up to index i without any + // number squared + // dp[i][1] means the sum of all elements in the subarray up to index i with nums[i] + // squared + // esentially, there are three dimensions: + // 1. the element nums[i] squared itself might be the biggest sum of subarray itself; + // 2. the subarray sum without any elemtns squared + nums[i] squared + // 3. the subarray sum with one element prior to i square + nums[i] + int[][] dp = new int[len][2]; + dp[0][0] = nums[0]; + dp[0][1] = nums[0] * nums[0]; + int maxSum = dp[0][1]; + for (int i = 1; i < len; i++) { + dp[i][0] = Math.max(dp[i - 1][0] + nums[i], nums[i]); + dp[i][1] = + Math.max( + nums[i] * nums[i], + Math.max(dp[i - 1][0] + nums[i] * nums[i], dp[i - 1][1] + nums[i])); + maxSum = Math.max(maxSum, dp[i][1]); + } + return maxSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1748.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1748.java new file mode 100644 index 0000000000..49731095e1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1748.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1748 { + public static class Solution1 { + public int sumOfUnique(int[] nums) { + Map map = new HashMap<>(); + int sum = 0; + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + for (int num : map.keySet()) { + if (map.get(num) == 1) { + sum += num; + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1749.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1749.java new file mode 100644 index 0000000000..56fe18d5fc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1749.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1749 { + public static class Solution1 { + public int maxAbsoluteSum(int[] nums) { + int min = 0; + int max = 0; + int s = 0; + for (int num : nums) { + s += num; + min = Math.min(min, s); + max = Math.max(max, s); + } + return max - min; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1750.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1750.java new file mode 100644 index 0000000000..fd7c617945 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1750.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1750 { + public static class Solution1 { + public int minimumLength(String s) { + int i = 0; + int j = s.length() - 1; + if (s.charAt(i) == s.charAt(j)) { + while (i < j && s.charAt(i) == s.charAt(j)) { + char c = s.charAt(i); + i++; + while (c == s.charAt(i) && i < j) { + i++; + } + j--; + while (c == s.charAt(j) && i < j) { + j--; + } + } + } + return i <= j ? s.substring(i, j).length() + 1 : 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1752.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1752.java new file mode 100644 index 0000000000..7df74c21c1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1752.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1752 { + public static class Solution1 { + public boolean check(int[] nums) { + int[] copy = Arrays.copyOf(nums, nums.length); + Arrays.sort(copy); + for (int i = 1; i <= nums.length; i++) { + int[] rotated = rotate(nums, i); + if (Arrays.equals(rotated, copy)) { + return true; + } + } + return false; + } + + private int[] rotate(int[] nums, int start) { + int[] rotated = new int[nums.length]; + int j = 0; + for (int i = start; i < nums.length; i++, j++) { + rotated[j] = nums[i]; + } + for (int i = 0; i < start; i++) { + rotated[j++] = nums[i]; + } + return rotated; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1753.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1753.java new file mode 100644 index 0000000000..ebd3aa06ed --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1753.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1753 { + public static class Solution1 { + public int maximumScore(int a, int b, int c) { + int[] nums = new int[] {a, b, c}; + Arrays.sort(nums); + if (nums[0] + nums[1] < nums[2]) { + return nums[0] + nums[1]; + } else { + return (nums[0] + nums[1] + nums[2]) / 2; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1754.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1754.java new file mode 100644 index 0000000000..cef97d0b62 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1754.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1754 { + public static class Solution1 { + public String largestMerge(String word1, String word2) { + int i = 0; + int j = 0; + StringBuilder sb = new StringBuilder(); + while (i < word1.length() && j < word2.length()) { + if (word1.substring(i).compareTo(word2.substring(j)) > 0) { + sb.append(word1.charAt(i++)); + } else { + sb.append(word2.charAt(j++)); + } + } + while (i < word1.length()) { + sb.append(word1.charAt(i++)); + } + while (j < word2.length()) { + sb.append(word2.charAt(j++)); + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1756.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1756.java new file mode 100644 index 0000000000..f980b6ccb0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1756.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1756 { + public static class Solution1 { + class MRUQueue { + List list; + + public MRUQueue(int n) { + list = new ArrayList(n); + for (int i = 1; i <= n; i++) { + list.add(i); + } + } + + public int fetch(int k) { + int fetched = list.remove(k - 1); + list.add(fetched); + return fetched; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1758.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1758.java new file mode 100644 index 0000000000..8e17380493 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1758.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1758 { + public static class Solution1 { + public int minOperations(String s) { + int ops1 = 0; + // start with 0 + boolean isZero = true; + for (int i = 0; i < s.length(); i++) { + if (i % 2 == 0) { + // should be zero, if not, change it to zero and increase ops1 by one + if (s.charAt(i) != '0') { + ops1++; + } + } else { + // should be one, if not, increase ops1 by one + if (s.charAt(i) != '1') { + ops1++; + } + } + } + + // start with 1 + int ops2 = 0; + for (int i = 0; i < s.length(); i++) { + if (i % 2 == 0) { + // should be one, if not, increase ops2 by one + if (s.charAt(i) != '1') { + ops2++; + } + } else { + if (s.charAt(i) != '0') { + ops2++; + } + } + } + return Math.min(ops1, ops2); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1759.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1759.java new file mode 100644 index 0000000000..cbd639a837 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1759.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1759 { + public static class Solution1 { + /* + * a -> 1 + * aa -> 3 + * aaa -> 6 + * aaaa -> 10 + * aaaaa -> 15 + */ + public int countHomogenous(String s) { + int total = 0; + int count = 0; + for (int i = 0; i < s.length(); i++) { + if (i > 0 && s.charAt(i) == s.charAt(i - 1)) { + count++; + } else { + count = 1; + } + total = (total + count) % 1000000007; + } + return total; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1762.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1762.java new file mode 100644 index 0000000000..79dbcae64a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1762.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1762 { + public static class Solution1 { + public int[] findBuildings(int[] heights) { + List list = new ArrayList(); + int len = heights.length; + int higher = heights[len - 1]; + list.add(len - 1); + for (int i = len - 2; i >= 0; i--) { + if (heights[i] > higher) { + higher = heights[i]; + list.add(i); + } + } + int[] res = new int[list.size()]; + for (int i = list.size() - 1, j = 0; i >= 0; i--, j++) { + res[j] = list.get(i); + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1763.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1763.java new file mode 100644 index 0000000000..99f5a7e7f9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1763.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1763 { + public static class Solution1 { + public String longestNiceSubstring(String s) { + String longest = ""; + for (int i = 0; i < s.length() - 1; i++) { + for (int j = i; j <= s.length(); j++) { + if (isNiceString(s.substring(i, j))) { + if (longest.length() < j - i) { + longest = s.substring(i, j); + } + } + } + } + return longest; + } + + private boolean isNiceString(String str) { + int[] uppercount = new int[26]; + int[] lowercount = new int[26]; + for (char c : str.toCharArray()) { + if (Character.isUpperCase(c)) { + uppercount[Character.toLowerCase(c) - 'a']++; + } else { + lowercount[c - 'a']++; + } + } + for (int i = 0; i < uppercount.length; i++) { + if (uppercount[i] > 0 && lowercount[i] > 0 + || (uppercount[i] == 0 && lowercount[i] == 0)) { + continue; + } else { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1764.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1764.java new file mode 100644 index 0000000000..dd40c1da58 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1764.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _1764 { + public static class Solution1 { + public boolean canChoose(int[][] groups, int[] nums) { + int n = groups.length; + List numsInt = new ArrayList<>(); + for (int num : nums) { + numsInt.add(num); + } + int prevIndex = 0; + for (int i = 0; i < n; i++) { + int[] group = groups[i]; + List groupInt = new ArrayList<>(); + for (int num : group) { + groupInt.add(num); + } + int index = + Collections.indexOfSubList( + numsInt.subList(prevIndex, numsInt.size()), groupInt); + if (index != -1) { + prevIndex = index + group.length; + } else { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1765.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1765.java new file mode 100644 index 0000000000..3f0f1fab20 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1765.java @@ -0,0 +1,51 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _1765 { + public static class Solution1 { + public int[][] highestPeak(int[][] isWater) { + int m = isWater.length; + int n = isWater[0].length; + int[][] result = new int[m][n]; + Queue queue = new LinkedList<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (isWater[i][j] == 1) { + queue.offer(new int[] {i, j}); + } + } + } + int[] directions = new int[] {0, 1, 0, -1, 0}; + int height = 1; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int j = 0; j < size; j++) { + int[] curr = queue.poll(); + for (int i = 0; i < directions.length - 1; i++) { + int newx = directions[i] + curr[0]; + int newy = directions[i + 1] + curr[1]; + if (newx >= 0 + && newx < m + && newy >= 0 + && newy < n + && result[newx][newy] == 0) { + result[newx][newy] = height; + queue.offer(new int[] {newx, newy}); + } + } + } + height++; + } + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (isWater[i][j] == 1) { + result[i][j] = 0; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1768.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1768.java new file mode 100644 index 0000000000..63ebf22083 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1768.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1768 { + public static class Solution1 { + public String mergeAlternately(String word1, String word2) { + StringBuilder sb = new StringBuilder(); + int i = 0; + int j = 0; + for (; i < word1.length() && j < word2.length(); ) { + sb.append(word1.charAt(i++)); + sb.append(word2.charAt(j++)); + } + while (i < word1.length()) { + sb.append(word1.charAt(i++)); + } + while (j < word2.length()) { + sb.append(word2.charAt(j++)); + } + return sb.toString(); + } + } + + public static class Solution2 { + public String mergeAlternately(String word1, String word2) { + int len1 = word1.length(); + int len2 = word2.length(); + StringBuilder sb = new StringBuilder(); + int diffLen = Math.min(len1, len2); + int i; + for (i = 0; i < diffLen; i++) { + sb.append(word1.charAt(i)); + sb.append(word2.charAt(i)); + } + if (i >= len1) { + sb.append(word2.substring(i)); + } else { + sb.append(word1.substring(i)); + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1769.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1769.java new file mode 100644 index 0000000000..ca06dff832 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1769.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1769 { + public static class Solution1 { + public int[] minOperations(String boxes) { + int[] box = new int[boxes.length()]; + for (int i = 0; i < boxes.length(); i++) { + box[i] = boxes.charAt(i) - '0'; + } + int[] result = new int[boxes.length()]; + for (int i = 0; i < boxes.length(); i++) { + int ops = 0; + for (int j = 0; j < boxes.length(); j++) { + if (i != j && box[j] == 1) { + ops += Math.abs(j - i); + } + } + result[i] = ops; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1772.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1772.java new file mode 100644 index 0000000000..b7af9c1347 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1772.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Set; + +public class _1772 { + public static class Solution1 { + public String[] sortFeatures(String[] features, String[] responses) { + Map map = new HashMap<>(); + for (int i = 0; i < features.length; i++) { + map.put(features[i], i); + } + Map countMap = new HashMap<>(); + for (String response : responses) { + Set strs = new HashSet(Arrays.asList(response.split(" "))); + for (String str : strs) { + if (map.containsKey(str)) { + countMap.put(str, countMap.getOrDefault(str, 0) + 1); + } + } + } + PriorityQueue maxHeap = + new PriorityQueue<>( + (a, b) -> a.freq != b.freq ? b.freq - a.freq : a.index - b.index); + for (String key : map.keySet()) { + maxHeap.offer(new Node(key, countMap.getOrDefault(key, 0), map.get(key))); + } + String[] result = new String[features.length]; + int i = 0; + while (!maxHeap.isEmpty()) { + result[i++] = maxHeap.poll().word; + } + return result; + } + + class Node { + String word; + Integer freq; + Integer index; + + public Node(String word, Integer freq, Integer index) { + this.word = word; + this.freq = freq; + this.index = index; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1773.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1773.java new file mode 100644 index 0000000000..f698f1a3fd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1773.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.List; + +public class _1773 { + public static class Solution1 { + public int countMatches(List> items, String ruleKey, String ruleValue) { + int match = 0; + for (List item : items) { + if (ruleKey.equals("type") && item.get(0).equals(ruleValue)) { + match++; + } else if (ruleKey.equals("color") && item.get(1).equals(ruleValue)) { + match++; + } else if (ruleKey.equals("name") && item.get(2).equals(ruleValue)) { + match++; + } + } + return match; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1774.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1774.java new file mode 100644 index 0000000000..e81956f238 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1774.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1774 { + public static class Solution1 { + int result = 0; + + public int closestCost(int[] baseCosts, int[] toppingCosts, int target) { + result = baseCosts[0]; + for (int i = 0; i < baseCosts.length; i++) { + recursion(baseCosts[i], toppingCosts, 0, target); + } + return result; + } + + private void recursion(int currentCost, int[] toppingCosts, int index, int target) { + if (Math.abs(currentCost - target) < Math.abs(result - target) + || (Math.abs(currentCost - target) < Math.abs(result - target) + && currentCost == result)) { + result = currentCost; + } + if (index == toppingCosts.length || currentCost == target) { + return; + } + recursion(currentCost, toppingCosts, index + 1, target); + recursion(currentCost + toppingCosts[index], toppingCosts, index + 1, target); + recursion(currentCost + toppingCosts[index] * 2, toppingCosts, index + 1, target); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1775.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1775.java new file mode 100644 index 0000000000..47d3a7e311 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1775.java @@ -0,0 +1,73 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1775 { + public static class Solution1 { + public int minOperations(int[] nums1, int[] nums2) { + int[] longer = nums1.length > nums2.length ? nums1 : nums2; + int[] shorter = nums1.length > nums2.length ? nums2 : nums1; + if (longer.length > shorter.length * 6) { + /*This is the impossible case that we'll rule out first.*/ + return -1; + } + Arrays.sort(longer); + Arrays.sort(shorter); + int i = 0; + int j = 0; + int diff = 0; + while (i < longer.length || j < shorter.length) { + if (i < longer.length) { + diff += longer[i++]; + } + if (j < shorter.length) { + diff -= shorter[j++]; + } + } + int minOps = 0; + i = 0; + j = shorter.length - 1; + if (diff < 0) { + /*if diff is negative, this means we'll need to decrease numbers in the shorter array and increase the numbers in the longer array to make the diff to be zero + * and each time, we'll be greedy: take the bigger delta from two of the arrays.*/ + while (diff < 0) { + if (i < longer.length && j >= 0) { + if (6 - longer[i] < shorter[j] - 1) { + diff += shorter[j--] - 1; + } else { + diff += 6 - longer[i++]; + } + } else if (i < longer.length) { + diff += 6 - longer[i++]; + } else { + diff += shorter[j--] - 1; + } + minOps++; + } + return minOps; + } else if (diff > 0) { + /*if diff is positive, this means we'll need to decrease the numbers in the longer array and increase the numbers in the shorter array to make the diff to be zero + * and each time, we'll be greedy: take the bigger delta from two of the arrays.*/ + i = longer.length - 1; + j = 0; + while (diff > 0) { + if (i >= 0 && j < shorter.length) { + if (longer[i] - 1 > 6 - shorter[j]) { + diff -= longer[i--] - 1; + } else { + diff -= 6 - shorter[j++]; + } + } else if (i >= 0) { + diff -= longer[i--] - 1; + } else { + diff -= 6 - shorter[j++]; + } + minOps++; + } + return minOps; + } else { + return minOps; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1779.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1779.java new file mode 100644 index 0000000000..f24214a261 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1779.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1779 { + public static class Solution1 { + public int nearestValidPoint(int x, int y, int[][] points) { + int nearestManDistance = Integer.MAX_VALUE; + int result = -1; + for (int i = 0; i < points.length; i++) { + int[] point = points[i]; + if (point[0] == x || point[1] == y) { + int distance = Math.abs(point[0] - x) + Math.abs(point[1] - y); + if (distance < nearestManDistance) { + result = i; + nearestManDistance = distance; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1780.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1780.java new file mode 100644 index 0000000000..a793ec2fbf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1780.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1780 { + public static class Solution1 { + public boolean checkPowersOfThree(int n) { + List powers = new ArrayList<>(); + int power = 1; + for (int i = 1; power <= n; i++) { + powers.add(power); + power = (int) Math.pow(3, i); + } + int i = powers.size() - 1; + while (n > 0 && i >= 0) { + if (n - powers.get(i) > 0) { + n -= powers.get(i--); + } else if (n - powers.get(i) == 0) { + return true; + } else { + i--; + } + } + return n == 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1781.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1781.java new file mode 100644 index 0000000000..0a970ffb0e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1781.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1781 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/1096380/Java-or-T%3A-O(N2)-or-S%3A-O(1)-Get-the-beauty-of-all-substrings-and-sum-them + */ + public int beautySum(String s) { + int sum = 0; + for (int i = 0; i < s.length(); i++) { + int[] charCount = new int[26]; + for (int j = i; j < s.length(); j++) { + charCount[s.charAt(j) - 'a']++; + // get beauty of s.substring(i, j) + int beauty = getMaxCount(charCount) - getMinCount(charCount); + sum += beauty; + } + } + return sum; + } + + private int getMinCount(int[] charCount) { + int min = Integer.MAX_VALUE; + for (int i = 0; i < 26; i++) { + if (charCount[i] != 0) { + min = Math.min(min, charCount[i]); + } + } + return min; + } + + private int getMaxCount(int[] charCount) { + int max = 0; + for (int i = 0; i < 26; i++) { + if (charCount[i] != 0) { + max = Math.max(max, charCount[i]); + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1784.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1784.java new file mode 100644 index 0000000000..8cd6001068 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1784.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1784 { + public static class Solution1 { + public boolean checkOnesSegment(String s) { + boolean metOne = false; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == '1' && metOne) { + return false; + } + if (s.charAt(i) == '1') { + metOne = true; + while (i < s.length() && s.charAt(i) == '1') { + i++; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1785.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1785.java new file mode 100644 index 0000000000..bea15d4638 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1785.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1785 { + public static class Solution1 { + public int minElements(int[] nums, int limit, int goal) { + long sum = 0; + for (int num : nums) { + sum += num; + } + long diff = Math.abs(goal - sum); + return diff % limit == 0 ? (int) (diff / limit) : (int) ((diff / limit) + 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1790.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1790.java new file mode 100644 index 0000000000..f394f81c70 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1790.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1790 { + public static class Solution1 { + public boolean areAlmostEqual(String s1, String s2) { + if (s1.equals(s2)) { + return true; + } + for (int i = 0; i < s1.length(); i++) { + for (int j = 0; j < i; j++) { + String newS1 = swap(s1, i, j); + if (newS1.equals(s2)) { + return true; + } + } + } + return false; + } + + private String swap(String str, int i, int j) { + char c = str.charAt(i); + StringBuilder sb = new StringBuilder(str); + sb.replace(i, i + 1, str.charAt(j) + ""); + sb.replace(j, j + 1, "" + c); + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1791.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1791.java new file mode 100644 index 0000000000..e395e5c62f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1791.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1791 { + public static class Solution1 { + public int findCenter(int[][] edges) { + int[] neighbors = new int[edges.length + 1]; + for (int[] edge : edges) { + int a = edge[0]; + int b = edge[1]; + neighbors[a - 1]++; + neighbors[b - 1]++; + } + for (int i = 0; i < neighbors.length; i++) { + if (neighbors[i] == edges.length) { + return i + 1; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1792.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1792.java new file mode 100644 index 0000000000..36c7765cb5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1792.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1792 { + public static class Solution1 { + /* + * We use the change size to order the elements in the maxHeap. + */ + public double maxAverageRatio(int[][] classes, int extraStudents) { + PriorityQueue maxHeap = + new PriorityQueue<>((a, b) -> -Double.compare(a[0], b[0])); + for (int[] c : classes) { + maxHeap.offer( + new double[] { + (double) (c[0] + 1) / (c[1] + 1) - (double) c[0] / c[1], c[0], c[1] + }); + } + while (extraStudents-- > 0) { + double[] curr = maxHeap.poll(); + curr[1]++; + curr[2]++; + curr[0] = (curr[1] + 1) / (curr[2] + 1) - curr[1] / curr[2]; + maxHeap.offer(curr); + } + double result = 0.0; + int size = maxHeap.size(); + while (!maxHeap.isEmpty()) { + double[] curr = maxHeap.poll(); + result += curr[1] / curr[2]; + } + return result / size; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1796.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1796.java new file mode 100644 index 0000000000..4ad0b0195e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1796.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeSet; + +public class _1796 { + public static class Solution1 { + public int secondHighest(String s) { + TreeSet set = new TreeSet<>(); + for (char c : s.toCharArray()) { + if (Character.isDigit(c)) { + set.add(Character.getNumericValue(c)); + } + } + if (set.isEmpty() || set.size() == 1) { + return -1; + } + List list = new ArrayList<>(set); + return list.get(list.size() - 2); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1797.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1797.java new file mode 100644 index 0000000000..b86882e751 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1797.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1797 { + public static class Solution1 { + public static class AuthenticationManager { + + int timeToLive; + int currentTime; + Map map; // tokenId -> expireTime + + public AuthenticationManager(int timeToLive) { + this.timeToLive = timeToLive; + this.currentTime = 0; + this.map = new HashMap<>(); + } + + public void generate(String tokenId, int currentTime) { + map.put(tokenId, currentTime + timeToLive); + } + + public void renew(String tokenId, int currentTime) { + Integer expireTime = map.getOrDefault(tokenId, -1); + if (expireTime == -1 || expireTime <= currentTime) { + return; + } + map.put(tokenId, currentTime + timeToLive); + } + + public int countUnexpiredTokens(int currentTime) { + Map tmp = new HashMap<>(); + for (String token : map.keySet()) { + if (map.get(token) > currentTime) { + tmp.put(token, map.get(token)); + } + } + map.clear(); + map.putAll(tmp); + return map.size(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1800.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1800.java new file mode 100644 index 0000000000..3bc577d63b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1800.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1800 { + public static class Solution1 { + public int maxAscendingSum(int[] nums) { + int maxSum = nums[0]; + for (int i = 0, j = i + 1; i < nums.length - 1 && j < nums.length; j++) { + int sum = nums[j - 1]; + while (j < nums.length && nums[j] - nums[j - 1] > 0) { + sum += nums[j]; + j++; + } + i = j; + maxSum = Math.max(maxSum, sum); + } + return maxSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1804.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1804.java new file mode 100644 index 0000000000..48343483c3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1804.java @@ -0,0 +1,75 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1804 { + public static class Solution1 { + public static class Trie { + class TrieNode { + int count; + int wordsCount; + TrieNode[] children; + + public TrieNode() { + this.children = new TrieNode[26]; + } + + boolean isWord; + } + + TrieNode root; + + public Trie() { + root = new TrieNode(); + } + + public void insert(String word) { + TrieNode node = this.root; + for (char c : word.toCharArray()) { + if (node.children[c - 'a'] == null) { + node.children[c - 'a'] = new TrieNode(); + } + if (node.children[c - 'a'].count < 0) { + node.children[c - 'a'].count = 0; + } + node.children[c - 'a'].count++; + node = node.children[c - 'a']; + } + node.isWord = true; + if (node.wordsCount < 0) { + node.wordsCount = 0; + } + node.wordsCount++; + } + + public int countWordsEqualTo(String word) { + TrieNode node = this.root; + for (char c : word.toCharArray()) { + if (node.children[c - 'a'] == null) { + return 0; + } + node = node.children[c - 'a']; + } + return node.isWord ? node.wordsCount : 0; + } + + public int countWordsStartingWith(String prefix) { + TrieNode node = this.root; + for (char c : prefix.toCharArray()) { + if (node.children[c - 'a'] == null) { + return 0; + } + node = node.children[c - 'a']; + } + return Math.max(node.count, 0); + } + + public void erase(String word) { + TrieNode node = this.root; + for (char c : word.toCharArray()) { + node.children[c - 'a'].count--; + node = node.children[c - 'a']; + } + node.wordsCount--; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1805.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1805.java new file mode 100644 index 0000000000..5618730ffd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1805.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _1805 { + public static class Solution1 { + public int numDifferentIntegers(String word) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < word.length(); i++) { + if (!Character.isDigit(word.charAt(i))) { + sb.append(" "); + } else { + sb.append(word.charAt(i)); + } + } + String[] numbers = sb.toString().split("\\s+"); + Set set = new HashSet<>(); + for (String num : numbers) { + if (!num.isEmpty()) { + set.add(num.replaceFirst("^0+(?!$)", "")); + } + } + return set.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1806.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1806.java new file mode 100644 index 0000000000..a66bce2ee6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1806.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1806 { + public static class Solution1 { + public int reinitializePermutation(int n) { + int[] initial = new int[n]; + int[] perm = new int[n]; + for (int i = 0; i < n; i++) { + initial[i] = i; + perm[i] = i; + } + int[] arr = new int[n]; + int times = 0; + do { + for (int i = 0; i < n; i++) { + if (i % 2 == 0) { + arr[i] = perm[i / 2]; + } else { + arr[i] = perm[n / 2 + (i - 1) / 2]; + } + } + times++; + for (int i = 0; i < n; i++) { + perm[i] = arr[i]; + } + } while (!Arrays.equals(arr, initial)); + return times; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1807.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1807.java new file mode 100644 index 0000000000..97be40f0e7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1807.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1807 { + public static class Solution1 { + public String evaluate(String s, List> knowledge) { + Map map = new HashMap<>(); + for (List know : knowledge) { + map.put(know.get(0), know.get(1)); + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == '(') { + int start = i; + while (i < s.length() && s.charAt(i) != ')') { + i++; + } + String key = s.substring(start + 1, i); + sb.append(map.getOrDefault(key, "?")); + } else { + sb.append(s.charAt(i)); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1812.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1812.java new file mode 100644 index 0000000000..3ba7f4db25 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1812.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1812 { + public static class Solution1 { + public boolean squareIsWhite(String coordinates) { + char x = coordinates.charAt(0); + int y = Integer.parseInt(coordinates.charAt(1) + ""); + switch (x) { + case 'a': + case 'c': + case 'e': + case 'g': + return y % 2 == 0; + default: + return y % 2 != 0; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1813.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1813.java new file mode 100644 index 0000000000..8187dab2e1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1813.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1813 { + public static class Solution1 { + public boolean areSentencesSimilar(String sentence1, String sentence2) { + String shorter = sentence1.length() < sentence2.length() ? sentence1 : sentence2; + String longer = shorter.equals(sentence1) ? sentence2 : sentence1; + String[] shortWords = shorter.split(" "); + String[] longWords = longer.split(" "); + int breaks = 0; + int j = 0; + int i = 0; + for (; i < shortWords.length && j < longWords.length; ) { + if (shortWords[i].equals(longWords[j])) { + j++; + i++; + } else { + breaks++; + if (breaks > 1) { + break; + } + while (j < longWords.length && !longWords[j].equals(shortWords[i])) { + j++; + } + } + } + if ((breaks == 1 && i == shortWords.length && j == longWords.length) + || (i == shortWords.length && breaks == 0)) { + return true; + } + // we'll check from the left side and move towards the right side + i = shortWords.length - 1; + j = longWords.length - 1; + breaks = 0; + for (; i >= 0 && j >= 0; ) { + if (shortWords[i].equals(longWords[j])) { + i--; + j--; + } else { + breaks++; + if (breaks > 1) { + return false; + } + while (j >= 0 && !longWords[j].equals(shortWords[i])) { + j--; + } + } + } + return (breaks == 1 && i == -1 && j == -1) || (breaks == 0 && i == -1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1814.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1814.java new file mode 100644 index 0000000000..2593b4534b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1814.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _1814 { + public static class Solution1 { + public int countNicePairs(int[] nums) { + long[] reverses = getRev(nums); + Map map = new HashMap<>(); + long nicePairs = 0; + for (int i = 0; i < nums.length; i++) { + long diff = nums[i] - reverses[i]; + map.put(diff, map.getOrDefault(diff, 0) + 1); + nicePairs = (nicePairs + map.get(nums[i] - reverses[i]) - 1) % 1000000007; + } + return (int) nicePairs; + } + + private long[] getRev(int[] nums) { + long[] reverses = new long[nums.length]; + for (int i = 0; i < nums.length; i++) { + long reverse = 0; + int num = nums[i]; + while (num != 0) { + reverse = reverse * 10 + num % 10; + num /= 10; + } + reverses[i] = reverse; + } + return reverses; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1816.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1816.java new file mode 100644 index 0000000000..770606d62c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1816.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1816 { + public static class Solution1 { + public String truncateSentence(String s, int k) { + String[] words = s.split(" "); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < k; i++) { + sb.append(words[i]); + sb.append(" "); + } + return sb.substring(0, sb.toString().length() - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1817.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1817.java new file mode 100644 index 0000000000..f3ba2966c6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1817.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +public class _1817 { + public static class Solution1 { + public int[] findingUsersActiveMinutes(int[][] logs, int k) { + Map> map = new HashMap<>(); + for (int[] log : logs) { + int user = log[0]; + if (!map.containsKey(user)) { + map.put(user, new HashSet<>()); + } + Set set = map.get(user); + set.add(log[1]); + } + int[] result = new int[k]; + TreeMap treeMap = new TreeMap<>(); + for (int key : map.keySet()) { + int uam = map.get(key).size(); + treeMap.put(uam, treeMap.getOrDefault(uam, 0) + 1); + } + for (int key : treeMap.keySet()) { + result[key - 1] = treeMap.get(key); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1822.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1822.java new file mode 100644 index 0000000000..e9f940a94a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1822.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1822 { + public static class Solution1 { + public int arraySign(int[] nums) { + int negativeCount = 0; + for (int num : nums) { + if (num == 0) { + return 0; + } else if (num < 0) { + negativeCount++; + } + } + return negativeCount % 2 == 0 ? 1 : -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1823.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1823.java new file mode 100644 index 0000000000..ae5ee4926c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1823.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +public class _1823 { + public static class Solution1 { + public int findTheWinner(int n, int k) { + List list = new ArrayList<>(n); + for (int i = 0; i < n; i++) { + list.add(i + 1); + } + int startIndex = 0; + while (list.size() != 1) { + int removeIndex = (startIndex + k - 1) % list.size(); + list.remove(removeIndex); + startIndex = removeIndex; + } + return list.get(0); + } + } + + public static class Solution2 { + /* + * My completely original solution: use a double linked list to keep moving (k - 1) people from + * the tail of the queue to the head of the queue, and then remove the kth person, + * until there's only one person in the queue who is the winner. + */ + public int findTheWinner(int n, int k) { + Deque doublyLinkedList = new LinkedList<>(); + for (int i = 1; i <= n; i++) { + doublyLinkedList.addFirst(i); + } + while (doublyLinkedList.size() > 1) { + int counter = 1; + while (counter++ < k) { + doublyLinkedList.addFirst(doublyLinkedList.pollLast()); + } + doublyLinkedList.pollLast(); + } + return doublyLinkedList.getLast(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1826.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1826.java new file mode 100644 index 0000000000..38a29dc045 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1826.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1826 { + public static class Solution1 { + public int badSensor(int[] sensor1, int[] sensor2) { + // check if sensor2 is faulty + int i = 0; + int j = 0; + for (; i < sensor1.length && j < sensor2.length - 1; ) { + if (sensor1[i] != sensor2[j]) { + i++; + } else { + i++; + j++; + } + } + boolean sensor2Faulty = false; + if (j == sensor2.length - 1 && i == sensor1.length) { + sensor2Faulty = true; + } + // check sensor1 + i = 0; + j = 0; + for (; i < sensor1.length - 1 && j < sensor2.length; ) { + if (sensor1[i] != sensor2[j]) { + j++; + } else { + i++; + j++; + } + } + boolean sensor1Faulty = false; + if (i == sensor1.length - 1 && j == sensor2.length) { + sensor1Faulty = true; + } + if (sensor1Faulty && sensor2Faulty) { + return -1; + } else if (sensor1Faulty) { + return 1; + } else if (sensor2Faulty) { + return 2; + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1827.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1827.java new file mode 100644 index 0000000000..3a752f8aa6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1827.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1827 { + public static class Solution1 { + public int minOperations(int[] nums) { + int minsOps = 0; + for (int i = 1; i < nums.length; i++) { + if (nums[i] <= nums[i - 1]) { + minsOps += nums[i - 1] - nums[i] + 1; + nums[i] = nums[i - 1] + 1; + } + } + return minsOps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1828.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1828.java new file mode 100644 index 0000000000..ab7d99a310 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1828.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1828 { + public static class Solution1 { + public int[] countPoints(int[][] points, int[][] queries) { + int[] result = new int[queries.length]; + int i = 0; + for (int[] query : queries) { + int pts = 0; + for (int[] point : points) { + if ((point[0] - query[0]) * (point[0] - query[0]) + + (point[1] - query[1]) * (point[1] - query[1]) + <= query[2] * query[2]) { + pts++; + } + } + result[i++] = pts; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1829.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1829.java new file mode 100644 index 0000000000..bad61816cd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1829.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1829 { + public static class Solution1 { + public int[] getMaximumXor(int[] nums, int maximumBit) { + int[] result = new int[nums.length]; + long[] xOr = new long[nums.length]; + xOr[0] = nums[0]; + for (int i = 1; i < nums.length; i++) { + xOr[i] ^= xOr[i - 1] ^ nums[i]; + } + long maxNum = (long) Math.pow(2, maximumBit) - 1; + for (int i = 0; i < nums.length; i++) { + result[nums.length - i - 1] = (int) (maxNum ^ xOr[i]); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1832.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1832.java new file mode 100644 index 0000000000..70750f1e4d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1832.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _1832 { + public static class Solution1 { + public boolean checkIfPangram(String sentence) { + Set alphabet = new HashSet<>(); + for (char c : sentence.toCharArray()) { + alphabet.add(c); + } + return alphabet.size() == 26; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1833.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1833.java new file mode 100644 index 0000000000..2ad445b06e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1833.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.TreeMap; + +public class _1833 { + public static class Solution1 { + public int maxIceCream(int[] costs, int coins) { + TreeMap treeMap = new TreeMap<>(); + for (int cost : costs) { + treeMap.put(cost, treeMap.getOrDefault(cost, 0) + 1); + } + int maxIceCream = 0; + for (int cost : treeMap.keySet()) { + if (cost * treeMap.get(cost) <= coins) { + maxIceCream += treeMap.get(cost); + coins -= cost * treeMap.get(cost); + } else { + while (coins > 0 && coins - cost >= 0) { + coins -= cost; + maxIceCream++; + } + break; + } + } + return maxIceCream; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1836.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1836.java new file mode 100644 index 0000000000..401c206d71 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1836.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.HashMap; +import java.util.Map; + +public class _1836 { + public static class Solution1 { + public ListNode deleteDuplicatesUnsorted(ListNode head) { + Map map = new HashMap<>(); + ListNode tmp = head; + while (tmp != null) { + map.put(tmp.val, map.getOrDefault(tmp.val, 0) + 1); + tmp = tmp.next; + } + ListNode pre = new ListNode(-1); + tmp = pre; + while (head != null) { + if (map.get(head.val) == 1) { + tmp.next = new ListNode(head.val); + tmp = tmp.next; + } + head = head.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1837.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1837.java new file mode 100644 index 0000000000..de889bc0bd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1837.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1837 { + public static class Solution1 { + public int sumBase(int n, int k) { + String str = Integer.toString(Integer.parseInt(n + "", 10), k); + int sum = 0; + for (char c : str.toCharArray()) { + sum += Character.getNumericValue(c); + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1844.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1844.java new file mode 100644 index 0000000000..7e16f2d501 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1844.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1844 { + public static class Solution1 { + public String replaceDigits(String s) { + StringBuilder sb = new StringBuilder(); + for (char c : s.toCharArray()) { + if (Character.isAlphabetic(c)) { + sb.append(c); + } else { + sb.append((char) (sb.charAt(sb.length() - 1) + Character.getNumericValue(c))); + } + } + return sb.toString(); + } + } + + public static class Solution2 { + public String replaceDigits(String s) { + char[] inpArr = s.toCharArray(); + for (int i = 1; i < inpArr.length; i += 2) { + inpArr[i] = (char) (inpArr[i - 1] + inpArr[i] - '0'); + } + return String.valueOf(inpArr); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1845.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1845.java new file mode 100644 index 0000000000..366f909ebe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1845.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1845 { + public static class Solution1 { + class SeatManager { + boolean[] seats; + int next; + + public SeatManager(int n) { + seats = new boolean[n + 1]; + next = 1; + } + + public int reserve() { + seats[next] = true; + int tmp = next; + while (next < seats.length && seats[next]) { + next++; + } + return tmp; + } + + public void unreserve(int seatNumber) { + seats[seatNumber] = false; + if (next > seatNumber) { + next = seatNumber; + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1848.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1848.java new file mode 100644 index 0000000000..3ad64888fb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1848.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1848 { + public static class Solution1 { + public int getMinDistance(int[] nums, int target, int start) { + int result = 0; + int minDiff = Integer.MAX_VALUE; + for (int i = 0; i < nums.length; i++) { + if (nums[i] == target) { + if (Math.abs(start - i) < minDiff) { + minDiff = Math.abs(start - i); + result = minDiff; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1854.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1854.java new file mode 100644 index 0000000000..da264b4378 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1854.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.TreeMap; + +public class _1854 { + public static class Solution1 { + public int maximumPopulation(int[][] logs) { + TreeMap map = new TreeMap<>(); + for (int[] log : logs) { + for (int start = log[0]; start < log[1]; start++) { + map.put(start, map.getOrDefault(start, 0) + 1); + } + } + int maxPop = 0; + int maxPopYear = 0; + for (int year : map.keySet()) { + if (map.get(year) > maxPop) { + maxPop = map.get(year); + maxPopYear = year; + } + } + return maxPopYear; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1859.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1859.java new file mode 100644 index 0000000000..3d65cee00c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1859.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.TreeMap; + +public class _1859 { + public static class Solution1 { + public String sortSentence(String s) { + String[] words = s.split(" "); + TreeMap treeMap = new TreeMap<>(); + for (String word : words) { + int key = Integer.parseInt(word.charAt(word.length() - 1) + ""); + treeMap.put(key, word.substring(0, word.length() - 1)); + } + StringBuilder sb = new StringBuilder(); + for (int key : treeMap.keySet()) { + sb.append(treeMap.get(key)); + sb.append(" "); + } + return sb.substring(0, sb.length() - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1860.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1860.java new file mode 100644 index 0000000000..8e798a9bfd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1860.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1860 { + public static class Solution1 { + public int[] memLeak(int memory1, int memory2) { + int time = 1; + while (memory1 >= time || memory2 >= time) { + if (memory1 >= memory2) { + memory1 -= time; + } else { + memory2 -= time; + } + time++; + } + return new int[] {time, memory1, memory2}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1861.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1861.java new file mode 100644 index 0000000000..d44a229cb8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1861.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1861 { + public static class Solution1 { + public char[][] rotateTheBox(char[][] box) { + int m = box.length; + int n = box[0].length; + for (int i = 0; i < m; i++) { + for (int j = n - 1; j >= 0; j--) { + if (box[i][j] == '#') { + int empty = j + 1; + while (empty < n && box[i][empty] == '.') { + empty++; + } + if (empty < n && box[i][empty] == '.') { + box[i][empty] = '#'; + box[i][j] = '.'; + } else if (empty - 1 < n && box[i][empty - 1] == '.') { + box[i][empty - 1] = '#'; + box[i][j] = '.'; + } + } + } + } + char[][] result = new char[n][m]; + int k = m - 1; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + result[j][k] = box[i][j]; + } + k--; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1862.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1862.java new file mode 100644 index 0000000000..fe8001bab2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1862.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _1862 { + public static class Solution1 { + /* + * TODO: this results in TLE, fix it. + */ + public int sumOfFlooredPairs(int[] nums) { + TreeMap map = new TreeMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + List list = new ArrayList<>(map.keySet()); + int mod = 1000000007; + long sum = 0L; + for (int i = list.size() - 1; i >= 0; i--) { + for (int j = i; j >= 0; j--) { + sum += + (list.get(i) / list.get(j)) + * map.get(list.get(j)) + * map.get(list.get(i)); + sum %= mod; + } + } + return (int) sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1863.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1863.java new file mode 100644 index 0000000000..81d114c8d8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1863.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1863 { + public static class Solution1 { + public int subsetXORSum(int[] nums) { + int sum = 0; + List> subsets = subsets(nums); + for (List subset : subsets) { + int xor = 0; + for (int i : subset) { + xor ^= i; + } + sum += xor; + } + return sum; + } + + public List> subsets(int[] nums) { + List> result = new ArrayList(); + backtracking(result, new ArrayList(), nums, 0); + return result; + } + + void backtracking(List> result, List list, int[] nums, int start) { + result.add(new ArrayList(list)); + for (int i = start; i < nums.length; i++) { + list.add(nums[i]); + backtracking(result, list, nums, i + 1); + list.remove(list.size() - 1); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1868.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1868.java new file mode 100644 index 0000000000..8e2c3273e2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1868.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _1868 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/product-of-two-run-length-encoded-arrays/ + */ + public List> findRLEArray(int[][] encoded1, int[][] encoded2) { + // local progress in the current intervals + int pointer1 = 0; + int pointer2 = 0; + + // global progress in the overall encoded arrays + int index1 = 0; + int index2 = 0; + + List> result = new ArrayList<>(); + while (index1 < encoded1.length && index2 < encoded2.length) { + int freq1 = encoded1[index1][1] - pointer1; + int freq2 = encoded2[index2][1] - pointer2; + // choose smaller one as the step size + int freq = Math.min(freq1, freq2); + + // update the local progress in the intervals + pointer1 += freq; + pointer2 += freq; + + int product = encoded1[index1][0] * encoded2[index2][0]; + + int size = result.size(); + // if the current product is the same as the most recent one in the result, + // concatenate it + if (size > 0 && result.get(size - 1).get(0) == product) { + freq += result.get(size - 1).get(1); + result.remove(size - 1); + } + result.add(Arrays.asList(product, freq)); + + // check if global progress is moving forward + if (pointer1 == encoded1[index1][1]) { + index1++; + pointer1 = 0; + } + + if (pointer2 == encoded2[index2][1]) { + index2++; + pointer2 = 0; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1869.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1869.java new file mode 100644 index 0000000000..28cae085a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1869.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1869 { + public static class Solution1 { + public boolean checkZeroOnes(String s) { + int zeroes = 0; + int ones = 0; + for (int i = 0; i < s.length(); ) { + int start = i; + while (i < s.length() && s.charAt(i) == '0') { + i++; + } + if (i > start) { + zeroes = Math.max(zeroes, i - start); + } + start = i; + while (i < s.length() && s.charAt(i) == '1') { + i++; + } + if (i > start) { + ones = Math.max(ones, i - start); + } + } + return ones > zeroes; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1874.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1874.java new file mode 100644 index 0000000000..a9573a64d6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1874.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1874 { + public static class Solution1 { + public int minProductSum(int[] nums1, int[] nums2) { + Arrays.sort(nums1); + Arrays.sort(nums2); + int ans = 0; + for (int i = 0, j = nums1.length - 1; i < nums1.length && j >= 0; i++, j--) { + ans += nums1[i] * nums2[j]; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1876.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1876.java new file mode 100644 index 0000000000..3026aeb265 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1876.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1876 { + public static class Solution1 { + public int countGoodSubstrings(String s) { + int count = 0; + for (int i = 0; i < s.length() - 2; i++) { + String candidate = s.substring(i, i + 3); + if (candidate.charAt(0) != candidate.charAt(1) + && candidate.charAt(0) != candidate.charAt(2) + && candidate.charAt(1) != candidate.charAt(2)) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1877.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1877.java new file mode 100644 index 0000000000..96716330bc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1877.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1877 { + public static class Solution1 { + public int minPairSum(int[] nums) { + Arrays.sort(nums); + int maxSum = 0; + for (int left = 0, right = nums.length - 1; left < right; left++, right--) { + maxSum = Math.max(maxSum, nums[left] + nums[right]); + } + return maxSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1880.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1880.java new file mode 100644 index 0000000000..a3dbfde441 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1880.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1880 { + public static class Solution1 { + public boolean isSumEqual(String firstWord, String secondWord, String targetWord) { + StringBuilder sb = new StringBuilder(); + int a = getSum(firstWord, sb); + sb.setLength(0); + int b = getSum(secondWord, sb); + sb.setLength(0); + int c = getSum(targetWord, sb); + return a + b == c; + } + + private int getSum(String firstWord, StringBuilder sb) { + for (char c : firstWord.toCharArray()) { + sb.append(c - 'a'); + } + return Integer.parseInt(sb.toString()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1886.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1886.java new file mode 100644 index 0000000000..4c79684a5f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1886.java @@ -0,0 +1,82 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1886 { + public static class Solution1 { + public boolean findRotation(int[][] mat, int[][] target) { + int m = mat.length; + int n = mat[0].length; + for (int i = 0; i < m; i++) { + int j = 0; + for (; j < n; j++) { + if (mat[i][j] != target[i][j]) { + break; + } + } + if (j < n) { + break; + } else if (i == m - 1) { + return true; + } + } + + // rotate 90 degrees once + for (int i = 0, k = n - 1; i < m; i++, k--) { + int j = 0; + for (; j < n; j++) { + if (mat[i][j] != target[j][k]) { + break; + } + } + if (j < n) { + break; + } else if (i == m - 1) { + return true; + } + } + int[][] rotated = new int[m][n]; + for (int i = 0, k = n - 1; i < m; i++, k--) { + for (int j = 0; j < n; j++) { + rotated[j][k] = mat[i][j]; + } + } + + // rotate 90 degrees the second time + for (int i = 0, k = n - 1; i < m; i++, k--) { + int j = 0; + for (; j < n; j++) { + if (rotated[i][j] != target[j][k]) { + break; + } + } + if (j < n) { + break; + } else if (i == m - 1) { + return true; + } + } + int[][] rotated2 = new int[m][n]; + for (int i = 0, k = n - 1; i < m; i++, k--) { + int j = 0; + for (; j < n; j++) { + rotated2[j][k] = rotated[i][j]; + } + } + + // rotate 90 degrees the third time + for (int i = 0, k = n - 1; i < m; i++, k--) { + int j = 0; + for (; j < n; j++) { + if (rotated2[i][j] != target[j][k]) { + break; + } + } + if (j < n) { + break; + } else if (i == m - 1) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1891.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1891.java new file mode 100644 index 0000000000..b017d793a1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1891.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1891 { + public static class Solution1 { + /* + * My completely original solution on 1/27/2022. + */ + public int maxLength(int[] ribbons, int k) { + long sum = 0L; + int max = ribbons[0]; + for (int num : ribbons) { + sum += num; + max = Math.max(max, num); + } + if (sum < k) { + return 0; + } else if (sum == k) { + return 1; + } else { + Arrays.sort(ribbons); + int left = 1; + int right = max; + int ans = 1; + while (left < right) { + int mid = left + (right - left) / 2; + int count = 0; + for (int i = ribbons.length - 1; i >= 0; i--) { + count += ribbons[i] / mid; + if (count >= k) { + ans = Math.max(ans, mid); + break; + } + } + if (count < k) { + right = mid - 1; + } else { + left = mid + 1; + } + } + int count = 0; + for (int i = ribbons.length - 1; i >= 0; i--) { + count += ribbons[i] / left; + if (count >= k) { + ans = Math.max(ans, left); + return ans; + } + } + return ans; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1893.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1893.java new file mode 100644 index 0000000000..79556e2323 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1893.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1893 { + public static class Solution1 { + public boolean isCovered(int[][] ranges, int left, int right) { + Arrays.sort(ranges, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); + int checked = left; + for (int[] range : ranges) { + while (checked >= range[0] && checked <= range[1]) { + checked++; + } + } + return checked >= (right + 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1894.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1894.java new file mode 100644 index 0000000000..51d32c4e9b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1894.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1894 { + public static class Solution1 { + public int chalkReplacer(int[] chalk, int k) { + long sum = 0; + for (int c : chalk) { + sum += c; + } + if (k >= sum) { + k %= (int) sum; + } + for (int i = 0; i < chalk.length; i++) { + if (chalk[i] > k) { + return i; + } + k -= chalk[i]; + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1897.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1897.java new file mode 100644 index 0000000000..7572452edf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1897.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1897 { + public static class Solution1 { + public boolean makeEqual(String[] words) { + int len = words.length; + int[] counts = new int[26]; + for (String word : words) { + for (char c : word.toCharArray()) { + counts[c - 'a']++; + } + } + for (int c : counts) { + if (c % len != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1899.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1899.java new file mode 100644 index 0000000000..2855f2856f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1899.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1899 { + public static class Solution1 { + public boolean mergeTriplets(int[][] triplets, int[] target) { + int[] base = new int[3]; + int baseIndex = -1; + for (int i = 0; i < triplets.length; i++) { + if (findBaseTriplet(triplets[i], target)) { + base = triplets[i]; + baseIndex = i; + break; + } + } + for (int i = 0; i < triplets.length; i++) { + if (i != baseIndex) { + boolean merged = false; + if (shouldMerge(triplets[i], target, 0)) { + merged = true; + base = mergeTriplets(triplets[i], base); + } + if (!merged && shouldMerge(triplets[i], target, 1)) { + merged = true; + base = mergeTriplets(triplets[i], base); + } + if (!merged && shouldMerge(triplets[i], target, 2)) { + base = mergeTriplets(triplets[i], base); + } + } + } + return base[0] == target[0] && base[1] == target[1] && base[2] == target[2]; + } + + private int[] mergeTriplets(int[] triplet, int[] base) { + return new int[] { + Math.max(triplet[0], base[0]), + Math.max(triplet[1], base[1]), + Math.max(triplet[2], base[2]) + }; + } + + private boolean shouldMerge(int[] triplet, int[] target, int i) { + if (triplet[i] == target[i]) { + // check the other two indexes not exceeding target + if (i == 0) { + return triplet[1] <= target[1] && triplet[2] <= target[2]; + } else if (i == 1) { + return triplet[0] <= target[0] && triplet[2] <= target[2]; + } else if (i == 2) { + return triplet[0] <= target[0] && triplet[1] <= target[1]; + } + } + return false; + } + + private boolean findBaseTriplet(int[] triplet, int[] target) { + if (triplet[0] <= target[0] && triplet[1] <= target[1] && triplet[2] <= target[2]) { + return true; + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1903.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1903.java new file mode 100644 index 0000000000..49b73a1845 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1903.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1903 { + public static class Solution1 { + public String largestOddNumber(String num) { + for (int i = num.length() - 1; i >= 0; i--) { + if (Integer.parseInt("" + num.charAt(i)) % 2 == 1) { + return num.substring(0, i + 1); + } + } + return ""; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1904.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1904.java new file mode 100644 index 0000000000..6409ecdbb6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1904.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1904 { + public static class Solution1 { + public int numberOfRounds(String startTime, String finishTime) { + int rounds = 0; + int startHour = Integer.parseInt(startTime.split(":")[0]); + int endHour = Integer.parseInt(finishTime.split(":")[0]); + int startMin = Integer.parseInt(startTime.split(":")[1]); + int endMin = Integer.parseInt(finishTime.split(":")[1]); + if (endHour < startHour) { + endHour += 24; + } else if (endHour == startHour && endMin < startMin) { + endHour += 24; + } + if (startHour == endHour) { + if (startMin == 0 && endMin >= 15) { + rounds++; + } + if (startMin <= 15 && endMin >= 30) { + rounds++; + } + if (startMin <= 30 && endMin >= 45) { + rounds++; + } + return rounds; + } else { + // compute all full rounds in the start hour + if (startMin == 0) { + rounds += 4; + } else if (startMin <= 15) { + rounds += 3; + } else if (startMin <= 30) { + rounds += 2; + } else if (startMin <= 45) { + rounds++; + } + + // compute all full rounds in the finish hour + if (endMin >= 45) { + rounds += 3; + } else if (endMin >= 30) { + rounds += 2; + } else if (endMin >= 15) { + rounds++; + } + + // compute all full rounds in the all full hours between finishHour and startHour + rounds += (endHour - startHour - 1) * 4; + return rounds; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1909.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1909.java new file mode 100644 index 0000000000..04e94e67f5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1909.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1909 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing/discuss/1298827/Java-Short + */ + public boolean canBeIncreasing(int[] nums) { + boolean removed = false; + for (int i = 1; i < nums.length; i++) { + if (nums[i] <= nums[i - 1]) { + if (removed) { + return false; + } else { + removed = true; + } + if (i > 1 && nums[i] <= nums[i - 2]) { + nums[i] = nums[i - 1]; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1910.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1910.java new file mode 100644 index 0000000000..fc15473db9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1910.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1910 { + public static class Solution1 { + public String removeOccurrences(String s, String part) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + sb.append(s.charAt(i)); + if (sb.length() >= part.length()) { + if (sb.substring(sb.length() - part.length()).equals(part)) { + sb.setLength(sb.length() - part.length()); + } + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1913.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1913.java new file mode 100644 index 0000000000..e5ac60eeb6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1913.java @@ -0,0 +1,13 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1913 { + public static class Solution1 { + public int maxProductDifference(int[] nums) { + Arrays.sort(nums); + int len = nums.length; + return nums[len - 1] * nums[len - 2] - nums[0] * nums[1]; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1920.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1920.java new file mode 100644 index 0000000000..2dab1553ba --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1920.java @@ -0,0 +1,13 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1920 { + public static class Solution1 { + public int[] buildArray(int[] nums) { + int[] ans = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + ans[i] = nums[nums[i]]; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1925.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1925.java new file mode 100644 index 0000000000..c17f893784 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1925.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1925 { + public static class Solution1 { + public int countTriples(int n) { + int count = 0; + for (int i = 1; i < n; i++) { + for (int j = 1; j < n; j++) { + int product = i * i + j * j; + double sq = Math.sqrt(product); + if (sq <= n && (sq - Math.floor(sq) == 0)) { + count++; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1926.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1926.java new file mode 100644 index 0000000000..2263654ab7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1926.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.LinkedList; +import java.util.Queue; + +public class _1926 { + public static class Solution1 { + public int nearestExit(char[][] maze, int[] entrance) { + int m = maze.length; + int n = maze[0].length; + int[] directions = new int[] {0, 1, 0, -1, 0}; + Queue queue = new LinkedList<>(); + queue.offer(new int[] {entrance[0], entrance[1], 0}); + boolean[][] visited = new boolean[m][n]; + visited[entrance[0]][entrance[1]] = true; + int shortestSteps = m * n; + while (!queue.isEmpty()) { + int[] curr = queue.poll(); + for (int i = 0; i < directions.length - 1; i++) { + int nextX = curr[0] + directions[i]; + int nextY = curr[1] + directions[i + 1]; + if (nextX >= 0 + && nextX < m + && nextY >= 0 + && nextY < n + && maze[nextX][nextY] == '.' + && !visited[nextX][nextY]) { + visited[nextX][nextY] = true; + if (nextX == 0 || nextX == m - 1 || nextY == 0 || nextY == n - 1) { + shortestSteps = Math.min(shortestSteps, curr[2] + 1); + } else { + queue.offer(new int[] {nextX, nextY, curr[2] + 1}); + } + } + } + } + return shortestSteps == m * n ? -1 : shortestSteps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1929.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1929.java new file mode 100644 index 0000000000..8a9a39b6d9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1929.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1929 { + public static class Solution1 { + public int[] getConcatenation(int[] nums) { + int[] result = new int[nums.length * 2]; + int i = 0; + for (; i < nums.length; i++) { + result[i] = nums[i]; + } + for (int j = 0; i < result.length && j < nums.length; i++, j++) { + result[i] = nums[j]; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1933.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1933.java new file mode 100644 index 0000000000..592a6e04fc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1933.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1933 { + public static class Solution1 { + public boolean isDecomposable(String s) { + int lengthTwoCount = 0; + for (int i = 0; i < s.length(); i++) { + int start = i; + char prev = s.charAt(start); + while (i < s.length() && s.charAt(i) == prev) { + i++; + } + if (i >= s.length()) { + i--; + } + if (s.charAt(i) != prev) { + i--; + } + if ((i - start + 1) % 3 == 2) { + lengthTwoCount++; + } else if ((i - start + 1) % 3 == 0) { + continue; + } else { + return false; + } + } + return lengthTwoCount == 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1935.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1935.java new file mode 100644 index 0000000000..681d42b1ad --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1935.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1935 { + public static class Solution1 { + public int canBeTypedWords(String text, String brokenLetters) { + String[] words = text.split(" "); + int count = 0; + for (String word : words) { + boolean broken = false; + for (char c : word.toCharArray()) { + if (brokenLetters.indexOf(c) != -1) { + broken = true; + break; + } + } + if (!broken) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1936.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1936.java new file mode 100644 index 0000000000..365bcd285e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1936.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1936 { + public static class Solution1 { + public int addRungs(int[] rungs, int dist) { + int addons = 0; + int currentHeight = 0; + for (int i = 0; i < rungs.length; ) { + int nextRung = rungs[i]; + if (nextRung - currentHeight <= dist) { + currentHeight = nextRung; + i++; + } else { + int adds = (nextRung - currentHeight - 1) / dist; + addons += adds; + currentHeight += dist * adds; + } + } + return addons; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1941.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1941.java new file mode 100644 index 0000000000..180b658bc9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1941.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.stream.Collectors; + +public class _1941 { + public static class Solution { + public boolean areOccurrencesEqual(String s) { + int[] counts = new int[26]; + char[] charArray = s.toCharArray(); + for (char c : charArray) { + counts[c - 'a']++; + } + return Arrays.stream(counts) + .filter(i -> i != 0) + .boxed() + .collect(Collectors.toSet()) + .size() + == 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1945.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1945.java new file mode 100644 index 0000000000..62cedb6b76 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1945.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _1945 { + public static class Solution1 { + public int getLucky(String s, int k) { + List list = new ArrayList<>(); + for (char c : s.toCharArray()) { + list.add(c - 'a' + 1); + } + int sum = 0; + for (int i : list) { + if (i >= 10) { + sum += i / 10; + } + sum += i % 10; + } + while (k-- > 1) { + int newSum = 0; + while (sum != 0) { + newSum += sum % 10; + sum /= 10; + } + sum = newSum; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1952.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1952.java new file mode 100644 index 0000000000..586b52fd51 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1952.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1952 { + public static class Solution1 { + public boolean isThree(int n) { + int divisors = 0; + for (int i = 1; i <= n; i++) { + if (n % i == 0) { + divisors++; + } + } + return divisors == 3; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1957.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1957.java new file mode 100644 index 0000000000..bd430ba2b9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1957.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1957 { + public static class Solution1 { + public String makeFancyString(String s) { + StringBuilder sb = new StringBuilder(); + int freq = 0; + for (int i = 0; i < s.length(); i++) { + if (i > 0) { + if (s.charAt(i) == s.charAt(i - 1)) { + freq++; + } else { + freq = 1; + } + } else { + freq = 1; + sb.append(s.charAt(i)); + } + if (freq <= 2) { + sb.append(s.charAt(i)); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1961.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1961.java new file mode 100644 index 0000000000..92fb837d71 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1961.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1961 { + public static class Solution1 { + public boolean isPrefixString(String s, String[] words) { + StringBuilder sb = new StringBuilder(); + for (String word : words) { + sb.append(word); + if (sb.toString().equals(s)) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1966.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1966.java new file mode 100644 index 0000000000..43863644e1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1966.java @@ -0,0 +1,84 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _1966 { + public static class Solution1 { + /* + * Brute force: this ends in TLE on LeetCode. + * The idea is: for every single number in the array, check if there's any number on its right side that's smaller than it + * and if there's any number on its left side that's bigger than it. + * If so, based on binary search, if that number gets picked as a pivot for the next search, then this number might not be found. + */ + public int binarySearchableNumbers(int[] nums) { + int ans = 0; + for (int i = 0; i < nums.length; i++) { + int j = i + 1; + for (; j < nums.length; j++) { + if (nums[j] < nums[i]) { + break; + } + } + if (j == nums.length) { + int k = i - 1; + for (; k >= 0; k--) { + if (nums[i] < nums[k]) { + break; + } + } + if (k <= 0) { + ans++; + } + } + } + return ans; + } + } + + public static class Solution2 { + /* + * My completely original solution. + */ + public int binarySearchableNumbers(int[] nums) { + int ans = 0; + int[] maxLeft = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + maxLeft[i] = i == 0 ? nums[i] : Math.max(nums[i], maxLeft[i - 1]); + } + int[] minRight = new int[nums.length]; + for (int i = nums.length - 1; i >= 0; i--) { + minRight[i] = i + 1 == nums.length ? nums[i] : Math.min(minRight[i + 1], nums[i]); + } + for (int i = 0; i < nums.length; i++) { + if (nums[i] >= maxLeft[i] && nums[i] <= minRight[i]) { + ans++; + } + } + return ans; + } + } + + public static class Solution3 { + /* + * Using monotonic stack: + * 1. we only add the ones that are greater than those already on the stack onto the stack. + * 2. if the existing ones on the stack are greater than the current one, + * pop them off because they won't be found based on binary search, as a smaller element is on their right side. + */ + public int binarySearchableNumbers(int[] nums) { + Deque stack = new LinkedList<>(); + int maxLeft = Integer.MIN_VALUE; + for (int num : nums) { + while (!stack.isEmpty() && stack.peekLast() > num) { + stack.pollLast(); + } + if (num >= maxLeft) { + stack.addLast(num); + } + maxLeft = Math.max(maxLeft, num); + } + return stack.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1967.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1967.java new file mode 100644 index 0000000000..a0f1b8be8f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1967.java @@ -0,0 +1,11 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1967 { + public static class Solution1 { + public int numOfStrings(String[] patterns, String word) { + return (int) Arrays.stream(patterns).filter(p -> word.indexOf(p) != -1).count(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1968.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1968.java new file mode 100644 index 0000000000..01e0dcd1b8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1968.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1968 { + public static class Solution1 { + public int[] rearrangeArray(int[] nums) { + Arrays.sort(nums); + int[] result = new int[nums.length]; + int j = 1; + for (int i = 0; i < nums.length / 2; i++) { + result[j] = nums[i]; + j += 2; + } + j = 0; + for (int i = nums.length / 2; i < nums.length; i++) { + result[j] = nums[i]; + j += 2; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1971.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1971.java new file mode 100644 index 0000000000..0e52df5835 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1971.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _1971 { + public static class Solution1 { + public boolean validPath(int n, int[][] edges, int start, int end) { + if (start == end) { + return true; + } + Map> neighborsMap = new HashMap<>(); + for (int[] edge : edges) { + int u = edge[0]; + int v = edge[1]; + Set neighbors1 = neighborsMap.getOrDefault(u, new HashSet<>()); + neighbors1.add(v); + neighborsMap.put(u, neighbors1); + + Set neighbors2 = neighborsMap.getOrDefault(v, new HashSet<>()); + neighbors2.add(u); + neighborsMap.put(v, neighbors2); + } + Queue queue = new LinkedList<>(); + Set visitedVertices = new HashSet<>(); + queue.offer(start); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + Integer curr = queue.poll(); + for (int neighbor : neighborsMap.getOrDefault(curr, new HashSet<>())) { + if (neighbor == end) { + return true; + } + if (visitedVertices.add(neighbor)) { + queue.offer(neighbor); + } + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1973.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1973.java new file mode 100644 index 0000000000..6f3a2d54bf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1973.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.secondthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _1973 { + public static class Solution1 { + /* + * This problem is almost identical to: + * https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/description/ + * https://leetcode.com/problems/maximum-average-subtree/description/ + *

+ * Post-order traversal/recursion comes handy if you need to process subtree before processing the root node. + */ + int count = 0; + + public int equalToDescendants(TreeNode root) { + postOrder(root); + return count; + } + + private int postOrder(TreeNode root) { + if (root == null) { + return 0; + } + int leftSum = postOrder(root.left); + int rightSum = postOrder(root.right); + int subtreeSum = leftSum + rightSum; + if (root.val == subtreeSum) { + count++; + } + return root.val + subtreeSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1974.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1974.java new file mode 100644 index 0000000000..34bb38e29e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1974.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1974 { + public static class Solution1 { + public int minTimeToType(String word) { + int min = 0; + char curr = 'a'; + for (int i = 0; i < word.length(); i++) { + int diff = curr - word.charAt(i); + curr = word.charAt(i); + min += Math.min(diff + 26, Math.min(Math.abs(diff), 26 - diff)); + min++; + } + return min; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1979.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1979.java new file mode 100644 index 0000000000..cf1abe4f5a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1979.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1979 { + public static class Solution1 { + public int findGCD(int[] nums) { + Arrays.sort(nums); + return getGcd(nums[0], nums[nums.length - 1]); + } + + int getGcd(int a, int b) { + return b == 0 ? a : getGcd(b, a % b); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1980.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1980.java new file mode 100644 index 0000000000..b99e190c0f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1980.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _1980 { + public static class Solution1 { + public String findDifferentBinaryString(String[] nums) { + Set set = new HashSet<>(Arrays.asList(nums)); + int len = nums[0].length(); + StringBuilder sb = new StringBuilder(); + int i = 0; + while (i < len) { + sb.append(1); + i++; + } + int max = Integer.parseInt(sb.toString(), 2); + for (int num = 0; num <= max; num++) { + String binary = Integer.toBinaryString(num); + if (binary.length() < len) { + sb.setLength(0); + sb.append(binary); + while (sb.length() < len) { + sb.insert(0, "0"); + } + binary = sb.toString(); + } + if (!set.contains(binary)) { + return binary; + } + } + return null; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1981.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1981.java new file mode 100644 index 0000000000..571bced18f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1981.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1981 { + public static class Solution1 { + /* + * creidt: https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/discuss/1418614/Java-dp-code-with-proper-comments-and-explanation + */ + int ans = Integer.MAX_VALUE; + boolean[][] dp; + + public int minimizeTheDifference(int[][] mat, int target) { + dp = + new boolean[mat.length] + [4900]; // we use 4900 due to the contraints in this problem: 70 * 70 = + // 4900 + memo(mat, 0, 0, target); + return ans; + } + + private void memo(int[][] mat, int row, int sum, int target) { + if (dp[row][sum]) { + return; + } + if (row == mat.length - 1) { + for (int i = 0; i < mat[0].length; i++) { + ans = Math.min(ans, Math.abs(sum + mat[row][i] - target)); + } + dp[row][sum] = true; + return; + } + for (int i = 0; i < mat[0].length; i++) { + memo(mat, row + 1, sum + mat[row][i], target); + } + dp[row][sum] = true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1984.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1984.java new file mode 100644 index 0000000000..c1dd922a61 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1984.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1984 { + public static class Solution1 { + public int minimumDifference(int[] nums, int k) { + Arrays.sort(nums); + int minDiff = nums[nums.length - 1]; + for (int i = 0; i <= nums.length - k; i++) { + minDiff = Math.min(minDiff, nums[i + k - 1] - nums[i]); + } + return minDiff; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1985.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1985.java new file mode 100644 index 0000000000..396548d3d7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1985.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.PriorityQueue; + +public class _1985 { + public static class Solution1 { + public String kthLargestNumber(String[] nums, int k) { + PriorityQueue maxHeap = + new PriorityQueue<>( + (a, b) -> + (a.length() != b.length() + ? b.length() - a.length() + : b.compareTo(a))); + for (String num : nums) { + maxHeap.offer(num); + } + while (k-- > 1) { + maxHeap.poll(); + } + return maxHeap.peek(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1991.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1991.java new file mode 100644 index 0000000000..017986d7f6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1991.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1991 { + public static class Solution1 { + public int findMiddleIndex(int[] nums) { + int middleIndex = -1; + long sum = 0; + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; + } + long leftSum = 0; + for (int i = 0; i < nums.length; i++) { + sum -= nums[i]; + if (i > 0) { + leftSum += nums[i - 1]; + } + if (sum == leftSum) { + return i; + } + } + return middleIndex; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1992.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1992.java new file mode 100644 index 0000000000..055170eba2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1992.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _1992 { + public static class Solution1 { + public int[][] findFarmland(int[][] land) { + List farms = new ArrayList<>(); + int m = land.length; + int n = land[0].length; + boolean[][] visited = new boolean[m][n]; + int[] directions = new int[] {0, 1, 0, -1, 0}; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (land[i][j] == 1 && !visited[i][j]) { + visited[i][j] = true; + Queue queue = new LinkedList<>(); + queue.offer(new int[] {i, j}); + int[] coords = new int[] {i, j, i, j}; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int k = 0; k < size; k++) { + int[] curr = queue.poll(); + for (int p = 0; p < directions.length - 1; p++) { + int newX = directions[p] + curr[0]; + int newY = directions[p + 1] + curr[1]; + if (newX >= 0 + && newY >= 0 + && newX < m + && newY < n + && land[newX][newY] == 1 + && !visited[newX][newY]) { + visited[newX][newY] = true; + queue.offer(new int[] {newX, newY}); + coords[0] = Math.min(coords[0], newX); + coords[1] = Math.min(coords[1], newY); + coords[2] = Math.max(coords[2], newX); + coords[3] = Math.max(coords[3], newY); + } + } + } + } + farms.add(coords); + } + } + } + int[][] result = new int[farms.size()][4]; + for (int i = 0; i < farms.size(); i++) { + for (int j = 0; j < farms.get(i).length; j++) { + result[i][j] = farms.get(i)[j]; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1993.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1993.java new file mode 100644 index 0000000000..5b336fa469 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1993.java @@ -0,0 +1,121 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _1993 { + public static class Solution1 { + /* + * My completely original solution: + * 1. use hashmap1 to store num to node mapping; + * 2. use hashmap2 to store num to user lock mapping; + * 3. use hashmap3 to store child to parent mapping; + * 4. build the tree: make sure to retrieve the node from map if it exists, otherwise, the tree might be disconnected, i.e. leading to wrong ansers. + */ + public static class LockingTree { + class TreeNode { + List children; + int val; + + public TreeNode(int val) { + this.val = val; + this.children = new ArrayList<>(); + } + } + + Map map; + Map childToParentMap; + Map lockMap; + TreeNode root; + + public LockingTree(int[] parent) { + this.map = new HashMap<>(); + this.root = new TreeNode(0); + this.map.put(0, root); + this.childToParentMap = new HashMap<>(); + constructTree(parent, map, childToParentMap); + this.lockMap = new HashMap<>(); + } + + private void constructTree( + int[] parent, + Map map, + Map childToParentMap) { + for (int i = 1; i < parent.length; i++) { + TreeNode parentNode = map.getOrDefault(parent[i], new TreeNode(parent[i])); + TreeNode childNode = map.getOrDefault(i, new TreeNode(i)); + parentNode.children.add(childNode); + map.put(parent[i], parentNode); + map.put(i, childNode); + childToParentMap.put(childNode, parentNode); + } + } + + public boolean lock(int num, int user) { + if (lockMap.containsKey(num)) { + return false; + } else { + lockMap.put(num, user); + return true; + } + } + + public boolean unlock(int num, int user) { + if (!lockMap.containsKey(num)) { + return false; + } else if (lockMap.get(num) == user || user == -1) { + lockMap.remove(num); + return true; + } else { + return false; + } + } + + public boolean upgrade(int num, int user) { + if (hasLockedAncestor(num) || !hasOneLockedChild(num) || lockMap.containsKey(num)) { + return false; + } + lock(num, user); + List children = map.get(num).children; + for (TreeNode child : children) { + unlockRegardlessUser(child); + } + return true; + } + + private boolean hasOneLockedChild(int num) { + if (lockMap.containsKey(num)) { + return true; + } + TreeNode node = map.get(num); + for (TreeNode child : node.children) { + if (hasOneLockedChild(child.val)) { + return true; + } + } + return false; + } + + private boolean hasLockedAncestor(int num) { + TreeNode node = map.get(num); + while (childToParentMap.containsKey(node)) { + TreeNode parent = childToParentMap.get(node); + if (lockMap.containsKey(parent.val)) { + return true; + } + node = parent; + } + return false; + } + + private void unlockRegardlessUser(TreeNode treeNode) { + unlock(treeNode.val, -1); + for (TreeNode child : treeNode.children) { + unlockRegardlessUser(child); + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1995.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1995.java new file mode 100644 index 0000000000..8db903cef7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1995.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.secondthousand; + +public class _1995 { + public static class Solution1 { + public int countQuadruplets(int[] nums) { + int count = 0; + for (int i = 0; i < nums.length - 3; i++) { + for (int j = i + 1; j < nums.length - 2; j++) { + for (int k = j + 1; k < nums.length - 1; k++) { + for (int l = k + 1; l < nums.length; l++) { + if (nums[i] + nums[j] + nums[k] == nums[l]) { + count++; + } + } + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/secondthousand/_1996.java b/src/main/java/com/fishercoder/solutions/secondthousand/_1996.java new file mode 100644 index 0000000000..3639935634 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/secondthousand/_1996.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.secondthousand; + +import java.util.Arrays; + +public class _1996 { + public static class Solution1 { + public int numberOfWeakCharacters(int[][] properties) { + int count = 0; + /*sort them based on: + * if attack values equal, then sort by defense value ascendingly + * if not, sort by attack values descendingly. + * */ + Arrays.sort(properties, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]); + int max = 0; + for (int i = 0; i < properties.length; i++) { + count += max > properties[i][1] ? 1 : 0; + max = Math.max(max, properties[i][1]); + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2000.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2000.java new file mode 100644 index 0000000000..1a4c5986f5 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2000.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2000 { + public static class Solution1 { + public String reversePrefix(String word, char ch) { + int end = word.length(); + for (int i = 0; i < word.length(); i++) { + if (word.charAt(i) == ch) { + end = i; + break; + } + } + if (end == word.length()) { + return word; + } + StringBuilder sb = new StringBuilder(word.substring(0, end + 1)); + sb.reverse(); + sb.append(word.substring(end + 1)); + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2001.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2001.java new file mode 100644 index 0000000000..12394eec2a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2001.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2001 { + public static class Solution1 { + /* + * credit: https://github.com/fishercoder1534/Leetcode/blob/master/python3/2001.py + */ + public long interchangeableRectangles(int[][] rectangles) { + Map map = new HashMap<>(); + for (int[] rec : rectangles) { + double ratio = (double) rec[0] / rec[1]; + map.put(ratio, map.getOrDefault(ratio, 0) + 1); + } + long answer = 0; + for (double ratio : map.keySet()) { + int count = map.get(ratio); + answer += (long) count * (count - 1) / 2; + } + return answer; + } + } + + public static class Solution2 { + /* + * credit: https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/1458404/Java-or-HashMap + *

+ * This is an even smarter way to solve this problem: + * whenever we encounter a rectangle that has the samw ratio we met before, just increment the answer by the count of this ratio + * because this new rectangle could be matched up with all previously encountered ones! + *

+ * This is 100% beat on all submissions on space and time as of 9/12/2021. + */ + public long interchangeableRectangles(int[][] rectangles) { + Map map = new HashMap<>(); + long ans = 0L; + for (int[] rec : rectangles) { + double ratio = (double) rec[0] / rec[1]; + ans += map.getOrDefault(ratio, 0); + map.put(ratio, map.getOrDefault(ratio, 0) + 1); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2006.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2006.java new file mode 100644 index 0000000000..f189532498 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2006.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2006 { + public static class Solution1 { + public int countKDifference(int[] nums, int k) { + int pairs = 0; + for (int i = 0; i < nums.length - 1; i++) { + for (int j = i + 1; j < nums.length; j++) { + if (Math.abs(nums[i] - nums[j]) == k) { + pairs++; + } + } + } + return pairs; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2007.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2007.java new file mode 100644 index 0000000000..16a1afc8bd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2007.java @@ -0,0 +1,55 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class _2007 { + public static class Solution1 { + /* + * My completely original, but a bit lengthy solution. + */ + public int[] findOriginalArray(int[] changed) { + if (changed.length % 2 != 0) { + return new int[] {}; + } + Arrays.sort(changed); + int[] ans = new int[changed.length / 2]; + int k = 0; + Map map = new HashMap<>(); + for (int i = 0; i < changed.length; i++) { + map.put(changed[i], map.getOrDefault(changed[i], 0) + 1); + } + for (int i = changed.length - 1; i >= 0; i--) { + int doubledNumber = changed[i]; + if (map.containsKey(doubledNumber)) { + int doubledNumberCount = map.get(doubledNumber); + int halfNumber = doubledNumber / 2; + if (!map.containsKey(halfNumber) + || map.get(halfNumber) < doubledNumberCount + || halfNumber * 2 != doubledNumber) { + return new int[] {}; + } else { + if (doubledNumber == halfNumber && map.get(halfNumber) % 2 == 0) { + doubledNumberCount /= 2; + while (doubledNumberCount-- > 0) { + ans[k++] = halfNumber; + } + map.remove(halfNumber); + } else { + map.put(halfNumber, map.get(halfNumber) - doubledNumberCount); + if (map.get(halfNumber) == 0) { + map.remove(halfNumber); + } + while (doubledNumberCount-- > 0) { + ans[k++] = halfNumber; + } + map.remove(doubledNumber); + } + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2011.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2011.java new file mode 100644 index 0000000000..08f5d6c201 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2011.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2011 { + public static class Solution1 { + public int finalValueAfterOperations(String[] operations) { + int val = 0; + for (String op : operations) { + if (op.equals("++X") || op.equals("X++")) { + val++; + } else { + val--; + } + } + return val; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2012.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2012.java new file mode 100644 index 0000000000..865d36b323 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2012.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2012 { + public static class Solution1 { + public int sumOfBeauties(int[] nums) { + int[] preMax = new int[nums.length]; + preMax[0] = nums[0]; + int max = nums[0]; + for (int i = 1; i < nums.length; i++) { + max = Math.max(max, nums[i]); + preMax[i] = max; + } + int[] postMin = new int[nums.length]; + Arrays.fill(postMin, Integer.MAX_VALUE); + int min = nums[nums.length - 1]; + postMin[nums.length - 1] = nums[nums.length - 1]; + for (int i = nums.length - 2; i >= 0; i--) { + min = Math.min(min, nums[i]); + postMin[i] = min; + } + int sum = 0; + for (int i = 1; i <= nums.length - 2; i++) { + if (nums[i] > preMax[i - 1] && nums[i] < postMin[i + 1]) { + sum += 2; + } else if (nums[i] > nums[i - 1] && nums[i] < nums[i + 1]) { + sum++; + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2016.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2016.java new file mode 100644 index 0000000000..df7b1cf14e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2016.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2016 { + public static class Solution1 { + public int maximumDifference(int[] nums) { + int maxDiff = -1; + for (int i = 0; i < nums.length - 1; i++) { + for (int j = i + 1; j < nums.length; j++) { + if (nums[j] >= nums[i]) { + maxDiff = Math.max(nums[j] - nums[i], maxDiff); + } + } + } + return maxDiff; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2017.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2017.java new file mode 100644 index 0000000000..d2b26815e0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2017.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2017 { + public static class Solution1 { + /* + * credit: https://leetcode.com/uwi/ + */ + public long gridGame(int[][] grid) { + int n = grid[0].length; + long[] cum0 = new long[n + 1]; + long[] cum1 = new long[n + 1]; + for (int i = 0; i < n; i++) { + cum0[i + 1] = cum0[i] + grid[0][i]; + cum1[i + 1] = cum1[i] + grid[1][i]; + } + long ans = Long.MAX_VALUE; + for (int i = 0; i < n; i++) { + ans = Math.min(ans, Math.max(cum0[n] - cum0[i + 1], cum1[i])); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2018.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2018.java new file mode 100644 index 0000000000..9bbadc9200 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2018.java @@ -0,0 +1,91 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2018 { + public static class Solution1 { + public boolean placeWordInCrossword(char[][] board, String word) { + int m = board.length; + int n = board[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == ' ' || board[i][j] == word.charAt(0)) { + if (canPlaceTopDown(word, board, i, j) + || canPlaceLeftRight(word, board, i, j) + || canPlaceBottomUp(word, board, i, j) + || canPlaceRightLeft(word, board, i, j)) { + return true; + } + } + } + } + return false; + } + + private boolean canPlaceRightLeft(String word, char[][] board, int row, int col) { + if (col + 1 < board[0].length + && (Character.isLowerCase(board[row][col + 1]) || board[row][col + 1] == ' ')) { + return false; + } + int k = 0; + int j = col; + for (; j >= 0 && k < word.length(); j--) { + if (board[row][j] != word.charAt(k) && board[row][j] != ' ') { + return false; + } else { + k++; + } + } + return k == word.length() && (j < 0 || board[row][j] == '#'); + } + + private boolean canPlaceBottomUp(String word, char[][] board, int row, int col) { + if (row + 1 < board.length + && (Character.isLowerCase(board[row + 1][col]) || board[row + 1][col] == ' ')) { + return false; + } + int k = 0; + int i = row; + for (; i >= 0 && k < word.length(); i--) { + if (board[i][col] != word.charAt(k) && board[i][col] != ' ') { + return false; + } else { + k++; + } + } + return k == word.length() && (i < 0 || board[i][col] == '#'); + } + + private boolean canPlaceLeftRight(String word, char[][] board, int row, int col) { + if (col > 0 + && (Character.isLowerCase(board[row][col - 1]) || board[row][col - 1] == ' ')) { + return false; + } + int k = 0; + int j = col; + for (; j < board[0].length && k < word.length(); j++) { + if (board[row][j] != word.charAt(k) && board[row][j] != ' ') { + return false; + } else { + k++; + } + } + return k == word.length() && (j == board[0].length || board[row][j] == '#'); + } + + private boolean canPlaceTopDown(String word, char[][] board, int row, int col) { + if (row > 0 + && (Character.isLowerCase(board[row - 1][col]) || board[row - 1][col] == ' ')) { + return false; + } + int k = 0; + int i = row; + for (; i < board.length && k < word.length(); i++) { + if (board[i][col] != word.charAt(k) && board[i][col] != ' ') { + return false; + } else { + k++; + } + } + return k == word.length() && (i == board.length || board[i][col] == '#'); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2022.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2022.java new file mode 100644 index 0000000000..79e128d4c6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2022.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2022 { + public static class Solution1 { + public int[][] construct2DArray(int[] original, int m, int n) { + if (m * n != original.length) { + return new int[][] {}; + } + int[][] ans = new int[m][n]; + int k = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + ans[i][j] = original[k++]; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2023.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2023.java new file mode 100644 index 0000000000..4d80c4934c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2023.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2023 { + public static class Solution1 { + public int numOfPairs(String[] nums, String target) { + int ans = 0; + for (int i = 0; i < nums.length; i++) { + for (int j = 0; j < nums.length; j++) { + if (i != j) { + String con = nums[i] + nums[j]; + if (con.equals(target)) { + ans++; + } + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2024.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2024.java new file mode 100644 index 0000000000..b13a0c5117 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2024.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2024 { + public static class Solution1 { + public int maxConsecutiveAnswers(String answerKey, int k) { + int max; + // change T to F and count number of Fs + int right = 0; + int originalK = k; + while (k > 0 && right < answerKey.length()) { + if (answerKey.charAt(right) == 'T') { + k--; + } + right++; + } + max = right; + int left = 0; + while (right < answerKey.length() && left < answerKey.length()) { + if (answerKey.charAt(right) == 'F') { + right++; + max = Math.max(max, right - left); + } else { + while (left < right && answerKey.charAt(left) == 'F') { + left++; + } + left++; + right++; + } + } + + // change F to T + right = 0; + k = originalK; + while (k > 0 && right < answerKey.length()) { + if (answerKey.charAt(right) == 'F') { + k--; + } + right++; + } + max = Math.max(max, right); + left = 0; + while (right < answerKey.length() && left < answerKey.length()) { + if (answerKey.charAt(right) == 'T') { + right++; + max = Math.max(max, right - left); + } else { + while (left < right && answerKey.charAt(left) == 'T') { + left++; + } + left++; + right++; + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2027.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2027.java new file mode 100644 index 0000000000..c8c17b40de --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2027.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2027 { + public static class Solution1 { + public int minimumMoves(String s) { + int moves = 0; + for (int i = 0; i < s.length(); ) { + if (s.charAt(i) == 'O') { + i++; + continue; + } + String candidate = i + 3 <= s.length() ? s.substring(i, i + 3) : s.substring(i); + if (candidate.contains("X")) { + moves++; + i += 3; + } else { + i++; + } + } + return moves; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2028.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2028.java new file mode 100644 index 0000000000..29d163e437 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2028.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2028 { + public static class Solution1 { + public int[] missingRolls(int[] rolls, int mean, int n) { + long sum = 0L; + for (int num : rolls) { + sum += num; + } + long totalSum = (rolls.length + n) * mean; + long remainder = totalSum - sum; + if (remainder / n > 6 + || (remainder / n == 6 && remainder % n != 0) + || remainder / n < 0 + || remainder < n) { + return new int[] {}; + } + int ave = (int) (remainder / n); + int remain = (int) (remainder % n); + int[] ans = new int[n]; + int k = 0; + while (k < n) { + ans[k++] = ave + remain > 0 ? 1 : 0; + remain--; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2032.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2032.java new file mode 100644 index 0000000000..72438d940b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2032.java @@ -0,0 +1,46 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _2032 { + public static class Solution1 { + public List twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) { + Set ans = new HashSet<>(); + Set set1 = new HashSet<>(); + for (int i : nums1) { + set1.add(i); + } + Set set2 = new HashSet<>(); + for (int i : nums2) { + set2.add(i); + } + Set set3 = new HashSet<>(); + for (int i : nums3) { + set3.add(i); + } + for (int i = 0; i < nums1.length; i++) { + if (set2.contains(nums1[i]) || set3.contains(nums1[i])) { + ans.add(nums1[i]); + } + } + for (int i = 0; i < nums2.length; i++) { + if (set1.contains(nums2[i]) || set3.contains(nums2[i])) { + ans.add(nums2[i]); + } + } + for (int i = 0; i < nums3.length; i++) { + if (set1.contains(nums3[i]) || set2.contains(nums3[i])) { + ans.add(nums3[i]); + } + } + List result = new ArrayList<>(); + for (int i : ans) { + result.add(i); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2033.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2033.java new file mode 100644 index 0000000000..16138a125c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2033.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _2033 { + public static class Solution1 { + public int minOperations(int[][] grid, int x) { + List list = new ArrayList<>(); + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + list.add(grid[i][j]); + } + } + if (list.size() <= 1) { + return 0; + } + Collections.sort(list); + int median = list.get(list.size() / 2); + int ops = 0; + for (int i = 0; i < list.size(); i++) { + int diff = Math.abs(list.get(i) - median); + if (diff % x != 0) { + return -1; + } + ops += diff; + } + return ops / x; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2034.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2034.java new file mode 100644 index 0000000000..fadc409186 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2034.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; +import java.util.TreeSet; + +public class _2034 { + public static class Solution1 { + class StockPrice { + TreeSet treeSet; + Map map; + Map countMap; + int current; + + public StockPrice() { + treeSet = new TreeSet<>(); + map = new HashMap<>(); + countMap = new HashMap<>(); + current = 0; + } + + public void update(int timestamp, int price) { + if (map.containsKey(timestamp)) { + int previousPrice = map.get(timestamp); + countMap.put(previousPrice, countMap.getOrDefault(previousPrice, 0) - 1); + if (countMap.get(previousPrice) <= 0) { + countMap.remove(previousPrice); + treeSet.remove(previousPrice); + } + } + map.put(timestamp, price); + treeSet.add(price); + countMap.put(price, countMap.getOrDefault(price, 0) + 1); + if (current < timestamp) { + current = timestamp; + } + } + + public int current() { + return map.get(current); + } + + public int maximum() { + return treeSet.last(); + } + + public int minimum() { + return treeSet.first(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2037.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2037.java new file mode 100644 index 0000000000..eab01552ca --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2037.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2037 { + public static class Solution1 { + public int minMovesToSeat(int[] seats, int[] students) { + int ans = 0; + Arrays.sort(seats); + Arrays.sort(students); + for (int i = 0; i < seats.length; i++) { + ans += Math.abs(seats[i] - students[i]); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2038.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2038.java new file mode 100644 index 0000000000..47f79e5a95 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2038.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2038 { + public static class Solution1 { + public boolean winnerOfGame(String colors) { + int ans = 0; + for (int i = 1; i < colors.length() - 1; i++) { + if (colors.charAt(i) == colors.charAt(i - 1) + && colors.charAt(i) == colors.charAt(i + 1)) { + if (colors.charAt(i) == 'A') { + ans++; + } else { + ans--; + } + } + } + return ans > 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2039.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2039.java new file mode 100644 index 0000000000..6d091532d6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2039.java @@ -0,0 +1,79 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _2039 { + public static class Solution1 { + /* + * My completely original solution, again, using a pen and paper to visualize my thought process helps out greatly! + */ + public int networkBecomesIdle(int[][] edges, int[] patience) { + int n = patience.length; + int[] distances = findShortestPathToMasterServer(edges, n); + int seconds = 0; + for (int i = 1; i < n; i++) { + int roundTripTime = distances[i] * 2; + int numberOfMessages = roundTripTime / patience[i]; + int lastMessageArriveTime = roundTripTime; + if (roundTripTime > patience[i]) { + lastMessageArriveTime += + patience[i] + * (roundTripTime % patience[i] == 0 + ? (numberOfMessages - 1) + : numberOfMessages); + } + seconds = Math.max(seconds, lastMessageArriveTime); + } + return seconds + 1; + } + + private int[] findShortestPathToMasterServer(int[][] edges, int n) { + int[] distances = new int[n]; + Arrays.fill(distances, n); + Queue queue = new LinkedList<>(); + Map> neighborsMap = new HashMap<>(); + for (int[] edge : edges) { + if (edge[0] == 0) { + queue.offer(edge[1]); + } + if (edge[1] == 0) { + queue.offer(edge[0]); + } + Set set1 = neighborsMap.getOrDefault(edge[0], new HashSet<>()); + set1.add(edge[1]); + neighborsMap.put(edge[0], set1); + Set set2 = neighborsMap.getOrDefault(edge[1], new HashSet<>()); + set2.add(edge[0]); + neighborsMap.put(edge[1], set2); + } + int distance = 1; + boolean[] visited = new boolean[n]; + visited[0] = true; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + Integer curr = queue.poll(); + if (visited[curr]) { + continue; + } + visited[curr] = true; + distances[curr] = Math.min(distance, distances[curr]); + Set neighbors = neighborsMap.getOrDefault(curr, new HashSet<>()); + for (int neighbor : neighbors) { + if (!visited[neighbor]) { + queue.offer(neighbor); + } + } + } + distance++; + } + return distances; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2042.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2042.java new file mode 100644 index 0000000000..5ece1e79fb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2042.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2042 { + public static class Solution1 { + public boolean areNumbersAscending(String s) { + String[] words = s.split("\\ "); + int prev = 0; + for (String word : words) { + if (Character.isDigit(word.charAt(0))) { + if (Integer.parseInt(word) <= prev) { + return false; + } else { + prev = Integer.parseInt(word); + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2043.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2043.java new file mode 100644 index 0000000000..ab27899c9c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2043.java @@ -0,0 +1,58 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2043 { + public static class Solution1 { + class Bank { + + List list; + + public Bank(long[] balance) { + list = new ArrayList<>(); + for (long b : balance) { + list.add(b); + } + } + + public boolean transfer(int account1, int account2, long money) { + if (account1 - 1 >= list.size()) { + return false; + } else if (list.get(account1 - 1) < money) { + return false; + } else if (account2 - 1 >= list.size()) { + return false; + } else { + Long original = list.remove(account1 - 1); + list.add(account1 - 1, original - money); + Long remove = list.remove(account2 - 1); + list.add(account2 - 1, remove + money); + return true; + } + } + + public boolean deposit(int account, long money) { + if (account - 1 >= list.size()) { + return false; + } else { + Long original = list.remove(account - 1); + list.add(account - 1, original + money); + return true; + } + } + + public boolean withdraw(int account, long money) { + if (account - 1 >= list.size()) { + return false; + } else if (list.get(account - 1) < money) { + return false; + } else { + Long original = list.remove(account - 1); + list.add(account - 1, original - money); + return true; + } + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2044.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2044.java new file mode 100644 index 0000000000..101e1353b1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2044.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _2044 { + public static class Solution1 { + public int countMaxOrSubsets(int[] nums) { + List> result = new ArrayList(); + List empty = new ArrayList(); + result.add(empty); + Arrays.sort(nums); + for (int i = 0; i < nums.length; i++) { + List> temp = new ArrayList<>(); + for (List list : result) { + List newList = new ArrayList(list); + newList.add(nums[i]); + temp.add(newList); + } + result.addAll(temp); + } + Map map = new HashMap<>(); + for (List list : result) { + if (list.isEmpty() || list.size() == 0) { + continue; + } + int or = list.get(0); + for (int i = 1; i < list.size(); i++) { + or |= list.get(i); + } + map.put(or, map.getOrDefault(or, 0) + 1); + } + int max = 0; + int ans = 0; + for (int key : map.keySet()) { + if (max < key) { + max = key; + ans = map.get(key); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2047.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2047.java new file mode 100644 index 0000000000..448a87e8fd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2047.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2047 { + public static class Solution1 { + public int countValidWords(String sentence) { + String[] tokens = sentence.split("\\s+"); + int count = 0; + for (String token : tokens) { + int hyphenCount = 0; + int punctuationMarkCount = 0; + boolean valid = true; + if (token.isEmpty() || token.equals("") || token.length() == 0) { + continue; + } + for (int i = 0; i < token.length(); i++) { + if (token.charAt(i) == '-') { + hyphenCount++; + if (hyphenCount > 1 + || i == 0 + || i == token.length() - 1 + || !Character.isAlphabetic(token.charAt(i - 1)) + || !Character.isAlphabetic(token.charAt(i + 1))) { + valid = false; + break; + } + } else if (token.charAt(i) == '!' + || token.charAt(i) == '.' + || token.charAt(i) == ',') { + punctuationMarkCount++; + if (punctuationMarkCount > 1 || i != token.length() - 1) { + valid = false; + break; + } + } else if (Character.isDigit(token.charAt(i))) { + valid = false; + break; + } else if (Character.isDigit(token.charAt(i))) { + valid = false; + break; + } + } + if (valid) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2048.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2048.java new file mode 100644 index 0000000000..d4d5971042 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2048.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2048 { + public static class Solution1 { + public int nextBeautifulNumber(int n) { + int ans = n; + do { + ans++; + if (isNumeric(ans)) { + return ans; + } + } while (true); + } + + private boolean isNumeric(int number) { + Map map = new HashMap<>(); + while (number != 0) { + int digit = number % 10; + map.put(digit, map.getOrDefault(digit, 0) + 1); + number /= 10; + } + for (int key : map.keySet()) { + if (key != map.get(key) || (key == 0 && map.get(key) != 0)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2049.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2049.java new file mode 100644 index 0000000000..e8f9f54d11 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2049.java @@ -0,0 +1,101 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _2049 { + public static class Solution1 { + /* + * My completely original solution. + * Practice makes perfect! + */ + public int countHighestScoreNodes(int[] parents) { + Map valToNodeMap = new HashMap<>(); + TreeNode root = buildBinaryTree(parents, valToNodeMap); + + // it'll be handy if we can cache the number of children each node has as we'll do this + // many times, so we can quickly calculate the score for each node + // key is the node since each node's value is unique, value if the number of children + // this node has + Map nodeCountMap = new HashMap<>(); + // naturally we should use post-order traversal since we need to count the children for + // each child first, then we can roll up to add one to get the number of children for + // the root node + long allNodeCount = postOrder(root, nodeCountMap); + nodeCountMap.put(root.val, allNodeCount); + + // now calculate the score of each node + List scoreList = new ArrayList<>(); + long highestScore = 0; + for (int i = 0; i < parents.length; i++) { + long score = computeScore(i, nodeCountMap, valToNodeMap); + highestScore = Math.max(score, highestScore); + scoreList.add(score); + } + int count = 0; + for (long score : scoreList) { + if (score == highestScore) { + count++; + } + } + return count; + } + + private Long computeScore( + int nodeVal, Map nodeCountMap, Map nodeValueMap) { + // since this is a binary tree, so, at most, removing a node, it'll split the original + // tree into three disjoint trees + TreeNode node = nodeValueMap.get(nodeVal); + Long leftSubtree = 1L; + Long rightSubtree = 1L; + Long parentSubtree = 1L; + if (node.left != null) { + if (nodeCountMap.get(node.left.val) > 0) { + leftSubtree = nodeCountMap.get(node.left.val); + } + } + if (node.right != null) { + if (nodeCountMap.get(node.right.val) > 0) { + rightSubtree = nodeCountMap.get(node.right.val); + } + } + if (nodeVal != 0) { + long diff = nodeCountMap.get(0) - nodeCountMap.get(nodeVal); + if (diff > 0) { + parentSubtree = diff; + } + } + return leftSubtree * rightSubtree * parentSubtree; + } + + private long postOrder(TreeNode root, Map map) { + if (root == null) { + return 0; + } + long leftCount = postOrder(root.left, map); + long rightCount = postOrder(root.right, map); + long sum = leftCount + rightCount + 1; + map.put(root.val, sum); + return sum; + } + + private TreeNode buildBinaryTree(int[] parents, Map map) { + map.put(0, new TreeNode(0)); + for (int i = 1; i < parents.length; i++) { + TreeNode childNode = map.getOrDefault(i, new TreeNode(i)); + TreeNode parentNode = map.getOrDefault(parents[i], new TreeNode(parents[i])); + if (parentNode.left == null) { + parentNode.left = childNode; + } else { + parentNode.right = childNode; + } + map.put(parents[i], parentNode); + map.put(i, childNode); + } + return map.get(0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2050.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2050.java new file mode 100644 index 0000000000..5e88aa0bd2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2050.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _2050 { + public static class Solution1 { + + /* + * My original solution, but results in TLE on LeetCode at 39/40 test cases... + */ + public int minimumTime(int n, int[][] relations, int[] time) { + Map> prereqMap = new HashMap<>(); + for (int[] rel : relations) { + int pre = rel[0]; + int then = rel[1]; + Set list = prereqMap.getOrDefault(then, new HashSet<>()); + list.add(pre); + prereqMap.put(then, list); + } + Queue queue = new LinkedList<>(); + Map minMap = new HashMap<>(); + for (int i = 0; i < n; i++) { + if (!prereqMap.containsKey(i + 1)) { + minMap.put(i + 1, time[i]); + } else { + queue.add(i + 1); + } + } + int minTime = 0; + while (!queue.isEmpty()) { + Integer curr = queue.poll(); + int thisMinTime = Integer.MIN_VALUE; + Set reqs = prereqMap.get(curr); + boolean valid = true; + for (int r : reqs) { + if (!minMap.containsKey(r)) { + queue.offer(curr); + valid = false; + break; + } else { + thisMinTime = Math.max(minMap.get(r), thisMinTime); + } + } + if (valid) { + minMap.put(curr, thisMinTime + time[curr - 1]); + } + } + for (int key : minMap.keySet()) { + minTime = Math.max(minTime, minMap.get(key)); + } + return minTime; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2053.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2053.java new file mode 100644 index 0000000000..efbb73457f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2053.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2053 { + public static class Solution1 { + public String kthDistinct(String[] arr, int k) { + Map map = new HashMap<>(); + for (String s : arr) { + map.put(s, map.getOrDefault(s, 0) + 1); + } + int count = 0; + for (String s : arr) { + if (map.get(s) == 1) { + count++; + if (k == count) { + return s; + } + } + } + return ""; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2054.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2054.java new file mode 100644 index 0000000000..35eac2762e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2054.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2054 { + public static class Solution1 { + public int maxTwoEvents(int[][] events) { + /*Credit: https://leetcode.com/nevergiveup/ on https://leetcode.com/contest/biweekly-contest-64/ranking/*/ + Arrays.sort(events, (a, b) -> a[0] - b[0]); + int[] max = new int[events.length]; + for (int i = events.length - 1; i >= 0; i--) { + if (i == events.length - 1) { + max[i] = events[i][2]; + } else { + max[i] = Math.max(events[i][2], max[i + 1]); + } + } + int ans = 0; + for (int i = 0; i < events.length; i++) { + int end = events[i][1]; + int left = i + 1; + int right = events.length; + while (left < right) { + int mid = left + (right - left) / 2; + if (events[mid][0] <= end) { + left = mid + 1; + } else { + right = mid; + } + } + int value = events[i][2]; + if (right >= 0 && right < events.length) { + value += max[right]; + } + ans = Math.max(ans, value); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2055.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2055.java new file mode 100644 index 0000000000..38cc4bba8a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2055.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.TreeMap; + +public class _2055 { + public static class Solution1 { + public int[] platesBetweenCandles(String s, int[][] queries) { + int[] ans = new int[queries.length]; + TreeMap treeMap = new TreeMap<>(); + int left = 0; + int right = 0; + while (left < s.length() && s.charAt(left) != '|') { + left++; + } + treeMap.put(left, 0); + int count = 0; + while (left < s.length()) { + if (s.charAt(left) == '|') { + right = left + 1; + while (right < s.length() && s.charAt(right) != '|') { + count++; + right++; + } + if (right < s.length()) { + treeMap.put(right, count); + } + left = right; + } else { + left++; + } + } + for (int i = 0; i < queries.length; i++) { + int l = queries[i][0]; + int r = queries[i][1]; + Integer start = treeMap.ceilingKey(l); + Integer end = treeMap.floorKey(r); + if (start == null || end == null || start >= end) { + continue; + } + ans[i] = treeMap.get(end) - treeMap.get(start); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2057.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2057.java new file mode 100644 index 0000000000..f2346f90b1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2057.java @@ -0,0 +1,14 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2057 { + public static class Solution1 { + public int smallestEqual(int[] nums) { + for (int i = 0; i < nums.length; i++) { + if (i % 10 == nums[i]) { + return i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2058.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2058.java new file mode 100644 index 0000000000..621edad7c1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2058.java @@ -0,0 +1,74 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.List; + +public class _2058 { + public static class Solution1 { + public int[] nodesBetweenCriticalPoints(ListNode head) { + List list = new ArrayList<>(); + while (head != null) { + list.add(head.val); + head = head.next; + } + List criticalPts = new ArrayList<>(); + for (int i = 1; i < list.size() - 1; i++) { + if (list.get(i) > list.get(i - 1) && list.get(i) > list.get(i + 1)) { + criticalPts.add(i); + } else if (list.get(i) < list.get(i - 1) && list.get(i) < list.get(i + 1)) { + criticalPts.add(i); + } + } + if (criticalPts.size() < 2) { + return new int[] {-1, -1}; + } + int min = Integer.MAX_VALUE; + for (int i = 0; i < criticalPts.size() - 1; i++) { + min = Math.min(min, criticalPts.get(i + 1) - criticalPts.get(i)); + } + int size = criticalPts.size(); + return new int[] {min, criticalPts.get(size - 1) - criticalPts.get(0)}; + } + } + + public static class Solution2 { + /* + * Without using an extra list of size N to hold all values. + */ + public int[] nodesBetweenCriticalPoints(ListNode head) { + List criticalPoints = new ArrayList<>(); + int prev = head.val; + head = head.next; + int index = 1; + int[] result = new int[2]; + result[0] = Integer.MAX_VALUE; + result[1] = Integer.MIN_VALUE; + while (head != null && head.next != null) { + if (head.val > prev && head.val > head.next.val) { + criticalPoints.add(index); + } else if (head.val < prev && head.val < head.next.val) { + criticalPoints.add(index); + } + if (criticalPoints.size() > 1) { + int len = criticalPoints.size(); + result[0] = + Math.min( + result[0], + criticalPoints.get(len - 1) - criticalPoints.get(len - 2)); + } + prev = head.val; + head = head.next; + index++; + } + if (criticalPoints.size() > 1) { + int len = criticalPoints.size(); + result[1] = + Math.max(result[1], criticalPoints.get(len - 1) - criticalPoints.get(0)); + return result; + } else { + return new int[] {-1, -1}; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2062.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2062.java new file mode 100644 index 0000000000..04b589d16d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2062.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _2062 { + public static class Solution1 { + public int countVowelSubstrings(String word) { + int count = 0; + Set vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u')); + Set window = new HashSet<>(); + for (int i = 0; i < word.length(); i++) { + window.clear(); + if (vowels.contains(word.charAt(i))) { + window.add(word.charAt(i)); + for (int j = i + 1; j < word.length(); j++) { + if (!vowels.contains(word.charAt(j))) { + break; + } else { + window.add(word.charAt(j)); + if (window.size() == 5) { + count++; + } + } + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2063.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2063.java new file mode 100644 index 0000000000..3c8e40e6e3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2063.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2063 { + public static class Solution1 { + /* + * credit: https://leetcode.com/nevergiveup/ + */ + public long countVowels(String word) { + long ans = 0; + for (int i = 0; i < word.length(); i++) { + if (isVowel(word.charAt(i))) { + long left = i; + long right = word.length() - left - 1; + ans += (left + 1) * (right + 1); + } + } + return ans; + } + + private boolean isVowel(char ch) { + return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2068.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2068.java new file mode 100644 index 0000000000..430bcc92a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2068.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2068 { + public static class Solution1 { + public boolean checkAlmostEquivalent(String word1, String word2) { + int[] count = new int[26]; + for (char c : word1.toCharArray()) { + count[c - 'a']++; + } + for (char c : word2.toCharArray()) { + count[c - 'a']--; + } + for (int i : count) { + if (Math.abs(i) > 3) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2070.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2070.java new file mode 100644 index 0000000000..133ba33ae0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2070.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2070 { + public static class Solution1 { + public int[] maximumBeauty(int[][] items, int[] queries) { + int len = queries.length; + Arrays.sort(items, (a, b) -> Integer.compare(a[0], b[0])); + int[][] queryPairs = new int[len][2]; + for (int i = 0; i < len; i++) { + queryPairs[i] = new int[] {queries[i], i}; + } + Arrays.sort(queryPairs, (a, b) -> Integer.compare(a[0], b[0])); + int[] ans = new int[len]; + int j = 0; + int max = 0; + for (int i = 0; i < len; i++) { + int[] queryPair = queryPairs[i]; + int price = queryPair[0]; + int index = queryPair[1]; + while (j < items.length && items[j][0] <= price) { + max = Math.max(max, items[j][1]); + j++; + } + ans[index] = max; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2073.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2073.java new file mode 100644 index 0000000000..446d7a1803 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2073.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _2073 { + public static class Solution1 { + public int timeRequiredToBuy(int[] tickets, int k) { + int time = 0; + Deque queue = new LinkedList<>(); + for (int i = 0; i < tickets.length; i++) { + queue.addLast(new int[] {tickets[i], i}); + } + while (!queue.isEmpty()) { + int[] curr = queue.pollFirst(); + if (curr[0] - 1 > 0) { + queue.addLast(new int[] {curr[0] - 1, curr[1]}); + } + time++; + if (curr[1] == k && curr[0] - 1 == 0) { + return time; + } + } + return time; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2074.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2074.java new file mode 100644 index 0000000000..e5d609c17a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2074.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _2074 { + public static class Solution1 { + public ListNode reverseEvenLengthGroups(ListNode head) { + List list = new ArrayList<>(); + ListNode tmp = head; + while (tmp != null) { + list.add(tmp.val); + tmp = tmp.next; + } + int len = 1; + int start = 0; + List reversed = new ArrayList<>(); + while (start + len <= list.size()) { + if (len % 2 == 0) { + List sub = list.subList(start, start + len); + Collections.reverse(sub); + reversed.addAll(sub); + } else { + reversed.addAll(list.subList(start, start + len)); + } + start += len; + len++; + } + if (start < list.size()) { + List sublist = list.subList(start, list.size()); + if (sublist.size() % 2 == 0) { + Collections.reverse(sublist); + } + reversed.addAll(sublist); + } + ListNode pre = new ListNode(-1); + head = new ListNode(reversed.get(0)); + pre.next = head; + for (int i = 1; i < reversed.size(); i++) { + head.next = new ListNode(reversed.get(i)); + head = head.next; + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2075.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2075.java new file mode 100644 index 0000000000..986145c79a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2075.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2075 { + public static class Solution1 { + public String decodeCiphertext(String encodedText, int rows) { + if (rows == 1) { + return encodedText; + } + int total = encodedText.length(); + int cols = total / rows; + char[][] grid = new char[rows][cols]; + int index = 0; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + grid[i][j] = encodedText.charAt(index++); + } + } + StringBuilder sb = new StringBuilder(); + int colIndex = 0; + while (colIndex < cols) { + for (int j = colIndex, i = 0; j < cols && i < rows; j++, i++) { + sb.append(grid[i][j]); + } + colIndex++; + } + int i = sb.length() - 1; + while (i >= 0 && sb.charAt(i) == ' ') { + i--; + } + return sb.substring(0, i + 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2076.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2076.java new file mode 100644 index 0000000000..e82671d4fc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2076.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2076 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/SaveVMK/ on https://leetcode.com/contest/weekly-contest-267/ranking/ + */ + public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) { + int[] head = new int[n]; + boolean[][] isr = new boolean[n][n]; + for (int i = 0; i < n; i++) { + head[i] = i; + } + List> ch = new ArrayList<>(); + for (int i = 0; i < n; i++) { + ch.add(new ArrayList<>()); + ch.get(i).add(i); + } + for (int[] res : restrictions) { + isr[res[0]][res[1]] = true; + isr[res[1]][res[0]] = true; + } + boolean[] ans = new boolean[requests.length]; + for (int i = 0; i < requests.length; i++) { + int u = head[requests[i][0]]; + int v = head[requests[i][1]]; + if (u == v) { + ans[i] = true; + continue; + } + if (isr[u][v]) { + continue; + } + ans[i] = true; + for (int v2 : ch.get(v)) { + ch.get(u).add(v2); + head[v2] = u; + } + for (int j = 0; j < n; j++) { + isr[u][j] |= isr[v][j]; + isr[j][u] |= isr[j][v]; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2078.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2078.java new file mode 100644 index 0000000000..6bc2cdfcbe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2078.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2078 { + public static class Solution1 { + public int maxDistance(int[] colors) { + int left = 0; + int right = colors.length - 1; + int max = 0; + while (left < right) { + if (colors[left] != colors[right]) { + max = Math.max(max, right - left); + break; + } else { + left++; + } + } + left = 0; + while (left < right) { + if (colors[left] != colors[right]) { + max = Math.max(max, right - left); + break; + } else { + right--; + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2079.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2079.java new file mode 100644 index 0000000000..d6766583c7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2079.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2079 { + public static class Solution1 { + public int wateringPlants(int[] plants, int capacity) { + int steps = 0; + int remainder = capacity; + for (int i = 0; i < plants.length; i++) { + if (plants[i] > remainder) { + steps += i * 2; + steps++; + remainder = capacity - plants[i]; + } else { + remainder -= plants[i]; + steps++; + } + } + return steps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2080.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2080.java new file mode 100644 index 0000000000..0c28cbcdfb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2080.java @@ -0,0 +1,65 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.*; + +public class _2080 { + public static class Solution1 { + + public static class RangeFreqQuery { + /* + * This post explains it well: https://leetcode.com/problems/range-frequency-queries/discuss/1589019/Java-or-Binary-Search-or-Log(n)-for-every-query + */ + + Map> map; + + public RangeFreqQuery(int[] arr) { + map = new HashMap<>(); + for (int i = 0; i < arr.length; i++) { + List list = map.getOrDefault(arr[i], new ArrayList<>()); + list.add(i); + map.put(arr[i], list); + } + } + + public int query(int left, int right, int value) { + List list = map.getOrDefault(value, new ArrayList<>()); + int start = Collections.binarySearch(list, left); + int end = Collections.binarySearch(list, right); + if (start < 0) { + start = (start + 1) * -1; + } + if (end < 0) { + end = (end + 2) * -1; + } + return end - start + 1; + } + } + } + + public static class Solution2 { + public static class RangeFreqQuery { + Map> map; + + public RangeFreqQuery(int[] arr) { + map = new HashMap<>(); + for (int i = 0; i < arr.length; i++) { + map.putIfAbsent(arr[i], new TreeMap<>()); + map.get(arr[i]).put(i, map.get(arr[i]).size()); + } + } + + public int query(int left, int right, int value) { + if (!map.containsKey(value)) { + return 0; + } + TreeMap indexMap = map.get(value); + Integer start = indexMap.ceilingKey(left); + Integer end = indexMap.floorKey(right); + if (start == null || end == null) { + return 0; + } + return indexMap.get(end) - indexMap.get(start) + 1; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2085.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2085.java new file mode 100644 index 0000000000..cf0c94eb49 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2085.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2085 { + public static class Solution1 { + public int countWords(String[] words1, String[] words2) { + Map map1 = new HashMap<>(); + Map map2 = new HashMap<>(); + for (String word : words1) { + map1.put(word, map1.getOrDefault(word, 0) + 1); + } + for (String word : words2) { + map2.put(word, map2.getOrDefault(word, 0) + 1); + } + int ans = 0; + for (String key : map1.keySet()) { + if (map1.get(key) == 1 && map2.containsKey(key) && map2.get(key) == 1) { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2086.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2086.java new file mode 100644 index 0000000000..570b120160 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2086.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2086 { + public static class Solution1 { + public int minimumBuckets(String street) { + int minBuckets = 0; + int[] buckets = new int[street.length()]; + for (int i = 0; i < street.length(); i++) { + if (street.charAt(i) == 'H') { + if (i + 1 < street.length() && street.charAt(i + 1) == '.') { + if (i - 1 >= 0 && buckets[i - 1] == 1) { + continue; + } + buckets[i + 1] = 1; + minBuckets++; + } else if (i + 1 < street.length() && street.charAt(i + 1) == 'H') { + if (i - 1 < 0) { + return -1; + } else if (i - 1 >= 0 && street.charAt(i - 1) == 'H') { + return -1; + } else { + if (buckets[i - 1] == 1) { + continue; + } else { + buckets[i - 1] = 1; + minBuckets++; + } + } + } else if (i + 1 >= street.length() && i - 1 < 0) { + return -1; + } else if (i - 1 >= 0 && street.charAt(i - 1) == '.') { + if (buckets[i - 1] == 1) { + continue; + } else { + minBuckets++; + buckets[i - 1] = 1; + } + } else if (i + 1 >= street.length() + && i - 1 >= 0 + && street.charAt(i - 1) == 'H') { + return -1; + } + } + } + return minBuckets; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2089.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2089.java new file mode 100644 index 0000000000..dca2d53d36 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2089.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _2089 { + public static class Solution1 { + public List targetIndices(int[] nums, int target) { + List ans = new ArrayList<>(); + Arrays.sort(nums); + for (int i = 0; i < nums.length; i++) { + if (nums[i] == target) { + ans.add(i); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2090.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2090.java new file mode 100644 index 0000000000..967b224d4f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2090.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2090 { + public static class Solution1 { + public int[] getAverages(int[] nums, int k) { + if (k == 0) { + return nums; + } + long[] preSums = new long[nums.length]; + preSums[0] = nums[0]; + for (int i = 1; i < nums.length; i++) { + preSums[i] = preSums[i - 1] + nums[i]; + } + int[] ans = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + if (i - k < 0) { + ans[i] = -1; + } else if (i + k >= nums.length) { + ans[i] = -1; + } else { + if (i - k == 0) { + ans[i] = (int) (preSums[i + k] / (2 * k + 1)); + } else { + ans[i] = (int) ((preSums[i + k] - preSums[i - k - 1]) / (2 * k + 1)); + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2091.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2091.java new file mode 100644 index 0000000000..ca96a60e92 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2091.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2091 { + public static class Solution1 { + public int minimumDeletions(int[] nums) { + int minIndex = 0; + int maxIndex = 0; + int min = nums[0]; + int max = nums[0]; + for (int i = 1; i < nums.length; i++) { + if (nums[i] > max) { + max = nums[i]; + maxIndex = i; + } + if (nums[i] < min) { + min = nums[i]; + minIndex = i; + } + } + int minDeletions = 0; + if (nums.length == 1) { + return 1; + } + int len = nums.length - 1; + int firstIndex = maxIndex > minIndex ? minIndex : maxIndex; + int secondIndex = maxIndex == firstIndex ? minIndex : maxIndex; + int firstDistance = firstIndex; + int lastDistance = len - secondIndex; + int betweenDistance = secondIndex - firstIndex; + if (firstDistance < lastDistance) { + minDeletions += firstDistance; + minDeletions++; + if (betweenDistance == 1 || lastDistance == 0) { + minDeletions++; + return minDeletions; + } + if (betweenDistance <= lastDistance) { + minDeletions += betweenDistance; + } else { + minDeletions += lastDistance; + minDeletions++; + } + } else { + minDeletions += lastDistance; + minDeletions++; + if (betweenDistance == 1 || firstDistance == 0) { + minDeletions++; + return minDeletions; + } + if (betweenDistance <= firstDistance) { + minDeletions += betweenDistance; + } else { + minDeletions += firstDistance; + minDeletions++; + } + } + return minDeletions; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2094.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2094.java new file mode 100644 index 0000000000..1a1e56934d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2094.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.*; + +public class _2094 { + public static class Solution1 { + public int[] findEvenNumbers(int[] digits) { + Set set = new HashSet<>(); + for (int i = 0; i < digits.length; i++) { + int number = digits[i] * 100; + if (digits[i] != 0) { + for (int j = 0; j < digits.length; j++) { + if (j != i) { + number += digits[j] * 10; + for (int k = 0; k < digits.length; k++) { + if (k != i && k != j && digits[k] % 2 == 0) { + number += digits[k]; + set.add(number); + number -= digits[k]; + } + } + number -= digits[j] * 10; + } + } + } + } + List list = new ArrayList<>(set); + Collections.sort(list); + int[] ans = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + ans[i] = list.get(i); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2095.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2095.java new file mode 100644 index 0000000000..5b3b6d092c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2095.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.List; + +public class _2095 { + public static class Solution1 { + public ListNode deleteMiddle(ListNode head) { + List list = new ArrayList<>(); + while (head != null) { + list.add(head.val); + head = head.next; + } + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + for (int i = 0; i < list.size(); i++) { + if (i != list.size() / 2) { + tmp.next = new ListNode(list.get(i)); + tmp = tmp.next; + } + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2096.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2096.java new file mode 100644 index 0000000000..32f1b85832 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2096.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _2096 { + public static class Solution1 { + /* + * Steps for this problem: + * 1. find the path from root the start and dest respectively, mark them using two directions: 'L' and 'R', i.e. you can only go down from root, so there's no up, 'U' direction; + * 2. the LCA (the lowest common ancestor) of start and dest will be the joint of the shortest path; + * 3. find the longest common prefix of these two paths, that can be cut off; + * 4. reverse the startPath, so it becomes the path from start to LCA, then concatenate with destPath + */ + public String getDirections(TreeNode root, int startValue, int destValue) { + StringBuilder sb = new StringBuilder(); + String pathForStart = ""; + if (findPathFromRoot(root, startValue, sb)) { + pathForStart = sb.toString(); + } + sb.setLength(0); + String pathForDest = ""; + if (findPathFromRoot(root, destValue, sb)) { + pathForDest = sb.toString(); + } + int lastIdenticalCharIndex = -1; + int minLen = Math.min(pathForStart.length(), pathForDest.length()); + for (int i = 0; i < minLen; i++) { + if (pathForStart.charAt(i) == pathForDest.charAt(i)) { + lastIdenticalCharIndex = i; + } else { + break; + } + } + sb.setLength(0); + sb.append(pathForStart.substring(lastIdenticalCharIndex + 1)); + for (int i = 0; i < sb.length(); i++) { + if (sb.charAt(i) == 'L' || sb.charAt(i) == 'R') { + sb.setCharAt(i, 'U'); + } + } + sb.append(pathForDest.substring(lastIdenticalCharIndex + 1)); + return sb.toString(); + } + + private boolean findPathFromRoot(TreeNode root, int target, StringBuilder sb) { + if (root == null) { + return false; + } + if (root.val == target) { + return true; + } + if (findPathFromRoot(root.left, target, sb.append("L"))) { + return true; + } + sb.setLength(sb.length() - 1); + if (findPathFromRoot(root.right, target, sb.append("R"))) { + return true; + } + sb.setLength(sb.length() - 1); + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2099.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2099.java new file mode 100644 index 0000000000..de6efe551b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2099.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2099 { + public static class Solution1 { + public int[] maxSubsequence(int[] nums, int k) { + int[][] map = new int[nums.length][2]; + for (int i = 0; i < nums.length; i++) { + map[i][0] = nums[i]; + map[i][1] = i; + } + Arrays.sort(map, (a, b) -> b[0] - a[0]); + int[][] arr = new int[k][2]; + for (int i = 0; i < k; i++) { + arr[i][0] = map[i][0]; + arr[i][1] = map[i][1]; + } + Arrays.sort(arr, (a, b) -> a[1] - b[1]); + int[] ans = new int[k]; + for (int i = 0; i < k; i++) { + ans[i] = arr[i][0]; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2103.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2103.java new file mode 100644 index 0000000000..16449ad565 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2103.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _2103 { + public static class Solution1 { + public int countPoints(String rings) { + Map> map = new HashMap<>(); + for (int i = 0; i < rings.length() - 1; i += 2) { + char color = rings.charAt(i); + int pos = Character.getNumericValue(rings.charAt(i + 1)); + Set set = map.getOrDefault(pos, new HashSet<>()); + if (color == 'R') { + set.add(1); + } else if (color == 'G') { + set.add(2); + } else { + set.add(3); + } + map.put(pos, set); + } + int ans = 0; + for (int pos : map.keySet()) { + if (map.get(pos).size() == 3) { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2108.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2108.java new file mode 100644 index 0000000000..25db5b2528 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2108.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2108 { + public static class Solution1 { + public String firstPalindrome(String[] words) { + for (String word : words) { + if (isPal(word)) { + return word; + } + } + return ""; + } + + private boolean isPal(String word) { + int left = 0; + int right = word.length() - 1; + while (left < right) { + if (word.charAt(left) != word.charAt(right)) { + return false; + } else { + left++; + right--; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2109.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2109.java new file mode 100644 index 0000000000..5a5f3f58b2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2109.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2109 { + public static class Solution1 { + public String addSpaces(String s, int[] spaces) { + StringBuilder sb = new StringBuilder(); + for (int i = 0, j = 0; i < s.length(); i++) { + if (j < spaces.length && i == spaces[j]) { + sb.append(" "); + j++; + } + sb.append(s.charAt(i)); + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2110.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2110.java new file mode 100644 index 0000000000..ee2f6773fa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2110.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2110 { + public static class Solution1 { + public long getDescentPeriods(int[] prices) { + long ans = prices.length; + for (int i = 0; i < prices.length; i++) { + long startI = i; + while (i + 1 < prices.length && prices[i] - 1 == prices[i + 1]) { + i++; + } + long totalNum = i - startI + 1; + if (totalNum > 1) { + ans += ((totalNum - 1) * totalNum) / 2; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2114.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2114.java new file mode 100644 index 0000000000..15654d1d78 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2114.java @@ -0,0 +1,13 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2114 { + public static class Solution1 { + public int mostWordsFound(String[] sentences) { + int max = 0; + for (String sentence : sentences) { + max = Math.max(max, sentence.split("\\ ").length); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2115.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2115.java new file mode 100644 index 0000000000..2c6848ecc3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2115.java @@ -0,0 +1,63 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +public class _2115 { + public static class Solution1 { + /* + * My completely original solution, topological sort template comes in pretty handy. + */ + public List findAllRecipes( + String[] recipes, List> ingredients, String[] supplies) { + Set allRecipes = new HashSet<>(); + Collections.addAll(allRecipes, recipes); + + Set allSupplies = new HashSet<>(); + Collections.addAll(allSupplies, supplies); + + Map indegree = new HashMap<>(); + Map> adjList = new HashMap<>(); + Map> ingredientMap = new HashMap<>(); + for (int i = 0; i < ingredients.size(); i++) { + int dependencyCount = 0; + for (String ingredient : ingredients.get(i)) { + if (allRecipes.contains(ingredient)) { + dependencyCount++; + List list = adjList.getOrDefault(ingredient, new ArrayList<>()); + list.add(recipes[i]); + adjList.put(ingredient, list); + } + } + indegree.put(recipes[i], dependencyCount); + ingredientMap.put(recipes[i], ingredients.get(i)); + } + Queue q = new LinkedList<>(); + for (Map.Entry entry : indegree.entrySet()) { + if (entry.getValue() == 0 + && allSupplies.containsAll(ingredientMap.get(entry.getKey()))) { + q.offer(entry.getKey()); + } + } + List result = new ArrayList<>(); + while (!q.isEmpty()) { + String curr = q.poll(); + result.add(curr); + for (String neighbor : adjList.getOrDefault(curr, new ArrayList<>())) { + indegree.put(neighbor, indegree.get(neighbor) - 1); + if (indegree.get(neighbor) == 0) { + q.offer(neighbor); + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2116.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2116.java new file mode 100644 index 0000000000..5050d71ebb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2116.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2116 { + public static class Solution1 { + /* + * credit: https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/discuss/1646594/Left-to-right-and-right-to-left + */ + public boolean canBeValid(String s, String locked) { + return s.length() % 2 == 0 && valid(s, locked, '(') && valid(s, locked, ')'); + } + + private boolean valid(String s, String locked, char op) { + int balance = 0; + int wildcards = 0; + int direction = op == '(' ? 1 : -1; + int start = op == '(' ? 0 : s.length() - 1; + for (int i = start; + i < s.length() && i >= 0 && balance + wildcards >= 0; + i += direction) { + if (locked.charAt(i) == '1') { + balance += s.charAt(i) == op ? 1 : -1; + } else { + wildcards++; + } + } + return wildcards + balance >= 0 && balance <= wildcards; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2119.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2119.java new file mode 100644 index 0000000000..5234b3289d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2119.java @@ -0,0 +1,12 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2119 { + public static class Solution1 { + public boolean isSameAfterReversals(int num) { + if (num == 0) { + return true; + } + return num % 10 != 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2120.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2120.java new file mode 100644 index 0000000000..35d86ce80a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2120.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2120 { + public static class Solution1 { + public int[] executeInstructions(int n, int[] startPos, String s) { + int[] ans = new int[s.length()]; + for (int i = 0; i < s.length(); i++) { + int y = startPos[1]; + int x = startPos[0]; + int j = i; + boolean broken = false; + for (; j < s.length(); j++) { + if (s.charAt(j) == 'R') { + if (y + 1 < n) { + y++; + } else { + ans[i] = j - i; + broken = true; + break; + } + } else if (s.charAt(j) == 'L') { + if (y - 1 >= 0) { + y--; + } else { + ans[i] = j - i; + broken = true; + break; + } + } else if (s.charAt(j) == 'U') { + if (x - 1 >= 0) { + x--; + } else { + ans[i] = j - i; + broken = true; + break; + } + } else if (s.charAt(j) == 'D') { + if (x + 1 < n) { + x++; + } else { + ans[i] = j - i; + broken = true; + break; + } + } + } + if (!broken) { + ans[i] = j - i; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2124.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2124.java new file mode 100644 index 0000000000..43a9aaf607 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2124.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2124 { + public static class Solution1 { + public boolean checkString(String s) { + int aIndex = s.lastIndexOf('a'); + if (aIndex < 0) { + return true; + } + for (int i = 0; i < aIndex; i++) { + if (s.charAt(i) == 'b') { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2125.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2125.java new file mode 100644 index 0000000000..fac82be4a8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2125.java @@ -0,0 +1,42 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2125 { + public static class Solution1 { + public int numberOfBeams(String[] bank) { + int ans = 0; + for (int i = 0; i < bank.length; ) { + String currRow = bank[i]; + if (currRow.indexOf('1') == -1) { + i++; + continue; + } + int num1 = 0; + for (char c : currRow.toCharArray()) { + if (c == '1') { + num1++; + } + } + i++; + while (i < bank.length) { + if (bank[i].indexOf('1') == -1) { + i++; + } else { + break; + } + } + int num2 = 0; + if (i >= bank.length) { + break; + } else { + for (char c : bank[i].toCharArray()) { + if (c == '1') { + num2++; + } + } + } + ans += num1 * num2; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2126.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2126.java new file mode 100644 index 0000000000..05b9954a55 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2126.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class _2126 { + public static class Solution1 { + public boolean asteroidsDestroyed(int mass, int[] asteroids) { + Map map = new HashMap<>(); + for (int a : asteroids) { + map.put(a, map.getOrDefault(a, 0) + 1); + } + int[] nums = new int[map.keySet().size()]; + int i = 0; + for (int key : map.keySet()) { + nums[i++] = key; + } + Arrays.sort(nums); + int startIndex = 0; + long sum = mass; + int upToIndex = binarySearch(sum, nums, startIndex, nums.length - 1); + while (upToIndex < nums.length) { + for (i = startIndex; i <= upToIndex; i++) { + sum += (long) map.get(nums[i]) * nums[i]; + } + if (upToIndex == nums.length - 1) { + return true; + } + startIndex = upToIndex + 1; + upToIndex = binarySearch(sum, nums, startIndex, nums.length - 1); + if (startIndex > upToIndex) { + return false; + } + } + return true; + } + + private int binarySearch(long sum, int[] nums, int left, int right) { + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] < sum) { + left = mid + 1; + } else if (nums[mid] > sum) { + right = mid - 1; + } else { + return mid; + } + } + return right < nums.length && nums[right] <= sum ? right : left - 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2129.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2129.java new file mode 100644 index 0000000000..8f510f1584 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2129.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Locale; + +public class _2129 { + public static class Solution1 { + public String capitalizeTitle(String title) { + String[] words = title.split("\\ "); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < words.length; i++) { + String tmp = words[i].toLowerCase(Locale.ROOT); + if (words[i].length() <= 2) { + sb.append(tmp); + } else { + sb.append(Character.toUpperCase(tmp.charAt(0)) + tmp.substring(1)); + } + if (i < words.length - 1) { + sb.append(" "); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2130.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2130.java new file mode 100644 index 0000000000..3c3ae43128 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2130.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.List; + +public class _2130 { + public static class Solution1 { + public int pairSum(ListNode head) { + List list = new ArrayList<>(); + while (head != null) { + list.add(head.val); + head = head.next; + } + int max = 0; + int len = list.size(); + for (int i = 0; i < len / 2; i++) { + max = Math.max(max, list.get(i) + list.get(len - i - 1)); + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2133.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2133.java new file mode 100644 index 0000000000..73a27925a0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2133.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2133 { + public static class Solution1 { + public boolean checkValid(int[][] matrix) { + int n = matrix.length; + Set set = new HashSet<>(); + for (int i = 1; i <= n; i++) { + set.add(i); + } + for (int i = 0; i < n; i++) { + Set copy = new HashSet<>(set); + for (int j = 0; j < n; j++) { + if (!copy.remove(matrix[i][j])) { + return false; + } + } + } + for (int j = 0; j < n; j++) { + Set copy = new HashSet<>(set); + for (int i = 0; i < n; i++) { + if (!copy.remove(matrix[i][j])) { + return false; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2134.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2134.java new file mode 100644 index 0000000000..34c26a5e9b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2134.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2134 { + public static class Solution1 { + /* + * Connect the original array with itself to simulate the circular property of this array. + * Then use a sliding window to find the minimum swaps. + */ + public int minSwaps(int[] nums) { + int ones = 0; + List list = new ArrayList<>(); + for (int num : nums) { + ones += num; + list.add(num); + } + // add it again to simulate the circular list + for (int num : nums) { + list.add(num); + } + int minSwaps = nums.length; + int zeroes = 0; + // as long as the size of the sliding window is smaller than 1s' count, we keep moving + // right pointer to the right + // as soon as the size of the sliding window is equal to 1s' count, we take the 0s count + // in this window against minSwaps to update it if possible + // then if the size of the sliding window is greater than 1s' count, we move the left + // pointer to the right + // One caveat: you don't really need to make the swaps to solve this problem, just + // counting the numbers is enough + for (int left = 0, right = 0; right < list.size(); right++) { + if (list.get(right) == 0) { + zeroes++; + } + int len = right - left + 1; + if (len < ones) { + continue; + } else if (len == ones) { + minSwaps = Math.min(minSwaps, zeroes); + } else { + if (list.get(left) == 0) { + zeroes--; + } + left++; + minSwaps = Math.min(minSwaps, zeroes); + } + } + return minSwaps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2135.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2135.java new file mode 100644 index 0000000000..7a8cae6c68 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2135.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _2135 { + public static class Solution1 { + public int wordCount(String[] startWords, String[] targetWords) { + Set startSet = new HashSet<>(); + for (String start : startWords) { + char[] charArray = start.toCharArray(); + Arrays.sort(charArray); + startSet.add(new String(charArray)); + } + int count = 0; + for (String target : targetWords) { + char[] charArray = target.toCharArray(); + Arrays.sort(charArray); + String sortedTarget = new String(charArray); + for (int i = 0; i < sortedTarget.length(); i++) { + String formedTargetByOmittingOneLetter = + sortedTarget.substring(0, i) + sortedTarget.substring(i + 1); + if (startSet.contains(formedTargetByOmittingOneLetter)) { + count++; + break; + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2138.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2138.java new file mode 100644 index 0000000000..f304fe642c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2138.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2138 { + public static class Solution1 { + public String[] divideString(String s, int k, char fill) { + int len = s.length() / k; + if (s.length() % k != 0) { + len++; + } + String[] ans = new String[len]; + for (int i = 0, j = 0; i < s.length(); i += k, j++) { + if (j == len - 1) { + StringBuilder sb = new StringBuilder(s.substring(i)); + if (sb.length() != k) { + while (sb.length() < k) { + sb.append(fill); + } + } + ans[j] = sb.toString(); + } else { + ans[j] = s.substring(i, i + k); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2139.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2139.java new file mode 100644 index 0000000000..ca3dd463b2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2139.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2139 { + public static class Solution1 { + public int minMoves(int target, int maxDoubles) { + int minMoves = 0; + while (target != 1) { + if (maxDoubles > 0) { + if (target % 2 == 0) { + target /= 2; + maxDoubles--; + } else { + target--; + } + } else { + minMoves += target - 1; + break; + } + minMoves++; + } + return minMoves; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2144.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2144.java new file mode 100644 index 0000000000..968d94cf4b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2144.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2144 { + public static class Solution1 { + public int minimumCost(int[] cost) { + Arrays.sort(cost); + int ans = 0; + for (int i = cost.length - 1; i >= 0; i--) { + ans += cost[i]; + i--; + if (i >= 0) { + ans += cost[i]; + } + i--; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2148.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2148.java new file mode 100644 index 0000000000..cef074dc8d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2148.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.TreeMap; + +public class _2148 { + public static class Solution1 { + public int countElements(int[] nums) { + TreeMap treeMap = new TreeMap<>(); + for (int num : nums) { + treeMap.put(num, treeMap.getOrDefault(num, 0) + 1); + } + int ans = 0; + int i = 0; + int len = treeMap.size(); + for (int key : treeMap.keySet()) { + if (i != 0 && i != len - 1) { + ans += treeMap.get(key); + } + i++; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2149.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2149.java new file mode 100644 index 0000000000..9c920b3df2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2149.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2149 { + public static class Solution1 { + public int[] rearrangeArray(int[] nums) { + int[] ans = new int[nums.length]; + List pos = new ArrayList<>(); + List neg = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + if (nums[i] > 0) { + pos.add(nums[i]); + } else { + neg.add(nums[i]); + } + } + for (int i = 0, j = 0; i < nums.length && j < pos.size(); i++, j++) { + ans[i] = pos.get(j); + i++; + ans[i] = neg.get(j); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2150.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2150.java new file mode 100644 index 0000000000..024ba0eba2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2150.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _2150 { + public static class Solution1 { + public List findLonely(int[] nums) { + TreeMap treeMap = new TreeMap<>(); + for (int num : nums) { + treeMap.put(num, treeMap.getOrDefault(num, 0) + 1); + } + List ans = new ArrayList<>(); + for (int key : treeMap.keySet()) { + if (treeMap.get(key) > 1) { + continue; + } else { + if (!treeMap.containsKey(key - 1) && !treeMap.containsKey(key + 1)) { + ans.add(key); + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2154.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2154.java new file mode 100644 index 0000000000..dfd44f0924 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2154.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2154 { + public static class Solution1 { + public int findFinalValue(int[] nums, int original) { + Set set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + while (set.contains(original)) { + original *= 2; + } + return original; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2155.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2155.java new file mode 100644 index 0000000000..e3e0ffa839 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2155.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +public class _2155 { + public static class Solution1 { + public List maxScoreIndices(int[] nums) { + TreeMap> treeMap = new TreeMap<>(); + int ones = 0; + for (int num : nums) { + ones += num; + } + int zeroes = 0; + List l = new ArrayList<>(); + l.add(0); + treeMap.put(ones, l); + for (int i = 0; i < nums.length; i++) { + if (nums[i] == 0) { + zeroes++; + } else { + ones--; + } + int score = ones + zeroes; + List list = treeMap.getOrDefault(score, new ArrayList<>()); + Integer index = i + 1; + list.add(index); + treeMap.put(score, list); + } + return treeMap.lastEntry().getValue(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2156.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2156.java new file mode 100644 index 0000000000..c1988af4b4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2156.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2156 { + public static class Solution1 { + /* + * Credit: https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1730100/Java-rolling-hash(back-to-front)/1242659 + *

+ * We start from the right side and compute rolling hash when moving the window of size k towards the left. + * This post explains why we need to start from the right and move towards the left: https://leetcode.com/problems/find-substring-with-given-hash-value/discuss/1730114/C%2B%2B-Rolling-Hash-O(N)-Time + * so that we could use Rabin-Karp algorithm. + */ + public String subStrHash(String s, int power, int modulo, int k, int hashValue) { + long weight = 1; + for (int j = 0; j < k - 1; j++) { + // calculate the weight which will be the power to the k-1 + // this will be used when we start shifting our window of size k to the left from + // the end of the string + weight = (weight * power) % modulo; + } + /*We'll have to use the above for loop to calculate weight instead of using Math.pow(power, k - 1) which will render wrong results when power and k are big enough.*/ + + // initialize the result string to empty string and keep updating it as we start from + // the end of the string, and we need to find the first substring that has the hashvalue + String result = ""; + + // right bound of the sliding window which starts at the end of the string + int right = s.length() - 1; + + long hash = 0; + for (int i = s.length() - 1; i >= 0; i--) { + + // add the next value of char for the left pointer into the sliding window + int val = s.charAt(i) - 'a' + 1; + + // update the current hash value + hash = (hash * power % modulo + val) % modulo; + + // when window is at size k, we need to check if we find a matching hash value + // and update the result, and remove the right most char out of the window to + // prepare for next iteration + if (right - i + 1 == k) { + if (hash == hashValue) { + result = s.substring(i, right + 1); + } + hash = + (hash + modulo - (s.charAt(right--) - 'a' + 1) * weight % modulo) + % modulo; + } + } + + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2160.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2160.java new file mode 100644 index 0000000000..0c2c838d99 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2160.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2160 { + public static class Solution1 { + public int minimumSum(int num) { + int[] digits = new int[4]; + int i = 0; + while (num != 0) { + digits[i++] = num % 10; + num /= 10; + } + Arrays.sort(digits); + int num1 = 0; + if (digits[0] == 0) { + num1 = digits[3]; + } else { + num1 = digits[0] * 10 + digits[3]; + } + int num2 = 0; + if (digits[1] == 0) { + num2 = digits[2]; + } else { + num2 = digits[1] * 10 + digits[2]; + } + return num1 + num2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2161.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2161.java new file mode 100644 index 0000000000..311e2cfedc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2161.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2161 { + public static class Solution1 { + public int[] pivotArray(int[] nums, int pivot) { + List less = new ArrayList<>(); + List greater = new ArrayList<>(); + int count = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] < pivot) { + less.add(nums[i]); + } else if (nums[i] > pivot) { + greater.add(nums[i]); + } else { + count++; + } + } + for (int i = 0; i < nums.length; ) { + int j = 0; + while (j < less.size()) { + nums[i++] = less.get(j++); + } + j = 0; + while (j < count) { + nums[i++] = pivot; + j++; + } + j = 0; + while (j < greater.size()) { + nums[i++] = greater.get(j++); + } + } + return nums; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2164.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2164.java new file mode 100644 index 0000000000..b53d2099db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2164.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _2164 { + public static class Solution1 { + public int[] sortEvenOdd(int[] nums) { + List odds = new ArrayList<>(); + List evens = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + if (i % 2 == 0) { + evens.add(nums[i]); + } else { + odds.add(nums[i]); + } + } + Collections.sort(odds, Collections.reverseOrder()); + Collections.sort(evens); + int[] ans = new int[nums.length]; + for (int i = 0, j = 0, k = 0; i < nums.length; i++) { + if (i % 2 == 0) { + ans[i] = evens.get(j++); + } else { + ans[i] = odds.get(k++); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2165.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2165.java new file mode 100644 index 0000000000..85f826b319 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2165.java @@ -0,0 +1,68 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.*; + +public class _2165 { + public static class Solution1 { + public long smallestNumber(long num) { + if (num == 0) { + return num; + } + boolean negative = num < 0; + TreeMap treeMap = new TreeMap<>(); + while (num != 0) { + int digit = (int) Math.abs(num % 10); + treeMap.put(digit, treeMap.getOrDefault(digit, 0) + 1); + num /= 10; + } + if (!negative) { + if (treeMap.firstKey() == 0) { + int zeroCount = treeMap.get(0); + treeMap.remove(0); + StringBuilder sb = new StringBuilder(); + int time = 0; + for (int key : treeMap.keySet()) { + int count = treeMap.get(key); + time++; + if (time == 1) { + sb.append(key); + while (zeroCount > 0) { + sb.append(0); + zeroCount--; + } + count--; + } + while (count > 0) { + sb.append(key); + count--; + } + } + return Long.parseLong(sb.toString()); + } else { + StringBuilder sb = new StringBuilder(); + for (int key : treeMap.keySet()) { + int count = treeMap.get(key); + while (count > 0) { + sb.append(key); + count--; + } + } + return Long.parseLong(sb.toString()); + } + } else { + Set keys = treeMap.keySet(); + List sorted = new ArrayList<>(keys); + Collections.sort(sorted, Collections.reverseOrder()); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < sorted.size(); i++) { + int count = treeMap.get(sorted.get(i)); + while (count > 0) { + sb.append(sorted.get(i)); + count--; + } + } + return -Long.parseLong(sb.toString()); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2166.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2166.java new file mode 100644 index 0000000000..0058dba51d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2166.java @@ -0,0 +1,88 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2166 { + public static class Solution1 { + class Bitset { + + int size; + int ones; + int[] nums1; + int[] nums2; + boolean checkingNums1; + + public Bitset(int size) { + this.nums1 = new int[size]; + this.nums2 = new int[size]; + for (int i = 0; i < size; i++) { + nums2[i] = 1; + } + this.ones = 0; + this.size = size; + this.checkingNums1 = true; + } + + public void fix(int idx) { + if (this.checkingNums1) { + if (nums1[idx] == 0) { + ones++; + } + nums1[idx] = 1; + nums2[idx] = 0; + } else { + if (nums2[idx] == 0) { + ones++; + } + nums2[idx] = 1; + nums1[idx] = 0; + } + } + + public void unfix(int idx) { + if (this.checkingNums1) { + if (nums1[idx] == 1) { + ones--; + } + nums1[idx] = 0; + nums2[idx] = 1; + } else { + if (nums2[idx] == 1) { + ones--; + } + nums2[idx] = 0; + nums1[idx] = 1; + } + } + + public void flip() { + this.ones = this.size - this.ones; + this.checkingNums1 = !this.checkingNums1; + } + + public boolean all() { + return ones == size; + } + + public boolean one() { + return ones > 0; + } + + public int count() { + return ones; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + if (this.checkingNums1) { + for (int i = 0; i < size; i++) { + sb.append(nums1[i]); + } + } else { + for (int i = 0; i < size; i++) { + sb.append(nums2[i]); + } + } + return sb.toString(); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2169.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2169.java new file mode 100644 index 0000000000..31fd7095da --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2169.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2169 { + public static class Solution1 { + public int countOperations(int num1, int num2) { + int ops = 0; + while (true) { + if (num1 == 0 || num2 == 0) { + return ops; + } + if (num1 >= num2) { + num1 -= num2; + } else { + num2 -= num1; + } + ops++; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2176.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2176.java new file mode 100644 index 0000000000..483ba1cc8e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2176.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _2176 { + public static class Solution1 { + public int countPairs(int[] nums, int k) { + Map> map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + List list = map.getOrDefault(nums[i], new ArrayList<>()); + list.add(i); + map.put(nums[i], list); + } + int ans = 0; + for (int key : map.keySet()) { + List list = map.get(key); + for (int i = 0; i < list.size() - 1; i++) { + for (int j = i + 1; j < list.size(); j++) { + if ((list.get(i) * list.get(j)) % k == 0) { + ans++; + } + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2177.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2177.java new file mode 100644 index 0000000000..83c3a29c5f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2177.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2177 { + public static class Solution1 { + public long[] sumOfThree(long num) { + long remainder = num % 3; + long ave = num / 3; + if (remainder == 0) { + return new long[] {ave - 1, ave, ave + 1}; + } else { + return new long[] {}; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2180.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2180.java new file mode 100644 index 0000000000..73568980ec --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2180.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2180 { + public static class Solution1 { + public int countEven(int num) { + int ans = 0; + for (int i = 1; i <= num; i++) { + List list = getAllDigits(i); + int sum = 0; + for (int l : list) { + sum += l; + } + if (sum % 2 == 0) { + ans++; + } + } + return ans; + } + + private List getAllDigits(int num) { + List ans = new ArrayList<>(); + while (num != 0) { + ans.add(num % 10); + num /= 10; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2181.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2181.java new file mode 100644 index 0000000000..09024ea8ac --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2181.java @@ -0,0 +1,52 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.List; + +public class _2181 { + public static class Solution1 { + public ListNode mergeNodes(ListNode head) { + List list = new ArrayList<>(); + while (head != null) { + list.add(head.val); + head = head.next; + } + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + for (int i = 1; i < list.size(); i++) { + int sum = 0; + while (i < list.size() && list.get(i) != 0) { + sum += list.get(i); + i++; + } + tmp.next = new ListNode(sum); + tmp = tmp.next; + } + return pre.next; + } + } + + public static class Solution2 { + /* + * Without using an extra list, do sum on the fly. + */ + public ListNode mergeNodes(ListNode head) { + ListNode pre = new ListNode(-1); + ListNode newHead = pre; + while (head != null && head.next != null) { + if (head.val == 0) { + int sum = 0; + head = head.next; + while (head.val != 0) { + sum += head.val; + head = head.next; + } + newHead.next = new ListNode(sum); + newHead = newHead.next; + } + } + return pre.next; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2182.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2182.java new file mode 100644 index 0000000000..aaa882ca24 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2182.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; + +public class _2182 { + public static class Solution1 { + public String repeatLimitedString(String s, int repeatLimit) { + Map map = new HashMap<>(); + for (char c : s.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + PriorityQueue heap = new PriorityQueue((a, b) -> b.c - a.c); + for (char c : map.keySet()) { + heap.offer(new Count(c, map.get(c))); + } + StringBuilder sb = new StringBuilder(); + while (!heap.isEmpty()) { + Count curr = heap.poll(); + int num = Math.min(curr.num, repeatLimit); + while (num > 0) { + sb.append(curr.c); + num--; + } + if (curr.num > repeatLimit) { + Count newCount = new Count(curr.c, curr.num - repeatLimit); + if (!heap.isEmpty()) { + Count next = heap.poll(); + sb.append(next.c); + if (next.num > 1) { + heap.offer(new Count(next.c, next.num - 1)); + } + } else { + break; + } + heap.offer(newCount); + } + } + return sb.toString(); + } + + class Count { + char c; + int num; + + public Count(char c, Integer integer) { + this.c = c; + this.num = integer; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2185.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2185.java new file mode 100644 index 0000000000..e1c9c3e81b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2185.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2185 { + public static class Solution1 { + public int prefixCount(String[] words, String pref) { + int count = 0; + for (String word : words) { + if (word.startsWith(pref)) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2186.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2186.java new file mode 100644 index 0000000000..8113925090 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2186.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2186 { + public static class Solution1 { + public int minSteps(String s, String t) { + int[] counts = new int[26]; + for (int i = 0; i < s.length(); i++) { + counts[s.charAt(i) - 'a']++; + } + for (int i = 0; i < t.length(); i++) { + counts[t.charAt(i) - 'a']--; + } + int ans = 0; + for (int i = 0; i < counts.length; i++) { + ans += Math.abs(counts[i]); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2190.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2190.java new file mode 100644 index 0000000000..10e9295f5f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2190.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2190 { + public static class Solution1 { + public int mostFrequent(int[] nums, int key) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] == key) { + map.put(nums[i + 1], map.getOrDefault(nums[i + 1], 0) + 1); + } + } + int most = 0; + int ans = 0; + for (int k : map.keySet()) { + if (map.get(k) > most) { + ans = k; + most = map.get(k); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2191.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2191.java new file mode 100644 index 0000000000..eae801ef27 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2191.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2191 { + public static class Solution1 { + public int[] sortJumbled(int[] mapping, int[] nums) { + int[][] result = new int[nums.length][2]; + for (int i = 0; i < nums.length; i++) { + result[i][0] = convert(nums[i], mapping); + result[i][1] = i; + } + Arrays.sort(result, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); + int[] list = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + list[i] = nums[result[i][1]]; + } + return list; + } + + private int convert(int num, int[] mapping) { + char[] charArray = String.valueOf(num).toCharArray(); + StringBuilder sb = new StringBuilder(); + for (char c : charArray) { + sb.append(mapping[Character.getNumericValue(c)]); + } + int i = 0; + while (i < sb.length() && sb.charAt(i) == '0') { + i++; + } + if (i >= sb.length()) { + return 0; + } + return Integer.parseInt(sb.substring(i)); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2192.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2192.java new file mode 100644 index 0000000000..91dc7601f8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2192.java @@ -0,0 +1,59 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.TreeSet; + +public class _2192 { + public static class Solution1 { + /* + * My completely original solution: + * topological sort template comes in handy here. + */ + public List> getAncestors(int n, int[][] edges) { + List[] adjList = new ArrayList[n]; + for (int i = 0; i < n; i++) { + adjList[i] = new ArrayList<>(); + } + int[] indegree = new int[n]; + for (int[] edge : edges) { + indegree[edge[1]]++; + adjList[edge[0]].add(edge[1]); + } + Queue q = new LinkedList<>(); + for (int i = 0; i < n; i++) { + if (indegree[i] == 0) { + q.offer(i); + } + } + List> treeSetList = new ArrayList<>(); + for (int i = 0; i < n; i++) { + treeSetList.add(new TreeSet<>()); + } + while (!q.isEmpty()) { + Integer curr = q.poll(); + for (int v : adjList[curr]) { + indegree[v]--; + treeSetList.get(v).add(curr); + treeSetList.get(v).addAll(treeSetList.get(curr)); + if (indegree[v] == 0) { + q.offer(v); + } + } + } + List> result = new ArrayList<>(); + for (int i = 0; i < n; i++) { + Iterator it = treeSetList.get(i).iterator(); + List list = new ArrayList<>(); + while (it.hasNext()) { + list.add(it.next()); + } + result.add(list); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2194.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2194.java new file mode 100644 index 0000000000..763fd61140 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2194.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _2194 { + public static class Solution1 { + public List cellsInRange(String s) { + char start = s.charAt(0); + int num1 = Integer.parseInt(s.charAt(1) + ""); + char end = s.charAt(3); + int num2 = Integer.parseInt(s.charAt(4) + ""); + List ans = new ArrayList<>(); + StringBuilder sb = new StringBuilder(); + for (char startChar = start; startChar <= end; startChar++) { + for (int c = num1; c <= num2; c++) { + sb.setLength(0); + sb.append(startChar); + sb.append(c); + ans.add(sb.toString()); + } + } + Collections.sort(ans); + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2196.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2196.java new file mode 100644 index 0000000000..cdaae24b2f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2196.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _2196 { + public static class Solution1 { + /* + * My completely original solution. + */ + public TreeNode createBinaryTree(int[][] descriptions) { + Map map = new HashMap<>(); + Set notRootVals = new HashSet<>(); + for (int[] des : descriptions) { + notRootVals.add(des[1]); + TreeNode node = map.getOrDefault(des[0], new TreeNode(des[0])); + if (des[2] == 1) { + node.left = map.getOrDefault(des[1], new TreeNode(des[1])); + map.put(des[1], node.left); + } else { + node.right = map.getOrDefault(des[1], new TreeNode(des[1])); + map.put(des[1], node.right); + } + map.put(des[0], node); + } + int rootVal = -1; + for (int[] des : descriptions) { + if (!notRootVals.contains(des[0])) { + rootVal = des[0]; + break; + } + } + return map.get(rootVal); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2200.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2200.java new file mode 100644 index 0000000000..f245a14944 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2200.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _2200 { + public static class Solution1 { + public List findKDistantIndices(int[] nums, int key, int k) { + List ans = new ArrayList<>(); + List keyIndices = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) { + if (nums[i] == key) { + keyIndices.add(i); + } + } + for (int i = 0; i < nums.length; i++) { + for (int j = 0; j < keyIndices.size(); j++) { + if (Math.abs(i - keyIndices.get(j)) <= k) { + ans.add(i); + break; + } + } + } + Collections.sort(ans); + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2201.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2201.java new file mode 100644 index 0000000000..c0677f6956 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2201.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2201 { + public static class Solution1 { + public int digArtifacts(int n, int[][] artifacts, int[][] dig) { + int ans = 0; + boolean[][] grid = new boolean[n][n]; + for (int[] d : dig) { + grid[d[0]][d[01]] = true; + } + for (int[] arti : artifacts) { + int r0 = arti[0]; + int c0 = arti[1]; + boolean cannotUnearth = false; + for (int r = r0; r <= arti[2]; r++) { + for (int c = c0; c <= arti[3]; c++) { + if (!grid[r][c]) { + cannotUnearth = true; + break; + } + } + if (cannotUnearth) { + break; + } + } + if (!cannotUnearth) { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2206.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2206.java new file mode 100644 index 0000000000..588aad8a67 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2206.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2206 { + public static class Solution1 { + public boolean divideArray(int[] nums) { + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + for (int key : map.keySet()) { + if (map.get(key) % 2 != 0) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2208.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2208.java new file mode 100644 index 0000000000..4189a585fd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2208.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.PriorityQueue; + +public class _2208 { + public static class Solution1 { + public int halveArray(int[] nums) { + PriorityQueue heap = new PriorityQueue<>((a, b) -> Double.compare(b, a)); + double sum = 0; + for (int num : nums) { + heap.offer(Double.valueOf(num)); + sum += num; + } + double half = (double) sum / 2; + int ops = 0; + while (sum > half) { + Double max = heap.poll(); + sum -= max; + double h = max / 2; + sum += h; + heap.offer(h); + ops++; + } + return ops; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2210.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2210.java new file mode 100644 index 0000000000..8e6c061d6d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2210.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2210 { + public static class Solution1 { + public int countHillValley(int[] nums) { + int ans = 0; + for (int i = 1; i < nums.length - 1; i++) { + if (nums[i] == nums[i - 1]) { + continue; + } else if (nums[i] > nums[i - 1]) { + int tmp = i; + boolean moved = false; + while (tmp + 1 < nums.length && nums[tmp] == nums[tmp + 1]) { + moved = true; + tmp++; + } + if (moved) { + if (tmp + 1 < nums.length && nums[i] > nums[tmp + 1]) { + ans++; + } + } else { + if (nums[i] > nums[i + 1]) { + ans++; + } + } + } else if (nums[i] < nums[i - 1]) { + int tmp = i; + boolean moved = false; + while (tmp + 1 < nums.length && nums[tmp] == nums[tmp + 1]) { + moved = true; + tmp++; + } + if (moved) { + if (tmp + 1 < nums.length && nums[i] < nums[tmp + 1]) { + ans++; + } + } else { + if (nums[i] < nums[i + 1]) { + ans++; + } + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2215.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2215.java new file mode 100644 index 0000000000..091c676042 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2215.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class _2215 { + public static class Solution1 { + public List> findDifference(int[] nums1, int[] nums2) { + Set set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet()); + Set set1Copy = new HashSet<>(set1); + Set set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet()); + set1.removeAll(set2); + set2.removeAll(set1Copy); + List list1 = + set1.stream().mapToInt(n -> n).boxed().collect(Collectors.toList()); + List list2 = + set2.stream().mapToInt(n -> n).boxed().collect(Collectors.toList()); + return new ArrayList<>(Arrays.asList(list1, list2)); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2220.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2220.java new file mode 100644 index 0000000000..e8463fbbe9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2220.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2220 { + public static class Solution1 { + public int minBitFlips(int start, int goal) { + String sBin = Integer.toBinaryString(start); + String s = String.format("%32s", sBin).replaceAll(" ", "0"); + String gBin = Integer.toBinaryString(goal); + String g = String.format("%32s", gBin).replaceAll(" ", "0"); + int ans = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) != g.charAt(i)) { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2224.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2224.java new file mode 100644 index 0000000000..89df27831c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2224.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2224 { + public static class Solution1 { + public int convertTime(String current, String correct) { + if (current.equals(correct)) { + return 0; + } + int ops = 0; + String[] startHourAndMinute = current.split(":"); + int start = + 60 * Integer.parseInt(startHourAndMinute[0]) + + Integer.parseInt(startHourAndMinute[1]); + String[] endHourAndMinute = correct.split(":"); + int end = + 60 * Integer.parseInt(endHourAndMinute[0]) + + Integer.parseInt(endHourAndMinute[1]); + int[] addons = new int[] {1, 5, 15, 60}; + int index = 3; + while (start < end) { + if (start + addons[index] == end) { + return ops + 1; + } else if (start + addons[index] < end) { + start += addons[index]; + ops++; + } else { + index--; + } + } + return ops; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2229.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2229.java new file mode 100644 index 0000000000..0d2457c0be --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2229.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.TreeSet; + +public class _2229 { + public static class Solution1 { + public boolean isConsecutive(int[] nums) { + TreeSet treeSet = new TreeSet<>(); + int min = nums[0]; + int max = nums[0]; + for (int num : nums) { + treeSet.add(num); + min = Math.min(min, num); + max = Math.max(max, num); + } + return treeSet.size() == max - min + 1 && treeSet.size() == nums.length; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2231.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2231.java new file mode 100644 index 0000000000..8fe3fbfe94 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2231.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.*; + +public class _2231 { + public static class Solution1 { + public int largestInteger(int num) { + List odds = new ArrayList<>(); + List evens = new ArrayList<>(); + PriorityQueue oddTimes = new PriorityQueue<>(); + PriorityQueue evenTimes = new PriorityQueue<>(); + int times = 1; + while (num != 0) { + int digit = num % 10; + num /= 10; + if (digit % 2 == 0) { + evens.add(digit); + evenTimes.offer(times); + } else { + odds.add(digit); + oddTimes.offer(times); + } + times *= 10; + } + Collections.sort(evens); + Collections.sort(odds); + int composite = 0; + for (int i = 0; i < odds.size(); i++) { + composite += odds.get(i) * oddTimes.poll(); + } + for (int i = 0; i < evens.size(); i++) { + composite += evens.get(i) * evenTimes.poll(); + } + return composite; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2235.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2235.java new file mode 100644 index 0000000000..e5f4ac73c2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2235.java @@ -0,0 +1,9 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2235 { + public static class Solution1 { + public int sum(int num1, int num2) { + return num1 + num2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2236.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2236.java new file mode 100644 index 0000000000..db124e0f58 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2236.java @@ -0,0 +1,11 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _2236 { + public static class Solution1 { + public boolean checkTree(TreeNode root) { + return root.val == root.left.val + root.right.val; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2239.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2239.java new file mode 100644 index 0000000000..29b435068b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2239.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2239 { + public static class Solution1 { + public int findClosestNumber(int[] nums) { + int ans = nums[0]; + int minDiff = Math.abs(nums[0]); + for (int i = 1; i < nums.length; i++) { + int diff = Math.abs(nums[i]); + if (diff < minDiff) { + minDiff = diff; + ans = nums[i]; + } else if (diff == minDiff && nums[i] > ans) { + ans = nums[i]; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2243.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2243.java new file mode 100644 index 0000000000..2e51d2ef94 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2243.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2243 { + public static class Solution1 { + public String digitSum(String s, int k) { + StringBuilder sb = new StringBuilder(); + while (s.length() > k) { + for (int i = 0; i < s.length(); i += k) { + int sum = 0; + for (int j = i; j < i + k && j < s.length(); j++) { + sum += Integer.parseInt(s.charAt(j) + ""); + } + sb.append(sum); + } + s = sb.toString(); + sb.setLength(0); + } + return s; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2244.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2244.java new file mode 100644 index 0000000000..2c2ad8a77b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2244.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2244 { + public static class Solution1 { + public int minimumRounds(int[] tasks) { + Map map = new HashMap<>(); + for (int task : tasks) { + map.put(task, map.getOrDefault(task, 0) + 1); + } + int rounds = 0; + for (int task : map.keySet()) { + int count = map.get(task); + if (count == 2 || count == 3) { + rounds++; + } else if (count == 1) { + return -1; + } else { + if (count % 3 == 0) { + rounds += count / 3; + } else { + rounds += (count / 3 + 1); + } + } + } + return rounds; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2248.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2248.java new file mode 100644 index 0000000000..e8959db007 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2248.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class _2248 { + public static class Solution1 { + public List intersection(int[][] nums) { + int[] inter = nums[0]; + for (int i = 0; i < nums.length; i++) { + inter = intersection(nums[i], inter); + } + Arrays.sort(inter); + List list = new ArrayList<>(); + for (int i = 0; i < inter.length; i++) { + list.add(inter[i]); + } + return list; + } + + public int[] intersection(int[] nums1, int[] nums2) { + Set set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet()); + Set set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet()); + set1.retainAll(set2); + int[] intersection = new int[set1.size()]; + int i = 0; + for (int num : set1) { + intersection[i++] = num; + } + return intersection; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2255.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2255.java new file mode 100644 index 0000000000..c3a03ee25b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2255.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2255 { + public static class Solution1 { + public int countPrefixes(String[] words, String s) { + int count = 0; + for (String word : words) { + if (s.startsWith(word)) { + count++; + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2256.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2256.java new file mode 100644 index 0000000000..72d7fa704c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2256.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2256 { + public static class Solution1 { + public int minimumAverageDifference(int[] nums) { + if (nums.length == 1) { + return 0; + } + long secondHalfSum = 0; + int minAveDiff = Integer.MAX_VALUE; + for (int i = 1; i < nums.length; i++) { + secondHalfSum += nums[i]; + } + long firstHalfSum = nums[0]; + int count = 1; + int minDiffIndex = 0; + for (int i = 0; i < nums.length; ) { + int firstHalfAve = (int) (firstHalfSum / count); + int secondHalfAve = 0; + if ((nums.length - count) != 0) { + secondHalfAve = (int) (secondHalfSum / (nums.length - count)); + } + if (minAveDiff > Math.abs(firstHalfAve - secondHalfAve)) { + minAveDiff = Math.abs(firstHalfAve - secondHalfAve); + minDiffIndex = i; + if (minAveDiff == 0) { + return minDiffIndex; + } + } + count++; + i++; + if (i < nums.length) { + firstHalfSum += nums[i]; + secondHalfSum -= nums[i]; + } + } + return minDiffIndex; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2259.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2259.java new file mode 100644 index 0000000000..3a0a21c918 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2259.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _2259 { + public static class Solution1 { + public String removeDigit(String number, char digit) { + StringBuilder sb = new StringBuilder(); + List nums = new ArrayList<>(); + String result = ""; + for (int i = 0; i < number.length(); i++) { + if (number.charAt(i) == digit) { + sb.append(number.substring(0, i)); + sb.append(number.substring(i + 1)); + nums.add(sb.toString()); + sb.setLength(0); + } + } + Collections.sort(nums, Collections.reverseOrder()); + return nums.get(0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2260.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2260.java new file mode 100644 index 0000000000..8bddbb6188 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2260.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _2260 { + public static class Solution1 { + public int minimumCardPickup(int[] cards) { + int min = cards.length; + Map> map = new HashMap<>(); + for (int i = 0; i < cards.length; i++) { + List list = map.getOrDefault(cards[i], new ArrayList<>()); + list.add(i); + map.put(cards[i], list); + } + boolean found = false; + for (int key : map.keySet()) { + List list = map.get(key); + for (int i = 0; i < list.size() - 1; i++) { + found = true; + min = Math.min(min, list.get(i + 1) - list.get(i) + 1); + } + } + if (!found) { + return -1; + } else { + return min; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2264.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2264.java new file mode 100644 index 0000000000..ff9a8ce678 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2264.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2264 { + public static class Solution1 { + public String largestGoodInteger(String num) { + String ans = ""; + int max = 0; + for (int i = 0; i < num.length() - 2; i++) { + if (num.charAt(i) == num.charAt(i + 1) && num.charAt(i + 1) == num.charAt(i + 2)) { + int candidate = Integer.parseInt(num.substring(i, i + 3)); + if (candidate >= max) { + max = candidate; + ans = num.substring(i, i + 3); + } + i += 2; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2265.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2265.java new file mode 100644 index 0000000000..5fde576569 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2265.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _2265 { + public static class Solution1 { + /* + * When it comes to process all subtrees first, and then process the root, it's a good candidate to use post-order traversal recursion. + * Credit: https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/editorial/ + */ + + int count; + + public int averageOfSubtree(TreeNode root) { + postOrder(root); + return count; + } + + private int[] postOrder(TreeNode root) { + if (root == null) { + return new int[2]; + } + int[] left = postOrder(root.left); + int[] right = postOrder(root.right); + int nodeSum = left[0] + right[0] + root.val; + int nodeCount = left[1] + right[1] + 1; + if (root.val == nodeSum / nodeCount) { + count++; + } + return new int[] {nodeSum, nodeCount}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2269.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2269.java new file mode 100644 index 0000000000..15d4f4d200 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2269.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2269 { + public static class Solution1 { + public int divisorSubstrings(int num, int k) { + String numStr = new StringBuilder().append(num).toString(); + int ans = 0; + for (int i = 0; i + k <= numStr.length(); i++) { + String candidate = numStr.substring(i, i + k); + int integer = Integer.parseInt(candidate); + if (integer == 0) { + continue; + } + if (num % integer == 0) { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2270.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2270.java new file mode 100644 index 0000000000..d5b9e27575 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2270.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2270 { + public static class Solution1 { + public int waysToSplitArray(int[] nums) { + long[] presum = new long[nums.length]; + for (int i = 0; i < nums.length; i++) { + if (i == 0) { + presum[i] = nums[i]; + } else { + presum[i] = presum[i - 1] + nums[i]; + } + } + int ways = 0; + long firstHalf = presum[0]; + long secondHalf = presum[nums.length - 1] - presum[0]; + for (int i = 0; i < nums.length - 1; ) { + if (firstHalf >= secondHalf) { + ways++; + } + i++; + if (i < nums.length - 1) { + firstHalf = presum[i]; + secondHalf = presum[nums.length - 1] - presum[i]; + } + } + return ways; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2273.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2273.java new file mode 100644 index 0000000000..04d796c627 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2273.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class _2273 { + public static class Solution1 { + public List removeAnagrams(String[] words) { + List result = new ArrayList<>(); + result.add(words[0]); + for (int i = 1; i < words.length; i++) { + String sorted0 = sortWord(words[i - 1]); + String sorted1 = sortWord(words[i]); + if (!sorted0.equals(sorted1)) { + result.add(words[i]); + } + } + return result; + } + + private static String sortWord(String words) { + char[] chars = words.toCharArray(); + Arrays.sort(chars); + return new String(chars); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2278.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2278.java new file mode 100644 index 0000000000..d3c765b62b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2278.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2278 { + public static class Solution1 { + public int percentageLetter(String s, char letter) { + int count = 0; + for (char c : s.toCharArray()) { + if (c == letter) { + count++; + } + } + return (int) Math.floor((count * 100.0 / s.length())); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2279.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2279.java new file mode 100644 index 0000000000..b02878cd88 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2279.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2279 { + public static class Solution1 { + public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) { + int[] delta = new int[capacity.length]; + int ans = 0; + for (int i = 0; i < capacity.length; i++) { + if (capacity[i] == rocks[i]) { + ans++; + } else { + delta[i] = capacity[i] - rocks[i]; + } + } + Arrays.sort(delta); + for (int i = 0; i < capacity.length; i++) { + if (delta[i] != 0) { + if (additionalRocks >= delta[i]) { + ans++; + additionalRocks -= delta[i]; + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2283.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2283.java new file mode 100644 index 0000000000..a5ae2f601c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2283.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2283 { + public static class Solution1 { + public boolean digitCount(String num) { + Map map = new HashMap<>(); + for (char c : num.toCharArray()) { + int n = Character.getNumericValue(c); + map.put(n, map.getOrDefault(n, 0) + 1); + } + for (int i = 0; i < num.length(); i++) { + int n = Character.getNumericValue(num.charAt(i)); + int times = map.getOrDefault(i, 0); + if (n != times) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2284.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2284.java new file mode 100644 index 0000000000..e62f8e2478 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2284.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2284 { + public static class Solution1 { + public String largestWordCount(String[] messages, String[] senders) { + Map map = new HashMap<>(); + for (int i = 0; i < messages.length; i++) { + String sender = senders[i]; + int count = messages[i].split(" ").length; + Integer existing = map.getOrDefault(sender, 0); + map.put(sender, existing + count); + } + int max = 0; + String result = ""; + for (String sender : map.keySet()) { + if (map.get(sender) > max) { + max = map.get(sender); + result = sender; + } else if (map.get(sender) == max) { + if (result.compareTo(sender) < 0) { + result = sender; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2287.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2287.java new file mode 100644 index 0000000000..0684161f54 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2287.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2287 { + public static class Solution1 { + public int rearrangeCharacters(String s, String target) { + Map targetMap = new HashMap<>(); + for (char c : target.toCharArray()) { + targetMap.put(c, targetMap.getOrDefault(c, 0) + 1); + } + Map sMap = new HashMap<>(); + for (char c : s.toCharArray()) { + sMap.put(c, sMap.getOrDefault(c, 0) + 1); + } + int result = Integer.MAX_VALUE; + for (char c : targetMap.keySet()) { + int targetCount = targetMap.get(c); + result = Math.min(result, sMap.getOrDefault(c, 0) / targetCount); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2288.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2288.java new file mode 100644 index 0000000000..f28f5c15d7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2288.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2288 { + public static class Solution1 { + public String discountPrices(String sentence, int discount) { + StringBuilder sb = new StringBuilder(); + String[] words = sentence.split(" "); + for (String word : words) { + if (word.charAt(0) == '$') { + try { + long num = Long.parseLong(word.substring(1)); + double newNum = + Math.round(num * (1 - ((discount * 1.0) / 100)) * 100.00) / 100.00; + sb.append("$"); + sb.append(String.format("%.2f", newNum)); + } catch (Exception e) { + sb.append(word); + } + } else { + sb.append(word); + } + sb.append(" "); + } + return sb.substring(0, sb.length() - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2293.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2293.java new file mode 100644 index 0000000000..406dc1026f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2293.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2293 { + public static class Solution1 { + public int minMaxGame(int[] nums) { + if (nums.length == 1) { + return nums[0]; + } + int[] newNums = new int[nums.length / 2]; + boolean min = true; + for (int i = 0, j = 0; i < nums.length; i += 2, j++) { + if (min) { + min = false; + newNums[j] = Math.min(nums[i], nums[i + 1]); + } else { + min = true; + newNums[j] = Math.max(nums[i], nums[i + 1]); + } + } + return minMaxGame(newNums); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2299.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2299.java new file mode 100644 index 0000000000..1a1fee808b --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2299.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2299 { + public static class Solution1 { + public boolean strongPasswordCheckerII(String password) { + if (password.length() < 8) { + return false; + } + boolean hasLower = false; + boolean hasUpper = false; + boolean hasDigit = false; + boolean hasSpecialChar = false; + Set specialChars = new HashSet<>(); + specialChars.add('!'); + specialChars.add('@'); + specialChars.add('%'); + specialChars.add('^'); + specialChars.add('&'); + specialChars.add('*'); + specialChars.add('('); + specialChars.add(')'); + specialChars.add('-'); + specialChars.add('+'); + specialChars.add('$'); + specialChars.add('#'); + for (int i = 0; i < password.length(); i++) { + if (Character.isLowerCase(password.charAt(i))) { + hasLower = true; + } + if (Character.isUpperCase(password.charAt(i))) { + hasUpper = true; + } + if (Character.isDigit(password.charAt(i))) { + hasDigit = true; + } + if (specialChars.contains(password.charAt(i))) { + hasSpecialChar = true; + } + if (i > 0 && password.charAt(i) == password.charAt(i - 1)) { + return false; + } + } + return hasLower && hasUpper && hasDigit && hasSpecialChar; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2300.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2300.java new file mode 100644 index 0000000000..50d1644cec --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2300.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2300 { + public static class Solution1 { + public int[] successfulPairs(int[] spells, int[] potions, long success) { + int[] result = new int[spells.length]; + Arrays.sort(potions); + for (int i = 0; i < spells.length; i++) { + int j = binarySearch(potions, success, spells[i]); + result[i] = potions.length - j; + } + return result; + } + + private int binarySearch(int[] potions, long success, int spell) { + int left = 0; + int right = potions.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if ((long) potions[mid] * spell >= success) { + right = mid; + } else { + left = mid + 1; + } + } + if (left == right + && left == potions.length - 1 + && (long) spell * potions[left] < success) { + return potions.length; + } + return right; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2303.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2303.java new file mode 100644 index 0000000000..b08e756dc6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2303.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2303 { + public static class Solution1 { + public double calculateTax(int[][] brackets, int income) { + double taxes = 0.0; + int lastUpper = 0; + for (int[] bracket : brackets) { + int upper = bracket[0]; + double percent = bracket[1] / 100.0; + if (income > upper) { + taxes += (upper - lastUpper) * percent; + } else { + taxes += (income - lastUpper) * percent; + break; + } + lastUpper = upper; + } + return taxes; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2309.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2309.java new file mode 100644 index 0000000000..55282660f8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2309.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _2309 { + public static class Solution1 { + public String greatestLetter(String s) { + List lowercase = new ArrayList<>(); + List uppercase = new ArrayList<>(); + for (char c : s.toCharArray()) { + if (Character.isLowerCase(c)) { + lowercase.add(c); + } else { + uppercase.add(c); + } + } + Collections.sort(uppercase, Collections.reverseOrder()); + Collections.sort(lowercase, Collections.reverseOrder()); + for (int i = 0; i < uppercase.size(); i++) { + for (int j = 0; j < lowercase.size(); j++) { + if (Character.toLowerCase(uppercase.get(i)) == lowercase.get(j)) { + return uppercase.get(i) + ""; + } + } + } + return ""; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2315.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2315.java new file mode 100644 index 0000000000..823ed18d17 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2315.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2315 { + public static class Solution1 { + public int countAsterisks(String s) { + int ans = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == '|') { + i++; + while (i < s.length() && s.charAt(i) != '|') { + i++; + } + } else if (s.charAt(i) == '*') { + ans++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2316.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2316.java new file mode 100644 index 0000000000..94a329ac9f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2316.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2316 { + public static class Solution1 { + public long countPairs(int n, int[][] edges) { + UnionFind uf = new UnionFind(n); + // this union is a first pass which doesn't union all nodes completely, if you set a + // debug point, you can clearly see that + for (int[] edge : edges) { + uf.union(edge[0], edge[1]); + } + Map countMap = new HashMap<>(); + // run i = 0 through to n - 1 again, and call find(), this will completely union all + // connected nodes + for (int i = 0; i < n; i++) { + int id = uf.find(i); + countMap.put(id, countMap.getOrDefault(id, 0) + 1); + } + long pairs = 0L; + long remaining = n; + for (int size : countMap.values()) { + pairs += size * (remaining - size); + remaining -= size; + } + return pairs; + } + + class UnionFind { + int[] ids; + + public UnionFind(int n) { + this.ids = new int[n]; + for (int i = 0; i < n; i++) { + this.ids[i] = i; + } + } + + public int find(int x) { + if (ids[x] != x) { + ids[x] = find(ids[x]); + } + return ids[x]; + } + + public void union(int x, int y) { + ids[find(x)] = find(y); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2319.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2319.java new file mode 100644 index 0000000000..83a6661a18 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2319.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2319 { + public static class Solution1 { + public boolean checkXMatrix(int[][] grid) { + int m = grid.length; + boolean[][] checked = new boolean[m][m]; + for (int i = 0; i < m; i++) { + if (grid[i][i] == 0) { + return false; + } else { + checked[i][i] = true; + } + } + for (int i = 0, j = m - 1; i < m && j >= 0; i++, j--) { + if (grid[i][j] == 0) { + return false; + } else { + checked[i][j] = true; + } + } + for (int i = 0; i < m; i++) { + for (int j = 0; j < m; j++) { + if (!checked[i][j] && grid[i][j] != 0) { + return false; + } + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2325.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2325.java new file mode 100644 index 0000000000..ae51f0ae1d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2325.java @@ -0,0 +1,64 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2325 { + public static class Solution1 { + public String decodeMessage(String key, String message) { + Map map = new HashMap<>(); + char realChar = 'a'; + for (char c : key.toCharArray()) { + if (c == ' ') { + continue; + } + if (!map.containsKey(c)) { + map.put(c, realChar); + realChar++; + } + if (map.size() >= 26) { + break; + } + } + StringBuilder sb = new StringBuilder(); + for (char c : message.toCharArray()) { + if (c == ' ') { + sb.append(' '); + } else { + sb.append(map.get(c)); + } + } + return sb.toString(); + } + } + + public static class Solution2 { + + public String decodeMessage(String key, String message) { + // put first occurrence of each char of key in hashmap, where k = char in key, v = + // incremental a - z alphabets + + Map bucket = new HashMap<>(); + char ch = 'a'; + char[] keyArr = key.toCharArray(); + StringBuilder result = new StringBuilder(); + + for (int i = 0; i < keyArr.length; i++) { + if (keyArr[i] != ' ' && !bucket.containsKey(keyArr[i])) { + bucket.put(keyArr[i], ch++); + } + } + + // decode the message using the bucket + char[] msgArr = message.toCharArray(); + for (int i = 0; i < msgArr.length; i++) { + if (msgArr[i] == ' ') { + result.append(" "); + } else { + result.append(bucket.get(msgArr[i])); + } + } + return result.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2326.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2326.java new file mode 100644 index 0000000000..32a851194c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2326.java @@ -0,0 +1,76 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.List; + +public class _2326 { + public static class Solution1 { + public int[][] spiralMatrix(int m, int n, ListNode head) { + List list = new ArrayList<>(); + while (head != null) { + list.add(head.val); + head = head.next; + } + int[][] matrix = new int[m][n]; + int i = 0; + int j = 0; + int index = 0; + int rightBorder = n - 1; + int bottom = m - 1; + int leftBorder = 0; + int top = 1; + int count = 0; + while (index < m * n) { + // go right + while (j <= rightBorder) { + matrix[i][j++] = index < list.size() ? list.get(index++) : -1; + count++; + } + if (count >= m * n) { + return matrix; + } + rightBorder--; + j--; + + // go down + i++; + while (i <= bottom) { + matrix[i++][j] = index < list.size() ? list.get(index++) : -1; + count++; + } + if (count >= m * n) { + return matrix; + } + i--; + bottom--; + + // go left + j--; + while (j >= leftBorder) { + matrix[i][j--] = index < list.size() ? list.get(index++) : -1; + count++; + } + if (count >= m * n) { + return matrix; + } + j++; + leftBorder++; + + // go top + i--; + while (i >= top) { + matrix[i--][j] = index < list.size() ? list.get(index++) : -1; + count++; + } + if (count >= m * n) { + return matrix; + } + i++; + top++; + j++; + } + return matrix; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2331.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2331.java new file mode 100644 index 0000000000..fb48bc2d2f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2331.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.TreeNode; + +public class _2331 { + public static class Solution1 { + public boolean evaluateTree(TreeNode root) { + if (root == null) { + return true; + } + if (root.left == null && root.right == null) { + if (root.val == 0) { + return false; + } else { + return true; + } + } + if (root.val == 2) { + return evaluateTree(root.left) || evaluateTree(root.right); + } else { + return evaluateTree(root.left) && evaluateTree(root.right); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2335.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2335.java new file mode 100644 index 0000000000..8b98c4edf9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2335.java @@ -0,0 +1,37 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.PriorityQueue; + +public class _2335 { + public static class Solution1 { + public int fillCups(int[] amount) { + PriorityQueue heap = new PriorityQueue<>((a, b) -> b - a); + for (int num : amount) { + if (num > 0) { + heap.offer(num); + } + } + int seconds = 0; + while (!heap.isEmpty()) { + if (heap.size() == 1) { + seconds += heap.poll(); + return seconds; + } + int one = heap.poll(); + one--; + if (!heap.isEmpty()) { + int two = heap.poll(); + two--; + if (two > 0) { + heap.offer(two); + } + } + if (one > 0) { + heap.offer(one); + } + seconds++; + } + return seconds; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2340.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2340.java new file mode 100644 index 0000000000..712e2988f2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2340.java @@ -0,0 +1,49 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2340 { + public static class Solution1 { + public int minimumSwaps(int[] nums) { + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + for (int num : nums) { + min = Math.min(min, num); + max = Math.max(max, num); + } + int minIndex = -1; + for (int i = 0; i < nums.length; i++) { + if (min == nums[i]) { + minIndex = i; + break; + } + } + int minSwaps = 0; + // now move the leftmost smallest index to the beginning of the array + for (int i = minIndex; i > 0; i--) { + swap(nums, i, i - 1); + minSwaps++; + } + + int maxIndex = -1; + for (int i = nums.length - 1; i >= 0; i--) { + if (max == nums[i]) { + maxIndex = i; + break; + } + } + + // now move the leftmost smallest index to the beginning of the array + for (int i = maxIndex; i < nums.length - 1; i++) { + swap(nums, i, i + 1); // this line is optional at this point + minSwaps++; + } + + return minSwaps; + } + + private void swap(int[] nums, int i, int j) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2341.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2341.java new file mode 100644 index 0000000000..b6be558282 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2341.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2341 { + public static class Solution1 { + public int[] numberOfPairs(int[] nums) { + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + int pairs = 0; + int leftover = 0; + for (int key : map.keySet()) { + if (map.get(key) > 1) { + pairs += map.get(key) / 2; + leftover += map.get(key) % 2; + } else { + leftover++; + } + } + return new int[] {pairs, leftover}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2347.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2347.java new file mode 100644 index 0000000000..329ecae9a6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2347.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class _2347 { + public static class Solution1 { + public String bestHand(int[] ranks, char[] suits) { + Set set = new HashSet<>(); + for (char c : suits) { + set.add(c); + } + if (set.size() == 1) { + return "Flush"; + } + Map map = new HashMap<>(); + for (int i : ranks) { + map.put(i, map.getOrDefault(i, 0) + 1); + } + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() >= 3) { + return "Three of a Kind"; + } + } + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() == 2) { + return "Pair"; + } + } + return "High Card"; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2351.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2351.java new file mode 100644 index 0000000000..5d4f899229 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2351.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2351 { + public static class Solution1 { + public char repeatedCharacter(String s) { + Set set = new HashSet<>(); + for (char c : s.toCharArray()) { + if (!set.add(c)) { + return c; + } + } + return ' '; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2352.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2352.java new file mode 100644 index 0000000000..fa9c14fa46 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2352.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _2352 { + public static class Solution1 { + public int equalPairs(int[][] grid) { + Map, Integer> rows = new HashMap<>(); + for (int i = 0; i < grid.length; i++) { + List row = new ArrayList<>(); + for (int j = 0; j < grid[0].length; j++) { + row.add(grid[i][j]); + } + rows.put(row, rows.getOrDefault(row, 0) + 1); + } + Map, Integer> columns = new HashMap<>(); + for (int j = 0; j < grid[0].length; j++) { + List column = new ArrayList<>(); + for (int i = 0; i < grid.length; i++) { + column.add(grid[i][j]); + } + columns.put(column, columns.getOrDefault(column, 0) + 1); + } + int pairs = 0; + for (List row : rows.keySet()) { + if (columns.containsKey(row)) { + pairs += rows.get(row) * columns.get(row); + } + } + return pairs; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2357.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2357.java new file mode 100644 index 0000000000..4a89f4d863 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2357.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.TreeSet; + +public class _2357 { + public static class Solution1 { + public int minimumOperations(int[] nums) { + TreeSet treeSet = new TreeSet<>(); + for (int num : nums) { + if (num > 0) { + treeSet.add(num); + } + } + int ops = 0; + while (!treeSet.isEmpty()) { + int min = treeSet.pollFirst(); + ops++; + TreeSet tmp = new TreeSet<>(); + while (!treeSet.isEmpty()) { + tmp.add(treeSet.pollFirst() - min); + } + treeSet.addAll(tmp); + } + return ops; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2363.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2363.java new file mode 100644 index 0000000000..8c0450851c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2363.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.TreeMap; + +public class _2363 { + public static class Solution1 { + public List> mergeSimilarItems(int[][] items1, int[][] items2) { + List> result = new ArrayList<>(); + TreeMap map = new TreeMap<>(); + for (int[] item : items1) { + map.put(item[0], map.getOrDefault(item[0], 0) + item[1]); + } + for (int[] item : items2) { + map.put(item[0], map.getOrDefault(item[0], 0) + item[1]); + } + for (int key : map.keySet()) { + result.add(new ArrayList<>(Arrays.asList(key, map.get(key)))); + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2367.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2367.java new file mode 100644 index 0000000000..60762e4a86 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2367.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2367 { + public static class Solution1 { + public int arithmeticTriplets(int[] nums, int diff) { + int count = 0; + for (int i = 0; i < nums.length - 2; i++) { + for (int j = i + 1; j < nums.length - 1; j++) { + for (int k = j + 1; k < nums.length; k++) { + if (nums[j] - nums[i] == nums[k] - nums[j] && nums[k] - nums[j] == diff) { + count++; + } + } + } + } + return count; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2373.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2373.java new file mode 100644 index 0000000000..79ede4b24a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2373.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2373 { + public static class Solution1 { + public int[][] largestLocal(int[][] grid) { + int m = grid.length; + int[][] result = new int[m - 2][m - 2]; + for (int i = 0; i < m - 2; i++) { + for (int j = 0; j < m - 2; j++) { + for (int ii = i; ii <= i + 2; ii++) { + for (int jj = j; jj <= j + 2; jj++) { + result[i][j] = Math.max(result[i][j], grid[ii][jj]); + } + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2379.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2379.java new file mode 100644 index 0000000000..55b38104f6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2379.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2379 { + public static class Solution1 { + public int minimumRecolors(String blocks, int k) { + int min = k; + int left = 0; + int right = 0; + int recolors = 0; + while (right - left < k) { + if (blocks.charAt(right) == 'W') { + recolors++; + } + right++; + } + if (right - left == k) { + min = Math.min(recolors, min); + } + while (right < blocks.length() && left < blocks.length() - k) { + if (blocks.charAt(right) == 'W') { + recolors++; + } + right++; + if (blocks.charAt(left) == 'W') { + recolors--; + } + left++; + min = Math.min(recolors, min); + } + return min; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2380.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2380.java new file mode 100644 index 0000000000..76e96a8760 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2380.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2380 { + public static class Solution1 { + public int secondsToRemoveOccurrences(String s) { + int seconds = 0; + StringBuilder sb = new StringBuilder(s); + while (hasZeroOneCount(sb)) { + for (int i = 0; i < sb.length() - 1; ) { + if (sb.charAt(i) == '0' && sb.charAt(i + 1) == '1') { + sb.setCharAt(i++, '1'); + sb.setCharAt(i++, '0'); + } else { + i++; + } + } + seconds++; + } + return seconds; + } + + private boolean hasZeroOneCount(StringBuilder sb) { + for (int i = 0; i < sb.length() - 1; i++) { + if (sb.charAt(i) == '0' && sb.charAt(i + 1) == '1') { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2385.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2385.java new file mode 100644 index 0000000000..de6255ec64 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2385.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.*; + +public class _2385 { + public static class Solution1 { + public int amountOfTime(TreeNode root, int start) { + Map> adjList = new HashMap<>(); + buildAdjList(root, adjList); + Queue q = new LinkedList<>(); + q.offer(start); + Set visited = new HashSet<>(); + visited.add(start); + int times = -1; + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + Integer curr = q.poll(); + if (adjList.containsKey(curr)) { + for (int node : adjList.get(curr)) { + if (visited.add(node)) { + q.offer(node); + } + } + } + } + times++; + } + return times; + } + + private void buildAdjList(TreeNode root, Map> adjList) { + if (root == null) { + return; + } + if (root.left != null) { + List list = adjList.getOrDefault(root.val, new ArrayList<>()); + list.add(root.left.val); + adjList.put(root.val, list); + + list = adjList.getOrDefault(root.left.val, new ArrayList<>()); + list.add(root.val); + adjList.put(root.left.val, list); + } + + if (root.right != null) { + List list = adjList.getOrDefault(root.val, new ArrayList<>()); + list.add(root.right.val); + adjList.put(root.val, list); + + list = adjList.getOrDefault(root.right.val, new ArrayList<>()); + list.add(root.val); + adjList.put(root.right.val, list); + } + buildAdjList(root.left, adjList); + buildAdjList(root.right, adjList); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2389.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2389.java new file mode 100644 index 0000000000..46f2f2c386 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2389.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Map; +import java.util.TreeMap; + +public class _2389 { + public static class Solution1 { + /* + * My completely original solution, not sure why it's labeled EASY, IMHO, it should be a soft MEDIUM. + */ + public int[] answerQueries(int[] nums, int[] queries) { + TreeMap map = new TreeMap<>(); + int total = 0; + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + total += num; + } + int[] answer = new int[queries.length]; + for (int i = 0; i < queries.length; i++) { + int sum = total; + int len = nums.length; + TreeMap copy = new TreeMap<>(map); + if (sum <= queries[i]) { + answer[i] = len; + } else { + do { + sum -= copy.lastKey(); + len--; + if (sum <= queries[i]) { + answer[i] = len; + break; + } + Map.Entry lastEntry = copy.pollLastEntry(); + if (lastEntry.getValue() > 1) { + copy.put(lastEntry.getKey(), lastEntry.getValue() - 1); + } + } while (sum > queries[i]); + } + } + return answer; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2392.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2392.java new file mode 100644 index 0000000000..c3a4602bfa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2392.java @@ -0,0 +1,68 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _2392 { + public static class Solution1 { + /* + * I figured out I needed to use Kahn's algorithm to topologically sort both rowConditions and colConditions, + * but unsure how to fill the matrix. + * https://leetcode.com/problems/build-a-matrix-with-conditions/editorial/ is brilliant as of how to build the matrix: + * using its slides to step through helps a lot! + */ + public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) { + int[] topologicallySortedRows = topologicalSort(rowConditions, k); + int[] topologicallySortedCols = topologicalSort(colConditions, k); + if (topologicallySortedRows.length == 0 || topologicallySortedCols.length == 0) { + return new int[][] {}; + } + int[][] matrix = new int[k][k]; + for (int i = 0; i < k; i++) { + for (int j = 0; j < k; j++) { + if (topologicallySortedRows[i] == topologicallySortedCols[j]) { + matrix[i][j] = topologicallySortedCols[j]; + } + } + } + return matrix; + } + + private int[] topologicalSort(int[][] conditions, int k) { + List[] adj = new ArrayList[k + 1]; + for (int i = 0; i <= k; i++) { + adj[i] = new ArrayList<>(); + } + int[] indegree = new int[k + 1]; + int[] order = new int[k]; + int index = 0; + for (int[] x : conditions) { + adj[x[0]].add(x[1]); + indegree[x[1]]++; + } + Queue q = new LinkedList<>(); + for (int i = 1; i <= k; i++) { + if (indegree[i] == 0) { + q.offer(i); + } + } + while (!q.isEmpty()) { + Integer curr = q.poll(); + order[index++] = curr; + k--; + for (int v : adj[curr]) { + indegree[v]--; + if (indegree[v] == 0) { + q.offer(v); + } + } + } + if (k != 0) { + return new int[0]; + } + return order; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2395.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2395.java new file mode 100644 index 0000000000..4973680843 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2395.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2395 { + public static class Solution1 { + public boolean findSubarrays(int[] nums) { + Set sums = new HashSet<>(); + for (int i = 0; i < nums.length - 1; i++) { + int sum = nums[i] + nums[i + 1]; + if (!sums.add(sum)) { + return true; + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2399.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2399.java new file mode 100644 index 0000000000..83265af33c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2399.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2399 { + public static class Solution1 { + public boolean checkDistances(String s, int[] distance) { + Map map = new HashMap<>(); + int i = 0; + for (char c : s.toCharArray()) { + if (!map.containsKey(c)) { + map.put(c, new int[] {-1, -1}); + } + int[] indices = map.get(c); + if (indices[0] == -1) { + indices[0] = i; + } else { + indices[1] = i; + } + i++; + } + for (char c : map.keySet()) { + int index = c - 'a'; + int[] indices = map.get(c); + if (distance[index] + 1 != indices[1] - indices[0]) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2404.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2404.java new file mode 100644 index 0000000000..9a314f983d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2404.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _2404 { + public static class Solution1 { + public int mostFrequentEven(int[] nums) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 2 == 0) { + map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); + } + } + List smallestEvens = new ArrayList<>(); + int freq = 0; + for (int key : map.keySet()) { + if (map.get(key) > freq) { + smallestEvens.clear(); + freq = map.get(key); + smallestEvens.add(key); + } else if (map.get(key) == freq) { + smallestEvens.add(key); + } + } + if (smallestEvens.size() < 1) { + return -1; + } + Collections.sort(smallestEvens); + return smallestEvens.get(0); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2409.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2409.java new file mode 100644 index 0000000000..b54c031419 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2409.java @@ -0,0 +1,79 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2409 { + public static class Solution1 { + /* + * Brute force: check each day of the 365 days if both of them are in Rome. + */ + public int countDaysTogether( + String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) { + int[] monthToDays = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int days = 0; + String[] arriveAliceParts = arriveAlice.split("-"); + String[] leaveAliceParts = leaveAlice.split("-"); + String[] arriveBobParts = arriveBob.split("-"); + String[] leaveBobParts = leaveBob.split("-"); + for (int i = 1; i < monthToDays.length; i++) { + int daysInMonth = monthToDays[i]; + for (int j = 1; j <= daysInMonth; j++) { + if (bothInRome( + i, + j, + arriveAliceParts, + leaveAliceParts, + arriveBobParts, + leaveBobParts)) { + days++; + } + } + } + return days; + } + + private boolean bothInRome( + int month, + int day, + String[] arriveAliceParts, + String[] leaveAliceParts, + String[] arriveBobParts, + String[] leaveBobParts) { + int aliceArriveMonth = Integer.parseInt(arriveAliceParts[0]); + int aliceArriveDay = Integer.parseInt(arriveAliceParts[1]); + int aliceLeaveMonth = Integer.parseInt(leaveAliceParts[0]); + int aliceLeaveDay = Integer.parseInt(leaveAliceParts[1]); + + int bobArriveMonth = Integer.parseInt(arriveBobParts[0]); + int bobArriveDay = Integer.parseInt(arriveBobParts[1]); + int bobLeaveMonth = Integer.parseInt(leaveBobParts[0]); + int bobLeaveDay = Integer.parseInt(leaveBobParts[1]); + + return inRome( + aliceArriveMonth, + aliceArriveDay, + aliceLeaveMonth, + aliceLeaveDay, + month, + day) + && inRome(bobArriveMonth, bobArriveDay, bobLeaveMonth, bobLeaveDay, month, day); + } + + private boolean inRome( + int arriveMonth, int arriveDay, int leaveMonth, int leaveDay, int month, int day) { + if (month < arriveMonth || month > leaveMonth) { + return false; + } + if (month > arriveMonth && month < leaveMonth) { + return true; + } + if (month == arriveMonth) { + if (arriveMonth == leaveMonth) { + return arriveDay <= day && leaveDay >= day; + } else { + return arriveDay <= day; + } + } + // now, only this case: month == leaveMonth + return day <= leaveDay; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2413.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2413.java new file mode 100644 index 0000000000..1b0bf816a7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2413.java @@ -0,0 +1,9 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2413 { + public static class Solution1 { + public int smallestEvenMultiple(int n) { + return n % 2 == 0 ? n : n * 2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2418.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2418.java new file mode 100644 index 0000000000..a4f28b1f55 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2418.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.PriorityQueue; + +public class _2418 { + public static class Solution1 { + public String[] sortPeople(String[] names, int[] heights) { + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b[1] - a[1]); + for (int i = 0; i < names.length; i++) { + maxHeap.offer(new int[] {i, heights[i]}); + } + String[] res = new String[names.length]; + int i = 0; + while (!maxHeap.isEmpty()) { + res[i++] = names[maxHeap.poll()[0]]; + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2423.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2423.java new file mode 100644 index 0000000000..f89c491c22 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2423.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2423 { + public static class Solution1 { + /* + * This is my original, but unnecessarily verbose solution. + * Instead, you can just brute force each one of the 26 letters, as long as any one of them makes it meet the requirement, it returns true. + */ + public boolean equalFrequency(String word) { + int[] count = new int[26]; + for (char c : word.toCharArray()) { + count[c - 'a']++; + } + Arrays.sort(count); + return decLast(count) || decFirst(count) || allOnes(count); + } + + private boolean allOnes(int[] count) { + for (int i : count) { + if (i != 1 && i != 0) { + return false; + } + } + return true; + } + + private boolean decFirst(int[] count) { + int start = 0; + int firstVal = -1; + for (int i = 0; i < 26; i++) { + if (count[i] != 0) { + start = i + 1; + firstVal = count[i] - 1; + break; + } + } + if (firstVal == 0) { + int nextVal = count[start++]; + for (; start < 26; start++) { + if (count[start] != nextVal) { + return false; + } + } + return true; + } + for (; start < 26; start++) { + if (count[start] != firstVal) { + return false; + } + } + return true; + } + + private boolean decLast(int[] count) { + int last = count[25] - 1; + for (int i = 24; i >= 0; i--) { + if (count[i] != 0 && count[i] != last) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2427.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2427.java new file mode 100644 index 0000000000..bb169c9653 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2427.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2427 { + public static class Solution1 { + public int commonFactors(int a, int b) { + int ans = 1; + int num = 2; + while (num <= a && num <= b) { + if (a % num == 0 && b % num == 0) { + ans++; + } + num++; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2432.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2432.java new file mode 100644 index 0000000000..57ab5fedfc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2432.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2432 { + public static class Solution1 { + public int hardestWorker(int n, int[][] logs) { + int startTime = 0; + int maxDuration = 0; + int result = 0; + for (int i = 0; i < logs.length; i++) { + int duration = logs[i][1] - startTime; + startTime = logs[i][1]; + if (duration > maxDuration) { + result = logs[i][0]; + maxDuration = duration; + } else if (duration == maxDuration) { + if (logs[i][0] < result) { + result = logs[i][0]; + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2433.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2433.java new file mode 100644 index 0000000000..d3396c18eb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2433.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2433 { + public static class Solution1 { + public int[] findArray(int[] pref) { + int[] arr = new int[pref.length]; + arr[0] = pref[0]; + int arrResult = arr[0]; + for (int i = 1; i < pref.length; i++) { + arr[i] = arrResult ^ pref[i]; + arrResult ^= arr[i]; + } + return arr; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2437.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2437.java new file mode 100644 index 0000000000..b83420b045 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2437.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2437 { + public static class Solution1 { + public int countTime(String time) { + int[] count = new int[] {2, 10, 0, 6, 10}; + int times = 1; + for (int i = 0; i < time.length(); i++) { + if (time.charAt(i) == '?') { + if (i == 0 && time.charAt(0) == '?') { + if (time.charAt(1) == '?') { + times *= 24; + } else if (Character.getNumericValue(time.charAt(1)) < 4) { + times *= 3; + } else if (Character.getNumericValue(time.charAt(1)) >= 4) { + times *= 2; + } + i++; + } else if (i == 1 && (time.charAt(0) == '2' || time.charAt(0) == '?')) { + times *= 4; + } else { + times *= count[i]; + } + } + } + return times; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2441.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2441.java new file mode 100644 index 0000000000..9fe8ebe2db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2441.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2441 { + public static class Solution1 { + public int findMaxK(int[] nums) { + int maxK = -1; + Set set = new HashSet<>(); + for (int i = 0; i < nums.length; i++) { + set.add(nums[i]); + if (set.contains(-nums[i])) { + maxK = Math.max(maxK, Math.abs(nums[i])); + } + } + return maxK; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2446.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2446.java new file mode 100644 index 0000000000..a1b876ef62 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2446.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2446 { + public static class Solution1 { + public boolean haveConflict(String[] event1, String[] event2) { + int startMinute1 = getAbsoluteMinute(event1[0]); + int endMinute1 = getAbsoluteMinute(event1[1]); + int startMinute2 = getAbsoluteMinute(event2[0]); + int endMinute2 = getAbsoluteMinute(event2[1]); + for (int h = 0; h <= 23; h++) { + for (int m = 0; m <= 59; m++) { + int currentTime = h * 60 + m; + if (inTime(currentTime, startMinute1, endMinute1) + && inTime(currentTime, startMinute2, endMinute2)) { + return true; + } + } + } + return false; + } + + private boolean inTime(int currentMinute, int startMinute, int endMinute) { + if (currentMinute >= startMinute && currentMinute <= endMinute) { + return true; + } + return false; + } + + private int getAbsoluteMinute(String event) { + String[] parts = event.split(":"); + String hour = parts[0]; + String minute = parts[1]; + int hourInt = Integer.parseInt(hour); + int minuteInt = Integer.parseInt(minute); + return hourInt * 60 + minuteInt; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2451.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2451.java new file mode 100644 index 0000000000..1fbccfadfb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2451.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _2451 { + public static class Solution1 { + public String oddString(String[] words) { + Map, List> map = new HashMap<>(); + for (String word : words) { + List diffs = computeDiff(word); + List list = map.getOrDefault(diffs, new ArrayList<>()); + list.add(word); + map.put(diffs, list); + } + for (Map.Entry, List> entry : map.entrySet()) { + if (entry.getValue().size() == 1) { + return entry.getValue().get(0); + } + } + return null; + } + + private List computeDiff(String word) { + List diffs = new ArrayList<>(); + for (int i = 0; i < word.length() - 1; i++) { + diffs.add(word.charAt(i + 1) - word.charAt(i)); + } + return diffs; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2455.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2455.java new file mode 100644 index 0000000000..e49e68f465 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2455.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2455 { + public static class Solution1 { + public int averageValue(int[] nums) { + Long sum = 0L; + int count = 0; + for (int num : nums) { + if (num % 3 == 0 && num % 2 == 0) { + sum += num; + count++; + } + } + return count != 0 ? (int) (sum / count) : 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2460.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2460.java new file mode 100644 index 0000000000..4825a7a406 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2460.java @@ -0,0 +1,32 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2460 { + public static class Solution1 { + public int[] applyOperations(int[] nums) { + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] == nums[i + 1]) { + nums[i] *= 2; + nums[i + 1] = 0; + } + } + // i points at the first zero element, j keeps moving and bypassing zeroes to find the + // next non-zero element + for (int i = 0, j = 0; i < nums.length && j < nums.length; ) { + while (i < nums.length && nums[i] != 0) { + i++; + } + if (j < i) { + j = i; + } + while (j < nums.length && nums[j] == 0) { + j++; + } + if (j < nums.length) { + nums[i++] = nums[j]; + nums[j++] = 0; + } + } + return nums; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2465.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2465.java new file mode 100644 index 0000000000..962c25b60d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2465.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _2465 { + public static class Solution1 { + public int distinctAverages(int[] nums) { + Arrays.sort(nums); + Set averageSet = new HashSet<>(); + for (int i = 0, j = nums.length - 1; i < j; i++, j--) { + averageSet.add((nums[i] + nums[j]) / 2.0); + } + return averageSet.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2469.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2469.java new file mode 100644 index 0000000000..19344acfdd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2469.java @@ -0,0 +1,9 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2469 { + public static class Solution1 { + public double[] convertTemperature(double celsius) { + return new double[] {celsius + 273.15, celsius * 1.80 + 32.00}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2473.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2473.java new file mode 100644 index 0000000000..b2812773f1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2473.java @@ -0,0 +1,60 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.PriorityQueue; + +public class _2473 { + public static class Solution1 { + /* + * My completely original solution, Dijkstra algorithm! + */ + public long[] minCost(int n, int[][] roads, int[] appleCost, int k) { + List[] graph = new ArrayList[n]; + for (int i = 0; i < n; i++) { + graph[i] = new ArrayList<>(); + } + for (int[] road : roads) { + graph[road[0] - 1].add(new int[] {road[1] - 1, road[2]}); + graph[road[1] - 1].add(new int[] {road[0] - 1, road[2]}); + } + long[] ans = new long[n]; + for (int i = 1; i <= n; i++) { + ans[i - 1] = dijkstra(graph, appleCost, k, i); + } + return ans; + } + + private long dijkstra(List[] graph, int[] appleCost, int k, int startCity) { + long[] minCostEachCity = new long[appleCost.length]; + Arrays.fill(minCostEachCity, Integer.MAX_VALUE); + minCostEachCity[startCity - 1] = 0; + PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]); + minHeap.offer(new int[] {startCity - 1, 0}); + while (!minHeap.isEmpty()) { + int[] curr = minHeap.poll(); + int currCity = curr[0]; + int currCost = curr[1]; + if (currCost > minCostEachCity[currCity]) { + continue; + } + for (int[] neighbor : graph[currCity]) { + int neighborCity = neighbor[0]; + int neighborCost = neighbor[1]; + int neighborTotalCost = currCost + neighborCost * (k + 1); + if (neighborTotalCost < minCostEachCity[neighborCity]) { + minCostEachCity[neighborCity] = neighborTotalCost; + minHeap.offer( + new int[] {neighborCity, (int) minCostEachCity[neighborCity]}); + } + } + } + long min = Long.MAX_VALUE; + for (int i = 0; i < minCostEachCity.length; i++) { + min = Math.min(min, minCostEachCity[i] + appleCost[i]); + } + return min; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2475.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2475.java new file mode 100644 index 0000000000..c2402a04e9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2475.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2475 { + public static class Solution1 { + public int unequalTriplets(int[] nums) { + int triplets = 0; + for (int i = 0; i < nums.length - 2; i++) { + for (int j = i + 1; j < nums.length - 1; j++) { + if (nums[i] != nums[j]) { + for (int k = j + 1; k < nums.length; k++) { + if (nums[i] != nums[k] && nums[j] != nums[k]) { + triplets++; + } + } + } + } + } + return triplets; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2481.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2481.java new file mode 100644 index 0000000000..86aa7e16a1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2481.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2481 { + public static class Solution1 { + public int numberOfCuts(int n) { + if (n == 1) { + return 0; + } + if (n % 2 == 0) { + return n / 2; + } + return n; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2485.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2485.java new file mode 100644 index 0000000000..c23529603a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2485.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2485 { + public static class Solution1 { + public int pivotInteger(int n) { + int sum1 = 1; + int sum2 = 0; + for (int i = 1; i <= n; i++) { + sum2 += i; + } + if (sum1 == sum2) { + return n; + } + for (int i = 2; i <= n; i++) { + sum1 += i; + sum2 -= i - 1; + if (sum1 == sum2) { + return i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2487.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2487.java new file mode 100644 index 0000000000..e4b6ddcddd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2487.java @@ -0,0 +1,53 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.ListNode; +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +public class _2487 { + public static class Solution1 { + /* + * This is sort of cheating, i.e. transforming the linked list into an array instead of operating on the linked list itself. + */ + public ListNode removeNodes(ListNode head) { + List list = getList(head); + Deque rightBiggest = getRightBiggest(list); + ListNode pre = new ListNode(-1); + ListNode tmp = pre; + for (int i = 0; i < list.size(); i++) { + if (list.get(i) >= rightBiggest.pollFirst()) { + tmp.next = new ListNode(list.get(i)); + tmp = tmp.next; + } + } + return pre.next; + } + + private Deque getRightBiggest(List list) { + Deque result = new LinkedList<>(); + int max = list.get(list.size() - 1); + result.addFirst(max); + for (int i = list.size() - 2; i >= 0; i--) { + max = Math.max(max, list.get(i)); + result.addFirst(max); + } + return result; + } + + private List getList(ListNode head) { + ListNode tmp = head; + List list = new ArrayList<>(); + while (tmp != null) { + list.add(tmp.val); + tmp = tmp.next; + } + return list; + } + } + + public static class Solution2 { + // TODO: use stack to solve this problem + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2490.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2490.java new file mode 100644 index 0000000000..414e9e9015 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2490.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2490 { + public static class Solution1 { + public boolean isCircularSentence(String sentence) { + String[] words = sentence.split("\\ "); + for (int i = 0; i < words.length - 1; i++) { + if (words[i].charAt(words[i].length() - 1) != words[i + 1].charAt(0)) { + return false; + } + } + return words[0].charAt(0) + == words[words.length - 1].charAt(words[words.length - 1].length() - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2492.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2492.java new file mode 100644 index 0000000000..f14ae78f5c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2492.java @@ -0,0 +1,57 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2492 { + public static class Solution1 { + public int minScore(int n, int[][] roads) { + UnionFind uf = new UnionFind(n); + // union all roads first + for (int[] road : roads) { + uf.union(road[0], road[1]); + } + // now call find() to completely union all connected cities + for (int i = 1; i <= n; i++) { + uf.find(i); + } + // now we'd like to find all cities that are connected to city 1 + Set nodes = new HashSet<>(); + int startIndex = uf.find(1); + for (int i = 2; i <= n; i++) { + if (uf.find(i) == startIndex) { + nodes.add(i); + } + } + int minScore = Integer.MAX_VALUE; + for (int[] road : roads) { + if (nodes.contains(road[0]) || nodes.contains(road[1])) { + minScore = Math.min(minScore, road[2]); + } + } + return minScore; + } + + static class UnionFind { + int[] ids; + + public UnionFind(int n) { + this.ids = new int[n + 1]; + for (int i = 1; i <= n; i++) { + this.ids[i] = i; + } + } + + public int find(int x) { + if (x != ids[x]) { + ids[x] = find(ids[x]); + } + return ids[x]; + } + + public void union(int x, int y) { + ids[find(x)] = find(y); + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2496.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2496.java new file mode 100644 index 0000000000..44a0b4762f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2496.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2496 { + public static class Solution1 { + public int maximumValue(String[] strs) { + int max = 0; + for (String str : strs) { + try { + int num = Integer.parseInt(str); + max = Math.max(max, num); + } catch (Exception e) { + max = Math.max(max, str.length()); + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2500.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2500.java new file mode 100644 index 0000000000..a9830a83bb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2500.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2500 { + public static class Solution1 { + public int deleteGreatestValue(int[][] grid) { + int sum = 0; + for (int i = 0; i < grid.length; i++) { + Arrays.sort(grid[i]); + } + for (int j = grid[0].length - 1; j >= 0; j--) { + int max = grid[0][j]; + for (int i = 1; i < grid.length; i++) { + max = Math.max(max, grid[i][j]); + } + sum += max; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2501.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2501.java new file mode 100644 index 0000000000..0b94ae4ee0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2501.java @@ -0,0 +1,48 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2501 { + public static class Solution1 { + /** + * Based on the constraints, we know the longest square streak is 5: 2, 4, 16, 256, 65536 or + * 3, 9, 81, 6561, 43046721 (> 10 to the power of 5 already) + */ + public int longestSquareStreak(int[] nums) { + Arrays.sort(nums); + int ans = -1; + for (int i = 0; i < nums.length; i++) { + int times = 1; + int square = (int) Math.pow(nums[i], 2); + while (square <= nums[nums.length - 1]) { + if (exists(nums, square)) { + square = (int) Math.pow(square, 2); + times++; + } else { + break; + } + } + if (times > 1) { + ans = Math.max(ans, times); + } + } + return ans; + } + + private boolean exists(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) { + return true; + } else if (nums[mid] > target) { + right = mid - 1; + } else { + left = mid + 1; + } + } + return left == right ? nums[left] == target ? true : false : false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2506.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2506.java new file mode 100644 index 0000000000..df069c451c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2506.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2506 { + public static class Solution1 { + public int similarPairs(String[] words) { + String[] symbols = new String[words.length]; + for (int i = 0; i < words.length; i++) { + symbols[i] = compress(words[i]); + } + int pairs = 0; + for (int i = 0; i < symbols.length - 1; i++) { + for (int j = i + 1; j < symbols.length; j++) { + if (symbols[i].equals(symbols[j])) { + pairs++; + } + } + } + return pairs; + } + + private String compress(String word) { + int[] count = new int[26]; + for (char c : word.toCharArray()) { + count[c - 'a']++; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 26; i++) { + if (count[i] > 0) { + sb.append(i + 'a'); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2511.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2511.java new file mode 100644 index 0000000000..a2ce9daeba --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2511.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2511 { + public static class Solution1 { + public int captureForts(int[] forts) { + int max = 0; + for (int i = 0; i < forts.length; i++) { + if (forts[i] == 1 || forts[i] == -1) { + for (int j = i + 1; j < forts.length; j++) { + if ((forts[i] == 1 && forts[j] == -1) + || (forts[i] == -1 && forts[j] == 1)) { + max = Math.max(max, j - i - 1); + break; + } else if ((forts[j] == 1 && forts[i] == 1) + || forts[j] == -1 && forts[i] == -1) { + break; + } + } + } + } + return max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2515.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2515.java new file mode 100644 index 0000000000..05d15f7912 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2515.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2515 { + public static class Solution1 { + public int closetTarget(String[] words, String target, int startIndex) { + int ans = words.length; + if (words[startIndex].equals(target)) { + return 0; + } + // move forward + int forwardSteps = 1; + for (int i = (startIndex + 1) % words.length; + i != startIndex; + i = ((i + 1) % words.length)) { + if (words[i].equals(target)) { + ans = Math.min(ans, forwardSteps); + break; + } + forwardSteps++; + } + // move backward + int backwardSteps = 1; + for (int i = (startIndex - 1 + words.length) % words.length; + i != startIndex; + i = ((i - 1 + words.length) % words.length)) { + if (words[i].equals(target)) { + ans = Math.min(ans, backwardSteps); + break; + } + backwardSteps++; + } + return ans == words.length ? -1 : ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2520.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2520.java new file mode 100644 index 0000000000..462a008d3c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2520.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2520 { + public static class Solution1 { + public int countDigits(int num) { + int original = num; + int sum = 0; + while (num != 0) { + int digit = num % 10; + num /= 10; + if (original % digit == 0) { + sum++; + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2525.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2525.java new file mode 100644 index 0000000000..841e86c6c4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2525.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2525 { + public static class Solution1 { + public String categorizeBox(int length, int width, int height, int mass) { + int dimensionLimit = 10000; + int volumeLimit = 1000000000; + boolean isBulky = false; + long volume = (long) length * width * height; + if (length >= dimensionLimit + || width >= dimensionLimit + || height >= dimensionLimit + || volume >= volumeLimit) { + isBulky = true; + } + boolean isHeavy = mass >= 100; + if (isBulky && isHeavy) { + return "Both"; + } else if (!isBulky && !isHeavy) { + return "Neither"; + } else if (isBulky && !isHeavy) { + return "Bulky"; + } else { + return "Heavy"; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2529.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2529.java new file mode 100644 index 0000000000..3ef7610179 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2529.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2529 { + public static class Solution1 { + public int maximumCount(int[] nums) { + int pos = 0; + int neg = 0; + for (int num : nums) { + if (num > 0) { + pos++; + } else if (num < 0) { + neg++; + } + } + return Math.max(pos, neg); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2530.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2530.java new file mode 100644 index 0000000000..ebff66b0e8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2530.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.PriorityQueue; + +public class _2530 { + public static class Solution1 { + public long maxKelements(int[] nums, int k) { + long ans = 0L; + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); + for (int num : nums) { + maxHeap.offer(num); + } + while (k-- > 0) { + int max = maxHeap.poll(); + ans += max; + maxHeap.offer((int) Math.ceil((double) max / 3)); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2535.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2535.java new file mode 100644 index 0000000000..0b8ba8f023 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2535.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2535 { + public static class Solution1 { + public int differenceOfSum(int[] nums) { + long elementSum = 0L; + long digitSum = 0L; + for (int num : nums) { + elementSum += num; + while (num != 0) { + digitSum += num % 10; + num /= 10; + } + } + return (int) Math.abs(elementSum - digitSum); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2536.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2536.java new file mode 100644 index 0000000000..3500a5eab4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2536.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2536 { + public static class Solution1 { + public int[][] rangeAddQueries(int n, int[][] queries) { + int[][] matrix = new int[n][n]; + for (int[] query : queries) { + int row1 = query[0]; + int col1 = query[1]; + int row2 = query[2]; + int col2 = query[3]; + for (int i = row1; i <= row2; i++) { + for (int j = col1; j <= col2; j++) { + matrix[i][j]++; + } + } + } + return matrix; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2540.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2540.java new file mode 100644 index 0000000000..d76973d8b3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2540.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2540 { + public static class Solution1 { + public int getCommon(int[] nums1, int[] nums2) { + Set set1 = new HashSet<>(); + for (int num : nums1) { + set1.add(num); + } + Set set2 = new HashSet<>(); + for (int num : nums2) { + set2.add(num); + } + int result = -1; + for (int num : nums1) { + if (set2.contains(num)) { + result = num; + break; + } + } + for (int num : nums2) { + if (set1.contains(num) && result > num) { + result = num; + break; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2544.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2544.java new file mode 100644 index 0000000000..b27cfaf9b4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2544.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2544 { + public static class Solution1 { + public int alternateDigitSum(int n) { + int result = 0; + int original = n; + int digits = 0; + while (n != 0) { + n /= 10; + digits++; + } + boolean plus = digits % 2 != 0; + while (original != 0) { + int lastDigit = original % 10; + if (plus) { + result += lastDigit; + } else { + result -= lastDigit; + } + plus = !plus; + original /= 10; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2549.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2549.java new file mode 100644 index 0000000000..6904149e57 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2549.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2549 { + public static class Solution1 { + public int distinctIntegers(int n) { + Set total = new HashSet<>(); + total.add(n); + Set setToGoThrough = new HashSet<>(); + setToGoThrough.add(n); + Set newSet = new HashSet<>(); + int days = 1000000000; + int lastTotal = total.size(); + while (days-- > 0) { + for (int num : setToGoThrough) { + for (int i = 1; i <= num; i++) { + if (num % i == 1 && !total.contains(i)) { + newSet.add(i); + } + } + } + setToGoThrough = new HashSet<>(newSet); + total.addAll(newSet); + if (lastTotal == total.size()) { + return total.size(); + } + lastTotal = total.size(); + newSet.clear(); + } + return total.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2553.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2553.java new file mode 100644 index 0000000000..8069ce4f1e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2553.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2553 { + public static class Solution1 { + public int[] separateDigits(int[] nums) { + List list = new ArrayList<>(); + for (int num : nums) { + List thisList = new ArrayList<>(); + while (num != 0) { + thisList.add(num % 10); + num /= 10; + } + for (int i = thisList.size() - 1; i >= 0; i--) { + list.add(thisList.get(i)); + } + } + int[] result = new int[list.size()]; + int i = 0; + for (int num : list) { + result[i++] = num; + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2554.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2554.java new file mode 100644 index 0000000000..38a0552493 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2554.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2554 { + public static class Solution1 { + public int maxCount(int[] banned, int n, int maxSum) { + Set set = new HashSet<>(); + for (int b : banned) { + set.add(b); + } + int maxCnt = 0; + int sum = 0; + for (int i = 1; i <= n; i++) { + if (!set.contains(i)) { + if (sum + i > maxSum) { + return maxCnt; + } else { + sum += i; + maxCnt++; + } + } + } + return maxCnt; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2558.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2558.java new file mode 100644 index 0000000000..1b19839795 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2558.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.PriorityQueue; + +public class _2558 { + public static class Solution1 { + public long pickGifts(int[] gifts, int k) { + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); + for (int g : gifts) { + maxHeap.offer(g); + } + while (k-- > 0) { + int max = maxHeap.poll(); + maxHeap.offer((int) Math.sqrt(max)); + } + long res = 0L; + while (!maxHeap.isEmpty()) { + res += maxHeap.poll(); + } + return res; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2559.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2559.java new file mode 100644 index 0000000000..b208cbd6a4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2559.java @@ -0,0 +1,47 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _2559 { + public static class Solution1 { + public int[] vowelStrings(String[] words, int[][] queries) { + int[] ans = new int[queries.length]; + boolean[] vowels = new boolean[words.length]; + Set set = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u')); + int i = 0; + for (String word : words) { + if (word.length() == 1) { + if (set.contains(word.charAt(0))) { + vowels[i] = true; + } + } else if (set.contains(word.charAt(0)) + && set.contains(word.charAt(word.length() - 1))) { + vowels[i] = true; + } + i++; + } + int[] preSums = new int[words.length]; + preSums[0] = vowels[0] == true ? 1 : 0; + for (int k = 1; k < words.length; k++) { + if (vowels[k]) { + preSums[k] = preSums[k - 1] + 1; + } else { + preSums[k] = preSums[k - 1]; + } + } + i = 0; + for (int[] query : queries) { + int start = query[0]; + int end = query[1]; + if (start == 0) { + ans[i++] = preSums[end]; + } else { + ans[i++] = preSums[end] - preSums[start - 1]; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2562.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2562.java new file mode 100644 index 0000000000..3ca45febc8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2562.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2562 { + public static class Solution1 { + public long findTheArrayConcVal(int[] nums) { + long sum = 0; + int left = 0; + int right = nums.length - 1; + while (left < right) { + int first = nums[left++]; + int last = nums[right--]; + int times = 1; + sum += last; + while (last != 0) { + last /= 10; + times *= 10; + } + sum += first * times; + } + if (left == right) { + sum += nums[left]; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2566.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2566.java new file mode 100644 index 0000000000..40dad3633d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2566.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2566 { + public static class Solution1 { + public int minMaxDifference(int num) { + List digits = new ArrayList<>(); + while (num != 0) { + digits.add(num % 10); + num /= 10; + } + int toReplace = Integer.MAX_VALUE; + List maxDigits = new ArrayList<>(); + for (int i = digits.size() - 1; i >= 0; i--) { + if (toReplace == Integer.MAX_VALUE && digits.get(i) != 9) { + toReplace = digits.get(i); + maxDigits.add(9); + } else if (digits.get(i) == toReplace) { + maxDigits.add(9); + } else { + maxDigits.add(digits.get(i)); + } + } + int max = 0; + int times = 1; + for (int i = maxDigits.size() - 1; i >= 0; i--) { + max += maxDigits.get(i) * times; + times *= 10; + } + + toReplace = Integer.MIN_VALUE; + List minDigits = new ArrayList<>(); + for (int i = digits.size() - 1; i >= 0; i--) { + if (toReplace == Integer.MIN_VALUE && digits.get(i) != 0) { + toReplace = digits.get(i); + minDigits.add(0); + } else if (digits.get(i) == toReplace) { + minDigits.add(0); + } else { + minDigits.add(digits.get(i)); + } + } + int min = 0; + times = 1; + for (int i = minDigits.size() - 1; i >= 0; i--) { + min += minDigits.get(i) * times; + times *= 10; + } + return max - min; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2570.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2570.java new file mode 100644 index 0000000000..967eddd18a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2570.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2570 { + public static class Solution1 { + public int[][] mergeArrays(int[][] nums1, int[][] nums2) { + List mergedList = new ArrayList<>(); + int i1 = 0; + int i2 = 0; + for (; i1 < nums1.length && i2 < nums2.length; ) { + int id1 = nums1[i1][0]; + int id2 = nums2[i2][0]; + if (id2 == id1) { + mergedList.add(new int[] {id1, nums1[i1][1] + nums2[i2][1]}); + i1++; + i2++; + } else if (id1 < id2) { + mergedList.add(new int[] {id1, nums1[i1][1]}); + i1++; + } else { + mergedList.add(new int[] {id2, nums2[i2][1]}); + i2++; + } + } + while (i1 < nums1.length) { + mergedList.add(new int[] {nums1[i1][0], nums1[i1][1]}); + i1++; + } + while (i2 < nums2.length) { + mergedList.add(new int[] {nums2[i2][0], nums2[i2][1]}); + i2++; + } + int[][] ans = new int[mergedList.size()][2]; + for (int i = 0; i < mergedList.size(); i++) { + ans[i][0] = mergedList.get(i)[0]; + ans[i][1] = mergedList.get(i)[1]; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2574.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2574.java new file mode 100644 index 0000000000..3a59360f61 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2574.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2574 { + public static class Solution1 { + public int[] leftRightDifference(int[] nums) { + int[] leftSums = new int[nums.length]; + int leftSum = nums[0]; + for (int i = 1; i < nums.length; i++) { + leftSums[i] = leftSum; + leftSum += nums[i]; + } + int[] rightSums = new int[nums.length]; + int rightSum = nums[nums.length - 1]; + for (int i = nums.length - 2; i >= 0; i--) { + rightSums[i] = rightSum; + rightSum += nums[i]; + } + int[] ans = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + ans[i] = Math.abs(leftSums[i] - rightSums[i]); + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2578.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2578.java new file mode 100644 index 0000000000..83988baff2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2578.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class _2578 { + public static class Solution1 { + public int splitNum(int num) { + List digits = new ArrayList<>(); + while (num != 0) { + digits.add(num % 10); + num /= 10; + } + Collections.sort(digits); + StringBuilder nums1 = new StringBuilder(); + StringBuilder nums2 = new StringBuilder(); + for (int i = 0; i < digits.size(); i++) { + if (nums1.length() < nums2.length()) { + nums1.append(digits.get(i)); + } else { + nums2.append(digits.get(i)); + } + } + return Integer.parseInt(nums1.toString()) + Integer.parseInt(nums2.toString()); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2582.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2582.java new file mode 100644 index 0000000000..80f70d130d --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2582.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2582 { + public static class Solution1 { + public int passThePillow(int n, int time) { + int person = 1; + boolean goLeft = true; + while (time-- > 0) { + if (goLeft) { + person++; + } else { + person--; + } + if (time == 0) { + return person; + } + if (person == n || person == 1) { + goLeft = !goLeft; + } + } + return person; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2583.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2583.java new file mode 100644 index 0000000000..2416814e6c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2583.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.*; + +public class _2583 { + public static class Solution1 { + public long kthLargestLevelSum(TreeNode root, int k) { + List list = new ArrayList<>(); + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + long thisSum = 0L; + for (int i = 0; i < size; i++) { + TreeNode curr = queue.poll(); + thisSum += curr.val; + if (curr.left != null) { + queue.offer(curr.left); + } + if (curr.right != null) { + queue.offer(curr.right); + } + } + list.add(thisSum); + } + Collections.sort(list, Collections.reverseOrder()); + return k > list.size() ? -1 : list.get(k - 1); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2586.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2586.java new file mode 100644 index 0000000000..444a58dc37 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2586.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class _2586 { + public static class Solution1 { + public int vowelStrings(String[] words, int left, int right) { + int count = 0; + for (int i = left; i <= right; i++) { + if (isVowelString(words[i])) { + count++; + } + } + return count; + } + + private boolean isVowelString(String word) { + Set set = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u')); + if (set.contains(word.charAt(0)) && set.contains(word.charAt(word.length() - 1))) { + return true; + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2591.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2591.java new file mode 100644 index 0000000000..8452ce9746 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2591.java @@ -0,0 +1,39 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.PriorityQueue; + +public class _2591 { + public static class Solution1 { + public int distMoney(int money, int children) { + if (money / children == 8 && money % children == 0) { + return children; + } + if (money < children) { + return -1; + } + PriorityQueue minHeap = new PriorityQueue<>(); + for (int i = 0; i < children; i++) { + minHeap.offer(1); + money--; + } + int maxEights = 0; + while (!minHeap.isEmpty() && money > 0) { + Integer curr = minHeap.poll(); + if (money < 7) { + curr += money; + minHeap.offer(curr); + break; + } else if (minHeap.size() > 0) { + money -= 7; + maxEights++; + } else if (minHeap.size() == 0) { + break; + } + } + if (!minHeap.isEmpty() && minHeap.peek() == 4) { + maxEights--; + } + return maxEights; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2595.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2595.java new file mode 100644 index 0000000000..a45d9543c0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2595.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2595 { + public static class Solution1 { + public int[] evenOddBit(int n) { + String str = Integer.toBinaryString(n); + String reverse = new StringBuilder(str).reverse().toString(); + int even = 0; + int odd = 0; + for (int i = 0; i < str.length(); i++) { + if (i % 2 == 0) { + if (reverse.charAt(i) == '1') { + even++; + } + } else { + if (reverse.charAt(i) == '1') { + odd++; + } + } + } + return new int[] {even, odd}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2596.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2596.java new file mode 100644 index 0000000000..2a3982aa96 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2596.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2596 { + public static class Solution1 { + public boolean checkValidGrid(int[][] grid) { + int n = grid.length; + int[][] offsets = + new int[][] { + {-2, 1}, + {-1, 2}, + {1, 2}, + {2, 1}, + {2, -1}, + {1, -2}, + {-1, -2}, + {-2, -1} + }; + int x = 0; + int y = 0; + int currentVal = 0; + while (currentVal != n * n - 1) { + boolean foundNext = false; + for (int[] offset : offsets) { + int newX = x + offset[0]; + int newY = y + offset[1]; + if (newX >= 0 + && newX < n + && newY >= 0 + && newY < n + && grid[newX][newY] == currentVal + 1) { + currentVal++; + x = newX; + y = newY; + foundNext = true; + break; + } + } + if (!foundNext) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2600.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2600.java new file mode 100644 index 0000000000..43fbaead0f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2600.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2600 { + public static class Solution1 { + public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) { + if (k <= numOnes) { + return k; + } else if (k <= (numOnes + numZeros)) { + return numOnes; + } else { + k -= numOnes; + k -= numZeros; + if (k > 0) { + return numOnes - k; + } + return numOnes; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2605.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2605.java new file mode 100644 index 0000000000..982d193f2c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2605.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2605 { + public static class Solution1 { + public int minNumber(int[] nums1, int[] nums2) { + for (int i = 0; i < nums1.length; i++) { + for (int j = 0; j < nums2.length; j++) { + if (nums1[i] == nums2[j]) { + return nums1[i]; + } + } + } + Arrays.sort(nums1); + Arrays.sort(nums2); + if (nums1[0] < nums2[0]) { + return nums1[0] * 10 + nums2[0]; + } else { + return nums2[0] * 10 + nums1[0]; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2609.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2609.java new file mode 100644 index 0000000000..9f15a678d6 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2609.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2609 { + public static class Solution1 { + public int findTheLongestBalancedSubstring(String s) { + int longest = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == '0') { + int zeroes = 0; + while (i < s.length() && s.charAt(i) == '0') { + i++; + zeroes++; + } + if (i < s.length()) { + int ones = 0; + while (i < s.length() && s.charAt(i) == '1') { + i++; + ones++; + } + longest = Math.max(longest, Math.min(ones, zeroes) * 2); + i--; + } + } + } + return longest; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2614.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2614.java new file mode 100644 index 0000000000..cfb6f318ee --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2614.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2614 { + public static class Solution1 { + public int diagonalPrime(int[][] nums) { + int ans = 0; + boolean[] nonPrimes = generatePrimes((int) (Math.pow(10, 6) * 4)); + for (int i = 0; i < nums.length; i++) { + for (int j = 0; j < nums[0].length; j++) { + if (i == j || i == nums.length - j - 1) { + if (!nonPrimes[nums[i][j]]) { + ans = Math.max(ans, nums[i][j]); + } + } + } + } + return ans; + } + + private boolean[] generatePrimes(int n) { + boolean[] nonPrimes = new boolean[n]; + // 1 is not a prime number + nonPrimes[1] = true; + for (int i = 2; i < n; i++) { + if (!nonPrimes[i]) { + for (int j = 2; i * j < n; j++) { + nonPrimes[i * j] = true; + } + } + } + return nonPrimes; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2639.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2639.java new file mode 100644 index 0000000000..db08e530c2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2639.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2639 { + public static class Solution1 { + public int[] findColumnWidth(int[][] grid) { + int[] ans = new int[grid[0].length]; + for (int j = 0; j < grid[0].length; j++) { + int width = 0; + for (int i = 0; i < grid.length; i++) { + width = Math.max(width, String.valueOf(grid[i][j]).length()); + } + ans[j] = width; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2641.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2641.java new file mode 100644 index 0000000000..3934637926 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2641.java @@ -0,0 +1,66 @@ +package com.fishercoder.solutions.thirdthousand; + +import com.fishercoder.common.classes.TreeNode; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; + +public class _2641 { + public static class Solution1 { + /* + * My completely original solution. + * Note: It's not really replacing the values in the original tree nodes, instead, I'm building a new tree with updated values. + */ + public TreeNode replaceValueInTree(TreeNode root) { + Map depthToLevelSumMap = new HashMap<>(); + Queue originalQ = new LinkedList<>(); + originalQ.offer(root); + int depth = 0; + while (!originalQ.isEmpty()) { + int size = originalQ.size(); + int levelSum = 0; + for (int i = 0; i < size; i++) { + TreeNode curr = originalQ.poll(); + levelSum += curr.val; + if (curr.left != null) { + originalQ.offer(curr.left); + } + if (curr.right != null) { + originalQ.offer(curr.right); + } + } + depthToLevelSumMap.put(depth++, levelSum); + } + depth = 0; + TreeNode newRoot = new TreeNode(0); + originalQ.offer(root); + Queue newQ = new LinkedList<>(); + newQ.add(newRoot); + while (!originalQ.isEmpty()) { + int size = originalQ.size(); + for (int i = 0; i < size; i++) { + TreeNode currOriginal = originalQ.poll(); + int childrenSum = currOriginal.left != null ? currOriginal.left.val : 0; + childrenSum += currOriginal.right != null ? currOriginal.right.val : 0; + int remainder = depthToLevelSumMap.getOrDefault(depth + 1, 0) - childrenSum; + TreeNode currNew = newQ.poll(); + if (currOriginal.left != null) { + TreeNode currNewLeftChild = new TreeNode(remainder); + currNew.left = currNewLeftChild; + newQ.offer(currNewLeftChild); + originalQ.offer(currOriginal.left); + } + if (currOriginal.right != null) { + TreeNode currNewRightChild = new TreeNode(remainder); + currNew.right = currNewRightChild; + newQ.offer(currNewRightChild); + originalQ.offer(currOriginal.right); + } + } + depth++; + } + return newRoot; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2643.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2643.java new file mode 100644 index 0000000000..07144abe05 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2643.java @@ -0,0 +1,22 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2643 { + public static class Solution1 { + public int[] rowAndMaximumOnes(int[][] mat) { + int maxOnes = 0; + int[] result = new int[2]; + for (int i = 0; i < mat.length; i++) { + int count = 0; + for (int j = 0; j < mat[0].length; j++) { + count += mat[i][j]; + } + if (count > maxOnes) { + result[0] = i; + result[1] = count; + maxOnes = count; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2644.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2644.java new file mode 100644 index 0000000000..f3d2759349 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2644.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2644 { + public static class Solution1 { + public int maxDivScore(int[] nums, int[] divisors) { + int[][] scores = new int[divisors.length][2]; + for (int i = 0; i < divisors.length; i++) { + int score = 0; + for (int j = 0; j < nums.length; j++) { + if (nums[j] % divisors[i] == 0) { + score++; + } + } + scores[i][0] = score; + scores[i][1] = divisors[i]; + } + int maxScore = -1; + for (int i = 0; i < divisors.length; i++) { + maxScore = Math.max(maxScore, scores[i][0]); + } + int ans = Integer.MAX_VALUE; + for (int i = 0; i < divisors.length; i++) { + if (maxScore == scores[i][0]) { + if (ans > scores[i][1]) { + ans = scores[i][1]; + } + } else if (maxScore < scores[i][0]) { + maxScore = scores[i][0]; + ans = scores[i][1]; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2651.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2651.java new file mode 100644 index 0000000000..8d77b03160 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2651.java @@ -0,0 +1,9 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2651 { + public static class Solution1 { + public int findDelayedArrivalTime(int arrivalTime, int delayedTime) { + return (arrivalTime + delayedTime) % 24; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2652.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2652.java new file mode 100644 index 0000000000..fc18b9f5ad --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2652.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2652 { + public static class Solution1 { + public int sumOfMultiples(int n) { + int sum = 0; + for (int i = 1; i <= n; i++) { + if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0) { + sum += i; + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2656.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2656.java new file mode 100644 index 0000000000..38009912f9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2656.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2656 { + public static class Solution1 { + public int maximizeSum(int[] nums, int k) { + int max = 0; + for (int i = 0; i < nums.length; i++) { + max = Math.max(max, nums[i]); + } + int sum = 0; + while (k-- > 0) { + sum += max; + max++; + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2660.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2660.java new file mode 100644 index 0000000000..3253f29331 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2660.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2660 { + public static class Solution1 { + public int isWinner(int[] player1, int[] player2) { + int sum1 = computeSum(player1); + int sum2 = computeSum(player2); + if (sum1 < sum2) { + return 2; + } else if (sum1 > sum2) { + return 1; + } else { + return 0; + } + } + + private int computeSum(int[] pins) { + int sum = 0; + for (int i = 0; i < pins.length; i++) { + if (i > 0 && pins[i - 1] == 10 || (i > 1 && pins[i - 2] == 10)) { + sum += pins[i] * 2; + } else { + sum += pins[i]; + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2670.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2670.java new file mode 100644 index 0000000000..a0aa99a134 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2670.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashMap; +import java.util.Map; + +public class _2670 { + public static class Solution1 { + public int[] distinctDifferenceArray(int[] nums) { + Map prefix = new HashMap<>(); + prefix.put(nums[0], 1); + Map suffix = new HashMap<>(); + for (int i = 1; i < nums.length; i++) { + suffix.put(nums[i], suffix.getOrDefault(nums[i], 0) + 1); + } + int[] result = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + result[i] = prefix.size() - suffix.size(); + if (i + 1 < nums.length) { + prefix.put(nums[i + 1], prefix.getOrDefault(nums[i + 1], 0) + 1); + if (suffix.containsKey(nums[i + 1]) && suffix.get(nums[i + 1]) == 1) { + suffix.remove(nums[i + 1]); + } else { + suffix.put(nums[i + 1], suffix.getOrDefault(nums[i + 1], 0) - 1); + } + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2673.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2673.java new file mode 100644 index 0000000000..fac8e68f90 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2673.java @@ -0,0 +1,110 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2673 { + public static class Solution1 { + /* + * My completely original solution, although verbose and could be further optimized. + * Practice makes perfect! + */ + class TreeNodeWithCost { + int val; + int cost; + int costSumFromRootToThisNode; + int maxCostFromThisNodeToAllPossibleLeafNodes; + TreeNodeWithCost left; + TreeNodeWithCost right; + + public TreeNodeWithCost(int val, int cost) { + this.val = val; + this.cost = cost; + this.costSumFromRootToThisNode = cost; + } + } + + int maxCostFromRootToLeaf = 0; + int minIncs = 0; + + public int minIncrements(int n, int[] cost) { + TreeNodeWithCost root = new TreeNodeWithCost(1, cost[0]); + preOrderBuildTree(root, n, cost, 1); + inOrderFindMaxCostPath(root); + // in order to do the minimum increments, we want to increment as many times as possible + // on the nodes as close to the root as possible + // but to how many? + // then we need to know the maximum cost of all paths from each node to all of its + // possible leaf nodes + // the difference is the number of increments we can do on this node + postOrderFindMaxCostForEachNode(root); + preOrderToIncrementCost(root); + return minIncs; + } + + private void preOrderToIncrementCost(TreeNodeWithCost root) { + if (root == null) { + return; + } + int incsNeeded = maxCostFromRootToLeaf - root.maxCostFromThisNodeToAllPossibleLeafNodes; + minIncs += incsNeeded; + if (incsNeeded > 0) { + root.costSumFromRootToThisNode += incsNeeded; + preOrderToUpdateCostSums(root, incsNeeded); + } + preOrderToIncrementCost(root.left); + preOrderToIncrementCost(root.right); + } + + private void preOrderToUpdateCostSums(TreeNodeWithCost root, int incsNeeded) { + if (root == null) { + return; + } + root.costSumFromRootToThisNode += incsNeeded; + root.maxCostFromThisNodeToAllPossibleLeafNodes += incsNeeded; + preOrderToUpdateCostSums(root.left, incsNeeded); + preOrderToUpdateCostSums(root.right, incsNeeded); + } + + private int postOrderFindMaxCostForEachNode(TreeNodeWithCost node) { + if (node == null) { + return 0; + } + int leftMaxCost = postOrderFindMaxCostForEachNode(node.left); + int rightMaxCost = postOrderFindMaxCostForEachNode(node.right); + if (leftMaxCost == 0 && rightMaxCost == 0) { + // this means this node is a leaf node + node.maxCostFromThisNodeToAllPossibleLeafNodes = node.costSumFromRootToThisNode; + } else { + // if it's not leaf node, then we take the bigger one from left and right + node.maxCostFromThisNodeToAllPossibleLeafNodes = + Math.max(leftMaxCost, rightMaxCost); + } + return node.maxCostFromThisNodeToAllPossibleLeafNodes; + } + + private void inOrderFindMaxCostPath(TreeNodeWithCost root) { + if (root == null) { + return; + } + inOrderFindMaxCostPath(root.left); + if (root.left == null && root.right == null) { + maxCostFromRootToLeaf = + Math.max(maxCostFromRootToLeaf, root.costSumFromRootToThisNode); + } + inOrderFindMaxCostPath(root.right); + } + + private int preOrderBuildTree(TreeNodeWithCost root, int n, int[] cost, int base) { + if (root == null || base * 2 >= n) { + return 0; + } + + root.left = new TreeNodeWithCost(base * 2, cost[base * 2 - 1]); + root.left.costSumFromRootToThisNode = root.left.cost + root.costSumFromRootToThisNode; + root.right = new TreeNodeWithCost(base * 2 + 1, cost[base * 2]); + root.right.costSumFromRootToThisNode = root.right.cost + root.costSumFromRootToThisNode; + + preOrderBuildTree(root.left, n, cost, base * 2); + preOrderBuildTree(root.right, n, cost, base * 2 + 1); + return root.costSumFromRootToThisNode; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2678.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2678.java new file mode 100644 index 0000000000..b29bc0df70 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2678.java @@ -0,0 +1,15 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2678 { + public static class Solution1 { + public int countSeniors(String[] details) { + int seniors = 0; + for (String detail : details) { + if (Integer.parseInt(detail.substring(11, 13)) > 60) { + seniors++; + } + } + return seniors; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2682.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2682.java new file mode 100644 index 0000000000..2834fd1968 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2682.java @@ -0,0 +1,35 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2682 { + public static class Solution1 { + public int[] circularGameLosers(int n, int k) { + if (n == 1) { + return new int[0]; + } + Set met = new HashSet<>(); + int i = 1; + int ball = 1; + while (met.add(ball)) { + ball += (i * k) % n; + if (ball > n) { + ball %= n; + } + i++; + } + if (n == met.size()) { + return new int[0]; + } + int[] ans = new int[n - met.size()]; + int q = 0; + for (int j = 1; j <= n; j++) { + if (!met.contains(j)) { + ans[q++] = j; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2689.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2689.java new file mode 100644 index 0000000000..3e5181a197 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2689.java @@ -0,0 +1,50 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2689 { + // Definition for a rope tree node. + public static class RopeTreeNode { + public int len; + public String val; + public RopeTreeNode left; + public RopeTreeNode right; + + public RopeTreeNode() {} + + public RopeTreeNode(String val) { + this.len = 0; + this.val = val; + } + + public RopeTreeNode(int len) { + this.len = len; + this.val = ""; + } + + public RopeTreeNode(int len, RopeTreeNode left, RopeTreeNode right) { + this.len = len; + this.val = ""; + this.left = left; + this.right = right; + } + } + + public static class Solution1 { + /* + * My completely original solution. + */ + public char getKthCharacter(RopeTreeNode root, int k) { + StringBuilder sb = new StringBuilder(); + postOrderToConcatenate(root, k, sb); + return sb.charAt(k - 1); + } + + private void postOrderToConcatenate(RopeTreeNode root, int k, StringBuilder sb) { + if (sb.length() >= k || root == null) { + return; + } + postOrderToConcatenate(root.left, k, sb); + postOrderToConcatenate(root.right, k, sb); + sb.append(root.val); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2696.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2696.java new file mode 100644 index 0000000000..8b836cef38 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2696.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Deque; +import java.util.LinkedList; + +public class _2696 { + public static class Solution1 { + public int minLength(String s) { + Deque stack = new LinkedList<>(); + for (int i = 0; i < s.length(); i++) { + if (stack.isEmpty()) { + stack.addLast(s.charAt(i)); + } else if (s.charAt(i) == 'B' && stack.peekLast() == 'A') { + stack.pollLast(); + } else if (s.charAt(i) == 'D' && stack.peekLast() == 'C') { + stack.pollLast(); + } else { + stack.addLast(s.charAt(i)); + } + } + return stack.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2697.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2697.java new file mode 100644 index 0000000000..3b20520a21 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2697.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2697 { + public static class Solution1 { + public String makeSmallestPalindrome(String s) { + char[] charArray = s.toCharArray(); + for (int i = 0, j = s.length() - 1; i < j; i++, j--) { + if (charArray[i] != charArray[j]) { + if (charArray[i] < charArray[j]) { + charArray[j] = charArray[i]; + } else { + charArray[i] = charArray[j]; + } + } + } + return new String(charArray); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2706.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2706.java new file mode 100644 index 0000000000..f0cfd99a29 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2706.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2706 { + public static class Solution1 { + public int buyChoco(int[] prices, int money) { + Arrays.sort(prices); + if (prices[0] + prices[1] > money) { + return money; + } else { + return money - prices[0] - prices[1]; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2710.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2710.java new file mode 100644 index 0000000000..75ce8b9ba2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2710.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2710 { + public static class Solution1 { + public String removeTrailingZeros(String num) { + StringBuilder sb = new StringBuilder(); + boolean trailing = true; + for (int i = num.length() - 1; i >= 0; i--) { + if (num.charAt(i) != '0' || !trailing) { + sb.append(num.charAt(i)); + trailing = false; + } + } + return sb.reverse().toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2716.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2716.java new file mode 100644 index 0000000000..9e01e984a1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2716.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2716 { + public static class Solution1 { + public int minimizedStringLength(String s) { + StringBuilder sb = new StringBuilder(); + Set set = new HashSet<>(); + for (int i = 0; i < s.length(); i++) { + if (set.add(s.charAt(i))) { + sb.append(s.charAt(i)); + } + } + return sb.length(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2717.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2717.java new file mode 100644 index 0000000000..ec05a258a8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2717.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2717 { + public static class Solution1 { + public int semiOrderedPermutation(int[] nums) { + int[] max = new int[] {nums[0], 0}; + int[] min = new int[] {nums[0], 0}; + for (int i = 1; i < nums.length; i++) { + if (nums[i] > max[0]) { + max[0] = nums[i]; + max[1] = i; + } + if (nums[i] < min[0]) { + min[0] = nums[i]; + min[1] = i; + } + } + if (max[1] > min[1]) { + return nums.length - max[1] - 1 + min[1]; + } + return nums.length - max[1] - 1 + min[1] - 1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2728.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2728.java new file mode 100644 index 0000000000..6de8772523 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2728.java @@ -0,0 +1,41 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2728 { + public static class Street { + // dummy class to make compilation possible + public Street(int[] doors) {} + + public void openDoor() {} + + public void closeDoor() {} + + public boolean isDoorOpen() { + return false; + } + + public void moveRight() {} + + public void moveLeft() {} + } + + public static class Solution1 { + public int houseCount(Street street, int k) { + // close all doors + for (int i = 0; i < k; i++) { + street.closeDoor(); + street.moveRight(); + } + // open one door + street.openDoor(); + int houses = 1; + for (int i = 0; i < k; i++) { + street.moveRight(); + if (street.isDoorOpen()) { + return houses; + } + houses++; + } + return houses; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2729.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2729.java new file mode 100644 index 0000000000..7f02f796f4 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2729.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2729 { + public static class Solution1 { + public boolean isFascinating(int n) { + StringBuilder sb = new StringBuilder(); + sb.append(n); + sb.append(n * 2); + sb.append(n * 3); + String num = sb.toString(); + Set set = new HashSet<>(); + for (int i = 0; i < num.length(); i++) { + if (num.charAt(i) == '0' || !set.add(Integer.parseInt(num.charAt(i) + ""))) { + return false; + } + } + return set.size() == 9; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2733.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2733.java new file mode 100644 index 0000000000..fa998b9c25 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2733.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2733 { + public static class Solution1 { + public int findNonMinOrMax(int[] nums) { + int max = 0; + int min = Integer.MAX_VALUE; + for (int num : nums) { + max = Math.max(max, num); + min = Math.min(min, num); + } + for (int num : nums) { + if (num != max && num != min) { + return num; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2739.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2739.java new file mode 100644 index 0000000000..1c48919430 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2739.java @@ -0,0 +1,21 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2739 { + public static class Solution1 { + public int distanceTraveled(int mainTank, int additionalTank) { + int distance = 0; + while (mainTank >= 5) { + distance += 5 * 10; + mainTank -= 5; + if (additionalTank > 0) { + mainTank++; + additionalTank--; + } + } + if (mainTank > 0) { + distance += 10 * mainTank; + } + return distance; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2744.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2744.java new file mode 100644 index 0000000000..9a30d596db --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2744.java @@ -0,0 +1,33 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2744 { + public static class Solution1 { + public int maximumNumberOfStringPairs(String[] words) { + int pairs = 0; + for (int i = 0; i < words.length - 1; i++) { + for (int j = i + 1; j < words.length; j++) { + if (couldPair(words[i], words[j])) { + pairs++; + } + } + } + return pairs; + } + + private boolean couldPair(String word1, String word2) { + if (word1.length() != word2.length()) { + return false; + } + int left = 0; + int right = word1.length() - 1; + while (left < right) { + if (word1.charAt(left) != word2.charAt(right)) { + return false; + } + left++; + right--; + } + return word1.charAt(left) == word2.charAt(right); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2748.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2748.java new file mode 100644 index 0000000000..e40c416d45 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2748.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2748 { + public static class Solution1 { + public int countBeautifulPairs(int[] nums) { + int pairs = 0; + for (int i = 0; i < nums.length - 1; i++) { + for (int j = i + 1; j < nums.length; j++) { + String iStr = String.valueOf(nums[i]); + String jStr = String.valueOf(nums[j]); + if (gcd( + Integer.parseInt(iStr.charAt(0) + ""), + Integer.parseInt(jStr.charAt(jStr.length() - 1) + "")) + == 1) { + pairs++; + } + } + } + return pairs; + } + + private int gcd(int a, int b) { + if (b == 0) { + return a; + } + return gcd(b, a % b); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2751.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2751.java new file mode 100644 index 0000000000..c80cd72a60 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2751.java @@ -0,0 +1,91 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +public class _2751 { + public static class Solution1 { + /* + * My completely original solution. + */ + public List survivedRobotsHealths( + int[] positions, int[] healths, String directions) { + List list = new ArrayList<>(); + for (int i = 0; i < positions.length; i++) { + list.add(new Robot(positions[i], healths[i], directions.charAt(i), i)); + } + Collections.sort(list, (a, b) -> a.position - b.position); + Deque stack = new LinkedList<>(); + for (int i = 0; i < list.size(); i++) { + Robot curr = list.get(i); + if (stack.isEmpty() || curr.direction == 'R') { + stack.addLast(curr); + } else if (curr.direction == 'L') { + if (stack.peekLast().direction == 'R') { + Robot last = stack.pollLast(); + if (last.health == curr.health) { + curr.health = 0; + } else if (last.health < curr.health) { + curr.health--; + if (!stack.isEmpty() && stack.peekLast().direction == 'R') { + while (!stack.isEmpty() && stack.peekLast().direction == 'R') { + Robot poll = stack.pollLast(); + if (poll.health < curr.health) { + curr.health--; + } else if (poll.health == curr.health) { + curr.health = 0; + break; + } else { + poll.health--; + stack.addLast(poll); + break; + } + } + if (stack.isEmpty() + || stack.peekLast().direction == 'L' && curr.health > 0) { + stack.addLast(curr); + } + } else { + stack.addLast(curr); + } + } else { + last.health--; + stack.addLast(last); + } + } else { + stack.addLast(curr); + } + } + } + List result = new ArrayList<>(); + while (!stack.isEmpty()) { + result.add(stack.pollLast()); + } + Collections.sort(result, (a, b) -> a.originalPosition - b.originalPosition); + List finalResult = new ArrayList<>(); + for (int i = 0; i < result.size(); i++) { + if (result.get(i).health > 0) { + finalResult.add(result.get(i).health); + } + } + return finalResult; + } + + class Robot { + int position; + int health; + char direction; + int originalPosition; + + public Robot(int position, int health, char direction, int originalPosition) { + this.position = position; + this.health = health; + this.direction = direction; + this.originalPosition = originalPosition; + } + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2760.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2760.java new file mode 100644 index 0000000000..7826f3fa41 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2760.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2760 { + public static class Solution1 { + public int longestAlternatingSubarray(int[] nums, int threshold) { + int longest = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 2 == 0 && nums[i] <= threshold) { + int start = i; + int j = i; + for (; j < nums.length - 1; j++) { + if (nums[j] % 2 != nums[j + 1] % 2 + && nums[j] <= threshold + && nums[j + 1] <= threshold) { + continue; + } else { + break; + } + } + longest = Math.max(longest, j - start + 1); + } + } + return longest; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2765.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2765.java new file mode 100644 index 0000000000..362c9757fb --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2765.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2765 { + public static class Solution1 { + public int alternatingSubarray(int[] nums) { + int len = nums.length; + int maxLen = -1; + for (int i = 0; i < len; i++) { + for (int j = i + 1; j < len; j++) { + if (j - i + 1 > maxLen && alternating(nums, i, j)) { + maxLen = j - i + 1; + } + } + } + return maxLen; + } + + private boolean alternating(int[] nums, int start, int finish) { + int expected = 1; + for (int i = start, j = start + 1; i < finish && j <= finish; i++, j++) { + if (nums[j] - nums[i] == expected) { + expected = -expected; + } else { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2769.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2769.java new file mode 100644 index 0000000000..dfd2e0a7bc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2769.java @@ -0,0 +1,9 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2769 { + public static class Solution1 { + public int theMaximumAchievableX(int num, int t) { + return num + t * 2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2778.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2778.java new file mode 100644 index 0000000000..1c322ff02a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2778.java @@ -0,0 +1,16 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2778 { + public static class Solution1 { + public int sumOfSquares(int[] nums) { + int sum = 0; + int len = nums.length; + for (int i = 0; i < len; i++) { + if (len % (i + 1) == 0) { + sum += nums[i] * nums[i]; + } + } + return sum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2784.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2784.java new file mode 100644 index 0000000000..55c5d47aad --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2784.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2784 { + public static class Solution1 { + public boolean isGood(int[] nums) { + int max = -1; + for (int i = 0; i < nums.length; i++) { + max = Math.max(max, nums[i]); + } + if (nums.length != max + 1) { + return false; + } + Set set = new HashSet<>(); + for (int num : nums) { + if (!set.add(num)) { + if (num != max) { + return false; + } + } + } + return set.size() == max; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2788.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2788.java new file mode 100644 index 0000000000..847effc4d2 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2788.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2788 { + public static class Solution1 { + public List splitWordsBySeparator(List words, char separator) { + List ans = new ArrayList<>(); + StringBuilder sb = new StringBuilder(); + for (String word : words) { + sb.setLength(0); + for (int i = 0; i < word.length(); i++) { + if (word.charAt(i) == separator) { + if (sb.length() != 0) { + ans.add(sb.toString()); + sb.setLength(0); + } + } else { + sb.append(word.charAt(i)); + } + } + if (sb.length() != 0) { + ans.add(sb.toString()); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2798.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2798.java new file mode 100644 index 0000000000..3a2ac3697f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2798.java @@ -0,0 +1,11 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2798 { + public static class Solution1 { + public int numberOfEmployeesWhoMetTarget(int[] hours, int target) { + return (int) Arrays.stream(hours).filter(hour -> hour >= target).count(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2806.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2806.java new file mode 100644 index 0000000000..e77f3b8625 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2806.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2806 { + public static class Solution1 { + public int accountBalanceAfterPurchase(int purchaseAmount) { + if (purchaseAmount == 100) { + return 0; + } + int balance = 100; + while (purchaseAmount >= 10) { + balance -= 10; + purchaseAmount -= 10; + } + if (purchaseAmount >= 5) { + balance -= 10; + } + return balance; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2810.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2810.java new file mode 100644 index 0000000000..4068b23ab9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2810.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2810 { + public static class Solution1 { + public String finalString(String s) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == 'i') { + sb.reverse(); + } else { + sb.append(s.charAt(i)); + } + } + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2812.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2812.java new file mode 100644 index 0000000000..a2e750b3ee --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2812.java @@ -0,0 +1,126 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class _2812 { + public static class Solution1 { + /* + * A great problem, credit: https://leetcode.com/problems/find-the-safest-path-in-a-grid/editorial/ + *

+ * BFS twice: + * 1. once: to build the safeness factor for each cell; + * 2. second time: check if there's a valid path from that cell; + */ + final int[] dirs = new int[] {0, 1, 0, -1, 0}; + + public int maximumSafenessFactor(List> grid) { + int n = grid.size(); + int[][] mat = new int[n][n]; + Queue multiSourceQueue = new LinkedList<>(); + + // To make modifications and navigation easier, the grid is converted into a 2-d array. + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (grid.get(i).get(j) == 1) { + // Push thief coordinates to the queue + multiSourceQueue.add(new int[] {i, j}); + // Mark thief cell with 0 + mat[i][j] = 0; + } else { + // Mark empty cell with -1 + mat[i][j] = -1; + } + } + } + + // Calculate safeness factor for each cell using BFS + while (!multiSourceQueue.isEmpty()) { + int size = multiSourceQueue.size(); + while (size-- > 0) { + int[] curr = multiSourceQueue.poll(); + // Check neighboring cells + for (int k = 0; k < dirs.length - 1; k++) { + int di = curr[0] + dirs[k]; + int dj = curr[1] + dirs[k + 1]; + int val = mat[curr[0]][curr[1]]; + // Check if the neighboring cell is valid and unvisited + if (isValidCell(mat, di, dj) && mat[di][dj] == -1) { + // Update safeness factor and push to the queue + mat[di][dj] = val + 1; + multiSourceQueue.add(new int[] {di, dj}); + } + } + } + } + + // Binary search for maximum safeness factor + int start = 0; + int end = 0; + int res = -1; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + // Set end as the maximum safeness factor possible + end = Math.max(end, mat[i][j]); + } + } + + while (start <= end) { + int mid = start + (end - start) / 2; + if (isValidSafeness(mat, mid)) { + // Store valid safeness and search for larger ones + res = mid; + start = mid + 1; + } else { + end = mid - 1; + } + } + return res; + } + + // Check if a path exists with given minimum safeness value + private boolean isValidSafeness(int[][] grid, int minSafeness) { + int n = grid.length; + + // Check if the source and destination cells satisfy minimum safeness + if (grid[0][0] < minSafeness || grid[n - 1][n - 1] < minSafeness) { + return false; + } + + Queue traversalQueue = new LinkedList<>(); + traversalQueue.add(new int[] {0, 0}); + boolean[][] visited = new boolean[n][n]; + visited[0][0] = true; + + // BFS to find a valid path + while (!traversalQueue.isEmpty()) { + int[] curr = traversalQueue.poll(); + if (curr[0] == n - 1 && curr[1] == n - 1) { + return true; // Valid path found + } + // Check neighboring cells + for (int k = 0; k < dirs.length - 1; k++) { + int di = curr[0] + dirs[k]; + int dj = curr[1] + dirs[k + 1]; + // Check if the neighboring cell is valid, unvisited and satisfying minimum + // safeness + if (isValidCell(grid, di, dj) + && !visited[di][dj] + && grid[di][dj] >= minSafeness) { + visited[di][dj] = true; + traversalQueue.add(new int[] {di, dj}); + } + } + } + + return false; // No valid path found + } + + // Check if a given cell lies within the grid + private boolean isValidCell(int[][] mat, int i, int j) { + int n = mat.length; + return i >= 0 && j >= 0 && i < n && j < n; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2815.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2815.java new file mode 100644 index 0000000000..17d9873dcf --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2815.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class _2815 { + public static class Solution1 { + public int maxSum(int[] nums) { + Map> map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + int biggestDigit = getBiggestDigit(nums[i]); + List list = map.getOrDefault(biggestDigit, new ArrayList<>()); + list.add(nums[i]); + map.put(biggestDigit, list); + } + int maxSum = -1; + for (Map.Entry> entry : map.entrySet()) { + List list = entry.getValue(); + if (list.size() > 1) { + Collections.sort(list); + maxSum = + Math.max(maxSum, list.get(list.size() - 1) + list.get(list.size() - 2)); + } + } + return maxSum; + } + + private int getBiggestDigit(int num) { + int biggestDigit = 0; + while (num != 0) { + biggestDigit = Math.max(biggestDigit, num % 10); + num /= 10; + } + return biggestDigit; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2824.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2824.java new file mode 100644 index 0000000000..11b525bc75 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2824.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.List; + +public class _2824 { + public static class Solution1 { + public int countPairs(List nums, int target) { + int pairs = 0; + for (int i = 0; i < nums.size() - 1; i++) { + for (int j = i + 1; j < nums.size(); j++) { + if (nums.get(i) + nums.get(j) < target) { + pairs++; + } + } + } + return pairs; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2828.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2828.java new file mode 100644 index 0000000000..03bc4ccfbd --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2828.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.List; + +public class _2828 { + public static class Solution1 { + public boolean isAcronym(List words, String s) { + if (words.size() != s.length()) { + return false; + } + int i = 0; + for (String word : words) { + if (word.charAt(0) != s.charAt(i++)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2833.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2833.java new file mode 100644 index 0000000000..fddad18168 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2833.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2833 { + public static class Solution1 { + public int furthestDistanceFromOrigin(String moves) { + int count = 0; + int any = 0; + for (char c : moves.toCharArray()) { + if (c == 'L') { + count++; + } else if (c == 'R') { + count--; + } else { + any++; + } + } + return Math.abs(count) + any; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2839.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2839.java new file mode 100644 index 0000000000..7a8619f575 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2839.java @@ -0,0 +1,54 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2839 { + public static class Solution1 { + /* + * Only a total of 6 possibilities, try them all. + */ + public boolean canBeEqual(String s1, String s2) { + if (s1.equals(s2)) { + return true; + } + char[] c1 = s1.toCharArray(); + char[] c2 = s2.toCharArray(); + for (int i = 0; i < 2; i++) { + swap(c1, i); + if (Arrays.equals(c1, c2)) { + return true; + } + } + c1 = s1.toCharArray(); + c2 = s2.toCharArray(); + // swap only (1,3) for c1 now + swap(c1, 1); + if (Arrays.equals(c1, c2)) { + return true; + } + + c1 = s1.toCharArray(); + c2 = s2.toCharArray(); + for (int i = 0; i < 2; i++) { + swap(c2, i); + if (Arrays.equals(c1, c2)) { + return true; + } + } + c1 = s1.toCharArray(); + c2 = s2.toCharArray(); + // swap only (1,3) for c2 now + swap(c2, 1); + if (Arrays.equals(c1, c2)) { + return true; + } + return false; + } + + private void swap(char[] c, int i) { + char tmp = c[i]; + c[i] = c[i + 2]; + c[i + 2] = tmp; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2843.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2843.java new file mode 100644 index 0000000000..399aafa2bc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2843.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2843 { + public static class Solution1 { + public int countSymmetricIntegers(int low, int high) { + int ans = 0; + for (int num = low; num <= high; num++) { + ans += isSymmetric(num); + } + return ans; + } + + private int isSymmetric(int num) { + String numStr = String.valueOf(num); + if (numStr.length() % 2 != 0) { + return 0; + } + int sum1 = 0; + int sum2 = 0; + for (int i = 0; i < numStr.length() / 2; i++) { + sum1 += Integer.parseInt(numStr.charAt(i) + ""); + sum2 += Integer.parseInt(numStr.charAt(numStr.length() - i - 1) + ""); + } + if (sum1 == sum2) { + return 1; + } + return 0; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2848.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2848.java new file mode 100644 index 0000000000..fe34de5e10 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2848.java @@ -0,0 +1,19 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _2848 { + public static class Solution1 { + public int numberOfPoints(List> nums) { + Set set = new HashSet<>(); + for (List num : nums) { + for (int i = num.get(0); i <= num.get(1); i++) { + set.add(i); + } + } + return set.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2855.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2855.java new file mode 100644 index 0000000000..4b3faa5927 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2855.java @@ -0,0 +1,38 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2855 { + public static class Solution1 { + public int minimumRightShifts(List nums) { + int shifts = 0; + do { + if (sorted(nums)) { + return shifts; + } + nums = shiftByOne(nums); + shifts++; + } while (shifts < nums.size()); + return -1; + } + + private List shiftByOne(List list) { + List shifted = new ArrayList<>(); + shifted.add(list.get(list.size() - 1)); + for (int i = 0; i < list.size() - 1; i++) { + shifted.add(list.get(i)); + } + return shifted; + } + + private boolean sorted(List nums) { + for (int i = 0; i < nums.size() - 1; i++) { + if (nums.get(i) >= nums.get(i + 1)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2859.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2859.java new file mode 100644 index 0000000000..c76ada9a14 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2859.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.List; + +public class _2859 { + public static class Solution1 { + public int sumIndicesWithKSetBits(List nums, int k) { + int sum = 0; + for (int i = 0; i < nums.size(); i++) { + if (setbit(i, k)) { + sum += nums.get(i); + } + } + return sum; + } + + private boolean setbit(int num, int k) { + String bin = Integer.toBinaryString(num); + int ones = 0; + for (char c : bin.toCharArray()) { + if (c == '1') { + ones++; + } + } + return ones == k; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2864.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2864.java new file mode 100644 index 0000000000..599834ef98 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2864.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2864 { + public static class Solution1 { + public String maximumOddBinaryNumber(String s) { + int ones = 0; + for (char c : s.toCharArray()) { + if (c == '1') { + ones++; + } + } + int zeroes = s.length() - ones; + StringBuilder sb = new StringBuilder(); + while (ones-- > 1) { + sb.append(1); + } + while (zeroes-- > 0) { + sb.append(0); + } + sb.append(1); + return sb.toString(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2869.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2869.java new file mode 100644 index 0000000000..62cb71802e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2869.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _2869 { + public static class Solution1 { + public int minOperations(List nums, int k) { + Set set = new HashSet<>(); + for (int i = 1; i <= k; i++) { + set.add(i); + } + for (int i = nums.size() - 1; i >= 0; i--) { + set.remove(nums.get(i)); + if (set.size() == 0) { + return nums.size() - i; + } + } + return -1; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2873.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2873.java new file mode 100644 index 0000000000..5547a9d87f --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2873.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2873 { + public static class Solution1 { + public long maximumTripletValue(int[] nums) { + long ans = Long.MIN_VALUE; + for (int i = 0; i < nums.length - 2; i++) { + for (int j = i + 1; j < nums.length - 1; j++) { + for (int k = j + 1; k < nums.length; k++) { + ans = Math.max(ans, (long) (nums[i] - nums[j]) * nums[k]); + } + } + } + if (ans < 0) { + return 0; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2894.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2894.java new file mode 100644 index 0000000000..0dbf8ccf0c --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2894.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2894 { + public static class Solution1 { + public int differenceOfSums(int n, int m) { + int sum1 = 0; + int sum2 = 0; + for (int num = 1; num <= n; num++) { + if (num % m != 0) { + sum1 += num; + } else { + sum2 += num; + } + } + return sum1 - sum2; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2899.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2899.java new file mode 100644 index 0000000000..f2c57732a1 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2899.java @@ -0,0 +1,28 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2899 { + public static class Solution1 { + public List lastVisitedIntegers(int[] nums) { + List ans = new ArrayList<>(); + List seen = new ArrayList<>(); + int k = 1; + for (int num : nums) { + if (num != -1) { + seen.add(num); + k = 1; + } else { + if (k <= seen.size()) { + ans.add(seen.get(seen.size() - k)); + } else { + ans.add(-1); + } + k++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2900.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2900.java new file mode 100644 index 0000000000..a16eb75eef --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2900.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2900 { + public static class Solution1 { + public List getLongestSubsequence(String[] words, int[] groups) { + int longest = 0; + List ans = new ArrayList<>(); + for (int i = 0; i < words.length; i++) { + List candidate = new ArrayList<>(); + candidate.add(words[i]); + int lastBit = groups[i]; + for (int j = i + 1; j < words.length; j++) { + if (groups[j] != lastBit) { + candidate.add(words[j]); + lastBit = groups[j]; + } + } + if (candidate.size() > longest) { + longest = candidate.size(); + ans = candidate; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2903.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2903.java new file mode 100644 index 0000000000..a88abc1eef --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2903.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2903 { + public static class Solution1 { + public int[] findIndices(int[] nums, int indexDifference, int valueDifference) { + for (int i = 0; i < nums.length - indexDifference; i++) { + for (int j = i + indexDifference; j < nums.length; j++) { + if (j - i >= indexDifference + && Math.abs(nums[i] - nums[j]) >= valueDifference) { + return new int[] {i, j}; + } + } + } + return new int[] {-1, -1}; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2908.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2908.java new file mode 100644 index 0000000000..b7227449d7 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2908.java @@ -0,0 +1,25 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2908 { + public static class Solution1 { + public int minimumSum(int[] nums) { + int minSum = Integer.MAX_VALUE; + for (int i = 0; i < nums.length - 2; i++) { + for (int j = i + 1; j < nums.length - 1; j++) { + if (nums[i] < nums[j]) { + int sum = nums[i] + nums[j]; + for (int k = j + 1; k < nums.length; k++) { + if (nums[k] < nums[j]) { + sum += nums[k]; + minSum = Math.min(minSum, sum); + sum -= nums[k]; + } + } + } + } + } + + return minSum == Integer.MAX_VALUE ? -1 : minSum; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2913.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2913.java new file mode 100644 index 0000000000..e467438b20 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2913.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class _2913 { + public static class Solution1 { + public int sumCounts(List nums) { + int ans = 0; + int subArraySize = 1; + while (subArraySize <= nums.size()) { + for (int i = 0; i < nums.size(); i++) { + Set set = new HashSet<>(); + set.add(nums.get(i)); + int j = i + 1; + for (; j < Math.min(i + subArraySize, nums.size()); j++) { + set.add(nums.get(j)); + } + if (j - i == subArraySize) { + ans += set.size() * set.size(); + } + } + subArraySize++; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2917.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2917.java new file mode 100644 index 0000000000..7b87cf81df --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2917.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2917 { + public static class Solution1 { + public int findKOr(int[] nums, int k) { + String[] strings = new String[nums.length]; + for (int i = 0; i < nums.length; i++) { + strings[i] = Integer.toBinaryString(nums[i]); + } + int ans = 0; + int base = 1; + for (int i = 1; i < 64; i++) { + int setBits = 0; + for (String str : strings) { + if (str.length() >= i) { + setBits += Integer.parseInt(str.charAt(str.length() - i) + ""); + } + } + if (setBits >= k) { + ans += base; + } + base *= 2; + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2923.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2923.java new file mode 100644 index 0000000000..4c37204317 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2923.java @@ -0,0 +1,24 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2923 { + public static class Solution1 { + public int findChampion(int[][] grid) { + int n = grid.length; + Set beat = new HashSet<>(); + int champion = -1; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i != j && grid[i][j] == 1) { + if (beat.add(j)) { + champion = i; + } + } + } + } + return champion; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2928.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2928.java new file mode 100644 index 0000000000..2a2b794b53 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2928.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2928 { + public static class Solution1 { + public int distributeCandies(int n, int limit) { + List> candidates = new ArrayList<>(); + for (int maxCandyForOneChild = limit; maxCandyForOneChild >= 0; maxCandyForOneChild--) { + int remainingCandies = n - maxCandyForOneChild; + for (int i = 0; i <= remainingCandies; i++) { + List candidate = new ArrayList(); + candidate.add(maxCandyForOneChild); + if (remainingCandies - i <= limit && i <= limit) { + candidate.add(i); + candidate.add(remainingCandies - i); + } + if (candidate.size() == 3) { + candidates.add(candidate); + } + } + } + return candidates.size(); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2932.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2932.java new file mode 100644 index 0000000000..1d8847641e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2932.java @@ -0,0 +1,17 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2932 { + public static class Solution1 { + public int maximumStrongPairXor(int[] nums) { + int ans = 0; + for (int i = 0; i < nums.length; i++) { + for (int j = i; j < nums.length; j++) { + if (Math.abs(nums[i] - nums[j]) <= Math.min(nums[i], nums[j])) { + ans = Math.max(ans, nums[j] ^ nums[i]); + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2937.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2937.java new file mode 100644 index 0000000000..858761aec0 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2937.java @@ -0,0 +1,30 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2937 { + public static class Solution1 { + public int findMinimumOperations(String s1, String s2, String s3) { + if (s1.charAt(0) != s2.charAt(0) + || s1.charAt(0) != s3.charAt(0) + || s2.charAt(0) != s3.charAt(0)) { + return -1; + } + int minOps = 0; + int minLen = Math.min(s1.length(), Math.min(s2.length(), s3.length())); + int i = 1; + int j = 1; + int k = 1; + for (; i < minLen && j < minLen && k < minLen; i++, j++, k++) { + if (s1.charAt(i) != s2.charAt(j) || s2.charAt(j) != s3.charAt(k)) { + minOps += s1.length() - i; + minOps += s2.length() - j; + minOps += s3.length() - k; + return minOps; + } + } + minOps += s1.length() - i; + minOps += s2.length() - j; + minOps += s3.length() - k; + return minOps; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2942.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2942.java new file mode 100644 index 0000000000..a4b4044e36 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2942.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2942 { + public static class Solution1 { + public List findWordsContaining(String[] words, char x) { + List result = new ArrayList<>(); + for (int i = 0; i < words.length; i++) { + if (words[i].indexOf(x) != -1) { + result.add(i); + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2946.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2946.java new file mode 100644 index 0000000000..17896706cc --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2946.java @@ -0,0 +1,29 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.Arrays; + +public class _2946 { + public static class Solution1 { + public boolean areSimilar(int[][] mat, int k) { + int m = mat.length; + int n = mat[0].length; + k %= n; + if (k == 0) { + return true; + } + int[][] updated = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + // regardless i is even or odd, it's the same formula below! + updated[i][(j + k) % n] = mat[i][j]; + } + } + for (int i = 0; i < m; i++) { + if (!Arrays.equals(mat[i], updated[i])) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2951.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2951.java new file mode 100644 index 0000000000..3067cc753a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2951.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2951 { + public static class Solution1 { + public List findPeaks(int[] mountain) { + List ans = new ArrayList<>(); + for (int i = 1; i < mountain.length - 1; i++) { + if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1]) { + ans.add(i); + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2956.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2956.java new file mode 100644 index 0000000000..614c5cf377 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2956.java @@ -0,0 +1,34 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2956 { + public static class Solution1 { + /* + * Although verbose, this is more efficient and faster than using Java.streams + */ + public int[] findIntersectionValues(int[] nums1, int[] nums2) { + Set set1 = new HashSet<>(); + for (int num : nums1) { + set1.add(num); + } + Set set2 = new HashSet<>(); + for (int num : nums2) { + set2.add(num); + } + int[] ans = new int[2]; + for (int num : nums1) { + if (set2.contains(num)) { + ans[0]++; + } + } + for (int num : nums2) { + if (set1.contains(num)) { + ans[1]++; + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2960.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2960.java new file mode 100644 index 0000000000..b24cffecc9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2960.java @@ -0,0 +1,18 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2960 { + public static class Solution1 { + public int countTestedDevices(int[] batteryPercentages) { + int ans = 0; + for (int i = 0; i < batteryPercentages.length; i++) { + if (batteryPercentages[i] > 0) { + ans++; + for (int j = i + 1; j < batteryPercentages.length; j++) { + batteryPercentages[j] = Math.max(0, batteryPercentages[j] - 1); + } + } + } + return ans; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2965.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2965.java new file mode 100644 index 0000000000..72e32f16ea --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2965.java @@ -0,0 +1,26 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2965 { + public static class Solution1 { + public int[] findMissingAndRepeatedValues(int[][] grid) { + Set set = new HashSet<>(); + int[] result = new int[2]; + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (!set.add(grid[i][j])) { + result[0] = grid[i][j]; + } + } + } + for (int i = 1; i <= grid.length * grid.length; i++) { + if (!set.contains(i)) { + result[1] = i; + } + } + return result; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2970.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2970.java new file mode 100644 index 0000000000..beef2fd859 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2970.java @@ -0,0 +1,36 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.List; + +public class _2970 { + public static class Solution1 { + public int incremovableSubarrayCount(int[] nums) { + int count = 0; + for (int i = 0; i < nums.length; i++) { + for (int j = i; j < nums.length; j++) { + if (strictlyInc(nums, i, j)) { + count++; + } + } + } + return count; + } + + private boolean strictlyInc(int[] nums, int start, int finish) { + List list = new ArrayList<>(); + for (int i = 0; i < start; i++) { + list.add(nums[i]); + } + for (int i = finish + 1; i < nums.length; i++) { + list.add(nums[i]); + } + for (int i = 0; i < list.size() - 1; i++) { + if (list.get(i) >= list.get(i + 1)) { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2974.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2974.java new file mode 100644 index 0000000000..ed85fd9fac --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2974.java @@ -0,0 +1,23 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.PriorityQueue; + +public class _2974 { + public static class Solution1 { + public int[] numberGame(int[] nums) { + PriorityQueue minHeap = new PriorityQueue<>(); + for (int num : nums) { + minHeap.offer(num); + } + int[] arr = new int[nums.length]; + int i = 0; + while (!minHeap.isEmpty()) { + int first = minHeap.poll(); + int second = minHeap.poll(); + arr[i++] = second; + arr[i++] = first; + } + return arr; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2976.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2976.java new file mode 100644 index 0000000000..ac0ec0a231 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2976.java @@ -0,0 +1,75 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +public class _2976 { + public static class Solution1 { + /* + * My completely original solution to use Dijkstra's algorithm. + * Dijkstra's algorithm is the way to go for finding + * the shortest path in a weighted (non-negative) graph. + */ + public long minimumCost( + String source, String target, char[] original, char[] changed, int[] cost) { + int alphabetSize = 26; + List[] graph = new ArrayList[alphabetSize]; + for (int i = 0; i < alphabetSize; i++) { + graph[i] = new ArrayList<>(); + } + for (int i = 0; i < original.length; i++) { + graph[original[i] - 'a'].add(new int[] {changed[i] - 'a', cost[i]}); + } + long minCost = 0L; + Map cache = new HashMap<>(); + for (int i = 0; i < source.length(); i++) { + long thisCost = + dijkstra(source.charAt(i) - 'a', target.charAt(i) - 'a', graph, cache); + if (thisCost != -1) { + minCost += thisCost; + } else { + return -1; + } + } + return minCost; + } + + private long dijkstra( + int source, int target, List[] graph, Map cache) { + if (cache.containsKey(source + "->" + target)) { + return cache.get(source + "->" + target); + } + int[] minCosts = new int[26]; + Arrays.fill(minCosts, Integer.MAX_VALUE); + minCosts[source] = 0; + Queue q = new LinkedList<>(); + q.offer(new int[] {source, 0}); + while (!q.isEmpty()) { + int[] curr = q.poll(); + int currNode = curr[0]; + int currCost = curr[1]; + if (currCost > minCosts[currNode]) { + continue; + } + for (int[] neighbor : graph[currNode]) { + int neighborNode = neighbor[0]; + int neighborCost = neighbor[1]; + if (currCost + neighborCost < minCosts[neighborNode]) { + minCosts[neighborNode] = currCost + neighborCost; + q.offer(new int[] {neighborNode, minCosts[neighborNode]}); + } + } + } + if (minCosts[target] == Integer.MAX_VALUE) { + minCosts[target] = -1; + } + cache.put(source + "->" + target, (long) minCosts[target]); + return cache.getOrDefault(source + "->" + target, -1L); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2980.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2980.java new file mode 100644 index 0000000000..5e1f114ff3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2980.java @@ -0,0 +1,20 @@ +package com.fishercoder.solutions.thirdthousand; + +public class _2980 { + public static class Solution1 { + /* + * 1. bitwise OR can never unset a bit, so if the solution exists, there must be a pair of elements; + * 2. as the rightmost bit must stay unset, it's essentially looking for a pair of two even numbers. + */ + public boolean hasTrailingZeros(int[] nums) { + for (int i = 0; i < nums.length; i++) { + for (int j = i + 1; j < nums.length; j++) { + if (nums[i] % 2 == 0 && nums[j] % 2 == 0) { + return true; + } + } + } + return false; + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/thirdthousand/_2996.java b/src/main/java/com/fishercoder/solutions/thirdthousand/_2996.java new file mode 100644 index 0000000000..34a44d8253 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/thirdthousand/_2996.java @@ -0,0 +1,27 @@ +package com.fishercoder.solutions.thirdthousand; + +import java.util.HashSet; +import java.util.Set; + +public class _2996 { + public static class Solution1 { + public int missingInteger(int[] nums) { + int sum = nums[0]; + for (int i = 1; i < nums.length; i++) { + if (nums[i - 1] + 1 == nums[i]) { + sum += nums[i]; + } else { + break; + } + } + Set seen = new HashSet<>(); + for (int i = 0; i < nums.length; i++) { + seen.add(nums[i]); + } + while (seen.contains(sum)) { + sum++; + } + return sum; + } + } +} diff --git a/src/test/java/com/fishercoder/_1002Test.java b/src/test/java/com/fishercoder/_1002Test.java deleted file mode 100644 index 4b3332211f..0000000000 --- a/src/test/java/com/fishercoder/_1002Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._1002; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _1002Test { - private static _1002.Solution1 solution1; - private static String[] A; - - @BeforeClass - public static void setup() { - solution1 = new _1002.Solution1(); - } - - @Test - public void test1() { - A = new String[] {"bella", "label", "roller"}; - CommonUtils.print(solution1.commonChars(A)); - } - - @Test - public void test2() { - A = new String[] {"cool", "lock", "cook"}; - CommonUtils.print(solution1.commonChars(A)); - } -} diff --git a/src/test/java/com/fishercoder/_1003Test.java b/src/test/java/com/fishercoder/_1003Test.java deleted file mode 100644 index 5dcd5115a9..0000000000 --- a/src/test/java/com/fishercoder/_1003Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1003; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class _1003Test { - private static _1003.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1003.Solution1(); - } - - @Test - public void test1() { - assertTrue(solution1.isValid("aabcbc")); - } - - @Test - public void test2() { - assertTrue(solution1.isValid("abcabcababcc")); - } - - @Test - public void test3() { - assertFalse(solution1.isValid("abccba")); - } - - @Test - public void test4() { - assertFalse(solution1.isValid("cababc")); - } -} diff --git a/src/test/java/com/fishercoder/_1005Test.java b/src/test/java/com/fishercoder/_1005Test.java deleted file mode 100644 index 7075c155e9..0000000000 --- a/src/test/java/com/fishercoder/_1005Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1005; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1005Test { - private static _1005.Solution1 solution1; - private static _1005.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _1005.Solution1(); - solution2 = new _1005.Solution2(); - } - - @Test - public void test1() { - assertEquals(5, solution1.largestSumAfterKNegations(new int[]{4, 2, 3}, 1)); - assertEquals(5, solution2.largestSumAfterKNegations(new int[]{4, 2, 3}, 1)); - } - - @Test - public void test2() { - assertEquals(6, solution1.largestSumAfterKNegations(new int[]{3, -1, 0, 2}, 3)); - assertEquals(6, solution2.largestSumAfterKNegations(new int[]{3, -1, 0, 2}, 3)); - } - - @Test - public void test3() { - assertEquals(13, solution1.largestSumAfterKNegations(new int[]{2, -3, -1, 5, -4}, 2)); - assertEquals(13, solution2.largestSumAfterKNegations(new int[]{2, -3, -1, 5, -4}, 2)); - } - - @Test - public void test4() { - assertEquals(22, solution1.largestSumAfterKNegations(new int[]{-8, 3, -5, -3, -5, -2}, 6)); - assertEquals(22, solution2.largestSumAfterKNegations(new int[]{-8, 3, -5, -3, -5, -2}, 6)); - } -} diff --git a/src/test/java/com/fishercoder/_1008Test.java b/src/test/java/com/fishercoder/_1008Test.java deleted file mode 100644 index 60afdcbea9..0000000000 --- a/src/test/java/com/fishercoder/_1008Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1008; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; - -public class _1008Test { - private static _1008.Solution1 solution1; - private static int[] preorder; - private static TreeNode expected; - private static TreeNode actual; - - @Test - public void test1() { - solution1 = new _1008.Solution1(); - preorder = new int[]{8, 5, 1, 7, 10, 12}; - expected = TreeUtils.constructBinaryTree(Arrays.asList(8, 5, 10, 1, 7, null, 12)); - TreeUtils.printBinaryTree(expected); - actual = solution1.bstFromPreorder(preorder); - TreeUtils.printBinaryTree(actual); - assertEquals(expected, actual); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1009Test.java b/src/test/java/com/fishercoder/_1009Test.java deleted file mode 100644 index 4b1e37a548..0000000000 --- a/src/test/java/com/fishercoder/_1009Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1009; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1009Test { - private static _1009.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1009.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.bitwiseComplement(5)); - } - - @Test - public void test2() { - assertEquals(5, solution1.bitwiseComplement(10)); - } - - @Test - public void test3() { - assertEquals(0, solution1.bitwiseComplement(7)); - } - - @Test - public void test4() { - assertEquals(3, solution1.bitwiseComplement(12)); - } - - @Test - public void test5() { - assertEquals(1, solution1.bitwiseComplement(0)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_100Test.java b/src/test/java/com/fishercoder/_100Test.java deleted file mode 100644 index 27207c25a4..0000000000 --- a/src/test/java/com/fishercoder/_100Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._100; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _100Test { - private static _100.Solution1 solution1; - private static TreeNode p; - private static TreeNode q; - - @BeforeClass - public static void setup() { - solution1 = new _100.Solution1(); - } - - @Test - public void test1() { - p = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); - TreeUtils.printBinaryTree(p); - q = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); - TreeUtils.printBinaryTree(p); - assertEquals(true, solution1.isSameTree(p, q)); - } - - @Test - public void test2() { - p = TreeUtils.constructBinaryTree(Arrays.asList(1, 2)); - TreeUtils.printBinaryTree(p); - q = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2)); - TreeUtils.printBinaryTree(p); - assertEquals(false, solution1.isSameTree(p, q)); - } - - @Test - public void test3() { - p = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 1)); - TreeUtils.printBinaryTree(p); - q = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 2)); - TreeUtils.printBinaryTree(p); - assertEquals(false, solution1.isSameTree(p, q)); - } -} diff --git a/src/test/java/com/fishercoder/_1010Test.java b/src/test/java/com/fishercoder/_1010Test.java deleted file mode 100644 index 92979bd326..0000000000 --- a/src/test/java/com/fishercoder/_1010Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1010; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1010Test { - private static _1010.Solution1 solution1; - private static int[] time; - - @BeforeClass - public static void setup() { - solution1 = new _1010.Solution1(); - } - - @Test - public void test1() { - time = new int[]{30, 20, 150, 100, 40}; - assertEquals(3, solution1.numPairsDivisibleBy60(time)); - } - - @Test - public void test2() { - time = new int[]{60, 60, 60}; - assertEquals(3, solution1.numPairsDivisibleBy60(time)); - } - -} diff --git a/src/test/java/com/fishercoder/_1011Test.java b/src/test/java/com/fishercoder/_1011Test.java deleted file mode 100644 index b5e4aae6bb..0000000000 --- a/src/test/java/com/fishercoder/_1011Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1011; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1011Test { - private static _1011.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1011.Solution1(); - } - - @Test - public void test1() { - int[] weights = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - assertEquals(solution1.shipWithinDays(weights, 5), 15); - } - - @Test - public void test2() { - int[] weights = {3, 2, 2, 4, 1, 4}; - assertEquals(solution1.shipWithinDays(weights, 3), 6); - } - - @Test - public void test3() { - int[] weights = {1, 2, 3, 1, 1}; - assertEquals(solution1.shipWithinDays(weights, 4), 3); - } -} diff --git a/src/test/java/com/fishercoder/_1013Test.java b/src/test/java/com/fishercoder/_1013Test.java deleted file mode 100644 index 06119a18a3..0000000000 --- a/src/test/java/com/fishercoder/_1013Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1013; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _1013Test { - private static _1013.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1013.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.canThreePartsEqualSum(new int[]{0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1})); - } - - @Test - public void test2() { - assertEquals(false, solution1.canThreePartsEqualSum(new int[]{0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1})); - } - - @Test - public void test3() { - assertEquals(true, solution1.canThreePartsEqualSum(new int[]{3, 3, 6, 5, -2, 2, 5, 1, -9, 4})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1014Test.java b/src/test/java/com/fishercoder/_1014Test.java deleted file mode 100644 index efcc68c79f..0000000000 --- a/src/test/java/com/fishercoder/_1014Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1014; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1014Test { - private static _1014.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1014.Solution1(); - } - - @Test - public void test1() { - assertEquals(solution1.maxScoreSightseeingPair(new int[]{1, 3, 5}), 7); - } - - @Test - public void test2() { - assertEquals(solution1.maxScoreSightseeingPair(new int[]{8, 1, 5, 2, 6}), 11); - } -} diff --git a/src/test/java/com/fishercoder/_1018Test.java b/src/test/java/com/fishercoder/_1018Test.java deleted file mode 100644 index 219db8776b..0000000000 --- a/src/test/java/com/fishercoder/_1018Test.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1018; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; - -public class _1018Test { - private static _1018.Solution1 solution1; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _1018.Solution1(); - } - - @Test - public void test1() { - A = new int[]{0, 1, 1}; - assertEquals(Arrays.asList(true, false, false), solution1.prefixesDivBy5(A)); - } - - @Test - public void test2() { - A = new int[]{1, 1, 1}; - assertEquals(Arrays.asList(false, false, false), solution1.prefixesDivBy5(A)); - } - - @Test - public void test3() { - A = new int[]{0, 1, 1, 1, 1, 1}; - assertEquals(Arrays.asList(true, false, false, false, true, false), solution1.prefixesDivBy5(A)); - } - - @Test - public void test4() { - A = new int[]{1, 1, 1, 0, 1}; - assertEquals(Arrays.asList(false, false, false, false, false), solution1.prefixesDivBy5(A)); - } - - @Test - public void test5() { - A = new int[]{1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1}; - assertEquals(Arrays.asList(false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, true, true, true, true, false), solution1.prefixesDivBy5(A)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1019Test.java b/src/test/java/com/fishercoder/_1019Test.java deleted file mode 100644 index e3901ba9f6..0000000000 --- a/src/test/java/com/fishercoder/_1019Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._1019; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1019Test { - private static _1019.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1019.Solution1(); - } - - @Test - public void test1() { - ListNode head = LinkedListUtils.contructLinkedList(new int[]{2, 1, 5}); - assertArrayEquals(new int[]{5, 5, 0}, solution1.nextLargerNodes(head)); - } - - @Test - public void test2() { - ListNode head = LinkedListUtils.contructLinkedList(new int[]{2, 7, 4, 3, 5}); - assertArrayEquals(new int[]{7, 0, 5, 5, 0}, solution1.nextLargerNodes(head)); - } - - @Test - public void test3() { - ListNode head = LinkedListUtils.contructLinkedList(new int[]{1, 7, 5, 1, 9, 2, 5, 1}); - assertArrayEquals(new int[]{7, 9, 9, 9, 0, 5, 0, 0}, solution1.nextLargerNodes(head)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_101Test.java b/src/test/java/com/fishercoder/_101Test.java deleted file mode 100644 index 5402f26be5..0000000000 --- a/src/test/java/com/fishercoder/_101Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._101; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _101Test { - private static _101.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _101.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 2, 3, 4, 4, 3)); - assertEquals(true, solution1.isSymmetric(root)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 2, null, 3, null, 3)); - assertEquals(false, solution1.isSymmetric(root)); - } -} diff --git a/src/test/java/com/fishercoder/_1020Test.java b/src/test/java/com/fishercoder/_1020Test.java deleted file mode 100644 index 359557c6c3..0000000000 --- a/src/test/java/com/fishercoder/_1020Test.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1020; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1020Test { - private static _1020.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1020.Solution1(); - } - - @Test - public void test1() { - int[][] map = { - {0, 0, 0, 0}, - {1, 0, 1, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 0} - }; - - assertEquals(solution1.numEnclaves(map), 3); - } - - @Test - public void test2() { - int[][] map = { - {0, 1, 1, 0}, - {0, 0, 1, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 0} - }; - - assertEquals(solution1.numEnclaves(map), 0); - } - - @Test - public void test3() { - int[][] map = { - {0, 1, 1, 0}, - {0, 0, 0, 0}, - {1, 0, 1, 0}, - {1, 0, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 0}, - }; - - assertEquals(solution1.numEnclaves(map), 3); - } -} diff --git a/src/test/java/com/fishercoder/_1021Test.java b/src/test/java/com/fishercoder/_1021Test.java deleted file mode 100644 index 78c25784eb..0000000000 --- a/src/test/java/com/fishercoder/_1021Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1021; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1021Test { - private static _1021.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1021.Solution1(); - } - - @Test - public void test1() { - assertEquals("()()()", solution1.removeOuterParentheses("(()())(())")); - } - - @Test - public void test2() { - assertEquals("()()()()(())", solution1.removeOuterParentheses("(()())(())(()(()))")); - } - - @Test - public void test3() { - assertEquals("", solution1.removeOuterParentheses("()()")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1022Test.java b/src/test/java/com/fishercoder/_1022Test.java deleted file mode 100644 index f509f4fdda..0000000000 --- a/src/test/java/com/fishercoder/_1022Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1022; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; - -public class _1022Test { - private static _1022.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _1022.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 1, 0, 1, 0, 1)); - TreeUtils.printBinaryTree(root); - assertEquals(22, solution1.sumRootToLeaf(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1025Test.java b/src/test/java/com/fishercoder/_1025Test.java deleted file mode 100644 index 39233fb984..0000000000 --- a/src/test/java/com/fishercoder/_1025Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1025; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -@Ignore -public class _1025Test { - private static _1025.Solution1 solution1; - - @Before - public void setup() { - solution1 = new _1025.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.divisorGame(2)); - } -} diff --git a/src/test/java/com/fishercoder/_1029Test.java b/src/test/java/com/fishercoder/_1029Test.java deleted file mode 100644 index 2bc0516e26..0000000000 --- a/src/test/java/com/fishercoder/_1029Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1029; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1029Test { - private static _1029.Solution1 solution1; - private static int[][] costs; - - @BeforeClass - public static void setup() { - solution1 = new _1029.Solution1(); - } - - @Test - public void test1() { - costs = new int[][]{ - {10, 20}, - {30, 200}, - {400, 50}, - {30, 20} - }; - assertEquals(110, solution1.twoCitySchedCost(costs)); - } - - @Test - public void test2() { - costs = new int[][]{ - {259, 770}, - {448, 54}, - {926, 667}, - {184, 139}, - {840, 118}, - {577, 469} - }; - assertEquals(1859, solution1.twoCitySchedCost(costs)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1030Test.java b/src/test/java/com/fishercoder/_1030Test.java deleted file mode 100644 index eb99a58f8e..0000000000 --- a/src/test/java/com/fishercoder/_1030Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._1030; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _1030Test { - private static _1030.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1030.Solution1(); - } - - @Test - public void test1() { - CommonUtils.print2DIntArray(solution1.allCellsDistOrder(1, 2, 0, 0)); - } - - @Test - public void test2() { - CommonUtils.print2DIntArray(solution1.allCellsDistOrder(2, 2, 0, 1)); - } - - @Test - public void test3() { - CommonUtils.print2DIntArray(solution1.allCellsDistOrder(2, 3, 1, 2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1033Test.java b/src/test/java/com/fishercoder/_1033Test.java deleted file mode 100644 index 6d2bc09028..0000000000 --- a/src/test/java/com/fishercoder/_1033Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1033; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1033Test { - private static _1033.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1033.Solution1(); - } - - @Test - public void test1() { - int[] expected = {1, 2}; - assertArrayEquals(expected, solution1.numMovesStones(1, 2, 5)); - } - - @Test - public void test2() { - int[] expected = {0, 0}; - assertArrayEquals(expected, solution1.numMovesStones(4, 3, 2)); - } - - @Test - public void test3() { - int[] expected = {1, 2}; - assertArrayEquals(expected, solution1.numMovesStones(3, 5, 1)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1037Test.java b/src/test/java/com/fishercoder/_1037Test.java deleted file mode 100644 index 7638038dcf..0000000000 --- a/src/test/java/com/fishercoder/_1037Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1037; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _1037Test { - private static _1037.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1037.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isBoomerang(new int[][]{new int[]{1, 1}, new int[]{2, 3}, new int[]{3, 2}})); - } - - @Test - public void test2() { - assertEquals(false, solution1.isBoomerang(new int[][]{new int[]{1, 1}, new int[]{2, 2}, new int[]{3, 3}})); - } - - @Test - public void test3() { - assertEquals(true, solution1.isBoomerang(new int[][]{new int[]{0, 0}, new int[]{0, 2}, new int[]{2, 1}})); - } - - @Test - public void test4() { - assertEquals(false, solution1.isBoomerang(new int[][]{new int[]{0, 0}, new int[]{1, 1}, new int[]{1, 1}})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1038Test.java b/src/test/java/com/fishercoder/_1038Test.java deleted file mode 100644 index e20d6fb0b9..0000000000 --- a/src/test/java/com/fishercoder/_1038Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1038; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1038Test { - private static _1038.Solution1 solution1; - private static TreeNode root; - private static TreeNode expected; - private static TreeNode actual; - - @BeforeClass - public static void setup() { - solution1 = new _1038.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(4, 1, 6, 0, 2, 5, 7, null, null, null, 3, null, null, null, 8)); - TreeUtils.printBinaryTree(root); - expected = TreeUtils.constructBinaryTree(Arrays.asList(30, 36, 21, 36, 35, 26, 15, null, null, null, 33, null, null, null, 8)); - TreeUtils.printBinaryTree(expected); - actual = solution1.bstToGst(root); - TreeUtils.printBinaryTree(actual); - assertEquals(expected, actual); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_103Test.java b/src/test/java/com/fishercoder/_103Test.java deleted file mode 100644 index e0ba1aa849..0000000000 --- a/src/test/java/com/fishercoder/_103Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._103; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -public class _103Test { - private static _103.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _103.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); - TreeUtils.printBinaryTree(root); - CommonUtils.printListList(solution1.zigzagLevelOrder(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1043Test.java b/src/test/java/com/fishercoder/_1043Test.java deleted file mode 100644 index cce85b234e..0000000000 --- a/src/test/java/com/fishercoder/_1043Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1043; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1043Test { - private static _1043.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1043.Solution1(); - } - - @Test - public void test1() { - assertEquals(84, solution1.maxSumAfterPartitioning(new int[]{1, 15, 7, 9, 2, 5, 10}, 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1046Test.java b/src/test/java/com/fishercoder/_1046Test.java deleted file mode 100644 index d8700f5253..0000000000 --- a/src/test/java/com/fishercoder/_1046Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1046; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _1046Test { - private static _1046.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1046.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.lastStoneWeight(new int[]{2, 7, 4, 1, 8, 1})); - } -} diff --git a/src/test/java/com/fishercoder/_1047Test.java b/src/test/java/com/fishercoder/_1047Test.java deleted file mode 100644 index d1b6b6b959..0000000000 --- a/src/test/java/com/fishercoder/_1047Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1047; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1047Test { - private static _1047.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1047.Solution1(); - } - - @Test - public void test1() { - assertEquals("ca", solution1.removeDuplicates("abbaca")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1049Test.java b/src/test/java/com/fishercoder/_1049Test.java deleted file mode 100644 index 2bde3d2851..0000000000 --- a/src/test/java/com/fishercoder/_1049Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1049; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1049Test { - private static _1049.Solution1 solution1; - private static int[] stones; - - @BeforeClass - public static void setup() { - solution1 = new _1049.Solution1(); - } - - @Test - public void test1() { - stones = new int[]{2, 7, 4, 1, 8, 1}; - assertEquals(1, solution1.lastStoneWeightII(stones)); - } - - -} diff --git a/src/test/java/com/fishercoder/_104Test.java b/src/test/java/com/fishercoder/_104Test.java deleted file mode 100644 index 8064338a60..0000000000 --- a/src/test/java/com/fishercoder/_104Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._104; - -import java.util.Arrays; - -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _104Test { - private static _104.Solution1 solution1; - private static _104.Solution2 solution2; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _104.Solution1(); - solution2 = new _104.Solution2(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); - assertEquals(3, solution1.maxDepth(root)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); - TreeUtils.printBinaryTree(root); - assertEquals(3, solution2.maxDepth(root)); - } -} diff --git a/src/test/java/com/fishercoder/_1051Test.java b/src/test/java/com/fishercoder/_1051Test.java deleted file mode 100644 index 072b21de93..0000000000 --- a/src/test/java/com/fishercoder/_1051Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1051; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1051Test { - private static _1051.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1051.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.heightChecker(new int[]{1, 1, 4, 2, 1, 3})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1055Test.java b/src/test/java/com/fishercoder/_1055Test.java deleted file mode 100644 index 2ff029b278..0000000000 --- a/src/test/java/com/fishercoder/_1055Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1055; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1055Test { - private static _1055.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1055.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.fixedPoint(new int[]{-10, -5, 0, 3, 7})); - } - - @Test - public void test2() { - assertEquals(0, solution1.fixedPoint(new int[]{0, 2, 5, 8, 17})); - } - - @Test - public void test3() { - assertEquals(-1, solution1.fixedPoint(new int[]{-10, -5, 3, 4, 7, 9})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1056Test.java b/src/test/java/com/fishercoder/_1056Test.java deleted file mode 100644 index a944c5bf48..0000000000 --- a/src/test/java/com/fishercoder/_1056Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1056; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1056Test { - private static _1056.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1056.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.confusingNumber(6)); - } - - @Test - public void test2() { - assertEquals(true, solution1.confusingNumber(89)); - } - - @Test - public void test3() { - assertEquals(false, solution1.confusingNumber(11)); - } - - @Test - public void test4() { - assertEquals(false, solution1.confusingNumber(25)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1057Test.java b/src/test/java/com/fishercoder/_1057Test.java deleted file mode 100644 index 1978bcc0ed..0000000000 --- a/src/test/java/com/fishercoder/_1057Test.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1057; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1057Test { - private static _1057.Solution1 solution1; - private static int[][] workers; - private static int[][] bikes; - - @BeforeClass - public static void setup() { - solution1 = new _1057.Solution1(); - } - - @Test - public void test1() { - workers = new int[][]{ - {0, 0}, - {2, 1}, - }; - bikes = new int[][]{ - {1, 2}, - {3, 3}, - }; - assertArrayEquals(new int[]{1, 0}, solution1.assignBikes(workers, bikes)); - } - - @Test - public void test2() { - workers = new int[][]{ - {0, 0}, - {1, 1}, - {2, 0}, - }; - bikes = new int[][]{ - {1, 0}, - {2, 2}, - {2, 1}, - }; - assertArrayEquals(new int[]{0, 2, 1}, solution1.assignBikes(workers, bikes)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_105Test.java b/src/test/java/com/fishercoder/_105Test.java deleted file mode 100644 index 26d99ac54b..0000000000 --- a/src/test/java/com/fishercoder/_105Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._105; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _105Test { - private static _105.Solution1 solution1; - private static TreeNode expected; - private static TreeNode actual; - private static int[] preorder; - private static int[] inorder; - - @BeforeClass - public static void setup() { - solution1 = new _105.Solution1(); - } - - @Test - public void test1() { - preorder = new int[] {1, 2, 3}; - inorder = new int[] {2, 1, 3}; - actual = solution1.buildTree(preorder, inorder); - expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); - assertEquals(expected, actual); - } - - @Test - public void test2() { - preorder = new int[] {1, 2, 4, 5, 3}; - inorder = new int[] {4, 2, 5, 1, 3}; - actual = solution1.buildTree(preorder, inorder); - expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_1062Test.java b/src/test/java/com/fishercoder/_1062Test.java deleted file mode 100644 index 38a2e942de..0000000000 --- a/src/test/java/com/fishercoder/_1062Test.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1062; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -@Ignore -public class _1062Test { - private static _1062.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1062.Solution1(); - } - - @Test - public void test1() { - assertEquals(0, (solution1.longestRepeatingSubstring("abcd"))); - } - - @Test - public void test2() { - assertEquals(2, (solution1.longestRepeatingSubstring("abbaba"))); - } - - @Test - public void test3() { - assertEquals(3, (solution1.longestRepeatingSubstring("aabcaabdaab"))); - } - - @Test - public void test4() { - assertEquals(4, (solution1.longestRepeatingSubstring("aaaaa"))); - } - - @Test - public void test5() { - assertEquals(10, (solution1.longestRepeatingSubstring("aaabaabbbaaabaabbaabbbabbbaaaabbaaaaaabbbaabbbbbbbbbaaaabbabbaba"))); - } -} diff --git a/src/test/java/com/fishercoder/_1065Test.java b/src/test/java/com/fishercoder/_1065Test.java deleted file mode 100644 index 2292fe55ea..0000000000 --- a/src/test/java/com/fishercoder/_1065Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._1065; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _1065Test { - private static _1065.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1065.Solution1(); - } - - @Test - public void test1() { - CommonUtils.print2DIntArray(solution1.indexPairs("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", new String[]{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"})); - } - - @Test - public void test2() { - CommonUtils.print2DIntArray(solution1.indexPairs("thestoryofleetcodeandme", new String[]{"story", "fleet", "leetcode"})); - } - - @Test - public void test3() { - CommonUtils.print2DIntArray(solution1.indexPairs("ababa", new String[]{"aba", "ab"})); - } - - @Test - public void test4() { - CommonUtils.print2DIntArray(solution1.indexPairs("aabaabbaabbaababaaaaaababaabaabaabaababbaabbbbaabbaaababbbbaabbabbabbababbabaabaaaabaabbbb", new String[]{"aabaaabbaba", "bbabbbaaabaaaab", "ababaabaababb", "bbbaaabababbba", "baaaabbaa"})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1066Test.java b/src/test/java/com/fishercoder/_1066Test.java deleted file mode 100644 index 7487bfab0a..0000000000 --- a/src/test/java/com/fishercoder/_1066Test.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1066; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1066Test { - private static _1066.Solution1 solution1; - private static int[][] workers; - private static int[][] bikes; - - @Test - public void test1() { - solution1 = new _1066.Solution1(); - workers = new int[][]{ - {0, 0}, - {2, 1}, - }; - bikes = new int[][]{ - {1, 2}, - {3, 3}, - }; - assertEquals(6, solution1.assignBikes(workers, bikes)); - } - - @Test - public void test2() { - solution1 = new _1066.Solution1(); - workers = new int[][]{ - {0, 0}, - {1, 1}, - {2, 0}, - }; - bikes = new int[][]{ - {1, 0}, - {2, 2}, - {2, 1}, - }; - assertEquals(4, solution1.assignBikes(workers, bikes)); - } - - @Test - public void test3() { - solution1 = new _1066.Solution1(); - workers = new int[][]{ - {0, 0}, - {1, 0}, - {2, 0}, - {3, 0}, - {4, 0}, - {5, 0}, - }; - bikes = new int[][]{ - {0, 999}, - {1, 999}, - {2, 999}, - {3, 999}, - {4, 999}, - {5, 999}, - {6, 999}, - {7, 999}, - }; - assertEquals(5994, solution1.assignBikes(workers, bikes)); - } - - @Test - public void test4() { - solution1 = new _1066.Solution1(); - workers = new int[][]{ - {815, 60}, - {638, 626}, - {6, 44}, - {103, 90}, - {591, 880}, - }; - bikes = new int[][]{ - {709, 161}, - {341, 339}, - {755, 955}, - {172, 27}, - {433, 489}, - }; - assertEquals(1458, solution1.assignBikes(workers, bikes)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_106Test.java b/src/test/java/com/fishercoder/_106Test.java deleted file mode 100644 index 267f4ef0af..0000000000 --- a/src/test/java/com/fishercoder/_106Test.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._106; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; - -public class _106Test { - private static _106.Solution1 solution1; - private static TreeNode expected; - private static TreeNode actual; - private static int[] inorder; - private static int[] postorder; - - @BeforeClass - public static void setup() { - solution1 = new _106.Solution1(); - } - - @Test - public void test1() { - /**it should be a tree like this: - * 3 - * / - * 1 - * \ - * 2 - */ - postorder = new int[]{2, 1, 3}; - inorder = new int[]{1, 2, 3}; - actual = solution1.buildTree(inorder, postorder); - expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 2)); - assertEquals(expected, actual); - } - - @Test - public void test2() { - /**it should be a tree like this: - * 3 - * / - * 1 - * \ - * 5 - * / - * 2 - * \ - * 4 - */ - postorder = new int[]{4, 2, 5, 1, 3}; - inorder = new int[]{1, 2, 4, 5, 3}; - actual = solution1.buildTree(inorder, postorder); - expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4)); - assertEquals(expected, actual); - } - - @Test - public void test3() { - /**it should be a tree like this: - * 2 - * / - * 1 - */ - inorder = new int[]{1, 2}; - postorder = new int[]{1, 2}; - actual = solution1.buildTree(inorder, postorder); - expected = TreeUtils.constructBinaryTree(Arrays.asList(2, 1)); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_1071Test.java b/src/test/java/com/fishercoder/_1071Test.java deleted file mode 100644 index 9608ae34e8..0000000000 --- a/src/test/java/com/fishercoder/_1071Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1071; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1071Test { - private static _1071.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1071.Solution1(); - } - - @Test - public void test1() { - assertEquals("ABC", solution1.gcdOfStrings("ABCABC", "ABC")); - } - - @Test - public void test2() { - assertEquals("AB", solution1.gcdOfStrings("ABABAB", "ABAB")); - } - - @Test - public void test3() { - assertEquals("", solution1.gcdOfStrings("LEET", "CODE")); - } - - @Test - public void test4() { - assertEquals("", solution1.gcdOfStrings("ABCABCD", "ABC")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1078Test.java b/src/test/java/com/fishercoder/_1078Test.java deleted file mode 100644 index 079de1a138..0000000000 --- a/src/test/java/com/fishercoder/_1078Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1078; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1078Test { - private static _1078.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1078.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new String[]{"girl", "student"}, solution1.findOcurrences("alice is a good girl she is a good student", "a", "good")); - } - - @Test - public void test2() { - assertArrayEquals(new String[]{"we", "rock"}, solution1.findOcurrences("we will we will rock you", "we", "will")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1079Test.java b/src/test/java/com/fishercoder/_1079Test.java deleted file mode 100644 index da874a0e04..0000000000 --- a/src/test/java/com/fishercoder/_1079Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1079; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1079Test { - private static _1079.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1079.Solution1(); - } - - @Test - public void test1() { - assertEquals(8, solution1.numTilePossibilities("AAB")); - } - - @Test - public void test2() { - assertEquals(188, solution1.numTilePossibilities("AAABBC")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_107Test.java b/src/test/java/com/fishercoder/_107Test.java deleted file mode 100644 index b4b644f3bf..0000000000 --- a/src/test/java/com/fishercoder/_107Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._107; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -public class _107Test { - private static _107.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _107.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); - TreeUtils.printBinaryTree(root); - CommonUtils.printListList(solution1.levelOrder(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1085Test.java b/src/test/java/com/fishercoder/_1085Test.java deleted file mode 100644 index 3534d827e3..0000000000 --- a/src/test/java/com/fishercoder/_1085Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1085; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1085Test { - private static _1085.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1085.Solution1(); - } - - @Test - public void test1() { - assertEquals(0, solution1.sumOfDigits(new int[]{34, 23, 1, 24, 75, 33, 54, 8})); - } - - @Test - public void test2() { - assertEquals(1, solution1.sumOfDigits(new int[]{99, 77, 33, 66, 55})); - } -} diff --git a/src/test/java/com/fishercoder/_1086Test.java b/src/test/java/com/fishercoder/_1086Test.java deleted file mode 100644 index 2d65570297..0000000000 --- a/src/test/java/com/fishercoder/_1086Test.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1086; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1086Test { - private static _1086.Solution1 solution1; - private static _1086.Solution2 solution2; - private static int[][] items; - - @BeforeClass - public static void setup() { - solution1 = new _1086.Solution1(); - solution2 = new _1086.Solution2(); - } - - @Test - public void test1() { - items = new int[][]{ - {1, 91}, - {1, 92}, - {2, 93}, - {2, 97}, - {1, 60}, - {2, 77}, - {1, 65}, - {1, 87}, - {1, 100}, - {2, 100}, - {2, 76} - }; - assertArrayEquals(new int[][]{ - {1, 87}, - {2, 88} - }, solution1.highFive(items)); - } - - @Test - public void test2() { - items = new int[][]{ - {1, 91}, - {1, 92}, - {2, 93}, - {2, 97}, - {1, 60}, - {2, 77}, - {1, 65}, - {1, 87}, - {1, 100}, - {2, 100}, - {2, 76} - }; - assertArrayEquals(new int[][]{ - {1, 87}, - {2, 88} - }, solution2.highFive(items)); - } - -} diff --git a/src/test/java/com/fishercoder/_1087Test.java b/src/test/java/com/fishercoder/_1087Test.java deleted file mode 100644 index eb58704446..0000000000 --- a/src/test/java/com/fishercoder/_1087Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1087; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1087Test { - private static _1087.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1087.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new String[]{"ade", "adf", "bde", "bdf", "cde", "cdf"}, solution1.expand("{a,b,c}d{e,f}")); - } - - @Test - public void test2() { - assertArrayEquals(new String[]{"abcd"}, solution1.expand("abcd")); - } - - @Test - public void test3() { - assertArrayEquals(new String[]{"acdf", "acef", "bcdf", "bcef"}, solution1.expand("{a,b}c{d,e}f")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1089Test.java b/src/test/java/com/fishercoder/_1089Test.java deleted file mode 100644 index c4300035e1..0000000000 --- a/src/test/java/com/fishercoder/_1089Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1089; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1089Test { - private static _1089.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1089.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{1, 0, 2, 3, 0, 4, 5, 0}; - solution1.duplicateZeros(arr); - assertArrayEquals(new int[]{1, 0, 0, 2, 3, 0, 0, 4}, arr); - } - - @Test - public void test2() { - arr = new int[]{1, 2, 3}; - solution1.duplicateZeros(arr); - assertArrayEquals(new int[]{1, 2, 3}, arr); - } - -} diff --git a/src/test/java/com/fishercoder/_108Test.java b/src/test/java/com/fishercoder/_108Test.java deleted file mode 100644 index 63d02209ef..0000000000 --- a/src/test/java/com/fishercoder/_108Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._108; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _108Test { - private static _108.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _108.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3}; - TreeUtils.printBinaryTree(solution1.sortedArrayToBST(nums)); - } - - @Test - public void test2() { - nums = new int[]{}; - TreeUtils.printBinaryTree(solution1.sortedArrayToBST(nums)); - } - - @Test - public void test3() { - nums = new int[]{-10, -3, 0, 5, 9}; - TreeUtils.printBinaryTree(solution1.sortedArrayToBST(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1099Test.java b/src/test/java/com/fishercoder/_1099Test.java deleted file mode 100644 index fa4ec05e2a..0000000000 --- a/src/test/java/com/fishercoder/_1099Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1099; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1099Test { - private static _1099.Solution1 solution1; - private static _1099.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _1099.Solution1(); - solution2 = new _1099.Solution2(); - } - - @Test - public void test1() { - assertEquals(58, solution1.twoSumLessThanK(new int[]{34, 23, 1, 24, 75, 33, 54, 8}, 60)); - } - - @Test - public void test2() { - assertEquals(-1, solution1.twoSumLessThanK(new int[]{10, 20, 30}, 15)); - } - - @Test - public void test3() { - assertEquals(58, solution2.twoSumLessThanK(new int[]{34, 23, 1, 24, 75, 33, 54, 8}, 60)); - } - - @Test - public void test4() { - assertEquals(-1, solution2.twoSumLessThanK(new int[]{10, 20, 30}, 15)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_109Test.java b/src/test/java/com/fishercoder/_109Test.java deleted file mode 100644 index 0655b3eecb..0000000000 --- a/src/test/java/com/fishercoder/_109Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._109; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -public class _109Test { - private static _109.Solution1 solution1; - private static ListNode head; - private static TreeNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _109.Solution1(); - } - - @Before - public void setUp() throws Exception { - } - - @Test - public void test1() { - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5}); - expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, 4, null, 2, null, 5)); - /**as long as it's a height-balanced tree, it's good for this problem requirement*/ - TreeUtils.printBinaryTree(solution1.sortedListToBST(head)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_10Test.java b/src/test/java/com/fishercoder/_10Test.java deleted file mode 100644 index 91e58216c1..0000000000 --- a/src/test/java/com/fishercoder/_10Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._10; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _10Test { - private static _10.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _10.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isMatch("", "")); - } - - @Test - public void test2() { - assertEquals(false, solution1.isMatch("aa", "a")); - } - - @Test - public void test3() { - assertEquals(true, solution1.isMatch("aab", "c*a*b")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1100Test.java b/src/test/java/com/fishercoder/_1100Test.java deleted file mode 100644 index 84901b2867..0000000000 --- a/src/test/java/com/fishercoder/_1100Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1100; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1100Test { - private static _1100.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1100.Solution1(); - } - - @Test - public void test1() { - assertEquals(6, solution1.numKLenSubstrNoRepeats("havefunonleetcode", 5)); - } - - @Test - public void test2() { - assertEquals(0, solution1.numKLenSubstrNoRepeats("home", 5)); - } -} diff --git a/src/test/java/com/fishercoder/_1103Test.java b/src/test/java/com/fishercoder/_1103Test.java deleted file mode 100644 index 5fd0981881..0000000000 --- a/src/test/java/com/fishercoder/_1103Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1103; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1103Test { - private static _1103.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1103.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new int[]{1, 2, 3, 1}, solution1.distributeCandies(7, 4)); - } - - @Test - public void test2() { - assertArrayEquals(new int[]{5, 2, 3}, solution1.distributeCandies(10, 3)); - } - - @Test - public void test3() { - assertArrayEquals(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 5, 0, 0, 0, 0, 0}, solution1.distributeCandies(600, 40)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1104Test.java b/src/test/java/com/fishercoder/_1104Test.java deleted file mode 100644 index b71d85dfb6..0000000000 --- a/src/test/java/com/fishercoder/_1104Test.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1104; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _1104Test { - private static _1104.Solution1 solution1; - private static _1104.Solution2 solution2; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _1104.Solution1(); - solution2 = new _1104.Solution2(); - } - - @Test - public void test1() { - expected = Arrays.asList(1, 3, 4, 14); - assertEquals(expected, solution1.pathInZigZagTree(14)); - } - - @Test - public void test2() { - expected = Arrays.asList(1, 2, 6, 10, 26); - assertEquals(expected, solution1.pathInZigZagTree(26)); - } - - @Test - public void test3() { - expected = Arrays.asList(1, 2, 7, 9, 28, 38); - assertEquals(expected, solution1.pathInZigZagTree(38)); - } - - @Test - public void test4() { - expected = Arrays.asList(1, 3, 5, 13, 20, 54, 83); - assertEquals(expected, solution1.pathInZigZagTree(83)); - } - - @Test - public void test5() { - expected = Arrays.asList(1, 2, 7, 9, 28, 39, 113, 156, 455, 625, 1821, 2500, 7287, 10000); - assertEquals(expected, solution1.pathInZigZagTree(10000)); - } - - @Test - @Ignore - public void test6() { - //takes too long to finish, ignore to let build pass - expected = Arrays.asList(1, 2, 6, 11, 24, 47, 97, 188, 390, 754, 1562, 3018, 6250, 12075, 25000, 48303, 100000); - assertEquals(expected, solution1.pathInZigZagTree(100000)); - } - - @Test - @Ignore - public void test7() { - //takes too long to finish, ignore to let build pass - expected = Arrays.asList(1, 3, 5, 12, 23, 48, 94, 195, 377, 781, 1509, 3125, 6037, 12500, 24151, 50000, 96607, 200000); - assertEquals(expected, solution1.pathInZigZagTree(200000)); - } - - @Test - @Ignore - public void test8() { - //takes too long to finish, ignore to let build pass - expected = Arrays.asList(1, 2, 6, 11, 24, 47, 97, 188, 390, 754, 1562, 3018, 6250, 12075, 25000, 48303, 100000, 193215, 400000); - assertEquals(expected, solution1.pathInZigZagTree(400000)); - } - - @Test - @Ignore - public void test9() { - //takes too long to finish, ignore to let build pass - expected = Arrays.asList(1, 2, 7, 8, 30, 34, 122, 139, 488, 559, 1953, 2237, 7812, 8950, 31250, 35803, 125000, 143215, 500000); - assertEquals(expected, solution1.pathInZigZagTree(500000)); - } - - @Test - public void test10() { - expected = Arrays.asList(1); - assertEquals(expected, solution1.pathInZigZagTree(1)); - } - - @Test - public void test11() { - expected = Arrays.asList(1, 3, 4, 14); - assertEquals(expected, solution2.pathInZigZagTree(14)); - } - - @Test - public void test12() { - expected = Arrays.asList(1); - assertEquals(expected, solution2.pathInZigZagTree(1)); - } - -} diff --git a/src/test/java/com/fishercoder/_1108Test.java b/src/test/java/com/fishercoder/_1108Test.java deleted file mode 100644 index 40ccde7645..0000000000 --- a/src/test/java/com/fishercoder/_1108Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1108; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1108Test { - private static _1108.Solution1 solution1; - private static _1108.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _1108.Solution1(); - solution2 = new _1108.Solution2(); - } - - @Test - public void test1() { - assertEquals("1[.]1[.]1[.]1", solution1.defangIPaddr("1.1.1.1")); - assertEquals("1[.]1[.]1[.]1", solution2.defangIPaddr("1.1.1.1")); - } - - @Test - public void test2() { - assertEquals("255[.]100[.]50[.]0", solution1.defangIPaddr("255.100.50.0")); - assertEquals("255[.]100[.]50[.]0", solution2.defangIPaddr("255.100.50.0")); - } -} diff --git a/src/test/java/com/fishercoder/_1118Test.java b/src/test/java/com/fishercoder/_1118Test.java deleted file mode 100644 index 633a16ab39..0000000000 --- a/src/test/java/com/fishercoder/_1118Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1118; -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1118Test { - private static _1118.Solution1 solution1; - - @Before - public void setupForEachTest() { - solution1 = new _1118.Solution1(); - } - - @Test - public void test1() { - assertEquals(31, solution1.numberOfDays(1992, 7)); - } - - @Test - public void test2() { - assertEquals(29, solution1.numberOfDays(2000, 2)); - } - - @Test - public void test3() { - assertEquals(28, solution1.numberOfDays(1900, 2)); - } - - @Test - public void test4() { - assertEquals(29, solution1.numberOfDays(1836, 2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1119Test.java b/src/test/java/com/fishercoder/_1119Test.java deleted file mode 100644 index 5ee900ef45..0000000000 --- a/src/test/java/com/fishercoder/_1119Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1119; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1119Test { - private static _1119.Solution1 solution1; - private static _1119.Solution2 solution2; - private static String S; - - @BeforeClass - public static void setup() { - solution1 = new _1119.Solution1(); - solution2 = new _1119.Solution2(); - } - - @Test - public void test1() { - S = "leetcodeisacommunityforcoders"; - assertEquals("ltcdscmmntyfrcdrs", solution1.removeVowels(S)); - assertEquals("ltcdscmmntyfrcdrs", solution2.removeVowels(S)); - } - - @Test - public void test2() { - S = "aeiou"; - assertEquals("", solution1.removeVowels(S)); - assertEquals("", solution2.removeVowels(S)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1122Test.java b/src/test/java/com/fishercoder/_1122Test.java deleted file mode 100644 index 6fd5bf313c..0000000000 --- a/src/test/java/com/fishercoder/_1122Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1122; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1122Test { - private static _1122.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1122.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new int[]{2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19}, solution1.relativeSortArray(new int[]{2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19}, new int[]{2, 1, 4, 3, 9, 6})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1128Test.java b/src/test/java/com/fishercoder/_1128Test.java deleted file mode 100644 index 1698108c65..0000000000 --- a/src/test/java/com/fishercoder/_1128Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1128; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1128Test { - private static _1128.Solution1 solution1; - private static int[][] dominoes; - - @BeforeClass - public static void setup() { - solution1 = new _1128.Solution1(); - } - - @Test - public void test1() { - dominoes = new int[][]{ - {1, 2}, - {2, 1}, - {3, 4}, - {5, 6} - }; - assertEquals(1, solution1.numEquivDominoPairs(dominoes)); - } -} diff --git a/src/test/java/com/fishercoder/_1133Test.java b/src/test/java/com/fishercoder/_1133Test.java deleted file mode 100644 index 6b614d24c0..0000000000 --- a/src/test/java/com/fishercoder/_1133Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1133; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1133Test { - private static _1133.Solution1 solution1; - private static _1133.Solution2 solution2; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _1133.Solution1(); - solution2 = new _1133.Solution2(); - } - - @Test - public void test1() { - A = new int[]{5, 7, 3, 9, 4, 9, 8, 3, 1}; - assertEquals(8, solution1.largestUniqueNumber(A)); - assertEquals(8, solution2.largestUniqueNumber(A)); - } - - @Test - public void test2() { - A = new int[]{9, 9, 8, 8}; - assertEquals(-1, solution1.largestUniqueNumber(A)); - assertEquals(-1, solution2.largestUniqueNumber(A)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1134Test.java b/src/test/java/com/fishercoder/_1134Test.java deleted file mode 100644 index 0a3c8ec701..0000000000 --- a/src/test/java/com/fishercoder/_1134Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1134; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1134Test { - private static _1134.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1134.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isArmstrong(153)); - } - - @Test - public void test2() { - assertEquals(false, solution1.isArmstrong(123)); - } -} diff --git a/src/test/java/com/fishercoder/_1137Test.java b/src/test/java/com/fishercoder/_1137Test.java deleted file mode 100644 index 7c86ba83e3..0000000000 --- a/src/test/java/com/fishercoder/_1137Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1137; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1137Test { - private static _1137.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1137.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.tribonacci(3)); - } - - @Test - public void test2() { - assertEquals(4, solution1.tribonacci(4)); - } - - @Test - public void test3() { - assertEquals(1389537, solution1.tribonacci(25)); - } - - @Test - public void test4() { - assertEquals(0, solution1.tribonacci(0)); - } - - @Test - public void test5() { - assertEquals(1, solution1.tribonacci(2)); - } -} diff --git a/src/test/java/com/fishercoder/_113Test.java b/src/test/java/com/fishercoder/_113Test.java deleted file mode 100644 index 0e04931314..0000000000 --- a/src/test/java/com/fishercoder/_113Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._113; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _113Test { - private static _113.Solution1 solution1; - private static TreeNode root; - private static int sum; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _113.Solution1(); - } - - @Test - public void test1() { - sum = 22; - root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1)); - TreeUtils.printBinaryTree(root); - expected = new ArrayList<>(); - expected.add(Arrays.asList(5, 4, 11, 2)); - expected.add(Arrays.asList(5, 8, 4, 5)); - assertEquals(expected, solution1.pathSum(root, sum)); - } -} diff --git a/src/test/java/com/fishercoder/_114Test.java b/src/test/java/com/fishercoder/_114Test.java deleted file mode 100644 index b9b3a5ecd9..0000000000 --- a/src/test/java/com/fishercoder/_114Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._114; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -public class _114Test { - private static _114.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _114.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 5, 3, 4, null, 6)); - TreeUtils.printBinaryTree(root); - solution1.flatten(root); - TreeUtils.printBinaryTree(root); - } -} diff --git a/src/test/java/com/fishercoder/_1150Test.java b/src/test/java/com/fishercoder/_1150Test.java deleted file mode 100644 index 02dc43476c..0000000000 --- a/src/test/java/com/fishercoder/_1150Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1150; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1150Test { - private static _1150.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _1150.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{2, 4, 5, 5, 5, 5, 5, 6, 6}; - assertEquals(true, solution1.isMajorityElement(nums, 5)); - } - - @Test - public void test2() { - nums = new int[]{10, 100, 101, 101}; - assertEquals(false, solution1.isMajorityElement(nums, 101)); - } - - @Test - public void test3() { - nums = new int[]{1, 1, 1, 2, 3, 3, 3}; - assertEquals(false, solution1.isMajorityElement(nums, 2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1154Test.java b/src/test/java/com/fishercoder/_1154Test.java deleted file mode 100644 index cf8383c215..0000000000 --- a/src/test/java/com/fishercoder/_1154Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1154; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1154Test { - private static _1154.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1154.Solution1(); - } - - @Test - public void test1() { - assertEquals(9, solution1.dayOfYear("2019-01-09")); - } - - @Test - public void test2() { - assertEquals(271, solution1.dayOfYear("1969-09-28")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_115Test.java b/src/test/java/com/fishercoder/_115Test.java deleted file mode 100644 index 1dbecc1a3d..0000000000 --- a/src/test/java/com/fishercoder/_115Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._115; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _115Test { - private static _115.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _115.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.numDistinct("rabbbit", "rabbit")); - } -} diff --git a/src/test/java/com/fishercoder/_1160Test.java b/src/test/java/com/fishercoder/_1160Test.java deleted file mode 100644 index 2da7ff63e9..0000000000 --- a/src/test/java/com/fishercoder/_1160Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1160; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1160Test { - private static _1160.Solution1 solution1; - private static String[] words; - - @BeforeClass - public static void setup() { - solution1 = new _1160.Solution1(); - } - - @Test - public void test1() { - words = new String[]{"cat", "bt", "hat", "tree"}; - assertEquals(6, solution1.countCharacters(words, "atach")); - } - - @Test - public void test2() { - words = new String[]{"hello", "world", "leetcode"}; - assertEquals(10, solution1.countCharacters(words, "welldonehoneyr")); - } -} diff --git a/src/test/java/com/fishercoder/_1161Test.java b/src/test/java/com/fishercoder/_1161Test.java deleted file mode 100644 index 243ed121d7..0000000000 --- a/src/test/java/com/fishercoder/_1161Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1161; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; - -public class _1161Test { - private static _1161.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1161.Solution1(); - } - - @Test - public void test1() { - TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 7, 0, 7, -8, null, null)); - assertEquals(2, solution1.maxLevelSum(root)); - } -} diff --git a/src/test/java/com/fishercoder/_1165Test.java b/src/test/java/com/fishercoder/_1165Test.java deleted file mode 100644 index 78eb56926f..0000000000 --- a/src/test/java/com/fishercoder/_1165Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1165; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1165Test { - private static _1165.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1165.Solution1(); - } - - @Test - public void test1() { - assertEquals(4, solution1.calculateTime("abcdefghijklmnopqrstuvwxyz", "cba")); - } - - @Test - public void test2() { - assertEquals(73, solution1.calculateTime("pqrstuvwxyzabcdefghijklmno", "leetcode")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_116Test.java b/src/test/java/com/fishercoder/_116Test.java deleted file mode 100644 index 4e040a46fa..0000000000 --- a/src/test/java/com/fishercoder/_116Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeLinkNode; -import com.fishercoder.solutions._116; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _116Test { - private static _116.Solution1 solution1; - private static TreeLinkNode root; - - @BeforeClass - public static void setup() { - solution1 = new _116.Solution1(); - } - - @Test - public void test1() { - root = new TreeLinkNode(1); - root.left = new TreeLinkNode(2); - root.right = new TreeLinkNode(3); - root.left.left = new TreeLinkNode(4); - root.left.right = new TreeLinkNode(5); - root.right.right = new TreeLinkNode(7); - - solution1.connect(root); - } -} diff --git a/src/test/java/com/fishercoder/_1170Test.java b/src/test/java/com/fishercoder/_1170Test.java deleted file mode 100644 index 630ebd870e..0000000000 --- a/src/test/java/com/fishercoder/_1170Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1170; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1170Test { - private static _1170.Solution1 solution1; - private static _1170.Solution2 solution2; - private static String[] queries; - private static String[] words; - - @BeforeClass - public static void setup() { - solution1 = new _1170.Solution1(); - solution2 = new _1170.Solution2(); - } - - @Test - public void test1() { - queries = new String[]{"cbd"}; - words = new String[]{"zaaaz"}; - assertArrayEquals(new int[]{1}, solution1.numSmallerByFrequency(queries, words)); - } - - @Test - public void test2() { - queries = new String[]{"bbb", "cc"}; - words = new String[]{"a", "aa", "aaa", "aaaa"}; - assertArrayEquals(new int[]{1, 2}, solution1.numSmallerByFrequency(queries, words)); - } - - @Test - public void test3() { - queries = new String[]{"cbd"}; - words = new String[]{"zaaaz"}; - assertArrayEquals(new int[]{1}, solution2.numSmallerByFrequency(queries, words)); - } - - @Test - public void test4() { - queries = new String[]{"bbb", "cc"}; - words = new String[]{"a", "aa", "aaa", "aaaa"}; - assertArrayEquals(new int[]{1, 2}, solution2.numSmallerByFrequency(queries, words)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1175Test.java b/src/test/java/com/fishercoder/_1175Test.java deleted file mode 100644 index be200f0a02..0000000000 --- a/src/test/java/com/fishercoder/_1175Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1175; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -@Ignore -public class _1175Test { - private static _1175.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1175.Solution1(); - } - - @Test - public void test1() { - assertEquals(12, solution1.numPrimeArrangements(5)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1176Test.java b/src/test/java/com/fishercoder/_1176Test.java deleted file mode 100644 index 5d01d87aee..0000000000 --- a/src/test/java/com/fishercoder/_1176Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1176; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _1176Test { - private static _1176.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1176.Solution1(); - } - - @Test - public void test1() { - assertEquals(0, solution1.dietPlanPerformance(new int[]{1, 2, 3, 4, 5}, 1, 3, 3)); - } - - @Test - public void test2() { - assertEquals(1, solution1.dietPlanPerformance(new int[]{3, 2}, 2, 0, 1)); - } - - @Test - public void test3() { - assertEquals(0, solution1.dietPlanPerformance(new int[]{6, 5, 0, 0}, 2, 1, 5)); - } -} diff --git a/src/test/java/com/fishercoder/_117Test.java b/src/test/java/com/fishercoder/_117Test.java deleted file mode 100644 index ad3ce73e2a..0000000000 --- a/src/test/java/com/fishercoder/_117Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeLinkNode; -import com.fishercoder.solutions._117; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _117Test { - private static _117.Solution1 solution1; - private static TreeLinkNode root; - - @BeforeClass - public static void setup() { - solution1 = new _117.Solution1(); - } - - @Test - public void test1() { - root = new TreeLinkNode(1); - root.left = new TreeLinkNode(2); - root.right = new TreeLinkNode(3); - root.left.left = new TreeLinkNode(4); - root.left.right = new TreeLinkNode(5); - root.right.right = new TreeLinkNode(7); - - solution1.connect(root); - } -} diff --git a/src/test/java/com/fishercoder/_1180Test.java b/src/test/java/com/fishercoder/_1180Test.java deleted file mode 100644 index 3e712cf223..0000000000 --- a/src/test/java/com/fishercoder/_1180Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1180; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1180Test { - private static _1180.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1180.Solution1(); - } - - @Test - public void test1() { - assertEquals(8, solution1.countLetters("aaaba")); - } - - @Test - public void test2() { - assertEquals(55, solution1.countLetters("aaaaaaaaaa")); - } -} diff --git a/src/test/java/com/fishercoder/_1182Test.java b/src/test/java/com/fishercoder/_1182Test.java deleted file mode 100644 index c20fb197c4..0000000000 --- a/src/test/java/com/fishercoder/_1182Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1182; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1182Test { - private static _1182.Solution1 solution1; - private static int[] colors; - private static int[][] queries; - - @BeforeClass - public static void setup() { - solution1 = new _1182.Solution1(); - } - - @Test - public void test1() { - colors = new int[]{1, 1, 2, 1, 3, 2, 2, 3, 3}; - queries = new int[][]{ - {1, 3}, - {2, 2}, - {6, 1} - }; - assertEquals(Arrays.asList(3, 0, 3), solution1.shortestDistanceColor(colors, queries)); - } - - @Test - public void test2() { - colors = new int[]{1, 2}; - queries = new int[][]{ - {0, 3} - }; - assertEquals(Arrays.asList(-1), solution1.shortestDistanceColor(colors, queries)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1184Test.java b/src/test/java/com/fishercoder/_1184Test.java deleted file mode 100644 index bf74323e89..0000000000 --- a/src/test/java/com/fishercoder/_1184Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1184; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1184Test { - private static _1184.Solution1 solution1; - private static int[] distance; - - @BeforeClass - public static void setup() { - solution1 = new _1184.Solution1(); - } - - @Test - public void test1() { - distance = new int[]{1, 2, 3, 4}; - assertEquals(1, solution1.distanceBetweenBusStops(distance, 0, 1)); - } - - @Test - public void test2() { - distance = new int[]{1, 2, 3, 4}; - assertEquals(4, solution1.distanceBetweenBusStops(distance, 0, 3)); - } - - @Test - public void test3() { - distance = new int[]{1, 2, 3, 4}; - assertEquals(3, solution1.distanceBetweenBusStops(distance, 0, 2)); - } - - @Test - public void test4() { - distance = new int[]{7,10,1,12,11,14,5,0}; - assertEquals(17, solution1.distanceBetweenBusStops(distance, 7, 2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1185Test.java b/src/test/java/com/fishercoder/_1185Test.java deleted file mode 100644 index b04a2f3a32..0000000000 --- a/src/test/java/com/fishercoder/_1185Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1185; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1185Test { - private static _1185.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1185.Solution1(); - } - - @Test - public void test1() { - assertEquals("Saturday", solution1.dayOfTheWeek(3, 8, 2019)); - } - - @Test - public void test2() { - assertEquals("Sunday", solution1.dayOfTheWeek(18, 7, 1999)); - } - - @Test - public void test3() { - assertEquals("Sunday", solution1.dayOfTheWeek(15, 8, 1993)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1189Test.java b/src/test/java/com/fishercoder/_1189Test.java deleted file mode 100644 index e67cbe61a4..0000000000 --- a/src/test/java/com/fishercoder/_1189Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1189; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1189Test { - private static _1189.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1189.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.maxNumberOfBalloons("nlaebolko")); - } - - @Test - public void test2() { - assertEquals(2, solution1.maxNumberOfBalloons("loonbalxballpoon")); - } - - @Test - public void test3() { - assertEquals(0, solution1.maxNumberOfBalloons("leetcode")); - } - -} diff --git a/src/test/java/com/fishercoder/_118Test.java b/src/test/java/com/fishercoder/_118Test.java deleted file mode 100644 index 5d1dfa7b0d..0000000000 --- a/src/test/java/com/fishercoder/_118Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._118; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _118Test { - private static _118.Solution1 solution1; - private static _118.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _118.Solution1(); - solution2 = new _118.Solution2(); - } - - @Test - public void test1() { - CommonUtils.printListList(solution1.generate(5)); - } - - @Test - public void test2() { - CommonUtils.printListList(solution2.generate(5)); - } - -} diff --git a/src/test/java/com/fishercoder/_1190Test.java b/src/test/java/com/fishercoder/_1190Test.java deleted file mode 100644 index 38a4a9b706..0000000000 --- a/src/test/java/com/fishercoder/_1190Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1190; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1190Test { - private static _1190.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1190.Solution1(); - } - - @Test - public void test1() { - assertEquals("dcba", solution1.reverseParentheses("(abcd)")); - } - - @Test - public void test2() { - assertEquals("iloveu", solution1.reverseParentheses("(u(love)i)")); - } - - @Test - public void test3() { - assertEquals("leetcode", solution1.reverseParentheses("(ed(et(oc))el)")); - } - - @Test - public void test4() { - assertEquals("apmnolkjihgfedcbq", solution1.reverseParentheses("a(bcdefghijkl(mno)p)q")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1196Test.java b/src/test/java/com/fishercoder/_1196Test.java deleted file mode 100644 index 9d1a4ee805..0000000000 --- a/src/test/java/com/fishercoder/_1196Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1196; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1196Test { - private static _1196.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1196.Solution1(); - } - - @Test - public void test1() { - assertEquals(4, solution1.maxNumberOfApples(new int[]{100, 200, 150, 1000})); - } - - @Test - public void test2() { - assertEquals(5, solution1.maxNumberOfApples(new int[]{900, 950, 800, 1000, 700, 800})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1198Test.java b/src/test/java/com/fishercoder/_1198Test.java deleted file mode 100644 index 39cbb1849b..0000000000 --- a/src/test/java/com/fishercoder/_1198Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1198; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1198Test { - private static _1198.Solution1 solution1; - private static int[][] mat; - - @BeforeClass - public static void setup() { - solution1 = new _1198.Solution1(); - } - - @Test - public void test1() { - mat = new int[][]{ - {1, 2, 3, 4, 5}, - {2, 4, 5, 8, 10}, - {3, 5, 7, 9, 11}, - {1, 3, 5, 7, 9} - }; - assertEquals(5, solution1.smallestCommonElement(mat)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_119Test.java b/src/test/java/com/fishercoder/_119Test.java deleted file mode 100644 index d67775c707..0000000000 --- a/src/test/java/com/fishercoder/_119Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._119; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _119Test { - private static _119.Solution1 solution1; - private static _119.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _119.Solution1(); - solution2 = new _119.Solution2(); - } - - @Test - public void test1() { - assertEquals(Arrays.asList(1, 3, 3, 1), solution1.getRow(3)); - assertEquals(Arrays.asList(1, 3, 3, 1), solution2.getRow(3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_11Test.java b/src/test/java/com/fishercoder/_11Test.java deleted file mode 100644 index 4256f29ce1..0000000000 --- a/src/test/java/com/fishercoder/_11Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._11; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _11Test { - private static _11.Solution1 solution1; - private static int[] height; - - @BeforeClass - public static void setup() { - solution1 = new _11.Solution1(); - } - - @Test - public void test1() { - height = new int[]{1, 1}; - assertEquals(1, solution1.maxArea(height)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1200Test.java b/src/test/java/com/fishercoder/_1200Test.java deleted file mode 100644 index f71e76b6fc..0000000000 --- a/src/test/java/com/fishercoder/_1200Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1200; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _1200Test { - private static _1200.Solution1 solution1; - private static int[] arr; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _1200.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{4, 2, 1, 3}; - expected = new ArrayList<>(); - expected.add(Arrays.asList(1, 2)); - expected.add(Arrays.asList(2, 3)); - expected.add(Arrays.asList(3, 4)); - assertEquals(expected, solution1.minimumAbsDifference(arr)); - } - - @Test - public void test2() { - arr = new int[]{40, 11, 26, 27, -20}; - expected = new ArrayList<>(); - expected.add(Arrays.asList(26, 27)); - assertEquals(expected, solution1.minimumAbsDifference(arr)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1207Test.java b/src/test/java/com/fishercoder/_1207Test.java deleted file mode 100644 index 8b3e326fac..0000000000 --- a/src/test/java/com/fishercoder/_1207Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1207; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1207Test { - private static _1207.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1207.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{1, 2, 2, 1, 1, 3}; - assertEquals(true, solution1.uniqueOccurrences(arr)); - } - - @Test - public void test2() { - arr = new int[]{1, 2}; - assertEquals(false, solution1.uniqueOccurrences(arr)); - } - - @Test - public void test3() { - arr = new int[]{-3, 0, 1, -3, 1, 1, 1, -3, 10, 0}; - assertEquals(true, solution1.uniqueOccurrences(arr)); - } -} diff --git a/src/test/java/com/fishercoder/_1209Test.java b/src/test/java/com/fishercoder/_1209Test.java deleted file mode 100644 index 8ca60ac585..0000000000 --- a/src/test/java/com/fishercoder/_1209Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1209; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1209Test { - - private static _1209.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1209.Solution1(); - } - - @Test - public void test1() { - assertEquals("abcd", solution1.removeDuplicates("abcd", 2)); - } - - @Test - public void test2() { - assertEquals("aa", solution1.removeDuplicates("deeedbbcccbdaa", 3)); - } - - @Test - public void test3() { - assertEquals("ps", solution1.removeDuplicates("pbbcggttciiippooaais", 2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_120Test.java b/src/test/java/com/fishercoder/_120Test.java deleted file mode 100644 index 9b74e524db..0000000000 --- a/src/test/java/com/fishercoder/_120Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._120; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _120Test { - private static _120.Solution1 solution1; - private static List> triangle; - - @BeforeClass - public static void setup() { - solution1 = new _120.Solution1(); - } - - @Test - public void test1() { - triangle = new ArrayList(); - triangle.add(Arrays.asList(1)); - triangle.add(Arrays.asList(2, 3)); - assertEquals(3, solution1.minimumTotal(triangle)); - } -} diff --git a/src/test/java/com/fishercoder/_1213Test.java b/src/test/java/com/fishercoder/_1213Test.java deleted file mode 100644 index 3a9509ab0a..0000000000 --- a/src/test/java/com/fishercoder/_1213Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._1213; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _1213Test { - private static _1213.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1213.Solution1(); - } - - @Test - public void test1() { - CommonUtils.printList(solution1.arraysIntersection(new int[]{1, 2, 3, 4, 5}, new int[]{1, 2, 5, 7, 9}, new int[]{1, 3, 4, 5, 8})); - } - -} diff --git a/src/test/java/com/fishercoder/_1217Test.java b/src/test/java/com/fishercoder/_1217Test.java deleted file mode 100644 index b0b08cbbc7..0000000000 --- a/src/test/java/com/fishercoder/_1217Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1217; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1217Test { - private static _1217.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1217.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.minCostToMoveChips(new int[]{1, 2, 3})); - } - - @Test - public void test2() { - assertEquals(2, solution1.minCostToMoveChips(new int[]{2, 2, 2, 3, 3})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1219Test.java b/src/test/java/com/fishercoder/_1219Test.java deleted file mode 100644 index 719c758ac2..0000000000 --- a/src/test/java/com/fishercoder/_1219Test.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1219; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _1219Test { - private static _1219.Solution1 solution1; - private static int[][] grid; - - @BeforeClass - public static void setup() { - solution1 = new _1219.Solution1(); - } - - @Test - public void test1() { - grid = new int[][]{ - {0, 6, 0}, - {5, 8, 7}, - {0, 9, 0}, - }; - assertEquals(24, solution1.getMaximumGold(grid)); - } - - @Test - public void test2() { - grid = new int[][]{ - {1, 0, 7}, - {2, 0, 6}, - {3, 4, 5}, - {0, 3, 0}, - {9, 0, 20}, - }; - assertEquals(28, solution1.getMaximumGold(grid)); - } - - @Test - public void test3() { - grid = new int[][]{ - {0, 0, 19, 5, 8}, - {11, 20, 14, 1, 0}, - {0, 0, 1, 1, 1}, - {0, 2, 0, 2, 0}, - }; - assertEquals(77, solution1.getMaximumGold(grid)); - } -} diff --git a/src/test/java/com/fishercoder/_121Test.java b/src/test/java/com/fishercoder/_121Test.java deleted file mode 100644 index 960d66ee1b..0000000000 --- a/src/test/java/com/fishercoder/_121Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._121; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _121Test { - private static _121.Solution1 solution1; - private static int[] prices; - - @BeforeClass - public static void setup() { - solution1 = new _121.Solution1(); - } - - @Test - public void test1() { - prices = new int[] {7, 1, 5, 3, 6, 4}; - assertEquals(5, solution1.maxProfit(prices)); - } - - @Test - public void test2() { - prices = new int[] {7, 6, 4, 3, 1}; - assertEquals(0, solution1.maxProfit(prices)); - } - - @Test - public void test3() { - prices = new int[] {2, 4, 1}; - assertEquals(2, solution1.maxProfit(prices)); - } - - @Test - public void test4() { - prices = new int[] {1, 2}; - assertEquals(1, solution1.maxProfit(prices)); - } -} diff --git a/src/test/java/com/fishercoder/_1221Test.java b/src/test/java/com/fishercoder/_1221Test.java deleted file mode 100644 index 87463fdda5..0000000000 --- a/src/test/java/com/fishercoder/_1221Test.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1221; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1221Test { - private static _1221.Solution1 solution1; - private static _1221.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _1221.Solution1(); - solution2 = new _1221.Solution2(); - } - - @Test - public void test1() { - assertEquals(4, solution1.balancedStringSplit("RLRRLLRLRL")); - assertEquals(4, solution2.balancedStringSplit("RLRRLLRLRL")); - } - - @Test - public void test2() { - assertEquals(3, solution1.balancedStringSplit("RLLLLRRRLR")); - assertEquals(3, solution2.balancedStringSplit("RLLLLRRRLR")); - } - - @Test - public void test3() { - assertEquals(1, solution1.balancedStringSplit("LLLLRRRR")); - assertEquals(1, solution2.balancedStringSplit("LLLLRRRR")); - } - - @Test - public void test4() { - assertEquals(2, solution1.balancedStringSplit("RLRRRLLRLL")); - assertEquals(2, solution2.balancedStringSplit("RLRRRLLRLL")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1228Test.java b/src/test/java/com/fishercoder/_1228Test.java deleted file mode 100644 index 9b64f6358a..0000000000 --- a/src/test/java/com/fishercoder/_1228Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1228; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1228Test { - private static _1228.Solution1 solution1; - private static _1228.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _1228.Solution1(); - solution2 = new _1228.Solution2(); - } - - @Test - public void test1() { - assertEquals(9, solution1.missingNumber(new int[]{5, 7, 11, 13})); - assertEquals(9, solution2.missingNumber(new int[]{5, 7, 11, 13})); - } - - @Test - public void test2() { - assertEquals(14, solution1.missingNumber(new int[]{15, 13, 12})); - assertEquals(14, solution2.missingNumber(new int[]{15, 13, 12})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_122Test.java b/src/test/java/com/fishercoder/_122Test.java deleted file mode 100644 index 1858ba8608..0000000000 --- a/src/test/java/com/fishercoder/_122Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._122; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _122Test { - private static _122.Solution1 solution1; - private static _122.Solution2 solution2; - private static int[] prices; - - @BeforeClass - public static void setup() { - solution1 = new _122.Solution1(); - solution2 = new _122.Solution2(); - } - - @Test - public void test1() { - prices = new int[] {1, 2, 4}; - assertEquals(3, solution1.maxProfit(prices)); - assertEquals(3, solution2.maxProfit(prices)); - } -} diff --git a/src/test/java/com/fishercoder/_1232Test.java b/src/test/java/com/fishercoder/_1232Test.java deleted file mode 100644 index cd87d64b86..0000000000 --- a/src/test/java/com/fishercoder/_1232Test.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1232; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1232Test { - private static _1232.Solution1 solution1; - private static int[][] coordinates; - - @BeforeClass - public static void setup() { - solution1 = new _1232.Solution1(); - } - - @Test - public void test1() { - coordinates = new int[][]{ - {1, 2}, - {2, 3}, - {3, 4}, - {4, 5}, - {5, 6}, - {6, 7} - }; - assertEquals(true, solution1.checkStraightLine(coordinates)); - } - - @Test - public void test2() { - coordinates = new int[][]{ - {1, 1}, - {2, 2}, - {3, 4}, - {4, 5}, - {5, 6}, - {7, 7} - }; - assertEquals(false, solution1.checkStraightLine(coordinates)); - } - - @Test - public void test3() { - coordinates = new int[][]{ - {-3, -2}, - {-1, -2}, - {2, -2}, - {-2, -2}, - {0, -2} - }; - assertEquals(true, solution1.checkStraightLine(coordinates)); - } - - @Test - public void test4() { - coordinates = new int[][]{ - {0, 1}, - {1, 3}, - {-4, -7}, - {5, 11} - }; - assertEquals(true, solution1.checkStraightLine(coordinates)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_123Test.java b/src/test/java/com/fishercoder/_123Test.java deleted file mode 100644 index bfd9f7a887..0000000000 --- a/src/test/java/com/fishercoder/_123Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._123; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _123Test { - private static _123.Solution1 solution1; - private static int[] prices; - - @BeforeClass - public static void setup() { - solution1 = new _123.Solution1(); - } - - @Test - public void test1() { - prices = new int[] {1}; - assertEquals(0, solution1.maxProfit(prices)); - } -} diff --git a/src/test/java/com/fishercoder/_1243Test.java b/src/test/java/com/fishercoder/_1243Test.java deleted file mode 100644 index 1475434726..0000000000 --- a/src/test/java/com/fishercoder/_1243Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1243; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.TestCase.assertTrue; - -public class _1243Test { - private static _1243.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1243.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{6, 2, 3, 4}; - assertTrue(solution1.transformArray(arr).equals(Arrays.asList(6, 3, 3, 4))); - } - - @Test - public void test2() { - arr = new int[]{1, 6, 3, 4, 3, 5}; - assertTrue(solution1.transformArray(arr).equals(Arrays.asList(1, 4, 4, 4, 4, 5))); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1249Test.java b/src/test/java/com/fishercoder/_1249Test.java deleted file mode 100644 index c5fdad28a7..0000000000 --- a/src/test/java/com/fishercoder/_1249Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1249; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _1249Test { - private static _1249.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1249.Solution1(); - } - - @Test - public void test1() { - System.out.println(solution1.minRemoveToMakeValid("lee(t(c)o)de)")); - } - - @Test - public void test2() { - System.out.println(solution1.minRemoveToMakeValid("a)b(c)d")); - } - - @Test - public void test3() { - System.out.println(solution1.minRemoveToMakeValid("))((")); - } - - @Test - public void test4() { - System.out.println(solution1.minRemoveToMakeValid("(a(b(c)d)")); - } - - @Test - public void test5() { - System.out.println(solution1.minRemoveToMakeValid("())()((("));//should be "()()" - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1252Test.java b/src/test/java/com/fishercoder/_1252Test.java deleted file mode 100644 index 43cf2e4c15..0000000000 --- a/src/test/java/com/fishercoder/_1252Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1252; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1252Test { - private static _1252.Solution1 solution1; - private static _1252.Solution2 solution2; - private static int[][] indices; - - @BeforeClass - public static void setup() { - solution1 = new _1252.Solution1(); - solution2 = new _1252.Solution2(); - } - - @Test - public void test1() { - indices = new int[][]{ - {0, 1}, - {1, 1} - }; - assertEquals(6, solution1.oddCells(2, 3, indices)); - } - - @Test - public void test2() { - indices = new int[][]{ - {1, 1}, - {0, 0} - }; - assertEquals(0, solution1.oddCells(2, 2, indices)); - } - - @Test - public void test3() { - indices = new int[][]{ - {0, 1}, - {1, 1} - }; - assertEquals(6, solution2.oddCells(2, 3, indices)); - } - - @Test - public void test4() { - indices = new int[][]{ - {1, 1}, - {0, 0} - }; - assertEquals(0, solution2.oddCells(2, 2, indices)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1258Test.java b/src/test/java/com/fishercoder/_1258Test.java deleted file mode 100644 index fc07784311..0000000000 --- a/src/test/java/com/fishercoder/_1258Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1258; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _1258Test { - - private static _1258.Solution1 solution1; - private static List> synonyms; - - @BeforeClass - public static void setup() { - solution1 = new _1258.Solution1(); - } - - @Test - public void test1() { - synonyms = Arrays.asList(Arrays.asList("happy", "joy"), Arrays.asList("sad", "sorrow"), Arrays.asList("joy", "cheerful")); - List expected = Arrays.asList("I am cheerful today but was sad yesterday", "I am cheerful today but was sorrow yesterday", "I am happy today but was sad yesterday", "I am happy today but was sorrow yesterday", - "I am joy today but was sad yesterday", "I am joy today but was sorrow yesterday"); - assertEquals(expected, solution1.generateSentences(synonyms, "I am happy today but was sad yesterday")); - } - -} diff --git a/src/test/java/com/fishercoder/_1260Test.java b/src/test/java/com/fishercoder/_1260Test.java deleted file mode 100644 index 371de6d69d..0000000000 --- a/src/test/java/com/fishercoder/_1260Test.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1260; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _1260Test { - private static _1260.Solution1 solution1; - private static int[][] grid; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _1260.Solution1(); - } - - @Test - public void test1() { - grid = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9} - }; - expected = Arrays.asList( - Arrays.asList(9, 1, 2), - Arrays.asList(3, 4, 5), - Arrays.asList(6, 7, 8) - ); - assertEquals(expected, solution1.shiftGrid(grid, 1)); - } - - @Test - public void test2() { - grid = new int[][]{ - {1}, - {2}, - {3}, - {4}, - {7}, - {6}, - {5} - }; - expected = Arrays.asList( - Arrays.asList(6), - Arrays.asList(5), - Arrays.asList(1), - Arrays.asList(2), - Arrays.asList(3), - Arrays.asList(4), - Arrays.asList(7) - ); - assertEquals(expected, solution1.shiftGrid(grid, 23)); - } - - @Test - public void test3() { - grid = new int[][]{ - {3, 8, 1, 9}, - {19, 7, 2, 5}, - {4, 6, 11, 10}, - {12, 0, 21, 13} - }; - expected = Arrays.asList( - Arrays.asList(12, 0, 21, 13), - Arrays.asList(3, 8, 1, 9), - Arrays.asList(19, 7, 2, 5), - Arrays.asList(4, 6, 11, 10) - ); - assertEquals(expected, solution1.shiftGrid(grid, 4)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1266Test.java b/src/test/java/com/fishercoder/_1266Test.java deleted file mode 100644 index 7365634a46..0000000000 --- a/src/test/java/com/fishercoder/_1266Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1266; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1266Test { - private static _1266.Solution1 solution1; - private static int[][] points; - - @BeforeClass - public static void setup() { - solution1 = new _1266.Solution1(); - } - - @Test - public void test1() { - points = new int[][]{ - {1, 1}, - {3, 4}, - {-1, 0} - }; - assertEquals(7, solution1.minTimeToVisitAllPoints(points)); - } - - @Test - public void test2() { - points = new int[][]{ - {3, 2}, - {-2, 2} - }; - assertEquals(5, solution1.minTimeToVisitAllPoints(points)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1267Test.java b/src/test/java/com/fishercoder/_1267Test.java deleted file mode 100644 index fa768c599b..0000000000 --- a/src/test/java/com/fishercoder/_1267Test.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1267; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1267Test { - private static _1267.Solution1 solution1; - private static int[][] grid; - - @BeforeClass - public static void setup() { - solution1 = new _1267.Solution1(); - } - - @Test - public void test1() { - grid = new int[][]{ - {1, 0}, - {0, 1} - }; - assertEquals(0, solution1.countServers(grid)); - } - - @Test - public void test2() { - grid = new int[][]{ - {1, 0}, - {1, 1} - }; - assertEquals(3, solution1.countServers(grid)); - } - - @Test - public void test3() { - grid = new int[][]{ - {1, 1, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1} - }; - assertEquals(4, solution1.countServers(grid)); - } - - @Test - public void test4() { - grid = new int[][]{ - {1, 1, 0, 0}, - {1, 1, 1, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1} - }; - assertEquals(6, solution1.countServers(grid)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1271Test.java b/src/test/java/com/fishercoder/_1271Test.java deleted file mode 100644 index 9deaf2122f..0000000000 --- a/src/test/java/com/fishercoder/_1271Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1271; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1271Test { - private static _1271.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1271.Solution1(); - } - - @Test - public void test1() { - assertEquals("IOI", solution1.toHexspeak("257")); - } - - @Test - public void test2() { - assertEquals("ERROR", solution1.toHexspeak("3")); - } - - @Test - public void test3() { - assertEquals("ERROR", solution1.toHexspeak("619879596177")); - } - - @Test - public void test4() { - assertEquals("AEIDBCDIBC", solution1.toHexspeak("747823223228")); - } - -} diff --git a/src/test/java/com/fishercoder/_1273Test.java b/src/test/java/com/fishercoder/_1273Test.java deleted file mode 100644 index 318d8d7841..0000000000 --- a/src/test/java/com/fishercoder/_1273Test.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1273; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1273Test { - private static _1273.Solution1 solution1; - private static int[] parent; - private static int[] value; - - @BeforeClass - public static void setup() { - solution1 = new _1273.Solution1(); - } - - @Test - public void test1() { - parent = new int[]{-1, 0, 0, 1, 2, 2, 2}; - value = new int[]{1, -2, 4, 0, -2, -1, -1}; - assertEquals(2, solution1.deleteTreeNodes(7, parent, value)); - } - - @Test - public void test2() { - parent = new int[]{-1, 0, 0, 1, 2, 2, 2}; - value = new int[]{1, -2, 3, 0, -2, -1, 0}; - assertEquals(2, solution1.deleteTreeNodes(7, parent, value)); - } - - @Test - public void test3() { - parent = new int[]{-1, 0, 0, 1, 2, 2, 2}; - value = new int[]{1, -2, 4, 0, -2, -1, -2}; - assertEquals(6, solution1.deleteTreeNodes(7, parent, value)); - } - - @Test - public void test4() { - parent = new int[]{-1, 0, 0, 1, 2, 2, 2}; - value = new int[]{3, -2, 4, 0, -2, -1, -2}; - assertEquals(0, solution1.deleteTreeNodes(7, parent, value)); - } - -} diff --git a/src/test/java/com/fishercoder/_1275Test.java b/src/test/java/com/fishercoder/_1275Test.java deleted file mode 100644 index 3fcb37a905..0000000000 --- a/src/test/java/com/fishercoder/_1275Test.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1275; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1275Test { - private static _1275.Solution1 solution1; - private static int[][] moves; - - @BeforeClass - public static void setup() { - solution1 = new _1275.Solution1(); - } - - @Before - public void clear() { - solution1 = new _1275.Solution1(); - } - - @Test - public void test1() { - moves = new int[][]{ - {0, 0}, - {2, 0}, - {1, 1}, - {2, 1}, - {2, 2}, - }; - assertEquals("A", solution1.tictactoe(moves)); - } - - @Test - public void test2() { - moves = new int[][]{ - {0, 0}, - {1, 1}, - {0, 1}, - {0, 2}, - {1, 0}, - {2, 0}, - }; - assertEquals("B", solution1.tictactoe(moves)); - } - - @Test - public void test3() { - moves = new int[][]{ - {0, 0}, - {1, 1}, - {2, 0}, - {1, 0}, - {1, 2}, - {2, 1}, - {0, 1}, - {0, 2}, - {2, 2}, - }; - assertEquals("Draw", solution1.tictactoe(moves)); - } - - @Test - public void test4() { - moves = new int[][]{ - {0, 0}, - {1, 1}, - }; - assertEquals("Pending", solution1.tictactoe(moves)); - } -} diff --git a/src/test/java/com/fishercoder/_1277Test.java b/src/test/java/com/fishercoder/_1277Test.java deleted file mode 100644 index 1a52b05219..0000000000 --- a/src/test/java/com/fishercoder/_1277Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1277; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1277Test { - private static _1277.Solution1 solution1; - private static _1277.Solution2 solution2; - private static int[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _1277.Solution1(); - solution2 = new _1277.Solution2(); - } - - @Test - public void test1() { - matrix = new int[][]{ - {0, 1, 1, 1}, - {1, 1, 1, 1}, - {0, 1, 1, 1} - }; - assertEquals(15, solution1.countSquares(matrix)); - } - - @Test - public void test2() { - matrix = new int[][]{ - {0, 1, 1, 1}, - {1, 1, 1, 1}, - {0, 1, 1, 1} - }; - assertEquals(15, solution2.countSquares(matrix)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_127Test.java b/src/test/java/com/fishercoder/_127Test.java deleted file mode 100644 index f2b6031e5d..0000000000 --- a/src/test/java/com/fishercoder/_127Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._127; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _127Test { - private static _127.Solution1 solution1; - private static List wordList; - - @BeforeClass - public static void setup() { - solution1 = new _127.Solution1(); - } - - @Test - public void test1() { - wordList = new ArrayList<>(Arrays.asList("hot", "dot", "dog", "lot", "log")); - assertEquals(0, solution1.ladderLength("hit", "cog", wordList)); - } - - @Test - public void test2() { - wordList = new ArrayList<>(Arrays.asList("hot", "dot", "dog", "lot", "log", "cog")); - assertEquals(5, solution1.ladderLength("hit", "cog", wordList)); - } -} diff --git a/src/test/java/com/fishercoder/_1281Test.java b/src/test/java/com/fishercoder/_1281Test.java deleted file mode 100644 index 74b46a4023..0000000000 --- a/src/test/java/com/fishercoder/_1281Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1281; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1281Test { - private static _1281.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1281.Solution1(); - } - - @Test - public void test1() { - assertEquals(15, solution1.subtractProductAndSum(234)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1282Test.java b/src/test/java/com/fishercoder/_1282Test.java deleted file mode 100644 index 7834c0b67d..0000000000 --- a/src/test/java/com/fishercoder/_1282Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._1282; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _1282Test { - private static _1282.Solution1 solution1; - private static int[] groupSizes; - - @BeforeClass - public static void setup() { - solution1 = new _1282.Solution1(); - } - - @Test - public void test1() { - groupSizes = new int[]{3, 3, 3, 3, 3, 1, 3}; - CommonUtils.printListList(solution1.groupThePeople(groupSizes)); - } - - @Test - public void test2() { - groupSizes = new int[]{2, 1, 3, 3, 3, 2}; - CommonUtils.printListList(solution1.groupThePeople(groupSizes)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1287Test.java b/src/test/java/com/fishercoder/_1287Test.java deleted file mode 100644 index 48d4231e10..0000000000 --- a/src/test/java/com/fishercoder/_1287Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1287; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1287Test { - private static _1287.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1287.Solution1(); - } - - @Test - public void test1() { - assertEquals(6, solution1.findSpecialInteger(new int[]{1, 2, 2, 6, 6, 6, 6, 7, 10})); - } - - @Test - public void test2() { - assertEquals(1, solution1.findSpecialInteger(new int[]{1})); - } - - @Test - public void test3() { - assertEquals(3, solution1.findSpecialInteger(new int[]{1, 2, 3, 3})); - } - -} diff --git a/src/test/java/com/fishercoder/_1289Test.java b/src/test/java/com/fishercoder/_1289Test.java deleted file mode 100644 index 2337333c27..0000000000 --- a/src/test/java/com/fishercoder/_1289Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1289; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1289Test { - private static _1289.Solution1 solution1; - private static int[][] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1289.Solution1(); - } - - @Test - public void test1() { - arr = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9} - }; - assertEquals(13, solution1.minFallingPathSum(arr)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1290Test.java b/src/test/java/com/fishercoder/_1290Test.java deleted file mode 100644 index 7023d9ec73..0000000000 --- a/src/test/java/com/fishercoder/_1290Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.solutions._1290; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.TestCase.assertEquals; - -public class _1290Test { - private static _1290.Solution1 solution1; - private static ListNode head; - - @BeforeClass - public static void setup() { - solution1 = new _1290.Solution1(); - } - - @Test - public void test1() { - head = ListNode.createSinglyLinkedList(Arrays.asList(1, 0, 1)); - assertEquals(5, solution1.getDecimalValue(head)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1291Test.java b/src/test/java/com/fishercoder/_1291Test.java deleted file mode 100644 index 04f0b34382..0000000000 --- a/src/test/java/com/fishercoder/_1291Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1291; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -@Ignore -public class _1291Test { - private static _1291.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1291.Solution1(); - } - - @Test - public void test1() { - assertEquals(Arrays.asList(), solution1.sequentialDigits(100, 300)); - } -} diff --git a/src/test/java/com/fishercoder/_1295Test.java b/src/test/java/com/fishercoder/_1295Test.java deleted file mode 100644 index 04cc087d15..0000000000 --- a/src/test/java/com/fishercoder/_1295Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1295; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1295Test { - private static _1295.Solution1 solution1; - private static _1295.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _1295.Solution1(); - solution2 = new _1295.Solution2(); - } - - @Test - public void test1() { - assertEquals(2, solution1.findNumbers(new int[]{12, 345, 2, 6, 7896})); - assertEquals(2, solution2.findNumbers(new int[]{12, 345, 2, 6, 7896})); - } - - @Test - public void test2() { - assertEquals(1, solution1.findNumbers(new int[]{555, 901, 482, 1771})); - assertEquals(1, solution2.findNumbers(new int[]{555, 901, 482, 1771})); - } - -} diff --git a/src/test/java/com/fishercoder/_1296Test.java b/src/test/java/com/fishercoder/_1296Test.java deleted file mode 100644 index d4a0ace94c..0000000000 --- a/src/test/java/com/fishercoder/_1296Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1296; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1296Test { - private static _1296.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1296.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isPossibleDivide(new int[]{1, 2, 3, 3, 4, 4, 5, 6}, 4)); - } - - @Test - public void test2() { - assertEquals(true, solution1.isPossibleDivide(new int[]{3, 2, 1, 2, 3, 4, 3, 4, 5, 9, 10, 11}, 3)); - } - - @Test - public void test3() { - assertEquals(true, solution1.isPossibleDivide(new int[]{3, 3, 2, 2, 1, 1}, 3)); - } - - @Test - public void test4() { - assertEquals(false, solution1.isPossibleDivide(new int[]{1, 2, 3, 4}, 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1297Test.java b/src/test/java/com/fishercoder/_1297Test.java deleted file mode 100644 index 7c87df1992..0000000000 --- a/src/test/java/com/fishercoder/_1297Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1297; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1297Test { - private static _1297.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1297.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.maxFreq("aababcaab", 2, 3, 4)); - } - - @Test - public void test2() { - assertEquals(2, solution1.maxFreq("aaaa", 1, 3, 3)); - } - - @Test - public void test3() { - assertEquals(3, solution1.maxFreq("aabcabcab", 2, 2, 3)); - } - - @Test - public void test4() { - assertEquals(0, solution1.maxFreq("abcde", 2, 3, 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1299Test.java b/src/test/java/com/fishercoder/_1299Test.java deleted file mode 100644 index f1d8eaec56..0000000000 --- a/src/test/java/com/fishercoder/_1299Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1299; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1299Test { - private static _1299.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1299.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{17, 18, 5, 4, 6, 1}; - assertArrayEquals(new int[]{18, 6, 6, 6, 1, -1}, solution1.replaceElements(arr)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_12Test.java b/src/test/java/com/fishercoder/_12Test.java deleted file mode 100644 index 483b7df9e0..0000000000 --- a/src/test/java/com/fishercoder/_12Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._12; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _12Test { - private static _12.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _12.Solution1(); - } - - @Test - public void test1() { - assertEquals("XII", solution1.intToRoman(12)); - } - - @Test - public void test2() { - assertEquals("M", solution1.intToRoman(1000)); - } - - @Test - public void test3() { - assertEquals("MMMCMXCIX", solution1.intToRoman(3999)); - } -} diff --git a/src/test/java/com/fishercoder/_1300Test.java b/src/test/java/com/fishercoder/_1300Test.java deleted file mode 100644 index ea60962eda..0000000000 --- a/src/test/java/com/fishercoder/_1300Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1300; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1300Test { - private static _1300.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1300.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{4, 9, 3}; - assertEquals(3, solution1.findBestValue(arr, 10)); - } - - @Test - public void test2() { - arr = new int[]{2, 3, 5}; - assertEquals(5, solution1.findBestValue(arr, 10)); - } - - @Test - public void test3() { - arr = new int[]{60864, 25176, 27249, 21296, 20204}; - assertEquals(11361, solution1.findBestValue(arr, 56803)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1302Test.java b/src/test/java/com/fishercoder/_1302Test.java deleted file mode 100644 index 04aa05dd71..0000000000 --- a/src/test/java/com/fishercoder/_1302Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1302; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1302Test { - private static _1302.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1302.Solution1(); - } - - @Test - public void test1() { - assertEquals(15, solution1.deepestLeavesSum(TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, null, 6, 7, null, null, null, null, 8)))); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1304Test.java b/src/test/java/com/fishercoder/_1304Test.java deleted file mode 100644 index 53a8a752bc..0000000000 --- a/src/test/java/com/fishercoder/_1304Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._1304; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _1304Test { - private static _1304.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1304.Solution1(); - } - - @Test - public void test1() { - CommonUtils.printArray(solution1.sumZero(5)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1305Test.java b/src/test/java/com/fishercoder/_1305Test.java deleted file mode 100644 index 9b40b85a80..0000000000 --- a/src/test/java/com/fishercoder/_1305Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1305; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1305Test { - private static _1305.Solution1 solution1; - private static TreeNode root1; - private static TreeNode root2; - - @BeforeClass - public static void setup() { - solution1 = new _1305.Solution1(); - } - - @Test - public void test1() { - root1 = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 4)); - root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 3)); - assertEquals(Arrays.asList(0, 1, 1, 2, 3, 4), solution1.getAllElements(root1, root2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1309Test.java b/src/test/java/com/fishercoder/_1309Test.java deleted file mode 100644 index da84912f69..0000000000 --- a/src/test/java/com/fishercoder/_1309Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1309; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _1309Test { - private static _1309.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1309.Solution1(); - } - - @Test - public void test1() { - assertEquals("jkab", solution1.freqAlphabets("10#11#12")); - } - - @Test - public void test2() { - assertEquals("acz", solution1.freqAlphabets("1326#")); - } - - @Test - public void test3() { - assertEquals("y", solution1.freqAlphabets("25#")); - } - - @Test - public void test4() { - assertEquals("abcdefghijklmnopqrstuvwxyz", solution1.freqAlphabets("12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#")); - } -} diff --git a/src/test/java/com/fishercoder/_1313Test.java b/src/test/java/com/fishercoder/_1313Test.java deleted file mode 100644 index 528b40aff6..0000000000 --- a/src/test/java/com/fishercoder/_1313Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1313; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1313Test { - private static _1313.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1313.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new int[]{2, 4, 4, 4}, solution1.decompressRLElist(new int[]{1, 2, 3, 4})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1314Test.java b/src/test/java/com/fishercoder/_1314Test.java deleted file mode 100644 index 9707785773..0000000000 --- a/src/test/java/com/fishercoder/_1314Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1314; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1314Test { - private static _1314.Solution1 solution1; - private static int[][] mat; - private static int[][] expected; - - @BeforeClass - public static void setup() { - solution1 = new _1314.Solution1(); - } - - @Test - public void test1() { - mat = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9} - }; - expected = new int[][]{ - {12, 21, 16}, - {27, 45, 33}, - {24, 39, 28} - }; - assertEquals(expected, solution1.matrixBlockSum(mat, 1)); - } - - @Test - public void test2() { - mat = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9} - }; - expected = new int[][]{ - {45, 45, 45}, - {45, 45, 45}, - {45, 45, 45} - }; - assertEquals(expected, solution1.matrixBlockSum(mat, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_1315Test.java b/src/test/java/com/fishercoder/_1315Test.java deleted file mode 100644 index 642cbd4d96..0000000000 --- a/src/test/java/com/fishercoder/_1315Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1315; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1315Test { - private static _1315.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _1315.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(6, 7, 8, 2, 7, 1, 3, 9, null, 1, 4, null, null, null, 5)); - TreeUtils.printBinaryTree(root); - assertEquals(18, solution1.sumEvenGrandparent(root)); - } -} diff --git a/src/test/java/com/fishercoder/_1317Test.java b/src/test/java/com/fishercoder/_1317Test.java deleted file mode 100644 index 1bdda715fc..0000000000 --- a/src/test/java/com/fishercoder/_1317Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1317; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1317Test { - private static _1317.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1317.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new int[]{11, 999}, solution1.getNoZeroIntegers(1010)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_131Test.java b/src/test/java/com/fishercoder/_131Test.java deleted file mode 100644 index 1fd1711dd2..0000000000 --- a/src/test/java/com/fishercoder/_131Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._131; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _131Test { - private static _131.Solution1 solution1; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _131.Solution1(); - } - - @Test - public void test1() { - expected = new ArrayList(); - expected.add(Arrays.asList("a", "a", "b")); - expected.add(Arrays.asList("aa", "b")); - assertEquals(expected, solution1.partition("aab")); - } -} diff --git a/src/test/java/com/fishercoder/_1323Test.java b/src/test/java/com/fishercoder/_1323Test.java deleted file mode 100644 index 4c6a812a57..0000000000 --- a/src/test/java/com/fishercoder/_1323Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1323; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1323Test { - private static _1323.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1323.Solution1(); - } - - @Test - public void test1() { - assertEquals(9969, solution1.maximum69Number(9669)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1324Test.java b/src/test/java/com/fishercoder/_1324Test.java deleted file mode 100644 index e0a899832e..0000000000 --- a/src/test/java/com/fishercoder/_1324Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1324; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1324Test { - private static _1324.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1324.Solution1(); - } - - @Test - public void test1() { - assertEquals(Arrays.asList("HAY", "ORO", "WEU"), solution1.printVertically("HOW ARE YOU")); - } - - @Test - public void test2() { - assertEquals(Arrays.asList("TBONTB", "OEROOE", " T"), solution1.printVertically("TO BE OR NOT TO BE")); - } - - @Test - public void test3() { - assertEquals(Arrays.asList("CIC", "OSO", "N M", "T I", "E N", "S G", "T"), solution1.printVertically("CONTEST IS COMING")); - } - -} diff --git a/src/test/java/com/fishercoder/_132Test.java b/src/test/java/com/fishercoder/_132Test.java deleted file mode 100644 index f0e1b90f97..0000000000 --- a/src/test/java/com/fishercoder/_132Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._132; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _132Test { - private static _132.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _132.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.minCut("aab")); - } -} diff --git a/src/test/java/com/fishercoder/_1331Test.java b/src/test/java/com/fishercoder/_1331Test.java deleted file mode 100644 index fb5de56806..0000000000 --- a/src/test/java/com/fishercoder/_1331Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1331; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1331Test { - private static _1331.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1331.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{40, 10, 20, 30}; - assertArrayEquals(new int[]{4, 1, 2, 3}, solution1.arrayRankTransform(arr)); - } - - @Test - public void test2() { - arr = new int[]{100, 100, 100}; - assertArrayEquals(new int[]{1, 1, 1}, solution1.arrayRankTransform(arr)); - } - - @Test - public void test3() { - arr = new int[]{-1, -3, 100}; - assertArrayEquals(new int[]{2, 1, 3}, solution1.arrayRankTransform(arr)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1333Test.java b/src/test/java/com/fishercoder/_1333Test.java deleted file mode 100644 index 6049f14b2d..0000000000 --- a/src/test/java/com/fishercoder/_1333Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1333; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1333Test { - private static _1333.Solution1 solution1; - private static int[][] restaurants; - - @BeforeClass - public static void setup() { - solution1 = new _1333.Solution1(); - } - - @Test - public void test1() { - restaurants = new int[][]{ - {1, 4, 1, 40, 10}, - {2, 8, 0, 50, 5}, - {3, 8, 1, 30, 4}, - {4, 10, 0, 10, 3}, - {5, 1, 1, 15, 1} - }; - assertEquals(Arrays.asList(3, 1, 5), solution1.filterRestaurants(restaurants, 1, 50, 10)); - } - - @Test - public void test2() { - restaurants = new int[][]{ - {1, 4, 1, 40, 10}, - {2, 8, 0, 50, 5}, - {3, 8, 1, 30, 4}, - {4, 10, 0, 10, 3}, - {5, 1, 1, 15, 1} - }; - assertEquals(Arrays.asList(4, 3, 2, 1, 5), solution1.filterRestaurants(restaurants, 0, 50, 10)); - } - - @Test - public void test3() { - restaurants = new int[][]{ - {1, 4, 1, 40, 10}, - {2, 8, 0, 50, 5}, - {3, 8, 1, 30, 4}, - {4, 10, 0, 10, 3}, - {5, 1, 1, 15, 1} - }; - assertEquals(Arrays.asList(4, 5), solution1.filterRestaurants(restaurants, 0, 30, 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1337Test.java b/src/test/java/com/fishercoder/_1337Test.java deleted file mode 100644 index e124207bdb..0000000000 --- a/src/test/java/com/fishercoder/_1337Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1337; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1337Test { - private static _1337.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1337.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.removePalindromeSub("ababa")); - } - - @Test - public void test2() { - assertEquals(2, solution1.removePalindromeSub("abb")); - } - - @Test - public void test3() { - assertEquals(2, solution1.removePalindromeSub("baabb")); - } - - @Test - public void test4() { - assertEquals(0, solution1.removePalindromeSub("")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1338Test.java b/src/test/java/com/fishercoder/_1338Test.java deleted file mode 100644 index 13f4689936..0000000000 --- a/src/test/java/com/fishercoder/_1338Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1338; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1338Test { - private static _1338.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1338.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{3, 3, 3, 3, 5, 5, 5, 2, 2, 7}; - assertEquals(2, solution1.minSetSize(arr)); - } - - @Test - public void test2() { - arr = new int[]{7, 7, 7, 7, 7, 7}; - assertEquals(1, solution1.minSetSize(arr)); - } - - @Test - public void test3() { - arr = new int[]{1, 9}; - assertEquals(1, solution1.minSetSize(arr)); - } - - @Test - public void test4() { - arr = new int[]{1000, 1000, 3, 7}; - assertEquals(1, solution1.minSetSize(arr)); - } - - @Test - public void test5() { - arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - assertEquals(5, solution1.minSetSize(arr)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1339Test.java b/src/test/java/com/fishercoder/_1339Test.java deleted file mode 100644 index 4c6db608d2..0000000000 --- a/src/test/java/com/fishercoder/_1339Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1339; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1339Test { - private static _1339.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _1339.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6)); - assertEquals(110, solution1.maxProduct(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_133Test.java b/src/test/java/com/fishercoder/_133Test.java deleted file mode 100644 index 980e460e36..0000000000 --- a/src/test/java/com/fishercoder/_133Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.UndirectedGraphNode; -import com.fishercoder.solutions._133; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _133Test { - private static _133.Solution1 solution1; - private static UndirectedGraphNode expected; - private static UndirectedGraphNode actual; - - @BeforeClass - public static void setup() { - solution1 = new _133.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = null; - actual = null; - } - - @Test - public void test1() { - UndirectedGraphNode node0 = new UndirectedGraphNode(0); - UndirectedGraphNode node1 = new UndirectedGraphNode(1); - UndirectedGraphNode node2 = new UndirectedGraphNode(2); - node0.neighbors.add(node1); - node0.neighbors.add(node2); - node1.neighbors.add(node2); - - expected = node0; - actual = solution1.cloneGraph(expected); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_1341Test.java b/src/test/java/com/fishercoder/_1341Test.java deleted file mode 100644 index f33efe5053..0000000000 --- a/src/test/java/com/fishercoder/_1341Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1341; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1341Test { - private static _1341.Solution1 solution1; - private static int[][] mat; - - @BeforeClass - public static void setup() { - solution1 = new _1341.Solution1(); - } - - @Test - public void test1() { - mat = new int[][]{ - {1, 1, 0, 0, 0}, - {1, 1, 1, 1, 0}, - {1, 0, 0, 0, 0}, - {1, 1, 0, 0, 0}, - {1, 1, 1, 1, 1} - }; - assertArrayEquals(new int[]{2, 0}, solution1.kWeakestRows(mat, 2)); - } - - @Test - public void test2() { - mat = new int[][]{ - {1, 0, 0, 0}, - {1, 1, 1, 1}, - {1, 0, 0, 0}, - {1, 0, 0, 0} - }; - assertArrayEquals(new int[]{0, 2}, solution1.kWeakestRows(mat, 2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1342Test.java b/src/test/java/com/fishercoder/_1342Test.java deleted file mode 100644 index 213693fa43..0000000000 --- a/src/test/java/com/fishercoder/_1342Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1342; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1342Test { - private static _1342.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1342.Solution1(); - } - - @Test - public void test1() { - assertEquals(12, solution1.numberOfSteps(123)); - } - - @Test - public void test2() { - assertEquals(6, solution1.numberOfSteps(14)); - } - - @Test - public void test3() { - assertEquals(4, solution1.numberOfSteps(8)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1343Test.java b/src/test/java/com/fishercoder/_1343Test.java deleted file mode 100644 index 7b5aa45ddd..0000000000 --- a/src/test/java/com/fishercoder/_1343Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1343; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1343Test { - private static _1343.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1343.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.numOfSubarrays(new int[]{2, 2, 2, 2, 5, 5, 5, 8}, 3, 4)); - } - - @Test - public void test2() { - assertEquals(5, solution1.numOfSubarrays(new int[]{1, 1, 1, 1, 1}, 1, 0)); - } - - @Test - public void test3() { - assertEquals(6, solution1.numOfSubarrays(new int[]{11, 13, 17, 23, 29, 31, 7, 5, 2, 3}, 3, 5)); - } - - @Test - public void test4() { - assertEquals(1, solution1.numOfSubarrays(new int[]{7, 7, 7, 7, 7, 7, 7}, 7, 7)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1344Test.java b/src/test/java/com/fishercoder/_1344Test.java deleted file mode 100644 index 54e2ba6e8b..0000000000 --- a/src/test/java/com/fishercoder/_1344Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1344; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1344Test { - private static _1344.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1344.Solution1(); - } - - @Test - public void test1() { - assertEquals(165, solution1.angleClock(12, 30), 0); - } - - @Test - public void test2() { - assertEquals(75, solution1.angleClock(3, 30), 0); - } - - @Test - public void test3() { - assertEquals(155, solution1.angleClock(4, 50), 0); - } - - @Test - public void test4() { - assertEquals(0, solution1.angleClock(12, 0), 0); - } - - @Test - public void test5() { - assertEquals(76.5, solution1.angleClock(1, 57), 0); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1345Test.java b/src/test/java/com/fishercoder/_1345Test.java deleted file mode 100644 index 2b0f6c375e..0000000000 --- a/src/test/java/com/fishercoder/_1345Test.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1345; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1345Test { - private static _1345.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1345.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{100, -23, -23, 404, 100, 23, 23, 23, 3, 404}; - assertEquals(3, solution1.minJumps(arr)); - } - - @Test - public void test2() { - arr = new int[]{7}; - assertEquals(0, solution1.minJumps(arr)); - } - - @Test - public void test3() { - arr = new int[]{7, 6, 9, 6, 9, 6, 9, 7}; - assertEquals(1, solution1.minJumps(arr)); - } - - @Test - public void test4() { - arr = new int[]{6, 1, 9}; - assertEquals(2, solution1.minJumps(arr)); - } - - @Test - public void test5() { - arr = new int[]{11, 22, 7, 7, 7, 7, 7, 7, 7, 22, 13}; - assertEquals(3, solution1.minJumps(arr)); - } - - @Test - public void test6() { - arr = new int[]{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 11}; - assertEquals(2, solution1.minJumps(arr)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1346Test.java b/src/test/java/com/fishercoder/_1346Test.java deleted file mode 100644 index f2b1387c93..0000000000 --- a/src/test/java/com/fishercoder/_1346Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1346; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1346Test { - private static _1346.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1346.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{10, 2, 5, 3}; - assertEquals(true, solution1.checkIfExist(arr)); - } - - @Test - public void test2() { - arr = new int[]{7, 1, 14, 11}; - assertEquals(true, solution1.checkIfExist(arr)); - } - - @Test - public void test3() { - arr = new int[]{3, 1, 7, 11}; - assertEquals(false, solution1.checkIfExist(arr)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1347Test.java b/src/test/java/com/fishercoder/_1347Test.java deleted file mode 100644 index 7acfc01aaf..0000000000 --- a/src/test/java/com/fishercoder/_1347Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1347; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1347Test { - private static _1347.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1347.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.minSteps("bab", "aba")); - } - - @Test - public void test2() { - assertEquals(5, solution1.minSteps("leetcode", "practice")); - } - - @Test - public void test3() { - assertEquals(0, solution1.minSteps("anagram", "mangaar")); - } - - @Test - public void test4() { - assertEquals(0, solution1.minSteps("xxyyzz", "xxyyzz")); - } - - @Test - public void test5() { - assertEquals(4, solution1.minSteps("friend", "family")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1348Test.java b/src/test/java/com/fishercoder/_1348Test.java deleted file mode 100644 index c85595c31d..0000000000 --- a/src/test/java/com/fishercoder/_1348Test.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1348; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1348Test { - private static _1348.Solution1.TweetCounts tweetCounts; - - @Test - public void test1() { - tweetCounts = new _1348.Solution1.TweetCounts(); - - tweetCounts.recordTweet("tweet3", 0); - tweetCounts.recordTweet("tweet3", 60); - tweetCounts.recordTweet("tweet3", 10); - assertEquals(Arrays.asList(2), tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59)); - assertEquals(Arrays.asList(2, 1), tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60)); - tweetCounts.recordTweet("tweet3", 120); - assertEquals(Arrays.asList(4), tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210)); - } - - @Test - public void test2() { - tweetCounts = new _1348.Solution1.TweetCounts(); - - tweetCounts.recordTweet("tweet0", 33); - tweetCounts.recordTweet("tweet1", 89); - tweetCounts.recordTweet("tweet2", 99); - tweetCounts.recordTweet("tweet3", 53); - tweetCounts.recordTweet("tweet4", 3); - assertEquals(Arrays.asList(0), tweetCounts.getTweetCountsPerFrequency("hour", "tweet0", 89, 3045)); - tweetCounts.recordTweet("tweet0", 28); - tweetCounts.recordTweet("tweet0", 91); - tweetCounts.recordTweet("tweet0", 9); - tweetCounts.recordTweet("tweet1", 6); - } - - @Test - public void test3() { - tweetCounts = new _1348.Solution1.TweetCounts(); - - tweetCounts.recordTweet("tweet0", 13); - tweetCounts.recordTweet("tweet1", 16); - tweetCounts.recordTweet("tweet2", 12); - tweetCounts.recordTweet("tweet3", 18); - tweetCounts.recordTweet("tweet4", 82); - tweetCounts.recordTweet("tweet3", 89); - assertEquals(Arrays.asList(0), tweetCounts.getTweetCountsPerFrequency("day", "tweet0", 89, 9471)); - assertEquals(Arrays.asList(2, 0), tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 13, 4024)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1349Test.java b/src/test/java/com/fishercoder/_1349Test.java deleted file mode 100644 index 839f49d6bb..0000000000 --- a/src/test/java/com/fishercoder/_1349Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1349; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1349Test { - private static _1349.Solution1 solution1; - private static char[][] seats; - - @BeforeClass - public static void setup() { - solution1 = new _1349.Solution1(); - } - - @Test - public void test1() { - seats = new char[][]{ - {'#', '.', '#', '#', '.', '#'}, - {'.', '#', '#', '#', '#', '.'}, - {'#', '.', '#', '#', '.', '#'} - }; - assertEquals(4, solution1.maxStudents(seats)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_134Test.java b/src/test/java/com/fishercoder/_134Test.java deleted file mode 100644 index 8977e8ab8c..0000000000 --- a/src/test/java/com/fishercoder/_134Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._134; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _134Test { - private static _134.Solution1 solution1; - private static int[] gas; - private static int[] cost; - - @BeforeClass - public static void setup() { - solution1 = new _134.Solution1(); - } - - @Test - public void test1() { - gas = new int[] {4}; - cost = new int[] {5}; - assertEquals(-1, solution1.canCompleteCircuit(gas, cost)); - } - - @Test - public void test2() { - gas = new int[] {5}; - cost = new int[] {4}; - assertEquals(0, solution1.canCompleteCircuit(gas, cost)); - } - - @Test - public void test3() { - gas = new int[] {2}; - cost = new int[] {2}; - assertEquals(0, solution1.canCompleteCircuit(gas, cost)); - } -} diff --git a/src/test/java/com/fishercoder/_1352Test.java b/src/test/java/com/fishercoder/_1352Test.java deleted file mode 100644 index 2132f5f158..0000000000 --- a/src/test/java/com/fishercoder/_1352Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1352; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1352Test { - private static _1352.Solution1.ProductOfNumbers productOfNumbers; - - @Test - public void test1() { - productOfNumbers = new _1352.Solution1.ProductOfNumbers(); - productOfNumbers.add(3); - productOfNumbers.add(0); - productOfNumbers.add(2); - productOfNumbers.add(5); - productOfNumbers.add(4); - assertEquals(20, productOfNumbers.getProduct(2)); - assertEquals(40, productOfNumbers.getProduct(3)); - assertEquals(0, productOfNumbers.getProduct(4)); - productOfNumbers.add(8); - assertEquals(32, productOfNumbers.getProduct(2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1353Test.java b/src/test/java/com/fishercoder/_1353Test.java deleted file mode 100644 index 892e368b7b..0000000000 --- a/src/test/java/com/fishercoder/_1353Test.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1353; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1353Test { - private static _1353.Solution1 solution1; - private static int[][] events; - - @BeforeClass - public static void setup() { - solution1 = new _1353.Solution1(); - } - - @Test - public void test1() { - events = new int[][]{ - {1, 2}, - {2, 3}, - {3, 4} - }; - assertEquals(3, solution1.maxEvents(events)); - } - - @Test - public void test2() { - events = new int[][]{ - {1, 2}, - {2, 3}, - {3, 4}, - {1, 2} - }; - assertEquals(4, solution1.maxEvents(events)); - } - - @Test - public void test3() { - events = new int[][]{ - {1, 4}, - {4, 4}, - {2, 2}, - {3, 4}, - {1, 1} - }; - assertEquals(4, solution1.maxEvents(events)); - } - - @Test - public void test4() { - events = new int[][]{ - {1, 100000} - }; - assertEquals(1, solution1.maxEvents(events)); - } - - @Test - public void test5() { - events = new int[][]{ - {1, 1}, - {1, 2}, - {1, 3}, - {1, 4}, - {1, 5}, - {1, 6}, - {1, 7}, - }; - assertEquals(7, solution1.maxEvents(events)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1354Test.java b/src/test/java/com/fishercoder/_1354Test.java deleted file mode 100644 index 026e345ddb..0000000000 --- a/src/test/java/com/fishercoder/_1354Test.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1354; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1354Test { - private static _1354.Solution1 solution1; - private static int[] target; - - @BeforeClass - public static void setup() { - solution1 = new _1354.Solution1(); - } - - @Test - public void test1() { - target = new int[]{9, 3, 5}; - assertEquals(true, solution1.isPossible(target)); - } - - @Test - public void test2() { - target = new int[]{1, 1, 1, 2}; - assertEquals(false, solution1.isPossible(target)); - } - - @Test - public void test3() { - target = new int[]{8, 5}; - assertEquals(true, solution1.isPossible(target)); - } - - @Test - public void test9() { - target = new int[]{9, 9, 9}; - assertEquals(false, solution1.isPossible(target)); - } - - @Test - public void test11() { - target = new int[]{1, 1, 2}; - assertEquals(false, solution1.isPossible(target)); - } - - @Test - public void test12() { - target = new int[]{1, 1000000000}; - assertEquals(true, solution1.isPossible(target)); - } - - @Test - public void test13() { - target = new int[]{5, 50}; - assertEquals(false, solution1.isPossible(target)); - } - - @Test - public void test10() { - target = new int[]{835647834, 503037935, 773002076, 731298404, 903645595, 488189634, 319785391, 111546683, 609144970, 415205491, 685900245, 878508756, 236413773, 991053691, 947595808, 913014938, 533038855, 88376603, 436545178, 983496954, 122530918, 346542007, 55893465, 472984628, 347337093, 322587100, 552866643, 609759356, 288893937, 471774337, 465491640, 783022406, 699817530, 340584553, 663909719, 651419106, 846593366, 952609573, 912379694, 661318302, 538633771, 745093202, 753577352, 60741272, 800245613, 228718955, 314289253, 384902244, 834091366, 330486268, 832528567, 405339553, 667374764, 477631332, 458512135, 281436435, 301856749, 331522322, 439316110, 65782135, 313620054, 377064760, 689776101, 453352404, 739524725, 113039032, 403624252, 864855957, 816177580, 472331359, 702356269, 634580725, 79566086, 723272803, 484129094, 785382934, 519691527, 303358848, 673141033, 900376058, 640090231, 332948759, 3578533, 603932450, 300252459, 455172786, 398327644, 667611961, 579527425, 847780358, 251487657, 239105566, 949519075, 816672375, 569680210, 522034786, 433488047, 339341869, 323607606, 695508020, 671840506, 403376732, 437224135, 704526427, 733331510, 566004060, 190603334, 401309800, 135615700, 480920888, 880495868, 394755529, 754131300, 980354442, 940475050, 455976643, 26150213, 620704469, 305714399, 452126616, 331922227, 285017717, 547688077, 571029451, 580099665, 888037179, 116069830, 492086251, 460673303, 652403652, 529457074, 959765712, 884239314, 707976976, 421820667, 102910899, 850649035, 332557694, 156833539, 193014693, 712889809, 65402492, 81873875, 205826928, 906576787, 536580780, 98764481, 614216242, 724741649, 926963940, 415525579, 707467586, 670685849, 645316339, 663012408, 451540628, 190074574, 100279961, 373676876, 108645392, 439186589, 289025528, 317751557, 696247292, 252536329, 524764647, 664019135, 681731166, 697044386, 721419536, 89117662, 386734310, 933623270, 18288756, 858592177, 843786785, 267433641, 104266328, 816027769, 103604036, 874219314, 626912072, 643973051, 138646542, 930635468, 638385362, 666376936, 75446650, 725532251, 558802301, 898021675, 602462415, 287100257, 103256759, 241469306, 418203100, 359820338, 439745508, 711532895, 913171524, 828285226, 490697952, 94810193, 272869408, 26560972, 979834901, 5721504, 185048349, 42545696, 396115122, 904051513, 220906107, 738993493, 588839845, 742987869, 641939237, 20134580, 613427664, 760712842, 725934923, 233138479, 694808775, 504699750, 45306699, 928921991, 348537469, 341584856, 314823473, 323200569, 5906520, 590908812, 7058731, 612527161, 95929757, 923027538, 504340570, 108267579, 981049825, 3621484, 268758794, 155138552, 967177696, 154587439, 226488698, 435889878, 282062129, 837804113, 874696398, 704916352, 672370979, 429701338, 699214790, 311303195, 947796876, 50395096, 943382310, 264134145, 110060599, 662341190, 707099896, 778109433, 209892183, 129970328, 614555781, 65561327, 723924768, 308208692, 907464540, 511861513, 664166961, 278299804, 383189150, 305144703, 399367644, 745158781, 611505481, 788035592, 958094209, 4691422, 982804694, 182195783, 644444281, 888532003, 913888496, 781449985, 252740240, 661510236, 998487245, 284318090, 646237565, 822471400, 371153017, 988783, 234143485, 906623027, 265667900, 88762375, 654203306, 124205686, 724165887, 715879556, 778678123, 201533689, 241172717, 220243793, 300732194, 834092268, 546274723, 978426161, 544966162, 860580567, 833904274, 408002579, 683573852, 414196298, 567536223, 401758309, 449922489, 264257881, 552000887, 313883439, 121158141, 816111867, 426696045, 166578559, 869183250, 677763442, 449243505, 446613544, 618933881, 765618361, 782239559, 139564825, 17361518, 375149549, 752695711, 170774896, 24223979, 929368291, 714737788, 968883177, 418171397, 28938451, 530535587, 831491933, 388232832, 375471966, 501547932, 42057135, 105907929, 319325198, 866969758, 883623220, 636639049, 98250237, 119670840, 810521959, 994818843, 979182684, 698619283, 306787611, 79779236, 316097105, 20407838, 246365554, 405157606, 746584313, 338447287, 956653822, 157277229, 382734547, 919850823, 17698838, 400783732, 846595424, 820236688, 844188244, 592045378, 664969413, 199985888, 986952695, 924241714, 545487784, 487539172, 147186879, 945151212, 958877753, 669099388, 403471548, 499546716, 915071435, 897865485, 892380510, 328284017, 649646782, 564916899, 126507447, 341928577, 916918420, 335542645, 562151880, 341948589, 153658487, 915169176, 189949851, 956608598, 92763236, 671268832, 829439855, 198464213, 96704707, 504412819, 126161975, 935193455, 513168023, 134286447, 385405295, 615567547, 17991890, 790707341, 991003956, 917307082, 145826213, 725182412, 707571456, 193431873, 694550855, 988821536, 296476226, 166333863, 595537769, 579249117, 788071113, 445822525, 312987370, 690589050, 707167073, 734770753, 836025747, 398024391, 614034356, 19892950, 177961692, 901857208, 570179250, 880745468, 628351955, 471663624, 537067058, 620422588, 801133337, 443608528, 655426152, 630822673, 323931531, 616054005, 715419230, 344542774, 569479396, 46182177, 115731968, 615621055, 927437470, 415304055, 613134814, 593402338, 555887984, 424883671, 737555229, 580850096, 206291958, 408415618, 568495923, 383046806, 331153888, 630013482, 639299672, 932192570, 426968629, 665396997, 377560684, 425576288, 499413833, 304016029, 435936610, 890997684, 37462641, 308478314, 648743689, 968679592, 6563304, 528867706, 815369648, 699526086, 854594514, 77461257, 912978080, 437849059, 584065006, 448033159, 851418436, 275202511, 933127854, 57918210, 936121297, 728315416, 493270959, 635640085, 751603865, 815510463, 322128401, 817518864, 113319096, 719706704, 151675058, 579563270, 817496583, 331259914, 304828788, 832758432, 514693374, 628452536, 859015740, 54698207, 502378937, 925823748, 468799569, 912430738, 503849861, 155092332, 42145830, 16914543, 954818713, 944191613, 753948564, 101131208, 628620169, 514926770, 674609079, 477754503, 226746575, 399775590, 317837974, 685000591, 380154246, 862619940, 156768091, 907663822, 640534228, 509892852, 140342102, 997418526, 552535460, 430179820, 454222315, 646598713, 14409817, 280310343, 324342456, 625965172, 814280682, 974660429, 122025967, 697839974, 290110820, 878936393, 874327021, 252896959, 368474189, 596100537, 759459589, 74112139, 496075772, 660681195, 476919942, 732314798, 881551587, 600771213, 916355905, 62502174, 722694262, 846208181, 235701369, 377851030, 444635218, 905821472, 6007590, 14543647, 806494416, 995077069, 714758118, 844053358, 554781732, 772216007, 970855797, 411569324, 513182664, 137085083, 204095216, 632442670, 843815967, 289982568, 631746100, 825138049, 349162102, 5326115, 207201935, 835205999, 161469959, 312314799, 644367317, 122663749, 602404965, 763996480, 974548439, 732304994, 808596406, 180421152, 975264158, 122323736, 966123769, 50110342, 744171007, 561228509, 513803739, 631885254, 848740278, 38362310, 334317889, 407483295, 704719903, 793514069, 3850010, 47429717, 332085264, 296925009, 179969654, 285965854, 188329006, 353044452, 907132439, 173498729, 971005353, 886796300, 374137584, 167007939, 257311427, 69578518, 986207933, 820577598, 762893591, 855603877, 154159499, 81230719, 988672413, 560039653, 187206037, 124634358, 756546188, 562543979, 382863463, 791317508, 638677331, 472652962, 704857946, 755439531, 209470628, 413904724, 236402946, 980178476, 925780627, 624361832, 986373739, 865597279, 733945791, 40519093, 346073968, 531933636, 452742332, 388931131, 509971886, 203046081, 86591499, 107265603, 170834917, 222658017, 931432329, 545903464, 244840320, 750824817, 665373361, 418717528, 723513553, 572491457, 458224851, 688126547, 872604574, 477404041, 810687226, 902428227, 809180291, 181691685, 593946617, 443990866, 886931195, 671951824, 117916938, 88508493, 101987861, 998221579, 302729532, 637782794, 713816070, 909221425, 478649015, 947515758, 588376423, 102208139, 387839447, 126033549, 266213171, 286274226, 201637730, 911783168, 261943681, 622500013, 456472374, 708804967, 489562877, 836248357, 622073857, 432626926, 157337505, 416320771, 436152162, 592575425, 42633820, 518316330, 29165774, 254298082, 747662083, 309417930, 635421959, 306846172}; - assertEquals(false, solution1.isPossible(target)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1356Test.java b/src/test/java/com/fishercoder/_1356Test.java deleted file mode 100644 index c41ef5370a..0000000000 --- a/src/test/java/com/fishercoder/_1356Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1356; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1356Test { - private static _1356.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1356.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new int[]{0, 1, 2, 4, 8, 3, 5, 6, 7}, solution1.sortByBits(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8})); - } - - @Test - public void test2() { - assertArrayEquals(new int[]{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}, solution1.sortByBits(new int[]{1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1})); - } - - @Test - public void test3() { - assertArrayEquals(new int[]{10000, 10000}, solution1.sortByBits(new int[]{10000, 10000})); - } - - @Test - public void test4() { - assertArrayEquals(new int[]{2, 3, 5, 17, 7, 11, 13, 19}, solution1.sortByBits(new int[]{2, 3, 5, 7, 11, 13, 17, 19})); - } - - @Test - public void test5() { - assertArrayEquals(new int[]{10, 100, 10000, 1000}, solution1.sortByBits(new int[]{10, 100, 1000, 10000})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1357Test.java b/src/test/java/com/fishercoder/_1357Test.java deleted file mode 100644 index 8ca7dd9e52..0000000000 --- a/src/test/java/com/fishercoder/_1357Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1357; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1357Test { - private static _1357.Solution1.Cashier cashier; - - @Test - public void test1() { - cashier = new _1357.Solution1.Cashier(3, 50, new int[]{1, 2, 3, 4, 5, 6, 7}, new int[]{100, 200, 300, 400, 300, 200, 100}); - assertEquals(500.0, cashier.getBill(new int[]{1, 2}, new int[]{1, 2}), 0); - assertEquals(4000.0, cashier.getBill(new int[]{3, 7}, new int[]{10, 10}), 0); - assertEquals(800.0, cashier.getBill(new int[]{1, 2, 3, 4, 5, 6, 7}, new int[]{1, 1, 1, 1, 1, 1, 1}), 0); - assertEquals(4000.0, cashier.getBill(new int[]{4}, new int[]{10}), 0); - assertEquals(4000.0, cashier.getBill(new int[]{7, 3}, new int[]{10, 10}), 0); - assertEquals(7350.0, cashier.getBill(new int[]{7, 5, 3, 1, 6, 4, 2}, new int[]{10, 10, 10, 9, 9, 9, 7}), 0); - assertEquals(2500.0, cashier.getBill(new int[]{2, 3, 5}, new int[]{5, 3, 2}), 0); - } -} diff --git a/src/test/java/com/fishercoder/_1358Test.java b/src/test/java/com/fishercoder/_1358Test.java deleted file mode 100644 index 12f6450786..0000000000 --- a/src/test/java/com/fishercoder/_1358Test.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1358; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1358Test { - private static _1358.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1358.Solution1(); - } - - @Test - public void test1() { - assertEquals(10, solution1.numberOfSubstrings("abcabc")); - } - - @Test - public void test2() { - assertEquals(3, solution1.numberOfSubstrings("aaacb")); - } - - @Test - public void test3() { - assertEquals(1, solution1.numberOfSubstrings("abc")); - } - - @Test - public void test4() { - assertEquals(19, solution1.numberOfSubstrings("abbbbcabc")); - } - - @Test - public void test5() { - assertEquals(3, solution1.numberOfSubstrings("ababbbc")); - } - - @Test - public void test6() { - assertEquals(0, solution1.numberOfSubstrings("abab")); - } - - @Test - public void test7() { - assertEquals(1244858478, solution1.numberOfSubstrings("cccbbbacababcccabcaaaababacaccbccccabcbcbcbabcabbcbbccaabababcabbaabcabbabcbaccbccaaababcbbbbcbbcaccacccabcabaabaaaccbcaccccbabccabacacbbbccbacbbbbbacbcabcabccbaccccccccaacabababababccbbcccbbcbcaabbcbbbbbcaacacaacccbbbbabcabbcacabbcbaabcaabcbcaaaabbcbbbacccbacaabacbbaacbcbacaaabcbaacbcbaccabccbacbacbabbbabbbcbcaaaaccccacbaaaaccbbccaaacbbabbbcabbaccbabbacbaccaaaababbbcbcaaccacbacbbaaabacbcacacacbccaaabbcccbbcaccbccbabcabcaacbaaacbbcababbbcbabbaacbcaaccbcacccabaacabcacaaccacacbcaaccbacccaacccbbbcbbcababbcbcacaccacccabababccbaaaabcaacaacbcbaababaccaaabcccababcbaaccbbabcacbbcbcccbbacacbbbacbbcbccabcbbabbbcbacbabcaacaacbaabcbbbcbcbcbaacaabcaccacccaacccccbcbccabbbacaccaababcbabcbacbccacbcccbaacccbbcabacaacabababcbcaacaacbabaccacccbbbbaabacbcaaaaaaaaaccbacaaabaaccbbacbaaacbcbbcaccbabbabbbabbbbccbaacbbabbbbaaabcabbbbbbbacbcacacaacaccbaccbcabbbbaabbacacbbcabcaaaaabbbcaacbabcbbcacbabbbaaaacbcbaabbcbbaacbacacbcbccbccacaccaacccbccbcbbcabacbbaabcbbcacbabaabacccbaacbbaaccbcababbcbcbbbcaacababaccbcccbbcbababccccaccbccccabaabcabbcacccbcbacaacccabbaacbbbbcbcbacacabaaaaabcbbbabbabcbcccabbaaaccbaaaabbaabbacbccabbbcaacbbbcabbcbcbbcbacbaababaabaaacbcaabcacbccaaacacabbccbcbcbbabcbbccacacbbcacaccaaacbabcababcbcbbcbcaccccabcbccbaacaacbbaaacbbccbbccbbcacbcabbacccaaabaacbabaacbaabaaabcabcbcaaaccaaccbbbccbcbacbacbababbcbcacaaaabcbaccccaccabcaabcabbbcabbbcaaccbcacaccbabbbcbacabbaccabbabcaccbbacabbbabbcaaaabbacaabaacbccacbaabbacbbabcbacabbacbbcbbcbbaabaabcabaaabcccbbcbbccbbccbccabbbabababcabcbbbaaaabbaacaccbaabcaacccbabbacacabcbbbababcbaababcbbaccaaaabbacccbacbcaacacaacacabcaacabbacacbcbacaabaaccaacbaaabbcaaabbbbbcbcbbcccbabcabbbabacaaaaaacacaacacacbaabccbacacabbaaababcaabbabbccabaaababbabbbbabcababbaacbcccbabbccbcaabccabbcbabaaaacbabcbbcbbabcbbaaacccacccbbbcbbccbcbaaaaacaaccabcbaacabcbabaaacbacccacabaacbababbcbbcbababcaaccabbcbbacaacaaccbacbbccbbaaccbbcaccaccbbaacccacaabbbcaabcbcbcaacbabacccbabcaababcaababbbcacbaabccaccabcacaaaaabaacaaaaababbbbbcaabaccbabacbaacbcababcbbaccaccbabcbbcbbbccbbcbbcabbbccaaccccaaccabaabcbcbcacbcbaabccaacabaaabacabacbccccccbbacabbaccacccaaaaabacaababcbababaaaccaabacbbbcacccccbccbaacbbccacacababbaacabbabcaaccccbababcbaacbccbbabbcbcaccbabaacbbcbbcaaccabccaacbbbaccaababbacabbcabcbccaabcbabaabaaababaaacbaacabaccaaacbaabaccbbcaaaabacacbabbaaabcbaaabbacbaabacababaabcccabbacaaaabccacbabbcbabbccaabacccabaccabcccaabcbbbcacbccabcacabaccbcabacbcbbabcbabcaaacbababaccbcccbacbcbbbcccbbcacbbaccbbbccaacaccbbbabacbbabcbaabcbacaabbbbcaacabbbbabcbabaaabacbbcacbaacbbccbabbcbccacaabcabaaaabbccabaabccabbcabccacccccbbacabbbbbbbcbccbbbcccaccccbcaaccbcacaacacbbbaaccbcabcbccccaabbbcccaacbbccacbcbcacabaccbaaaabaaccacbabccccacaccababcacbcbbcbbbaaabacbaccabcabbbbaababbabcaacbcbcbccccaabbccccbaccbabaabbacbacaabbbbbacbcbcbccccbaccbbacabcbabbaabccacaaaaabcbaaabcbcacccaccbabaccacbcaaaabaabcbaacabacbacaaabcaacaaccbcaabbcbcbaabcccacaabcccabccaacbcbbbaccacbacbabbccaaacbabacbcababbcabcbbbbacaaaacacccbaccbbabbbccbccbacabbbcaabaccaccbcbbacbacacaaaaacbcababaaabccbcaabaabccbbbcbccbbacaccbacbcaaabacabcccacbaccbbccccabcaaabcccbacabbbbcaacbbbacacaaacccaaccbbaccbcabacaccbcbabcabbacaacaaaabaacbaccbacccbaccaaabbbbcbaacbcacaaaaaacbccccabcbaaaccccacbbcacccabbccabbcaaccbbccccbbaaacabbcbbbabacaaccbbaabaabaacbbacacbbbbbccbaaaabcbaaccacabacbaaaaabcbabbaabbbcbbbacbbbcbbbcaaacacbcbbcaccaabbcaacaaacbcbbcaaaccccbabababbaabaacacababbabcccccbaccaccabcbccbccbbababbbacbbcaaaaccbaacaabccacaabcbbabcbbccaabbbacacccabbabbaacaccbbabbbabccccbcbbcbbcbbccacccbcacbcacccbccabcccbcacbacaccbacbacaaacabacbbacabababacaabcbaccbbabcaaacbbbabacbaabbccabbbcbbbbccacaccccaabcbcbbcccabbababcbaaaacccaccabbabaababccbaabbbcaacbaacabaababcbacbbabbcbacccaaaaaaaaaacbcaccbacacbbccaabbcbacbbccbabaaabacccbaccbbbbbbcaaababaabbbcbbababaabaaacbbbacaabcbbccbbacccabbbcbcbacbccacbcbbaccacbccbaaaccbcbaaacbaabaccbbaaabaaccabbcaccccacbaacaacabbcccbccbbaaacaacbcaacbbcaccbbabcaaaacccaabacbababcbcbcccbcccaabcaccbacbabcbaaccaacaaaaacababacaccccccbaabcbbacaccbbaabaacaccbccbacaaabcccbcbaabbcabbbabbbcbbbabccbbacccabbccabbccabcacbcaacaccbccccaaacbcbabbabbaacbcbcbcbcbbcbaccbcbcbacbbcaaaaaabbaccbcbabbabbabaacacbcbabcccbbaaccbaaaabcabbccbabbabaababaaababcabcacaccccbaaabcacbababacacaacaaacaabaabbbbbcacabbacbcaacbbccacccbabbcbaaacaabaacaabaacbbaabcaaacbccabbbcbacccabcbacacacbcabbbccacabacbaaababccaababaabacacaaabacacbabcacacacaccacbbcbcbcbcbbbaabaaaabbbacbbaaccabcaabaabcbbbbbabbbababbcbbaacbacbbccbbbbcccabccacbaccaccbbccaaccabbaaaccabccbbabccacaaabbbcacacaabbaabaabcbbaaacacccbccbbbcaabcacbcaabaaccccccabcaabcacbccaacbcaaaccbccbcacbcabaacaaaccabbbcbccbcabaabbcacbcbacccaabaccaabbbabcccbacbbcbcbccbaaccacbcbbacabbababbbbccacbaaabacaacbbacbccbbcbccbaaaaacabbbbbcbacbabbacabbaaaccaaaacccbcbaabbbbcbcaaabcccbbabcacbaccacabbbcaabbacaaabccabacccaccbcbabcbbaabbaccabcaccaaccababcccbbacacaaaacccccbaaaacccbbacbabcbbcabaaccacacaaaccbcbcacabbacccccabcbacbacbcbcbbcaaaccccabccbcacaaaaabacacaacbbbcaaacaabbbabbaaabbabbbcbabaaaaccbbbcccbaacaabacaaacbccbcbabbbcacabaacaabababcbaabcaaaccccbcbccbbaabbacccacccbcaaacccaccaabbbcbaaacccccabcabbbbbbaaacbbccacabacabaabaacbbabaacbcccbabcacaccbacabababbcaaccbacbabababbabbbcabaccbaacbbcacbcacbabbcbbaaabbbcbacbcbcbbbaacbbbbaccabaaaabaaacbbbaccaaabbccabbcbcccabcacbabbbabbbbcbcbabacbcacbcbacaacbaabacbbccacbbaacbaccccaccbacababbbcababbbacaaabacbbabcabbcabbaacaccbbbbcabcccaccacbcaaabaccbaaaacacabcabaabaccaaacbbaabcaccacbbabababcccbccccaaccabbcabaaaacbaacaacabbcbbaacccbabcbcbbbacbbcacbabbbbaccbbbbbaaaabcaabbaabbbabbcbaaccbbabaaabababbabcbcbcabcaccabaacbabcbbcbaaaacbacccbabbbcbbbccabbccaccbabcbbcbbacbcaccbcaaccabbabcbabcabacabbbaaabbacacaaaacbaababbbaaacbcbabacacbcbbacbccaaacbcacaacaccacaabbcbcccbccacbabbcaabcbbcbabababbabbabbcabbbbabbcbaacaacbbbcaaacbbcabcbaabccccabcbaabbabbbbcabcabccbcabcccbbbcbbaacccbbaabaaababccaacbabaaaabacaabbcccabaabaabbcacccabbcbcccbbbaababbabcaabbaaaaccbcbacabacabbbaccbbabbabcaaacbcacabababbbcbccbaaabbcccbbaacaacbababbabbcababcabbcaacbaacaabacbaaacbcbbaccabbbccbbccbbaaacababbabbacbbaccccabaaacbcacabbabaacbbacaacaaacaaaaabbacbaaacbbbacbacbbbbcabbcbbcacbbaaccaaacacbcabacababcbcacabbccccacbbababaccbabcacbcbccacaaacccabaabaabbcabbbacacbabccbcaaaababaabaaaccbbbababcabaababbcababbcacaaacaaaccbcbabcbabcbacbccacabcbcaacbcacbcbcaabcbabaacbacbbcbcbabbbacbaaacccbacabcbcbcbbbabbcbcbbacabcabaaacbbacbbbaabcaaabcbaabcabacacaabaaaaabcccabbacabaccbbbbabbbababaabccabbbacaabcbccacaaaababbcccababaaabaccbaccbaabccbaabcaaaabaccacbabaaaaaccbcbbaabcbaccccbbaaccabbacacaccbabbbbaacabacababcaaaabbacbcbbcbcbababaccbbbaccbbbabcbababcacbbbbcbaabbcbbbccbaccabccababccaccbbacbbaabcaaaacaaccbabbbacaaacabbcacacbcabaacabbbacbaaababaaccccacacacbcbaaabcbbbbcbabcbccbbbaacabcabbcabaacbacbaabbcccaacccbabcaaccaaaaaacabacbacbcbbcccbaccaccacaaabcccbacabcbabaaaababccbaccbbcaaabcccababacccbabcaabcbcabbbbcbccbaccaaccacbaabbbabbcbbaccaaacaaccbbaacbabcbbaaaccabaaabbbaabcacabaaabacbabcaaabbacbbacbaaccaaacabbcaaacbbbacbcbbcbabbabaacbcababbbbbcbbabbacacabbbccbbccbbbcabcbcbccacbaabbcacccbbbbbccbaaccabbcbacbaaaaabababbcccbaabbcababbbbaacbccbbacabbaaacbbccaaaabaabcbacbaccbcccbabaaaccaaccbcccaacabbcbbcbbccaaaabcccbccbaaccabacaaabaaabacacbbaaacbbbcbabbcaabccbcaabbacaaacccbbcaacacbbaccbbabbcbbbaacbccccbabcbbacaccbbcaaaaabcabccccaaccacbcbababcbbaaaabbbcaabbabbcabbaaaabcbbacccbbaabbbacaacacaaaacccbaabacaaccbccacbbcaaacbcaaaaabaaccbcaccabbaaacbbbbabaaaabbcaacabbbccbaacbbbcbaccaabbcabbcbcccacbbbacbbabaaaabacbcbcababcbaaaabcaacbacbbccbaacacccbaacabbcccabbaabaaaabcccbaabbcbcbcacbcaabcccbabaabccccabccbbaaabccaabbacbcccbaacabccacccabbbbcacaaccbccbcccacbaccacaaaababbcabccabcaacacbcacbcccababbababbbccacabbcaccabccacccbababbbcccccacbbacaacaaaacabcacccabbbcabccacaccaabbcaabacbabccccacabcaacabbabccacacbaabaccabaacccbbcabaccaaaaaacbbbbbacacccbaabbaaabaccbaaccaaccaacbaacabbbcaaaaaabccbaccbaaacccaaabcbbbabccbababcbbbcbabcabcccababacabcbabacaacaccbabaaacccacbbcaaaaaccabbcaaccbbbccbabababccccaccbccbcbacaabbabbcbaabbbcabbccaaaabbacababbaaacccaacbcabacacabacabbccabbccccbcbbaabbaacbabbaaaaabaccaaabbcabacabacaccccbcababbacabaabbcbbaccabbbcbccaababbbababacbbcaaccabbabbabbcbaabaacccaaacbabbcbcbcccaababaabbcaaaacbcccaacbccbcccbbbbbcbbcaababccccaaacacaaabbccacabbaabccbcaabbaaccbacabcaccaaababbaabccaaabaababbababcaacbbacccabccbcccccabaacaaccacbcbaaacbbacbbaccabbbbacccaccbaccccbbacacbcccbcaacaccbaccccbcaaaababacbbbaccabccbabaaccaabbbbcaaabcbbbbcbbabbbbaaccabcaacccabacabccaccbbabaacbcaabcbabbbbaabbbbbacbabbbaacaababaabababaaaababcacbccbcbbcacaccaccbababcaacacbcbababacabcbbcbcabbcabaacccaaabbbcabacabbccbccabcccbbaabccaccaacaccaacabbccccbababacbaacacaacaacbbcbcccacccababccabcccaabacbabcbbbcabaaccaaabcbccbcaabaacaccaccacccbaaccbcaabcccabacbbbacbabacaccacccbbbaccacbccbcbbbaabcacccacaabcaaabbbcacacbbbcbccaccbcbabbbccbaacbbcccbbabcbabababcbcccabaabbaabacccbacaaccbbccaacbbbcbaabaaabbcbaccbaccbabaaacbbbcaabacacccabccbbcccabcbbbaaacbccbaaaacbbcbccbacacccccbcbbcccbbaaaccbabaaccabbaabcabccbbbaccaabbbcbbbaaaabccccacbbabbccbccbaccbacbcbccbbcbcabbbcbcabacbabcacaaabccbcbcacbbaacabacaaccaabacbcbaabbaccbcccbbacacbcccabccbaaacacbbabccbcacacccacbaabaaababbabaacacbbcbaccaacabaaaaabcbcccbbabaabcbabbbccabccccbbcbccaaacaabcccacaaacbacabbccbcbaaacbcababaaaaccbcbccacccabbcabbabbccaaababaaaabbabbabcbbaaaccabbcbbccbacbbcaacbabaaaacbaaabaaabaaaaaacabbbbbbabcbacbbbbbbcbccaabccccacaabcaabbabbabacbcccbcbcabacbacabbcbbcbbacbbcabaccabaabababaacbbcbcabbcacacaabbcbcabbcacaaaaacbacacaacbbbbbaaabbbbabcccbaaccccbcccbbccaaccaacaaccccacacacbcabcbccbaaccccabbbbabbacbbbaccccbcccbcabbbbababcaacbbacccbbcbbbcccababacbaacacbaacbbbacacaababccccabcbcbccacbbbaaaccbbcaaacbcaaccbbcbccabbcbcaabacaaacacccaccaaabacbacbabacbbcabaacbccbccaccbcccaaaabbaacbaccbcbbaccccacacaabcccbaacbccaabbcbbbbabacacacbbabaaaabcccccbcbabaaabbbabcabccbbcaababaccacacccbbcabcabbccaaacbabcacbaaaababababcbbbaccccacbacaccbbcccbbbbccaabbccccbabbbccbccbbbbbaaacaaabbcabaabbbcacaaabcbbabaaacbbccabbcbabbccaabacbcaaaccabcbbbaaabacbcaccabbabacccabbcbccbabaccccbbbbabbcbacabcbbbcabbbcccbbbbcccccacaacacabacaaabbcacabaacbbbcaabcbbaccbaaabcaccbaccaacbbbaababaccbcaacacabcacabacabaacbcaacacabacbcbaccacccbaacccaabababbbaabbbaacccbccacbaccbaacabcbbbcaaaabcacbbcbcccbaaacbcabbaaaaabccbcbcccabaabacaccbbbacaaccbcbababcbbbacccbbccbabcccacaabccaaabbcbbbaccbbabcbbabcaabcbaacbbcabcabbbcbaababaacbabbccbcabbabcbbbabcccbbabacbabbaabcaccbccacbabccbaaabbbacccababbaaabbbbabbccabababbcabbabcccccccccbcbccaaacbccabbabbbaccaacacbbcbbbbcbabccbbacbabbababcabbcccbbabaaaaacccacacbaaaaccccbcbabcabbbcbaccbbbbcbbbaaabcbaccbabbaacaaacbcbabaacbcaacbccbcbbacbbcbabcbcaaaaaabbbabcbaacabbaacbcbaaaaacaabbaabbccbcaccacbccabbcaaacbababbcbccbcacbbbcaabaccbcbcbbaaccabbabcabccaccbbaaaaacabbbababccbaccacbcaaaaacccaaacacabacabcbcacaabbcbcacbbcaaccacacacbbbbbbcccbbbccbbabbcccabbccacacbacabababbbaabcbcbbabbaabaababbcaccabbcabacbcacaacbbbbaabbccbaaacccacbbbcbbbababaabaaababcbcbaacccbbccbcbcbaaaacbbbbcaaaabaaabbbccabcbaacabcaacbbcaabacabababbabbbabbbbcccabaccbabbccbbbabcabaabbcacbbcacacbacacababbbccababcbccbabaaaaabccbabbabccacaaabcacacaabbababababaccbbaabcacbbabacbacbbbabaacaabacbbbaaabcaacbcaccbbbabccababcbcbcbcabaaaccbcabcacbbbababaaaababbbbccaacbabaababcbcabccaccbacbabaaaaabbccbcaaaaaabccbbabcaabacbabacccabbcbcbccbacbaabbaacaaacbacbbaaccaabaaabacacccaabccabbbaacaaccacccacacccaccbccabcaaabacacbbccbabbacabbbaaabbabcaacbabbabcbbcaccacccbbccaccbacbcacccbabcbcbcbbabcccccacbabbacacbbabcacabbaababbcccaababaccacaacabacaaaccababacabbcabcabacaccbbcacaaabbaacbbcaabcccbcbabccbabbaccbcabbacabccbcbbcbacabcabbbbcababacaababccbbbccbbaababbcabbcaaaabbbacaabaabccaaccbbcbaabbbccabbccabcbacbcaccabababbbccbbccbbcccbacccccaacbbabccababcbcbcbcaacbbbbaaccacccabbbaabaccbacacbbbcbbabaaaaccabcaabbaccbccbaacabaacaabbaaaacabaaccabacbccabbcccbcccbcaaccaaccbcaabcbabbbaacabbaabcccbaccacbbacbbbcacabbcbaaabcaaaccbbcbaaabbabcaccacbbbbccaccabcaacabcaacbaccbcabbccaaabcbccbaaabbcbaacbccbacbbcbaccbaaabaaaababccbbcbacabcbabcbbacbccaabaaccbabcbcbcaaababbbcbccababbacaccaabcbbaacbabaacabaaaaaabcaabcbbcacbcacabcbbbabacbbbbaabacbcaaaaacaacacbcbcbbcbcbccacbaccacbbcbcbcabcbaaabbcbabacabacbabccbbbcbbcccbaaabcccacabbbcaacaacbaabbcbcbacbbcbbbaaaacbbabcaaacbccbccaacbacaaccaababbacbcaabcbaabcbbcabbabcbccaaaacbabbbcaabccacaaccbbbccbaccbaaccabbcbbbccbcaabbbacbbcacaccacbcbbbbabcccabbccabbcbacaccbaaacbabbcbacccaacaabacbbabcaaacaacbbabcacacaabbbaabaaccccbbcaccbbbacacbbabcaacbbbbbababbaabacbaabbcaabccaaabbacbacacccccbaccbbbccbcaaaabccbbbababbbbbaabccaccaaaccaabbbbacaacbbcaaccccabcaaabcabcaacbcbbcccabaababbbbbabcbcbccababcbccacbcbcacbaabcacccbacaccacabbbababbbabcaabbbcccacccabbcaacababcbbcbbaaccacbbaacbaabcabcababbacbcabaaacccacbbaacbabbcccabcbaaccbbcbcabacbcbbbbcccaccacaacacaacaabbcccaaccbcacbabbacabaabccccaaabccabcabacaabbaaaabcbbbbabbcbbbbabbcbcccbabbccabbaaacbbcabcabbabccccbbacaacbbccbccccababbcacabbacaccbbbcbcaaabcbcbccbbbccaacaaabacacaabacbacbbcaaccaaaaccccbcbbcbaacbcbaccccaccbbcaaccccaabbcbbbabbaaacbcbbaacaccababaaacbbaaccaccbcaacabaccacbbaccaaaacbcbabacabacbbaaababcbbbaacbaccbccbacabacbaaababcbcaccbabbaccbbbcabbabbabcbcaaccaccbbbbacbbaaabcaabacccbbbacbaacabacabbaaabcbcaaaaccccabacaacccacbbbcbababaaabbbaccbcaabbccaabaccbbaababaabbcbbabcbcabbbacabaccbbabcbbbbcacabbacbacacccacbaacabaaaacacbcbaaacbcbbabccbaaabaaabaccccaaabaabcbccaaabcabcacabacccbabccbcacacbbbaaacabaacabbbcaacabcaacbabccacbccbacbcbbcaaaabcbbbbbbcbcbaabbcbcabbbababbabaaacbaaccbacccbbccabacaacbbbbacbbaaccacbcccaacbbbbccaabaccaccbbcaaacabaabcabaacccbabbaacaabaacccabcccccbabaacaaccbcbbabaccbbbcaabacaccbcbacccbccbaacacccacaccabcaababbaaaaccbcabcbbacbccaaaabcacbbacacbaababbccccbabababbaaabbcbabbcaabbacaaaabaaaaaaaaaccbaacbbbaacbbbabccaccacacacbacabbbbabbaacbbbbacbcbbbcacccbabcabbbacabcbbbcbcacbabbacbccbccabcbcbacbbbbcbccacacccaabcbcaabbababababbacabbbbbcaabcacabbbcababaabcbaabaaaacbcbbbcbcbcbacbbcbabccabbcacaaabababacbcaacacabcbccccaacccacbaaaacaccabcabbaccbcbccbabccccaaaaacacabbabcacabcbabccbaccaacbcbcaaaabaacacabaacbaccccabbaccabaabccccaccbccbabcbcccabbbccabaaccbcaccbbabcbabcccbcccaacacaccacbbbacbbbcaacbaabbbcaccccbbccacccacaabbbcabbbacbcacbccbaccbbccabcbbbabcacabbbaacbbbcccaaabacaaabcbcccbccaaabcbbabaabcbacbbbabbccbcacbccaacbcbabbbbababbcacccbabbaaccbbcbcbcacccaabbaabcaabbcacbcbabacbcaabaaabcbaabbbbabacbbcabbaaccbcccbcabcbabccbbcaaccbacabcaacaccbcacbbaccacbbcbbccbaccbcbaccbaabcccbaabacbcaaabcacbbaaaabacaccbbbcbbacabcababbcbbabbccacbabcbcbabcbbaccbacbbbabcbbcaaacbaabcbcbccaccbacacbbabaccccccbccaccbaacabacbccbcaabbaacbcacaabacbacbbaccbabbbbaccccaabbacccababbbbaabccaaccbabbbaabbaacbababcbcbbabcbacbabccccbbcbcabcbbabacacbbccbaabbccacaabbcbccbcbbcccbbababbccccabaacbabaabcaacbbbcacbbabaaabcbcbbbbacaccacacacacacbacabacbcbabcccaaababcaabcbabbcbbacbaacccaaabccacccaaabaaabbccccaccbacbcccaabbacbbccbbbcbaaacabbcbbcacaacbcabcabcbacbababbccabbbbbcbcaaccbcabababbaaacbabcbcaccababbacabaaaaccabaacbbcaaababccaccbaacacacbcabcbabaabcbcbbccbaabaabacaaaaccbbbbabbaabccabbcacacacbcacbccccaacaccbabbbcbbacaccaccbacacbacacbbabaaabaccbcccaacbaaababcbcbbabacacbbcaacbaabbcabbbbccacabaccaacacacccacacacbbcaaaacbccccbcbaaaabccccababcbbcbbbcaaaccbaaccaaacbababccbbbbababcbcabbabbcbbbbbccabaabaabcacaaaababcabbbabbacbaaaaccacbcbacbabcccbaaabbbcbcbcbbbccbabaccabbacacbbcaaacbbaabcacabbbcabcbbccacaacaacbbbbcbbcbbcacccbaacabbaabcbaaabbbaabbcbbaccacbccaacacbbbbbaabbaacabbcacbabbbcabccbabbabcbbbbacabbcaaccacabcbbcaaaccabcbacbaacbacbbacabcaaacaabbbaaccbccabaccbcbcbbbccccbbabcaabbbcabccacacacbbcaccaabbbaaacbaababcaaacbabbbaaaccaaabbaacacabbabcbccbbcaacbbccbccccabccabbabcaccbcbbabaaacaccabcbcbcbbabbaabcaabccbabaabccbabcbcacbcabccbcabaaaaccacbbacbcabaaacacbcababbaacbcaaacccacaaabbbccaaccabbbbcabcbccbcaabbccbacccabcbabbcccacbaabccaabbbbabbcabababaacaaacbaaaacaaabcaccbcabbbcccaabcaaaaabacaaababbacccababbcabbcaacbcaacbabbaccaabbababbabcbaaaaccaaaaaaaccccbbbababbabbbaabbcaabaacbacababcbbcbbbbbbcaaaccbaacbccbabbaabbacabbcbacccaccccacacabcbaabcccabacbcacacabaabcbcbcabacbcaaacbaabccbcaabbaccabcbcbcccbcaaaaaacccbbbccccaaaaccccaccacaabaabbababbcbbaaabacacacbcacccaacbcaaccbaabaccabcacbcbcababbaaacaaacccbbabccabbbbbbccababcaaccabbcbccbaacaabaccbaaacabcbacbccacaaabcbaccacccabacbaabccbccaabacaabbcbccbacbbcabababcccbaccbbacbbcccbbbabccaabcababbcaccaccabbacbacccccabaccaabcbaabbcbababcacbbaacaabcabacccaabcbccabaccccccabbaabcccababbaaacbaacbcbcaabccaccccacaaacaabbccccaacbbccababbbbcbbbaaabaccabcaccacbbbabacbacbbbbccacacbabcbccabcabacbbccbacbccaacbacbcaababccaaababbbbcccbaabbbabbbabaacaabcbacbbccccbcaccbcbccbcccaccbbbbaababacbaccccaaaaccbbaabaaabccbaaaacaaaaccaabaaabaccccacabbabcbbacbacabacabcbbbabccbacbcbacbbbcabcbcccacbababaccbabccbacaabbbccbbcaaaaccbaacbbbaaacbbbccbacccbabccaababababbbbbbcaacccccabbcccccabcbbbabbabbcaacaaabbabbcaacbbcaccabcacbaabcaabbcacccbbaacbbcccccbcccccbaaacbcccaabaaaaaaabbcccbcbacbbaaccaacbabacbccbabaacabbcaabbcbccccacccacbcccbaccbbbcbbccbbcbbabcaacaaabaccacabcbbbacbcabcbccacaaabaabbaccaccbccccabaabcabccbaaaacacacabababcbcbaaabbccaccaaacbbccaaccbbccaaacbbbbaabcbbbccabaacabccbabbcabcababbbccabaaababbbcbbbcbbcbbbacbbbbaacbcacccacccbbcbcbccabbcaabacaaaccabaaacbacbaaabbccccacaabcbbbccababacbaaaccbcacbabaccaccccabcccabccabacacccbccaaabcbacbaacbaabaabbacbbacbbbcccabbccbcccaacababbacaccbabbacbcbabbccaacbabbccbabcaccbbbaaaaacbacbcbaaaccaaacaccbaccaccaaaaabbacaaaabcbbaaabacaccaabcaaaccbaabaccacabccbccccbbbaccaacbaccacaabccbcabacaacccbcbbcacacaaabcbcaaccccccbbcacbacaabccaccabbaaaaacbcccaaacbacaacabbaacbaccababbacbcccbbbbbcabbbacaaccccbaabacbaccacacccbababbbbbbccccbbbbbacabbacbaabbaaacbbcbcaccbabcabcbaaacacbacbababababacbaababacccccacbbcbcacbccbaabcccaccbcabccacbabcbacbbccccbacaccbbbacaaabcbaaaccbbbaaacabababacccbabcaabcbcaaabaabaaacbacbcaabacaaacbcbacccabcabaccaabcbaaabbbcacabbaabacaabbbcaaccbaacbbbcbbbbcccbcaabbacabacaabcabaaabcbbabbaaabbbabcabaacbabbbcbacccabbbabcccaccbbbbbbaacacaabcabcbcbaaabacbaabbacbacabccccaacbcbcabccccaccacabbcacabbccbacacacabccbcaaaabbacccbaacbccbbaccbbbaccaabbacbcaccbababbbccaaccbccccbaabbbacaaababaabacabcbcbaaccbcbcaccccbbcbaabcaaccabaccccbcabbccccbabcbcacbcccacbbacbcabbbbaacbabcbbccbccaaabaccbabbbcbbacacaaabbaacabcbcbccbbacbccaaabacbccaababaccbbcbabccacccabcaccbbcbabbacbbcabbaaaabcacccbcacababccacbbbcbbaacbcbabacbbaaabaababcbaaaabaccbbcccccabbbacaaccbaabbaaaaaababbcaccbbbaacbbacacbaccaaccaccaccaabaabcbabaccccabbacaacacccbacccccacbcbaaabcaaaaccbcacaaccbaaabbcbcacabcacccbbbabbbcaccaaabacbcccbbbacababbacacccbaabcccaccacababcbaccaccabbcaccacccaacacbcccbbabaacbcccacbbaacbbaabbbbcabbcccbbcaaabacabbababccacaccccaabbcbbbbabcaaaabaccccbccabcbabcacacaababccabbbbbaaccaabbcacbacbaabcabbbbccbcbbcbccccbcbabbbbbbabbbacabccbaacbbbabccababccaaabaccbcacbbabacabcaabbcbcacbbccabaaccaabbbccaaabccccbabaacacbbaccabbabababbbacbbbcbaccccbcabacbcbcbaacbcaaababacbcbabcbccabcaccccaaacbaabaccaababaaaabacbccccaccbaaacbcaaabbcbacbbcabcbbbabccccaaaaabbcbacbcbabbabbbbccccaacacccccaaccaabcbbbcaaaccabcaabbaaaccacbbcccbaaccbcbabbbcaccbbaaaabaabbcbcaaacbabbbcacacbacbabacacabbacabcabbabaabcaaaccaabccaccaabbcaccbccbbcacbbaacaababcbcaacbbccabbcccaabcbbccbacabcbcbbbcbbaacaaabbcbbcbbbccbbabccacbbbccbbccbacacaabbbbbaaccbbbaccccbaccccaaaabbabaacbcaaaccbbcbbacbccbabaaacabbabcabbccbbbaaacaccaaccaabbbcbcccaccbcbcccaabbabababcacbaaacabccbacbbcaaabbbbbbbbbccbcccbcbabaaaabbcaacbcbcbbccaabbcbacbabcaaabcbbbcbcbbbccbaacabbcbcbababbacbbbbaaaacbbacbbbbbaacbaccbbbbacbbacaabbcbacccabaccaacbbababbabaaacbaabaccbabcaabbcbaababbacbacacaaacbbbaacbaacccbcabccabacbaaacbccbbcaabcbbcaabaabaaaccacbaacbabccbbbcabbabcbcababcaaabcaabcccabacbbcaacabbbccccacabcaabbaabcbcbacaabcccbbbbabbcbcccabbabaaccaacaccccbacccaacabbbbaabcbcacacbbbbaaaacbcacabaabccaaacbabccbbaaccbbaaaabacccabcabbaabcabccbccbbabacbbcbcccccacbaabacacaccaaaabbccbabccabacaabbbccbcbaabbaaccababccbbbbccaabaaaacccaacbbacacbcaccbbccbcaabbcaccbcacacbcbbacccabcbcaabcabcbbaaccabaaccaaacccccbccbbbcabcbbcbcbababacacabbabaacbcaaaaacbccaccaaabcaaaaaccabccbbbcbbacaacbbbabcbbaaacacbbbcccacbcacabbbcbcabbacacbaaacbbbbcbcaccaaaccbcbcaccaaaccbbcaaaccbaabacbbcaacabbbccaabaacbaabcbcabbabbacbaabcacacbccaccbcabbbcbbcabacbbbcabcabbcbaabbacbcbbcbcbacaacbcabccaacbbcbaabcacbccbbbacabacccbcbbaaaababaaabacaaccabccbcacbabcbaacbcaaacacccbbababcbbbccbcabbcacbabaabbaacabbbbacacbababacbbbaabacbbbaabbaaabccaaaabbaabbaabaabbccabbabaccaccbcaaaacabbcbaabbcbacaacabccbabcbbaacccaaccccabaaaccccbbbaaabbaaabaccccacbbabaacaaacaacbbbbcabbbbabaabaaccbabbbbaaabcaaabaabbcbbaacbcacbcabbcacaaacbaacbbbaaabbbccabbbabbcbacbccbabbbbbbbababaabaaccabcbccccaababbccabcabbccbabaccabcbcacbaabccacaccbbbacbaabacbbcabbcaabbcbbaaacbcbacaacbacabaababaaaaccaaaccccccbcaabcbccaccbaabccccaabbccbbbaaacbbbbabbccbbcacaaacaccaaacababaacabbabcccccbabcbaccaacbccaabbaacaaccbbaaccccbbaaaaacbabbcaacaabbbbcbabacbcacbcabbcacbcabaaabaaabbaacccbcacacaabcbabbaabcacaacabbcacccbacbcbcaaabaccbacacabcbabbcaccabcabcacbabbbbbcbaaacaccaabcccabcbaabcbababbcbacaaccaabcacbbccbaababbcbccaabbbbcacaaabccaaacbbaaccccbababaacccabaababbabccbbcbccccbcbbacbaabaaccabcccacaccabcccaacbcaaccbccccccacbaabbabbabcbaababccbaccccccbbbcaaaccaacacabcabaccabbbacbaccccbccaacabacccbbbcababcbcbaccabacbbbbaaccbbacbabbaaacccbaaabbcbcabcbcaaabcbbccbbbbabcccaaccababccacbcbccaccabbabacbcbbbcacccbaacbcacbbcbcacbcabcbcbabbccacbaacbcbbcbcbabcaacaaabbbaacbbcbcacbaabcbcaaacbccbbaacbbabcabbbbaccbacaccbabbccbbbccabbbcbcccacbcaccbacbacabaacbcacacabbbcbbbabaaccaacacaccbbcbacaacabaaccaacbcccaabacccbcbaaacacacbbccbbccaabacababbcccacccbbcaaacabbbbcbbcbaaacbbacacaaccacabcbaabcbccaabaacacbcacbcbbbacccabbcbbabcbbabaabcaabbbcbaccaabbbcabccacbbcaabcacbcacbbabaaacbcabbcbaaccaaacacccbacbbcccbbbabcbcbacacccbcbaabcaacaaccbaccbbbcccbacabacbbbbbcaaacaabbaabacbbccabbbcaacccccbacabcaababcbccaabbcbaaaacbaccbcccaabaccbaaabbbcacabacaabccbbcaaccacbbcabcabccbbbbcbabacaaaacaacbbbcaacbcbababacbaabaaaabccbbbccbbccaaaaacabaaacbccbabbaaaacaabbccccccbaabcbccbacbcabbcbaacaabbcbccbaaabacccaaccaabbaaaacacabcaaaccabaabccbabbbaabbcbaaaabcbaabaaaacabaccbabcacabbcbcbabccccbcbcaabccacacbcaaacbaccccbcaccbcbcccccccccabccccabbcbbcbbbacacaaabacbcbaacbbacaabccabccabaccbaaabccabbbcaaacbbcaabacbcccbbbcbbcacacaabacbbbacabbabbccaccababcbacaabcacbbcbcbccaabcaccbabcacbbacbaacabcbbabaccccbbccccbccabbbcccabaabbababcabcaccbccabbbababacaccaaaaabbaaccacbabbbcaaacaccaaaccabbabaabccaaabaccccabcabbabcbbbccbacaccbaabbcbaacbcbbcbabcbbccaacaabbcbaaabbbbbaaabaaabcbaccbbbacbcbbcbaaccacbbbbcbcaaabaccbbbabbaabcaccaabacbcbacbabaaacbcbabccabbcacacbbcabccccbbaacaabaccbcbabcbbabaccbbbacbbcbbbbbbbbabccacacabbbccacabcbcbbbcbccacbabccccacacbbccabbbbbbbbacaabbbbcccacacbbaccaaabbaccbacabacaccacbcbbcccacaccaacbaaacabcaccabacbabcacabccaabaaabaabbbaacabaaccbccbbcbcbacccbaccbbbccacbcbccbbacabcabaccaccbaccacaaaaccaacaabaccaaacabaacbcbbaaacccbcaacbaccacbcabcbabccbcaaaaaccbacaaacbbbcaabccbcaabbcbbbacbacbcaacbbcaababacbcbccbcccaacbaabbccbaaccbbbcacbcbabcaacccbabaabbabcbbbacabbbababaabaaaaacbccbaccbabbbcaabcaabcacbaabaababbcacbccbabbbabcabbabcaacbaabaaaccabacacaacaacacabacbabacbbcabaaccbccabbacccccbabaabaacabacbbbcaccabcacabccccccacacbabaccaaaaabcababaabacaabcbcacbbccaabababbcbcccaccccacbbcababbacbabacbbabbbcacbcccbbaabcbaaaccbbbbcbacbabcabaabbccaaaabaccbaaaccbbbaabbbcababbcbacbbabbabbbabbcbabbcbbbcaacbabaabaacbacababcccbacabbacaaccacaabcbbbbaccbabbabacacbcbcaacccaabcbccbabacbbccccabaacabaabcbcbcbcbaaccbaacacbcccaacabaacbabbaabaccbbcccbacbcabccbccccaabaabbbcabbaabbcbcabacacacababcacacbcacbbbbbcabacbcabacaabaaabbacbacbabacacabccabacbabcccbbcbccabbaccacccacbccaaaaaabaabcbbbcacbbbbbcbaaaccccaaabaacbacabbcabaacbbaacacaaccbbaaabcabbacbbbacbbabcaaababccacbbacbccbccbabcccabacacbacbbcaacbaacaabacbcbaacbbcaaabbaccbcbccbcacccabcabbcaaccbaababbccbbaabcabcbbaabcbbcabbaabccbbabccaaaccabacaabcaacbacccaacbaacabccbaacaaccbbbcabbbacbcaaaacbacccaccbaacaaaabccbcbccbbababcbabcaacabaaaaaaccaabcacabbbabbcbcabacbcbcabaabcbccabbcbcaccaccbacaacbbbaabcbccabacaabaaabbbcbbaacabbbccaabccbcbaccacabbabbcbacabcbacbabacbbaaacabaabaaacacacaccabbbccacaccbbbbccaacbccaaaabababacbabaaaaccaaaccbccabcbabcbacaccbccaabbbcacacacabbaacacbcaaacacbbaacbaacacccbabacabbcbbbaabaacaaccbaabcbcbbccabcccbcbbcbcbaaccbaabbccbbacbccbacbcbbbacabaaababbcbccbbaacbbbacabbcbabaccbbaccccbaacabccaccbacccabbbccbcaaccabbbbbbcbabccacbcbacaaaabcabaccbabaabcaaccacbacccbcaccaccaacaaaccbbacaabbccaacbaccccacbaccaababacaacccaabcbcccabcacccbbbbaabaacaabcbabcaccaccaaabacababaacbcccbaaabcacacacacbcaabaccabcabbbcbcbcbaacaaccbcbbccccbbacaccbbbabbacacacaaabbacbbabcbcaaabccacabcbabbcbcbbcaabacbbbbcaababcaabccaaccbbaaccbabccbcbaabbbccacabbaaaccccaacbabbbabcacbabaaccbbccaabbbacccbcacabbbcbcbcbabcccbaababbbbcaabbabbbcbbaaabacbcbcbabcbccbbaabaababbabbbcaccaacacbacbaaccaacacbabbaacaabbaacacccbaabbbbbbaaababacccabccabbacabbbbaabaabcbacbaaacbbaccbaaaabccbacaaabaacbbbaaacbaacbcbcccaacaacbabbccccabbacbaccabccabbbbabaaaacccacbcabacaacbababbcabcbcbccbcaccabbbcbacbabccaccaaabacbacaaacacccbacabaaacabbccaccbbaabbbbaccaccabcaaacbbaabacbacbcabcccacbaccbabacbcacbbcaabcbbbcccaaccccbcaabababbbabaabbccbbbabaaacbbacaaccacbaabcbbabacbabccabccaaccacacabaabcccaabcccccccaabbcacbacabbccbbbbaabaabcaabaaacacbbaabcaacccccacbccbaacbabaabcccabcbabaccccbbbacabcbcbcbaccabbbcbbcabbbbbbcacbcabaabcaabcabaaaaaccccabcacbbccbbaabaccbcabaabababaccacbacbbccabbcbbbaaaaababbbcacacccccaacbbcbabbaaaabcbcbcaacccbccbccacbbbaacabbcbbbcbcabaacabcbabaaaabaacacbbabcabcabccbbacbcbcabccccbcabbcbccabbcbbbaabcccabbccabcbcaaacbbacaccbccbbbaaccccacbcbbacabaaaaaacacbcacabcabcabacccabcabbbcbabbaccbbaacaacbbcbbbbbbbcacaabaccbccacaaabcabaabbaacbbbacbcbcaaabbbbaccbcbacacbbcaacbccccaabaacacacabcbabbcbacababbbbabcaccccccbbcabbabbaabbccbbccaaaabcbabbaacbaabcaaaaabbcccbabcbbabbccaccbccbcaabacbcbcaabcbbbcabacacccaacccbacaabababbccbcbbcaaccbbaacbcaabbbcabbcccbccbbaccaacabababcaaacbbacaaabcabaabcacacacacbaaaabccbcabbccacbccaaccbabacbabababbbabcccbbbbbaccabbbbcacacbaaaababcabcaaaacbabaabbbaabcbcccbcabcaabccbbbccbabbcbbbbcbbcabcababcaacabbaabbccaabcbacccababaacbaacabcbbbabcccbcccbabbabcaacacacaaaabbbbbbabbcbbcacccaaacbcacbbaaccccbbbcabbbabccbcbabcabbacacabbaccabcacacbbacccbcbbccabbaacabbaaacaaabbccaccbacacbbcaacbccabababaabaccbbbaaacccaaccccccabcacabcbabbbbbbbacbbbaccbbcabacbcacabaaacaabaacbbbcabaabbbaabcaccacbababcbcabcaaacbaacaaaccacbbbabaccccabbababbcaaaacaabcbbbcbabacaaabbcaccaabcbbbcaccbaccbabccaaaacbaaaaccacbabbbcbbcaccccabcaaaacbabacccbbaaabbcbbaacbaaaccbccbbabbabbabaaacaaaccaaccacaabcaacaaabcaacaccccabcaaaacbcacaabbabaaababccaababbabccbbccbbbcccaaaaacbccbcbaaccbbcbcbbaacbcabcbacaabcbbaaacaaccaccccbabaaacabcbacaacacabababccabbcbabccccbbccccbccbaabbacbcacbbcacbababbaacbcbcbcbbabbbcbccacaabbbcbabaccabcbacbccbbbaaabacbaababbccacaccaabcbacbaaaccccbcbbcababbacaaacaacabacbcbabccbbcbabaccbccaccaacbacaaacacaacaccbaccbcbbccabbaaabcccccabccaccbbbabbbbbbacacbabcabcababaaaaaccabacaabacbaaabaaaccccabaccbbcccbaaaccbabaacbcbcaccbaabbaccaacaabcbaccaacabcbcaacccccbaabbbccabcabaccacabcababccbaabcacabcbbaacccacbccbbaabbbccaccababbbacbcaccbbaccbcacaabbbcbcbcacbaabbcbaacaabbabbcccbcabbccaabbbcababccccaabcbcbaaaccaacbbacbbcbccbaccaaaccccccccbcbbcbaacbcbaaabbccaccaaaabcbacabcbcaabcbbbcbaaabcabcaaacbaaccaccaccbcacacccccbcabbacbcbcccccabcbcbcabbaaabcacaccbbbcababbaccabcccbcaabbacbbaabaabbabcccbccccccacacccbaaaccabcccaaaaaabbbcbaabcccccaccbccbbbcccaabbacbbababbcbcacabbcabbabacacccabcbbabbcbabbcbccbccaacbbcbbbbabcbcaacbbcacabacbbccbccbcabcabbccbaaacabcccacbccbabcbbcbaccabbcabcabccbbcbcaabccaacbaacbababcbccabcbcaccaccbbcbbcacbaaaccbabbcbabcbaccbaacbcacbbaccbcbaabbcaacbcacabbcbbbcababccbabcbbacaaccbbacbbaabbbcbccbbacbccbcaaaaccbcacbabbabbacacaacaaabcbbabbbcbabcaaaaacacabbcbccaaacabacbaaababacbcacbabacbaaccaaacbbcacbbaccaabbacbcccbccabcacbcbacbabbaaabcbabccbaaaacaaacbbcaabbcaaaababcbaccaabbcaccbacccccaaababaaabcaacabbcbbcbcaabccbbbbcbabccabccbccaccaacccbaaaabbcbbacacccbaabbabbcbbbacaaaabcbbbbacabcaabcbcaaabaacccacbcbccbbcacaacbccbcccbbcaaaccbcbbaabababbaacbaaaccabaacbacbcaacacbbacaacabacabcabccabbcbbaccbcbcbccbbaaabcaabccaacbbcabcccacbccacbbaccabccbaaaaccbbcbacabbcccbbbaaacbbaccbacbbbcbbabbbbccacccacbaabaacbbccaababbacbccbcccccbbbbcbacbabcaababcaabbbabaaaabcacacbbbcababccbcaabacaaacaaaaacbaacabcacaaccabacaabacacccccaccabcbaaaccccabbaaacbabcabbabcaccbaaacaacbbabbcabbbabcaccccbccacabaaccbccacacaacccaabacbaccccabcacaaacaabacabbaaaacaabbacccbbaaaababaaaacbabcbcacbbcbbcbaabaabcbbacacccbbccccbbcabccabaccccccaaabcabacbbabbccbacaaacabccabccacbaabbcbbccbcbaaaabaaacbbbbcccbbbcaccaabcbbbccbacbcbacabbabaabbcababcccabbbcccacbcaaabbabbabbbbaaabbcacccabccaaacbcabbcccabacbbccbabcbbabbcaacbaabaccbbbaabccbacabcccabccaccbcbbccbcbacacaaccacabcaaacbccacbcabcaccbcccbaaababacbbaababaacbaacabcaccbbaccaabaacbccaacaabaabbcabcacabbbababbacaaaabcbbbcccaccbbbaacbabbbbacabbacacbbbbaccabacacbbababaccabcbccbbcacaacacacbbcacaabcaccccacabaabacabbbacbaaacbcacbbccbaacccbcbcacbcaccccabbacbccbabacbaaabbaccabaabaabacaaaabbbabaaabcbcacbaabcbababcabacaaccacacaccaacabaacaacbcbbaccbaacbabacaaccbaacbbaabaaccaccbaabbabbbbbcbccbcbacccacaacabacbbacabbbbbcccbccaaabacccbaacbaccabbccaaaabcbaaacccbbacbaaababbbbbbcabbabacacaaabbaccbcccbaaabbccccbacacbaccbbbcbbcaaaaabaaacaaabacbbaaaaccbaabcaccbabbcbbabcbcbcbccaaaabcaaabccabbaaccacbccaacabbcaaabcacaaabaabacaabcbbbbcaccbacaabaaacacbaaccbbacccaaababbbccbcccbccbabbabaabcbccbccbbacbbbabcbbccbbbccbcbcbcacbcbbcccbbcbaccaccbbabbaaabacbcacacbbacbbcccbbbcbbaacabacbcbcabbacbccbaaabcbbacbbbbbabcbaccabbabcbbcccacccaacbabacbbbcbbacbbabaaaaacacacaaccacaacbbbbaabbacbbaccbbbcccbcabcaaacbbaabacbccaababacccacaaabaccbcabacbaaacaaacabaccbbbabbbbcccabcaaacbcabbaaabbcccaaabbbaacaacbbacbbaaccacbcabcccabcbbbccbbcacbccbbbcabaaaacbbcaccaaaaacaccabcbbabacbcbcaabaaabbccabbbcaaacaaacbbabacccabaacbbbbaccbaabccacbaccbbaabbaaacbbacacacccbacaaaabacabaabaabcbbcbabcaaabacbcbabcccacacbccbccbcbbbbbabccbaaacabbcaacbcabcaabaacabbbaabbacaabbabaacbabccccacccccaaccacabcccacaabaababcbabaccbabaaabcaaaaacaccbcbcaaccbabaacaaabbcccbcababbabbccbcacabacbbcccbcacbbcbaabcababacacbcbaacbbbabbcbacbbaccbcbaabbbbbacabaabcbacaabbbccbbbaabbcbcccaacbcccabacbaaabbccbcbbabaccbbababcbaaacabbaccabbcaabbacaacacaaababbccbabacbabaabbacbbacacaabaabcccacbccbcabaacbccabbbabbbcbbaacbabbbaabbbaccacbacaabccccbacaaacabbaaababcccbabcbbaacababccacbbcbbaabacabccaabbccaaacacacbcaaaccacacacbccbabbbbcbbcbaabababaaccacbaabbccccbbbabbccbbaaacaabacacccbcaacbcbabcbbccbabaacbacabbacabbcbacbaabccbcabbababaaabcacabbabcaaaabbbabcccccaaaccbbacaabacaaabbcbcbabbbcacbacacaabcbcaaacbaabaaabcacbbbbaabbabaacacacbbcaababaabcbccccbaabcbcaaaaacabbbcabbbcaabbcbabcbbccacabcaaabbaccacaccbcaaacbccbcbbbbbccacccacbbcbbacacaabbcbbcccabbbbbaaabbbccbaaccbcaaaabccccacccabbabcaaabcabcccbabbcccbbbacaccbccabcabbcccaacababcbcccccacbccaabcbcacbbaacbcaababbcbabaaacbabacababaabaabaccbcbbcaacabbababccabbbaabcbcbacbacbccccaaccabacbaccbbbccacacaabaaaaacabbaccaccbbaaabaacabacaacbcbbbcaaaabccbcbcccbbcaaaacacbccbacbcbacbbaaabbbaaaacbbbbabbcccaabbaaccbaaaacbacbaacabacacbbcabbaaabaaccbbccbcaacaaabcabacccbbbcaabbcaaccacbbcbccbbcacabcacacbaaacbcbbbaaababbbbabccacabaaabcabaccabbcabcaacbbbabcbcababbaaccacaccbccbccbaacacacacccccababccbcbacbacabbccaacbbaabcbcaabbcacccbbacaaaccabccacaaaacabbacababacacacababbabaabbabcbcabcccabacbcbbabcaccacaacacabbcccbacaaccbbbabaccacbcaaacacacbabccabaaacbacaaacbaabcacbcbcaaaacbbabaacbacaabbaaaabbcbacaaabbbbcccacabacbaccabbabbcaaaacbbccbcbaabbacacabbccbcbaccaaacbabbacccaacaaaacbcacaaabcaacbccbabcaabacaaababccbcacccbbbaaccccbcaaacbbabcabaaaccbcabbcbbaaacacabcbbcccbaabbbccaccbacacbabbbbabccbbbacbaacbaaccbbccccbbbabacbacbabcabcbcabcabacaccbccbbabbbbbbbabaacaacccacbcbaabaabcbcaaababbaaaccabbcacbaaabbbaccbbacbbbacaaaabaabccbbbccabaacbccabccaabbccccbbaaaabbcabacbbccbccaccabcaacbcbcabaabcaabccabbcbbababcacbaacacaaabbccbbbbacccaaaabbacbcbcbaacbcbbcbbcbcacbcbccaccbcaaacccbcccacaccabacbabccaccaaaaabcaaaacccacbcaaabbcabbccaaaabbababacabcccacaaccbacabcbbcacbcacbabbcaccaaacbbacbababaacabbbcbacbaaccabccaacbbbccccabbababbaaabbccbccacbabacbaccabcbabcabacabacbcbbcbbbaaaccaaaacbbacbbaccbbcbabcaacaaababaabcabbbbacacabbacccbcbbabcacbcbabcacbaaccbabacbcbbbcbbaabbacccccccaacaacaababbbaaabacacbaabbcacbccbbaaaccaacacbaaacabcbbabccbbbacabaacbcbaaaabaccacbacbabcaccacbbacbccbcbabcacbccccabaccaabbbbbccbaaacbacbaabaabccbcabbcaacbaabaacbcbbccbcaccccbaacbbbabccbbacacabcbbccaabacabbcbacbaabcccbbabbbaacbcccabcbaabcbaacaabbcbcbbcaabcabcbccbbbbbcacbbbcbaaabbccbbccbaaccbacabbccbccbbaabbabcbbbbabccacbcbbaaabccaabbcacbcacaaaabbbbbbaacbaaabbacacbccccbccacacbcbaaccabbbacaccacbacbabababcbbbcababbaabaacbcbacababcbabacabcabcbcbabacbbbbbbabacacbabaabcccbbaccaacbacbaabcbcaacaabcbabccbcabcbcbcbcbbccabbaaacabcaacbcacccbabbcababccbcbbbbbacbacccbaccccbacccacbccaaaabaaccaabababbbcaabcbcbccbbacbccccccbaaaacaaccacbcbabacabcabaccbaccbcabcabccccaaababcabccabcbaccabcbbbaaccbaaaaaaccacccbbbbccbbbbaababacbacabcacbabcaabbabacababcacbbcbabbaaccabbbaccabbcaacbbacbccacccbcaccbacacccbbccaaacaabaaaccbabcbcccaabacabcabbbcaccbbbbcbccbbccacbbbcaaaccaaaabbbbbabababcbbacaccabaabcbabbaabbcaabaacbcbbabcbbcccbbbacbcabccbbbbcbcababacaaccbcbabcabcbcbbcbccbacbcccbbcaaabacabbabccacababccbbbcacbaababbccbcaccbaccbbbcbbabbbcccbbcabcbbccabbcbcccabcaabcbabcacccaccbabcbbbaccbabcacababcacbcbbbcbabcacbcccabcbbcbabbabcbbbaaaabcbbbaabbbcbaaabccaccabcaacacccaccbaabaccbababbbbcbbccbaacacbcabccbcbacaccaccaccbaccbbaababaacbcbacbcabbbaccaacacbcbabcbabbaabbacbacccaabaaaacbabccbabbcacacbccbcccaabcccbabbabccacbaabbccbaabacbbcbacacaccaacabbbababccbbacccbbaccabcabacaacbbbaccabaaababbbcaaabccaacabcccaabaccbccbbcabacaaacbaabaabababcabcabaaacaccaabacbaccccbccaacabaabaaabaccacccaacbaacaacabcbaabbabaababcccccbcbacaacacbbbaaccaacbcacccccacaabbcbcacbbbabbabbbcbbbbbbcabbbabbccbbcccabcbbbccbaacbacaacbcbaabbbabaabcbbbcccbcbcbaacbabbccbabccaabaccaaabcabaacacccbcbbaccbbaaabbabaaabbaccbbababcbabbbbcbbaccabbbbbbaabaccccacccbabbbaabacccacababcababacbbbbbabcbbbbaacbbcbaaacbabbcabacaaabaaabbbbaabaaaacabccbacaaacaccacaccbaaccbcbbccbabcaccaaacccbacabbabaccabacbcaaaccacbabcbbcaabccaaaababcccabccbaaabaccbbcacccabbcbbaccbcbacabccabacbaaaaaaccbacacabacabcbbcbaccbcaacbaabccabaaabbbbbabbaabbcbbaccccbbaabbaccbbcabbbaacccaaacabcbaabbaaaacccbabababbcbbccbcacaccbcaacbccaccbbaacbabbcbababbaccacbabababbacbccabbccbaacccbaacabcbcbabbbbbacbabaaaaaaabcaccaabacbcabaabbbacabbaccacbacbbbacbaccccbaacbcbaabaabacccacacaacccccacccacbbbccabaccbbbbbcaacbcbbabcacaabcacacacbaababacaaacabbabccacbabacbbaabcaabaaaacbabababcbaababbbcaccaaabaacabbbaacbbacacbcbcaabbcbaccbbacabcacaacbbcaccbcacbcababacaabacbbcbbbcaccbabbcbaacbbcaabcaccbbabaaccccaabaacbcbcbbbbbaacbcbcbaabcaaaaacabccccbcacabbcbcabbabbcaabbbccbbbccabacabaccccbcbabbbbabaccacaabababacababbbabcacbbbaccbabacacaaacacbbabaacbabcbbbccaaccccaabaaaacbbabbaabbaccbbcbbbacccacbccbbbabcacabaaabcabbbcbaabccabbbabcbaccccbabacbcaaaaacbcabbcaabaccaaaacaaabccaccccaabaaabcabaabcacaabccabcabccbaacbccabbaaababacbcbaccaabbbaacbcacabcaaabccbcabacabbccaaaacbcbbbbbcacababcbbbbbcacbbabccccbcbabbbcaacbcbbbcabbbcabcccabcaccbabbbbbccbacabbbaacacbaccbaabcaabcccbcbbababccabbabaacccccbaacbcbabaaabbcbaacabababcccbcababcccbcccabbabccccaccaacaacabacbabbcbbcbcaaaabaccaaccabbbaababbcccaababcbacaccacbccbcbcbcbbaccacacbbcbbcbaabaaacccabcbcacbccbcbbcabaacaaaaccbabbbaaacbcacacccabcbbcaabaacccacbabacaaacbabaaabbbbbcacbacabbaccacbbccaaacaabccbaaacabcbccbbcaaccbcccaacaccbcacaacabcacacbbaaaccbbabbcbbacbacabbacabaccccccbbbcbbaacbcacabbbcbaaccaccbcabbcccabbbbbbcbbbacbcabaaabcbaaccaacbccaaaacbcbbccccabbaaabcaaaabcbbabbaaaabbcbbbaaabbcabcccccccbcacbbcaccccacbbbbcccacabcbbaccbcccccbcacbaacccccbbaabaccbaaabcbbababaaacacaababbaaaacbacbccaabbcbcabababacabcbccbaabcbccbaabbacccbcbbbabacccbcbcacacbcccbbaacbcacbabbbbccaabbcaaacbaabcabbabbcccccbccaacccaccbbaccbcbaaccbaabcccbcbaccacbaababaccbbccaccbcabaabcabbbacbabaaacbcaaaccaaaccacbbaaaccabcbccaabcacbcbcccacbccaabcbaaaaccbabbabacbbbcbacccbaaaacaaacbcbccbaacaabbaccbacbbcbcbaaacbaacbaacaaacccbcacabaccacbbaccbaaabacbbbaabcccaaaacbabccaababaacccccabccaabbababbabaabbccbabacbbbcaccabcaabbbbcbccabaacacbbbccbaacbacbbcabbccbccbcbaaabacaaaabbacbbcccccaabccbaaabbaabccaabaccabbaccabaabbabacbabaacbccbabbaabbacacccacbabbcbbcbcbcbcabccaacbabacbccabaababbcccccbacacccacbacbaacabcacaaacacbcbaccbbccaaccbccbbbcbbcbaacbbbabaabcacaababbccacbbcabcbbcacaccbaaaaabbaaaaabbcacaccbbccbcacacbaaaacbcbabacacbababcabcacaaaabacbcaaacccbbcbbbcbaacacbacaabbcabacabbbbaccaccccbcaabacacabcacbabcbaacabacacbbcacabbbbccaaaccacabcaabcbbbaacacabbaacbabcaacbabcbabacaabcacbacacccacaaabcacccbccbcbbabaaabaaaabbcbbbccabccaabacaccbcbaacbaacbacabccbbaccbccbacbbacbbcbaabcacbcccacabcbbacabbbcacbbbbaabbbccbcbbcbabacccabaabccbcacacaaaccccaabbbcbcbbbacabcacbcbcbbbcbbbbccbacbababacabcabcabbcabcccbbbcaabccaccbcbbacbcbaccaabacacaccbacabbacbaaaabcccabbaabcbccacbcbbaacaaaacbcbbcbbcccbcbcbbcbccccccbacaccaabccaabbccabaaacbbbabbbbacacabaaacbbaccbcaaaaaccbaccbbabcbabbcaaccaccbcccbbaccacbccacbbaabcccccbbcbaaccbbabcbcabccacbbcbccccccccccaabcacbcaabbacbabccccbccbbbbabbbcbccaccaabcabacbbcbaabaaaaaccabaacbccccabccabbbaccccbbcccabacaaccacbccbaaaacaacbccaacacbbaccccacbbccaacabccacbacababbacaccabcbbbaabacaaaaccbcbacbaccbcbbacabaccbcbbbbccacccabcaabccbcbbabbcbabbbcabbccccabaccbacabcbbbbcaaacaabaacabbabccccbaabccbcacacabbbcbccaaccbcbbacacaccbbccbccaacaaacabcabbbaaccbaabaabcacbabccbbaaccbbacbcaacabcabcbbacaaabacbaacbabcacacaaccaabbbbcbcababbbbabbababcbabbbbcacbbbbccaacaacbaaaacbcabbbaabaabcabacccabbcccbcbcbcacbcccabbccccabaaabababccbcbabacaacaccbabcbaaacabbaabacabbbacbbcbbccbabcaaacccbacccabcbbccccbcaacabababacbbcbacbbbaaabacbbbbbcabaabcaacabacbcbcbccaaacbabcbbabbbbccbabacbaaccaaacbaccbbaaaacababcabcbaccbbcabaabcbbbabacccccababcacaacacabbbbcacbacbaabbabcaabaaaaabcbacaaacbcbbbccbcccaacbcabccaccbbbccaaaccacbacabcbcbcbaabccbaccabcbabbbccaacccaaaababccccbaaabacccbcbacbaccbbbabbbacabaacbcaacbaaacbaacabbcbacabcbabbabbcbaaacabcccabcbcababcbaacabccaabacababbabbcbbaabacabbcbbbaccbbbcbcaccacabcccbacaaaaaaacaccbaababaabcaccabbaacbabcbbccbbabaabcbcbaacaccabaccaacbacabbababcbbbcacaacacbcbabcbbbabbbccacbaaabcabaccabbabcaccbcccbabacbbbcccbbbcbcaacbbacaacacacbccccbccaaccbbbccacbccbccaababcaaaaabbccbbccbaababccababbacabbaaaaacbbabbcaacaacbbbcccbbbcabcabbcbbabcbcbcacabaaaaacaaccbcccacbbaccaabacbbbbabacbcaabbbaaaaccaacacccbbcaacbbacacabbaaabacbaccaabbbaacccaacbabbbbaaabccbaaabbaabccaabcababaabacbaaccbbccccccbbacacbaabbcaabcbbbacaaccbaaababcbcbbbabbbbbacaabbaacbcaabbaccbacbbcacbcccaaabaaaabccbbcacaabbabbcabbaccbbabbcaccccbcbcaccacccbcbbcaacbcaacbbbcaacccbbacababaacbaabacaababbcbcbaabccabcbcabbcbbcbaaaaabaaaccbaaabacccaabcabbaacccbcbcbbbaccbcccccbbcbcacaabbcaccaaabcccacbbbbabaccbacccabcacacbaccacbbbbbaaaacacbacbbcbabaaaaccbcbccbbabcaacbaacaaacaabbacacbcaccbccaaccaacacbabacbbbabbcaacbcbbbaaaacccacbaacaaacbcaccaaccbcacbbabacccbccacaaacbbcbcbaabaabbacbbcacbbaabcabcaccbaccbcbbbccccbbbbcaaaabbcbcababcbbacbabcacbcabcaaacbbbcaaabbcaccacbcaacbacccabcbcbcbcaccbcacaabcacacaabaabccbbcabbcccccbcbaccabbbcacbcbbaaabaabbaacbbababaabaaccccacabcbbcababbccabaaaabcccbcaccccaaabbacacbccbaaaccccccbaaacabcccaaaaaabccccccacacabcaabaccaacbcbaabccbacacabaaaabbcbabcbaaacaaabacaacabbaacbbbabababbbcabcbacbabacaccbbbbaaabccbcccbccbbcacabcbacbbcbbaabacbaaaacaaabbacbacbccaacabcbbbaaabaaccbcacccaaacccacbaabcbcacacbacabaacbccbbabcbccaccacaccbcbbaccbacbcccaabcbcabaacbababbcabbacabcaabaabcaaabaccccbcacacccbaaaacccacaabcabbacbbcaaabcaccbabacccaccbaaaaabbbcbaccbaaaabcacacaaaaabbbcbbcacbacacabcabcbbabbacaaacbbcbacbababaababcbbccbacbbbcaccbabaccbcaabbaacbabaccbcababbbccaabaabbbbacaaaccbcbbaabbaabcabacbcbbbbbacacaaacaabcaabacaaaccbaabcacacccccabbbcaaaccaaccbabbacccbbbbcbaacccabcbbbcccabcacaccbbcccacbabcbbaacacaccaabcacbbbaaaabccbbcbacacacbbbbaacacaacbbaaabcbbacacbbbbcaaaaaabbcaacbcbbaabccacbbcbaaaabacacccbcaabaccbacbcabaabacabcacbaabcccccabbcaabbcaacababacbbbcacbcbbcbaccacabaaacabcbbaabbabcacbabcbbacbacabacbcbbcbacbcaaaccabccccbcbcbcccaaccabcabcaccabbbbbaaaacbbcccabccaabaccaccccabccbcaaccacbabbbbcccabbacbbbaababbcbccbccaabbaaabababcaccbcbbcccacacacacccbbaacbababccababbaacbcacbcccccacabaaccaccabcabcaccbaabccaaccabbbabbacccbaaacbbabcbacbacbbacaabccccaacaccbcabcbbcbcbacaaccaacaccacbbcbccccccaacbabbbcccabaabacaaacbbacbaababbbccbcccccbccacbcabbacaacabcabbcccabaccababbaabaacaaacbccbbbccbcaaaaacbaccbaccbcccabaccabbbbacccbbcacbaaaabcaabbbcaaabccabcbabbaaaacbabcbcbcabacbccaabbbcaaacbcacaaabcabbcabccbccaacbccbcbcacaaabaccbcbbbcbcccabbbcbabcaabacacabacccbabbaccbbbabcbababcbbbcbaacaabbaccbaaacacacbaaaaabbcacaacaccaacaabacbabaaabbacbcbacccccbbcbcabaccbcababbcccbcaabbacaaaabcaababcbbcaccbbabcaccbbabcabababbacbcbbcbcacacaaaaacbacbabaababccbbaccccabaaaabcabacbbbaaaabbbccabcabcbccbcaccacbcccbbcbcabccbcbcacacaacbbbccbcbbccacbcbabbbccbcaccbccbaccbcbccbbbacccacbacaacbcababcbaaacccabcbcacbacabaccaaccbabcbbcacbacaccbbababbccbaaccccbcaabbabbccbccaaababacaccccbcbcaababaaabbbbbbbaaccccbaacabacbacaabcbacccbbacacbcbacbcbacaabbbabcbcaccaacabaccabaacaaacccccbcaaabacaccacacccbbabbccccbacbccacaabbcbaaacccbccbabbbbbabbaaaaacbcbbccccccbbbccccbbbcaaabacbbabaabcbcbcbaabbbbcbcbabcabcaaccbabbbabbcbccbacbaaccaacbcccbbcbabbbbbcbacbccabbabacbaaccabbbcbcbbbcccbccbcbcbbbbccacaabbbaaabbaabaabbaabacbbbcabbbbbcacbbccabaaaabbcbacbccccacabcbaccabababaacbbccaacbbcccaccbaaaccccbcbacbbbcbabbcbccbccccbacaaccabbbacccabcbcaccbcbcccbaabcaabbaaacaaabbccbccabbcbaaaabacbcaccabbcbacabaaccacbbaacababaabcacccabbcccababacbbcabcacaababbcbabaacbbbbbbacccbbcbabaccbcbcabcbabaacbbccbcbabaaaabcccbcacbaccbbbbccabaacacabbbcacabaaacabccacabbacaababcbabbbbaabbaccbaaaacaaacacababbbbacbbbbacbcacbabaaaacbacbcbccabccbaccacbabcbbccccbaaccaccbcabbcacaaaacbbbbcabcbccbbcbabaacbbbcabcbabcbbccbcbaabacbbacaabacbbcbbbaccaacbcabccbbaaabbaaaccabbbbabcccbcaaaaabcaaabcbcbcacbbaccabccbbcbabcbbbbaacbbcccbaaabcbccabacaacccacccbacbbaccbacabbcacabbcaabccaacbabaaaaccbabaaaacababbccbaabaabccccbbbcacabaaccacbbccbaccabbacccaaabcabcccbcacbbabbcacacaaaccbaacbccacbabaabaccabbabbabaaacbacccabbcaccababbcbccbbaaaacccbbbcabcacabacbccccacbcbbcbbaaaccbacaacabcbaaaccacabcabcabbbbccacaccabbbccbaccabbacbbacccccbcbbaaababccabaaaacccaabbccacbcbcccbacaaabcaccbaaaaacbaccabcbbcbbcacbbaabbccacbbccccacbcbbabaaacbccaaababcacbbacbccbabacbbacbccaccccbcaabacbcbccaaacbbbbbbaabcbacbaaccbbbaabbccaaabbccbbbbabcacabaacccaacbbbaaccabccbcbcbabbbacabcbabacaabbaacabaaaccbabbabbcabaacaccbabbbbbbbcbcbaaabccacacbacaabbbcababbccbccbcbacaaacbaaaccaccbbbbbaacaaabccaccccbababccbaaaacabbacacaaabbbcbacaabccababaacbbbbbbbabacacabcabcbbcbbcbcabcaacbbcacaaaaabcbacabcbabbaaccbacabbaaaaccbbaabbaacbbcbbcbccbcbabaaaaccabbccabcccaaccbcccbcaccccacbbcbcbaaabbbabbcbccbaccaacbabcaccbaccacccbbcbbaaabaacaaababcabbbcabaabbbabbcbaccaacbaabbccabcbccbbbccccaaaaaabaaccabbcacbcbabcacaabcbcacabbcbacbbbaaaaccbcabbcccbabcbaaaacbccaababccccacaaabbbcbabaaababaababacabaabaacbcaaabcbbcbcabbcbbbababacbcbabaaccaccaabccaaaabbcaaabbaaaccaacacaacaabbbaccbbcbcabbbbbcacacaabbbbcaccabcbabccacabbaabbcabacbacacbcacabacababbaaaaabbcabbabcaccaaacaacbbbcababbbbcbbabbcabababacaaacaaabaccaccabacccabbbababaabbcaaabcbcbbcbbbbaccabccbabacababccaaaabbbbbaabbcaaacacccbbaaabcaaabcacbbcbbccbbccbbcbbaababbcabccbacbbcbcacbcbabcbbacacbbacccbaccccbcbbcaacbcbaacbcaabccbccaaccabbcbaaccbbccaacbcccabcabcbbacccccaaaaaabbabbbbabcacabcbcabbbcabbbaaabccabbaacbbacbabcaacccacabccaabaccaabbabcbccbcaaabcaccaccababcbcacbacbaccbcaacababaaaaacccaabcbccbbacaccbcbabbaaaccbbbbbcbcaccaaaacaccbcaaccabcbacccbbcbabbbbcbcabcaabcccabcaccabacacbbbabaaccabbbbabbccbbbacacaabbcacacccbbcaaacbcccabbacabccaabbabbbabaaabccaabaacacbbcbbbacaabacabbbccccacbcbbacaacaacbbaaacbbbbbaaaaaaababbabccbaccbbacbbcabaacaabcaaaacabaccbbbaacabbbbcbacbbbaabbbacabbbbccbacbaaacbcacccacbbaacbbbbabbcbbccbcbccabbbbcbbabbaaabacaccacacaabaaaaabaccbbaccbaabbbacacbcbcbbbcbcbccbccaaacaaccbacaababbccaaabaabaabbaacbacabbcacbacaabaababbbbcabbabbcaaaccbacaaccbabcbabaacccbbacacccaaabcacaaccbaccbbabbbacaaababaababacccabbbcbaccabacaccbaccbbbbabbcabccacbbcacabaaacbccacabccacbbcbaababababaabcbbbcaaacababbcaaaccbcbacbababcacaabaacacabbaacaabaaccaabbbbcaacaccaaaccabbabbbaacaccbbaacacccbbacaacaaaccabacbcbcacbacbcabcabacbacccbabbbcbccabaaaabaccababbbbbbcacbcbabcabcacacbaacaccaacabaabaabbabbabbccaccaccaaababccaccacbabbaabbcaaaaabccaabbbbaccbcabbacaababccbcacaacbabccacbcabaccacbaaabaccabbabaabccaaaccbbabbccbacabcacabbcaaaabcbccabaaccbbbbbbcbbcbbccaaccbaabcaaccbbccaccbcbcaccaacbcbccabbccaabcbbabacabaabbcbbacacbbbcacabcacbcbbabbabcaacccaacbbaabccbbaabbaacccccbabacccabbcabbccaccaacbcaaabbabbcabbbbbabaaaccaccaaacabcccaccbcabcbabcbccbabcacccbacbaacbcabcbaaabacabcbcababbcbbcccbbbbaccaccabbaabbcbabcbcbaabccbcbabcaabcacbcabcaccaccaccbbbacaccacabccbbcbabcbacbaacacbbbccccabbcbcbabcabaaabbacbacbaabccacacaccccbaaaabccabaaaaacbaabcababccbbbcbbcaaccccacaaaacbacbcbcbabccbbcbbcbabbbaccbabcaabbacccccbaaccabbccbcbbbccbacbccaaacbaababacbbbbcacbcaabacacacbaacbcbbcbcbcaabbbcbbbacbbccbcabaaaaaabbcaabcbcbaccabbbbcabbacbabbbabbcacbbbbbcaccbcbaccbabbcbabcabbccacbbbabcababcbcbacbbacacbccababaabbbaacbacaaccaccbccbabbcbbcaacabcbcbaccccbcbcbcbacaabacbbabcbbacbbcaacbacbcabcaaacaaaccbacaabcabcccaacbaacaabbaabacacbacbababccbabbbcbabcbbabbaabccccbcbbbbbaccbccaaabcbbbbbacabccabbacabaccbbababaabbacaabcbacaacacccabbbabaabcacaccacabbaaacccaacbbcbaaaaacaccabacbcabcabcaabacbaacacbbbccabbacbcbcbabbbcbcccbbcbacacacbcccbcbcbbacacaabbbccacbbacabccbabaccaabcabbcccacacbcbbcacbaabaabaabbcacccaacabccaacabbaaabbccaccabccbaaacacbaaccbacacabbbcbbcaaabcaacccbaccbcbbaabacabcaaabbcbbcbacbccabbabcbbcbccbcbabcaababacbabbaaaaaabbbababbbcbccabcabbabacbbcbbabccaabccacbcaaacbbcababbbccabbabbaaaacbcaababcccbaaccabcbcbbabacbbabccaacccbbaaabcabbcabcacabccabcccaccccccbaacaabcbbcabaabbaccaacaabbbacbcbbbabbacabbcccccacaabcabaabaabbbabcbbabbaaccccacabababccbbbaacacbcacbabccbbabaccbacbcacaabbacaccccbaacabaaacccacbacaaccacbaabccbcaaabaabbcbbaabbcbccbcbccbcbaccabaacaacabacacaccbbabbacbbbcccacacbcabbaacbbbbbcbacababbcacccabacaaaacbcbbcbaabbcbcccbcbccbcbaccbbaabbcacacabaaaaaccabcacbaabaaaacacabcccccabbbcacbabbbbcaabaabbabcabccbcccbabbabacabaaacaacccbbaacbbaaabacaccbcaacbbcaaabcbcbaacabccabcbababbbacacaaaacabbbccbcbaabababbabbbbaabacbccaabbccccccbcbbccaccacbbbaabbcacbccabacbccccaccaabbacabbccccbabaaccccbbabcccbcacaabbacabbbbbbacbabccccccaacbbcbaacbaccbacaccabbabbbcabacabacbbcbabbaabccaaaccccbabbbcbcccaaabacccbccbccacbbbbabbbccabaccaabbabcacababccacabacabbbbbbbbcbbccbcbabbacbbbcbcbbbaacabaacbbcacaaabccbcccbcbcacbbbccacbabccbccacabbaccbaaabbcccbbabcccccbbccacbccccbaaaaababcabacaabacbabccbcbabcaacbababaacbacacbaabcbacabaccababbbbbaccbcaccbacaacaacbbaacccbacabcaacabbaaaaacccabbcaacabcccbcacbbcbabacacacaccaccbcaabcbcbbbbcbccaaacbccbaacaaaaccacbaaaaacaccccccaacbccbcccbccaaabbbbaacbbbaaabaacbccabbbcaccacbbcabcbcaccacccbaacababbaaaabacbccacbcababbbbaacaacaccaccbbaabcbbaaccccbbbcbababacacacbbacbaaaabacacccbcabcaaaabcbbcbaacccbbbbabcaaccbccccaccbcacccbacccbacaabbacbbaaacabaccabcabbcabccacabaaabbbacbbbbccbabbcaaabccaaacbccababacccaaaaccaccaccaccbacbbacabbcabaccbbaacabcbcaaaacbabacabaabccaacbabbabccacabaaccbbcacbabaaccaabbbabccccabbbababbcacabcccabacabacbccacabcbbccaccabaabacbcccaaccabcbabbcacbcbbccacabbaaacbccbcacccbaccaabbaabbabaaaaccacbbccababcaabcbcbbbbcabbccbbcbcbbaaaccccacbbabbaccaacbacbccacccabcbabaaabcbaabccbcbcbaabcbbababbcabcacbbbcabcbcaacbaccbcabaabbccabaaacacabaccacbbcbcbcccbabcacabbbaccbbcacbcbccbbacaacbabbcacbbabacccbbccbabaccbbcccacccaccccabbcbbcacabacbcaacacaccaacacaacacaababcbbbcbacaabcbccaacabcacbcacbbbaaacacbcbccabcaccaaabbcaaabcabccbbacaaabbbabcbabcccccccccbbbaabaccbcabaacabaababcccbcbababcaaacccbcbcccbabacbccbbaaacaaaacabaabaacbbabbbaaccbaaacbccabacbbcccbbcbabaabcacaccacbcbabacbcbcbcaabbacaacaacbbabccbaacacbabaabcbbccaccbbbcaacbbcaccbbcbaaabbcbcbacbbaacabcbaccbccbaacabbbcacbcccaccbbaaccccbbcabcaabccbbbabaacbcabcbbbbcbaacbccbacbcaaabbcaaaaaabbaababaabaaacbcbbbaaababaacbacbabbcbbcbabcaccaacababbbbcaaaabbbaabbabacaaaaacccbaccaabbbabbbcbabaaabcaacbcbbcbbbbbabbccabbabccccbacbbabaaccbbaabbabbcacaaacaccabbaaacabccaacacacbbcacbaabbacaccbbabbccbacbcccabacccbaabbabcbbccacabbcbaabbbabbcaaaabbccbbcccaababbccaabbbabcbabbcbcaabbaacbbcabbaabbcaabcaaccabacaacbaabbbbbacabcaaabbcbcbbbaaccbaacccaccbbaabaabbbbabcbcabbaccbaacccabbbabbcaaaabcbbcaabbcccbcabbbcbbccccbabbcbccacbabbcaabcabccaccccbabbcabacbababbcbbcbbbcccbcaabcbbcabaccbabcbacabcbbabcaacccaaabcabacbccaaabcabaccccbaaabbbccbbabbabccbabccbabbccccbbccacbcabbcabaccaaaabccbbcaaaccacbabbccabbbcacaacaabaabcbbaacacabbabbcbaacaaccaabbccccaccaaacbaaacaccabcbbacbcacaaaacaaaccaacbaaccbbacbaabccbbcbacbccaabbabbacacbcbaacbbcabbbabcccccbcacaabbaabaacbbbbbcccbcacbbabcaabcabbacabbbcacababcbabbbbcbaaccabacaabbaaabcbcccaabaacbbacacbacbacacccbcbacaaccbaababbacaacacbbacbcaaabacbbbcbbcccbacaabaabbabaabbcabbbbcbbcccbabcbaccbaabbcbccaabababbcacaaaccbbbaacaaababbbacbaaaaaabbacbaacbcbacaccacbcccaacbbabacbcabbcabacabbccbbccbaaababbaccbbbbabbacabbacbccaccaabbbabbcbbaccaaabbbcbaabcbbbbcbbbabbbbbabcacacaacabcaaaaaabbaccaaccbcacccaacaabcbaababcbcbbbaabbcbbaaccccaaaccabcabcbbacbaaaacabacbabcccbcaccbbabbabcbbabbccbbcbcababaccbcbbaccaaaabbbbcaaaccabbbcabacaccbaabcabbcaacabbbaaababbcccabacaabbcaaaababacbccbccbcaccbaacbcbbabbaaacbacbbbbbacaaacccbcaaaabbccacbcbabbababcccacacaabacacbbcccaacabcccacccababaabccbcaabcccbacbacbababacaabacaabababbababaabcaacaacacaccaacaaccbbaabccabaaacccacacbbbcabbbcaaaaccbaacaaccbaabbabaccbbcbabbabbcccbcaccaccabbacbbbabcccabcbbbbabaabcbcccbacbacbaabbcbcbabbbccaabbcacacbcbbbaaccaabcbccbbaccabaacacbcacbbababbabcaacbbbbccbabbbbacababbabacaabacabbcaaaccbbbbcaaabcacabacaaacbccccabccbababcbaaccaacbbbccaacabbbbbcacbcaaabbaababacacccbccccbcabccbccbacccacababbccbacbcbcababaaccabbaccacccababccccaccbbcaaaaccaccabcaabbcbcaabbbaacbabaabaaabbacabaccbbccbcabcaacaaacacccbcbccacabbcbaacbccbaaccacacacababacbbbbacbbcccacbcbbcbbcbaacbcabcbaacaacccacabaccbbabacbaaaabbbcccbbabbabaccccabacaababccccaccbabcabccbbaccbaabcacaabacccbcacaacabacbbcccaccacacacabbbbbccbaaabbcbaccbaaaabbcabcbaacbbcccabbabbbbaccacbabcacacbaabcaacbccacacbcccbcbacaabacaacbacccaabaabbabcaaccacababaabacaabaacbccabcaaacbbbacccacaaaaaaaabcccaabcaaccbabacbbaabbcabbbbbcbcabbbbbccacacbabbaabbcbacabbacbaaabcaaccbbbbabacbcaaababacbacbacbccccacbcbccbccabacbaacaabcbacccacbcaccbcaaccbbbabbbbaaccbbabbabcbbccbcbbcbaccbbababbaabbbbbcaaaabaccababccacaccaabbbcbccacbcabcbbcccccacbabcbcbccaabababbcbbabbababcccabacbcccbcaaabacbacbacaaaaacbbabacaacbcabaaaccabcabacccabcbabacaaccbcccaaabbaccbcbbaccccbcccbaaacccbccaccccabccbababcbccccbbcbaacccaabbcbbbbcabaabcbaabcbbaaccacabcbbccccbabcccbacababbacccccaaabbabcbcbabcbbabbbbaaccaccaaacacaababbbcbabcbcbaacbaacbbcccbabbbbaccbacbcabbbbcbcbaaacaacccbbcbbbaababacccccbbccbaaaabbbacbcacabcbbababcbbbaaaaccccababcabbaacbaaccabcbaababcbbcacaabcaaaccbccabcababacccabaccbbacaabbabccccaaaabbbbabcabaabbbbccabcacccccaaacbbbacbcacaccccacbcaabbbcababcaacaaccaacbbacaaccccbababbbbbbcccbccbbacaabcacbccbbabbaababcbbacaccbabacbbbcbbabcabbaabcabcacaacccbcbaabbaccabaccccabacbaccaaababbabaaaabbbaaacbcacbbcacabaabccaacacacabacbbbbbcabbbbbccbbcabcbbcacbcbcccaabccbcaaabaccaabacccccbb")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1360Test.java b/src/test/java/com/fishercoder/_1360Test.java deleted file mode 100644 index e752d0e8bd..0000000000 --- a/src/test/java/com/fishercoder/_1360Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1360; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1360Test { - private static _1360.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1360.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.daysBetweenDates("2019-06-29", "2019-06-30")); - } - - @Test - public void test2() { - assertEquals(15, solution1.daysBetweenDates("2020-01-15", "2019-12-31")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1361Test.java b/src/test/java/com/fishercoder/_1361Test.java deleted file mode 100644 index eb1addd3ed..0000000000 --- a/src/test/java/com/fishercoder/_1361Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1361; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1361Test { - private static _1361.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1361.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.validateBinaryTreeNodes(4, new int[]{1, -1, 3, -1}, new int[]{2, -1, -1, -1})); - } - - @Test - public void test2() { - assertEquals(false, solution1.validateBinaryTreeNodes(4, new int[]{1, -1, 3, -1}, new int[]{2, 3, -1, -1})); - } - - @Test - public void test3() { - assertEquals(false, solution1.validateBinaryTreeNodes(2, new int[]{1, 0}, new int[]{-1, -1})); - } - - @Test - public void test4() { - assertEquals(false, solution1.validateBinaryTreeNodes(6, new int[]{1, -1, -1, 4, -1, -1}, new int[]{2, -1, -1, 5, -1, -1})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1362Test.java b/src/test/java/com/fishercoder/_1362Test.java deleted file mode 100644 index a9c09351eb..0000000000 --- a/src/test/java/com/fishercoder/_1362Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._1362; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1362Test { - private static _1362.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1362.Solution1(); - } - - @Test - public void test1() { - CommonUtils.printArray(solution1.closestDivisors(8)); - } - - @Test - public void test2() { - CommonUtils.printArray(solution1.closestDivisors(123)); - } - - @Test - public void test3() { - CommonUtils.printArray(solution1.closestDivisors(999)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1365Test.java b/src/test/java/com/fishercoder/_1365Test.java deleted file mode 100644 index 1bda6e3a24..0000000000 --- a/src/test/java/com/fishercoder/_1365Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1365; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1365Test { - private static _1365.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _1365.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{8, 1, 2, 2, 3}; - assertArrayEquals(new int[]{4, 0, 1, 1, 3}, solution1.smallerNumbersThanCurrent(nums)); - } - - @Test - public void test2() { - nums = new int[]{6, 5, 4, 8}; - assertArrayEquals(new int[]{2, 1, 0, 3}, solution1.smallerNumbersThanCurrent(nums)); - } - - @Test - public void test3() { - nums = new int[]{7, 7, 7, 7}; - assertArrayEquals(new int[]{0, 0, 0, 0}, solution1.smallerNumbersThanCurrent(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1366Test.java b/src/test/java/com/fishercoder/_1366Test.java deleted file mode 100644 index 8db41361b8..0000000000 --- a/src/test/java/com/fishercoder/_1366Test.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1366; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1366Test { - private static _1366.Solution1 solution1; - private static String[] votes; - - @Test - public void test1() { - solution1 = new _1366.Solution1(); - votes = new String[]{"ABC", "ACB", "ABC", "ACB", "ACB"}; - assertEquals("ACB", solution1.rankTeams(votes)); - } - - @Test - public void test2() { - solution1 = new _1366.Solution1(); - votes = new String[]{"WXYZ", "XYZW"}; - assertEquals("XWYZ", solution1.rankTeams(votes)); - } - - @Test - public void test3() { - solution1 = new _1366.Solution1(); - votes = new String[]{"ZMNAGUEDSJYLBOPHRQICWFXTVK"}; - assertEquals("ZMNAGUEDSJYLBOPHRQICWFXTVK", solution1.rankTeams(votes)); - } - - @Test - public void test4() { - solution1 = new _1366.Solution1(); - votes = new String[]{"BCA", "CAB", "CBA", "ABC", "ACB", "BAC"}; - assertEquals("ABC", solution1.rankTeams(votes)); - } - - @Test - public void test5() { - solution1 = new _1366.Solution1(); - votes = new String[]{"M", "M", "M", "M"}; - assertEquals("M", solution1.rankTeams(votes)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1367Test.java b/src/test/java/com/fishercoder/_1367Test.java deleted file mode 100644 index a82cb7e11b..0000000000 --- a/src/test/java/com/fishercoder/_1367Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1367; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1367Test { - private static _1367.Solution1 solution1; - - @Test - public void test1() { - solution1 = new _1367.Solution1(); - ListNode head = LinkedListUtils.contructLinkedList(new int[]{4, 2, 8}); - TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, 3)); - assertEquals(true, solution1.isSubPath(head, root)); - } - - @Test - public void test2() { - solution1 = new _1367.Solution1(); - ListNode head = LinkedListUtils.contructLinkedList(new int[]{1, 4, 2, 6}); - TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, 3)); - assertEquals(true, solution1.isSubPath(head, root)); - } - - @Test - public void test3() { - solution1 = new _1367.Solution1(); - ListNode head = LinkedListUtils.contructLinkedList(new int[]{1, 4, 2, 6, 8}); - TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, 3)); - assertEquals(false, solution1.isSubPath(head, root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_136Test.java b/src/test/java/com/fishercoder/_136Test.java deleted file mode 100644 index 8b61857fe1..0000000000 --- a/src/test/java/com/fishercoder/_136Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._136; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _136Test { - private static _136.Solution1 solution1; - private static _136.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _136.Solution1(); - solution2 = new _136.Solution2(); - } - - @Test - public void test1() { - assertEquals(1, (solution1.singleNumber(new int[]{2, 2, 1}))); - assertEquals(1, (solution2.singleNumber(new int[]{2, 2, 1}))); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1370Test.java b/src/test/java/com/fishercoder/_1370Test.java deleted file mode 100644 index e44d8593cc..0000000000 --- a/src/test/java/com/fishercoder/_1370Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1370; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1370Test { - private static _1370.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1370.Solution1(); - } - - @Test - public void test1() { - assertEquals("abccbaabccba", solution1.sortString("aaaabbbbcccc")); - } - - @Test - public void test2() { - assertEquals("art", solution1.sortString("rat")); - } - - @Test - public void test3() { - assertEquals("cdelotee", solution1.sortString("leetcode")); - } - - @Test - public void test4() { - assertEquals("ggggggg", solution1.sortString("ggggggg")); - } - - @Test - public void test5() { - assertEquals("ops", solution1.sortString("spo")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1371Test.java b/src/test/java/com/fishercoder/_1371Test.java deleted file mode 100644 index 3406f4800a..0000000000 --- a/src/test/java/com/fishercoder/_1371Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1371; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1371Test { - private static _1371.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1371.Solution1(); - } - - @Test - public void test1() { - assertEquals(13, solution1.findTheLongestSubstring("eleetminicoworoep")); - } - - @Test - public void test2() { - assertEquals(5, solution1.findTheLongestSubstring("leetcodeisgreat")); - } - - @Test - public void test3() { - assertEquals(6, solution1.findTheLongestSubstring("bcbcbc")); - } - - @Test - public void test4() { - assertEquals(1, solution1.findTheLongestSubstring("id")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1372Test.java b/src/test/java/com/fishercoder/_1372Test.java deleted file mode 100644 index d27ce710e9..0000000000 --- a/src/test/java/com/fishercoder/_1372Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1372; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1372Test { - private static _1372.Solution1 solution1; - private static TreeNode root; - - @Test - public void test1() { - solution1 = new _1372.Solution1(); - root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 1, 1, 1, null, null, 1, 1, null, 1, null, null, null, 1, null, 1)); - TreeUtils.printBinaryTree(root); - assertEquals(3, solution1.longestZigZag(root)); - } - - @Test - public void test2() { - solution1 = new _1372.Solution1(); - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 1, null, 1, null, null, 1, 1, null, 1)); - TreeUtils.printBinaryTree(root); - assertEquals(4, solution1.longestZigZag(root)); - } - - @Test - public void test3() { - solution1 = new _1372.Solution1(); - root = TreeUtils.constructBinaryTree(Arrays.asList(1)); - TreeUtils.printBinaryTree(root); - assertEquals(0, solution1.longestZigZag(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1373Test.java b/src/test/java/com/fishercoder/_1373Test.java deleted file mode 100644 index 7f6a8697b2..0000000000 --- a/src/test/java/com/fishercoder/_1373Test.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1373; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1373Test { - private static _1373.Solution1 solution1; - private static TreeNode root; - - @Test - public void test1() { - solution1 = new _1373.Solution1(); - root = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3)); - TreeUtils.printBinaryTree(root); - assertEquals(6, solution1.maxSumBST(root)); - } - - @Test - public void test2() { - solution1 = new _1373.Solution1(); - root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 8, 3, null, 6, 3)); - TreeUtils.printBinaryTree(root); - assertEquals(7, solution1.maxSumBST(root)); - } - - @Test - public void test3() { - solution1 = new _1373.Solution1(); - root = TreeUtils.constructBinaryTree(Arrays.asList(-4, -2, -5)); - TreeUtils.printBinaryTree(root); - assertEquals(0, solution1.maxSumBST(root)); - } - - @Test - public void test4() { - solution1 = new _1373.Solution1(); - root = TreeUtils.constructBinaryTree(Arrays.asList(4, 3, null, 1, 2)); - TreeUtils.printBinaryTree(root); - assertEquals(2, solution1.maxSumBST(root)); - } - - @Test - public void test5() { - solution1 = new _1373.Solution1(); - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 4, 3, 2, 4, 2, 5, null, null, null, null, null, null, 4, 6)); - TreeUtils.printBinaryTree(root); - assertEquals(20, solution1.maxSumBST(root)); - } - - @Test - public void test6() { - solution1 = new _1373.Solution1(); - root = TreeUtils.constructBinaryTree(Arrays.asList(4, 8, null, 6, 1, 9, null, -5, 4, null, null, null, -3, null, 10)); - TreeUtils.printBinaryTree(root); - assertEquals(14, solution1.maxSumBST(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1374Test.java b/src/test/java/com/fishercoder/_1374Test.java deleted file mode 100644 index 57f96f99aa..0000000000 --- a/src/test/java/com/fishercoder/_1374Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1374; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1374Test { - private static _1374.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1374.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.generateTheString(1).length()); - } - - @Test - public void test2() { - assertEquals(4, solution1.generateTheString(4).length()); - } - - @Test - public void test3() { - assertEquals(2, solution1.generateTheString(2).length()); - } - - @Test - public void test4() { - assertEquals(7, solution1.generateTheString(7).length()); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1375Test.java b/src/test/java/com/fishercoder/_1375Test.java deleted file mode 100644 index ec4f5397f6..0000000000 --- a/src/test/java/com/fishercoder/_1375Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1375; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1375Test { - private static _1375.Solution1 solution1; - private static int[] light; - - @BeforeClass - public static void setup() { - solution1 = new _1375.Solution1(); - } - - @Test - public void test1() { - light = new int[]{2, 1, 3, 5, 4}; - assertEquals(3, solution1.numTimesAllBlue(light)); - } - - @Test - public void test2() { - light = new int[]{3, 2, 4, 1, 5}; - assertEquals(2, solution1.numTimesAllBlue(light)); - } - - @Test - public void test3() { - light = new int[]{1, 2, 3, 4, 5, 6}; - assertEquals(6, solution1.numTimesAllBlue(light)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1376Test.java b/src/test/java/com/fishercoder/_1376Test.java deleted file mode 100644 index 2c3cec5a7f..0000000000 --- a/src/test/java/com/fishercoder/_1376Test.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1376; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1376Test { - private static _1376.Solution1 solution1; - private static int[] manager; - private static int[] informTime; - - @Test - public void test1() { - solution1 = new _1376.Solution1(); - manager = new int[]{-1}; - informTime = new int[]{0}; - assertEquals(0, solution1.numOfMinutes(1, 0, manager, informTime)); - } - - @Test - public void test2() { - solution1 = new _1376.Solution1(); - manager = new int[]{2, 2, -1, 2, 2, 2}; - informTime = new int[]{0, 0, 1, 0, 0, 0}; - assertEquals(1, solution1.numOfMinutes(6, 2, manager, informTime)); - } - - @Test - public void test3() { - solution1 = new _1376.Solution1(); - manager = new int[]{1, 2, 3, 4, 5, 6, -1}; - informTime = new int[]{0, 6, 5, 4, 3, 2, 1}; - assertEquals(21, solution1.numOfMinutes(7, 6, manager, informTime)); - } - - @Test - public void test4() { - solution1 = new _1376.Solution1(); - manager = new int[]{-1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6}; - informTime = new int[]{1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}; - assertEquals(3, solution1.numOfMinutes(15, 0, manager, informTime)); - } - - @Test - public void test5() { - solution1 = new _1376.Solution1(); - manager = new int[]{3, 3, -1, 2}; - informTime = new int[]{0, 0, 162, 914}; - assertEquals(1076, solution1.numOfMinutes(4, 2, manager, informTime)); - } - - @Test - public void test6() { - solution1 = new _1376.Solution1(); - manager = new int[]{5, 9, 6, 10, -1, 8, 9, 1, 9, 3, 4}; - informTime = new int[]{0, 213, 0, 253, 686, 170, 975, 0, 261, 309, 337}; - assertEquals(2560, solution1.numOfMinutes(11, 4, manager, informTime)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1377Test.java b/src/test/java/com/fishercoder/_1377Test.java deleted file mode 100644 index b885bea9ed..0000000000 --- a/src/test/java/com/fishercoder/_1377Test.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1377; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1377Test { - private static _1377.Solution1 solution1; - private static int[][] edges; - - @BeforeClass - public static void setup() { - solution1 = new _1377.Solution1(); - } - - @Test - public void test1() { - edges = new int[][]{ - {1, 2}, - {1, 3}, - {1, 7}, - {2, 4}, - {2, 6}, - {3, 5}, - }; - assertEquals(0.16666666666666666, solution1.frogPosition(7, edges, 2, 4), 0); - } - - @Test - public void test2() { - edges = new int[][]{ - {1, 2}, - {1, 3}, - {1, 7}, - {2, 4}, - {2, 6}, - {3, 5}, - }; - assertEquals(0.3333333333333333, solution1.frogPosition(7, edges, 1, 7), 0); - } - - @Test - public void test3() { - edges = new int[][]{ - {1, 2}, - {1, 3}, - {1, 7}, - {2, 4}, - {2, 6}, - {3, 5}, - }; - assertEquals(0.16666666666666666, solution1.frogPosition(7, edges, 20, 6), 0); - } - - @Test - public void test4() { - edges = new int[][]{ - {2, 1}, - {3, 2}, - }; - assertEquals(1.0, solution1.frogPosition(3, edges, 1, 2), 0); - } - - @Test - public void test5() { - edges = new int[][]{ - {2, 1}, - {3, 2}, - {4, 1}, - {5, 1}, - {6, 4}, - {7, 1}, - {8, 7}, - }; - assertEquals(0.0, solution1.frogPosition(8, edges, 7, 7), 0); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1379Test.java b/src/test/java/com/fishercoder/_1379Test.java deleted file mode 100644 index 4980e46b9f..0000000000 --- a/src/test/java/com/fishercoder/_1379Test.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1379; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -public class _1379Test { - private static _1379.Solution1 solution1; - private static TreeNode original; - private static TreeNode cloned; - private static TreeNode target; - - @BeforeClass - public static void setup() { - solution1 = new _1379.Solution1(); - } - - @Test - public void test1() { - original = TreeUtils.constructBinaryTree(Arrays.asList(7, 4, 3, null, null, 6, 19)); - cloned = TreeUtils.constructBinaryTree(Arrays.asList(7, 4, 3, null, null, 6, 19)); - target = TreeUtils.constructBinaryTree(Arrays.asList(3, 6, 19)); - TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); - } - - @Test - public void test2() { - original = TreeUtils.constructBinaryTree(Arrays.asList(7)); - cloned = TreeUtils.constructBinaryTree(Arrays.asList(7)); - target = TreeUtils.constructBinaryTree(Arrays.asList(7)); - TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); - } - - @Test - public void test3() { - original = TreeUtils.constructBinaryTree(Arrays.asList(8, null, 6, null, 5, null, 4, null, 3, null, 2, null, 1)); - cloned = TreeUtils.constructBinaryTree(Arrays.asList(8, null, 6, null, 5, null, 4, null, 3, null, 2, null, 1)); - target = TreeUtils.constructBinaryTree(Arrays.asList(4, null, 3, null, 2, null, 1)); - TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); - } - - @Test - public void test4() { - original = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - cloned = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - target = TreeUtils.constructBinaryTree(Arrays.asList(5, 10)); - TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); - } - - @Test - public void test5() { - original = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 3)); - cloned = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 3)); - target = TreeUtils.constructBinaryTree(Arrays.asList(2, 3)); - TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1380Test.java b/src/test/java/com/fishercoder/_1380Test.java deleted file mode 100644 index 95f870926e..0000000000 --- a/src/test/java/com/fishercoder/_1380Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1380; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1380Test { - private static _1380.Solution1 solution1; - private static int[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _1380.Solution1(); - } - - @Test - public void test1() { - matrix = new int[][]{ - {3, 7, 8}, - {9, 11, 13}, - {15, 16, 17} - }; - assertEquals(Arrays.asList(15), solution1.luckyNumbers(matrix)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1381Test.java b/src/test/java/com/fishercoder/_1381Test.java deleted file mode 100644 index 277042fade..0000000000 --- a/src/test/java/com/fishercoder/_1381Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1381; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1381Test { - private static _1381.Solution1.CustomStack customStack; - - @Test - public void test1() { - customStack = new _1381.Solution1.CustomStack(3); - customStack.push(1); - customStack.push(2); - assertEquals(2, customStack.pop()); - customStack.push(2); - customStack.push(3); - customStack.push(4); - customStack.increment(5, 100); - customStack.increment(2, 100); - assertEquals(103, customStack.pop()); - assertEquals(202, customStack.pop()); - assertEquals(201, customStack.pop()); - assertEquals(-1, customStack.pop()); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1382Test.java b/src/test/java/com/fishercoder/_1382Test.java deleted file mode 100644 index c4cda392fe..0000000000 --- a/src/test/java/com/fishercoder/_1382Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1382; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -public class _1382Test { - private static _1382.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1382.Solution1(); - } - - @Test - public void test1() { - TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2, null, 3, null, 4, null, null)); - TreeUtils.printBinaryTree(root); - TreeUtils.printBinaryTree(solution1.balanceBST(root)); - } - - @Test - public void test2() { - TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2, null, 3, null, 4, null, 5, null, null)); - TreeUtils.printBinaryTree(root); - TreeUtils.printBinaryTree(solution1.balanceBST(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1385Test.java b/src/test/java/com/fishercoder/_1385Test.java deleted file mode 100644 index 819501b06f..0000000000 --- a/src/test/java/com/fishercoder/_1385Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1385; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1385Test { - private static _1385.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1385.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.findTheDistanceValue(new int[]{4, 5, 8}, new int[]{10, 9, 1, 8}, 2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1386Test.java b/src/test/java/com/fishercoder/_1386Test.java deleted file mode 100644 index 3842ce7f1a..0000000000 --- a/src/test/java/com/fishercoder/_1386Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1386; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1386Test { - private static _1386.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1386.Solution1(); - } - - @Test - public void test1() { - assertEquals(4, solution1.maxNumberOfFamilies(3, new int[][]{ - {1, 2}, - {1, 3}, - {1, 8}, - {2, 6}, - {3, 1}, - {3, 10}, - })); - } - - @Test - public void test2() { - assertEquals(2, solution1.maxNumberOfFamilies(2, new int[][]{ - {2, 1}, - {1, 8}, - {2, 6}, - })); - } - - @Test - public void test3() { - assertEquals(4, solution1.maxNumberOfFamilies(4, new int[][]{ - {4, 3}, - {1, 4}, - {4, 6}, - {1, 7}, - })); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1387Test.java b/src/test/java/com/fishercoder/_1387Test.java deleted file mode 100644 index 75ee13d6b1..0000000000 --- a/src/test/java/com/fishercoder/_1387Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1387; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1387Test { - private static _1387.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1387.Solution1(); - } - - @Test - public void test1() { - assertEquals(13, solution1.getKth(12, 15, 2)); - } - - @Test - public void test2() { - assertEquals(1, solution1.getKth(1, 1, 1)); - } - - @Test - public void test3() { - assertEquals(7, solution1.getKth(7, 11, 4)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1388Test.java b/src/test/java/com/fishercoder/_1388Test.java deleted file mode 100644 index 63fb138f67..0000000000 --- a/src/test/java/com/fishercoder/_1388Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1388; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1388Test { - private static _1388.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1388.Solution1(); - } - - @Test - public void test1() { - assertEquals(10, solution1.maxSizeSlices(new int[]{1, 2, 3, 4, 5, 6})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1389Test.java b/src/test/java/com/fishercoder/_1389Test.java deleted file mode 100644 index d0ccc6a630..0000000000 --- a/src/test/java/com/fishercoder/_1389Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1389; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1389Test { - private static _1389.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1389.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new int[]{0, 4, 1, 3, 2}, solution1.createTargetArray(new int[]{0, 1, 2, 3, 4}, new int[]{0, 1, 2, 2, 1})); - } - - @Test - public void test2() { - assertArrayEquals(new int[]{0, 1, 2, 3, 4}, solution1.createTargetArray(new int[]{1, 2, 3, 4, 0}, new int[]{0, 1, 2, 3, 0})); - } - - @Test - public void test3() { - assertArrayEquals(new int[]{1}, solution1.createTargetArray(new int[]{1}, new int[]{0})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1390Test.java b/src/test/java/com/fishercoder/_1390Test.java deleted file mode 100644 index dcfb2b9c2c..0000000000 --- a/src/test/java/com/fishercoder/_1390Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1390; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1390Test { - private static _1390.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1390.Solution1(); - } - - @Test - public void test1() { - assertEquals(32, solution1.sumFourDivisors(new int[]{21, 4, 7})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1392Test.java b/src/test/java/com/fishercoder/_1392Test.java deleted file mode 100644 index c5ceda4c91..0000000000 --- a/src/test/java/com/fishercoder/_1392Test.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1392; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1392Test { - - private static _1392.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1392.Solution1(); - } - - @Test - public void test1() { - assertEquals("l", solution1.longestPrefix("level")); - } - - @Test - public void test2() { - assertEquals("abab", solution1.longestPrefix("ababab")); - } - - @Test - public void test3() { - assertEquals("leet", solution1.longestPrefix("leetcodeleet")); - } - - @Test - public void test4() { - assertEquals("", solution1.longestPrefix("a")); - } - - @Test - public void test5() { - assertEquals("aaaa", solution1.longestPrefix("aaaaa")); - } - - @Test - public void test6() { - assertEquals("", solution1.longestPrefix("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab")); - } - - @Test - public void test7() { - assertEquals("abbbaaabbabbaaaaaabbabaabbabbaabbabaababbbabbaabaabbbabbaabbbbbbabaaaaabbababaabaaaabaaaaabaabbbabbbbaaaababbbbbaabbaabbbaaabaabaababaaababaabbaaaabbbaababbbaabaabaaabbbbabbaaabbbabaabbbabaabbbbaabaaaaabbabbbaabaabaaaaabaabaabbaaaabaabbaabbbabaabaababaabababbbabbaaabbbabababbbbababaaaabbbbababbbabbaaabbabbaaaaaaabbbabbbbbaabababbbbabbaaabababbbaaabbbbbbbbabababbbaabbbabbbaababbbaabbbaababaabbbaaaaaaabaabbabbaaaaaaababababbbbbaaaaabbababaabababbbabbaaaaaaaabbabbaabbbaaaabbabaaababaabaabbaababaababaabbabbabbbaaaabbbaabaabbbbababaabbbaaaabbbbbaaabbaabaaabbbaaaabbbababababbabbbbbaaababbaabbaaabbbabbbabaaaabbbaabbabaababbababbaaababbbaabbaabbaababbababaabbbababaaaabbabaaaaaababababbaabaababbbaaabaabaababbbbaaabbbbbaaabbabaaabbbaabaaabaabaababababaabaaaaaaaabbaabbbaaabababbbbbbabbbabbbbabbbabaabbbabbbaaabaaababaaaaababbabbbbaaaabbbababaabababbabbaaaabaabbabbbbbbbabbabbbbbbababbbabbabbabbabaabaaabbbbaaabbbbaabbabbbaaaaaabbabbbbbbaabbababbaaaabbbabaababaabbbaabbaabbaaaaabaaababaabbabaabaabaabbaabaaaabbabaaabaabababbaabbababbbabbabbbbabbbbababbabbbbbaabbaabababbaabaababaabaaabbaabababaabaaaababbbaabbabbaaaabbabaaaaaababbaaabaaabbabaaabaaaaaabbbbbbaabaabbababaaabababaaabbbbbabaaababbaaabbbbabaaababaaaabaababababbbabbbbabbbabbbbaabbbabbaabbaabaabbbababaaabbaabaaaabaabbbbbbababbbababaabababbbaabbbbbabbaaaaaabbababbbabaaabaabaabbbaabaaabbbabaaaabababbbbaaaabbaaabbbaababaaaaabaabbaababaabbabaabbaaabbbbbaababababbbabbbaaaaaaabaaaabbabaaababbaaaaabababababaaababbbabaaabaaaaabaaaaaaabaabbbaabbbbbabaaaababaaaaaaaabbaabbbbbbabaaabbababbbbbaaaabaaabababaababababbbaabaabaabaaabaabbaababbbbaabbaaabbaaaababaaababababbbaaababbababaaabbabaaabbaaaaaaababaababbaaaabbbbaababbbabaaabababaabbbabbabababbababbaaabaabbbbbbaaaabbbbababbabaaabababbbbbbbbabbbbaaabaaababaaaababaabababbbabaaabaabbbbbbbbbaabaaababbabababbbaabbaababbaaaabbbabababaaabbbaaabbaababbabbaababbbabbbaabbbbaaaaaaaabbabaaabbabbaaaabbbaaaaaaabbbbaaaabbabaabbabbbbbabaabbbbbaaabbbabbbaabbaabbabbabbaaabaabaabbaabbbbaabbaaaaabaaaaabababaaababaababababbbbaabababbbbaabaabbaaaababbbaaababaaaabbbabababaabbbabbbbaababbabaaaabababbaaaabaabaabbbababbbbabbaaaaaaababbabbbabaaabaabbaabaaabbbbaabbaabbababaabaaabaababbaabbbbbbaaababbbbbbababbaabaaabbbaaababaaaaabbabaaaaaaaabbbababbabababbabbbaababaaaabaabaabbbbbbaabbbaaaababaaaabbbaabaaaaabbabaabaaabaaabbabbabaaaaabaaaabbaabaaabaabbbabaaaabbaabababaaaabaaababbbbaabbbbbabaababbababaaaababbbbabbaaababbabbabbaabababbbbaabbaaabaabababbbabbbabbbbabaaabbbabbaaabbabbbababbbbaaaaabbbaaabbaaaabbabbaabaaabbbbbaabbbabbabaabbabbbaaaabbbbaaaabaabbaabababbbbbabbbabbbbaabbabbaaaaaabaababaaaaabbabaabbbbbaaaabbaabbaabbbaabaaaabbabbbbbbaaaabbbbaaaaaaabaaabbaababaaaaaaabaaabababbbbabbababbbbbaaabbabbaaaabbaaabbbabbbaababbaaabbbbbaaaaabaaaabaaaaaaaabaaaabaababbabababbaabababbaabbaabaababbbababbbbbabaaabaabaabbabbbababbbbabaaaaaabbbaaaabaaabbaaabbabbbbaaabbaaaabbaabbbbbbababaabbbabbabababaabaabbaabaaaababbabababbabbaaabbabbaabbbbbaaaaabbbabaabaaabaaababbabaaaabaababbabbaaaaabababbbbaaabaaabbababbabbbbabaaaaaabaaabbabbaabbaabaaabbbbbaaaababaaaababbabaaaabbabaabbabbaababbbaaabaabbabbabbbabbbabbbbbbbbabbabbaababbbabaaaaaaaaaaababbbaabbbabbbaababababaaabbabaababbaabbaaaaabababbbbbaabbabbabaabaaabaaaabbabbbbaabbabbbaababbabbaabbababaaabbaaabbbbaaaabbbbabbbbabbbabbbbaaabbaaabaabaabbaabbabbaaaaababbbaaabbbbbbbbbbaaaabbaaaabbaabaaaaabaabbbbbaabaaabbabbabaababbbbbbbaabaaababbaaabaabbababaaaababbabbabbbbaabbbaaabbbbbaaaabbbababbbabbaabaaabbabaabbabaaaaaaaabababbbababbbbabbbbbabbaaaabbabaabaabbabaaaaabbaababaabbaabaababbaaaaabbaaaabbbbaabaaabbabaaaabbababbaaabababaaaaabaaabaaabbbaababababaabbabbbbbbabbaaaaabbbaabaaaaabbbaabaaabbaaabababbabbbbbbbaaaabbaaabbabbbbbbbbbaabababbbbabbbbabbbbaaaabaababbbabbaabababababbaaababbaaabababaaabaaabaaabaabbbbbbbbbabbbaaabbabbbbabbbababaaaaaabbbbbbbbbbaababbbbaaaababaabbaabbaabaabababbbbabbabbbbbabbabbbabaaabbaababaabababbabaabbbbbaabbbbabbbabaaabbbbaaaaabbbabbbbababbbabaabbaabbabbaabbabbaabbbbabbbbabbaabaaaaaaabaaaabbaaaabbbbbabababbbbaabbbbbbabbbaaaababbbbababbbbabaabaabbaabaabbaaabaabbbbabaaaabbabbabaaaaaababbbbbbabbaabaaababbabbbaaaaabaaaabbaaabbbbaaabaaaabbbaabaababaababababaabaaabbaabaabbaabbbbabbabbbbaaabbabaabbbaabbabaaaaabbaabaabababbbaabbbabaabaaaababaabbbbbabaaaabaabbbbbbbbbabbaaaaabbbbabbbaaabbbbbbaababbaababbbbbbbbbababbabbbabbabaabaaabababbbbababaabbbbabbaabbbabaababbbbbbbabaabaabbbabbbaaabbbabaaaabbbbbbbbabbaababbbbbaabbbababbbbbaaaaabbbbbbbbabbabbbababababaababbbaaabbabbababbabaaaaaabaaabaabbaabaaabaabaabbbabbabaaaaabbbabbabbabbbbababbabbbabbbabaababaaaaaabbaabaaaaaaabbabaabaabbbbaaaaaababaaabbbbababbaabbaaababaaabbaaabaabbbbbbabbbaabaaabbabaabbbbbaaaabaabbbbbabbabaaaaaaaaaabbbbbaabbbbaabbbbbbaabbababbbaabaabbbabbaabaabbbbaaaabbabaabaabbabbabababbbbbbabbbbbbaabbabaababaabaababababbabbaaabbbbaaababbbaaaaaaaaabaabbbbababbbbaaaaabbbbbbbabaaaaaabbabbbababbbbbabbaaaaabbaabbbaabbbaaaaabaaaaaabbaaabbbbbbbbbbbbbababbbaabbbbbabaaababbbbaabaabbbbabaaaaababbbbbbbbbbbaabaaaaabaaabababbbabbaabaabaaaababbababaaaabbabababbbababbabbaababbaabaaabaabaaaababbbbabbbabbbbaaabbbbbbbbaaabaababaabbbbaabababaaaabbaababbaababababbaabababbbbbbaabaabaaaaabbbbaabbaaabaaabbbaababababababbbabababaaaabbaaaaaababaabbaabbaababbbababbaababaaabbbaababbbababbbabbabbbbababbbbbabbabbbbaaabbbbbaaabaaabbabbaabbaababbbaaabbbabababbbbbbabbbaaababaaaaaaaaabaabaaaababbbaababbbbbaabbabaaabbaaaabbabbaababbbaabbbababaababaabbbbbababbaaaaaaabbaaaabaaaaaaaabaaabbbaabbaababaabaabbbaaabbaaaabbbaaabaabbbbaaaabbbbbaaabbaaabbbababaaaaabbbbbaaabbaabbbabbabbbbababaaabbbabbbaaabbbaaabaaabbaaaabaaabbabbabbbaabbaaabbabbbaabbaababbbaababaaabababbaabaabbaabbbabbbaabbabbabbabbababbaabaabbbbaabaabbabbbbbbbbbbaabbababbabaaabaaabbababbabbbbbbaabaabbaababaabababbbbbabaabaabbaaaababaaaabbbbbbaabaabbbbaababbbbabbbaaaaabaababababbabbababaabaabbbbbbaaabbaabbabbabbbaabbbaabbbbaabbbbbbbaabbaabaaaabbabbbaabababbbabaaabbbbbbbbabbbbbbababababaaaaaabaaabaabbbbbabbbbabbbaaabbaaababbaabbbbbaaaaabbbabaaabbaaaabaaababaaaabaabbabbabaabaaaaaaabaababaabaaaaaaabbaaaabaababbbbbbababbbabbaabbbbbbbbabababbbbbbbabbbabababaaabaabbaabbbaababbaabaaabbbaaaabababababbbbbbbbaababbbababbabbbaaaaaabbabbabaabaababbabaabbaabbbaaabbaaababbaaabaabbbbbbbaaabaabbbaabbbabaabaaabaaaabaaabbbbbbbbbaababbaabaaaabbbaabbbbaaababaabaaabaaaaabaababbaabaaaabbbabbababaaaabaaaabbaaaaaababababaaabbaabbabbbaaabaaaaaaaabababbabaaaabbabbabbbbbaaaaaaabbbbbaaabbaabbbbbaabbabbbabbabbbaaaababaaabbbababaaabbbbbbbbbabaabaaabbbaaabababbabbababababbbbbabbbabaaaabbbabbbabbbaaabbaabbaabbabbaabaaabbbbaaaabababbabaaaabbbabaaaaaabbabbaabbaaaaaaaabaaabaaabbbbaabbbbbaabbaabaababaaaabbbbbaabaabbabbbbabbbaabaabaaabbabbbabaabaabbabbabbabaabbababababbababbaaabaaabbbabbaaaaababbbaaaaabbbaaaaabbabaaabaabbaaabbbaaabbabbbaabbabbbabbabbbbbbbbbbaaababaaabbababbbaababbaabbaababbbbababbbabababbabbbabbabababbaaabbaabaaaabbbaaabbababbbaaaaaabaabbabaabaabaaaaabbbabaaabaaaaabbbbabbbbbabbaababbabaabbbabbaababbbbbbaabaabbabbbbbbbaaabbbbabbaaabbababbbabbabbabbabaaaaabbaaaaabbaaaabaaabbbbaabbabaababbbbababbbbbbbaabbbabbaaaabaabbbaabbaaabbabaaaaabbaaabaababbbbbbaabaaabababbabaabaabbbabaabbbbbbbaabaaabbbbaabababbaabaaaababbbabbabbaabbbbaaabaabbbababbbaaaabbaababbababaaabaaaabaaaaaaaaabbbbbabbaaabbabaaabbaaabaabaababaaaabbbaababaabaaaabbbabbbbaaababaaabbbabbabbbababaaababbabaabbbaaabbaabbbabbaabbbabaabbaaababbbabbaaaabbaaababbabbaaabaabbbbbbaaaaaaabababbabaabaaabbabbaaaaabbabababbbbababbbbababbbbaabbababaaaaababbabaaababaaaabbaabbbabbaaaaaabaaabbabbbaabbabbaaabbabbaabbbabbaaaaaabbbabbbaababbbbbaabbaaaaaababbabbbaaaaabababbaaabbbaabbabbaaababbbbbaabbbbbabbbababaaabbabbaabaaabbbbbaabbabbbbabbbbaabbbbbbbbaaabaababaabbaabaaababbaababbbbbabaababbbaabbbabbbbbababaaabbbabbaaaababbabbaaaabababbbbabbbabaabbbbaaabbaaaabaabbabaabbbaabbbabbbbbabbabaabaaabbbaabbaabbbbbbbbababbbabbbbabababbaaababaabbbbbaabbbbbbaaaabababbababaabbaaabaaabbaabbaaabbbbbabbabbabbaaaabababaaababababaaabbabbbaabbaaabababbabbbaaabbbbaabbaabbabbbaaaaabbbbabbbbbabbaaababaabbbbaabbbbaaaabaababbbabbaabbbabbbbbaaaaabbababbbaabbbbaaabbaaabbbaaaaaaabbabbbbbabbbbaababaabbbaaabbabbbbbbaabbabbabbaababbaaababbabbabaabaabbbaaabbabaaaaababbabbabababbabbbbabaaababbaabbaabbaababbabaabbabbbbbabbaabbbbababbbbaaabbbaabaabbababaaaabaababbbbabaaababbbbababbbabbbbbbbbabbaababaabaaaabbbbbbaabbaaabbbababbbbabbbbbabbbbaabababaabbbaababaaaabbabbaaaabaaababbaabbbaababaabaabbabbabaabaabbbbbbaaaababbbbbbbaabbbababababaaabbaabbaabbaaaaaaabbaaabbbaabbababaaabbaaaaabababbabbabbbaabaaaaababbbbbabaaaababababaabaabbbbabbabaababbbabbaababababbaabaabbbbaabbabbbaaabbaaaabbaabbaabbbbabbbbabbbbbabbbaaabaaabbaaabaababbbbbbbbbbbbaabaaabbbbabbabbbbbbaaabbaabbabaaaabaabbbbbabbbababbabbabaabaabaaababbbbbbbbabababaaaabbbaababbaabaaabaababbbaababbbbbabaabaabbbbabaababbabababbaaabaababbababbbabbbbbbabaabababaaabbabbababbbbbabbbbbbbbabaaabbaaaaabaaaaabbaaabaaaaaaabaaabbabbbbbbabbbabbbbbababbbbbabaabaaaabbabbbababbbbbabaabbbaaababaaaaaabbabaabbbbbbbbabbaabaabaabbabbabbbababbbbabaababaababaabbabbbabbbabaaabaaabaababbaabaaababababbbaaabbbabbbbbbbbbabbabbababbbbbbbaaaaabbaabbababbbbabaaabbababaabaaaaaaababbabababbaabaabaaababbbabbbbbabababaabbbabaaabaabaabbaabbbbbbbaabaabbaababbbaaabbbabbaaabaabbbbaaaabbaababbababbbaabaaaabbbabbabaabaababbaababbbbbbababbbbbbaaabaabbbbababaabbbbaabbbabbabaaabbaaaaabbbabbaaaabbabaabbbaaaabbababbabbbaabaabbbbaabbabaaababaabaaaaabaabbabbbabbbaabbababbababaababbbababaaabaabbabaabbbbaaaaaaaababbbbbbbbbbababbbbababbbbbabaaabbbbbabbbabaaaabbbaabaaabaaabbbabbbbaaaabaaabababbbabaaaabbbbbabbbbaabbabaabbabababaabbbababbbabbbabbbbbbbbaaabbabbaabbabbbbbbbbbbbbbbbabaaaababababbbabaaababbabbaaabbabbababbaaaaaaaaabbaabbaababbbabbaabbabaabababaabaaaabbababbbabaabbbaaaaabaababbaabbaabbaaabaaabbbbaaababbbabababababbbbaababbbababaabaaaaaababbbaabbaaaababbabbaabbbbbbbbabbaabbbbbaabaababbaaaaaabaaaabbbabaabbbaaabbbababbbaababbbabbaabaaabbabaaaabaabaaaabbbababaabbaabaabaabaabbaabbbbbbbabababaabbbbababaababaaabbbbaabbbbbbaabbbbaaabbbaabababbaababababbaaaabbbbbabababbbbbbaababaabbbabaabbbabbabbbbbababababbababbbbaaabaaaaaabbbbbbbbbbaaaaaaabbaaabaaaababbaaaaabbbaaaaababbaaabbbbbaaabbaabbababaabbaabaaaaabbaaabbbaaababbabaaabbabbabbabbbabbababbbaabaaaaaaaababbbbbaabbaabbaaaaaaabbbbbbbabbabbabbbababbbababbaabbbabbbaaabbbbaabaabaaaaabaaaaabbbaaababbbababbbabbabaabaabaaaabababaabbbabbbababbaaabaababbbaaabaaaabaaaabbbbaabbabababababababababbabbaabbaaabbbbaabbbabbbbbbaaaaabbbaabbbbababbbabaaaaabaabbabbaabbaabaaababaaaabaababbbaabbabbaaaaabaaabaababbababbababbaaabbbbbababbbbaabbaabaabbabbbbbaabbbaababbbaaabababbaabbabaaabaabbaaaabaaaabababbbbbbbbbaabbaaaaabbabaabbbbaaabbaaaaaaabaabbbabbabbbbbbaaabbaaabbbababbabbbababbbbaaabaabaabbbaabaababbaabbaaababbbabbaabaabaababbabababaabbabbaabbaabaabbbbbaabbbabbabbbabbbbaabbbbaaabaaabbaaaaababbbababbbabbbbabaabbaabababaaabbbbaaaaabbbbabbbabbbbbaabbaabbbbaaaabbbbbbbbabbbaabaaabbbbabaabaababbbbabaabaabaabbbbbbbbaaabbbaabbaaababbabbaaabbabbbbbabbaababaabbaabbaabbabbaabbabbaabaaaabbaaaaaaabbbbaaaabaaabbbbbaabaababbbaabbababbbbabbabbbaaaaabbaabbababbaaaaaababbabbaababaaaaabaabbababbbbbaabbaaaabaaabaabbbbbaabbabbbabaaabaabababbbaaaaabbbbbbbabbaaaaaaaababbaaaaaabababaaabbbbaaabbabaaabbabaaaabbaaaaaabababaaaabbaaabbbabbbbabbbabbaabbbabaabbbbbbaababaabbaabaabbbbbabbbababbabaaabbabbbbababaaaaaaaaaabbbbaaaabbabaabbaaaaaaaaaabbaaabbabbbbbababbbbbbbbbbaabbbbbbabaabbbaaaabbaabbaaaaaaabaabbaaabbabbbababbbbabbabbbbabaaaaaabaabbaaaaaaabbabababbabbabbbbbbabaaabbabbbabbaaabbbaabbbbbabaabbbbababababbabbbbbbabbbbbabbbabbabaaabbababbabbabaaaaaabaabbabbaaababbaabbbaaabbabaabbbabaaaabaabbbabbaaababaaaabbabbbabaaaaaababaabbaabbbbabbaababaababaabaaaaabbaaaaaabbbbabbbaaababbababaaababaababaabbabbbabbabbaabbaabbbbabbaaaaaaababaaaabaabbbbbaabaabbbaababbbaaabbaaaabbabbaababbaabbbaabbabaaaabbabbbbaaabaaabbabbaabaabababaaaabaaaaabbaaababbaaabaaabaaababaabbbabbabaabaabbaaababbbbababbaaababbabbbabbbbabaaabbabbaaabbbababbababababbaabbbbbababababaababbbabaaaabbaabaaaabababaabbabbaabaaaababaaaabaabbbababbaaabaaaabaabbbbababaaaaabaabababbaaaaabbbababbbaababbabbaaaababaaaaabaabaaaaaabaaabbbabababababaabbbabaaabbaabbbbabaabaaabababbbaabbaabbaabbababaaaaabbbbabaabbabbabbbbaabbbaaaabbbaabaabbabbababbaaaaaaabbaabaaaaaabbabbabaabaaabbaaabbababbabbbbbbabbaabbaabababbaabaaabaabbbbbbbabbaaaabaaaaaabbabbbbaaabbaabbbbaaaabababaababbabaaaabbabbbbaabbbbbbbbbbbaabbaabbbaabbbbbaaababbbaabbabaabbaabbbbabbbabaaabbaabababbabaabaabbbaaaaabaabbbaabbaabbbaabbababbababbababbbaaaabaabbbabbaaaabaaabbaaaabbbbbbaabaaaabbababbaabaababbababaaaaaabbabbbbbababbabaababaabababaababbbaabaaaabbabbaaababbaabaaababbbaaabbaabbbbbbabbbbbbbbabaabaabbbbbabababbabaaabaaabaaaabbbababaababbabbaaaabababababaabbbababbabbbbbabaabbabbaabaaababbabbbbaabbabbabbbbababaaabbababbaabaaaabbbbabbbabaaababaabbaaaabbbaaaabababaabbbabababbbbbbaababbababbaabbbbaaaabaabbaaaabbbabbaaabaaaabbababaaaaaaabaabbaaabbaabaabbbaaabbbbbaababbbbbbbababbaababaabbbaababbbabbbbbbbaabaaaabbabaabaababaaaaabbaaaaabaabababaaaaaaabbabaabababbbaaaabababbbbbbbaababbbabaaaaaababbabbabaabaaaaaaaabbaabaabaabbbababbbbababababbaabaaaabbbbabbababaaabbaaabbbababbbbabbbaabbbbbaabaaaaaaabbaaabbbbbaaabbbababbbabbbaabbabaaaababbaaabbbbbababbaaaabbbbaaaaabaabbbbaaabbbababaaabbbbababaaaaaaaaababbaaabaababbabbabbaababababbababbbaaaaaabaabaabaaababaaaababbababbbababbbabbaabbbbbaabbbbaabbbbbabaaaaabbbabababaaabbbaabbbaaaaababbbaaaaaaabbbbaababbbbbababbabbababbabbaaabbabaaaabbbbabbaabbabbaaabbbababbbbaaabbaaaabbbbaaabbbabbbababbababaaaaabaaabaaabbabbbbabababbbbbbbabaabbbbbababbaaaabbabbabaabbabbababbbabbababbaaaaabbaabaaabaabbbabbbabababaabaabbbbbbabbaababbaaabbababbabbaaaabaabbabbbabbaaaabababbbaabbbaaababaabaaaabbabbbaaaabaaabaabbbaaaaabbbbbbbbaaaaabbbababbbaaabbaaabaababbaabbaaaabababbbaabaaabaaabbbbaaaabbbaaabbbbbaabbaabbbaaabbbabababbbbbbaaaaaabababbabaabbbbababbabbbababbbababbbbabbbbabaaaaabbaaaaaababaabbabbaabaababbaaabbaaaabbbabbbbaabbabaabaababaababaaabbbbbaabbabbabaabaabbabbbabbabababbbbbaabbbaabaaaaabbbbabaaaabbabbbaabaabbabaababbbabaaabababbbaabbababbaaabaaaabbaaabaaaabbbbbabbbabbbbbaaabaabbabababababbaaaaaabaaaabaabbbababbabababbbaabbabbabbaababbabbbbaaabbabbababbbaabbabbbbaabbbabaabbababbaababbaaabaababaaaabbaaabbbbabbbbaaabbbbaabaaabaabababbbaabababbaaabbbbbabbbabbabbbabbabbabaaababbbbaabbabaaaabbbaaabbaaaaababaababbaabbbababababbbbaaaaaabbaabaaaabaaaaaaabbabbbbaababbbbababbaaaabbbaabbbbaaaabaababaabbbbbaaabbbbaabaaaababababbbaabbaaaabaabbabbabbababbaababaabbbbbbbbbababbbaaaabbabaaabbaaabbababbbbbbbbbbaababbabbbababbaaabaaaaabbbbbaaaabaaaabbabaababbaabbbabbaabbbaaaabbabaababaaabbbababbababaabaaabaaabaaaabbbaabbbbabaabbaaaaaaabbaaaaabbbabbbaabbbaabababbaaabbaaabbaabbbabbaabbaaabbababababbbbabbaaaabaabaaaabaaabbbaabaaaaabbbabbaaabaaabaababaaaaaababbbabaaabbaababbbabaabaabbabaababbaaabaaabbbbaabbbbbabaabbbbaababaaaabbbbbbaababaababaaabbbaaaaaaaaaababbbaababbababbbabbababababaaabbaaabbaababbbbabbbbbbbababaaabbbbabbabbabbbaaaabbbbbbaabbbbbaaabbbabbaabbaaaabaabbbbbbbabaababbababbbababababbababbbaababbbabbabaababbabbaabababbaabaabaabbaaabbaaababbbbbbababaaababbbabbbababbbbabbaabbaaabbbaabaaaaaaabbaababaababaabbabbbbbaaaabbbabbababbbbbabbababaaaaabbabbababaaabaabaabaaaaabbabbabababbababbbaaaababbaaaaaaabbaaababaababbbbaaaabbbaabaababaaaabbababaaabbaaabaaabbaaaababbaabbbbbbabbbaabaaaaaabbbaaabbaabbabaaaabaaabbababaaababababbbbaabbbaabbbababbaababbbabbbabbabaabbababababbbbbaabaaabbbaababbbbabbbaabbabaaabbbbbabbbabbbbabaabbabbbbaabaaabbbaabaaaabaabbaabbaabababaaabbbbaaaaaabbbababbabaaabbbbababbabaaabbbaaaaaaabbababbbbabaaabababbaaaaabbbaababbaaababbababaabbbbbabaaababbbbbbbaaaaaaabbaaabbababbabbbaaaaababbaaaaabaabbaabaabaaabaaaaababbabbaaabbabbbbaabbabbabbabbbbabaabbabababbabbbbaababbabbbabbabbbabaaaaaababbbabaaaaabbbbbaaaababbbbababbbbbabbbbbbbbabbaaabbabaaaaabbaabbabaabbaabbaababbabaabaaababbbbbaaababababbaabbaabbabbababbabbaaaabababbbbabbbaaabababaaaabbbabbaabbbabaabbaaaabbaababababbbababbbbababaaaabaaabbbabbaabbbaabbabbbabbbaaabaaabaaaabaabababbbabbaaaabbaababababaabaabbaabbaababbaaabaababbbbabaaaaaabbbaabbaaabaabbbbaaabbaabaabbaababaabbbabbbabbbabaaababaababaababaaabbbbbabbbbbaabbbaaabaabbbabaabababbabababaaaabbbaabbbabaaabbabbbaabbbaabaaaaabbbabbabaababaaaababbbbaaaaaabaaaabbabbbbabbbaabaabbbabaabbbabababbbbbbbbaabaabbaaaaabbbbabbbaabbabaaabaaaabbabaabbbbbbaaaabbbababaabaabbbaaaaaaaabbbbabbbabbbaaabbbbabaabbaaabbababaabaabbaabaababbabbbabbbababbbbabbbbabbbaabbbabbaababbabbabbaabaabbaababbababababaaabbaabaabaaabababbabaabbaaababbbaaababbababaabbabaaabbbaaaabaababbbabaabbaaaabbbaababaabbbbaababbbbbaabbaaaaaabababaaabbababbaaabbaabababaabbbbbbabbbbbaabaabbaaabbaabbbbabbbaabbabaaaabbbababaabaabaaaabbabbbabbaabbbbbabaaabbabbabbbbbaabaaabbabababbaaabbbaabaabbbbbaaaaabbaaaabbbabbbaabaabbabbabbabbaabababbbbbbbbabaabaabbbabbbabbababbbaabbabaabbbaabaaaabbaabbaabbbabaabbaabaabbbabaaabbaaabbbaaaabbbbbaaaabbbabbbbaaababbbbbbbbababbaabbabbabbbbbabaaaabaabbaabbbbbabbabaaaaaabaababbabbabbaabbabbbabbbbaaabbbaabbbaabbbaaaababbbbbaaabaababbbbbabbbaabbaabbbbaaabbbbbbaabaaaabaaabaaabbabbaaabbaababaaabbaababbabbaaaabababbbabbaaabaaaaabbaabababbaabaaaabaabbbbaaaaabbaabbbaaabbabbbaaabbabbbabbbbbbababbbbaaabaaababbbabbaababbbbbabbabababbbbbabbaaaaaabbababbbababaaababbaabbabbbbaaabbbababaaaabababaaababbbababaabaaaabbabbbbabababababbbbbbaaabaaabaaabaababaaababbababbababbabbbabaabbabaaaaabaaabaaaabaaaabbabbaababbaababaabaabaabaabbaababbaaaabaaaaaaaaabbbaabaabaababbaaaabbababaababaaabababaabbbbbaaabaabbbbaaaaaaabbaabababaabbbabbaaabbaaabaaababaaababbabbbbabaabbaaaaaababbbaaababbaaabbabababbbaaaabaaaabaabbabbaaaaababaaabbaabbbbabaabbbbbbbaaabbaabbbaabbbbbbbbbbaaaabbabaabaaababbabbbaabababbaaaabaabbabbbaaaabaaaaaaaaabaaabaabbbbbababbbbababbbbaaabbbaabababbbbbbbaabbaaabbbaabbaabaabababaabbababbbabaaaabbabaabbbaabbababbaaabbabbaabaaaaaaabbbbabaaabbbababbabbaababaabababbaaaabbabababbbaabbbaabbaaabaaabbabbbbbbaababaaabbbaaabbabbbaabbbaabbabbaababbbabbaabbbaaaaabbabaaabaaabaaabaaaabbabaabbabbbbbabaababbabbbbbbaaabbababaaaabaabbbaababaabaaaabaabaabbaaababbbbbbaabbbbbbaaabbaaaaabaabbbbababbaaabaaabbabbaaabbababbabaaabbababbbaabaabbbabbbabbbbabbbbbbbbbabababbbababbbababbbabbbabbaabaaababaabbbbaaaabbaabaaabbbbbbbaaabababbbbaabababbababaaabaabbabbaabbabaaabbabaababbababaaabbbbbbaabbbaaaabaaabaababbbbabaaabbaaababbbbabaaaaabbbabaabaabbabaabbaaaabbaabbbbabbababbaaaababbbababbabbbbaaabbaaabaabababbbaaaaaaabbbbbaaababaaaabbbbaabaaaabaaababbbabbaaabaaabbabbabbbabaaaababaaaababaababaaaababbaaaaababbaababaaabbaabbbbaababbbaabaaababaaaabbbaabbbbbbaabbbbabbbabbaababbbbaabbbabaaaabaaabbbbbbaabaababababaaababbabbbbaababaaabaabbaabaaaaabbbbbbabbababaabaaabaabbaaabaaaaabbbaabbababbababaaababaaabbabbababaaaaababaabbbbbababaaaabaabbabababaabbaabaabababbbbaabbabaaaabbbbbbaaababbbbababbbbbabbaabbbbabbaababababbbbaaabbababbbbabaaabbbaaaababaababaaaabbababbabbbbbbaabaaaabaabbbbbbbbbababbbabbabbbbbabaabbababbaabbaabbbbbaabbbbbabaababaabaaaabbabbaabaabaabbababbababaaaabababaaaababababaabbbbaaaabbbabaababbabaaaaaaabbbbaaababbaaaabbbbabbbbaaaaaabaaaabbababaababbabbaabbbaaabbbbaaaaabababaabaaabbbaaabaaaaaaababababababbbaabbaabaaabbabbaabbbbaaaabbbababaabbbbbbbabbabaaabbaaaaaaabbababababbbbababbbababaaabbbabbbbababbbbbbbbabbbbababbabbbbaabaaaaababbabababaabbbaaabaabaabbaabaabbbbabbbbabaabababbabbaaaabaaaababbabababababaabbabbaabaabaaaabbaaabababbaababbabaaaaabbaaabaabaaabbbbbbabaababbbaabbaaabaababaaaaaaabbbaaabaaabbbbbabaaabbabbaabaabbaaababbbaabbbaabaaabaababaabbbbbabaaaaaaaaaaaabbaabbababaababbbbbaabbabaabbaabbaababaabbaaabbabbabbbbabbbabbabaaaababaabbabbabababbaaaaaaababbbabbbbabababbaabbbabababbbabbabbabbbabbbbbbababbaaabbaababaaaaabbbaaabababbabbaaabbabbaaaabaababaabbbaaabaaaabaabbabbaabaaaabbbbbabbabbbabaaabbbbbbababababaaaa", solution1.longestPrefix("abbbaaabbabbaaaaaabbabaabbabbaabbabaababbbabbaabaabbbabbaabbbbbbabaaaaabbababaabaaaabaaaaabaabbbabbbbaaaababbbbbaabbaabbbaaabaabaababaaababaabbaaaabbbaababbbaabaabaaabbbbabbaaabbbabaabbbabaabbbbaabaaaaabbabbbaabaabaaaaabaabaabbaaaabaabbaabbbabaabaababaabababbbabbaaabbbabababbbbababaaaabbbbababbbabbaaabbabbaaaaaaabbbabbbbbaabababbbbabbaaabababbbaaabbbbbbbbabababbbaabbbabbbaababbbaabbbaababaabbbaaaaaaabaabbabbaaaaaaababababbbbbaaaaabbababaabababbbabbaaaaaaaabbabbaabbbaaaabbabaaababaabaabbaababaababaabbabbabbbaaaabbbaabaabbbbababaabbbaaaabbbbbaaabbaabaaabbbaaaabbbababababbabbbbbaaababbaabbaaabbbabbbabaaaabbbaabbabaababbababbaaababbbaabbaabbaababbababaabbbababaaaabbabaaaaaababababbaabaababbbaaabaabaababbbbaaabbbbbaaabbabaaabbbaabaaabaabaababababaabaaaaaaaabbaabbbaaabababbbbbbabbbabbbbabbbabaabbbabbbaaabaaababaaaaababbabbbbaaaabbbababaabababbabbaaaabaabbabbbbbbbabbabbbbbbababbbabbabbabbabaabaaabbbbaaabbbbaabbabbbaaaaaabbabbbbbbaabbababbaaaabbbabaababaabbbaabbaabbaaaaabaaababaabbabaabaabaabbaabaaaabbabaaabaabababbaabbababbbabbabbbbabbbbababbabbbbbaabbaabababbaabaababaabaaabbaabababaabaaaababbbaabbabbaaaabbabaaaaaababbaaabaaabbabaaabaaaaaabbbbbbaabaabbababaaabababaaabbbbbabaaababbaaabbbbabaaababaaaabaababababbbabbbbabbbabbbbaabbbabbaabbaabaabbbababaaabbaabaaaabaabbbbbbababbbababaabababbbaabbbbbabbaaaaaabbababbbabaaabaabaabbbaabaaabbbabaaaabababbbbaaaabbaaabbbaababaaaaabaabbaababaabbabaabbaaabbbbbaababababbbabbbaaaaaaabaaaabbabaaababbaaaaabababababaaababbbabaaabaaaaabaaaaaaabaabbbaabbbbbabaaaababaaaaaaaabbaabbbbbbabaaabbababbbbbaaaabaaabababaababababbbaabaabaabaaabaabbaababbbbaabbaaabbaaaababaaababababbbaaababbababaaabbabaaabbaaaaaaababaababbaaaabbbbaababbbabaaabababaabbbabbabababbababbaaabaabbbbbbaaaabbbbababbabaaabababbbbbbbbabbbbaaabaaababaaaababaabababbbabaaabaabbbbbbbbbaabaaababbabababbbaabbaababbaaaabbbabababaaabbbaaabbaababbabbaababbbabbbaabbbbaaaaaaaabbabaaabbabbaaaabbbaaaaaaabbbbaaaabbabaabbabbbbbabaabbbbbaaabbbabbbaabbaabbabbabbaaabaabaabbaabbbbaabbaaaaabaaaaabababaaababaababababbbbaabababbbbaabaabbaaaababbbaaababaaaabbbabababaabbbabbbbaababbabaaaabababbaaaabaabaabbbababbbbabbaaaaaaababbabbbabaaabaabbaabaaabbbbaabbaabbababaabaaabaababbaabbbbbbaaababbbbbbababbaabaaabbbaaababaaaaabbabaaaaaaaabbbababbabababbabbbaababaaaabaabaabbbbbbaabbbaaaababaaaabbbaabaaaaabbabaabaaabaaabbabbabaaaaabaaaabbaabaaabaabbbabaaaabbaabababaaaabaaababbbbaabbbbbabaababbababaaaababbbbabbaaababbabbabbaabababbbbaabbaaabaabababbbabbbabbbbabaaabbbabbaaabbabbbababbbbaaaaabbbaaabbaaaabbabbaabaaabbbbbaabbbabbabaabbabbbaaaabbbbaaaabaabbaabababbbbbabbbabbbbaabbabbaaaaaabaababaaaaabbabaabbbbbaaaabbaabbaabbbaabaaaabbabbbbbbaaaabbbbaaaaaaabaaabbaababaaaaaaabaaabababbbbabbababbbbbaaabbabbaaaabbaaabbbabbbaababbaaabbbbbaaaaabaaaabaaaaaaaabaaaabaababbabababbaabababbaabbaabaababbbababbbbbabaaabaabaabbabbbababbbbabaaaaaabbbaaaabaaabbaaabbabbbbaaabbaaaabbaabbbbbbababaabbbabbabababaabaabbaabaaaababbabababbabbaaabbabbaabbbbbaaaaabbbabaabaaabaaababbabaaaabaababbabbaaaaabababbbbaaabaaabbababbabbbbabaaaaaabaaabbabbaabbaabaaabbbbbaaaababaaaababbabaaaabbabaabbabbaababbbaaabaabbabbabbbabbbabbbbbbbbabbabbaababbbabaaaaaaaaaaababbbaabbbabbbaababababaaabbabaababbaabbaaaaabababbbbbaabbabbabaabaaabaaaabbabbbbaabbabbbaababbabbaabbababaaabbaaabbbbaaaabbbbabbbbabbbabbbbaaabbaaabaabaabbaabbabbaaaaababbbaaabbbbbbbbbbaaaabbaaaabbaabaaaaabaabbbbbaabaaabbabbabaababbbbbbbaabaaababbaaabaabbababaaaababbabbabbbbaabbbaaabbbbbaaaabbbababbbabbaabaaabbabaabbabaaaaaaaabababbbababbbbabbbbbabbaaaabbabaabaabbabaaaaabbaababaabbaabaababbaaaaabbaaaabbbbaabaaabbabaaaabbababbaaabababaaaaabaaabaaabbbaababababaabbabbbbbbabbaaaaabbbaabaaaaabbbaabaaabbaaabababbabbbbbbbaaaabbaaabbabbbbbbbbbaabababbbbabbbbabbbbaaaabaababbbabbaabababababbaaababbaaabababaaabaaabaaabaabbbbbbbbbabbbaaabbabbbbabbbababaaaaaabbbbbbbbbbaababbbbaaaababaabbaabbaabaabababbbbabbabbbbbabbabbbabaaabbaababaabababbabaabbbbbaabbbbabbbabaaabbbbaaaaabbbabbbbababbbabaabbaabbabbaabbabbaabbbbabbbbabbaabaaaaaaabaaaabbaaaabbbbbabababbbbaabbbbbbabbbaaaababbbbababbbbabaabaabbaabaabbaaabaabbbbabaaaabbabbabaaaaaababbbbbbabbaabaaababbabbbaaaaabaaaabbaaabbbbaaabaaaabbbaabaababaababababaabaaabbaabaabbaabbbbabbabbbbaaabbabaabbbaabbabaaaaabbaabaabababbbaabbbabaabaaaababaabbbbbabaaaabaabbbbbbbbbabbaaaaabbbbabbbaaabbbbbbaababbaababbbbbbbbbababbabbbabbabaabaaabababbbbababaabbbbabbaabbbabaababbbbbbbabaabaabbbabbbaaabbbabaaaabbbbbbbbabbaababbbbbaabbbababbbbbaaaaabbbbbbbbabbabbbababababaababbbaaabbabbababbabaaaaaabaaabaabbaabaaabaabaabbbabbabaaaaabbbabbabbabbbbababbabbbabbbabaababaaaaaabbaabaaaaaaabbabaabaabbbbaaaaaababaaabbbbababbaabbaaababaaabbaaabaabbbbbbabbbaabaaabbabaabbbbbaaaabaabbbbbabbabaaaaaaaaaabbbbbaabbbbaabbbbbbaabbababbbaabaabbbabbaabaabbbbaaaabbabaabaabbabbabababbbbbbabbbbbbaabbabaababaabaababababbabbaaabbbbaaababbbaaaaaaaaabaabbbbababbbbaaaaabbbbbbbabaaaaaabbabbbababbbbbabbaaaaabbaabbbaabbbaaaaabaaaaaabbaaabbbbbbbbbbbbbababbbaabbbbbabaaababbbbaabaabbbbabaaaaababbbbbbbbbbbaabaaaaabaaabababbbabbaabaabaaaababbababaaaabbabababbbababbabbaababbaabaaabaabaaaababbbbabbbabbbbaaabbbbbbbbaaabaababaabbbbaabababaaaabbaababbaababababbaabababbbbbbaabaabaaaaabbbbaabbaaabaaabbbaababababababbbabababaaaabbaaaaaababaabbaabbaababbbababbaababaaabbbaababbbababbbabbabbbbababbbbbabbabbbbaaabbbbbaaabaaabbabbaabbaababbbaaabbbabababbbbbbabbbaaababaaaaaaaaabaabaaaababbbaababbbbbaabbabaaabbaaaabbabbaababbbaabbbababaababaabbbbbababbaaaaaaabbaaaabaaaaaaaabaaabbbaabbaababaabaabbbaaabbaaaabbbaaabaabbbbaaaabbbbbaaabbaaabbbababaaaaabbbbbaaabbaabbbabbabbbbababaaabbbabbbaaabbbaaabaaabbaaaabaaabbabbabbbaabbaaabbabbbaabbaababbbaababaaabababbaabaabbaabbbabbbaabbabbabbabbababbaabaabbbbaabaabbabbbbbbbbbbaabbababbabaaabaaabbababbabbbbbbaabaabbaababaabababbbbbabaabaabbaaaababaaaabbbbbbaabaabbbbaababbbbabbbaaaaabaababababbabbababaabaabbbbbbaaabbaabbabbabbbaabbbaabbbbaabbbbbbbaabbaabaaaabbabbbaabababbbabaaabbbbbbbbabbbbbbababababaaaaaabaaabaabbbbbabbbbabbbaaabbaaababbaabbbbbaaaaabbbabaaabbaaaabaaababaaaabaabbabbabaabaaaaaaabaababaabaaaaaaabbaaaabaababbbbbbababbbabbaabbbbbbbbabababbbbbbbabbbabababaaabaabbaabbbaababbaabaaabbbaaaabababababbbbbbbbaababbbababbabbbaaaaaabbabbabaabaababbabaabbaabbbaaabbaaababbaaabaabbbbbbbaaabaabbbaabbbabaabaaabaaaabaaabbbbbbbbbaababbaabaaaabbbaabbbbaaababaabaaabaaaaabaababbaabaaaabbbabbababaaaabaaaabbaaaaaababababaaabbaabbabbbaaabaaaaaaaabababbabaaaabbabbabbbbbaaaaaaabbbbbaaabbaabbbbbaabbabbbabbabbbaaaababaaabbbababaaabbbbbbbbbabaabaaabbbaaabababbabbababababbbbbabbbabaaaabbbabbbabbbaaabbaabbaabbabbaabaaabbbbaaaabababbabaaaabbbabaaaaaabbabbaabbaaaaaaaabaaabaaabbbbaabbbbbaabbaabaababaaaabbbbbaabaabbabbbbabbbaabaabaaabbabbbabaabaabbabbabbabaabbababababbababbaaabaaabbbabbaaaaababbbaaaaabbbaaaaabbabaaabaabbaaabbbaaabbabbbaabbabbbabbabbbbbbbbbbaaababaaabbababbbaababbaabbaababbbbababbbabababbabbbabbabababbaaabbaabaaaabbbaaabbababbbaaaaaabaabbabaabaabaaaaabbbabaaabaaaaabbbbabbbbbabbaababbabaabbbabbaababbbbbbaabaabbabbbbbbbaaabbbbabbaaabbababbbabbabbabbabaaaaabbaaaaabbaaaabaaabbbbaabbabaababbbbababbbbbbbaabbbabbaaaabaabbbaabbaaabbabaaaaabbaaabaababbbbbbaabaaabababbabaabaabbbabaabbbbbbbaabaaabbbbaabababbaabaaaababbbabbabbaabbbbaaabaabbbababbbaaaabbaababbababaaabaaaabaaaaaaaaabbbbbabbaaabbabaaabbaaabaabaababaaaabbbaababaabaaaabbbabbbbaaababaaabbbabbabbbababaaababbabaabbbaaabbaabbbabbaabbbabaabbaaababbbabbaaaabbaaababbabbaaabaabbbbbbaaaaaaabababbabaabaaabbabbaaaaabbabababbbbababbbbababbbbaabbababaaaaababbabaaababaaaabbaabbbabbaaaaaabaaabbabbbaabbabbaaabbabbaabbbabbaaaaaabbbabbbaababbbbbaabbaaaaaababbabbbaaaaabababbaaabbbaabbabbaaababbbbbaabbbbbabbbababaaabbabbaabaaabbbbbaabbabbbbabbbbaabbbbbbbbaaabaababaabbaabaaababbaababbbbbabaababbbaabbbabbbbbababaaabbbabbaaaababbabbaaaabababbbbabbbabaabbbbaaabbaaaabaabbabaabbbaabbbabbbbbabbabaabaaabbbaabbaabbbbbbbbababbbabbbbabababbaaababaabbbbbaabbbbbbaaaabababbababaabbaaabaaabbaabbaaabbbbbabbabbabbaaaabababaaababababaaabbabbbaabbaaabababbabbbaaabbbbaabbaabbabbbaaaaabbbbabbbbbabbaaababaabbbbaabbbbaaaabaababbbabbaabbbabbbbbaaaaabbababbbaabbbbaaabbaaabbbaaaaaaabbabbbbbabbbbaababaabbbaaabbabbbbbbaabbabbabbaababbaaababbabbabaabaabbbaaabbabaaaaababbabbabababbabbbbabaaababbaabbaabbaababbabaabbabbbbbabbaabbbbababbbbaaabbbaabaabbababaaaabaababbbbabaaababbbbababbbabbbbbbbbabbaababaabaaaabbbbbbaabbaaabbbababbbbabbbbbabbbbaabababaabbbaababaaaabbabbaaaabaaababbaabbbaababaabaabbabbabaabaabbbbbbaaaababbbbbbbaabbbababababaaabbaabbaabbaaaaaaabbaaabbbaabbababaaabbaaaaabababbabbabbbaabaaaaababbbbbabaaaababababaabaabbbbabbabaababbbabbaababababbaabaabbbbaabbabbbaaabbaaaabbaabbaabbbbabbbbabbbbbabbbaaabaaabbaaabaababbbbbbbbbbbbaabaaabbbbabbabbbbbbaaabbaabbabaaaabaabbbbbabbbababbabbabaabaabaaababbbbbbbbabababaaaabbbaababbaabaaabaababbbaababbbbbabaabaabbbbabaababbabababbaaabaababbababbbabbbbbbabaabababaaabbabbababbbbbabbbbbbbbabaaabbaaaaabaaaaabbaaabaaaaaaabaaabbabbbbbbabbbabbbbbababbbbbabaabaaaabbabbbababbbbbabaabbbaaababaaaaaabbabaabbbbbbbbabbaabaabaabbabbabbbababbbbabaababaababaabbabbbabbbabaaabaaabaababbaabaaababababbbaaabbbabbbbbbbbbabbabbababbbbbbbaaaaabbaabbababbbbabaaabbababaabaaaaaaababbabababbaabaabaaababbbabbbbbabababaabbbabaaabaabaabbaabbbbbbbaabaabbaababbbaaabbbabbaaabaabbbbaaaabbaababbababbbaabaaaabbbabbabaabaababbaababbbbbbababbbbbbaaabaabbbbababaabbbbaabbbabbabaaabbaaaaabbbabbaaaabbabaabbbaaaabbababbabbbaabaabbbbaabbabaaababaabaaaaabaabbabbbabbbaabbababbababaababbbababaaabaabbabaabbbbaaaaaaaababbbbbbbbbbababbbbababbbbbabaaabbbbbabbbabaaaabbbaabaaabaaabbbabbbbaaaabaaabababbbabaaaabbbbbabbbbaabbabaabbabababaabbbababbbabbbabbbbbbbbaaabbabbaabbabbbbbbbbbbbbbbbabaaaababababbbabaaababbabbaaabbabbababbaaaaaaaaabbaabbaababbbabbaabbabaabababaabaaaabbababbbabaabbbaaaaabaababbaabbaabbaaabaaabbbbaaababbbabababababbbbaababbbababaabaaaaaababbbaabbaaaababbabbaabbbbbbbbabbaabbbbbaabaababbaaaaaabaaaabbbabaabbbaaabbbababbbaababbbabbaabaaabbabaaaabaabaaaabbbababaabbaabaabaabaabbaabbbbbbbabababaabbbbababaababaaabbbbaabbbbbbaabbbbaaabbbaabababbaababababbaaaabbbbbabababbbbbbaababaabbbabaabbbabbabbbbbababababbababbbbaaabaaaaaabbbbbbbbbbaaaaaaabbaaabaaaababbaaaaabbbaaaaababbaaabbbbbaaabbaabbababaabbaabaaaaabbaaabbbaaababbabaaabbabbabbabbbabbababbbaabaaaaaaaababbbbbaabbaabbaaaaaaabbbbbbbabbabbabbbababbbababbaabbbabbbaaabbbbaabaabaaaaabaaaaabbbaaababbbababbbabbabaabaabaaaabababaabbbabbbababbaaabaababbbaaabaaaabaaaabbbbaabbabababababababababbabbaabbaaabbbbaabbbabbbbbbaaaaabbbaabbbbababbbabaaaaabaabbabbaabbaabaaababaaaabaababbbaabbabbaaaaabaaabaababbababbababbaaabbbbbababbbbaabbaabaabbabbbbbaabbbaababbbaaabababbaabbabaaabaabbaaaabaaaabababbbbbbbbbaabbaaaaabbabaabbbbaaabbaaaaaaabaabbbabbabbbbbbaaabbaaabbbababbabbbababbbbaaabaabaabbbaabaababbaabbaaababbbabbaabaabaababbabababaabbabbaabbaabaabbbbbaabbbabbabbbabbbbaabbbbaaabaaabbaaaaababbbababbbabbbbabaabbaabababaaabbbbaaaaabbbbabbbabbbbbaabbaabbbbaaaabbbbbbbbabbbaabaaabbbbabaabaababbbbabaabaabaabbbbbbbbaaabbbaabbaaababbabbaaabbabbbbbabbaababaabbaabbaabbabbaabbabbaabaaaabbaaaaaaabbbbaaaabaaabbbbbaabaababbbaabbababbbbabbabbbaaaaabbaabbababbaaaaaababbabbaababaaaaabaabbababbbbbaabbaaaabaaabaabbbbbaabbabbbabaaabaabababbbaaaaabbbbbbbabbaaaaaaaababbaaaaaabababaaabbbbaaabbabaaabbabaaaabbaaaaaabababaaaabbaaabbbabbbbabbbabbaabbbabaabbbbbbaababaabbaabaabbbbbabbbababbabaaabbabbbbababaaaaaaaaaabbbbaaaabbabaabbaaaaaaaaaabbaaabbabbbbbababbbbbbbbbbaabbbbbbabaabbbaaaabbaabbaaaaaaabaabbaaabbabbbababbbbabbabbbbabaaaaaabaabbaaaaaaabbabababbabbabbbbbbabaaabbabbbabbaaabbbaabbbbbabaabbbbababababbabbbbbbabbbbbabbbabbabaaabbababbabbabaaaaaabaabbabbaaababbaabbbaaabbabaabbbabaaaabaabbbabbaaababaaaabbabbbabaaaaaababaabbaabbbbabbaababaababaabaaaaabbaaaaaabbbbabbbaaababbababaaababaababaabbabbbabbabbaabbaabbbbabbaaaaaaababaaaabaabbbbbaabaabbbaababbbaaabbaaaabbabbaababbaabbbaabbabaaaabbabbbbaaabaaabbabbaabaabababaaaabaaaaabbaaababbaaabaaabaaababaabbbabbabaabaabbaaababbbbababbaaababbabbbabbbbabaaabbabbaaabbbababbababababbaabbbbbababababaababbbabaaaabbaabaaaabababaabbabbaabaaaababaaaabaabbbababbaaabaaaabaabbbbababaaaaabaabababbaaaaabbbababbbaababbabbaaaababaaaaabaabaaaaaabaaabbbabababababaabbbabaaabbaabbbbabaabaaabababbbaabbaabbaabbababaaaaabbbbabaabbabbabbbbaabbbaaaabbbaabaabbabbababbaaaaaaabbaabaaaaaabbabbabaabaaabbaaabbababbabbbbbbabbaabbaabababbaabaaabaabbbbbbbabbaaaabaaaaaabbabbbbaaabbaabbbbaaaabababaababbabaaaabbabbbbaabbbbbbbbbbbaabbaabbbaabbbbbaaababbbaabbabaabbaabbbbabbbabaaabbaabababbabaabaabbbaaaaabaabbbaabbaabbbaabbababbababbababbbaaaabaabbbabbaaaabaaabbaaaabbbbbbaabaaaabbababbaabaababbababaaaaaabbabbbbbababbabaababaabababaababbbaabaaaabbabbaaababbaabaaababbbaaabbaabbbbbbabbbbbbbbabaabaabbbbbabababbabaaabaaabaaaabbbababaababbabbaaaabababababaabbbababbabbbbbabaabbabbaabaaababbabbbbaabbabbabbbbababaaabbababbaabaaaabbbbabbbabaaababaabbaaaabbbaaaabababaabbbabababbbbbbaababbababbaabbbbaaaabaabbaaaabbbabbaaabaaaabbababaaaaaaabaabbaaabbaabaabbbaaabbbbbaababbbbbbbababbaababaabbbaababbbabbbbbbbaabaaaabbabaabaababaaaaabbaaaaabaabababaaaaaaabbabaabababbbaaaabababbbbbbbaababbbabaaaaaababbabbabaabaaaaaaaabbaabaabaabbbababbbbababababbaabaaaabbbbabbababaaabbaaabbbababbbbabbbaabbbbbaabaaaaaaabbaaabbbbbaaabbbababbbabbbaabbabaaaababbaaabbbbbababbaaaabbbbaaaaabaabbbbaaabbbababaaabbbbababaaaaaaaaababbaaabaababbabbabbaababababbababbbaaaaaabaabaabaaababaaaababbababbbababbbabbaabbbbbaabbbbaabbbbbabaaaaabbbabababaaabbbaabbbaaaaababbbaaaaaaabbbbaababbbbbababbabbababbabbaaabbabaaaabbbbabbaabbabbaaabbbababbbbaaabbaaaabbbbaaabbbabbbababbababaaaaabaaabaaabbabbbbabababbbbbbbabaabbbbbababbaaaabbabbabaabbabbababbbabbababbaaaaabbaabaaabaabbbabbbabababaabaabbbbbbabbaababbaaabbababbabbaaaabaabbabbbabbaaaabababbbaabbbaaababaabaaaabbabbbaaaabaaabaabbbaaaaabbbbbbbbaaaaabbbababbbaaabbaaabaababbaabbaaaabababbbaabaaabaaabbbbaaaabbbaaabbbbbaabbaabbbaaabbbabababbbbbbaaaaaabababbabaabbbbababbabbbababbbababbbbabbbbabaaaaabbaaaaaababaabbabbaabaababbaaabbaaaabbbabbbbaabbabaabaababaababaaabbbbbaabbabbabaabaabbabbbabbabababbbbbaabbbaabaaaaabbbbabaaaabbabbbaabaabbabaababbbabaaabababbbaabbababbaaabaaaabbaaabaaaabbbbbabbbabbbbbaaabaabbabababababbaaaaaabaaaabaabbbababbabababbbaabbabbabbaababbabbbbaaabbabbababbbaabbabbbbaabbbabaabbababbaababbaaabaababaaaabbaaabbbbabbbbaaabbbbaabaaabaabababbbaabababbaaabbbbbabbbabbabbbabbabbabaaababbbbaabbabaaaabbbaaabbaaaaababaababbaabbbababababbbbaaaaaabbaabaaaabaaaaaaabbabbbbaababbbbababbaaaabbbaabbbbaaaabaababaabbbbbaaabbbbaabaaaababababbbaabbaaaabaabbabbabbababbaababaabbbbbbbbbababbbaaaabbabaaabbaaabbababbbbbbbbbbaababbabbbababbaaabaaaaabbbbbaaaabaaaabbabaababbaabbbabbaabbbaaaabbabaababaaabbbababbababaabaaabaaabaaaabbbaabbbbabaabbaaaaaaabbaaaaabbbabbbaabbbaabababbaaabbaaabbaabbbabbaabbaaabbababababbbbabbaaaabaabaaaabaaabbbaabaaaaabbbabbaaabaaabaababaaaaaababbbabaaabbaababbbabaabaabbabaababbaaabaaabbbbaabbbbbabaabbbbaababaaaabbbbbbaababaababaaabbbaaaaaaaaaababbbaababbababbbabbababababaaabbaaabbaababbbbabbbbbbbababaaabbbbabbabbabbbaaaabbbbbbaabbbbbaaabbbabbaabbaaaabaabbbbbbbabaababbababbbababababbababbbaababbbabbabaababbabbaabababbaabaabaabbaaabbaaababbbbbbababaaababbbabbbababbbbabbaabbaaabbbaabaaaaaaabbaababaababaabbabbbbbaaaabbbabbababbbbbabbababaaaaabbabbababaaabaabaabaaaaabbabbabababbababbbaaaababbaaaaaaabbaaababaababbbbaaaabbbaabaababaaaabbababaaabbaaabaaabbaaaababbaabbbbbbabbbaabaaaaaabbbaaabbaabbabaaaabaaabbababaaababababbbbaabbbaabbbababbaababbbabbbabbabaabbababababbbbbaabaaabbbaababbbbabbbaabbabaaabbbbbabbbabbbbabaabbabbbbaabaaabbbaabaaaabaabbaabbaabababaaabbbbaaaaaabbbababbabaaabbbbababbabaaabbbaaaaaaabbababbbbabaaabababbaaaaabbbaababbaaababbababaabbbbbabaaababbbbbbbaaaaaaabbaaabbababbabbbaaaaababbaaaaabaabbaabaabaaabaaaaababbabbaaabbabbbbaabbabbabbabbbbabaabbabababbabbbbaababbabbbabbabbbabaaaaaababbbabaaaaabbbbbaaaababbbbababbbbbabbbbbbbbabbaaabbabaaaaabbaabbabaabbaabbaababbabaabaaababbbbbaaababababbaabbaabbabbababbabbaaaabababbbbabbbaaabababaaaabbbabbaabbbabaabbaaaabbaababababbbababbbbababaaaabaaabbbabbaabbbaabbabbbabbbaaabaaabaaaabaabababbbabbaaaabbaababababaabaabbaabbaababbaaabaababbbbabaaaaaabbbaabbaaabaabbbbaaabbaabaabbaababaabbbabbbabbbabaaababaababaababaaabbbbbabbbbbaabbbaaabaabbbabaabababbabababaaaabbbaabbbabaaabbabbbaabbbaabaaaaabbbabbabaababaaaababbbbaaaaaabaaaabbabbbbabbbaabaabbbabaabbbabababbbbbbbbaabaabbaaaaabbbbabbbaabbabaaabaaaabbabaabbbbbbaaaabbbababaabaabbbaaaaaaaabbbbabbbabbbaaabbbbabaabbaaabbababaabaabbaabaababbabbbabbbababbbbabbbbabbbaabbbabbaababbabbabbaabaabbaababbababababaaabbaabaabaaabababbabaabbaaababbbaaababbababaabbabaaabbbaaaabaababbbabaabbaaaabbbaababaabbbbaababbbbbaabbaaaaaabababaaabbababbaaabbaabababaabbbbbbabbbbbaabaabbaaabbaabbbbabbbaabbabaaaabbbababaabaabaaaabbabbbabbaabbbbbabaaabbabbabbbbbaabaaabbabababbaaabbbaabaabbbbbaaaaabbaaaabbbabbbaabaabbabbabbabbaabababbbbbbbbabaabaabbbabbbabbababbbaabbabaabbbaabaaaabbaabbaabbbabaabbaabaabbbabaaabbaaabbbaaaabbbbbaaaabbbabbbbaaababbbbbbbbababbaabbabbabbbbbabaaaabaabbaabbbbbabbabaaaaaabaababbabbabbaabbabbbabbbbaaabbbaabbbaabbbaaaababbbbbaaabaababbbbbabbbaabbaabbbbaaabbbbbbaabaaaabaaabaaabbabbaaabbaababaaabbaababbabbaaaabababbbabbaaabaaaaabbaabababbaabaaaabaabbbbaaaaabbaabbbaaabbabbbaaabbabbbabbbbbbababbbbaaabaaababbbabbaababbbbbabbabababbbbbabbaaaaaabbababbbababaaababbaabbabbbbaaabbbababaaaabababaaababbbababaabaaaabbabbbbabababababbbbbbaaabaaabaaabaababaaababbababbababbabbbabaabbabaaaaabaaabaaaabaaaabbabbaababbaababaabaabaabaabbaababbaaaabaaaaaaaaabbbaabaabaababbaaaabbababaababaaabababaabbbbbaaabaabbbbaaaaaaabbaabababaabbbabbaaabbaaabaaababaaababbabbbbabaabbaaaaaababbbaaababbaaabbabababbbaaaabaaaabaabbabbaaaaababaaabbaabbbbabaabbbbbbbaaabbaabbbaabbbbbbbbbbaaaabbabaabaaababbabbbaabababbaaaabaabbabbbaaaabaaaaaaaaabaaabaabbbbbababbbbababbbbaaabbbaabababbbbbbbaabbaaabbbaabbaabaabababaabbababbbabaaaabbabaabbbaabbababbaaabbabbaabaaaaaaabbbbabaaabbbababbabbaababaabababbaaaabbabababbbaabbbaabbaaabaaabbabbbbbbaababaaabbbaaabbabbbaabbbaabbabbaababbbabbaabbbaaaaabbabaaabaaabaaabaaaabbabaabbabbbbbabaababbabbbbbbaaabbababaaaabaabbbaababaabaaaabaabaabbaaababbbbbbaabbbbbbaaabbaaaaabaabbbbababbaaabaaabbabbaaabbababbabaaabbababbbaabaabbbabbbabbbbabbbbbbbbbabababbbababbbababbbabbbabbaabaaababaabbbbaaaabbaabaaabbbbbbbaaabababbbbaabababbababaaabaabbabbaabbabaaabbabaababbababaaabbbbbbaabbbaaaabaaabaababbbbabaaabbaaababbbbabaaaaabbbabaabaabbabaabbaaaabbaabbbbabbababbaaaababbbababbabbbbaaabbaaabaabababbbaaaaaaabbbbbaaababaaaabbbbaabaaaabaaababbbabbaaabaaabbabbabbbabaaaababaaaababaababaaaababbaaaaababbaababaaabbaabbbbaababbbaabaaababaaaabbbaabbbbbbaabbbbabbbabbaababbbbaabbbabaaaabaaabbbbbbaabaababababaaababbabbbbaababaaabaabbaabaaaaabbbbbbabbababaabaaabaabbaaabaaaaabbbaabbababbababaaababaaabbabbababaaaaababaabbbbbababaaaabaabbabababaabbaabaabababbbbaabbabaaaabbbbbbaaababbbbababbbbbabbaabbbbabbaababababbbbaaabbababbbbabaaabbbaaaababaababaaaabbababbabbbbbbaabaaaabaabbbbbbbbbababbbabbabbbbbabaabbababbaabbaabbbbbaabbbbbabaababaabaaaabbabbaabaabaabbababbababaaaabababaaaababababaabbbbaaaabbbabaababbabaaaaaaabbbbaaababbaaaabbbbabbbbaaaaaabaaaabbababaababbabbaabbbaaabbbbaaaaabababaabaaabbbaaabaaaaaaababababababbbaabbaabaaabbabbaabbbbaaaabbbababaabbbbbbbabbabaaabbaaaaaaabbababababbbbababbbababaaabbbabbbbababbbbbbbbabbbbababbabbbbaabaaaaababbabababaabbbaaabaabaabbaabaabbbbabbbbabaabababbabbaaaabaaaababbabababababaabbabbaabaabaaaabbaaabababbaababbabaaaaabbaaabaabaaabbbbbbabaababbbaabbaaabaababaaaaaaabbbaaabaaabbbbbabaaabbabbaabaabbaaababbbaabbbaabaaabaababaabbbbbabaaaaaaaaaaaabbaabbababaababbbbbaabbabaabbaabbaababaabbaaabbabbabbbbabbbabbabaaaababaabbabbabababbaaaaaaababbbabbbbabababbaabbbabababbbabbabbabbbabbbbbbababbaaabbaababaaaaabbbaaabababbabbaaabbabbaaaabaababaabbbaaabaaaabaabbabbaabaaaabbbbbabbabbbabaaabbbbbbababababaaaababababbaaabaaaaabbabbababbbbbbbbabbbaabbababaaaaaabaabbbbaaaaabbabbbaababbbaaababababbabbaaabbabbbaabbbababaabbbbbabbbabaaaaabbaaababbaabaaabababaabaaaaabbabbaabbabbaaaaaaabbaabbbaabbbbbbbaaaaabbabbaabbbaabbabbbaaabababaabababbaabbabbbaabababbbabbbbaabbbaabbabbabbababbbaabbaababbbbbaabaaabbbbbababaaaaaaaaaabaabbaabbbbbbbbaababbbabbabababaaaabbbaabbababbbbaabbbaabbababbbabaaaabaaabbbbaababbbaababbbbbaabbbaabbbababbbabbabbbaaaabbbbbaaaaaaabaaabbbbbbbaabbabbaaaaaaaabbbbabbaabaabbbaabababaabbbabababaaaaabbaaabbbaaaaaaabbbbabaababaababbbbbbaaaaabbaabbaabbbaabaaabbbbabbbbbaaaaaaababaaaabbaaaaaaaaaaabbabbaaaabbaabbbbababaabababababbabaaaaaaabbbaabbaaaabaaaaaabbabaaabbbaabbbbbbbabbaabbaababbbbabaabbbababbaabababbbbabbabababbababbbbabbbbaabbabbbbbaabaabbbabbabbbabbbaabaaaabbabbabaaabbbbbbabaaababbababbababaabababaabbbbbbbbbabbabaaaababbbbabbaaabbbaabaabbbaabaaaaaabbbaabbbaabaaaabbaaaabbabababbababbbababbabbaaaabbbaaabaaaabbababbababbabababbbaabaaaabbbaaabbaaaabbbbbbbbbaabaaaabababbbababaaaabaaaaaaabbaabbbbbabbaaaaabbababbbbbabbbabbbbbaaabbaaaaaababaaaaaaaaabbaaaaaababaaabaaabbabaaaaabbbbabbaabbbbabbbbaaaaaaabbaabbbaabbbbabbbbaaabbaabbbbbaaababbbbabaaaaaabaababaabbbaaabbbbbabbbbbbbbbbabaaabbbaabaaabbabaaaabababbaabaaaabaabbabbbaaaababaaaabbaabaaabbabbabbaaabbaabbaabbbaabbabbabbabbabbabbababaabaabaababbbbabbabaababaabbbbbaabaababbabbaaabbbabaaababaababbbabbbabaabbbaaaaabaaabaaaabbababbbaaabaabaabbbbaaaababaaabaababbabababbaaababaaabaaaaabbababbaababbbbabaaaabbabbbaaabbabbbbbbabbaaababaababbbbaababbbbabbabaabbabbabbbbabbbabbaabbbabbabbbbbaabbbabaaabababbabbaaaaabbabbbababbbababbbbaaabbabbabaababbabbaabbababbbbabbaaabbbaaabaaabbbbbabbbbaaabaaabaaaaaaabbbbaabbbaaabaabbbbaaabaaabaaaaabbbbbbbbababbabbaaaaabababaaaaaabbbbbbaaabbbbaaabaabbaababbbbaaaaaabbabbbaaabbbbababaababaabbabbbabbaaabaaabbabbbbbabaabbbaabbaabbbabaabbbbaabbbaabaaaaabbaaabbaaababbaababbabbbbaabbabaababababbaababbbabbababaabaabbbbbbaabbaabbaababaabaabaaaabbaabbbabbbbabababbababaabaaabaabaabbabbababababbbaabababbabbbbbaaabaaabaabbbababbbaaaababbbbaabababbbabaabaaabbaaaababababbababababaaaaaaaaababababaabbbbbbbbabbaabbabababababbaaaaaabbababbabbbbbabbaaababbaaabaabbaababbaabbabbaaabaaaababbababaabbbaaaaaabbabbbaaaaabbabbaababaabaabaabbbaaabbbbabababbbaaabaaaabbbbbbaaababbabaabbbbababbabaabaaabbaaabaababababbbbbaaababbbbaabaabbbaabbbbaabaabbbbbabbaaaaaabaababbbabbaaaaaaaabbbbbbaabbaaabbbbbbabaabbabaaabaaabbbaabbaaaabaaabaabbbaaabaaabbaabbaaaabbababbbbbbbabbaabaaaaabbbababbbbbbbbaababaababbbbbababbabbbaaaabbbabbbaabbaaababbaaaabbbbbbaababaaabbabbbbbbbababbbbbbbabbabbabbbbaaabaaababbbbbaabababababbaabbbaaaabbbbaababaaabaaababbbbaaabbabbbbaaabbaaababaabbbaaaaaababbbaaabbbaabaabaaabbabbbaabaababbaabbaabbaabbaaaaaaaaabaaababbbbabbabbaaabbaabababaabaaaababbbbbbbbabbbaabbabbaabaaababbabbaaaaaabababbabaabaabbbbabaaabbbaaabbaaaaababbaabababbaabaaaaabbbaabbbbaaaaabbbaaabbbabbbaaaaabbabbbbaababaaaabbaaabaababbaaaaabbbaabaabbabaabbabbbaaaaababaabbbababbaaaaabbaabbaababbaabbabbbbababbbabaaaaabaaabbbabaaaaaabbbaaaaaaaababaaaababbbbaabbaababbaaaaabaabbaababbbbaabbbbababbbbbbaabaabbbbbbbbabbaabbaabababaaabaabbabbaaaabbbaaabaababaababbababababbbbbababbaaabaaabbbbbaabbbaabaaababbabbaaababaaaababbaaabaabaabaababbabbbabaabbbbbabaabbbaaabaaaababababbbababaaaaabbbababaaaabbababaaaababbbbabaaaaaabbabaabbbaababbaaabbabbaaababbbaabbbbbbbabbabbabaabbbbbbaaabbbabaabbaabbbbbbbbaabbabaabbbaaaaabaabbbabbbbbbaabaababbbbabbbbbbaabbbaaaaaaabbbababbbbabbbabbabbbabbabbabbabbbaabaaaabbbaaaaababbbaabbbaababbabaabbaababbbabbabaaaaaabaaaababababaababbaabbabbbbabaaabbbbaaababaabaabbababaaaababbbbbbbabaabbbabaabaabbaabbabbbababaabaabaaabaabbaabaabbbaaabbababbbaabbaabbbaaaabbbaabaababbbababbbaababbbababbbababbaabaaabbaaabbbaabbaaaabbaaabbbbbabbbbbababbabaaabbabbababbbaabbaabbbaabbbaababbbabbbaabbabaaabbaaaabbabbbabbbbbbaaabbbbbabbabbbaabaaabaabbaabbbabbbabbababababababbaaabbbbbababbabbbbbbabbabbbabbabababbaaababbaaababaaaaabbbabbbabbaaabaabbbaabbaaababbaabbaababababbbbabbbbaabbbaababbaaaabbababbabaaaabbaabaabaabaabababbbbabbbaababbabaaababaabababbabbbbabbabbabbbbbbbbababbabaaabbbabaabbbbbabaabaaaabbababbbbbaabaabaaabaabbbbbaaababbbbabbabaaaabbabaaabbbaaaabaabaaabbbbbaababbbababbbbbbababaaababbababbbbbbbabbbaabbbabaababbbaaaaaaaaaababbaabaabbbbbaababbabaaaabbbaaabaaababbbbaabaaabababbbabbababbaabbbbababaababbababbaaabbbaaabbbbaaabbabbababbbabababaabbaaaaaaabbbbbbbbbaaaababbbaabbbbabbabbbbbaabbaaabaababbbabbbaabbbabaababbaaaaababbbabbababbabbababaabababaabbabaaaabbbaabbababaababbaabababbbbabbaabbaababbaaaaabaaaaaabbaabababaaaaabaabbbaababababaaabaaababbababbabbbbaabbbaaabaababaaabbbaaabaabbbbaabbabaabaabbabbbbabaaabaabbbaaaaababbbbbbbabbbabbbaaabbabbaaaaabbabaaaaabbaaaaaabbbbbbbaabaabbbbbbbbabaaaabababaaabaaabaaabaababbaabbaababbbbaaabbbbaaabaaabbabbababababaaaaaaaaababaabbaabaaaabbbabbbaababababaabaaababbbaabbaaabbbbaaaabababaabaaabbabaaaababbabbaaaabbbaaaaaaaaaaaaaaaabbaabaabbbbbaabaaaaabaaababbabbbbbbaaaabbaabaaaaabbabbbbbababaabbabbabbabbabbaaabaabaabbbbbabbaabbababbabaabbaaaaabbababbaaabaaaaaaabbaaabbbbbbbbabaaaabbbbbabbbaaaabbaaaaababaaabbaabbbbbaaaabaaabbbaabbaaaababbaabaabbaaaabbaaaabbaabaabbabbababaabaaaaaaabababaabaaababbabbbbbbabbbbabbabbbabbbabaababaaababbbbbbaaaabbbababbbabbbaaaaaabaabbbaabbbababbbbbbbbabaabbabbbbbbaaabbbbbababbabbabbaabbaabaaabbaaaabaaababaaabbbbbbababbaabbaaababbabbbaabababbbbbaaaaaaaaabaabaabaababbaabaaaababaabbabaabbbbaaaabaabbbbbbbabbaaabbbabbaababbabaabbbaabbbbaabaaaaabbbabaabaabbabaabbabababaabbbabbbbbbabbbbbbabaabbbbaabaabbbaaaaababbaaabbbabaaababbbaabbabbaaabaabaabbbbbabbbabaabbabababbbabbbbbbbbabbaaaaabaaaaaaabbabbbaaaababbbbaabaabbbbbbbaabbbaabaabaabababbaaabababaabaaabaaabbbbbabbabaaaaabababababbbaababbabaaabbbbaabababbbbabbabaaaaaabbabbbbaabaaaaabbbbbaaaaababbbbbbaabbbaababaaaabaaaaabbabbabbabbabbbababbbbbbbaaaaaabaabaabbaaaabbbabaaaabbbbaaabaaabbbaababbbabbaabbbbbabbaaababaababbbbabaababaabaaabababbaaabbbaaababaaaabaabaabbbbaabaabaaabbbbababaaaababbaaabbaaabbaabbbbbabbbaaaabbbabbaaaaaabababbbaabaabbabbabbbbabbbbbbabbbbbabbabbbababbabaabbbababaaababbbbbabbbbaabbbaaaabbaabbbaababbbabaaaabbbaaababbbaabbaaabbabbaabbbabbbbbaaaaaaaaabbabbabbabbbbbabbbbaaabbbabbababaabababbaabbbbbbbbbaabaababababaabaaaaabababaaaaaaabbababbabbaabaababaaaabbababbbaaaabaaaabbbaabbbbbbabbbbabbabaabbbaaababababbababbbaabaaaaaabaaabaaaaabbabbbaaabbabbabbbabbbbabbabaabaabbbbbaabbbbabbbaaaaabbbaaabbabbbabbbbababbaabbbaabababbabbbabbbaabbabbbbababbbbbbabbabaabababbbbbababbbbabbbabbbabbabbabbaababbbbaaabaaabbabaaabababababbbaaababbabbabaaabbabbababaaaaabbabaaaabbbbbbbbaaabaaabbbabbaaaabbbbbaababbbbbbabbbbbbbbabaabbbaabaaaabababbbabaaaaaaabaabbbbaaaabbabaaabbaaaaaaabbaaababbbbaaaabaaababbabababababbaaaabaabaaabbabaaababaabaaabaabbabbbbbaabaababaabbbbababaaaaaaaabbaabbbabbaaabbbaababbbbaaabaabbababbaaaaaaaababbbbaaaabbbaababbbaaabaabbbabbbbabbbbaabababaabbbabbabaabaaabaabbaaabaabbaabbbabaabbbbbbbbaaaabbaabbbbbbaabbbababaaaabbbabababbbbbbaaababbbaabaabbbbbbaaaabbbaabbbbbbaaaabbabaaabbbaaabbbbbabbbbabaaabaabbbaaaaaabaabbaababaabaaababbbbbbaaaabababbabbaababbbabbababaaaabbbaabaababbaaaabbaaaaaaabbbabbbabbbbbaaabbbaabababbabbababbabbababbbabbabaaaabbabbbaaaaabbaabbababbabaabbaaaaabbaabbbbbbabbbabaabbbabbbabaaabbbaaabaaabbaababbbbabbbbabbabbababbabaaababaabbbbbbabaabaabaaabbbaabbaaabaabbbbbbbaabbababaabbbbbbababbbbaabaaabbaabaaabbaaaaaabaaababaababaabbabbbbababbaabababbabbabbababaaabbbababbbabbaabaabaaababababbbbababaaabaaabaababaabaaaaaaabbbabaaaaabaabbbbbabbabbbaabaabaabaababbaaaaaabbbabbabababaabbabbaabbaabbaabbbbabbaababbabaaababaabaababaaabbabbbaabaaabaabbaabbbababababaabbbaabbabbbaaaabbbabaabbabbabbabaaabbabababbbbababbaaabaaaabbabbbaaabbbabbabbaabbaabbaaabbbaaabbaaababaaababbaabbaabaaaaabbbbbbababbaabababaaaababaaabbababaaaaaabaaabbabbabaabbbabaababbaaabbabaaabbbaaaaaababbaaaabbaaaaabbabbabbbaaabaabaaabbaabbbaaabbbabbaaabaaababbaaaaaabaaaaaaabbabaababaaaaabaabaaaaaaabbabbbaaabaabbbbbababaaabbabaaababbaabbaabbabbbababbabaabbaaabbbbbabbbbbbaabbbaabaaaabbaaabbbaabaaabaaaabbbabbbbabbbbbbbaabaabbbabababbbababbabbabaabbbbababaaababbbbbbabbbababbaababaabaaaabaabbbbaaababbaaaabaaaaabbabaaababbaabbaaaabaabbbabbbabbaabbabbbabaababbaabababbaabbbbbabbbaaaabbabaaabbaaabaababbabbabababbabbabbbabababaabbbbaababbaabbbabaabaaabbababababbababbbbbabbbabbaabbbbabaabbbbabaabbbbbaabbabaaaabbaaaababbaabaabaabbbbbaaaabbababbbabbbaabbaaaabbabaababbababbbbababaaabaabbbbbbbabbbbabaaaaaababaaabbbaabbbabaaabbbaaabbbaabababbaabbaaababaaaabababbbaaaaaabbbbaabbbabbbbbaaabaaabaaabbaabbbaababaaabaabababaabbaabbababbaaaabbbaaaababaabbbabbbbbbabbbaaaababbbbbbbbbabbbbaaabbabaaabbbabbbaabaaaabbabbbbaaababaabbabaaaababbabaaabbabbabbbbaaabbababbbababbbbbbbabababbbbaabaabbbabbbababbaabbbbbabaabbbabaaaabaaaaaaabbbbbbaaaaaaaaabaaabaaababbaabaaaaababbbbaaabbaabbbabbabababbaabbababbaaababbbbbbaabbbbbaaaaabbbbbbaabbaaaaaabbbababbbaababbbaaabbaabbbaabaaaaaababbaaabbbabaaabbabababbaaabababaaaababaabbbbbaaaaabababbabbaaababbabaabbbabbbbbabbbabbbaaabbabbbaaaaaababbabababbaaaaaaabaaaabbaaaaababbbabaabbbaaaabbbaaabbaabaaaabbabbbababaabbbbbaaaaabbaaaaaaabaababbaabbbabaabbbbabbbaaabaabbaabaabbbaaababbabbbbbabbbbabaabbaabbbabbaaababbbabbbbaababbbabbaabbbaaaaaabbababbbbbabaaaaaaaaabababaabbbaabbaabbbaabbbaaabbaabbaabbabbabaaaaabaaaabbaaabbaabbabbbaaaabbbaaabaababaaabbaaabbbaabaababbaaaababaababaabababbbaaaaabbbbbabababbbaaaabaaaaaabbbababaabbbbaaaabbbbbaabbaaaaaaaabbaabbbbbbbbbaabbbababbaabbabbaabaababbbbbabababbbbbabaabbbaaabbbababbaaaabababaaaababbabbbbbbbbababbaaabbabbaaabababbbababaabaaaaaaaaababbaababbbaaabbabbbaabaaaaaababaaabbaabbbabaabbabbbbababaaaaaababbabbaaabbaabaaaaaaaabbbaaabababbbbabbbababbaabbabaaaaaabababbaaaabbaaaaabaabbbbaabaababababbbabaaabbbbbaababaaaabababbaabaabbaaaaaaabbbaaaaaabbaaabbaaaaabbbbbbbaaababbbababbbaabbaabbbbaaabbabbbbaabaabbaaabbabaaaabaaabaaababaabbabbbabaaaaaaabaababbaabbabbbbbbaaabbbaaaaaaabbaabbbabaaabbaabbabaaabaaaaaaaabbabbbaaaaabababbbbbabbbaaaaabbbbaaaaabababbbabaababababbaaababbaabbbaaaaabaabababaababbbabbbaabbaaaaaabbaaabbbbbbababaaabbaaabaabaaaabbabaaababbbbbbbbbbbababbabbaaabbbabbababbabbbbbbabaaaabbabbbababbbabbbbaaabbaaaaababbbbababbbbbaaabbaaabbbbbbbaaaabbbabaabbbaabababaababbaabaaababbbbbaaaababaaabbbbabaaababbabababbaaaababaaabaaaaaabaaaabaaaabaabbaaaaaabbabaabaabababbbbabbbabbaababbbbabbbabbbabbbbaaabbbbaabbbaababaababbaaabaaababababbbbbbbbaaaabaaaaaaaabaaaaaaabbbababbaabbbaaabbbaaabaababbabbaaaabaaabbabbaaabbaaabbbabbababbaabaaabbbababbabbaabaabbaabbbbaabbbbbabbabbbbbaaabbbaaabbbbbaaabbaaaaabbbbbbaaabbaabbabaaababbabababaaaabbababbaabaaaaaabbabaaabbababaaabbaaabbabbaaaabbbabbbaaaaababbaaaaaaaaaabbabaabbbbaaaababbbbbbabaaabbbbbaabbbbbababbaabbbbaaaabaabbbababbabaaababaaabbabbbabbababababaabababaaaabbbbbaaababaaaaababaabaabbaaaababaaaabbbaaaaaaaabaaaabbbaaababbbaaaaabaaaababababbbabbbaabaaabbaabaaababaabbbaaaaaabaaaaabbaabbbbaabbabbbbabbabaaaaabbabbaabaabbabbaababbaaabbabbbbbbbbbbaabababaaabbbabbaaabbbbabbbbbbabaaaaaaaaaaababababbabbbaaaabbbbabbaabaababbbbaababaaaaaaabaabbbaabbaabaabaaababbaababaabbbbbbabbaaabaabaaaabbbbabbababaaabaabbaabababbbbbaaaaabbaaaabbbaabaaaaaaaababbaaabaaaaabbbabbbbbaaaaaabbababaaaaababbbbbbaaabbaaaabbaabaaaabbbbbabaaabbbaaabaaaaabbbaabbaaabbabbaaabaabbbaababbbbaabbababbbabbaaabbbababbbbbbbbbbbaaababaabababaaaababbbbababaabaabaaabbbababaaaaaaabbaaaababaabaabbaabbabbbabaabbabbbbbbabbabbaabbabbabbbbabbabaabbabaaaabbaaabababbaabbaaaabbbbabbbaabaaaaaaaabbbbbbbbabbbaaabbbaaabbaaabbbbbabbaaabbabbababaaabaaababbbbbaabbaabbaabbbabaaabbbaaaabbabbabbaaababbbbbbaaabbabbbaaaaaaaaaababaabbaabaabbaababaabaaabbbabbababbabbababbbababbababaabbbabbabbbbbbaabbaababbaaaabbbaabbbbaabbbbbbabbbababbbbbbbaaaabbbbbbabaaaaabbbbaaabaabbabaaabaaaaaaaaaaababaabbbbabababbbabaaabbbbbabbbbbbaabaabbbbaaabaabbbabaabaaabbbababaaabaabbaabbbbaaaaababababbabaabbbaabaababbbbbaabaabababbabbaabbbabaaaaaaaabbabbaaaaaaaabbaabbbabbbaabbbabababbbababbbaababaaaaaababbaaaabbaaaaaaaaaaabbbbaabbbbbbbabbbababbaaabbbaaabaaabaabbaabaaaaabbbabbbbbbabbabbabbaaaabaaabbaaaaaaabbbbabababbbbbaababaaaababbabaaaababbbbbaaaabbaaaabbbbaaabbbabaabaaaaaaaabbbbaaabbabaaabaabbbbabbbaaabaabaababaaababaaaaaaabaabbaabaaabaaaabbbbaaabaaaabaaaaaababaaaaabbabbaaababaaababbbbaabbbbbbabaababbbbbaabbbaababaaabaabbaaaabaaaaabaababbabbbabbaabaabbaabbaabbbbababbbbbbbbaabbbaababbabbbaabbabaaaaaaabbbbaaaababbabaaaaababbbabbbabbbabbbaaaaaaaaabaaaababbbaabababbabbaababbaaabbbbbbbabbbbbabaaabbbabbbbbaabbabbaaababbbababaabbaaabbbbbababaabaaabababbbbbaabbbabbbaababbbabaaaababbbaabbaaabbabbababababaabbabaaabaabbabaaabbabbbaaaaaabaaaaabbabbbbabbababbbabbaababbbaaabbbbbaaababaaaaabbbababaaabaabaaaaabbbaaaabbbbbabbabaaabbbabbbbbaaaabaabbbaaabbbbbaaabbaaaaabbbbabaabaaabbaaaababababbbbaaaaababaabaababaaabaabbabbabbaabbaaabaabaaabbbababaabbabbbaababbbbaaaabbabbabbbbbbbbaababbbbabbabaaabbbaabbabbaabaabbaabbbbabbabaabbbabbbbbbbabbbbaaaabbbbabbaabaaaababaaaabaaaaababbaaaabbbaaaabbabbabbababbbbaaabbbbbbbabbbaaabaaaaabaaababbabbaaabbaaaaabaaabbbabbabbabaaababbbbbaaaabbbabaabbabaaaaaaaaaabbabbbababbaaabbbaabbababbaabaaabbbbabbbaababbbabaabbbbbbabaabbaabaabbbabaaaabbababbabbabaabababaabbbbabbbaaaaabaaabbabaabaabaaabaaaabaabbaabbaabbbababaababbababbbbbababaaababbaabbbbbbbbbaaaaabbbababaabaababbaabbabababababbbaaabbbbbabbbbaaababbabaababbbbabbabbaabbaaabbaabbabbababbbbaabaabbbbbbabbaababbaabababbaaaabaabbabbaaaaabbaabbbbbbbabbbbbaaaabbaaaabaaabaaabbbbbbaaabbbaaaabbbaaababbabbbbbbbbabaababaabbabbbaabbabaaaaabababaaaabbbaabaabaaaaabaaabbaabbbaaabababbabbbaabababbbbbbbbbabbbbabaabbaabababaabaaababbaaaabbaabbbabbbabbbaabaabababaabbbbaabbbaaaaaabaababaabbbbabbababaabbabbbbaaabaabaaabaaabaaaabbaabaabbbabaaabbabbaabbabbbbababbbaaababababababbbbabaababaaaaabbbbbabaababbbabaabbaababbabaabbaabbbaaabababababbababaaabaaaaabababbbaabbabbbabbbababaaabbbbbaaaaaabbababbabbbaaabbbbbaabbbbaaabababaababaaaaabaaaaaaaabbabaabbaabababbabaababbabbbaaabbaabbaaaaaaabaaabaaabaaabbababbabbbbbbaababbabaabaaabbbabbbaaabbaababbbaabbbbbbbbbaabbbabaabbbaaaabbabbbaabababbaaabaabbaaababaaabbaabbbbaaaabaaaabbabbaabaabbaababbbbababbabbabbbababaabbabbbaabaabababbbbaaaabaabbaaababababbbabababbbababaabbaabbaaabababbaabbbbbbbabbaabbababbaabbbaababbabbbbbbbbaaabaaaabbaabbaabbabbbaaababaaaaabbaababbbabbbabbabaabababbbaaaaabaabaabababbabababababbbbababbabbbbbbaabaaaabaabbbaabaabaabaababaabababbaababbaaabaabaababbaabbbaabbbbabbaababbabaabbabbbbaaababaaabbbbbbaaaababaabbbaaabbbbbbaababbbbbbabababbabbbbaababbbabbbbaabaabaabaabbbababbaaabbbaabaaabaaaababbabbbaaabbaaaaababababbbbabaaaabaabbaabbbabbbabaaaaabbbbbbbabbabbaabaaaaabbbbbaaababababaaabbabbaabbbabaabbabaaaabbbbbbabaabbbbbbaababbbbbbbabaababaabaabbbaaabbbbaaaaabbbbbaaaabbababbbbaabbabbbaabaabaaaaaabbaaaabbbbabbbbababbababbabbbababbaaabbbbababaaabbaabbbabbabbabaabbaabbbbaabaaababbbbbbbbbbbabaaaabbaaabaaababaaabbbbbaabbbbaaababaaaaababbbbbaaaabababbaaabaaaabbabaabbaabaaabaaaaaaabbbbbbbaaabbbaabbbabbaaababaabbbbbabbbbbabaabbababbbbbababababbababbbbbaaaaaabaaababbbbaabababaaabaabbbaababbbbbabbbbbbaabaabbabbbaaaaaaabbaabbbaabbbaabbbbaabbaaabababbbbbbbbaaabaabbbababaaababaabbaabbbabbaabbbaabbbbbbbbbabababbaaabaabaabbaababaaabbabbbaaaaaababaaaaaaaabbaaaabbabbabaaaabbbabbbaaabbbbababbabbababbaaaaaabaaabaaaaabaabbababababaabaabbbbbbbaababaaaaabababaaaaabaababaababbbbbabbabbbaababbbabbababbbababbbbabaabbaaaabbbabbaabbaababaaaabababbbbabbbabaaabbababbbabbababaaaabbabababbaabbbbabaabaaaaaababbaabbbaaaabbbbababbbbaaaabbaaaabbbaababbbabaabbbaabaabbabbabbabbbbbabbbbabbabaaaabaaaabaabaababbabbbbbbbaaabbbbabbbabababbbbbaaaabbabbbaababaaaaabbbaabababaabbbaabaababbaabbbabaaabbabbabbbbbbbaabbbaaabbababbababbabbbaaaaaaaabaabbbaabbababaaaaaabaabaabbaaabbbbbabaabaaaabbbbababbbbbbbbababbbbaaabbaaababaaabbabbababbababaaaaabbbaabaabababbababbbbaaaaaaaababbbbaaababaaabaabaabaaaabbbbaabbbaabbbbaaabbbabbaabaabbaabaaabbabbaaaaaababaaabababbababbaabbaaaabaaabbabbbaaabbabbaaaaaabaaaaaababbbbbaababaaabbbaabbaaaaabbbabbaabbaaaaaaaabaabaaabbabbbbababbabaaaabbbbabaaababaaabaabbaabbabbbbbaabbaaabbabbbabbbbbbbbabaabababaabbbbbaaababbaababbababbaababbaaaabbabaaababaabbbababbaaaababaaabaaabababbaaaaabaabaabbbaaaaaabbbaaababbabbaabbaaaaaabbbabbbbbabaabbaaaabbabbbbaaaaaaababbababbbbbababbbaabaaabaabaaababababaaabaaaababbbbaaabaababbaaabbbbaaaaaababbabaaaabaaabaabbaaaabbabbbbabababbaabbbabbbbabaaabbbaabbbaabbababaaababaabaabbabbaaaababababaaaabbaabaaaabbbaaabaababababaabbabbaaabbaabaaaaababababbbbbbbababaaabbbbbbbabaabbaabbbbabaaaaaaaabbbbaaababaaabaabaabbbbababaababbbabbaababbbbbbbabbbbabbaabababbaabababaaaabaaaababbbabbabaaaabbaabaaaababbaaabbbbabaabbaaabbbabbababbbaabbabaabaabbbaaabbbbaabbbbbabbaaaabbbbabbababababababbbbbaaababaabbaabbaaaaaaabbabaabbabbaaaaabaabbaababbababaaabbbaababbbbbaabaaaabaabaaaababbbbaababbbbabbaabbbbbaaaaaabbaaabbaaabbbbaabbaababaaababbbabbbbbaabaabbbbbaabbbbaaabbbbbbbbbbaabbbbabbbbbbbbaabaabaababbaaaabbbaabaaabbabbabaaaabbbaaaabbbbbbabbaaabbabbaababaabbbaaabbabbaaaaaabbabaabbabbaabbabaababbbabbaabaabbbabbaabbbbbbabaaaaabbababaabaaaabaaaaabaabbbabbbbaaaababbbbbaabbaabbbaaabaabaababaaababaabbaaaabbbaababbbaabaabaaabbbbabbaaabbbabaabbbabaabbbbaabaaaaabbabbbaabaabaaaaabaabaabbaaaabaabbaabbbabaabaababaabababbbabbaaabbbabababbbbababaaaabbbbababbbabbaaabbabbaaaaaaabbbabbbbbaabababbbbabbaaabababbbaaabbbbbbbbabababbbaabbbabbbaababbbaabbbaababaabbbaaaaaaabaabbabbaaaaaaababababbbbbaaaaabbababaabababbbabbaaaaaaaabbabbaabbbaaaabbabaaababaabaabbaababaababaabbabbabbbaaaabbbaabaabbbbababaabbbaaaabbbbbaaabbaabaaabbbaaaabbbababababbabbbbbaaababbaabbaaabbbabbbabaaaabbbaabbabaababbababbaaababbbaabbaabbaababbababaabbbababaaaabbabaaaaaababababbaabaababbbaaabaabaababbbbaaabbbbbaaabbabaaabbbaabaaabaabaababababaabaaaaaaaabbaabbbaaabababbbbbbabbbabbbbabbbabaabbbabbbaaabaaababaaaaababbabbbbaaaabbbababaabababbabbaaaabaabbabbbbbbbabbabbbbbbababbbabbabbabbabaabaaabbbbaaabbbbaabbabbbaaaaaabbabbbbbbaabbababbaaaabbbabaababaabbbaabbaabbaaaaabaaababaabbabaabaabaabbaabaaaabbabaaabaabababbaabbababbbabbabbbbabbbbababbabbbbbaabbaabababbaabaababaabaaabbaabababaabaaaababbbaabbabbaaaabbabaaaaaababbaaabaaabbabaaabaaaaaabbbbbbaabaabbababaaabababaaabbbbbabaaababbaaabbbbabaaababaaaabaababababbbabbbbabbbabbbbaabbbabbaabbaabaabbbababaaabbaabaaaabaabbbbbbababbbababaabababbbaabbbbbabbaaaaaabbababbbabaaabaabaabbbaabaaabbbabaaaabababbbbaaaabbaaabbbaababaaaaabaabbaababaabbabaabbaaabbbbbaababababbbabbbaaaaaaabaaaabbabaaababbaaaaabababababaaababbbabaaabaaaaabaaaaaaabaabbbaabbbbbabaaaababaaaaaaaabbaabbbbbbabaaabbababbbbbaaaabaaabababaababababbbaabaabaabaaabaabbaababbbbaabbaaabbaaaababaaababababbbaaababbababaaabbabaaabbaaaaaaababaababbaaaabbbbaababbbabaaabababaabbbabbabababbababbaaabaabbbbbbaaaabbbbababbabaaabababbbbbbbbabbbbaaabaaababaaaababaabababbbabaaabaabbbbbbbbbaabaaababbabababbbaabbaababbaaaabbbabababaaabbbaaabbaababbabbaababbbabbbaabbbbaaaaaaaabbabaaabbabbaaaabbbaaaaaaabbbbaaaabbabaabbabbbbbabaabbbbbaaabbbabbbaabbaabbabbabbaaabaabaabbaabbbbaabbaaaaabaaaaabababaaababaababababbbbaabababbbbaabaabbaaaababbbaaababaaaabbbabababaabbbabbbbaababbabaaaabababbaaaabaabaabbbababbbbabbaaaaaaababbabbbabaaabaabbaabaaabbbbaabbaabbababaabaaabaababbaabbbbbbaaababbbbbbababbaabaaabbbaaababaaaaabbabaaaaaaaabbbababbabababbabbbaababaaaabaabaabbbbbbaabbbaaaababaaaabbbaabaaaaabbabaabaaabaaabbabbabaaaaabaaaabbaabaaabaabbbabaaaabbaabababaaaabaaababbbbaabbbbbabaababbababaaaababbbbabbaaababbabbabbaabababbbbaabbaaabaabababbbabbbabbbbabaaabbbabbaaabbabbbababbbbaaaaabbbaaabbaaaabbabbaabaaabbbbbaabbbabbabaabbabbbaaaabbbbaaaabaabbaabababbbbbabbbabbbbaabbabbaaaaaabaababaaaaabbabaabbbbbaaaabbaabbaabbbaabaaaabbabbbbbbaaaabbbbaaaaaaabaaabbaababaaaaaaabaaabababbbbabbababbbbbaaabbabbaaaabbaaabbbabbbaababbaaabbbbbaaaaabaaaabaaaaaaaabaaaabaababbabababbaabababbaabbaabaababbbababbbbbabaaabaabaabbabbbababbbbabaaaaaabbbaaaabaaabbaaabbabbbbaaabbaaaabbaabbbbbbababaabbbabbabababaabaabbaabaaaababbabababbabbaaabbabbaabbbbbaaaaabbbabaabaaabaaababbabaaaabaababbabbaaaaabababbbbaaabaaabbababbabbbbabaaaaaabaaabbabbaabbaabaaabbbbbaaaababaaaababbabaaaabbabaabbabbaababbbaaabaabbabbabbbabbbabbbbbbbbabbabbaababbbabaaaaaaaaaaababbbaabbbabbbaababababaaabbabaababbaabbaaaaabababbbbbaabbabbabaabaaabaaaabbabbbbaabbabbbaababbabbaabbababaaabbaaabbbbaaaabbbbabbbbabbbabbbbaaabbaaabaabaabbaabbabbaaaaababbbaaabbbbbbbbbbaaaabbaaaabbaabaaaaabaabbbbbaabaaabbabbabaababbbbbbbaabaaababbaaabaabbababaaaababbabbabbbbaabbbaaabbbbbaaaabbbababbbabbaabaaabbabaabbabaaaaaaaabababbbababbbbabbbbbabbaaaabbabaabaabbabaaaaabbaababaabbaabaababbaaaaabbaaaabbbbaabaaabbabaaaabbababbaaabababaaaaabaaabaaabbbaababababaabbabbbbbbabbaaaaabbbaabaaaaabbbaabaaabbaaabababbabbbbbbbaaaabbaaabbabbbbbbbbbaabababbbbabbbbabbbbaaaabaababbbabbaabababababbaaababbaaabababaaabaaabaaabaabbbbbbbbbabbbaaabbabbbbabbbababaaaaaabbbbbbbbbbaababbbbaaaababaabbaabbaabaabababbbbabbabbbbbabbabbbabaaabbaababaabababbabaabbbbbaabbbbabbbabaaabbbbaaaaabbbabbbbababbbabaabbaabbabbaabbabbaabbbbabbbbabbaabaaaaaaabaaaabbaaaabbbbbabababbbbaabbbbbbabbbaaaababbbbababbbbabaabaabbaabaabbaaabaabbbbabaaaabbabbabaaaaaababbbbbbabbaabaaababbabbbaaaaabaaaabbaaabbbbaaabaaaabbbaabaababaababababaabaaabbaabaabbaabbbbabbabbbbaaabbabaabbbaabbabaaaaabbaabaabababbbaabbbabaabaaaababaabbbbbabaaaabaabbbbbbbbbabbaaaaabbbbabbbaaabbbbbbaababbaababbbbbbbbbababbabbbabbabaabaaabababbbbababaabbbbabbaabbbabaababbbbbbbabaabaabbbabbbaaabbbabaaaabbbbbbbbabbaababbbbbaabbbababbbbbaaaaabbbbbbbbabbabbbababababaababbbaaabbabbababbabaaaaaabaaabaabbaabaaabaabaabbbabbabaaaaabbbabbabbabbbbababbabbbabbbabaababaaaaaabbaabaaaaaaabbabaabaabbbbaaaaaababaaabbbbababbaabbaaababaaabbaaabaabbbbbbabbbaabaaabbabaabbbbbaaaabaabbbbbabbabaaaaaaaaaabbbbbaabbbbaabbbbbbaabbababbbaabaabbbabbaabaabbbbaaaabbabaabaabbabbabababbbbbbabbbbbbaabbabaababaabaababababbabbaaabbbbaaababbbaaaaaaaaabaabbbbababbbbaaaaabbbbbbbabaaaaaabbabbbababbbbbabbaaaaabbaabbbaabbbaaaaabaaaaaabbaaabbbbbbbbbbbbbababbbaabbbbbabaaababbbbaabaabbbbabaaaaababbbbbbbbbbbaabaaaaabaaabababbbabbaabaabaaaababbababaaaabbabababbbababbabbaababbaabaaabaabaaaababbbbabbbabbbbaaabbbbbbbbaaabaababaabbbbaabababaaaabbaababbaababababbaabababbbbbbaabaabaaaaabbbbaabbaaabaaabbbaababababababbbabababaaaabbaaaaaababaabbaabbaababbbababbaababaaabbbaababbbababbbabbabbbbababbbbbabbabbbbaaabbbbbaaabaaabbabbaabbaababbbaaabbbabababbbbbbabbbaaababaaaaaaaaabaabaaaababbbaababbbbbaabbabaaabbaaaabbabbaababbbaabbbababaababaabbbbbababbaaaaaaabbaaaabaaaaaaaabaaabbbaabbaababaabaabbbaaabbaaaabbbaaabaabbbbaaaabbbbbaaabbaaabbbababaaaaabbbbbaaabbaabbbabbabbbbababaaabbbabbbaaabbbaaabaaabbaaaabaaabbabbabbbaabbaaabbabbbaabbaababbbaababaaabababbaabaabbaabbbabbbaabbabbabbabbababbaabaabbbbaabaabbabbbbbbbbbbaabbababbabaaabaaabbababbabbbbbbaabaabbaababaabababbbbbabaabaabbaaaababaaaabbbbbbaabaabbbbaababbbbabbbaaaaabaababababbabbababaabaabbbbbbaaabbaabbabbabbbaabbbaabbbbaabbbbbbbaabbaabaaaabbabbbaabababbbabaaabbbbbbbbabbbbbbababababaaaaaabaaabaabbbbbabbbbabbbaaabbaaababbaabbbbbaaaaabbbabaaabbaaaabaaababaaaabaabbabbabaabaaaaaaabaababaabaaaaaaabbaaaabaababbbbbbababbbabbaabbbbbbbbabababbbbbbbabbbabababaaabaabbaabbbaababbaabaaabbbaaaabababababbbbbbbbaababbbababbabbbaaaaaabbabbabaabaababbabaabbaabbbaaabbaaababbaaabaabbbbbbbaaabaabbbaabbbabaabaaabaaaabaaabbbbbbbbbaababbaabaaaabbbaabbbbaaababaabaaabaaaaabaababbaabaaaabbbabbababaaaabaaaabbaaaaaababababaaabbaabbabbbaaabaaaaaaaabababbabaaaabbabbabbbbbaaaaaaabbbbbaaabbaabbbbbaabbabbbabbabbbaaaababaaabbbababaaabbbbbbbbbabaabaaabbbaaabababbabbababababbbbbabbbabaaaabbbabbbabbbaaabbaabbaabbabbaabaaabbbbaaaabababbabaaaabbbabaaaaaabbabbaabbaaaaaaaabaaabaaabbbbaabbbbbaabbaabaababaaaabbbbbaabaabbabbbbabbbaabaabaaabbabbbabaabaabbabbabbabaabbababababbababbaaabaaabbbabbaaaaababbbaaaaabbbaaaaabbabaaabaabbaaabbbaaabbabbbaabbabbbabbabbbbbbbbbbaaababaaabbababbbaababbaabbaababbbbababbbabababbabbbabbabababbaaabbaabaaaabbbaaabbababbbaaaaaabaabbabaabaabaaaaabbbabaaabaaaaabbbbabbbbbabbaababbabaabbbabbaababbbbbbaabaabbabbbbbbbaaabbbbabbaaabbababbbabbabbabbabaaaaabbaaaaabbaaaabaaabbbbaabbabaababbbbababbbbbbbaabbbabbaaaabaabbbaabbaaabbabaaaaabbaaabaababbbbbbaabaaabababbabaabaabbbabaabbbbbbbaabaaabbbbaabababbaabaaaababbbabbabbaabbbbaaabaabbbababbbaaaabbaababbababaaabaaaabaaaaaaaaabbbbbabbaaabbabaaabbaaabaabaababaaaabbbaababaabaaaabbbabbbbaaababaaabbbabbabbbababaaababbabaabbbaaabbaabbbabbaabbbabaabbaaababbbabbaaaabbaaababbabbaaabaabbbbbbaaaaaaabababbabaabaaabbabbaaaaabbabababbbbababbbbababbbbaabbababaaaaababbabaaababaaaabbaabbbabbaaaaaabaaabbabbbaabbabbaaabbabbaabbbabbaaaaaabbbabbbaababbbbbaabbaaaaaababbabbbaaaaabababbaaabbbaabbabbaaababbbbbaabbbbbabbbababaaabbabbaabaaabbbbbaabbabbbbabbbbaabbbbbbbbaaabaababaabbaabaaababbaababbbbbabaababbbaabbbabbbbbababaaabbbabbaaaababbabbaaaabababbbbabbbabaabbbbaaabbaaaabaabbabaabbbaabbbabbbbbabbabaabaaabbbaabbaabbbbbbbbababbbabbbbabababbaaababaabbbbbaabbbbbbaaaabababbababaabbaaabaaabbaabbaaabbbbbabbabbabbaaaabababaaababababaaabbabbbaabbaaabababbabbbaaabbbbaabbaabbabbbaaaaabbbbabbbbbabbaaababaabbbbaabbbbaaaabaababbbabbaabbbabbbbbaaaaabbababbbaabbbbaaabbaaabbbaaaaaaabbabbbbbabbbbaababaabbbaaabbabbbbbbaabbabbabbaababbaaababbabbabaabaabbbaaabbabaaaaababbabbabababbabbbbabaaababbaabbaabbaababbabaabbabbbbbabbaabbbbababbbbaaabbbaabaabbababaaaabaababbbbabaaababbbbababbbabbbbbbbbabbaababaabaaaabbbbbbaabbaaabbbababbbbabbbbbabbbbaabababaabbbaababaaaabbabbaaaabaaababbaabbbaababaabaabbabbabaabaabbbbbbaaaababbbbbbbaabbbababababaaabbaabbaabbaaaaaaabbaaabbbaabbababaaabbaaaaabababbabbabbbaabaaaaababbbbbabaaaababababaabaabbbbabbabaababbbabbaababababbaabaabbbbaabbabbbaaabbaaaabbaabbaabbbbabbbbabbbbbabbbaaabaaabbaaabaababbbbbbbbbbbbaabaaabbbbabbabbbbbbaaabbaabbabaaaabaabbbbbabbbababbabbabaabaabaaababbbbbbbbabababaaaabbbaababbaabaaabaababbbaababbbbbabaabaabbbbabaababbabababbaaabaababbababbbabbbbbbabaabababaaabbabbababbbbbabbbbbbbbabaaabbaaaaabaaaaabbaaabaaaaaaabaaabbabbbbbbabbbabbbbbababbbbbabaabaaaabbabbbababbbbbabaabbbaaababaaaaaabbabaabbbbbbbbabbaabaabaabbabbabbbababbbbabaababaababaabbabbbabbbabaaabaaabaababbaabaaababababbbaaabbbabbbbbbbbbabbabbababbbbbbbaaaaabbaabbababbbbabaaabbababaabaaaaaaababbabababbaabaabaaababbbabbbbbabababaabbbabaaabaabaabbaabbbbbbbaabaabbaababbbaaabbbabbaaabaabbbbaaaabbaababbababbbaabaaaabbbabbabaabaababbaababbbbbbababbbbbbaaabaabbbbababaabbbbaabbbabbabaaabbaaaaabbbabbaaaabbabaabbbaaaabbababbabbbaabaabbbbaabbabaaababaabaaaaabaabbabbbabbbaabbababbababaababbbababaaabaabbabaabbbbaaaaaaaababbbbbbbbbbababbbbababbbbbabaaabbbbbabbbabaaaabbbaabaaabaaabbbabbbbaaaabaaabababbbabaaaabbbbbabbbbaabbabaabbabababaabbbababbbabbbabbbbbbbbaaabbabbaabbabbbbbbbbbbbbbbbabaaaababababbbabaaababbabbaaabbabbababbaaaaaaaaabbaabbaababbbabbaabbabaabababaabaaaabbababbbabaabbbaaaaabaababbaabbaabbaaabaaabbbbaaababbbabababababbbbaababbbababaabaaaaaababbbaabbaaaababbabbaabbbbbbbbabbaabbbbbaabaababbaaaaaabaaaabbbabaabbbaaabbbababbbaababbbabbaabaaabbabaaaabaabaaaabbbababaabbaabaabaabaabbaabbbbbbbabababaabbbbababaababaaabbbbaabbbbbbaabbbbaaabbbaabababbaababababbaaaabbbbbabababbbbbbaababaabbbabaabbbabbabbbbbababababbababbbbaaabaaaaaabbbbbbbbbbaaaaaaabbaaabaaaababbaaaaabbbaaaaababbaaabbbbbaaabbaabbababaabbaabaaaaabbaaabbbaaababbabaaabbabbabbabbbabbababbbaabaaaaaaaababbbbbaabbaabbaaaaaaabbbbbbbabbabbabbbababbbababbaabbbabbbaaabbbbaabaabaaaaabaaaaabbbaaababbbababbbabbabaabaabaaaabababaabbbabbbababbaaabaababbbaaabaaaabaaaabbbbaabbabababababababababbabbaabbaaabbbbaabbbabbbbbbaaaaabbbaabbbbababbbabaaaaabaabbabbaabbaabaaababaaaabaababbbaabbabbaaaaabaaabaababbababbababbaaabbbbbababbbbaabbaabaabbabbbbbaabbbaababbbaaabababbaabbabaaabaabbaaaabaaaabababbbbbbbbbaabbaaaaabbabaabbbbaaabbaaaaaaabaabbbabbabbbbbbaaabbaaabbbababbabbbababbbbaaabaabaabbbaabaababbaabbaaababbbabbaabaabaababbabababaabbabbaabbaabaabbbbbaabbbabbabbbabbbbaabbbbaaabaaabbaaaaababbbababbbabbbbabaabbaabababaaabbbbaaaaabbbbabbbabbbbbaabbaabbbbaaaabbbbbbbbabbbaabaaabbbbabaabaababbbbabaabaabaabbbbbbbbaaabbbaabbaaababbabbaaabbabbbbbabbaababaabbaabbaabbabbaabbabbaabaaaabbaaaaaaabbbbaaaabaaabbbbbaabaababbbaabbababbbbabbabbbaaaaabbaabbababbaaaaaababbabbaababaaaaabaabbababbbbbaabbaaaabaaabaabbbbbaabbabbbabaaabaabababbbaaaaabbbbbbbabbaaaaaaaababbaaaaaabababaaabbbbaaabbabaaabbabaaaabbaaaaaabababaaaabbaaabbbabbbbabbbabbaabbbabaabbbbbbaababaabbaabaabbbbbabbbababbabaaabbabbbbababaaaaaaaaaabbbbaaaabbabaabbaaaaaaaaaabbaaabbabbbbbababbbbbbbbbbaabbbbbbabaabbbaaaabbaabbaaaaaaabaabbaaabbabbbababbbbabbabbbbabaaaaaabaabbaaaaaaabbabababbabbabbbbbbabaaabbabbbabbaaabbbaabbbbbabaabbbbababababbabbbbbbabbbbbabbbabbabaaabbababbabbabaaaaaabaabbabbaaababbaabbbaaabbabaabbbabaaaabaabbbabbaaababaaaabbabbbabaaaaaababaabbaabbbbabbaababaababaabaaaaabbaaaaaabbbbabbbaaababbababaaababaababaabbabbbabbabbaabbaabbbbabbaaaaaaababaaaabaabbbbbaabaabbbaababbbaaabbaaaabbabbaababbaabbbaabbabaaaabbabbbbaaabaaabbabbaabaabababaaaabaaaaabbaaababbaaabaaabaaababaabbbabbabaabaabbaaababbbbababbaaababbabbbabbbbabaaabbabbaaabbbababbababababbaabbbbbababababaababbbabaaaabbaabaaaabababaabbabbaabaaaababaaaabaabbbababbaaabaaaabaabbbbababaaaaabaabababbaaaaabbbababbbaababbabbaaaababaaaaabaabaaaaaabaaabbbabababababaabbbabaaabbaabbbbabaabaaabababbbaabbaabbaabbababaaaaabbbbabaabbabbabbbbaabbbaaaabbbaabaabbabbababbaaaaaaabbaabaaaaaabbabbabaabaaabbaaabbababbabbbbbbabbaabbaabababbaabaaabaabbbbbbbabbaaaabaaaaaabbabbbbaaabbaabbbbaaaabababaababbabaaaabbabbbbaabbbbbbbbbbbaabbaabbbaabbbbbaaababbbaabbabaabbaabbbbabbbabaaabbaabababbabaabaabbbaaaaabaabbbaabbaabbbaabbababbababbababbbaaaabaabbbabbaaaabaaabbaaaabbbbbbaabaaaabbababbaabaababbababaaaaaabbabbbbbababbabaababaabababaababbbaabaaaabbabbaaababbaabaaababbbaaabbaabbbbbbabbbbbbbbabaabaabbbbbabababbabaaabaaabaaaabbbababaababbabbaaaabababababaabbbababbabbbbbabaabbabbaabaaababbabbbbaabbabbabbbbababaaabbababbaabaaaabbbbabbbabaaababaabbaaaabbbaaaabababaabbbabababbbbbbaababbababbaabbbbaaaabaabbaaaabbbabbaaabaaaabbababaaaaaaabaabbaaabbaabaabbbaaabbbbbaababbbbbbbababbaababaabbbaababbbabbbbbbbaabaaaabbabaabaababaaaaabbaaaaabaabababaaaaaaabbabaabababbbaaaabababbbbbbbaababbbabaaaaaababbabbabaabaaaaaaaabbaabaabaabbbababbbbababababbaabaaaabbbbabbababaaabbaaabbbababbbbabbbaabbbbbaabaaaaaaabbaaabbbbbaaabbbababbbabbbaabbabaaaababbaaabbbbbababbaaaabbbbaaaaabaabbbbaaabbbababaaabbbbababaaaaaaaaababbaaabaababbabbabbaababababbababbbaaaaaabaabaabaaababaaaababbababbbababbbabbaabbbbbaabbbbaabbbbbabaaaaabbbabababaaabbbaabbbaaaaababbbaaaaaaabbbbaababbbbbababbabbababbabbaaabbabaaaabbbbabbaabbabbaaabbbababbbbaaabbaaaabbbbaaabbbabbbababbababaaaaabaaabaaabbabbbbabababbbbbbbabaabbbbbababbaaaabbabbabaabbabbababbbabbababbaaaaabbaabaaabaabbbabbbabababaabaabbbbbbabbaababbaaabbababbabbaaaabaabbabbbabbaaaabababbbaabbbaaababaabaaaabbabbbaaaabaaabaabbbaaaaabbbbbbbbaaaaabbbababbbaaabbaaabaababbaabbaaaabababbbaabaaabaaabbbbaaaabbbaaabbbbbaabbaabbbaaabbbabababbbbbbaaaaaabababbabaabbbbababbabbbababbbababbbbabbbbabaaaaabbaaaaaababaabbabbaabaababbaaabbaaaabbbabbbbaabbabaabaababaababaaabbbbbaabbabbabaabaabbabbbabbabababbbbbaabbbaabaaaaabbbbabaaaabbabbbaabaabbabaababbbabaaabababbbaabbababbaaabaaaabbaaabaaaabbbbbabbbabbbbbaaabaabbabababababbaaaaaabaaaabaabbbababbabababbbaabbabbabbaababbabbbbaaabbabbababbbaabbabbbbaabbbabaabbababbaababbaaabaababaaaabbaaabbbbabbbbaaabbbbaabaaabaabababbbaabababbaaabbbbbabbbabbabbbabbabbabaaababbbbaabbabaaaabbbaaabbaaaaababaababbaabbbababababbbbaaaaaabbaabaaaabaaaaaaabbabbbbaababbbbababbaaaabbbaabbbbaaaabaababaabbbbbaaabbbbaabaaaababababbbaabbaaaabaabbabbabbababbaababaabbbbbbbbbababbbaaaabbabaaabbaaabbababbbbbbbbbbaababbabbbababbaaabaaaaabbbbbaaaabaaaabbabaababbaabbbabbaabbbaaaabbabaababaaabbbababbababaabaaabaaabaaaabbbaabbbbabaabbaaaaaaabbaaaaabbbabbbaabbbaabababbaaabbaaabbaabbbabbaabbaaabbababababbbbabbaaaabaabaaaabaaabbbaabaaaaabbbabbaaabaaabaababaaaaaababbbabaaabbaababbbabaabaabbabaababbaaabaaabbbbaabbbbbabaabbbbaababaaaabbbbbbaababaababaaabbbaaaaaaaaaababbbaababbababbbabbababababaaabbaaabbaababbbbabbbbbbbababaaabbbbabbabbabbbaaaabbbbbbaabbbbbaaabbbabbaabbaaaabaabbbbbbbabaababbababbbababababbababbbaababbbabbabaababbabbaabababbaabaabaabbaaabbaaababbbbbbababaaababbbabbbababbbbabbaabbaaabbbaabaaaaaaabbaababaababaabbabbbbbaaaabbbabbababbbbbabbababaaaaabbabbababaaabaabaabaaaaabbabbabababbababbbaaaababbaaaaaaabbaaababaababbbbaaaabbbaabaababaaaabbababaaabbaaabaaabbaaaababbaabbbbbbabbbaabaaaaaabbbaaabbaabbabaaaabaaabbababaaababababbbbaabbbaabbbababbaababbbabbbabbabaabbababababbbbbaabaaabbbaababbbbabbbaabbabaaabbbbbabbbabbbbabaabbabbbbaabaaabbbaabaaaabaabbaabbaabababaaabbbbaaaaaabbbababbabaaabbbbababbabaaabbbaaaaaaabbababbbbabaaabababbaaaaabbbaababbaaababbababaabbbbbabaaababbbbbbbaaaaaaabbaaabbababbabbbaaaaababbaaaaabaabbaabaabaaabaaaaababbabbaaabbabbbbaabbabbabbabbbbabaabbabababbabbbbaababbabbbabbabbbabaaaaaababbbabaaaaabbbbbaaaababbbbababbbbbabbbbbbbbabbaaabbabaaaaabbaabbabaabbaabbaababbabaabaaababbbbbaaababababbaabbaabbabbababbabbaaaabababbbbabbbaaabababaaaabbbabbaabbbabaabbaaaabbaababababbbababbbbababaaaabaaabbbabbaabbbaabbabbbabbbaaabaaabaaaabaabababbbabbaaaabbaababababaabaabbaabbaababbaaabaababbbbabaaaaaabbbaabbaaabaabbbbaaabbaabaabbaababaabbbabbbabbbabaaababaababaababaaabbbbbabbbbbaabbbaaabaabbbabaabababbabababaaaabbbaabbbabaaabbabbbaabbbaabaaaaabbbabbabaababaaaababbbbaaaaaabaaaabbabbbbabbbaabaabbbabaabbbabababbbbbbbbaabaabbaaaaabbbbabbbaabbabaaabaaaabbabaabbbbbbaaaabbbababaabaabbbaaaaaaaabbbbabbbabbbaaabbbbabaabbaaabbababaabaabbaabaababbabbbabbbababbbbabbbbabbbaabbbabbaababbabbabbaabaabbaababbababababaaabbaabaabaaabababbabaabbaaababbbaaababbababaabbabaaabbbaaaabaababbbabaabbaaaabbbaababaabbbbaababbbbbaabbaaaaaabababaaabbababbaaabbaabababaabbbbbbabbbbbaabaabbaaabbaabbbbabbbaabbabaaaabbbababaabaabaaaabbabbbabbaabbbbbabaaabbabbabbbbbaabaaabbabababbaaabbbaabaabbbbbaaaaabbaaaabbbabbbaabaabbabbabbabbaabababbbbbbbbabaabaabbbabbbabbababbbaabbabaabbbaabaaaabbaabbaabbbabaabbaabaabbbabaaabbaaabbbaaaabbbbbaaaabbbabbbbaaababbbbbbbbababbaabbabbabbbbbabaaaabaabbaabbbbbabbabaaaaaabaababbabbabbaabbabbbabbbbaaabbbaabbbaabbbaaaababbbbbaaabaababbbbbabbbaabbaabbbbaaabbbbbbaabaaaabaaabaaabbabbaaabbaababaaabbaababbabbaaaabababbbabbaaabaaaaabbaabababbaabaaaabaabbbbaaaaabbaabbbaaabbabbbaaabbabbbabbbbbbababbbbaaabaaababbbabbaababbbbbabbabababbbbbabbaaaaaabbababbbababaaababbaabbabbbbaaabbbababaaaabababaaababbbababaabaaaabbabbbbabababababbbbbbaaabaaabaaabaababaaababbababbababbabbbabaabbabaaaaabaaabaaaabaaaabbabbaababbaababaabaabaabaabbaababbaaaabaaaaaaaaabbbaabaabaababbaaaabbababaababaaabababaabbbbbaaabaabbbbaaaaaaabbaabababaabbbabbaaabbaaabaaababaaababbabbbbabaabbaaaaaababbbaaababbaaabbabababbbaaaabaaaabaabbabbaaaaababaaabbaabbbbabaabbbbbbbaaabbaabbbaabbbbbbbbbbaaaabbabaabaaababbabbbaabababbaaaabaabbabbbaaaabaaaaaaaaabaaabaabbbbbababbbbababbbbaaabbbaabababbbbbbbaabbaaabbbaabbaabaabababaabbababbbabaaaabbabaabbbaabbababbaaabbabbaabaaaaaaabbbbabaaabbbababbabbaababaabababbaaaabbabababbbaabbbaabbaaabaaabbabbbbbbaababaaabbbaaabbabbbaabbbaabbabbaababbbabbaabbbaaaaabbabaaabaaabaaabaaaabbabaabbabbbbbabaababbabbbbbbaaabbababaaaabaabbbaababaabaaaabaabaabbaaababbbbbbaabbbbbbaaabbaaaaabaabbbbababbaaabaaabbabbaaabbababbabaaabbababbbaabaabbbabbbabbbbabbbbbbbbbabababbbababbbababbbabbbabbaabaaababaabbbbaaaabbaabaaabbbbbbbaaabababbbbaabababbababaaabaabbabbaabbabaaabbabaababbababaaabbbbbbaabbbaaaabaaabaababbbbabaaabbaaababbbbabaaaaabbbabaabaabbabaabbaaaabbaabbbbabbababbaaaababbbababbabbbbaaabbaaabaabababbbaaaaaaabbbbbaaababaaaabbbbaabaaaabaaababbbabbaaabaaabbabbabbbabaaaababaaaababaababaaaababbaaaaababbaababaaabbaabbbbaababbbaabaaababaaaabbbaabbbbbbaabbbbabbbabbaababbbbaabbbabaaaabaaabbbbbbaabaababababaaababbabbbbaababaaabaabbaabaaaaabbbbbbabbababaabaaabaabbaaabaaaaabbbaabbababbababaaababaaabbabbababaaaaababaabbbbbababaaaabaabbabababaabbaabaabababbbbaabbabaaaabbbbbbaaababbbbababbbbbabbaabbbbabbaababababbbbaaabbababbbbabaaabbbaaaababaababaaaabbababbabbbbbbaabaaaabaabbbbbbbbbababbbabbabbbbbabaabbababbaabbaabbbbbaabbbbbabaababaabaaaabbabbaabaabaabbababbababaaaabababaaaababababaabbbbaaaabbbabaababbabaaaaaaabbbbaaababbaaaabbbbabbbbaaaaaabaaaabbababaababbabbaabbbaaabbbbaaaaabababaabaaabbbaaabaaaaaaababababababbbaabbaabaaabbabbaabbbbaaaabbbababaabbbbbbbabbabaaabbaaaaaaabbababababbbbababbbababaaabbbabbbbababbbbbbbbabbbbababbabbbbaabaaaaababbabababaabbbaaabaabaabbaabaabbbbabbbbabaabababbabbaaaabaaaababbabababababaabbabbaabaabaaaabbaaabababbaababbabaaaaabbaaabaabaaabbbbbbabaababbbaabbaaabaababaaaaaaabbbaaabaaabbbbbabaaabbabbaabaabbaaababbbaabbbaabaaabaababaabbbbbabaaaaaaaaaaaabbaabbababaababbbbbaabbabaabbaabbaababaabbaaabbabbabbbbabbbabbabaaaababaabbabbabababbaaaaaaababbbabbbbabababbaabbbabababbbabbabbabbbabbbbbbababbaaabbaababaaaaabbbaaabababbabbaaabbabbaaaabaababaabbbaaabaaaabaabbabbaabaaaabbbbbabbabbbabaaabbbbbbababababaaaa")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1394Test.java b/src/test/java/com/fishercoder/_1394Test.java deleted file mode 100644 index 81d5b06a9c..0000000000 --- a/src/test/java/com/fishercoder/_1394Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1394; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1394Test { - - private static _1394.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1394.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.findLucky(new int[]{2, 2, 3, 4})); - } - - @Test - public void test2() { - assertEquals(3, solution1.findLucky(new int[]{1, 2, 2, 3, 3, 3})); - } - - @Test - public void test3() { - assertEquals(-1, solution1.findLucky(new int[]{2, 2, 2, 3, 3})); - } - - @Test - public void test4() { - assertEquals(-1, solution1.findLucky(new int[]{5})); - } - - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1395Test.java b/src/test/java/com/fishercoder/_1395Test.java deleted file mode 100644 index 42b0584420..0000000000 --- a/src/test/java/com/fishercoder/_1395Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1395; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1395Test { - private static _1395.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1395.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.numTeams(new int[]{2, 5, 3, 4, 1})); - } - - @Test - public void test2() { - assertEquals(0, solution1.numTeams(new int[]{2, 1, 3})); - } - - @Test - public void test3() { - assertEquals(4, solution1.numTeams(new int[]{1, 2, 3, 4})); - } - -} diff --git a/src/test/java/com/fishercoder/_1399Test.java b/src/test/java/com/fishercoder/_1399Test.java deleted file mode 100644 index 2891de68da..0000000000 --- a/src/test/java/com/fishercoder/_1399Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1399; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1399Test { - private static _1399.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1399.Solution1(); - } - - @Test - public void test1() { - assertEquals(4, solution1.countLargestGroup(13)); - } - - @Test - public void test2() { - assertEquals(2, solution1.countLargestGroup(2)); - } - - @Test - public void test3() { - assertEquals(6, solution1.countLargestGroup(15)); - } - - @Test - public void test4() { - assertEquals(5, solution1.countLargestGroup(24)); - } - -} diff --git a/src/test/java/com/fishercoder/_139Test.java b/src/test/java/com/fishercoder/_139Test.java deleted file mode 100644 index 3cd07eb4ac..0000000000 --- a/src/test/java/com/fishercoder/_139Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._139; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _139Test { - private static _139.Solution1 solution1; - private static _139.Solution2 solution2; - private static _139.Solution3 solution3; - private static String s; - private static List wordDict; - - @BeforeClass - public static void setup() { - solution1 = new _139.Solution1(); - solution2 = new _139.Solution2(); - solution3 = new _139.Solution3(); - } - - @Test - public void test1() { - s = "leetcode"; - wordDict = new ArrayList<>(Arrays.asList("leet", "code")); - assertEquals(true, solution1.wordBreak(s, wordDict)); - assertEquals(true, solution2.wordBreak(s, wordDict)); - assertEquals(true, solution3.wordBreak(s, wordDict)); - } -} diff --git a/src/test/java/com/fishercoder/_13Test.java b/src/test/java/com/fishercoder/_13Test.java deleted file mode 100644 index 4c44db79da..0000000000 --- a/src/test/java/com/fishercoder/_13Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._13; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _13Test { - private static _13.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _13.Solution1(); - } - - @Test - public void test1() { - assertEquals(12, solution1.romanToInt("XII")); - } - - @Test - public void test2() { - assertEquals(1000, solution1.romanToInt("M")); - } - - @Test - public void test3() { - assertEquals(3999, solution1.romanToInt("MMMCMXCIX")); - } -} diff --git a/src/test/java/com/fishercoder/_1400Test.java b/src/test/java/com/fishercoder/_1400Test.java deleted file mode 100644 index da60bc486b..0000000000 --- a/src/test/java/com/fishercoder/_1400Test.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1400; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1400Test { - private static _1400.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1400.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.canConstruct("annabelle", 2)); - } - - @Test - public void test2() { - assertEquals(false, solution1.canConstruct("leetcode", 3)); - } - - @Test - public void test3() { - assertEquals(true, solution1.canConstruct("true", 4)); - } - - @Test - public void test4() { - assertEquals(true, solution1.canConstruct("yzyzyzyzyzyzyzy", 2)); - } - - @Test - public void test5() { - assertEquals(false, solution1.canConstruct("cr", 7)); - } - - @Test - public void test6() { - assertEquals(true, solution1.canConstruct("qlkzenwmmnpkopu", 15)); - } - - @Test - public void test7() { - assertEquals(true, solution1.canConstruct("jsautfnlcmwqpzycehdulmdencthhlzsnijd", 35)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1401Test.java b/src/test/java/com/fishercoder/_1401Test.java deleted file mode 100644 index 59fdfb44ed..0000000000 --- a/src/test/java/com/fishercoder/_1401Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1401; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1401Test { - private static _1401.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1401.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.checkOverlap(1, 0, 0, 1, -1, 3, 1)); - } - - @Test - public void test2() { - assertEquals(true, solution1.checkOverlap(1, 0, 0, -1, 0, 0, 1)); - } - - @Test - public void test3() { - assertEquals(true, solution1.checkOverlap(1, 1, 1, -3, -3, 3, 3)); - } - - @Test - public void test4() { - assertEquals(true, solution1.checkOverlap(1, 1, 1, 1, -3, 2, 1)); - } - - @Test - public void test5() { - assertEquals(false, solution1.checkOverlap(1, 1, 1, 1, -3, 2, -1)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1403Test.java b/src/test/java/com/fishercoder/_1403Test.java deleted file mode 100644 index b6099d3999..0000000000 --- a/src/test/java/com/fishercoder/_1403Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1403; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _1403Test { - private static _1403.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1403.Solution1(); - } - - @Test - public void test1() { - assertEquals(Arrays.asList(10, 9), solution1.minSubsequence(new int[]{4, 3, 10, 9, 8})); - } - - @Test - public void test2() { - assertEquals(Arrays.asList(7, 7, 6), solution1.minSubsequence(new int[]{4, 4, 7, 6, 7})); - } - - @Test - public void test3() { - assertEquals(Arrays.asList(6), solution1.minSubsequence(new int[]{6})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1408Test.java b/src/test/java/com/fishercoder/_1408Test.java deleted file mode 100644 index 2c93745c23..0000000000 --- a/src/test/java/com/fishercoder/_1408Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1408; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _1408Test { - private static _1408.Solution1 solution1; - private static String[] words; - private static List expected; - private static List actual; - - @BeforeClass - public static void setup() { - solution1 = new _1408.Solution1(); - } - - @Test - public void test1() { - words = new String[]{"mass", "as", "hero", "superhero"}; - expected = Arrays.asList("as", "hero"); - actual = solution1.stringMatching(words); - assertEquals(expected.containsAll(actual), actual.containsAll(expected)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1409Test.java b/src/test/java/com/fishercoder/_1409Test.java deleted file mode 100644 index cb10660f94..0000000000 --- a/src/test/java/com/fishercoder/_1409Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1409; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1409Test { - private static _1409.Solution1 solution1; - private static int[] queries; - - @BeforeClass - public static void setup() { - solution1 = new _1409.Solution1(); - } - - @Test - public void test1() { - queries = new int[]{3, 1, 2, 1}; - assertArrayEquals(new int[]{2, 1, 2, 1}, solution1.processQueries(queries, 5)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1410Test.java b/src/test/java/com/fishercoder/_1410Test.java deleted file mode 100644 index 912448998d..0000000000 --- a/src/test/java/com/fishercoder/_1410Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1410; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1410Test { - private static _1410.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1410.Solution1(); - } - - @Test - public void test1() { - assertEquals("& is an HTML entity but &ambassador; is not.", solution1.entityParser("& is an HTML entity but &ambassador; is not.")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1413Test.java b/src/test/java/com/fishercoder/_1413Test.java deleted file mode 100644 index 1695dd13cd..0000000000 --- a/src/test/java/com/fishercoder/_1413Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1413; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1413Test { - private static _1413.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _1413.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{-3, 2, -3, 4, 2}; - assertEquals(5, solution1.minStartValue(nums)); - } - - @Test - public void test2() { - nums = new int[]{1, 2}; - assertEquals(1, solution1.minStartValue(nums)); - } - - @Test - public void test3() { - nums = new int[]{1, -2, -3}; - assertEquals(5, solution1.minStartValue(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1415Test.java b/src/test/java/com/fishercoder/_1415Test.java deleted file mode 100644 index 85a644801d..0000000000 --- a/src/test/java/com/fishercoder/_1415Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1415; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1415Test { - - private static _1415.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1415.Solution1(); - } - - @Test - public void test1() { - assertEquals("cab", solution1.getHappyString(3, 9)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1417Test.java b/src/test/java/com/fishercoder/_1417Test.java deleted file mode 100644 index 1343491b76..0000000000 --- a/src/test/java/com/fishercoder/_1417Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1417; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1417Test { - private static _1417.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1417.Solution1(); - } - - @Test - public void test1() { - assertEquals("0a1b2c", solution1.reformat("a0b1c2")); - } - - @Test - public void test2() { - assertEquals("c2o0v1i9d", solution1.reformat("covid2019")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1418Test.java b/src/test/java/com/fishercoder/_1418Test.java deleted file mode 100644 index 999966eff3..0000000000 --- a/src/test/java/com/fishercoder/_1418Test.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1418; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _1418Test { - private static _1418.Solution1 solution1; - private static List> orders; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _1418.Solution1(); - } - - @Test - public void test1() { - orders = Arrays.asList(Arrays.asList("Laura", "2", "Bean Burrito"), Arrays.asList("Jhon", "2", "Beef Burrito"), Arrays.asList("Melissa", "2", "Soda")); - expected = Arrays.asList(Arrays.asList("Table", "Bean Burrito", "Beef Burrito", "Soda"), Arrays.asList("2", "1", "1", "1")); - assertEquals(expected, solution1.displayTable(orders)); - } - - @Test - public void test2() { - orders = Arrays.asList(Arrays.asList("James", "12", "Fried Chicken"), Arrays.asList("Ratesh", "12", "Fried Chicken"), Arrays.asList("Amadeus", "12", "Fried Chicken"), Arrays.asList("Adam", "1", "Canadian Waffles"), Arrays.asList("Brianna", "1", "Canadian Waffles")); - expected = Arrays.asList(Arrays.asList("Table", "Canadian Waffles", "Fried Chicken"), Arrays.asList("1", "2", "0"), Arrays.asList("12", "0", "3")); - assertEquals(expected, solution1.displayTable(orders)); - } - - @Test - public void test3() { - orders = Arrays.asList(Arrays.asList("David", "3", "Ceviche"), Arrays.asList("Corina", "10", "Beef Burrito"), Arrays.asList("David", "3", "Fried Chicken"), Arrays.asList("Carla", "5", "Water"), Arrays.asList("Carla", "5", "Ceviche"), Arrays.asList("Rous", "3", "Ceviche")); - expected = Arrays.asList(Arrays.asList("Table", "Beef Burrito", "Ceviche", "Fried Chicken", "Water"), Arrays.asList("3", "0", "2", "1", "0"), Arrays.asList("5", "0", "1", "0", "1"), Arrays.asList("10", "1", "0", "0", "0")); - assertEquals(expected, solution1.displayTable(orders)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_141Test.java b/src/test/java/com/fishercoder/_141Test.java deleted file mode 100644 index 277d92b393..0000000000 --- a/src/test/java/com/fishercoder/_141Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._141; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _141Test { - private static _141.Solution1 solution1; - private static _141.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _141.Solution1(); - solution2 = new _141.Solution2(); - } - - @Test - public void test1() { - assertEquals(false, solution1.hasCycle(LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4}))); - } - - @Test - public void test2() { - assertEquals(false, solution2.hasCycle(LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4}))); - } -} diff --git a/src/test/java/com/fishercoder/_1422Test.java b/src/test/java/com/fishercoder/_1422Test.java deleted file mode 100644 index 19ab19decf..0000000000 --- a/src/test/java/com/fishercoder/_1422Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1422; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1422Test { - private static _1422.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1422.Solution1(); - } - - @Test - public void test1() { - assertEquals(5, solution1.maxScore("011101")); - } - - @Test - public void test2() { - assertEquals(5, solution1.maxScore("00111")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1423Test.java b/src/test/java/com/fishercoder/_1423Test.java deleted file mode 100644 index 23b756b584..0000000000 --- a/src/test/java/com/fishercoder/_1423Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1423; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1423Test { - private static _1423.Solution1 solution1; - private static int[] cardPoints; - - @BeforeClass - public static void setup() { - solution1 = new _1423.Solution1(); - } - - @Test - public void test1() { - cardPoints = new int[]{1, 2, 3, 4, 5, 6, 1}; - assertEquals(12, solution1.maxScore(cardPoints, 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1426Test.java b/src/test/java/com/fishercoder/_1426Test.java deleted file mode 100644 index 3515fdb9e7..0000000000 --- a/src/test/java/com/fishercoder/_1426Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1426; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1426Test { - private static _1426.Solution1 solution1; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _1426.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{1, 1, 3, 3, 5, 5, 7, 7}; - assertEquals(0, solution1.countElements(arr)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1427Test.java b/src/test/java/com/fishercoder/_1427Test.java deleted file mode 100644 index 170514b2c7..0000000000 --- a/src/test/java/com/fishercoder/_1427Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1427; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1427Test { - private static _1427.Solution1 solution1; - private static int[][] shift; - - @BeforeClass - public static void setup() { - solution1 = new _1427.Solution1(); - } - - @Test - public void test1() { - shift = new int[][]{ - {0, 1}, - {1, 2}, - }; - assertEquals("cab", solution1.stringShift("abc", shift)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1428Test.java b/src/test/java/com/fishercoder/_1428Test.java deleted file mode 100644 index f6fb6d1e58..0000000000 --- a/src/test/java/com/fishercoder/_1428Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.BinaryMatrix; -import com.fishercoder.common.classes.BinaryMatrixImpl; -import com.fishercoder.solutions._1428; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1428Test { - private static _1428.Solution1 solution1; - private static BinaryMatrix binaryMatrix; - - @BeforeClass - public static void setup() { - solution1 = new _1428.Solution1(); - } - - @Test - public void test1() { - binaryMatrix = new BinaryMatrixImpl(new int[][]{ - {0, 0}, - {1, 1} - }); - assertEquals(0, solution1.leftMostColumnWithOne(binaryMatrix)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1431Test.java b/src/test/java/com/fishercoder/_1431Test.java deleted file mode 100644 index c9dd6c51cf..0000000000 --- a/src/test/java/com/fishercoder/_1431Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1431; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.TestCase.assertEquals; - -public class _1431Test { - private static _1431.Solution1 solution1; - private static int[] candies; - - @BeforeClass - public static void setup() { - solution1 = new _1431.Solution1(); - } - - @Test - public void test1() { - candies = new int[]{2, 3, 5, 1, 3}; - assertEquals(Arrays.asList(true, true, true, false, true), solution1.kidsWithCandies(candies, 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1432Test.java b/src/test/java/com/fishercoder/_1432Test.java deleted file mode 100644 index 1205f4379a..0000000000 --- a/src/test/java/com/fishercoder/_1432Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1432; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _1432Test { - private static _1432.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _1432.Solution1(); - } - - @Test - public void test1() { - assertEquals(888, solution1.maxDiff(555)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1436Test.java b/src/test/java/com/fishercoder/_1436Test.java deleted file mode 100644 index 6c92e9dafd..0000000000 --- a/src/test/java/com/fishercoder/_1436Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1436; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static junit.framework.TestCase.assertEquals; - -public class _1436Test { - private static _1436.Solution1 solution1; - private static List> paths; - - @BeforeClass - public static void setup() { - solution1 = new _1436.Solution1(); - } - - @Test - public void test1() { - paths = Arrays.asList(Arrays.asList("Lima", "Sao Paulo"), Arrays.asList("New York", "Lima"), Arrays.asList("London", "New York")); - assertEquals("Sao Paulo", solution1.destCity(paths)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1437Test.java b/src/test/java/com/fishercoder/_1437Test.java deleted file mode 100644 index fe20e99d72..0000000000 --- a/src/test/java/com/fishercoder/_1437Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1437; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _1437Test { - private static _1437.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _1437.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 0, 0, 0, 1, 0, 0, 1}; - assertEquals(true, solution1.kLengthApart(nums, 2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1441Test.java b/src/test/java/com/fishercoder/_1441Test.java deleted file mode 100644 index ccd4845234..0000000000 --- a/src/test/java/com/fishercoder/_1441Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1441; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.TestCase.assertEquals; - -public class _1441Test { - private static _1441.Solution1 solution1; - private static int[] target; - - @BeforeClass - public static void setup() { - solution1 = new _1441.Solution1(); - } - - @Test - public void test1() { - target = new int[]{1, 3}; - assertEquals(Arrays.asList("Push", "Push", "Pop", "Push"), solution1.buildArray(target, 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_144Test.java b/src/test/java/com/fishercoder/_144Test.java deleted file mode 100644 index 182e90d8cd..0000000000 --- a/src/test/java/com/fishercoder/_144Test.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._144; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _144Test { - private static _144.Solution1 solution1; - private static _144.Solution2 solution2; - private static _144.Solution3 solution3; - private static TreeNode root; - private static List inorder; - - @BeforeClass - public static void setup() { - solution1 = new _144.Solution1(); - solution2 = new _144.Solution2(); - solution3 = new _144.Solution3(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4)); - inorder = solution1.preorderTraversal(root); - assertEquals(Arrays.asList(3, 1, 5, 2, 4), inorder); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9)); - TreeUtils.printBinaryTree(root); - inorder = solution1.preorderTraversal(root); - assertEquals(Arrays.asList(1, 2, 4, 7, 8, 9, 3, 5, 6), inorder); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4)); - inorder = solution2.preorderTraversal(root); - assertEquals(Arrays.asList(3, 1, 5, 2, 4), inorder); - } - - @Test - public void test4() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9)); - TreeUtils.printBinaryTree(root); - inorder = solution2.preorderTraversal(root); - assertEquals(Arrays.asList(1, 2, 4, 7, 8, 9, 3, 5, 6), inorder); - } - - @Test - public void test5() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, null, null, 6)); - TreeUtils.printBinaryTree(root); - inorder = solution1.preorderTraversal(root); - assertEquals(Arrays.asList(1, 2, 4, 6, 3, 5), inorder); - } - - @Test - public void test6() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, null, null, 6)); - TreeUtils.printBinaryTree(root); - inorder = solution3.preorderTraversal(root); - assertEquals(Arrays.asList(1, 2, 4, 6, 3, 5), inorder); - } - -} diff --git a/src/test/java/com/fishercoder/_145Test.java b/src/test/java/com/fishercoder/_145Test.java deleted file mode 100644 index 8efee31ffe..0000000000 --- a/src/test/java/com/fishercoder/_145Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._145; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _145Test { - private static _145.Solution1 solution1; - private static _145.Solution2 solution2; - private static _145.Solution3 solution3; - private static TreeNode root; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _145.Solution1(); - solution2 = new _145.Solution2(); - solution3 = new _145.Solution3(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9)); - TreeUtils.printBinaryTree(root); - CommonUtils.printList(solution1.postorderTraversal(root)); - CommonUtils.printList(solution2.postorderTraversal(root)); - CommonUtils.printList(solution3.postorderTraversal(root)); - expected = new ArrayList<>(Arrays.asList(8, 9, 7, 4, 2, 5, 6, 3, 1)); - assertEquals(expected, solution1.postorderTraversal(root)); - assertEquals(expected, solution2.postorderTraversal(root)); - assertEquals(expected, solution3.postorderTraversal(root)); - } -} diff --git a/src/test/java/com/fishercoder/_149Test.java b/src/test/java/com/fishercoder/_149Test.java deleted file mode 100644 index fd631db963..0000000000 --- a/src/test/java/com/fishercoder/_149Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._149; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _149Test { - private static _149.Solution1 solution1; - private static int[][] points; - - @BeforeClass - public static void setup() { - solution1 = new _149.Solution1(); - } - - @Test - public void test1() { - points = new int[][]{ - {1, 1}, - {2, 2}, - {3, 3} - }; - assertEquals(3, solution1.maxPoints(points)); - } -} diff --git a/src/test/java/com/fishercoder/_14Test.java b/src/test/java/com/fishercoder/_14Test.java deleted file mode 100644 index d648afd9aa..0000000000 --- a/src/test/java/com/fishercoder/_14Test.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._14; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _14Test { - private static _14.Solution1 solution1; - private static _14.Solution2 solution2; - private static String[] strs; - - @BeforeClass - public static void setup() { - solution1 = new _14.Solution1(); - solution2 = new _14.Solution2(); - } - - @Test - public void test1() { - strs = new String[]{"a", "b"}; - assertEquals("", solution1.longestCommonPrefix(strs)); - assertEquals("", solution2.longestCommonPrefix(strs)); - } - - @Test - public void test2() { - strs = new String[]{"leetcode", "lead"}; - assertEquals("le", solution1.longestCommonPrefix(strs)); - assertEquals("le", solution2.longestCommonPrefix(strs)); - } - - @Test - public void test3() { - strs = new String[]{"leetcode", "code"}; - assertEquals("", solution1.longestCommonPrefix(strs)); - assertEquals("", solution2.longestCommonPrefix(strs)); - } - - @Test - public void test4() { - strs = new String[]{"flower", "flow", "flight"}; - assertEquals("fl", solution1.longestCommonPrefix(strs)); - assertEquals("fl", solution2.longestCommonPrefix(strs)); - } - - @Test - public void test5() { - strs = new String[]{}; - assertEquals("", solution1.longestCommonPrefix(strs)); - assertEquals("", solution2.longestCommonPrefix(strs)); - } - - @Test - public void test6() { - strs = new String[]{"a"}; - assertEquals("a", solution1.longestCommonPrefix(strs)); - assertEquals("a", solution2.longestCommonPrefix(strs)); - } - - @Test - public void test7() { - strs = new String[]{"c", "c"}; - assertEquals("c", solution1.longestCommonPrefix(strs)); - assertEquals("c", solution2.longestCommonPrefix(strs)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_151Test.java b/src/test/java/com/fishercoder/_151Test.java deleted file mode 100644 index 3f653f6436..0000000000 --- a/src/test/java/com/fishercoder/_151Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._151; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _151Test { - private static _151.Solution1 solution1; - private static String s; - - @BeforeClass - public static void setup() { - solution1 = new _151.Solution1(); - } - - @Test - public void test1() { - s = " "; - assertEquals("", solution1.reverseWords(s)); - } - - @Test - public void test2() { - s = " 1"; - assertEquals("1", solution1.reverseWords(s)); - } - - @Test - public void test3() { - s = " a b "; - assertEquals("b a", solution1.reverseWords(s)); - } - - @Test - public void test4() { - s = "a b c"; - assertEquals("c b a", solution1.reverseWords(s)); - } -} diff --git a/src/test/java/com/fishercoder/_153Test.java b/src/test/java/com/fishercoder/_153Test.java deleted file mode 100644 index 752b319348..0000000000 --- a/src/test/java/com/fishercoder/_153Test.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._153; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _153Test { - private static _153.Solution1 solution1; - private static int expected; - private static int actual; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _153.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {4, 5, 6, 7, 0, 1, 2}; - expected = 0; - actual = solution1.findMin(nums); - assertEquals(expected, actual); - } - - @Test - public void test2() { - nums = new int[] {1}; - expected = 1; - actual = solution1.findMin(nums); - assertEquals(expected, actual); - } - - @Test - public void test3() { - nums = new int[] {2, 1}; - expected = 1; - actual = solution1.findMin(nums); - assertEquals(expected, actual); - } - - @Test - public void test4() { - nums = new int[] {2, 3, 4, 5, 1}; - expected = 1; - actual = solution1.findMin(nums); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_154Test.java b/src/test/java/com/fishercoder/_154Test.java deleted file mode 100644 index c26c41687d..0000000000 --- a/src/test/java/com/fishercoder/_154Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._154; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _154Test { - private static _154.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _154.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {1, 1, 1}; - assertEquals(1, solution1.findMin(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_156Test.java b/src/test/java/com/fishercoder/_156Test.java deleted file mode 100644 index 64284a2c54..0000000000 --- a/src/test/java/com/fishercoder/_156Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._156; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _156Test { - private static _156.Solution1 solution1; - private static TreeNode root; - private static TreeNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _156.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); - expected = TreeUtils.constructBinaryTree(Arrays.asList(4, 5, 2, null, null, 3, 1)); - assertEquals(expected, solution1.upsideDownBinaryTree(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_15Test.java b/src/test/java/com/fishercoder/_15Test.java deleted file mode 100644 index 8303928244..0000000000 --- a/src/test/java/com/fishercoder/_15Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._15; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _15Test { - private static _15.Solution1 solution1; - private static int[] nums; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _15.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{-1, 0, 1, 2, -1, -4}; - expected = new ArrayList<>(); - expected.add(Arrays.asList(-1, -1, 2)); - expected.add(Arrays.asList(-1, 0, 1)); - assertEquals(expected, solution1.threeSum(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_160Test.java b/src/test/java/com/fishercoder/_160Test.java deleted file mode 100644 index ddf0685ce3..0000000000 --- a/src/test/java/com/fishercoder/_160Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.solutions._160; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _160Test { - private static _160.Solution1 solution1; - private static _160.Solution2 solution2; - private static _160.Solution3 solution3; - private static ListNode headA; - private static ListNode headB; - private static ListNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _160.Solution1(); - solution2 = new _160.Solution2(); - solution3 = new _160.Solution3(); - } - - @Test - @Ignore - public void test1() { - headA = new ListNode(3); - headB = new ListNode(2); - headB.next = new ListNode(3); - expected = new ListNode(3); - /**TODO: both solution1 and solution2 are ACCEPTED on OJ, but somehow it's not passing in this unit test.*/ - assertEquals(expected, solution1.getIntersectionNode(headA, headB)); - } - - @Test - @Ignore - public void test2() { - headA = new ListNode(3); - headB = new ListNode(2); - headB.next = new ListNode(3); - expected = new ListNode(3); - /**TODO: both solution1 and solution2 are ACCEPTED on OJ, but somehow it's not passing in this unit test.*/ - assertEquals(expected, solution2.getIntersectionNode(headA, headB)); - } - - @Test - public void test3() { - headA = new ListNode(3); - headB = new ListNode(2); - headB.next = new ListNode(3); - expected = new ListNode(3); - assertEquals(expected, solution3.getIntersectionNode(headA, headB)); - } -} diff --git a/src/test/java/com/fishercoder/_161Test.java b/src/test/java/com/fishercoder/_161Test.java deleted file mode 100644 index 8d018b7a8c..0000000000 --- a/src/test/java/com/fishercoder/_161Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._161; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _161Test { - private static _161.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _161.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isOneEditDistance("a", "ac")); - } -} diff --git a/src/test/java/com/fishercoder/_162Test.java b/src/test/java/com/fishercoder/_162Test.java deleted file mode 100644 index a6f223c059..0000000000 --- a/src/test/java/com/fishercoder/_162Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._162; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _162Test { - private static _162.Solution1 solution1; - private static _162.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _162.Solution1(); - solution2 = new _162.Solution2(); - } - - @Test - public void test1() { - nums = new int[] {1, 2}; - assertEquals(1, solution1.findPeakElement(nums)); - assertEquals(1, solution2.findPeakElement(nums)); - } - - @Test - public void test2() { - nums = new int[] {1}; - assertEquals(0, solution1.findPeakElement(nums)); - assertEquals(0, solution2.findPeakElement(nums)); - } - - @Test - public void test3() { - nums = new int[] {1, 2, 3, 1}; - assertEquals(2, solution1.findPeakElement(nums)); - assertEquals(2, solution2.findPeakElement(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_163Test.java b/src/test/java/com/fishercoder/_163Test.java deleted file mode 100644 index 6c60ad73e8..0000000000 --- a/src/test/java/com/fishercoder/_163Test.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._163; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _163Test { - - private static _163.Solution1 solution1; - private static List expected; - private static List actual; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _163.Solution1(); - expected = new ArrayList(); - actual = new ArrayList(); - } - - @Before - public void setupForEachTest() { - expected.clear(); - actual.clear(); - } - - @Test - public void test1() { - //solution1 case 1: should return ["0->2147483646"] - nums = new int[] {2147483647}; - expected.add("0->2147483646"); - actual = solution1.findMissingRanges(nums, 0, 2147483647); - assertEquals(expected, actual); - } - - @Test - public void test2() { - //solution1 case 2: should return ["-2147483647->-1","1->2147483646"] - nums = new int[] {-2147483648, -2147483648, 0, 2147483647, 2147483647}; - expected.add("-2147483647->-1"); - expected.add("1->2147483646"); - actual = solution1.findMissingRanges(nums, -2147483648, 2147483647); - assertEquals(expected, actual); - } - - @Test - public void test3() { - //solution1 case 3: should return ["-2147483648->2147483647"] - nums = new int[] {}; - expected.add("-2147483648->2147483647"); - actual = solution1.findMissingRanges(nums, -2147483648, 2147483647); - assertEquals(expected, actual); - } - - @Test - public void test4() { - //solution1 case 4: should return ["-2147483648->2147483646"] - nums = new int[] {2147483647}; - expected.add("-2147483648->2147483646"); - actual = solution1.findMissingRanges(nums, -2147483648, 2147483647); - assertEquals(expected, actual); - } - - @Test - public void test5() { - //solution1 case 5: should return ["0->2147483647"] - nums = new int[] {}; - expected.add("0->2147483647"); - actual = solution1.findMissingRanges(nums, 0, 2147483647); - assertEquals(expected, actual); - } - - @Test - public void test6() { - //solution1 case 6: should return ["-2147483647->2147483647"] - nums = new int[] {-2147483648}; - expected.add("-2147483647->2147483647"); - actual = solution1.findMissingRanges(nums, -2147483648, 2147483647); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_164Test.java b/src/test/java/com/fishercoder/_164Test.java deleted file mode 100644 index 84d4e37fd5..0000000000 --- a/src/test/java/com/fishercoder/_164Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._164; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _164Test { - private static _164.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _164.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {}; - assertEquals(0, solution1.maximumGap(nums)); - } - - @Test - public void test2() { - nums = new int[] {1, 3, 6, 5}; - assertEquals(2, solution1.maximumGap(nums)); - } - - @Test - public void test3() { - nums = new int[] {1, 100000}; - assertEquals(99999, solution1.maximumGap(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_165Test.java b/src/test/java/com/fishercoder/_165Test.java deleted file mode 100644 index 550f8b9c4e..0000000000 --- a/src/test/java/com/fishercoder/_165Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._165; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _165Test { - private static _165.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _165.Solution1(); - } - - @Test - public void test1() { - assertEquals(-1, solution1.compareVersion("1.1", "1.2")); - } - - @Test - public void test2() { - assertEquals(1, solution1.compareVersion("1.0.1", "1")); - } - - @Test - public void test3() { - assertEquals(-0, solution1.compareVersion("1.0", "1")); - } -} diff --git a/src/test/java/com/fishercoder/_166Test.java b/src/test/java/com/fishercoder/_166Test.java deleted file mode 100644 index 10e1fe3a2f..0000000000 --- a/src/test/java/com/fishercoder/_166Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._166; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _166Test { - private static _166.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _166.Solution1(); - } - - @Test - public void test1() { - assertEquals("0.5", solution1.fractionToDecimal(1, 2)); - } - - @Test - public void test2() { - assertEquals("2", solution1.fractionToDecimal(2, 1)); - } - - @Test - public void test3() { - assertEquals("0.(6)", solution1.fractionToDecimal(2, 3)); - } - - @Test - public void test4() { - assertEquals("-6.25", solution1.fractionToDecimal(-50, 8)); - } - - @Test - public void test5() { - assertEquals("-0.58(3)", solution1.fractionToDecimal(7, -12)); - } - - @Test - public void test6() { - assertEquals("0.0000000004656612873077392578125", solution1.fractionToDecimal(-1, -2147483648)); - } - - @Test - public void test7() { - assertEquals("0", solution1.fractionToDecimal(0, -5)); - } - - @Test - public void test8() { - assertEquals("-2147483648", solution1.fractionToDecimal(-2147483648, 1)); - } -} diff --git a/src/test/java/com/fishercoder/_167Test.java b/src/test/java/com/fishercoder/_167Test.java deleted file mode 100644 index d9cfcce322..0000000000 --- a/src/test/java/com/fishercoder/_167Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._167; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _167Test { - private static _167.Solution1 solution1; - private static int[] numbers; - private static int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _167.Solution1(); - } - - @Test - public void test1() { - numbers = new int[]{-3, 3, 4, 90}; - expected = new int[]{1, 2}; - assertArrayEquals(expected, solution1.twoSum(numbers, 0)); - } -} diff --git a/src/test/java/com/fishercoder/_168Test.java b/src/test/java/com/fishercoder/_168Test.java deleted file mode 100644 index 8c0c52920b..0000000000 --- a/src/test/java/com/fishercoder/_168Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._168; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _168Test { - private static _168.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _168.Solution1(); - } - - @Test - public void test1() { - assertEquals("APSM", solution1.convertToTitle(28899)); - } -} diff --git a/src/test/java/com/fishercoder/_16Test.java b/src/test/java/com/fishercoder/_16Test.java deleted file mode 100644 index 115976406e..0000000000 --- a/src/test/java/com/fishercoder/_16Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._16; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _16Test { - private static _16.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _16.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{-1, 2, 1, -4}; - assertEquals(2, solution1.threeSumClosest(nums, 1)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_171Test.java b/src/test/java/com/fishercoder/_171Test.java deleted file mode 100644 index 4329f0263f..0000000000 --- a/src/test/java/com/fishercoder/_171Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._171; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _171Test { - private static _171.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _171.Solution1(); - } - - @Test - public void test1() { - assertEquals(28, solution1.titleToNumber("AB")); - } -} diff --git a/src/test/java/com/fishercoder/_174Test.java b/src/test/java/com/fishercoder/_174Test.java deleted file mode 100644 index a7c9a2b47d..0000000000 --- a/src/test/java/com/fishercoder/_174Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._174; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _174Test { - private static _174.Solution1 solution1; - private int[][] dungeon; - - @BeforeClass - public static void setup() { - solution1 = new _174.Solution1(); - } - - @Test - public void test1() { - dungeon = new int[][] { - {0} - }; - assertEquals(1, solution1.calculateMinimumHP(dungeon)); - } - - @Test - public void test2() { - dungeon = new int[][] { - {-200} - }; - assertEquals(201, solution1.calculateMinimumHP(dungeon)); - } -} diff --git a/src/test/java/com/fishercoder/_17Test.java b/src/test/java/com/fishercoder/_17Test.java deleted file mode 100644 index bea0e12b21..0000000000 --- a/src/test/java/com/fishercoder/_17Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._17; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class _17Test { - private static _17.Solution1 solution1; - private static String digits; - private static List expected; - private static List actual; - - @BeforeClass - public static void setup() { - solution1 = new _17.Solution1(); - } - - @Test - public void test1() { - digits = "2"; - actual = solution1.letterCombinations(digits); - expected = new ArrayList<>(Arrays.asList("a", "b", "c")); - assertEquals(expected, actual); - } - - @Test - public void test2() { - digits = "23"; - actual = solution1.letterCombinations(digits); - expected = new ArrayList<>(Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")); - /**order doesn't matter, so we check like below*/ - assertTrue(expected.containsAll(actual) && actual.containsAll(expected)); - } -} diff --git a/src/test/java/com/fishercoder/_186Test.java b/src/test/java/com/fishercoder/_186Test.java deleted file mode 100644 index 187c02a827..0000000000 --- a/src/test/java/com/fishercoder/_186Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._186; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _186Test { - private static _186.Solution1 solution1; - private static char[] s; - private static char[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _186.Solution1(); - } - - @Test - public void test1() { - s = new char[] {'h', 'i', '!'}; - solution1.reverseWords(s); - expected = new char[] {'h', 'i', '!'}; - assertArrayEquals(expected, s); - } -} diff --git a/src/test/java/com/fishercoder/_187Test.java b/src/test/java/com/fishercoder/_187Test.java deleted file mode 100644 index d767acb064..0000000000 --- a/src/test/java/com/fishercoder/_187Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._187; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _187Test { - private static _187.Solution1 solution1; - private static String s; - private static List expected; - private static List actual; - - @BeforeClass - public static void setup() { - solution1 = new _187.Solution1(); - } - - @Test - public void test1() { - s = "AAAAAAAAAAA"; - System.out.println(s.length()); - actual = solution1.findRepeatedDnaSequences(s); - expected = new ArrayList<>(Arrays.asList("AAAAAAAAAA")); - System.out.println(expected.get(0).length()); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_189Test.java b/src/test/java/com/fishercoder/_189Test.java deleted file mode 100644 index 9e22d417b7..0000000000 --- a/src/test/java/com/fishercoder/_189Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._189; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _189Test { - private static _189.Solution1 solution1; - private static _189.Solution2 solution2; - private static _189.Solution3 solution3; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _189.Solution1(); - solution2 = new _189.Solution2(); - solution3 = new _189.Solution3(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3}; - solution1.rotate(nums, 1); - CommonUtils.printArray(nums); - } - - @Test - public void test2() { - nums = new int[]{1, 2, 3}; - solution2.rotate(nums, 1); - CommonUtils.printArray(nums); - } - - @Test - public void test3() { - nums = new int[]{1, 2, 3}; - solution3.rotate(nums, 1); - CommonUtils.printArray(nums); - } - -} diff --git a/src/test/java/com/fishercoder/_18Test.java b/src/test/java/com/fishercoder/_18Test.java deleted file mode 100644 index 8bf22fce2c..0000000000 --- a/src/test/java/com/fishercoder/_18Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._18; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _18Test { - private static _18.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _18.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 0, -1, 0, -2, 2}; - CommonUtils.printListList(solution1.fourSum(nums, 0)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_190Test.java b/src/test/java/com/fishercoder/_190Test.java deleted file mode 100644 index c610b55abe..0000000000 --- a/src/test/java/com/fishercoder/_190Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._190; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _190Test { - private static _190.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _190.Solution1(); - } - - @Test - public void test1() { - assertEquals(536870912, solution1.reverseBits(4)); - } - - - @Test - public void test2() { - assertEquals(964176192, solution1.reverseBits( 43261596)); - } -} diff --git a/src/test/java/com/fishercoder/_191Test.java b/src/test/java/com/fishercoder/_191Test.java deleted file mode 100644 index b658bd8d5c..0000000000 --- a/src/test/java/com/fishercoder/_191Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._191; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _191Test { - private static _191.Solution1 solution1; - private static _191.Solution2 solution2; - private static _191.Solution3 solution3; - private static _191.Solution4 solution4; - - @BeforeClass - public static void setup() { - solution1 = new _191.Solution1(); - solution2 = new _191.Solution2(); - solution3 = new _191.Solution3(); - solution4 = new _191.Solution4(); - } - - @Test - public void test1() { - assertEquals(1, solution1.hammingWeight(1)); - assertEquals(1, solution2.hammingWeight(1)); - assertEquals(1, solution3.hammingWeight(1)); - assertEquals(1, solution4.hammingWeight(1)); - } - - @Test - public void test2() { -// System.out.println(Integer.MAX_VALUE); -// assertEquals(2147483648, Integer.MAX_VALUE); - } - -} diff --git a/src/test/java/com/fishercoder/_199Test.java b/src/test/java/com/fishercoder/_199Test.java deleted file mode 100644 index 806ef9575c..0000000000 --- a/src/test/java/com/fishercoder/_199Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._199; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _199Test { - private static _199.Solution1 solution1; - private static _199.Solution2 solution2; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _199.Solution1(); - solution2 = new _199.Solution2(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 3)); - assertEquals(Arrays.asList(1, 3), solution1.rightSideView(root)); - assertEquals(Arrays.asList(1, 3), solution2.rightSideView(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_19Test.java b/src/test/java/com/fishercoder/_19Test.java deleted file mode 100644 index f77f7af43b..0000000000 --- a/src/test/java/com/fishercoder/_19Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._19; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _19Test { - private static _19.Solution1 solution1; - private static _19.Solution3 solution3; - private static ListNode head; - private static ListNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _19.Solution1(); - solution3 = new _19.Solution3(); - } - - @Test - public void test1() { - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5}); - expected = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 5}); - assertEquals(expected, solution1.removeNthFromEnd(head, 2)); - } - - @Test - public void test2() { - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5}); - expected = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 5}); - assertEquals(expected, solution3.removeNthFromEnd(head, 2)); - } - - @Test - public void test3() { - head = LinkedListUtils.contructLinkedList(new int[]{1}); - expected = LinkedListUtils.contructLinkedList(new int[]{}); - assertEquals(expected, solution1.removeNthFromEnd(head, 1)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_1Test.java b/src/test/java/com/fishercoder/_1Test.java deleted file mode 100644 index b1146c8487..0000000000 --- a/src/test/java/com/fishercoder/_1Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._1; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _1Test { - private static _1.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _1.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{2, 7, 11, 15}; - assertArrayEquals(new int[]{0, 1}, solution1.twoSum(nums, 9)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_200Test.java b/src/test/java/com/fishercoder/_200Test.java deleted file mode 100644 index bb6c529f7a..0000000000 --- a/src/test/java/com/fishercoder/_200Test.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._200; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _200Test { - private static _200.Solution1 solution1; - private static _200.Solution2 solution2; - private static char[][] grid; - - @Before - public void setup() { - solution1 = new _200.Solution1(); - solution2 = new _200.Solution2(); - } - - @Test - public void test1() { - grid = new char[][]{ - {'1', '1', '1'}, - {'0', '1', '0'}, - {'1', '1', '1'}, - }; - assertEquals(1, solution1.numIslands(grid)); - - grid = new char[][]{ - {'1', '1', '1'}, - {'0', '1', '0'}, - {'1', '1', '1'}, - }; - assertEquals(1, solution2.numIslands(grid)); - } - - @Test - public void test2() { - grid = new char[][]{ - {'1', '1', '1', '1', '0'}, - {'1', '1', '0', '1', '0'}, - {'1', '1', '0', '0', '0'}, - {'0', '0', '0', '0', '0'}, - }; - assertEquals(1, solution1.numIslands(grid)); - - grid = new char[][]{ - {'1', '1', '1', '1', '0'}, - {'1', '1', '0', '1', '0'}, - {'1', '1', '0', '0', '0'}, - {'0', '0', '0', '0', '0'}, - }; - assertEquals(1, solution2.numIslands(grid)); - } - - @Test - public void test3() { - grid = new char[][]{ - {'1', '1', '0', '0', '0'}, - {'1', '1', '0', '0', '0'}, - {'0', '0', '1', '0', '0'}, - {'0', '0', '0', '1', '1'}, - }; - assertEquals(3, solution1.numIslands(grid)); - - grid = new char[][]{ - {'1', '1', '0', '0', '0'}, - {'1', '1', '0', '0', '0'}, - {'0', '0', '1', '0', '0'}, - {'0', '0', '0', '1', '1'}, - }; - assertEquals(3, solution2.numIslands(grid)); - } - -} diff --git a/src/test/java/com/fishercoder/_201Test.java b/src/test/java/com/fishercoder/_201Test.java deleted file mode 100644 index 61d3502b02..0000000000 --- a/src/test/java/com/fishercoder/_201Test.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._201; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/3/17. - */ -public class _201Test { - private static _201.Solution1 solution1; - private static int m; - private static int n; - private static int actual; - private static int expected; - - @BeforeClass - public static void setup() { - solution1 = new _201.Solution1(); - } - - @Test - public void test1() { - m = 5; - n = 7; - actual = solution1.rangeBitwiseAnd(m, n); - expected = 4; - assertEquals(expected, actual); - actual = solution1.rangeBitwiseAnd(m, n); - assertEquals(expected, actual); - } - - @Test - public void test2() { - m = 1; - n = 2; - actual = solution1.rangeBitwiseAnd(m, n); - expected = 0; - assertEquals(expected, actual); - actual = solution1.rangeBitwiseAnd(m, n); - assertEquals(expected, actual); - } - - @Test - public void test3() { - m = 0; - n = 2147483647; - actual = solution1.rangeBitwiseAnd(m, n); - expected = 0; - assertEquals(expected, actual); - actual = solution1.rangeBitwiseAnd(m, n); - assertEquals(expected, actual); - } - - @Test - public void test4() { - m = 20000; - n = 2147483647; - actual = solution1.rangeBitwiseAnd(m, n); - expected = 0; - assertEquals(expected, actual); - actual = solution1.rangeBitwiseAnd(m, n); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_202Test.java b/src/test/java/com/fishercoder/_202Test.java deleted file mode 100644 index 95ffe0c157..0000000000 --- a/src/test/java/com/fishercoder/_202Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._202; -import com.fishercoder.solutions._53; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _202Test { - private static _202.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _202.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isHappy(7)); - } -} diff --git a/src/test/java/com/fishercoder/_203Test.java b/src/test/java/com/fishercoder/_203Test.java deleted file mode 100644 index 75a4780e59..0000000000 --- a/src/test/java/com/fishercoder/_203Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._203; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _203Test { - private static _203.Solution1 solution1; - private static ListNode head; - private static ListNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _203.Solution1(); - } - - @Test - public void test1() { - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 6, 3, 4, 5, 6}); - expected = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5}); - assertEquals(expected, solution1.removeElements(head, 6)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_206Test.java b/src/test/java/com/fishercoder/_206Test.java deleted file mode 100644 index 11d0220cfe..0000000000 --- a/src/test/java/com/fishercoder/_206Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._206; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _206Test { - private static _206.Solution1 solution1; - private static _206.Solution2 solution2; - private static ListNode head; - - @BeforeClass - public static void setup() { - solution1 = new _206.Solution1(); - solution2 = new _206.Solution2(); - } - - @Test - public void test1() { - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4}); - assertEquals(LinkedListUtils.contructLinkedList(new int[]{4, 3, 2, 1}), solution1.reverseList(head)); - } - - @Test - public void test2() { - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4}); - assertEquals(LinkedListUtils.contructLinkedList(new int[]{4, 3, 2, 1}), solution2.reverseList(head)); - } -} diff --git a/src/test/java/com/fishercoder/_207Test.java b/src/test/java/com/fishercoder/_207Test.java deleted file mode 100644 index 1bc2f676b8..0000000000 --- a/src/test/java/com/fishercoder/_207Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._207; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _207Test { - private static _207.Solution1 test; - private static boolean actual; - private static boolean expected; - private static int[][] prerequisites; - private static int numCourses; - - @BeforeClass - public static void setup() { - test = new _207.Solution1(); - } - - @Test - public void test1() { - numCourses = 2; - prerequisites = new int[][]{{0, 1}}; - expected = true; - actual = test.canFinish(numCourses, prerequisites); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_209Test.java b/src/test/java/com/fishercoder/_209Test.java deleted file mode 100644 index eb2f293a61..0000000000 --- a/src/test/java/com/fishercoder/_209Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._209; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _209Test { - private static _209.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _209.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{2, 3, 1, 2, 4, 3}; - assertEquals(2, solution1.minSubArrayLen(7, nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_20Test.java b/src/test/java/com/fishercoder/_20Test.java deleted file mode 100644 index 5a686a2593..0000000000 --- a/src/test/java/com/fishercoder/_20Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._20; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _20Test { - private static _20.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _20.Solution1(); - } - - @Test - public void test1() { - assertEquals(false, solution1.isValid("(]")); - } - - @Test - public void test2() { - assertEquals(false, solution1.isValid("([)]")); - } - - @Test - public void test3() { - assertEquals(true, solution1.isValid("()[]{}")); - } - - @Test - public void test4() { - assertEquals(true, solution1.isValid("()")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_211Test.java b/src/test/java/com/fishercoder/_211Test.java deleted file mode 100644 index 986af8ee15..0000000000 --- a/src/test/java/com/fishercoder/_211Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._211; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _211Test { - private static _211.Solution1.WordDictionary wordDictionarySolution1; - - @BeforeClass - public static void setup() { - wordDictionarySolution1 = new _211.Solution1.WordDictionary(); - } - - @Test - public void test1() { - wordDictionarySolution1.addWord("bad"); - wordDictionarySolution1.addWord("dad"); - wordDictionarySolution1.addWord("mad"); - assertEquals(false, wordDictionarySolution1.search("pad")); - assertEquals(true, wordDictionarySolution1.search("bad")); - assertEquals(true, wordDictionarySolution1.search(".ad")); - assertEquals(true, wordDictionarySolution1.search("b..")); - } -} diff --git a/src/test/java/com/fishercoder/_215Test.java b/src/test/java/com/fishercoder/_215Test.java deleted file mode 100644 index ace0d95599..0000000000 --- a/src/test/java/com/fishercoder/_215Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._215; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/9/17. - */ -public class _215Test { - private static _215.Solution2 solution2; - private static int k; - private static int[] nums; - private static int actual; - private static int expected; - - @BeforeClass - public static void setup() { - solution2 = new _215.Solution2(); - } - - @Test - public void test1() { - k = 2; - nums = new int[]{3, 2, 1, 5, 6, 4}; - actual = solution2.findKthLargest(nums, k); - expected = 5; - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_217Test.java b/src/test/java/com/fishercoder/_217Test.java deleted file mode 100644 index 67735b27b5..0000000000 --- a/src/test/java/com/fishercoder/_217Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._217; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _217Test { - private static _217.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _217.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3, 4, 3}; - assertEquals(true, solution1.containsDuplicate(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_219Test.java b/src/test/java/com/fishercoder/_219Test.java deleted file mode 100644 index 2897694554..0000000000 --- a/src/test/java/com/fishercoder/_219Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._219; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _219Test { - private static _219.Solution1 solution1; - private static _219.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _219.Solution1(); - solution2 = new _219.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3, 1}; - assertEquals(true, solution1.containsNearbyDuplicate(nums, 3)); - } - - @Test - public void test2() { - nums = new int[]{1, 2, 3, 1}; - assertEquals(true, solution2.containsNearbyDuplicate(nums, 3)); - } - - @Test - public void test3() { - nums = new int[]{1, 2, 3, 1, 2, 3}; - assertEquals(false, solution2.containsNearbyDuplicate(nums, 2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_21Test.java b/src/test/java/com/fishercoder/_21Test.java deleted file mode 100644 index 20146894ec..0000000000 --- a/src/test/java/com/fishercoder/_21Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.solutions._21; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _21Test { - private static _21.Solution1 solution1; - private static _21.Solution2 solution2; - private static ListNode l1; - private static ListNode l2; - - @BeforeClass - public static void setup() { - solution1 = new _21.Solution1(); - solution2 = new _21.Solution2(); - } - - @Test - public void test1() { - l1 = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 5)); - l2 = ListNode.createSinglyLinkedList(Arrays.asList(2, 4, 6)); - assertEquals(ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 2, 3, 4, 5, 6)), solution1.mergeTwoLists(l1, l2)); - } - - @Test - public void test2() { - l1 = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 5)); - l2 = ListNode.createSinglyLinkedList(Arrays.asList(2, 4, 6)); - assertEquals(ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 2, 3, 4, 5, 6)), solution2.mergeTwoLists(l1, l2)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_220Test.java b/src/test/java/com/fishercoder/_220Test.java deleted file mode 100644 index 6a1ae77fdb..0000000000 --- a/src/test/java/com/fishercoder/_220Test.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._220; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _220Test { - private static _220.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _220.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{-1, -1}; - assertEquals(true, solution1.containsNearbyAlmostDuplicate(nums, 1, 0)); - } - - @Test - public void test2() { - nums = new int[]{1, 2}; - assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 0, 1)); - } - - @Test - public void test3() { - nums = new int[]{4, 2}; - assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 2, 1)); - } - - @Test - public void test4() { - nums = new int[]{2, 2}; - assertEquals(true, solution1.containsNearbyAlmostDuplicate(nums, 3, 0)); - } - - @Test - public void test5() { - nums = new int[]{1}; - assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 1, 1)); - } - - @Test - public void test6() { - nums = new int[]{2147483647, -2147483647}; - assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 1, 2147483647)); - } - - @Test - public void test7() { - nums = new int[]{-1, 2147483647}; - assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 1, 2147483647)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_221Test.java b/src/test/java/com/fishercoder/_221Test.java deleted file mode 100644 index 4050781c0f..0000000000 --- a/src/test/java/com/fishercoder/_221Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._221; -import com.fishercoder.solutions._50; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _221Test { - private static _221.Solution1 solution1; - private static char[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _221.Solution1(); - } - - @Test - public void test1() { - matrix = new char[][]{ - {'1', '0', '1', '0', '0'}, - {'1', '0', '1', '1', '1'}, - {'1', '1', '1', '1', '1'}, - {'1', '0', '0', '1', '0'}, - }; - - assertEquals(4, solution1.maximalSquare(matrix)); - } -} diff --git a/src/test/java/com/fishercoder/_227Test.java b/src/test/java/com/fishercoder/_227Test.java deleted file mode 100644 index 1ba1e050bf..0000000000 --- a/src/test/java/com/fishercoder/_227Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._227; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _227Test { - private static _227.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _227.Solution1(); - } - - @Test - public void test1() { - assertEquals(7, solution1.calculate("3+2*2")); - } - - @Test - public void test2() { - assertEquals(1, solution1.calculate(" 3/2 ")); - } - - @Test - public void test3() { - assertEquals(5, solution1.calculate(" 3+5 / 2 ")); - } - - @Test - public void test4() { - assertEquals(27, solution1.calculate("100000000/1/2/3/4/5/6/7/8/9/10")); - } - -} diff --git a/src/test/java/com/fishercoder/_228Test.java b/src/test/java/com/fishercoder/_228Test.java deleted file mode 100644 index b41e9d15b9..0000000000 --- a/src/test/java/com/fishercoder/_228Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._228; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _228Test { - private static _228.Solution1 solution1; - private static List expected; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _228.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{0, 1, 2, 4, 5, 7}; - expected = Arrays.asList("0->2", "4->5", "7"); - assertEquals(expected, solution1.summaryRanges(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_229Test.java b/src/test/java/com/fishercoder/_229Test.java deleted file mode 100644 index 3c11489454..0000000000 --- a/src/test/java/com/fishercoder/_229Test.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._229; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _229Test { - private static _229.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _229.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1}; - assertEquals(Arrays.asList(1), solution1.majorityElement(nums)); - } - - @Test - public void test2() { - nums = new int[]{1, 2}; - assertEquals(Arrays.asList(2, 1), solution1.majorityElement(nums)); - } - - @Test - public void test3() { - nums = new int[]{2, 2}; - assertEquals(Arrays.asList(2), solution1.majorityElement(nums)); - } - - @Test - public void test4() { - nums = new int[]{1, 2, 3}; - assertEquals(Arrays.asList(), solution1.majorityElement(nums)); - } - - @Test - public void test5() { - nums = new int[]{3, 2, 3}; - assertEquals(Arrays.asList(3), solution1.majorityElement(nums)); - } - - @Test - public void test6() { - nums = new int[]{3, 3, 4}; - assertEquals(Arrays.asList(3), solution1.majorityElement(nums)); - } - - @Test - public void test7() { - nums = new int[]{2, 2, 1, 3}; - assertEquals(Arrays.asList(2), solution1.majorityElement(nums)); - } - - @Test - public void test8() { - nums = new int[]{2, 2, 3, 3, 2, 4}; - assertEquals(Arrays.asList(2), solution1.majorityElement(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_22Test.java b/src/test/java/com/fishercoder/_22Test.java deleted file mode 100644 index 30c98d2c7f..0000000000 --- a/src/test/java/com/fishercoder/_22Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._22; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _22Test { - private static _22.Solution1 solution1; - private static _22.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _22.Solution1(); - solution2 = new _22.Solution2(); - } - - @Test - public void test1() { - CommonUtils.printList(solution1.generateParenthesis(3)); - } - - @Test - public void test2() { - CommonUtils.printList(solution2.generateParenthesis(3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_230Test.java b/src/test/java/com/fishercoder/_230Test.java deleted file mode 100644 index 8c54b1656a..0000000000 --- a/src/test/java/com/fishercoder/_230Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._230; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/19/17. - */ -public class _230Test { - private static _230.Solution1 solution1; - private static _230.Solution2 solution2; - private static TreeNode root; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _230.Solution1(); - solution2 = new _230.Solution2(); - } - - @Test - public void test1() { - root = new TreeNode(1); - k = 1; - assertEquals(1, solution1.kthSmallest(root, k)); - assertEquals(1, solution2.kthSmallest(root, k)); - } - - @Test - public void test2() { - root = new TreeNode(2); - root.left = new TreeNode(1); - k = 1; - assertEquals(1, solution1.kthSmallest(root, k)); - assertEquals(1, solution2.kthSmallest(root, k)); - } -} diff --git a/src/test/java/com/fishercoder/_234Test.java b/src/test/java/com/fishercoder/_234Test.java deleted file mode 100644 index 9570cb106c..0000000000 --- a/src/test/java/com/fishercoder/_234Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._234; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _234Test { - private static _234.Solution1 solution1; - private static _234.Solution2 solution2; - private static ListNode head; - - @BeforeClass - public static void setup() { - solution1 = new _234.Solution1(); - solution2 = new _234.Solution2(); - } - - @Test - public void test1() { - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 2, 1}); - assertEquals(true, solution1.isPalindrome(head)); - - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 2, 1}); - assertEquals(true, solution2.isPalindrome(head)); - } - - @Test - public void test2() { - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 2, 1}); - assertEquals(true, solution1.isPalindrome(head)); - - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 2, 1}); - assertEquals(true, solution2.isPalindrome(head)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_235Test.java b/src/test/java/com/fishercoder/_235Test.java deleted file mode 100644 index ef01b11f97..0000000000 --- a/src/test/java/com/fishercoder/_235Test.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._235; - -import java.util.Arrays; - -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _235Test { - private static _235.Solution1 solution1; - private static TreeNode root; - private static TreeNode p; - private static TreeNode q; - - @BeforeClass - public static void setup() { - solution1 = new _235.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5)); - TreeUtils.printBinaryTree(root); - - p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5)); - TreeUtils.printBinaryTree(p); - - q = TreeUtils.constructBinaryTree(Arrays.asList(8, 7, 9)); - TreeUtils.printBinaryTree(q); - - assertEquals(root, solution1.lowestCommonAncestor(root, p, q)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5)); - TreeUtils.printBinaryTree(root); - - p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5)); - TreeUtils.printBinaryTree(p); - - q = TreeUtils.constructBinaryTree(Arrays.asList(4)); - TreeUtils.printBinaryTree(q); - - assertEquals(p, solution1.lowestCommonAncestor(root, p, q)); - } -} diff --git a/src/test/java/com/fishercoder/_237Test.java b/src/test/java/com/fishercoder/_237Test.java deleted file mode 100644 index e2867c3297..0000000000 --- a/src/test/java/com/fishercoder/_237Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._237; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -public class _237Test { - private static _237.Solution1 solution1; - private static ListNode head; - - @BeforeClass - public static void setup() { - solution1 = new _237.Solution1(); - } - - @Test - public void test1() { - head = ListNode.createSinglyLinkedList(Arrays.asList(4, 5, 1, 9)); - CommonUtils.printList(head); - solution1.deleteNode(head.next); - CommonUtils.printList(head); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_238Test.java b/src/test/java/com/fishercoder/_238Test.java deleted file mode 100644 index 72d584c6cc..0000000000 --- a/src/test/java/com/fishercoder/_238Test.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._238; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _238Test { - private static _238.Solution1 solution1; - private static int[] expected; - private static int[] actual; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _238.Solution1(); - expected = new int[]{}; - actual = new int[]{}; - } - - @Test - public void test1() { - nums = new int[]{0, 0}; - expected = new int[]{0, 0}; - actual = solution1.productExceptSelf(nums); - assertArrayEquals(expected, actual); - } - - @Test - public void test2() { - nums = new int[]{1, 0}; - expected = new int[]{0, 1}; - actual = solution1.productExceptSelf(nums); - assertArrayEquals(expected, actual); - } - - @Test - public void test3() { - nums = new int[]{1, 2, 3, 4}; - expected = new int[]{24, 12, 8, 6}; - actual = solution1.productExceptSelf(nums); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_239Test.java b/src/test/java/com/fishercoder/_239Test.java deleted file mode 100644 index 6dc00152ad..0000000000 --- a/src/test/java/com/fishercoder/_239Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._239; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _239Test { - private static _239.Solution1 solution1; - private static int[] expected; - private static int[] actual; - private static int[] nums; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _239.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = new int[]{}; - actual = new int[]{}; - nums = new int[]{}; - k = 0; - } - - @Test - public void test1() { - - nums = new int[]{1, 3, -1, -3, 5, 3, 6, 7}; - k = 3; - expected = new int[]{3, 3, 5, 5, 6, 7}; - actual = solution1.maxSlidingWindow(nums, k); - Assert.assertArrayEquals(expected, actual); - - } -} diff --git a/src/test/java/com/fishercoder/_23Test.java b/src/test/java/com/fishercoder/_23Test.java deleted file mode 100644 index 0beea4852c..0000000000 --- a/src/test/java/com/fishercoder/_23Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._23; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -public class _23Test { - private static _23.Solution1 solution1; - private static ListNode[] lists; - - @BeforeClass - public static void setup() { - solution1 = new _23.Solution1(); - } - - @Test - public void test1() { - ListNode head1 = ListNode.createSinglyLinkedList(Arrays.asList(1, 3, 5, 7, 11)); - ListNode head2 = ListNode.createSinglyLinkedList(Arrays.asList(2, 8, 12)); - ListNode head3 = ListNode.createSinglyLinkedList(Arrays.asList(4, 6, 9, 10)); - lists = new ListNode[]{head1, head2, head3}; - CommonUtils.printList(solution1.mergeKLists(lists)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_240Test.java b/src/test/java/com/fishercoder/_240Test.java deleted file mode 100644 index 9c6282dfd3..0000000000 --- a/src/test/java/com/fishercoder/_240Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._240; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _240Test { - private static _240.Solution1 solution1; - private static boolean actual; - private static boolean expected; - private static int target; - private static int[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _240.Solution1(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - target = 5; - matrix = new int[][]{ - {1, 4, 7, 11, 15}, - {2, 5, 8, 12, 19}, - {3, 6, 9, 16, 22}, - {10, 13, 14, 17, 24}, - {18, 21, 23, 26, 30} - }; - expected = true; - actual = solution1.searchMatrix(matrix, target); - assertEquals(expected, actual); - } - - @Test - public void test2() { - target = 0; - matrix = new int[][]{}; - expected = false; - actual = solution1.searchMatrix(matrix, target); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_247Test.java b/src/test/java/com/fishercoder/_247Test.java deleted file mode 100644 index 89a42853e2..0000000000 --- a/src/test/java/com/fishercoder/_247Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._247; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _247Test { - private static _247.Solution1 solution1; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _247.Solution1(); - } - - @Test - public void test1() { - expected = Arrays.asList("11","69","88","96"); - assertEquals(expected, solution1.findStrobogrammatic(2)); - } - -} diff --git a/src/test/java/com/fishercoder/_24Test.java b/src/test/java/com/fishercoder/_24Test.java deleted file mode 100644 index 98a53a8aa5..0000000000 --- a/src/test/java/com/fishercoder/_24Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._24; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -public class _24Test { - private static _24.Solution1 solution1; - private static ListNode head; - - @BeforeClass - public static void setup() { - solution1 = new _24.Solution1(); - } - - @Test - public void test1() { - head = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4)); - CommonUtils.printList(solution1.swapPairs(head)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_25Test.java b/src/test/java/com/fishercoder/_25Test.java deleted file mode 100644 index d726ec397a..0000000000 --- a/src/test/java/com/fishercoder/_25Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._25; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _25Test { - private static _25 test; - private static ListNode actual; - private static ListNode expected; - private static ListNode head; - - @BeforeClass - public static void setup() { - test = new _25(); - } - - @Test - public void test1() { - head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5}); - actual = test.reverseKGroup(head, 2); - expected = LinkedListUtils.contructLinkedList(new int[]{2, 1, 4, 3, 5}); - assertEquals(actual, expected); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_264Test.java b/src/test/java/com/fishercoder/_264Test.java deleted file mode 100644 index 6c2afbcc19..0000000000 --- a/src/test/java/com/fishercoder/_264Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._264; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _264Test { - private static _264.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _264.Solution1(); - } - - @Test - public void test1() { - assertEquals(12, solution1.nthUglyNumber(10)); - } - - @Test - public void test2() { - assertEquals(402653184, solution1.nthUglyNumber(1352)); - } -} diff --git a/src/test/java/com/fishercoder/_269Test.java b/src/test/java/com/fishercoder/_269Test.java deleted file mode 100644 index d5afb02e6f..0000000000 --- a/src/test/java/com/fishercoder/_269Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._269; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _269Test { - private static _269.Solution1 solution1; - private static String[] words; - - @BeforeClass - public static void setup() { - solution1 = new _269.Solution1(); - } - - @Test - public void test1() { - words = new String[]{"wrt", "wrf", "er", "ett", "rftt"}; - assertEquals("wertf", solution1.alienOrder(words)); - } - -} diff --git a/src/test/java/com/fishercoder/_26Test.java b/src/test/java/com/fishercoder/_26Test.java deleted file mode 100644 index 3a05a7121a..0000000000 --- a/src/test/java/com/fishercoder/_26Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._26; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _26Test { - private static _26.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _26.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 1, 2}; - assertEquals(2, solution1.removeDuplicates(nums)); - } - - @Test - public void test2() { - nums = new int[]{1, 1, 2, 2, 3}; - assertEquals(3, solution1.removeDuplicates(nums)); - } - - @Test - public void test3() { - nums = new int[]{1, 1}; - assertEquals(1, solution1.removeDuplicates(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_273Test.java b/src/test/java/com/fishercoder/_273Test.java deleted file mode 100644 index 6cae2d5d1f..0000000000 --- a/src/test/java/com/fishercoder/_273Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._273; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _273Test { - private static _273.Solution1 solution1; - private static int num; - - @BeforeClass - public static void setup() { - solution1 = new _273.Solution1(); - } - - @Test - public void test1() { - num = 123; - assertEquals("One Hundred Twenty Three", solution1.numberToWords(num)); - } - - @Test - public void test2() { - num = 12345; - assertEquals("Twelve Thousand Three Hundred Forty Five", solution1.numberToWords(num)); - } - - @Test - public void test3() { - num = 1234567; - assertEquals("One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven", solution1.numberToWords(num)); - } -} diff --git a/src/test/java/com/fishercoder/_27Test.java b/src/test/java/com/fishercoder/_27Test.java deleted file mode 100644 index 1b482f732d..0000000000 --- a/src/test/java/com/fishercoder/_27Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._27; -import com.fishercoder.solutions._734; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _27Test { - private static _27.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _27.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {3, 2, 2, 3}; - assertEquals(2, solution1.removeElement(nums, 3)); - } - - @Test - public void test2() { - nums = new int[] {2, 2, 3}; - assertEquals(1, solution1.removeElement(nums, 2)); - } - - @Test - public void test3() { - nums = new int[] {1}; - assertEquals(0, solution1.removeElement(nums, 1)); - } -} diff --git a/src/test/java/com/fishercoder/_283Test.java b/src/test/java/com/fishercoder/_283Test.java deleted file mode 100644 index 2c27505666..0000000000 --- a/src/test/java/com/fishercoder/_283Test.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._283; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _283Test { - private static _283.Solution1 solution1; - private static _283.Solution2 solution2; - private static _283.Solution3 solution3; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _283.Solution1(); - solution2 = new _283.Solution2(); - solution3 = new _283.Solution3(); - } - - @Test - public void test1() { - nums = new int[]{0, 1, 0, 3, 12}; - solution1.moveZeroes(nums); - CommonUtils.printArray(nums); - } - - @Test - public void test2() { - nums = new int[]{0, 1, 0, 3, 12}; - solution2.moveZeroes(nums); - CommonUtils.printArray(nums); - } - - @Test - public void test3() { - nums = new int[]{0, 1, 0, 3, 12}; - solution3.moveZeroes(nums); - CommonUtils.printArray(nums); - } - - @Test - public void test4() { - nums = new int[]{1, 0}; - solution1.moveZeroes(nums); - CommonUtils.printArray(nums); - } - - @Test - public void test5() { - nums = new int[]{0, 1, 0, 3, 12}; - solution1.moveZeroes(nums); - assertArrayEquals(new int[]{1, 3, 12, 0, 0}, nums); - } - - @Test - public void test6() { - nums = new int[]{1, 0, 0}; - solution1.moveZeroes(nums); - assertArrayEquals(new int[]{1, 0, 0}, nums); - } - - @Test - public void test7() { - nums = new int[]{1, 0}; - solution2.moveZeroes(nums); - CommonUtils.printArray(nums); - } - - @Test - public void test8() { - nums = new int[]{0, 1, 0, 3, 12}; - solution2.moveZeroes(nums); - assertArrayEquals(new int[]{1, 3, 12, 0, 0}, nums); - } - - @Test - public void test9() { - nums = new int[]{1, 0, 0}; - solution2.moveZeroes(nums); - assertArrayEquals(new int[]{1, 0, 0}, nums); - } - - @Test - public void test10() { - nums = new int[]{1, 0}; - solution3.moveZeroes(nums); - CommonUtils.printArray(nums); - } - - @Test - public void test11() { - nums = new int[]{0, 1, 0, 3, 12}; - solution3.moveZeroes(nums); - assertArrayEquals(new int[]{1, 3, 12, 0, 0}, nums); - } - - @Test - public void test12() { - nums = new int[]{1, 0, 0}; - solution3.moveZeroes(nums); - assertArrayEquals(new int[]{1, 0, 0}, nums); - } -} diff --git a/src/test/java/com/fishercoder/_28Test.java b/src/test/java/com/fishercoder/_28Test.java deleted file mode 100644 index ce9378a6f2..0000000000 --- a/src/test/java/com/fishercoder/_28Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._28; -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _28Test { - private static _28.Solution1 solution1; - - @Before - public void setupForEachTest() { - solution1 = new _28.Solution1(); - } - - @Test - public void test1() { - assertEquals(0, solution1.strStr("a", "")); - } - - @Test - public void test2() { - assertEquals(-1, solution1.strStr("mississippi", "a")); - } - - @Test - public void test3() { - assertEquals(0, solution1.strStr("a", "a")); - } -} diff --git a/src/test/java/com/fishercoder/_291Test.java b/src/test/java/com/fishercoder/_291Test.java deleted file mode 100644 index ad3a4958ab..0000000000 --- a/src/test/java/com/fishercoder/_291Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._291; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _291Test { - private static _291.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _291.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.wordPatternMatch("abab", "redblueredblue")); - } -} diff --git a/src/test/java/com/fishercoder/_294Test.java b/src/test/java/com/fishercoder/_294Test.java deleted file mode 100644 index eb36bae131..0000000000 --- a/src/test/java/com/fishercoder/_294Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._294; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _294Test { - private static _294.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _294.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.canWin("++++")); - } -} diff --git a/src/test/java/com/fishercoder/_295Test.java b/src/test/java/com/fishercoder/_295Test.java deleted file mode 100644 index 453982b37f..0000000000 --- a/src/test/java/com/fishercoder/_295Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._295; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/27/17. - */ -public class _295Test { - private static _295.Solution1.MedianFinder solution1; - - @BeforeClass - public static void setup() { - solution1 = new _295.Solution1.MedianFinder(); - } - - @Test - public void test1() { - solution1.addNum(1); - solution1.addNum(3); - solution1.addNum(-1); - assertEquals(1.0, solution1.findMedian(), 0); - } -} diff --git a/src/test/java/com/fishercoder/_29Test.java b/src/test/java/com/fishercoder/_29Test.java deleted file mode 100644 index 9f9ebc10c7..0000000000 --- a/src/test/java/com/fishercoder/_29Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._29; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _29Test { - private static _29.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _29.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.divide(4, 3)); - } -} diff --git a/src/test/java/com/fishercoder/_2Test.java b/src/test/java/com/fishercoder/_2Test.java deleted file mode 100644 index 9cd4a02800..0000000000 --- a/src/test/java/com/fishercoder/_2Test.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._2; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _2Test { - private static _2.Solution1 solution1; - private static ListNode l1; - private static ListNode l2; - private static ListNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _2.Solution1(); - } - - @Test - public void test1() { - l1 = LinkedListUtils.contructLinkedList(new int[]{2, 4, 3}); - l2 = LinkedListUtils.contructLinkedList(new int[]{5, 6, 4}); - expected = LinkedListUtils.contructLinkedList(new int[]{7, 0, 8}); - assertEquals(expected, solution1.addTwoNumbers(l1, l2)); - } - - @Test - public void test2() { - l1 = LinkedListUtils.contructLinkedList(new int[]{1, 8}); - l2 = LinkedListUtils.contructLinkedList(new int[]{0}); - expected = LinkedListUtils.contructLinkedList(new int[]{1, 8}); - assertEquals(expected, solution1.addTwoNumbers(l1, l2)); - } - - @Test - public void test3() { - l1 = LinkedListUtils.contructLinkedList(new int[]{5}); - l2 = LinkedListUtils.contructLinkedList(new int[]{5}); - expected = LinkedListUtils.contructLinkedList(new int[]{0, 1}); - assertEquals(expected, solution1.addTwoNumbers(l1, l2)); - } -} diff --git a/src/test/java/com/fishercoder/_300Test.java b/src/test/java/com/fishercoder/_300Test.java deleted file mode 100644 index 253eb0318b..0000000000 --- a/src/test/java/com/fishercoder/_300Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._300; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _300Test { - - private static _300.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _300.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{10, 9, 2, 5, 3, 7, 101, 18}; - assertEquals(4, solution1.lengthOfLIS(nums)); - - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_306Test.java b/src/test/java/com/fishercoder/_306Test.java deleted file mode 100644 index 5820a3cfdd..0000000000 --- a/src/test/java/com/fishercoder/_306Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._306; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _306Test { - private static _306.Solution1 solution1; - private static String num; - - @BeforeClass - public static void setup() { - solution1 = new _306.Solution1(); - } - - @Test - public void test1() { - num = "0235813"; - assertEquals(false, solution1.isAdditiveNumber(num)); - } - - @Test - public void test2() { - num = "000"; - assertEquals(true, solution1.isAdditiveNumber(num)); - } - - @Test - public void test3() { - num = "011235"; - assertEquals(true, solution1.isAdditiveNumber(num)); - } -} diff --git a/src/test/java/com/fishercoder/_30Test.java b/src/test/java/com/fishercoder/_30Test.java deleted file mode 100644 index 60fe1fe520..0000000000 --- a/src/test/java/com/fishercoder/_30Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._30; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _30Test { - private static _30.Solution1 solution1; - private static String[] words; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _30.Solution1(); - } - - @Test - @Ignore - public void test1() { - words = new String[] {"foo", "bar"}; - expected = Arrays.asList(0, 9); - assertEquals(expected, solution1.findSubstring("barfoothefoobarman", words)); - } -} diff --git a/src/test/java/com/fishercoder/_316Test.java b/src/test/java/com/fishercoder/_316Test.java deleted file mode 100644 index ca7f79b0ba..0000000000 --- a/src/test/java/com/fishercoder/_316Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._316; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _316Test { - private static _316.Solution1 solution1; - private static _316.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _316.Solution1(); - solution2 = new _316.Solution2(); - } - - @Test - public void test1() { - assertEquals("abc", solution1.removeDuplicateLetters("bcabc")); - assertEquals("abc", solution2.removeDuplicateLetters("bcabc")); - } - - @Test - public void test2() { - assertEquals("acdb", solution1.removeDuplicateLetters("cbacdcbc")); - assertEquals("acdb", solution2.removeDuplicateLetters("cbacdcbc")); - } -} diff --git a/src/test/java/com/fishercoder/_319Test.java b/src/test/java/com/fishercoder/_319Test.java deleted file mode 100644 index 30a9f59d52..0000000000 --- a/src/test/java/com/fishercoder/_319Test.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._319; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _319Test { - private static _319.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _319.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.bulbSwitch(2)); - } - - @Test - public void test2() { - assertEquals(1, solution1.bulbSwitch(3)); - } - - @Test - public void test3() { - assertEquals(2, solution1.bulbSwitch(4)); - } - - @Test - public void test4() { - assertEquals(2, solution1.bulbSwitch(5)); - } - - @Test - public void test5() { - assertEquals(2, solution1.bulbSwitch(6)); - } - - @Test - public void test6() { - assertEquals(2, solution1.bulbSwitch(7)); - } - - @Test - public void test7() { - assertEquals(2, solution1.bulbSwitch(8)); - } - - @Test - public void test8() { - assertEquals(3, solution1.bulbSwitch(9)); - } - - @Test - public void test11() { - assertEquals(3, solution1.bulbSwitch(15)); - } - - @Test - public void test9() { - assertEquals(4, solution1.bulbSwitch(17)); - } - - @Test - public void test10() { - assertEquals(4, solution1.bulbSwitch(16)); - } -} diff --git a/src/test/java/com/fishercoder/_31Test.java b/src/test/java/com/fishercoder/_31Test.java deleted file mode 100644 index 98ee63a8e1..0000000000 --- a/src/test/java/com/fishercoder/_31Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._31; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _31Test { - private static _31.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _31.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3}; - solution1.nextPermutation(nums); - CommonUtils.printArray(nums); - } -} diff --git a/src/test/java/com/fishercoder/_320Test.java b/src/test/java/com/fishercoder/_320Test.java deleted file mode 100644 index 65720d29fc..0000000000 --- a/src/test/java/com/fishercoder/_320Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._320; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertTrue; - -public class _320Test { - private static _320.Solution1 solution1; - private static List expected; - private static List actual; - private static String word; - - @BeforeClass - public static void setup() { - solution1 = new _320.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = new ArrayList<>(); - actual = new ArrayList<>(); - } - - @Test - public void test1() { - word = "word"; - expected = - Arrays.asList("word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", - "w1r1", "1o2", "2r1", "3d", "w3", "4"); - actual = solution1.generateAbbreviations(word); - assertTrue(expected.containsAll(actual) && actual.containsAll(expected)); - } -} diff --git a/src/test/java/com/fishercoder/_325Test.java b/src/test/java/com/fishercoder/_325Test.java deleted file mode 100644 index 248498373f..0000000000 --- a/src/test/java/com/fishercoder/_325Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._325; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _325Test { - private static _325.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _325.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{-2, -1, 2, 1}; - assertEquals(2, solution1.maxSubArrayLen(nums, 1)); - } - - @Test - public void test2() { - nums = new int[]{1, -1, 5, -2, 3}; - assertEquals(4, solution1.maxSubArrayLen(nums, 3)); - } -} diff --git a/src/test/java/com/fishercoder/_326Test.java b/src/test/java/com/fishercoder/_326Test.java deleted file mode 100644 index 624f0f1a21..0000000000 --- a/src/test/java/com/fishercoder/_326Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._326; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _326Test { - private static _326.Solution1 solution1; - private static _326.Solution2 solution2; - private static _326.Solution3 solution3; - - @BeforeClass - public static void setup() { - solution1 = new _326.Solution1(); - solution2 = new _326.Solution2(); - solution3 = new _326.Solution3(); - } - - @Test - public void test1() { - assertEquals(false, solution1.isPowerOfThree(12)); - assertEquals(false, solution2.isPowerOfThree(12)); - assertEquals(false, solution3.isPowerOfThree(12)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_327Test.java b/src/test/java/com/fishercoder/_327Test.java deleted file mode 100644 index 292846d5ba..0000000000 --- a/src/test/java/com/fishercoder/_327Test.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._327; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _327Test { - private static _327.Solution1 solution1; - private static _327.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _327.Solution1(); - solution2 = new _327.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{-2, 5, -1}; - assertEquals(3, solution1.countRangeSum(nums, -2, 2)); - assertEquals(3, solution2.countRangeSum(nums, -2, 2)); - } - - @Test - public void test2() { - nums = new int[]{-1, 1}; - assertEquals(1, solution1.countRangeSum(nums, 0, 0)); - assertEquals(1, solution2.countRangeSum(nums, 0, 0)); - } - - @Test - public void test3() { - nums = new int[]{0}; - assertEquals(1, solution1.countRangeSum(nums, 0, 0)); - assertEquals(1, solution2.countRangeSum(nums, 0, 0)); - } - - @Test - public void test4() { - nums = new int[]{0, 0}; - assertEquals(3, solution1.countRangeSum(nums, 0, 0)); - assertEquals(3, solution2.countRangeSum(nums, 0, 0)); - } - - @Test - public void test5() { - nums = new int[]{-2147483647, 0, -2147483647, 2147483647}; - assertEquals(3, solution1.countRangeSum(nums, -564, 3864)); - assertEquals(3, solution2.countRangeSum(nums, -564, 3864)); - } - - @Test - public void test6() { - nums = new int[]{28, 26, 22, 7, 12, -26, 25, 11, -14, 0, 0, 6, 20, 4, 17, -7, 4, -14, -19, -16, 8, -21, -26, 24, -29, -18, -13, 2, -16, -14, -26, -14, -7, -14, -30, -3, 9, -16, 28, 3, -26, -5, 4, -28, -3, 11, 26, 14, 20, 15, -4, -12, -18, -21, -28, 22, -4, 0, 2, 22, 16, -10, -14, -5, 2, -3, -16, -23, -26, -5, 21, 17, 4, 29, 19, -2, -8, 5, -14, 13, 10, -15, 2, 25, -5, -21, 13, -28, 7, -12, 9, -1, 15, -26, -9, -3, 3, 14, -25, -8, 12, -4, 0, -28, -30, -13, -11, 16, -3, 5, 5, 22, 7, -5, 20, -9, -29, 29, -25, -27, -6, -22, 22, 11, -18, 3, -18, -21, -8, 6, -20, -22, -2, 25, 25, -4, 19, 13, 9, 18, -25, -9, 16, -30, -30, 18, 29, 27, -26, 11, 28, -6, 4, 29, -18, 28, 9, 23, 13, -22, -10, 21, 23, -13, -22, 8, -7, -6, 24, 11, 27, 8, 15, 23, -11, -28, 7, -11, -2, -26, -21, -13, 22, 2, 6, 18, -17, 12, 11, -28, -3, -15, -5, -14, -21, -9, -30, 12, 22, 1, 2, -8, 24, 22, 5, 29, -11, 25, -13, 1, -22, -1, 11, 11, 12, 5, 14, 20, 13, 9, 2, 16, 10, 8, -30, -18, 8, 18, 23, -3, -11, 5, -30, -7, -12, 23, -19, 9, 13, -4, 29, 14, 2, 29, -30, 6, -14, 16, 26, 28, -9, -8, -4, 9, -7, 28, 20, -27, -13, 12, 13, -17, -14, 19, 7, 17, 14, -3, 15, 24, -25, 6, 29, 24, 21, -20, 12, -24, -21, -30, -18, -2, 22, 5, 2, 27, 4, 24, -21, 18, -4, 8, 11, -18, -2, -9, -22, 21, -6, 7, 10, 20, 9, -7, 15, -26, -4, 3, -24, -26, 29, -28, -8, -3, -6, 4, -28, 12, -19, 17, 2, -30, 14, 8, -1, 6, 19, -20, -30, 5, 5, 16, -9, 11, 25, 0, -24, 1, 18, 28, -5, -14, 7, -3, 18, -26, 26, 1, -19, -19, 14, 29, 25, 13, 6, 2, 19, 28, -10, 26, -29, -12, 7, -11, -18, 28, 1, 0, 19, 12, 20, -19, -7, -10, 5, 25, 19, 25, 2, 8, -5, -21, -7, -29, -19, -22, 7, -20, -8, 7, -28, -10, 25, -29, -13, 2, -1, -21, -1, 11, -20, 28, 18, -28, -22, 25, -29, -11, -20, 1, 3, -16, 20, -11, 20, -3, 9, -2, 25, 0, -27, 27, 11, -5, 24, -18, 18, 28, -2, 29, -7, 21, -5, 9, -13, -25, -14, 23, -15, 8, 15, 3, -28, 15, -23, 3, 13, -9, -7, 8, -6, -25, -16, 24, -23, 29, -7, -28, 15, 9, -18, -8, 17, 29, 25, -2, -17, -9, -12, 20, 15, -17, -18, 23, 2, -9, -7, 1, 18, 13, -11, -26, -13, 29, 7, -22, -16, -19, 7, 18, 19, 29, 0, 10, 21, -1, 25, 0, -13, 0, -3, 16, 13, -19, 1, -23, 13, -10, -18, -1, 13, -27, -30, 21, 7, -18, 27, 21, -30, -22, -29, 4, 21, 26, 12, -22, -12, -20, -20, 7, 7, -22, 17, -20, 19, -22, -22, -24, -16, 2, 6, -11, -19, 24, -25, 28, -12, -30, 20, -18, 29, 17, -20, -27, 18, -5, -11, -20, -15, 3, -4, 15, 2, -23, 16, -18, 21, 27, 7, 16, -24, -16, -14, 28, 29, -24, 20, -19, -18, 5, 17, 0, -29, 1, 26, 6, 17, -8, -7, -24, -30, -7, -29, -13, -20, 4, -7, 20, -13, -8, 19, 23, 20, 0, 2, -2, 27, 16, 2, -15, -10, -18, -24, 2, 10, -2, -23, -29, -9, 4, -10, -10, -10, -11, -28, -5, -21, 5, 6, -7, 17, 3, -25, 27, 28, -14, -2, -7, 18, 5, 16, -16, -29, 15, -25, -6, -16, -15, 5, -21, -12, 17, 17, 10, 16, 11, -28, 1, -16, -13, 9, -3, 3, 2, -15, 16, 10, -10, -27, 16, -18, 14, 6, 9, -6, -26, 23, 24, 28, 1, 27, -29, -13, -27, -22, -19, -10, -4, -26, 3, -26, 9, -6, -16, -15, -7, -21, 11, -5, 24, 28, 27, -13, 11, -12, 1, 15, -19, 20, 6, -11, 15, -10, 27, -8, 1, -12, -30, -9, -25, 26, 13, 6, -4, -13, -5, 0, 23, -4, -24, 14, 24, -13, -29, 24, 29, 19, 3, -15, 26, -23, -27, -23, 14, -17, 14, 0, 5, -19, -3, 27, 4, 20, 25, 15, 1, 26, -8, 22, 16, 9, 11, -16, 17, -6, 26, -21, -21, 14, 28, 28, 18, -9, -14, -9, -17, -4, -27, -5, -10, 23, -10, -22, -28, 25, 1, -2, 22, -3, 21, -4, 5, 17, 11, 20, -27, -24, 2, -21, -10, 1, -16, 13, -10, 2, -20, -21, 13, -5, 16, 4, -3, 27, 25, -12, -13, -5, -3, -22, 0, -18, 13, 23, -19, 4, 29, -20, -10, -26, 26, 20, -7, -16, -17, 0, -28, 10, -17, 24, 0, -17, 26, 15, -19, 28, 14, -19, 27, -7, -6, -19, -17, 29, 20, 9, 4, 22, -23, 0, 18, 12, -2, 6, -27, -28, -20, 8, -23, -1, 23, -16, 25, 25, 4, -30, 21, 12, -22, 17, -1, -28, -16, 3, 11, 8, -14, 11, -17, -4, -30, -23, 11, -10, 10, -27, 0, -4, -21, -26, -4, -20, 24, 2, 7, 28, 29, -6, -19, 29, 27, -28, 0, 2, -29, -3, -4, -15, 19, 18, 13, 21, -15, 18, 6, 8, 26, -23, -23, 13, -22, -11, -25, 3, -9, -22, -26, 12, 3, -27, -24, 0, 7, -10, -8, 6, -6, 10, -15, -11, 20, -28, 19, 1, 29, 24, -25, -3, -10, 26, 29, -19, 27, -14, -27, -27, -18, -8, -25, -7, 20, 11, -12, 9, -15, -1, -1, 8, -2, -1, -4, -20, 0, 3, 4, -24, 0, 15, 22, 15, 19, 8, -25, -30, -5, -24, 29, -19, 19, -28, 17, -8, 12, 28, 3, -25, -7, 7, 15, 13, -25, -12, -5, -18, -18, -3, 25, -25, -9, 22, -17, -29, 16, 28, 16, -24, 5, -8, -19, -10, 9, 14, -7, 12, 12, 6, -1, 0, 20, 18, -29, 11, -16, 0, 8, 6, -8, 8, 29, 29, 21, 29, 2, -8, 12, -16, 26, 21, 14, 13, 23, 7, -20, -26, -18, -23, 4, -12, 10, -30, -8, -20, -8, -19, -30, -16, -23, 25, -27, -10, -4, 27, 12, -30, 26, -15, -19, -27, -16, 23, -30, -28, 12, 9, 28, -28, 25, -1, 28, -4, -11, 6, 15, -22, -24, -12, 28, -17, -13, -29, 18, 9, -25, -21, -19, 6, 17, 29, -17, -1, -5, 29, -20, 16, 13, -12, 21, -4, 13, -17, -1, -26, 20, -29, -24, -14, 19, -30, 16, 5, 11, -20, 29, 23, -3, 7, -1, -21, -7, -19, -4, 22, -2, -5, 5, -2, 4, 4, -20, -13, -10, -28, 22, 27, -25, 11, -6, -11, 8, -10, -9, -16, -25, -28, 24, 2, -25, -10, -28, 25, -12, -11, 17, -9, -2, 2, 21, -30, -14, 8, 19, -9, 5, 2, 20, 26, -18, -4, 28, -4, 19, 20, 28, 17, 24, -18, 26, 1, -12, -23, -7, 3, 16, -7, -29, -16, 10, 21, 11, -9, 3, -7, -8, -3, 13, 22, -6, 24, -23, -25, -5, 13, -19, 9, 5, 23, 1, 11, 22, -4, -12, -26, 17, 22, 22, -20, -11, -10, 10, 4, -28, -11, -5, -25, -18, 17, -13, 23, 0, -19, 23, -24, 12, -19, -15, -1, 7, 3, -8, -25, -30, -22, 4, -9, -26, -23, 22, -17, -1, -10, 11, -25, -16, 25, -23, -30, 13, 3, 14, 9, 21, -4, -2, 27, 8, 17, 15, -12, -23, -8, 24, 2, 26, 13, 3, 20, 25, 17, 16, -22, 29, -22, -24, -5, -14, -1, -21, -30, -14, 4, -2, 24, -6, -7, 8, -29, -23, -13, 4, 8, 4, -12, 1, 19, 4, -16, -2, 11, 13, -15, -28, 9, -17, -4, -6, -17, 15, 22, -21, -6, -5, -18, 20, -15, -29, 10, -17, 25, -19, 17, 27, -7, 16, -13, -26, 11, -10, -25, -4, -18, -13, 26, -6, 28, 14, 22, -12, -17, 7, 19, 15, -21, 22, -30, 11, 11, -4, 4, -7, -15, 11, -11, 21, 21, -23, 18, -4, -30, 10, 10, -7, -16, 19, -24, -2, -26, -4, -10, 2, 24, 13, 5, -27, 29, -14, 13, 24, 26, -20, 21, -17, -25, 15, 1, 5, 0, -22, -8, 12, 28, 22, 1, 26, 22, -8, 22, -12, -28, -21, -26, -23, 21, -22, -15, 14, 28, -26, -27, -14, 3, -1, 13, -23, -14, 2, 23, 16, -24, 12, -10, -20, 17, -27, 15, 18, -26, 27, 24, 3, 26, 6, -16, -28, 26, -7, -22, -5, -1, 24, 16, -3, 5, -21, -25, 7, 8, 22, 25, -8, -25, -2, 4, -19, 16, -25, 14, 26, -26, 8, 27, 21, 16, 29, 22, 12, -9, 28, 1, 18, -5, 16, -28, 18, 20, 10, -24, 13, 7, -11, -3, -4, 8, 21, 17, -17, -14, 5, 1, 29, 10, 2, 15, 0, 25, -12, 14, 16, -15, -19, -19, -24, 28, 24, 23, 24, 28, 24, -5, -17, -29, -30, 29, -11, -25, 21, 25, 10, -17, -23, 12, -9, -20, -2, -13, 29, -2, 8, -17, 16, 7, -4, 27, -3, -10, -30, 3, -4, -6, 27, -11, 25, -18, 9, -12, 10, 25, 25, -7, 6, -13, 9, 0, 25, -26, 24, 15, 1, -17, 3, -4, -19, 17, 2, 22, 28, -23, 24, -30, -11, 25, -15, 2, 27, 10, -18, -3, 12, 13, -2, 9, -29, -11, -13, -1, -26, 23, 23, 21, -14, -4, -21, -17, -6, -8, 7, -29, 11, -28, 28, -2, -8, -3, 10, -23, 12, 9, -19, 2, -15, -13, 24, -24, 28, 14, -11, 14, 19, 8, 12, 6, -19, -27, -13, -17, -12, 19, 23, -7, -27, -12, -11, 13, 3, -5, 24, -26, -30, -15, -10, -22, -24, 12, -10, -17, 7, 22, -22, -3, -29, 11, -1, -12, -7, 21, -21, 8, 22, -10, 1, -27, -11, 13, -1, -2, 9, 10, 1, -10, -5, 22, -2, -27, 6, 14, 13, 17, -2, 29, -13, 8, 15, -26, -21, -5, 20, -19, -17, 22, 28, 9, 26, 7, 21, 27, 15, 21, -13, 2, -23, 28, -16, -6, 16, -21, -22, -1, -19, -13, -5, 11, -14, -9, 25, 16, 21, -18, -3, 13, -5, -24, 19, -15, 13, 14, -21, -30, -17, 5, 19, 27, -14, -6, 29, -5, 12, 3, -15, 3, 1, 4, -20, 3, 25, 3, -8, 22, -19, 6, -8, 13, 0, 20, -2, -11, 15, 10, -20, 5, -23, -8, -27, -30, 14, -27, -22, -10, 14, -17, -22, 12, -14, -23, -30, 2, -6, 14, 29, 27, 14, -13, -16, -24, 16, -11, -14, -19, 8, 12, -12, -3, -28, 9, -12, -6, -19, 15, 19, 27, -22, -2, -27, -8, 16, 25, 25, -12, 11, -11, 7, 3, -3, -13, -5, 4, 25, -21, 26, -30, -20, -12, 23, -2, -8, 20, 4, -25, -4, -4, 28, 26, 4, 0, -13, 26, -26, 25, 17, -7, 15, 29, -29, 18, 6, 17, -1, 3, -6, 6, -5, 15, -26, -11, -15, -1, -23, -27, -10, 9, -29, 23, -11, -18, -3, -7, 23, 19, 21, -27, -2, -7, -26, -8, -29, 29, 0, 23, -19, 1, -29, 26, -9, 12, -10, 0, 6, 14, 7, 19, -23, -19, -22, 21, -18, 13, -25, 9, -10, 6, -23, -16, -20, 27, -11, -9, 26, -25, -8, 5, -3, 12, 12, -17, 1, 25, -6, -20, 26, -19, 2, 20, -7, -26, 12, -19, -2, -3, -4, -20, 15, -9, -19, -22, 2, 28, -2, 11, -3, -20, -11, 24, -29, -10, 22, -19, 10, 7, 28, -22, -12, 5, 19, -9, -21, 5, 2, -28, 23, 18, -17, 18, -2, 26, -26, -20, -18, 16, 3, 6, 7, -16, 24, -20, 27, 1, -13, -4, -7, -27, 1, -11, -26, -10, 9, -24, 23, 24, 19, 17, -9, 22, -28, 0, -4, -29, 11, -18, -13, 11, 11, -26, 21, -28, -19, 16, 17, -1, 21, -3, -1, 11, -12, -18, -18, -1, 27, -9, -13, -7, 29, -11, 28, -29, -20, 16, -24, -1, 26, 7, 16, 28, -18, -3, -18, -13, 24, -12, 21, -12, 27, -14, 22, 1, 26, 24, 22, 13, -28, 12, 6, 15, 29, -29, -16, -10, 1, -9, 27, -23, 8, 23, 10, -20, -20, 29, -6, 26, 8, 17, -5, 14, 17, -20, 21, -28, 11, -8, 20, 17, 1, 7, 25, 3, -18, 28, 0, 27, -11, 17, 12, -26, -28, 3, 2, 7, -11, 29, -2, -21, 26, 23, -22, 23, 19, -5, -27, 15, -2, 13, 25, -20, -29, -15, 18, 8, 14, -21, -24, -30, -29, -6, -9, -20, -28, 3, -14, -3, 25, 12, 1, -16, 1, 1, -20, 8, 21, -1, -23, -18, 8, -12, 1, 5, 15, -12, -27, -30, -14, -3, -4, 14, -22, -17, 29, 21, -3, -22, -13, 5, -11, 16, -9, -20, 18, -16, 19, 29, -16, 17, -26, 10, -19, -15, -12, 11, -17, -11, -20, 21, 21, -26, 12, 5, 25, -18, 29, 17, -29, 25, -27, -7, 8, 11, -15, -12, -19, 27, -19, 6, -1, 3, 23, -6, -8, 23, -2, -15, -3, -20, -11, -23, -28, -7, 12, -15, 1, -8, -16, 22, 9, 3, -16, 11, 10, -25, -25, -26, -14, 11, 0, -22, -7, 18, -12, 26, -14, -2, 19, -28, 4, 29, -16, 15, 11, -22, -13, 11, 7, -2, -23, 18, 3, -7, -17, -16, 23, -29, 16, -8, -28, -21, 17, 14, -24, -13, 18, 3, -25, -5, -17, -1, 20, -19, 28, -2, 6, -22, -13, -30, 16, -22, 9, 28, -25, 14, 16, 27, 7, -13, -16, -14, -20, -28, -12, -14, 4, 16, -16, 7, -18, 26, -30, -4, -7, -30, -7, 19, -12, 1, -26, 0, -18, -10, -22, -19, 27, -21, 18, -22, -22, 7, -29, -18, -27, -11, 1, 14, -5, 19, 15, 20, -20, 18, -13, -23, -4, -23, -16, 21, -18, 26, -14, -25, -17, -12, -25, -5, 28, -29, 28, -21, 1, -10, -30, -2, 4, -5, 28, -14, 2, -22, -14, 26, -23, -28, -4, -20, 3, -13, -12, 12, 15, 26, -7, 12, -6, -23, -7, 0, -11, -13, 8, 7, -22, 22, 13, 25, 5, 18, 0, 13, 14, 15, 29, 15, 12, 3, -5, 14, -20, -26, 6, -12, -28, -28, 4, -2, 27, -2, -17, -13, 7, -25, -28, 6, -13, -10, -5, -8, 11, -3, 23, -16, 10, -24, -15, -15, 6, 28, 13, -18, 22, -25, -7, -3, -23, 16, -25, 7, -29, 2, -14, -27, -22, -2, 21, -17, -5, -6, -12, -27, -9, -8, 24, -21, 22, -13, 29, 3, 19, 10, -22, -29, 21, -13, -6, -24, -3, -20, 22, 9, 8, -28, 8, -11, 6, -4, 3, -4, -10, -2, -23, -14, -3, -24, 26, 6, 6, -22, 10, 18, -2, -22, 14, -20, 29, -28, -25, -16, -23, 29, -15, 21, 8, -22, 0, -5, -26, 10, -30, -29, -19, -14, -15, -5, -8, -27, 18, 4, 26, -9, -29, 24, -8, 11, -19, 6, 3, -4, 3, -6, 1, 22, -12, 15, 18, -26, 11, -1, 2, 20, 14, -11, -18, -26, -8, 14, 13, 22, 5, -14, -22, -7, -4, -21, 20, 6, 7, 29, -7, 13, 19, 5, -28, 13, -17, -1, 6, -26, -16, -18, 16, 9, -1, -26, 3, 10, -5, 13, -16, 19, -11, 16, 19, -17, -6, -13, 3, 22, 8, 20, -15, 21, 21, 13, 7, -10, -3, 20, -20, -19, 28, 27, -6, -14, 25, -15, 3, 18, -27, 15, -4, 25, -25, 2, -17, -17, -28, -30, -27, 28, -24, 25, -22, 3, 13, 10, 6, 18, -12, -5, -12, -29, -26, -22, -8, -18, -9, 7, 14, -22, -13, 5, 26, -28, -1, -24, -4, 8, 7, 14, -30, -21, -10, -30, 12, 18, 14, 4, 8, 24, -6, -8, 22, -21, -2, -13, -28, -3, -25, 4, 20, -21, -28, 25, -26, -25, -23, 5, 1, -30, -6, -22, -15, -28, 4, 4, -1, -24, -12, 8, -2, 13, 1, 7, 19, -9, 17, -5, -20, -7, -21, -12, 25, 19, -24, -27, 27, -29, -19, -2, -18, 20, 13, -17, 23, -29, 12, -30, -9, -28, 8, 5, 24, 17, 19, 12, 6, 2, -10, 9, -8, -1, 1, 27, 9, -17, -8, 8, -2, 11, -2, -5, 0, -28, 25, -29, 19, -25, -24, -30, 29, 1, 17, 16, 13, -5, 29, -26, 15, 24, -3, 25, -30, 2, 15, 12, 5, 23, 24, -17, -21, -4, 14, 12, 9, 13, 17, 12, -2, 24, 2, -6, 12, 5, 11, -17, -18, -29, -5, 10, 18, -11, 15, -20, -4, 16, 26, 28, 24, 0, -22, 17, -15, 25, -4, 28, -17, 3, -2, -23, -28, 27, -5, -25, -13, 11, -25, 0, -18, 22, -27, -12, 11, 21, -4, -3, 2, -27, -18, -17, -7, -8, -8, -22, 25, 7, -9, 5, -19, 14, 7, 27, 9, 8, 19, -7, -20, 15, 24, -18, -9, -8, 19, 28, 18, 28, 11, -28, -30, -26, 7, -11, -22, -24, 26, 14, -22, -2, 20, -27, 18, 26, -3, 13, 20, -26, 18, -12, 2, 29, -9, 26, -25, 12, 21, 17, 16, 26, -12, -14, 2, 26, 17, -10, 11, -27, 23, -18, -30, -11, 0, -3, 1, -9, -18, 23, 10, -22, 4, 11, -30, 13, 7, -21, -26, 11, 15, 24, -5, 11, -21, 1, 26, 18, -24, 25, -2, 1, -29, -23, -13, -16, -11, -20, -21, -23, -11, -15, 14, 3, -12, -1, 22, -1, -11, 21, 13, 12, -4, 17, 9, 27, 29, -13, 3, 10, 28, -1, -24, 14, 23, -21, -23, 28, -20, 16, -15, 28, -9, -4, -15, 26, -12, 4, 20, 6, 16, -12, 16, 16, 22, 4, -7, -2, -10, 4, 21, -19, 12, 16, 18, 6, -14, -24, 25, 12, -3, -5, -4, 8, -5, 11, -17, -6, -17, 2, -2, 20, -28, -15, 23, -2, 23, -23, 19, 8, 1, 26, -28, 15, 18, 23, 23, -14, 20, -7, 27, -27, 8, -22, 3, 5, -30, 10, -9, 4, 24, -7, -6, 25, -18, -17, 1, -5, 25, 3, 20, 5, -14, 7, -25, -15, 20, -10, 7, -25, -2, 9, 19, -17, 20, -24, -3, 4, 22, -18, -26, 23, 9, 24, -25, -29, -19, -30, 27, -2, 18, 15, -26, -19, 29, -30, 23, 7, -20, 15, -6, -8, 24, -27, -20, -27, -13, 7, 24, 7, 10, -7, -8, 28, 4, -8, -3, 3, -7, -30, -4, 12, 19, -7, 23, -3, 11, 12, 16, -8, 12, -13, -16, 4, 17, -1, 11, -8, 4, 13, -12, -29, 7, 13, -12, -25, 22, 13, 13, 29, -29, 8, 29, 17, 28, 5, 14, 23, 8, 19, -19, 13, -27, -30, -6, 13, 25, -27, -1, -21, 10, 15, -17, -4, -8, -18, -25, 2, 11, 10, -11, 29, 3, -25, -18, -4, 5, -30, -10, -24, -1, 1, -29, 25, 23, 19, -10, -19, 28, -18, 29, -1, -1, 5, 18, -29, 10, 26, 29, -20, -21, 9, 15, 19, -4, -25, -9, -23, -2, -3, 26, 0, 0, -19, 29, -24, -10, 12, 22, -26, -9, 15, 29, -9, 9, -1, 14, -6, 20, -13, 9, -13, 18, 20, 14, -25, 22, -23, 27, 25, -21, -2, -29, -8, -16, 8, 20, 16, -6, 7, 13, 11, -26, -14, 15, -24, 1, 0, -26, 26, -2, -8, 8, 23, 0, 20, -10, 2, 29, 4, -17, -13, -17, 13, 10, -6, 6, -23, 20, 18, -9, -11, 27, -16, -5, 14, -2, 29, -1, 18, 18, -8, -15, -2, 2, -23, -7, -11, -1, -22, -26, 24, -18, 17, -8, -30, -16, 20, 14, 20, 17, -15, 20, 9, 20, -20, 13, -12, -10, 15, -17, -17, 10, 6, -4, 16, -10, -7, 16, -22, 25, -4, 9, 25, -3, 8, 12, 12, 27, 16, -15, 9, 18, -25, -25, 1, 16, 25, 13, 17, 29, -11, 5, 23, -30, 14, 28, 22, 9, -30, 3, -15, 29, 24, -11, 28, -8, 26, -11, -24, -27, -5, -14, 29, -5, -4, -10, -25, -28, -6, -15, 18, -11, -16, 25, -20, 19, 26, 16, 28, -5, -15, -28, -4, -15, 7, 22, 0, 22, -21, -19, -18, 9, 26, -2, -1, 10, -27, 11, 12, 15, 19, -27, 22, 23, -25, -13, -28, 19, 24, 20, 3, -17, 25, 1, -8, 3, -17, -20, 5, 12, 18, 25, -2, -9, -13, 4, 11, 17, -15, 4, -28, -7, -12, -17, 14, 18, -1, 2, -4, -16, -26, -11, 1, 18, 15, 8, -8, 18, -10, 8, -8, -26, 8, 17, -5, -4, -12, -27, -18, -19, -27, 26, 10, -30, 29, 10, -15, -17, -8, 28, -24, -27, -30, -14, -15, 8, 2, 18, -14, 26, 14, 10, -4, -13, -4, -7, 15, -18, -24, -16, -10, 9, 5, 21, -28, 18, -18, -1, 2, -26, 9, -17, -12, 18, 22, 4, -16, 0, 27, -21, -20, -21, 5, -3, 21, -24, -21, 25, -8, 11, -22, 29, 10, 2, 8, 17, 8, 21, -13, -28, 12, 14, -30, -23, 23, -29, 12, -10, -20, -29, -11, 15, -29, -6, 17, -11, -5, 9, 0, -23, -5, -22, -8, 13, -29, 28, -2, 14, 27, -21, 20, -11, 20, -16, -16, -24, -17, 1, 19, -28, 16, -4, 21, -30, 3, -7, 24, -1, 27, 9, 20, 13, 19, 11, 13, 23, 15, 18, 17, -4, 24, -19, 17, -27, 16, 20, -13, 5, 14, -14, -2, 7, 12, -11, 26, 20, -28, -22, 16, -26, 4, 9, 14, 16, -10, 18, -8, 11, 12, -19, 2, 19, 17, -26, 10, -24, 11, 21, -18, 12, 28, -4, -17, -14, 4, -21, -19, 16, -7, -28, 7, -2, -30, 25, -29, 28, -6, -21, -21, 18, -7, 29, -13, -29, 16, -30, 23, 27, 22, -8, -2, -11, -1, 6, 5, 14, -7, -7, 13, 24, 19, -13, 8, -24, 6, 20, -9, 11, -4, -14, 25, 15, -5, -27, -20, 11, -18, -15, 20, 17, -25, -15, 6, -28, -19, 28, -4, -22, -2, 13, 23, -22, -18, 15, 10, -25, 3, -13, 17, 7, 16, 24, -6, 7, -16, 14, -16, -23, -9, 19, 6, -2, -4, -9, -21, 13, -25, 14, 15, -2, -28, 16, 23, 16, 10, 4, 27, -8, -19, -6, 1, -22, -23, 20, 21, 13, -25, 16, -16, -29, -13, 4, -25, 3, -4, 7, -16, 4, -23, 8, -16, 3, 26, -19, -8, 4, 10, 7, 2, -18, -12, -4, 28, -27, -11, -18, -24, -26, -4, 10, 11, 10, -15, -19, 8, -13, -20, -15, 2, -8, -13, -21, 26, -24, -13, -30, -30, 15, -8, -22, -6, -23, -11, 2, 18, -24, -2, 10, 6, 5, -12, -11, 10, 18, 18, -19, -11, 15, -9, -4, -12, 23, 1, -27, -23, 10, -10, 0, -25, 22, -2, -9, -19, -10, 27, 28, -18, -5, 28, 8, 11, 22, -2, 5, -16, -9, 18, 24, 3, 2, -3, 29, -21, 27, -14, 0, 29, 8, 12, -14, -8, 3, -4, 17, -30, -19, -18, -7, 8, -18, 5, 16, 15, -22, -22, -23, 2, 21, 21, -22, -12, -3, 22, -1, 2, 26, -5, -9, 22, -1, -13, -22, 23, 3, -28, -12, -27, 8, -18, 18, -26, 1, -8, 27, 2, -8, 8, 26, -16, 24, -26, -5, -11, -21, -1, 8, -6, 26, 24, -30, 28, 6, 26, -3, -8, -19, 24, 4, 6, -18, -20, -25, -3, -16, 24, -9, -12, -11, -23, 24, -7, -16, 11, 19, 15, 0, -5, 1, -25, -12, -28, 5, -9, -19, -22, -22, -6, 15, -26, 14, 5, 0, -21, -17, 19, 14, -22, 14, -19, 8, 0, -18, -27, -7, -2, -25, 28, -23, -14, -19, -12, 24, -27, 5, 19, -27, -27, 14, 22, 24, 0, 8, 6, -16, -4, -1, 8, 5, -26, 7, 25, 13, 1, 29, -7, -26, 12, 25, 18, 5, -20, 14, -19, 2, -23, 24, 8, -27, -27, 9, 11, -25, -6, -24, -7, 13, -7, 14, 19, 27, -1, 14, -28, -3, -23, -23, -27, 29, -20, -21, 11, -12, -18, 1, 3, -26, -15, 8, 26, -19, 17, -6, -26, 14, -18, 9, -6, 28, -20, 1, 1, 5, 23, 16, -9, 15, 23, -11, -16, 12, 20, -4, 29, -13, -25, -2, -6, 0, 9, -24, -6, -22, -26, -10, 2, -1, -10, -5, 25, 12, -24, -8, 26, -8, -4, 7, -16, -20, 11, -10, -22, -11, -18, 8, -22, -2, 16, -20, 25, 4, -24, -4, -26, 15, 15, -19, 12, -1, -1, -30, -14, -18, -6, 11, 12, -10, -9, -3, -20, 19, 13, -1, -4, 2, -21, 27, 20, 15, 3, 12, 25, 21, -1, 20, -25, 25, 4, 22, 20, -25, 29, 23, 25, 17, 9, -3, 18, 28, 15, 16, -17, 8, -30, -7, -26, 16, 23, -30, -26, 12, 18, -11, -19, 29, 11, -28, 29, -9, 9, -26, 29, 17, 24, -24, 14, -15, 29, -17, -22, 2, 22, -10, 6, -20, -19, 20, -29, -9, -3, 15, 11, -11, -16, 16, -15, 22, 25, 25, 21, -6, -17, -27, -5, -18, -17, -9, 9, 15, -2, -28, -4, 20, 6, 22, 15, -10, 6, 12, -20, -1, 19, 16, -3, -11, -18, 1, -17, -19, 12, 18, -3, 9, 4, -30, 23, 14, 11, -6, 2, 22, 16, 13, 9, 9, 20, -3, 23, 11, 6, -24, 8, 0, 19, 28, 7, 24, 6, 19, -20, -1, 2, 18, 10, 16, -25, 18, -28, 21, -28, 27, -22, 15, -8, 6, 5, -17, 12, -27, -5, 22, 24, 29, -20, -18, 14, -1, 24, 11, 3, 7, 3, 18, 21, 7, 1, -16, -7, 17, 8, 18, -30, -30, 27, 1, -7, 26, -25, 5, -27, 8, 5, 8, 24, 14, -21, -12, 27, 25, 14, -19, -22, 5, 6, -29, -1, 12, -12, 24, 28, 18, 12, 7, -7, -19, 26, 28, 12, -10, -21, -30, 25, 0, 14, -12, 22, 0, -18, 12, -8, 7, 28, 11, 28, -19, -27, -30, -16, -30, 13, 21, -5, -30, 22, -30, -20, 9, 16, 25, -8, -18, 7, -20, -6, -17, 7, -30, 19, -5, 13, 1, -5, -17, 18, 16, 2, 14, 1, 2, -6, -5, 18, -18, -18, -7, -30, 13, -16, 3, -29, -1, 21, 26, 28, -27, 13, 24, -22, 27, 18, -29, 6, 26, -23, -29, 8, 24, 7, -4, 21, -11, 19, -19, 5, -25, -2, 18, -1, 2, 13, -6, 4, 7, 3, -7, 20, 22, -6, 11, 5, -2, 15, -9, -8, 16, -10, 6, -30, -16, 28, -3, 14, 22, 16, 19, 16, -27, -10, -18, 2, 6, -1, 6, 20, -12, -21, -29, -29, -5, 12, -19, 6, 3, -22, 3, -27, -9, -23, 12, -12, -17, 4, 19, 0, -8, -7, 28, -19, -9, 2, -23, -10, -18, -23, -5, 13, 10, -17, -26, -20, 28, -20, 8, 11, -5, -20, 29, 29, -12, -8, -11, 6, -2, 14, 23, -26, 21, 26, 23, 22, 6, 26, 22, 20, -20, -11, -7, -27, -6, -19, 28, -8, -23, 2, -18, 23, -7, -28, 23, -23, 1, -3, -15, 20, 20, -3, -12, 12, -17, 2, 5, 13, -7, -29, 22, -25, 21, 13, -12, 14, -29, 5, 29, -26, -6, 10, -22, 5, -2, -21, -2, 17, -30, -7, -3, 22, 22, 6, -9, -9, 0, -5, -25, -3, 28, 8, -28, 20, -19, 17, 21, -6, 6, -26, 27, -17, -1, 20, -12, 23, 13, 3, 21, 8, 1, -29, 7, -20, -6, 3, -28, 11, -26, -5, 14, -15, -24, -7, -6, 6, -9, -29, 27, -17, -22, -20, 4, -17, 3, 15, 4, 8, 11, 18, 17, 19, 1, -27, 29, 6, -21, -25, 22, 19, -30, -6, -20, -21, 28, 23, 5, -10, -27, -27, 25, 27, -17, 11, -20, 1, 4, 5, 14, -9, 27, -17, -14, -27, 22, -23, 6, 28, 18, -1, 22, -4, 28, 26, -1, 14, -7, -19, 28, 16, -14, 29, 5, 29, -14, 11, 9, -27, -4, 12, 18, -30, -15, -25, -17, -25, -1, 16, 10, -6, 13, -23, 4, 5, -24, 29, 26, -30, -6, -17, 25, 27, -7, 4, 11, -16, 15, 13, 13, 20, 17, -24, 25, 12, -26, 21, -3, 12, -30, -18, -11, -28, 16, -9, -29, 19, 18, 29, 21, -6, -5, 21, 9, 3, 4, 13, 25, -2, 0, 24, -12, 13, -11, 15, -26, 5, -11, -23, 8, 9, 13, -5, -20, 22, -10, 0, -8, -19, -19, -24, -7, -8, 1, -4, -4, -11, -11, 19, 16, -30, -1, -11, -4, -23, 17, -29, -11, 13, -2, 27, 3, 9, 5, -22, -11, 25, -1, -15, 26, 28, 19, -18, -8, 3, -25, -29, 21, 21, -7, 29, -15, -8, 24, 14, 19, -16, -7, 13, -22, 20, 10, 8, 3, 1, -13, 0, 26, 11, -16, 21, -29, -1, -14, 1, -12, -17, 22, 14, -24, -10, 20, -8, 23, -9, 14, 5, 29, -23, 5, -7, -11, 18, 29, -10, 8, 16, 25, -3, 18, -11, 4, -20, 17, 19, -12, -29, -8, 28, 29, 2, -21, -28, 6, -28, -6, -3, -19, -27, -13, -3, 1, -1, 6, -9, -26, 20, 9, 11, 24, -27, -7, -16, -9, 26, 24, -13, -1, -7, 27, -2, 29, 5, 24, 20, 19, -24, 14, 1, -22, 7, -15, -9, 25, -22, 10, -29, -3, -17, -5, 13, -25, 7, -29, 14, -26, 16, -27, -20, 0, 27, -4, 5, 29, -3, 4, -6, -1, 18, -21, -13, -28, 10, 19, -24, 13, -13, 27, -14, 10, -3, 25, 27, 20, -19, 24, 8, 13, 29, -28, -6, 12, 28, -4, 29, 25, 14, 2, -27, 14, -12, 5, 15, 11, -22, -28, -13, -28, -2, -13, -12, 26, 29, 17, 1, -10, 17, -15, 15, -6, 13, 21, 16, -3, -10, 12, 10, 18, -14, -29, -14, 27, 15, 13, -19, -12, 15, 21, -3, -12, 11, -19, 17, -23, 27, -23, -18, -3, -16, 21, 29, 24, -23, 27, -10, -4, -17, -19, -8, 11, -13, 27, 8, 8, 27, 6, 21, 19, -30, -27, 17, 23, -6, 13, 17, -14, -8, -3, 18, 28, -23, 7, 28, 24, 19, 7, -3, -24, 8, 9, 3, -28, 17, 17, 4, 19, 27, -28, -29, 6, -18, -30, -8, 26, -18, -4, -28, -15, -13, 22, -18, 9, 22, -3, -30, 0, -23, 28, 17, -25, 25, -6, -24, 10, 6, -30, -23, 6, 25, -9, 23, 3, -21, -11, -27, -10, -24, -20, -14, -18, -12, -16, 6, -6, -7, -30, 17, -5, 11, 4, -19, -20, 1, -25, -11, 20, 4, -26, -8, -9, 15, -9, 9, -30, 9, -3, -3, -27, 20, 11, 19, -17, -17, -25, -7, -20, -1, -25, 12, 18, -13, -20, -11, -17, -6, -27, 11, -21, -26, 17, -19, -17, 14, -24, -11, -2, -23, -16, -1, -4, 26, -9, -24, 5, -27, 10, -4, 3, -20, -14, -30, 0, -1, -17, 15, 11, -6, 10, 6, -14, -24, 22, 11, -6, 21, 11, -1, 27, -30, 23, 8, 27, 21, 14, -5, 2, 21, 26, -10, 11, -1, -11, 21, 26, -18, 23, 4, -15, -22, -9, -9, 18, 15, 21, 6, -16, 22, -30, -5, -10, -4, 21, 16, -26, -17, -21, 21, 9, 11, 6, -12, 0, 9, 14, -4, 15, 25, 17, 3, -10, -27, 25, -28, -2, -6, 12, -13, -23, -5, 7, 2, 26, 28, -24, -30, 20, 10, -1, 27, -13, 8, 15, -3, 10, -13, -21, 18, 11, -5, -28, 1, -4, 9, -1, -18, -18, 9, 5, 18, -7, 13, -11, -2, 12, -27, -11, -26, -9, -2, -24, -5, 11, -6, -27, -10, 17, -22, -21, -3, 19, -24, -27, -11, -4, 16, -11, 10, -1, 8, 12, -26, -16, 0, 0, -4, 15, 19, -17, -19, -10, -19, -24, -14, 13, 27, 16, 18, -27, 5, -1, 28, -30, -8, -24, 24, -6, 3, -29, -26, -24, -28, -21, 3, -18, -25, -11, 13, -25, -14, 12, 23, 28, 25, -7, 5, 6, -5, 15, 2, 1, 27, 5, -9, 21, 3, -23, -11, -22, -2, -19, 27, -10, 24, 19, -26, -22, 12, -2, 19, -27, -21, 25, 27, -6, -12, -19, 22, 21, 25, -1, 5, -9, 23, 18, 6, -13, 26, -5, -18, 24, 12, -8, -18, -8, 8, -21, -17, 22, -15, 13, -29, -4, 29, -11, 10, 25, 12, 23, 11, -1, -16, -29, 8, 21, -22, 23, 20, 16, 0, -22, 15, -16, 2, -14, 29, 2, -23, 1, -11, 20, 7, 20, -18, -11, 20, 25, -17, 19, -15, -17, -10, 27, 3, -4, 3, -16, -4, -25, 11, 21, -18, 24, 5, -11, -19, 5, 2, 21, -22, 10, 25, 25, 25, 17, -26, -29, 5, -11, 2, 5, 13, -26, 1, 2, -1, -22, 24, 29, -4, 6, -26, -3, -1, 3, 15, -15, -22, -12, -20, 28, -1, -26, 2, 17, 21, -5, -8, 6, 23, -28, -27, 19, 6, -6, 27, -1, -2, 12, -2, -2, -9, 24, 5, 21, 3, -25, 27, -2, -16, -17, -10, -16, -4, -12, 9, 24, 21, -17, 21, -21, 18, 3, -23, -7, 5, -19, -5, -10, 7, 3, -21, 8, 6, 29, -4, 8, -6, 2, 22, -18, -10, 6, 14, 16, -2, -3, -15, 2, -26, -17, -9, -15, 18, 22, -3, -13, -14, -2, 15, -24, 6, -1, 20, 15, -5, 14, -21, -10, -24, 2, -6, -9, -29, -2, -1, 4, 24, 21, -17, 3, 12, 5, 20, 1, -16, -4, 19, -23, -23, 17, -7, -12, -20, -16, 24, 18, -22, -19, -14, -12, -26, 19, -9, 14, 4, -7, 8, 9, 10, 27, 2, -13, 10, -1, 7, 26, 27, -2, -9, 6, -7, -26, 27, -6, -12, -23, 19, 15, 5, 12, 9, 18, -13, 15, 12, -9, -27, -7, -9, 10, 29, -28, 7, -2, -6, 7, -25, 13, 23, 4, 26, 8, -9, -11, 20, 23, 2, -6, 2, -15, -14, 22, -27, 26, 12, -16, -2, -2, -23, 17, 3, 12, 24, 15, 22, 15, -18, -4, -26, 1, -11, -3, -9, -7, -17, 10, 12, 15, 3, -19, -3, 3, 2, 15, 28, -30, -1, 26, -4, -1, -30, 5, 23, -10, -21, -15, 10, -4, -5, -2, 3, 18, 24, -18, 23, -17, -15, -18, -9, -4, -19, -16, -25, -21, 25, -20, 23, -18, -2, -13, 14, -6, -2, -19, -23, -28, 18, 14, -21, -21, 25, -10, 5, 20, 0, 11, 26, -12, -4, -4, 28, 28, -2, -1, 9, 19, -2, -9, 2, -15, 5, -19, 2, 23, -30, -27, -28, -21, -22, 10, -26, -18, 7, -6, -2, -11, 9, -9, -1, -3, -26, 16, -27, 7, -30, 24, 2, 4, -29, 21, 1, -17, -22, -16, -2, 13, 5, 22, -28, 10, -18, -16, -25, -25, 6, -8, 17, 18, 16, 23, -2, -30, 29, 10, -23, 8, 27, -15, 15, 3, -26, -25, 29, 1, 3, -14, 18, 26, 5, -26, -19, -30, -30, 29, -7, -17, 14, -18, 26, -11, 20, -9, 20, 23, 6, 14, -16, -21, 27, -6, 24, -26, 20, -24, -4, 6, -9, 4, 3, 28, 0, 7, -29, -18, -9, -16, 12, 2, -17, -18, -16, -23, 25, 6, 9, -28, -24, 4, -14, 1, -7, 4, -17, -26, 17, 28, -17, 22, -18, -3, -28, 1, -9, -24, -10, -17, 16, -2, 13, -16, -3, 24, -27, 11, 19, -11, 20, -27, 21, 21, 3, -23, 18, 3, -17, 3, 24, -1, 27, 18, -15, 7, -6, -29, -10, -3, 10, -16, -3, -5, 4, -15, -29, 6, 20, 22, -5, -9, 18, 17, -17, -25, 19, 16, 21, 15, -26, -24, -30, -10, 6, -23, 7, 3, -4, 19, -27, -10, -9, -15, 10, 14, -24, 12, 6, 9, 5, 15, 20, 13, -26, -22, -17, -4, 22, -15, 6, -1, 0, 7, 20, -21, -26, 4, 28, 29, 7, 29, 23, 16, -15, 11, -12, 28, -6, -13, 14, 2, 8, 29, -25, 7, 13, -21, -23, 15, -4, -16, 17, -4, 10, 23, 11, 24, 9, 6, 13, 20, -21, -23, -11, 21, 4, -7, -2, -27, 25, 13, 3, 27, -19, 12, -22, 25, -18, 11, -12, -16, -19, 6, -6, -30, -12, 19, -22, 28, 9, 28, -8, 25, 15, -29, -9, -17, 16, -9, -4, 21, -30, 5, 25, 2, 28, 17, -1, -2, 6, -13, -22, 20, 18, -18, 10, 20, 9, -11, 15, 10, 4, 17, 27, -13, 28, -13, 5, -3, 29, 14, -18, -9, -4, -3, 15, 14, 17, 3, -11, 3, -28, -16, 9, -23, -7, 21, 8, -21, 25, -17, -28, -11, -4, -7, -26, -5, -3, -4, 22, 3, -16, 9, -3, -9, -28, 11, -6, 16, -24, 6, -7, 19, 25, 28, -14, 9, 27, -14, -15, -18, -26, -14, -25, -10, -9, 13, 23, 5, 2, -8, 16, -19, -5, -27, 20, -21, -10, 24, 26, 2, -23, 3, 10, 15, 1, 20, 25, -26, -24, 1, -16, -29, -23, -26, 4, -22, 7, -12, -8, -19, 16, -19, 21, 19, 7, -11, 7, 9, -15, 28, -5, -19, 12, -6, -20, 11, -6, -20, -11, 26, 28, 19, -3, -21, 0, 11, 13, 14, -9, -5, 28, -2, -17, -11, -26, -18, -24, -18, -17, -23, -3, -9, -4, 14, -30, 25, 15, 15, -9, 12, 15, 4, -20, -16, 16, 21, -9, 25, 12, 21, 14, 5, 4, -27, -2, -14, 2, 25, 7, -9, -21, -15, 15, -15, -2, -16, 5, 11, 3, -2, -30, -5, -17, 21, 26, 14, -11, -5, -7, -20, 2, -5, 22, -6, 11, -27, -6, 29, 15, -1, -30, -12, -25, -5, 11, 6, 13, 11, -2, -15, 13, -23, 23, 27, 5, 27, -29, -29, -27, 18, -5, 8, -29, -20, 26, 15, 13, 19, -30, -5, 19, 6, -5, -14, 20, -5, -16, 18, -15, -5, -19, -19, 13, -17, -8, 11, -11, -28, -12, -25, -1, -3, 9, 13, 11, -28, 2, -5, 2, -17, -13, -26, -21, -19, 16, 24, 4, 3, 6, 4, 14, 20, -29, 11, -11, -27, -22, 22, 18, 10, 7, -7, -20, 22, -23, -26, 13, -29, -27, -5, 14, -8, 13, 26, 7, 9, -10, -13, -23, 21, 26, -18, -21, -17, 6, -7, -29, -25, 11, 9, 29, -5, 14, -7, 10, 18, -26, -23, -9, -2, -15, 6, -6, 7, -18, -11, 22, 12, -2, 6, 19, 9, 0, 10, 9, 2, 19, 2, -30, -24, -13, -23, -22, 14, -21, 24, 23, -8, 6, -24, 19, 19, -7, -16, -23, -8, 13, -2, -18, 16, 22, 29, 24, -24, -13, -6, -24, 11, 19, -1, -14, 13, 17, -4, -11, 23, 28, 17, -26, -30, 5, -23, -3, -16, 17, -22, -8, -25, 4, -11, 21, 12, -24, -15, 18, 6, 16, 8, 8, -27, -20, -22, 8, 10, 22, 3, 29, 11, -1, 8, -28, 5, -16, 18, -5, -20, -3, -1, 22, 15, 19, 1, -12, 15, 11, 25, 4, 19, 2, -11, 0, -28, -16, 19, 14, -14, -15, 2, -24, 8, -14, 14, -12, 28, -17, 0, 15, -1, 17, 20, -21, 24, 19, -12, -18, -29, -14, 14, -25, 26, -26, -19, -11, 12, -27, -3, -6, -21, -13, 17, 20, -20, -8, -14, -6, 20, -15, -13, -1, 2, -19, 28, 27, -20, -8, 16, 29, 8, -9, -25, 5, 4, -1, 12, -1, -26, -23, 16, 12, 14, 11, -20, -26, -2, -8, -15, -26, -27, -10, -17, 0, 19, -15, -1, 10, 25, -7, 18, 27, 24, 24, -12, 28, -3, 26, 10, 10, 16, -10, -8, 27, -18, -18, 6, -27, -30, -16, -26, 9, -26, -1, 12, 1, 24, -15, -6, 29, 7, 15, 23, -3, 29, -1, -2, 20, 0, -10, -17, 2, 20, -10, -2, 11, -7, -13, 9, 23, 20, 23, 14, -26, -20, 12, -23, 13, 3, 5, -12, 23, -20, -19, 21, 7, -7, 3, -12, 15, 26, 0, -6, -26, -3, -9, 14, 16, 27, -9, -24, -18, 12, -20, 24, 13, 15, 28, -20, -25, -26, -3, 10, -19, -14, -1, -19, -24, -14, 16, 7, -29, 22, 24, 13, 7, -13, -1, -1, 6, -11, 22, -18, -1, -10, -7, -4, 22, -6, -24, -28, 4, 5, -27, -29, -27, 13, -25, -9, -2, -13, 29, -26, -25, 25, -11, 10, 21, -2, 21, 25, -18, -13, 7, 16, 26, 23, -19, -29, -30, -22, -5, -4, 0, -2, -6, 0, 13, 23, -30, 10, -5, 12, -28, -3, 4, -29, 27, 23, 28, 2, -16, -11, -17, -15, 6, -13, -2, 6, 12, 0, -12, -21, -2, -19, -22, 6, 26, -25, 17, -19, 11, -12, -20, -14, 28, -19, -12, 12, -5, 6, -9, -16, -14, -22, -30, -30, -20, 29, -8, -29, 14, 20, -23, 8, 8, -8, 16, -18, 10, 7, -5, 2, -21, -12, -24, -25, -7, -18, 21, -1, 1, 4, -24, 26, 2, 4, -27, -21, -3, -25, -12, 22, -29, 6, -29, -10, -14, -17, 24, -24, 22, 17, 13, 28, -23, -11, 13, -11, -29, -22, -29, -15, -12, -6, -3, -18, -28, 28, -18, 16, -24, 17, -26, -29, 15, -18, -4, 13, 19, 3, 11, 6, -29, -21, 6, 20, -10, -15, -11, 18, -14, -25, 28, 5, 12, 18, 15, -25, -12, -22, 28, -8, 9, -17, -26, 14, -19, -23, 15, -20, 12, 4, -19, 15, -21, 13, -2, -2, 29, 10, 29, 16, -18, 5, 14, -24, 11, -12, 1, -27, 21, -20, 16, -8, 22, 13, 7, -24, -29, 18, 29, 0, 4, 6, 21, 23, -13, 2, 26, 17, -12, -15, -19, 26, -9, -1, -12, 10, 22, 18, -27, 17, -7, -16, 1, -12, 4, -10, -17, 15, 17, -7, 4, -7, -16, -7, -12, 11, 4, -30, 29, -26, -13, 22, 21, -15, 5, -27, -18, 14, 19, 27, 5, -11, -1, -10, -9, -25, 29, 16, 4, 29, -7, -21, -14, -2, -4, 12, 2, -3, -28, -26, -5, 29, 3, 17, 24, -5, -12, -4, 29, -15, 10, -25, 18, -10, 15, -19, -30, -18, -27, -19, -26, 9, 4, 0, -1, 4, -21, 13, 19, -17, 9, 29, -27, -19, -12, -30, 19, -2, -16, -1, 24, -13, -26, -19, -12, -11, 13, -11, 3, 24, -17, 15, -25, -21, 14, -5, 29, -10, -27, -2, 7, 28, -3, -8, -23, -9, 5, 13, -14, -20, 21, 26, 19, 28, -28, -17, -14, 5, -23, 11, 25, 14, -20, 0, 17, -8, 11, -16, 22, -14, 11, -19, -23, 28, 6, 18, -27, 22, -18, 2, 13, -5, -9, -22, 3, 3, 28, 15, 17, -22, -29, -30, 16, 4, 8, 1, 6, 2, 11, 8, -27, -7, -27, 20, 19, 15, -26, -1, 5, -1, -4, 29, 28, 22, 19, 23, 26, 24, -12, -6, -15, 0, -5, 23, 12, 2, 9, -29, 3, 27, -26, -10, 27, 10, -30, -20, 3, 29, -25, 28, -1, 26, -13, -3, -2, 2, -5, 23, 14, 9, -21, 21, -22, 12, 10, 17, 2, 19, -26, 12, -29, -19, 21, -2, 26, -1, 23, -6, 1, 19, -16, 24, 17, -9, 22, -28, -8, 1, -20, 8, 19, 16, 19, -6, 28, -12, -6, -20, 13, 24, 13, 9, 29, 6, -18, -3, 24, -19, -1, -20, -9, -5, 28, -17, 2, -27, -14, 28, 2, -4, -6, -26, 27, 29, 0, 10, -18, -2, 16, 1, -19, -20, -30, 17, 8, -11, 2, -14, 13, 20, 7, 11, -17, 11, -27, -10, 7, -8, -26, 7, 23, -16, -10, 0, 0, 22, 11, 9, -22, -3, 0, 15, -17, 16, -22, -10, -27, -21, 10, 18, -9, 7, -26, -18, -6, -22, 18, 25, 27, -18, -1, -21, -11, 16, -18, -14, 17, -3, 8, -26, -18, -23, 6, 11, -10, -24, -27, -21, 28, 22, 20, 4, -28, -9, -7, 3, -24, -24, 10, 19, -30, 17, -4, 29, 26, -20, -13, -15, -25, 14, -10, -21, 2, 18, -9, 7, 14, -5, 26, -17, 16, 8, -3, 24, -25, 11, 19, 29, -27, -23, -17, -24, -28, -26, 22, 17, -5, -24, -14, 20, -27, -1, -3, -9, -20, 12, -15, 2, -1, -17, 9, 16, -13, -17, 14, -29, -8, 4, -11, 24, -28, 9, 16, -16, 9, -8, 28, 15, -4, -24, 29, 20, 11, 22, -26, 13, -14, 20, -19, -22, -30, -15, 8, 4, -29, -11, 21, 28, -4, 22, 10, 7, -1, 21, 25, 0, 14, 24, 3, 3, 20, -7, -22, 24, 17, -25, 13, 7, -11, -24, 16, -25, 26, -28, -20, -7, -3, 1, 4, 24, -2, 8, -1, -25, 0, 28, 22, 4, 6, -26, -17, 12, -27, -11, -9, -12, 8, -4, 9, 23, 9, -14, -12, 6, -5, 14, -5, -27, 1, -21, 1, -26, 6, -17, 4, -27, 3, -7, -9, -13, 2, 25, -21, 8, 9, 2, 14, 5, 14, -26, 15, -26, -26, 13, 10, -14, -22, -25, 9, -15, -22, 24, 10, -5, 13, -25, 20, 7, -14, 18, 20, 21, 29, 22, -1, -14, 25, -17, 29, -30, 26, 28, -14, -19, -13, 13, -9, -13, 10, -7, -7, -15, -7, 14, 13, 10, -15, 28, 19, -26, -13, -21, -3, -6, 15, -11, -8, 25, 8, 8, 10, 24, -3, -20, 23, -2, -18, -18, -7, -14, 15, 10, -8, -19, 2, -9, 12, 15, 1, 9, -26, -30, -29, 2, 18, 24, 8, -11, 22, 27, -16, 29, -24, -30, -3, 11, -7, 6, -11, -12, -14, 18, -15, 15, 21, -15, -3, 6, 10, 7, -18, 26, -3, 19, 2, -28, -1, -4, -28, -28, -4, -6, 11, -25, 22, -12, 25, -7, 13, 29, -27, -28, -4, 20, 5, 3, -23, 8, -2, -25, -22, 1, -2, 1, 7, -3, 28, -6, 24, -20, -24, -9, 22, 19, 22, 24, -10, -5, -15, 29, 6, 10, 1, 21, 13, 19, 14, -9, 28, -4, -4, 2, 14, -27, 27, 28, -8, -12, 24, 15, 9, -9, -29, -4, -15, 26, -23, -26, -13, -2, -2, -12, -15, -25, 7, -21, -16, -25, 12, -17, -23, -4, 4, 7, 1, -12, -12, 3, 6, 15, -16, 15, 18, 22, -14, 11, -7, 27, -1, -4, 25, 26, -6, 29, -14, -13, -19, -22, -8, -7, -24, 14, 20, -5, 6, 10, -5, -18, 2, 8, -10, -20, -18, -30, -29, 20, -9, 24, 29, 18, 6, -22, 27, 10, -1, -7, 4, -3, -12, 18, 19, 12, -3, 9, 26, 22, 10, 8, -17, -27, -13, 25, -17, -23, -25, -20, -24, 17, 4, 26, -6, -5, 28, -24, -5, -18, -27, 24, -29, 23, -5, -5, 0, 1, -13, -21, -2, -14, 8, 8, 19, -16, -14, -10, 26, 20, 6, 17, -10, -2, 17, -26, -5, 10, -18, -30, 12, -29, 23, 6, -12, 7, -28, 2, -14, 3, -22, 22, 5, 5, -21, -6, 22, -24, 23, -23, -6, -17, -7, -14, -25, -10, 12, 8, 21, -14, 12, 9, 14, 25, 1, 22, 7, 14, 13, -7, 7, 27, 11, -12, -1, -4, -4, -22, 20, -17, 11, 24, -12, -5, -26, -30, 12, -26, -5, -25, -25, 13, -11, -1, 29, -30, 7, 29, -25, -12, 15, -30, 6, 20, -11, 1, 7, -5, 11, -16, -3, 4, -19, -1, -21, -16, 6, -23, 29, -11, -27, 16, -10, 9, -18, 14, 29, 28, 1, 18, -28, -15, 6, -4, 19, 6, 4, 26, -14, 2, -28, -4, -27, 12, -23, -16, 17, 18, -28, 15, -21, -20, 26, 11, -14, -29, 7, 1, 22, -13, 12, -10, 20, 5, -18, -1, 26, -14, -20, 27, -29, 13, -14, 3, -4, 22, -1, -27, 26, -7, -18, -4, 0, 6, -9, 19, 22, 28, -12, 24, -28, 10, -25, 25, 28, 24, 14, -25, -25, 11, 10, 16, 19, -29, -10, 7, -10, -29, -20, -18, -17, 1, -10, -8, 6, -29, 27, 29, 2, -22, -7, -21, -17, 29, 23, 3, -14, -22, 11, -23, -6, -15, 9, 12, -23, 10, 20, -30, -9, 18, -25, 20, 19, -3, -7, 24, 15, 16, 14, 16, 23, -30, -3, -21, 7, -19, 6, 19, -30, 26, -15, 19, 22, 29, 2, -16, -3, 16, -12, 15, 7, 11, 8, 15, -9, 11, -21, -18, -24, -16, -23, 26, 5, -28, 9, 10, 16, 21, 6, -7, 11, 14, 8, -1, -11, -22, -9, -20, 1, 23, 22, 6, 26, -21, 16, -9, -5, -26, -28, 0, -30, -25, -22, 0, -29, 4, 1, 18, -10, 17, -14, 11, 2, -28, -25, 12, -30, 17, 14, -5, -13, 29, 29, 20, 16, -3, 18, -21, 6, 13, 19, -23, -4, -14, 11, -3, 25, 14, -27, 6, 5, -18, 11, 27, 3, -2, -16, -10, -14, 9, 14, 18, -25, 19, -14, -29, -2, -2, -14, -4, 21, 29, 9, 25, 25, -7, 12, 15, -4, -19, 5, 10, 6, 12, 12, 21, -20, -22, -15, 17, -17, 3, -20, 4, -2, -6, -11, 5, 18, 29, 12, -26, -21, -17, 6, 12, -29, -6, -17, 15, 12, -5, -28, 2, -25, -11, -11, -1, 24, 13, 3, 9, 26, -22, -13, -30, -29, -4, -22, -16, 13, 11, -15, -12, 28, -23, 10, -13, 20, -24, -26, 8, 0, 2, 3, 2, -27, -1, 26, -17, -27, 23, 5, -9, 11, 26, -1, -4, 19, 9, -16, 18, -5, -10, -24, -9, 22, 28, 29, 4, -18, 24, 12, 20, -11, -24, 6, -9, 8, 10, 4, 2, 3, -12, -23, -6, 10, 24, 4, -7, 26, -25, -8, -14, -7, 1, 25, 6, 22, -8, 14, -23, -20, 24, 26, -8, 18, -9, 25, 7, 29, 17, -18, -21, 3, 24, -11, -11, -29, 11, -14, -1, 11, 1, 27, 17, 5, 29, 17, 22, -9, -15, -10, 9, 21, -20, -29, -9, 20, -12, -9, 24, 14, 4, 28, 27, 29, 27, 2, 26, 23, -20, -26, 27, -2, -1, 18, 25, -8, 0, 27, -17, 2, 22, 17, -16, 19, 22, 27, 19, -14, -9, -3, 25, 4, 16, -18, -22, 28, 1, 8, 29, -20, -14, -8, 20, -8, 12, -4, -16, -1, -26, 23, 29, 22, -11, 15, 26, 1, 29, -18, -27, -3, -20, -13, -6, 0, 12, 21, -9, -24, 10, 18, 8, -1, -23, 18, 8, 29, -30, -1, 24, -12, 26, -23, -13, 28, 5, -29, -28, -13, 6, -4, 16, 16, 11, -19, 20, -4, 9, 4, -5, 14, 9, -2, -20, -28, -30, -22, 20, 12, 19, 8, -5, 29, -16, -6, 25, 7, -27, 26, 4, 29, 7, 18, -11, -8, 28, -18, -15, 22, 2, -13, -11, -14, 11, -14, -22, 28, 28, 11, 19, -26, -23, 11, -20, -12, -23, -15, 5, -3, -13, -28, 12, -6, -12, 7, 12, -15, -1, -20, -21, 0, -17, -9, -5, -25, -23, 3, -11, -11, 24, -11, -6, 26, 2, -20, 15, -1, 8, 26, 21, 26, 1, 1, 23, 28, -13, 4, 4, -27, -8, 29, 24, -24, 13, 23, 3, 18, -15, -8, 17, 9, 25, 29, -7, -8, 7, 7, -15, 4, -3, 13, -30, -9, 0, 5, 25, 2, 23, -28, -26, -1, -10, 6, -26, 24, 19, 27, 17, 26, -4, -25, -2, -2, -5, 17, -17, -2, 16, 7, 16, 17, -7, -9, -29, -4, -24, 23, 14, 28, 13, 23, 22, 17, 15, -9, -12, 6, 10, 3, -18, 28, -2, -18, 20, 22, 3, -16, -11, -19, -3, -15, -10, 28, -4, 15, -27, -8, 29, 4, 2, -6, 15, 17, 27, -4, -7, 15, -13, 9, -11, -20, 18, 3, -29, -22, -17, -1, -4, -29, 19, 4, -22, -7, -14, -25, 24, -2, 28, 19, 7, 8, -6, -12, 29, 6, -26, 7, -19, -25, -19, -20, 28, 25, -11, -4, -25, -21, 8, -9, 19, 20, 0, -15, -30, 10, 28, 17, -29, 6, 7, 2, -22, 3, -8, -14, 6, 23, -1, -6, -29, -29, 28, 21, 16, -14, -22, 15, -26, 29, 17, -16, -29, 18, 13, -12, 15, 18, 8, 22, -19, 17, -5, -27, 26, -12, -6, -15, 22, -25, -3, -26, -1, -9, 18, -9, -28, 8, -6, -6, -16, 7, -16, 28, -7, 1, -22, -18, 0, -1, 18, -15, 5, 14, -16, 19, -18, 9, 27, -17, 2, 19, 9, -21, -3, -13, -23, 18, 9, -2, -4, 16, -16, 18, -24, 1, 11, 29, 2, -18, 16, -30, -10, -16, -13, -14, 1, 17, 16, -12, -21, -3, -27, -19, 23, 24, 25, 22, 12, -11, -2, 0, 10, -9, -16, 26, 13, 9, -28, 19, 14, -3, -4, -21, 1, -22, 8, 4, 19, 10, 22, 6, 6, -1, -29, -13, -14, 18, -17, -19, 3, -26, 7, -22, -27, 3, 10, -1, 16, -25, -3, 6, -26, 11, -7, -15, -14, -25, 15, 20, -18, -17, 10, -2, -27, -7, -7, 9, 7, -8, 27, -8, -9, -20, -11, 25, -8, 5, 3, 12, 19, -9, 28, 19, -27, 27, 29, 14, -3, -21, 13, -26, -19, -29, 18, -7, 2, -29, -2, -7, 22, 7, 25, -25, -26, -13, -16, 20, 9, -7, 12, -30, -8, 28, 13, -8, 16, -3, -15, -24, -22, -11, -3, -1, 9, 11, 27, 24, -13, -26, -1, -23, 6, 10, 24, 5, -10, -21, 22, -1, 1, 14, -13, -26, 2, 20, 11}; - assertEquals(48645, solution1.countRangeSum(nums, 1, 4)); - assertEquals(48645, solution2.countRangeSum(nums, 1, 4)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_328Test.java b/src/test/java/com/fishercoder/_328Test.java deleted file mode 100644 index 0d7a1c8c24..0000000000 --- a/src/test/java/com/fishercoder/_328Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.solutions._328; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _328Test { - private static _328.Solution1 solution1; - private static ListNode expected; - private static ListNode node; - - @BeforeClass - public static void setup() { - solution1 = new _328.Solution1(); - } - - @Test - public void test1() { - node = new ListNode(1); - node.next = new ListNode(2); - node.next.next = new ListNode(3); - node.next.next.next = new ListNode(4); - node.next.next.next.next = new ListNode(5); - - expected = new ListNode(1); - expected.next = new ListNode(3); - expected.next.next = new ListNode(5); - expected.next.next.next = new ListNode(2); - expected.next.next.next.next = new ListNode(4); - assertEquals(expected, solution1.oddEvenList(node)); - } -} diff --git a/src/test/java/com/fishercoder/_32Test.java b/src/test/java/com/fishercoder/_32Test.java deleted file mode 100644 index c1db3cfae9..0000000000 --- a/src/test/java/com/fishercoder/_32Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._32; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _32Test { - private static _32.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _32.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.longestValidParentheses("(()")); - } -} diff --git a/src/test/java/com/fishercoder/_330Test.java b/src/test/java/com/fishercoder/_330Test.java deleted file mode 100644 index d595f1db7a..0000000000 --- a/src/test/java/com/fishercoder/_330Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._330; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _330Test { - private static _330.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _330.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {1, 2, 4, 13, 43}; - List expected = new ArrayList(Arrays.asList(8, 29)); - assertEquals(expected, solution1.findPatches(nums, 100)); - assertEquals(2, solution1.minPatches(nums, 100)); - } -} diff --git a/src/test/java/com/fishercoder/_331Test.java b/src/test/java/com/fishercoder/_331Test.java deleted file mode 100644 index 9b75120530..0000000000 --- a/src/test/java/com/fishercoder/_331Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._331; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _331Test { - private static _331.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _331.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#")); - } - - @Test - public void test2() { - assertEquals(false, solution1.isValidSerialization("1,#")); - } - - @Test - public void test3() { - assertEquals(false, solution1.isValidSerialization("9,#,#,1")); - } - - @Test - public void test4() { - assertEquals(false, solution1.isValidSerialization("1")); - } - - @Test - public void test5() { - assertEquals(false, solution1.isValidSerialization("#,7,6,9,#,#,#")); - } -} diff --git a/src/test/java/com/fishercoder/_332Test.java b/src/test/java/com/fishercoder/_332Test.java deleted file mode 100644 index d1765e31ca..0000000000 --- a/src/test/java/com/fishercoder/_332Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._332; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.List; - -public class _332Test { - private static _332.Solution1 solution1; - private static String[][] tickets; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _332.Solution1(); - } - - @Test - public void test1() { - tickets = new String[][] {{"MUC", "LHR"}, {"JFK", "MUC"}, {"SFO", "SJC"}, {"LHR", "SFO"}}; - expected = solution1.findItinerary(tickets); - CommonUtils.print(expected); - } - - @Test - public void test2() { - tickets = new String[][] {{"JFK", "SFO"}, {"JFK", "ATL"}, {"SFO", "ATL"}, {"ATL", "JFK"}, - {"ATL", "SFO"}}; - expected = solution1.findItinerary(tickets); - CommonUtils.print(expected); - } -} diff --git a/src/test/java/com/fishercoder/_338Test.java b/src/test/java/com/fishercoder/_338Test.java deleted file mode 100644 index a913ad1f26..0000000000 --- a/src/test/java/com/fishercoder/_338Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._338; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _338Test { - private static _338.Solution1 test; - private static int[] expected; - private static int[] actual; - - @BeforeClass - public static void setup() { - test = new _338.Solution1(); - } - - @Test - public void test1() { - expected = new int[] {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; - actual = test.countBits(15); - CommonUtils.printArray(actual); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_33Test.java b/src/test/java/com/fishercoder/_33Test.java deleted file mode 100644 index aa0e6f14cd..0000000000 --- a/src/test/java/com/fishercoder/_33Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._33; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _33Test { - private static _33.Solution1 solution1; - private static _33.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _33.Solution1(); - solution2 = new _33.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{4, 5, 6, 7, 0, 1, 2}; - assertEquals(3, solution1.search(nums, 7)); - assertEquals(3, solution2.search(nums, 7)); - } -} diff --git a/src/test/java/com/fishercoder/_340Test.java b/src/test/java/com/fishercoder/_340Test.java deleted file mode 100644 index e409a37e2f..0000000000 --- a/src/test/java/com/fishercoder/_340Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._340; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _340Test { - private static _340.Solution1 solution1; - private static _340.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _340.Solution1(); - solution2 = new _340.Solution2(); - } - - @Test - public void test1() { - assertEquals(3, solution1.lengthOfLongestSubstringKDistinct("eceba", 2)); - assertEquals(3, solution2.lengthOfLongestSubstringKDistinct("eceba", 2)); - } - - @Test - public void test2() { - assertEquals(0, solution1.lengthOfLongestSubstringKDistinct("", 0)); - assertEquals(0, solution2.lengthOfLongestSubstringKDistinct("", 0)); - } - - @Test - public void test3() { - assertEquals(0, solution1.lengthOfLongestSubstringKDistinct("a", 0)); - } - - @Test - public void test4() { - assertEquals(1, solution1.lengthOfLongestSubstringKDistinct("a", 1)); - } - - @Test - public void test5() { - assertEquals(1, solution1.lengthOfLongestSubstringKDistinct("a", 2)); - } - - @Test - public void test6() { - assertEquals(2, solution1.lengthOfLongestSubstringKDistinct("aa", 1)); - } - - @Test - public void test7() { - assertEquals(3, solution1.lengthOfLongestSubstringKDistinct("bacc", 2)); - } - -} diff --git a/src/test/java/com/fishercoder/_341Test.java b/src/test/java/com/fishercoder/_341Test.java deleted file mode 100644 index 49cafca5c9..0000000000 --- a/src/test/java/com/fishercoder/_341Test.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.NestedInteger; -import com.fishercoder.solutions._341; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; - -import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.assertEquals; - -public class _341Test { - private static _341.Solution1 test; - private static List nestedList; - - @BeforeClass - public static void setup() { - test = new _341.Solution1(); - } - - @Test - public void test1() { - NestedInteger six = new NestedInteger(6); - List sixList = new ArrayList<>(); - sixList.add(six); - NestedInteger four = new NestedInteger(4); - List fourList = new ArrayList<>(); - fourList.add(four); - fourList.addAll(sixList); - NestedInteger one = new NestedInteger(1); - List oneList = new ArrayList<>(); - oneList.add(one); - oneList.addAll(fourList); - _341.Solution1.NestedIterator nestedIterator = new _341.Solution1.NestedIterator(oneList); - assertTrue(nestedIterator.hasNext()); - assertEquals(1, (int) nestedIterator.next()); - } - - @Test - public void test2() { - List bigList = new ArrayList<>(); - - NestedInteger one = new NestedInteger(1); - NestedInteger two = new NestedInteger(2); - List oneList = new ArrayList<>(); - oneList.add(one); - oneList.add(two); - NestedInteger oneNestedInteger = new NestedInteger(oneList); - bigList.add(oneNestedInteger); - - NestedInteger three = new NestedInteger(3); - bigList.add(three); - - NestedInteger four = new NestedInteger(4); - NestedInteger five = new NestedInteger(5); - List threeList = new ArrayList<>(); - threeList.add(four); - threeList.add(five); - NestedInteger threeNestedInteger = new NestedInteger(threeList); - bigList.add(threeNestedInteger); - - _341.Solution1.NestedIterator nestedIterator = new _341.Solution1.NestedIterator(bigList); - assertTrue(nestedIterator.hasNext()); - assertEquals(1, (int) nestedIterator.next()); - } -} diff --git a/src/test/java/com/fishercoder/_347Test.java b/src/test/java/com/fishercoder/_347Test.java deleted file mode 100644 index e2dd69639c..0000000000 --- a/src/test/java/com/fishercoder/_347Test.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._347; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _347Test { - private static _347.Solution1 solution1; - private static _347.Solution2 solution2; - private static int[] nums; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _347.Solution1(); - solution2 = new _347.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{3, 0, 1, 0}; - expected = new ArrayList<>(Arrays.asList(0, 3)); - /**Comment out until Leetcode addresses this test case: - * https://discuss.leetcode.com/topic/44237/java-o-n-solution-bucket-sort/75 - * Then I'll update this Solution1 code accordingly. - * - * My post is still un-addressed. - 3/12/2018*/ - //assertEquals(expected, solution1.topKFrequent(nums, 2)); - } - - @Test - public void test2() { - nums = new int[]{3, 0, 1, 0}; - expected = new ArrayList<>(Arrays.asList(0, 3)); - assertEquals(expected, solution2.topKFrequent(nums, 2)); - } - - @Test - public void test3() { - nums = new int[] {1, 1, 1, 2, 2, 3}; - expected = new ArrayList<>(Arrays.asList(1, 2)); - assertEquals(expected, solution1.topKFrequent(nums, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_348Test.java b/src/test/java/com/fishercoder/_348Test.java deleted file mode 100644 index f531a35d76..0000000000 --- a/src/test/java/com/fishercoder/_348Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._348; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _348Test { - @Test - public void test1() { - int n = 3; - _348.Solution1.TicTacToe ticTacToe = new _348.Solution1.TicTacToe(n); - assertEquals(0, ticTacToe.move(0, 0, 1)); - assertEquals(0, ticTacToe.move(0, 2, 2)); - assertEquals(0, ticTacToe.move(2, 2, 1)); - assertEquals(0, ticTacToe.move(1, 1, 2)); - assertEquals(0, ticTacToe.move(2, 0, 1)); - assertEquals(0, ticTacToe.move(1, 0, 2)); - assertEquals(1, ticTacToe.move(2, 1, 1)); - } - - @Test - public void test2() { - int n = 3; - _348.Solution1.TicTacToe ticTacToe = new _348.Solution1.TicTacToe(n); - assertEquals(0, ticTacToe.move(0, 0, 1)); - assertEquals(0, ticTacToe.move(1, 1, 1)); - assertEquals(1, ticTacToe.move(2, 2, 1)); - } - - @Test - public void test3() { - int n = 3; - _348.Solution1.TicTacToe ticTacToe = new _348.Solution1.TicTacToe(n); - assertEquals(0, ticTacToe.move(0, 2, 2)); - assertEquals(0, ticTacToe.move(1, 1, 2)); - assertEquals(2, ticTacToe.move(2, 0, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_349Test.java b/src/test/java/com/fishercoder/_349Test.java deleted file mode 100644 index a47e1e61af..0000000000 --- a/src/test/java/com/fishercoder/_349Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._349; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _349Test { - private static _349.Solution1 solution1; - private static _349.Solution2 solution2; - private static _349.Solution3 solution3; - - @BeforeClass - public static void setup() { - solution1 = new _349.Solution1(); - solution2 = new _349.Solution2(); - solution3 = new _349.Solution3(); - } - - @Test - public void test1() { - assertArrayEquals(new int[]{2}, solution1.intersection(new int[]{1, 2, 2, 1}, new int[]{2, 2})); - } - - @Test - public void test2() { - assertArrayEquals(new int[]{2}, solution2.intersection(new int[]{1, 2, 2, 1}, new int[]{2, 2})); - } - - @Test - public void test3() { - assertArrayEquals(new int[]{2}, solution3.intersection(new int[]{1, 2, 2, 1}, new int[]{2, 2})); - } -} diff --git a/src/test/java/com/fishercoder/_34Test.java b/src/test/java/com/fishercoder/_34Test.java deleted file mode 100644 index 4dab5c9a92..0000000000 --- a/src/test/java/com/fishercoder/_34Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._34; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _34Test { - private static _34 test; - private static int[] nums; - - @BeforeClass - public static void setup() { - test = new _34(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3}; - assertArrayEquals(new int[]{1, 1}, test.searchRange(nums, 2)); - } - - @Test - public void test2() { - nums = new int[]{}; - assertArrayEquals(new int[]{-1, -1}, test.searchRange(nums, 0)); - } -} diff --git a/src/test/java/com/fishercoder/_350Test.java b/src/test/java/com/fishercoder/_350Test.java deleted file mode 100644 index 958e99fc60..0000000000 --- a/src/test/java/com/fishercoder/_350Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._350; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _350Test { - private static _350.Solution1 solution1; - private static _350.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _350.Solution1(); - solution2 = new _350.Solution2(); - } - - @Test - public void test1() { - assertArrayEquals(new int[]{2, 2}, solution1.intersect(new int[]{1, 2, 2, 1}, new int[]{2, 2})); - } - - @Test - public void test2() { - assertArrayEquals(new int[]{2, 2}, solution2.intersect(new int[]{1, 2, 2, 1}, new int[]{2, 2})); - } - -} diff --git a/src/test/java/com/fishercoder/_352Test.java b/src/test/java/com/fishercoder/_352Test.java deleted file mode 100644 index 7171632e74..0000000000 --- a/src/test/java/com/fishercoder/_352Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.Interval; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._352; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.List; - -public class _352Test { - private static _352.Solution1.SummaryRanges test; - private static List actual; - - @BeforeClass - public static void setup() { - test = new _352.Solution1.SummaryRanges(); - } - - @Test - public void test1() { - test.addNum(1); - actual = test.getIntervals(); - CommonUtils.printIntervals(actual); - - test.addNum(3); - actual = test.getIntervals(); - CommonUtils.printIntervals(actual); - - test.addNum(7); - actual = test.getIntervals(); - CommonUtils.printIntervals(actual); - - test.addNum(2); - actual = test.getIntervals(); - CommonUtils.printIntervals(actual); - - test.addNum(6); - actual = test.getIntervals(); - CommonUtils.printIntervals(actual); - } -} diff --git a/src/test/java/com/fishercoder/_356Test.java b/src/test/java/com/fishercoder/_356Test.java deleted file mode 100644 index 2aeec29091..0000000000 --- a/src/test/java/com/fishercoder/_356Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._356; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _356Test { - private static _356.Solution1 solution1; - private static int[][] points; - - @BeforeClass - public static void setup() { - solution1 = new _356.Solution1(); - } - - @Test - public void test1() { - points = new int[][] { - {1, 1}, - {-1, 1}, - }; - assertEquals(true, solution1.isReflected(points)); - } - - @Test - public void test2() { - points = new int[][] { - {1, 1}, - {-1, -1}, - }; - assertEquals(false, solution1.isReflected(points)); - } -} diff --git a/src/test/java/com/fishercoder/_358Test.java b/src/test/java/com/fishercoder/_358Test.java deleted file mode 100644 index 0e085d6ad2..0000000000 --- a/src/test/java/com/fishercoder/_358Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._358; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _358Test { - - private static _358.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _358.Solution1(); - } - - @Test - public void test1() { - System.out.println(solution1.rearrangeString("aabbcc", 3)); - } - - @Test - public void test2() { - System.out.println(solution1.rearrangeString("aaabc", 3)); - } - - @Test - public void test3() { - System.out.println(solution1.rearrangeString("aaadbbcc", 2)); - } -} diff --git a/src/test/java/com/fishercoder/_35Test.java b/src/test/java/com/fishercoder/_35Test.java deleted file mode 100644 index 3062dcd46a..0000000000 --- a/src/test/java/com/fishercoder/_35Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._35; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _35Test { - private static _35.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _35.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 3, 5, 6}; - assertEquals(2, solution1.searchInsert(nums, 5)); - } -} diff --git a/src/test/java/com/fishercoder/_362Test.java b/src/test/java/com/fishercoder/_362Test.java deleted file mode 100644 index b583e49fa5..0000000000 --- a/src/test/java/com/fishercoder/_362Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._362; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _362Test { - private static _362.Solution1.HitCounter hitCounter; - - @BeforeClass - public static void setup() { - hitCounter = new _362.Solution1.HitCounter(); - } - - @Test - public void test1() { - hitCounter.hit(1); - hitCounter.hit(2); - hitCounter.hit(3); - assertEquals(3, hitCounter.getHits(4)); - - hitCounter.hit(300); - assertEquals(4, hitCounter.getHits(300)); - assertEquals(3, hitCounter.getHits(301)); - - hitCounter.hit(301); - assertEquals(4, hitCounter.getHits(300)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_368Test.java b/src/test/java/com/fishercoder/_368Test.java deleted file mode 100644 index 2840b7b882..0000000000 --- a/src/test/java/com/fishercoder/_368Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._368; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - -public class _368Test { - private static _368.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _368.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {1, 2, 4, 8}; - assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(8, 4, 2, 1))); - } - - @Test - public void test2() { - nums = new int[] {1, 2, 3}; - assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(2, 1))); - } - - @Test - public void test3() { - nums = new int[] {1}; - assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(1))); - } - - @Test - public void test4() { - nums = new int[] {546, 669}; - assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(546))); - } - - @Test - public void test5() { - nums = new int[] {}; - assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList())); - } - - @Test - public void test6() { - nums = new int[] {4, 8, 10, 240}; - assertThat(solution1.largestDivisibleSubset(nums), is(Arrays.asList(240, 8, 4))); - } -} diff --git a/src/test/java/com/fishercoder/_36Test.java b/src/test/java/com/fishercoder/_36Test.java deleted file mode 100644 index 690b341528..0000000000 --- a/src/test/java/com/fishercoder/_36Test.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._36; -import com.fishercoder.solutions._735; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; -import static org.junit.Assert.assertArrayEquals; - -public class _36Test { - private static _36.Solution1 solution1; - private static char[][] board; - - @BeforeClass - public static void setup() { - solution1 = new _36.Solution1(); - } - - @Test - public void test1() { - board = new char[][] { - {'4', '3', '5', '2', '6', '9', '7', '8', '1'}, - {'6', '8', '2', '5', '7', '1', '4', '9', '3'}, - {'1', '9', '7', '8', '3', '4', '5', '6', '2'}, - {'8', '2', '6', '1', '9', '5', '3', '4', '7'}, - {'3', '7', '4', '6', '8', '2', '9', '1', '5'}, - {'9', '5', '1', '7', '4', '3', '6', '2', '8'}, - {'5', '1', '9', '3', '2', '6', '8', '7', '4'}, - {'2', '4', '8', '9', '5', '7', '1', '3', '6'}, - {'7', '6', '3', '4', '1', '8', '2', '5', '9'}, - }; - assertEquals(true, solution1.isValidSudoku(board)); - } - - @Test - public void test2() { - board = new char[][] { - {'.', '8', '7', '6', '5', '4', '3', '2', '1'}, - {'2', '.', '.', '.', '.', '.', '.', '.', '.'}, - {'3', '.', '.', '.', '.', '.', '.', '.', '.'}, - {'4', '.', '.', '.', '.', '.', '.', '.', '.'}, - {'5', '.', '.', '.', '.', '.', '.', '.', '.'}, - {'6', '.', '.', '.', '.', '.', '.', '.', '.'}, - {'7', '.', '.', '.', '.', '.', '.', '.', '.'}, - {'8', '.', '.', '.', '.', '.', '.', '.', '.'}, - {'9', '.', '.', '.', '.', '.', '.', '.', '.'}, - }; - assertEquals(true, solution1.isValidSudoku(board)); - } - - @Test - public void test3() { - board = new char[][] { - {'.', '.', '.', '.', '5', '.', '.', '1', '.'}, - // this upper right corner 3*3 square is invalid, '1' appears twice - {'.', '4', '.', '3', '.', '.', '.', '.', '.'}, - {'.', '.', '.', '.', '.', '3', '.', '.', '1'}, - {'8', '.', '.', '.', '.', '.', '.', '2', '.'}, - {'.', '.', '2', '.', '7', '.', '.', '.', '.'}, - {'.', '1', '5', '.', '.', '.', '.', '.', '.'}, - {'.', '.', '.', '.', '.', '2', '.', '.', '.'}, - {'.', '2', '.', '9', '.', '.', '.', '.', '.'}, - {'.', '.', '4', '.', '.', '.', '.', '.', '.'}, - }; - assertEquals(false, solution1.isValidSudoku(board)); - } - - @Test - public void test4() { - board = new char[][] { - {'.', '.', '4', '.', '.', '.', '6', '3', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.', '.'}, - {'5', '.', '.', '.', '.', '.', '.', '9', '.'}, - {'.', '.', '.', '5', '6', '.', '.', '.', '.'}, - {'4', '.', '3', '.', '.', '.', '.', '.', '1'}, - {'.', '.', '.', '7', '.', '.', '.', '.', '.'}, - {'.', '.', '.', '5', '.', '.', '.', '.', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.', '.'} - }; - assertEquals(false, solution1.isValidSudoku(board)); - } -} diff --git a/src/test/java/com/fishercoder/_370Test.java b/src/test/java/com/fishercoder/_370Test.java deleted file mode 100644 index 0ff8ae51f7..0000000000 --- a/src/test/java/com/fishercoder/_370Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._370; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _370Test { - private static _370.Solution1 solution1; - private static int[][] updates; - private static int length; - private static int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _370.Solution1(); - } - - @Test - public void test1() { - updates = new int[][]{ - {1, 3, 2}, - {2, 4, 3}, - {0, 2, -2}, - }; - length = 5; - expected = new int[]{-2, 0, 3, 5, 3}; - assertArrayEquals(expected, solution1.getModifiedArray(length, updates)); - } - -} diff --git a/src/test/java/com/fishercoder/_372Test.java b/src/test/java/com/fishercoder/_372Test.java deleted file mode 100644 index 296a1e09a6..0000000000 --- a/src/test/java/com/fishercoder/_372Test.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._372; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _372Test { - private static _372.Solution1 solution1; - private static int expected; - private static int actual; - private static int a; - private static int[] b; - - @BeforeClass - public static void setup() { - solution1 = new _372.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = 0; - actual = 0; - a = 0; - b = new int[10000]; - } - - @Test - public void test1() { - - a = 78267; - b = new int[] {1, 7, 7, 4, 3, 1, 7, 0, 1, 4, 4, 9, 2, 8, 5, 0, 0, 9, 3, 1, 2, 5, 9, 6, 0, 9, 9, - 0, 9, 6, 0, 5, 3, 7, 9, 8, 8, 9, 8, 2, 5, 4, 1, 9, 3, 8, 0, 5, 9, 5, 6, 1, 1, 8, 9, 3, 7, 8, - 5, 8, 5, 5, 3, 0, 4, 3, 1, 5, 4, 1, 7, 9, 6, 8, 8, 9, 8, 0, 6, 7, 8, 3, 1, 1, 1, 0, 6, 8, 1, - 1, 6, 6, 9, 1, 8, 5, 6, 9, 0, 0, 1, 7, 1, 7, 7, 2, 8, 5, 4, 4, 5, 2, 9, 6, 5, 0, 8, 1, 0, 9, - 5, 8, 7, 6, 0, 6, 1, 8, 7, 2, 9, 8, 1, 0, 7, 9, 4, 7, 6, 9, 2, 3, 1, 3, 9, 9, 6, 8, 0, 8, 9, - 7, 7, 7, 3, 9, 5, 5, 7, 4, 9, 8, 3, 0, 1, 2, 1, 5, 0, 8, 4, 4, 3, 8, 9, 3, 7, 5, 3, 9, 4, 4, - 9, 3, 3, 2, 4, 8, 9, 3, 3, 8, 2, 8, 1, 3, 2, 2, 8, 4, 2, 5, 0, 6, 3, 0, 9, 0, 5, 4, 1, 1, 8, - 0, 4, 2, 5, 8, 2, 4, 2, 7, 5, 4, 7, 6, 9, 0, 8, 9, 6, 1, 4, 7, 7, 9, 7, 8, 1, 4, 4, 3, 6, 4, - 5, 2, 6, 0, 1, 1, 5, 3, 8, 0, 9, 8, 8, 0, 0, 6, 1, 6, 9, 6, 5, 8, 7, 4, 8, 9, 9, 2, 4, 7, 7, - 9, 9, 5, 2, 2, 6, 9, 7, 7, 9, 8, 5, 9, 8, 5, 5, 0, 3, 5, 8, 9, 5, 7, 3, 4, 6, 4, 6, 2, 3, 5, - 2, 3, 1, 4, 5, 9, 3, 3, 6, 4, 1, 3, 3, 2, 0, 0, 4, 4, 7, 2, 3, 3, 9, 8, 7, 8, 5, 5, 0, 8, 3, - 4, 1, 4, 0, 9, 5, 5, 4, 4, 9, 7, 7, 4, 1, 8, 7, 5, 2, 4, 9, 7, 9, 1, 7, 8, 9, 2, 4, 1, 1, 7, - 6, 4, 3, 6, 5, 0, 2, 1, 4, 3, 9, 2, 0, 0, 2, 9, 8, 4, 5, 7, 3, 5, 8, 2, 3, 9, 5, 9, 1, 8, 8, - 9, 2, 3, 7, 0, 4, 1, 1, 8, 7, 0, 2, 7, 3, 4, 6, 1, 0, 3, 8, 5, 8, 9, 8, 4, 8, 3, 5, 1, 1, 4, - 2, 5, 9, 0, 5, 3, 1, 7, 4, 8, 9, 6, 7, 2, 3, 5, 5, 3, 9, 6, 9, 9, 5, 7, 3, 5, 2, 9, 9, 5, 5, - 1, 0, 6, 3, 8, 0, 5, 5, 6, 5, 6, 4, 5, 1, 7, 0, 6, 3, 9, 4, 4, 9, 1, 3, 4, 7, 7, 5, 8, 2, 0, - 9, 2, 7, 3, 0, 9, 0, 7, 7, 7, 4, 1, 2, 5, 1, 3, 3, 6, 4, 8, 2, 5, 9, 5, 0, 8, 2, 5, 6, 4, 8, - 8, 8, 7, 3, 1, 8, 5, 0, 5, 2, 4, 8, 5, 1, 1, 0, 7, 9, 6, 5, 1, 2, 6, 6, 4, 7, 0, 9, 5, 6, 9, - 3, 7, 8, 8, 8, 6, 5, 8, 3, 8, 5, 4, 5, 8, 5, 7, 5, 7, 3, 2, 8, 7, 1, 7, 1, 8, 7, 3, 3, 6, 2, - 9, 3, 3, 9, 3, 1, 5, 1, 5, 5, 8, 1, 2, 7, 8, 9, 2, 5, 4, 5, 4, 2, 6, 1, 3, 6, 0, 6, 9, 6, 1, - 0, 1, 4, 0, 4, 5, 5, 8, 2, 2, 6, 3, 4, 3, 4, 3, 8, 9, 7, 5, 5, 9, 1, 8, 5, 9, 9, 1, 8, 7, 2, - 1, 1, 8, 1, 5, 6, 8, 5, 8, 0, 2, 4, 4, 7, 8, 9, 5, 9, 8, 0, 5, 0, 3, 5, 5, 2, 6, 8, 3, 4, 1, - 4, 7, 1, 7, 2, 7, 5, 8, 8, 7, 2, 2, 3, 9, 2, 2, 7, 3, 2, 9, 0, 2, 3, 6, 9, 7, 2, 8, 0, 8, 1, - 6, 5, 2, 3, 0, 2, 0, 0, 0, 9, 2, 2, 2, 3, 6, 6, 0, 9, 1, 0, 0, 3, 5, 8, 3, 2, 0, 3, 5, 1, 4, - 1, 6, 8, 7, 6, 0, 9, 8, 0, 1, 0, 4, 5, 6, 0, 2, 8, 2, 5, 0, 2, 8, 5, 2, 3, 0, 2, 6, 7, 3, 0, - 0, 2, 1, 9, 0, 1, 9, 9, 2, 0, 1, 6, 7, 7, 9, 9, 6, 1, 4, 8, 5, 5, 6, 7, 0, 6, 1, 7, 3, 5, 9, - 3, 9, 0, 5, 9, 2, 4, 8, 6, 6, 2, 2, 3, 9, 3, 5, 7, 4, 1, 6, 9, 8, 2, 6, 9, 0, 0, 8, 5, 7, 7, - 0, 6, 0, 5, 7, 4, 9, 6, 0, 7, 8, 4, 3, 9, 8, 8, 7, 4, 1, 5, 6, 0, 9, 4, 1, 9, 4, 9, 4, 1, 8, - 6, 7, 8, 2, 5, 2, 3, 3, 4, 3, 3, 1, 6, 4, 1, 6, 1, 5, 7, 8, 1, 9, 7, 6, 0, 8, 0, 1, 4, 4, 0, - 1, 1, 8, 3, 8, 3, 8, 3, 9, 1, 6, 0, 7, 1, 3, 3, 4, 9, 3, 5, 2, 4, 2, 0, 7, 3, 3, 8, 7, 7, 8, - 8, 0, 9, 3, 1, 2, 2, 4, 3, 3, 3, 6, 1, 6, 9, 6, 2, 0, 1, 7, 5, 6, 2, 5, 3, 5, 0, 3, 2, 7, 2, - 3, 0, 3, 6, 1, 7, 8, 7, 0, 4, 0, 6, 7, 6, 6, 3, 9, 8, 5, 8, 3, 3, 0, 9, 6, 7, 1, 9, 2, 1, 3, - 5, 1, 6, 3, 4, 3, 4, 1, 6, 8, 4, 2, 5}; - expected = 70; - actual = solution1.superPow(a, b); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_376Test.java b/src/test/java/com/fishercoder/_376Test.java deleted file mode 100644 index 3be8a8909b..0000000000 --- a/src/test/java/com/fishercoder/_376Test.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._376; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _376Test { - private static _376.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _376.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {1, 7, 4, 9, 2, 5}; - assertEquals(6, solution1.wiggleMaxLength(nums)); - } - - @Test - public void test2() { - nums = new int[] {1, 17, 5, 10, 13, 15, 10, 5, 16, 8}; - assertEquals(7, solution1.wiggleMaxLength(nums)); - } - - @Test - public void test3() { - nums = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; - assertEquals(2, solution1.wiggleMaxLength(nums)); - } - - @Test - public void test4() { - nums = - new int[] {33, 53, 12, 64, 50, 41, 45, 21, 97, 35, 47, 92, 39, 0, 93, 55, 40, 46, 69, 42, 6, - 95, 51, 68, 72, 9, 32, 84, 34, 64, 6, 2, 26, 98, 3, 43, 30, 60, 3, 68, 82, 9, 97, 19, - 27, 98, 99, 4, 30, 96, 37, 9, 78, 43, 64, 4, 65, 30, 84, 90, 87, 64, 18, 50, 60, 1, 40, - 32, 48, 50, 76, 100, 57, 29, 63, 53, 46, 57, 93, 98, 42, 80, 82, 9, 41, 55, 69, 84, 82, - 79, 30, 79, 18, 97, 67, 23, 52, 38, 74, 15}; - assertEquals(67, solution1.wiggleMaxLength(nums)); - } - - @Test - public void test5() { - nums = new int[] {3, 3, 3, 2, 5}; - assertEquals(3, solution1.wiggleMaxLength(nums)); - } - - @Test - public void test6() { - nums = - new int[] {372, 492, 288, 399, 81, 2, 320, 94, 416, 469, 427, 117, 265, 357, 399, 456, 496, - 337, 355, 219, 475, 295, 457, 350, 490, 470, 281, 127, 131, 36, 430, 412, 442, 174, 128, - 253, 1, 56, 306, 295, 340, 73, 253, 130, 259, 223, 14, 79, 409, 384, 209, 151, 317, 441, - 156, 275, 140, 224, 128, 250, 290, 191, 161, 472, 477, 125, 470, 230, 321, 5, 311, 23, - 27, 248, 138, 284, 215, 356, 320, 194, 434, 136, 221, 273, 450, 440, 28, 179, 36, 386, - 482, 203, 24, 8, 391, 21, 500, 484, 135, 348, 292, 396, 145, 443, 406, 61, 212, 480, - 455, 78, 309, 318, 84, 474, 209, 225, 177, 356, 227, 263, 181, 476, 478, 151, 494, 395, - 23, 114, 395, 429, 450, 247, 245, 150, 354, 230, 100, 172, 454, 155, 189, 33, 290, 187, - 443, 123, 59, 358, 241, 141, 39, 196, 491, 381, 157, 157, 134, 431, 295, 20, 123, 118, - 207, 199, 317, 188, 267, 335, 315, 308, 115, 321, 56, 52, 253, 492, 97, 374, 398, 272, - 74, 206, 109, 172, 471, 55, 452, 452, 329, 367, 372, 252, 99, 62, 122, 287, 320, 325, - 307, 481, 316, 378, 87, 97, 457, 21, 312, 249, 354, 286, 196, 43, 170, 500, 265, 253, - 19, 480, 438, 113, 473, 247, 257, 33, 395, 456, 246, 310, 469, 408, 112, 385, 53, 449, - 117, 122, 210, 286, 149, 20, 364, 372, 71, 26, 155, 292, 16, 72, 384, 160, 79, 241, 346, - 230, 15, 427, 96, 95, 59, 151, 325, 490, 223, 131, 81, 294, 18, 70, 171, 339, 14, 40, - 463, 421, 355, 123, 408, 357, 202, 235, 390, 344, 198, 98, 361, 434, 174, 216, 197, 274, - 231, 85, 494, 57, 136, 258, 134, 441, 477, 456, 318, 155, 138, 461, 65, 426, 162, 90, - 342, 284, 374, 204, 464, 9, 280, 391, 491, 231, 298, 284, 82, 417, 355, 356, 207, 367, - 262, 244, 283, 489, 477, 143, 495, 472, 372, 447, 322, 399, 239, 450, 168, 202, 89, 333, - 276, 199, 416, 490, 494, 488, 137, 327, 113, 189, 430, 320, 197, 120, 71, 262, 31, 295, - 218, 74, 238, 169, 489, 308, 300, 260, 397, 308, 328, 267, 419, 84, 357, 486, 289, 312, - 230, 64, 468, 227, 268, 28, 243, 267, 254, 153, 407, 399, 346, 385, 77, 297, 273, 484, - 366, 482, 491, 368, 221, 423, 107, 272, 98, 309, 426, 181, 320, 77, 185, 382, 478, 398, - 476, 22, 328, 450, 299, 211, 285, 62, 344, 484, 395, 466, 291, 487, 301, 407, 28, 295, - 36, 429, 99, 462, 240, 124, 261, 387, 30, 362, 161, 156, 184, 188, 99, 377, 392, 442, - 300, 98, 285, 312, 312, 365, 415, 367, 105, 81, 378, 413, 43, 326, 490, 320, 266, 390, - 53, 327, 75, 332, 454, 29, 370, 392, 360, 1, 335, 355, 344, 120, 417, 455, 93, 60, 256, - 451, 188, 161, 388, 338, 238, 26, 275, 340, 109, 185}; - assertEquals(334, solution1.wiggleMaxLength(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_377Test.java b/src/test/java/com/fishercoder/_377Test.java deleted file mode 100644 index fd001626f3..0000000000 --- a/src/test/java/com/fishercoder/_377Test.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._377; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _377Test { - private static _377.Solution1 solution1; - private static _377.Solution2 solution2; - private static _377.Solution3 solution3; - private static _377.Solution4 solution4; - private static int[] nums; - private static int target; - - @BeforeClass - public static void setup() { - solution1 = new _377.Solution1(); - solution2 = new _377.Solution2(); - solution3 = new _377.Solution3(); - solution4 = new _377.Solution4(); - } - - @Before - public void setUp() throws Exception { - //always have to reset these global variables before using it again - solution2.count = 0; - solution4.map.clear(); - } - - @Test - public void test1() { - nums = new int[]{1,2,3}; - target = 4; - assertEquals(7, solution1.combinationSum4(nums, target)); - assertEquals(7, solution2.combinationSum4(nums, target)); - assertEquals(7, solution3.combinationSum4(nums, target)); - assertEquals(7, solution4.combinationSum4(nums, target)); - } - - @Test - public void test2() { - nums = new int[]{4,2,1}; - target = 32; -// assertEquals(39882198, solution1.combinationSum4(nums, target));//this results in MLE, so comment out - - assertEquals(39882198, solution2.combinationSum4(nums, target)); - - assertEquals(39882198, solution3.combinationSum4(nums, target)); - - assertEquals(39882198, solution4.combinationSum4(nums, target)); - } - - @Test - public void test3() { - nums = new int[]{9}; - target = 3; - assertEquals(0, solution1.combinationSum4(nums, target)); - assertEquals(0, solution2.combinationSum4(nums, target)); - assertEquals(0, solution3.combinationSum4(nums, target)); - assertEquals(0, solution4.combinationSum4(nums, target)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_37Test.java b/src/test/java/com/fishercoder/_37Test.java deleted file mode 100644 index c058a24a6b..0000000000 --- a/src/test/java/com/fishercoder/_37Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._37; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _37Test { - private static _37.Solution1 solution1; - private static char[][] board; - - @BeforeClass - public static void setup() { - solution1 = new _37.Solution1(); - } - - @Test - public void test1() { - board = new char[][]{ - {'5', '3', '.', '.', '7', '.', '.', '.', '.'}, - {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, - {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, - {'8', '3', '.', '.', '6', '.', '.', '.', '3'}, - {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, - {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, - {'.', '6', '.', '.', '7', '.', '2', '8', '.'}, - {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, - {'.', '.', '.', '.', '8', '.', '.', '7', '9'} - }; - CommonUtils.print2DCharArray(board); - solution1.solveSudoku(board); - CommonUtils.print2DCharArray(board); - } -} diff --git a/src/test/java/com/fishercoder/_385Test.java b/src/test/java/com/fishercoder/_385Test.java deleted file mode 100644 index 1a7fed63b0..0000000000 --- a/src/test/java/com/fishercoder/_385Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._385; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _385Test { - private static _385.Solution1 solution1; - - @BeforeClass - public static void setUp() { - solution1 = new _385.Solution1(); - } - - @Test - public void test1() { - solution1.deserialize("324"); - } - - @Test - public void test2() { - solution1.deserialize("[-1]"); - } - - @Test - public void test3() { - solution1.deserialize("[]"); - } - - @Test - public void test4() { - solution1.deserialize("[-1,-2]"); - } - - @Test - public void test5() { - solution1.deserialize("[-1,-2,[-3,-4,[5,[6,[7,8]]]]]"); - } - -} diff --git a/src/test/java/com/fishercoder/_388Test.java b/src/test/java/com/fishercoder/_388Test.java deleted file mode 100644 index e163bc79c8..0000000000 --- a/src/test/java/com/fishercoder/_388Test.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._388; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _388Test { - private static _388.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _388.Solution1(); - } - - @Test - public void test1() { - assertEquals(10, solution1.lengthLongestPath( - "dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext")); - } - - @Test - public void test2() { - assertEquals(9, solution1.lengthLongestPath( - "dir\\n\\tsubdir1\\n\\tsubdir2\\n\\t\\tfile.ext")); - } - - @Test - public void test3() { - assertEquals(7, solution1.lengthLongestPath( - "aaaaaaaaaaaaaaaaaaaaa/sth.png")); - } - - @Test - public void test4() { - assertEquals(9, solution1.lengthLongestPath( - "a/aa/aaa/file1.txt")); - } - - @Test - public void test5() { - assertEquals(25, solution1.lengthLongestPath( - "file name with space.txt")); - } - - @Test - public void test6() { - assertEquals(13, solution1.lengthLongestPath( - "dir\\n file.txt")); - } - - @Test - public void test7() { - assertEquals(12, solution1.lengthLongestPath( - "dir\n file.txt")); - } - - @Test - public void test8() { - assertEquals(7, solution1.lengthLongestPath( - "a\\n\\tb1\\n\\t\\tf1.txt\\n\\taaaaa\\n\\t\\tf2.txt")); - } -} diff --git a/src/test/java/com/fishercoder/_38Test.java b/src/test/java/com/fishercoder/_38Test.java deleted file mode 100644 index d2d2644546..0000000000 --- a/src/test/java/com/fishercoder/_38Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._38; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _38Test { - private static _38.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _38.Solution1(); - } - - @Test - public void test1() { - assertEquals("21", solution1.countAndSay(3)); - } -} diff --git a/src/test/java/com/fishercoder/_392Test.java b/src/test/java/com/fishercoder/_392Test.java deleted file mode 100644 index 6b5824b051..0000000000 --- a/src/test/java/com/fishercoder/_392Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._392; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _392Test { - private static _392.Solution1 solution1; - private static String s; - private static String t; - private static boolean expected; - private static boolean actual; - - @BeforeClass - public static void setup() { - solution1 = new _392.Solution1(); - } - - @Test - public void test1() { - s = "abc"; - t = "ahbgdc"; - expected = true; - actual = solution1.isSubsequence(s, t); - assertEquals(expected, actual); - } - - @Test - public void test2() { - s = "axc"; - t = "ahbgdc"; - expected = false; - actual = solution1.isSubsequence(s, t); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_393Test.java b/src/test/java/com/fishercoder/_393Test.java deleted file mode 100644 index d794bdd847..0000000000 --- a/src/test/java/com/fishercoder/_393Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._393; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _393Test { - private static _393.Solution1 solution1; - private static boolean expected; - private static boolean actual; - private static int[] data; - - @BeforeClass - public static void setup() { - solution1 = new _393.Solution1(); - } - - @Test - @Ignore - public void test1() { - data = new int[] {197, 130, 1}; - expected = true; - actual = solution1.validUtf8(data); - assertEquals(expected, actual); - } - - @Test - public void test2() { - data = new int[] {5}; - expected = true; - actual = solution1.validUtf8(data); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_394Test.java b/src/test/java/com/fishercoder/_394Test.java deleted file mode 100644 index 10714810c1..0000000000 --- a/src/test/java/com/fishercoder/_394Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._394; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by varunu28 on 1/08/19. - */ - -public class _394Test { - private static _394.Solution1 test; - - @BeforeClass - public static void setUp() { - test = new _394.Solution1(); - } - - @Test - public void test1() { - assertEquals("aaabcbc", test.decodeString("3[a]2[bc]")); - } - - @Test - public void test2() { - assertEquals("accaccacc", test.decodeString("3[a2[c]]")); - } - - @Test - public void test3() { - assertEquals("abcabccdcdcdef", test.decodeString("2[abc]3[cd]ef")); - } -} diff --git a/src/test/java/com/fishercoder/_395Test.java b/src/test/java/com/fishercoder/_395Test.java deleted file mode 100644 index d46f00450b..0000000000 --- a/src/test/java/com/fishercoder/_395Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._395; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 12/31/16. - */ -public class _395Test { - - private static _395.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _395.Solution1(); - } - - @Test - public void test1() { - assertEquals(5, solution1.longestSubstring("ababbc", 2)); - } - - @Test - public void test2() { - assertEquals(3, solution1.longestSubstring("aaabb", 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_396Test.java b/src/test/java/com/fishercoder/_396Test.java deleted file mode 100644 index 7a9b4fbbfc..0000000000 --- a/src/test/java/com/fishercoder/_396Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._396; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _396Test { - private static _396.Solution1 solution1; - private static _396.Solution2 solution2; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _396.Solution1(); - solution2 = new _396.Solution2(); - } - - @Test - public void test1() { - A = new int[]{4, 3, 2, 6}; - assertEquals(26, solution1.maxRotateFunction(A)); - assertEquals(26, solution2.maxRotateFunction(A)); - } - - @Test - public void test2() { - A = new int[]{1,2,3,4,5,6,7,8,9,10}; - assertEquals(330, solution1.maxRotateFunction(A)); - assertEquals(330, solution2.maxRotateFunction(A)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_397Test.java b/src/test/java/com/fishercoder/_397Test.java deleted file mode 100644 index 16c9dfca39..0000000000 --- a/src/test/java/com/fishercoder/_397Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._397; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _397Test { - private static _397.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _397.Solution1(); - } - - @Test - public void test1() { - assertEquals(17, solution1.integerReplacement(65535)); - } - - @Test - public void test2() { - assertEquals(14, solution1.integerReplacement(1234)); - } - - @Test - public void test3() { - assertEquals(3, solution1.integerReplacement(5)); - } -} diff --git a/src/test/java/com/fishercoder/_39Test.java b/src/test/java/com/fishercoder/_39Test.java deleted file mode 100644 index 9f3af7b18f..0000000000 --- a/src/test/java/com/fishercoder/_39Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._39; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _39Test { - private static _39.Solution1 solution1; - private static int[] candidates; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _39.Solution1(); - } - - @Test - public void test1() { - candidates = new int[] {2, 3, 6, 7}; - expected = new ArrayList<>(); - expected.add(Arrays.asList(2, 2, 3)); - expected.add(Arrays.asList(7)); - assertEquals(expected, solution1.combinationSum(candidates, 7)); - } -} diff --git a/src/test/java/com/fishercoder/_3Test.java b/src/test/java/com/fishercoder/_3Test.java deleted file mode 100644 index c7feb50676..0000000000 --- a/src/test/java/com/fishercoder/_3Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._3; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _3Test { - private static _3.Solution1 solution1; - private static _3.Solution2 solution2; - private static _3.Solution3 solution3; - private static _3.Solution4 solution4; - - @BeforeClass - public static void setup() { - solution1 = new _3.Solution1(); - solution2 = new _3.Solution2(); - solution3 = new _3.Solution3(); - solution4 = new _3.Solution4(); - } - - @Test - public void test1() { - assertEquals(3, solution1.lengthOfLongestSubstring("abcabcbb")); - assertEquals(3, solution2.lengthOfLongestSubstring("abcabcbb")); - assertEquals(3, solution3.lengthOfLongestSubstring("abcabcbb")); - assertEquals(3, solution4.lengthOfLongestSubstring("abcabcbb")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_400Test.java b/src/test/java/com/fishercoder/_400Test.java deleted file mode 100644 index 06de026cae..0000000000 --- a/src/test/java/com/fishercoder/_400Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._400; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _400Test { - private static _400.Solution1 solution1; - private static int expected; - private static int actual; - private static int n; - - @BeforeClass - public static void setup() { - solution1 = new _400.Solution1(); - } - - @Test - public void test1() { - n = 11; - expected = 0; - actual = solution1.findNthDigit(n); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_401Test.java b/src/test/java/com/fishercoder/_401Test.java deleted file mode 100644 index 259fa2dd3f..0000000000 --- a/src/test/java/com/fishercoder/_401Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._401; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _401Test { - private static _401.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _401.Solution1(); - } - - @Test - public void test1() { - assertEquals( - Arrays.asList("0:01", "0:02", "0:04", "0:08", "0:16", "0:32", "1:00", "2:00", "4:00", - "8:00"), solution1.readBinaryWatch(1)); - } -} diff --git a/src/test/java/com/fishercoder/_402Test.java b/src/test/java/com/fishercoder/_402Test.java deleted file mode 100644 index 2b003ab6e5..0000000000 --- a/src/test/java/com/fishercoder/_402Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._402; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _402Test { - private static _402.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _402.Solution1(); - } - - @Test - public void test1() { - assertEquals("1219", solution1.removeKdigits("1432219", 3)); - } -} diff --git a/src/test/java/com/fishercoder/_406Test.java b/src/test/java/com/fishercoder/_406Test.java deleted file mode 100644 index 903c7ad580..0000000000 --- a/src/test/java/com/fishercoder/_406Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._406; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _406Test { - private static _406.Solution1 solution1; - private static int[][] people; - private static int[][] actual; - - @BeforeClass - public static void setup() { - solution1 = new _406.Solution1(); - } - - @Test - public void test1() { - people = new int[][] { - {7, 0}, - {4, 4}, - {7, 1}, - {5, 0}, - {6, 1}, - {5, 2} - }; - actual = solution1.reconstructQueue(people); - CommonUtils.printArrayArray(actual); - } -} diff --git a/src/test/java/com/fishercoder/_408Test.java b/src/test/java/com/fishercoder/_408Test.java deleted file mode 100644 index 11b5b9cfd2..0000000000 --- a/src/test/java/com/fishercoder/_408Test.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._408; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _408Test { - private static _408.Solution1 solution1; - private static Boolean expected; - private static Boolean actual; - private static String word; - private static String abbr; - - @BeforeClass - public static void setup() { - solution1 = new _408.Solution1(); - } - - @Before - public void setupForEachTest() { - word = ""; - abbr = ""; - } - - @Test - public void test1() { - word = "internationalization"; - abbr = "i12iz4n"; - expected = true; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test2() { - word = "apple"; - abbr = "a2e"; - expected = false; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test3() { - word = "internationalization"; - abbr = "i5a11o1"; - expected = true; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test4() { - word = "hi"; - abbr = "1"; - expected = false; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test5() { - word = "a"; - abbr = "1"; - expected = true; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test6() { - word = "a"; - abbr = "2"; - expected = false; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test7() { - word = "hi"; - abbr = "1i"; - expected = true; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test8() { - word = "hi"; - abbr = "3"; - expected = false; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test9() { - word = "hi"; - abbr = "11"; - expected = false; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test10() { - word = "word"; - abbr = "1o1d"; - expected = true; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } - - @Test - public void test11() { - word = "abbreviation"; - abbr = "a010n"; - expected = false; - actual = solution1.validWordAbbreviation(word, abbr); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_409Test.java b/src/test/java/com/fishercoder/_409Test.java deleted file mode 100644 index 86329db810..0000000000 --- a/src/test/java/com/fishercoder/_409Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._409; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _409Test { - private static _409.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _409.Solution1(); - } - - @Test - public void test1() { - assertEquals(7, solution1.longestPalindrome("abccccdd")); - } - - @Test - public void test2() { - assertEquals(7, solution1.longestPalindrome("abccAccdd")); - } - - @Test - public void test3() { - assertEquals(983, solution1.longestPalindrome( - "civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth")); - } - - @Test - public void test4() { - assertEquals(3, solution1.longestPalindrome("ccc")); - } -} diff --git a/src/test/java/com/fishercoder/_40Test.java b/src/test/java/com/fishercoder/_40Test.java deleted file mode 100644 index bf771702bb..0000000000 --- a/src/test/java/com/fishercoder/_40Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._40; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _40Test { - private static _40.Solution1 solution1; - private static int[] candidates; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _40.Solution1(); - } - - @Test - public void test1() { - candidates = new int[] {10, 1, 2, 7, 6, 1, 5}; - expected = new ArrayList<>(); - expected.add(Arrays.asList(1, 1, 6)); - expected.add(Arrays.asList(1, 2, 5)); - expected.add(Arrays.asList(1, 7)); - expected.add(Arrays.asList(2, 6)); - assertEquals(expected, solution1.combinationSum2(candidates, 8)); - } -} diff --git a/src/test/java/com/fishercoder/_410Test.java b/src/test/java/com/fishercoder/_410Test.java deleted file mode 100644 index 0c62fd6bc5..0000000000 --- a/src/test/java/com/fishercoder/_410Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._410; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _410Test { - private static _410.Solution1 test; - private static int[] nums; - - @BeforeClass - public static void setup() { - test = new _410.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {7, 2, 5, 10, 8}; - assertEquals(18, test.splitArray(nums, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_415Test.java b/src/test/java/com/fishercoder/_415Test.java deleted file mode 100644 index 01b862d618..0000000000 --- a/src/test/java/com/fishercoder/_415Test.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._415; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _415Test { - private static _415.Solution1 solution1; - private static String expected; - private static String actual; - private static String num1; - private static String num2; - - @BeforeClass - public static void setup() { - solution1 = new _415.Solution1(); - expected = new String(); - actual = new String(); - num1 = new String(); - num2 = new String(); - } - - @Before - public void setupForEachTest() { - expected = ""; - actual = ""; - num1 = ""; - num2 = ""; - } - - @Test - public void test1() { - - num1 = "123"; - num2 = "34567"; - expected = "34690"; - actual = solution1.addStrings(num1, num2); - assertEquals(expected, actual); - - } - - @Test - public void test2() { - - num1 = "1"; - num2 = "9"; - expected = "10"; - actual = solution1.addStrings(num1, num2); - assertEquals(expected, actual); - - } - - @Test - public void test3() { - - num1 = "9"; - num2 = "99"; - expected = "108"; - actual = solution1.addStrings(num1, num2); - assertEquals(expected, actual); - - } - -} diff --git a/src/test/java/com/fishercoder/_416Test.java b/src/test/java/com/fishercoder/_416Test.java deleted file mode 100644 index 1bfbc8ab88..0000000000 --- a/src/test/java/com/fishercoder/_416Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._416; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _416Test { - private static _416.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _416.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 5, 11, 5}; - assertEquals(true, solution1.canPartition(nums)); - } - - @Test - public void test2() { - nums = new int[]{1, 2, 3, 5}; - assertEquals(false, solution1.canPartition(nums)); - } - - @Test - public void test3() { - nums = new int[]{1, 2, 3, 4, 5, 6, 7}; - assertEquals(true, solution1.canPartition(nums)); - } - -} - diff --git a/src/test/java/com/fishercoder/_417Test.java b/src/test/java/com/fishercoder/_417Test.java deleted file mode 100644 index 6a77b6600d..0000000000 --- a/src/test/java/com/fishercoder/_417Test.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._417; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _417Test { - private static _417.Solution1 solution1; - private static int[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _417.Solution1(); - } - - @Test - public void test1() { - matrix = new int[][]{ - {2, 3, 5}, - {3, 4, 4}, - {5, 3, 1}, - }; - for (int[] arr : solution1.pacificAtlantic(matrix)) { - CommonUtils.printArray(arr); - } - } - - @Test - public void test2() { - matrix = new int[][]{ - {3, 5}, - {4, 4}, - }; - for (int[] arr : solution1.pacificAtlantic(matrix)) { - CommonUtils.printArray(arr); - } - } - - @Test - public void test3() { - matrix = new int[][]{ - {1, 2, 2, 3, 5}, - {3, 2, 3, 4, 4}, - {2, 4, 5, 3, 1}, - {6, 7, 1, 4, 5}, - {5, 1, 1, 2, 4}, - }; - for (int[] arr : solution1.pacificAtlantic(matrix)) { - CommonUtils.printArray(arr); - } - } - - @Test - public void test4() { - matrix = new int[][]{ - {2, 3, 5}, - {3, 4, 4}, - }; - for (int[] arr : solution1.pacificAtlantic(matrix)) { - CommonUtils.printArray(arr); - } - } - -} diff --git a/src/test/java/com/fishercoder/_418Test.java b/src/test/java/com/fishercoder/_418Test.java deleted file mode 100644 index f80ee55266..0000000000 --- a/src/test/java/com/fishercoder/_418Test.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._418; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _418Test { - private static _418.Solution1 test; - private static String[] sentence; - - @BeforeClass - public static void setup() { - test = new _418.Solution1(); - } - - @Test - public void test1() { - sentence = new String[]{"hello", "world"}; - assertEquals(1, test.wordsTyping(sentence, 2, 8)); - } - - @Test - public void test2() { - sentence = new String[]{"a", "bcd", "e"}; - assertEquals(2, test.wordsTyping(sentence, 3, 6)); - } - - @Test - public void test3() { - sentence = new String[]{"I", "had", "apple", "pie"}; - assertEquals(1, test.wordsTyping(sentence, 4, 5)); - } - - @Test - public void test4() { - sentence = new String[]{"f", "p", "a"}; - assertEquals(10, test.wordsTyping(sentence, 8, 7)); - } - - @Test - public void test5() { - sentence = new String[]{"hello", "leetcode"}; - assertEquals(1, test.wordsTyping(sentence, 1, 20)); - } - - @Test - public void test6() { - sentence = new String[]{"h"}; - assertEquals(4, test.wordsTyping(sentence, 2, 3)); - } -} diff --git a/src/test/java/com/fishercoder/_41Test.java b/src/test/java/com/fishercoder/_41Test.java deleted file mode 100644 index e61b907f82..0000000000 --- a/src/test/java/com/fishercoder/_41Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._41; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _41Test { - private static _41.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _41.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {1, 2, 0}; - assertEquals(3, solution1.firstMissingPositive(nums)); - } - - @Test - public void test2() { - nums = new int[] {}; - assertEquals(1, solution1.firstMissingPositive(nums)); - } - - @Test - public void test3() { - nums = new int[] {3, 4, -1, 1}; - assertEquals(2, solution1.firstMissingPositive(nums)); - } - - @Test - public void test4() { - nums = new int[] {2}; - assertEquals(1, solution1.firstMissingPositive(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_421Test.java b/src/test/java/com/fishercoder/_421Test.java deleted file mode 100644 index 115ad050ec..0000000000 --- a/src/test/java/com/fishercoder/_421Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._421; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _421Test { - private static _421.Solution1 solution1; - private static int expected; - private static int actual; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _421.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{3, 10, 5, 25, 2, 8}; - expected = 28; - actual = solution1.findMaximumXOR(nums); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_422Test.java b/src/test/java/com/fishercoder/_422Test.java deleted file mode 100644 index da24345bc7..0000000000 --- a/src/test/java/com/fishercoder/_422Test.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._422; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _422Test { - private static _422.Solution1 test; - private static boolean expected; - private static boolean actual; - private static List words; - - @BeforeClass - public static void setup() { - test = new _422.Solution1(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - words = new ArrayList<>(Arrays.asList("abcd", "bnrt", "crmy", "dtye")); - expected = true; - actual = test.validWordSquare(words); - assertEquals(expected, actual); - } - - @Test - public void test2() { - words = new ArrayList<>(Arrays.asList("abcd", "bnrt", "crm", "dt")); - expected = true; - actual = test.validWordSquare(words); - assertEquals(expected, actual); - } - - @Test - public void test3() { - words = new ArrayList<>(Arrays.asList("ball", "asee", "let", "lep")); - expected = false; - actual = test.validWordSquare(words); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_423Test.java b/src/test/java/com/fishercoder/_423Test.java deleted file mode 100644 index 92102bdf55..0000000000 --- a/src/test/java/com/fishercoder/_423Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._423; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _423Test { - private static _423.Solution1 solution1; - private static String expected; - private static String actual; - private static String s; - - @BeforeClass - public static void setup() { - solution1 = new _423.Solution1(); - } - - @Test - public void test1() { - s = "fviefuro"; - expected = "45"; - actual = solution1.originalDigits(s); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_424Test.java b/src/test/java/com/fishercoder/_424Test.java deleted file mode 100644 index 99b42f00b4..0000000000 --- a/src/test/java/com/fishercoder/_424Test.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._424; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _424Test { - private static _424.Solution1 solution1; - private static String s; - private static int k; - private static int actual; - private static int expected; - - @BeforeClass - public static void setup() { - solution1 = new _424.Solution1(); - } - - @Test - public void test1() { - s = "ABAB"; - k = 2; - actual = solution1.characterReplacement(s, k); - expected = 4; - assertEquals(expected, actual); - } - - @Test - public void test2() { - s = "AABABBA"; - k = 1; - actual = solution1.characterReplacement(s, k); - expected = 4; - assertEquals(expected, actual); - } - - @Test - public void test3() { - s = "AAAA"; - k = 2; - actual = solution1.characterReplacement(s, k); - expected = 4; - assertEquals(expected, actual); - } - - @Test - public void test4() { - s = "AAAB"; - k = 0; - actual = solution1.characterReplacement(s, k); - expected = 3; - assertEquals(expected, actual); - } - - @Test - public void test5() { - s = "AABA"; - k = 0; - actual = solution1.characterReplacement(s, k); - expected = 2; - assertEquals(expected, actual); - } - - @Test - public void test6() { - s = "ABBB"; - k = 2; - actual = solution1.characterReplacement(s, k); - expected = 4; - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_425Test.java b/src/test/java/com/fishercoder/_425Test.java deleted file mode 100644 index 3577766fe7..0000000000 --- a/src/test/java/com/fishercoder/_425Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._425; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.List; - -public class _425Test { - private static _425.Solution1 solution1; - private static String[] words; - - @BeforeClass - public static void setup() { - solution1 = new _425.Solution1(); - } - - @Test - public void test1() { - words = new String[]{"area", "lead", "wall", "lady", "ball"}; - List> result = solution1.wordSquares(words); - CommonUtils.printListList(result); - } -} diff --git a/src/test/java/com/fishercoder/_429Test.java b/src/test/java/com/fishercoder/_429Test.java deleted file mode 100644 index f527473138..0000000000 --- a/src/test/java/com/fishercoder/_429Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.Node; -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._429; -import com.fishercoder.solutions._98; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _429Test { - private static _429.Solution1 solution1; - private static Node root; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _429.Solution1(); - } - - @Test - public void test1() { - root = new Node(1); - Node node3 = new Node(3); - Node node2 = new Node(2); - Node node4 = new Node(4); - root.children = Arrays.asList(node3, node2, node4); - Node node5 = new Node(5); - Node node6 = new Node(6); - node3.children = Arrays.asList(node5, node6); - expected = new ArrayList<>(); - expected.add(Arrays.asList(1)); - expected.add(Arrays.asList(3, 2, 4)); - expected.add(Arrays.asList(5, 6)); - assertEquals(expected, solution1.levelOrder(root)); - } - - @Test - public void test2() { - root = null; - expected = new ArrayList<>(); - assertEquals(expected, solution1.levelOrder(root)); - } -} diff --git a/src/test/java/com/fishercoder/_42Test.java b/src/test/java/com/fishercoder/_42Test.java deleted file mode 100644 index 2ef0e96c7e..0000000000 --- a/src/test/java/com/fishercoder/_42Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._42; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/13/17. - */ -public class _42Test { - private static _42.Solution1 solution1; - private static int[] height; - - @BeforeClass - public static void setup() { - solution1 = new _42.Solution1(); - } - - @Test - public void test1() { - height = new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; - assertEquals(6, solution1.trap(height)); - } - - -} diff --git a/src/test/java/com/fishercoder/_434Test.java b/src/test/java/com/fishercoder/_434Test.java deleted file mode 100644 index be0022cc35..0000000000 --- a/src/test/java/com/fishercoder/_434Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._434; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _434Test { - private static _434.Solution1 solution1; - private static int expected; - private static int actual; - private static String s; - - @BeforeClass - public static void setup() { - solution1 = new _434.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = 0; - actual = 0; - } - - @Test - public void test1() { - s = "Hello, my name is John"; - expected = 5; - actual = solution1.countSegments(s); - assertEquals(expected, actual); - } - - @Test - public void test2() { - s = ", , , , a, eaefa"; - expected = 6; - actual = solution1.countSegments(s); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_435Test.java b/src/test/java/com/fishercoder/_435Test.java deleted file mode 100644 index e002809778..0000000000 --- a/src/test/java/com/fishercoder/_435Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.Interval; -import com.fishercoder.solutions._435; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _435Test { - private static _435.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _435.Solution1(); - } - - @Test - public void test1() { - Interval interval1 = new Interval(1, 100); - Interval interval2 = new Interval(11, 22); - Interval interval3 = new Interval(1, 11); - Interval interval4 = new Interval(2, 12); - Interval[] intervals = new Interval[]{interval1, interval2, interval3, interval4}; - assertEquals(2, solution1.eraseOverlapIntervals(intervals)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_436Test.java b/src/test/java/com/fishercoder/_436Test.java deleted file mode 100644 index 9b4c22f24b..0000000000 --- a/src/test/java/com/fishercoder/_436Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.Interval; -import com.fishercoder.solutions._436; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _436Test { - private static _436.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _436.Solution1(); - } - - @Test - public void test1() { - Interval[] intervals = new Interval[3]; - intervals[0] = new Interval(3, 4); - intervals[1] = new Interval(2, 3); - intervals[2] = new Interval(1, 2); - solution1.findRightInterval(intervals); - } - - @Test - public void test2() { - Interval[] intervals = new Interval[3]; - intervals[0] = new Interval(1, 4); - intervals[1] = new Interval(2, 3); - intervals[2] = new Interval(3, 4); - solution1.findRightInterval(intervals); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_439Test.java b/src/test/java/com/fishercoder/_439Test.java deleted file mode 100644 index debf8d46af..0000000000 --- a/src/test/java/com/fishercoder/_439Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._439; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/18/17. - */ -public class _439Test { - private static _439.Solution1 solution1; - private static String expression; - private static String expected; - - @BeforeClass - public static void setup() { - solution1 = new _439.Solution1(); - } - - @Test - public void test1() { - expression = "T?2:3"; - expected = "2"; - assertEquals(expected, solution1.parseTernary(expression)); - } - - @Test - public void test2() { - expression = "F?1:T?4:5"; - expected = "4"; - assertEquals(expected, solution1.parseTernary(expression)); - } - - @Test - public void test3() { - expression = "T?T?F:5:3"; - expected = "F"; - assertEquals(expected, solution1.parseTernary(expression)); - } - - @Test - public void test4() { - expression = "T?T:F?T?1:2:F?3:4"; - expected = "T"; - assertEquals(expected, solution1.parseTernary(expression)); - } -} diff --git a/src/test/java/com/fishercoder/_43Test.java b/src/test/java/com/fishercoder/_43Test.java deleted file mode 100644 index 7fcdf911fd..0000000000 --- a/src/test/java/com/fishercoder/_43Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._43; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/28/17. - */ -public class _43Test { - private static _43 test; - - @BeforeClass - public static void setup() { - test = new _43(); - } - - @Test - public void test1() { - assertEquals("5535", test.multiply("123", "45")); - } - - @Test - public void test2() { - assertEquals("0", test.multiply("9133", "0")); - } - - @Test - public void test3() { - assertEquals("491555843274052692", test.multiply("6913259244", "71103343")); - } - - @Test - public void test4() { - assertEquals("67143675422804947379429215144664313370120390398055713625298709447", test.multiply("401716832807512840963", "167141802233061013023557397451289113296441069")); - } -} diff --git a/src/test/java/com/fishercoder/_441Test.java b/src/test/java/com/fishercoder/_441Test.java deleted file mode 100644 index 19b9fd0c5e..0000000000 --- a/src/test/java/com/fishercoder/_441Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._441; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _441Test { - private static _441.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _441.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.arrangeCoins(3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_443Test.java b/src/test/java/com/fishercoder/_443Test.java deleted file mode 100644 index 6b5d3aecaa..0000000000 --- a/src/test/java/com/fishercoder/_443Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._443; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _443Test { - private static _443.Solution1 solution1; - private static char[] chars; - - @BeforeClass - public static void setup() { - solution1 = new _443.Solution1(); - } - - @Test - public void test1() { - chars = new char[]{'a', 'a', 'b', 'b', 'c', 'c', 'c'}; - assertEquals(6, solution1.compress(chars)); - } - - @Test - public void test2() { - chars = new char[]{'a'}; - assertEquals(1, solution1.compress(chars)); - } - - @Test - public void test3() { - chars = new char[]{'a', 'b'}; - assertEquals(2, solution1.compress(chars)); - } - - @Test - public void test4() { - chars = new char[]{'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'}; - assertEquals(4, solution1.compress(chars)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_444Test.java b/src/test/java/com/fishercoder/_444Test.java deleted file mode 100644 index a1bee622ec..0000000000 --- a/src/test/java/com/fishercoder/_444Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._444; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _444Test { - private static _444.Solution1 solution1; - private static int[] org; - private static List> seqs; - - @BeforeClass - public static void setup() { - solution1 = new _444.Solution1(); - } - - @Test - public void test1() { - org = new int[]{1, 2, 3}; - seqs = new ArrayList<>(); - seqs.add(Arrays.asList(1, 2)); - seqs.add(Arrays.asList(1, 3)); - assertEquals(false, solution1.sequenceReconstruction(org, seqs)); - } -} diff --git a/src/test/java/com/fishercoder/_445Test.java b/src/test/java/com/fishercoder/_445Test.java deleted file mode 100644 index 4c77b7c364..0000000000 --- a/src/test/java/com/fishercoder/_445Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._445; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/13/17. - */ -public class _445Test { - private static _445.Solution1 solution1; - private static _445.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _445.Solution1(); - solution2 = new _445.Solution2(); - } - - @Test - public void test1() { - ListNode l1 = LinkedListUtils.contructLinkedList(new int[]{7, 2, 4, 3}); - - ListNode l2 = LinkedListUtils.contructLinkedList(new int[]{5, 6, 4}); - - ListNode expected = LinkedListUtils.contructLinkedList(new int[]{7, 8, 0, 7}); - - assertEquals(expected, solution1.addTwoNumbers(l1, l2)); - } - - @Test - public void test2() { - ListNode l1 = LinkedListUtils.contructLinkedList(new int[]{7, 2, 4, 3}); - - ListNode l2 = LinkedListUtils.contructLinkedList(new int[]{5, 6, 4}); - - ListNode expected = LinkedListUtils.contructLinkedList(new int[]{7, 8, 0, 7}); - - assertEquals(expected, solution2.addTwoNumbers(l1, l2)); - } - - @Test - public void test3() { - ListNode l1 = LinkedListUtils.contructLinkedList(new int[]{5}); - - ListNode l2 = LinkedListUtils.contructLinkedList(new int[]{5}); - - ListNode expected = LinkedListUtils.contructLinkedList(new int[]{1, 0}); - - assertEquals(expected, solution2.addTwoNumbers(l1, l2)); - } -} diff --git a/src/test/java/com/fishercoder/_447Test.java b/src/test/java/com/fishercoder/_447Test.java deleted file mode 100644 index c6ae1cbd0d..0000000000 --- a/src/test/java/com/fishercoder/_447Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._447; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _447Test { - private static _447.Solution1 solution1; - private static int[][] points; - - @BeforeClass - public static void setup() { - solution1 = new _447.Solution1(); - } - - @Test - public void test1() { - points = new int[][]{ - {0, 0}, - {1, 0}, - {2, 0}, - }; - assertEquals(2, solution1.numberOfBoomerangs(points)); - } - - @Test - public void test2() { - points = new int[][]{ - {3, 6}, - {7, 5}, - {3, 5}, - {6, 2}, - {9, 1}, - {2, 7}, - {0, 9}, - {0, 6}, - {2, 6} - }; - assertEquals(10, solution1.numberOfBoomerangs(points)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_449Test.java b/src/test/java/com/fishercoder/_449Test.java deleted file mode 100644 index 96ea8a499f..0000000000 --- a/src/test/java/com/fishercoder/_449Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._449; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _449Test { - private static _449.Solution1 solution1; - private static _449.Solution2 solution2; - private static _449.Solution3 solution3; - private static TreeNode expectedRoot; - - @BeforeClass - public static void setup() { - solution1 = new _449.Solution1(); - solution2 = new _449.Solution2(); - solution3 = new _449.Solution3(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - expectedRoot = new TreeNode(3); - expectedRoot.left = new TreeNode(1); - expectedRoot.right = new TreeNode(4); - expectedRoot.left.right = new TreeNode(2); - assertEquals(expectedRoot.toString(), solution1.deserialize(solution1.serialize(expectedRoot)).toString()); - assertEquals(expectedRoot.toString(), solution2.deserialize(solution2.serialize(expectedRoot)).toString()); - assertEquals(expectedRoot.toString(), solution3.deserialize(solution3.serialize(expectedRoot)).toString()); - } - - @Test - public void test2() { - expectedRoot = new TreeNode(2); - expectedRoot.left = new TreeNode(1); - expectedRoot.right = new TreeNode(3); - assertEquals(expectedRoot.toString(), solution1.deserialize(solution1.serialize(expectedRoot)).toString()); - assertEquals(expectedRoot.toString(), solution2.deserialize(solution2.serialize(expectedRoot)).toString()); - assertEquals(expectedRoot.toString(), solution3.deserialize(solution3.serialize(expectedRoot)).toString()); - } -} diff --git a/src/test/java/com/fishercoder/_44Test.java b/src/test/java/com/fishercoder/_44Test.java deleted file mode 100644 index 9ccc443fc7..0000000000 --- a/src/test/java/com/fishercoder/_44Test.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._44; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _44Test { - private static _44.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _44.Solution1(); - } - - @Test - public void test1() { - assertEquals(false, solution1.isMatch("aa", "a")); - } - - @Test - public void test2() { - assertEquals(true, solution1.isMatch("aa", "aa")); - } - - @Test - public void test3() { - assertEquals(false, solution1.isMatch("aaa", "aa")); - } - - @Test - public void test4() { - assertEquals(true, solution1.isMatch("aa", "*")); - } - - @Test - public void test5() { - assertEquals(true, solution1.isMatch("aa", "a*")); - } - - @Test - public void test6() { - assertEquals(true, solution1.isMatch("ab", "?*")); - } - - @Test - public void test7() { - assertEquals(false, solution1.isMatch("aab", "c*a*b")); - } -} diff --git a/src/test/java/com/fishercoder/_450Test.java b/src/test/java/com/fishercoder/_450Test.java deleted file mode 100644 index 853df8c16d..0000000000 --- a/src/test/java/com/fishercoder/_450Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._450; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _450Test { - private static _450.Solution1 solution1; - private static TreeNode expected; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _450.Solution1(); - } - - @Test - public void test1() { - root = new TreeNode(5); - root.left = new TreeNode(3); - root.left.left = new TreeNode(2); - root.left.right = new TreeNode(4); - root.right = new TreeNode(6); - root.right.right = new TreeNode(7); - - expected = new TreeNode(5); - expected.left = new TreeNode(4); - expected.right = new TreeNode(6); - expected.left.left = new TreeNode(2); - expected.right.right = new TreeNode(7); - - assertEquals(expected, solution1.deleteNode(root, 3)); - } -} diff --git a/src/test/java/com/fishercoder/_451Test.java b/src/test/java/com/fishercoder/_451Test.java deleted file mode 100644 index cd7a2d6cb1..0000000000 --- a/src/test/java/com/fishercoder/_451Test.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._451; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 1/15/17. - */ -public class _451Test { - private static _451.Solution1 solution1; - private static String expected; - private static String actual; - private static String input; - - @BeforeClass - public static void setup() { - solution1 = new _451.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = ""; - actual = ""; - } - - @Test - public void test1() { - input = "tree"; - expected = "eert"; - actual = solution1.frequencySort(input); - assertEquals(expected, actual); - } - - @Test - public void test2() { - input = "cccaaa"; - expected = "aaaccc"; - actual = solution1.frequencySort(input); - assertEquals(expected, actual); - } - - @Test - public void test3() { - input = "Aabb"; - expected = "bbAa"; - actual = solution1.frequencySort(input); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_453Test.java b/src/test/java/com/fishercoder/_453Test.java deleted file mode 100644 index a5cbeed79b..0000000000 --- a/src/test/java/com/fishercoder/_453Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._453; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - - -public class _453Test { - private static _453.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _453.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.minMoves(new int[]{1, 2, 3})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_454Test.java b/src/test/java/com/fishercoder/_454Test.java deleted file mode 100644 index 7a55460af6..0000000000 --- a/src/test/java/com/fishercoder/_454Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._454; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _454Test { - private static _454.Solution1 solution1; - private static int expected; - private static int actual; - private static int[] A; - private static int[] B; - private static int[] C; - private static int[] D; - - @BeforeClass - public static void setup() { - solution1 = new _454.Solution1(); - } - - @Test - public void test1() { - A = new int[]{1, 2}; - B = new int[]{-2, -1}; - C = new int[]{-1, 2}; - D = new int[]{0, 2}; - expected = 2; - actual = solution1.fourSumCount(A, B, C, D); - assertEquals(expected, actual); - - } -} diff --git a/src/test/java/com/fishercoder/_455Test.java b/src/test/java/com/fishercoder/_455Test.java deleted file mode 100644 index eb52156073..0000000000 --- a/src/test/java/com/fishercoder/_455Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._455; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _455Test { - private static _455.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _455.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.findContentChildren(new int[]{1, 2, 3}, new int[]{1, 1})); - - } - - @Test - public void test2() { - assertEquals(2, solution1.findContentChildren(new int[]{1, 2}, new int[]{1, 2, 3})); - - } -} diff --git a/src/test/java/com/fishercoder/_456Test.java b/src/test/java/com/fishercoder/_456Test.java deleted file mode 100644 index e4d5e1cd4c..0000000000 --- a/src/test/java/com/fishercoder/_456Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._456; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _456Test { - private static _456.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _456.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.find132pattern(new int[]{-1, 3, 2, 0})); - - } - -} diff --git a/src/test/java/com/fishercoder/_457Test.java b/src/test/java/com/fishercoder/_457Test.java deleted file mode 100644 index 67952196a8..0000000000 --- a/src/test/java/com/fishercoder/_457Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._457; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _457Test { - private static _457.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _457.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{2, -1, 1, 2, 2}; - assertEquals(true, solution1.circularArrayLoop(nums)); - } - - @Test - public void test2() { - nums = new int[]{-1, 2}; - assertEquals(false, solution1.circularArrayLoop(nums)); - } - - @Test - public void test3() { - nums = new int[]{-1, 2, 3}; - assertEquals(false, solution1.circularArrayLoop(nums)); - } - - @Test - public void test4() { - nums = new int[]{2, 1, 9}; - assertEquals(false, solution1.circularArrayLoop(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_459Test.java b/src/test/java/com/fishercoder/_459Test.java deleted file mode 100644 index 9f3163ead1..0000000000 --- a/src/test/java/com/fishercoder/_459Test.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._459; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _459Test { - private static _459.Solution1 solution1; - private static _459.Solution2 solution2; - private static _459.Solution3 solution3; - - @BeforeClass - public static void setup() { - solution1 = new _459.Solution1(); - solution2 = new _459.Solution2(); - solution3 = new _459.Solution3(); - } - - @Test - public void test1() { - assertEquals(true, solution1.repeatedSubstringPattern("abab")); - assertEquals(true, solution2.repeatedSubstringPattern("abab")); - assertEquals(true, solution3.repeatedSubstringPattern("abab")); - } - - @Test - public void test2() { - assertEquals(false, solution1.repeatedSubstringPattern("aba")); - assertEquals(false, solution2.repeatedSubstringPattern("aba")); - assertEquals(false, solution3.repeatedSubstringPattern("aba")); - } - - @Test - public void test3() { - assertEquals(false, solution1.repeatedSubstringPattern("a")); - assertEquals(false, solution2.repeatedSubstringPattern("a")); - assertEquals(false, solution3.repeatedSubstringPattern("a")); - } - - @Test - public void test4() { - assertEquals(false, solution1.repeatedSubstringPattern("qtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvbus")); - assertEquals(false, solution2.repeatedSubstringPattern("qtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvbus")); - assertEquals(false, solution3.repeatedSubstringPattern("qtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvbus")); - } - - @Test - public void test5() { - assertEquals(true, solution1.repeatedSubstringPattern("ababab")); - assertEquals(true, solution2.repeatedSubstringPattern("ababab")); - assertEquals(true, solution3.repeatedSubstringPattern("ababab")); - } - - @Test - public void test6() { - assertEquals(false, solution1.repeatedSubstringPattern("abababc")); - assertEquals(false, solution2.repeatedSubstringPattern("abababc")); - assertEquals(false, solution3.repeatedSubstringPattern("abababc")); - } - - @Test - public void test7() { - assertEquals(false, solution1.repeatedSubstringPattern("abababaaba")); - assertEquals(false, solution2.repeatedSubstringPattern("abababaaba")); - assertEquals(false, solution3.repeatedSubstringPattern("abababaaba")); - } -} diff --git a/src/test/java/com/fishercoder/_45Test.java b/src/test/java/com/fishercoder/_45Test.java deleted file mode 100644 index 1b73069a26..0000000000 --- a/src/test/java/com/fishercoder/_45Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._45; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _45Test { - private static _45.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _45.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {2, 3, 1, 1, 4}; - assertEquals(2, solution1.jump(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_460Test.java b/src/test/java/com/fishercoder/_460Test.java deleted file mode 100644 index aae065c354..0000000000 --- a/src/test/java/com/fishercoder/_460Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._460; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _460Test { - - private static _460.Solution1.LFUCache lfuCache; - - @Test - public void test1() { - lfuCache = new _460.Solution1.LFUCache(2); - lfuCache.put(1, 1); - lfuCache.put(2, 2); - assertEquals(1, lfuCache.get(1)); - lfuCache.put(3, 3); - assertEquals(-1, lfuCache.get(2)); - assertEquals(3, lfuCache.get(3)); - lfuCache.put(4, 4); - assertEquals(-1, lfuCache.get(1)); - assertEquals(3, lfuCache.get(3)); - assertEquals(4, lfuCache.get(4)); - } -} diff --git a/src/test/java/com/fishercoder/_462Test.java b/src/test/java/com/fishercoder/_462Test.java deleted file mode 100644 index 5b4942665b..0000000000 --- a/src/test/java/com/fishercoder/_462Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._462; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _462Test { - private static _462.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _462.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.minMoves2(new int[]{1, 2, 3})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_467Test.java b/src/test/java/com/fishercoder/_467Test.java deleted file mode 100644 index b6c8eab151..0000000000 --- a/src/test/java/com/fishercoder/_467Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._467; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _467Test { - private static _467.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _467.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.findSubstringInWraproundString("a")); - - } - -} diff --git a/src/test/java/com/fishercoder/_46Test.java b/src/test/java/com/fishercoder/_46Test.java deleted file mode 100644 index 506567ecc5..0000000000 --- a/src/test/java/com/fishercoder/_46Test.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._46; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _46Test { - private static _46.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _46.Solution1(); - } - - @Test - public void test1() { - CommonUtils.printListList(solution1.permute(new int[]{1, 2, 3})); - } -} diff --git a/src/test/java/com/fishercoder/_473Test.java b/src/test/java/com/fishercoder/_473Test.java deleted file mode 100644 index afe7f74020..0000000000 --- a/src/test/java/com/fishercoder/_473Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._473; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _473Test { - private static _473.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _473.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 1, 2, 2, 2}; - assertEquals(true, solution1.makesquare(nums)); - } - - @Test - public void test2() { - nums = new int[]{3, 3, 3, 3, 4}; - assertEquals(false, solution1.makesquare(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_474Test.java b/src/test/java/com/fishercoder/_474Test.java deleted file mode 100644 index 35d60c5ae0..0000000000 --- a/src/test/java/com/fishercoder/_474Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._474; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _474Test { - private static _474.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _474.Solution1(); - } - - @Test - public void test1() { - assertEquals(4, solution1.findMaxForm(new String[]{"10", "0001", "111001", "1", "0"}, 5, 3)); - } - -} diff --git a/src/test/java/com/fishercoder/_475Test.java b/src/test/java/com/fishercoder/_475Test.java deleted file mode 100644 index 7906fdeaee..0000000000 --- a/src/test/java/com/fishercoder/_475Test.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._475; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 4/23/17. - */ -public class _475Test { - private static _475.Solution1 test; - private static int expected; - private static int actual; - private static int[] houses; - private static int[] heaters; - - @BeforeClass - public static void setup() { - test = new _475.Solution1(); - } - - @Test - public void test1() { - houses = new int[]{1, 2, 3}; - heaters = new int[]{2}; - expected = 1; - actual = test.findRadius(houses, heaters); - assertEquals(expected, actual); - } - - @Test - public void test2() { - houses = new int[]{1, 2, 3, 4}; - heaters = new int[]{1, 4}; - expected = 1; - actual = test.findRadius(houses, heaters); - assertEquals(expected, actual); - } - - @Test - public void test3() { - houses = new int[]{1}; - heaters = new int[]{1, 2, 3, 4}; - expected = 0; - actual = test.findRadius(houses, heaters); - assertEquals(expected, actual); - } - - @Test - public void test4() { - houses = new int[]{1, 2, 3, 5, 15}; - heaters = new int[]{2, 30}; - expected = 13; - actual = test.findRadius(houses, heaters); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_476Test.java b/src/test/java/com/fishercoder/_476Test.java deleted file mode 100644 index 8a8160af0e..0000000000 --- a/src/test/java/com/fishercoder/_476Test.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._476; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 1/14/17. - */ -public class _476Test { - private static _476.Solution1 solution1; - private static _476.Solution2 solution2; - private static int expected; - private static int actual; - private static int input; - - @BeforeClass - public static void setup() { - solution1 = new _476.Solution1(); - solution2 = new _476.Solution2(); - } - - @Before - public void setupForEachTest() { - expected = 0; - actual = 0; - input = 0; - } - - @Test - public void test1() { - - input = 5; - expected = 2; - actual = solution1.findComplement(input); - actual = solution2.findComplement(input); - assertEquals(expected, actual); - - } - - @Test - public void test2() { - - input = 5; - expected = 2; - actual = solution1.findComplement(input); - actual = solution2.findComplement(input); - assertEquals(expected, actual); - - } -} diff --git a/src/test/java/com/fishercoder/_479Test.java b/src/test/java/com/fishercoder/_479Test.java deleted file mode 100644 index 0b1d0ddad1..0000000000 --- a/src/test/java/com/fishercoder/_479Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._479; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _479Test { - private static _479.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _479.Solution1(); - } - - @Test - public void test1() { - assertEquals(123, solution1.largestPalindrome(3)); - } -} diff --git a/src/test/java/com/fishercoder/_47Test.java b/src/test/java/com/fishercoder/_47Test.java deleted file mode 100644 index 4ab52b0473..0000000000 --- a/src/test/java/com/fishercoder/_47Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._47; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.List; - -public class _47Test { - private static _47.Solution1 solution1; - private static List> actual; - - @BeforeClass - public static void setup() { - solution1 = new _47.Solution1(); - } - - @Test - public void test1() { - actual = solution1.permuteUnique(new int[]{1, 1, 2}); - CommonUtils.printListList(actual); - } - -} diff --git a/src/test/java/com/fishercoder/_480Test.java b/src/test/java/com/fishercoder/_480Test.java deleted file mode 100644 index ebe40df945..0000000000 --- a/src/test/java/com/fishercoder/_480Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._480; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -/** - * Created by fishercoder on 5/27/17. - */ -public class _480Test { - private static _480.Solution1 solution1; - private static int[] nums; - private static double[] expected; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _480.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 3, -1, -3, 5, 3, 6, 7}; - expected = new double[]{1, -1, -1, 3, 5, 6}; - k = 3; - assertArrayEquals(expected, solution1.medianSlidingWindow(nums, k), 0); - } -} diff --git a/src/test/java/com/fishercoder/_485Test.java b/src/test/java/com/fishercoder/_485Test.java deleted file mode 100644 index d2954f600b..0000000000 --- a/src/test/java/com/fishercoder/_485Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._485; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _485Test { - private static _485.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _485.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.findMaxConsecutiveOnes(new int[]{1, 1, 0, 1, 1, 1})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_487Test.java b/src/test/java/com/fishercoder/_487Test.java deleted file mode 100644 index 7caf955387..0000000000 --- a/src/test/java/com/fishercoder/_487Test.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._487; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _487Test { - private static _487 test; - private static int[] nums; - private static int expected; - private static int actual; - - @BeforeClass - public static void setup() { - test = new _487(); - } - - @Before - public void setupForEachTest() { - expected = 0; - actual = 0; - nums = new int[1000]; - } - - @Test - public void test1() { - nums = new int[]{1, 1, 0, 1, 1, 1}; - expected = 3; - actual = test.findMaxConsecutiveOnes(nums); - assertEquals(expected, actual); - - } - - @Test - public void test2() { - nums = new int[]{1, 1, 1, 1, 1, 1}; - expected = 6; - actual = test.findMaxConsecutiveOnes(nums); - assertEquals(expected, actual); - - } - - @Test - public void test3() { - nums = new int[]{}; - expected = 0; - actual = test.findMaxConsecutiveOnes(nums); - assertEquals(expected, actual); - - } -} diff --git a/src/test/java/com/fishercoder/_48Test.java b/src/test/java/com/fishercoder/_48Test.java deleted file mode 100644 index d07eb57fd3..0000000000 --- a/src/test/java/com/fishercoder/_48Test.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._48; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _48Test { - private static _48.Solution1 solution1; - private static _48.Solution2 solution2; - private static _48.Solution3 solution3; - private static int[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _48.Solution1(); - solution2 = new _48.Solution2(); - solution3 = new _48.Solution3(); - } - - @Test - public void test1() { - matrix = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9}, - }; - solution1.rotate(matrix); - CommonUtils.print2DIntArray(matrix); - } - - @Test - public void test2() { - matrix = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9}, - }; - solution2.rotate(matrix); - CommonUtils.print2DIntArray(matrix); - } - - @Test - public void test3() { - matrix = new int[][]{ - {1, 2, 3, 4}, - {5, 6, 7, 8}, - {9, 10, 11, 12}, - {13, 14, 15, 16} - }; - solution2.rotate(matrix); - CommonUtils.print2DIntArray(matrix); - } - - @Test - public void test4() { - matrix = new int[][]{ - {1, 2}, - {3, 4} - }; - solution1.rotate(matrix); - CommonUtils.print2DIntArray(matrix); - } - - @Test - public void test5() { - matrix = new int[][]{ - {1, 2, 3, 4}, - {5, 6, 7, 8}, - {9, 10, 11, 12}, - {13, 14, 15, 16} - }; - solution3.rotate(matrix); - CommonUtils.print2DIntArray(matrix); - } - - @Test - public void test6() { - matrix = new int[][]{ - {1, 2, 3, 4}, - {5, 6, 7, 8}, - {9, 10, 11, 12}, - {13, 14, 15, 16} - }; - solution1.rotate(matrix); - CommonUtils.print2DIntArray(matrix); - } -} diff --git a/src/test/java/com/fishercoder/_490Test.java b/src/test/java/com/fishercoder/_490Test.java deleted file mode 100644 index 8195eb745e..0000000000 --- a/src/test/java/com/fishercoder/_490Test.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._490; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _490Test { - private static _490 test; - private static boolean expected; - private static boolean actual; - private static int[][] maze; - private static int[] start; - private static int[] destination; - - @BeforeClass - public static void setup() { - test = new _490(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - maze = new int[][]{ - {0, 0, 0, 0, 0}, - {1, 1, 0, 0, 1}, - {0, 0, 0, 0, 0}, - {0, 1, 0, 0, 1}, - {0, 1, 0, 0, 0} - }; - start = new int[]{4, 3}; - destination = new int[]{0, 1}; - actual = test.hasPath(maze, start, destination); - expected = false; - assertEquals(expected, actual); - - } - - @Test - public void test2() { - maze = new int[][]{ - {0, 0, 1, 0, 0}, - {0, 0, 0, 0, 0}, - {0, 0, 0, 1, 0}, - {1, 1, 0, 1, 1}, - {0, 0, 0, 0, 0} - }; - start = new int[]{0, 4}; - destination = new int[]{4, 4}; - actual = test.hasPath(maze, start, destination); - expected = true; - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_491Test.java b/src/test/java/com/fishercoder/_491Test.java deleted file mode 100644 index 26334567a1..0000000000 --- a/src/test/java/com/fishercoder/_491Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._491; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.List; - -public class _491Test { - private static _491.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _491.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {4, 6, 7, 7}; - List> actual = solution1.findSubsequences(nums); - CommonUtils.printListList(actual); - } -} diff --git a/src/test/java/com/fishercoder/_492Test.java b/src/test/java/com/fishercoder/_492Test.java deleted file mode 100644 index 59faba5e0e..0000000000 --- a/src/test/java/com/fishercoder/_492Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._492; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -/** - * Created by fishercoder on 1/25/17. - */ -public class _492Test { - private static _492.Solution1 solution1; - private static int[] expected; - private static int[] actual; - private static int area; - - @BeforeClass - public static void setup() { - solution1 = new _492.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = new int[]{0, 0}; - actual = new int[]{0, 0}; - area = 0; - } - - @Test - public void test1() { - area = 4; - expected = new int[]{2, 2}; - actual = solution1.constructRectangle(area); - assertArrayEquals(expected, actual); - } - - @Test - public void test2() { - area = 3; - expected = new int[]{3, 1}; - actual = solution1.constructRectangle(area); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_493Test.java b/src/test/java/com/fishercoder/_493Test.java deleted file mode 100644 index a735059149..0000000000 --- a/src/test/java/com/fishercoder/_493Test.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._493; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _493Test { - private static _493.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _493.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 3, 2, 3, 1}; - assertEquals(2, solution1.reversePairs(nums)); - } - - @Test - public void test2() { - nums = new int[]{2, 4, 3, 5, 1}; - assertEquals(3, solution1.reversePairs(nums)); - } - - @Test - public void test3() { - nums = new int[]{2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647}; - assertEquals(0, solution1.reversePairs(nums)); - } - - @Test - public void test4() { - nums = new int[]{1, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647}; - assertEquals(0, solution1.reversePairs(nums)); - } - - @Test - public void test5() { - nums = new int[]{2147483647, 2147483646, 2147483647, 2147483647, 2147483647}; - assertEquals(0, solution1.reversePairs(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_494Test.java b/src/test/java/com/fishercoder/_494Test.java deleted file mode 100644 index ebac03dde1..0000000000 --- a/src/test/java/com/fishercoder/_494Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._494; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _494Test { - private static _494.Solution1 solution1; - private static int expected; - private static int actual; - private static int S; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _494.Solution1(); - } - - @Test - public void test1() { - S = 3; - nums = new int[]{1, 1, 1, 1, 1}; - expected = 5; - actual = solution1.findTargetSumWays(nums, S); - assertEquals(expected, actual); - } - - @Test - public void test2() { - S = 3; - nums = new int[]{1, 1, 1, 1, 5}; - expected = 4; - actual = solution1.findTargetSumWays(nums, S); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_495Test.java b/src/test/java/com/fishercoder/_495Test.java deleted file mode 100644 index b45330258c..0000000000 --- a/src/test/java/com/fishercoder/_495Test.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._495; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/8/17. - */ -public class _495Test { - _495.Solution1 solution1 = new _495.Solution1(); - private static int actual = 0; - private static int expected = 0; - private static int[] timeSeries; - private static int duration = 0; - - @Before - public void setup() { - timeSeries = new int[]{}; - duration = 0; - expected = 0; - actual = 0; - } - - @Test - public void test1() { - timeSeries = new int[]{1, 4}; - duration = 2; - actual = solution1.findPoisonedDuration(timeSeries, duration); - expected = 4; - assertEquals(expected, actual); - } - - @Test - public void test2() { - timeSeries = new int[]{1, 2}; - duration = 2; - actual = solution1.findPoisonedDuration(timeSeries, duration); - expected = 3; - assertEquals(expected, actual); - } - - @Test - public void test3() { - timeSeries = new int[]{}; - duration = 100000; - actual = solution1.findPoisonedDuration(timeSeries, duration); - expected = 0; - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_496Test.java b/src/test/java/com/fishercoder/_496Test.java deleted file mode 100644 index 7b2cc61289..0000000000 --- a/src/test/java/com/fishercoder/_496Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._496; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _496Test { - private static _496.Solution1 solution1; - private static int[] findNums; - private static int[] nums; - private static int[] expected; - private static int[] actual; - - @BeforeClass - public static void setup() { - solution1 = new _496.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = new int[]{}; - actual = new int[]{}; - findNums = new int[]{}; - nums = new int[]{}; - } - - @Test - public void test1() { - findNums = new int[]{4, 1, 2}; - nums = new int[]{1, 3, 4, 2}; - expected = new int[]{-1, 3, -1}; - actual = solution1.nextGreaterElement(findNums, nums); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_498Test.java b/src/test/java/com/fishercoder/_498Test.java deleted file mode 100644 index b368609783..0000000000 --- a/src/test/java/com/fishercoder/_498Test.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._498; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -/** - * Created by fishercoder on 5/26/17. - */ -public class _498Test { - private static _498.Solutoin1 solutoin1; - private static int[][] matrix; - private static int[] expected; - - @BeforeClass - public static void setup() { - solutoin1 = new _498.Solutoin1(); - } - - @Test - public void test1() { - matrix = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9}, - }; - expected = new int[]{1, 2, 4, 7, 5, 3, 6, 8, 9}; - assertArrayEquals(expected, solutoin1.findDiagonalOrder(matrix)); - } - - @Test - public void test2() { - matrix = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9}, - {10, 11, 12}, - {13, 14, 15}, - }; - expected = new int[]{1, 2, 4, 7, 5, 3, 6, 8, 10, 13, 11, 9, 12, 14, 15}; - assertArrayEquals(expected, solutoin1.findDiagonalOrder(matrix)); - } -} diff --git a/src/test/java/com/fishercoder/_49Test.java b/src/test/java/com/fishercoder/_49Test.java deleted file mode 100644 index 04cf6a46e7..0000000000 --- a/src/test/java/com/fishercoder/_49Test.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._49; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.apache.commons.collections4.CollectionUtils; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertTrue; - -public class _49Test { - private static _49.Solution1 solution1; - private static String[] words; - private static List> expected; - private static List> actual; - - @BeforeClass - public static void setup() { - solution1 = new _49.Solution1(); - } - - @Test - public void test1() { - words = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}; - List e1 = Arrays.asList("bat"); - List e2 = Arrays.asList("tan", "nat"); - List e3 = Arrays.asList("ate", "eat", "tea"); - expected = Arrays.asList(e1, e2, e3); - actual = solution1.groupAnagrams(words); - assertEquals(expected.size(), actual.size()); - assertEquals(expected.containsAll(actual), actual.containsAll(expected)); - for (List a : actual) { - switch (a.size()) { - case 1: - assertTrue(CollectionUtils.isEqualCollection(e1, a)); - break; - case 2: - assertTrue(CollectionUtils.isEqualCollection(e2, a)); - break; - case 3: - assertTrue(CollectionUtils.isEqualCollection(e3, a)); - break; - default: - //Should not have come into this branch ever. - assertTrue(false); - } - } - } -} diff --git a/src/test/java/com/fishercoder/_4Test.java b/src/test/java/com/fishercoder/_4Test.java deleted file mode 100644 index 749ba3da88..0000000000 --- a/src/test/java/com/fishercoder/_4Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._4; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _4Test { - private static _4.Solution1 solution1; - private static _4.Solution2 solution2; - private static int[] A; - private static int[] B; - - @BeforeClass - public static void setup() { - solution1 = new _4.Solution1(); - solution2 = new _4.Solution2(); - } - - @Test - public void test1() { - A = new int[]{1, 3}; - B = new int[]{2}; - assertEquals(2.0, solution1.findMedianSortedArrays(A, B), 0.0); - assertEquals(2.0, solution2.findMedianSortedArrays(A, B), 0.0); - } - - @Test - public void test2() { - A = new int[]{1, 2}; - B = new int[]{3, 4}; - assertEquals(2.5, solution1.findMedianSortedArrays(A, B), 0.0); - assertEquals(2.5, solution2.findMedianSortedArrays(A, B), 0.0); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_500Test.java b/src/test/java/com/fishercoder/_500Test.java deleted file mode 100644 index 4f7465597c..0000000000 --- a/src/test/java/com/fishercoder/_500Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._500; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -/** - * Created by fishercoder on 1/15/17. - */ -public class _500Test { - private static _500 test; - private static String[] expected; - private static String[] actual; - private String[] words; - - @BeforeClass - public static void setup() { - test = new _500(); - } - - @Test - public void test1() { - words = new String[]{"Alaska", "Hello", "Dad", "Peace"}; - expected = new String[]{"Alaska", "Dad"}; - actual = test.findWords(words); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_501Test.java b/src/test/java/com/fishercoder/_501Test.java deleted file mode 100644 index 237d2eed04..0000000000 --- a/src/test/java/com/fishercoder/_501Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._501; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -/** - * Created by fishercoder on 1/28/17. - */ -public class _501Test { - private static _501.Solution1 solution1; - private static _501.Solution2 solution2; - private static int[] expected; - private static int[] actual; - private static TreeNode treeNode; - - @BeforeClass - public static void setup() { - solution1 = new _501.Solution1(); - solution2 = new _501.Solution2(); - } - - @Before - public void setupForEachTest() { - expected = new int[]{}; - actual = new int[]{}; - treeNode = new TreeNode(0); - } - - @Test - public void test1() { - treeNode = new TreeNode(1); - treeNode.right = new TreeNode(2); - treeNode.right.left = new TreeNode(2); - expected = new int[]{2}; - CommonUtils.printArray(expected); - CommonUtils.printArray(actual); - actual = solution1.findMode(treeNode); - assertArrayEquals(expected, actual); - - actual = solution2.findMode(treeNode); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_502Test.java b/src/test/java/com/fishercoder/_502Test.java deleted file mode 100644 index 9272251d31..0000000000 --- a/src/test/java/com/fishercoder/_502Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._502; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _502Test { - private static _502.Solution1 solution1; - private static int[] Profits; - private static int[] Capital; - - @BeforeClass - public static void setup() { - solution1 = new _502.Solution1(); - } - - @Test - public void test1() { - Profits = new int[]{1, 2, 3}; - Capital = new int[]{0, 1, 1}; - assertEquals(4, solution1.findMaximizedCapital(2, 0, Profits, Capital)); - } -} diff --git a/src/test/java/com/fishercoder/_503Test.java b/src/test/java/com/fishercoder/_503Test.java deleted file mode 100644 index 61bbd8ef04..0000000000 --- a/src/test/java/com/fishercoder/_503Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._503; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _503Test { - private static _503.Solution1 solution1; - private static _503.Solution2 solution2; - private static int[] nums; - private static int[] expected; - private static int[] actual; - - @BeforeClass - public static void setup() { - solution1 = new _503.Solution1(); - solution2 = new _503.Solution2(); - } - - @Before - public void setupForEachTest() { - expected = new int[]{}; - nums = new int[]{}; - } - - @Test - public void test1() { - nums = new int[]{1, 2, 1}; - expected = new int[]{2, -1, 2}; - actual = solution1.nextGreaterElements(nums); - assertArrayEquals(expected, actual); - - actual = solution2.nextGreaterElements(nums); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_504Test.java b/src/test/java/com/fishercoder/_504Test.java deleted file mode 100644 index be7cc5d771..0000000000 --- a/src/test/java/com/fishercoder/_504Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._504; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 1/15/17. - */ -public class _504Test { - private static _504.Solution1 solution1; - private static String expected; - private static String actual; - private static int num; - - @BeforeClass - public static void setup() { - solution1 = new _504.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = ""; - actual = ""; - num = 0; - } - - @Test - public void test1() { - num = 100; - expected = "202"; - actual = solution1.convertToBase7(num); - assertEquals(expected, actual); - } - - @Test - public void test2() { - num = -7; - expected = "-10"; - actual = solution1.convertToBase7(num); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_505Test.java b/src/test/java/com/fishercoder/_505Test.java deleted file mode 100644 index 1531e15afa..0000000000 --- a/src/test/java/com/fishercoder/_505Test.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._505; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _505Test { - private static _505.Solution1 solution1; - private static int expected; - private static int actual; - private static int[][] maze; - private static int[] start; - private static int[] destination; - - @BeforeClass - public static void setup() { - solution1 = new _505.Solution1(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - maze = new int[][]{ - {0, 0, 0, 0, 0}, - {1, 1, 0, 0, 1}, - {0, 0, 0, 0, 0}, - {0, 1, 0, 0, 1}, - {0, 1, 0, 0, 0} - }; - start = new int[]{4, 3}; - destination = new int[]{0, 1}; - actual = solution1.shortestDistance(maze, start, destination); - expected = -1; - assertEquals(expected, actual); - - } - - @Test - public void test2() { - maze = new int[][]{ - {0, 0, 1, 0, 0}, - {0, 0, 0, 0, 0}, - {0, 0, 0, 1, 0}, - {1, 1, 0, 1, 1}, - {0, 0, 0, 0, 0} - }; - start = new int[]{0, 4}; - destination = new int[]{4, 4}; - actual = solution1.shortestDistance(maze, start, destination); - expected = 12; - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_506Test.java b/src/test/java/com/fishercoder/_506Test.java deleted file mode 100644 index 1ed3bdbe2d..0000000000 --- a/src/test/java/com/fishercoder/_506Test.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._506; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -/** - * Created by fishercoder on 1/15/17. - */ -public class _506Test { - private static _506.Solution1 solution1; - private static String[] expected; - private static String[] actual; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _506.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = new String[]{}; - actual = new String[]{}; - } - - @Test - public void test1() { - nums = new int[]{2, 4, 1}; - expected = new String[]{"Silver Medal", "Gold Medal", "Bronze Medal"}; - actual = solution1.findRelativeRanks(nums); - assertArrayEquals(expected, actual); - - } - - @Test - public void test2() { - nums = new int[]{5, 4, 3, 2, 1}; - expected = new String[]{"Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"}; - actual = solution1.findRelativeRanks(nums); - assertArrayEquals(expected, actual); - - } -} diff --git a/src/test/java/com/fishercoder/_507Test.java b/src/test/java/com/fishercoder/_507Test.java deleted file mode 100644 index e1906fdf49..0000000000 --- a/src/test/java/com/fishercoder/_507Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._507; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 1/25/17. - */ -public class _507Test { - private static _507.Solution1 solution1; - private static boolean expected; - private static boolean actual; - private static int num; - - @BeforeClass - public static void setup() { - solution1 = new _507.Solution1(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - expected = true; - num = 28; - actual = solution1.checkPerfectNumber(num); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_508Test.java b/src/test/java/com/fishercoder/_508Test.java deleted file mode 100644 index 332d7b642e..0000000000 --- a/src/test/java/com/fishercoder/_508Test.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._508; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertArrayEquals; - -public class _508Test { - private static _508.Solution1 solution1; - private static _508.Solution2 solution2; - private static int[] expected; - private static int[] actual; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _508.Solution1(); - solution2 = new _508.Solution2(); - } - - @Before - public void setupForEachTest() { - expected = new int[]{}; - actual = new int[]{}; - root = null; - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(5, 2, -3)); - expected = new int[]{2, -3, 4}; - /**Since order does NOT matter, so I'll sort them and then compare*/ - Arrays.sort(expected); - actual = solution1.findFrequentTreeSum(root); - Arrays.sort(actual); - assertArrayEquals(expected, actual); - - actual = solution2.findFrequentTreeSum(root); - Arrays.sort(actual); - assertArrayEquals(expected, actual); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(5, 2, -5)); - expected = new int[]{2}; - actual = solution1.findFrequentTreeSum(root); - assertArrayEquals(expected, actual); - - actual = solution2.findFrequentTreeSum(root); - assertArrayEquals(expected, actual); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, 5, 0, 2, 4, 6, null, null, null, 3)); - TreeUtils.printBinaryTree(root); - expected = new int[]{6}; - actual = solution1.findFrequentTreeSum(root); - assertArrayEquals(expected, actual); - - actual = solution2.findFrequentTreeSum(root); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_509Test.java b/src/test/java/com/fishercoder/_509Test.java deleted file mode 100644 index d7b2f61a46..0000000000 --- a/src/test/java/com/fishercoder/_509Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._509; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _509Test { - private static _509.Solution1 solution1; - private static _509.Solution2 solution2; - private static _509.Solution3 solution3; - - @BeforeClass - public static void setup() { - solution1 = new _509.Solution1(); - solution2 = new _509.Solution2(); - solution3 = new _509.Solution3(); - } - - @Test - public void test1() { - assertEquals(1, solution1.fib(2)); - assertEquals(1, solution2.fib(2)); - assertEquals(1, solution3.fib(2)); - } - - @Test - public void test2() { - assertEquals(2, solution1.fib(3)); - assertEquals(2, solution2.fib(3)); - assertEquals(2, solution3.fib(3)); - } - - @Test - public void test3() { - assertEquals(3, solution1.fib(4)); - assertEquals(3, solution2.fib(4)); - assertEquals(3, solution3.fib(4)); - } - - @Test - public void test4() { - assertEquals(0, solution1.fib(0)); - assertEquals(0, solution2.fib(0)); - assertEquals(0, solution3.fib(0)); - } -} diff --git a/src/test/java/com/fishercoder/_50Test.java b/src/test/java/com/fishercoder/_50Test.java deleted file mode 100644 index 93c2145a15..0000000000 --- a/src/test/java/com/fishercoder/_50Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._50; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _50Test { - private static _50.Solution1 solution1; - private static _50.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _50.Solution1(); - solution2 = new _50.Solution2(); - } - - @Test - public void test1() { - assertEquals(1024.00000, solution1.myPow(2.00000, 10), 0.00001); - assertEquals(1024.00000, solution2.myPow(2.00000, 10), 0.00001); - } -} diff --git a/src/test/java/com/fishercoder/_513Test.java b/src/test/java/com/fishercoder/_513Test.java deleted file mode 100644 index da7707ef1a..0000000000 --- a/src/test/java/com/fishercoder/_513Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._513; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 1/15/17. - */ -public class _513Test { - private static _513.Solution1 solution1; - private static int expected; - private static int actual; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _513.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = 0; - actual = 0; - root = new TreeNode(0); - } - - @Test - public void test1() { - TreeNode root = new TreeNode(2); - root.left = new TreeNode(1); - root.right = new TreeNode(3); - expected = 1; - actual = solution1.findBottomLeftValue(root); - assertEquals(expected, actual); - - } - - @Test - public void test2() { - TreeNode root = new TreeNode(1); - root.left = new TreeNode(2); - root.right = new TreeNode(3); - root.left.left = new TreeNode(4); - root.right.left = new TreeNode(5); - root.right.right = new TreeNode(6); - root.right.left.left = new TreeNode(7); - expected = 7; - actual = solution1.findBottomLeftValue(root); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_515Test.java b/src/test/java/com/fishercoder/_515Test.java deleted file mode 100644 index 6a558f353b..0000000000 --- a/src/test/java/com/fishercoder/_515Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._515; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _515Test { - private static _515.Solution1 solution1; - private static _515.Solution2 solution2; - private static List expected; - private static List actual; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _515.Solution1(); - solution2 = new _515.Solution2(); - } - - @Test - public void test1() { - TreeNode root = new TreeNode(1); - root.left = new TreeNode(3); - root.right = new TreeNode(2); - expected = Arrays.asList(1, 3); - actual = solution1.largestValues(root); - assertEquals(expected, actual); - - actual = solution2.largestValues(root); - assertEquals(expected, actual); - } - - @Test - public void test2() { - expected = new ArrayList<>(); - actual = solution1.largestValues(null); - assertEquals(expected, actual); - - actual = solution2.largestValues(null); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_516Test.java b/src/test/java/com/fishercoder/_516Test.java deleted file mode 100644 index e5affeee6a..0000000000 --- a/src/test/java/com/fishercoder/_516Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._516; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _516Test { - private static _516.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _516.Solution1(); - } - - @Test - public void test1() { - assertEquals(4, solution1.longestPalindromeSubseq("bbbab")); - } - -} diff --git a/src/test/java/com/fishercoder/_51Test.java b/src/test/java/com/fishercoder/_51Test.java deleted file mode 100644 index 4a5c69c82c..0000000000 --- a/src/test/java/com/fishercoder/_51Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._51; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _51Test { - private static _51.Solution1 solution1; - private static List> expected; - private static List> actual; - private static int n; - - @BeforeClass - public static void setup() { - solution1 = new _51.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = new ArrayList<>(); - actual = new ArrayList<>(); - } - - @Test - public void test1() { - n = 4; - expected = new ArrayList<>(); - expected.add(Arrays.asList("..Q.", "Q...", "...Q", ".Q..")); - expected.add(Arrays.asList(".Q..", "...Q", "Q...", "..Q.")); - actual = solution1.solveNQueens(n); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_522Test.java b/src/test/java/com/fishercoder/_522Test.java deleted file mode 100644 index 60da1435ec..0000000000 --- a/src/test/java/com/fishercoder/_522Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._522; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 12/31/16. - */ -public class _522Test { - - private static _522.Solution1 solution1; - private static int expected; - private static int actual; - private static String[] strs; - - @BeforeClass - public static void setup() { - solution1 = new _522.Solution1(); - } - - @Test - public void test1() { - strs = new String[]{"aaa", "aaa", "aa"}; - expected = -1; - actual = solution1.findLUSlength(strs); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_523Test.java b/src/test/java/com/fishercoder/_523Test.java deleted file mode 100644 index 6cbcfe783c..0000000000 --- a/src/test/java/com/fishercoder/_523Test.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._523; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _523Test { - private static _523.Solution1 solution1; - private static _523.Solution2 solution2; - private static boolean expected; - private static int[] nums; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _523.Solution1(); - solution2 = new _523.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{23, 2, 4, 6, 7}; - expected = true; - k = 6; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - - @Test - public void test2() { - nums = new int[]{23, 2, 6, 4, 7}; - expected = true; - k = 6; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - - @Test - public void test3() { - nums = new int[]{23, 2, 6, 4, 7}; - expected = false; - k = 0; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - - @Test - public void test4() { - nums = new int[]{0, 1, 0}; - expected = false; - k = 0; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - - @Test - public void test5() { - nums = new int[]{0, 0}; - expected = true; - k = 0; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - - @Test - public void test6() { - nums = new int[]{1, 1}; - expected = true; - k = 2; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - - @Test - public void test7() { - nums = new int[]{0}; - expected = false; - k = -1; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - - @Test - public void test8() { - nums = new int[]{23, 2, 4, 6, 7}; - expected = true; - k = -6; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - - @Test - public void test9() { - nums = new int[]{1, 2, 3}; - expected = false; - k = 4; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - - @Test - public void test10() { - nums = new int[]{5, 2, 4}; - expected = false; - k = 5; - assertEquals(expected, solution1.checkSubarraySum(nums, k)); - assertEquals(expected, solution2.checkSubarraySum(nums, k)); - } - -} diff --git a/src/test/java/com/fishercoder/_524Test.java b/src/test/java/com/fishercoder/_524Test.java deleted file mode 100644 index 64ef51f2d8..0000000000 --- a/src/test/java/com/fishercoder/_524Test.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._524; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 4/30/17. - */ -public class _524Test { - private static _524.Solution1 solution1; - private static String expected; - private static String actual; - private static String s; - private static ArrayList d; - - @BeforeClass - public static void setup() { - solution1 = new _524.Solution1(); - } - - @Test - public void test1() { - d = new ArrayList(Arrays.asList("ale", "apple", "monkey", "plea")); - s = "abpcplea"; - expected = "apple"; - actual = solution1.findLongestWord(expected, d); - assertEquals(expected, actual); - } - - @Test - public void test2() { - d = new ArrayList(Arrays.asList("a", "b", "c")); - s = "abpcplea"; - expected = "a"; - actual = solution1.findLongestWord(expected, d); - assertEquals(expected, actual); - } - - @Test - public void test3() { - d = new ArrayList(Arrays.asList("apple", "ewaf", "awefawfwaf", "awef", "awefe", "ewafeffewafewf")); - s = "aewfafwafjlwajflwajflwafj"; - expected = "ewaf"; - actual = solution1.findLongestWord(expected, d); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_525Test.java b/src/test/java/com/fishercoder/_525Test.java deleted file mode 100644 index d754304934..0000000000 --- a/src/test/java/com/fishercoder/_525Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._525; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _525Test { - private static _525.Solution1 solution1; - private static int expected; - private static int actual; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _525.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{0, 1}; - expected = 2; - actual = solution1.findMaxLength(nums); - assertEquals(expected, actual); - } - - @Test - public void test2() { - nums = new int[]{0, 1, 0}; - expected = 2; - actual = solution1.findMaxLength(nums); - assertEquals(expected, actual); - } - - @Test - public void test3() { - nums = new int[]{0, 0, 1, 0, 0, 0, 1, 1}; - expected = 6; - actual = solution1.findMaxLength(nums); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_526Test.java b/src/test/java/com/fishercoder/_526Test.java deleted file mode 100644 index 12dc269f6e..0000000000 --- a/src/test/java/com/fishercoder/_526Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._526; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _526Test { - private static _526.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _526.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.countArrangement(2)); - } - - @Test - public void test2() { - assertEquals(5, solution1.countArrangement(3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_527Test.java b/src/test/java/com/fishercoder/_527Test.java deleted file mode 100644 index 24b51e5f77..0000000000 --- a/src/test/java/com/fishercoder/_527Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._527; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _527Test { - - private static _527.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _527.Solution1(); - } - - @Test - public void test1() { - assertEquals(Arrays.asList("l2e", "god", "internal", "me", "i6t", "interval", "inte4n", "f2e", "intr4n"), solution1.wordsAbbreviation(Arrays.asList("like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"))); - } - -} diff --git a/src/test/java/com/fishercoder/_52Test.java b/src/test/java/com/fishercoder/_52Test.java deleted file mode 100644 index 417d2233eb..0000000000 --- a/src/test/java/com/fishercoder/_52Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._52; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _52Test { - private static _52.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _52.Solution1(); - } - - @Before - public void clear() { - /**Solution1 has an instance variable `count`, so I'll have to create a new one for each test*/ - solution1 = new _52.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.totalNQueens(1)); - } - - @Test - public void test2() { - assertEquals(92, solution1.totalNQueens(8)); - } - - @Test - public void test3() { - assertEquals(0, solution1.totalNQueens(2)); - } -} diff --git a/src/test/java/com/fishercoder/_532Test.java b/src/test/java/com/fishercoder/_532Test.java deleted file mode 100644 index 202b8aec8e..0000000000 --- a/src/test/java/com/fishercoder/_532Test.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._532; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; - -import static junit.framework.Assert.assertEquals; - -public class _532Test { - private static _532.Solution1 test; - private static int expected; - private static int actual; - private static int k; - private static int[] nums; - - @BeforeClass - public static void setup() throws IOException { - test = new _532.Solution1(); - Properties properties = new Properties(); - InputStream inputStream = _532.class.getClassLoader().getResourceAsStream("fishercoder.properties"); - properties.load(inputStream); - } - - @Before - public void setupForEachTest() { - expected = 0; - actual = 0; - k = 0; - nums = new int[10000]; - } - - @Test - public void test1() { - k = 2; - nums = new int[]{3, 1, 4, 1, 5}; - expected = 2; - actual = test.findPairs(nums, k); - assertEquals(expected, actual); - } - - @Test - public void test2() { - k = 1; - nums = new int[]{1, 2, 3, 4, 5}; - expected = 4; - actual = test.findPairs(nums, k); - assertEquals(expected, actual); - } - - @Test - public void test3() { - k = 0; - nums = new int[]{1, 3, 1, 5, 4}; - expected = 1; - actual = test.findPairs(nums, k); - assertEquals(expected, actual); - } - -// This test case will throw TLE error if your algorithm is O(n^2) -// And it doesn't compile in IntelliJ, it throws "java: code too large" error -// so I'll comment it out for build -// @Test -// public void test4() { -// k = -139; -// nums = new int[]{32196,25662,20113,8991,14524,22650,29922,23567,24083,35838,49415,21584,31906,7236,672,28196,40565,17915,31989,43287,2911,44179,35938,7755,31891,34685,54614,46501,32365,1269,49348,23164,22259,34750,29889,24471,52189,54257,20514,27263,46438,54041,42826,211,15954,29874,31160,3511,1091,39059,51850,18957,23086,112,38817,32389,17860,8,29479,774,33497,55493,25741,15363,25706,7951,31961,23162,32613,34616,14693,48726,17341,53668,458,36597,28752,32215,14322,39975,31848,24846,42980,30957,35787,16670,3952,2886,38448,47644,55049,26426,33899,38998,36414,2388,5121,47599,8774,9148,15117,21696,17737,51500,11336,20896,4766,53999,27252,22109,24015,54663,28571,43978,1740,14785,10141,20019,13503,6515,27590,947,50841,5779,24068,11730,40286,3234,18279,2765,6052,24902,27020,20278,36152,36247,19831,20235,8798,13809,13906,37247,37702,35412,36194,32718,35458,47620,14501,25623,41152,42175,48674,15585,42236,21872,39004,28638,54912,36515,24355,53765,32928,36332,43966,33281,53220,17,38581,25567,38285,31125,55445,16154,33575,475,48688,17202,41092,38465,54816,2020,9194,862,31441,39638,13576,16073,9480,21395,19668,31980,50527,50619,17278,16688,26088,4953,51389,55146,19908,55071,30693,34312,6419,39717,30346,43734,43827,13584,41010,935,38147,26518,46409,52594,23276,13459,29334,12386,151,55154,53765,30122,53357,7159,25227,50383,37460,1116,2529,37133,51391,12681,22955,41006,50924,23239,32863,11161,31422,7450,52849,49240,38933,10038,6355,21937,18966,54561,51486,53702,51151,36965,10531,14662,13755,19194,14584,8562,32798,28241,23009,12170,41928,25733,42394,35933,38724,32579,13536,31032,21204,27260,38737,9947,31527,38198,48146,32034,26733,9844,19620,7068,32453,50978,5571,9372,46512,45623,24719,32443,25175,31428,14185,46196,50140,2890,15140,1558,32166,26632,48420,25872,399,25075,53136,5001,12307,6619,45377,48536,39123,2576,6394,45275,15308,44765,21206,49367,31185,19190,45590,9909,13168,39395,19533,36877,41393,6962,34706,10107,7467,38734,31008,14216,49241,3671,26884,30386,43388,21102,14572,21356,22450,43365,26645,50549,15276,6863,53622,4344,49819,46650,47388,45294,35030,1447,36017,46167,17212,2623,32634,49557,53708,14446,38588,5649,44805,42448,31599,55183,997,8311,34660,52148,14272,26911,26754,26723,44568,14490,22241,39669,12113,14450,16460,12823,49804,33914,24661,32178,50501,12689,12085,22257,54841,42364,6447,5470,36103,46496,6750,26462,3802,25507,31784,33498,3357,42658,44574,22348,38148,1624,46864,38928,18494,15408,24885,23971,40930,22850,35088,37741,45600,41620,52377,49812,9234,20215,4519,30221,55387,26976,40308,10630,44462,52025,50184,16952,13074,19695,8052,8040,19847,52618,43223,6115,23590,24160,51992,35131,2218,2149,19684,32472,52398,49558,27301,25550,48465,38878,46962,30043,18604,54909,9522,13893,44743,40643,3835,50385,43864,19480,19828,38178,44482,24616,25354,1837,16217,7419,28653,45204,55042,41556,38993,13251,30387,14423,17588,45230,41108,28967,43136,45918,7516,16669,23874,19471,29914,51379,35706,50297,40996,38997,21957,258,12159,42203,10545,40130,53143,54369,49918,12139,23336,24607,12538,51171,12021,18593,44836,26918,43108,4818,449,18814,52186,17390,11595,19300,49077,52096,52304,29333,18904,14791,10793,24509,55123,28770,20881,22917,25437,20336,7607,17515,43755,4211,25601,55344,42014,36246,4235,31909,46790,6194,47102,22036,13535,14177,43361,42787,49938,40304,39519,23669,29420,2927,48980,22450,19423,11234,22472,9042,12787,28890,43057,51659,20169,33790,41879,6491,54775,36100,44315,13387,8536,51296,49923,23414,18541,45084,23044,54602,17406,16380,10982,52754,44488,19769,39854,42002,35875,23765,7081,27460,8,54137,23841,41029,28713,53870,38165,11791,51826,36146,38678,29076,54120,7665,46165,34117,34153,32284,249,5470,51121,21421,27289,40198,34143,20440,12113,39959,27250,11346,10878,18450,20606,22592,53135,38699,353,29930,39360,15987,47312,28183,13731,53310,26046,45571,55214,20880,29609,5971,49014,2836,35381,17807,31381,20893,1013,31606,13487,40770,52515,51951,36231,16391,20292,1178,2167,48222,47029,40381,37849,51485,8234,22503,28250,41234,10234,46254,6813,38584,24494,20030,30884,35316,23630,17172,4926,39660,46702,47665,9422,7233,29190,55191,47319,30645,25270,36605,47729,13814,43758,20184,2984,5842,45826,44739,49595,22164,12326,36743,38698,51726,13294,38747,16481,54631,9316,10524,28614,45760,13099,9964,47536,27808,30583,6952,49509,20065,53331,48832,8978,52086,29308,51588,2658,30219,18044,14131,20391,42231,29272,2818,28854,31337,52687,46583,40418,7051,6891,37865,4319,45281,26248,2191,19302,17819,50807,21547,17922,15313,45828,53680,55231,38811,11457,20106,11146,6884,27947,55151,20694,3460,25503,20389,1384,28539,48509,19480,40175,50893,33217,18760,24051,29980,674,9345,43571,30850,38533,44570,37513,30613,45909,31018,52880,27871,31604,6064,52668,8299,41884,17599,12466,14761,32862,28615,18111,1875,5040,48669,11338,54528,55347,54142,32462,2716,28033,2200,2027,32579,49920,5073,31526,45654,42888,43834,49605,10786,55065,24606,1285,30528,31279,13858,1049,9981,30474,4928,19415,22397,30149,22395,2289,26727,25314,36378,18493,10727,22257,39483,16460,45801,8379,8406,19114,54240,5783,9941,2968,10543,18267,33981,52572,33026,3178,45500,11869,28000,13452,3017,33341,53949,51582,55454,32612,12395,32410,37072,45792,50050,4022,4253,25851,27246,31334,23721,19262,37319,5102,18615,29534,14606,5302,44639,12031,6394,2738,51552,41989,1450,13680,21446,48938,45576,10216,48924,34699,33377,44846,55430,54623,12811,7956,13237,15237,39565,34224,27286,8081,1277,42916,33101,11791,42238,41645,51653,28609,46333,5569,37704,13907,40143,50603,19628,39940,1944,44681,49874,27177,3063,1005,14636,28522,34561,51396,5606,32192,36364,52769,18829,18198,19570,26751,40292,48546,35981,7579,26662,47893,14688,22893,18127,16212,22399,17975,40739,35181,50185,17120,27963,46959,31658,44160,30914,43402,25734,12087,6490,9480,17650,43168,12475,22584,32015,16918,14704,49062,8401,25506,16029,45088,34167,14151,18610,14563,38544,30908,34608,18774,17411,24695,50245,26753,11702,32096,16257,52755,6947,41806,44487,17147,50839,51290,44840,32043,15995,34959,1761,39296,37647,24503,21421,17005,48697,8170,5289,16261,25246,31541,50997,18289,41941,10305,46091,12892,37212,18033,14443,45177,39876,24746,22870,15886,10187,17429,14175,10991,27391,48344,42205,6789,22755,40200,27148,46178,18978,26480,14615,53478,8087,41812,33170,40743,7223,31647,33231,28002,45961,12174,30725,4089,53330,19748,11002,22466,14489,14115,13617,22197,9467,12319,40261,27741,11613,15980,52958,22676,42599,4652,49637,13732,168,47785,38306,45279,10463,14826,51934,35341,17595,851,4718,34197,45800,30513,20348,28176,13657,45396,49324,6568,3487,1267,8702,20856,26089,48082,52056,3327,20711,32152,33478,45852,55288,8567,4606,40072,12947,52918,35383,23123,39247,27737,43637,4836,38253,19565,49170,12384,51602,4003,49764,9047,26861,41856,33948,29924,16845,24757,22712,39592,6378,15899,17809,47405,42282,7497,15628,38824,46631,6400,19773,32296,50984,24527,24457,49616,32627,1274,9661,51794,47284,28768,44799,29413,52873,31643,52741,29458,37651,40158,2604,18822,39274,47672,53732,25201,12811,8332,31974,42538,483,5484,18764,38612,49046,15774,55273,2333,35698,3858,4676,709,21693,49251,19993,28425,46000,44089,18206,28488,4097,21872,20737,34751,51871,27190,50596,7173,11312,4116,48012,2468,20786,50589,16340,40740,39301,29346,41769,10207,39331,26160,1144,29630,15747,29755,12341,9737,19677,44914,7537,15208,13877,44013,35969,31013,53624,21491,19378,28011,38070,19472,54758,2091,46407,43703,8908,16819,43598,23455,39358,44744,51218,48019,21835,6290,50576,46260,36090,49548,29386,1814,43402,39919,37559,13956,26318,6031,13660,45724,40085,428,43462,986,2524,1369,40818,32556,31015,8227,41226,44790,3745,15149,18170,1751,24759,23632,12711,34328,2216,53792,30424,35949,53880,27005,23411,31599,19064,5698,33822,39662,30083,16755,11654,48410,8768,19635,27877,52417,40712,25938,5458,451,30565,4433,44588,28065,45890,19354,53235,33098,27247,7528,15810,54205,22279,11903,8371,48003,39188,23060,5446,49340,4562,10447,50314,32737,44520,26324,45624,23514,25236,13906,28444,43760,21652,46075,17012,47719,9937,24384,46069,47486,13691,41515,53304,32847,49562,37098,45053,44787,15028,27016,1087,899,54161,42714,25632,18225,16279,34559,25857,55106,18522,10686,39346,48359,12594,6485,18908,2177,29110,54631,9432,10021,24643,52385,36706,3038,49044,43733,46332,30581,6931,29670,36974,27313,8636,3675,18087,55039,47312,32714,4213,28143,32098,31821,47702,49800,50768,54674,50632,8045,10642,14031,44927,1265,49650,45958,8320,47969,47867,51743,39380,50950,33189,27158,49779,5418,55012,20261,22716,21597,4387,6734,17096,30370,42587,19945,29825,44219,5963,45751,13092,41338,20592,43232,3706,48221,28162,16656,20353,9603,52818,30409,42646,38058,42889,37940,40923,15162,8495,8668,24976,38335,17678,409,20833,10979,22271,47517,12480,25210,48375,5424,6322,10633,35421,52720,43277,37436,47471,17407,24607,15195,52700,52503,2760,39354,27562,17245,25880,17840,1994,21409,30887,13320,42246,52701,23200,13907,40518,18189,36984,40213,38744,41266,46333,4705,19895,32475,49844,13814,54538,10218,40091,39242,42104,42571,40564,17149,29184,24843,12976,38797,19921,39739,1031,39387,37882,44180,52184,35437,28526,47250,50628,22837,24570,48803,22840,19601,6962,25033,33341,32463,39275,54194,43365,55034,27672,28105,13615,5067,49227,50669,45712,7757,19104,9316,28694,4709,50669,4472,30254,38375,22197,26531,10700,8609,41273,42354,30257,44728,14353,55417,38651,34696,11324,7754,52388,23857,8080,24231,11953,52748,54000,33518,28327,50309,54885,50724,41379,33851,42556,38424,29811,5470,31417,47493,12301,195,23702,27227,34820,9991,7582,26169,30408,43544,35028,5070,12316,29310,24586,7867,2278,765,49407,18339,10444,29027,37,14833,22857,23175,25356,47901,42145,28963,43935,810,2371,15898,26578,7633,12997,10028,23625,33906,6672,43014,3,33869,36242,48861,39365,31457,21112,1842,50722,41871,14195,48844,3948,20456,44923,5624,50702,42835,18318,46537,28553,27307,30953,3568,11605,1293,2106,33941,25948,1466,28953,29858,50965,22021,52275,32625,26558,34176,9904,22046,4830,14412,53828,17913,33081,21661,22044,1385,30755,19311,28265,16188,52376,29256,1286,16503,37657,40964,16723,28409,32633,14221,53708,43698,19031,43716,17077,10090,29383,34963,6753,7769,51810,37289,28398,25914,24636,7733,53100,42048,16072,26812,19240,38252,53960,30907,43122,6809,10129,13161,50608,16891,19871,22523,6734,27874,10290,2503,27992,15525,19372,23120,15336,52048,3183,50188,46515,12235,47343,25239,8015,20195,55084,49946,4317,19145,9067,21108,19261,10111,21790,537,46356,34969,30784,9686,13354,53725,51704,17730,25408,7209,14657,48129,23060,29592,46784,34435,32501,52597,22972,2068,47880,36875,15393,33329,17803,47232,8142,42245,46810,21230,3194,16364,17827,43204,3438,48057,19491,23160,29529,52762,21240,54967,42228,26817,4433,3602,50396,3556,13040,48025,14722,19405,47962,6333,34006,24875,26189,6011,33765,24655,1914,6065,40579,13762,19441,30495,51928,33070,33007,32296,45893,41799,28369,31741,31579,8351,2231,24965,593,9244,1426,16053,2838,53091,37610,8306,40717,15473,13989,39331,21117,2837,33659,10119,43167,1358,45489,15453,49349,34826,9127,1385,4196,9221,36479,7138,18325,9966,8698,22076,2732,23148,38452,46713,51439,41135,46718,12775,35399,46535,10995,10286,42034,33558,45957,22727,47474,15827,8912,33269,13025,14669,41006,35770,41428,46369,7177,2021,42551,34585,12703,30490,39885,42223,3688,48869,24682,21132,49541,14243,52081,40283,42969,46421,36603,50505,19351,37536,26276,26774,7946,50258,41054,48266,44524,46422,11487,8457,29759,4630,20312,50307,35147,11274,37262,20911,38186,23629,33055,29333,8262,41757,16832,22307,52886,23583,41659,27882,26557,44359,46962,20782,47197,14000,52607,12849,1766,16653,34003,22081,46840,52372,38722,54286,3637,21160,4788,38255,11240,6849,52670,29518,8836,8060,27598,35569,53523,33557,47845,37843,25855,16945,3170,1849,13508,20957,7573,3840,9764,53827,5567,43745,52100,10592,7914,46499,30574,7571,22565,4220,9674,50919,23540,9360,55015,18916,42948,7813,3889,16678,52200,53283,48560,14495,4214,9838,42089,46405,12202,38438,49694,5855,27284,25138,39991,14737,7910,32187,15419,44407,27314,27044,14381,54015,34096,27404,5871,9367,36889,50193,46075,24179,51178,47394,24782,32253,33653,14252,43455,1628,44396,16149,31443,9196,7947,49801,54360,50044,32330,27967,28557,54692,13466,37445,25447,32875,1451,12267,51907,48069,31592,26025,29116,15626,28829,5870,8686,1818,49385,1098,8519,7140,44161,1882,41502,28721,29427,3169,15643,24826,41157,10544,8630,38983,43273,13695,23368,48531,51273,54561,26777,41541,25952,27063,3670,2434,26764,20296,25123,48232,18894,20586,9222,44872,3182,8438,55090,30423,45126,40234,44791,19272,30502,17800,7298,22448,49938,6797,8274,8328,23661,26333,8055,24767,3823,9797,32661,42466,8973,30323,15172,45046,983,53335,25407,9900,50873,29431,6901,46831,36236,33748,29794,409,9454,2943,53387,7412,41945,50168,13785,6931,33788,8674,14865,4383,14258,49404,17655,48264,33318,53890,19307,30046,32961,16938,19441,50044,18401,42502,54287,14056,27803,30085,12705,48589,49718,10783,33793,44436,37455,46251,13658,21765,6199,30815,37711,37122,9286,48039,43361,2805,39535,40663,47242,54181,5530,32452,4819,9241,26793,55301,22882,24751,40839,54612,984,9661,48455,33471,46716,47475,1048,47560,39634,25984,24473,13198,50118,10309,48597,52109,42050,46702,55495,28998,41876,12905,49826,40559,46723,9137,15274,2365,11601,12527,33313,5186,26926,9008,42277,39301,29208,24769,38184,8133,32119,8499,17883,15797,11777,40147,11701,13742,50442,52282,19249,16090,169,21906,33201,2652,6014,46966,33299,50720,35383,5924,27404,51403,17718,10636,34831,1846,34072,31619,25419,28881,4291,25467,27622,43342,13237,184,17051,31653,42058,37976,14826,26955,15189,41984,18844,45734,46140,923,35442,29587,54013,34827,5110,21856,16237,34668,12249,33209,49993,32547,10742,35386,13675,47169,27183,48652,28402,28729,46476,39845,31794,14504,36167,13806,8795,29691,11332,6744,23048,40389,30754,28642,42046,13988,50789,26925,32029,28564,1674,2579,40896,42632,20009,13343,26538,2860,44768,22690,12377,27862,19080,28702,509,28891,50874,39562,38337,48819,19161,53831,20721,17076,32987,4382,1605,48765,24875,6712,52711,20096,31243,44771,47928,51452,17345,29021,7506,28663,11723,9203,38844,16037,55043,18455,42032,44813,43678,4707,26060,2353,26014,3342,44720,16642,33806,40379,30863,4439,23581,12251,50800,51164,9477,5130,51183,46275,45778,15076,44246,22068,21625,10138,3193,41550,45136,50373,549,2623,54103,54746,47304,54605,51666,52766,2020,1104,36436,22827,54712,46368,1796,21028,1654,38688,52529,53634,25518,36888,22218,49350,34848,47243,34277,28350,16891,37593,3370,42330,21853,4097,40653,47094,50938,44704,31099,17224,14418,55348,51705,28340,5063,42086,2609,38223,6592,12976,51059,25270,15834,52921,51412,52244,5161,8459,26407,48679,11110,21236,32351,31935,38886,7586,42316,29825,28762,48674,39439,29714,30360,20319,28791,29056,44101,11014,14900,10076,26505,49872,29438,33830,55100,29530,6668,12209,14041,47184,28253,26034,47223,46209,27508,2017,36128,4295,21931,21664,47194,17995,14471,20057,38066,40719,24858,51237,11618,23954,12222,18979,43100,35323,223,51672,54034,33831,49738,6003,40024,25585,3039,45916,50864,5467,46925,35230,41960,32887,47302,18570,50514,21897,19145,18707,50386,20076,16513,28901,35442,50989,20887,16349,27260,40693,54149,35312,11927,19125,42198,31926,9655,15550,38928,16246,53428,13537,895,37215,3631,23284,38257,24827,5704,32247,50725,22854,7120,31518,45579,40936,52007,31795,17096,25706,9963,1252,42363,54375,35398,47672,20924,37690,42159,4747,33510,19429,8163,53503,5138,37296,3156,43892,22033,37543,18177,54915,23562,11036,25466,19107,8396,45508,16241,16794,35737,4000,35935,49673,46071,47758,9827,30396,11487,35189,30048,35416,39041,27486,30039,50118,47120,9404,38051,11126,31451,7724,39707,53155,28211,38638,41511,16470,46327,38272,6151,55148,13762,40620,34766,16147,825,33838,35986,32324,34242,36740,42934,52260,11935,46291,18330,46294,9614,23307,39985,30669,21295,28908,9703,41414,25087,44552,47540,5950,10221,2054,27312,40598,18702,49413,16914,51903,31618,47676,23091,42618,19605,50762,12254,12659,39737,21857,23425,40689,17127,41590,22667,603,28989,46616,17860,23486,17084,23665,22573,43869,16968,9138,495,22149,16191,1281,12511,24343,24554,37445,34181,54979,35223,45871,47785,26557,26717,35794,17120,9538,24281,7048,30114,43507,54655,7227,45105,2665,10269,51636,47388,43864,36694,48605,16346,19833,3822,6697,53324,928,5004,52890,40446,30934,20437,73,31747,48358,40141,9462,17679,28759,6209,15891,2274,11302,12533,35908,25993,22814,9663,47128,38034,21287,4174,28988,16513,33483,48344,31680,26676,7124,38081,28593,54815,53250,54025,32490,39288,28226,28950,16303,43288,45007,38202,20839,50678,43937,6926,47729,37532,43214,48895,2659,37541,42968,31110,2148,49002,1666,47505,45435,47221,22793,31009,19888,28978,40411,7420,18301,1513,6885,24492,5793,45998,9065,21551,3962,46266,30019,42262,29118,42974,36602,33068,9559,28408,4065,2237,44839,20651,17188,2901,29388,26491,41733,38296,42765,41097,38780,51180,50363,26030,25562,4272,30504,37707,19986,8413,46056,50465,32997,49313,328,2050,3778,23131,13884,15382,8937,41948,1592,34894,15277,43218,38957,2009,22872,18527,11580,25258,4802,45523,5069,24950,16957,38568,40146,18116,3445,16090,34443,29902,5973,37776,31097,47243,52443,4147,49831,3957,38208,46387,24446,37315,3244,30771,25611,53873,44094,40718,37697,35406,39116,25709,1333,30545,16907,14385,30659,42452,30273,32267,44259,47334,31888,33915,21290,24986,53839,16712,42700,35218,51397,24847,48943,3970,30216,45979,34617,6367,52296,2593,52777,7802,40673,37970,26005,5223,1837,39037,11492,50094,21147,19728,54350,10802,3185,16696,32366,27817,151,47784,39566,16411,13381,18351,37256,16510,42436,4119,3401,36799,18733,45307,18408,36159,10577,17690,4185,23345,18213,16558,40120,47703,32245,51457,32847,46872,14757,18335,4921,967,36425,15290,6028,13010,7793,26735,23707,16948,25908,19236,17374,42397,41029,8281,18961,5925,34252,28576,54845,12949,31432,18836,42455,44903,24758,9855,31615,2975,33161,13983,51957,39300,47221,23676,43919,27099,834,41972,6811,218,30418,25321,42686,4623,23795,29489,40884,19755,6343,5919,8365,39383,35850,33850,41020,33130,38161,25869,28283,42005,35029,23937,36041,46806,10942,18809,34301,41895,52583,1281,18758,50443,23459,15235,18622,29083,22337,13924,3382,13056,17090,25241,2519,12840,8601,43974,7238,35428,3708,39352,46083,47466,31907,30156,39019,27238,30012,24583,13348,48677,33532,11038,33693,21733,53431,31756,6735,46744,36487,49834,31667,36354,4880,55414,22728,14120,39129,8729,23318,37117,1379,50499,5381,44518,33711,52317,38119,50718,17535,25713,36990,1873,29912,14500,31803,49388,10024,14498,28888,40772,53945,43846,21950,23849,46623,26978,27350,40703,32639,11675,44993,23491,28345,53087,26829,49991,25379,15898,38515,33397,21162,50835,33799,14848,50203,13565,13715,21515,45418,37138,7738,53529,6730,9360,33528,47646,45565,49313,25936,39680,47967,26018,52824,39744,5756,54079,53458,2174,3465,22783,26302,15805,19411,1398,8704,12632,20722,44647,41612,2019,54435,15323,5960,45680,20235,47140,20035,6199,6313,33321,29494,55363,38577,26755,24202,31675,10921,5063,31019,32993,18946,37767,44944,51704,31472,14893,29051,28363,474,15502,3493,6042,31939,32180,55442,31904,3348,17690,6280,23580,45432,20567,29957,35319,33097,53691,18600,33724,47650,30215,51052,35053,52584,15500,21352,15771,26393,46156,18476,49885,21028,21812,29712,11645,19827,11513,27434,49548,40777,6583,20426,33485,52875,124,34386,40892,24328,33491,41869,32054,39709,12224,50156,32877,30641,36065,50987,47023,5348,40057,19607,17121,35523,18094,48389,51092,30501,30226,9347,36558,17183,29564,50108,50092,17740,13943,17640,8473,26300,53451,31811,6811,22217,45109,28007,30591,2736,31835,44416,3073,38971,41678,11439,29230,35106,21941,20791,2729,27469,5654,33333,42378,51940,31468,54345,19568,22746,30994,20656,51726,21503,2941,54509,40788,107,37146,43178,22056,49535,43658,31336,21922,41714,38106,7710,31366,50165,1489,49765,53132,47035,46435,12336,53187,8018,29359,23937,29980,3263,53525,44662,27755,33815,48152,4287,26486,38100,34400,49542,11007,6092,37785,28058,39907,47270,49801,5628,2777,46214,44964,38359,9902,53395,15459,40842,20630,31588,39010,35814,3009,1374,45949,28799,14696,21045,28949,20147,28216,25398,44370,10996,20861,32893,17055,15499,48137,32452,20228,37635,32988,26432,3803,43084,27695,6657,25101,51119,28516,2951,19596,43091,52545,26184,28824,8473,30917,7292,34225,13921,12507,20066,256,34496,27609,51552,38052,53046,28401,50677,41400,21241,43221,5687,45439,33600,34710,54639,38169,35155,9308,9480,34796,3544,6778,10673,51914,16796,18899,8584,36312,54348,23113,6216,1408,55129,40066,21915,52519,28202,934,43371,34111,55090,9512,5406,16571,29539,5316,37775,30720,1073,53516,1780,15645,4258,31784,31166,51470,11572,54401,24583,52103,21426,54810,14614,19988,25476,44572,53792,40858,4521,46194,51020,17568,27450,16718,53861,6127,43356,53662,21055,40503,26229,21651,1070,17278,32470,33019,20661,28761,7351,50377,7925,34351,31509,23838,20764,18281,27184,14121,55376,32036,29898,34659,27308,46060,31658,51241,18755,39456,33682,49169,18749,27906,17217,10049,50776,34758,5820,14761,20211,35437,22499,17943,21819,2392,17642,11491,42055,43000,39456,2589,26195,46389,9717,46780,17404,26750,6744,4083,1660,10341,1993,39626,32892,34673,37230,54543,44809,53499,52365,6873,11114,41862,21934,19900,5731,43890,43551,24817,4793,50350,21267,49958,26435,10124,46715,22989,10804,6622,1958,39537,45686,47316,2051,55251,3655,35723,21845,45634,34643,35802,51611,14653,445,27850,7097,51045,34213,42479,6347,17692,47199,19688,23386,35096,35825,46814,7939,39611,17482,24364,20984,1502,31118,38409,41372,9475,54117,493,34812,48070,43517,34605,32526,1474,26645,53101,21683,35689,23853,45059,42254,15334,54551,52648,50135,33227,13676,5095,48624,16858,27668,46865,3308,19346,29747,19483,4713,1906,48013,44303,53269,6558,41754,9781,19770,32735,2513,1920,5239,27851,45122,41099,7912,14481,22961,49679,48002,37959,12406,47287,42821,38992,51273,22209,48747,32894,10855,13270,42789,4766,52755,32538,53454,54905,31144,43767,14015,53045,45806,17391,41360,540,38204,1259,12675,45272,4237,46217,49224,2666,5352,54280,32269,44431,21005,20684,20834,34768,53285,51609,42014,43517,9392,27598,12140,6035,20728,16064,53913,51390,44898,10175,10400,51196,35464,21114,6496,17031,23320,11561,18618,9498,3332,14810,53043,47487,28524,9057,45685,21874,28962,11666,19715,28014,46109,30041,38250,24550,25913,18420,50510,3334,6811,38191,39541,9489,6315,39330,30753,2210,33013,13279,32991,19031,27539,14106,24472,5600,45585,35700,38749,12514,49186,30927,27599,21916,27216,44634,13660,21972,45257,13690,18835,40204,43175,48001,8316,15608,9907,49566,22322,42618,32066,25558,34100,22100,39504,16823,31288,30673,238,41663,13943,9400,48112,49570,19859,51289,2835,15100,12707,41390,34630,14077,51412,48514,10068,30479,33251,19306,16815,1737,16726,42101,45022,34708,25790,38844,13771,32593,32977,6652,41392,17944,20377,25735,30388,15227,13862,5955,33959,3234,9667,21764,1872,8356,35182,13131,23740,46130,11109,36033,17511,1475,6910,12497,14077,4723,1166,7962,14229,48575,33339,22618,12247,36643,41263,39822,21486,37251,48213,40127,32010,14635,14067,2616,24573,9966,28944,18902,9081,21376,28281,46393,38959,12194,23222,3829,35347,32140,1983,30644,23943,10333,13920,21582,13770,51818,2546,42667,19896,25363,47586,38964,31422,41336,46510,24487,54250,8296,39208,24595,50652,13259,6321,9386,3160,35248,7365,11536,47364,53336,27534,52599,30124,19880,48807,52915,46586,39913,16099,53476,8327,36838,35770,9521,121,19641,46730,4837,10346,31475,48699,46140,7700,15518,21985,35075,4303,51189,51085,27752,20241,42107,15316,6651,46533,46852,34862,16130,8454,30809,24219,36627,39665,22509,13630,977,19784,39663,17552,54045,43189,20311,7278,18082,37309,40409,53389,22006,47200,36237,26546,51549,12187,40002,19649,27993,3129,33462,48819,8232,13429,54862,6834,38301,3183,9323,19935,19205,3479,23584,31699,51083,1645,7977,35362,45965,48631,20681,45706,18849,2792,29799,9883,23438,25191,53361,28616,8540,31769,41349,39657,25872,17870,35093,1309,42610,28959,21096,27914,52663,4585,31689,8366,23990,54024,53091,12360,38929,20042,15234,52557,21040,7811,9795,3681,26710,5323,26428,14459,5978,43484,23437,2542,17564,31397,30209,2041,36223,20539,25091,8559,49566,3321,17267,33736,12738,42767,49082,43773,13204,659,49343,42515,38614,33245,27300,30828,45423,43085,35168,6478,46010,9501,33489,20974,26931,34877,30314,11195,40483,2286,16594,46797,49434,39517,764,6327,14381,6527,2608,48097,7702,12082,39465,47839,32340,13826,2429,22176,25913,53020,48247,50973,12171,50013,33713,1118,3465,42810,19759,34359,53752,49315,36816,45738,6984,34066,22366,42102,10933,30921,4776,9227,14706,42458,34283,30074,38184,8541,2719,44,11213,6703,55360,41777,34836,43788,25583,3017,47115,16362,35893,21841,45120,8452,619,31252,50231,44567,28501,9016,53881,41957,27383,51619,714,49198,27771,23716,29421,53039,24071,54063,33980,10819,2467,17638,50169,31986,48729,22488,10118,16792,3026,793,14941,20727,1079,28063,15477,6057,37489,32015,5322,50075,16818,5557,21908,28011,28773,40378,37707,43281,336,17365,22023,29033,12404,2584,25593,33781,20908,23269,51556,45360,14172,9895,40988,5713,44906,4887,13651,23120,16593,36919,17805,21743,39364,44512,7244,41289,31731,8034,38333,4867,46956,3099,10678,35522,18485,29055,5481,1055,13820,9333,11175,13385,10250,6650,5631,21814,36763,15769,50415,54350,4144,41741,24293,21214,17075,14907,45790,20505,41092,11494,1639,32660,20242,40881,2894,43393,36639,5622,8864,30532,41176,16963,11516,30010,4490,18103,21224,18174,45001,11752,53591,34582,37188,52453,47066,2672,10939,19554,37104,1130,37194,52014,34184,4240,43222,3204,10418,27838,4851,46068,9563,16287,34040,37990,15675,32370,50584,36465,55480,54332,11603,51437,7076,9371,37012,23981,14207,33109,54521,14290,52167,1537,6039,21500,35784,3248,689,1921,12901,9434,195,27432,41184,14575,27352,51964,29025,51030,39542,35186,43785,19208,28885,42473,5132,19550,39672,25309,10508,13766,54995,34613,31463,51718,45311,52177,31686,3662,31338,49183,17179,134,55361,38422,44605,43070,49370,52778,29840,45072,10089,42678,10815,39735,6493,15681,25888,14700,54746,30465,31142,23281,30643,49703,17871,14848,16319,7887,1057,54264,52052,25439,978,30971,20883,22272,25621,2275,15900,3081,54082,36895,25817,34770,48932,23079,21729,41933,11367,222,39807,37562,33641,11648,43549,21037,38269,31771,4216,31842,9600,10131,37682,11398,13589,37101,15988,20036,48821,46202,13639,26475,51983,47521,53469,20137,5422,41703,51734,49704,31427,22390,53809,27631,48447,38484,25020,6304,39320,29434,9784,12010,47053,21471,49509,42642,44147,48678,45215,39783,21665,18674,22854,8939,35481,45582,15362,3922,35646,27894,4383,4740,22366,20563,4886,19748,40341,31950,20496,42053,41310,27186,27540,18891,27663,21456,5051,37190,33334,47793,31572,973,3944,34886,25896,13481,30787,33630,10921,54546,8017,49706,1577,15330,43379,19799,50916,13742,47105,8654,3336,4190,37908,23675,15004,1295,34457,33171,27870,42213,32062,44961,49184,22173,36807,54927,39291,54072,24762,11323,51602,32962,5752,29606,4738,42302,27784,8950,32545,43346,5175,33062,102,23129,24250,28644,15941,36753,32220,24323,43191,45636,43586,13824,6515,6324,4297,5855,13679,19300,7871,54368,53934,11640,1350,54112,31185,45804,1807,36823,31240,22906,36902,12558,13956,7077,26556,5960,29421,55269,19970,21695,43260,49659,36279,22353,36570,46263,6415,2420,35977,37203,51822,7965,51231,53158,37241,25250,7669,17835,16004,21039,9567,12855,29780,15671,982,4788,41049,51942,21456,19051,38002,21304,37484,41955,54335,55142,8648,53082,53589,14113,5408,41178,8148,55464,54571,38724,17952,38755,8196,3173,28533,25750,2705,49833,49407,22597,42653,37911,19522,8752,12447,48962,35386,39334,16166,5869,9982,36163,28445,6518,38868,42793,16300,23104,3146,55170,33420,47820,4595,16714,4900,49851,51910,31622,12602,41312,54774,41696,53684,7814,50852,29733,47645,27791,13145,54560,13123,41647,49733,4517,53342,45727,31303,8075,28225,53555,45016,39297,17761,29001,9647,8049,47630,9114,34456,26156,47644,44903,31995,19640,29348,54425,48393,25402,23462,39658,38798,48971,23479,46517,5049,4614,6927,36560,39218,45501,21884,43276,5351,17754,9122,17384,16581,35223,18524,15800,14299,21321,49027,52621,38288,39504,23739,22923,6576,27177,29997,53071,9956,22121,9369,20306,26970,24449,27322,20257,10796,8164,47647,12056,27432,47841,18122,53289,55426,39870,42906,40904,278,52532,17778,47239,20596,1924,46978,50138,50703,29387,25195,29426,10762,49558,10751,6314,9855,15970,756,20997,50146,27586,43288,37479,32940,53370,54391,31570,7202,50405,12962,43397,28825,10124,35681,861,5645,30854,43558,31654,25865,20859,6992,31399,18393,33112,52700,31048,38879,46144,6019,3412,45461,24635,45550,27335,9105,14984,9841,31006,6144,17408,5896,43635,43653,17705,26079,50269,23171,47658,25485,16505,30661,34415,55347,38561,45021,14495,33565,32718,14770,44893,49845,49042,20282,21108,37001,52494,5647,23909,5038,3000,41378,2656,45182,28882,53861,48783,27945,28551,33434,44835,15420,16892,32792,22174,37300,6205,48307,24627,47724,38514,54168,36421,9852,15759,37377,7242,34459,22226,40657,16057,21164,40449,26544,25871,49062,28767,2128,30543,20789,53162,21069,26385,28015,26530,26978,32323,37086,36902,36009,6758,37366,25892,10752,24565,42921,24677,31192,17356,44847,14395,20931,3729,53901,36004,17585,24062,25974,25144,8087,32631,51372,47603,47922,14869,25824,20539,50443,41709,36465,15774,18070,11296,40333,29895,23247,23151,27034,2520,3235,5395,54445,35916,35335,3318,34258,48010,6927,38997,22889,46539,17385,4167,46570,42211,19105,41411,25031,15304,11697,17467,23631,12251,28337,50797,13652,7183,1755,3205,45496,53173,47625,49838,49430,45657,12310,45329,11486,12457,52228,42226,30053,34211,44419,40029,48182,13377,51382,35779,50001,55084,773,38962,11769,39677,14775,10539,3310,42114,28198,4247,47863,42613,52955,37843,2596,30508,51482,40667,12659,2823,1652,45717,43228,42950,39744,19123,20452,42347,10,30062,3043,43673,53061,83,40730,16270,20675,17622,109,32058,43085,20869,28285,22476,2783,1473,16000,44721,46478,13730,13549,7447,39958,35507,1477,43863,38621,18354,704,40165,13065,23175,7866,20651,40311,16913,16896,21630,18602,23902,27471,20640,15493,49917,21178,27151,1022,41335,31113,11076,40763,45079,29540,36953,16718,29763,10902,41244,11042,43143,46280,7300,46357,368,38036,9238,48295,29402,51635,29305,46138,51866,47818,8087,14310,49256,22929,38630,16199,1903,12219,31149,10448,40899,29049,26982,5288,53905,25945,44781,1793,23359,17068,37627,6350,18494,32386,5447,49921,54896,54649,45788,49622,11247,22520,5058,21837,47839,4261,31614,950,54235,956,2599,21168,40335,12018,47437,40507,54788,34682,55373,51749,37240,21585,24566,28654,14558,29038,28399,49891,10593,47096,38888,3213,19793,27567,5245,25356,21099,54394,39676,53885,49521,30717,7233,54201,26140,28640,27975,54328,42349,13545,11938,52000,10535,6543,35932,30635,44300,11482,52723,11559,25916,30361,16285,7683,7326,40729,27492,34860,13201,50718,1446,10746,55364,3009,10125,54106,887,5996,31741,13548,43815,20756,8346,14758,51383,2696,38125,28842,9054,9976,28713,2002,22286,21652,39827,50813,38869,15976,49957,37143,26300,30923,18840,4980,31614,12018,53937,47550,11800,9792,11950,16679,3537,30727,2338,20274,20652,25857,49677,32657,28583,25159,10693,35758,18529,50727,23997,569,48889,53478,9381,27794,21832,23248,25174,12509,7942,28732,33644,33141,12870,18659,27383,1882,46082,34776,30157,7810,42043,21529,32036,21709,3464,28554,34918,25643,24144,23898,25815,26846,19583,41341,7759,23105,2719,29089,19481,11364,41411,31842,49545,3826,47686,48251,29098,1354,27179,39588,26940,5704,10605,37539,33737,5692,9875,8788,30976,38666,7318,30456,15196,16759,4011,35198,9441,21335,5930,48900,706,41270,13045,27142,28446,4614,49987,30314,4026,9112,42169,39253,15792,42113,43058,45222,29626,50682,52234,46099,44213,38414,7815,52420,42416,3539,39804,35925,1101,17020,33200,54653,9030,21816,31386,27046,3927,33885,21325,23404,6240,31912,54151,40304,36899,42115,20279,53915,54644,34478,21290,50680,34898,17121,5351,42494,15249,24764,11276,21904,49560,13187,30049,5943,45729,52692,38279,36348,4156,3290,14764,50844,6212,9951,17246,54268,36614,20310,44257,34680,19413,29580,49341,36979,51753,52755,34447,34470,26814,40118,15192,52903,32408,1828,34546,43381,10607,9379,40986,48724,47257,10164,40661,13454,40449,45412,7945,5545,54553,651,5901,4312,18831,6127,26189,51409,21012,14183,6060,28880,32062,41132,8387,12349,33159,11667,8018,27045,19267,906,18278,45555,11131,37926,18945,35607,54757,2809,32369,3284,31495,12842,4228,40315,46684,51713,50925,53905,31957,37155,4674,24614,19168,28945,44579,18097,33103,51398,46354,27150,54284,11722,36033,54137,9811,18610,42638,41523,3117,24458,3341,16963,27661,6728,15354,25198,15876,39613,1870,55380,14111,49830,1367,37718,18144,41618,50230,15773,48175,6760,45967,21544,5897,41833,9694,39023,48732,30033,35535,12590,41197,21057,6585,2075,29854,35091,18605,20959,53956,14538,21201,45427,53714,46227,33810,51297,47215,35606,45172,32297,5996,12074,6270,16689,23024,7998,32692,32251,16964,27626,31942,9063,40547,3169,15983,53674,8908,16612,300,29978,53137,51570,43811,28277,53779,52066,55338,30514,13250,12766,13324,2459,4927,18648,42831,43464,40494,37527,24378,24349,26702,6370,16950,27493,34183,8649,13052,586,45384,17554,44038,38279,22434,98,39351,33247,14483,44846,35220,9958,53727,11623,12909,54444,14940,41190,13743,25277,47986,31642,48602,15267,42749,20831,45460,5616,19043,37430,12864,10756,13930,29716,806,21322,39597,48662,48573,46196,12183,26325,11319,2167,47799,5035,36730,41053,25145,21148,21668,47744,49672,13749,50792,37604,37009,4945,19283,32320,43039,30951,25188,9327,37303,32468,25402,38211,38810,53310,38507,21433,51191,7481,50071,35355,51511,20659,33867,10187,53576,34363,45328,52505,50253,23662,7838,37392,53538,42242,5070,21730,1325,19967,37883,52031,40927,9857,44302,34061,11685,34515,49596,32858,752,22010,17952,10950,12142,7245,33628,3805,40019,54593,21329,51115,35086,3735,24124,8197,9031,27489,22345,33802,40563,11786,14272,43259,30875,43498,12875,32461,24376,8887,5541,54615,51523,41668,6932,33415,11887,20784,14812,45379,33300,16687,8944,54077,21615,11606,33102,33350,2963,36401,7773,48593,36100,6308,5625,40234,38510,15598,5759,6577,48159,29086,22458,37978,1990,42605,48555,3095,17693,23614,44124,53967,49098,10125,8184,26880,14834,50480,50980,52380,11211,23624,27260,23549,29497,20970,40583,19127,912,40349,24037,10743,47016,30882,53110,1891,3845,2041,28354,29667,10340,28470,50445,38817,52496,39789,36674,44345,12262,47164,30634,14004,7317,32514,35837,25087,4605,28134,29807,43470,47090,451,15666,29276,37866,31157,54729,36766,43492,38718,3284,37557,52010,27116,15036,43216,39089,33902,10088,51378,271,4593,38404,24545,46137,22272,12202,34520,9671,6426,38072,37898,49957,15534,16128,398,10469,48495,41821,47158,30677,51884,24357,10264,5794,55259,33345,6769,33073,34118,53431,46879,51172,25188,32838,9058,27773,38856,21210,28545,19939,23004,54118,5364,18094,33968,49594,44948,13566,38728,18789,50014,5241,9262,55351,39160,1218,23835,21595,21105,27822,46708,55211,5773,21356,16866,52246,11104,24755,54295,17199,3290,54117,8242,32492,11177,53249,14471,19461,39199,17453,12632,12107,6714,21657,40769,2282,49180,36947,47992,32161,12016,27087,45157,15626,28316,5687,3710,10276,18544,11868,40242,45259,9351,13815,48187,37494,54907,49163,25128,19989,7170,22276,44609,28989,18182,43191,446,55333,27718,29093,6598,15114,47896,7504,3222,52509,31254,38808,11506,22642,781,8132,35677,9470,10452,47185,20166,43629,16303,32630,22752,18640,37310,48529,18971,16513,33142,5007,12280,35758,10459,3703,39060,46184,26528,38726,40636,10637,14926,47905,34673,16901,44571,2560,9017,43463,26699,17026,17120,18785,12107,44021,31055,39620,29409,9435,20785,15155,2923,6389,1218,12906,50115,43199,26061,42111,39940,7002,24395,16247,12066,50005,2456,16895,9744,52441,16197,37997,42907,50578,9945,12984,50078,847,46633,10830,10056,29976,41791,31924,42346,17647,12433,9121,13382,4901,624,21213,32105,5881,17571,28050,23452,16010,9382,19912,17085,4069,12392,5946,40487,35915,4937,6503,32228,20145,37850,53972,20449,19051,53816,14559,43543,36015,1266,19628,44776,12479,47175,13755,20591,46568,1037,35207,29934,12034,17399,50784,35227,47050,8019,11243,93,34254,35643,14936,25659,7186,5369,12973,6170,27072,35098,25083,51477,42982,4157,585,10564,41274,26468,3378,30694,17576,20837,16755,13476,37047,42156,4264,35294,35610,4991,40700,39942,12250,53847,41001,36107,39008,54318,5047,17109,37927,25498,342,25067,18187,1356,50698,43226,42539,49847,18855,39105,46655,618,2645,30289,10574,48882,2099,34766,26840,14077,5775,45041,9068,16308,12625,1204,30921,9813,18187,30865,29045,25429,38632,20104,28665,24427,12946,23257,16978,37482,16599,46192,49575,7604,1981,14275,37711,38349,52408,9030,7857,10918,26789,18874,33468,22943,51058,190,35771,36251,25371,16550,49660,19458,35642,55048,6196,15692,54627,24191,39799,15878,5920,44822,17368,15336,19117,52268,52046,32414,45253,32143,38220,5395,10684,10445,47652,5064,30951,52992,10822,37346,28291,38127,493,50138,7792,53741,30605,49900,39313,6933,6989,43487,4882,10958,27238,42120,35900,37073,34008,16773,23062,6299,41871,48082,1468,6512,5026,8247,10737,33029,29680,54418,21328,29111,10462,13975,48000,42740,4463,27030,42579,48118,23252,20122,50324,45193,44082,26880,47324,47711,3768,50265,13359,46748,18832,21165,45779,1780,35041,6785,45040,10666,49370,43262,9649,27941,23385,9080,11384,9470,41970,47877,31664,44165,32981,26479,4705,16661,13784,39519,6094,35502,13114,53535,26384,31559,41719,3564,45095,46230,29212,36672,23434,14510,6459,13363,35755,45159,27531,24653,2627,55418,40730,39649,2505,48598,49816,6547,12931,47045,28801,16091,18676,20589,33400,9417,45608,11835,23895,25679,23586,7094,55244,36903,53258,27146,25045,12118,21896,47459,21701,32457,34602,21406,26147,30779,17784,6413,31384,45453,7037,53263,3959,41714,40149,25427,43020,39631,16922,11608,41092,47662,30309,5164,20945,17552,3101,2766,28646,20183,18768,22273,10466,22186,34893,15193,52697,17609,9634,11408,22776,45839,93,52624,14538,7968,23567,35222,53088,10704,26885,55161,38925,25946,13026,30064,25439,41414,26210,30450,50299,5494,4677,41778,51985,3983,25224,9498,23780,5686,11879,29066,6071,24209,49165,22798,37314,44448,21038,26813,13946,24333,13880,22937,16347,9639,23383,37410,9660,11982,4064,45179,18239,19906,50319,52033,18917,47545,20534,24165,34122,19602,2114,53975,24079,43579,38316,1106,15919,38075,53310,40535,4418,9035,24802,15061,20802,39143,12419,5972,7943,53645,8453,22055,49638,31319,21492,49361,53657,43201,18795,7572,17338,30817,9312,12548,917,19870,46926,41397,273,15290,45513,16925,12462,8797,33389,31458,39297,49945,29418,6462,16210,27708,10486,21967,29947,38866,32884,33287,23314,40283,15675,27040,36033,28905,27580,15544,47587,14360,32795,1521,43009,54280,53299,46191,30603,44283,28355,53912,33037,38046,21422,47481,46072,34529,36736,15140,30198,20047,15832,11542,9998,13429,31296,12361,9643,20509,51944,40682,30677,36816,20436,37291,41305,40397,40829,8323,38163,41996,53544,40255,47436,38702,35993,49052,3162,41736,15636,32976,55435,40477,6295,23392,7857,28643,51103,18507,6329,7403,21585,45019,32116,40491,37692,26411,10013,34575,37707,19869,34356,38307,3464,4395,44050,3554,30966,21679,35072,11588,30157,626,31466,7602,42621,35716,44079,16170,30580,21576,9847,46390,33862,10350,13073,35071,1624,38436,12572,22090,41159,6050,32817,55232,50190,16840,20019,52324,19753,54547,54771,31480,40956,26674,9898,15706,44185,12841,16622,34975,41024,26475,54046,3705,7167,342,46148,10107,42681,17016,50174,45598,9542,43680,27899,27669,14535,42329,29769,23080,43966,37910,1593,22856,7633,10798,10296,10292,16535,53018,42082,27770,48048,27672,53442,48695,47433,25401,25949,53912,18304,16992,16914,54869,48030,34037,45495,29164,35415,20124,46954,34240,3449,16826,14216,28964,31743,200,16044,48819,11175,33376,53747,5583,45193,19018,40060,12611,26783,20231,13595,38262,40362,7705,11912,9249,50604,37321,17550,54540,10408,16505,30050,18614,30247,5780,5288,31670,37429,20529,36779,52919,31349,55340,45399,32846,50204,10724,19292,20121,21392,12271,30112,29062,28645,21033,49115,11981,35237,6024,20923,48995,31762,8344,10840,33398,10319,5600,41605,6610,37042,23760,17635,39220,20081,24944,9934,33352,17712,46910,11683,17435,43962,21867,3874,11467,30062,5683,35883,18306,43643,25528,45221,832,52558,22334,31069,29486,2856,31469,37743,37292,27482,43026,48066,14464,54220,18173,5428,1421,40008,35136,46187,55410,20614,39594,45489,9152,33884,466,39978,22639,30130,12194,21792,26558,25204,43790,16166,8693,51226,35053,26090,54676,33797,15101,925,36025,31856,8663,16798,19381,31626,6801,47392,53436,50085,49438,1087,16918,30542,18810,51810,43795,43652,44371,42992,45884,13419,7393,45213,28694,39852,32468,51642,41331,4562,24765,45739,54406,25679,13201,55230,31288,1504,32941,55093,32399,20902,46419,36429,12260,38547,42353,22915,25811,15401,51727,46516,215,3579,8891,1631,23371,20082,53843,39842,33204,22473,431,6960,38040,36639,26704,29804,53920,55113,23808,7943,5934,12161,28995,45484,46046,15703,26305,24627,4828,7613,561,44764,2714,12092,32820,17383,46528,22961,814,49646,8221,33761,33673,7407,10064,50893,52832,25722,23565,45982,16707,7092,27725,31247,52204,34831,40397,19320,37279,52977,37575,35495,21470,6635,51896,21987,39227,25928,27870,35366,47861,33211,8589,17963,50671,5717,53015,35792,18105,19338,48247,51741,5612,46808,55044,8987,4675,1765,47454,48598,39019,46057,53105,40352,2108,12148,54690,27236,6001,44841,41992,44107,24729,14660,50751,26699,27074,16998,11145,27436,2109,54017,30286,38462,25046,13500,33158,31213,5866,31139,6558,10303,10742,22121,18887,10682,10240,36506,375,26770,54802,13684,33282,16938,38104,53394,44538,50402,49519,50126,26288,40856,6066,31577,26655,17066,52577,25965,53723,41244,16733,22835,9959,15365,46479,26431,33070,42048,6910,5654,25772,36303,26874,49240,42257,42444,2408,71,42503,13880,5484,47935,49418,29399,45384,8734,40777,4808,20585,45795,24393,1584,53837,30197,1859,38581,15058,35204,11438,17564,12413,11976,51304,12420,13418,9694,24651,8416,12838,23088,15811,47226,43299,39209,13583,27777,15123,16270,8228,46406,6293,32752,12545,11218,6518,2835,9787,24138,27672,19952,45633,37981,51,18558,17573,31321,39014,20967,38552,18432,44782,1252,46758,39037,18407,9442,28809,54701,12181,8072,26318,11889,30866,11290,54512,44279,49738,12034,46363,494,10056,27797,13921,12611,38427,43671,16537,6435,17903,26153,32603,21613,32972,33191,31960,4110,40820,42878,18279,24955,31483,25121,21311,16751,10712,38762,13889,15334,16650,53431,20026,2138,44103,44543,2816,32275,50248,26042,46401,27338,8188,52068,23326,21566,17278,54166,16706,3529,37272,8290,7446,26675,2901,15119,27775,44186,31521,25503,27145,16174,29019,22661,29203,44871,3134,7806,53363,32827,13252,13772,19859,43930,46336,13644,53759,17020,1855,25296,34757,6754,53879,15835,51510,30993,3692,40735,9926,22242,20138,12335,10415,9922,13352,42297,50064,6145,25989,28006,53422,45031,22217,51904,10826,40598,44512,46632,27412,38268,27670,23423,19150,33118,37469,30516,23329,22369,32551,51645,12494,28324,43953,2530,4288,4590,27895,25090,4248,19736,36665,13040,27407,36119,27072,34094,90,36666,21829,14050,23221,46566,46686,19654,34317,41654,53076,40983,50065,46726,36144,30382,53477,31110,19349,31030,15262,43251,34616,17796,54253,31578,34081,25890,15741,36681,24311,2622,48398,54197,24328,18370,44208,33571,4577,32972,28239,42993,19409,46109,50777,4333,46624,38946,28830,46481,19383,44003,25178,42448,43638,273,18865,54216,3171,33813,50006,39014,45230,28917,26635,11393,22814,50817,25676,7017,17448,38001,27981,55475,46480,54579,3331,25125,43364,52256,3317,47761,26123,29509,33124,40455,30402,18695,27337,28499,25288,34309,28466,24657,54426,992,19167,45137,12746,28033,35201,12593,34476,52431,22386,2380,37057,20269,6194,15517,22958,45489,51158,48039,39733,362,27779,4195,1714,3558,320,26510,30295,26943,29901,25777,50218,18691,34724,39432,50495,9362,22187,50970,51839,39840,55477,27562,3673,44121,13660,10477,22127,48777,27631,46062,35130,11903,33827,27325,39845,16062,35503,14359,25624,20416,2316,42032,19222,34231,6336,19179,19336,21835,1626,39017,25792,24423,29098,10331,42541,15335,41544,29872,52509,33144,21456,11387,25854,47951,35352,46125,13522,19883,25125,53118,16457,8153,52524,27029,13701,4561,29140,53592,25950,42809,43816,35230,38659,13609,37188,25654,27326,1516,53932,7617,25971,22435,33134,39327,32608,36350,44257,33553,17620,54852,24788,32739,9650,36286,52806,26720,51377,26646,26029,8757,54359,24334,38098,14169,19499,55010,7946,27867,14997,22335,20411,26709,42205,1968,12491,49812,51136,17075,30028,25653,46870,13582,36361,25081,40387,7817,35888,21683,27462,16987,44948,50909,19151,35486,47715,41129,40548,42058,39443,48734,11138,14489,52903,4977,17664,46029,8246,35476,8456,23730,23388,25529,27768,30833,26374,6583,46546,43413,46841,16819,22363,40770,16244,36072,30495,19989,34959,41298,44593,44059,5384,20516,34655,36172,20859,13939,19201,44050,20766,15168,31933,16391,20733,11454,21652,10404,32558,26946,12333,13205,1241,23113,11479,17573,35018,49957,26533,7330,53182,16237,32300,52675,10384,42328,45281,41336,36767,43596,19965,20427,12063,18232,52061,6973,54693,15816,10487,32273,5104,16336,33752,41907,33630,11261,27868,31294,248,48773,21922,37646,2148,236,49014,49611,6159,31897,48689,25206,9378,24658,1320,12620,15926,45578,37445,53662,6376,8814,1650,4801,12930,1151,11213,53748,35264,19863,14276,5037,29360,41478,2607,280,17848,8096,12739,51523,51575,42864,8320,34225,30470,51715,48731,22590,19593,1833,41464,35063,47657,14113,51620,36102,12852,12309,49208,46204,2524,52601,37584,5826,25359,49727,4188,54985,35317,10502,6645,55226,45806,10410,20458,20168,17117,8567,48058,27394,28882,45621,11831,52990,34761,12395,33823,25023,20389,973,55467,27129,8831,23785,13896,40423,46935,54804,11610,17359,28098,34416,12523,19246,47863,13342,18618,27748,24562,11962,26433,36446,15004,35570,350,27935,30755,51596,25707,24808,1054,43122,38157,39757,20819,20347,9971,10424,15141,5782,29688,39170,28302,12667,1181,25686,31409,10487,38552,38593,19414,19622,6065,19910,9403,11216,44079,10839,39268,5846,5578,32625,27988,27044,4450,10396,6244,36363,26545,48719,5314,44093,2017,38102,32376,41550,31902,32888,12041,48839,1082,51038,36775,12015,20888,42793,15228,21898,18292,22550,13471,24904,32111,967,49282,615,40698,38115,34131,5668,3073,13326,37398,34914,55195,1670,41958,25882,1888,21960,34500,9140,1810,49035,17363,41125,40052,17544,32141,16474,31413,1039,9360,11767,16894,14987,39538,10728,31998,29738,16868,33468,1843,5244,42363,8165,49522,50573,16817,32936,7343,54879,48419,23691,23861,10369,15802,48648,6261,26285,48104,37248,34960,12303,24940,34908,9469,42885,51965,1509,23155,36774,49050,15900,12669,29719,37742,17676,27548,2538,14820,45969,25619,3723,2355,37885,4307,28107,27801,24125,43751,54503,43898,53625,48802,6562,22258,34036,28466,7770,45476,53885,23234,17736,53084,39752,9443,25916,21168,36688,47869,6352,29598,14470,19356,9002,2412,29071,10284,32142,17734,38234,8952,7788,6997,24545,20048,8511,12464,49663,51354,39428,23112,51494,51441,45392,25737,13159,803,14761,6317,4307,42308,11441,41357,40551,22689,6787,31185,22117,54044,42269,7877,20867,1517,18740,46386,25875,39412,39986,8993,44545,3516,24198,12647,55014,9416,2442,11123,35876,31015,42098,19019,23528,31193,49096,12065,5204,43548,5217,1728,811,12882,26440,27836,32011,14124,19144,38287,12412,37723,28756,22907,5710,34481,53066,19144,34726,29420,51863,41618,12137,26854,24648,25729,37620,22945,28518,22908,44617,7359,37853,33678,16712,48609,39317,6986,13314,37283,1197,20084,5587,8472,10121,6357,50800,5139,10533,23977,10216,29430,53216,13819,15499,18032,20777,12984,45040,32924,34351,39393,53844,33561,9079,1115,54288,24718,26638,23226,6592,38007,34865,30268,3776,26826,1155,37767,5869,6372,36463,18701,30764,38661,40139,1459,49872,24794,10796,18380,48404,42117,48205,34546,21701,19549,6858,52239,27002,4426,7971,8298,8185,19304,43563,1096,34911,52878,17018,2551,12005,27244,28027,37134,51985,37575,2072,54522,52736,21842,54452,13618,13359,25142,47794,49894,14925,40173,30101,16369,45623,54929,20376,2371,13754,48498,53522,3283,25656,48916,49009,46301,5129,12583,11445,24896,51990,23603,54704,4301,9199,49396,34960,29192,6276,15727,37871,19652,32781,41214,2515,51828,30105,19349,4876,9962,51329,48495,45210,36642,32736,47006,47154,27232,53603,54193,1508,3913,26599,28020,44995,5444,48076,38959,1073,48644,4623,35234,54206,33860,11336,16297,17006,55372,49535,36623,34465,18380,37749,41198,4727,21418,4519,36487,24329,53435,39457,30378,5902,47983,10136,51396,39251,42709,35775,1387,54901,50243,51086,10432,54656,31901,43617,51230,37012,10959,14878,6170,41291,40345,30505,37741,15972,7979,831,36891,55323,53586,33014,51849,32818,13926,46208,35592,814,7794,40357,16212,44520,445,5444,17431,26830,53552,43446,5143,50804,36661,37504,43541,49772,50788,48581,51448,30279,12164,46570,11444,2535,6997,34096,52301,47365,489,27250,14921,38445,6471,10636,23284,51724,13665,10275,1347,50589,10827,9442,53057,8133,10053,11019,43052,9883,19423,22902,274,21452,12305,41508,54848,11607,17474,36662,27955,55411,46240,25055,53371,42505,24507,46196,8916,4620,26805,1390,19131,16459,5074,38114,41315,21099,29212,48713,15300,12339,53512,32969,31538,22388,13538,10707,26154,3679,38372,51343,2055,48443,18932,43490,38680,50717,32980,45583,2120,5147,54043,42795,39733,26721,32868,51337,52639,4355,19781,9267,16227,42578,21569,21298,25486,32443,18020,41365,14525,31945,18380,6741,22620,48228,50631,48724,38454,19489,13079,47540,55349,35040,42508,51327,29797,36239,11431,35728,12333,34538,15141,10825,30196,47188,41046,9166,26236,12020,3764,1001,48210,47769,10282,21224,52668,51991,37790,13247,7160,43855,4557,15602,160,25571,16680,29434,36536,24090,16333,53652,6366,2641,12609,48878,23601,11520,40161,1961,24266,46263,19749,42370,24570,17777,38181,3789,6782,8908,49901,20534,41362,15852,45632,43974,33372,47801,27717,36226,14071,37408,22396,47191,38186,48797,508,8456,33944,14031,33925,23188,6960,22871,54557,43701,37951,20279,51256,46268,14035,20378,36261,20136,30366,34717,1999,35068,18224,36969,9058,38212,38878,11273,8385,49570,27912,33468,39184,13281,25348,28923,9395,45108,7626,24091,26482,50103,38010,37799,16801,46320,52723,35894,8389,36102,19372,13345,28122,43247,39920,24092,6868,37179,38004,604,53783,2793,6779,19221,15484,12465,27220,3464,15139,9734,34862,45382,7761,55054,5099,34875,14816,3412,55285,10759,10662,43221,54656,8831,23345,18109,32658,38409,34341,12902,38141,21537,47688,31869,25365,47144,13226,46916,17957,25377,29582,25914,13163,22105,8417,49720,52115,46482,37553,55173,17875,43375,40825,28598,27321,38800,21406,35036,21475,31243,20303,21112,29733,25732,49783,10158,34900,4629,14551,15818,8802,5245,9912,17305,40298,27422,27246,18562,2863,453,44019,33335,49891,13828,5886,19229,27104,28815,3967,25873,52929,43196,30351,16413,18510,30237,20549,16607,37874,53896,38810,8426,9340,3347,46277,55333,41186,48017,22889,24590,12826,23263,810,13610,15684,43478,55321,9519,33833,33850,42948,33579,51446,803,20006,19739,22113,47011,26264,3725,19636,45211,55070,773,11156,41155,32029,10688,23528,12838,11679,44961,26486,20340,19089,42008,12620,7108,14703,53283,44353,1439,15010,37008,37686,11061,15395,27542,32769,43060,24520,2416,33334,12138,5286,29895,1899,43441,13394,4542,39915,48828,41628,19286,8330,20717,23184,18280,53033,37548,4443,13829,28619,8271,30239,53974,2825,46315,41564,16920,37930,49310,4717,19855,42074,49742,29611,38648,35784,12052,8054,53270,26307,9440,52425,25818,29744,630,8738,38877,7131,28384,7461,22587,35831,24003,40692,14920,54127,49116,50756,52651,12660,40389,53077,39623,16539,43723,45758,670,24666,36915,7466,10536,11752,39014,32205,8252,23612,12165,48305,42777,14067,16764,14473,1688,10627,7101,22427,23095,9606,586,3121,21092,5109,38477,54608,14007,28692,35420,17540,6428,26663,7282,18615,43004,5916,55305,46655,37379,43307,51750,21781,54796,13796,5272,26013,50608,37493,42507,32734,32159,39891,29305,12201,52448,25247,10122,1028,48085,40328,24082,32518,14492,23772,46387,37281,40664,6632,17071,21764,45020,8978,5757,41463,13028,45730,31900,52266,16065,39491,36017,9306,9611,36603,50163,54779,49895,3463,55132,42762,27776,53607,20316,27848,6599,13243,39898,36787,46647,2534,4698,50148,7616,40695,35459,50717,10230,7907,52007,9133,22950,55253,54465,19608,34286,47140,39786,3384,53177,38079,23892,44184,41700,29389,38242,31094,36240,11244,433,45918,32886,54763,24832,44228,14826,19857,20230,53863,37476,8843,20734,41797,33477,6963,28561,11732,33697,27186,22906,5673,19360,33937,29961,2121,50385,6266,32403,23359,17804,41477,50948,14529,30520,8553,4337,35427,6649,19578,49337,27032,20718,8459,9631,25505,50474,39158,3279,14074,4159,36580,8133,37074,36600,6312,3622,54282,14614,20002,30122,46733,41087,15183,24254,11327,49202,23058,42495,50585,42562,49597,32980,5807,54987,28967,47413,20969,43954,41916,53100,36326,9019,53308,34863,8470,10614,3391,9732,2558,16084,49762,1775,11455,29452,48916,15180,44477,55243,5944,32976,9751,50409,2838,4664,28194,21969,38256,42350,34288,32529,53394,37013,49813,28477,22748,49316,33021,37589,50477,16514,6049,40191,11388,22213,33834,46812,39606,25989,35907,13955,36073,41834,40653,41507,38483,37482,34904,15750,38376,52658,5837,52835,29474,50333,49802,37401,28535,51141,5112,49479,4727,7776,16434,16505,21689,49437,47692,18219,11300,42685,25022,18414,16582,24293,30968,140,5921,19232,36420,18248,55177,26223,50577,1225,34841,1260,24715,23263,47482,53840,10126,55114,40188,37865,610,36539,38411,47266,45725,16417,11718,22546,24260,13277,37771,53191,25219,6174,18104,39737,42866,38834,28950,2495,8912,8989,15542,54328,29852,14399,39819,7978,16169,49324,30611,50384,42924,50781,39397,23758,46235,25557,28293,28901,34707,23900,56,52209,4103,8322,45962,49194,21226,6783,19329,25663,22242,24076,27487,5639,36791,50805,45928,35534,24168,42398,18485,18892,38393,36433,23761,42544,52594,22966,19614,47641,6261,23235,23987,19102,14011,21796,28232,19540,14068,7217,17593,33643,1639,47389,27602,29941,33139,41562,48824,27153,241,34633,7168,53453,42866,29914,24289,22690,11594,35216,55433,36591,49024,23141,16225,51141,46763,34515,8611,29749,10067,40475,17737,20360,55092,18435,46027,3562,31963,44499,3708,13352,14885,7676,15525,50650,11961,5417,2056,32064,38122,35872,32492,51106,51048,2424,52274,29288,3381,30475,43976,11765,40236,53271,23906,47466,17092,10445,50260,28748,17892,31454,10805,35300,2786,29929,5681,48347,3081,52175,26064,20047,26817,16962,46799,581,15856,12043,8859,29948,50175,16344,1801,13440,8507,17023,190,8472,23148,54934,32009,52433,45460,53171,33320,28811,29042,17879,20559,28570,29045,34999,48188,32761,46657,12401,52268,7909,28135,14672,1848,8127,18899,23977,32148,24670,14409,7920,34380,9831,40327,52094,15730,26619,3952,44092,33975,18370,50029,49666,51629,52046,46988,45158,15647,49742,14812,11566,42969,33326,30677,29905,40321,43302,20,11904,38902,49232,1670,55048,3625,10917,13844,48198,19303,51704,28392,45760,42219,16079,10222,20736,45241,40373,17713,41840,13719,16650,42551,28592,18697,55163,44392,4307,39635,35081,32575,43736,43268,50451,22648,32999,13332,17789,4579,34881,44944,19897,7689,27442,5095,35039,39474,19225,45914,49705,53850,35385,31772,53781,24774,41831,1078,27503,25884,24896,48925,14812,24452,35412,54381,53659,20387,36491,24689,15537,25278,16864,2164,35941,15744,30127,55471,16760,6655,3988,3791,19727,17926,53629,9935,31602,40661,14445,33155,42138,3940,16411,1566,12625,44861,16966,51311,3079,14829,10747,10224,41099,47411,44107,43613,50357,17000,8115,45331,40279,50031,44374,10710,7359,1310,760,13030,715,40855,3158,3079,8192,35654,23561,12652,21342,11827,6432,47920,22256,13653,48966,51720,30329,25173,47810,17918,406,8567,35491,24123,53008,13189,22660,29065,9459,1857,24072,31997,52711,37598,37428,42698,21920,2309,9176,4111,27810,55358,18700,437,3868,39524,40817,4374,44317,13372,4025,44185,41360,53414,30397,41696,46975,40613,26722,20336,37476,3106,31208,12708,12821,7184,258,22694,11067,842,4535,642,23157,44509,35643,9723,38490,50854,1757,48402,48942,35806,8244,4383,24183,32553,50098,20264,17202,27710,52957,52046,47636,43206,28609,53671,49041,5538,37195,34225,49031,25753,23699,42481,6060,31669,42380,16524,51629,30386,31059,54029,24628,38304,41104,11237,52444,12677,10658,53140,1412,11543,26427,2332,37809,18239,38514,32017,18948,3563,17969,45978,50911,3846,42278,48226,22737,2158,7997,19785,37246,44085,52187,39373,31004,1545,33888,2274,55418,48259,1673,9576,22619,17338,35582,38961,43046,39948,19170,52990,4096,27338,47028,44810,20536,17018,18215,12592,11436,53152,41286,19736,16232,40233,52795,9596,23747,32043,17901,32367,25468,39559,14194,23012,26340,44639,41520,11813,16790,17104,27470,11735,50418,40679,26380,275,50946,25456,14990,17100,10047,50144,36846,25511,4372,1494,8905,10207,23425,44603,43456,41168,49512,27899,46313,36180,15043,48996,43629,47260,6588,32707,8271,54302,1610,22905,46320,39783,19625,21227,13153,3979,55443,35699,25055,46765,25851,44119,16774,38015,3695,4263,7593,25043,21211,1066,14240,8098,54727,43098,38299,48489,33573,26732,25438,37599,13777,5580,48597,30197,4141,53660,52006,50419,49602,13062,13300,19611,1610,5999,40068,54850,35019,19143,20715,25820,34431,25479,19902,29300,2134,25406,51034,38081,1009,35582,52960,32636,33570,8287,17861,22636,4535,6075,4918,14416,45010,52948,45087,26363,51932,22315,3344,26367,2897,49883,33507,22455,50350,50,14490,20930,6640,15787,42928,14297,18814,7744,30786,52358,8463,1909,16108,1276,52299,48264,11334,55067,28727,30010,14756,7927,19316,45226,16227,12914,37968,45586,17786,27819,10979,17999,10662,1435,18430,9008,51627,30999,2421,38498,8128,32504,27162,24174,1709,10719,24499,29076,28016,30295,32546,37186,25393,33364,24890,10199,40836,16278,13683,17515,11865,5010,14535,49141,4109,25154,34830,52516,53262,29945,14327,48286,20755,40698,16506,2894,32444,18907,16324,24972,31248,12308,16328,28243,44771,34307,31920,46582,1970,49393,15719,37746,47497,19172,41330,54910,33447,53994,26837,43209,15516,50966,15007,7908,10326,21968,24346,35775,13064,43627,55018,19432,7516,7525,24323,14193,41761,29097,39446,995,34188,53369,31571,47948,37902,49311,256,48643,42949,31554,34885,36250,13961,17201,27637,9084,42953,25237,51706,43580,21970,2245,24194,49149,22814,27442,23079,29643,32552,48376,37881,53412,4664,1393,32044,15435,4464,11773,14314,45361,7553,19295,16243,54650,50966,40940,2992,50089,43054,19522,12174,4945,25210,39162,16237,9009,3323,54523,33137,24786,11026,19818,14652,36170,40984,6189,54659,22283,27150,3357,18346,20838,16130,10306,1124,24518,28315,16221,50725,30722,5634,38099,33702,25151,30179,34740,32227,542,46937,5700,13853,50492,10126,2293,23937,50820,45434,20009,6923,26902,50138,18862,44693,41563,4723,7909,48192,24268}; -// expected = 0; -// actual = test.findPairs(nums, k); -// assertEquals(expected, actual); -// } - - @Test - public void test5() { - k = -1; - nums = new int[]{1, 2, 3, 4, 5}; - expected = 0; - actual = test.findPairs(nums, k); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_533Test.java b/src/test/java/com/fishercoder/_533Test.java deleted file mode 100644 index b1f4d0bcae..0000000000 --- a/src/test/java/com/fishercoder/_533Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._533; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _533Test { - private static _533.Solution1 solution1; - private static char[][] picture; - - @BeforeClass - public static void setup() { - solution1 = new _533.Solution1(); - } - - @Test - public void test1() { - picture = new char[][]{ - {'W', 'B', 'W', 'B', 'B', 'W'}, - {'W', 'B', 'W', 'B', 'B', 'W'}, - {'W', 'B', 'W', 'B', 'B', 'W'}, - {'W', 'W', 'B', 'W', 'B', 'W'}}; - assertEquals(6, solution1.findBlackPixel(picture, 3)); - } - - @Test - public void test2() { - picture = new char[][]{{'B'}}; - assertEquals(1, solution1.findBlackPixel(picture, 1)); - } -} diff --git a/src/test/java/com/fishercoder/_536Test.java b/src/test/java/com/fishercoder/_536Test.java deleted file mode 100644 index cc57a125d2..0000000000 --- a/src/test/java/com/fishercoder/_536Test.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._536; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; - -public class _536Test { - private static _536.Solution1 solution1; - private static _536.Solution2 solution2; - private static TreeNode expected; - private static String s; - - @BeforeClass - public static void setup() { - solution1 = new _536.Solution1(); - solution2 = new _536.Solution2(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - s = "4(2(3)(1))(6(5))"; - expected = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 3, 1, 5)); - assertEquals(expected, solution1.str2tree(s)); - assertEquals(expected, solution2.str2tree(s)); - } - - @Test - public void test2() { - s = "51(232)(434)"; - expected = TreeUtils.constructBinaryTree(Arrays.asList(51, 232, 434)); - assertEquals(expected, solution1.str2tree(s)); - assertEquals(expected, solution2.str2tree(s)); - } -} diff --git a/src/test/java/com/fishercoder/_537Test.java b/src/test/java/com/fishercoder/_537Test.java deleted file mode 100644 index 5228a3d096..0000000000 --- a/src/test/java/com/fishercoder/_537Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._537; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 1/25/17. - */ -public class _537Test { - private static _537 .Solution1 solution1; - private static _537 .Solution2 solution2; - private static String expected; - private static String a; - private static String b; - - @BeforeClass - public static void setup() { - solution1 = new _537.Solution1(); - solution2 = new _537.Solution2(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - expected = "0+2i"; - a = "1+1i"; - b = "1+1i"; - assertEquals(expected, solution1.complexNumberMultiply(a, b)); - assertEquals(expected, solution2.complexNumberMultiply(a, b)); - } - - @Test - public void test2() { - expected = "0+-2i"; - a = "1+-1i"; - b = "1+-1i"; - assertEquals(expected, solution2.complexNumberMultiply(a, b)); - } -} diff --git a/src/test/java/com/fishercoder/_538Test.java b/src/test/java/com/fishercoder/_538Test.java deleted file mode 100644 index 30b3ef0007..0000000000 --- a/src/test/java/com/fishercoder/_538Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._538; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _538Test { - private static _538.Solution2 solution2; - private static _538.Solution1 solution1; - private static TreeNode expectedRoot; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _538.Solution1(); - solution2 = new _538.Solution2(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - root = new TreeNode(5); - root.left = new TreeNode(2); - root.right = new TreeNode(13); - expectedRoot = new TreeNode(18); - expectedRoot.left = new TreeNode(20); - expectedRoot.right = new TreeNode(13); - assertEquals(expectedRoot.toString(), solution2.convertBST(root).toString()); - assertEquals(expectedRoot.toString(), solution1.convertBST(root).toString()); - } - - @Test - public void test2() { - root = null; - expectedRoot = null; - assertEquals(expectedRoot, solution2.convertBST(root)); - assertEquals(expectedRoot, solution1.convertBST(root)); - } -} diff --git a/src/test/java/com/fishercoder/_539Test.java b/src/test/java/com/fishercoder/_539Test.java deleted file mode 100644 index 9f275d5862..0000000000 --- a/src/test/java/com/fishercoder/_539Test.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._539; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _539Test { - private static _539.Soluiton1 soluiton1; - private static int expected; - private static int actual; - private static List timePoints; - - @BeforeClass - public static void setup() { - soluiton1 = new _539.Soluiton1(); - } - - @Before - public void setupForEachTest() { - expected = 0; - actual = 0; - } - - @Test - public void test1() { - timePoints = new ArrayList<>(Arrays.asList("23:59", "00:00")); - expected = 1; - actual = soluiton1.findMinDifference(timePoints); - assertEquals(expected, actual); - } - - @Test - public void test2() { - timePoints = new ArrayList<>(Arrays.asList("23:59", "00:00", "01:20")); - expected = 1; - actual = soluiton1.findMinDifference(timePoints); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_53Test.java b/src/test/java/com/fishercoder/_53Test.java deleted file mode 100644 index 50bae09af9..0000000000 --- a/src/test/java/com/fishercoder/_53Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._53; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _53Test { - private static _53.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _53.Solution1(); - } - - @Before - public void clear() { - solution1 = new _53.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {-2, 1, -3, 4, -1, 2, 1, -5, 4}; - assertEquals(6, solution1.maxSubArray(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_540Test.java b/src/test/java/com/fishercoder/_540Test.java deleted file mode 100644 index 975ca4b9dc..0000000000 --- a/src/test/java/com/fishercoder/_540Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._540; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _540Test { - private static _540.Solution1 solution1; - private static _540.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _540.Solution1(); - solution2 = new _540.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{1, 1, 2, 3, 3, 4, 4, 8, 8}; - assertEquals(2, solution1.singleNonDuplicate(nums)); - assertEquals(2, solution2.singleNonDuplicate(nums)); - } - - @Test - public void test2() { - nums = new int[]{3, 3, 7, 7, 10, 11, 11}; - assertEquals(10, solution1.singleNonDuplicate(nums)); - assertEquals(10, solution2.singleNonDuplicate(nums)); - } - - @Test - public void test3() { - nums = new int[]{1, 1, 2}; - assertEquals(2, solution1.singleNonDuplicate(nums)); - assertEquals(2, solution2.singleNonDuplicate(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_541Test.java b/src/test/java/com/fishercoder/_541Test.java deleted file mode 100644 index 24a812ec92..0000000000 --- a/src/test/java/com/fishercoder/_541Test.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._541; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _541Test { - private static _541.Solution1 solution1; - private static String expected; - private static String actual; - private static String s; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _541.Solution1(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - s = "abcd"; - k = 5; - expected = "dcba"; - actual = solution1.reverseStr(s, k); - assertEquals(expected, actual); - } - - @Test - public void test2() { - s = "abcdefg"; - k = 2; - expected = "bacdfeg"; - actual = solution1.reverseStr(s, k); - assertEquals(expected, actual); - } - - @Test - public void test3() { - s = "abcd"; - k = 4; - expected = "dcba"; - actual = solution1.reverseStr(s, k); - assertEquals(expected, actual); - } - - @Test - public void test4() { - s = "abcdefg"; - k = 3; - expected = "cbadefg"; - actual = solution1.reverseStr(s, k); - assertEquals(expected, actual); - } - - @Test - public void test5() { - s = "abcd"; - k = 3; - expected = "cbad"; - actual = solution1.reverseStr(s, k); - assertEquals(expected, actual); - } - - @Test - public void test6() { - s = "hyzqyljrnigxvdtneasepfahmtyhlohwxmkqcdfehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqlimjkfnqcqnajmebeddqsgl"; - System.out.println("s.length() = " + s.length()); - k = 39; - expected = "fdcqkmxwholhytmhafpesaentdvxginrjlyqzyhehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqllgsqddebemjanqcqnfkjmi"; - actual = solution1.reverseStr(s, k); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_543Test.java b/src/test/java/com/fishercoder/_543Test.java deleted file mode 100644 index 71302b2e7c..0000000000 --- a/src/test/java/com/fishercoder/_543Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._543; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.TestCase.assertEquals; - -public class _543Test { - private static _543.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _543.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); - TreeUtils.printBinaryTree(root); - assertEquals(3, solution1.diameterOfBinaryTree(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_544Test.java b/src/test/java/com/fishercoder/_544Test.java deleted file mode 100644 index 892d387492..0000000000 --- a/src/test/java/com/fishercoder/_544Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._544; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _544Test { - private static _544 test; - private static int n; - private static String expected; - private static String actual; - - @BeforeClass - public static void setup() { - test = new _544(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - n = 2; - expected = "(1,2)"; - actual = test.findContestMatch(n); - assertEquals(expected, actual); - } - - @Test - public void test2() { - n = 4; - expected = "((1,4),(2,3))"; - actual = test.findContestMatch(n); - assertEquals(expected, actual); - } - - @Test - public void test3() { - n = 8; - expected = "(((1,8),(4,5)),((2,7),(3,6)))"; - actual = test.findContestMatch(n); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_545Test.java b/src/test/java/com/fishercoder/_545Test.java deleted file mode 100644 index 43d05e043d..0000000000 --- a/src/test/java/com/fishercoder/_545Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._545; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _545Test { - private static _545.Solution1 test; - private static TreeNode root; - private static List expected; - - @BeforeClass - public static void setup() { - test = new _545.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4, 5, null, 6, 7)); - TreeUtils.printBinaryTree(root); - expected = Arrays.asList(1, 2, 4, 6, 7, 5, 3); - assertEquals(expected, test.boundaryOfBinaryTree(root)); - } - -} diff --git a/src/test/java/com/fishercoder/_547Test.java b/src/test/java/com/fishercoder/_547Test.java deleted file mode 100644 index 86dcfbdd41..0000000000 --- a/src/test/java/com/fishercoder/_547Test.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._547; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 1/9/17. - */ -public class _547Test { - private static _547.Solution1 test; - private static int expected; - private static int actual; - private static int[][] M; - - @BeforeClass - public static void setup() { - test = new _547.Solution1(); - } - - @Test - public void test1() { - M = new int[][]{ - {1, 1, 0}, - {1, 1, 0}, - {0, 0, 1}, - }; - expected = 2; - actual = test.findCircleNum(M); - assertEquals(expected, actual); - } - - @Test - public void test2() { - M = new int[][]{ - {1, 1, 0}, - {1, 1, 1}, - {0, 1, 1}, - }; - expected = 1; - actual = test.findCircleNum(M); - assertEquals(expected, actual); - } - - @Test - public void test3() { - M = new int[][]{ - {1, 0, 0, 1}, - {0, 1, 1, 0}, - {0, 1, 1, 1}, - {1, 0, 1, 1}, - }; - expected = 1; - actual = test.findCircleNum(M); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_548Test.java b/src/test/java/com/fishercoder/_548Test.java deleted file mode 100644 index c30e6a85d0..0000000000 --- a/src/test/java/com/fishercoder/_548Test.java +++ /dev/null @@ -1,2061 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._548; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _548Test { - private static _548.Solution1 test; - private static boolean expected; - private static boolean actual; - private static int[] nums; - - @BeforeClass - public static void setup() { - test = new _548.Solution1(); - } - - @Before - public void setupForEachTest() { - nums = new int[1000]; - } - - @Test - public void test1() { - nums = new int[]{1, 2, 1, 2, 1, 2, 1}; - expected = true; - actual = test.splitArray(nums); - assertEquals(expected, actual); - } - - @Test - public void test2() { - nums = new int[]{1, 2, 1, 2, 1, 2, 1, 2}; - expected = false; - actual = test.splitArray(nums); - assertEquals(expected, actual); - } - - @Test - public void test3() { - nums = new int[]{ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - }; - expected = false; - long start = System.currentTimeMillis(); - actual = test.splitArray(nums); - System.out.println("It took " + (System.currentTimeMillis() - start) + " ms to solve this test case."); - assertEquals(expected, actual); - } - - @Test - public void test4() { - //equalSum is 3, j = 6, k = 9 - nums = new int[]{1, 2, 1, 3, 0, 0, 2, 2, 1, 3, 3}; - expected = true; - actual = test.splitArray(nums); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_54Test.java b/src/test/java/com/fishercoder/_54Test.java deleted file mode 100644 index 372bc8adaa..0000000000 --- a/src/test/java/com/fishercoder/_54Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._54; - -import java.util.Arrays; - -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _54Test { - - private static _54.Solution1 solution1; - private static int[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _54.Solution1(); - } - - @Test - public void test1() { - matrix = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9}, - }; - assertEquals(Arrays.asList(1, 2, 3, 6, 9, 8, 7, 4, 5), solution1.spiralOrder(matrix)); - } - - @Test - public void test2() { - matrix = new int[][]{}; - assertEquals(Arrays.asList(), solution1.spiralOrder(matrix)); - } -} diff --git a/src/test/java/com/fishercoder/_551Test.java b/src/test/java/com/fishercoder/_551Test.java deleted file mode 100644 index 76e858fd9b..0000000000 --- a/src/test/java/com/fishercoder/_551Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._551; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _551Test { - private static _551.Solution1 test; - private static boolean expected; - private static boolean actual; - private static String s; - - @BeforeClass - public static void setup() { - test = new _551.Solution1(); - } - - @Test - public void test1() { - s = "ALLAPPL"; - expected = false; - actual = test.checkRecord(s); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_553Test.java b/src/test/java/com/fishercoder/_553Test.java deleted file mode 100644 index 3ec9be9546..0000000000 --- a/src/test/java/com/fishercoder/_553Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._553; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/25/17. - */ -public class _553Test { - private static _553.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _553.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1000, 100, 10, 2}; - assertEquals("1000/(100/10/2)", solution1.optimalDivision(nums)); - } - - @Test - public void test2() { - nums = new int[]{1000, 100, 40, 10, 2}; - assertEquals("1000/(100/40/10/2)", solution1.optimalDivision(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_554Test.java b/src/test/java/com/fishercoder/_554Test.java deleted file mode 100644 index a45a57ff4e..0000000000 --- a/src/test/java/com/fishercoder/_554Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._554; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _554Test { - private static _554.Solution1 test; - private static int expected; - private static int actual; - private static List> wall; - - @BeforeClass - public static void setup() { - test = new _554.Solution1(); - } - - @Before - public void setupForEachTest() { - } - - @Test - public void test1() { - wall = new ArrayList<>(); - wall.add(Arrays.asList(1, 2, 2, 1)); - wall.add(Arrays.asList(3, 1, 2)); - wall.add(Arrays.asList(1, 3, 2)); - wall.add(Arrays.asList(2, 4)); - wall.add(Arrays.asList(3, 1, 2)); - wall.add(Arrays.asList(1, 3, 1, 1)); - expected = 2; - actual = test.leastBricks(wall); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_555Test.java b/src/test/java/com/fishercoder/_555Test.java deleted file mode 100644 index 464a538f94..0000000000 --- a/src/test/java/com/fishercoder/_555Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._555; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 4/29/17. - */ -public class _555Test { - private static _555.Solution1 solution1; - private static String expected; - private static String actual; - private static String[] strs; - - @BeforeClass - public static void setup() { - solution1 = new _555.Solution1(); - } - - @Test - public void test1() { - strs = new String[]{"abc", "xyz"}; - expected = "zyxcba"; - actual = solution1.splitLoopedString(strs); - assertEquals(expected, actual); - } - - @Test - public void test2() { - strs = new String[]{"lc", "evol", "cdy"}; - expected = "ylclovecd"; - actual = solution1.splitLoopedString(strs); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_559Test.java b/src/test/java/com/fishercoder/_559Test.java deleted file mode 100644 index 5364768492..0000000000 --- a/src/test/java/com/fishercoder/_559Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.Node; -import com.fishercoder.solutions._559; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _559Test { - private static _559.Solution1 solution1; - private static Node root; - - @BeforeClass - public static void setup() { - solution1 = new _559.Solution1(); - } - - @Test - public void test1() { - root = new Node(1); - Node node3 = new Node(3); - Node node2 = new Node(2); - Node node4 = new Node(4); - root.children = Arrays.asList(node3, node2, node4); - Node node5 = new Node(5); - Node node6 = new Node(6); - node3.children = Arrays.asList(node5, node6); - - assertEquals(3, solution1.maxDepth(root)); - } -} diff --git a/src/test/java/com/fishercoder/_55Test.java b/src/test/java/com/fishercoder/_55Test.java deleted file mode 100644 index 74f60b699e..0000000000 --- a/src/test/java/com/fishercoder/_55Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._55; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _55Test { - private static _55.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _55.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{0, 2, 3}; - assertEquals(false, solution1.canJump(nums)); - } - - @Test - public void test2() { - nums = new int[]{1, 2}; - assertEquals(true, solution1.canJump(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_560Test.java b/src/test/java/com/fishercoder/_560Test.java deleted file mode 100644 index 462e80f91b..0000000000 --- a/src/test/java/com/fishercoder/_560Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._560; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 4/29/17. - */ -public class _560Test { - private static _560.Solution1 solution1; - private static int expected; - private static int actual; - private static int[] nums; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _560.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 1, 1}; - k = 2; - expected = 2; - actual = solution1.subarraySum(nums, k); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_561Test.java b/src/test/java/com/fishercoder/_561Test.java deleted file mode 100644 index 7109a466c2..0000000000 --- a/src/test/java/com/fishercoder/_561Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._561; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 4/23/17. - */ -public class _561Test { - private static _561.Solution1 solution1; - private static int expected; - private static int actual; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _561.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 4, 3, 2}; - expected = 4; - actual = solution1.arrayPairSum(nums); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_562Test.java b/src/test/java/com/fishercoder/_562Test.java deleted file mode 100644 index 29d5199726..0000000000 --- a/src/test/java/com/fishercoder/_562Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._562; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 4/23/17. - */ -public class _562Test { - private static _562.Solution1 solution1; - private static int expected; - private static int actual; - private static int[][] M; - - @BeforeClass - public static void setup() { - solution1 = new _562.Solution1(); - } - - @Test - public void test1() { - M = new int[][]{ - {0, 1, 1, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 1} - }; - expected = 3; - actual = solution1.longestLine(M); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_563Test.java b/src/test/java/com/fishercoder/_563Test.java deleted file mode 100644 index 774c943eb7..0000000000 --- a/src/test/java/com/fishercoder/_563Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._563; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -/** - * Created by fishercoder on 4/23/17. - */ -public class _563Test { - private static _563.Solution1 solution1; - private static int expected; - private static int actual; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _563.Solution1(); - actual = 0; - } - - @Test - public void test1() { - root = new TreeNode(1); - root.left = new TreeNode(2); - root.right = new TreeNode(3); - expected = 1; - actual = solution1.findTilt(root); - assertEquals(expected, actual); - } - - @Ignore - @Test - public void test2() { - root = new TreeNode(1); - root.left = new TreeNode(2); - root.right = new TreeNode(3); - root.left.left = new TreeNode(4); - root.right.left = new TreeNode(5); - expected = 11; - actual = solution1.findTilt(root); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_566Test.java b/src/test/java/com/fishercoder/_566Test.java deleted file mode 100644 index 205bb401a9..0000000000 --- a/src/test/java/com/fishercoder/_566Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._566; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -/** - * Created by fishercoder on 4/29/17. - */ -public class _566Test { - private static _566.Solution1 solution1; - private static int[][] expected; - private static int[][] actual; - private static int[][] nums; - private static int r; - private static int c; - - @BeforeClass - public static void setup() { - solution1 = new _566.Solution1(); - } - - @Test - public void test1() { - nums = new int[][]{ - {1, 2}, - {3, 4}, - }; - r = 1; - c = 4; - expected = new int[][]{{1, 2, 3, 4}}; - actual = solution1.matrixReshape(nums, r, c); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_567Test.java b/src/test/java/com/fishercoder/_567Test.java deleted file mode 100644 index 47642074d0..0000000000 --- a/src/test/java/com/fishercoder/_567Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._567; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _567Test { - private static _567.Solution1 solution1; - private static boolean expected; - private static boolean actual; - private static String s1; - private static String s2; - - @BeforeClass - public static void setup() { - solution1 = new _567.Solution1(); - } - - @Test - public void test1() { - s1 = "ab"; - s2 = "eidbaooo"; - expected = true; - actual = solution1.checkInclusion(s1, s2); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_56Test.java b/src/test/java/com/fishercoder/_56Test.java deleted file mode 100644 index aa3e175f5b..0000000000 --- a/src/test/java/com/fishercoder/_56Test.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.Interval; -import com.fishercoder.solutions._56; -import java.util.ArrayList; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _56Test { - private static _56.Solution1 solution1; - private static List intervals; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _56.Solution1(); - } - - @Test - public void test1() { - intervals = new ArrayList(); - intervals.add(new Interval(2, 3)); - intervals.add(new Interval(5, 5)); - intervals.add(new Interval(2, 2)); - intervals.add(new Interval(3, 4)); - intervals.add(new Interval(3, 4)); - - expected = new ArrayList<>(); - expected.add(new Interval(2, 4)); - expected.add(new Interval(5, 5)); - assertEquals(expected, solution1.merge(intervals)); - } - - @Test - public void test2() { - intervals = new ArrayList(); - intervals.add(new Interval(1, 3)); - intervals.add(new Interval(2, 6)); - intervals.add(new Interval(8, 10)); - intervals.add(new Interval(15, 18)); - - expected = new ArrayList<>(); - expected.add(new Interval(1, 6)); - expected.add(new Interval(8, 10)); - expected.add(new Interval(15, 18)); - assertEquals(expected, solution1.merge(intervals)); - } -} diff --git a/src/test/java/com/fishercoder/_572Test.java b/src/test/java/com/fishercoder/_572Test.java deleted file mode 100644 index df69cf520d..0000000000 --- a/src/test/java/com/fishercoder/_572Test.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._572; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/6/17. - */ -public class _572Test { - private static _572.Solution1 solution1; - private static boolean expected; - private static boolean actual; - - @BeforeClass - public static void setup() { - solution1 = new _572.Solution1(); - } - - @Test - public void test1() { - TreeNode s = new TreeNode(3); - s.left = new TreeNode(4); - s.right = new TreeNode(5); - s.left.left = new TreeNode(1); - s.left.right = new TreeNode(2); - s.left.right.left = new TreeNode(0); - - TreeNode t = new TreeNode(4); - t.left = new TreeNode(1); - t.right = new TreeNode(2); - expected = false; - actual = solution1.isSubtree(s, t); - assertEquals(expected, actual); - } - - @Test - public void test2() { - TreeNode s = new TreeNode(3); - s.left = new TreeNode(4); - s.right = new TreeNode(5); - s.left.left = new TreeNode(1); - s.left.right = new TreeNode(2); - - TreeNode t = new TreeNode(4); - t.left = new TreeNode(1); - t.right = new TreeNode(2); - expected = true; - actual = solution1.isSubtree(s, t); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_575Test.java b/src/test/java/com/fishercoder/_575Test.java deleted file mode 100644 index a1c7bb4942..0000000000 --- a/src/test/java/com/fishercoder/_575Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._575; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/6/17. - */ -public class _575Test { - private static _575.Solution1 solution1; - private static int expected; - private static int actual; - private static int[] candies; - - @BeforeClass - public static void setup() { - solution1 = new _575.Solution1(); - } - - @Test - public void test1() { - candies = new int[]{1, 1, 2, 3}; - expected = 2; - actual = solution1.distributeCandies(candies); - assertEquals(expected, actual); - } - - @Test - public void test2() { - candies = new int[]{1, 1, 2, 2, 3, 3}; - expected = 3; - actual = solution1.distributeCandies(candies); - assertEquals(expected, actual); - } - - @Test - public void test3() { - candies = new int[]{1000, 1, 1, 1}; - expected = 2; - actual = solution1.distributeCandies(candies); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_57Test.java b/src/test/java/com/fishercoder/_57Test.java deleted file mode 100644 index d5e8809ddf..0000000000 --- a/src/test/java/com/fishercoder/_57Test.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.Interval; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._57; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _57Test { - private static _57.Solution1 solution1; - private static List intervals; - private static List expected; - private static List actual; - - @BeforeClass - public static void setup() { - solution1 = new _57.Solution1(); - } - - @Test - public void test1() { - intervals = new ArrayList<>(Arrays.asList(new Interval(1, 3), new Interval(6, 9))); - expected = new ArrayList<>(Arrays.asList(new Interval(1, 5), new Interval(6, 9))); - CommonUtils.printIntervals(intervals); - actual = solution1.insert(intervals, new Interval(2, 5)); - CommonUtils.printIntervals(actual); - assertEquals(expected, actual); - } - - - @Test - public void test2() { - intervals = new ArrayList<>(Arrays.asList(new Interval(1, 2), new Interval(3, 5), new Interval(6, 7), new Interval(8, 10), new Interval(12, 16))); - CommonUtils.printIntervals(intervals); - expected = new ArrayList<>(Arrays.asList(new Interval(1, 2), new Interval(3, 10), new Interval(12, 16))); - actual = solution1.insert(intervals, new Interval(4, 9)); - CommonUtils.printIntervals(actual); - assertEquals(expected, actual); - } - - @Test - public void test3() { - intervals = new ArrayList<>(Arrays.asList(new Interval(1, 5))); - CommonUtils.printIntervals(intervals); - expected = new ArrayList<>(Arrays.asList(new Interval(1, 5))); - actual = solution1.insert(intervals, new Interval(2, 3)); - CommonUtils.printIntervals(actual); - assertEquals(expected, actual); - } - - @Test - public void test4() { - intervals = new ArrayList<>(Arrays.asList()); - CommonUtils.printIntervals(intervals); - expected = new ArrayList<>(Arrays.asList(new Interval(5, 7))); - actual = solution1.insert(intervals, new Interval(5, 7)); - CommonUtils.printIntervals(actual); - assertEquals(expected, actual); - } - - @Test - public void test5() { - intervals = new ArrayList<>(Arrays.asList(new Interval(1, 5))); - expected = new ArrayList<>(Arrays.asList(new Interval(1, 5), new Interval(6, 8))); - CommonUtils.printIntervals(intervals); - actual = solution1.insert(intervals, new Interval(6, 8)); - CommonUtils.printIntervals(actual); - assertEquals(expected, actual); - } - - @Test - public void test6() { - intervals = new ArrayList<>(Arrays.asList(new Interval(1, 5))); - expected = new ArrayList<>(Arrays.asList(new Interval(0, 5))); - CommonUtils.printIntervals(intervals); - actual = solution1.insert(intervals, new Interval(0, 3)); - CommonUtils.printIntervals(actual); - assertEquals(expected, actual); - } - - @Test - public void test7() { - intervals = new ArrayList<>(Arrays.asList(new Interval(1, 5))); - expected = new ArrayList<>(Arrays.asList(new Interval(0, 0), new Interval(1, 5))); - CommonUtils.printIntervals(intervals); - actual = solution1.insert(intervals, new Interval(0, 0)); - CommonUtils.printIntervals(actual); - assertEquals(expected, actual); - } - - @Test - public void test8() { - intervals = new ArrayList<>(Arrays.asList(new Interval(2, 5), new Interval(6, 7), new Interval(8, 9))); - expected = new ArrayList<>(Arrays.asList(new Interval(0, 1), new Interval(2, 5), new Interval(6, 7), new Interval(8, 9))); - CommonUtils.printIntervals(intervals); - actual = solution1.insert(intervals, new Interval(0, 1)); - CommonUtils.printIntervals(actual); - assertEquals(expected, actual); - } - - @Test - public void test9() { - intervals = new ArrayList<>(Arrays.asList(new Interval(2, 4), new Interval(5, 7), new Interval(8, 10), new Interval(11, 13))); - expected = new ArrayList<>(Arrays.asList(new Interval(2, 7), new Interval(8, 10), new Interval(11, 13))); - CommonUtils.printIntervals(intervals); - actual = solution1.insert(intervals, new Interval(3, 6)); - CommonUtils.printIntervals(actual); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_581Test.java b/src/test/java/com/fishercoder/_581Test.java deleted file mode 100644 index 736f8fa8bc..0000000000 --- a/src/test/java/com/fishercoder/_581Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._581; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/17/17. - */ -public class _581Test { - private static _581.Solution1 solution1; - private static _581.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _581.Solution1(); - solution2 = new _581.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3, 4}; - assertEquals(0, solution1.findUnsortedSubarray(nums)); - assertEquals(0, solution2.findUnsortedSubarray(nums)); - } - - @Test - public void test2() { - nums = new int[]{2, 6, 4, 8, 10, 9, 15}; - assertEquals(5, solution1.findUnsortedSubarray(nums)); - assertEquals(5, solution2.findUnsortedSubarray(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_582Test.java b/src/test/java/com/fishercoder/_582Test.java deleted file mode 100644 index 5be6909750..0000000000 --- a/src/test/java/com/fishercoder/_582Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._582; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/18/17. - */ -public class _582Test { - private static _582.Solution1 solution1; - private static List pid; - private static List ppid; - private static List expected; - private static Integer kill; - - @BeforeClass - public static void setup() { - solution1 = new _582.Solution1(); - } - - @Test - public void test1() { - pid = Arrays.asList(1, 3, 10, 5); - ppid = Arrays.asList(3, 0, 5, 3); - kill = 5; - expected = Arrays.asList(5, 10); - assertEquals(expected, solution1.killProcess(pid, ppid, kill)); - } - - @Test - public void test2() { - pid = Arrays.asList(1, 3, 10, 5); - ppid = Arrays.asList(3, 0, 5, 3); - kill = 3; - expected = Arrays.asList(3, 1, 5, 10); - assertEquals(expected, solution1.killProcess(pid, ppid, kill)); - } -} diff --git a/src/test/java/com/fishercoder/_583Test.java b/src/test/java/com/fishercoder/_583Test.java deleted file mode 100644 index beccba8c3b..0000000000 --- a/src/test/java/com/fishercoder/_583Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._583; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/18/17. - */ -public class _583Test { - private static _583.Solution1 solution1; - private static String word1; - private static String word2; - - @BeforeClass - public static void setup() { - solution1 = new _583.Solution1(); - } - - @Test - public void test1() { - word1 = "sea"; - word2 = "eat"; - assertEquals(2, solution1.minDistance(word1, word2)); - } - - @Test - public void test2() { - word1 = "sea"; - word2 = "ate"; - assertEquals(4, solution1.minDistance(word1, word2)); - } -} diff --git a/src/test/java/com/fishercoder/_588Test.java b/src/test/java/com/fishercoder/_588Test.java deleted file mode 100644 index 223caf91b5..0000000000 --- a/src/test/java/com/fishercoder/_588Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._588; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/23/17. - */ -public class _588Test { - - private static _588.Solution1.FileSystem fileSystem; - - @BeforeClass - public static void setup() { - } - - @Test - public void test1() { - fileSystem = new _588.Solution1.FileSystem(); - List list = new ArrayList<>(); - assertEquals(list, fileSystem.ls("/")); - fileSystem.mkdir("/a/b/c"); - fileSystem.addContentToFile("/a/b/c/d", "hello"); - list.add("a"); - assertEquals(list, fileSystem.ls("/")); - assertEquals("hello", fileSystem.readContentFromFile("/a/b/c/d")); - } - - @Test - public void test2() { - fileSystem = new _588.Solution1.FileSystem(); - List list = new ArrayList<>(); - assertEquals(list, fileSystem.ls("/")); - fileSystem.mkdir("/a/b/c"); - list.add("c"); - assertEquals(list, fileSystem.ls("/a/b")); - } -} diff --git a/src/test/java/com/fishercoder/_589Test.java b/src/test/java/com/fishercoder/_589Test.java deleted file mode 100644 index 45ad4fb6f4..0000000000 --- a/src/test/java/com/fishercoder/_589Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.Node; -import com.fishercoder.solutions._589; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _589Test { - private static _589.Solution1 solution1; - private static Node root; - private static List expectedPreOrder; - - @BeforeClass - public static void setup() { - solution1 = new _589.Solution1(); - } - - @Test - @Ignore//todo: Node.createNaryTree method hasn't been implemented yet - public void test1() { - expectedPreOrder = Arrays.asList(1, 3, 5, 6, 2, 4); - root = Node.createNaryTree(expectedPreOrder); - assertEquals(expectedPreOrder, solution1.preorder(root)); - } -} diff --git a/src/test/java/com/fishercoder/_58Test.java b/src/test/java/com/fishercoder/_58Test.java deleted file mode 100644 index 9dca450c7a..0000000000 --- a/src/test/java/com/fishercoder/_58Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._58; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _58Test { - private static _58.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _58.Solution1(); - } - - @Test - public void test1() { - assertEquals(5, solution1.lengthOfLastWord("Hello World")); - } -} diff --git a/src/test/java/com/fishercoder/_591Test.java b/src/test/java/com/fishercoder/_591Test.java deleted file mode 100644 index 2b798f6fde..0000000000 --- a/src/test/java/com/fishercoder/_591Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._591; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _591Test { - private static _591.Solution1 test; - - @BeforeClass - public static void setup() { - test = new _591.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, test.isValid("

This is the first line ]]>
")); - } - - @Test - public void test2() { - assertEquals(false, test.isValid("This is the first line ]]>"));//tag_name is too long (> 9) - } -} diff --git a/src/test/java/com/fishercoder/_592Test.java b/src/test/java/com/fishercoder/_592Test.java deleted file mode 100644 index 0895f74c4a..0000000000 --- a/src/test/java/com/fishercoder/_592Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._592; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/23/17. - */ -public class _592Test { - private static _592.Solution1 test; - private static String expression; - - @BeforeClass - public static void setup() { - test = new _592.Solution1(); - } - - @Test - public void test1() { - expression = "-1/2+1/2+1/3"; - assertEquals("1/3", test.fractionAddition(expression)); - } -} diff --git a/src/test/java/com/fishercoder/_593Test.java b/src/test/java/com/fishercoder/_593Test.java deleted file mode 100644 index 59aef626f4..0000000000 --- a/src/test/java/com/fishercoder/_593Test.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._593; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/22/17. - */ -public class _593Test { - private static _593.Solution1 solution1; - private static int[] p1; - private static int[] p2; - private static int[] p3; - private static int[] p4; - - @BeforeClass - public static void setup() { - solution1 = new _593.Solution1(); - } - - @Test - public void test1() { - p1 = new int[]{0, 0}; - p2 = new int[]{1, 1}; - p3 = new int[]{1, 0}; - p4 = new int[]{0, 1}; - assertEquals(true, solution1.validSquare(p1, p2, p3, p4)); - } - - @Test - public void test2() { - p1 = new int[]{1, 1}; - p2 = new int[]{5, 3}; - p3 = new int[]{3, 5}; - p4 = new int[]{7, 7}; - assertEquals(false, solution1.validSquare(p1, p2, p3, p4)); - } - - @Test - public void test3() { - p1 = new int[]{0, 0}; - p2 = new int[]{0, 0}; - p3 = new int[]{0, 0}; - p4 = new int[]{0, 0}; - System.out.println(solution1.noDuplicate(p1, p2, p3, p4)); - assertEquals(false, solution1.validSquare(p1, p2, p3, p4)); - } -} diff --git a/src/test/java/com/fishercoder/_594Test.java b/src/test/java/com/fishercoder/_594Test.java deleted file mode 100644 index 5a6417a06d..0000000000 --- a/src/test/java/com/fishercoder/_594Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._594; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/20/17. - */ -public class _594Test { - private static _594.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _594.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 3, 2, 2, 5, 2, 3, 7}; - assertEquals(5, solution1.findLHS(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_59Test.java b/src/test/java/com/fishercoder/_59Test.java deleted file mode 100644 index dc31098345..0000000000 --- a/src/test/java/com/fishercoder/_59Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._59; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _59Test { - private static _59.Solution1 solution1; - private static int[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _59.Solution1(); - } - - @Test - public void test1() { - matrix = solution1.generateMatrix(6); - CommonUtils.print2DIntArray(matrix); - } -} diff --git a/src/test/java/com/fishercoder/_5Test.java b/src/test/java/com/fishercoder/_5Test.java deleted file mode 100644 index 3c7f5e829d..0000000000 --- a/src/test/java/com/fishercoder/_5Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._5; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _5Test { - private static _5.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _5.Solution1(); - } - - @Test - public void test1() { - assertEquals("bab", solution1.longestPalindrome("babad")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_600Test.java b/src/test/java/com/fishercoder/_600Test.java deleted file mode 100644 index 7fc438d926..0000000000 --- a/src/test/java/com/fishercoder/_600Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._600; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/28/17. - */ -public class _600Test { - private static _600.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _600.Solution1(); - } - - @Test - public void test1() { - assertEquals(5, solution1.findIntegers(5)); - assertEquals(514229, solution1.findIntegers(100000000)); - } -} diff --git a/src/test/java/com/fishercoder/_605Test.java b/src/test/java/com/fishercoder/_605Test.java deleted file mode 100644 index 7feb222b30..0000000000 --- a/src/test/java/com/fishercoder/_605Test.java +++ /dev/null @@ -1,160 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._605; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _605Test { - private static _605.Solution1 solution1; - private static _605.Solution2 solution2; - private static int[] flowerbed; - private static int n; - - @BeforeClass - public static void setup() { - solution1 = new _605.Solution1(); - solution2 = new _605.Solution2(); - } - - @Test - public void test1() { - flowerbed = new int[]{1, 0, 0, 0, 1}; - n = 1; - assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test2() { - flowerbed = new int[]{1, 0, 0, 0, 1}; - n = 2; - assertEquals(false, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test3() { - flowerbed = new int[]{1, 0, 0, 0, 0, 1}; - n = 2; - assertEquals(false, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test4() { - flowerbed = new int[]{1, 0, 1, 0, 1, 0, 1}; - n = 1; - assertEquals(false, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test5() { - flowerbed = new int[]{0, 0, 1, 0, 1}; - n = 1; - assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test6() { - flowerbed = new int[]{1, 0, 0, 0, 1, 0, 0}; - n = 2; - assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test7() { - flowerbed = new int[]{0, 0, 1, 0, 0}; - n = 2; - assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test8() { - flowerbed = new int[]{1}; - n = 0; - assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test9() { - flowerbed = new int[]{0}; - n = 0; - assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test10() { - flowerbed = new int[]{0}; - n = 1; - assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test11() { - flowerbed = new int[]{1, 0, 0, 0, 1}; - n = 1; - assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test12() { - flowerbed = new int[]{1, 0, 0, 0, 1}; - n = 2; - assertEquals(false, solution2.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test13() { - flowerbed = new int[]{1, 0, 0, 0, 0, 1}; - n = 2; - assertEquals(false, solution2.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test14() { - flowerbed = new int[]{1, 0, 1, 0, 1, 0, 1}; - n = 1; - assertEquals(false, solution2.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test15() { - flowerbed = new int[]{0, 0, 1, 0, 1}; - n = 1; - assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test16() { - flowerbed = new int[]{1, 0, 0, 0, 1, 0, 0}; - n = 2; - assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test17() { - flowerbed = new int[]{0, 0, 1, 0, 0}; - n = 2; - assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test18() { - flowerbed = new int[]{1}; - n = 0; - assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test19() { - flowerbed = new int[]{0}; - n = 0; - assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); - } - - @Test - public void test20() { - flowerbed = new int[]{0}; - n = 1; - assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); - } -} diff --git a/src/test/java/com/fishercoder/_609Test.java b/src/test/java/com/fishercoder/_609Test.java deleted file mode 100644 index 3fc398d80c..0000000000 --- a/src/test/java/com/fishercoder/_609Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._609; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.List; - -public class _609Test { - private static _609.Solution1 solution1; - private static String[] paths; - private static List> actual; - - @BeforeClass - public static void setup() { - solution1 = new _609.Solution1(); - } - - @Test - public void test1() { - paths = new String[]{"root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"}; - actual = solution1.findDuplicate(paths); - CommonUtils.printListList(actual); - } -} diff --git a/src/test/java/com/fishercoder/_60Test.java b/src/test/java/com/fishercoder/_60Test.java deleted file mode 100644 index 0c28e11bb6..0000000000 --- a/src/test/java/com/fishercoder/_60Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._60; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _60Test { - private static _60.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _60.Solution1(); - } - - @Test - public void test1() { - assertEquals("231", solution1.getPermutation(3, 4)); - } -} diff --git a/src/test/java/com/fishercoder/_611Test.java b/src/test/java/com/fishercoder/_611Test.java deleted file mode 100644 index 30f97676cf..0000000000 --- a/src/test/java/com/fishercoder/_611Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._611; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _611Test { - private static _611.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _611.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{2, 2, 3, 4}; - assertEquals(3, solution1.triangleNumber(nums)); - } - - @Test - public void test2() { - nums = new int[]{85, 88, 57, 88, 61, 71, 24, 12, 11, 37, 37, 72, 43, 22, 65, 79, 34, 41, 37, 74, 28, 25, 73, 44, 11, 84, 29, 33, 21, 87, 87, 21, 14, 47, 45, 31, 40, 33, 85, 99, 47, 29, 13, 82, 56, 33, 47, 11, 80, 59, 16, 16, 57, 53, 37, 20, 90, 84, 97, 39, 50, 22, 44, 54, 67, 69, 46, 58, 11, 29, 89, 10, 89, 30, 12, 55, 30, 32, 23, 74, 69, 19, 48, 72, 42, 12, 19, 88, 39, 32, 98, 89, 99, 19, 78, 35, 45, 16, 22, 73, 86, 76, 39, 50, 40, 85, 85, 61, 71, 97, 48, 39, 21, 39, 35, 11, 58, 94, 37, 50, 13, 78, 67, 23, 24, 53, 98, 41, 85, 90, 85, 92, 31, 11, 46, 88, 83, 68, 87, 33, 13, 43, 95, 39, 33, 40, 47, 95, 28, 52, 91, 60, 19, 76, 80, 75, 51, 64, 67, 33, 43, 47, 15, 80, 36, 78, 33, 98, 18, 15, 77, 100, 60, 28, 51, 86, 30, 67, 48, 43, 62, 23, 82, 26, 93, 99, 19, 93, 21, 22, 34, 100, 46, 94, 42, 58, 31, 27, 81, 47, 90, 53, 85, 69, 41, 29, 73, 26, 23, 54, 59, 83, 99, 19, 28, 96, 15, 56, 40, 32, 32, 42, 85, 43, 71, 36, 98, 74, 38, 91, 11, 79, 62, 24, 34, 29, 10, 31, 54, 59, 97, 35, 23, 92, 72, 96, 52, 45, 57, 39, 69, 53, 89, 46, 44, 66, 43, 60, 62, 28, 53, 72, 82, 63, 92, 39, 20, 56, 72, 25, 36, 33, 60, 92, 29, 40, 21, 53, 45, 53, 80, 56, 42, 67, 79, 11, 39, 11, 77, 82, 97, 91, 55, 96, 73, 48, 26, 11, 90, 40, 28, 21, 35, 99, 25, 73, 39, 62, 66, 50, 72, 58, 26, 91, 96, 88, 98, 25, 99, 27, 26, 26, 40, 43, 80, 21, 99, 25, 94, 61, 32, 49, 40, 67, 91, 99, 17, 99, 76, 34, 21, 100, 14, 98, 68, 83, 54, 21, 52, 84, 37, 87, 100, 69, 43, 76, 31, 31, 39, 29, 90, 82, 37, 26, 36, 11, 37, 95, 26, 43, 29, 66, 70, 99, 88, 44, 37, 96, 83, 38, 44, 50, 48, 10, 98, 67, 58, 66, 74, 91, 24, 19, 82, 94, 94, 58, 89, 73, 92, 80, 19, 76, 60, 23, 54, 78, 55, 46, 20, 27, 88, 23, 93, 16, 60, 97, 67, 78, 86, 83, 73, 48, 32, 39, 83, 49, 58, 82, 50, 99, 67, 39, 79, 48, 76, 82, 72, 92, 32, 98, 97, 82, 44, 37, 11, 50, 94, 51, 89, 100, 46, 95, 10, 99, 39, 68, 81, 34, 61, 78, 95, 54, 85, 31, 74, 38, 95, 85, 88, 45, 78, 35, 82, 42, 19, 12, 89, 24, 42, 88, 57, 79, 82, 13, 22, 64, 66, 42, 73, 44, 54, 68, 70, 15, 27, 44, 72, 44, 49, 42, 40, 26, 50, 33, 24, 56, 79, 68, 100, 41, 42, 92, 89, 27, 50, 100, 35, 54, 89, 98, 32, 80, 15, 90, 66, 53, 27, 77, 82, 97, 58, 95, 70, 24, 79, 27, 29, 54, 90, 25, 76, 26, 90, 79, 67, 31, 85, 15, 74, 22, 59, 20, 71, 62, 15, 48, 31, 48, 91, 89, 19, 51, 17, 60, 13, 65, 44, 89, 19, 45, 72, 84, 94, 98, 21, 89, 24, 10, 19, 14, 26, 68, 100, 44, 26, 21, 65, 14, 79, 93, 48, 29, 60, 47, 84, 54, 13, 89, 45, 91, 29, 15, 65, 52, 39, 59, 13, 25, 59, 91, 22, 74, 77, 62, 95, 28, 86, 30, 24, 61, 53, 12, 16, 89, 79, 64, 36, 66, 47, 40, 13, 94, 28, 51, 64, 79, 38, 28, 54, 99, 11, 69, 22, 42, 89, 36, 62, 96, 32, 50, 24, 45, 90, 46, 13, 31, 84, 39, 84, 51, 34, 97, 13, 49, 10, 43, 42, 47, 86, 33, 76, 72, 39, 26, 77, 70, 100, 93, 64, 20, 80, 76, 48, 56, 32, 38, 56, 67, 15, 14, 14, 11, 61, 44, 31, 57, 39, 65, 71, 16, 68, 75, 47, 81, 55, 16, 80, 63, 10, 73, 81, 20, 20, 20, 34, 37, 40, 43, 44, 13, 89, 31, 75, 54, 37, 62, 76, 63, 94, 65, 82, 51, 69, 56, 76, 45, 12, 55, 25, 29, 65, 49, 16, 43, 48, 65, 20, 39, 81, 81, 61, 85, 73, 51, 81, 30, 18, 62, 54, 85, 85, 80, 59, 63, 37, 58, 32, 48, 84, 64, 50, 47, 99, 49, 62, 69, 60, 100, 67, 50, 16, 22, 65, 97, 58, 23, 70, 27, 14, 91, 84, 38, 91, 37, 31, 53, 100, 45, 29, 64, 75, 94, 70, 89, 78, 81, 70, 18, 79, 70, 83, 70, 61, 15, 42, 70, 77, 45, 36, 59, 65, 71, 41, 82, 89, 64, 48, 67, 89, 28, 20, 10, 99, 23, 27, 65, 77, 32, 96, 98, 59, 64, 99, 21, 77, 65, 43, 37, 62, 64, 78, 91, 57, 98, 75, 62, 92, 61, 27, 40, 51, 66, 55, 60, 44, 83, 71, 38, 34, 32, 72, 22, 45, 89, 26, 96, 87, 59, 92, 14, 94, 80, 44, 36, 94, 36, 21, 18, 95, 80, 88, 95, 87, 86, 53, 14, 55, 10, 24, 13, 11, 10, 61, 47, 76, 94, 75, 85, 25, 22, 22, 76, 40, 43, 69, 87, 30, 66, 43, 74, 70, 30, 94, 32, 47, 88, 95, 67, 25, 59, 64, 12, 20, 44, 48, 84, 21, 34, 63, 79, 21, 13, 33, 71, 16, 45, 37, 34, 39, 24, 64, 67, 42, 64, 65, 55, 89, 96, 72, 58, 54, 17, 43, 30, 54, 27, 82, 66, 17, 43, 41, 38, 84, 37, 93, 70, 75, 53, 81, 35, 87, 70, 77, 52, 92, 90, 19, 45, 33, 58, 20, 77, 88, 37, 49, 82, 22, 24, 63, 47}; - assertEquals(105489315, solution1.triangleNumber(nums)); - } - -} diff --git a/src/test/java/com/fishercoder/_617Test.java b/src/test/java/com/fishercoder/_617Test.java deleted file mode 100644 index 7e9f3e9cf1..0000000000 --- a/src/test/java/com/fishercoder/_617Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._617; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.TestCase.assertEquals; - -public class _617Test { - private static _617.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _617.Solution1(); - } - - @Test - public void test1() { - assertEquals(TreeUtils.constructBinaryTree(Arrays.asList(3, 4, 5, 5, 4, null, 7)), solution1.mergeTrees(TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5)), TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3, null, 4, null, 7)))); - } - -} diff --git a/src/test/java/com/fishercoder/_61Test.java b/src/test/java/com/fishercoder/_61Test.java deleted file mode 100644 index 649c462cee..0000000000 --- a/src/test/java/com/fishercoder/_61Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.solutions._61; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _61Test { - private static _61.Solution1 solution1; - private static ListNode expected; - private static ListNode actual; - private static ListNode head; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _61.Solution1(); - } - - @Test - public void test1() { - k = 2; - expected = new ListNode(4); - expected.next = new ListNode(5); - expected.next.next = new ListNode(1); - expected.next.next.next = new ListNode(2); - expected.next.next.next.next = new ListNode(3); - - head = new ListNode(1); - head.next = new ListNode(2); - head.next.next = new ListNode(3); - head.next.next.next = new ListNode(4); - head.next.next.next.next = new ListNode(5); - - actual = solution1.rotateRight(head, k); - assertEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_621Test.java b/src/test/java/com/fishercoder/_621Test.java deleted file mode 100644 index 66aa324da9..0000000000 --- a/src/test/java/com/fishercoder/_621Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._621; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _621Test { - private static _621.Solution1 solution1; - private static char[] tasks; - - @BeforeClass - public static void setup() { - solution1 = new _621.Solution1(); - } - - @Test - public void test1() { - tasks = new char[]{'A', 'A', 'A', 'B', 'B', 'B'}; - assertEquals(8, solution1.leastInterval(tasks, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_628Test.java b/src/test/java/com/fishercoder/_628Test.java deleted file mode 100644 index 3c2d8479eb..0000000000 --- a/src/test/java/com/fishercoder/_628Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._628; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _628Test { - private static _628.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _628.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3}; - assertEquals(6, solution1.maximumProduct(nums)); - } - - @Test - public void test2() { - nums = new int[]{1, 2, 3, 4}; - assertEquals(24, solution1.maximumProduct(nums)); - } - - @Test - public void test3() { - nums = new int[]{-4, -3, -2, -1, 60}; - assertEquals(720, solution1.maximumProduct(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_62Test.java b/src/test/java/com/fishercoder/_62Test.java deleted file mode 100644 index 3bb803f3c7..0000000000 --- a/src/test/java/com/fishercoder/_62Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._62; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _62Test { - private static _62.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _62.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.uniquePaths(1, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_630Test.java b/src/test/java/com/fishercoder/_630Test.java deleted file mode 100644 index b8d23f78aa..0000000000 --- a/src/test/java/com/fishercoder/_630Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._630; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _630Test { - private static _630.Solution1 solution1; - private static int[][] courses; - - @BeforeClass - public static void setup() { - solution1 = new _630.Solution1(); - } - - @Test - public void test1() { - courses = new int[][]{ - {100, 200}, - {200, 1300}, - {1000, 1250}, - {2000, 3200}, - {300, 1200} - }; - assertEquals(4, solution1.scheduleCourse(courses)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_631Test.java b/src/test/java/com/fishercoder/_631Test.java deleted file mode 100644 index abd97597e5..0000000000 --- a/src/test/java/com/fishercoder/_631Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._631; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _631Test { - private static _631.Solution1.Excel excel; - - @Test - public void test1() { - excel = new _631.Solution1.Excel(3, 'C'); - assertEquals(0, excel.get(1, 'A')); - excel.set(1, 'A', 1); - assertEquals(1, excel.get(1, 'A')); - } - - @Test - public void test2() { - excel = new _631.Solution1.Excel(3, 'C'); - assertEquals(0, excel.sum(1, 'A', new String[]{"A2"})); - excel.set(2, 'A', 1); - assertEquals(1, excel.get(1, 'A')); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_635Test.java b/src/test/java/com/fishercoder/_635Test.java deleted file mode 100644 index 7853903752..0000000000 --- a/src/test/java/com/fishercoder/_635Test.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._635; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static junit.framework.TestCase.assertEquals; - -/** - * Created by fishercoder on 9/9/17. - */ -public class _635Test { - private static _635.Solution1.LogSystem logSystem; - private static List expected; - - @BeforeClass - public static void setup() { - logSystem = new _635.Solution1.LogSystem(); - } - - @Before - public void clear() { - logSystem = new _635.Solution1.LogSystem(); - expected = new ArrayList<>(); - } - - @Test - public void test1() { - logSystem.put(1, "2017:01:01:23:59:59"); - logSystem.put(2, "2017:01:01:22:59:59"); - logSystem.put(3, "2016:01:01:00:00:00"); - expected = Arrays.asList(1, 2, 3); - assertEquals(expected, logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year")); - } - - @Test - public void test2() { - logSystem.put(1, "2017:01:01:23:59:59"); - logSystem.put(2, "2017:01:01:22:59:59"); - logSystem.put(3, "2016:01:01:00:00:00"); - expected = Arrays.asList(1, 2); - assertEquals(expected, logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour")); - } - -} diff --git a/src/test/java/com/fishercoder/_63Test.java b/src/test/java/com/fishercoder/_63Test.java deleted file mode 100644 index abf9c9b383..0000000000 --- a/src/test/java/com/fishercoder/_63Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._63; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _63Test { - private static _63.Solution1 solution1; - private static int[][] obstacleGrid; - - @BeforeClass - public static void setup() { - solution1 = new _63.Solution1(); - } - - @Test - public void test1() { - obstacleGrid = new int[][] { - {0, 0}, - {0, 1}, - }; - assertEquals(0, solution1.uniquePathsWithObstacles(obstacleGrid)); - } - - @Test - public void test2() { - obstacleGrid = new int[][] { - {0, 0, 0}, - {0, 1, 0}, - {0, 0, 0}, - }; - assertEquals(2, solution1.uniquePathsWithObstacles(obstacleGrid)); - } - - @Test - public void test3() { - int[][] obstacleGrid = new int[][] { - {1, 0} - }; - assertEquals(0, solution1.uniquePathsWithObstacles(obstacleGrid)); - } -} diff --git a/src/test/java/com/fishercoder/_643Test.java b/src/test/java/com/fishercoder/_643Test.java deleted file mode 100644 index 047d9155ee..0000000000 --- a/src/test/java/com/fishercoder/_643Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._643; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _643Test { - private static _643.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _643.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 12, -5, -6, 50, 3}; - assertEquals(12.75, solution1.findMaxAverage(nums, 4), 0.01); - } - - @Test - public void test2() { - nums = new int[]{-1}; - assertEquals(-1.0, solution1.findMaxAverage(nums, 1), 0.01); - } - - @Test - public void test3() { - nums = new int[]{-6662, 5432, -8558, -8935, 8731, -3083, 4115, 9931, -4006, -3284, -3024, 1714, -2825, -2374, -2750, -959, 6516, 9356, 8040, -2169, -9490, -3068, 6299, 7823, -9767, 5751, -7897, 6680, -1293, -3486, -6785, 6337, -9158, -4183, 6240, -2846, -2588, -5458, -9576, -1501, -908, -5477, 7596, -8863, -4088, 7922, 8231, -4928, 7636, -3994, -243, -1327, 8425, -3468, -4218, -364, 4257, 5690, 1035, 6217, 8880, 4127, -6299, -1831, 2854, -4498, -6983, -677, 2216, -1938, 3348, 4099, 3591, 9076, 942, 4571, -4200, 7271, -6920, -1886, 662, 7844, 3658, -6562, -2106, -296, -3280, 8909, -8352, -9413, 3513, 1352, -8825}; - assertEquals(37.25555555555555, solution1.findMaxAverage(nums, 90), 0.01); - } -} diff --git a/src/test/java/com/fishercoder/_645Test.java b/src/test/java/com/fishercoder/_645Test.java deleted file mode 100644 index 276cd1a26f..0000000000 --- a/src/test/java/com/fishercoder/_645Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._645; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _645Test { - private static _645.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _645.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 2, 4}; - assertArrayEquals(new int[]{2, 3}, solution1.findErrorNums(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_646Test.java b/src/test/java/com/fishercoder/_646Test.java deleted file mode 100644 index 5b13d224d0..0000000000 --- a/src/test/java/com/fishercoder/_646Test.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._646; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _646Test { - - private static _646.Solution1 solution1; - private static int[][] pairs; - - @BeforeClass - public static void setup() { - solution1 = new _646.Solution1(); - } - - @Test - public void test1() { - pairs = new int[][]{ - {1, 2}, - {2, 3}, - {5, 6}, - {3, 4} - }; - assertEquals(3, solution1.findLongestChain(pairs)); - } - - @Test - public void test2() { - pairs = new int[][]{ - {9, 10}, - {-9, 9}, - {-6, 1}, - {-4, 1}, - {8, 10}, - {7, 10}, - {9, 10}, - {2, 10} - }; - assertEquals(2, solution1.findLongestChain(pairs)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_647Test.java b/src/test/java/com/fishercoder/_647Test.java deleted file mode 100644 index aea9e08b04..0000000000 --- a/src/test/java/com/fishercoder/_647Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._647; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _647Test { - private static _647.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _647.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.countSubstrings("abc")); - } - -} diff --git a/src/test/java/com/fishercoder/_648Test.java b/src/test/java/com/fishercoder/_648Test.java deleted file mode 100644 index 85df5d1ae8..0000000000 --- a/src/test/java/com/fishercoder/_648Test.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._648; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _648Test { - private static _648.Solution1 solution1; - private static List dict; - private static String sentence; - - @BeforeClass - public static void setup() { - solution1 = new _648.Solution1(); - } - - @Test - public void test1() { - dict = Arrays.asList("cat", "bat", "rat"); - sentence = "the cattle was rattled by the battery"; - assertEquals("the cat was rat by the bat", solution1.replaceWords(dict, sentence)); - } - - @Test - public void test2() { - dict = Arrays.asList("e", "k", "c", "harqp", "h", "gsafc", "vn", "lqp", "soy", "mr", "x", - "iitgm", "sb", "oo", "spj", "gwmly", "iu", "z", "f", "ha", "vds", - "v", "vpx", "fir", "t", "xo", "apifm", "tlznm", "kkv", "nxyud", "j", - "qp", "omn", "zoxp", "mutu", "i", "nxth", "dwuer", "sadl", "pv", "w", - "mding", "mubem", "xsmwc", "vl", "farov", "twfmq", "ljhmr", "q", "bbzs", - "kd", "kwc", "a", "buq", "sm", "yi", "nypa", "xwz", "si", "amqx", "iy", "eb", - "qvgt", "twy", "rf", "dc", "utt", "mxjfu", "hm", "trz", "lzh", "lref", "qbx", - "fmemr", "gil", "go", "qggh", "uud", "trnhf", "gels", "dfdq", "qzkx", "qxw"); - sentence = "ikkbp miszkays wqjferqoxjwvbieyk gvcfldkiavww vhokchxz dvypwyb " - + "bxahfzcfanteibiltins ueebf lqhflvwxksi dco kddxmckhvqifbuzkhstp wc " - + "ytzzlm gximjuhzfdjuamhsu gdkbmhpnvy ifvifheoxqlbosfww mengfdydekwttkhbzenk wjhmmyltmeufqvcpcxg " - + "hthcuovils ldipovluo aiprogn nusquzpmnogtjkklfhta klxvvlvyh nxzgnrveghc mpppfhzjkbucv cqcft " - + "uwmahhqradjtf iaaasabqqzmbcig zcpvpyypsmodtoiif qjuiqtfhzcpnmtk yzfragcextvx ivnvgkaqs " - + "iplazv jurtsyh gzixfeugj rnukjgtjpim hscyhgoru aledyrmzwhsz xbahcwfwm hzd ygelddphxnbh " - + "rvjxtlqfnlmwdoezh zawfkko iwhkcddxgpqtdrjrcv bbfj mhs nenrqfkbf spfpazr " - + "wrkjiwyf cw dtd cqibzmuuhukwylrnld dtaxhddidfwqs bgnnoxgyynol hg " - + "dijhrrpnwjlju muzzrrsypzgwvblf zbugltrnyzbg hktdviastoireyiqf qvufxgcixvhrjqtna ipfzhuvgo daee " - + "r nlipyfszvxlwqw yoq dewpgtcrzausqwhh qzsaobsghgm ichlpsjlsrwzhbyfhm ksenb " - + "bqprarpgnyemzwifqzz oai pnqottd nygesjtlpala qmxixtooxtbrzyorn gyvukjpc s mxhlkdaycskj uvwmerplaibeknltuvd ocnn " - + "frotscysdyclrc ckcttaceuuxzcghw pxbd oklwhcppuziixpvihihp"; - assertEquals("i miszkays w gvcfldkiavww v dvypwyb bxahfzcfanteibiltins ueebf " - + "lqhflvwxksi dc k w ytzzlm gximjuhzfdjuamhsu gdkbmhpnvy i mengfdydekwttkhbzenk w " - + "h ldipovluo a nusquzpmnogtjkklfhta k nxzgnrveghc mpppfhzjkbucv c uwmahhqradjtf i z " - + "q yzfragcextvx i i j gzixfeugj rnukjgtjpim h a x h " - + "ygelddphxnbh rvjxtlqfnlmwdoezh z i bbfj mhs nenrqfkbf " - + "spfpazr w c dtd c dtaxhddidfwqs bgnnoxgyynol h " - + "dijhrrpnwjlju muzzrrsypzgwvblf z h q i daee r nlipyfszvxlwqw " - + "yoq dewpgtcrzausqwhh q i k bqprarpgnyemzwifqzz " - + "oai pnqottd nygesjtlpala q gyvukjpc s mxhlkdaycskj " - + "uvwmerplaibeknltuvd ocnn f c pxbd oklwhcppuziixpvihihp", solution1.replaceWords(dict, sentence)); - } - -} diff --git a/src/test/java/com/fishercoder/_649Test.java b/src/test/java/com/fishercoder/_649Test.java deleted file mode 100644 index abeabf483d..0000000000 --- a/src/test/java/com/fishercoder/_649Test.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._649; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/8/17. - */ -public class _649Test { - private static _649.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _649.Solution1(); - } - - @Test - public void test1() { - assertEquals("Dire", solution1.predictPartyVictory("RDDDR")); - } - - @Test - public void test2() { - assertEquals("Radiant", solution1.predictPartyVictory("RD")); - } - - @Test - public void test3() { - assertEquals("Dire", solution1.predictPartyVictory("RDD")); - } - - @Test - public void test4() { - assertEquals("Radiant", solution1.predictPartyVictory("RDDR")); - } - - @Test - public void test5() { - assertEquals("Dire", solution1.predictPartyVictory("RDDRD")); - } - - @Test - public void test6() { - assertEquals("Dire", solution1.predictPartyVictory("RDDDDDRR")); - } - - @Test - public void test7() { - assertEquals("Dire", solution1.predictPartyVictory("RDDDDRR")); - } -} diff --git a/src/test/java/com/fishercoder/_64Test.java b/src/test/java/com/fishercoder/_64Test.java deleted file mode 100644 index 9b15182842..0000000000 --- a/src/test/java/com/fishercoder/_64Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._64; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _64Test { - private static _64.Solution1 solution1; - private static int[][] grid; - - @BeforeClass - public static void setup() { - solution1 = new _64.Solution1(); - } - - @Test - public void test1() { - grid = new int[][] { - {1, 2}, - {1, 1} - }; - assertEquals(3, solution1.minPathSum(grid)); - } - - @Test - public void test2() { - grid = new int[][] {{1}}; - assertEquals(1, solution1.minPathSum(grid)); - } - - @Test - public void test3() { - grid = new int[][] { - {1, 3, 1}, - {1, 5, 1}, - {4, 2, 1} - }; - assertEquals(7, solution1.minPathSum(grid)); - } -} diff --git a/src/test/java/com/fishercoder/_650Test.java b/src/test/java/com/fishercoder/_650Test.java deleted file mode 100644 index 9a07d3f7f9..0000000000 --- a/src/test/java/com/fishercoder/_650Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._650; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _650Test { - private static _650.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _650.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.minSteps(3)); - } - - @Test - public void test2() { - assertEquals(9, solution1.minSteps(20)); - } - - @Test - public void test3() { - assertEquals(19, solution1.minSteps(19)); - } - - @Test - public void test4() { - assertEquals(0, solution1.minSteps(1)); - } - - @Test - public void test5() { - assertEquals(35, solution1.minSteps(741)); - } -} diff --git a/src/test/java/com/fishercoder/_651Test.java b/src/test/java/com/fishercoder/_651Test.java deleted file mode 100644 index 2eedf42799..0000000000 --- a/src/test/java/com/fishercoder/_651Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._651; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _651Test { - private static _651.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _651.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.maxA(3)); - } - - @Test - public void test2() { - assertEquals(324, solution1.maxA(20)); - } - - @Test - public void test3() { - assertEquals(256, solution1.maxA(19)); - } - - @Test - public void test4() { - assertEquals(1, solution1.maxA(1)); - } - - @Test - public void test5() { - assertEquals(1327104, solution1.maxA(50)); - } - - @Test - public void test6() { - assertEquals(9, solution1.maxA(7)); - } -} diff --git a/src/test/java/com/fishercoder/_653Test.java b/src/test/java/com/fishercoder/_653Test.java deleted file mode 100644 index 77750cbcb9..0000000000 --- a/src/test/java/com/fishercoder/_653Test.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._653; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; - -public class _653Test { - private static _653.Solution1 solution1; - private static boolean expected; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _653.Solution1(); - } - - @Before - public void setupForEachTest() { - root = null; - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(5, 3, 2, 4, 6, 7))); - expected = true; - assertEquals(expected, solution1.findTarget(root, 9)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 1, 3))); - expected = true; - assertEquals(expected, solution1.findTarget(root, 4)); - } - - @Test - public void test3() { - root = new TreeNode(1); - expected = false; - assertEquals(expected, solution1.findTarget(root, 2)); - } - - @Test - public void test4() { - /** - * 2 - * / \ - * 0 3 - * / \ - * -4 1 - * - * target = 1; - * expected = true; - * */ - - root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 0, -4, 1, 3))); - expected = true; - assertEquals(expected, solution1.findTarget(root, -1)); - } - - @Test - public void test5() { - root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 1, 3, -4, 0))); - expected = true; - assertEquals(expected, solution1.findTarget(root, 2)); - } - - @Test - public void test6() { - root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList( - 3393, 2264, 4972, 1908, 3252, 4128, 5140, 965, 2018, - 3082, null, 3838, 4196, 5085, null, 559, 1187, null, 2143, 2968, - null, 3810, 3957, null, 4825, null, null, 0, 908, 1135, 1659, null, - null, 2764, null, 3581, null, null, 4106, 4498, null, null, - 498, 821, null, null, null, 1262, 1826, 2513, 2910, 3486, 3708, - null, null, 4377, 4673, 231, null, null, null, null, 1482, - null, null, 2386, 2690, null, null, null, null, null, null, 4349, null, - null, null, 170, 376, 1421, 1613, null, null, 2534, null, - null, null, 96, null, null, null, 1303))); - expected = true; - assertEquals(expected, solution1.findTarget(root, 5831)); -// TreeUtils.printBinaryTree(root); -// assertEquals(expected, mapSolution.findTarget(root, 5831)); - } -} diff --git a/src/test/java/com/fishercoder/_654Test.java b/src/test/java/com/fishercoder/_654Test.java deleted file mode 100644 index 88236f092c..0000000000 --- a/src/test/java/com/fishercoder/_654Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._654; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _654Test { - private static int[] nums; - private static TreeNode expected; - private static _654.Solution1 solution1; - private static _654.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _654.Solution1(); - solution2 = new _654.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{3, 2, 1, 6, 0, 5}; - expected = TreeUtils.constructBinaryTree(Arrays.asList(6, 3, 5, null, 2, 0, null, null, 1)); - assertEquals(expected, solution1.constructMaximumBinaryTree(nums)); - assertEquals(expected, solution2.constructMaximumBinaryTree(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_655Test.java b/src/test/java/com/fishercoder/_655Test.java deleted file mode 100644 index 761babf1d4..0000000000 --- a/src/test/java/com/fishercoder/_655Test.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._655; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _655Test { - private static List> expected; - private static TreeNode root; - private static _655.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _655.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2)); - expected = new ArrayList<>(2); - List row1 = new ArrayList<>(3); - row1.add(""); - row1.add("1"); - row1.add(""); - expected.add(row1); - List row2 = new ArrayList<>(3); - row2.add(0, "2"); - row2.add(""); - row2.add(""); - expected.add(row2); - CommonUtils.printListList(expected); - assertEquals(expected, solution1.printTree(root)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4)); - TreeUtils.printBinaryTree(root); - CommonUtils.printListList(solution1.printTree(root)); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, null, 30, 10, null, null, 15, null, 45)); - TreeUtils.printBinaryTree(root); - CommonUtils.printListList(solution1.printTree(root)); - System.out.println(solution1.printTree(root)); - } -} diff --git a/src/test/java/com/fishercoder/_656Test.java b/src/test/java/com/fishercoder/_656Test.java deleted file mode 100644 index 276a0d5480..0000000000 --- a/src/test/java/com/fishercoder/_656Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._656; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/25/17. - */ -public class _656Test { - private static _656.Solution1 solution1; - private static int[] A; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _656.Solution1(); - } - - @Test - public void test1() { - A = new int[]{1, 2, 4, -1, 2}; - expected = new ArrayList<>(Arrays.asList(1, 3, 5)); - assertEquals(expected, solution1.cheapestJump(A, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_659Test.java b/src/test/java/com/fishercoder/_659Test.java deleted file mode 100644 index b2214dd164..0000000000 --- a/src/test/java/com/fishercoder/_659Test.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._659; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _659Test { - private static _659.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _659.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3, 3, 4, 5}; - assertEquals(true, solution1.isPossible(nums)); - } - - @Test - public void test2() { - nums = new int[]{1, 2, 3, 3, 4, 4, 5, 5}; - assertEquals(true, solution1.isPossible(nums)); - } - - @Test - public void test3() { - nums = new int[]{1, 2, 3, 4, 4, 5}; - assertEquals(false, solution1.isPossible(nums)); - } - - @Test - public void test4() { - /**This is one cool test case from Leetcode...*/ - nums = new int[]{ - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, - 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, - 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, - 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, - 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, - 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, - 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, - 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, - 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, - 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, - 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, - 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, - 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, - 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, - 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, - 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, - 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, - 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, - 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, - 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, - 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, - 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, - 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, - 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, - 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, - 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, - 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, - 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, - 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, - 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, - 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, - 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, - 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, - 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, - 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, - 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, - 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, - 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, - 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, - 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, - 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, - 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, - 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, - 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, - 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, - 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, - 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, - 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, - 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, - 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, - 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, - 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, - 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, - 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, - 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, - 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, - 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, - 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, - 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, - 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, - 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, - 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, - 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, - 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, - 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, - 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, - 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, - 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, - 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, - 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, - 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, - 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, - 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, - 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, - 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; - assertEquals(true, solution1.isPossible(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_65Test.java b/src/test/java/com/fishercoder/_65Test.java deleted file mode 100644 index 5e35ba07e9..0000000000 --- a/src/test/java/com/fishercoder/_65Test.java +++ /dev/null @@ -1,162 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._65; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _65Test { - private static _65.Solution1 solution1; - private static _65.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _65.Solution1(); - solution2 = new _65.Solution2(); - } - - @Test - public void test1() { - assertEquals(false, solution1.isNumber("1 a")); - assertEquals(false, solution2.isNumber("1 a")); - } - - @Test - public void test2() { - assertEquals(false, solution1.isNumber("4e+")); - assertEquals(false, solution2.isNumber("4e+")); - } - - @Test - public void test3() { - assertEquals(true, solution1.isNumber("005047e+6")); - assertEquals(true, solution2.isNumber("005047e+6")); - } - - @Test - public void test4() { - assertEquals(false, solution1.isNumber(".e10")); - assertEquals(false, solution2.isNumber(".e10")); - } - - @Test - public void test5() { - assertEquals(true, solution1.isNumber("2e10")); - assertEquals(true, solution2.isNumber("2e10")); - } - - @Test - public void test6() { - assertEquals(false, solution1.isNumber("abc")); - assertEquals(false, solution2.isNumber("abc")); - } - - @Test - public void test7() { - assertEquals(false, solution1.isNumber(" -.")); - assertEquals(false, solution2.isNumber(" -.")); - } - - @Test - public void test8() { - assertEquals(true, solution1.isNumber("+.8")); - assertEquals(true, solution2.isNumber("+.8")); - } - - @Test - public void test9() { - assertEquals(false, solution1.isNumber(".")); - assertEquals(false, solution2.isNumber(".")); - } - - @Test - public void test10() { - assertEquals(false, solution1.isNumber(".e1")); - assertEquals(false, solution2.isNumber(".e1")); - } - - @Test - public void test11() { - assertEquals(true, solution1.isNumber("0")); - assertEquals(true, solution2.isNumber("0")); - } - - @Test - public void test12() { - assertEquals(false, solution1.isNumber("0e")); - assertEquals(false, solution2.isNumber("0e")); - } - - @Test - public void test13() { - assertEquals(false, solution1.isNumber("6ee69")); - assertEquals(false, solution2.isNumber("6ee69")); - } - - @Test - public void test14() { - assertEquals(false, solution1.isNumber("+++")); - assertEquals(false, solution2.isNumber("+++")); - } - - @Test - public void test15() { - assertEquals(false, solution1.isNumber("0e")); - assertEquals(false, solution2.isNumber("0e")); - } - - @Test - public void test16() { - assertEquals(false, solution1.isNumber("e9")); - assertEquals(false, solution2.isNumber("e9")); - } - - @Test - public void test17() { - assertEquals(true, solution1.isNumber(" 0.1 ")); - assertEquals(true, solution2.isNumber(" 0.1 ")); - } - - @Test - public void test18() { - assertEquals(true, solution1.isNumber("46.e3")); - assertEquals(true, solution2.isNumber("46.e3")); - } - - @Test - public void test19() { - assertEquals(false, solution1.isNumber("..")); - assertEquals(false, solution2.isNumber("..")); - } - - @Test - public void test20() { - assertEquals(false, solution1.isNumber(".e1")); - assertEquals(false, solution2.isNumber(".e1")); - } - - @Test - public void test21() { - assertEquals(false, solution1.isNumber("..")); - assertEquals(false, solution2.isNumber("..")); - } - - @Test - public void test22() { - assertEquals(false, solution1.isNumber("1e.")); - assertEquals(false, solution2.isNumber("1e.")); - } - - @Test - public void test24() { - assertEquals(true, solution1.isNumber("-1.")); - assertEquals(true, solution2.isNumber("-1.")); - } - - @Test - public void test25() { - assertEquals(false, solution1.isNumber("6e6.5")); - assertEquals(false, solution2.isNumber("6e6.5")); - } -} diff --git a/src/test/java/com/fishercoder/_661Test.java b/src/test/java/com/fishercoder/_661Test.java deleted file mode 100644 index 30240cf564..0000000000 --- a/src/test/java/com/fishercoder/_661Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._661; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _661Test { - private static _661.Solution1 solution1; - private static int[][] M; - private static int[][] expected; - - @BeforeClass - public static void setup() { - solution1 = new _661.Solution1(); - } - - @Test - public void test1() { - M = new int[][]{ - {1, 1, 1}, - {1, 0, 1}, - {1, 1, 1} - }; - expected = M = new int[][]{ - {0, 0, 0}, - {0, 0, 0}, - {0, 0, 0} - }; - assertArrayEquals(expected, solution1.imageSmoother(M)); - } - -} diff --git a/src/test/java/com/fishercoder/_662Test.java b/src/test/java/com/fishercoder/_662Test.java deleted file mode 100644 index 810ffef137..0000000000 --- a/src/test/java/com/fishercoder/_662Test.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._662; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.TestCase.assertEquals; - -public class _662Test { - private static _662.Solution1 solution1; - private static TreeNode root; - private static int expected; - - @BeforeClass - public static void setup() { - solution1 = new _662.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5, 3, null, 9)); - expected = 4; - assertEquals(expected, solution1.widthOfBinaryTree(root)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, null, 5, 3)); - expected = 2; - assertEquals(expected, solution1.widthOfBinaryTree(root)); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5)); - expected = 2; - assertEquals(expected, solution1.widthOfBinaryTree(root)); - } - - @Test - @Ignore - /**TODO: need to figure out how to pass in the input for the 4th example on Leetcode*/ - public void test4() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5, null, null, 9, 6, null, null, null, null, null, null, 7)); - expected = 8; - assertEquals(expected, solution1.widthOfBinaryTree(root)); - } - - @Test - public void test5() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1)); - expected = 1; - assertEquals(expected, solution1.widthOfBinaryTree(root)); - } -} diff --git a/src/test/java/com/fishercoder/_664Test.java b/src/test/java/com/fishercoder/_664Test.java deleted file mode 100644 index ece5a3a492..0000000000 --- a/src/test/java/com/fishercoder/_664Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._664; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _664Test { - private static _664.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _664.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.strangePrinter("aaabbb")); - } - - @Test - public void test2() { - assertEquals(2, solution1.strangePrinter("aba")); - } - -} diff --git a/src/test/java/com/fishercoder/_665Test.java b/src/test/java/com/fishercoder/_665Test.java deleted file mode 100644 index 4cfea0717f..0000000000 --- a/src/test/java/com/fishercoder/_665Test.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._665; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _665Test { - private static _665.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _665.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{4, 2, 3}; - assertEquals(true, solution1.checkPossibility(nums)); - } - - @Test - public void test2() { - nums = new int[]{4, 2, 1}; - assertEquals(false, solution1.checkPossibility(nums)); - } - - @Test - public void test3() { - nums = new int[]{3, 4, 2, 3}; - assertEquals(false, solution1.checkPossibility(nums)); - } - - @Test - public void test4() { - nums = new int[]{2, 3, 3, 2, 4}; - assertEquals(true, solution1.checkPossibility(nums)); - } - - @Test - public void test5() { - nums = new int[]{2, 3, 3, 2, 2, 4}; - assertEquals(false, solution1.checkPossibility(nums)); - } - - @Test - public void test6() { - nums = new int[]{2, 3, 3, 2, 2, 2, 4}; - assertEquals(false, solution1.checkPossibility(nums)); - } - - @Test - public void test7() { - nums = new int[]{3, 3, 2, 2}; - assertEquals(false, solution1.checkPossibility(nums)); - } - - @Test - public void test8() { - nums = new int[]{-1, 4, 2, 3}; - assertEquals(true, solution1.checkPossibility(nums)); - } - - @Test - public void test9() { - nums = new int[]{1, 2, 4, 5, 3}; - assertEquals(true, solution1.checkPossibility(nums)); - } - - @Test - public void test10() { - nums = new int[]{1, 2, 4, 5, 3, 6}; - assertEquals(true, solution1.checkPossibility(nums)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_666Test.java b/src/test/java/com/fishercoder/_666Test.java deleted file mode 100644 index 8120b264fc..0000000000 --- a/src/test/java/com/fishercoder/_666Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._666; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _666Test { - private static _666.Solution1 solution1; - private static _666.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _666.Solution1(); - solution2 = new _666.Solution2(); - } - - @Before - public void cleanUp() { - solution1.totalSum = 0; - solution2.totalSum = 0; - } - - @Test - public void test1() { - nums = new int[]{113, 215, 221}; - assertEquals(12, solution1.pathSum(nums)); - assertEquals(12, solution2.pathSum(nums)); - } - - @Test - public void test2() { - nums = new int[]{113, 221}; - assertEquals(4, solution1.pathSum(nums)); - assertEquals(4, solution2.pathSum(nums)); - } - - @Test - public void test3() { - nums = new int[]{113, 214, 221, 348, 487}; - assertEquals(26, solution1.pathSum(nums)); - assertEquals(26, solution2.pathSum(nums)); - } - -} diff --git a/src/test/java/com/fishercoder/_667Test.java b/src/test/java/com/fishercoder/_667Test.java deleted file mode 100644 index c77bf7a2d9..0000000000 --- a/src/test/java/com/fishercoder/_667Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._667; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _667Test { - private static _667.Solutoin1 solution1; - private static _667.Solutoin2 solution2; - private static int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _667.Solutoin1(); - solution2 = new _667.Solutoin2(); - } - - @Test - public void test1() { - expected = new int[] { 1, 2, 3 }; - assertArrayEquals(expected, solution1.constructArray(3, 1)); - assertArrayEquals(expected, solution2.constructArray(3, 1)); - } - - @Test - @Ignore//this problem requires you to return any one of the legit answer, so the results vary from time to time, so comment out this test - public void test2() { - expected = new int[] { 1, 3, 2 }; - assertArrayEquals(expected, solution1.constructArray(3, 2)); - assertArrayEquals(expected, solution2.constructArray(3, 2)); - } - - @Test - @Ignore//this problem requires you to return any one of the legit answer, so the results vary from time to time, so comment out this test - public void test3() { - expected = new int[] { 1, 5, 2, 4, 3, 6, 7, 8, 9, 10 }; - // assertArrayEquals(expected, solution1.constructArray(10, 4));//this is not working, so comment out - assertArrayEquals(expected, solution2.constructArray(10, 4)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_668Test.java b/src/test/java/com/fishercoder/_668Test.java deleted file mode 100644 index c35a8bf3a0..0000000000 --- a/src/test/java/com/fishercoder/_668Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._668; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _668Test { - private static _668.Solution1 solution1; - private static _668.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _668.Solution1(); - solution2 = new _668.Solution2(); - } - - @Test - public void test1() { - assertEquals(3, solution1.findKthNumber(3, 3, 5)); - assertEquals(3, solution2.findKthNumber(3, 3, 5)); - } - - @Test - public void test2() { - assertEquals(6, solution1.findKthNumber(2, 3, 6)); - assertEquals(6, solution2.findKthNumber(2, 3, 6)); - } - - @Test - public void test3() { -// assertEquals(31666344, solution1.findKthNumber(9895, 28405, 100787757));//this will run into OOM error, so comment out - assertEquals(31666344, solution2.findKthNumber(9895, 28405, 100787757)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_669Test.java b/src/test/java/com/fishercoder/_669Test.java deleted file mode 100644 index 592bc8754d..0000000000 --- a/src/test/java/com/fishercoder/_669Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._669; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _669Test { - private static _669.Solution1 solution1; - private static TreeNode root; - private static TreeNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _669.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 2)); - expected = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2)); - assertEquals(expected, solution1.trimBST(root, 1, 2)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, 0, 4, null, 2, null, null, 1)); - expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 2, null, 1)); - assertEquals(expected, solution1.trimBST(root, 1, 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_66Test.java b/src/test/java/com/fishercoder/_66Test.java deleted file mode 100644 index d8e45a2d57..0000000000 --- a/src/test/java/com/fishercoder/_66Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._66; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _66Test { - private static _66.Solution1 solution1; - private static int[] digits; - - @BeforeClass - public static void setup() { - solution1 = new _66.Solution1(); - } - - @Test - public void test1() { - digits = new int[]{9, 9, 9, 9}; - assertArrayEquals(new int[]{1, 0, 0, 0, 0}, solution1.plusOne(digits)); - } - - @Test - public void test2() { - digits = new int[]{8, 9, 9, 9}; - assertArrayEquals(new int[]{9, 0, 0, 0}, solution1.plusOne(digits)); - } - - @Test - public void test3() { - digits = new int[]{2, 4, 9, 3, 9}; - assertArrayEquals(new int[]{2, 4, 9, 4, 0}, solution1.plusOne(digits)); - } -} diff --git a/src/test/java/com/fishercoder/_670Test.java b/src/test/java/com/fishercoder/_670Test.java deleted file mode 100644 index a737395cf7..0000000000 --- a/src/test/java/com/fishercoder/_670Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._670; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _670Test { - private static _670.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _670.Solution1(); - } - - @Test - public void test1() { - assertEquals(7236, solution1.maximumSwap(2736)); - } - - @Test - public void test2() { - assertEquals(9973, solution1.maximumSwap(9973)); - } - - @Test - public void test3() { - assertEquals(73236, solution1.maximumSwap(23736)); - } - - @Test - public void test4() { - assertEquals(98213, solution1.maximumSwap(91283)); - } - -} diff --git a/src/test/java/com/fishercoder/_671Test.java b/src/test/java/com/fishercoder/_671Test.java deleted file mode 100644 index c37ef2ba9a..0000000000 --- a/src/test/java/com/fishercoder/_671Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._671; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _671Test { - private static _671.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _671.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 5, null, null, 5, 7)); - assertEquals(5, solution1.findSecondMinimumValue(root)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 2)); - assertEquals(-1, solution1.findSecondMinimumValue(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_673Test.java b/src/test/java/com/fishercoder/_673Test.java deleted file mode 100644 index 64a9cdb7f4..0000000000 --- a/src/test/java/com/fishercoder/_673Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._673; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _673Test { - private static _673.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _673.Solution1(); - } - - @Test - @Ignore - public void test1() { - nums = new int[]{1, 3, 5, 4, 7}; - assertEquals(2, solution1.findNumberOfLIS(nums)); - } - - @Test - public void test2() { - nums = new int[]{2, 2, 2, 2, 2}; - assertEquals(5, solution1.findNumberOfLIS(nums)); - } - -} diff --git a/src/test/java/com/fishercoder/_674Test.java b/src/test/java/com/fishercoder/_674Test.java deleted file mode 100644 index 634e3e61ab..0000000000 --- a/src/test/java/com/fishercoder/_674Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._674; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _674Test { - private static _674.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _674.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 3, 5, 4, 7}; - assertEquals(3, solution1.findLengthOfLCIS(nums)); - } - - @Test - public void test2() { - nums = new int[]{2, 2, 2, 2, 2}; - assertEquals(1, solution1.findLengthOfLCIS(nums)); - } - -} diff --git a/src/test/java/com/fishercoder/_675Test.java b/src/test/java/com/fishercoder/_675Test.java deleted file mode 100644 index 6b60d20657..0000000000 --- a/src/test/java/com/fishercoder/_675Test.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.ArrayUtils; -import com.fishercoder.solutions._675; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _675Test { - private static _675.Solution1 solution1; - private static List> forest; - - @BeforeClass - public static void setup() { - solution1 = new _675.Solution1(); - } - - @Test - public void test1() { - forest = new ArrayList<>(); - forest.add(Arrays.asList(1, 2, 3)); - forest.add(Arrays.asList(0, 0, 4)); - forest.add(Arrays.asList(7, 6, 5)); - assertEquals(6, solution1.cutOffTree(forest)); - } - - @Test - public void test2() { - forest = new ArrayList<>(); - forest.add(Arrays.asList(1, 2, 3)); - forest.add(Arrays.asList(0, 0, 0)); - forest.add(Arrays.asList(7, 6, 5)); - assertEquals(-1, solution1.cutOffTree(forest)); - } - - @Test - public void test3() { - forest = new ArrayList<>(); - forest.add(Arrays.asList(2, 3, 4)); - forest.add(Arrays.asList(0, 0, 5)); - forest.add(Arrays.asList(8, 7, 6)); - assertEquals(6, solution1.cutOffTree(forest)); - } - - @Test - public void test4() { - forest = ArrayUtils.buildList(new int[][]{ - {2, 3, 4, 9}, - {0, 0, 5, 10}, - {8, 7, 6, 12}, - }); - assertEquals(13, solution1.cutOffTree(forest)); - } - - @Test - public void test5() { - forest = ArrayUtils.buildList(new int[][]{ - {0, 0, 0, 3528, 2256, 9394, 3153}, - {8740, 1758, 6319, 3400, 4502, 7475, 6812}, - {0, 0, 3079, 6312, 0, 0, 0}, - {6828, 0, 0, 0, 0, 0, 8145}, - {6964, 4631, 0, 0, 0, 4811, 0}, - {0, 0, 0, 0, 9734, 4696, 4246}, - {3413, 8887, 0, 4766, 0, 0, 0}, - {7739, 0, 0, 2920, 0, 5321, 2250}, - {3032, 0, 3015, 0, 3269, 8582, 0}}); - assertEquals(-1, solution1.cutOffTree(forest)); - } - - @Test - public void test6() { - forest = ArrayUtils.buildList(new int[][]{ - {54581641, 64080174, 24346381, 69107959}, - {86374198, 61363882, 68783324, 79706116}, - {668150, 92178815, 89819108, 94701471}, - {83920491, 22724204, 46281641, 47531096}, - {89078499, 18904913, 25462145, 60813308} - }); - assertEquals(57, solution1.cutOffTree(forest)); - } - -} diff --git a/src/test/java/com/fishercoder/_676Test.java b/src/test/java/com/fishercoder/_676Test.java deleted file mode 100644 index cb6353ffdc..0000000000 --- a/src/test/java/com/fishercoder/_676Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._676; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _676Test { - private static _676.Solution1.MagicDictionary magicDictionarySol1; - - @BeforeClass - public static void setup() { - magicDictionarySol1 = new _676.Solution1.MagicDictionary(); - } - - @Before - public void cleanup() { - magicDictionarySol1 = new _676.Solution1.MagicDictionary(); - } - - @Test - public void test1() { - magicDictionarySol1.buildDict(new String[]{"hello", "leetcode"}); - assertEquals(false, magicDictionarySol1.search("hello")); - assertEquals(true, magicDictionarySol1.search("hhllo")); - assertEquals(false, magicDictionarySol1.search("hell")); - assertEquals(false, magicDictionarySol1.search("leetcoded")); - } -} diff --git a/src/test/java/com/fishercoder/_678Test.java b/src/test/java/com/fishercoder/_678Test.java deleted file mode 100644 index 4fb2e9c63b..0000000000 --- a/src/test/java/com/fishercoder/_678Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._678; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _678Test { - private static _678.Solution1 solution1; - private static _678.Solution2 solution2; - private static _678.Solution3 solution3; - - @BeforeClass - public static void setup() { - solution1 = new _678.Solution1(); - solution2 = new _678.Solution2(); - solution3 = new _678.Solution3(); - } - - @Test - public void test1() { - assertEquals(true, solution1.checkValidString("()")); - assertEquals(true, solution2.checkValidString("()")); - assertEquals(true, solution3.checkValidString("()")); - } - - @Test - public void test2() { - assertEquals(true, solution1.checkValidString("(*)")); - assertEquals(true, solution2.checkValidString("(*)")); - assertEquals(true, solution3.checkValidString("(*)")); - } - - @Test - public void test3() { - assertEquals(true, solution1.checkValidString("(*))")); - assertEquals(true, solution2.checkValidString("(*))")); - assertEquals(true, solution3.checkValidString("(*))")); - } - - @Test - public void test4() { - assertEquals(false, solution1.checkValidString("(((()))())))*))())()(**(((())(()(*()((((())))*())(())*(*(()(*)))()*())**((()(()))())(*(*))*))())")); - assertEquals(false, solution2.checkValidString("(((()))())))*))())()(**(((())(()(*()((((())))*())(())*(*(()(*)))()*())**((()(()))())(*(*))*))())")); - assertEquals(false, solution3.checkValidString("(((()))())))*))())()(**(((())(()(*()((((())))*())(())*(*(()(*)))()*())**((()(()))())(*(*))*))())")); - } - - @Test - public void test5() { - assertEquals(true, solution1.checkValidString("(((******)))")); - assertEquals(true, solution2.checkValidString("(((******)))")); - assertEquals(true, solution3.checkValidString("(((******)))")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_679Test.java b/src/test/java/com/fishercoder/_679Test.java deleted file mode 100644 index 8ff82a1fd8..0000000000 --- a/src/test/java/com/fishercoder/_679Test.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._679; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _679Test { - private static _679.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _679.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.judgePoint24(new int[]{4,1,8,7})); - } - - @Test - public void test2() { - assertEquals(false, solution1.judgePoint24(new int[]{1,2,1,2})); - } - - @Test - public void test3() { -// 8 / (1 - 2/3) = 24 - assertEquals(true, solution1.judgePoint24(new int[]{1,2,3,8})); - } - - @Test - public void test4() { - assertEquals(true, solution1.judgePoint24(new int[]{1,3,4,6})); - } - - @Test - public void test5() { - assertEquals(true, solution1.judgePoint24(new int[]{1,9,1,2})); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_67Test.java b/src/test/java/com/fishercoder/_67Test.java deleted file mode 100644 index e9c0203424..0000000000 --- a/src/test/java/com/fishercoder/_67Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._67; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _67Test { - private static _67.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _67.Solution1(); - } - - @Test - public void test1() { - assertEquals("100", solution1.addBinary("11", "1")); - } -} diff --git a/src/test/java/com/fishercoder/_681Test.java b/src/test/java/com/fishercoder/_681Test.java deleted file mode 100644 index 77e268f34f..0000000000 --- a/src/test/java/com/fishercoder/_681Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._681; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _681Test { - private static _681.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _681.Solution1(); - } - - @Test - public void test1() { - assertEquals("19:39", solution1.nextClosestTime("19:34")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_682Test.java b/src/test/java/com/fishercoder/_682Test.java deleted file mode 100644 index 913e30c6a1..0000000000 --- a/src/test/java/com/fishercoder/_682Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._682; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _682Test { - private static _682.Solution1 solution1; - private static String[] ops; - - @BeforeClass - public static void setup() { - solution1 = new _682.Solution1(); - } - - @Test - public void test1() { - ops = new String[]{"5", "2", "C", "D", "+"}; - assertEquals(30, solution1.calPoints(ops)); - } - - @Test - public void test2() { - ops = new String[]{"5", "-2", "4", "C", "D", "9", "+", "+"}; - assertEquals(27, solution1.calPoints(ops)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_683Test.java b/src/test/java/com/fishercoder/_683Test.java deleted file mode 100644 index d8e449e946..0000000000 --- a/src/test/java/com/fishercoder/_683Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._683; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _683Test { - private static _683.Solution1 solution1; - private static int[] flowers; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _683.Solution1(); - } - - @Test - public void test1() { - flowers = new int[]{1, 3, 2}; - k = 1; - assertEquals(2, solution1.kEmptySlots(flowers, k)); - } - - @Test - public void test2() { - flowers = new int[]{1, 2, 3}; - k = 1; - assertEquals(-1, solution1.kEmptySlots(flowers, k)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_684Test.java b/src/test/java/com/fishercoder/_684Test.java deleted file mode 100644 index dfa4d99e29..0000000000 --- a/src/test/java/com/fishercoder/_684Test.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._684; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _684Test { - private static _684.Solution1 solution1; - private static int[][] edges; - private static int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _684.Solution1(); - } - - @Test - public void test1() { - edges = new int[][]{ - {1, 2}, - {1, 3}, - {2, 3} - }; - expected = new int[]{2, 3}; - assertArrayEquals(expected, solution1.findRedundantConnection(edges)); - } - - @Test - public void test2() { - edges = new int[][]{ - {1, 2}, - {2, 3}, - {3, 4}, - {1, 4}, - {1, 5} - }; - expected = new int[]{1, 4}; - assertArrayEquals(expected, solution1.findRedundantConnection(edges)); - } - - @Test - public void test3() { - edges = new int[][]{ - {9, 10}, - {5, 8}, - {2, 6}, - {1, 5}, - {3, 8}, - {4, 9}, - {8, 10}, - {4, 10}, - {6, 8}, - {7, 9} - }; - expected = new int[]{4, 10}; - assertArrayEquals(expected, solution1.findRedundantConnection(edges)); - } - - @Test - public void test4() { - edges = new int[][]{ - {1, 2}, - {2, 3}, - {1, 5}, - {3, 4}, - {1, 4} - }; - expected = new int[]{1, 4}; - assertArrayEquals(expected, solution1.findRedundantConnection(edges)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_685Test.java b/src/test/java/com/fishercoder/_685Test.java deleted file mode 100644 index 5c5294c623..0000000000 --- a/src/test/java/com/fishercoder/_685Test.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._685; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _685Test { - private static _685.Solution1 solution1; - private static _685.Solution2 solution2; - private static int[][] edges; - private static int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _685.Solution1(); - solution2 = new _685.Solution2(); - } - - @Test - public void test1() { - edges = new int[][]{ - {2, 1}, - {3, 1}, - {4, 2}, - {1, 4} - }; - expected = new int[]{2, 1}; - assertArrayEquals(expected, solution1.findRedundantDirectedConnection(edges)); - assertArrayEquals(expected, solution2.findRedundantDirectedConnection(edges)); - } - - @Test - public void test2() { - edges = new int[][]{ - {2, 1}, - {1, 4}, - {4, 3}, - {3, 2} - }; - expected = new int[]{3, 2}; - assertArrayEquals(expected, solution1.findRedundantDirectedConnection(edges)); - assertArrayEquals(expected, solution2.findRedundantDirectedConnection(edges)); - } - - @Test - public void test3() { - edges = new int[][]{ - {1, 2}, - {1, 3}, - {2, 3}, - }; - expected = new int[]{2, 3}; -// assertArrayEquals(expected, solution1.findRedundantDirectedConnection(edges)); - assertArrayEquals(expected, solution2.findRedundantDirectedConnection(edges)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_687Test.java b/src/test/java/com/fishercoder/_687Test.java deleted file mode 100644 index 4334656103..0000000000 --- a/src/test/java/com/fishercoder/_687Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._687; -import org.junit.Before; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _687Test { - private static _687.Solution1 solution1; - private static TreeNode root; - - @Before - public void setup() { - solution1 = new _687.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 5, 1, 1, null, 5)); - assertEquals(2, solution1.longestUnivaluePath(root)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 4, 5, 4, 4, null, 5)); - TreeUtils.printBinaryTree(root); - assertEquals(2, solution1.longestUnivaluePath(root)); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 5, 1, 1, null, 5, null, 1, 1)); - TreeUtils.printBinaryTree(root); - assertEquals(4, solution1.longestUnivaluePath(root)); - } - - @Test - - public void test4() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 5, 8, 9, 7, 7, 8, 1, 4, 8, 1, 9, 0, 8, 7, 1, 7, 4, 2, 9, 8, 2, 4, null, null, 9, null, null, null, 6, 0, 9, 4, 1, 0, 1, 8, 9, 0, 1, 8, 9, 1, 0, 9, 6, 2, 5, null, 2, 3, 0, 2, 4, 8, 8, 8, 5, 0, 0, 9, 4, 9, 1, null, 0, 7, 2, 2, 3, null, 6, 1, 0, 8, 9, 9, 9, 4, 8, 4, 3, 4, 4, 0, null, null, 8, 3, 8, null, null, 0, null, 0, 4, 9, 1, 2, null, 4, 4, 0, 4, 3, 5, 5, 7, 4, 1, 6, null, 1, 0, null, null, null, 2, 8, 7, 7, null, null, 0, 2, 5, 5, 9, 3, 3, null, 7, 6, 6, 7, 9, 8, 1, 7, 7, 7, 2, 6, null, 7, null, 4, 6, 4, 6, null, null, 9, 1, null, null, null, 5, 5, 5, 4, 2, 2, 8, 5, 1, 1, 3, 1, 3, 7, null, 2, null, 9, 1, 4, 4, 7, 7, null, 1, 5, 6, 2, 7, 3, null, 9, 1, null, 2, 4, 4, 8, null, null, 7, null, 6, null, 7, 4, 3, 5, 8, 4, 8, 5, null, null, 8, null, null, null, 4, 4, null, null, null, null, 8, 3, 5, 5, null, null, null, 1, 2, 0, null, null, 9, 3, null, 8, 3, 7, 1, 8, 9, 0, 1, 8, 2, null, 4, null, null, 8, null, null, null, null, 2, null, 4, 8, 5, 5, 3, 1, null, null, 6, null, 1, null, null, 6, null, null, null, null, 7, 3, null, null, null, 8, 6, 4, null, 6, 9, 0, 7, 8, null, null, 0, 6, 7, null, null, 0, 0, 7, 2, 3, 2, null, 0, 2, 3, null, 0, 1, 7, 9, 0, 7, null, null, null, null, 5, 8, 2, 6, 3, 2, 0, 4, null, null, 0, 9, 1, 1, 1, null, 1, 3, null, 7, 9, 1, 3, 3, 8, null, null, null, null, 6, null, null, null, null, 9, 8, 1, 3, 8, 3, 0, 6, null, null, 8, 5, 6, 5, 2, 1, null, 5, null, 7, 0, 0, null, 9, 3, 9, null, 3, 0, 0, 9, 1, 7, 0, 2, null, 6, 8, 5, null, null, null, null, null, 7, null, 2, 5, null, null, 9, null, null, null, null, null, null, null, null, null, null, null, 4, 1, null, 3, 6, 6, 2, 5, 5, 9, null, null, 7, 8, null, null, 2, 7, 3, 7, 2, 5, null, 1, 3, 4, null, null, 8, 3, 6, 9, null, 1, null, null, null, null, 9, 7, 5, 2, null, 5, null, 6, 4, 5, null, 1, 2, 0, 6, null, 1, 6, null, null, 5, null, 7, 8, 4, 7, 8, 6, 4, null, 5, 6, 7, 9, 1, 0, 4, null, null, null, 6, 4, 8, 4, 5, null, 0, 4, 4, 0, 1, 7, 1, null, 1, null, 3, 6, null, null, null, null, 8, null, 5, 0, 7, 5, null, null, 5, 8, null, null, 3, null, null, 8, null, 2, 4, null, null, null, null, null, null, null, 9, null, 9, null, 9, null, null, null, null, 7, 1, null, null, 2, null, null, 5, 5, 5, 5, 6, 4, null, null, 1, 6, 4, 0, null, 0, 6, 3, 0, null, 5, 5, null, null, null, null, 2, null, 3, 6, null, 3, 0, 5, 0, 1, 0, 3, 4, 9, 9, 2, 7, 3, 8, 6, 9, null, 5, 8, null, null, null, null, 9, 8, 0, 7, null, null, 8, 8, 6, 6, 0, 2, 7, 4, 2, 3, 8, 6, 4, null, 8, null, null, null, 2, 0, null, 1, 3, 5, 4, 2, 2, 5, 8, 8, null, 3, 0, null, 1, 6, 0, null, null, 9, null, 2, null, 6, 8, 2, null, null, 5, null, null, null, 9, 6, 6, 4, 2, 0, null, null, 1, null, 0, null, null, null, 6, 6, null, null, null, 4, 7, 9, null, 0, 1, null, null, 9, null, null, null, 4, null, 8, null, null, null, null, null, null, 4, null, 6, null, 3, null, null, 5, 1, 2, 5, null, 0, 7, 8, null, 7, null, null, 4, null, 4, 4, null, 2, null, 6, null, null, null, 7, null, null, null, null, 6, 4, null, 6, null, 6, 9, null, null, null, 9, 6, null, 9, null, 3, null, 2, null, 7, 7, null, null, 0, null, 6, 3, null, null, null, null, null, null, 1, null, null, null, 6, 9, 7, null, 7, null, 9, 3, 3, null, null, null, null, 4, null, null, 3, null, null, null, 3, 9, null, 0, 3, 1, 9, 6, 7, 9, 4, 8, null, null, 6, null, 1, 3, 7, null, null, null, 3, null, 2, null, 8, 1, 1, null, null, 6, null, 7, 3, 5, null, 6, 3, 4, null, null, 5, 7, 1, null, null, 6, 4, 6, null, null, null, null, 5, 7, 0, 7, 0, null, 5, 8, 5, 5, 4, 5, null, null, null, null, null, null, 1, 7, null, null, 7, null, 9, 9, 6, 4, null, null, 3, 2, 1, null, 0, null, 0, 6, null, null, null, 1, 5, null, null, null, 8, null, null, null, null, 3, 4, 8, null, null, 9, 6, 4, null, null, null, null, 8, 9, null, 1, null, null, null, 7, null, null, null, null, null, 9, null, null, null, 4, 1, 6, 7, 0, null, null, null, 7, null, null, 8, null, null, null, null, null, null, null, 4, null, 9, null, null, null, null, 3, 0, 6, null, 5, null, 9, 9, null, null, 4, 3, 4, null, null, null, null, 8, null, 5, null, null, null, null, 5, 2, null, null, null, null, null, null, null, 2, null, null, 2, 1, 8, 5, null, 0, null, 0, 3, 2, 4, 5, null, null, null, null, null, 7, null, null, 0, null, 0, null, null, null, 0, 3, 9, null, null, null, null, 5, null, null, 0, 5, 0, 0, null, 9, null, null, null, null, null, null, null, null, 8, null, 9, 3, 5, 9, 0, 5, 9, null, null, 9, 4, null, 0, 2, 0, null, null, 7, null, 7, null, 5, 7, 8, 7, null, null, null, 3, 0, 3, null, null, null, null, null, 4, 5, null, null, 2, 3, null, 2, null, null, 7, null, null, 9, null, null, 9, 7, 1, null, null, 1, 6, 1, 8, null, null, 5, null, null, 3, 7, 9, 6, null, null, null, null, 1, null, null, null, 3, 7, 3, 2, 3, 3, null, 1, null, null, null, 1, null, null, 4, 3, 4, 8, 7, null, 0, 3, 0, null, 1, 1, null, null, null, null, null, 5, null, 6, 0, null, 3, 1, null, 6, null, null, 4, 0, 1, null, 6, 1, null, null, 9, 6, 4, 9, 0, 8, 9, 3, 3, 6, null, null, null, null, null, null, null, null, null, null, null, null, 2, null, null, null, null, null, 8, 5, 8, 3, 5, 4, null, 6, null, 0, null, null, 6, null, 4, 3, null, null, null, null, null, null, null, null, null, null, null, null, null, null, 7, 3, null, null, 1, null, 2, 4, null, null, null, 6, null, null, null, 6, null, 5, null, null, null, null, 1, null, null, 3, null, 1, null, 7, 1, null, null, 7, 1, 3, 4, 8, null, null, null, null, null, 4, null, null, 4, null, null, null, 7, null, 6, null, null, 1, null, null, null, 7, 3, 3, null, null, null, null, 3, 0, null, null, 4, null, null, null, null, null, null, null, null, null, null, 8, null, null, 9, null, null, 6, 6, 5, 2, null, 8, 3, 8, null, null, null, null, 6, 7, 0, null, null, null, null, 1, 1, 5, null, 0, 5, null, 5, null, null, null, 1, 2, null, 2, 9, 1, null, 2, 4, 1, null, null, null, 1, 8, 4, 4, 5, 2, null, null, 6, 4, 7, 5, 2, 9, null, 4, null, null, null, null, null, 3, null, null, 5, 9, null, null, null, null, 9, null, 9, null, null, null, 2, null, 1, 9, null, null, null, null, null, 1, 9, 3, null, null, 1, 9, null, 5, 2, 1, 0, null, null, 1, 9, 8, 4, 7, null, null, 5, 7, null, null, null, null, 1, 2, 8, null, 6, 0, null, null, null, null, 0, null, null, null, 6, null, 2, 3, 0, 9, null, null, 1, 4, 6, null, 8, null, null, 5, null, 3, 0, null, 6, null, null, null, null, null, 2, null, null, null, null, null, null, 2, 5, 8, 6, 9, null, null, null, 8, null, null, 9, 6, null, null, null, null, 3, null, null, null, null, 9, null, null, 2, null, null, null, null, null, null, 8, 8, null, null, null, null, null, 9, null, 6, null, 2, 5, null, null, 1, 2, null, 4, null, null, 4, null, null, 3, 5, null, 3, 3, null, null, 1, null, null, null, null, 4, null, 2, 3, null, 4, 5, 3, null, 7, null, null, null, 7, 6, null, null, 1, 3, null, 4, 9, 8, null, null, 0, null, 3, 4, null, 8, null, 1, null, null, 2, 2, null, null, 4, null, null, null, 3, null, null, 2, null, null, null, 4, null, 5, null, null, null, null, 2, null, 5, null, null, null, null, null, null, 2, 7, 5, null, 6, null, null, null, null, 2, null, 0, null, 3, null, 1, null, 9, 4, null, 3, null, null, null, null, null, null, null, 5, 5, 7, null, null, 1, null, 4, 6, null, null, null, 2, null, 5, 9, 0, 6, 2, null, null, null, null, null, null, null, null, null, null, null, null, 5, null, 7, null, 2, 9, null, null, 1, null, null, null, 1, 6, null, 6, null, null, 0, 8, null, 4, null, null, null, null, 4, null, null, 0, null, 6, 0, null, null, null, 4, null, null, null, null, null, 0, null, null, null, null, null, null, null, null, null, null, null, null, 0, 5, 4, 2, 6, 4, 5, 3, 4, null, null, 5, null, null, null, null, 4, null, null, 3, 6, 2, 0, null, 6, 6, null, null, null, null, 0, 6, null, null, null, 3, 9, 4, null, null, null, null, null, 0, null, null, 6, 7, 0, null, 9, 2, null, 3, 3, null, null, 8, null, 3, null, null, null, 8, 5, 3, null, 2, 4, null, 9, 6, 9, null, null, null, null, 6, null, 6, null, 5, 3, null, null, null, null, 4, null, null, null, 9, 0, 9, 7, 1, 1, null, 1, null, 1, 6, null, 5, null, 6, null, null, 1, null, null, null, null, null, null, 5, null, null, null, null, null, 3, null, 6, 1, null, 0, 2, null, null, 0, null, null, 0, null, null, null, null, null, 3, null, null, 8, null, null, 5, 3, 3, null, null, null, null, null, null, null, 3, null, null, 0, 8, 7, null, null, 8, 1, null, null, null, null, null, null, 7, null, null, null, null, null, null, null, null, null, null, null, 5, 2, null, 2, 6, null, null, null, null, null, null, null, 1, 5, 0, null, null, 2, null, 7, null, null, 6, null, null, null, null, null, null, null, null, null, null, null, null, null, 8, null, null, null, null, 3, null, null, 4, null, null, 2, null, null, null, null, 0, 3, null, null, null, null, null, 7, null, 8, null, null, null, null, 8, 5, null, 3, 4, null, null, null, 8, null, null, null, null, null, null, null, null, null, 3, 7, null, null, null, 4, 0, 3, null, null, 6, null, null, null, null, null, null, null, null, null, null, null, null, 8, null, null, null, null, null, 2, null, null, null, null, null, null, null, null, null, 0, null, null, null, 2, null, null, null, 8, 2, null, null, null, null, null, null, null, 8, null, null, null, null, null, null, null, null, null, null, 2, null, null, null, 2, 5, null, null, null, null, null, null, null, null, null, null, null, 2, null, null, null, null, null, 8, null, null, null, null, null, null, null, null, null, null, 0, 5)); - assertEquals(2, solution1.longestUnivaluePath(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_688Test.java b/src/test/java/com/fishercoder/_688Test.java deleted file mode 100644 index decaafc0b7..0000000000 --- a/src/test/java/com/fishercoder/_688Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._688; -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class _688Test { - private static _688.Solution1 solution1; - private static _688.Solution2 solution2; - - @Before - public void setupForEachTest() { - solution1 = new _688.Solution1(); - solution2 = new _688.Solution2(); - } - - @Test - public void test1() { - assertEquals(0.0625, solution1.knightProbability(3, 2, 0, 0)); - assertEquals(0.0625, solution2.knightProbability(3, 2, 0, 0)); - } - - @Test - public void test2() { - assertTrue(Math.abs(0.00019 - solution2.knightProbability(8, 30, 6, 4)) < 10e-7); - } -} diff --git a/src/test/java/com/fishercoder/_689Test.java b/src/test/java/com/fishercoder/_689Test.java deleted file mode 100644 index b6eb275d19..0000000000 --- a/src/test/java/com/fishercoder/_689Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._689; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _689Test { - private static _689.Solution1 solution1; - private static int[] nums; - private static int[] expected; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _689.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 1, 2, 6, 7, 5, 1}; - expected = new int[]{0, 3, 5}; - k = 2; - assertArrayEquals(expected, solution1.maxSumOfThreeSubarrays(nums, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_68Test.java b/src/test/java/com/fishercoder/_68Test.java deleted file mode 100644 index 93d522dc3f..0000000000 --- a/src/test/java/com/fishercoder/_68Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._68; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _68Test { - private static _68.Solution1 solution1; - private static String[] words; - - @BeforeClass - public static void setup() { - solution1 = new _68.Solution1(); - } - - @Test - public void test1() { - words = - new String[] {"This", "is", "a", "good", "test!", "\n", "What", "do", "you", "\n", "think?", - "\n", "I", "think", "so", "too!"}; - assertEquals(Arrays.asList( - "This is a good", - "test! \n What do", - "you \n think? \n I", - "think so too! "), solution1.fullJustify(words, 16)); - } - - @Test - public void test2() { - words = new String[] {"This", "is", "an", "example", "of", "text", "justification."}; - assertEquals(Arrays.asList( - "This is an", - "example of text", - "justification. "), solution1.fullJustify(words, 16)); - } -} diff --git a/src/test/java/com/fishercoder/_690Test.java b/src/test/java/com/fishercoder/_690Test.java deleted file mode 100644 index 7df5c71c6f..0000000000 --- a/src/test/java/com/fishercoder/_690Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.Employee; -import com.fishercoder.solutions._690; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static junit.framework.TestCase.assertEquals; - -/** - * Created by fishercoder on 5/18/17. - */ -public class _690Test { - private static _690.Solution1 solution1; - private static List employees; - private static int id; - - @Before - public void setupForEachTest() { - solution1 = new _690.Solution1(); - } - - @Test - public void test1() { - employees = new ArrayList(Arrays.asList( - new Employee(1, 5, Arrays.asList(2,3)), - new Employee(2, 3, Arrays.asList()), - new Employee(3, 3, Arrays.asList()))); - id = 1; - assertEquals(11, solution1.getImportance(employees, id)); - } - - @Test - public void test2() { - employees = new ArrayList(Arrays.asList( - new Employee(1, 5, Arrays.asList(2,3)), - new Employee(2, 3, Arrays.asList(4)), - new Employee(3, 4, Arrays.asList()), - new Employee(4, 1, Arrays.asList()))); - id = 1; - assertEquals(13, solution1.getImportance(employees, id)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_692Test.java b/src/test/java/com/fishercoder/_692Test.java deleted file mode 100644 index 9d4a2f0f1f..0000000000 --- a/src/test/java/com/fishercoder/_692Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._692; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _692Test { - private static _692.Solution1 solution1; - private static String[] words; - private static List expected; - private static List actual; - - @BeforeClass - public static void setup() { - solution1 = new _692.Solution1(); - } - - @Test - public void test1() { - words = new String[]{"i", "love", "leetcode", "i", "love", "coding"}; - actual = solution1.topKFrequent(words, 2); - expected = new ArrayList<>(Arrays.asList("i", "love")); - assertEquals(expected, actual); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_694Test.java b/src/test/java/com/fishercoder/_694Test.java deleted file mode 100644 index ead965951b..0000000000 --- a/src/test/java/com/fishercoder/_694Test.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._694; -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _694Test { - private static _694.Solution1 solution1; - private static _694.Solution2 solution2; - private static int[][] grid; - - @Before - public void setup() { - solution1 = new _694.Solution1(); - solution2 = new _694.Solution2(); - } - - @Test - public void test1() { - grid = new int[][]{ - {1, 1, 0, 1, 1}, - {1, 0, 0, 0, 0}, - {0, 0, 0, 0, 1}, - {1, 1, 0, 1, 1} - }; -// assertEquals(3, solution1.numDistinctIslands(grid)); - assertEquals(3, solution2.numDistinctIslands(grid)); - } - - @Test - public void test2() { - grid = new int[][]{ - {1, 1, 0, 0, 0}, - {1, 1, 0, 0, 0}, - {0, 0, 0, 1, 1}, - {0, 0, 0, 1, 1} - }; -// assertEquals(1, solution1.numDistinctIslands(grid)); - assertEquals(1, solution2.numDistinctIslands(grid)); - } -} diff --git a/src/test/java/com/fishercoder/_695Test.java b/src/test/java/com/fishercoder/_695Test.java deleted file mode 100644 index 1c2376d79c..0000000000 --- a/src/test/java/com/fishercoder/_695Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._695; -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _695Test { - private static _695.Solution1 solution1; - private static int[][] grid; - - @Before - public void setup() { - solution1 = new _695.Solution1(); - } - - @Test - public void test1() { - grid = new int[][]{ - {0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, - {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0}, - {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0} - }; - assertEquals(6, solution1.maxAreaOfIsland(grid)); - } - -} diff --git a/src/test/java/com/fishercoder/_697Test.java b/src/test/java/com/fishercoder/_697Test.java deleted file mode 100644 index 07a0cb7e4f..0000000000 --- a/src/test/java/com/fishercoder/_697Test.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._697; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _697Test { - private static _697.Solution1 solution1; - private static _697.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _697.Solution1(); - solution2 = new _697.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{1}; - assertEquals(1, solution1.findShortestSubArray(nums)); - assertEquals(1, solution2.findShortestSubArray(nums)); - } - - @Test - public void test2() { - nums = new int[]{1, 2, 2, 3, 1}; - assertEquals(2, solution1.findShortestSubArray(nums)); - assertEquals(2, solution2.findShortestSubArray(nums)); - } - - @Test - public void test3() { - nums = new int[]{1, 2, 2, 3, 1, 1}; - assertEquals(6, solution1.findShortestSubArray(nums)); - assertEquals(6, solution2.findShortestSubArray(nums)); - } - - @Test - public void test4() { - nums = new int[]{1, 2, 2, 3, 1, 1, 5}; - assertEquals(6, solution1.findShortestSubArray(nums)); - assertEquals(6, solution2.findShortestSubArray(nums)); - } - - @Test - public void test5() { - nums = new int[]{1, 2, 2, 3, 1, 4, 2}; - assertEquals(6, solution1.findShortestSubArray(nums)); - assertEquals(6, solution2.findShortestSubArray(nums)); - } - -} diff --git a/src/test/java/com/fishercoder/_698Test.java b/src/test/java/com/fishercoder/_698Test.java deleted file mode 100644 index 108992b5e3..0000000000 --- a/src/test/java/com/fishercoder/_698Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._698; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _698Test { - private static _698.Solution1 solution1; - private static int[] nums; - private static int k; - - @BeforeClass - public static void setup() { - solution1 = new _698.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{4, 3, 2, 3, 5, 2, 1}; - k = 4; - assertEquals(true, solution1.canPartitionKSubsets(nums, k)); - } - - @Test - public void test2() { - nums = new int[]{-1,1,0,0}; - k = 4; - assertEquals(false, solution1.canPartitionKSubsets(nums, k)); - } -} diff --git a/src/test/java/com/fishercoder/_699Test.java b/src/test/java/com/fishercoder/_699Test.java deleted file mode 100644 index b94e27c263..0000000000 --- a/src/test/java/com/fishercoder/_699Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._699; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.TestCase.assertEquals; - -public class _699Test { - private static _699.Solution1 solution1; - private static int[][] positions; - - @BeforeClass - public static void setup() { - solution1 = new _699.Solution1(); - } - - @Test - public void test1() { - positions = new int[][]{ - {1, 2}, - {2, 3}, - {6, 1} - }; - assertEquals(Arrays.asList(2, 5, 5), solution1.fallingSquares(positions)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_69Test.java b/src/test/java/com/fishercoder/_69Test.java deleted file mode 100644 index 43182539e5..0000000000 --- a/src/test/java/com/fishercoder/_69Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._69; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _69Test { - private static _69.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _69.Solution1(); - } - - @Test - public void test1() { - assertEquals(4, solution1.mySqrt(16)); - } - - @Test - public void test2() { - assertEquals(2, solution1.mySqrt(8)); - } -} diff --git a/src/test/java/com/fishercoder/_6Test.java b/src/test/java/com/fishercoder/_6Test.java deleted file mode 100644 index 62e6a8f1f3..0000000000 --- a/src/test/java/com/fishercoder/_6Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._6; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _6Test { - private static _6.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _6.Solution1(); - } - - @Test - public void test1() { - assertEquals("PAHNAPLSIIGYIR", solution1.convert("PAYPALISHIRING", 3)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_700Test.java b/src/test/java/com/fishercoder/_700Test.java deleted file mode 100644 index f78d63f61b..0000000000 --- a/src/test/java/com/fishercoder/_700Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._700; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; - -public class _700Test { - private static _700.Solution1 solution1; - private static TreeNode root; - private static TreeNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _700.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 7, 1, 3)); - expected = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3)); - assertEquals(expected, solution1.searchBST(root, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_703Test.java b/src/test/java/com/fishercoder/_703Test.java deleted file mode 100644 index a530f81535..0000000000 --- a/src/test/java/com/fishercoder/_703Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._703; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _703Test { - private static _703.Solution1.KthLargest solution1; - private static int[] A; - - @Test - public void test1() { - solution1 = new _703.Solution1.KthLargest(3, new int[] {4, 5, 8, 2}); - assertEquals(4, solution1.add(3)); - assertEquals(5, solution1.add(5)); - assertEquals(5, solution1.add(10)); - assertEquals(8, solution1.add(9)); - assertEquals(8, solution1.add(4)); - } -} diff --git a/src/test/java/com/fishercoder/_704Test.java b/src/test/java/com/fishercoder/_704Test.java deleted file mode 100644 index 6cb489a62f..0000000000 --- a/src/test/java/com/fishercoder/_704Test.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._704; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _704Test { - private static _704.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _704.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {-1, 0, 3, 5, 9, 12}; - assertEquals(4, solution1.search(nums, 9)); - } - - @Test - public void test2() { - nums = new int[] {-1, 0, 3, 5, 9, 12}; - assertEquals(-1, solution1.search(nums, 2)); - } - - @Test - public void test3() { - nums = new int[] {5}; - assertEquals(0, solution1.search(nums, 5)); - } - - @Test - public void test4() { - nums = new int[] {-1, 0}; - assertEquals(1, solution1.search(nums, 0)); - } - - @Test - public void test5() { - nums = new int[] {-1, 0, 3, 5, 9, 12}; - assertEquals(1, solution1.search(nums, 0)); - } -} diff --git a/src/test/java/com/fishercoder/_709Test.java b/src/test/java/com/fishercoder/_709Test.java deleted file mode 100644 index 858fbe72f9..0000000000 --- a/src/test/java/com/fishercoder/_709Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._709; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _709Test { - private static _709.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _709.Solution1(); - } - - @Test - public void test1() { - assertEquals("hello", solution1.toLowerCase("Hello")); - } -} diff --git a/src/test/java/com/fishercoder/_70Test.java b/src/test/java/com/fishercoder/_70Test.java deleted file mode 100644 index 535ef75137..0000000000 --- a/src/test/java/com/fishercoder/_70Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._70; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _70Test { - private static _70.Solution1 solution1; - private static _70.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _70.Solution1(); - solution2 = new _70.Solution2(); - } - - @Test - public void test1() { - assertEquals(3, solution1.climbStairs(3)); - } - - @Test - public void test2() { - assertEquals(3, solution2.climbStairs(3)); - } - - @Test - public void test3() { - assertEquals(13, solution1.climbStairs(6)); - } - - @Test - public void test4() { - assertEquals(13, solution2.climbStairs(6)); - } -} diff --git a/src/test/java/com/fishercoder/_712Test.java b/src/test/java/com/fishercoder/_712Test.java deleted file mode 100644 index 45a85e5425..0000000000 --- a/src/test/java/com/fishercoder/_712Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._712; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _712Test { - private static _712.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _712.Solution1(); - } - - @Test - public void test1() { - assertEquals(231, solution1.minimumDeleteSum("sea", "eat")); - } - - @Test - public void test2() { - assertEquals(403, solution1.minimumDeleteSum("delete", "leet")); - } - -} diff --git a/src/test/java/com/fishercoder/_713Test.java b/src/test/java/com/fishercoder/_713Test.java deleted file mode 100644 index 1b3c338492..0000000000 --- a/src/test/java/com/fishercoder/_713Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._713; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _713Test { - private static _713.Solution1 solution1; - private static _713.Solution2 solution2; - private static int[] nums; - private static int k; - - @BeforeClass - public static void setup() { - solution2 = new _713.Solution2(); - solution1 = new _713.Solution1(); - } - - @Test - public void test1() { - nums = new int[]{1, 2, 3}; - k = 0; - assertEquals(0, solution2.numSubarrayProductLessThanK(nums, k)); - assertEquals(0, solution1.numSubarrayProductLessThanK(nums, k)); - } - -} diff --git a/src/test/java/com/fishercoder/_714Test.java b/src/test/java/com/fishercoder/_714Test.java deleted file mode 100644 index 188dd5455e..0000000000 --- a/src/test/java/com/fishercoder/_714Test.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._714; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _714Test { - private static _714.Solution1 solution1; - private static _714.Solution2 solution2; - private static int[] prices; - private static int fee; - - @BeforeClass - public static void setup() { - solution1 = new _714.Solution1(); - solution2 = new _714.Solution2(); - } - - @Test - public void test1() { - prices = new int[]{1, 3, 2, 8, 4, 9}; - fee = 2; - assertEquals(8, solution1.maxProfit(prices, fee)); - assertEquals(8, solution2.maxProfit(prices, fee)); - } - - @Test - public void test2() { - prices = new int[]{1, 3, 7, 5, 10, 3}; - fee = 3; - assertEquals(6, solution1.maxProfit(prices, fee)); - assertEquals(6, solution2.maxProfit(prices, fee)); - } - - @Test - public void test3() { - prices = new int[]{1, 4, 6, 2, 8, 3, 10, 14}; - fee = 3; - assertEquals(13, solution1.maxProfit(prices, fee)); - assertEquals(13, solution2.maxProfit(prices, fee)); - } - -} diff --git a/src/test/java/com/fishercoder/_716Test.java b/src/test/java/com/fishercoder/_716Test.java deleted file mode 100644 index 275caf9a74..0000000000 --- a/src/test/java/com/fishercoder/_716Test.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._716; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _716Test { - private static _716.Solution1.MaxStack maxStackSolution1; - private static _716.Solution2.MaxStack maxStackSolution2; - - @Before - public void setup() { - maxStackSolution1 = new _716.Solution1.MaxStack(); - maxStackSolution2 = new _716.Solution2.MaxStack(); - } - - @Test - public void test1() { - maxStackSolution1.push(5); - assertEquals(5, maxStackSolution1.peekMax()); - assertEquals(5, maxStackSolution1.popMax()); - } - - @Test - public void test2() { - maxStackSolution1.push(5); - maxStackSolution1.push(1); - assertEquals(5, maxStackSolution1.popMax()); - assertEquals(1, maxStackSolution1.peekMax()); - } - - @Test - public void test3() { - maxStackSolution1.push(74); - assertEquals(74, maxStackSolution1.popMax()); - maxStackSolution1.push(89); - maxStackSolution1.push(67); - assertEquals(89, maxStackSolution1.popMax()); - assertEquals(67, maxStackSolution1.pop()); - maxStackSolution1.push(61); - maxStackSolution1.push(-77); - assertEquals(61, maxStackSolution1.peekMax()); - assertEquals(61, maxStackSolution1.popMax()); - maxStackSolution1.push(81); - assertEquals(81, maxStackSolution1.peekMax()); - assertEquals(81, maxStackSolution1.popMax()); - maxStackSolution1.push(81); - assertEquals(81, maxStackSolution1.pop()); - maxStackSolution1.push(-71); - maxStackSolution1.push(32); - } - - @Test - public void test4() { - maxStackSolution2.push(5); - assertEquals(5, maxStackSolution2.peekMax()); - assertEquals(5, maxStackSolution2.popMax()); - } - - @Test - public void test5() { - maxStackSolution2.push(5); - maxStackSolution2.push(1); - assertEquals(5, maxStackSolution2.popMax()); - assertEquals(1, maxStackSolution2.peekMax()); - } - - @Test - public void test6() { - maxStackSolution2.push(74); - assertEquals(74, maxStackSolution2.popMax()); - maxStackSolution2.push(89); - maxStackSolution2.push(67); - assertEquals(89, maxStackSolution2.popMax()); - assertEquals(67, maxStackSolution2.pop()); - maxStackSolution2.push(61); - maxStackSolution2.push(-77); - assertEquals(61, maxStackSolution2.peekMax()); - assertEquals(61, maxStackSolution2.popMax()); - maxStackSolution2.push(81); - assertEquals(81, maxStackSolution2.peekMax()); - assertEquals(81, maxStackSolution2.popMax()); - maxStackSolution2.push(81); - assertEquals(81, maxStackSolution2.pop()); - maxStackSolution2.push(-71); - maxStackSolution2.push(32); - } -} diff --git a/src/test/java/com/fishercoder/_719Test.java b/src/test/java/com/fishercoder/_719Test.java deleted file mode 100644 index 3e44371726..0000000000 --- a/src/test/java/com/fishercoder/_719Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._719; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _719Test { - private static _719.Solution1 solution1; - private static _719.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _719.Solution1(); - solution2 = new _719.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{1, 3, 1}; - assertEquals(0, solution1.smallestDistancePair(nums, 1)); - assertEquals(0, solution2.smallestDistancePair(nums, 1)); - } -} diff --git a/src/test/java/com/fishercoder/_71Test.java b/src/test/java/com/fishercoder/_71Test.java deleted file mode 100644 index 34ad690cc7..0000000000 --- a/src/test/java/com/fishercoder/_71Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._71; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _71Test { - private static _71.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _71.Solution1(); - } - - @Test - public void test1() { - assertEquals("/home", solution1.simplifyPath("/home/")); - } - - @Test - public void test2() { - assertEquals("/c", solution1.simplifyPath("/a/./b/../../c/")); - } -} diff --git a/src/test/java/com/fishercoder/_720Test.java b/src/test/java/com/fishercoder/_720Test.java deleted file mode 100644 index 369dbdce92..0000000000 --- a/src/test/java/com/fishercoder/_720Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._720; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _720Test { - private static _720.Solution1 solution1; - private static String[] words; - - @BeforeClass - public static void setup() { - solution1 = new _720.Solution1(); - } - - @Test - public void test1() { - words = new String[]{"w", "wo", "wor", "worl", "world"}; - assertEquals("world", solution1.longestWord(words)); - } - - @Test - public void test2() { - words = new String[]{"a", "banana", "app", "appl", "ap", "apply", "apple"}; - assertEquals("apple", solution1.longestWord(words)); - } - -} diff --git a/src/test/java/com/fishercoder/_721Test.java b/src/test/java/com/fishercoder/_721Test.java deleted file mode 100644 index 4c9912eca2..0000000000 --- a/src/test/java/com/fishercoder/_721Test.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._721; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class _721Test { - private static _721.Solution1 solution1; - private static _721.Solution2 solution2; - private static List> accounts; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _721.Solution1(); - solution2 = new _721.Solution2(); - } - - @Test - public void test1() throws Exception { - accounts = new ArrayList<>(); - List account1 = new ArrayList<>(Arrays.asList("John", "johnsmith@mail.com", "john00@mail.com")); - List account2 = new ArrayList<>(Arrays.asList("John", "johnnybravo@mail.com")); - List account3 = new ArrayList<>(Arrays.asList("John", "johnsmith@mail.com", "john_newyork@mail.com")); - List account4 = new ArrayList<>(Arrays.asList("Mary", "mary@mail.com")); - accounts.add(account1); - accounts.add(account2); - accounts.add(account3); - accounts.add(account4); - - expected = new ArrayList<>(); - List expected1 = new ArrayList<>(Arrays.asList("Mary", "mary@mail.com")); - List expected2 = new ArrayList<>(Arrays.asList("John", "john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com")); - List expected3 = new ArrayList<>(Arrays.asList("John", "johnnybravo@mail.com")); - expected.add(expected1); - expected.add(expected2); - expected.add(expected3); - - assertEqualsIgnoreOrdering(expected, solution1.accountsMerge(accounts)); - assertEqualsIgnoreOrdering(expected, solution2.accountsMerge(accounts)); - } - - private void assertEqualsIgnoreOrdering(List> expected, List> actual) throws Exception { - //TODO: implement this method - if (true) { - return; - } else { - throw new Exception(); - } - } - - @Test - public void test2() throws Exception { - accounts = new ArrayList<>(); - List account1 = new ArrayList<>(Arrays.asList("Alex", "Alex5@m.co", "Alex4@m.co", "Alex0@m.co")); - List account2 = new ArrayList<>(Arrays.asList("Ethan", "Ethan3@m.co", "Ethan3@m.co", "Ethan0@m.co")); - List account3 = new ArrayList<>(Arrays.asList("Kevin", "Kevin4@m.co", "Kevin2@m.co", "Kevin2@m.co")); - List account4 = new ArrayList<>(Arrays.asList("Gabe", "Gabe0@m.co", "Gabe3@m.co", "Gabe2@m.co")); - List account5 = new ArrayList<>(Arrays.asList("Gabe", "Gabe3@m.co", "Gabe4@m.co", "Gabe2@m.co")); - accounts.add(account1); - accounts.add(account2); - accounts.add(account3); - accounts.add(account4); - accounts.add(account5); - - expected = new ArrayList<>(); - List expected1 = new ArrayList<>(Arrays.asList("Alex", "Alex0@m.co", "Alex4@m.co", "Alex5@m.co")); - List expected2 = new ArrayList<>(Arrays.asList("Kevin", "Kevin2@m.co", "Kevin4@m.co")); - List expected3 = new ArrayList<>(Arrays.asList("Ethan", "Ethan0@m.co", "Ethan3@m.co")); - List expected4 = new ArrayList<>(Arrays.asList("Gabe", "Gabe0@m.co", "Gabe2@m.co", "Gabe3@m.co", "Gabe4@m.co")); - expected.add(expected1); - expected.add(expected2); - expected.add(expected3); - expected.add(expected4); - - assertEqualsIgnoreOrdering(expected, solution1.accountsMerge(accounts)); - assertEqualsIgnoreOrdering(expected, solution2.accountsMerge(accounts)); - } - - @Test - public void test3() throws Exception { - accounts = new ArrayList<>(); - List account1 = new ArrayList<>(Arrays.asList("David", "David0@m.co", "David1@m.co")); - List account2 = new ArrayList<>(Arrays.asList("David", "David3@m.co", "David4@m.co")); - List account3 = new ArrayList<>(Arrays.asList("David", "David4@m.co", "David5@m.co")); - List account4 = new ArrayList<>(Arrays.asList("David", "David2@m.co", "David3@m.co")); - List account5 = new ArrayList<>(Arrays.asList("David", "David1@m.co", "David2@m.co")); - accounts.add(account1); - accounts.add(account2); - accounts.add(account3); - accounts.add(account4); - accounts.add(account5); - - expected = new ArrayList<>(); - List expected1 = new ArrayList<>(Arrays.asList("David", "David0@m.co", "David1@m.co", "David2@m.co", "David3@m.co", "David4@m.co", "David5@m.co")); - expected.add(expected1); - - assertEqualsIgnoreOrdering(expected, solution1.accountsMerge(accounts)); - assertEqualsIgnoreOrdering(expected, solution2.accountsMerge(accounts)); - } - -} diff --git a/src/test/java/com/fishercoder/_723Test.java b/src/test/java/com/fishercoder/_723Test.java deleted file mode 100644 index 0313d55564..0000000000 --- a/src/test/java/com/fishercoder/_723Test.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._723; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _723Test { - private static _723.Solution1 solution1; - private static int[][] board; - private static int[][] expected; - - @BeforeClass - public static void setup() { - solution1 = new _723.Solution1(); - } - - @Test - public void test1() { - board = new int[][]{ - {110, 5, 112, 113, 114}, - {210, 211, 5, 213, 214}, - {310, 311, 3, 313, 314}, - {410, 411, 412, 5, 414}, - {5, 1, 512, 3, 3}, - {610, 4, 1, 613, 614}, - {710, 1, 2, 713, 714}, - {810, 1, 2, 1, 1}, - {1, 1, 2, 2, 2}, - {4, 1, 4, 4, 1014}, - }; - - expected = new int[][]{ - {0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0}, - {110, 0, 0, 0, 114}, - {210, 0, 0, 0, 214}, - {310, 0, 0, 113, 314}, - {410, 0, 0, 213, 414}, - {610, 211, 112, 313, 614}, - {710, 311, 412, 613, 714}, - {810, 411, 512, 713, 1014} - }; - assert2dArrayEquals(expected, solution1.candyCrush(board)); - } - - private void assert2dArrayEquals(int[][] expected, int[][] actual) { - for (int i = 0; i < expected.length; i++) { - assertArrayEquals(expected[i], actual[i]); - } - } - -} diff --git a/src/test/java/com/fishercoder/_724Test.java b/src/test/java/com/fishercoder/_724Test.java deleted file mode 100644 index 3da93781cf..0000000000 --- a/src/test/java/com/fishercoder/_724Test.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._724; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _724Test { - private static _724.Solution1 solution1; - private static _724.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _724.Solution1(); - solution2 = new _724.Solution2(); - } - - @Test - public void test1() { - nums = new int[]{1, 7, 3, 6, 5, 6}; - assertEquals(3, solution1.pivotIndex(nums)); - } - - @Test - public void test2() { - nums = new int[]{1, 2, 3}; - assertEquals(-1, solution1.pivotIndex(nums)); - } - - @Test - public void test3() { - nums = new int[]{-1, -1, -1, 0, 1, 1}; - assertEquals(0, solution1.pivotIndex(nums)); - } - - @Test - public void test4() { - nums = new int[]{-1, -1, 0, 1, 1, 0}; - assertEquals(5, solution1.pivotIndex(nums)); - } - - @Test - public void test5() { - nums = new int[]{1, 7, 3, 6, 5, 6}; - assertEquals(3, solution2.pivotIndex(nums)); - } - - @Test - public void test6() { - nums = new int[]{1, 2, 3}; - assertEquals(-1, solution2.pivotIndex(nums)); - } - - @Test - public void test7() { - nums = new int[]{-1, -1, -1, 0, 1, 1}; - assertEquals(0, solution2.pivotIndex(nums)); - } - - @Test - public void test8() { - nums = new int[]{-1, -1, 0, 1, 1, 0}; - assertEquals(5, solution2.pivotIndex(nums)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_725Test.java b/src/test/java/com/fishercoder/_725Test.java deleted file mode 100644 index 98c50cabb6..0000000000 --- a/src/test/java/com/fishercoder/_725Test.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._725; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _725Test { - private static _725.Solution1 solution1; - private static _725.Solution2 solution2; - private static ListNode root; - private static int k; - private static ListNode[] actual; - - @BeforeClass - public static void setup() { - solution1 = new _725.Solution1(); - solution2 = new _725.Solution2(); - } - - @Test - public void test1() { - root = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3}); - k = 5; - actual = solution1.splitListToParts(root, k); - for (ListNode head : actual) { - ListNode.printList(head); - } - } - - @Test - public void test2() { - root = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); - k = 3; - actual = solution1.splitListToParts(root, k); - for (ListNode head : actual) { - ListNode.printList(head); - } - } - - @Test - public void test3() { - root = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3}); - k = 5; - actual = solution2.splitListToParts(root, k); - for (ListNode head : actual) { - ListNode.printList(head); - } - } - - @Test - public void test4() { - root = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); - k = 3; - actual = solution2.splitListToParts(root, k); - for (ListNode head : actual) { - ListNode.printList(head); - } - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_727Test.java b/src/test/java/com/fishercoder/_727Test.java deleted file mode 100644 index cfa6986d4d..0000000000 --- a/src/test/java/com/fishercoder/_727Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._727; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _727Test { - private static _727.Solution1 solution1; - private static _727.Solution2 solution2; - private static String S; - private static String T; - - @Before - public void setup() { - solution1 = new _727.Solution1(); - solution2 = new _727.Solution2(); - } - - @Test - public void test1() { - S = "abcdebdde"; - T = "bde"; - assertEquals("bcde", solution1.minWindow(S, T)); - assertEquals("bcde", solution2.minWindow(S, T)); - } - - @Test - public void test2() { - String S = "jmeqksfrsdcmsiwvaovztaqenprpvnbstl"; - String T = "l"; - assertEquals("l", solution1.minWindow(S, T)); - assertEquals("l", solution2.minWindow(S, T)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_728Test.java b/src/test/java/com/fishercoder/_728Test.java deleted file mode 100644 index 2f869a3828..0000000000 --- a/src/test/java/com/fishercoder/_728Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._728; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _728Test { - private static _728.Solution1 solution1; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _728.Solution1(); - } - - @Test - public void test1() { - expected = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22); - assertEquals(expected, solution1.selfDividingNumbers(1, 22)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_72Test.java b/src/test/java/com/fishercoder/_72Test.java deleted file mode 100644 index 9e5349871e..0000000000 --- a/src/test/java/com/fishercoder/_72Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._72; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _72Test { - private static _72.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _72.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.minDistance("Ada", "Adam")); - } -} diff --git a/src/test/java/com/fishercoder/_733Test.java b/src/test/java/com/fishercoder/_733Test.java deleted file mode 100644 index e10d7c4b2d..0000000000 --- a/src/test/java/com/fishercoder/_733Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._733; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _733Test { - private static _733.Solution1 solution1; - private static int[][] image; - private static int[][] result; - - @BeforeClass - public static void setup() { - solution1 = new _733.Solution1(); - } - - @Test - public void test1() { - image = new int[][]{ - {1, 1, 1}, - {1, 1, 0}, - {1, 0, 1} - }; - result = solution1.floodFill(image, 1, 1, 2); - CommonUtils.print2DIntArray(result); - } - - @Test - public void test2() { - image = new int[][]{ - {0, 0, 0}, - {0, 0, 0} - }; - result = solution1.floodFill(image, 0, 0, 2); - CommonUtils.print2DIntArray(result); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_734Test.java b/src/test/java/com/fishercoder/_734Test.java deleted file mode 100644 index 06aa38400f..0000000000 --- a/src/test/java/com/fishercoder/_734Test.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._734; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _734Test { - private static _734.Solution1 solution1; - private static String[] words1; - private static String[] words2; - private static String[][] pairs; - - @BeforeClass - public static void setup() { - solution1 = new _734.Solution1(); - } - - @Test - public void test1() { - words1 = new String[]{"great", "acting", "skills"}; - words2 = new String[]{"fine", "drama", "talent"}; - pairs = new String[][]{ - {"great", "fine"}, - {"acting", "drama"}, - {"skills", "talent"} - }; - assertEquals(true, solution1.areSentencesSimilar(words1, words2, pairs)); - } - - @Test - public void test2() { - String[] words1 = new String[]{"one", "excellent", "meal"}; - String[] words2 = new String[]{"one", "good", "dinner"}; - String[][] pairs = new String[][]{ - {"great", "good"}, - {"extraordinary", "good"}, - {"well", "good"}, - {"wonderful", "good"}, - {"excellent", "good"}, - {"dinner", "meal"}, - {"fine", "good"}, - {"nice", "good"}, - {"any", "one"}, - {"unique", "one"}, - {"some", "one"}, - {"the", "one"}, - {"an", "one"}, - {"single", "one"}, - {"a", "one"}, - {"keep", "own"}, - {"truck", "car"}, - {"super", "very"}, - {"really", "very"}, - {"actually", "very"}, - {"extremely", "very"}, - {"have", "own"}, - {"possess", "own"}, - {"lunch", "meal"}, - {"super", "meal"}, - {"food", "meal"}, - {"breakfast", "meal"}, - {"brunch", "meal"}, - {"wagon", "car"}, - {"automobile", "car"}, - {"auto", "car"}, - {"fruits", "meal"}, - {"vehicle", "car"}, - {"entertain", "have"}, - {"drink", "have"}, - {"eat", "have"}, - {"take", "have"}, - }; - assertEquals(true, solution1.areSentencesSimilar(words1, words2, pairs)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_735Test.java b/src/test/java/com/fishercoder/_735Test.java deleted file mode 100644 index 9878ebd7d1..0000000000 --- a/src/test/java/com/fishercoder/_735Test.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._735; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _735Test { - private static _735.Solution1 solution1; - private static int[] asteroids; - - @BeforeClass - public static void setup() { - solution1 = new _735.Solution1(); - } - - @Test - public void test1() { - asteroids = new int[]{5, 10, -5}; - asteroids = solution1.asteroidCollision(asteroids); - assertArrayEquals(new int[]{5, 10}, asteroids); - } - - @Test - public void test2() { - asteroids = new int[]{8, -8}; - asteroids = solution1.asteroidCollision(asteroids); - assertArrayEquals(new int[]{}, asteroids); - } - - @Test - public void test3() { - asteroids = new int[]{10, 2, -5}; - asteroids = solution1.asteroidCollision(asteroids); - assertArrayEquals(new int[]{10}, asteroids); - } - - @Test - public void test4() { - asteroids = new int[]{-2, 1, 2, -2}; - asteroids = solution1.asteroidCollision(asteroids); - assertArrayEquals(new int[]{-2, 1}, asteroids); - } - - @Test - public void test5() { - asteroids = new int[]{-2, -2, -2, 1}; - asteroids = solution1.asteroidCollision(asteroids); - assertArrayEquals(new int[]{-2, -2, -2, 1}, asteroids); - } - - @Test - public void test6() { - asteroids = new int[]{-2, -1, 1, 2}; - asteroids = solution1.asteroidCollision(asteroids); - assertArrayEquals(new int[]{-2, -1, 1, 2}, asteroids); - } - - @Test - public void test7() { - asteroids = new int[]{-2, -2, 1, -2}; - asteroids = solution1.asteroidCollision(asteroids); - assertArrayEquals(new int[]{-2, -2, -2}, asteroids); - } - - @Test - public void test8() { - asteroids = new int[]{-4, -1, 10, 2, -1, 8, -9, -6, 5, 2}; - asteroids = solution1.asteroidCollision(asteroids); - assertArrayEquals(new int[]{-4, -1, 10, 5, 2}, asteroids); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_737Test.java b/src/test/java/com/fishercoder/_737Test.java deleted file mode 100644 index ba6dd98043..0000000000 --- a/src/test/java/com/fishercoder/_737Test.java +++ /dev/null @@ -1,355 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._737; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _737Test { - private static _737.Solution1 solution1; - private static String[] words1; - private static String[] words2; - private static String[][] pairs; - - @BeforeClass - public static void setup() { - solution1 = new _737.Solution1(); - } - - @Test - public void test1() { - words1 = new String[]{"great", "acting", "skills"}; - words2 = new String[]{"fine", "drama", "talent"}; - pairs = new String[][]{ - {"great", "fine"}, - {"acting", "drama"}, - {"skills", "talent"} - }; - assertEquals(true, solution1.areSentencesSimilarTwo(words1, words2, pairs)); - } - - @Test - public void test2() { - words1 = new String[]{"great", "acting", "skills"}; - words2 = new String[]{"fine", "drama", "talent"}; - pairs = new String[][]{ - {"great", "good"}, - {"fine", "good"}, - {"drama", "acting"}, - {"skills", "talent"} - }; - assertEquals(true, solution1.areSentencesSimilarTwo(words1, words2, pairs)); - } - - - @Test - public void test3() { - String[] words1 = new String[]{"one", "excellent", "meal"}; - String[] words2 = new String[]{"one", "good", "dinner"}; - String[][] pairs = new String[][]{ - {"great", "good"}, - {"extraordinary", "good"}, - {"well", "good"}, - {"wonderful", "good"}, - {"excellent", "good"}, - {"dinner", "meal"}, - {"fine", "good"}, - {"nice", "good"}, - {"any", "one"}, - {"unique", "one"}, - {"some", "one"}, - {"the", "one"}, - {"an", "one"}, - {"single", "one"}, - {"a", "one"}, - {"keep", "own"}, - {"truck", "car"}, - {"super", "very"}, - {"really", "very"}, - {"actually", "very"}, - {"extremely", "very"}, - {"have", "own"}, - {"possess", "own"}, - {"lunch", "meal"}, - {"super", "meal"}, - {"food", "meal"}, - {"breakfast", "meal"}, - {"brunch", "meal"}, - {"wagon", "car"}, - {"automobile", "car"}, - {"auto", "car"}, - {"fruits", "meal"}, - {"vehicle", "car"}, - {"entertain", "have"}, - {"drink", "have"}, - {"eat", "have"}, - {"take", "have"}, - }; - assertEquals(true, solution1.areSentencesSimilarTwo(words1, words2, pairs)); - } - - @Test - public void test4() { - words1 = new String[]{"jrocadcojmybpxmuj", "livgsrfvgtovcurzq", "mnrdscqkycodx", "wgcjlntupylayse", "tglnshmqlmkqqfbpf", "uzlxmaoro", "narvuaqmmkqhd", "xozoyaqxtbustrymo", "jrocadcojmybpxmuj", "ainlwrwabqcwq", "qnjidlmwmxxjgntez", "bbchthovla", "vaufbmwdrupcxpg", "zwwgloilddclufwze", "tyxrlpmcy", "wtjtdrlm", "edurtetzseifez", "yzxogkunvohdmro", "livgsrfvgtovcurzq", "wmpvjvzljhnaxvp", "rqbswlkw", "umlzibkkpsyvpdol", "jkcmceinlyhi", "wlvmfxbleuot", "aeloeauxmc", "ooyllkxg", "wlvmfxbleuot", "cuewcvuy", "vaufbmwdrupcxpg", "bbchthovla", "arigdtezmyz", "yzxogkunvohdmro", "wrszraxxdum", "dhmiuqhqlsprxy", "xpmxtfyvjrnujyxjh", "bfxbncez", "cjjkmybleu", "mnrdscqkycodx", "mzfpofjn", "livgsrfvgtovcurzq", "shfzcyboj", "xozoyaqxtbustrymo", "xozoyaqxtbustrymo", "orlzzpytpzazxr", "filnwifbukdqijgr", "fllqjtnxwmfoou", "mkmawbogphdttd", "rthpxoxyyiy", "dkhfozltuckwog", "wmpvjvzljhnaxvp", "dhmiuqhqlsprxy", "yltljjairlkrmdq", "cuewcvuy", "subzoyxjkfiwmfb", "mzvbgcizeeth", "narvuaqmmkqhd", "tglnshmqlmkqqfbpf", "rpesfkhfjucj", "xrgfejybbkezgor", "vaufbmwdrupcxpg", "czlgbqzffodsoxng", "suvvqdiceuogcmv", "fllqjtnxwmfoou", "yltljjairlkrmdq", "bubwouozgs", "mnrdscqkycodx", "rqbswlkw", "ooyllkxg", "livgsrfvgtovcurzq", "rthpxoxyyiy", "pyzcbpjhntpefbq", "wtjtdrlm", "rztcppnmud", "inuzvkgolupxelcal", "pdxsxjop", "wmpvjvzljhnaxvp", "xydwvemqvtgvzl", "hqpnoczciajvkbdy", "rvihrzzkt", "jzquemjzpvfbka", "gkqrglav", "qyaxqaqxiwr", "mzvbgcizeeth", "umlzibkkpsyvpdol", "vaufbmwdrupcxpg", "ooyllkxg", "arigdtezmyz", "bubwouozgs", "wtjtdrlm", "xozoyaqxtbustrymo", "jrocadcojmybpxmuj", "rnlryins", "fllqjtnxwmfoou", "livgsrfvgtovcurzq", "czlgbqzffodsoxng", "hlcsiukaroscfg", "bfxbncez", "ainlwrwabqcwq", "vaufbmwdrupcxpg", "vaufbmwdrupcxpg"}; - words2 = new String[]{"jrocadcojmybpxmuj", "livgsrfvgtovcurzq", "mnrdscqkycodx", "wgcjlntupylayse", "bbchthovla", "bfxbncez", "ztisufueqzequ", "yutahdply", "suvvqdiceuogcmv", "ainlwrwabqcwq", "fquzrlhdsnuwhhu", "tglnshmqlmkqqfbpf", "vaufbmwdrupcxpg", "zwwgloilddclufwze", "livgsrfvgtovcurzq", "wtjtdrlm", "edurtetzseifez", "ecqfdkebnamkfglk", "livgsrfvgtovcurzq", "wmpvjvzljhnaxvp", "ryubcgbzmxc", "pzlmeboecybxmetz", "hqpnoczciajvkbdy", "xpmxtfyvjrnujyxjh", "zwwgloilddclufwze", "khcyhttaaxp", "wlvmfxbleuot", "jzquemjzpvfbka", "vaufbmwdrupcxpg", "tglnshmqlmkqqfbpf", "mzvbgcizeeth", "cjjkmybleu", "orlzzpytpzazxr", "dhmiuqhqlsprxy", "mzfpofjn", "bfxbncez", "inuzvkgolupxelcal", "inhzsspqltvl", "wlvmfxbleuot", "livgsrfvgtovcurzq", "orlzzpytpzazxr", "yutahdply", "yutahdply", "orlzzpytpzazxr", "gdziaihbagl", "yltljjairlkrmdq", "mkmawbogphdttd", "aotjpvanljxe", "aeloeauxmc", "wmpvjvzljhnaxvp", "dhmiuqhqlsprxy", "yltljjairlkrmdq", "dnaaehrekqms", "khcyhttaaxp", "mzvbgcizeeth", "narvuaqmmkqhd", "rvihrzzkt", "bfufqsusp", "xrgfejybbkezgor", "vaufbmwdrupcxpg", "czlgbqzffodsoxng", "jrocadcojmybpxmuj", "yltljjairlkrmdq", "yltljjairlkrmdq", "bubwouozgs", "inhzsspqltvl", "bsybvehdny", "subzoyxjkfiwmfb", "livgsrfvgtovcurzq", "stkglpqdjzxmnlito", "evepphnzuw", "xrgfejybbkezgor", "rztcppnmud", "cjjkmybleu", "qyaxqaqxiwr", "ibwfxvxswjbecab", "xydwvemqvtgvzl", "hqpnoczciajvkbdy", "tglnshmqlmkqqfbpf", "dnaaehrekqms", "gkqrglav", "bfxbncez", "qvwvgzxqihvk", "umlzibkkpsyvpdol", "vaufbmwdrupcxpg", "khcyhttaaxp", "arigdtezmyz", "bubwouozgs", "fllqjtnxwmfoou", "xozoyaqxtbustrymo", "jrocadcojmybpxmuj", "rnlryins", "wtjtdrlm", "livgsrfvgtovcurzq", "gkqrglav", "orileazg", "uzlxmaoro", "ainlwrwabqcwq", "vaufbmwdrupcxpg", "vaufbmwdrupcxpg"}; - pairs = new String[][]{ - {"yutahdply", "yutahdply"}, - {"xozoyaqxtbustrymo", "xozoyaqxtbustrymo"}, - {"xozoyaqxtbustrymo", "xozoyaqxtbustrymo"}, - {"yutahdply", "yutahdply"}, - {"shfzcyboj", "orlzzpytpzazxr"}, - {"suvvqdiceuogcmv", "llrzqdnoxbscnkqy"}, - {"jkcmceinlyhi", "hqpnoczciajvkbdy"}, - {"hqpnoczciajvkbdy", "hqpnoczciajvkbdy"}, - {"rztcppnmud", "vdjccijgqk"}, - {"vdjccijgqk", "vdjccijgqk"}, - {"jkcmceinlyhi", "hqpnoczciajvkbdy"}, - {"rztcppnmud", "rztcppnmud"}, - {"vdjccijgqk", "vdjccijgqk"}, - {"hqpnoczciajvkbdy", "hqpnoczciajvkbdy"}, - {"umlzibkkpsyvpdol", "ryubcgbzmxc"}, - {"ryubcgbzmxc", "ryubcgbzmxc"}, - {"pzlmeboecybxmetz", "bsybvehdny"}, - {"rqbswlkw", "bsybvehdny"}, - {"bsybvehdny", "bsybvehdny"}, - {"umlzibkkpsyvpdol", "umlzibkkpsyvpdol"}, - {"ryubcgbzmxc", "ryubcgbzmxc"}, - {"rqbswlkw", "rqbswlkw"}, - {"pzlmeboecybxmetz", "pzlmeboecybxmetz"}, - {"bsybvehdny", "bsybvehdny"}, - {"dkhfozltuckwog", "zwwgloilddclufwze"}, - {"zfmpxgrevxp", "pyzcbpjhntpefbq"}, - {"gkqrglav", "czlgbqzffodsoxng"}, - {"tyxrlpmcy", "livgsrfvgtovcurzq"}, - {"shsgrqol", "cufxsgbpjgqvk"}, - {"rphnhtvnihyfkrgv", "yykdqtkkdacpbwtbq"}, - {"dhmiuqhqlsprxy", "ztisufueqzequ"}, - {"ibwfxvxswjbecab", "xydwvemqvtgvzl"}, - {"mkmawbogphdttd", "ainlwrwabqcwq"}, - {"pdxsxjop", "uzlxmaoro"}, - {"ooyllkxg", "khcyhttaaxp"}, - {"jrocadcojmybpxmuj", "jrocadcojmybpxmuj"}, - {"lkopigreodypvude", "lkopigreodypvude"}, - {"hqpnoczciajvkbdy", "rztcppnmud"}, - {"llrzqdnoxbscnkqy", "jrocadcojmybpxmuj"}, - {"cuewcvuy", "jzquemjzpvfbka"}, - {"wlvmfxbleuot", "bfufqsusp"}, - {"bfufqsusp", "bfufqsusp"}, - {"xpmxtfyvjrnujyxjh", "rpesfkhfjucj"}, - {"mzfpofjn", "rpesfkhfjucj"}, - {"rpesfkhfjucj", "rpesfkhfjucj"}, - {"xpmxtfyvjrnujyxjh", "mzfpofjn"}, - {"wlvmfxbleuot", "bfufqsusp"}, - {"rpesfkhfjucj", "xpmxtfyvjrnujyxjh"}, - {"cuewcvuy", "dnaaehrekqms"}, - {"dnaaehrekqms", "dnaaehrekqms"}, - {"rpesfkhfjucj", "wlvmfxbleuot"}, - {"lkopigreodypvude", "mzvbgcizeeth"}, - {"tglnshmqlmkqqfbpf", "bbchthovla"}, - {"orileazg", "filnwifbukdqijgr"}, - {"yltljjairlkrmdq", "xrgfejybbkezgor"}, - {"inuzvkgolupxelcal", "hgxrhkanzvzmsjpzl"}, - {"jzquemjzpvfbka", "iziepzqne"}, - {"muaskefecskjghzn", "iziepzqne"}, - {"hhrllhedyy", "wzflhbbgtc"}, - {"cemnayjhlnj", "hgtyntdmrgjh"}, - {"iziepzqne", "iziepzqne"}, - {"cuewcvuy", "dnaaehrekqms"}, - {"muaskefecskjghzn", "iziepzqne"}, - {"jzquemjzpvfbka", "muaskefecskjghzn"}, - {"dnaaehrekqms", "dnaaehrekqms"}, - {"jrocadcojmybpxmuj", "jrocadcojmybpxmuj"}, - {"llrzqdnoxbscnkqy", "suvvqdiceuogcmv"}, - {"suvvqdiceuogcmv", "suvvqdiceuogcmv"}, - {"bbchthovla", "bbchthovla"}, - {"rvihrzzkt", "tglnshmqlmkqqfbpf"}, - {"filnwifbukdqijgr", "pkirimjwvyxs"}, - {"tglnshmqlmkqqfbpf", "tglnshmqlmkqqfbpf"}, - {"rvihrzzkt", "tglnshmqlmkqqfbpf"}, - {"bbchthovla", "bbchthovla"}, - {"tglnshmqlmkqqfbpf", "tglnshmqlmkqqfbpf"}, - {"hjogoueazw", "lkopigreodypvude"}, - {"lkopigreodypvude", "lkopigreodypvude"}, - {"mzvbgcizeeth", "arigdtezmyz"}, - {"qvwvgzxqihvk", "arigdtezmyz"}, - {"arigdtezmyz", "arigdtezmyz"}, - {"mzvbgcizeeth", "arigdtezmyz"}, - {"qvwvgzxqihvk", "qvwvgzxqihvk"}, - {"hjogoueazw", "hjogoueazw"}, - {"subzoyxjkfiwmfb", "khcyhttaaxp"}, - {"subzoyxjkfiwmfb", "subzoyxjkfiwmfb"}, - {"khcyhttaaxp", "subzoyxjkfiwmfb"}, - {"ooyllkxg", "ooyllkxg"}, - {"orlzzpytpzazxr", "orlzzpytpzazxr"}, - {"oufzmjgplt", "oufzmjgplt"}, - {"shfzcyboj", "shfzcyboj"}, - {"oufzmjgplt", "oufzmjgplt"}, - {"orlzzpytpzazxr", "oufzmjgplt"}, - {"wrszraxxdum", "wrszraxxdum"}, - {"wrszraxxdum", "wrszraxxdum"}, - {"shfzcyboj", "wrszraxxdum"}, - {"yutahdply", "xozoyaqxtbustrymo"}, - {"umlzibkkpsyvpdol", "pzlmeboecybxmetz"}, - {"hgxrhkanzvzmsjpzl", "gwkkpxuvgp"}, - {"xrgfejybbkezgor", "wtjtdrlm"}, - {"wtjtdrlm", "wtjtdrlm"}, - {"yltljjairlkrmdq", "fllqjtnxwmfoou"}, - {"xrgfejybbkezgor", "wtjtdrlm"}, - {"filnwifbukdqijgr", "pkirimjwvyxs"}, - {"pkirimjwvyxs", "pkirimjwvyxs"}, - {"gdziaihbagl", "orileazg"}, - {"orileazg", "orileazg"}, - {"gdziaihbagl", "orileazg"}, - {"hlcsiukaroscfg", "orileazg"}, - {"hlcsiukaroscfg", "hlcsiukaroscfg"}, - {"gdziaihbagl", "gdziaihbagl"}, - {"ainlwrwabqcwq", "ainlwrwabqcwq"}, - {"uzlxmaoro", "bfxbncez"}, - {"qyaxqaqxiwr", "qyaxqaqxiwr"}, - {"pdxsxjop", "pdxsxjop"}, - {"pdxsxjop", "pdxsxjop"}, - {"subzoyxjkfiwmfb", "subzoyxjkfiwmfb"}, - {"uzlxmaoro", "bfxbncez"}, - {"bfxbncez", "bfxbncez"}, - {"qyaxqaqxiwr", "pdxsxjop"}, - {"ooyllkxg", "ooyllkxg"}, - {"hgxrhkanzvzmsjpzl", "ecqfdkebnamkfglk"}, - {"gwkkpxuvgp", "ecqfdkebnamkfglk"}, - {"ecqfdkebnamkfglk", "ecqfdkebnamkfglk"}, - {"yzxogkunvohdmro", "yzxogkunvohdmro"}, - {"inuzvkgolupxelcal", "yzxogkunvohdmro"}, - {"yzxogkunvohdmro", "yzxogkunvohdmro"}, - {"cjjkmybleu", "yzxogkunvohdmro"}, - {"inuzvkgolupxelcal", "inuzvkgolupxelcal"}, - {"ecqfdkebnamkfglk", "gwkkpxuvgp"}, - {"dwojnswr", "dkhfozltuckwog"}, - {"yltljjairlkrmdq", "fllqjtnxwmfoou"}, - {"fllqjtnxwmfoou", "fllqjtnxwmfoou"}, - {"wzflhbbgtc", "zzdvolqtndzfjvqqr"}, - {"dkhfozltuckwog", "dkhfozltuckwog"}, - {"zfmpxgrevxp", "stkglpqdjzxmnlito"}, - {"wzflhbbgtc", "wzflhbbgtc"}, - {"cjjkmybleu", "cjjkmybleu"}, - {"wgcjlntupylayse", "wgcjlntupylayse"}, - {"vyrvelteblnqaabc", "vyrvelteblnqaabc"}, - {"bvxiilsnsarhsyl", "zzdvolqtndzfjvqqr"}, - {"stkglpqdjzxmnlito", "stkglpqdjzxmnlito"}, - {"cemnayjhlnj", "cemnayjhlnj"}, - {"cemnayjhlnj", "cemnayjhlnj"}, - {"hgtyntdmrgjh", "hgtyntdmrgjh"}, - {"rnlryins", "vyrvelteblnqaabc"}, - {"hhrllhedyy", "vyrvelteblnqaabc"}, - {"rnlryins", "rnlryins"}, - {"fquzrlhdsnuwhhu", "zzdvolqtndzfjvqqr"}, - {"zzdvolqtndzfjvqqr", "bvxiilsnsarhsyl"}, - {"wmpvjvzljhnaxvp", "wmpvjvzljhnaxvp"}, - {"qnjidlmwmxxjgntez", "vyrvelteblnqaabc"}, - {"fquzrlhdsnuwhhu", "zzdvolqtndzfjvqqr"}, - {"zzdvolqtndzfjvqqr", "zzdvolqtndzfjvqqr"}, - {"edurtetzseifez", "rphnhtvnihyfkrgv"}, - {"wgcjlntupylayse", "wgcjlntupylayse"}, - {"zwwgloilddclufwze", "aeloeauxmc"}, - {"rphnhtvnihyfkrgv", "rphnhtvnihyfkrgv"}, - {"aeloeauxmc", "aeloeauxmc"}, - {"hgtyntdmrgjh", "wgcjlntupylayse"}, - {"rphnhtvnihyfkrgv", "rphnhtvnihyfkrgv"}, - {"cufxsgbpjgqvk", "cufxsgbpjgqvk"}, - {"mnrdscqkycodx", "shsgrqol"}, - {"qnjidlmwmxxjgntez", "hhrllhedyy"}, - {"shsgrqol", "shsgrqol"}, - {"vyrvelteblnqaabc", "qnjidlmwmxxjgntez"}, - {"zwwgloilddclufwze", "aeloeauxmc"}, - {"evepphnzuw", "rthpxoxyyiy"}, - {"rthpxoxyyiy", "rthpxoxyyiy"}, - {"aotjpvanljxe", "aotjpvanljxe"}, - {"aotjpvanljxe", "stkglpqdjzxmnlito"}, - {"dkhfozltuckwog", "dwojnswr"}, - {"rthpxoxyyiy", "pyzcbpjhntpefbq"}, - {"evepphnzuw", "evepphnzuw"}, - {"aeloeauxmc", "aeloeauxmc"}, - {"zfmpxgrevxp", "aotjpvanljxe"}, - {"stkglpqdjzxmnlito", "aotjpvanljxe"}, - {"bubwouozgs", "mkmawbogphdttd"}, - {"pyzcbpjhntpefbq", "rthpxoxyyiy"}, - {"gkqrglav", "gkqrglav"}, - {"czlgbqzffodsoxng", "czlgbqzffodsoxng"}, - {"yykdqtkkdacpbwtbq", "yykdqtkkdacpbwtbq"}, - {"dhmiuqhqlsprxy", "dhmiuqhqlsprxy"}, - {"ztisufueqzequ", "ztisufueqzequ"}, - {"ztisufueqzequ", "narvuaqmmkqhd"}, - {"narvuaqmmkqhd", "narvuaqmmkqhd"}, - {"narvuaqmmkqhd", "narvuaqmmkqhd"}, - {"ibwfxvxswjbecab", "ibwfxvxswjbecab"}, - {"dhmiuqhqlsprxy", "dhmiuqhqlsprxy"}, - {"xydwvemqvtgvzl", "wmpvjvzljhnaxvp"}, - {"wmpvjvzljhnaxvp", "wmpvjvzljhnaxvp"}, - {"xydwvemqvtgvzl", "wmpvjvzljhnaxvp"}, - {"ibwfxvxswjbecab", "ibwfxvxswjbecab"}, - {"bubwouozgs", "mkmawbogphdttd"}, - {"mkmawbogphdttd", "mkmawbogphdttd"}, - {"ainlwrwabqcwq", "ainlwrwabqcwq"}, - {"mkmawbogphdttd", "mkmawbogphdttd"}, - {"edurtetzseifez", "edurtetzseifez"}, - {"inhzsspqltvl", "inhzsspqltvl"}, - {"cufxsgbpjgqvk", "inhzsspqltvl"}, - {"yykdqtkkdacpbwtbq", "yykdqtkkdacpbwtbq"}, - {"mnrdscqkycodx", "mnrdscqkycodx"}, - {"shsgrqol", "shsgrqol"}, - {"cufxsgbpjgqvk", "inhzsspqltvl"}, - {"livgsrfvgtovcurzq", "livgsrfvgtovcurzq"}, - {"tyxrlpmcy", "tyxrlpmcy"}, - {"livgsrfvgtovcurzq", "livgsrfvgtovcurzq"}, - {"tyxrlpmcy", "tyxrlpmcy"}, - {"czlgbqzffodsoxng", "czlgbqzffodsoxng"}, - {"gkqrglav", "gkqrglav"}, - }; - assertEquals(true, solution1.areSentencesSimilarTwo(words1, words2, pairs)); - } - - @Test - public void test5() { - words1 = new String[]{"a", "very", "delicious", "meal"}; - words2 = new String[]{"one", "really", "good", "dinner"}; - pairs = new String[][]{ - {"great", "good"}, - {"extraordinary", "good"}, - {"well", "good"}, - {"wonderful", "good"}, - {"excellent", "good"}, - {"fine", "good"}, - {"nice", "good"}, - {"any", "one"}, - {"some", "one"}, - {"unique", "one"}, - {"the", "one"}, - {"an", "one"}, - {"single", "one"}, - {"a", "one"}, - {"truck", "car"}, - {"wagon", "car"}, - {"automobile", "car"}, - {"auto", "car"}, - {"vehicle", "car"}, - {"entertain", "have"}, - {"drink", "have"}, - {"eat", "have"}, - {"take", "have"}, - {"fruits", "meal"}, - {"brunch", "meal"}, - {"breakfast", "meal"}, - {"food", "meal"}, - {"dinner", "meal"}, - {"super", "meal"}, - {"lunch", "meal"}, - {"possess", "own"}, - {"keep", "own"}, - {"have", "own"}, - {"extremely", "very"}, - {"really", "very"}, - {"super", "very"}, - }; - assertEquals(false, solution1.areSentencesSimilarTwo(words1, words2, pairs)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_738Test.java b/src/test/java/com/fishercoder/_738Test.java deleted file mode 100644 index e6f599f223..0000000000 --- a/src/test/java/com/fishercoder/_738Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._738; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _738Test { - private static _738.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _738.Solution1(); - } - - @Test - public void test1() { - assertEquals(9, solution1.monotoneIncreasingDigits(10)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_739Test.java b/src/test/java/com/fishercoder/_739Test.java deleted file mode 100644 index 71690b113d..0000000000 --- a/src/test/java/com/fishercoder/_739Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._739; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _739Test { - private static _739.Solution1 solution1; - private static int[] temperatures; - private static int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _739.Solution1(); - } - - @Test - public void test1() { - temperatures = new int[]{73, 74, 75, 71, 69, 72, 76, 73}; - expected = new int[]{1, 1, 4, 2, 1, 1, 0, 0}; - assertArrayEquals(expected, solution1.dailyTemperatures(temperatures)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_73Test.java b/src/test/java/com/fishercoder/_73Test.java deleted file mode 100644 index 5b82a37d24..0000000000 --- a/src/test/java/com/fishercoder/_73Test.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._73; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _73Test { - private static _73.Solution1 solution1; - private static _73.Solution2 solution2; - private static _73.Solution3 solution3; - private static _73.Solution4 solution4; - private static int[][] matrix; - private static int[][] expected; - - @BeforeClass - public static void setup() { - solution1 = new _73.Solution1(); - solution2 = new _73.Solution2(); - solution3 = new _73.Solution3(); - solution4 = new _73.Solution4(); - } - - @Test - public void test1() { - matrix = new int[][]{ - {0, 0, 0, 5}, - {4, 3, 1, 4}, - {0, 1, 1, 4}, - {1, 2, 1, 3}, - {0, 0, 1, 1} - }; - solution1.setZeroes(matrix); - expected = new int[][]{ - {0, 0, 0, 0}, - {0, 0, 0, 4}, - {0, 0, 0, 0}, - {0, 0, 0, 3}, - {0, 0, 0, 0} - }; - assertArrayEquals(expected, matrix); - } - - @Test - public void test2() { - matrix = new int[][]{ - {0, 0, 0, 5}, - {4, 3, 1, 4}, - {0, 1, 1, 4}, - {1, 2, 1, 3}, - {0, 0, 1, 1} - }; - solution2.setZeroes(matrix); - expected = new int[][]{ - {0, 0, 0, 0}, - {0, 0, 0, 4}, - {0, 0, 0, 0}, - {0, 0, 0, 3}, - {0, 0, 0, 0} - }; - assertArrayEquals(expected, matrix); - } - - @Test - public void test3() { - matrix = new int[][]{ - {0, 0, 0, 5}, - {4, 3, 1, 4}, - {0, 1, 1, 4}, - {1, 2, 1, 3}, - {0, 0, 1, 1} - }; - solution3.setZeroes(matrix); - expected = new int[][]{ - {0, 0, 0, 0}, - {0, 0, 0, 4}, - {0, 0, 0, 0}, - {0, 0, 0, 3}, - {0, 0, 0, 0} - }; - assertArrayEquals(expected, matrix); - } - - @Test - public void test4() { - matrix = new int[][]{ - {0, 0, 0, 5}, - {4, 3, 1, 4}, - {0, 1, 1, 4}, - {1, 2, 1, 3}, - {0, 0, 1, 1} - }; - solution4.setZeroes(matrix); - expected = new int[][]{ - {0, 0, 0, 0}, - {0, 0, 0, 4}, - {0, 0, 0, 0}, - {0, 0, 0, 3}, - {0, 0, 0, 0} - }; - assertArrayEquals(expected, matrix); - } - -} diff --git a/src/test/java/com/fishercoder/_740Test.java b/src/test/java/com/fishercoder/_740Test.java deleted file mode 100644 index f90716d580..0000000000 --- a/src/test/java/com/fishercoder/_740Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._740; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _740Test { - private static _740.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _740.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {3, 4, 2}; - assertEquals(6, solution1.deleteAndEarn(nums)); - } - - @Test - public void test2() { - nums = new int[] {2, 2, 3, 3, 3, 4}; - assertEquals(9, solution1.deleteAndEarn(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_742Test.java b/src/test/java/com/fishercoder/_742Test.java deleted file mode 100644 index de11fdf0f4..0000000000 --- a/src/test/java/com/fishercoder/_742Test.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._742; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _742Test { - private static _742.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _742.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2)); - System.out.println(solution1.findClosestLeaf(root, 1)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1)); - assertEquals(1, solution1.findClosestLeaf(root, 1)); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, null, null, 5, null, 6)); - TreeUtils.printBinaryTree(root); - assertEquals(3, solution1.findClosestLeaf(root, 2)); - } - - @Test - public void test4() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); - TreeUtils.printBinaryTree(root); - assertEquals(5, solution1.findClosestLeaf(root, 5)); - } - - @Test - public void test5() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9, null, 10, null, 11, null, 12, null, 13, null, 14, null, 15, null, 16, null, 17, null, 18, null, 19, null, 20, null, 21, null, 22, null, 23, null, 24, null, 25, null, 26, null, 27, null, 28, null, 29, null, 30, null, 31, null, 32, null, 33, null, 34, null, 35, null, 36, null, 37, null, 38, null, 39, null, 40, null, 41, null, 42, null, 43, null, 44, null, 45, null, 46, null, 47, null, 48, null, 49, null, 50, null, 51, null, 52, null, 53, null, 54, null, 55, null, 56, null, 57, null, 58, null, 59, null, 60, null, 61, null, 62, null, 63, null, 64, null, 65, null, 66, null, 67, null, 68, null, 69, null, 70, null, 71, null, 72, null, 73, null, 74, null, 75, null, 76, null, 77, null, 78, null, 79, null, 80, null, 81, null, 82, null, 83, null, 84, null, 85, null, 86, null, 87, null, 88, null, 89, null, 90, null, 91, null, 92, null, 93, null, 94, null, 95, null, 96, null, 97, null, 98, null, 99, null, 100, null, 101, null, 102, null, 103, null, 104, null, 105, null, 106, null, 107, null, 108, null, 109, null, 110, null, 111, null, 112, null, 113, null, 114, null, 115, null, 116, null, 117, null, 118, null, 119, null, 120, null, 121, null, 122, null, 123, null, 124, null, 125, null, 126, null, 127, null, 128, null, 129, null, 130, null, 131, null, 132, null, 133, null, 134, null, 135, null, 136, null, 137, null, 138, null, 139, null, 140, null, 141, null, 142, null, 143, null, 144, null, 145, null, 146, null, 147, null, 148, null, 149, null, 150, null, 151, null, 152, null, 153, null, 154, null, 155, null, 156, null, 157, null, 158, null, 159, null, 160, null, 161, null, 162, null, 163, null, 164, null, 165, null, 166, null, 167, null, 168, null, 169, null, 170, null, 171, null, 172, null, 173, null, 174, null, 175, null, 176, null, 177, null, 178, null, 179, null, 180, null, 181, null, 182, null, 183, null, 184, null, 185, null, 186, null, 187, null, 188, null, 189, null, 190, null, 191, null, 192, null, 193, null, 194, null, 195, null, 196, null, 197, null, 198, null, 199, null, 200, null, 201, null, 202, null, 203, null, 204, null, 205, null, 206, null, 207, null, 208, null, 209, null, 210, null, 211, null, 212, null, 213, null, 214, null, 215, null, 216, null, 217, null, 218, null, 219, null, 220, null, 221, null, 222, null, 223, null, 224, null, 225, null, 226, null, 227, null, 228, null, 229, null, 230, null, 231, null, 232, null, 233, null, 234, null, 235, null, 236, null, 237, null, 238, null, 239, null, 240, null, 241, null, 242, null, 243, null, 244, null, 245, null, 246, null, 247, null, 248, null, 249, null, 250, null, 251, null, 252, null, 253, null, 254, null, 255, null, 256, null, 257, null, 258, null, 259, null, 260, null, 261, null, 262, null, 263, null, 264, null, 265, null, 266, null, 267, null, 268, null, 269, null, 270, null, 271, null, 272, null, 273, null, 274, null, 275, null, 276, null, 277, null, 278, null, 279, null, 280, null, 281, null, 282, null, 283, null, 284, null, 285, null, 286, null, 287, null, 288, null, 289, null, 290, null, 291, null, 292, null, 293, null, 294, null, 295, null, 296, null, 297, null, 298, null, 299, null, 300, null, 301, null, 302, null, 303, null, 304, null, 305, null, 306, null, 307, null, 308, null, 309, null, 310, null, 311, null, 312, null, 313, null, 314, null, 315, null, 316, null, 317, null, 318, null, 319, null, 320, null, 321, null, 322, null, 323, null, 324, null, 325, null, 326, null, 327, null, 328, null, 329, null, 330, null, 331, null, 332, null, 333, null, 334, null, 335, null, 336, null, 337, null, 338, null, 339, null, 340, null, 341, null, 342, null, 343, null, 344, null, 345, null, 346, null, 347, null, 348, null, 349, null, 350, null, 351, null, 352, null, 353, null, 354, null, 355, null, 356, null, 357, null, 358, null, 359, null, 360, null, 361, null, 362, null, 363, null, 364, null, 365, null, 366, null, 367, null, 368, null, 369, null, 370, null, 371, null, 372, null, 373, null, 374, null, 375, null, 376, null, 377, null, 378, null, 379, null, 380, null, 381, null, 382, null, 383, null, 384, null, 385, null, 386, null, 387, null, 388, null, 389, null, 390, null, 391, null, 392, null, 393, null, 394, null, 395, null, 396, null, 397, null, 398, null, 399, null)); - assertEquals(399, solution1.findClosestLeaf(root, 100)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_743Test.java b/src/test/java/com/fishercoder/_743Test.java deleted file mode 100644 index 825ea25425..0000000000 --- a/src/test/java/com/fishercoder/_743Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._743; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _743Test { - private static _743.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _743.Solution1(); - } - - @Test - public void test1() { - int[][] times = {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}}; - assertEquals(solution1.networkDelayTime(times, 4, 2), 2); - } - - @Test - public void test2() { - int[][] times = {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}}; - assertEquals(solution1.networkDelayTime(times, 4, 3), -1); - } -} diff --git a/src/test/java/com/fishercoder/_744Test.java b/src/test/java/com/fishercoder/_744Test.java deleted file mode 100644 index 920cf4de78..0000000000 --- a/src/test/java/com/fishercoder/_744Test.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._2; -import com.fishercoder.solutions._744; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _744Test { - private static _744.Solution1 solution1; - private static char[] letters; - - @BeforeClass - public static void setup() { - solution1 = new _744.Solution1(); - } - - @Test - public void test1() { - letters = new char[]{'c', 'f', 'j'}; - assertEquals('c', solution1.nextGreatestLetter(letters, 'a')); - } - - @Test - public void test2() { - letters = new char[]{'c', 'f', 'j'}; - assertEquals('f', solution1.nextGreatestLetter(letters, 'c')); - } - - @Test - public void test3() { - letters = new char[]{'c', 'f', 'j'}; - assertEquals('f', solution1.nextGreatestLetter(letters, 'd')); - } - - @Test - public void test4() { - letters = new char[]{'c', 'f', 'j'}; - assertEquals('j', solution1.nextGreatestLetter(letters, 'g')); - } - - @Test - public void test5() { - letters = new char[]{'c', 'f', 'j'}; - assertEquals('c', solution1.nextGreatestLetter(letters, 'j')); - } - - @Test - public void test6() { - letters = new char[]{'c', 'f', 'j'}; - assertEquals('c', solution1.nextGreatestLetter(letters, 'k')); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_746Test.java b/src/test/java/com/fishercoder/_746Test.java deleted file mode 100644 index 8bc6107250..0000000000 --- a/src/test/java/com/fishercoder/_746Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._746; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _746Test { - private static _746.Solution1 solution1; - private static int[] cost; - - @BeforeClass - public static void setup() { - solution1 = new _746.Solution1(); - } - - @Test - public void test1() { - cost = new int[] {10, 15, 20}; - assertEquals(15, solution1.minCostClimbingStairs(cost)); - } - - @Test - public void test2() { - cost = new int[] {1, 100, 1, 1, 1, 100, 1, 1, 100, 1}; - assertEquals(6, solution1.minCostClimbingStairs(cost)); - } -} diff --git a/src/test/java/com/fishercoder/_747Test.java b/src/test/java/com/fishercoder/_747Test.java deleted file mode 100644 index 98f6409fb8..0000000000 --- a/src/test/java/com/fishercoder/_747Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._747; -import com.fishercoder.solutions._9; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _747Test { - private static _747.Solution1 solution1; - private static _747.Solution2 solution2; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _747.Solution1(); - solution2 = new _747.Solution2(); - } - - @Test - public void test1() { - nums = new int[] {3, 6, 1, 0}; - assertEquals(1, solution1.dominantIndex(nums)); - } - - @Test - public void test2() { - nums = new int[] {3, 6, 1, 0}; - assertEquals(1, solution2.dominantIndex(nums)); - } - - @Test - public void test3() { - nums = new int[] {1, 2, 3, 4}; - assertEquals(-1, solution1.dominantIndex(nums)); - } - - @Test - public void test4() { - nums = new int[] {1, 2, 3, 4}; - assertEquals(-1, solution2.dominantIndex(nums)); - } -} diff --git a/src/test/java/com/fishercoder/_748Test.java b/src/test/java/com/fishercoder/_748Test.java deleted file mode 100644 index df3f17f15a..0000000000 --- a/src/test/java/com/fishercoder/_748Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._748; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _748Test { - private static _748.Solution1 solution1; - private static String[] words; - private static String licensePlate; - - @BeforeClass - public static void setup() { - solution1 = new _748.Solution1(); - } - - @Test - public void test1() { - words = new String[] {"step", "steps", "stripe", "stepple"}; - licensePlate = "1s3 PSt"; - assertEquals("steps", solution1.shortestCompletingWord(licensePlate, words)); - } - - @Test - public void test2() { - words = new String[] {"looks", "pest", "stew", "show"}; - licensePlate = "1s3 456"; - assertEquals("pest", solution1.shortestCompletingWord(licensePlate, words)); - } - - @Test - public void test3() { - words = new String[]{"suggest","letter","of","husband","easy","education","drug","prevent","writer","old"}; - licensePlate = "Ah71752"; - assertEquals("husband", solution1.shortestCompletingWord(licensePlate, words)); - } -} diff --git a/src/test/java/com/fishercoder/_74Test.java b/src/test/java/com/fishercoder/_74Test.java deleted file mode 100644 index e7f6634f9f..0000000000 --- a/src/test/java/com/fishercoder/_74Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._74; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _74Test { - private static _74.Solution1 solution1; - private static int target; - private static int[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _74.Solution1(); - } - - @Test - public void test1() { - target = 3; - matrix = new int[][] { - {1, 3, 5, 7}, - {10, 11, 16, 20}, - {23, 30, 34, 50}, - }; - assertEquals(true, solution1.searchMatrix(matrix, target)); - } -} diff --git a/src/test/java/com/fishercoder/_750Test.java b/src/test/java/com/fishercoder/_750Test.java deleted file mode 100644 index 86632f830e..0000000000 --- a/src/test/java/com/fishercoder/_750Test.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._750; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _750Test { - private static _750.Solution1 solution1; - private static int[][] grid; - - @BeforeClass - public static void setup() { - solution1 = new _750.Solution1(); - } - - @Test - public void test1() { - grid = new int[][] { - {1, 0, 0, 1, 0}, - {0, 0, 1, 0, 1}, - {0, 0, 0, 1, 0}, - {1, 0, 1, 0, 1}}; - assertEquals(1, solution1.countCornerRectangles(grid)); - } - - @Test - public void test2() { - grid = new int[][] { - {1, 1, 1}, - {1, 1, 1}, - {1, 1, 1}}; - assertEquals(9, solution1.countCornerRectangles(grid)); - } - - @Test - public void test3() { - grid = new int[][] { - {1, 1, 1, 1}}; - assertEquals(0, solution1.countCornerRectangles(grid)); - } -} diff --git a/src/test/java/com/fishercoder/_754Test.java b/src/test/java/com/fishercoder/_754Test.java deleted file mode 100644 index e1377fa22b..0000000000 --- a/src/test/java/com/fishercoder/_754Test.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._754; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _754Test { - private static _754.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _754.Solution1(); - } - - @Test - public void test4() { - assertEquals(1, solution1.reachNumber(1)); - } - - @Test - public void test2() { - assertEquals(3, solution1.reachNumber(2)); - } - - @Test - public void test1() { - assertEquals(2, solution1.reachNumber(3)); - } - - @Test - public void test3() { - assertEquals(3, solution1.reachNumber(4)); - } - - @Test - public void test5() { - assertEquals(5, solution1.reachNumber(5)); - } - - @Test - public void test6() { - assertEquals(3, solution1.reachNumber(6)); - } - - @Test - public void test7() { - assertEquals(5, solution1.reachNumber(7)); - } - - @Test - public void test8() { - assertEquals(4, solution1.reachNumber(8)); - } - - @Test - public void test9() { - assertEquals(5, solution1.reachNumber(9)); - } - - @Test - public void test10() { - assertEquals(4, solution1.reachNumber(10)); - } - - @Test - public void test11() { - assertEquals(15, solution1.reachNumber(100)); - } - - @Test - public void test12() { - assertEquals(47, solution1.reachNumber(1000)); - } - - @Test - public void test13() { - assertEquals(143, solution1.reachNumber(10000)); - } - - @Test - public void test14() { - assertEquals(447, solution1.reachNumber(100000)); - } - - @Test - public void test15() { - assertEquals(1415, solution1.reachNumber(1000000)); - } - - @Test - public void test16() { - assertEquals(4472, solution1.reachNumber(10000000)); - } -} diff --git a/src/test/java/com/fishercoder/_755Test.java b/src/test/java/com/fishercoder/_755Test.java deleted file mode 100644 index 3b0fd9ca3a..0000000000 --- a/src/test/java/com/fishercoder/_755Test.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._755; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _755Test { - private static _755.Solution1 solution1; - private static int[] heights; - private static int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _755.Solution1(); - } - - @Test - public void test1() { - heights = new int[] {2, 1, 1, 2, 1, 2, 2}; - expected = new int[] {2, 2, 2, 3, 2, 2, 2}; - assertArrayEquals(expected, solution1.pourWater(heights, 4, 3)); - } - - @Test - public void test2() { - heights = new int[] {1, 2, 3, 4}; - expected = new int[] {2, 3, 3, 4}; - assertArrayEquals(expected, solution1.pourWater(heights, 2, 2)); - } - - @Test - public void test3() { - heights = new int[] {3, 1, 3}; - expected = new int[] {4, 4, 4}; - assertArrayEquals(expected, solution1.pourWater(heights, 5, 1)); - } - - @Test - public void test4() { - heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - expected = new int[] {1, 2, 3, 4, 3, 3, 2, 2, 3, 4, 3, 2, 1}; - assertArrayEquals(expected, solution1.pourWater(heights, 2, 5)); - } - - @Test - public void test5() { - heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - expected = new int[] {3, 4, 4, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - assertArrayEquals(expected, solution1.pourWater(heights, 5, 2)); - } - - @Test - public void test6() { - heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - expected = new int[] {4, 4, 4, 4, 3, 3, 3, 3, 3, 4, 3, 2, 1}; - assertArrayEquals(expected, solution1.pourWater(heights, 10, 2)); - } - - @Test - public void test7() { - heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - expected = new int[] {2, 3, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - assertArrayEquals(expected, solution1.pourWater(heights, 2, 2)); - } - - @Test - public void test8() { - heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - expected = new int[] {3, 3, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - assertArrayEquals(expected, solution1.pourWater(heights, 3, 2)); - } - - @Test - public void test9() { - heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - expected = new int[] {3, 3, 4, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - assertArrayEquals(expected, solution1.pourWater(heights, 4, 2)); - } - - @Test - public void test10() { - heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - expected = new int[] {3, 4, 4, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; - assertArrayEquals(expected, solution1.pourWater(heights, 5, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_756Test.java b/src/test/java/com/fishercoder/_756Test.java deleted file mode 100644 index d8dbf98253..0000000000 --- a/src/test/java/com/fishercoder/_756Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._756; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _756Test { - private static _756.Solution1 solution1; - private static List allowed; - - @BeforeClass - public static void setup() { - solution1 = new _756.Solution1(); - } - - @Test - public void test1() { - allowed = Arrays.asList("XYD", "YZE", "DEA", "FFF"); - assertEquals(true, solution1.pyramidTransition("XYZ", allowed)); - } - - @Test - public void test2() { - allowed = Arrays.asList("XXX", "XXY", "XYX", "XYY", "YXZ"); - assertEquals(false, solution1.pyramidTransition("XXYX", allowed)); - } - - @Test - public void test3() { - allowed = Arrays.asList("BCE", "BCF", "ABA", "CDA", "AEG", "FAG", "GGG"); - assertEquals(false, solution1.pyramidTransition("ABCD", allowed)); - } -} diff --git a/src/test/java/com/fishercoder/_758Test.java b/src/test/java/com/fishercoder/_758Test.java deleted file mode 100644 index 23656ce160..0000000000 --- a/src/test/java/com/fishercoder/_758Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._758; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _758Test { - private static _758.Solution1 solution1; - private static String[] words; - - @BeforeClass - public static void setup() { - solution1 = new _758.Solution1(); - } - - @Test - public void test1() { - words = new String[] {"ab", "bc"}; - assertEquals("aabcd", solution1.boldWords(words, "aabcd")); - } - - @Test - public void test2() { - words = new String[] {"ccb", "b", "d", "cba", "dc"}; - assertEquals("eeaadadadc", solution1.boldWords(words, "eeaadadadc")); - } -} diff --git a/src/test/java/com/fishercoder/_75Test.java b/src/test/java/com/fishercoder/_75Test.java deleted file mode 100644 index 35017a1050..0000000000 --- a/src/test/java/com/fishercoder/_75Test.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._75; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _75Test { - private static _75.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _75.Solution1(); - } - - @Test - public void test1() { - nums = new int[] {2, 2, 1}; - solution1.sortColors(nums); - assertArrayEquals(new int[] {1, 2, 2}, nums); - } - - @Test - public void test2() { - nums = new int[] {0, 1, 2, 0, 2, 1}; - solution1.sortColors(nums); - assertArrayEquals(new int[] {0, 0, 1, 1, 2, 2}, nums); - } - - @Test - public void test3() { - nums = new int[] {0}; - solution1.sortColors(nums); - assertArrayEquals(new int[] {0}, nums); - } - - @Test - public void test4() { - nums = new int[] {1, 0}; - solution1.sortColors(nums); - assertArrayEquals(new int[] {0, 1}, nums); - } - - @Test - public void test5() { - nums = new int[] {2}; - solution1.sortColors(nums); - assertArrayEquals(new int[] {2}, nums); - } -} diff --git a/src/test/java/com/fishercoder/_760Test.java b/src/test/java/com/fishercoder/_760Test.java deleted file mode 100644 index 8e5c93ffea..0000000000 --- a/src/test/java/com/fishercoder/_760Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._760; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _760Test { - private static _760.Solution1 solution1; - private static int[] A; - private static int[] B; - private static int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _760.Solution1(); - } - - @Test - public void test1() { - A = new int[] {12, 28, 46, 32, 50}; - B = new int[] {50, 12, 32, 46, 28}; - expected = new int[] {1, 4, 3, 2, 0}; - assertArrayEquals(expected, solution1.anagramMappings(A, B)); - } -} diff --git a/src/test/java/com/fishercoder/_762Test.java b/src/test/java/com/fishercoder/_762Test.java deleted file mode 100644 index dba0b50d5c..0000000000 --- a/src/test/java/com/fishercoder/_762Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._762; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _762Test { - private static _762.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _762.Solution1(); - } - - @Test - public void test1() { - assertEquals(4, solution1.countPrimeSetBits(6, 10)); - } -} diff --git a/src/test/java/com/fishercoder/_763Test.java b/src/test/java/com/fishercoder/_763Test.java deleted file mode 100644 index 54719a809c..0000000000 --- a/src/test/java/com/fishercoder/_763Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._763; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _763Test { - private static _763.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _763.Solution1(); - } - - @Test - public void test1() { - assertEquals(Arrays.asList(9, 7, 8), solution1.partitionLabels("ababcbacadefegdehijhklij")); - } - - @Test - public void test2() { - assertEquals(Arrays.asList(9, 7, 8), solution1.partitionLabels("ababcbacadefegdehijhklij")); - } -} diff --git a/src/test/java/com/fishercoder/_764Test.java b/src/test/java/com/fishercoder/_764Test.java deleted file mode 100644 index fb4ca787ed..0000000000 --- a/src/test/java/com/fishercoder/_764Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._764; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _764Test { - private static _764.Solution1 solution1; - private static _764.Solution2 solution2; - private static int[][] mines; - - @BeforeClass - public static void setup() { - solution1 = new _764.Solution1(); - solution2 = new _764.Solution2(); - } - - @Test - public void test1() { - mines = new int[][] {{0, 1}, {1, 0}, {1, 1}}; - assertEquals(1, solution1.orderOfLargestPlusSign(2, mines)); - assertEquals(1, solution2.orderOfLargestPlusSign(2, mines)); - } - - @Test - public void test2() { - mines = new int[][] {{4, 2}}; - assertEquals(2, solution1.orderOfLargestPlusSign(5, mines)); - assertEquals(2, solution2.orderOfLargestPlusSign(5, mines)); - } -} diff --git a/src/test/java/com/fishercoder/_765Test.java b/src/test/java/com/fishercoder/_765Test.java deleted file mode 100644 index 41103271ba..0000000000 --- a/src/test/java/com/fishercoder/_765Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._765; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _765Test { - private static _765.Solution1 solution1; - private static int[] row; - - @BeforeClass - public static void setup() { - solution1 = new _765.Solution1(); - } - - @Test - public void test1() { - row = new int[] {0, 2, 1, 3}; - assertEquals(1, solution1.minSwapsCouples(row)); - } - - @Test - public void test2() { - row = new int[] {3, 2, 0, 1}; - assertEquals(0, solution1.minSwapsCouples(row)); - } - - @Test - public void test3() { - row = new int[] {0, 4, 7, 3, 1, 5, 2, 8, 6, 9}; - assertEquals(3, solution1.minSwapsCouples(row)); - } - - @Test - public void test4() { - row = new int[] {5, 6, 4, 0, 2, 1, 9, 3, 8, 7, 11, 10}; - assertEquals(4, solution1.minSwapsCouples(row)); - } -} diff --git a/src/test/java/com/fishercoder/_766Test.java b/src/test/java/com/fishercoder/_766Test.java deleted file mode 100644 index 04132e754a..0000000000 --- a/src/test/java/com/fishercoder/_766Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._766; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _766Test { - private static _766.Solution1 solution1; - private static int[][] matrix; - - @BeforeClass - public static void setup() { - solution1 = new _766.Solution1(); - } - - @Test - public void test1() { - matrix = new int[][] { - {1, 2, 3, 4}, - {5, 1, 2, 3}, - {9, 5, 1, 2} - }; - assertEquals(true, solution1.isToeplitzMatrix(matrix)); - } - - @Test - public void test2() { - matrix = new int[][] { - {1, 2}, - {2, 2}, - }; - assertEquals(false, solution1.isToeplitzMatrix(matrix)); - } - - @Test - public void test3() { - matrix = new int[][] { - {1, 2, 3, 4, 5, 9}, - {5, 1, 2, 3, 4, 5}, - {9, 5, 1, 2, 3, 4} - }; - assertEquals(true, solution1.isToeplitzMatrix(matrix)); - } -} diff --git a/src/test/java/com/fishercoder/_767Test.java b/src/test/java/com/fishercoder/_767Test.java deleted file mode 100644 index 8201390ec1..0000000000 --- a/src/test/java/com/fishercoder/_767Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._767; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _767Test { - private static _767.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _767.Solution1(); - } - - @Test - public void test1() { - assertEquals("aba", solution1.reorganizeString("aab")); - } - - @Test - public void test2() { - assertEquals("", solution1.reorganizeString("aaab")); - } - - @Test - public void test3() { - assertEquals("bababab", solution1.reorganizeString("aaabbbb")); - } - - @Test - public void test4() { - assertEquals("vovlv", solution1.reorganizeString("vvvlo")); - } -} diff --git a/src/test/java/com/fishercoder/_769Test.java b/src/test/java/com/fishercoder/_769Test.java deleted file mode 100644 index 9c21d9afb9..0000000000 --- a/src/test/java/com/fishercoder/_769Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._769; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _769Test { - private static _769.Solution1 solution1; - private static _769.Solution2 solution2; - private static int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _769.Solution1(); - solution2 = new _769.Solution2(); - } - - @Test - public void test1() { - arr = new int[] {4, 3, 2, 1, 0}; - assertEquals(1, solution1.maxChunksToSorted(arr)); - assertEquals(1, solution2.maxChunksToSorted(arr)); - } - - @Test - public void test2() { - arr = new int[] {1, 0, 2, 3, 4}; - assertEquals(4, solution1.maxChunksToSorted(arr)); - assertEquals(4, solution2.maxChunksToSorted(arr)); - } -} diff --git a/src/test/java/com/fishercoder/_76Test.java b/src/test/java/com/fishercoder/_76Test.java deleted file mode 100644 index 75483576f1..0000000000 --- a/src/test/java/com/fishercoder/_76Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._76; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _76Test { - private static _76.Solution1 solution1; - private static int[] nums; - - @BeforeClass - public static void setup() { - solution1 = new _76.Solution1(); - } - - @Test - public void test1() { - assertEquals("BANC", solution1.minWindow("ADOBECODEBANC", "ABC")); - } -} diff --git a/src/test/java/com/fishercoder/_771Test.java b/src/test/java/com/fishercoder/_771Test.java deleted file mode 100644 index 3dd87926c1..0000000000 --- a/src/test/java/com/fishercoder/_771Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._771; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _771Test { - private static _771.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _771.Solution1(); - } - - @Test - public void test1() { - assertEquals(3, solution1.numJewelsInStones("aA", "aAAbbbb")); - } -} diff --git a/src/test/java/com/fishercoder/_776Test.java b/src/test/java/com/fishercoder/_776Test.java deleted file mode 100644 index c05fcddfe2..0000000000 --- a/src/test/java/com/fishercoder/_776Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._776; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _776Test { - private static _776.Solution1 solution1; - private static _776.Solution2 solution2; - private static TreeNode root; - private static TreeNode small; - private static TreeNode big; - - @BeforeClass - public static void setup() { - solution1 = new _776.Solution1(); - solution2 = new _776.Solution2(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 1, 3, 5, 7)); - small = TreeUtils.constructBinaryTree(Arrays.asList(2, 1)); - big = TreeUtils.constructBinaryTree(Arrays.asList(4, 3, 6, null, null, 5, 7)); - assertArrayEquals(new TreeNode[] {small, big}, solution1.splitBST(root, 2)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 1, 3, 5, 7)); - small = TreeUtils.constructBinaryTree(Arrays.asList(2, 1)); - big = TreeUtils.constructBinaryTree(Arrays.asList(4, 3, 6, null, null, 5, 7)); - assertArrayEquals(new TreeNode[] {small, big}, solution2.splitBST(root, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_779Test.java b/src/test/java/com/fishercoder/_779Test.java deleted file mode 100644 index 2e7e5d3bd5..0000000000 --- a/src/test/java/com/fishercoder/_779Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._779; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _779Test { - private static _779.Solution1 solution1; - private static _779.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _779.Solution1(); - solution2 = new _779.Solution2(); - } - - @Test - public void test1() { - assertEquals(0, solution1.kthGrammar(1, 1)); - assertEquals(0, solution2.kthGrammar(1, 1)); - } - - @Test - public void test2() { - assertEquals(0, solution1.kthGrammar(2, 1)); - assertEquals(0, solution2.kthGrammar(2, 1)); - } - - @Test - public void test3() { - assertEquals(1, solution1.kthGrammar(2, 2)); - assertEquals(1, solution2.kthGrammar(2, 2)); - } - - @Test - public void test4() { - assertEquals(1, solution1.kthGrammar(4, 5)); - assertEquals(1, solution2.kthGrammar(4, 5)); - } -} diff --git a/src/test/java/com/fishercoder/_77Test.java b/src/test/java/com/fishercoder/_77Test.java deleted file mode 100644 index a7f9b7cd4c..0000000000 --- a/src/test/java/com/fishercoder/_77Test.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._77; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _77Test { - private static _77.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _77.Solution1(); - } - - @Test - public void test1() { - CommonUtils.printListList(solution1.combine(4, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_783Test.java b/src/test/java/com/fishercoder/_783Test.java deleted file mode 100644 index af466bb7c6..0000000000 --- a/src/test/java/com/fishercoder/_783Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._783; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _783Test { - private static _783.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _783.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 1, 3, null, null)); - TreeUtils.printBinaryTree(root); - assertEquals(1, solution1.minDiffInBST(root)); - } -} diff --git a/src/test/java/com/fishercoder/_784Test.java b/src/test/java/com/fishercoder/_784Test.java deleted file mode 100644 index 06208347a0..0000000000 --- a/src/test/java/com/fishercoder/_784Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._784; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _784Test { - private static _784.Solution1 solution1; - private static List expected; - - @BeforeClass - public static void setup() { - solution1 = new _784.Solution1(); - } - - @Test - public void test1() { - expected = Arrays.asList("a1b2", "a1B2", "A1b2", "A1B2"); - assertEquals(expected, solution1.letterCasePermutation("a1b2")); - } - - @Test - public void test2() { - expected = Arrays.asList("3z4", "3Z4"); - assertEquals(expected, solution1.letterCasePermutation("3z4")); - } - - @Test - public void test3() { - expected = Arrays.asList("12345"); - assertEquals(expected, solution1.letterCasePermutation("12345")); - } -} diff --git a/src/test/java/com/fishercoder/_788Test.java b/src/test/java/com/fishercoder/_788Test.java deleted file mode 100644 index 302950c001..0000000000 --- a/src/test/java/com/fishercoder/_788Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._788; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _788Test { - private static _788.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _788.Solution1(); - } - - @Test - public void test1() { - assertEquals(4, solution1.rotatedDigits(10)); - } -} diff --git a/src/test/java/com/fishercoder/_789Test.java b/src/test/java/com/fishercoder/_789Test.java deleted file mode 100644 index 057798e066..0000000000 --- a/src/test/java/com/fishercoder/_789Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._789; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Created by varunu28 on 1/01/19. - */ - -public class _789Test { - private static _789.Solution test; - - @BeforeClass - public static void setup() { - test = new _789.Solution(); - } - - - @Test - public void test1() { - assertEquals(true, test.escapeGhosts(new int[][]{{1, 0}, {0, 3}}, new int[]{0, 1})); - } - - @Test - public void test2() { - assertEquals(false, test.escapeGhosts(new int[][]{{1, 0}}, new int[]{2, 0})); - } - - @Test - public void test3() { - assertEquals(false, test.escapeGhosts(new int[][]{{2, 0}}, new int[]{1, 0})); - } -} diff --git a/src/test/java/com/fishercoder/_78Test.java b/src/test/java/com/fishercoder/_78Test.java deleted file mode 100644 index 6b2264c284..0000000000 --- a/src/test/java/com/fishercoder/_78Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._78; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _78Test { - private static _78.Solution1 solution1; - private static _78.Solution2 solution2; - private static _78.Solution3 solution3; - - @BeforeClass - public static void setup() { - solution1 = new _78.Solution1(); - solution2 = new _78.Solution2(); - solution3 = new _78.Solution3(); - } - - @Test - public void test1() { - CommonUtils.printListList(solution1.subsets(new int[]{1, 2, 3})); - } - - @Test - public void test2() { - CommonUtils.printListList(solution2.subsets(new int[]{1, 2, 3})); - } - - @Test - public void test3() { - CommonUtils.printListList(solution3.subsets(new int[]{1, 2, 3})); - } -} diff --git a/src/test/java/com/fishercoder/_791Test.java b/src/test/java/com/fishercoder/_791Test.java deleted file mode 100644 index dd750b2916..0000000000 --- a/src/test/java/com/fishercoder/_791Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._791; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _791Test { - private static _791.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _791.Solution1(); - } - - @Test - public void test1() { - assertEquals("cbad", solution1.customSortString("cba", "abcd")); - } -} diff --git a/src/test/java/com/fishercoder/_796Test.java b/src/test/java/com/fishercoder/_796Test.java deleted file mode 100644 index 6b759fe5e8..0000000000 --- a/src/test/java/com/fishercoder/_796Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._796; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _796Test { - private static _796.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _796.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.rotateString("abcde", "cdeab")); - } - - @Test - public void test2() { - assertEquals(false, solution1.rotateString("abcde", "abced")); - } -} diff --git a/src/test/java/com/fishercoder/_799Test.java b/src/test/java/com/fishercoder/_799Test.java deleted file mode 100644 index 7181186305..0000000000 --- a/src/test/java/com/fishercoder/_799Test.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._799; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _799Test { - private static _799.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _799.Solution1(); - } - - @Test - public void test1() { - assertEquals(0.125, solution1.champagneTower(8, 3, 0), 0); - } - - @Test - public void test2() { - assertEquals(0.875, solution1.champagneTower(8, 3, 1), 0); - } - - @Test - public void test3() { - assertEquals(0.875, solution1.champagneTower(8, 3, 2), 0); - } - - @Test - public void test4() { - assertEquals(0.125, solution1.champagneTower(8, 3, 3), 0); - } - - @Test - public void test5() { - assertEquals(0.0, solution1.champagneTower(1, 1, 1), 0); - } - - @Test - public void test6() { - assertEquals(0.5, solution1.champagneTower(2, 1, 1), 0); - } - - @Test - public void test7() { - assertEquals(0.0, solution1.champagneTower(1000000000, 99, 99), 0); - } -} diff --git a/src/test/java/com/fishercoder/_79Test.java b/src/test/java/com/fishercoder/_79Test.java deleted file mode 100644 index cceba97871..0000000000 --- a/src/test/java/com/fishercoder/_79Test.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._79; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _79Test { - private static _79.Solution1 solution1; - private static _79.Solution2 solution2; - private static char[][] board; - - @BeforeClass - public static void setup() { - solution1 = new _79.Solution1(); - solution2 = new _79.Solution2(); - } - - @Test - public void test1() { - board = new char[][]{ - {'A', 'B', 'C', 'E'}, - {'S', 'F', 'E', 'S'}, - {'A', 'D', 'E', 'E'}, - }; - assertEquals(true, solution1.exist(board, "ABCEFSADEESE")); - assertEquals(true, solution2.exist(board, "ABCEFSADEESE")); - } - - @Test - public void test2() { - board = new char[][]{ - {'A', 'B', 'C', 'E'}, - {'S', 'F', 'C', 'S'}, - {'A', 'D', 'E', 'E'}, - }; - assertEquals(true, solution1.exist(board, "ABCCED")); - assertEquals(true, solution2.exist(board, "ABCCED")); - - assertEquals(true, solution1.exist(board, "SEE")); - assertEquals(true, solution2.exist(board, "SEE")); - - assertEquals(false, solution1.exist(board, "ABCD")); - assertEquals(false, solution2.exist(board, "ABCD")); - } - - @Test - public void test3() { - board = new char[][]{ - {'a'}, - {'a'}, - }; - assertEquals(false, solution1.exist(board, "aaa")); - assertEquals(false, solution2.exist(board, "aaa")); - } - -} diff --git a/src/test/java/com/fishercoder/_7Test.java b/src/test/java/com/fishercoder/_7Test.java deleted file mode 100644 index b607cbd98b..0000000000 --- a/src/test/java/com/fishercoder/_7Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._7; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _7Test { - private static _7.Solution1 solution1; - private static _7.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _7.Solution1(); - solution2 = new _7.Solution2(); - } - - @Test - public void test1() { - /**its reversed number is greater than Integer.MAX_VALUE, thus return 0*/ - assertEquals(0, solution1.reverse(1534236469)); - System.out.println(Integer.MAX_VALUE); - System.out.println(1534236469); - System.out.println(Integer.MAX_VALUE - 1534236469); - } - - @Test - public void test2() { - assertEquals(0, solution2.reverse(1534236469)); - } - - @Test - public void test3() { - assertEquals(-123, solution2.reverse(-321)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_800Test.java b/src/test/java/com/fishercoder/_800Test.java deleted file mode 100644 index 80a707aa30..0000000000 --- a/src/test/java/com/fishercoder/_800Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._800; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _800Test { - private static _800.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _800.Solution1(); - } - - @Test - public void test1() { - assertEquals("#11ee66", solution1.similarRGB("#09f166")); - } -} diff --git a/src/test/java/com/fishercoder/_804Test.java b/src/test/java/com/fishercoder/_804Test.java deleted file mode 100644 index f1cd458321..0000000000 --- a/src/test/java/com/fishercoder/_804Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._804; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _804Test { - private static _804.Solution1 solution1; - private static String[] words; - - @BeforeClass - public static void setup() { - solution1 = new _804.Solution1(); - } - - @Test - public void test1() { - words = new String[] {"gin", "zen", "gig", "msg"}; - assertEquals(2, solution1.uniqueMorseRepresentations(words)); - } -} diff --git a/src/test/java/com/fishercoder/_806Test.java b/src/test/java/com/fishercoder/_806Test.java deleted file mode 100644 index 7a358d1f62..0000000000 --- a/src/test/java/com/fishercoder/_806Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._806; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _806Test { - private static _806.Solution1 solution1; - private static int[] widths; - - @BeforeClass - public static void setup() { - solution1 = new _806.Solution1(); - } - - @Test - public void test1() { - widths = - new int[] {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10}; - assertArrayEquals(new int[] {3, 60}, solution1.numberOfLines(widths, "abcdefghijklmnopqrstuvwxyz")); - } - - @Test - public void test2() { - widths = - new int[] {4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10}; - assertArrayEquals(new int[] {2, 4}, solution1.numberOfLines(widths, "bbbcccdddaaa")); - } -} diff --git a/src/test/java/com/fishercoder/_807Test.java b/src/test/java/com/fishercoder/_807Test.java deleted file mode 100644 index a2c768cc84..0000000000 --- a/src/test/java/com/fishercoder/_807Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._807; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _807Test { - private static _807.Solution1 solution1; - private static int[][] grid; - - @BeforeClass - public static void setup() { - solution1 = new _807.Solution1(); - } - - @Test - public void test1() { - grid = new int[][]{ - {3, 0, 8, 4}, - {2, 4, 5, 7}, - {9, 2, 6, 3}, - {0, 3, 1, 0} - }; - assertEquals(35, solution1.maxIncreaseKeepingSkyline(grid)); - } - - -} diff --git a/src/test/java/com/fishercoder/_80Test.java b/src/test/java/com/fishercoder/_80Test.java deleted file mode 100644 index 6607567f64..0000000000 --- a/src/test/java/com/fishercoder/_80Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._80; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _80Test { - private static _80.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _80.Solution1(); - } - - @Test - public void test1() { - assertEquals(5, (solution1.removeDuplicates(new int[]{1, 1, 1, 2, 2, 3}))); - } - -} diff --git a/src/test/java/com/fishercoder/_811Test.java b/src/test/java/com/fishercoder/_811Test.java deleted file mode 100644 index 877612ef8e..0000000000 --- a/src/test/java/com/fishercoder/_811Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._811; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _811Test { - private static _811.Solution1 solution1; - private static String[] cpdomains; - - @BeforeClass - public static void setup() { - solution1 = new _811.Solution1(); - } - - @Test - public void test1() { - cpdomains = new String[] {"9001 discuss.leetcode.com"}; - CommonUtils.print(solution1.subdomainVisits(cpdomains)); - } - - @Test - public void test2() { - cpdomains = - new String[] {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"}; - CommonUtils.print(solution1.subdomainVisits(cpdomains)); - } -} diff --git a/src/test/java/com/fishercoder/_819Test.java b/src/test/java/com/fishercoder/_819Test.java deleted file mode 100644 index cf22972295..0000000000 --- a/src/test/java/com/fishercoder/_819Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._819; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _819Test { - private static _819.Solution1 solution1; - private static String[] banned; - - @BeforeClass - public static void setup() { - solution1 = new _819.Solution1(); - } - - @Test - public void test1() { - banned = new String[] {"hit"}; - assertEquals("ball", - solution1.mostCommonWord("Bob hit a ball, the hit BALL flew far after it was hit.", - banned)); - } -} diff --git a/src/test/java/com/fishercoder/_81Test.java b/src/test/java/com/fishercoder/_81Test.java deleted file mode 100644 index 69dd4d3f52..0000000000 --- a/src/test/java/com/fishercoder/_81Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._81; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _81Test { - private static _81.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _81.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, (solution1.search(new int[]{2, 5, 6, 0, 0, 1, 2}, 0))); - } - - @Test - public void test2() { - assertEquals(false, (solution1.search(new int[]{2, 5, 6, 0, 0, 1, 2}, 3))); - } - -} diff --git a/src/test/java/com/fishercoder/_821Test.java b/src/test/java/com/fishercoder/_821Test.java deleted file mode 100644 index 3bb4045baf..0000000000 --- a/src/test/java/com/fishercoder/_821Test.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._821; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _821Test { - private static _821.Solution1 solution1; - private static int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _821.Solution1(); - } - - @Test - public void test1() { - expected = new int[] {3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0}; - assertArrayEquals(expected, solution1.shortestToChar("loveleetcode", 'e')); - } -} diff --git a/src/test/java/com/fishercoder/_824Test.java b/src/test/java/com/fishercoder/_824Test.java deleted file mode 100644 index 5bd98cc211..0000000000 --- a/src/test/java/com/fishercoder/_824Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._824; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _824Test { - private static _824.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _824.Solution1(); - } - - @Test - public void test1() { - assertEquals("Imaa peaksmaaa oatGmaaaa atinLmaaaaa", - solution1.toGoatLatin("I speak Goat Latin")); - } - - @Test - public void test2() { - assertEquals( - "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa", - solution1.toGoatLatin("The quick brown fox jumped over the lazy dog")); - } -} diff --git a/src/test/java/com/fishercoder/_82Test.java b/src/test/java/com/fishercoder/_82Test.java deleted file mode 100644 index c71f202166..0000000000 --- a/src/test/java/com/fishercoder/_82Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.common.utils.LinkedListUtils; -import com.fishercoder.solutions._82; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _82Test { - - private static _82.Solution1 solution1; - private static ListNode head; - private static ListNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _82.Solution1(); - } - - @Test - public void test1() { - head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 3, 4, 4, 5}); - expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 5}); - Assert.assertEquals(expected, solution1.deleteDuplicates(head)); - } - - @Test - public void test2() { - head = LinkedListUtils.contructLinkedList(new int[] {1, 1, 1, 2, 3}); - expected = LinkedListUtils.contructLinkedList(new int[] {2, 3}); - Assert.assertEquals(expected, solution1.deleteDuplicates(head)); - } -} diff --git a/src/test/java/com/fishercoder/_830Test.java b/src/test/java/com/fishercoder/_830Test.java deleted file mode 100644 index 80e9e7de85..0000000000 --- a/src/test/java/com/fishercoder/_830Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._830; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _830Test { - private static _830.Solution1 solution1; - private static List> expected; - - @BeforeClass - public static void setup() { - solution1 = new _830.Solution1(); - } - - @Test - public void test1() { - expected = new ArrayList<>(); - expected.add(Arrays.asList(3, 6)); - assertEquals(expected, solution1.largeGroupPositions("abbxxxxzzy")); - } - - @Test - public void test2() { - expected = new ArrayList<>(); - assertEquals(expected, solution1.largeGroupPositions("abc")); - } - - @Test - public void test3() { - expected = new ArrayList<>(); - expected.add(Arrays.asList(3, 5)); - expected.add(Arrays.asList(6, 9)); - expected.add(Arrays.asList(12, 14)); - assertEquals(expected, solution1.largeGroupPositions("abcdddeeeeaabbbcd")); - } -} diff --git a/src/test/java/com/fishercoder/_832Test.java b/src/test/java/com/fishercoder/_832Test.java deleted file mode 100644 index 5f761d7de8..0000000000 --- a/src/test/java/com/fishercoder/_832Test.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._832; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _832Test { - private static _832.Solution1 solution1; - private static int[][] expected; - private static int[][] A; - - @BeforeClass - public static void setup() { - solution1 = new _832.Solution1(); - } - - @Test - public void test1() { - A = new int[][] { - {1, 1, 0}, - {1, 0, 1}, - {0, 0, 0} - }; - expected = new int[][] { - {1, 0, 0}, - {0, 1, 0}, - {1, 1, 1} - }; - assertArrayEquals(expected, solution1.flipAndInvertImage(A)); - } - - @Test - public void test2() { - A = new int[][] { - {1, 1, 0, 0}, - {1, 0, 0, 1}, - {0, 1, 1, 1}, - {1, 0, 1, 0} - }; - expected = new int[][] { - {1, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 1}, - {1, 0, 1, 0} - }; - assertArrayEquals(expected, solution1.flipAndInvertImage(A)); - } -} diff --git a/src/test/java/com/fishercoder/_836Test.java b/src/test/java/com/fishercoder/_836Test.java deleted file mode 100644 index d9064b5045..0000000000 --- a/src/test/java/com/fishercoder/_836Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._836; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _836Test { - private static _836.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _836.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isRectangleOverlap(new int[]{0, 0, 2, 2}, new int[]{1, 1, 3, 3})); - } - - @Test - public void test2() { - assertEquals(false, solution1.isRectangleOverlap(new int[]{0, 0, 1, 1}, new int[]{1, 0, 2, 1})); - } -} diff --git a/src/test/java/com/fishercoder/_83Test.java b/src/test/java/com/fishercoder/_83Test.java deleted file mode 100644 index dc9d6528b3..0000000000 --- a/src/test/java/com/fishercoder/_83Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.solutions._83; -import java.util.Arrays; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Created by fishercoder on 4/18/17. - */ -public class _83Test { - - private static _83.Solution1 solution1; - private static _83.Solution2 solution2; - private static ListNode head; - private static ListNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _83.Solution1(); - solution2 = new _83.Solution2(); - } - - @Test - public void test1() { - head = ListNode.createSinglyLinkedList(Arrays.asList(1, 1, 2, 3, 3)); - expected = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3)); - Assert.assertEquals(expected, solution1.deleteDuplicates(head)); - } - - @Test - public void test2() { - head = ListNode.createSinglyLinkedList(Arrays.asList(1, 1, 2, 3, 3)); - expected = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3)); - Assert.assertEquals(expected, solution2.deleteDuplicates(head)); - } -} diff --git a/src/test/java/com/fishercoder/_840Test.java b/src/test/java/com/fishercoder/_840Test.java deleted file mode 100644 index 91769e12c0..0000000000 --- a/src/test/java/com/fishercoder/_840Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._840; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _840Test { - private static _840.Solution1 test; - private static int[][] grid; - - @BeforeClass - public static void setUp() { - test = new _840.Solution1(); - } - - @Test - public void test1() { - grid = new int[][]{ - {4,3,8,4}, - {9,5,1,9}, - {2,7,6,2} - }; - assertEquals(1, test.numMagicSquaresInside(grid)); - } - - @Test - public void test2() { - grid = new int[][]{ - {5,5,5}, - {5,5,5}, - {5,5,5} - }; - assertEquals(0, test.numMagicSquaresInside(grid)); - } - - @Test - public void test3() { - grid = new int[][]{ - {10,3,5}, - {1,6,11}, - {7,9,2} - }; - assertEquals(0, test.numMagicSquaresInside(grid)); - } - -} diff --git a/src/test/java/com/fishercoder/_841Test.java b/src/test/java/com/fishercoder/_841Test.java deleted file mode 100644 index 9ba364f26c..0000000000 --- a/src/test/java/com/fishercoder/_841Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._841; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class _841Test { - private static _841.Solution1 solution1; - private static List> rooms; - - @BeforeClass - public static void setup() { - solution1 = new _841.Solution1(); - } - - @Test - public void test1() { - rooms = new ArrayList<>(); - rooms.add(Arrays.asList(1)); - rooms.add(Arrays.asList(2)); - rooms.add(Arrays.asList(3)); - rooms.add(Arrays.asList()); - assertEquals(true, solution1.canVisitAllRooms(rooms)); - } - - @Test - public void test2() { - rooms = new ArrayList<>(); - rooms.add(Arrays.asList(1, 3)); - rooms.add(Arrays.asList(3, 0, 1)); - rooms.add(Arrays.asList(2)); - rooms.add(Arrays.asList(0)); - assertEquals(false, solution1.canVisitAllRooms(rooms)); - } -} diff --git a/src/test/java/com/fishercoder/_844Test.java b/src/test/java/com/fishercoder/_844Test.java deleted file mode 100644 index 5c8ec2d54a..0000000000 --- a/src/test/java/com/fishercoder/_844Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._844; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _844Test { - private static _844.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _844.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.backspaceCompare("ab#c", "ad#c")); - } - - @Test - public void test2() { - assertEquals(true, solution1.backspaceCompare("ab##", "c#d#")); - } - - @Test - public void test3() { - assertEquals(true, solution1.backspaceCompare("a##c", "#a#c")); - } - - @Test - public void test4() { - assertEquals(false, solution1.backspaceCompare("a#c", "b")); - } - -} diff --git a/src/test/java/com/fishercoder/_849Test.java b/src/test/java/com/fishercoder/_849Test.java deleted file mode 100644 index 579f32777e..0000000000 --- a/src/test/java/com/fishercoder/_849Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._849; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _849Test { - - private static _849.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _849.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.maxDistToClosest(new int[]{1, 0, 0, 0, 1, 0, 1})); - } - - @Test - public void test2() { - assertEquals(3, solution1.maxDistToClosest(new int[]{1, 0, 0, 0})); - } -} diff --git a/src/test/java/com/fishercoder/_84Test.java b/src/test/java/com/fishercoder/_84Test.java deleted file mode 100644 index 4b2c962ce6..0000000000 --- a/src/test/java/com/fishercoder/_84Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._84; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _84Test { - private static _84.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _84.Solution1(); - } - - @Test - public void test1() { - assertEquals(10, (solution1.largestRectangleArea(new int[]{2, 1, 5, 6, 2, 3}))); - } - -} diff --git a/src/test/java/com/fishercoder/_852Test.java b/src/test/java/com/fishercoder/_852Test.java deleted file mode 100644 index 687c4f56e0..0000000000 --- a/src/test/java/com/fishercoder/_852Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._852; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _852Test { - private static _852.Solution1 solution1; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _852.Solution1(); - } - - @Test - public void test1() { - A = new int[]{0, 1, 0}; - assertEquals(1, solution1.peakIndexInMountainArray(A)); - } - - @Test - public void test2() { - A = new int[]{0, 2, 1, 0}; - assertEquals(1, solution1.peakIndexInMountainArray(A)); - } -} diff --git a/src/test/java/com/fishercoder/_859Test.java b/src/test/java/com/fishercoder/_859Test.java deleted file mode 100644 index f4b157019b..0000000000 --- a/src/test/java/com/fishercoder/_859Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._859; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _859Test { - private static _859.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _859.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.buddyStrings("ab", "ba")); - } - - @Test - public void test2() { - assertEquals(false, solution1.buddyStrings("ab", "ab")); - } - - @Test - public void test3() { - assertEquals(true, solution1.buddyStrings("aa", "aa")); - } - - @Test - public void test4() { - assertEquals(true, solution1.buddyStrings("aaaaaaabc", "aaaaaaacb")); - } - - @Test - public void test5() { - assertEquals(false, solution1.buddyStrings("", "aa")); - } - - @Test - public void test6() { - assertEquals(true, solution1.buddyStrings("aaa", "aaa")); - } -} diff --git a/src/test/java/com/fishercoder/_85Test.java b/src/test/java/com/fishercoder/_85Test.java deleted file mode 100644 index 8c8707cb74..0000000000 --- a/src/test/java/com/fishercoder/_85Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._85; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _85Test { - private static _85.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _85.Solution1(); - } - - @Test - public void test1() { - assertEquals(6, (solution1.maximalRectangle(new char[][]{ - {'1', '0', '1', '0', '0'}, - {'1', '0', '1', '1', '1'}, - {'1', '1', '1', '1', '1'}, - {'1', '0', '0', '1', '0'} - }))); - } - -} diff --git a/src/test/java/com/fishercoder/_860Test.java b/src/test/java/com/fishercoder/_860Test.java deleted file mode 100644 index 668546338f..0000000000 --- a/src/test/java/com/fishercoder/_860Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._860; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _860Test { - - private static _860.Solution1 test; - private static int[] bills; - - @BeforeClass - public static void setUp() { - test = new _860.Solution1(); - } - - @Test - public void test1() { - bills = new int[] {5, 5, 5, 10, 20}; - assertEquals(true, test.lemonadeChange(bills)); - } - - @Test - public void test2() { - bills = new int[] {5, 5, 10}; - assertEquals(true, test.lemonadeChange(bills)); - } - - @Test - public void test3() { - bills = new int[] {10, 10}; - assertEquals(false, test.lemonadeChange(bills)); - } - - @Test - public void test4() { - bills = new int[] {5, 5, 10, 10, 20}; - assertEquals(false, test.lemonadeChange(bills)); - } - - @Test - public void test5() { - bills = new int[] {5, 5, 5, 20, 5, 5, 5, 10, 20, 5, 10, 20, 5, 20, 5, 10, 5, 5, 5, 5}; - assertEquals(false, test.lemonadeChange(bills)); - } -} diff --git a/src/test/java/com/fishercoder/_867Test.java b/src/test/java/com/fishercoder/_867Test.java deleted file mode 100644 index a97c471d1b..0000000000 --- a/src/test/java/com/fishercoder/_867Test.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._867; -import com.fishercoder.solutions._9; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; - -public class _867Test { - private static _867.Solution1 solution1; - private static int[][] A; - private static int[][] result; - - @BeforeClass - public static void setup() { - solution1 = new _867.Solution1(); - } - - @Test - public void test1() { - A = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9} - }; - result = new int[][]{ - {1, 4, 7}, - {2, 5, 8}, - {3, 6, 9} - }; - assertArrayEquals(result, solution1.transpose(A)); - } - - @Test - public void test2() { - A = new int[][]{ - {1, 2, 3} - }; - result = new int[][]{ - {1}, - {2}, - {3} - }; - assertArrayEquals(result, solution1.transpose(A)); - } - - @Test - public void test3() { - A = new int[][]{ - {1}, - {2}, - {3} - }; - result = new int[][]{ - {1, 2, 3} - }; - assertArrayEquals(result, solution1.transpose(A)); - } - - @Test - public void test4() { - A = new int[][]{ - {1, 2, 3, 4}, - {5, 6, 7, 8}, - {9, 10, 11, 12} - }; - result = new int[][]{ - {1, 5, 9}, - {2, 6, 10}, - {3, 7, 11}, - {4, 8, 12} - }; - assertArrayEquals(result, solution1.transpose(A)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_868Test.java b/src/test/java/com/fishercoder/_868Test.java deleted file mode 100644 index 60fee4a30c..0000000000 --- a/src/test/java/com/fishercoder/_868Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._868; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _868Test { - private static _868.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _868.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.binaryGap(22)); - } - - @Test - public void test2() { - assertEquals(2, solution1.binaryGap(5)); - } - - @Test - public void test3() { - assertEquals(1, solution1.binaryGap(6)); - } - - @Test - public void test4() { - assertEquals(0, solution1.binaryGap(8)); - } -} diff --git a/src/test/java/com/fishercoder/_86Test.java b/src/test/java/com/fishercoder/_86Test.java deleted file mode 100644 index cd33cdba41..0000000000 --- a/src/test/java/com/fishercoder/_86Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.solutions._86; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _86Test { - private static _86.Solution1 solution1; - private static ListNode head; - private static ListNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _86.Solution1(); - } - - @Test - public void test1() { - head = ListNode.createSinglyLinkedList(Arrays.asList(1, 4, 3, 2, 5, 2)); - expected = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 2, 4, 3, 5)); - assertEquals(expected, (solution1.partition(head, 3))); - } - -} diff --git a/src/test/java/com/fishercoder/_872Test.java b/src/test/java/com/fishercoder/_872Test.java deleted file mode 100644 index 23eb79c18d..0000000000 --- a/src/test/java/com/fishercoder/_872Test.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._872; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _872Test { - private static _872.Solution1 solution1; - private static TreeNode root1; - private static TreeNode root2; - - @BeforeClass - public static void setup() { - solution1 = new _872.Solution1(); - } - - @Test - public void test1() { - root1 = TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 6, 2, 7, 4, 1, 9, 8)); - root2 = TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 6, 2, 7, 4, 1, 9, 8)); - TreeUtils.printBinaryTree(root1); - TreeUtils.printBinaryTree(root2); - assertEquals(true, solution1.leafSimilar(root1, root2)); - } - - @Test - public void test2() { - root1 = - TreeUtils.constructBinaryTree(Arrays.asList(18, 35, 22, null, 103, 43, 101, 58, null, 97)); - TreeUtils.printBinaryTree(root1); - root2 = - TreeUtils.constructBinaryTree(Arrays.asList(94, 102, 17, 122, null, null, 54, 58, 101, 97)); - TreeUtils.printBinaryTree(root2); - assertEquals(false, solution1.leafSimilar(root1, root2)); - } -} diff --git a/src/test/java/com/fishercoder/_876Test.java b/src/test/java/com/fishercoder/_876Test.java deleted file mode 100644 index fc0074baec..0000000000 --- a/src/test/java/com/fishercoder/_876Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.solutions._325; -import com.fishercoder.solutions._876; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _876Test { - private static _876.Solution1 solution1; - private static ListNode head; - private static ListNode middle; - - @BeforeClass - public static void setup() { - solution1 = new _876.Solution1(); - } - - @Test - public void test1() { - head = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5)); - middle = solution1.middleNode(head); - assertEquals(middle, ListNode.createSinglyLinkedList(Arrays.asList(3, 4, 5))); - } - - @Test - public void test2() { - head = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5, 6)); - middle = solution1.middleNode(head); - assertEquals(middle, ListNode.createSinglyLinkedList(Arrays.asList(4, 5, 6))); - } -} diff --git a/src/test/java/com/fishercoder/_87Test.java b/src/test/java/com/fishercoder/_87Test.java deleted file mode 100644 index 5dff4bd391..0000000000 --- a/src/test/java/com/fishercoder/_87Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._87; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _87Test { - private static _87.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _87.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isScramble("great", "rgeat")); - } - - @Test - public void test2() { - assertEquals(true, solution1.isScramble("great", "rgtae")); - } - - @Test - public void test3() { - assertEquals(true, solution1.isScramble("abc", "bca")); - } -} diff --git a/src/test/java/com/fishercoder/_880Test.java b/src/test/java/com/fishercoder/_880Test.java deleted file mode 100644 index 674c1fa11e..0000000000 --- a/src/test/java/com/fishercoder/_880Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._880; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -@Ignore -public class _880Test { - private static _880.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _880.Solution1(); - } - - @Test - public void test1() { - assertEquals("o", solution1.decodeAtIndex("leet2code3", 10)); - } - - @Test - public void test2() { - assertEquals("h", solution1.decodeAtIndex("ha22", 5)); - } - - @Test - public void test3() { - assertEquals("a", solution1.decodeAtIndex("a2345678999999999999999", 1)); - } - - @Test - public void test4() { - assertEquals("a", solution1.decodeAtIndex("abc", 1)); - } - - @Test - public void test5() { - assertEquals("a", solution1.decodeAtIndex("a2b3c4d5e6f7g8h9", 10)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_883Test.java b/src/test/java/com/fishercoder/_883Test.java deleted file mode 100644 index 0b057270a9..0000000000 --- a/src/test/java/com/fishercoder/_883Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._883; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _883Test { - private static _883.Solution1 solution1; - private static int[][] grid; - - @BeforeClass - public static void setup() { - solution1 = new _883.Solution1(); - } - - @Test - public void test1() { - grid = new int[][]{ - {2} - }; - assertEquals(5, solution1.projectionArea(grid)); - } - - @Test - public void test2() { - grid = new int[][]{ - {1, 2}, - {3, 4}, - }; - assertEquals(17, solution1.projectionArea(grid)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_884Test.java b/src/test/java/com/fishercoder/_884Test.java deleted file mode 100644 index b4d3973a09..0000000000 --- a/src/test/java/com/fishercoder/_884Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._884; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _884Test { - private static _884.Solution1 solution1; - private static String[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _884.Solution1(); - } - - @Test - public void test1() { - expected = new String[] {"sweet", "sour"}; - assertArrayEquals(expected, - solution1.uncommonFromSentences("this apple is sweet", "this apple is sour")); - } - - @Test - public void test2() { - expected = new String[] {"banana"}; - assertArrayEquals(expected, - solution1.uncommonFromSentences("apple apple", "banana")); - } -} diff --git a/src/test/java/com/fishercoder/_885Test.java b/src/test/java/com/fishercoder/_885Test.java deleted file mode 100644 index effb203320..0000000000 --- a/src/test/java/com/fishercoder/_885Test.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._885; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _885Test { - private static _885.Solution1 solution1; - private static int[][] expected; - - @BeforeClass - public static void setup() { - solution1 = new _885.Solution1(); - } - - @Test - public void test1() { - expected = new int[][]{ - {0, 0}, - {0, 1}, - {0, 2}, - {0, 3} - }; - assertArrayEquals(expected, solution1.spiralMatrixIII(1, 4, 0, 0)); - } - - @Test - public void test2() { - expected = new int[][]{ - {1, 4}, - {1, 5}, - {2, 5}, - {2, 4}, - {2, 3}, - {1, 3}, - {0, 3}, - {0, 4}, - {0, 5}, - {3, 5}, - {3, 4}, - {3, 3}, - {3, 2}, - {2, 2}, - {1, 2}, - {0, 2}, - {4, 5}, - {4, 4}, - {4, 3}, - {4, 2}, - {4, 1}, - {3, 1}, - {2, 1}, - {1, 1}, - {0, 1}, - {4, 0}, - {3, 0}, - {2, 0}, - {1, 0}, - {0, 0} - }; - assertArrayEquals(expected, solution1.spiralMatrixIII(5, 6, 1, 4)); - } - -} diff --git a/src/test/java/com/fishercoder/_888Test.java b/src/test/java/com/fishercoder/_888Test.java deleted file mode 100644 index 28f6ab7ff4..0000000000 --- a/src/test/java/com/fishercoder/_888Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._888; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _888Test { - private static _888.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _888.Solution1(); - } - - @Test - public void test1() { - int [] A = {1, 1}; - int [] B = {2, 2}; - int [] ans = {1, 2}; - assertArrayEquals(ans, solution1.fairCandySwap(A, B)); - } - - @Test - public void test2() { - int [] A = {1, 2}; - int [] B = {2, 3}; - int [] ans = {1, 2}; - assertArrayEquals(ans, solution1.fairCandySwap(A, B)); - } - - @Test - public void test3() { - int [] A = {2}; - int [] B = {1, 3}; - int [] ans = {2, 3}; - assertArrayEquals(ans, solution1.fairCandySwap(A, B)); - } - - @Test - public void test4() { - int [] A = {1, 2, 5}; - int [] B = {2, 4}; - int [] ans = {5, 4}; - assertArrayEquals(ans, solution1.fairCandySwap(A, B)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_88Test.java b/src/test/java/com/fishercoder/_88Test.java deleted file mode 100644 index 8e3f60f109..0000000000 --- a/src/test/java/com/fishercoder/_88Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._88; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _88Test { - - private static _88.Solution1 solution1; - private int[] nums1; - private int[] nums2; - private int[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _88.Solution1(); - } - - @Test - public void test1() { - nums1 = new int[] {2, 0}; - nums2 = new int[] {1}; - expected = new int[] {1, 2}; - solution1.merge(nums1, 1, nums2, 1); - assertArrayEquals(expected, nums1); - } - - @Test - public void test2() { - nums1 = new int[] {4, 5, 6, 0, 0, 0}; - nums2 = new int[] {1, 2, 3}; - expected = new int[] {1, 2, 3, 4, 5, 6}; - solution1.merge(nums1, 3, nums2, 3); - assertArrayEquals(expected, nums1); - } -} diff --git a/src/test/java/com/fishercoder/_890Test.java b/src/test/java/com/fishercoder/_890Test.java deleted file mode 100644 index 8d0d27b4f5..0000000000 --- a/src/test/java/com/fishercoder/_890Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._890; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _890Test { - private static _890.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _890.Solution1(); - } - - @Test - public void test1() { - assertEquals(Arrays.asList("mee", "aqq"), - solution1.findAndReplacePattern(new String[] {"abc", "deq", "mee", "aqq", "dkd", "ccc"}, - "abb")); - } -} diff --git a/src/test/java/com/fishercoder/_893Test.java b/src/test/java/com/fishercoder/_893Test.java deleted file mode 100644 index d491ac0bd1..0000000000 --- a/src/test/java/com/fishercoder/_893Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._893; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _893Test { - private static _893.Solution1 solution1; - private static _893.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _893.Solution1(); - solution2 = new _893.Solution2(); - } - - @Test - public void test1() { - assertEquals(3, solution1.numSpecialEquivGroups(new String[]{"abcd", "cdab", "cbad", "xyzz", "zzxy", "zzyx"})); - assertEquals(3, solution2.numSpecialEquivGroups(new String[]{"abcd", "cdab", "cbad", "xyzz", "zzxy", "zzyx"})); - } - - @Test - public void test2() { - assertEquals(3, solution1.numSpecialEquivGroups(new String[]{"abc", "acb", "bac", "bca", "cab", "cba"})); - assertEquals(3, solution2.numSpecialEquivGroups(new String[]{"abc", "acb", "bac", "bca", "cab", "cba"})); - } - - @Test - public void test3() { - assertEquals(1, solution1.numSpecialEquivGroups(new String[]{"abcd", "cdab", "adcb", "cbad"})); - assertEquals(1, solution2.numSpecialEquivGroups(new String[]{"abcd", "cdab", "adcb", "cbad"})); - } - -} diff --git a/src/test/java/com/fishercoder/_896Test.java b/src/test/java/com/fishercoder/_896Test.java deleted file mode 100644 index 971920919c..0000000000 --- a/src/test/java/com/fishercoder/_896Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._896; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _896Test { - private static _896.Solution1 solution1; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _896.Solution1(); - } - - @Test - public void test1() { - A = new int[]{1, 3, 2}; - assertEquals(false, solution1.isMonotonic(A)); - } - -} diff --git a/src/test/java/com/fishercoder/_897Test.java b/src/test/java/com/fishercoder/_897Test.java deleted file mode 100644 index b311f6f903..0000000000 --- a/src/test/java/com/fishercoder/_897Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._897; -import java.util.Arrays; -import org.junit.Before; -import org.junit.Test; - -public class _897Test { - private static _897.Solution1 solution1; - private static TreeNode root; - private static TreeNode actual; - - @Before - public void setup() { - solution1 = new _897.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree( - Arrays.asList(5, 3, 6, 2, 4, null, 8, 1, null, null, null, 7, 9)); - TreeUtils.printBinaryTree(root); - actual = solution1.increasingBST(root); - TreeUtils.printBinaryTree(actual); - } -} diff --git a/src/test/java/com/fishercoder/_89Test.java b/src/test/java/com/fishercoder/_89Test.java deleted file mode 100644 index 6773111a58..0000000000 --- a/src/test/java/com/fishercoder/_89Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._89; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _89Test { - - private static _89.Solution1 solution1; - private static _89.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _89.Solution1(); - solution2 = new _89.Solution2(); - } - - @Test - public void test1() { - assertEquals(Arrays.asList(0, 1, 3, 2, 6, 7, 5, 4), solution1.grayCode(3)); - assertEquals(Arrays.asList(0, 1, 3, 2, 6, 7, 5, 4), solution2.grayCode(3)); - } -} diff --git a/src/test/java/com/fishercoder/_900Test.java b/src/test/java/com/fishercoder/_900Test.java deleted file mode 100644 index 430b5b4952..0000000000 --- a/src/test/java/com/fishercoder/_900Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._900; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _900Test { - private static _900.Solution1.RLEIterator rleIterator; - - @Test - public void test1() { - rleIterator = new _900.Solution1.RLEIterator(new int[] {3, 8, 0, 9, 2, 5}); - assertEquals(8, rleIterator.next(2)); - assertEquals(8, rleIterator.next(1)); - assertEquals(5, rleIterator.next(1)); - assertEquals(-1, rleIterator.next(2)); - } - - @Test - public void test2() { - rleIterator = new _900.Solution1.RLEIterator( - new int[] {811, 903, 310, 730, 899, 684, 472, 100, 434, 611}); - assertEquals(903, rleIterator.next(358)); - assertEquals(903, rleIterator.next(345)); - assertEquals(730, rleIterator.next(154)); - assertEquals(684, rleIterator.next(265)); - assertEquals(684, rleIterator.next(73)); - assertEquals(684, rleIterator.next(220)); - assertEquals(684, rleIterator.next(138)); - assertEquals(684, rleIterator.next(4)); - assertEquals(684, rleIterator.next(170)); - assertEquals(684, rleIterator.next(88)); - } -} diff --git a/src/test/java/com/fishercoder/_905Test.java b/src/test/java/com/fishercoder/_905Test.java deleted file mode 100644 index 34582e884b..0000000000 --- a/src/test/java/com/fishercoder/_905Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._905; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _905Test { - private static _905.Solution1 solution1; - private static int[] A; - private static int[] actual; - - @BeforeClass - public static void setup() { - solution1 = new _905.Solution1(); - } - - @Test - public void test1() { - A = new int[]{3, 1, 2, 4}; - actual = solution1.sortArrayByParity(A); - CommonUtils.printArray(actual); - } - - @Test - public void test2() { - A = new int[]{1, 3}; - actual = solution1.sortArrayByParity(A); - CommonUtils.printArray(actual); - } -} diff --git a/src/test/java/com/fishercoder/_908Test.java b/src/test/java/com/fishercoder/_908Test.java deleted file mode 100644 index 6ac4731ec7..0000000000 --- a/src/test/java/com/fishercoder/_908Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._908; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _908Test { - private static _908.Solution1 solution1; - private static _908.Solution2 solution2; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _908.Solution1(); - solution2 = new _908.Solution2(); - } - - @Test - public void test1() { - A = new int[] {1}; - assertEquals(0, solution1.smallestRangeI(A, 0)); - assertEquals(0, solution2.smallestRangeI(A, 0)); - } - - @Test - public void test2() { - A = new int[] {0, 10}; - assertEquals(6, solution1.smallestRangeI(A, 2)); - assertEquals(6, solution2.smallestRangeI(A, 2)); - } - - @Test - public void test3() { - A = new int[] {1, 3, 6}; - assertEquals(0, solution1.smallestRangeI(A, 3)); - assertEquals(0, solution2.smallestRangeI(A, 3)); - } -} diff --git a/src/test/java/com/fishercoder/_90Test.java b/src/test/java/com/fishercoder/_90Test.java deleted file mode 100644 index 703669ce11..0000000000 --- a/src/test/java/com/fishercoder/_90Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._90; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _90Test { - private static _90.Solution1 solution1; - private static _90.Solution2 solution2; - private static _90.Solution3 solution3; - - @BeforeClass - public static void setup() { - solution1 = new _90.Solution1(); - solution2 = new _90.Solution2(); - solution3 = new _90.Solution3(); - } - - @Test - public void test1() { - CommonUtils.printListList(solution1.subsetsWithDup(new int[]{1, 2, 2})); - } - - @Test - public void test2() { - CommonUtils.printListList(solution2.subsetsWithDup(new int[]{1, 2, 2})); - } - - @Test - public void test3() { - CommonUtils.printListList(solution3.subsetsWithDup(new int[]{1, 2, 2})); - } - -} diff --git a/src/test/java/com/fishercoder/_914Test.java b/src/test/java/com/fishercoder/_914Test.java deleted file mode 100644 index 8056c6f23b..0000000000 --- a/src/test/java/com/fishercoder/_914Test.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._914; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _914Test { - private static _914.Solution1 solution1; - private int[] arr; - - @BeforeClass - public static void setup() { - solution1 = new _914.Solution1(); - } - - @Test - public void test1() { - arr = new int[]{1}; - assertEquals(false, solution1.hasGroupsSizeX(arr)); - } - - @Test - public void test2() { - arr = new int[]{1,1}; - assertEquals(true, solution1.hasGroupsSizeX(arr)); - } - - @Test - public void test3() { - arr = new int[]{1,1,1,1,2,2,2,2,2,2}; - assertEquals(true, solution1.hasGroupsSizeX(arr)); - } - - @Test - public void test4() { - arr = new int[]{1,1,1,2,2,2,3,3}; - assertEquals(false, solution1.hasGroupsSizeX(arr)); - } - - @Test - public void test5() { - arr = new int[]{0,0,1,1,1,1,2,2,3,4}; - assertEquals(false, solution1.hasGroupsSizeX(arr)); - } - - @Test - public void test6() { - arr = new int[]{1,2,3,4,4,3,2,1}; - assertEquals(true, solution1.hasGroupsSizeX(arr)); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_917Test.java b/src/test/java/com/fishercoder/_917Test.java deleted file mode 100644 index 8c3ca495f1..0000000000 --- a/src/test/java/com/fishercoder/_917Test.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._917; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _917Test { - private static _917.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _917.Solution1(); - } - - @Test - public void test1() { - assertEquals("dc-ba", solution1.reverseOnlyLetters("ab-cd")); - } - - @Test - public void test2() { - assertEquals("j-Ih-gfE-dCba", solution1.reverseOnlyLetters("a-bC-dEf-ghIj")); - } - - @Test - public void test3() { - assertEquals("Qedo1ct-eeLg=ntse-T!", solution1.reverseOnlyLetters("Test1ng-Leet=code-Q!")); - } - -} diff --git a/src/test/java/com/fishercoder/_91Test.java b/src/test/java/com/fishercoder/_91Test.java deleted file mode 100644 index 0ee9075c47..0000000000 --- a/src/test/java/com/fishercoder/_91Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._91; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _91Test { - private static _91.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _91.Solution1(); - } - - @Test - public void test1() { - assertEquals(2, solution1.numDecodings("12")); - } - -} diff --git a/src/test/java/com/fishercoder/_921Test.java b/src/test/java/com/fishercoder/_921Test.java deleted file mode 100644 index 3accaa394f..0000000000 --- a/src/test/java/com/fishercoder/_921Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._921; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _921Test { - private static _921.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _921.Solution1(); - } - - @Test - public void test1() { - assertEquals(1, solution1.minAddToMakeValid("())")); - } - - @Test - public void test2() { - assertEquals(3, solution1.minAddToMakeValid("(((")); - } - - @Test - public void test3() { - assertEquals(0, solution1.minAddToMakeValid("()")); - } - - @Test - public void test4() { - assertEquals(4, solution1.minAddToMakeValid("()))((")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_922Test.java b/src/test/java/com/fishercoder/_922Test.java deleted file mode 100644 index 55fc4bed32..0000000000 --- a/src/test/java/com/fishercoder/_922Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._922; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _922Test { - private static _922.Solution1 solution1; - private static int[] A; - private static int[] result; - - @BeforeClass - public static void setup() { - solution1 = new _922.Solution1(); - } - - @Test - public void test1() { - A = new int[]{4, 2, 5, 7}; - result = solution1.sortArrayByParityII(A); - CommonUtils.printArray(result); - } - - @Test - public void test2() { - A = new int[]{3, 1, 4, 2}; - result = solution1.sortArrayByParityII(A); - CommonUtils.printArray(result); - } -} diff --git a/src/test/java/com/fishercoder/_925Test.java b/src/test/java/com/fishercoder/_925Test.java deleted file mode 100644 index 29c7c75dd0..0000000000 --- a/src/test/java/com/fishercoder/_925Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._925; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _925Test { - private static _925.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _925.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isLongPressedName("alex", "aaleex")); - } - - @Test - public void test2() { - assertEquals(false, solution1.isLongPressedName("saeed", "ssaaedd")); - } - - @Test - public void test3() { - assertEquals(true, solution1.isLongPressedName("leelee", "lleeelee")); - } - - @Test - public void test4() { - assertEquals(true, solution1.isLongPressedName("laiden", "laiden")); - } - - @Test - public void test5() { - assertEquals(false, solution1.isLongPressedName("pyplrz", "ppyypllr")); - } - - @Test - public void test6() { - assertEquals(true, solution1.isLongPressedName("leelee", "lleeelee")); - } -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_929Test.java b/src/test/java/com/fishercoder/_929Test.java deleted file mode 100644 index 93c2fd5c8b..0000000000 --- a/src/test/java/com/fishercoder/_929Test.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._929; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _929Test { - private static _929.Solution1 solution1; - private static String[] emails; - - @BeforeClass - public static void setup() { - solution1 = new _929.Solution1(); - } - - @Test - public void test1() { - emails = new String[]{"test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com", "testemail+david@lee.tcode.com"}; - assertEquals(2, solution1.numUniqueEmails(emails)); - } - -} diff --git a/src/test/java/com/fishercoder/_92Test.java b/src/test/java/com/fishercoder/_92Test.java deleted file mode 100644 index d36ec572ca..0000000000 --- a/src/test/java/com/fishercoder/_92Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.ListNode; -import com.fishercoder.solutions._92; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _92Test { - private static _92.Solution1 solution1; - private static ListNode head; - private static ListNode expected; - - @BeforeClass - public static void setup() { - solution1 = new _92.Solution1(); - } - - @Test - public void test1() { - head = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5)); - expected = ListNode.createSinglyLinkedList(Arrays.asList(1, 4, 3, 2, 5)); - assertEquals(expected, solution1.reverseBetween(head, 2, 4)); - } - -} diff --git a/src/test/java/com/fishercoder/_931Test.java b/src/test/java/com/fishercoder/_931Test.java deleted file mode 100644 index 052317df26..0000000000 --- a/src/test/java/com/fishercoder/_931Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._931; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _931Test { - private static _931.Solution1 solution1; - private static int[][] A; - - @BeforeClass - public static void setup() { - solution1 = new _931.Solution1(); - } - - @Test - public void test1() { - A = new int[][]{ - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9} - }; - assertEquals(12, solution1.minFallingPathSum(A)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_933Test.java b/src/test/java/com/fishercoder/_933Test.java deleted file mode 100644 index dd9b355cbf..0000000000 --- a/src/test/java/com/fishercoder/_933Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._933; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _933Test { - private static _933.Solution1.RecentCounter solution1; - - @BeforeClass - public static void setup() { - solution1 = new _933.Solution1.RecentCounter(); - } - - @Test - public void test1() { - assertEquals(1, solution1.ping(1)); - } - -} diff --git a/src/test/java/com/fishercoder/_935Test.java b/src/test/java/com/fishercoder/_935Test.java deleted file mode 100644 index dd9140107a..0000000000 --- a/src/test/java/com/fishercoder/_935Test.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._935; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _935Test { - private static _935.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _935.Solution1(); - } - - @Test - public void test1() { - assertEquals(solution1.knightDialer(1), 10); - } - - @Test - public void test2() { - assertEquals(solution1.knightDialer(2), 20); - } - - @Test - public void test3() { - assertEquals(solution1.knightDialer(3), 46); - } -} diff --git a/src/test/java/com/fishercoder/_937Test.java b/src/test/java/com/fishercoder/_937Test.java deleted file mode 100644 index 7604bba70c..0000000000 --- a/src/test/java/com/fishercoder/_937Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._937; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _937Test { - private static _937.Solution1 solution1; - private static String[] logs; - private static String[] expected; - - @BeforeClass - public static void setup() { - solution1 = new _937.Solution1(); - } - - @Test - public void test1() { - logs = new String[] {"a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo"}; - expected = - new String[] {"g1 act car", "a8 act zoo", "ab1 off key dog", "a1 9 2 3 1", "zo4 4 7"}; - assertArrayEquals(expected, solution1.reorderLogFiles(logs)); - } - - @Test - public void test2() { - logs = new String[] {"t kvr", "r 3 1", "i 403", "7 so", "t 54"}; - expected = new String[] {"t kvr", "7 so", "r 3 1", "i 403", "t 54"}; - assertArrayEquals(expected, solution1.reorderLogFiles(logs)); - } -} diff --git a/src/test/java/com/fishercoder/_938Test.java b/src/test/java/com/fishercoder/_938Test.java deleted file mode 100644 index 480b80b464..0000000000 --- a/src/test/java/com/fishercoder/_938Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._938; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _938Test { - private static _938.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _938.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(10, 5, 15, 3, 7, null, 18)); - TreeUtils.printBinaryTree(root); - assertEquals(32, solution1.rangeSumBST(root, 7, 15)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(10, 5, 15, 3, 7, 13, 18, 1, null, 6)); - TreeUtils.printBinaryTree(root); - assertEquals(23, solution1.rangeSumBST(root, 6, 10)); - } -} diff --git a/src/test/java/com/fishercoder/_93Test.java b/src/test/java/com/fishercoder/_93Test.java deleted file mode 100644 index e9aee353ea..0000000000 --- a/src/test/java/com/fishercoder/_93Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._93; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _93Test { - private static _93.Solution1 solution1; - private static List expected; - private static String s; - - @BeforeClass - public static void setup() { - solution1 = new _93.Solution1(); - } - - @Before - public void setupForEachTest() { - expected = new ArrayList<>(); - } - - @Test - public void test1() { - s = "25525511135"; - expected.add("255.255.11.135"); - expected.add("255.255.111.35"); - assertEquals(expected, solution1.restoreIpAddresses(s)); - } -} diff --git a/src/test/java/com/fishercoder/_941Test.java b/src/test/java/com/fishercoder/_941Test.java deleted file mode 100644 index 7396177d04..0000000000 --- a/src/test/java/com/fishercoder/_941Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._941; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _941Test { - private static _941.Solution1 solution1; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _941.Solution1(); - } - - @Test - public void test1() { - A = new int[]{0, 3, 2, 1}; - assertEquals(true, solution1.validMountainArray(A)); - } - - @Test - public void test2() { - A = new int[]{2, 1}; - assertEquals(false, solution1.validMountainArray(A)); - } - - @Test - public void test3() { - A = new int[]{3, 5, 5}; - assertEquals(false, solution1.validMountainArray(A)); - } - - @Test - public void test4() { - A = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - assertEquals(false, solution1.validMountainArray(A)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_942Test.java b/src/test/java/com/fishercoder/_942Test.java deleted file mode 100644 index 47c753622f..0000000000 --- a/src/test/java/com/fishercoder/_942Test.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._942; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _942Test { - private static _942.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _942.Solution1(); - } - - @Test - public void test1() { - CommonUtils.printArray(solution1.diStringMatch("IDID")); - } - - @Test - public void test2() { - CommonUtils.printArray(solution1.diStringMatch("III")); - } - - @Test - public void test3() { - CommonUtils.printArray(solution1.diStringMatch("DDI")); - } -} diff --git a/src/test/java/com/fishercoder/_944Test.java b/src/test/java/com/fishercoder/_944Test.java deleted file mode 100644 index 862b2596a6..0000000000 --- a/src/test/java/com/fishercoder/_944Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._944; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _944Test { - private static _944.Solution1 solution1; - private static String[] A; - - @BeforeClass - public static void setup() { - solution1 = new _944.Solution1(); - } - - @Test - public void test1() { - A = new String[] {"cba", "daf", "ghi"}; - assertEquals(1, solution1.minDeletionSize(A)); - } - - @Test - public void test2() { - A = new String[] {"a", "b"}; - assertEquals(0, solution1.minDeletionSize(A)); - } - - @Test - public void test3() { - A = new String[] {"zyx", "wvu", "tsr"}; - assertEquals(3, solution1.minDeletionSize(A)); - } -} diff --git a/src/test/java/com/fishercoder/_946Test.java b/src/test/java/com/fishercoder/_946Test.java deleted file mode 100644 index 3c00a64e58..0000000000 --- a/src/test/java/com/fishercoder/_946Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._946; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _946Test { - - private static _946.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _946.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.validateStackSequences(new int[]{1, 2, 3, 4, 5}, new int[]{4, 5, 3, 2, 1})); - } - - @Test - public void test2() { - assertEquals(false, solution1.validateStackSequences(new int[]{1, 2, 3, 4, 5}, new int[]{4, 3, 5, 1, 2})); - } - - @Test - public void test3() { - assertEquals(true, solution1.validateStackSequences(new int[]{}, new int[]{})); - } - - @Test - public void test4() { - assertEquals(false, solution1.validateStackSequences(new int[]{4, 0, 1, 2, 3}, new int[]{4, 2, 3, 0, 1})); - } -} diff --git a/src/test/java/com/fishercoder/_94Test.java b/src/test/java/com/fishercoder/_94Test.java deleted file mode 100644 index dd4b37d20b..0000000000 --- a/src/test/java/com/fishercoder/_94Test.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._94; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -public class _94Test { - private static _94.Solution1 solution1; - private static _94.Solution2 solution2; - private static TreeNode root; - private static List inorder; - - @BeforeClass - public static void setup() { - solution1 = new _94.Solution1(); - solution2 = new _94.Solution2(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4)); - inorder = solution1.inorderTraversal(root); - CommonUtils.printList(inorder); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9)); - TreeUtils.printBinaryTree(root); - inorder = solution1.inorderTraversal(root); - CommonUtils.printList(inorder); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, null, null, 6)); - TreeUtils.printBinaryTree(root); - inorder = solution2.inorderTraversal(root); - CommonUtils.printList(inorder); - } - -} diff --git a/src/test/java/com/fishercoder/_950Test.java b/src/test/java/com/fishercoder/_950Test.java deleted file mode 100644 index 1cc0a19823..0000000000 --- a/src/test/java/com/fishercoder/_950Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._950; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _950Test { - private static _950.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _950.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new int[] {2, 13, 3, 11, 5, 17, 7}, - solution1.deckRevealedIncreasing(new int[] {17, 13, 11, 2, 3, 5, 7})); - } -} diff --git a/src/test/java/com/fishercoder/_951Test.java b/src/test/java/com/fishercoder/_951Test.java deleted file mode 100644 index 4750a22c18..0000000000 --- a/src/test/java/com/fishercoder/_951Test.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._951; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class _951Test { - private static _951.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _951.Solution1(); - } - - @Test - public void test1() { - TreeNode root1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, null, null, null, 7, 8)); - TreeNode root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, null, 6, 4, 5, null, null, null, null, 8, 7)); - assertTrue(solution1.flipEquiv(root1, root2)); - } - - @Test - public void test2() { - TreeNode root1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); - TreeNode root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 4)); - assertFalse(solution1.flipEquiv(root1, root2)); - } -} diff --git a/src/test/java/com/fishercoder/_953Test.java b/src/test/java/com/fishercoder/_953Test.java deleted file mode 100644 index 5554516bc4..0000000000 --- a/src/test/java/com/fishercoder/_953Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._953; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _953Test { - private static _953.Solution1 solution1; - private static String[] words; - private static String order; - - @BeforeClass - public static void setup() { - solution1 = new _953.Solution1(); - } - - @Test - public void test1() { - words = new String[] {"hello", "leetcode"}; - order = "hlabcdefgijkmnopqrstuvwxyz"; - assertEquals(true, solution1.isAlienSorted(words, order)); - } - - @Test - public void test2() { - words = new String[] {"word", "world", "row"}; - order = "worldabcefghijkmnpqstuvxyz"; - assertEquals(false, solution1.isAlienSorted(words, order)); - } - - @Test - public void test3() { - words = new String[] {"apple", "app"}; - order = "abcdefghijklmnopqrstuvwxyz"; - assertEquals(false, solution1.isAlienSorted(words, order)); - } -} diff --git a/src/test/java/com/fishercoder/_954Test.java b/src/test/java/com/fishercoder/_954Test.java deleted file mode 100644 index 2a82af6056..0000000000 --- a/src/test/java/com/fishercoder/_954Test.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._954; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _954Test { - private static _954.Solution1 solution1; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _954.Solution1(); - } - - @Test - public void test1() { - A = new int[]{3, 1, 3, 6}; - assertEquals(false, solution1.canReorderDoubled(A)); - } - - @Test - public void test2() { - A = new int[]{2, 1, 2, 6}; - assertEquals(false, solution1.canReorderDoubled(A)); - } - - @Test - public void test3() { - A = new int[]{4, -2, 2, -4}; - assertEquals(true, solution1.canReorderDoubled(A)); - } - - @Test - public void test4() { - A = new int[]{1, 2, 4, 16, 8, 4}; - assertEquals(false, solution1.canReorderDoubled(A)); - } - - @Test - public void test5() { - A = new int[]{1, 2, 4, 8}; - assertEquals(true, solution1.canReorderDoubled(A)); - } - - @Test - public void test6() { - A = new int[]{10, 20, 40, 80}; - assertEquals(true, solution1.canReorderDoubled(A)); - } - - @Test - public void test7() { - A = new int[]{0, 0}; - assertEquals(true, solution1.canReorderDoubled(A)); - } -} diff --git a/src/test/java/com/fishercoder/_958Test.java b/src/test/java/com/fishercoder/_958Test.java deleted file mode 100644 index f28dd4eb0b..0000000000 --- a/src/test/java/com/fishercoder/_958Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._958; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static junit.framework.TestCase.assertEquals; - -public class _958Test { - private static _958.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _958.Solution1(); - } - - @Test - public void test1() { - TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6)); - assertEquals(true, solution1.isCompleteTree(root)); - } - - @Test - public void test2() { - TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, null, 7)); - assertEquals(false, solution1.isCompleteTree(root)); - } - - @Test - public void test3() { - TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 8)); - assertEquals(true, solution1.isCompleteTree(root)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_95Test.java b/src/test/java/com/fishercoder/_95Test.java deleted file mode 100644 index d75c6d4845..0000000000 --- a/src/test/java/com/fishercoder/_95Test.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._95; -import org.junit.BeforeClass; -import org.junit.Test; - -public class _95Test { - private static _95.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _95.Solution1(); - } - - @Test - public void test1() { - solution1.generateTrees(3).forEach(TreeUtils::printBinaryTree); - } - -} diff --git a/src/test/java/com/fishercoder/_961Test.java b/src/test/java/com/fishercoder/_961Test.java deleted file mode 100644 index 63eabb0119..0000000000 --- a/src/test/java/com/fishercoder/_961Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._961; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.TestCase.assertEquals; - -public class _961Test { - private static _961.Solution1 solution1; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _961.Solution1(); - } - - @Test - public void test1() { - A = new int[]{1, 2, 3, 3}; - assertEquals(3, solution1.repeatedNTimes(A)); - } - - @Test - public void test2() { - A = new int[]{2, 1, 2, 5, 3, 2}; - assertEquals(2, solution1.repeatedNTimes(A)); - } - - @Test - public void test3() { - A = new int[]{5, 1, 5, 2, 5, 3, 5, 4}; - assertEquals(5, solution1.repeatedNTimes(A)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_965Test.java b/src/test/java/com/fishercoder/_965Test.java deleted file mode 100644 index 50b355d150..0000000000 --- a/src/test/java/com/fishercoder/_965Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._14; -import com.fishercoder.solutions._965; -import java.util.Arrays; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _965Test { - private static _965.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _965.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 1, 1, 1, null, 1)); - assertEquals(true, solution1.isUnivalTree(root)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 2, 5, 2)); - assertEquals(false, solution1.isUnivalTree(root)); - } -} diff --git a/src/test/java/com/fishercoder/_966Test.java b/src/test/java/com/fishercoder/_966Test.java deleted file mode 100644 index c69ef051a1..0000000000 --- a/src/test/java/com/fishercoder/_966Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._966; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -/** - * Created by varunu28 on 1/01/19. - */ - - -public class _966Test { - private static _966.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _966.Solution1(); - } - - - @Test - public void test1() { - assertEquals( - Arrays.toString(new String[]{"kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"}), - Arrays.toString(solution1 - .spellchecker( - new String[]{"KiTe","kite","hare","Hare"}, - new String[]{"kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"}))); - } -} diff --git a/src/test/java/com/fishercoder/_96Test.java b/src/test/java/com/fishercoder/_96Test.java deleted file mode 100644 index a65e234d09..0000000000 --- a/src/test/java/com/fishercoder/_96Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._96; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _96Test { - private static _96.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _96.Solution1(); - } - - @Test - public void test1() { - assertEquals(5, solution1.numTrees(3)); - } - -} diff --git a/src/test/java/com/fishercoder/_970Test.java b/src/test/java/com/fishercoder/_970Test.java deleted file mode 100644 index b17945a61f..0000000000 --- a/src/test/java/com/fishercoder/_970Test.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._970; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _970Test { - private static _970.Solution1 solution1; - private static _970.Solution2 solution2; - - @BeforeClass - public static void setup() { - solution1 = new _970.Solution1(); - solution2 = new _970.Solution2(); - } - - @Test - public void test1() { - assertEquals(Arrays.asList(2, 3, 4, 5, 7, 9, 10), solution1.powerfulIntegers(2, 3, 10)); - assertEquals(Arrays.asList(2, 3, 4, 5, 7, 9, 10), solution2.powerfulIntegers(2, 3, 10)); - } - - @Test - public void test2() { - assertEquals(Arrays.asList(2, 4, 6, 8, 10, 14), solution1.powerfulIntegers(3, 5, 15)); - assertEquals(Arrays.asList(2, 4, 6, 8, 10, 14), solution2.powerfulIntegers(3, 5, 15)); - } - - @Test - public void test3() { - assertEquals(Arrays.asList(2, 3, 5, 7, 8, 9, 10), solution1.powerfulIntegers(2, 6, 12)); - assertEquals(Arrays.asList(2, 3, 5, 7, 8, 9, 10), solution2.powerfulIntegers(2, 6, 12)); - } - - @Test - public void test4() { - assertEquals(Arrays.asList(2, 3, 5, 9, 10, 11), solution1.powerfulIntegers(2, 9, 12)); - assertEquals(Arrays.asList(2, 3, 5, 9, 10, 11), solution2.powerfulIntegers(2, 9, 12)); - } - - @Test - public void test5() { - assertEquals(Arrays.asList(2, 91, 180, 8101, 8190, 16200, 729001, 729090, - 737100), solution1.powerfulIntegers(90, 90, 1000000)); - List actual = solution2.powerfulIntegers(90, 90, 1000000); - Collections.sort(actual); - assertEquals(Arrays.asList(2, 91, 180, 8101, 8190, 16200, 729001, 729090, - 737100), actual); - } -} diff --git a/src/test/java/com/fishercoder/_973Test.java b/src/test/java/com/fishercoder/_973Test.java deleted file mode 100644 index abc218d1c1..0000000000 --- a/src/test/java/com/fishercoder/_973Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._973; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _973Test { - private static _973.Solution1 test; - - @BeforeClass - public static void setUp() { - test = new _973.Solution1(); - } - - @Test - public void test1() { - assertArrayEquals(new int[][]{{-2, 2}}, test.kClosest(new int[][]{{1, 3}, {-2, 2}}, 1)); - } - - @Test - public void test2() { - assertArrayEquals(new int[][]{{3, 3},{-2, 4}}, test.kClosest(new int[][]{{3, 3},{5, -1},{-2, 4}}, 2)); - } -} diff --git a/src/test/java/com/fishercoder/_974Test.java b/src/test/java/com/fishercoder/_974Test.java deleted file mode 100644 index 2e5260aadc..0000000000 --- a/src/test/java/com/fishercoder/_974Test.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._974; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _974Test { - - private static _974.Solution1 test; - - @BeforeClass - public static void setUp() { - test = new _974.Solution1(); - } - - @Test - public void test1() { - assertEquals(7, test.subarraysDivByK(new int[]{4, 5, 0, -2, -3, 1}, 5)); - } -} diff --git a/src/test/java/com/fishercoder/_976Test.java b/src/test/java/com/fishercoder/_976Test.java deleted file mode 100644 index 5073b68616..0000000000 --- a/src/test/java/com/fishercoder/_976Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._976; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _976Test { - - private static _976.Solution1 test; - - @BeforeClass - public static void setUp() { - test = new _976.Solution1(); - } - - @Test - public void test1() { - assertEquals(5, test.largestPerimeter(new int[]{2, 1, 2})); - } - - @Test - public void test2() { - assertEquals(0, test.largestPerimeter(new int[]{1, 2, 1})); - } - - @Test - public void test3() { - assertEquals(10, test.largestPerimeter(new int[]{3, 2, 3, 4})); - } - - @Test - public void test4() { - assertEquals(8, test.largestPerimeter(new int[]{3, 6, 2, 3})); - } -} diff --git a/src/test/java/com/fishercoder/_977Test.java b/src/test/java/com/fishercoder/_977Test.java deleted file mode 100644 index ee10597c16..0000000000 --- a/src/test/java/com/fishercoder/_977Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._977; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _977Test { - private static _977.Solution1 solution1; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _977.Solution1(); - } - - @Test - public void test1() { - A = new int[] {-4, -1, 0, 3, 10}; - assertArrayEquals(new int[] {0, 1, 9, 16, 100}, solution1.sortedSquares(A)); - } - - @Test - public void test2() { - A = new int[] {-7, -3, 2, 3, 11}; - assertArrayEquals(new int[] {4, 9, 9, 49, 121}, solution1.sortedSquares(A)); - } -} diff --git a/src/test/java/com/fishercoder/_97Test.java b/src/test/java/com/fishercoder/_97Test.java deleted file mode 100644 index 1d90d828bc..0000000000 --- a/src/test/java/com/fishercoder/_97Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._97; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _97Test { - private static _97.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _97.Solution1(); - } - - @Test - public void test1() { - assertEquals(true, solution1.isInterleave("aabcc", "dbbca", "aadbbcbcac")); - } - - @Test - public void test2() { - assertEquals(false, solution1.isInterleave("aabcc", "dbbca", "aadbbbaccc")); - } - -} diff --git a/src/test/java/com/fishercoder/_980Test.java b/src/test/java/com/fishercoder/_980Test.java deleted file mode 100644 index dfcc82ccff..0000000000 --- a/src/test/java/com/fishercoder/_980Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._980; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _980Test { - private static _980.Solution1 solution1; - private static int[][] grid; - - @Test - public void test1() { - solution1 = new _980.Solution1(); - grid = new int[][]{ - {1, 0, 0, 0}, - {0, 0, 0, 0}, - {0, 0, 2, -1}, - }; - assertEquals(2, solution1.uniquePathsIII(grid)); - } - - @Test - public void test2() { - solution1 = new _980.Solution1(); - grid = new int[][]{ - {1, 0, 0, 0}, - {0, 0, 0, 0}, - {0, 0, 0, 2}, - }; - assertEquals(4, solution1.uniquePathsIII(grid)); - } - - @Test - public void test3() { - solution1 = new _980.Solution1(); - grid = new int[][]{ - {0, 1}, - {2, 0}, - }; - assertEquals(0, solution1.uniquePathsIII(grid)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_985Test.java b/src/test/java/com/fishercoder/_985Test.java deleted file mode 100644 index ab2e057455..0000000000 --- a/src/test/java/com/fishercoder/_985Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._985; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertArrayEquals; - -public class _985Test { - private static _985.Solution1 solution1; - private static int[] expected; - private static int[] actual; - private static int[] A; - private static int[][] queries; - - @BeforeClass - public static void setup() { - solution1 = new _985.Solution1(); - } - - @Test - public void test1() { - A = new int[]{1, 2, 3, 4}; - queries = new int[][]{{1, 0}, {-3, 1}, {-4, 0}, {2, 3}}; - expected = new int[]{8, 6, 2, 4}; - actual = solution1.sumEvenAfterQueries(A, queries); - assertArrayEquals(expected, actual); - } -} diff --git a/src/test/java/com/fishercoder/_986Test.java b/src/test/java/com/fishercoder/_986Test.java deleted file mode 100644 index 48d58058b9..0000000000 --- a/src/test/java/com/fishercoder/_986Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.utils.CommonUtils; -import com.fishercoder.solutions._986; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _986Test { - private static _986.Solution1 solution1; - private static int[][] A; - private static int[][] B; - private static int[][] expected; - - @BeforeClass - public static void setup() { - solution1 = new _986.Solution1(); - } - - @Test - public void test1() { - A = new int[][]{ - {0, 2}, - {5, 10}, - {13, 23}, - {24, 25} - }; - B = new int[][]{ - {1, 5}, - {8, 12}, - {15, 24}, - {25, 26} - }; - expected = new int[][]{ - {1, 2}, - {5, 5}, - {8, 10}, - {15, 23}, - {24, 24}, - {25, 25} - }; - int[][] actual = solution1.intervalIntersection(A, B); - CommonUtils.print2DIntArray(actual); - assertEquals(expected, actual); - } - - -} diff --git a/src/test/java/com/fishercoder/_987Test.java b/src/test/java/com/fishercoder/_987Test.java deleted file mode 100644 index 2ddba2a0b8..0000000000 --- a/src/test/java/com/fishercoder/_987Test.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._987; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static junit.framework.Assert.assertEquals; - -public class _987Test { - private static _987.Solution1 solution1; - private static TreeNode root; - private static List> expected; - private static List> actual; - - @BeforeClass - public static void setup() { - solution1 = new _987.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); - expected = Arrays.asList(Arrays.asList(9), Arrays.asList(3, 15), Arrays.asList(20), Arrays.asList(7)); - actual = solution1.verticalTraversal(root); - assertEquals(expected, actual); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); - expected = Arrays.asList(Arrays.asList(4), Arrays.asList(2), Arrays.asList(1, 5, 6), Arrays.asList(3), Arrays.asList(7)); - actual = solution1.verticalTraversal(root); - assertEquals(expected, actual); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(0, 5, 1, 9, null, 2, null, null, null, null, 3, 4, 8, 6, null, null, null, 7)); - TreeUtils.printBinaryTree(root); - expected = Arrays.asList(Arrays.asList(9, 7), Arrays.asList(5, 6), Arrays.asList(0, 2, 4), Arrays.asList(1, 3), Arrays.asList(8)); - actual = solution1.verticalTraversal(root); - assertEquals(expected, actual); - } - - @Test - public void test4() { - root = TreeUtils.constructBinaryTree(Arrays.asList(0, 2, 1, 3, null, null, null, 4, 5, null, 7, 6, null, 10, 8, 11, 9)); - TreeUtils.printBinaryTree(root); - expected = Arrays.asList(Arrays.asList(4, 10, 11), Arrays.asList(3, 6, 7), Arrays.asList(2, 5, 8, 9), Arrays.asList(0), Arrays.asList(1)); - actual = solution1.verticalTraversal(root); - assertEquals(expected, actual); - } - -} diff --git a/src/test/java/com/fishercoder/_988Test.java b/src/test/java/com/fishercoder/_988Test.java deleted file mode 100644 index ed718eb5ff..0000000000 --- a/src/test/java/com/fishercoder/_988Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._988; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -public class _988Test { - private static _988.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _988.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(0, 1, 2, 3, 4, 3, 4)); - assertEquals("dba", solution1.smallestFromLeaf(root)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(25, 1, 3, 1, 3, 0, 2)); - assertEquals("adz", solution1.smallestFromLeaf(root)); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 1, null, 1, 0, null, 0)); - assertEquals("abc", solution1.smallestFromLeaf(root)); - } - -} diff --git a/src/test/java/com/fishercoder/_989Test.java b/src/test/java/com/fishercoder/_989Test.java deleted file mode 100644 index 31f4c13932..0000000000 --- a/src/test/java/com/fishercoder/_989Test.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._989; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _989Test { - private static _989.Solution1 solution1; - private static int[] A; - - @BeforeClass - public static void setup() { - solution1 = new _989.Solution1(); - } - - @Test - public void test1() { - A = new int[] {1, 2, 0, 0}; - assertEquals(Arrays.asList(1, 2, 3, 4), solution1.addToArrayForm(A, 34)); - } - - @Test - public void test2() { - A = new int[] {2, 7, 4}; - assertEquals(Arrays.asList(4, 5, 5), solution1.addToArrayForm(A, 181)); - } - - @Test - public void test3() { - A = new int[] {2, 1, 5}; - assertEquals(Arrays.asList(1, 0, 2, 1), solution1.addToArrayForm(A, 806)); - } - - @Test - public void test4() { - A = new int[] {9, 9, 9, 9, 9, 9, 9, 9, 9, 9}; - assertEquals(Arrays.asList(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), solution1.addToArrayForm(A, 1)); - } -} diff --git a/src/test/java/com/fishercoder/_98Test.java b/src/test/java/com/fishercoder/_98Test.java deleted file mode 100644 index d65dec0834..0000000000 --- a/src/test/java/com/fishercoder/_98Test.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._98; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/17/17. - */ -public class _98Test { - private static _98.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setup() { - solution1 = new _98.Solution1(); - } - - @Test - public void test1() { - root = new TreeNode(2); - root.left = new TreeNode(1); - root.right = new TreeNode(3); - assertEquals(true, solution1.isValidBST(root)); - } - - @Test - public void test2() { - root = new TreeNode(0); - assertEquals(true, solution1.isValidBST(root)); - } - - @Test - public void test3() { - root = new TreeNode(1); - root.left = new TreeNode(1); - assertEquals(false, solution1.isValidBST(root)); - } - - @Test - public void test4() { - root = TreeUtils.constructBinaryTree(Arrays.asList(5, 1, 7, null, null, 3, 8)); - TreeUtils.printBinaryTree(root); - assertEquals(false, solution1.isValidBST(root)); - } -} diff --git a/src/test/java/com/fishercoder/_993Test.java b/src/test/java/com/fishercoder/_993Test.java deleted file mode 100644 index 41e6f772bc..0000000000 --- a/src/test/java/com/fishercoder/_993Test.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._703; -import com.fishercoder.solutions._976; -import com.fishercoder.solutions._993; -import java.util.Arrays; -import org.junit.BeforeClass; -import org.junit.Test; - -import static junit.framework.Assert.assertEquals; - -public class _993Test { - private static _993.Solution1 solution1; - private static TreeNode root; - - @BeforeClass - public static void setUp() { - solution1 = new _993.Solution1(); - } - - @Test - public void test1() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4)); - TreeUtils.printBinaryTree(root); - assertEquals(false, solution1.isCousins(root, 4, 3)); - } - - @Test - public void test2() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4, null, 5)); - TreeUtils.printBinaryTree(root); - assertEquals(true, solution1.isCousins(root, 5, 4)); - } - - @Test - public void test3() { - root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4)); - TreeUtils.printBinaryTree(root); - assertEquals(false, solution1.isCousins(root, 2, 3)); - } -} diff --git a/src/test/java/com/fishercoder/_994Test.java b/src/test/java/com/fishercoder/_994Test.java deleted file mode 100644 index 42b52921e4..0000000000 --- a/src/test/java/com/fishercoder/_994Test.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._994; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _994Test { - private static _994.Solution1 solution1; - private static int[][] grid; - - @BeforeClass - public static void setUp() { - solution1 = new _994.Solution1(); - } - - @Test - public void test1() { - grid = new int[][]{ - {2, 1, 1}, - {1, 1, 0}, - {0, 1, 1} - }; - assertEquals(4, solution1.orangesRotting(grid)); - } - - @Test - public void test2() { - grid = new int[][]{ - {2, 1, 1}, - {0, 1, 1}, - {1, 0, 1} - }; - assertEquals(-1, solution1.orangesRotting(grid)); - } - - @Test - public void test3() { - grid = new int[][]{ - {0, 2} - }; - assertEquals(0, solution1.orangesRotting(grid)); - } -} diff --git a/src/test/java/com/fishercoder/_997Test.java b/src/test/java/com/fishercoder/_997Test.java deleted file mode 100644 index b2ecbf15ea..0000000000 --- a/src/test/java/com/fishercoder/_997Test.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._997; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _997Test { - private static _997.Solution1 solution1; - private static int[][] trust; - - @BeforeClass - public static void setup() { - solution1 = new _997.Solution1(); - } - - @Test - public void test1() { - trust = new int[][] {{1, 2}}; - assertEquals(2, solution1.findJudge(2, trust)); - } - - @Test - public void test2() { - trust = new int[][] {{1, 3}, {2, 3}}; - assertEquals(3, solution1.findJudge(3, trust)); - } - - @Test - public void test3() { - trust = new int[][] {{1, 2}, {2, 3}, {3, 1}}; - assertEquals(-1, solution1.findJudge(3, trust)); - } - - @Test - public void test4() { - trust = new int[][] {{1, 2}, {2, 3}}; - assertEquals(-1, solution1.findJudge(3, trust)); - } - - @Test - public void test5() { - trust = new int[][] {{1, 3}, {1, 4}, {2, 3}, {2, 4}, {4, 3}}; - assertEquals(3, solution1.findJudge(4, trust)); - } - - @Test - public void test6() { - trust = new int[][] {{1, 3}, {2, 3}, {3, 1}}; - assertEquals(-1, solution1.findJudge(3, trust)); - } -} diff --git a/src/test/java/com/fishercoder/_999Test.java b/src/test/java/com/fishercoder/_999Test.java deleted file mode 100644 index b218d6f3f6..0000000000 --- a/src/test/java/com/fishercoder/_999Test.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._999; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _999Test { - private static _999.Solution1 solution1; - private static char[][] board; - - @BeforeClass - public static void setup() { - solution1 = new _999.Solution1(); - } - - @Test - public void test1() { - board = new char[][] { - {'.', '.', '.', '.', '.', '.', '.', '.'}, - {'.', '.', '.', 'p', '.', '.', '.', '.'}, - {'.', '.', '.', 'R', '.', '.', '.', 'p'}, - {'.', '.', '.', '.', '.', '.', '.', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.'}, - {'.', '.', '.', 'p', '.', '.', '.', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.'}, - }; - assertEquals(3, solution1.numRookCaptures(board)); - } - - @Test - public void test2() { - board = new char[][] { - {'.', '.', '.', '.', '.', '.', '.', '.'}, - {'.', 'p', 'p', 'p', 'p', 'p', '.', '.'}, - {'.', 'p', 'p', 'B', 'p', 'p', '.', '.'}, - {'.', 'p', 'B', 'R', 'B', 'p', '.', '.'}, - {'.', 'p', 'p', 'B', 'p', 'p', '.', '.'}, - {'.', 'p', 'p', 'p', 'p', 'p', '.', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.'}, - }; - assertEquals(0, solution1.numRookCaptures(board)); - } - - @Test - public void test3() { - board = new char[][] { - {'.', '.', '.', '.', '.', '.', '.', '.'}, - {'.', '.', '.', 'p', '.', '.', '.', '.'}, - {'.', '.', '.', 'p', '.', '.', '.', 'p'}, - {'p', 'p', '.', 'R', '.', 'p', 'B', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.'}, - {'.', '.', '.', 'B', '.', '.', '.', '.'}, - {'.', '.', '.', 'p', '.', '.', '.', '.'}, - {'.', '.', '.', '.', '.', '.', '.', '.'}, - }; - assertEquals(3, solution1.numRookCaptures(board)); - } -} diff --git a/src/test/java/com/fishercoder/_9Test.java b/src/test/java/com/fishercoder/_9Test.java deleted file mode 100644 index 8911ffbaac..0000000000 --- a/src/test/java/com/fishercoder/_9Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.fishercoder; - -import com.fishercoder.solutions._9; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class _9Test { - private static _9.Solution1 solution1; - - @BeforeClass - public static void setup() { - solution1 = new _9.Solution1(); - } - - @Test - public void test1() { - assertEquals(false, solution1.isPalindrome(2147483647)); - } - - @Test - public void test2() { - assertEquals(true, solution1.isPalindrome(0)); - } - - @Test - public void test3() { - assertEquals(true, solution1.isPalindrome(1)); - } - - @Test - public void test4() { - assertEquals(false, solution1.isPalindrome(10)); - } - -} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/firstthousand/_100Test.java b/src/test/java/com/fishercoder/firstthousand/_100Test.java new file mode 100644 index 0000000000..7db12e7592 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_100Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.*; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._100; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _100Test { + private _100.Solution1 solution1; + private static TreeNode p; + private static TreeNode q; + + @BeforeEach + public void setup() { + solution1 = new _100.Solution1(); + } + + @Test + public void test1() { + p = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); + TreeUtils.printBinaryTree(p); + q = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); + TreeUtils.printBinaryTree(p); + assertTrue(solution1.isSameTree(p, q)); + } + + @Test + public void test2() { + p = TreeUtils.constructBinaryTree(Arrays.asList(1, 2)); + TreeUtils.printBinaryTree(p); + q = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2)); + TreeUtils.printBinaryTree(p); + assertFalse(solution1.isSameTree(p, q)); + } + + @Test + public void test3() { + p = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 1)); + TreeUtils.printBinaryTree(p); + q = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 2)); + TreeUtils.printBinaryTree(p); + assertFalse(solution1.isSameTree(p, q)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_101Test.java b/src/test/java/com/fishercoder/firstthousand/_101Test.java new file mode 100644 index 0000000000..0a22aa4c11 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_101Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._101; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _101Test { + private _101.Solution1 solution1; + private _101.Solution2 solution2; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _101.Solution1(); + solution2 = new _101.Solution2(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 2, 3, 4, 4, 3)); + assertEquals(true, solution1.isSymmetric(root)); + assertEquals(true, solution2.isSymmetric(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 2, null, 3, null, 3)); + assertEquals(false, solution1.isSymmetric(root)); + assertEquals(false, solution2.isSymmetric(root)); + } +} diff --git a/src/test/java/com/fishercoder/_102Test.java b/src/test/java/com/fishercoder/firstthousand/_102Test.java similarity index 75% rename from src/test/java/com/fishercoder/_102Test.java rename to src/test/java/com/fishercoder/firstthousand/_102Test.java index 38660f3e71..5a553907ba 100644 --- a/src/test/java/com/fishercoder/_102Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_102Test.java @@ -1,20 +1,19 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.CommonUtils; import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._102; -import org.junit.BeforeClass; -import org.junit.Test; - +import com.fishercoder.solutions.firstthousand._102; import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _102Test { - private static _102.Solution1 solution1; + private _102.Solution1 solution1; private static TreeNode treeRoot; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _102.Solution1(); } @@ -31,5 +30,4 @@ public void test2() { TreeUtils.printBinaryTree(treeRoot); CommonUtils.printListList(solution1.levelOrder(treeRoot)); } - -} \ No newline at end of file +} diff --git a/src/test/java/com/fishercoder/firstthousand/_103Test.java b/src/test/java/com/fishercoder/firstthousand/_103Test.java new file mode 100644 index 0000000000..b769365e40 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_103Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._103; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _103Test { + private _103.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _103.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); + TreeUtils.printBinaryTree(root); + CommonUtils.printListList(solution1.zigzagLevelOrder(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_104Test.java b/src/test/java/com/fishercoder/firstthousand/_104Test.java new file mode 100644 index 0000000000..deae3cc5a7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_104Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._104; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _104Test { + private _104.Solution1 solution1; + private _104.Solution2 solution2; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _104.Solution1(); + solution2 = new _104.Solution2(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); + assertEquals(3, solution1.maxDepth(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); + TreeUtils.printBinaryTree(root); + assertEquals(3, solution2.maxDepth(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_105Test.java b/src/test/java/com/fishercoder/firstthousand/_105Test.java new file mode 100644 index 0000000000..4c6a5951dc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_105Test.java @@ -0,0 +1,60 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._105; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _105Test { + private _105.Solution1 solution1; + private static TreeNode expected; + private static TreeNode actual; + private static int[] preorder; + private static int[] inorder; + + @BeforeEach + public void setup() { + solution1 = new _105.Solution1(); + } + + @Test + public void test1() { + preorder = new int[] {1, 2, 3}; + inorder = new int[] {2, 1, 3}; + actual = solution1.buildTree(preorder, inorder); + expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); + assertEquals(expected, actual); + } + + @Test + public void test2() { + preorder = new int[] {1, 2, 4, 5, 3}; + inorder = new int[] {4, 2, 5, 1, 3}; + actual = solution1.buildTree(preorder, inorder); + expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); + assertEquals(expected, actual); + } + + @Test + public void test3() { + preorder = new int[] {3, 9, 20, 15, 7}; + inorder = new int[] {9, 3, 15, 20, 7}; + actual = solution1.buildTree(preorder, inorder); + expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); + assertEquals(expected, actual); + } + + @Test + public void test4() { + preorder = new int[] {3, 1, 2, 4}; + inorder = new int[] {1, 2, 3, 4}; + actual = solution1.buildTree(preorder, inorder); + expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, 4, null, 2)); + TreeUtils.printBinaryTree(expected); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_106Test.java b/src/test/java/com/fishercoder/firstthousand/_106Test.java new file mode 100644 index 0000000000..ad7a652a55 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_106Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._106; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _106Test { + private _106.Solution1 solution1; + private _106.Solution2 solution2; + private static TreeNode expected; + private static TreeNode actual; + private static int[] inorder; + private static int[] postorder; + + @BeforeEach + public void setup() { + solution1 = new _106.Solution1(); + solution2 = new _106.Solution2(); + } + + @Test + public void test1() { + /** it should be a tree like this: 3 / 1 \ 2 */ + postorder = new int[] {2, 1, 3}; + inorder = new int[] {1, 2, 3}; + actual = solution1.buildTree(inorder, postorder); + expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 2)); + assertEquals(expected, actual); + actual = solution2.buildTree(inorder, postorder); + assertEquals(expected, actual); + } + + @Test + public void test2() { + /** it should be a tree like this: 3 / 1 \ 5 / 2 \ 4 */ + postorder = new int[] {4, 2, 5, 1, 3}; + inorder = new int[] {1, 2, 4, 5, 3}; + actual = solution1.buildTree(inorder, postorder); + expected = + TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4)); + assertEquals(expected, actual); + } + + @Test + public void test3() { + /** it should be a tree like this: 2 / 1 */ + inorder = new int[] {1, 2}; + postorder = new int[] {1, 2}; + actual = solution1.buildTree(inorder, postorder); + expected = TreeUtils.constructBinaryTree(Arrays.asList(2, 1)); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_107Test.java b/src/test/java/com/fishercoder/firstthousand/_107Test.java new file mode 100644 index 0000000000..b72ea5973d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_107Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._107; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _107Test { + private _107.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _107.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); + TreeUtils.printBinaryTree(root); + CommonUtils.printListList(solution1.levelOrderBottom(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_108Test.java b/src/test/java/com/fishercoder/firstthousand/_108Test.java new file mode 100644 index 0000000000..3d87820873 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_108Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._108; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _108Test { + private _108.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _108.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3}; + TreeUtils.printBinaryTree(solution1.sortedArrayToBST(nums)); + } + + @Test + public void test2() { + nums = new int[] {}; + TreeUtils.printBinaryTree(solution1.sortedArrayToBST(nums)); + } + + @Test + public void test3() { + nums = new int[] {-10, -3, 0, 5, 9}; + TreeUtils.printBinaryTree(solution1.sortedArrayToBST(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_109Test.java b/src/test/java/com/fishercoder/firstthousand/_109Test.java new file mode 100644 index 0000000000..afc14569e5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_109Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._109; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _109Test { + private _109.Solution1 solution1; + private static ListNode head; + private static TreeNode expected; + + @BeforeEach + public void setup() { + solution1 = new _109.Solution1(); + } + + @Test + public void test1() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5}); + expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, 4, null, 2, null, 5)); + /** as long as it's a height-balanced tree, it's good for this problem requirement */ + TreeUtils.printBinaryTree(solution1.sortedListToBST(head)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_10Test.java b/src/test/java/com/fishercoder/firstthousand/_10Test.java new file mode 100644 index 0000000000..c40a983339 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_10Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._10; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _10Test { + private _10.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _10.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isMatch("", "")); + } + + @Test + public void test2() { + assertEquals(false, solution1.isMatch("aa", "a")); + } + + @Test + public void test3() { + assertEquals(true, solution1.isMatch("aab", "c*a*b")); + } +} diff --git a/src/test/java/com/fishercoder/_110Test.java b/src/test/java/com/fishercoder/firstthousand/_110Test.java similarity index 75% rename from src/test/java/com/fishercoder/_110Test.java rename to src/test/java/com/fishercoder/firstthousand/_110Test.java index 6eb5eefd49..2e26d0cb27 100644 --- a/src/test/java/com/fishercoder/_110Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_110Test.java @@ -1,22 +1,21 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._110; -import org.junit.BeforeClass; -import org.junit.Test; - +import com.fishercoder.solutions.firstthousand._110; import java.util.Arrays; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _110Test { - private static _110.Solution1 solution1; - private static _110.Solution2 solution2; + private _110.Solution1 solution1; + private _110.Solution2 solution2; private static TreeNode treeNode; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _110.Solution1(); solution2 = new _110.Solution2(); } @@ -44,6 +43,4 @@ public void test4() { treeNode = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 2, 3, 3, null, null, 4, 4)); assertEquals(false, solution2.isBalanced(treeNode)); } - - -} \ No newline at end of file +} diff --git a/src/test/java/com/fishercoder/firstthousand/_113Test.java b/src/test/java/com/fishercoder/firstthousand/_113Test.java new file mode 100644 index 0000000000..45472c4fd2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_113Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._113; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _113Test { + private _113.Solution1 solution1; + private _113.Solution2 solution2; + private static TreeNode root; + private static int sum; + private static List> expected; + + @BeforeEach + public void setup() { + solution1 = new _113.Solution1(); + solution2 = new _113.Solution2(); + } + + @Test + public void test1() { + sum = 22; + root = + TreeUtils.constructBinaryTree( + Arrays.asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1)); + TreeUtils.printBinaryTree(root); + expected = new ArrayList<>(); + expected.add(Arrays.asList(5, 4, 11, 2)); + expected.add(Arrays.asList(5, 8, 4, 5)); + assertEquals(expected, solution1.pathSum(root, sum)); + assertEquals(expected, solution2.pathSum(root, sum)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_114Test.java b/src/test/java/com/fishercoder/firstthousand/_114Test.java new file mode 100644 index 0000000000..9ee847a21c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_114Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._114; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _114Test { + private _114.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setUp() { + solution1 = new _114.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 5, 3, 4, null, 6)); + TreeUtils.printBinaryTree(root); + solution1.flatten(root); + TreeUtils.printBinaryTree(root); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_115Test.java b/src/test/java/com/fishercoder/firstthousand/_115Test.java new file mode 100644 index 0000000000..3a94e823e8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_115Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._115; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _115Test { + private _115.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _115.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.numDistinct("rabbbit", "rabbit")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_116Test.java b/src/test/java/com/fishercoder/firstthousand/_116Test.java new file mode 100644 index 0000000000..79d9214f5b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_116Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._116; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _116Test { + private _116.Solution1 solution1; + private _116.Solution2 solution2; + private _116.Node root; + + @BeforeEach + public void setup() { + solution1 = new _116.Solution1(); + solution2 = new _116.Solution2(); + } + + @Test + public void test1() { + root = new _116.Node(1); + root.left = new _116.Node(2); + root.right = new _116.Node(3); + root.left.left = new _116.Node(4); + root.left.right = new _116.Node(5); + root.right.right = new _116.Node(7); + solution1.connect(root); + } + + @Test + public void test2() { + root = new _116.Node(1); + root.left = new _116.Node(2); + root.right = new _116.Node(3); + root.left.left = new _116.Node(4); + root.left.right = new _116.Node(5); + root.right.right = new _116.Node(7); + + solution2.connect(root); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_117Test.java b/src/test/java/com/fishercoder/firstthousand/_117Test.java new file mode 100644 index 0000000000..0054547070 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_117Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._117; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _117Test { + private _117.Solution1 solution1; + private _117.Node root; + + @BeforeEach + public void setup() { + solution1 = new _117.Solution1(); + } + + @Test + public void test1() { + root = new _117.Node(1); + root.left = new _117.Node(2); + root.right = new _117.Node(3); + root.left.left = new _117.Node(4); + root.left.right = new _117.Node(5); + root.right.right = new _117.Node(7); + + solution1.connect(root); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_118Test.java b/src/test/java/com/fishercoder/firstthousand/_118Test.java new file mode 100644 index 0000000000..5746084ac2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_118Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._118; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _118Test { + private _118.Solution1 solution1; + private _118.Solution2 solution2; + private _118.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _118.Solution1(); + solution2 = new _118.Solution2(); + solution3 = new _118.Solution3(); + } + + @Test + public void test1() { + CommonUtils.printListList(solution1.generate(5)); + } + + @Test + public void test2() { + CommonUtils.printListList(solution2.generate(5)); + } + + @Test + public void test3() { + CommonUtils.printListList(solution3.generate(5)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_119Test.java b/src/test/java/com/fishercoder/firstthousand/_119Test.java new file mode 100644 index 0000000000..3a1cbe92c5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_119Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._119; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _119Test { + private _119.Solution1 solution1; + private _119.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _119.Solution1(); + solution2 = new _119.Solution2(); + } + + @Test + public void test1() { + assertEquals(Arrays.asList(1, 3, 3, 1), solution1.getRow(3)); + assertEquals(Arrays.asList(1, 3, 3, 1), solution2.getRow(3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_11Test.java b/src/test/java/com/fishercoder/firstthousand/_11Test.java new file mode 100644 index 0000000000..7b26115275 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_11Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._11; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _11Test { + private _11.Solution1 solution1; + private _11.Solution2 solution2; + private static int[] height; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _11.Solution1(); + solution2 = new _11.Solution2(); + } + + @Test + public void test1() { + height = new int[] {1, 1}; + expected = 1; + assertEquals(expected, solution1.maxArea(height)); + assertEquals(expected, solution2.maxArea(height)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_120Test.java b/src/test/java/com/fishercoder/firstthousand/_120Test.java new file mode 100644 index 0000000000..d6a284f760 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_120Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._120; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _120Test { + private _120.Solution1 solution1; + private static List> triangle; + + @BeforeEach + public void setUp() { + solution1 = new _120.Solution1(); + } + + @Test + public void test1() { + triangle = new ArrayList(); + triangle.add(Arrays.asList(1)); + triangle.add(Arrays.asList(2, 3)); + assertEquals(3, solution1.minimumTotal(triangle)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_121Test.java b/src/test/java/com/fishercoder/firstthousand/_121Test.java new file mode 100644 index 0000000000..f7da7d7c18 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_121Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._121; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _121Test { + private _121.Solution1 solution1; + private _121.Solution2 solution2; + private static int[] prices; + + @BeforeEach + public void setup() { + solution1 = new _121.Solution1(); + solution2 = new _121.Solution2(); + } + + @Test + public void test1() { + prices = new int[] {7, 1, 5, 3, 6, 4}; + assertEquals(5, solution1.maxProfit(prices)); + assertEquals(5, solution2.maxProfit(prices)); + } + + @Test + public void test2() { + prices = new int[] {7, 6, 4, 3, 1}; + assertEquals(0, solution1.maxProfit(prices)); + assertEquals(0, solution2.maxProfit(prices)); + } + + @Test + public void test3() { + prices = new int[] {2, 4, 1}; + assertEquals(2, solution1.maxProfit(prices)); + assertEquals(2, solution2.maxProfit(prices)); + } + + @Test + public void test4() { + prices = new int[] {1, 2}; + assertEquals(1, solution1.maxProfit(prices)); + assertEquals(1, solution2.maxProfit(prices)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_122Test.java b/src/test/java/com/fishercoder/firstthousand/_122Test.java new file mode 100644 index 0000000000..3f386d61ea --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_122Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._122; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _122Test { + private _122.Solution1 solution1; + private _122.Solution2 solution2; + private static int[] prices; + + @BeforeEach + public void setUp() { + solution1 = new _122.Solution1(); + solution2 = new _122.Solution2(); + } + + @Test + public void test1() { + prices = new int[] {1, 2, 4}; + assertEquals(3, solution1.maxProfit(prices)); + assertEquals(3, solution2.maxProfit(prices)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_123Test.java b/src/test/java/com/fishercoder/firstthousand/_123Test.java new file mode 100644 index 0000000000..5212feb7d1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_123Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._123; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _123Test { + private _123.Solution1 solution1; + private static int[] prices; + + @BeforeEach + public void setUp() { + solution1 = new _123.Solution1(); + } + + @Test + public void test1() { + prices = new int[] {1}; + assertEquals(0, solution1.maxProfit(prices)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_125Test.java b/src/test/java/com/fishercoder/firstthousand/_125Test.java new file mode 100644 index 0000000000..e0fa636817 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_125Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._125; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _125Test { + private _125.Solution1 solution1; + private static String s; + + @BeforeEach + public void setup() { + solution1 = new _125.Solution1(); + } + + @Test + public void test1() { + s = "A man, a plan, a canal: Panama"; + assertEquals(true, solution1.isPalindrome(s)); + } + + @Test + public void test2() { + s = "race a car"; + assertEquals(false, solution1.isPalindrome(s)); + } + + @Test + public void test3() { + s = " "; + assertEquals(true, solution1.isPalindrome(s)); + } + + @Test + public void test4() { + s = "0P"; + assertEquals(false, solution1.isPalindrome(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_127Test.java b/src/test/java/com/fishercoder/firstthousand/_127Test.java new file mode 100644 index 0000000000..1d0510e6f2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_127Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._127; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _127Test { + private _127.Solution1 solution1; + private static List wordList; + + @BeforeEach + public void setUp() { + solution1 = new _127.Solution1(); + } + + @Test + public void test1() { + wordList = new ArrayList<>(Arrays.asList("hot", "dot", "dog", "lot", "log")); + assertEquals(0, solution1.ladderLength("hit", "cog", wordList)); + } + + @Test + public void test2() { + wordList = new ArrayList<>(Arrays.asList("hot", "dot", "dog", "lot", "log", "cog")); + assertEquals(5, solution1.ladderLength("hit", "cog", wordList)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_128Test.java b/src/test/java/com/fishercoder/firstthousand/_128Test.java new file mode 100644 index 0000000000..d57256dd0f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_128Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._128; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _128Test { + private _128.Solution3 solution3; + private _128.Solution4 solution4; + private static int[] nums; + + @BeforeEach + public void setup() { + solution3 = new _128.Solution3(); + solution4 = new _128.Solution4(); + } + + @Test + public void test1() { + nums = new int[] {100, 4, 200, 1, 3, 2}; + assertEquals(4, solution3.longestConsecutive(nums)); + assertEquals(4, solution4.longestConsecutive(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_129Test.java b/src/test/java/com/fishercoder/firstthousand/_129Test.java new file mode 100644 index 0000000000..8b3c4a658f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_129Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._129; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _129Test { + private _129.Solution1 solution1; + private _129.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _129.Solution1(); + solution2 = new _129.Solution2(); + } + + @Test + public void test1() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); + assertEquals(25, (solution1.sumNumbers(root))); + assertEquals(25, (solution2.sumNumbers(root))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_12Test.java b/src/test/java/com/fishercoder/firstthousand/_12Test.java new file mode 100644 index 0000000000..f487244d57 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_12Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._12; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _12Test { + private _12.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _12.Solution1(); + } + + @Test + public void test1() { + assertEquals("XII", solution1.intToRoman(12)); + } + + @Test + public void test2() { + assertEquals("M", solution1.intToRoman(1000)); + } + + @Test + public void test3() { + assertEquals("MMMCMXCIX", solution1.intToRoman(3999)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_130Test.java b/src/test/java/com/fishercoder/firstthousand/_130Test.java new file mode 100644 index 0000000000..bb45dd573e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_130Test.java @@ -0,0 +1,56 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._130; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _130Test { + private _130.Solution1 solution1; + private _130.Solution2 solution2; + private char[][] board; + private char[][] expected; + + @BeforeEach + public void setup() { + solution1 = new _130.Solution1(); + solution2 = new _130.Solution2(); + } + + @Test + public void test1() { + board = + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"X\",\"O\",\"X\",\"O\",\"X\",\"O\",\"O\",\"O\",\"X\",\"O\"],[\"X\",\"O\",\"O\",\"X\",\"X\",\"X\",\"O\",\"O\",\"O\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"X\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"X\",\"O\",\"O\",\"X\"]," + + "[\"O\",\"O\",\"X\",\"X\",\"O\",\"X\",\"X\",\"O\",\"O\",\"O\"],[\"X\",\"O\",\"O\",\"X\",\"X\",\"X\",\"O\",\"X\",\"X\",\"O\"],[\"X\",\"O\",\"X\",\"O\",\"O\",\"X\",\"X\",\"O\",\"X\",\"O\"],[\"X\",\"X\",\"O\",\"X\",\"X\",\"O\",\"X\",\"O\",\"O\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"X\",\"O\",\"X\",\"O\",\"X\",\"O\"]," + + "[\"X\",\"X\",\"O\",\"X\",\"X\",\"X\",\"X\",\"O\",\"O\",\"O\"]"); + expected = + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"X\",\"O\",\"X\",\"O\",\"X\",\"O\",\"O\",\"O\",\"X\",\"O\"],[\"X\",\"O\",\"O\",\"X\",\"X\",\"X\",\"O\",\"O\",\"O\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"X\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"X\",\"O\",\"O\",\"X\"]," + + "[\"O\",\"O\",\"X\",\"X\",\"O\",\"X\",\"X\",\"O\",\"O\",\"O\"],[\"X\",\"O\",\"O\",\"X\",\"X\",\"X\",\"X\",\"X\",\"X\",\"O\"],[\"X\",\"O\",\"X\",\"X\",\"X\",\"X\",\"X\",\"O\",\"X\",\"O\"],[\"X\",\"X\",\"O\",\"X\",\"X\",\"X\",\"X\",\"O\",\"O\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"X\",\"X\",\"X\",\"O\",\"X\",\"O\"]," + + "[\"X\",\"X\",\"O\",\"X\",\"X\",\"X\",\"X\",\"O\",\"O\",\"O\"]"); + solution1.solve(board); + assertArrayEquals(expected, board); + } + + @Test + public void test2() { + board = + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"X\",\"O\",\"X\",\"O\",\"X\",\"O\",\"O\",\"O\",\"X\",\"O\"],[\"X\",\"O\",\"O\",\"X\",\"X\",\"X\",\"O\",\"O\",\"O\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"X\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"X\",\"O\",\"O\",\"X\"]," + + "[\"O\",\"O\",\"X\",\"X\",\"O\",\"X\",\"X\",\"O\",\"O\",\"O\"],[\"X\",\"O\",\"O\",\"X\",\"X\",\"X\",\"O\",\"X\",\"X\",\"O\"],[\"X\",\"O\",\"X\",\"O\",\"O\",\"X\",\"X\",\"O\",\"X\",\"O\"],[\"X\",\"X\",\"O\",\"X\",\"X\",\"O\",\"X\",\"O\",\"O\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"X\",\"O\",\"X\",\"O\",\"X\",\"O\"]," + + "[\"X\",\"X\",\"O\",\"X\",\"X\",\"X\",\"X\",\"O\",\"O\",\"O\"]"); + expected = + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"X\",\"O\",\"X\",\"O\",\"X\",\"O\",\"O\",\"O\",\"X\",\"O\"],[\"X\",\"O\",\"O\",\"X\",\"X\",\"X\",\"O\",\"O\",\"O\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"X\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"O\",\"O\",\"X\",\"O\",\"O\",\"X\"]," + + "[\"O\",\"O\",\"X\",\"X\",\"O\",\"X\",\"X\",\"O\",\"O\",\"O\"],[\"X\",\"O\",\"O\",\"X\",\"X\",\"X\",\"X\",\"X\",\"X\",\"O\"],[\"X\",\"O\",\"X\",\"X\",\"X\",\"X\",\"X\",\"O\",\"X\",\"O\"],[\"X\",\"X\",\"O\",\"X\",\"X\",\"X\",\"X\",\"O\",\"O\",\"X\"],[\"O\",\"O\",\"O\",\"O\",\"X\",\"X\",\"X\",\"O\",\"X\",\"O\"]," + + "[\"X\",\"X\",\"O\",\"X\",\"X\",\"X\",\"X\",\"O\",\"O\",\"O\"]"); + CommonUtils.print2DCharArray(board); + solution2.solve(board); + CommonUtils.print2DCharArray(board); + CommonUtils.print2DCharArray(expected); + assertArrayEquals(expected, board); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_131Test.java b/src/test/java/com/fishercoder/firstthousand/_131Test.java new file mode 100644 index 0000000000..64497de0b9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_131Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._131; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _131Test { + private _131.Solution1 solution1; + private static List> expected; + + @BeforeEach + public void setUp() { + solution1 = new _131.Solution1(); + } + + @Test + public void test1() { + expected = new ArrayList(); + expected.add(Arrays.asList("a", "a", "b")); + expected.add(Arrays.asList("aa", "b")); + assertEquals(expected, solution1.partition("aab")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_132Test.java b/src/test/java/com/fishercoder/firstthousand/_132Test.java new file mode 100644 index 0000000000..a0aba295f4 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_132Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._132; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _132Test { + private _132.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _132.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minCut("aab")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_133Test.java b/src/test/java/com/fishercoder/firstthousand/_133Test.java new file mode 100644 index 0000000000..539581ea80 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_133Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._133; +import com.fishercoder.solutions.firstthousand._133.Solution1.Node; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _133Test { + private _133.Solution1 solution1; + private static Node root; + private static Node actual; + + @BeforeEach + public void setup() { + solution1 = new _133.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + root = null; + actual = null; + } + + @Test + public void test1() { + // TODO: implement it + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_134Test.java b/src/test/java/com/fishercoder/firstthousand/_134Test.java new file mode 100644 index 0000000000..c061fefe9c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_134Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._134; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _134Test { + private _134.Solution1 solution1; + private static int[] gas; + private static int[] cost; + + @BeforeEach + public void setUp() { + solution1 = new _134.Solution1(); + } + + @Test + public void test1() { + gas = new int[] {4}; + cost = new int[] {5}; + assertEquals(-1, solution1.canCompleteCircuit(gas, cost)); + } + + @Test + public void test2() { + gas = new int[] {5}; + cost = new int[] {4}; + assertEquals(0, solution1.canCompleteCircuit(gas, cost)); + } + + @Test + public void test3() { + gas = new int[] {2}; + cost = new int[] {2}; + assertEquals(0, solution1.canCompleteCircuit(gas, cost)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_136Test.java b/src/test/java/com/fishercoder/firstthousand/_136Test.java new file mode 100644 index 0000000000..e0089f7a8b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_136Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._136; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _136Test { + private _136.Solution1 solution1; + private _136.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _136.Solution1(); + solution2 = new _136.Solution2(); + } + + @Test + public void test1() { + assertEquals(1, (solution1.singleNumber(new int[] {2, 2, 1}))); + assertEquals(1, (solution2.singleNumber(new int[] {2, 2, 1}))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_139Test.java b/src/test/java/com/fishercoder/firstthousand/_139Test.java new file mode 100644 index 0000000000..f7190ff024 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_139Test.java @@ -0,0 +1,106 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._139; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _139Test { + private _139.Solution1 solution1; + private _139.Solution2 solution2; + private _139.Solution3 solution3; + private static String s; + private static List wordDict; + + @BeforeEach + public void setup() { + solution1 = new _139.Solution1(); + solution2 = new _139.Solution2(); + solution3 = new _139.Solution3(); + } + + @Test + public void test1() { + s = "leetcode"; + wordDict = new ArrayList<>(Arrays.asList("leet", "code")); + assertEquals(true, solution1.wordBreak(s, wordDict)); + } + + @Test + public void test2() { + s = "leetcode"; + wordDict = new ArrayList<>(Arrays.asList("leet", "code")); + assertEquals(true, solution2.wordBreak(s, wordDict)); + } + + @Test + public void test3() { + s = "leetcode"; + wordDict = new ArrayList<>(Arrays.asList("leet", "code")); + assertEquals(true, solution3.wordBreak(s, wordDict)); + } + + @Test + public void test4() { + s = + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; + wordDict = + new ArrayList<>( + Arrays.asList( + "a", + "aa", + "aaa", + "aaaa", + "aaaaa", + "aaaaaa", + "aaaaaaa", + "aaaaaaaa", + "aaaaaaaaa", + "aaaaaaaaaa")); + assertEquals(false, solution1.wordBreak(s, wordDict)); + } + + @Test + public void test5() { + s = + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; + wordDict = + new ArrayList<>( + Arrays.asList( + "a", + "aa", + "aaa", + "aaaa", + "aaaaa", + "aaaaaa", + "aaaaaaa", + "aaaaaaaa", + "aaaaaaaaa", + "aaaaaaaaaa")); + assertEquals(false, solution2.wordBreak(s, wordDict)); + } + + @Test + public void test6() { + s = + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; + wordDict = + new ArrayList<>( + Arrays.asList( + "a", + "aa", + "aaa", + "aaaa", + "aaaaa", + "aaaaaa", + "aaaaaaa", + "aaaaaaaa", + "aaaaaaaaa", + "aaaaaaaaaa")); + assertEquals(false, solution3.wordBreak(s, wordDict)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_13Test.java b/src/test/java/com/fishercoder/firstthousand/_13Test.java new file mode 100644 index 0000000000..1dbb3968af --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_13Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._13; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _13Test { + private _13.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _13.Solution1(); + } + + @Test + public void test1() { + assertEquals(12, solution1.romanToInt("XII")); + } + + @Test + public void test2() { + assertEquals(1000, solution1.romanToInt("M")); + } + + @Test + public void test3() { + assertEquals(3999, solution1.romanToInt("MMMCMXCIX")); + } + + @Test + public void test4() { + assertEquals(3045, solution1.romanToInt("MMMXLV")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_140Test.java b/src/test/java/com/fishercoder/firstthousand/_140Test.java new file mode 100644 index 0000000000..d00d9b96a7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_140Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.firstthousand._140; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _140Test { + private _140.Solution1 solution1; + private static String s; + private static List wordDict; + + @BeforeEach + public void setup() { + solution1 = new _140.Solution1(); + } + + @Test + public void test1() { + s = "catsanddog"; + wordDict = new ArrayList<>(Arrays.asList("cat", "cats", "and", "sand", "dog")); + List actual = solution1.wordBreak(s, wordDict); + List expected = Arrays.asList("cats and dog", "cat sand dog"); + // assert equals ignoring order + assertTrue( + expected.size() == actual.size() + && actual.containsAll(expected) + && expected.containsAll(actual)); + } + + @Test + public void test2() { + s = "pineapplepenapple"; + wordDict = new ArrayList<>(Arrays.asList("apple", "pen", "applepen", "pine", "pineapple")); + List actual = solution1.wordBreak(s, wordDict); + List expected = + Arrays.asList("pine apple pen apple", "pineapple pen apple", "pine applepen apple"); + // assert equals ignoring order + assertTrue( + expected.size() == actual.size() + && actual.containsAll(expected) + && expected.containsAll(actual)); + } + + @Test + public void test3() { + s = "catsandog"; + wordDict = new ArrayList<>(Arrays.asList("cats", "dog", "sand", "and", "cat")); + List actual = solution1.wordBreak(s, wordDict); + List expected = Arrays.asList(); + // assert equals ignoring order + assertTrue( + expected.size() == actual.size() + && actual.containsAll(expected) + && expected.containsAll(actual)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_141Test.java b/src/test/java/com/fishercoder/firstthousand/_141Test.java new file mode 100644 index 0000000000..8f2fcdfee7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_141Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._141; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _141Test { + private _141.Solution1 solution1; + private _141.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _141.Solution1(); + solution2 = new _141.Solution2(); + } + + @Test + public void test1() { + assertEquals( + false, + solution1.hasCycle(LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4}))); + } + + @Test + public void test2() { + assertEquals( + false, + solution2.hasCycle(LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4}))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_143Test.java b/src/test/java/com/fishercoder/firstthousand/_143Test.java new file mode 100644 index 0000000000..b8a864afd3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_143Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._143; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _143Test { + private _143.Solution1 solution1; + private _143.Solution2 solution2; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _143.Solution1(); + solution2 = new _143.Solution2(); + } + + @Test + public void test1() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); + expected = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 7, 2, 6, 3, 5, 4)); + solution1.reorderList(head); + assertEquals(expected, head); + } + + @Test + public void test2() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); + expected = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 7, 2, 6, 3, 5, 4)); + solution2.reorderList(head); + assertEquals(expected, head); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_144Test.java b/src/test/java/com/fishercoder/firstthousand/_144Test.java new file mode 100644 index 0000000000..14dc5bef30 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_144Test.java @@ -0,0 +1,78 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._144; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _144Test { + private _144.Solution1 solution1; + private _144.Solution2 solution2; + private _144.Solution3 solution3; + private static TreeNode root; + private static List inorder; + + @BeforeEach + public void setup() { + solution1 = new _144.Solution1(); + solution2 = new _144.Solution2(); + solution3 = new _144.Solution3(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4)); + inorder = solution1.preorderTraversal(root); + assertEquals(Arrays.asList(3, 1, 5, 2, 4), inorder); + } + + @Test + public void test2() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9)); + TreeUtils.printBinaryTree(root); + inorder = solution1.preorderTraversal(root); + assertEquals(Arrays.asList(1, 2, 4, 7, 8, 9, 3, 5, 6), inorder); + } + + @Test + public void test3() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4)); + inorder = solution2.preorderTraversal(root); + assertEquals(Arrays.asList(3, 1, 5, 2, 4), inorder); + } + + @Test + public void test4() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9)); + TreeUtils.printBinaryTree(root); + inorder = solution2.preorderTraversal(root); + assertEquals(Arrays.asList(1, 2, 4, 7, 8, 9, 3, 5, 6), inorder); + } + + @Test + public void test5() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, null, null, 6)); + TreeUtils.printBinaryTree(root); + inorder = solution1.preorderTraversal(root); + assertEquals(Arrays.asList(1, 2, 4, 6, 3, 5), inorder); + } + + @Test + public void test6() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, null, null, 6)); + TreeUtils.printBinaryTree(root); + inorder = solution3.preorderTraversal(root); + assertEquals(Arrays.asList(1, 2, 4, 6, 3, 5), inorder); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_145Test.java b/src/test/java/com/fishercoder/firstthousand/_145Test.java new file mode 100644 index 0000000000..d445a9ded1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_145Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._145; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _145Test { + private _145.Solution1 solution1; + private _145.Solution2 solution2; + private _145.Solution3 solution3; + private static TreeNode root; + private static List expected; + + @BeforeEach + public void setup() { + solution1 = new _145.Solution1(); + solution2 = new _145.Solution2(); + solution3 = new _145.Solution3(); + } + + @Test + public void test1() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9)); + TreeUtils.printBinaryTree(root); + CommonUtils.printList(solution1.postorderTraversal(root)); + CommonUtils.printList(solution2.postorderTraversal(root)); + CommonUtils.printList(solution3.postorderTraversal(root)); + expected = new ArrayList<>(Arrays.asList(8, 9, 7, 4, 2, 5, 6, 3, 1)); + assertEquals(expected, solution1.postorderTraversal(root)); + assertEquals(expected, solution2.postorderTraversal(root)); + assertEquals(expected, solution3.postorderTraversal(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_148Test.java b/src/test/java/com/fishercoder/firstthousand/_148Test.java new file mode 100644 index 0000000000..f9d71c1112 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_148Test.java @@ -0,0 +1,54 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._148; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _148Test { + private _148.Solution1 solution1; + private _148.Solution2 solution2; + private _148.Solution3 solution3; + private _148.Solution4 solution4; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _148.Solution1(); + solution2 = new _148.Solution2(); + solution3 = new _148.Solution3(); + solution4 = new _148.Solution4(); + } + + @Test + public void test1() { + head = LinkedListUtils.contructLinkedList(new int[] {4, 2, 1, 3}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4}); + assertEquals(expected, solution1.sortList(head)); + } + + @Test + public void test2() { + head = LinkedListUtils.contructLinkedList(new int[] {4, 2, 1, 3}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4}); + assertEquals(expected, solution2.sortList(head)); + } + + @Test + public void test3() { + head = LinkedListUtils.contructLinkedList(new int[] {4, 2, 1, 3}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4}); + assertEquals(expected, solution3.sortList(head)); + } + + @Test + public void test4() { + head = LinkedListUtils.contructLinkedList(new int[] {4, 2, 1, 3}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4}); + assertEquals(expected, solution4.sortList(head)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_149Test.java b/src/test/java/com/fishercoder/firstthousand/_149Test.java new file mode 100644 index 0000000000..a90f70fb86 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_149Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._149; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _149Test { + private _149.Solution1 solution1; + private static int[][] points; + + @BeforeEach + public void setup() { + solution1 = new _149.Solution1(); + } + + @Test + public void test1() { + points = + new int[][] { + {1, 1}, + {2, 2}, + {3, 3} + }; + assertEquals(3, solution1.maxPoints(points)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_14Test.java b/src/test/java/com/fishercoder/firstthousand/_14Test.java new file mode 100644 index 0000000000..c603f497e3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_14Test.java @@ -0,0 +1,68 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._14; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _14Test { + private _14.Solution1 solution1; + private _14.Solution2 solution2; + private static String[] strs; + + @BeforeEach + public void setup() { + solution1 = new _14.Solution1(); + solution2 = new _14.Solution2(); + } + + @Test + public void test1() { + strs = new String[] {"a", "b"}; + assertEquals("", solution1.longestCommonPrefix(strs)); + assertEquals("", solution2.longestCommonPrefix(strs)); + } + + @Test + public void test2() { + strs = new String[] {"leetcode", "lead"}; + assertEquals("le", solution1.longestCommonPrefix(strs)); + assertEquals("le", solution2.longestCommonPrefix(strs)); + } + + @Test + public void test3() { + strs = new String[] {"leetcode", "code"}; + assertEquals("", solution1.longestCommonPrefix(strs)); + assertEquals("", solution2.longestCommonPrefix(strs)); + } + + @Test + public void test4() { + strs = new String[] {"flower", "flow", "flight"}; + assertEquals("fl", solution1.longestCommonPrefix(strs)); + assertEquals("fl", solution2.longestCommonPrefix(strs)); + } + + @Test + public void test5() { + strs = new String[] {}; + assertEquals("", solution1.longestCommonPrefix(strs)); + assertEquals("", solution2.longestCommonPrefix(strs)); + } + + @Test + public void test6() { + strs = new String[] {"a"}; + assertEquals("a", solution1.longestCommonPrefix(strs)); + assertEquals("a", solution2.longestCommonPrefix(strs)); + } + + @Test + public void test7() { + strs = new String[] {"c", "c"}; + assertEquals("c", solution1.longestCommonPrefix(strs)); + assertEquals("c", solution2.longestCommonPrefix(strs)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_150Test.java b/src/test/java/com/fishercoder/firstthousand/_150Test.java new file mode 100644 index 0000000000..aaba7ebe91 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_150Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._150; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _150Test { + private _150.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _150.Solution1(); + } + + @Test + public void test1() { + assertEquals(9, solution1.evalRPN(new String[] {"2", "1", "+", "3", "*"})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_151Test.java b/src/test/java/com/fishercoder/firstthousand/_151Test.java new file mode 100644 index 0000000000..fe246f6ee5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_151Test.java @@ -0,0 +1,59 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._151; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _151Test { + private _151.Solution1 solution1; + private _151.Solution2 solution2; + private _151.Solution3 solution3; + private static String s; + + @BeforeEach + public void setup() { + solution1 = new _151.Solution1(); + solution2 = new _151.Solution2(); + solution3 = new _151.Solution3(); + } + + @Test + public void test1() { + s = " "; + assertEquals("", solution1.reverseWords(s)); + } + + @Test + public void test2() { + s = " 1"; + assertEquals("1", solution1.reverseWords(s)); + } + + @Test + public void test3() { + s = " a b "; + assertEquals("b a", solution1.reverseWords(s)); + } + + @Test + public void test4() { + s = "a b c"; + assertEquals("c b a", solution1.reverseWords(s)); + } + + @Test + public void test5() { + s = " hello world "; + assertEquals("world hello", solution2.reverseWords(s)); + assertEquals("world hello", solution3.reverseWords(s)); + } + + @Test + public void test6() { + s = "Bob Loves Alice "; + assertEquals("Alice Loves Bob", solution2.reverseWords(s)); + assertEquals("Alice Loves Bob", solution3.reverseWords(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_153Test.java b/src/test/java/com/fishercoder/firstthousand/_153Test.java new file mode 100644 index 0000000000..2784b4aea7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_153Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._153; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _153Test { + private _153.Solution1 solution1; + private static int expected; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _153.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {4, 5, 6, 7, 0, 1, 2}; + expected = 0; + assertEquals(expected, solution1.findMin(nums)); + } + + @Test + public void test2() { + nums = new int[] {1}; + expected = 1; + assertEquals(expected, solution1.findMin(nums)); + } + + @Test + public void test3() { + nums = new int[] {2, 1}; + expected = 1; + assertEquals(expected, solution1.findMin(nums)); + } + + @Test + public void test4() { + nums = new int[] {2, 3, 4, 5, 1}; + expected = 1; + assertEquals(expected, solution1.findMin(nums)); + } + + @Test + public void test5() { + nums = new int[] {3, 1, 2}; + expected = 1; + assertEquals(expected, solution1.findMin(nums)); + } + + @Test + public void test6() { + nums = new int[] {3, 4, 5, 1, 2}; + expected = 1; + assertEquals(expected, solution1.findMin(nums)); + } + + @Test + public void test7() { + nums = new int[] {5, 1, 2, 3, 4}; + expected = 1; + assertEquals(expected, solution1.findMin(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_154Test.java b/src/test/java/com/fishercoder/firstthousand/_154Test.java new file mode 100644 index 0000000000..c08c83cfc5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_154Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._154; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _154Test { + private _154.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _154.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 1, 1}; + assertEquals(1, solution1.findMin(nums)); + } + + @Test + public void test2() { + nums = new int[] {4, 5, 6, 7, 0, 1, 4}; + assertEquals(0, solution1.findMin(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 3, 5}; + assertEquals(1, solution1.findMin(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_155Test.java b/src/test/java/com/fishercoder/firstthousand/_155Test.java new file mode 100644 index 0000000000..6ec570fb2e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_155Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._155; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _155Test { + private _155.Solution1.MinStack minStack1; + private _155.Solution2.MinStack minStack2; + + @BeforeEach + public void setup() { + minStack1 = new _155.Solution1.MinStack(); + minStack2 = new _155.Solution2.MinStack(); + } + + @Test + public void test1() { + minStack1.push(-2); + minStack1.push(0); + minStack1.push(-3); + assertEquals(-3, minStack1.getMin()); + minStack1.pop(); + assertEquals(0, minStack1.top()); + assertEquals(-2, minStack1.getMin()); + } + + @Test + public void test2() { + minStack2.push(-2); + minStack2.push(0); + minStack2.push(-3); + assertEquals(-3, minStack2.getMin()); + minStack2.pop(); + assertEquals(0, minStack2.top()); + assertEquals(-2, minStack2.getMin()); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_156Test.java b/src/test/java/com/fishercoder/firstthousand/_156Test.java new file mode 100644 index 0000000000..3c89354776 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_156Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._156; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _156Test { + private _156.Solution1 solution1; + private static TreeNode root; + private static TreeNode expected; + + @BeforeEach + public void setup() { + solution1 = new _156.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); + expected = TreeUtils.constructBinaryTree(Arrays.asList(4, 5, 2, null, null, 3, 1)); + assertEquals(expected, solution1.upsideDownBinaryTree(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_159Test.java b/src/test/java/com/fishercoder/firstthousand/_159Test.java new file mode 100644 index 0000000000..d5be0ee448 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_159Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._159; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _159Test { + private _159.Solution1 solution1; + private _159.Solution2 solution2; + private static String s; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _159.Solution1(); + solution2 = new _159.Solution2(); + } + + @Test + public void test1() { + s = "eceba"; + expected = 3; + assertEquals(expected, solution1.lengthOfLongestSubstringTwoDistinct(s)); + assertEquals(expected, solution2.lengthOfLongestSubstringTwoDistinct(s)); + } + + @Test + public void test2() { + s = "ccaabbb"; + expected = 5; + assertEquals(expected, solution1.lengthOfLongestSubstringTwoDistinct(s)); + assertEquals(expected, solution2.lengthOfLongestSubstringTwoDistinct(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_15Test.java b/src/test/java/com/fishercoder/firstthousand/_15Test.java new file mode 100644 index 0000000000..0c7437f30c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_15Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._15; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _15Test { + private _15.Solution1 solution1; + private static int[] nums; + private static List> expected; + + @BeforeEach + public void setup() { + solution1 = new _15.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {-1, 0, 1, 2, -1, -4}; + expected = new ArrayList<>(); + expected.add(Arrays.asList(-1, -1, 2)); + expected.add(Arrays.asList(-1, 0, 1)); + assertEquals(expected, solution1.threeSum(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2, -2, -1}; + expected = new ArrayList<>(); + assertEquals(expected, solution1.threeSum(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_160Test.java b/src/test/java/com/fishercoder/firstthousand/_160Test.java new file mode 100644 index 0000000000..1e4511ec37 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_160Test.java @@ -0,0 +1,62 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.solutions.firstthousand._160; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +public class _160Test { + private _160.Solution1 solution1; + private _160.Solution2 solution2; + private _160.Solution3 solution3; + private ListNode headA; + private ListNode headB; + private ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _160.Solution1(); + solution2 = new _160.Solution2(); + solution3 = new _160.Solution3(); + } + + @Test + @Disabled + public void test1() { + headA = new ListNode(3); + headB = new ListNode(2); + headB.next = new ListNode(3); + expected = new ListNode(3); + /** + * TODO: both solution1 and solution2 are ACCEPTED on OJ, but somehow it's not passing in + * this unit test. + */ + assertEquals(expected, solution1.getIntersectionNode(headA, headB)); + } + + @Test + @Disabled + public void test2() { + headA = new ListNode(3); + headB = new ListNode(2); + headB.next = new ListNode(3); + expected = new ListNode(3); + /** + * TODO: both solution1 and solution2 are ACCEPTED on OJ, but somehow it's not passing in + * this unit test. + */ + assertEquals(expected, solution2.getIntersectionNode(headA, headB)); + } + + @Test + public void test3() { + headA = new ListNode(3); + headB = new ListNode(2); + headB.next = new ListNode(3); + expected = new ListNode(3); + assertEquals(expected, solution3.getIntersectionNode(headA, headB)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_161Test.java b/src/test/java/com/fishercoder/firstthousand/_161Test.java new file mode 100644 index 0000000000..d99baf6fe9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_161Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._161; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _161Test { + private _161.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _161.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isOneEditDistance("a", "ac")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_162Test.java b/src/test/java/com/fishercoder/firstthousand/_162Test.java new file mode 100644 index 0000000000..d22ee58aa9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_162Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._162; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _162Test { + private _162.Solution1 solution1; + private _162.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _162.Solution1(); + solution2 = new _162.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 2}; + assertEquals(1, solution1.findPeakElement(nums)); + assertEquals(1, solution2.findPeakElement(nums)); + } + + @Test + public void test2() { + nums = new int[] {1}; + assertEquals(0, solution1.findPeakElement(nums)); + assertEquals(0, solution2.findPeakElement(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 3, 1}; + assertEquals(2, solution1.findPeakElement(nums)); + assertEquals(2, solution2.findPeakElement(nums)); + } + + @Test + public void test4() { + nums = new int[] {1, 2, 1, 3, 5, 6, 4}; + assertEquals(5, solution1.findPeakElement(nums)); + assertEquals(1, solution2.findPeakElement(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_163Test.java b/src/test/java/com/fishercoder/firstthousand/_163Test.java new file mode 100644 index 0000000000..90bcc88fce --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_163Test.java @@ -0,0 +1,77 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._163; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _163Test { + + private _163.Solution1 solution1; + private static List> expected; + private static List> actual; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _163.Solution1(); + expected = new ArrayList(); + actual = new ArrayList(); + + expected.clear(); + actual.clear(); + } + + @Test + public void test1() { + nums = new int[] {2147483647}; + expected.add(Arrays.asList(0, 2147483646)); + actual = solution1.findMissingRanges(nums, 0, 2147483647); + assertEquals(expected, actual); + } + + @Test + public void test2() { + nums = new int[] {-2147483648, 0, 2147483647}; + expected.add(Arrays.asList(-2147483647, -1)); + expected.add(Arrays.asList(1, 2147483646)); + actual = solution1.findMissingRanges(nums, -2147483648, 2147483647); + assertEquals(expected, actual); + } + + @Test + public void test3() { + nums = new int[] {}; + expected.add(Arrays.asList(-2147483648, 2147483647)); + actual = solution1.findMissingRanges(nums, -2147483648, 2147483647); + assertEquals(expected, actual); + } + + @Test + public void test4() { + nums = new int[] {2147483647}; + expected.add(Arrays.asList(-2147483648, 2147483646)); + actual = solution1.findMissingRanges(nums, -2147483648, 2147483647); + assertEquals(expected, actual); + } + + @Test + public void test5() { + nums = new int[] {}; + expected.add(Arrays.asList(0, 2147483647)); + actual = solution1.findMissingRanges(nums, 0, 2147483647); + assertEquals(expected, actual); + } + + @Test + public void test6() { + nums = new int[] {-2147483648}; + expected.add(Arrays.asList(-2147483647, 2147483647)); + actual = solution1.findMissingRanges(nums, -2147483648, 2147483647); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_164Test.java b/src/test/java/com/fishercoder/firstthousand/_164Test.java new file mode 100644 index 0000000000..0b75beb5a4 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_164Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._164; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _164Test { + private _164.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setUp() { + solution1 = new _164.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {}; + assertEquals(0, solution1.maximumGap(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 3, 6, 5}; + assertEquals(2, solution1.maximumGap(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 100000}; + assertEquals(99999, solution1.maximumGap(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_165Test.java b/src/test/java/com/fishercoder/firstthousand/_165Test.java new file mode 100644 index 0000000000..81e77f3e24 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_165Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._165; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _165Test { + private _165.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _165.Solution1(); + } + + @Test + public void test1() { + assertEquals(-1, solution1.compareVersion("1.1", "1.2")); + } + + @Test + public void test2() { + assertEquals(1, solution1.compareVersion("1.0.1", "1")); + } + + @Test + public void test3() { + assertEquals(-0, solution1.compareVersion("1.0", "1")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_166Test.java b/src/test/java/com/fishercoder/firstthousand/_166Test.java new file mode 100644 index 0000000000..6cd4307e08 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_166Test.java @@ -0,0 +1,57 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._166; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _166Test { + private _166.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _166.Solution1(); + } + + @Test + public void test1() { + assertEquals("0.5", solution1.fractionToDecimal(1, 2)); + } + + @Test + public void test2() { + assertEquals("2", solution1.fractionToDecimal(2, 1)); + } + + @Test + public void test3() { + assertEquals("0.(6)", solution1.fractionToDecimal(2, 3)); + } + + @Test + public void test4() { + assertEquals("-6.25", solution1.fractionToDecimal(-50, 8)); + } + + @Test + public void test5() { + assertEquals("-0.58(3)", solution1.fractionToDecimal(7, -12)); + } + + @Test + public void test6() { + assertEquals( + "0.0000000004656612873077392578125", solution1.fractionToDecimal(-1, -2147483648)); + } + + @Test + public void test7() { + assertEquals("0", solution1.fractionToDecimal(0, -5)); + } + + @Test + public void test8() { + assertEquals("-2147483648", solution1.fractionToDecimal(-2147483648, 1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_167Test.java b/src/test/java/com/fishercoder/firstthousand/_167Test.java new file mode 100644 index 0000000000..46c2e97e8f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_167Test.java @@ -0,0 +1,62 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._167; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _167Test { + private _167.Solution1 solution1; + private _167.Solution2 solution2; + private _167.Solution3 solution3; + private static int[] numbers; + private static int[] expected; + private int target; + + @BeforeEach + public void setup() { + solution1 = new _167.Solution1(); + solution2 = new _167.Solution2(); + solution3 = new _167.Solution3(); + } + + @Test + public void test1() { + numbers = new int[] {-3, 3, 4, 90}; + expected = new int[] {1, 2}; + target = 0; + assertArrayEquals(expected, solution1.twoSum(numbers, target)); + assertArrayEquals(expected, solution2.twoSum(numbers, target)); + assertArrayEquals(expected, solution3.twoSum(numbers, target)); + } + + @Test + public void test2() { + expected = new int[] {2, 3}; + target = 100; + assertArrayEquals(expected, solution1.twoSum(new int[] {5, 25, 75}, target)); + assertArrayEquals(expected, solution2.twoSum(new int[] {5, 25, 75}, target)); + assertArrayEquals(expected, solution3.twoSum(new int[] {5, 25, 75}, target)); + } + + @Test + public void test3() { + numbers = new int[] {1, 2, 3, 4, 4, 9, 56, 90}; + expected = new int[] {4, 5}; + target = 8; + assertArrayEquals(expected, solution1.twoSum(numbers, target)); + assertArrayEquals(expected, solution2.twoSum(numbers, target)); + assertArrayEquals(expected, solution3.twoSum(numbers, target)); + } + + @Test + public void test4() { + numbers = new int[] {2, 3, 4}; + expected = new int[] {1, 3}; + target = 6; + assertArrayEquals(expected, solution1.twoSum(numbers, target)); + assertArrayEquals(expected, solution2.twoSum(numbers, target)); + assertArrayEquals(expected, solution3.twoSum(numbers, target)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_168Test.java b/src/test/java/com/fishercoder/firstthousand/_168Test.java new file mode 100644 index 0000000000..a238196a45 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_168Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._168; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _168Test { + private _168.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _168.Solution1(); + } + + @Test + public void test1() { + assertEquals("APSM", solution1.convertToTitle(28899)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_169Test.java b/src/test/java/com/fishercoder/firstthousand/_169Test.java new file mode 100644 index 0000000000..7ddc8df811 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_169Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._169; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _169Test { + private _169.Solution1 solution1; + private _169.Solution2 solution2; + private _169.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _169.Solution1(); + solution2 = new _169.Solution2(); + solution3 = new _169.Solution3(); + } + + @Test + public void test1() { + assertEquals( + 1, solution1.majorityElement(new int[] {1, 3, 1, 1, 4, 1, 1, 5, 1, 1, 6, 2, 2})); + assertEquals( + 1, solution2.majorityElement(new int[] {1, 3, 1, 1, 4, 1, 1, 5, 1, 1, 6, 2, 2})); + assertEquals( + 1, solution3.majorityElement(new int[] {1, 3, 1, 1, 4, 1, 1, 5, 1, 1, 6, 2, 2})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_16Test.java b/src/test/java/com/fishercoder/firstthousand/_16Test.java new file mode 100644 index 0000000000..891b6a7dc8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_16Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._16; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _16Test { + private _16.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _16.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {-1, 2, 1, -4}; + assertEquals(2, solution1.threeSumClosest(nums, 1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_171Test.java b/src/test/java/com/fishercoder/firstthousand/_171Test.java new file mode 100644 index 0000000000..b8e6b491ca --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_171Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._171; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _171Test { + private _171.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _171.Solution1(); + } + + @Test + public void test1() { + assertEquals(28, solution1.titleToNumber("AB")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_174Test.java b/src/test/java/com/fishercoder/firstthousand/_174Test.java new file mode 100644 index 0000000000..596da26997 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_174Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._174; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _174Test { + private _174.Solution1 solution1; + private int[][] dungeon; + + @BeforeEach + public void setUp() { + solution1 = new _174.Solution1(); + } + + @Test + public void test1() { + dungeon = new int[][] {{0}}; + assertEquals(1, solution1.calculateMinimumHP(dungeon)); + } + + @Test + public void test2() { + dungeon = new int[][] {{-200}}; + assertEquals(201, solution1.calculateMinimumHP(dungeon)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_179Test.java b/src/test/java/com/fishercoder/firstthousand/_179Test.java new file mode 100644 index 0000000000..d88ec952ad --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_179Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._179; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _179Test { + private _179.Solution1 solution1; + private _179.Solution2 solution2; + private static int[] nums; + private static String expected; + + @BeforeEach + public void setup() { + solution1 = new _179.Solution1(); + solution2 = new _179.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {34323, 3432}; + expected = "343234323"; + assertEquals(expected, solution1.largestNumber(nums)); + assertEquals(expected, solution2.largestNumber(nums)); + } + + @Test + public void test2() { + nums = new int[] {111311, 1113}; + expected = "1113111311"; + assertEquals(expected, solution1.largestNumber(nums)); + assertEquals(expected, solution2.largestNumber(nums)); + } + + @Test + public void test3() { + nums = new int[] {3, 30, 34, 5, 9}; + expected = "9534330"; + assertEquals(expected, solution1.largestNumber(nums)); + assertEquals(expected, solution2.largestNumber(nums)); + } + + @Test + public void test4() { + nums = new int[] {0, 0}; + expected = "0"; + assertEquals(expected, solution1.largestNumber(nums)); + assertEquals(expected, solution2.largestNumber(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_17Test.java b/src/test/java/com/fishercoder/firstthousand/_17Test.java new file mode 100644 index 0000000000..7162fe2070 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_17Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._17; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _17Test { + private _17.Solution1 solution1; + private _17.Solution2 solution2; + private _17.Solution3 solution3; + private static String digits; + private static List expected; + + @BeforeEach + public void setup() { + solution1 = new _17.Solution1(); + solution2 = new _17.Solution2(); + solution3 = new _17.Solution3(); + } + + @Test + public void test1() { + digits = "2"; + expected = new ArrayList<>(Arrays.asList("a", "b", "c")); + assertEquals(expected, solution1.letterCombinations(digits)); + assertEquals(expected, solution2.letterCombinations(digits)); + assertEquals(expected, solution3.letterCombinations(digits)); + } + + @Test + public void test2() { + digits = "23"; + expected = + new ArrayList<>( + Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")); + Collections.sort(expected); + List actual = solution1.letterCombinations(digits); + Collections.sort(actual); + assertEquals(expected, actual); + actual = solution2.letterCombinations(digits); + assertEquals(expected, actual); + actual = solution3.letterCombinations(digits); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_186Test.java b/src/test/java/com/fishercoder/firstthousand/_186Test.java new file mode 100644 index 0000000000..ace3cefd34 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_186Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._186; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _186Test { + private _186.Solution1 solution1; + private static char[] s; + private static char[] expected; + + @BeforeEach + public void setUp() { + solution1 = new _186.Solution1(); + } + + @Test + public void test1() { + s = new char[] {'h', 'i', '!'}; + solution1.reverseWords(s); + expected = new char[] {'h', 'i', '!'}; + assertArrayEquals(expected, s); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_187Test.java b/src/test/java/com/fishercoder/firstthousand/_187Test.java new file mode 100644 index 0000000000..cf1de8891f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_187Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._187; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _187Test { + private _187.Solution1 solution1; + private _187.Solution2 solution2; + private static String s; + private static List expected; + + @BeforeEach + public void setup() { + solution1 = new _187.Solution1(); + solution2 = new _187.Solution2(); + } + + @Test + public void test1() { + s = "AAAAAAAAAAA"; + System.out.println(s.length()); + expected = new ArrayList<>(Arrays.asList("AAAAAAAAAA")); + assertEquals(expected, solution1.findRepeatedDnaSequences(s)); + assertEquals(expected, solution2.findRepeatedDnaSequences(s)); + } + + @Test + public void test2() { + s = "AAAAAAAAAAAAA"; + System.out.println(s.length()); + expected = new ArrayList<>(Arrays.asList("AAAAAAAAAA")); + assertEquals(expected, solution1.findRepeatedDnaSequences(s)); + assertEquals(expected, solution2.findRepeatedDnaSequences(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_189Test.java b/src/test/java/com/fishercoder/firstthousand/_189Test.java new file mode 100644 index 0000000000..64ee2469dc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_189Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._189; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _189Test { + private _189.Solution1 solution1; + private _189.Solution2 solution2; + private _189.Solution3 solution3; + private _189.Solution4 solution4; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _189.Solution1(); + solution2 = new _189.Solution2(); + solution3 = new _189.Solution3(); + solution4 = new _189.Solution4(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3}; + solution1.rotate(nums, 1); + assertArrayEquals(new int[] {3, 1, 2}, nums); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 3}; + solution2.rotate(nums, 1); + assertArrayEquals(new int[] {3, 1, 2}, nums); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 3}; + solution3.rotate(nums, 1); + assertArrayEquals(new int[] {3, 1, 2}, nums); + } + + @Test + public void test4() { + nums = new int[] {1, 2, 3}; + solution4.rotate(nums, 1); + assertArrayEquals(new int[] {3, 1, 2}, nums); + } + + @Test + public void test5() { + nums = new int[] {-1, -100, 3, 99}; + solution4.rotate(nums, 2); + assertArrayEquals(new int[] {3, 99, -1, -100}, nums); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_18Test.java b/src/test/java/com/fishercoder/firstthousand/_18Test.java new file mode 100644 index 0000000000..11b8702e87 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_18Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._18; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _18Test { + private _18.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _18.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 0, -1, 0, -2, 2}; + CommonUtils.printListList(solution1.fourSum(nums, 0)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_190Test.java b/src/test/java/com/fishercoder/firstthousand/_190Test.java new file mode 100644 index 0000000000..0336b87f90 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_190Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._190; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _190Test { + private _190.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _190.Solution1(); + } + + @Test + public void test1() { + assertEquals(536870912, solution1.reverseBits(4)); + } + + @Test + public void test2() { + assertEquals(964176192, solution1.reverseBits(43261596)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_191Test.java b/src/test/java/com/fishercoder/firstthousand/_191Test.java new file mode 100644 index 0000000000..7c6f7362ad --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_191Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._191; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _191Test { + private _191.Solution1 solution1; + private _191.Solution2 solution2; + private _191.Solution3 solution3; + private _191.Solution4 solution4; + + @BeforeEach + public void setup() { + solution1 = new _191.Solution1(); + solution2 = new _191.Solution2(); + solution3 = new _191.Solution3(); + solution4 = new _191.Solution4(); + } + + @Test + public void test1() { + assertEquals(1, solution1.hammingWeight(1)); + assertEquals(1, solution2.hammingWeight(1)); + assertEquals(1, solution3.hammingWeight(1)); + assertEquals(1, solution4.hammingWeight(1)); + } + + @Test + public void test2() { + // System.out.println(Integer.MAX_VALUE); + // assertEquals(2147483648, Integer.MAX_VALUE); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_198Test.java b/src/test/java/com/fishercoder/firstthousand/_198Test.java new file mode 100644 index 0000000000..a1b6a501d4 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_198Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._198; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _198Test { + private _198.Solution1 solution1; + private _198.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _198.Solution1(); + solution2 = new _198.Solution2(); + } + + @Test + public void test1() { + assertEquals(4, solution1.rob(new int[] {1, 2, 3, 1})); + assertEquals(4, solution2.rob(new int[] {1, 2, 3, 1})); + } + + @Test + public void test2() { + assertEquals(4, solution1.rob(new int[] {2, 1, 1, 2})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_199Test.java b/src/test/java/com/fishercoder/firstthousand/_199Test.java new file mode 100644 index 0000000000..ebaca8ba46 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_199Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._199; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _199Test { + private _199.Solution1 solution1; + private _199.Solution2 solution2; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _199.Solution1(); + solution2 = new _199.Solution2(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 3)); + assertEquals(Arrays.asList(1, 3), solution1.rightSideView(root)); + assertEquals(Arrays.asList(1, 3), solution2.rightSideView(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_19Test.java b/src/test/java/com/fishercoder/firstthousand/_19Test.java new file mode 100644 index 0000000000..74d8c64967 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_19Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._19; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _19Test { + private _19.Solution1 solution1; + private _19.Solution3 solution3; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _19.Solution1(); + solution3 = new _19.Solution3(); + } + + @Test + public void test1() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 5}); + assertEquals(expected, solution1.removeNthFromEnd(head, 2)); + } + + @Test + public void test2() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 5}); + assertEquals(expected, solution3.removeNthFromEnd(head, 2)); + } + + @Test + public void test3() { + head = LinkedListUtils.contructLinkedList(new int[] {1}); + expected = LinkedListUtils.contructLinkedList(new int[] {}); + assertEquals(expected, solution1.removeNthFromEnd(head, 1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_1Test.java b/src/test/java/com/fishercoder/firstthousand/_1Test.java new file mode 100644 index 0000000000..97f07191ca --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_1Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._1; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1Test { + private _1.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 7, 11, 15}; + assertArrayEquals(new int[] {0, 1}, solution1.twoSum(nums, 9)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_200Test.java b/src/test/java/com/fishercoder/firstthousand/_200Test.java new file mode 100644 index 0000000000..197751aa1a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_200Test.java @@ -0,0 +1,80 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._200; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _200Test { + private _200.Solution1 solution1; + private _200.Solution2 solution2; + private char[][] grid; + + @BeforeEach + public void setup() { + solution1 = new _200.Solution1(); + solution2 = new _200.Solution2(); + } + + @Test + public void test1() { + grid = + new char[][] { + {'1', '1', '1'}, + {'0', '1', '0'}, + {'1', '1', '1'}, + }; + assertEquals(1, solution1.numIslands(grid)); + + grid = + new char[][] { + {'1', '1', '1'}, + {'0', '1', '0'}, + {'1', '1', '1'}, + }; + assertEquals(1, solution2.numIslands(grid)); + } + + @Test + public void test2() { + grid = + new char[][] { + {'1', '1', '1', '1', '0'}, + {'1', '1', '0', '1', '0'}, + {'1', '1', '0', '0', '0'}, + {'0', '0', '0', '0', '0'}, + }; + assertEquals(1, solution1.numIslands(grid)); + + grid = + new char[][] { + {'1', '1', '1', '1', '0'}, + {'1', '1', '0', '1', '0'}, + {'1', '1', '0', '0', '0'}, + {'0', '0', '0', '0', '0'}, + }; + assertEquals(1, solution2.numIslands(grid)); + } + + @Test + public void test3() { + grid = + new char[][] { + {'1', '1', '0', '0', '0'}, + {'1', '1', '0', '0', '0'}, + {'0', '0', '1', '0', '0'}, + {'0', '0', '0', '1', '1'}, + }; + assertEquals(3, solution1.numIslands(grid)); + + grid = + new char[][] { + {'1', '1', '0', '0', '0'}, + {'1', '1', '0', '0', '0'}, + {'0', '0', '1', '0', '0'}, + {'0', '0', '0', '1', '1'}, + }; + assertEquals(3, solution2.numIslands(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_201Test.java b/src/test/java/com/fishercoder/firstthousand/_201Test.java new file mode 100644 index 0000000000..0365709fe2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_201Test.java @@ -0,0 +1,64 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._201; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _201Test { + private _201.Solution1 solution1; + private static int left; + private static int right; + private static int actual; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _201.Solution1(); + } + + @Test + public void test1() { + left = 5; + right = 7; + actual = solution1.rangeBitwiseAnd(left, right); + expected = 4; + assertEquals(expected, actual); + actual = solution1.rangeBitwiseAnd(left, right); + assertEquals(expected, actual); + } + + @Test + public void test2() { + left = 1; + right = 2; + actual = solution1.rangeBitwiseAnd(left, right); + expected = 0; + assertEquals(expected, actual); + actual = solution1.rangeBitwiseAnd(left, right); + assertEquals(expected, actual); + } + + @Test + public void test3() { + left = 0; + right = 2147483647; + actual = solution1.rangeBitwiseAnd(left, right); + expected = 0; + assertEquals(expected, actual); + actual = solution1.rangeBitwiseAnd(left, right); + assertEquals(expected, actual); + } + + @Test + public void test4() { + left = 20000; + right = 2147483647; + actual = solution1.rangeBitwiseAnd(left, right); + expected = 0; + assertEquals(expected, actual); + actual = solution1.rangeBitwiseAnd(left, right); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_202Test.java b/src/test/java/com/fishercoder/firstthousand/_202Test.java new file mode 100644 index 0000000000..053e1f0c5b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_202Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._202; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _202Test { + private _202.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _202.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isHappy(7)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_203Test.java b/src/test/java/com/fishercoder/firstthousand/_203Test.java new file mode 100644 index 0000000000..8b36ac4d82 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_203Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._203; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _203Test { + private _203.Solution1 solution1; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _203.Solution1(); + } + + @Test + public void test1() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 6, 3, 4, 5, 6}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5}); + assertEquals(expected, solution1.removeElements(head, 6)); + } +} diff --git a/src/test/java/com/fishercoder/_204Test.java b/src/test/java/com/fishercoder/firstthousand/_204Test.java similarity index 75% rename from src/test/java/com/fishercoder/_204Test.java rename to src/test/java/com/fishercoder/firstthousand/_204Test.java index 6cac0b95c0..764e57e7f2 100644 --- a/src/test/java/com/fishercoder/_204Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_204Test.java @@ -1,16 +1,16 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._204; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._204; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _204Test { - private static _204.Solution1 solution1; + private _204.Solution1 solution1; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _204.Solution1(); } @@ -58,5 +58,4 @@ public void test8() { public void test9() { assertEquals(15, solution1.countPrimes(50)); } - } diff --git a/src/test/java/com/fishercoder/firstthousand/_206Test.java b/src/test/java/com/fishercoder/firstthousand/_206Test.java new file mode 100644 index 0000000000..4123862854 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_206Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._206; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _206Test { + private _206.Solution1 solution1; + private _206.Solution2 solution2; + private _206.Solution3 solution3; + private static ListNode head; + + @BeforeEach + public void setup() { + solution1 = new _206.Solution1(); + solution2 = new _206.Solution2(); + solution3 = new _206.Solution3(); + } + + @Test + public void test1() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4}); + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {4, 3, 2, 1}), + solution1.reverseList(head)); + } + + @Test + public void test2() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4}); + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {4, 3, 2, 1}), + solution2.reverseList(head)); + } + + @Test + public void test3() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4}); + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {4, 3, 2, 1}), + solution3.reverseList(head)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_207Test.java b/src/test/java/com/fishercoder/firstthousand/_207Test.java new file mode 100644 index 0000000000..d4976e021a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_207Test.java @@ -0,0 +1,76 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._207; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _207Test { + private _207.Solution1 solution1; + private _207.Solution2 solution2; + private _207.Solution3 solution3; + private _207.Solution4 solution4; + private static int[][] prerequisites; + private static int numCourses; + + @BeforeEach + public void setup() { + solution1 = new _207.Solution1(); + solution2 = new _207.Solution2(); + solution3 = new _207.Solution3(); + solution4 = new _207.Solution4(); + } + + @Test + public void test1() { + numCourses = 2; + prerequisites = new int[][] {{0, 1}}; + assertEquals(true, solution1.canFinish(numCourses, prerequisites)); + assertEquals(true, solution2.canFinish(numCourses, prerequisites)); + assertEquals(true, solution3.canFinish(numCourses, prerequisites)); + assertEquals(true, solution4.canFinish(numCourses, prerequisites)); + } + + @Test + public void test2() { + numCourses = 8; + prerequisites = + new int[][] { + {3, 0}, + {3, 1}, + {5, 3}, + {5, 2}, + {6, 3}, + {6, 1}, + {7, 3}, + {7, 4}, + {4, 2}, + }; + assertEquals(true, solution1.canFinish(numCourses, prerequisites)); + assertEquals(true, solution2.canFinish(numCourses, prerequisites)); + assertEquals(true, solution3.canFinish(numCourses, prerequisites)); + assertEquals(true, solution4.canFinish(numCourses, prerequisites)); + } + + @Test + public void test3() { + numCourses = 8; + prerequisites = + new int[][] { + {3, 2}, + {3, 0}, + {5, 3}, + {5, 1}, + {7, 3}, + {7, 0}, + {6, 3}, + {6, 4}, + {4, 1}, + }; + assertEquals(true, solution1.canFinish(numCourses, prerequisites)); + assertEquals(true, solution2.canFinish(numCourses, prerequisites)); + assertEquals(true, solution3.canFinish(numCourses, prerequisites)); + assertEquals(true, solution4.canFinish(numCourses, prerequisites)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_208Test.java b/src/test/java/com/fishercoder/firstthousand/_208Test.java new file mode 100644 index 0000000000..bddde5f4eb --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_208Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._208; +import org.junit.jupiter.api.Test; + +public class _208Test { + private _208.Solution1.Trie trie; + + @Test + public void test1() { + trie = new _208.Solution1.Trie(); + trie.insert("apple"); + assertEquals(true, trie.search("apple")); + assertEquals(false, trie.search("app")); + assertEquals(true, trie.startsWith("app")); + trie.insert("app"); + assertEquals(true, trie.search("app")); + } + + @Test + public void test2() { + trie = new _208.Solution1.Trie(); + trie.insert("fisher"); + trie.insert("coder"); + trie.insert("apple"); + trie.insert("april"); + trie.insert("cad"); + assertEquals(true, trie.search("fisher")); + assertEquals(true, trie.search("apple")); + assertEquals(true, trie.search("coder")); + assertEquals(true, trie.search("april")); + assertEquals(true, trie.search("cad")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_209Test.java b/src/test/java/com/fishercoder/firstthousand/_209Test.java new file mode 100644 index 0000000000..3b01e9f856 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_209Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._209; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _209Test { + private _209.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _209.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 3, 1, 2, 4, 3}; + assertEquals(2, solution1.minSubArrayLen(7, nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_20Test.java b/src/test/java/com/fishercoder/firstthousand/_20Test.java new file mode 100644 index 0000000000..bf88f1e36a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_20Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._20; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _20Test { + private _20.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _20.Solution1(); + } + + @Test + public void test1() { + assertEquals(false, solution1.isValid("(]")); + } + + @Test + public void test2() { + assertEquals(false, solution1.isValid("([)]")); + } + + @Test + public void test3() { + assertEquals(true, solution1.isValid("()[]{}")); + } + + @Test + public void test4() { + assertEquals(true, solution1.isValid("()")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_210Test.java b/src/test/java/com/fishercoder/firstthousand/_210Test.java new file mode 100644 index 0000000000..f7fa981ef0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_210Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._210; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _210Test { + private _210.Solution1 solution1; + private _210.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _210.Solution1(); + solution2 = new _210.Solution2(); + } + + @Test + public void test1() { + assertArrayEquals(new int[] {0, 1}, solution1.findOrder(2, new int[][] {{1, 0}})); + assertArrayEquals(new int[] {0, 1}, solution2.findOrder(2, new int[][] {{1, 0}})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_211Test.java b/src/test/java/com/fishercoder/firstthousand/_211Test.java new file mode 100644 index 0000000000..250e09c6bb --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_211Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._211; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _211Test { + private _211.Solution1.WordDictionary wordDictionarySolution1; + + @BeforeEach + public void setUp() { + wordDictionarySolution1 = new _211.Solution1.WordDictionary(); + } + + @Test + public void test1() { + wordDictionarySolution1.addWord("bad"); + wordDictionarySolution1.addWord("dad"); + wordDictionarySolution1.addWord("mad"); + assertEquals(false, wordDictionarySolution1.search("pad")); + assertEquals(true, wordDictionarySolution1.search("bad")); + assertEquals(true, wordDictionarySolution1.search(".ad")); + assertEquals(true, wordDictionarySolution1.search("b..")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_212Test.java b/src/test/java/com/fishercoder/firstthousand/_212Test.java new file mode 100644 index 0000000000..919219ae5a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_212Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._212; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _212Test { + private _212.Solution1 solution1; + private _212.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _212.Solution1(); + solution2 = new _212.Solution2(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("oa", "oaa"), + solution1.findWords( + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"o\",\"a\",\"b\",\"n\"],[\"o\",\"t\",\"a\",\"e\"],[\"a\",\"h\",\"k\",\"r\"],[\"a\",\"f\",\"l\",\"v\"]"), + new String[] {"oa", "oaa"})); + assertEquals( + Arrays.asList("oa", "oaa"), + solution2.findWords( + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"o\",\"a\",\"b\",\"n\"],[\"o\",\"t\",\"a\",\"e\"],[\"a\",\"h\",\"k\",\"r\"],[\"a\",\"f\",\"l\",\"v\"]"), + new String[] {"oa", "oaa"})); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList("oath", "eat"), + solution1.findWords( + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"o\",\"a\",\"a\",\"n\"],[\"e\",\"t\",\"a\",\"e\"],[\"i\",\"h\",\"k\",\"r\"],[\"i\",\"f\",\"l\",\"v\"]"), + new String[] {"oath", "pea", "eat", "rain"})); + assertEquals( + Arrays.asList("oath", "eat"), + solution2.findWords( + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"o\",\"a\",\"a\",\"n\"],[\"e\",\"t\",\"a\",\"e\"],[\"i\",\"h\",\"k\",\"r\"],[\"i\",\"f\",\"l\",\"v\"]"), + new String[] {"oath", "pea", "eat", "rain"})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_213Test.java b/src/test/java/com/fishercoder/firstthousand/_213Test.java new file mode 100644 index 0000000000..aa4b1e6724 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_213Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._213; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _213Test { + private _213.Solution1 solution1; + private _213.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _213.Solution1(); + solution2 = new _213.Solution2(); + } + + @Test + public void test1() { + assertEquals(3, solution1.rob(new int[] {2, 3, 2})); + assertEquals(3, solution2.rob(new int[] {2, 3, 2})); + } + + @Test + public void test2() { + assertEquals(4, solution1.rob(new int[] {1, 2, 3, 1})); + assertEquals(4, solution2.rob(new int[] {1, 2, 3, 1})); + } + + @Test + public void test3() { + assertEquals(0, solution1.rob(new int[] {0})); + assertEquals(0, solution2.rob(new int[] {0})); + } + + @Test + public void test4() { + assertEquals(0, solution1.rob(new int[] {0, 0})); + assertEquals(0, solution2.rob(new int[] {0, 0})); + } + + @Test + public void test5() { + assertEquals(340, solution1.rob(new int[] {200, 3, 140, 20, 10})); + assertEquals(340, solution2.rob(new int[] {200, 3, 140, 20, 10})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_215Test.java b/src/test/java/com/fishercoder/firstthousand/_215Test.java new file mode 100644 index 0000000000..bad7ec40d1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_215Test.java @@ -0,0 +1,53 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._215; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _215Test { + private _215.Solution1 solution1; + private _215.Solution2 solution2; + private _215.Solution3 solution3; + private static int k; + private static int[] nums; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _215.Solution1(); + solution2 = new _215.Solution2(); + solution3 = new _215.Solution3(); + } + + @Test + public void test1() { + k = 2; + nums = new int[] {3, 2, 1, 5, 6, 4}; + expected = 5; + assertEquals(expected, solution1.findKthLargest(nums, k)); + assertEquals(expected, solution2.findKthLargest(nums, k)); + assertEquals(expected, solution3.findKthLargest(nums, k)); + } + + @Test + public void test2() { + k = 1; + nums = new int[] {1}; + expected = 1; + assertEquals(expected, solution1.findKthLargest(nums, k)); + assertEquals(expected, solution2.findKthLargest(nums, k)); + assertEquals(expected, solution3.findKthLargest(nums, k)); + } + + @Test + public void test3() { + k = 2; + nums = new int[] {2, 1}; + expected = 1; + assertEquals(expected, solution1.findKthLargest(nums, k)); + assertEquals(expected, solution2.findKthLargest(nums, k)); + assertEquals(expected, solution3.findKthLargest(nums, k)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_216Test.java b/src/test/java/com/fishercoder/firstthousand/_216Test.java new file mode 100644 index 0000000000..7f0e6cba5c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_216Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._216; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _216Test { + private _216.Solution1 solution1; + private static int k; + private static int n; + + @BeforeEach + public void setup() { + solution1 = new _216.Solution1(); + } + + @Test + public void test1() { + k = 3; + n = 7; + assertEquals(Arrays.asList(Arrays.asList(1, 2, 4)), solution1.combinationSum3(k, n)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_217Test.java b/src/test/java/com/fishercoder/firstthousand/_217Test.java new file mode 100644 index 0000000000..f84763f6bd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_217Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._217; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _217Test { + private _217.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _217.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3, 4, 3}; + assertEquals(true, solution1.containsDuplicate(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_219Test.java b/src/test/java/com/fishercoder/firstthousand/_219Test.java new file mode 100644 index 0000000000..f2515396dd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_219Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._219; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _219Test { + private _219.Solution1 solution1; + private _219.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _219.Solution1(); + solution2 = new _219.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3, 1}; + assertEquals(true, solution1.containsNearbyDuplicate(nums, 3)); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 3, 1}; + assertEquals(true, solution2.containsNearbyDuplicate(nums, 3)); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 3, 1, 2, 3}; + assertEquals(false, solution2.containsNearbyDuplicate(nums, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_21Test.java b/src/test/java/com/fishercoder/firstthousand/_21Test.java new file mode 100644 index 0000000000..175f2bd608 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_21Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._21; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _21Test { + private _21.Solution1 solution1; + private _21.Solution2 solution2; + private static ListNode l1; + private static ListNode l2; + + @BeforeEach + public void setup() { + solution1 = new _21.Solution1(); + solution2 = new _21.Solution2(); + } + + @Test + public void test1() { + l1 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 5)); + l2 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(2, 4, 6)); + assertEquals( + LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 2, 3, 4, 5, 6)), + solution1.mergeTwoLists(l1, l2)); + } + + @Test + public void test2() { + l1 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 5)); + l2 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(2, 4, 6)); + assertEquals( + LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 2, 3, 4, 5, 6)), + solution2.mergeTwoLists(l1, l2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_220Test.java b/src/test/java/com/fishercoder/firstthousand/_220Test.java new file mode 100644 index 0000000000..10db7e3175 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_220Test.java @@ -0,0 +1,59 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._220; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _220Test { + private _220.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _220.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {-1, -1}; + assertEquals(true, solution1.containsNearbyAlmostDuplicate(nums, 1, 0)); + } + + @Test + public void test2() { + nums = new int[] {1, 2}; + assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 0, 1)); + } + + @Test + public void test3() { + nums = new int[] {4, 2}; + assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 2, 1)); + } + + @Test + public void test4() { + nums = new int[] {2, 2}; + assertEquals(true, solution1.containsNearbyAlmostDuplicate(nums, 3, 0)); + } + + @Test + public void test5() { + nums = new int[] {1}; + assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 1, 1)); + } + + @Test + public void test6() { + nums = new int[] {2147483647, -2147483647}; + assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 1, 2147483647)); + } + + @Test + public void test7() { + nums = new int[] {-1, 2147483647}; + assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 1, 2147483647)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_221Test.java b/src/test/java/com/fishercoder/firstthousand/_221Test.java new file mode 100644 index 0000000000..ade37c7596 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_221Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._221; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _221Test { + private _221.Solution1 solution1; + private static char[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _221.Solution1(); + } + + @Test + public void test1() { + matrix = + new char[][] { + {'1', '0', '1', '0', '0'}, + {'1', '0', '1', '1', '1'}, + {'1', '1', '1', '1', '1'}, + {'1', '0', '0', '1', '0'}, + }; + + assertEquals(4, solution1.maximalSquare(matrix)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_222Test.java b/src/test/java/com/fishercoder/firstthousand/_222Test.java new file mode 100644 index 0000000000..cc92163013 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_222Test.java @@ -0,0 +1,50 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._222; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _222Test { + private _222.Solution1 solution1; + private _222.Solution2 solution2; + private static int expected; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _222.Solution1(); + solution2 = new _222.Solution2(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); + TreeUtils.printBinaryTree(root); + expected = 3; + assertEquals(expected, solution1.countNodes(root)); + assertEquals(expected, solution2.countNodes(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6)); + TreeUtils.printBinaryTree(root); + expected = 6; + assertEquals(expected, solution1.countNodes(root)); + assertEquals(expected, solution2.countNodes(root)); + } + + @Test + public void test3() { + root = TreeUtils.constructBinaryTree(Arrays.asList(0)); + TreeUtils.printBinaryTree(root); + expected = 1; + assertEquals(expected, solution1.countNodes(root)); + assertEquals(expected, solution2.countNodes(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_224Test.java b/src/test/java/com/fishercoder/firstthousand/_224Test.java new file mode 100644 index 0000000000..18ac8459fd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_224Test.java @@ -0,0 +1,54 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._224; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _224Test { + private _224.Solution1 solution1; + private _224.Solution2 solution2; + private _224.Solution3 solution3; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _224.Solution1(); + solution2 = new _224.Solution2(); + solution3 = new _224.Solution3(); + } + + @Test + public void test1() { + String s = "1 + 1"; + expected = 2; + assertEquals(expected, solution1.calculate(s)); + assertEquals(expected, solution2.calculate(s)); + } + + @Test + public void test2() { + String s = " 2-1 + 2 "; + expected = 3; + assertEquals(expected, solution1.calculate(s)); + assertEquals(expected, solution2.calculate(s)); + } + + @Test + public void test3() { + String s = "(1+(4+5+2)-3)+(6+8)"; + expected = 23; + assertEquals(expected, solution1.calculate(s)); + assertEquals(expected, solution2.calculate(s)); + // assertEquals(expected, solution3.calculate(s));//TODO: fix this + } + + @Test + public void test4() { + String s = "1-(-2)"; + expected = 3; + assertEquals(expected, solution1.calculate(s)); + assertEquals(expected, solution2.calculate(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_227Test.java b/src/test/java/com/fishercoder/firstthousand/_227Test.java new file mode 100644 index 0000000000..07ca81c72d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_227Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._227; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _227Test { + private _227.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _227.Solution1(); + } + + @Test + public void test1() { + assertEquals(7, solution1.calculate("3+2*2")); + } + + @Test + public void test2() { + assertEquals(1, solution1.calculate(" 3/2 ")); + } + + @Test + public void test3() { + assertEquals(5, solution1.calculate(" 3+5 / 2 ")); + } + + @Test + public void test4() { + assertEquals(27, solution1.calculate("100000000/1/2/3/4/5/6/7/8/9/10")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_228Test.java b/src/test/java/com/fishercoder/firstthousand/_228Test.java new file mode 100644 index 0000000000..9fde401f52 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_228Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._228; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _228Test { + private _228.Solution1 solution1; + private static List expected; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _228.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {0, 1, 2, 4, 5, 7}; + expected = Arrays.asList("0->2", "4->5", "7"); + assertEquals(expected, solution1.summaryRanges(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_229Test.java b/src/test/java/com/fishercoder/firstthousand/_229Test.java new file mode 100644 index 0000000000..0fd4af04ad --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_229Test.java @@ -0,0 +1,66 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._229; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _229Test { + private _229.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _229.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1}; + assertEquals(Arrays.asList(1), solution1.majorityElement(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2}; + assertEquals(Arrays.asList(2, 1), solution1.majorityElement(nums)); + } + + @Test + public void test3() { + nums = new int[] {2, 2}; + assertEquals(Arrays.asList(2), solution1.majorityElement(nums)); + } + + @Test + public void test4() { + nums = new int[] {1, 2, 3}; + assertEquals(Arrays.asList(), solution1.majorityElement(nums)); + } + + @Test + public void test5() { + nums = new int[] {3, 2, 3}; + assertEquals(Arrays.asList(3), solution1.majorityElement(nums)); + } + + @Test + public void test6() { + nums = new int[] {3, 3, 4}; + assertEquals(Arrays.asList(3), solution1.majorityElement(nums)); + } + + @Test + public void test7() { + nums = new int[] {2, 2, 1, 3}; + assertEquals(Arrays.asList(2), solution1.majorityElement(nums)); + } + + @Test + public void test8() { + nums = new int[] {2, 2, 3, 3, 2, 4}; + assertEquals(Arrays.asList(2), solution1.majorityElement(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_22Test.java b/src/test/java/com/fishercoder/firstthousand/_22Test.java new file mode 100644 index 0000000000..a440353778 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_22Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._22; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _22Test { + private _22.Solution1 solution1; + private _22.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _22.Solution1(); + solution2 = new _22.Solution2(); + } + + @Test + public void test1() { + CommonUtils.printList(solution1.generateParenthesis(3)); + } + + @Test + public void test2() { + CommonUtils.printList(solution2.generateParenthesis(3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_230Test.java b/src/test/java/com/fishercoder/firstthousand/_230Test.java new file mode 100644 index 0000000000..72b615e25a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_230Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.solutions.firstthousand._230; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/19/17. */ +public class _230Test { + private _230.Solution1 solution1; + private static TreeNode root; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _230.Solution1(); + } + + @Test + public void test1() { + root = new TreeNode(1); + k = 1; + assertEquals(1, solution1.kthSmallest(root, k)); + } + + @Test + public void test2() { + root = new TreeNode(2); + root.left = new TreeNode(1); + k = 1; + assertEquals(1, solution1.kthSmallest(root, k)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_234Test.java b/src/test/java/com/fishercoder/firstthousand/_234Test.java new file mode 100644 index 0000000000..902c1c2e56 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_234Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._234; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _234Test { + private _234.Solution1 solution1; + private _234.Solution2 solution2; + private static ListNode head; + + @BeforeEach + public void setup() { + solution1 = new _234.Solution1(); + solution2 = new _234.Solution2(); + } + + @Test + public void test1() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 2, 1}); + assertEquals(true, solution1.isPalindrome(head)); + + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 2, 1}); + assertEquals(true, solution2.isPalindrome(head)); + } + + @Test + public void test2() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 2, 1}); + assertEquals(true, solution1.isPalindrome(head)); + + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 2, 1}); + assertEquals(true, solution2.isPalindrome(head)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_235Test.java b/src/test/java/com/fishercoder/firstthousand/_235Test.java new file mode 100644 index 0000000000..7e7ddef330 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_235Test.java @@ -0,0 +1,64 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._235; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _235Test { + private _235.Solution1 solution1; + private static TreeNode root; + private static TreeNode p; + private static TreeNode q; + + @BeforeEach + public void setup() { + solution1 = new _235.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5)); + TreeUtils.printBinaryTree(root); + + p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5)); + TreeUtils.printBinaryTree(p); + + q = TreeUtils.constructBinaryTree(Arrays.asList(8, 7, 9)); + TreeUtils.printBinaryTree(q); + + assertEquals(root, solution1.lowestCommonAncestor(root, p, q)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5)); + TreeUtils.printBinaryTree(root); + + p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5)); + TreeUtils.printBinaryTree(p); + + q = TreeUtils.constructBinaryTree(Arrays.asList(4)); + TreeUtils.printBinaryTree(q); + + assertEquals(p, solution1.lowestCommonAncestor(root, p, q)); + } + + @Test + public void test3() { + root = TreeUtils.constructBinaryTree(Arrays.asList(0, -1000000000, 1000000000)); + TreeUtils.printBinaryTree(root); + + p = TreeUtils.constructBinaryTree(Arrays.asList(-1000000000)); + TreeUtils.printBinaryTree(p); + + q = TreeUtils.constructBinaryTree(Arrays.asList(1000000000)); + TreeUtils.printBinaryTree(q); + + assertEquals(root, solution1.lowestCommonAncestor(root, p, q)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_237Test.java b/src/test/java/com/fishercoder/firstthousand/_237Test.java new file mode 100644 index 0000000000..d9227f14b6 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_237Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._237; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _237Test { + private _237.Solution1 solution1; + private static ListNode head; + + @BeforeEach + public void setup() { + solution1 = new _237.Solution1(); + } + + @Test + public void test1() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(4, 5, 1, 9)); + CommonUtils.printList(head); + solution1.deleteNode(head.next); + CommonUtils.printList(head); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_238Test.java b/src/test/java/com/fishercoder/firstthousand/_238Test.java new file mode 100644 index 0000000000..93f8f2daa8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_238Test.java @@ -0,0 +1,45 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._238; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _238Test { + private _238.Solution1 solution1; + private static int[] expected; + private static int[] actual; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _238.Solution1(); + expected = new int[] {}; + actual = new int[] {}; + } + + @Test + public void test1() { + nums = new int[] {0, 0}; + expected = new int[] {0, 0}; + actual = solution1.productExceptSelf(nums); + assertArrayEquals(expected, actual); + } + + @Test + public void test2() { + nums = new int[] {1, 0}; + expected = new int[] {0, 1}; + actual = solution1.productExceptSelf(nums); + assertArrayEquals(expected, actual); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 3, 4}; + expected = new int[] {24, 12, 8, 6}; + actual = solution1.productExceptSelf(nums); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_239Test.java b/src/test/java/com/fishercoder/firstthousand/_239Test.java new file mode 100644 index 0000000000..c01f892336 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_239Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._239; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _239Test { + private _239.Solution1 solution1; + private static int[] expected; + private static int[] actual; + private static int[] nums; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _239.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = new int[] {}; + actual = new int[] {}; + nums = new int[] {}; + k = 0; + } + + @Test + public void test1() { + + nums = new int[] {1, 3, -1, -3, 5, 3, 6, 7}; + k = 3; + expected = new int[] {3, 3, 5, 5, 6, 7}; + actual = solution1.maxSlidingWindow(nums, k); + Assertions.assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_23Test.java b/src/test/java/com/fishercoder/firstthousand/_23Test.java new file mode 100644 index 0000000000..5638e46c81 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_23Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._23; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _23Test { + private _23.Solution1 solution1; + private static ListNode[] lists; + + @BeforeEach + public void setup() { + solution1 = new _23.Solution1(); + } + + @Test + public void test1() { + ListNode head1 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 3, 5, 7, 11)); + ListNode head2 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(2, 8, 12)); + ListNode head3 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(4, 6, 9, 10)); + lists = new ListNode[] {head1, head2, head3}; + CommonUtils.printList(solution1.mergeKLists(lists)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_240Test.java b/src/test/java/com/fishercoder/firstthousand/_240Test.java new file mode 100644 index 0000000000..1479abce4c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_240Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._240; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _240Test { + private _240.Solution1 solution1; + private static boolean actual; + private static boolean expected; + private static int target; + private static int[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _240.Solution1(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + target = 5; + matrix = + new int[][] { + {1, 4, 7, 11, 15}, + {2, 5, 8, 12, 19}, + {3, 6, 9, 16, 22}, + {10, 13, 14, 17, 24}, + {18, 21, 23, 26, 30} + }; + expected = true; + actual = solution1.searchMatrix(matrix, target); + assertEquals(expected, actual); + } + + @Test + public void test2() { + target = 0; + matrix = new int[][] {}; + expected = false; + actual = solution1.searchMatrix(matrix, target); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_242Test.java b/src/test/java/com/fishercoder/firstthousand/_242Test.java new file mode 100644 index 0000000000..082b84ff7e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_242Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._242; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _242Test { + private _242.Solution1 solution1; + private _242.Solution2 solution2; + private _242.Solution3 solution3; + private static String s; + private static String t; + + @BeforeEach + public void setup() { + solution1 = new _242.Solution1(); + solution2 = new _242.Solution2(); + solution3 = new _242.Solution3(); + } + + @Test + public void test1() { + s = "anagram"; + t = "nagaram"; + assertEquals(true, solution1.isAnagram(s, t)); + assertEquals(true, solution2.isAnagram(s, t)); + } + + @Test + public void test2() { + s = "代码写开心"; + t = "开心写代码"; + assertEquals(true, solution1.isAnagram(s, t)); + // assertEquals(true, solution3.isAnagram(s, t));//solution2 won't work for unicode + // character input + assertEquals(true, solution3.isAnagram(s, t)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_247Test.java b/src/test/java/com/fishercoder/firstthousand/_247Test.java new file mode 100644 index 0000000000..e76780a148 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_247Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._247; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _247Test { + private _247.Solution1 solution1; + private static List expected; + + @BeforeEach + public void setUp() { + solution1 = new _247.Solution1(); + } + + @Test + public void test1() { + expected = Arrays.asList("11", "69", "88", "96"); + assertEquals(expected, solution1.findStrobogrammatic(2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_249Test.java b/src/test/java/com/fishercoder/firstthousand/_249Test.java new file mode 100644 index 0000000000..cb714d73ae --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_249Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.firstthousand._249; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _249Test { + private _249.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _249.Solution1(); + } + + @Test + public void test1() { + List> expected = + Arrays.asList( + Arrays.asList("acef"), + Arrays.asList("a", "z"), + Arrays.asList("abc", "bcd", "xyz"), + Arrays.asList("az", "ba")); + List> actual = + solution1.groupStrings( + new String[] {"abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"}); + assertTrue(expected.containsAll(actual)); + assertTrue(actual.containsAll(expected)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_24Test.java b/src/test/java/com/fishercoder/firstthousand/_24Test.java new file mode 100644 index 0000000000..0366344f9f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_24Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._24; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _24Test { + private _24.Solution1 solution1; + private _24.Solution2 solution2; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _24.Solution1(); + solution2 = new _24.Solution2(); + } + + @Test + public void test1() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4)); + expected = LinkedListUtils.createSinglyLinkedList(Arrays.asList(2, 1, 4, 3)); + assertEquals(expected, solution1.swapPairs(head)); + } + + @Test + public void test2() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4)); + expected = LinkedListUtils.createSinglyLinkedList(Arrays.asList(2, 1, 4, 3)); + assertEquals(expected, solution2.swapPairs(head)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_256Test.java b/src/test/java/com/fishercoder/firstthousand/_256Test.java new file mode 100644 index 0000000000..e1b252bcee --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_256Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._256; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _256Test { + private _256.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _256.Solution1(); + } + + @Test + public void test1() { + assertEquals(10, solution1.minCost(new int[][] {{17, 2, 17}, {16, 16, 5}, {14, 3, 19}})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_25Test.java b/src/test/java/com/fishercoder/firstthousand/_25Test.java new file mode 100644 index 0000000000..e624f15d05 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_25Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._25; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _25Test { + private _25.Solution1 solution1; + private _25.Solution2 solution2; + private _25.Solution3 solution3; + private static ListNode expected; + private static ListNode head; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _25.Solution1(); + solution2 = new _25.Solution2(); + solution3 = new _25.Solution3(); + } + + @Test + public void test1() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5}); + k = 2; + expected = LinkedListUtils.contructLinkedList(new int[] {2, 1, 4, 3, 5}); + assertEquals(expected, solution1.reverseKGroup(head, k)); + } + + @Test + public void test2() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7}); + expected = LinkedListUtils.contructLinkedList(new int[] {4, 3, 2, 1, 5, 6, 7}); + assertEquals(expected, solution2.reverseKGroup(head, 4)); + } + + @Test + public void test3() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7}); + expected = LinkedListUtils.contructLinkedList(new int[] {4, 3, 2, 1, 5, 6, 7}); + assertEquals(expected, solution3.reverseKGroup(head, 4)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_264Test.java b/src/test/java/com/fishercoder/firstthousand/_264Test.java new file mode 100644 index 0000000000..17c64b999f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_264Test.java @@ -0,0 +1,69 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._264; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _264Test { + private _264.Solution1 solution1; + private _264.Solution2 solution2; + private _264.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _264.Solution1(); + solution2 = new _264.Solution2(); + solution3 = new _264.Solution3(); + } + + @Test + public void test1() { + assertEquals(12, solution1.nthUglyNumber(10)); + assertEquals(12, solution2.nthUglyNumber(10)); + assertEquals(12, solution3.nthUglyNumber(10)); + } + + @Test + public void test2() { + assertEquals(402653184, solution1.nthUglyNumber(1352)); + assertEquals(402653184, solution2.nthUglyNumber(1352)); + } + + @Test + public void test3() { + assertEquals(1, solution1.nthUglyNumber(1)); + assertEquals(1, solution2.nthUglyNumber(1)); + } + + @Test + public void test4() { + assertEquals(2, solution1.nthUglyNumber(2)); + assertEquals(2, solution2.nthUglyNumber(2)); + } + + @Test + public void test5() { + assertEquals(3, solution1.nthUglyNumber(3)); + assertEquals(3, solution2.nthUglyNumber(3)); + } + + @Test + public void test6() { + assertEquals(5, solution1.nthUglyNumber(5)); + assertEquals(5, solution2.nthUglyNumber(5)); + } + + @Test + public void test7() { + assertEquals(3888, solution1.nthUglyNumber(134)); + assertEquals(3888, solution2.nthUglyNumber(134)); + } + + @Test + public void test8() { + assertEquals(536870912, solution1.nthUglyNumber(1407)); + assertEquals(536870912, solution2.nthUglyNumber(1407)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_269Test.java b/src/test/java/com/fishercoder/firstthousand/_269Test.java new file mode 100644 index 0000000000..948f17a8d6 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_269Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._269; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _269Test { + private _269.Solution1 solution1; + private static String[] words; + + @BeforeEach + public void setup() { + solution1 = new _269.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"wrt", "wrf", "er", "ett", "rftt"}; + assertEquals("wertf", solution1.alienOrder(words)); + } + + @Test + public void test2() { + words = new String[] {"abc", "ab"}; + assertEquals("", solution1.alienOrder(words)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_26Test.java b/src/test/java/com/fishercoder/firstthousand/_26Test.java new file mode 100644 index 0000000000..7b778eacbd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_26Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._26; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _26Test { + private _26.Solution1 solution1; + private _26.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _26.Solution1(); + solution2 = new _26.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 1, 2}; + assertEquals(2, solution1.removeDuplicates(nums)); + assertEquals(2, solution2.removeDuplicates(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 1, 2, 2, 3}; + assertEquals(3, solution1.removeDuplicates(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 1}; + assertEquals(1, solution1.removeDuplicates(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_270Test.java b/src/test/java/com/fishercoder/firstthousand/_270Test.java new file mode 100644 index 0000000000..0e27f3dcea --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_270Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._270; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _270Test { + private _270.Solution1 solution1; + private static int expected; + private static TreeNode root; + private static double target; + + @BeforeEach + public void setup() { + solution1 = new _270.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 5, 1, 3)); + expected = 4; + target = 3.714286; + assertEquals(expected, solution1.closestValue(root, target)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_273Test.java b/src/test/java/com/fishercoder/firstthousand/_273Test.java new file mode 100644 index 0000000000..8014734e40 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_273Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._273; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _273Test { + private _273.Solution1 solution1; + private static int num; + + @BeforeEach + public void setup() { + solution1 = new _273.Solution1(); + } + + @Test + public void test1() { + num = 123; + assertEquals("One Hundred Twenty Three", solution1.numberToWords(num)); + } + + @Test + public void test2() { + num = 12345; + assertEquals("Twelve Thousand Three Hundred Forty Five", solution1.numberToWords(num)); + } + + @Test + public void test3() { + num = 1234567; + assertEquals( + "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven", + solution1.numberToWords(num)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_276Test.java b/src/test/java/com/fishercoder/firstthousand/_276Test.java new file mode 100644 index 0000000000..fa410fe71f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_276Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._276; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _276Test { + private _276.Solution1 solution1; + private _276.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _276.Solution1(); + solution2 = new _276.Solution2(); + } + + @Test + public void test1() { + assertEquals(6, solution1.numWays(3, 2)); + assertEquals(6, solution2.numWays(3, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_279Test.java b/src/test/java/com/fishercoder/firstthousand/_279Test.java new file mode 100644 index 0000000000..8244e94a4a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_279Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._279; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _279Test { + private _279.Solution1 solution1; + private _279.Solution2 solution2; + private _279.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _279.Solution1(); + solution2 = new _279.Solution2(); + solution3 = new _279.Solution3(); + } + + @Test + public void test1() { + int n = 9; + int expected = 1; + assertEquals(expected, solution1.numSquares(n)); + assertEquals(expected, solution2.numSquares(n)); + assertEquals(expected, solution3.numSquares(n)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_27Test.java b/src/test/java/com/fishercoder/firstthousand/_27Test.java new file mode 100644 index 0000000000..b25c33b739 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_27Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._27; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _27Test { + private _27.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _27.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {3, 2, 2, 3}; + assertEquals(2, solution1.removeElement(nums, 3)); + } + + @Test + public void test2() { + nums = new int[] {2, 2, 3}; + assertEquals(1, solution1.removeElement(nums, 2)); + } + + @Test + public void test3() { + nums = new int[] {1}; + assertEquals(0, solution1.removeElement(nums, 1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_283Test.java b/src/test/java/com/fishercoder/firstthousand/_283Test.java new file mode 100644 index 0000000000..e395566102 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_283Test.java @@ -0,0 +1,136 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._283; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _283Test { + private _283.Solution1 solution1; + private _283.Solution2 solution2; + private _283.Solution3 solution3; + private _283.Solution4 solution4; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _283.Solution1(); + solution2 = new _283.Solution2(); + solution3 = new _283.Solution3(); + solution4 = new _283.Solution4(); + } + + @Test + public void test1() { + nums = new int[] {0, 1, 0, 3, 12}; + solution1.moveZeroes(nums); + CommonUtils.printArray(nums); + } + + @Test + public void test2() { + nums = new int[] {0, 1, 0, 3, 12}; + solution2.moveZeroes(nums); + CommonUtils.printArray(nums); + } + + @Test + public void test3() { + nums = new int[] {0, 1, 0, 3, 12}; + solution3.moveZeroes(nums); + CommonUtils.printArray(nums); + } + + @Test + public void test4() { + nums = new int[] {1, 0}; + solution1.moveZeroes(nums); + CommonUtils.printArray(nums); + } + + @Test + public void test5() { + nums = new int[] {0, 1, 0, 3, 12}; + solution1.moveZeroes(nums); + assertArrayEquals(new int[] {1, 3, 12, 0, 0}, nums); + } + + @Test + public void test6() { + nums = new int[] {1, 0, 0}; + solution1.moveZeroes(nums); + assertArrayEquals(new int[] {1, 0, 0}, nums); + } + + @Test + public void test7() { + nums = new int[] {1, 0}; + solution2.moveZeroes(nums); + CommonUtils.printArray(nums); + } + + @Test + public void test8() { + nums = new int[] {0, 1, 0, 3, 12}; + solution2.moveZeroes(nums); + assertArrayEquals(new int[] {1, 3, 12, 0, 0}, nums); + } + + @Test + public void test9() { + nums = new int[] {1, 0, 0}; + solution2.moveZeroes(nums); + assertArrayEquals(new int[] {1, 0, 0}, nums); + } + + @Test + public void test10() { + nums = new int[] {1, 0}; + solution3.moveZeroes(nums); + CommonUtils.printArray(nums); + } + + @Test + public void test11() { + nums = new int[] {0, 1, 0, 3, 12}; + solution3.moveZeroes(nums); + assertArrayEquals(new int[] {1, 3, 12, 0, 0}, nums); + } + + @Test + public void test12() { + nums = new int[] {1, 0, 0}; + solution3.moveZeroes(nums); + assertArrayEquals(new int[] {1, 0, 0}, nums); + } + + @Test + public void test13() { + nums = new int[] {0}; + solution3.moveZeroes(nums); + assertArrayEquals(new int[] {0}, nums); + } + + @Test + public void test14() { + nums = new int[] {0, 0, 1}; + solution3.moveZeroes(nums); + assertArrayEquals(new int[] {1, 0, 0}, nums); + } + + @Test + public void test15() { + nums = new int[] {1, 0, 1}; + solution3.moveZeroes(nums); + assertArrayEquals(new int[] {1, 1, 0}, nums); + } + + @Test + public void test16() { + nums = new int[] {2, 1}; + solution4.moveZeroes(nums); + assertArrayEquals(new int[] {2, 1}, nums); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_289Test.java b/src/test/java/com/fishercoder/firstthousand/_289Test.java new file mode 100644 index 0000000000..1d0471a640 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_289Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._289; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _289Test { + private _289.Solution1 solution1; + private int[][] board; + + @BeforeEach + public void setup() { + solution1 = new _289.Solution1(); + } + + @Test + public void test1() { + board = + new int[][] { + {0, 1, 0}, + {0, 0, 1}, + {1, 1, 1}, + {0, 0, 0} + }; + solution1.gameOfLife(board); + int[][] expected = + new int[][] { + {0, 0, 0}, + {1, 0, 1}, + {0, 1, 1}, + {0, 1, 0} + }; + assertArrayEquals(expected, board); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_28Test.java b/src/test/java/com/fishercoder/firstthousand/_28Test.java new file mode 100644 index 0000000000..2a11a0222a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_28Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._28; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _28Test { + private _28.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _28.Solution1(); + } + + @Test + public void test1() { + assertEquals(0, solution1.strStr("a", "")); + } + + @Test + public void test2() { + assertEquals(-1, solution1.strStr("mississippi", "a")); + } + + @Test + public void test3() { + assertEquals(0, solution1.strStr("a", "a")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_291Test.java b/src/test/java/com/fishercoder/firstthousand/_291Test.java new file mode 100644 index 0000000000..dc7ecf8c26 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_291Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._291; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _291Test { + private _291.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _291.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.wordPatternMatch("abab", "redblueredblue")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_294Test.java b/src/test/java/com/fishercoder/firstthousand/_294Test.java new file mode 100644 index 0000000000..97c63aa744 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_294Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._294; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _294Test { + private _294.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _294.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.canWin("++++")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_295Test.java b/src/test/java/com/fishercoder/firstthousand/_295Test.java new file mode 100644 index 0000000000..a64334002f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_295Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._295; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/27/17. */ +public class _295Test { + private _295.Solution1.MedianFinder solution1; + private _295.Solution2.MedianFinder solution2; + + @BeforeEach + public void setup() { + solution1 = new _295.Solution1.MedianFinder(); + solution2 = new _295.Solution2.MedianFinder(); + } + + @Test + public void test1() { + solution1.addNum(1); + solution1.addNum(3); + solution1.addNum(-1); + assertEquals(1.0, solution1.findMedian(), 0); + } + + @Test + public void test2() { + solution2.addNum(1); + solution2.addNum(3); + solution2.addNum(-1); + assertEquals(1.0, solution2.findMedian(), 0); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_297Test.java b/src/test/java/com/fishercoder/firstthousand/_297Test.java new file mode 100644 index 0000000000..bce7dab061 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_297Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._297; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _297Test { + private _297.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _297.Solution1(); + } + + @Test + public void test1() { + TreeNode root = + TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, null, 4, 5, 6, 7)); + TreeUtils.printBinaryTree(root); + String str = solution1.serialize(root); + System.out.println(str); + TreeUtils.printBinaryTree(solution1.deserialize(str)); + assertEquals(root, solution1.deserialize(str)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_298Test.java b/src/test/java/com/fishercoder/firstthousand/_298Test.java new file mode 100644 index 0000000000..fea815149d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_298Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._298; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _298Test { + private _298.Solution1 solution1; + private _298.Solution2 solution2; + + @Test + public void test1() { + solution1 = new _298.Solution1(); + solution2 = new _298.Solution2(); + TreeNode root = + TreeUtils.constructBinaryTree(Arrays.asList(1, null, 3, 2, 4, null, null, null, 5)); + assertEquals(3, solution1.longestConsecutive(root)); + assertEquals(3, solution2.longestConsecutive(root)); + } + + @Test + public void test2() { + solution1 = new _298.Solution1(); + solution2 = new _298.Solution2(); + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(2, null, 3, 2, null, 1)); + TreeUtils.printBinaryTree(root); + assertEquals(2, solution1.longestConsecutive(root)); + assertEquals(2, solution2.longestConsecutive(root)); + } + + @Test + public void test3() { + solution1 = new _298.Solution1(); + solution2 = new _298.Solution2(); + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList(1, 2, 3, null, null, 4, 4, null, 5, null, null, 6)); + TreeUtils.printBinaryTree(root); + assertEquals(4, solution1.longestConsecutive(root)); + assertEquals(4, solution2.longestConsecutive(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_29Test.java b/src/test/java/com/fishercoder/firstthousand/_29Test.java new file mode 100644 index 0000000000..5b7a73c4dc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_29Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._29; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _29Test { + private _29.Solution1 solution1; + private _29.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _29.Solution1(); + solution2 = new _29.Solution2(); + } + + @Test + public void test1() { + assertEquals(1, solution1.divide(4, 3)); + } + + @Test + public void test2() { + assertEquals(Integer.MAX_VALUE, solution1.divide(Integer.MIN_VALUE, -1)); + } + + @Test + public void test3() { + assertEquals(3, solution2.divide(10, 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_2Test.java b/src/test/java/com/fishercoder/firstthousand/_2Test.java new file mode 100644 index 0000000000..03d4225345 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_2Test.java @@ -0,0 +1,45 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._2; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2Test { + private _2.Solution1 solution1; + private static ListNode l1; + private static ListNode l2; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _2.Solution1(); + } + + @Test + public void test1() { + l1 = LinkedListUtils.contructLinkedList(new int[] {2, 4, 3}); + l2 = LinkedListUtils.contructLinkedList(new int[] {5, 6, 4}); + expected = LinkedListUtils.contructLinkedList(new int[] {7, 0, 8}); + assertEquals(expected, solution1.addTwoNumbers(l1, l2)); + } + + @Test + public void test2() { + l1 = LinkedListUtils.contructLinkedList(new int[] {1, 8}); + l2 = LinkedListUtils.contructLinkedList(new int[] {0}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 8}); + assertEquals(expected, solution1.addTwoNumbers(l1, l2)); + } + + @Test + public void test3() { + l1 = LinkedListUtils.contructLinkedList(new int[] {5}); + l2 = LinkedListUtils.contructLinkedList(new int[] {5}); + expected = LinkedListUtils.contructLinkedList(new int[] {0, 1}); + assertEquals(expected, solution1.addTwoNumbers(l1, l2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_300Test.java b/src/test/java/com/fishercoder/firstthousand/_300Test.java new file mode 100644 index 0000000000..623619a989 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_300Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._300; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _300Test { + + private _300.Solution1 solution1; + private _300.Solution2 solution2; + private _300.Solution3 solution3; + private _300.Solution4 solution4; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _300.Solution1(); + solution2 = new _300.Solution2(); + solution3 = new _300.Solution3(); + solution4 = new _300.Solution4(); + } + + @Test + public void test1() { + nums = new int[] {10, 9, 2, 5, 3, 7, 101, 18}; + assertEquals(4, solution1.lengthOfLIS(nums)); + assertEquals(4, solution2.lengthOfLIS(nums)); + assertEquals(4, solution3.lengthOfLIS(nums)); + assertEquals(4, solution4.lengthOfLIS(nums)); + } + + @Test + public void test2() { + nums = new int[] {0, 1, 0, 3, 2, 3}; + assertEquals(4, solution1.lengthOfLIS(nums)); + assertEquals(4, solution2.lengthOfLIS(nums)); + assertEquals(4, solution3.lengthOfLIS(nums)); + assertEquals(4, solution4.lengthOfLIS(nums)); + } + + @Test + public void test3() { + nums = new int[] {7, 7, 7, 7, 7, 7, 7}; + assertEquals(1, solution1.lengthOfLIS(nums)); + assertEquals(1, solution2.lengthOfLIS(nums)); + assertEquals(1, solution3.lengthOfLIS(nums)); + assertEquals(1, solution3.lengthOfLIS(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_304Test.java b/src/test/java/com/fishercoder/firstthousand/_304Test.java new file mode 100644 index 0000000000..fe023fe906 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_304Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._304; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _304Test { + private _304.Solution1.NumMatrix numMatrix; + private static int[][] matrix; + + @BeforeEach + public void setup() {} + + @Test + public void test1() { + matrix = + new int[][] { + {3, 0, 1, 4, 2}, + {5, 6, 3, 2, 1}, + {1, 2, 0, 1, 5}, + {4, 1, 0, 1, 7}, + {1, 0, 3, 0, 5} + }; + numMatrix = new _304.Solution1.NumMatrix(matrix); + assertEquals(8, numMatrix.sumRegion(2, 1, 4, 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_306Test.java b/src/test/java/com/fishercoder/firstthousand/_306Test.java new file mode 100644 index 0000000000..bde8f71d29 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_306Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._306; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _306Test { + private _306.Solution1 solution1; + private static String num; + + @BeforeEach + public void setUp() { + solution1 = new _306.Solution1(); + } + + @Test + public void test1() { + num = "0235813"; + assertEquals(false, solution1.isAdditiveNumber(num)); + } + + @Test + public void test2() { + num = "000"; + assertEquals(true, solution1.isAdditiveNumber(num)); + } + + @Test + public void test3() { + num = "011235"; + assertEquals(true, solution1.isAdditiveNumber(num)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_30Test.java b/src/test/java/com/fishercoder/firstthousand/_30Test.java new file mode 100644 index 0000000000..2f4598d187 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_30Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._30; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +public class _30Test { + private _30.Solution1 solution1; + private static String[] words; + private static List expected; + + @BeforeEach + public void setup() { + solution1 = new _30.Solution1(); + } + + @Test + @Disabled + public void test1() { + words = new String[] {"foo", "bar"}; + expected = Arrays.asList(0, 9); + assertEquals(expected, solution1.findSubstring("barfoothefoobarman", words)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_312Test.java b/src/test/java/com/fishercoder/firstthousand/_312Test.java new file mode 100644 index 0000000000..b86dd8dc28 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_312Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._312; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _312Test { + private _312.Solution1 solution1; + private static int[] nums; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _312.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {3, 1, 5, 8}; + expected = 167; + assertEquals(expected, solution1.maxCoins(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 5}; + expected = 10; + assertEquals(expected, solution1.maxCoins(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_316Test.java b/src/test/java/com/fishercoder/firstthousand/_316Test.java new file mode 100644 index 0000000000..4786da7c1f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_316Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._316; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _316Test { + private _316.Solution1 solution1; + private _316.Solution2 solution2; + + @BeforeEach + public void setUp() { + solution1 = new _316.Solution1(); + solution2 = new _316.Solution2(); + } + + @Test + public void test1() { + assertEquals("abc", solution1.removeDuplicateLetters("bcabc")); + assertEquals("abc", solution2.removeDuplicateLetters("bcabc")); + } + + @Test + public void test2() { + assertEquals("acdb", solution1.removeDuplicateLetters("cbacdcbc")); + assertEquals("acdb", solution2.removeDuplicateLetters("cbacdcbc")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_319Test.java b/src/test/java/com/fishercoder/firstthousand/_319Test.java new file mode 100644 index 0000000000..06e1110ba9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_319Test.java @@ -0,0 +1,71 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._319; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _319Test { + private _319.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _319.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.bulbSwitch(2)); + } + + @Test + public void test2() { + assertEquals(1, solution1.bulbSwitch(3)); + } + + @Test + public void test3() { + assertEquals(2, solution1.bulbSwitch(4)); + } + + @Test + public void test4() { + assertEquals(2, solution1.bulbSwitch(5)); + } + + @Test + public void test5() { + assertEquals(2, solution1.bulbSwitch(6)); + } + + @Test + public void test6() { + assertEquals(2, solution1.bulbSwitch(7)); + } + + @Test + public void test7() { + assertEquals(2, solution1.bulbSwitch(8)); + } + + @Test + public void test8() { + assertEquals(3, solution1.bulbSwitch(9)); + } + + @Test + public void test11() { + assertEquals(3, solution1.bulbSwitch(15)); + } + + @Test + public void test9() { + assertEquals(4, solution1.bulbSwitch(17)); + } + + @Test + public void test10() { + assertEquals(4, solution1.bulbSwitch(16)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_31Test.java b/src/test/java/com/fishercoder/firstthousand/_31Test.java new file mode 100644 index 0000000000..cb033e3898 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_31Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._31; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _31Test { + private _31.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _31.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3}; + solution1.nextPermutation(nums); + assertArrayEquals(new int[] {1, 3, 2}, nums); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 4, 6, 3}; + solution1.nextPermutation(nums); + assertArrayEquals(new int[] {1, 2, 6, 3, 4}, nums); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 4, 6, 3, 2, 1}; + solution1.nextPermutation(nums); + assertArrayEquals(new int[] {1, 2, 6, 1, 2, 3, 4}, nums); + } + + @Test + public void test4() { + nums = new int[] {1, 2, 5, 4, 3}; + solution1.nextPermutation(nums); + assertArrayEquals(new int[] {1, 3, 2, 4, 5}, nums); + } + + @Test + public void test5() { + nums = new int[] {3, 2, 1}; + solution1.nextPermutation(nums); + assertArrayEquals(new int[] {1, 2, 3}, nums); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_320Test.java b/src/test/java/com/fishercoder/firstthousand/_320Test.java new file mode 100644 index 0000000000..35427ba927 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_320Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.firstthousand._320; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _320Test { + private _320.Solution1 solution1; + private static List expected; + private static List actual; + private static String word; + + @BeforeEach + public void setUp() { + solution1 = new _320.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = new ArrayList<>(); + actual = new ArrayList<>(); + } + + @Test + public void test1() { + word = "word"; + expected = + Arrays.asList( + "word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", + "w1r1", "1o2", "2r1", "3d", "w3", "4"); + actual = solution1.generateAbbreviations(word); + assertTrue(expected.containsAll(actual) && actual.containsAll(expected)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_325Test.java b/src/test/java/com/fishercoder/firstthousand/_325Test.java new file mode 100644 index 0000000000..d0686e6836 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_325Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._325; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _325Test { + private _325.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _325.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {-2, -1, 2, 1}; + assertEquals(2, solution1.maxSubArrayLen(nums, 1)); + } + + @Test + public void test2() { + nums = new int[] {1, -1, 5, -2, 3}; + assertEquals(4, solution1.maxSubArrayLen(nums, 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_326Test.java b/src/test/java/com/fishercoder/firstthousand/_326Test.java new file mode 100644 index 0000000000..5895ba97d4 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_326Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._326; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _326Test { + private _326.Solution1 solution1; + private _326.Solution2 solution2; + private _326.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _326.Solution1(); + solution2 = new _326.Solution2(); + solution3 = new _326.Solution3(); + } + + @Test + public void test1() { + assertEquals(false, solution1.isPowerOfThree(12)); + assertEquals(false, solution2.isPowerOfThree(12)); + assertEquals(false, solution3.isPowerOfThree(12)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_327Test.java b/src/test/java/com/fishercoder/firstthousand/_327Test.java new file mode 100644 index 0000000000..fbde2e23c7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_327Test.java @@ -0,0 +1,539 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._327; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _327Test { + private _327.Solution1 solution1; + private _327.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _327.Solution1(); + solution2 = new _327.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {-2, 5, -1}; + assertEquals(3, solution1.countRangeSum(nums, -2, 2)); + assertEquals(3, solution2.countRangeSum(nums, -2, 2)); + } + + @Test + public void test2() { + nums = new int[] {-1, 1}; + assertEquals(1, solution1.countRangeSum(nums, 0, 0)); + assertEquals(1, solution2.countRangeSum(nums, 0, 0)); + } + + @Test + public void test3() { + nums = new int[] {0}; + assertEquals(1, solution1.countRangeSum(nums, 0, 0)); + assertEquals(1, solution2.countRangeSum(nums, 0, 0)); + } + + @Test + public void test4() { + nums = new int[] {0, 0}; + assertEquals(3, solution1.countRangeSum(nums, 0, 0)); + assertEquals(3, solution2.countRangeSum(nums, 0, 0)); + } + + @Test + public void test5() { + nums = new int[] {-2147483647, 0, -2147483647, 2147483647}; + assertEquals(3, solution1.countRangeSum(nums, -564, 3864)); + assertEquals(3, solution2.countRangeSum(nums, -564, 3864)); + } + + @Test + public void test6() { + nums = + new int[] { + 28, 26, 22, 7, 12, -26, 25, 11, -14, 0, 0, 6, 20, 4, 17, -7, 4, -14, -19, -16, + 8, -21, -26, 24, -29, -18, -13, 2, -16, -14, -26, -14, -7, -14, -30, -3, 9, -16, + 28, 3, -26, -5, 4, -28, -3, 11, 26, 14, 20, 15, -4, -12, -18, -21, -28, 22, -4, + 0, 2, 22, 16, -10, -14, -5, 2, -3, -16, -23, -26, -5, 21, 17, 4, 29, 19, -2, -8, + 5, -14, 13, 10, -15, 2, 25, -5, -21, 13, -28, 7, -12, 9, -1, 15, -26, -9, -3, 3, + 14, -25, -8, 12, -4, 0, -28, -30, -13, -11, 16, -3, 5, 5, 22, 7, -5, 20, -9, + -29, 29, -25, -27, -6, -22, 22, 11, -18, 3, -18, -21, -8, 6, -20, -22, -2, 25, + 25, -4, 19, 13, 9, 18, -25, -9, 16, -30, -30, 18, 29, 27, -26, 11, 28, -6, 4, + 29, -18, 28, 9, 23, 13, -22, -10, 21, 23, -13, -22, 8, -7, -6, 24, 11, 27, 8, + 15, 23, -11, -28, 7, -11, -2, -26, -21, -13, 22, 2, 6, 18, -17, 12, 11, -28, -3, + -15, -5, -14, -21, -9, -30, 12, 22, 1, 2, -8, 24, 22, 5, 29, -11, 25, -13, 1, + -22, -1, 11, 11, 12, 5, 14, 20, 13, 9, 2, 16, 10, 8, -30, -18, 8, 18, 23, -3, + -11, 5, -30, -7, -12, 23, -19, 9, 13, -4, 29, 14, 2, 29, -30, 6, -14, 16, 26, + 28, -9, -8, -4, 9, -7, 28, 20, -27, -13, 12, 13, -17, -14, 19, 7, 17, 14, -3, + 15, 24, -25, 6, 29, 24, 21, -20, 12, -24, -21, -30, -18, -2, 22, 5, 2, 27, 4, + 24, -21, 18, -4, 8, 11, -18, -2, -9, -22, 21, -6, 7, 10, 20, 9, -7, 15, -26, -4, + 3, -24, -26, 29, -28, -8, -3, -6, 4, -28, 12, -19, 17, 2, -30, 14, 8, -1, 6, 19, + -20, -30, 5, 5, 16, -9, 11, 25, 0, -24, 1, 18, 28, -5, -14, 7, -3, 18, -26, 26, + 1, -19, -19, 14, 29, 25, 13, 6, 2, 19, 28, -10, 26, -29, -12, 7, -11, -18, 28, + 1, 0, 19, 12, 20, -19, -7, -10, 5, 25, 19, 25, 2, 8, -5, -21, -7, -29, -19, -22, + 7, -20, -8, 7, -28, -10, 25, -29, -13, 2, -1, -21, -1, 11, -20, 28, 18, -28, + -22, 25, -29, -11, -20, 1, 3, -16, 20, -11, 20, -3, 9, -2, 25, 0, -27, 27, 11, + -5, 24, -18, 18, 28, -2, 29, -7, 21, -5, 9, -13, -25, -14, 23, -15, 8, 15, 3, + -28, 15, -23, 3, 13, -9, -7, 8, -6, -25, -16, 24, -23, 29, -7, -28, 15, 9, -18, + -8, 17, 29, 25, -2, -17, -9, -12, 20, 15, -17, -18, 23, 2, -9, -7, 1, 18, 13, + -11, -26, -13, 29, 7, -22, -16, -19, 7, 18, 19, 29, 0, 10, 21, -1, 25, 0, -13, + 0, -3, 16, 13, -19, 1, -23, 13, -10, -18, -1, 13, -27, -30, 21, 7, -18, 27, 21, + -30, -22, -29, 4, 21, 26, 12, -22, -12, -20, -20, 7, 7, -22, 17, -20, 19, -22, + -22, -24, -16, 2, 6, -11, -19, 24, -25, 28, -12, -30, 20, -18, 29, 17, -20, -27, + 18, -5, -11, -20, -15, 3, -4, 15, 2, -23, 16, -18, 21, 27, 7, 16, -24, -16, -14, + 28, 29, -24, 20, -19, -18, 5, 17, 0, -29, 1, 26, 6, 17, -8, -7, -24, -30, -7, + -29, -13, -20, 4, -7, 20, -13, -8, 19, 23, 20, 0, 2, -2, 27, 16, 2, -15, -10, + -18, -24, 2, 10, -2, -23, -29, -9, 4, -10, -10, -10, -11, -28, -5, -21, 5, 6, + -7, 17, 3, -25, 27, 28, -14, -2, -7, 18, 5, 16, -16, -29, 15, -25, -6, -16, -15, + 5, -21, -12, 17, 17, 10, 16, 11, -28, 1, -16, -13, 9, -3, 3, 2, -15, 16, 10, + -10, -27, 16, -18, 14, 6, 9, -6, -26, 23, 24, 28, 1, 27, -29, -13, -27, -22, + -19, -10, -4, -26, 3, -26, 9, -6, -16, -15, -7, -21, 11, -5, 24, 28, 27, -13, + 11, -12, 1, 15, -19, 20, 6, -11, 15, -10, 27, -8, 1, -12, -30, -9, -25, 26, 13, + 6, -4, -13, -5, 0, 23, -4, -24, 14, 24, -13, -29, 24, 29, 19, 3, -15, 26, -23, + -27, -23, 14, -17, 14, 0, 5, -19, -3, 27, 4, 20, 25, 15, 1, 26, -8, 22, 16, 9, + 11, -16, 17, -6, 26, -21, -21, 14, 28, 28, 18, -9, -14, -9, -17, -4, -27, -5, + -10, 23, -10, -22, -28, 25, 1, -2, 22, -3, 21, -4, 5, 17, 11, 20, -27, -24, 2, + -21, -10, 1, -16, 13, -10, 2, -20, -21, 13, -5, 16, 4, -3, 27, 25, -12, -13, -5, + -3, -22, 0, -18, 13, 23, -19, 4, 29, -20, -10, -26, 26, 20, -7, -16, -17, 0, + -28, 10, -17, 24, 0, -17, 26, 15, -19, 28, 14, -19, 27, -7, -6, -19, -17, 29, + 20, 9, 4, 22, -23, 0, 18, 12, -2, 6, -27, -28, -20, 8, -23, -1, 23, -16, 25, 25, + 4, -30, 21, 12, -22, 17, -1, -28, -16, 3, 11, 8, -14, 11, -17, -4, -30, -23, 11, + -10, 10, -27, 0, -4, -21, -26, -4, -20, 24, 2, 7, 28, 29, -6, -19, 29, 27, -28, + 0, 2, -29, -3, -4, -15, 19, 18, 13, 21, -15, 18, 6, 8, 26, -23, -23, 13, -22, + -11, -25, 3, -9, -22, -26, 12, 3, -27, -24, 0, 7, -10, -8, 6, -6, 10, -15, -11, + 20, -28, 19, 1, 29, 24, -25, -3, -10, 26, 29, -19, 27, -14, -27, -27, -18, -8, + -25, -7, 20, 11, -12, 9, -15, -1, -1, 8, -2, -1, -4, -20, 0, 3, 4, -24, 0, 15, + 22, 15, 19, 8, -25, -30, -5, -24, 29, -19, 19, -28, 17, -8, 12, 28, 3, -25, -7, + 7, 15, 13, -25, -12, -5, -18, -18, -3, 25, -25, -9, 22, -17, -29, 16, 28, 16, + -24, 5, -8, -19, -10, 9, 14, -7, 12, 12, 6, -1, 0, 20, 18, -29, 11, -16, 0, 8, + 6, -8, 8, 29, 29, 21, 29, 2, -8, 12, -16, 26, 21, 14, 13, 23, 7, -20, -26, -18, + -23, 4, -12, 10, -30, -8, -20, -8, -19, -30, -16, -23, 25, -27, -10, -4, 27, 12, + -30, 26, -15, -19, -27, -16, 23, -30, -28, 12, 9, 28, -28, 25, -1, 28, -4, -11, + 6, 15, -22, -24, -12, 28, -17, -13, -29, 18, 9, -25, -21, -19, 6, 17, 29, -17, + -1, -5, 29, -20, 16, 13, -12, 21, -4, 13, -17, -1, -26, 20, -29, -24, -14, 19, + -30, 16, 5, 11, -20, 29, 23, -3, 7, -1, -21, -7, -19, -4, 22, -2, -5, 5, -2, 4, + 4, -20, -13, -10, -28, 22, 27, -25, 11, -6, -11, 8, -10, -9, -16, -25, -28, 24, + 2, -25, -10, -28, 25, -12, -11, 17, -9, -2, 2, 21, -30, -14, 8, 19, -9, 5, 2, + 20, 26, -18, -4, 28, -4, 19, 20, 28, 17, 24, -18, 26, 1, -12, -23, -7, 3, 16, + -7, -29, -16, 10, 21, 11, -9, 3, -7, -8, -3, 13, 22, -6, 24, -23, -25, -5, 13, + -19, 9, 5, 23, 1, 11, 22, -4, -12, -26, 17, 22, 22, -20, -11, -10, 10, 4, -28, + -11, -5, -25, -18, 17, -13, 23, 0, -19, 23, -24, 12, -19, -15, -1, 7, 3, -8, + -25, -30, -22, 4, -9, -26, -23, 22, -17, -1, -10, 11, -25, -16, 25, -23, -30, + 13, 3, 14, 9, 21, -4, -2, 27, 8, 17, 15, -12, -23, -8, 24, 2, 26, 13, 3, 20, 25, + 17, 16, -22, 29, -22, -24, -5, -14, -1, -21, -30, -14, 4, -2, 24, -6, -7, 8, + -29, -23, -13, 4, 8, 4, -12, 1, 19, 4, -16, -2, 11, 13, -15, -28, 9, -17, -4, + -6, -17, 15, 22, -21, -6, -5, -18, 20, -15, -29, 10, -17, 25, -19, 17, 27, -7, + 16, -13, -26, 11, -10, -25, -4, -18, -13, 26, -6, 28, 14, 22, -12, -17, 7, 19, + 15, -21, 22, -30, 11, 11, -4, 4, -7, -15, 11, -11, 21, 21, -23, 18, -4, -30, 10, + 10, -7, -16, 19, -24, -2, -26, -4, -10, 2, 24, 13, 5, -27, 29, -14, 13, 24, 26, + -20, 21, -17, -25, 15, 1, 5, 0, -22, -8, 12, 28, 22, 1, 26, 22, -8, 22, -12, + -28, -21, -26, -23, 21, -22, -15, 14, 28, -26, -27, -14, 3, -1, 13, -23, -14, 2, + 23, 16, -24, 12, -10, -20, 17, -27, 15, 18, -26, 27, 24, 3, 26, 6, -16, -28, 26, + -7, -22, -5, -1, 24, 16, -3, 5, -21, -25, 7, 8, 22, 25, -8, -25, -2, 4, -19, 16, + -25, 14, 26, -26, 8, 27, 21, 16, 29, 22, 12, -9, 28, 1, 18, -5, 16, -28, 18, 20, + 10, -24, 13, 7, -11, -3, -4, 8, 21, 17, -17, -14, 5, 1, 29, 10, 2, 15, 0, 25, + -12, 14, 16, -15, -19, -19, -24, 28, 24, 23, 24, 28, 24, -5, -17, -29, -30, 29, + -11, -25, 21, 25, 10, -17, -23, 12, -9, -20, -2, -13, 29, -2, 8, -17, 16, 7, -4, + 27, -3, -10, -30, 3, -4, -6, 27, -11, 25, -18, 9, -12, 10, 25, 25, -7, 6, -13, + 9, 0, 25, -26, 24, 15, 1, -17, 3, -4, -19, 17, 2, 22, 28, -23, 24, -30, -11, 25, + -15, 2, 27, 10, -18, -3, 12, 13, -2, 9, -29, -11, -13, -1, -26, 23, 23, 21, -14, + -4, -21, -17, -6, -8, 7, -29, 11, -28, 28, -2, -8, -3, 10, -23, 12, 9, -19, 2, + -15, -13, 24, -24, 28, 14, -11, 14, 19, 8, 12, 6, -19, -27, -13, -17, -12, 19, + 23, -7, -27, -12, -11, 13, 3, -5, 24, -26, -30, -15, -10, -22, -24, 12, -10, + -17, 7, 22, -22, -3, -29, 11, -1, -12, -7, 21, -21, 8, 22, -10, 1, -27, -11, 13, + -1, -2, 9, 10, 1, -10, -5, 22, -2, -27, 6, 14, 13, 17, -2, 29, -13, 8, 15, -26, + -21, -5, 20, -19, -17, 22, 28, 9, 26, 7, 21, 27, 15, 21, -13, 2, -23, 28, -16, + -6, 16, -21, -22, -1, -19, -13, -5, 11, -14, -9, 25, 16, 21, -18, -3, 13, -5, + -24, 19, -15, 13, 14, -21, -30, -17, 5, 19, 27, -14, -6, 29, -5, 12, 3, -15, 3, + 1, 4, -20, 3, 25, 3, -8, 22, -19, 6, -8, 13, 0, 20, -2, -11, 15, 10, -20, 5, + -23, -8, -27, -30, 14, -27, -22, -10, 14, -17, -22, 12, -14, -23, -30, 2, -6, + 14, 29, 27, 14, -13, -16, -24, 16, -11, -14, -19, 8, 12, -12, -3, -28, 9, -12, + -6, -19, 15, 19, 27, -22, -2, -27, -8, 16, 25, 25, -12, 11, -11, 7, 3, -3, -13, + -5, 4, 25, -21, 26, -30, -20, -12, 23, -2, -8, 20, 4, -25, -4, -4, 28, 26, 4, 0, + -13, 26, -26, 25, 17, -7, 15, 29, -29, 18, 6, 17, -1, 3, -6, 6, -5, 15, -26, + -11, -15, -1, -23, -27, -10, 9, -29, 23, -11, -18, -3, -7, 23, 19, 21, -27, -2, + -7, -26, -8, -29, 29, 0, 23, -19, 1, -29, 26, -9, 12, -10, 0, 6, 14, 7, 19, -23, + -19, -22, 21, -18, 13, -25, 9, -10, 6, -23, -16, -20, 27, -11, -9, 26, -25, -8, + 5, -3, 12, 12, -17, 1, 25, -6, -20, 26, -19, 2, 20, -7, -26, 12, -19, -2, -3, + -4, -20, 15, -9, -19, -22, 2, 28, -2, 11, -3, -20, -11, 24, -29, -10, 22, -19, + 10, 7, 28, -22, -12, 5, 19, -9, -21, 5, 2, -28, 23, 18, -17, 18, -2, 26, -26, + -20, -18, 16, 3, 6, 7, -16, 24, -20, 27, 1, -13, -4, -7, -27, 1, -11, -26, -10, + 9, -24, 23, 24, 19, 17, -9, 22, -28, 0, -4, -29, 11, -18, -13, 11, 11, -26, 21, + -28, -19, 16, 17, -1, 21, -3, -1, 11, -12, -18, -18, -1, 27, -9, -13, -7, 29, + -11, 28, -29, -20, 16, -24, -1, 26, 7, 16, 28, -18, -3, -18, -13, 24, -12, 21, + -12, 27, -14, 22, 1, 26, 24, 22, 13, -28, 12, 6, 15, 29, -29, -16, -10, 1, -9, + 27, -23, 8, 23, 10, -20, -20, 29, -6, 26, 8, 17, -5, 14, 17, -20, 21, -28, 11, + -8, 20, 17, 1, 7, 25, 3, -18, 28, 0, 27, -11, 17, 12, -26, -28, 3, 2, 7, -11, + 29, -2, -21, 26, 23, -22, 23, 19, -5, -27, 15, -2, 13, 25, -20, -29, -15, 18, 8, + 14, -21, -24, -30, -29, -6, -9, -20, -28, 3, -14, -3, 25, 12, 1, -16, 1, 1, -20, + 8, 21, -1, -23, -18, 8, -12, 1, 5, 15, -12, -27, -30, -14, -3, -4, 14, -22, -17, + 29, 21, -3, -22, -13, 5, -11, 16, -9, -20, 18, -16, 19, 29, -16, 17, -26, 10, + -19, -15, -12, 11, -17, -11, -20, 21, 21, -26, 12, 5, 25, -18, 29, 17, -29, 25, + -27, -7, 8, 11, -15, -12, -19, 27, -19, 6, -1, 3, 23, -6, -8, 23, -2, -15, -3, + -20, -11, -23, -28, -7, 12, -15, 1, -8, -16, 22, 9, 3, -16, 11, 10, -25, -25, + -26, -14, 11, 0, -22, -7, 18, -12, 26, -14, -2, 19, -28, 4, 29, -16, 15, 11, + -22, -13, 11, 7, -2, -23, 18, 3, -7, -17, -16, 23, -29, 16, -8, -28, -21, 17, + 14, -24, -13, 18, 3, -25, -5, -17, -1, 20, -19, 28, -2, 6, -22, -13, -30, 16, + -22, 9, 28, -25, 14, 16, 27, 7, -13, -16, -14, -20, -28, -12, -14, 4, 16, -16, + 7, -18, 26, -30, -4, -7, -30, -7, 19, -12, 1, -26, 0, -18, -10, -22, -19, 27, + -21, 18, -22, -22, 7, -29, -18, -27, -11, 1, 14, -5, 19, 15, 20, -20, 18, -13, + -23, -4, -23, -16, 21, -18, 26, -14, -25, -17, -12, -25, -5, 28, -29, 28, -21, + 1, -10, -30, -2, 4, -5, 28, -14, 2, -22, -14, 26, -23, -28, -4, -20, 3, -13, + -12, 12, 15, 26, -7, 12, -6, -23, -7, 0, -11, -13, 8, 7, -22, 22, 13, 25, 5, 18, + 0, 13, 14, 15, 29, 15, 12, 3, -5, 14, -20, -26, 6, -12, -28, -28, 4, -2, 27, -2, + -17, -13, 7, -25, -28, 6, -13, -10, -5, -8, 11, -3, 23, -16, 10, -24, -15, -15, + 6, 28, 13, -18, 22, -25, -7, -3, -23, 16, -25, 7, -29, 2, -14, -27, -22, -2, 21, + -17, -5, -6, -12, -27, -9, -8, 24, -21, 22, -13, 29, 3, 19, 10, -22, -29, 21, + -13, -6, -24, -3, -20, 22, 9, 8, -28, 8, -11, 6, -4, 3, -4, -10, -2, -23, -14, + -3, -24, 26, 6, 6, -22, 10, 18, -2, -22, 14, -20, 29, -28, -25, -16, -23, 29, + -15, 21, 8, -22, 0, -5, -26, 10, -30, -29, -19, -14, -15, -5, -8, -27, 18, 4, + 26, -9, -29, 24, -8, 11, -19, 6, 3, -4, 3, -6, 1, 22, -12, 15, 18, -26, 11, -1, + 2, 20, 14, -11, -18, -26, -8, 14, 13, 22, 5, -14, -22, -7, -4, -21, 20, 6, 7, + 29, -7, 13, 19, 5, -28, 13, -17, -1, 6, -26, -16, -18, 16, 9, -1, -26, 3, 10, + -5, 13, -16, 19, -11, 16, 19, -17, -6, -13, 3, 22, 8, 20, -15, 21, 21, 13, 7, + -10, -3, 20, -20, -19, 28, 27, -6, -14, 25, -15, 3, 18, -27, 15, -4, 25, -25, 2, + -17, -17, -28, -30, -27, 28, -24, 25, -22, 3, 13, 10, 6, 18, -12, -5, -12, -29, + -26, -22, -8, -18, -9, 7, 14, -22, -13, 5, 26, -28, -1, -24, -4, 8, 7, 14, -30, + -21, -10, -30, 12, 18, 14, 4, 8, 24, -6, -8, 22, -21, -2, -13, -28, -3, -25, 4, + 20, -21, -28, 25, -26, -25, -23, 5, 1, -30, -6, -22, -15, -28, 4, 4, -1, -24, + -12, 8, -2, 13, 1, 7, 19, -9, 17, -5, -20, -7, -21, -12, 25, 19, -24, -27, 27, + -29, -19, -2, -18, 20, 13, -17, 23, -29, 12, -30, -9, -28, 8, 5, 24, 17, 19, 12, + 6, 2, -10, 9, -8, -1, 1, 27, 9, -17, -8, 8, -2, 11, -2, -5, 0, -28, 25, -29, 19, + -25, -24, -30, 29, 1, 17, 16, 13, -5, 29, -26, 15, 24, -3, 25, -30, 2, 15, 12, + 5, 23, 24, -17, -21, -4, 14, 12, 9, 13, 17, 12, -2, 24, 2, -6, 12, 5, 11, -17, + -18, -29, -5, 10, 18, -11, 15, -20, -4, 16, 26, 28, 24, 0, -22, 17, -15, 25, -4, + 28, -17, 3, -2, -23, -28, 27, -5, -25, -13, 11, -25, 0, -18, 22, -27, -12, 11, + 21, -4, -3, 2, -27, -18, -17, -7, -8, -8, -22, 25, 7, -9, 5, -19, 14, 7, 27, 9, + 8, 19, -7, -20, 15, 24, -18, -9, -8, 19, 28, 18, 28, 11, -28, -30, -26, 7, -11, + -22, -24, 26, 14, -22, -2, 20, -27, 18, 26, -3, 13, 20, -26, 18, -12, 2, 29, -9, + 26, -25, 12, 21, 17, 16, 26, -12, -14, 2, 26, 17, -10, 11, -27, 23, -18, -30, + -11, 0, -3, 1, -9, -18, 23, 10, -22, 4, 11, -30, 13, 7, -21, -26, 11, 15, 24, + -5, 11, -21, 1, 26, 18, -24, 25, -2, 1, -29, -23, -13, -16, -11, -20, -21, -23, + -11, -15, 14, 3, -12, -1, 22, -1, -11, 21, 13, 12, -4, 17, 9, 27, 29, -13, 3, + 10, 28, -1, -24, 14, 23, -21, -23, 28, -20, 16, -15, 28, -9, -4, -15, 26, -12, + 4, 20, 6, 16, -12, 16, 16, 22, 4, -7, -2, -10, 4, 21, -19, 12, 16, 18, 6, -14, + -24, 25, 12, -3, -5, -4, 8, -5, 11, -17, -6, -17, 2, -2, 20, -28, -15, 23, -2, + 23, -23, 19, 8, 1, 26, -28, 15, 18, 23, 23, -14, 20, -7, 27, -27, 8, -22, 3, 5, + -30, 10, -9, 4, 24, -7, -6, 25, -18, -17, 1, -5, 25, 3, 20, 5, -14, 7, -25, -15, + 20, -10, 7, -25, -2, 9, 19, -17, 20, -24, -3, 4, 22, -18, -26, 23, 9, 24, -25, + -29, -19, -30, 27, -2, 18, 15, -26, -19, 29, -30, 23, 7, -20, 15, -6, -8, 24, + -27, -20, -27, -13, 7, 24, 7, 10, -7, -8, 28, 4, -8, -3, 3, -7, -30, -4, 12, 19, + -7, 23, -3, 11, 12, 16, -8, 12, -13, -16, 4, 17, -1, 11, -8, 4, 13, -12, -29, 7, + 13, -12, -25, 22, 13, 13, 29, -29, 8, 29, 17, 28, 5, 14, 23, 8, 19, -19, 13, + -27, -30, -6, 13, 25, -27, -1, -21, 10, 15, -17, -4, -8, -18, -25, 2, 11, 10, + -11, 29, 3, -25, -18, -4, 5, -30, -10, -24, -1, 1, -29, 25, 23, 19, -10, -19, + 28, -18, 29, -1, -1, 5, 18, -29, 10, 26, 29, -20, -21, 9, 15, 19, -4, -25, -9, + -23, -2, -3, 26, 0, 0, -19, 29, -24, -10, 12, 22, -26, -9, 15, 29, -9, 9, -1, + 14, -6, 20, -13, 9, -13, 18, 20, 14, -25, 22, -23, 27, 25, -21, -2, -29, -8, + -16, 8, 20, 16, -6, 7, 13, 11, -26, -14, 15, -24, 1, 0, -26, 26, -2, -8, 8, 23, + 0, 20, -10, 2, 29, 4, -17, -13, -17, 13, 10, -6, 6, -23, 20, 18, -9, -11, 27, + -16, -5, 14, -2, 29, -1, 18, 18, -8, -15, -2, 2, -23, -7, -11, -1, -22, -26, 24, + -18, 17, -8, -30, -16, 20, 14, 20, 17, -15, 20, 9, 20, -20, 13, -12, -10, 15, + -17, -17, 10, 6, -4, 16, -10, -7, 16, -22, 25, -4, 9, 25, -3, 8, 12, 12, 27, 16, + -15, 9, 18, -25, -25, 1, 16, 25, 13, 17, 29, -11, 5, 23, -30, 14, 28, 22, 9, + -30, 3, -15, 29, 24, -11, 28, -8, 26, -11, -24, -27, -5, -14, 29, -5, -4, -10, + -25, -28, -6, -15, 18, -11, -16, 25, -20, 19, 26, 16, 28, -5, -15, -28, -4, -15, + 7, 22, 0, 22, -21, -19, -18, 9, 26, -2, -1, 10, -27, 11, 12, 15, 19, -27, 22, + 23, -25, -13, -28, 19, 24, 20, 3, -17, 25, 1, -8, 3, -17, -20, 5, 12, 18, 25, + -2, -9, -13, 4, 11, 17, -15, 4, -28, -7, -12, -17, 14, 18, -1, 2, -4, -16, -26, + -11, 1, 18, 15, 8, -8, 18, -10, 8, -8, -26, 8, 17, -5, -4, -12, -27, -18, -19, + -27, 26, 10, -30, 29, 10, -15, -17, -8, 28, -24, -27, -30, -14, -15, 8, 2, 18, + -14, 26, 14, 10, -4, -13, -4, -7, 15, -18, -24, -16, -10, 9, 5, 21, -28, 18, + -18, -1, 2, -26, 9, -17, -12, 18, 22, 4, -16, 0, 27, -21, -20, -21, 5, -3, 21, + -24, -21, 25, -8, 11, -22, 29, 10, 2, 8, 17, 8, 21, -13, -28, 12, 14, -30, -23, + 23, -29, 12, -10, -20, -29, -11, 15, -29, -6, 17, -11, -5, 9, 0, -23, -5, -22, + -8, 13, -29, 28, -2, 14, 27, -21, 20, -11, 20, -16, -16, -24, -17, 1, 19, -28, + 16, -4, 21, -30, 3, -7, 24, -1, 27, 9, 20, 13, 19, 11, 13, 23, 15, 18, 17, -4, + 24, -19, 17, -27, 16, 20, -13, 5, 14, -14, -2, 7, 12, -11, 26, 20, -28, -22, 16, + -26, 4, 9, 14, 16, -10, 18, -8, 11, 12, -19, 2, 19, 17, -26, 10, -24, 11, 21, + -18, 12, 28, -4, -17, -14, 4, -21, -19, 16, -7, -28, 7, -2, -30, 25, -29, 28, + -6, -21, -21, 18, -7, 29, -13, -29, 16, -30, 23, 27, 22, -8, -2, -11, -1, 6, 5, + 14, -7, -7, 13, 24, 19, -13, 8, -24, 6, 20, -9, 11, -4, -14, 25, 15, -5, -27, + -20, 11, -18, -15, 20, 17, -25, -15, 6, -28, -19, 28, -4, -22, -2, 13, 23, -22, + -18, 15, 10, -25, 3, -13, 17, 7, 16, 24, -6, 7, -16, 14, -16, -23, -9, 19, 6, + -2, -4, -9, -21, 13, -25, 14, 15, -2, -28, 16, 23, 16, 10, 4, 27, -8, -19, -6, + 1, -22, -23, 20, 21, 13, -25, 16, -16, -29, -13, 4, -25, 3, -4, 7, -16, 4, -23, + 8, -16, 3, 26, -19, -8, 4, 10, 7, 2, -18, -12, -4, 28, -27, -11, -18, -24, -26, + -4, 10, 11, 10, -15, -19, 8, -13, -20, -15, 2, -8, -13, -21, 26, -24, -13, -30, + -30, 15, -8, -22, -6, -23, -11, 2, 18, -24, -2, 10, 6, 5, -12, -11, 10, 18, 18, + -19, -11, 15, -9, -4, -12, 23, 1, -27, -23, 10, -10, 0, -25, 22, -2, -9, -19, + -10, 27, 28, -18, -5, 28, 8, 11, 22, -2, 5, -16, -9, 18, 24, 3, 2, -3, 29, -21, + 27, -14, 0, 29, 8, 12, -14, -8, 3, -4, 17, -30, -19, -18, -7, 8, -18, 5, 16, 15, + -22, -22, -23, 2, 21, 21, -22, -12, -3, 22, -1, 2, 26, -5, -9, 22, -1, -13, -22, + 23, 3, -28, -12, -27, 8, -18, 18, -26, 1, -8, 27, 2, -8, 8, 26, -16, 24, -26, + -5, -11, -21, -1, 8, -6, 26, 24, -30, 28, 6, 26, -3, -8, -19, 24, 4, 6, -18, + -20, -25, -3, -16, 24, -9, -12, -11, -23, 24, -7, -16, 11, 19, 15, 0, -5, 1, + -25, -12, -28, 5, -9, -19, -22, -22, -6, 15, -26, 14, 5, 0, -21, -17, 19, 14, + -22, 14, -19, 8, 0, -18, -27, -7, -2, -25, 28, -23, -14, -19, -12, 24, -27, 5, + 19, -27, -27, 14, 22, 24, 0, 8, 6, -16, -4, -1, 8, 5, -26, 7, 25, 13, 1, 29, -7, + -26, 12, 25, 18, 5, -20, 14, -19, 2, -23, 24, 8, -27, -27, 9, 11, -25, -6, -24, + -7, 13, -7, 14, 19, 27, -1, 14, -28, -3, -23, -23, -27, 29, -20, -21, 11, -12, + -18, 1, 3, -26, -15, 8, 26, -19, 17, -6, -26, 14, -18, 9, -6, 28, -20, 1, 1, 5, + 23, 16, -9, 15, 23, -11, -16, 12, 20, -4, 29, -13, -25, -2, -6, 0, 9, -24, -6, + -22, -26, -10, 2, -1, -10, -5, 25, 12, -24, -8, 26, -8, -4, 7, -16, -20, 11, + -10, -22, -11, -18, 8, -22, -2, 16, -20, 25, 4, -24, -4, -26, 15, 15, -19, 12, + -1, -1, -30, -14, -18, -6, 11, 12, -10, -9, -3, -20, 19, 13, -1, -4, 2, -21, 27, + 20, 15, 3, 12, 25, 21, -1, 20, -25, 25, 4, 22, 20, -25, 29, 23, 25, 17, 9, -3, + 18, 28, 15, 16, -17, 8, -30, -7, -26, 16, 23, -30, -26, 12, 18, -11, -19, 29, + 11, -28, 29, -9, 9, -26, 29, 17, 24, -24, 14, -15, 29, -17, -22, 2, 22, -10, 6, + -20, -19, 20, -29, -9, -3, 15, 11, -11, -16, 16, -15, 22, 25, 25, 21, -6, -17, + -27, -5, -18, -17, -9, 9, 15, -2, -28, -4, 20, 6, 22, 15, -10, 6, 12, -20, -1, + 19, 16, -3, -11, -18, 1, -17, -19, 12, 18, -3, 9, 4, -30, 23, 14, 11, -6, 2, 22, + 16, 13, 9, 9, 20, -3, 23, 11, 6, -24, 8, 0, 19, 28, 7, 24, 6, 19, -20, -1, 2, + 18, 10, 16, -25, 18, -28, 21, -28, 27, -22, 15, -8, 6, 5, -17, 12, -27, -5, 22, + 24, 29, -20, -18, 14, -1, 24, 11, 3, 7, 3, 18, 21, 7, 1, -16, -7, 17, 8, 18, + -30, -30, 27, 1, -7, 26, -25, 5, -27, 8, 5, 8, 24, 14, -21, -12, 27, 25, 14, + -19, -22, 5, 6, -29, -1, 12, -12, 24, 28, 18, 12, 7, -7, -19, 26, 28, 12, -10, + -21, -30, 25, 0, 14, -12, 22, 0, -18, 12, -8, 7, 28, 11, 28, -19, -27, -30, -16, + -30, 13, 21, -5, -30, 22, -30, -20, 9, 16, 25, -8, -18, 7, -20, -6, -17, 7, -30, + 19, -5, 13, 1, -5, -17, 18, 16, 2, 14, 1, 2, -6, -5, 18, -18, -18, -7, -30, 13, + -16, 3, -29, -1, 21, 26, 28, -27, 13, 24, -22, 27, 18, -29, 6, 26, -23, -29, 8, + 24, 7, -4, 21, -11, 19, -19, 5, -25, -2, 18, -1, 2, 13, -6, 4, 7, 3, -7, 20, 22, + -6, 11, 5, -2, 15, -9, -8, 16, -10, 6, -30, -16, 28, -3, 14, 22, 16, 19, 16, + -27, -10, -18, 2, 6, -1, 6, 20, -12, -21, -29, -29, -5, 12, -19, 6, 3, -22, 3, + -27, -9, -23, 12, -12, -17, 4, 19, 0, -8, -7, 28, -19, -9, 2, -23, -10, -18, + -23, -5, 13, 10, -17, -26, -20, 28, -20, 8, 11, -5, -20, 29, 29, -12, -8, -11, + 6, -2, 14, 23, -26, 21, 26, 23, 22, 6, 26, 22, 20, -20, -11, -7, -27, -6, -19, + 28, -8, -23, 2, -18, 23, -7, -28, 23, -23, 1, -3, -15, 20, 20, -3, -12, 12, -17, + 2, 5, 13, -7, -29, 22, -25, 21, 13, -12, 14, -29, 5, 29, -26, -6, 10, -22, 5, + -2, -21, -2, 17, -30, -7, -3, 22, 22, 6, -9, -9, 0, -5, -25, -3, 28, 8, -28, 20, + -19, 17, 21, -6, 6, -26, 27, -17, -1, 20, -12, 23, 13, 3, 21, 8, 1, -29, 7, -20, + -6, 3, -28, 11, -26, -5, 14, -15, -24, -7, -6, 6, -9, -29, 27, -17, -22, -20, 4, + -17, 3, 15, 4, 8, 11, 18, 17, 19, 1, -27, 29, 6, -21, -25, 22, 19, -30, -6, -20, + -21, 28, 23, 5, -10, -27, -27, 25, 27, -17, 11, -20, 1, 4, 5, 14, -9, 27, -17, + -14, -27, 22, -23, 6, 28, 18, -1, 22, -4, 28, 26, -1, 14, -7, -19, 28, 16, -14, + 29, 5, 29, -14, 11, 9, -27, -4, 12, 18, -30, -15, -25, -17, -25, -1, 16, 10, -6, + 13, -23, 4, 5, -24, 29, 26, -30, -6, -17, 25, 27, -7, 4, 11, -16, 15, 13, 13, + 20, 17, -24, 25, 12, -26, 21, -3, 12, -30, -18, -11, -28, 16, -9, -29, 19, 18, + 29, 21, -6, -5, 21, 9, 3, 4, 13, 25, -2, 0, 24, -12, 13, -11, 15, -26, 5, -11, + -23, 8, 9, 13, -5, -20, 22, -10, 0, -8, -19, -19, -24, -7, -8, 1, -4, -4, -11, + -11, 19, 16, -30, -1, -11, -4, -23, 17, -29, -11, 13, -2, 27, 3, 9, 5, -22, -11, + 25, -1, -15, 26, 28, 19, -18, -8, 3, -25, -29, 21, 21, -7, 29, -15, -8, 24, 14, + 19, -16, -7, 13, -22, 20, 10, 8, 3, 1, -13, 0, 26, 11, -16, 21, -29, -1, -14, 1, + -12, -17, 22, 14, -24, -10, 20, -8, 23, -9, 14, 5, 29, -23, 5, -7, -11, 18, 29, + -10, 8, 16, 25, -3, 18, -11, 4, -20, 17, 19, -12, -29, -8, 28, 29, 2, -21, -28, + 6, -28, -6, -3, -19, -27, -13, -3, 1, -1, 6, -9, -26, 20, 9, 11, 24, -27, -7, + -16, -9, 26, 24, -13, -1, -7, 27, -2, 29, 5, 24, 20, 19, -24, 14, 1, -22, 7, + -15, -9, 25, -22, 10, -29, -3, -17, -5, 13, -25, 7, -29, 14, -26, 16, -27, -20, + 0, 27, -4, 5, 29, -3, 4, -6, -1, 18, -21, -13, -28, 10, 19, -24, 13, -13, 27, + -14, 10, -3, 25, 27, 20, -19, 24, 8, 13, 29, -28, -6, 12, 28, -4, 29, 25, 14, 2, + -27, 14, -12, 5, 15, 11, -22, -28, -13, -28, -2, -13, -12, 26, 29, 17, 1, -10, + 17, -15, 15, -6, 13, 21, 16, -3, -10, 12, 10, 18, -14, -29, -14, 27, 15, 13, + -19, -12, 15, 21, -3, -12, 11, -19, 17, -23, 27, -23, -18, -3, -16, 21, 29, 24, + -23, 27, -10, -4, -17, -19, -8, 11, -13, 27, 8, 8, 27, 6, 21, 19, -30, -27, 17, + 23, -6, 13, 17, -14, -8, -3, 18, 28, -23, 7, 28, 24, 19, 7, -3, -24, 8, 9, 3, + -28, 17, 17, 4, 19, 27, -28, -29, 6, -18, -30, -8, 26, -18, -4, -28, -15, -13, + 22, -18, 9, 22, -3, -30, 0, -23, 28, 17, -25, 25, -6, -24, 10, 6, -30, -23, 6, + 25, -9, 23, 3, -21, -11, -27, -10, -24, -20, -14, -18, -12, -16, 6, -6, -7, -30, + 17, -5, 11, 4, -19, -20, 1, -25, -11, 20, 4, -26, -8, -9, 15, -9, 9, -30, 9, -3, + -3, -27, 20, 11, 19, -17, -17, -25, -7, -20, -1, -25, 12, 18, -13, -20, -11, + -17, -6, -27, 11, -21, -26, 17, -19, -17, 14, -24, -11, -2, -23, -16, -1, -4, + 26, -9, -24, 5, -27, 10, -4, 3, -20, -14, -30, 0, -1, -17, 15, 11, -6, 10, 6, + -14, -24, 22, 11, -6, 21, 11, -1, 27, -30, 23, 8, 27, 21, 14, -5, 2, 21, 26, + -10, 11, -1, -11, 21, 26, -18, 23, 4, -15, -22, -9, -9, 18, 15, 21, 6, -16, 22, + -30, -5, -10, -4, 21, 16, -26, -17, -21, 21, 9, 11, 6, -12, 0, 9, 14, -4, 15, + 25, 17, 3, -10, -27, 25, -28, -2, -6, 12, -13, -23, -5, 7, 2, 26, 28, -24, -30, + 20, 10, -1, 27, -13, 8, 15, -3, 10, -13, -21, 18, 11, -5, -28, 1, -4, 9, -1, + -18, -18, 9, 5, 18, -7, 13, -11, -2, 12, -27, -11, -26, -9, -2, -24, -5, 11, -6, + -27, -10, 17, -22, -21, -3, 19, -24, -27, -11, -4, 16, -11, 10, -1, 8, 12, -26, + -16, 0, 0, -4, 15, 19, -17, -19, -10, -19, -24, -14, 13, 27, 16, 18, -27, 5, -1, + 28, -30, -8, -24, 24, -6, 3, -29, -26, -24, -28, -21, 3, -18, -25, -11, 13, -25, + -14, 12, 23, 28, 25, -7, 5, 6, -5, 15, 2, 1, 27, 5, -9, 21, 3, -23, -11, -22, + -2, -19, 27, -10, 24, 19, -26, -22, 12, -2, 19, -27, -21, 25, 27, -6, -12, -19, + 22, 21, 25, -1, 5, -9, 23, 18, 6, -13, 26, -5, -18, 24, 12, -8, -18, -8, 8, -21, + -17, 22, -15, 13, -29, -4, 29, -11, 10, 25, 12, 23, 11, -1, -16, -29, 8, 21, + -22, 23, 20, 16, 0, -22, 15, -16, 2, -14, 29, 2, -23, 1, -11, 20, 7, 20, -18, + -11, 20, 25, -17, 19, -15, -17, -10, 27, 3, -4, 3, -16, -4, -25, 11, 21, -18, + 24, 5, -11, -19, 5, 2, 21, -22, 10, 25, 25, 25, 17, -26, -29, 5, -11, 2, 5, 13, + -26, 1, 2, -1, -22, 24, 29, -4, 6, -26, -3, -1, 3, 15, -15, -22, -12, -20, 28, + -1, -26, 2, 17, 21, -5, -8, 6, 23, -28, -27, 19, 6, -6, 27, -1, -2, 12, -2, -2, + -9, 24, 5, 21, 3, -25, 27, -2, -16, -17, -10, -16, -4, -12, 9, 24, 21, -17, 21, + -21, 18, 3, -23, -7, 5, -19, -5, -10, 7, 3, -21, 8, 6, 29, -4, 8, -6, 2, 22, + -18, -10, 6, 14, 16, -2, -3, -15, 2, -26, -17, -9, -15, 18, 22, -3, -13, -14, + -2, 15, -24, 6, -1, 20, 15, -5, 14, -21, -10, -24, 2, -6, -9, -29, -2, -1, 4, + 24, 21, -17, 3, 12, 5, 20, 1, -16, -4, 19, -23, -23, 17, -7, -12, -20, -16, 24, + 18, -22, -19, -14, -12, -26, 19, -9, 14, 4, -7, 8, 9, 10, 27, 2, -13, 10, -1, 7, + 26, 27, -2, -9, 6, -7, -26, 27, -6, -12, -23, 19, 15, 5, 12, 9, 18, -13, 15, 12, + -9, -27, -7, -9, 10, 29, -28, 7, -2, -6, 7, -25, 13, 23, 4, 26, 8, -9, -11, 20, + 23, 2, -6, 2, -15, -14, 22, -27, 26, 12, -16, -2, -2, -23, 17, 3, 12, 24, 15, + 22, 15, -18, -4, -26, 1, -11, -3, -9, -7, -17, 10, 12, 15, 3, -19, -3, 3, 2, 15, + 28, -30, -1, 26, -4, -1, -30, 5, 23, -10, -21, -15, 10, -4, -5, -2, 3, 18, 24, + -18, 23, -17, -15, -18, -9, -4, -19, -16, -25, -21, 25, -20, 23, -18, -2, -13, + 14, -6, -2, -19, -23, -28, 18, 14, -21, -21, 25, -10, 5, 20, 0, 11, 26, -12, -4, + -4, 28, 28, -2, -1, 9, 19, -2, -9, 2, -15, 5, -19, 2, 23, -30, -27, -28, -21, + -22, 10, -26, -18, 7, -6, -2, -11, 9, -9, -1, -3, -26, 16, -27, 7, -30, 24, 2, + 4, -29, 21, 1, -17, -22, -16, -2, 13, 5, 22, -28, 10, -18, -16, -25, -25, 6, -8, + 17, 18, 16, 23, -2, -30, 29, 10, -23, 8, 27, -15, 15, 3, -26, -25, 29, 1, 3, + -14, 18, 26, 5, -26, -19, -30, -30, 29, -7, -17, 14, -18, 26, -11, 20, -9, 20, + 23, 6, 14, -16, -21, 27, -6, 24, -26, 20, -24, -4, 6, -9, 4, 3, 28, 0, 7, -29, + -18, -9, -16, 12, 2, -17, -18, -16, -23, 25, 6, 9, -28, -24, 4, -14, 1, -7, 4, + -17, -26, 17, 28, -17, 22, -18, -3, -28, 1, -9, -24, -10, -17, 16, -2, 13, -16, + -3, 24, -27, 11, 19, -11, 20, -27, 21, 21, 3, -23, 18, 3, -17, 3, 24, -1, 27, + 18, -15, 7, -6, -29, -10, -3, 10, -16, -3, -5, 4, -15, -29, 6, 20, 22, -5, -9, + 18, 17, -17, -25, 19, 16, 21, 15, -26, -24, -30, -10, 6, -23, 7, 3, -4, 19, -27, + -10, -9, -15, 10, 14, -24, 12, 6, 9, 5, 15, 20, 13, -26, -22, -17, -4, 22, -15, + 6, -1, 0, 7, 20, -21, -26, 4, 28, 29, 7, 29, 23, 16, -15, 11, -12, 28, -6, -13, + 14, 2, 8, 29, -25, 7, 13, -21, -23, 15, -4, -16, 17, -4, 10, 23, 11, 24, 9, 6, + 13, 20, -21, -23, -11, 21, 4, -7, -2, -27, 25, 13, 3, 27, -19, 12, -22, 25, -18, + 11, -12, -16, -19, 6, -6, -30, -12, 19, -22, 28, 9, 28, -8, 25, 15, -29, -9, + -17, 16, -9, -4, 21, -30, 5, 25, 2, 28, 17, -1, -2, 6, -13, -22, 20, 18, -18, + 10, 20, 9, -11, 15, 10, 4, 17, 27, -13, 28, -13, 5, -3, 29, 14, -18, -9, -4, -3, + 15, 14, 17, 3, -11, 3, -28, -16, 9, -23, -7, 21, 8, -21, 25, -17, -28, -11, -4, + -7, -26, -5, -3, -4, 22, 3, -16, 9, -3, -9, -28, 11, -6, 16, -24, 6, -7, 19, 25, + 28, -14, 9, 27, -14, -15, -18, -26, -14, -25, -10, -9, 13, 23, 5, 2, -8, 16, + -19, -5, -27, 20, -21, -10, 24, 26, 2, -23, 3, 10, 15, 1, 20, 25, -26, -24, 1, + -16, -29, -23, -26, 4, -22, 7, -12, -8, -19, 16, -19, 21, 19, 7, -11, 7, 9, -15, + 28, -5, -19, 12, -6, -20, 11, -6, -20, -11, 26, 28, 19, -3, -21, 0, 11, 13, 14, + -9, -5, 28, -2, -17, -11, -26, -18, -24, -18, -17, -23, -3, -9, -4, 14, -30, 25, + 15, 15, -9, 12, 15, 4, -20, -16, 16, 21, -9, 25, 12, 21, 14, 5, 4, -27, -2, -14, + 2, 25, 7, -9, -21, -15, 15, -15, -2, -16, 5, 11, 3, -2, -30, -5, -17, 21, 26, + 14, -11, -5, -7, -20, 2, -5, 22, -6, 11, -27, -6, 29, 15, -1, -30, -12, -25, -5, + 11, 6, 13, 11, -2, -15, 13, -23, 23, 27, 5, 27, -29, -29, -27, 18, -5, 8, -29, + -20, 26, 15, 13, 19, -30, -5, 19, 6, -5, -14, 20, -5, -16, 18, -15, -5, -19, + -19, 13, -17, -8, 11, -11, -28, -12, -25, -1, -3, 9, 13, 11, -28, 2, -5, 2, -17, + -13, -26, -21, -19, 16, 24, 4, 3, 6, 4, 14, 20, -29, 11, -11, -27, -22, 22, 18, + 10, 7, -7, -20, 22, -23, -26, 13, -29, -27, -5, 14, -8, 13, 26, 7, 9, -10, -13, + -23, 21, 26, -18, -21, -17, 6, -7, -29, -25, 11, 9, 29, -5, 14, -7, 10, 18, -26, + -23, -9, -2, -15, 6, -6, 7, -18, -11, 22, 12, -2, 6, 19, 9, 0, 10, 9, 2, 19, 2, + -30, -24, -13, -23, -22, 14, -21, 24, 23, -8, 6, -24, 19, 19, -7, -16, -23, -8, + 13, -2, -18, 16, 22, 29, 24, -24, -13, -6, -24, 11, 19, -1, -14, 13, 17, -4, + -11, 23, 28, 17, -26, -30, 5, -23, -3, -16, 17, -22, -8, -25, 4, -11, 21, 12, + -24, -15, 18, 6, 16, 8, 8, -27, -20, -22, 8, 10, 22, 3, 29, 11, -1, 8, -28, 5, + -16, 18, -5, -20, -3, -1, 22, 15, 19, 1, -12, 15, 11, 25, 4, 19, 2, -11, 0, -28, + -16, 19, 14, -14, -15, 2, -24, 8, -14, 14, -12, 28, -17, 0, 15, -1, 17, 20, -21, + 24, 19, -12, -18, -29, -14, 14, -25, 26, -26, -19, -11, 12, -27, -3, -6, -21, + -13, 17, 20, -20, -8, -14, -6, 20, -15, -13, -1, 2, -19, 28, 27, -20, -8, 16, + 29, 8, -9, -25, 5, 4, -1, 12, -1, -26, -23, 16, 12, 14, 11, -20, -26, -2, -8, + -15, -26, -27, -10, -17, 0, 19, -15, -1, 10, 25, -7, 18, 27, 24, 24, -12, 28, + -3, 26, 10, 10, 16, -10, -8, 27, -18, -18, 6, -27, -30, -16, -26, 9, -26, -1, + 12, 1, 24, -15, -6, 29, 7, 15, 23, -3, 29, -1, -2, 20, 0, -10, -17, 2, 20, -10, + -2, 11, -7, -13, 9, 23, 20, 23, 14, -26, -20, 12, -23, 13, 3, 5, -12, 23, -20, + -19, 21, 7, -7, 3, -12, 15, 26, 0, -6, -26, -3, -9, 14, 16, 27, -9, -24, -18, + 12, -20, 24, 13, 15, 28, -20, -25, -26, -3, 10, -19, -14, -1, -19, -24, -14, 16, + 7, -29, 22, 24, 13, 7, -13, -1, -1, 6, -11, 22, -18, -1, -10, -7, -4, 22, -6, + -24, -28, 4, 5, -27, -29, -27, 13, -25, -9, -2, -13, 29, -26, -25, 25, -11, 10, + 21, -2, 21, 25, -18, -13, 7, 16, 26, 23, -19, -29, -30, -22, -5, -4, 0, -2, -6, + 0, 13, 23, -30, 10, -5, 12, -28, -3, 4, -29, 27, 23, 28, 2, -16, -11, -17, -15, + 6, -13, -2, 6, 12, 0, -12, -21, -2, -19, -22, 6, 26, -25, 17, -19, 11, -12, -20, + -14, 28, -19, -12, 12, -5, 6, -9, -16, -14, -22, -30, -30, -20, 29, -8, -29, 14, + 20, -23, 8, 8, -8, 16, -18, 10, 7, -5, 2, -21, -12, -24, -25, -7, -18, 21, -1, + 1, 4, -24, 26, 2, 4, -27, -21, -3, -25, -12, 22, -29, 6, -29, -10, -14, -17, 24, + -24, 22, 17, 13, 28, -23, -11, 13, -11, -29, -22, -29, -15, -12, -6, -3, -18, + -28, 28, -18, 16, -24, 17, -26, -29, 15, -18, -4, 13, 19, 3, 11, 6, -29, -21, 6, + 20, -10, -15, -11, 18, -14, -25, 28, 5, 12, 18, 15, -25, -12, -22, 28, -8, 9, + -17, -26, 14, -19, -23, 15, -20, 12, 4, -19, 15, -21, 13, -2, -2, 29, 10, 29, + 16, -18, 5, 14, -24, 11, -12, 1, -27, 21, -20, 16, -8, 22, 13, 7, -24, -29, 18, + 29, 0, 4, 6, 21, 23, -13, 2, 26, 17, -12, -15, -19, 26, -9, -1, -12, 10, 22, 18, + -27, 17, -7, -16, 1, -12, 4, -10, -17, 15, 17, -7, 4, -7, -16, -7, -12, 11, 4, + -30, 29, -26, -13, 22, 21, -15, 5, -27, -18, 14, 19, 27, 5, -11, -1, -10, -9, + -25, 29, 16, 4, 29, -7, -21, -14, -2, -4, 12, 2, -3, -28, -26, -5, 29, 3, 17, + 24, -5, -12, -4, 29, -15, 10, -25, 18, -10, 15, -19, -30, -18, -27, -19, -26, 9, + 4, 0, -1, 4, -21, 13, 19, -17, 9, 29, -27, -19, -12, -30, 19, -2, -16, -1, 24, + -13, -26, -19, -12, -11, 13, -11, 3, 24, -17, 15, -25, -21, 14, -5, 29, -10, + -27, -2, 7, 28, -3, -8, -23, -9, 5, 13, -14, -20, 21, 26, 19, 28, -28, -17, -14, + 5, -23, 11, 25, 14, -20, 0, 17, -8, 11, -16, 22, -14, 11, -19, -23, 28, 6, 18, + -27, 22, -18, 2, 13, -5, -9, -22, 3, 3, 28, 15, 17, -22, -29, -30, 16, 4, 8, 1, + 6, 2, 11, 8, -27, -7, -27, 20, 19, 15, -26, -1, 5, -1, -4, 29, 28, 22, 19, 23, + 26, 24, -12, -6, -15, 0, -5, 23, 12, 2, 9, -29, 3, 27, -26, -10, 27, 10, -30, + -20, 3, 29, -25, 28, -1, 26, -13, -3, -2, 2, -5, 23, 14, 9, -21, 21, -22, 12, + 10, 17, 2, 19, -26, 12, -29, -19, 21, -2, 26, -1, 23, -6, 1, 19, -16, 24, 17, + -9, 22, -28, -8, 1, -20, 8, 19, 16, 19, -6, 28, -12, -6, -20, 13, 24, 13, 9, 29, + 6, -18, -3, 24, -19, -1, -20, -9, -5, 28, -17, 2, -27, -14, 28, 2, -4, -6, -26, + 27, 29, 0, 10, -18, -2, 16, 1, -19, -20, -30, 17, 8, -11, 2, -14, 13, 20, 7, 11, + -17, 11, -27, -10, 7, -8, -26, 7, 23, -16, -10, 0, 0, 22, 11, 9, -22, -3, 0, 15, + -17, 16, -22, -10, -27, -21, 10, 18, -9, 7, -26, -18, -6, -22, 18, 25, 27, -18, + -1, -21, -11, 16, -18, -14, 17, -3, 8, -26, -18, -23, 6, 11, -10, -24, -27, -21, + 28, 22, 20, 4, -28, -9, -7, 3, -24, -24, 10, 19, -30, 17, -4, 29, 26, -20, -13, + -15, -25, 14, -10, -21, 2, 18, -9, 7, 14, -5, 26, -17, 16, 8, -3, 24, -25, 11, + 19, 29, -27, -23, -17, -24, -28, -26, 22, 17, -5, -24, -14, 20, -27, -1, -3, -9, + -20, 12, -15, 2, -1, -17, 9, 16, -13, -17, 14, -29, -8, 4, -11, 24, -28, 9, 16, + -16, 9, -8, 28, 15, -4, -24, 29, 20, 11, 22, -26, 13, -14, 20, -19, -22, -30, + -15, 8, 4, -29, -11, 21, 28, -4, 22, 10, 7, -1, 21, 25, 0, 14, 24, 3, 3, 20, -7, + -22, 24, 17, -25, 13, 7, -11, -24, 16, -25, 26, -28, -20, -7, -3, 1, 4, 24, -2, + 8, -1, -25, 0, 28, 22, 4, 6, -26, -17, 12, -27, -11, -9, -12, 8, -4, 9, 23, 9, + -14, -12, 6, -5, 14, -5, -27, 1, -21, 1, -26, 6, -17, 4, -27, 3, -7, -9, -13, 2, + 25, -21, 8, 9, 2, 14, 5, 14, -26, 15, -26, -26, 13, 10, -14, -22, -25, 9, -15, + -22, 24, 10, -5, 13, -25, 20, 7, -14, 18, 20, 21, 29, 22, -1, -14, 25, -17, 29, + -30, 26, 28, -14, -19, -13, 13, -9, -13, 10, -7, -7, -15, -7, 14, 13, 10, -15, + 28, 19, -26, -13, -21, -3, -6, 15, -11, -8, 25, 8, 8, 10, 24, -3, -20, 23, -2, + -18, -18, -7, -14, 15, 10, -8, -19, 2, -9, 12, 15, 1, 9, -26, -30, -29, 2, 18, + 24, 8, -11, 22, 27, -16, 29, -24, -30, -3, 11, -7, 6, -11, -12, -14, 18, -15, + 15, 21, -15, -3, 6, 10, 7, -18, 26, -3, 19, 2, -28, -1, -4, -28, -28, -4, -6, + 11, -25, 22, -12, 25, -7, 13, 29, -27, -28, -4, 20, 5, 3, -23, 8, -2, -25, -22, + 1, -2, 1, 7, -3, 28, -6, 24, -20, -24, -9, 22, 19, 22, 24, -10, -5, -15, 29, 6, + 10, 1, 21, 13, 19, 14, -9, 28, -4, -4, 2, 14, -27, 27, 28, -8, -12, 24, 15, 9, + -9, -29, -4, -15, 26, -23, -26, -13, -2, -2, -12, -15, -25, 7, -21, -16, -25, + 12, -17, -23, -4, 4, 7, 1, -12, -12, 3, 6, 15, -16, 15, 18, 22, -14, 11, -7, 27, + -1, -4, 25, 26, -6, 29, -14, -13, -19, -22, -8, -7, -24, 14, 20, -5, 6, 10, -5, + -18, 2, 8, -10, -20, -18, -30, -29, 20, -9, 24, 29, 18, 6, -22, 27, 10, -1, -7, + 4, -3, -12, 18, 19, 12, -3, 9, 26, 22, 10, 8, -17, -27, -13, 25, -17, -23, -25, + -20, -24, 17, 4, 26, -6, -5, 28, -24, -5, -18, -27, 24, -29, 23, -5, -5, 0, 1, + -13, -21, -2, -14, 8, 8, 19, -16, -14, -10, 26, 20, 6, 17, -10, -2, 17, -26, -5, + 10, -18, -30, 12, -29, 23, 6, -12, 7, -28, 2, -14, 3, -22, 22, 5, 5, -21, -6, + 22, -24, 23, -23, -6, -17, -7, -14, -25, -10, 12, 8, 21, -14, 12, 9, 14, 25, 1, + 22, 7, 14, 13, -7, 7, 27, 11, -12, -1, -4, -4, -22, 20, -17, 11, 24, -12, -5, + -26, -30, 12, -26, -5, -25, -25, 13, -11, -1, 29, -30, 7, 29, -25, -12, 15, -30, + 6, 20, -11, 1, 7, -5, 11, -16, -3, 4, -19, -1, -21, -16, 6, -23, 29, -11, -27, + 16, -10, 9, -18, 14, 29, 28, 1, 18, -28, -15, 6, -4, 19, 6, 4, 26, -14, 2, -28, + -4, -27, 12, -23, -16, 17, 18, -28, 15, -21, -20, 26, 11, -14, -29, 7, 1, 22, + -13, 12, -10, 20, 5, -18, -1, 26, -14, -20, 27, -29, 13, -14, 3, -4, 22, -1, + -27, 26, -7, -18, -4, 0, 6, -9, 19, 22, 28, -12, 24, -28, 10, -25, 25, 28, 24, + 14, -25, -25, 11, 10, 16, 19, -29, -10, 7, -10, -29, -20, -18, -17, 1, -10, -8, + 6, -29, 27, 29, 2, -22, -7, -21, -17, 29, 23, 3, -14, -22, 11, -23, -6, -15, 9, + 12, -23, 10, 20, -30, -9, 18, -25, 20, 19, -3, -7, 24, 15, 16, 14, 16, 23, -30, + -3, -21, 7, -19, 6, 19, -30, 26, -15, 19, 22, 29, 2, -16, -3, 16, -12, 15, 7, + 11, 8, 15, -9, 11, -21, -18, -24, -16, -23, 26, 5, -28, 9, 10, 16, 21, 6, -7, + 11, 14, 8, -1, -11, -22, -9, -20, 1, 23, 22, 6, 26, -21, 16, -9, -5, -26, -28, + 0, -30, -25, -22, 0, -29, 4, 1, 18, -10, 17, -14, 11, 2, -28, -25, 12, -30, 17, + 14, -5, -13, 29, 29, 20, 16, -3, 18, -21, 6, 13, 19, -23, -4, -14, 11, -3, 25, + 14, -27, 6, 5, -18, 11, 27, 3, -2, -16, -10, -14, 9, 14, 18, -25, 19, -14, -29, + -2, -2, -14, -4, 21, 29, 9, 25, 25, -7, 12, 15, -4, -19, 5, 10, 6, 12, 12, 21, + -20, -22, -15, 17, -17, 3, -20, 4, -2, -6, -11, 5, 18, 29, 12, -26, -21, -17, 6, + 12, -29, -6, -17, 15, 12, -5, -28, 2, -25, -11, -11, -1, 24, 13, 3, 9, 26, -22, + -13, -30, -29, -4, -22, -16, 13, 11, -15, -12, 28, -23, 10, -13, 20, -24, -26, + 8, 0, 2, 3, 2, -27, -1, 26, -17, -27, 23, 5, -9, 11, 26, -1, -4, 19, 9, -16, 18, + -5, -10, -24, -9, 22, 28, 29, 4, -18, 24, 12, 20, -11, -24, 6, -9, 8, 10, 4, 2, + 3, -12, -23, -6, 10, 24, 4, -7, 26, -25, -8, -14, -7, 1, 25, 6, 22, -8, 14, -23, + -20, 24, 26, -8, 18, -9, 25, 7, 29, 17, -18, -21, 3, 24, -11, -11, -29, 11, -14, + -1, 11, 1, 27, 17, 5, 29, 17, 22, -9, -15, -10, 9, 21, -20, -29, -9, 20, -12, + -9, 24, 14, 4, 28, 27, 29, 27, 2, 26, 23, -20, -26, 27, -2, -1, 18, 25, -8, 0, + 27, -17, 2, 22, 17, -16, 19, 22, 27, 19, -14, -9, -3, 25, 4, 16, -18, -22, 28, + 1, 8, 29, -20, -14, -8, 20, -8, 12, -4, -16, -1, -26, 23, 29, 22, -11, 15, 26, + 1, 29, -18, -27, -3, -20, -13, -6, 0, 12, 21, -9, -24, 10, 18, 8, -1, -23, 18, + 8, 29, -30, -1, 24, -12, 26, -23, -13, 28, 5, -29, -28, -13, 6, -4, 16, 16, 11, + -19, 20, -4, 9, 4, -5, 14, 9, -2, -20, -28, -30, -22, 20, 12, 19, 8, -5, 29, + -16, -6, 25, 7, -27, 26, 4, 29, 7, 18, -11, -8, 28, -18, -15, 22, 2, -13, -11, + -14, 11, -14, -22, 28, 28, 11, 19, -26, -23, 11, -20, -12, -23, -15, 5, -3, -13, + -28, 12, -6, -12, 7, 12, -15, -1, -20, -21, 0, -17, -9, -5, -25, -23, 3, -11, + -11, 24, -11, -6, 26, 2, -20, 15, -1, 8, 26, 21, 26, 1, 1, 23, 28, -13, 4, 4, + -27, -8, 29, 24, -24, 13, 23, 3, 18, -15, -8, 17, 9, 25, 29, -7, -8, 7, 7, -15, + 4, -3, 13, -30, -9, 0, 5, 25, 2, 23, -28, -26, -1, -10, 6, -26, 24, 19, 27, 17, + 26, -4, -25, -2, -2, -5, 17, -17, -2, 16, 7, 16, 17, -7, -9, -29, -4, -24, 23, + 14, 28, 13, 23, 22, 17, 15, -9, -12, 6, 10, 3, -18, 28, -2, -18, 20, 22, 3, -16, + -11, -19, -3, -15, -10, 28, -4, 15, -27, -8, 29, 4, 2, -6, 15, 17, 27, -4, -7, + 15, -13, 9, -11, -20, 18, 3, -29, -22, -17, -1, -4, -29, 19, 4, -22, -7, -14, + -25, 24, -2, 28, 19, 7, 8, -6, -12, 29, 6, -26, 7, -19, -25, -19, -20, 28, 25, + -11, -4, -25, -21, 8, -9, 19, 20, 0, -15, -30, 10, 28, 17, -29, 6, 7, 2, -22, 3, + -8, -14, 6, 23, -1, -6, -29, -29, 28, 21, 16, -14, -22, 15, -26, 29, 17, -16, + -29, 18, 13, -12, 15, 18, 8, 22, -19, 17, -5, -27, 26, -12, -6, -15, 22, -25, + -3, -26, -1, -9, 18, -9, -28, 8, -6, -6, -16, 7, -16, 28, -7, 1, -22, -18, 0, + -1, 18, -15, 5, 14, -16, 19, -18, 9, 27, -17, 2, 19, 9, -21, -3, -13, -23, 18, + 9, -2, -4, 16, -16, 18, -24, 1, 11, 29, 2, -18, 16, -30, -10, -16, -13, -14, 1, + 17, 16, -12, -21, -3, -27, -19, 23, 24, 25, 22, 12, -11, -2, 0, 10, -9, -16, 26, + 13, 9, -28, 19, 14, -3, -4, -21, 1, -22, 8, 4, 19, 10, 22, 6, 6, -1, -29, -13, + -14, 18, -17, -19, 3, -26, 7, -22, -27, 3, 10, -1, 16, -25, -3, 6, -26, 11, -7, + -15, -14, -25, 15, 20, -18, -17, 10, -2, -27, -7, -7, 9, 7, -8, 27, -8, -9, -20, + -11, 25, -8, 5, 3, 12, 19, -9, 28, 19, -27, 27, 29, 14, -3, -21, 13, -26, -19, + -29, 18, -7, 2, -29, -2, -7, 22, 7, 25, -25, -26, -13, -16, 20, 9, -7, 12, -30, + -8, 28, 13, -8, 16, -3, -15, -24, -22, -11, -3, -1, 9, 11, 27, 24, -13, -26, -1, + -23, 6, 10, 24, 5, -10, -21, 22, -1, 1, 14, -13, -26, 2, 20, 11 + }; + assertEquals(48645, solution1.countRangeSum(nums, 1, 4)); + assertEquals(48645, solution2.countRangeSum(nums, 1, 4)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_328Test.java b/src/test/java/com/fishercoder/firstthousand/_328Test.java new file mode 100644 index 0000000000..be2a3cc40a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_328Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.solutions.firstthousand._328; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _328Test { + private _328.Solution1 solution1; + private static ListNode expected; + private static ListNode node; + + @BeforeEach + public void setUp() { + solution1 = new _328.Solution1(); + } + + @Test + public void test1() { + node = new ListNode(1); + node.next = new ListNode(2); + node.next.next = new ListNode(3); + node.next.next.next = new ListNode(4); + node.next.next.next.next = new ListNode(5); + + expected = new ListNode(1); + expected.next = new ListNode(3); + expected.next.next = new ListNode(5); + expected.next.next.next = new ListNode(2); + expected.next.next.next.next = new ListNode(4); + assertEquals(expected, solution1.oddEvenList(node)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_32Test.java b/src/test/java/com/fishercoder/firstthousand/_32Test.java new file mode 100644 index 0000000000..3cbee264c9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_32Test.java @@ -0,0 +1,66 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._32; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _32Test { + private _32.Solution1 solution1; + private _32.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _32.Solution1(); + solution2 = new _32.Solution2(); + } + + @Test + public void test1() { + assertEquals(2, solution1.longestValidParentheses("(()")); + assertEquals(2, solution2.longestValidParentheses("(()")); + } + + @Test + public void test2() { + assertEquals(2, solution1.longestValidParentheses("()(()")); + assertEquals(2, solution2.longestValidParentheses("()(()")); + } + + @Test + public void test3() { + assertEquals(4, solution1.longestValidParentheses("(())(")); + assertEquals(4, solution2.longestValidParentheses("(())(")); + } + + @Test + public void test4() { + assertEquals(6, solution1.longestValidParentheses("()(())")); + assertEquals(6, solution2.longestValidParentheses("()(())")); + } + + @Test + public void test5() { + assertEquals(4, solution1.longestValidParentheses(")()())")); + assertEquals(4, solution2.longestValidParentheses(")()())")); + } + + @Test + public void test6() { + assertEquals(4, solution1.longestValidParentheses(")()())()()(")); + assertEquals(4, solution2.longestValidParentheses(")()())()()(")); + } + + @Test + public void test7() { + assertEquals(8, solution1.longestValidParentheses("((())())")); + assertEquals(8, solution2.longestValidParentheses("((())())")); + } + + @Test + public void test8() { + assertEquals(10, solution1.longestValidParentheses(")()(((())))(")); + assertEquals(10, solution2.longestValidParentheses(")()(((())))(")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_330Test.java b/src/test/java/com/fishercoder/firstthousand/_330Test.java new file mode 100644 index 0000000000..69f754c46b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_330Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._330; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _330Test { + private _330.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setUp() { + solution1 = new _330.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 4, 13, 43}; + List expected = new ArrayList(Arrays.asList(8, 29)); + assertEquals(expected, solution1.findPatches(nums, 100)); + assertEquals(2, solution1.minPatches(nums, 100)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_331Test.java b/src/test/java/com/fishercoder/firstthousand/_331Test.java new file mode 100644 index 0000000000..08444a13c9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_331Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._331; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _331Test { + private _331.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _331.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#")); + } + + @Test + public void test2() { + assertEquals(false, solution1.isValidSerialization("1,#")); + } + + @Test + public void test3() { + assertEquals(false, solution1.isValidSerialization("9,#,#,1")); + } + + @Test + public void test4() { + assertEquals(false, solution1.isValidSerialization("1")); + } + + @Test + public void test5() { + assertEquals(false, solution1.isValidSerialization("#,7,6,9,#,#,#")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_332Test.java b/src/test/java/com/fishercoder/firstthousand/_332Test.java new file mode 100644 index 0000000000..c2a61424ac --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_332Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._332; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _332Test { + private _332.Solution1 solution1; + private static List> tickets; + private static List expected; + + @BeforeEach + public void setup() { + solution1 = new _332.Solution1(); + } + + @Test + public void test1() { + tickets = new ArrayList<>(); + tickets.add(Arrays.asList("MUC", "LHR")); + tickets.add(Arrays.asList("JFK", "MUC")); + tickets.add(Arrays.asList("SFO", "SJC")); + tickets.add(Arrays.asList("LHR", "SFO")); + expected = solution1.findItinerary(tickets); + CommonUtils.print(expected); + } + + @Test + public void test2() { + tickets = new ArrayList<>(); + tickets.add(Arrays.asList("JFK", "SFO")); + tickets.add(Arrays.asList("JFK", "ATL")); + tickets.add(Arrays.asList("SFO", "ATL")); + tickets.add(Arrays.asList("ATL", "JFK")); + tickets.add(Arrays.asList("ATL", "SFO")); + expected = solution1.findItinerary(tickets); + CommonUtils.print(expected); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_334Test.java b/src/test/java/com/fishercoder/firstthousand/_334Test.java new file mode 100644 index 0000000000..62059764fc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_334Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.firstthousand._334; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _334Test { + private _334.Solution1 solution1; + private _334.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _334.Solution1(); + solution2 = new _334.Solution2(); + } + + @Test + public void test1() { + assertTrue(solution1.increasingTriplet(new int[] {2, 1, 5, 0, 4, 6})); + assertTrue(solution2.increasingTriplet(new int[] {2, 1, 5, 0, 4, 6})); + } + + @Test + public void test2() { + assertFalse(solution1.increasingTriplet(new int[] {5, 4, 3, 2, 1})); + assertFalse(solution2.increasingTriplet(new int[] {5, 4, 3, 2, 1})); + } + + @Test + public void test3() { + assertFalse(solution1.increasingTriplet(new int[] {1, 1, -2, 6})); + assertFalse(solution2.increasingTriplet(new int[] {1, 1, -2, 6})); + } + + @Test + public void test4() { + assertFalse(solution1.increasingTriplet(new int[] {1, 1, 1, 1, 1, 1, 1})); + assertFalse(solution2.increasingTriplet(new int[] {1, 1, 1, 1, 1, 1, 1})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_337Test.java b/src/test/java/com/fishercoder/firstthousand/_337Test.java new file mode 100644 index 0000000000..cb76b27abe --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_337Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._337; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _337Test { + private _337.Solution1 solution1; + private _337.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _337.Solution1(); + solution2 = new _337.Solution2(); + } + + @Test + public void test1() { + assertEquals( + 7, + solution1.rob( + TreeUtils.constructBinaryTree(Arrays.asList(3, 2, 3, null, 3, null, 1)))); + assertEquals( + 7, + solution2.rob( + TreeUtils.constructBinaryTree(Arrays.asList(3, 2, 3, null, 3, null, 1)))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_338Test.java b/src/test/java/com/fishercoder/firstthousand/_338Test.java new file mode 100644 index 0000000000..1cd3007985 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_338Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._338; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _338Test { + private _338.Solution1 test; + private static int[] expected; + private static int[] actual; + + @BeforeEach + public void setUp() { + test = new _338.Solution1(); + } + + @Test + public void test1() { + expected = new int[] {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; + actual = test.countBits(15); + CommonUtils.printArray(actual); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_33Test.java b/src/test/java/com/fishercoder/firstthousand/_33Test.java new file mode 100644 index 0000000000..17d60a61ac --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_33Test.java @@ -0,0 +1,123 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._33; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _33Test { + private _33.Solution1 solution1; + private static int[] nums; + private static int expected; + private static int target; + + @BeforeEach + public void setup() { + solution1 = new _33.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {4, 5, 6, 7, 0, 1, 2}; + expected = 3; + target = 7; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test2() { + nums = new int[] {4, 5, 6, 7, 0, 1, 2}; + expected = 4; + target = 0; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test3() { + nums = new int[] {4, 5, 6, 7, 0, 1, 2}; + expected = 1; + target = 5; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test4() { + nums = new int[] {4, 5, 6, 7, 0, 1, 2}; + expected = -1; + target = 3; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test5() { + nums = new int[] {1}; + expected = -1; + target = 0; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test6() { + nums = new int[] {1, 3, 5}; + expected = -1; + target = 4; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test7() { + nums = new int[] {1, 3, 5}; + expected = -1; + target = 6; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test8() { + nums = new int[] {1, 3, 5}; + expected = -1; + target = 2; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test9() { + nums = new int[] {5, 1, 3}; + expected = -1; + target = 4; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test10() { + nums = new int[] {1, 2, 3, 4, 5, 6}; + expected = 3; + target = 4; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test11() { + nums = new int[] {5, 1, 2, 3, 4}; + expected = 1; + target = 1; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test12() { + nums = new int[] {8, 9, 2, 3, 4}; + expected = 1; + target = 9; + assertEquals(expected, solution1.search(nums, target)); + } + + @Test + public void test13() { + nums = new int[] {4, 5, 6, 7, 8, 1, 2, 3}; + expected = 4; + target = 8; + assertEquals(expected, solution1.search(nums, target)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_340Test.java b/src/test/java/com/fishercoder/firstthousand/_340Test.java new file mode 100644 index 0000000000..241ffee76a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_340Test.java @@ -0,0 +1,64 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._340; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _340Test { + private _340.Solution1 solution1; + private _340.Solution2 solution2; + private _340.Solution3 solution3; + private static String s; + private static int k; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _340.Solution1(); + solution2 = new _340.Solution2(); + solution3 = new _340.Solution3(); + } + + @Test + public void test1() { + s = "eceba"; + k = 2; + expected = 3; + assertEquals(expected, solution1.lengthOfLongestSubstringKDistinct(s, k)); + assertEquals(expected, solution2.lengthOfLongestSubstringKDistinct(s, k)); + assertEquals(expected, solution3.lengthOfLongestSubstringKDistinct(s, k)); + } + + @Test + public void test2() { + assertEquals(0, solution1.lengthOfLongestSubstringKDistinct("", 0)); + assertEquals(0, solution2.lengthOfLongestSubstringKDistinct("", 0)); + } + + @Test + public void test3() { + assertEquals(0, solution1.lengthOfLongestSubstringKDistinct("a", 0)); + } + + @Test + public void test4() { + assertEquals(1, solution1.lengthOfLongestSubstringKDistinct("a", 1)); + } + + @Test + public void test5() { + assertEquals(1, solution1.lengthOfLongestSubstringKDistinct("a", 2)); + } + + @Test + public void test6() { + assertEquals(2, solution1.lengthOfLongestSubstringKDistinct("aa", 1)); + } + + @Test + public void test7() { + assertEquals(3, solution1.lengthOfLongestSubstringKDistinct("bacc", 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_341Test.java b/src/test/java/com/fishercoder/firstthousand/_341Test.java new file mode 100644 index 0000000000..bbc38a078c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_341Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.common.classes.NestedInteger; +import com.fishercoder.solutions.firstthousand._341; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _341Test { + private _341.Solution1 test; + private static List nestedList; + + @BeforeEach + public void setUp() { + test = new _341.Solution1(); + } + + @Test + public void test1() { + NestedInteger six = new NestedInteger(6); + List sixList = new ArrayList<>(); + sixList.add(six); + NestedInteger four = new NestedInteger(4); + List fourList = new ArrayList<>(); + fourList.add(four); + fourList.addAll(sixList); + NestedInteger one = new NestedInteger(1); + List oneList = new ArrayList<>(); + oneList.add(one); + oneList.addAll(fourList); + _341.Solution1.NestedIterator nestedIterator = new _341.Solution1.NestedIterator(oneList); + assertTrue(nestedIterator.hasNext()); + assertEquals(1, (int) nestedIterator.next()); + } + + @Test + public void test2() { + List bigList = new ArrayList<>(); + + NestedInteger one = new NestedInteger(1); + NestedInteger two = new NestedInteger(2); + List oneList = new ArrayList<>(); + oneList.add(one); + oneList.add(two); + NestedInteger oneNestedInteger = new NestedInteger(oneList); + bigList.add(oneNestedInteger); + + NestedInteger three = new NestedInteger(3); + bigList.add(three); + + NestedInteger four = new NestedInteger(4); + NestedInteger five = new NestedInteger(5); + List threeList = new ArrayList<>(); + threeList.add(four); + threeList.add(five); + NestedInteger threeNestedInteger = new NestedInteger(threeList); + bigList.add(threeNestedInteger); + + _341.Solution1.NestedIterator nestedIterator = new _341.Solution1.NestedIterator(bigList); + assertTrue(nestedIterator.hasNext()); + assertEquals(1, (int) nestedIterator.next()); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_347Test.java b/src/test/java/com/fishercoder/firstthousand/_347Test.java new file mode 100644 index 0000000000..be6dfe9a4b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_347Test.java @@ -0,0 +1,54 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._347; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _347Test { + private _347.Solution1 solution1; + private _347.Solution2 solution2; + private static int[] nums; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _347.Solution1(); + solution2 = new _347.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {3, 0, 1, 0}; + expected = new int[] {0, 3}; + /** + * Comment out until Leetcode addresses this test case: + * https://discuss.leetcode.com/topic/44237/java-o-n-solution-bucket-sort/75 Then I'll + * update this Solution1 code accordingly. + * + *

My post is still un-addressed. - 3/12/2018 + */ + // assertArrayEquals(expected, solution1.topKFrequent(nums, 2)); + } + + @Test + public void test2() { + nums = new int[] {3, 0, 1, 0}; + expected = new int[] {0, 3}; + assertArrayEquals(expected, solution2.topKFrequent(nums, 2)); + } + + @Test + public void test3() { + nums = new int[] {3, 0, 1, 0}; + expected = new int[] {0, 3}; + } + + @Test + public void test4() { + nums = new int[] {1, 1, 1, 2, 2, 3}; + expected = new int[] {1, 2}; + assertArrayEquals(expected, solution1.topKFrequent(nums, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_348Test.java b/src/test/java/com/fishercoder/firstthousand/_348Test.java new file mode 100644 index 0000000000..98ccce9510 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_348Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._348; +import org.junit.jupiter.api.Test; + +public class _348Test { + @Test + public void test1() { + int n = 3; + _348.Solution1.TicTacToe ticTacToe = new _348.Solution1.TicTacToe(n); + assertEquals(0, ticTacToe.move(0, 0, 1)); + assertEquals(0, ticTacToe.move(0, 2, 2)); + assertEquals(0, ticTacToe.move(2, 2, 1)); + assertEquals(0, ticTacToe.move(1, 1, 2)); + assertEquals(0, ticTacToe.move(2, 0, 1)); + assertEquals(0, ticTacToe.move(1, 0, 2)); + assertEquals(1, ticTacToe.move(2, 1, 1)); + } + + @Test + public void test2() { + int n = 3; + _348.Solution1.TicTacToe ticTacToe = new _348.Solution1.TicTacToe(n); + assertEquals(0, ticTacToe.move(0, 0, 1)); + assertEquals(0, ticTacToe.move(1, 1, 1)); + assertEquals(1, ticTacToe.move(2, 2, 1)); + } + + @Test + public void test3() { + int n = 3; + _348.Solution1.TicTacToe ticTacToe = new _348.Solution1.TicTacToe(n); + assertEquals(0, ticTacToe.move(0, 2, 2)); + assertEquals(0, ticTacToe.move(1, 1, 2)); + assertEquals(2, ticTacToe.move(2, 0, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_349Test.java b/src/test/java/com/fishercoder/firstthousand/_349Test.java new file mode 100644 index 0000000000..8aaf76ee45 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_349Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._349; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _349Test { + private _349.Solution1 solution1; + private _349.Solution2 solution2; + private _349.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _349.Solution1(); + solution2 = new _349.Solution2(); + solution3 = new _349.Solution3(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {2}, solution1.intersection(new int[] {1, 2, 2, 1}, new int[] {2, 2})); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {2}, solution2.intersection(new int[] {1, 2, 2, 1}, new int[] {2, 2})); + } + + @Test + public void test3() { + assertArrayEquals( + new int[] {2}, solution3.intersection(new int[] {1, 2, 2, 1}, new int[] {2, 2})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_34Test.java b/src/test/java/com/fishercoder/firstthousand/_34Test.java new file mode 100644 index 0000000000..6178511220 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_34Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._34; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _34Test { + private _34.Solution1 solution1; + private _34.Solution2 solution2; + private _34.Solution3 solution3; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _34.Solution1(); + solution2 = new _34.Solution2(); + solution3 = new _34.Solution3(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3}; + assertArrayEquals(new int[] {1, 1}, solution1.searchRange(nums, 2)); + assertArrayEquals(new int[] {1, 1}, solution2.searchRange(nums, 2)); + assertArrayEquals(new int[] {1, 1}, solution3.searchRange(nums, 2)); + } + + @Test + public void test2() { + nums = new int[] {}; + assertArrayEquals(new int[] {-1, -1}, solution1.searchRange(nums, 0)); + assertArrayEquals(new int[] {-1, -1}, solution2.searchRange(nums, 0)); + assertArrayEquals(new int[] {-1, -1}, solution3.searchRange(nums, 0)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_350Test.java b/src/test/java/com/fishercoder/firstthousand/_350Test.java new file mode 100644 index 0000000000..eb80d41eb7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_350Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._350; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _350Test { + private _350.Solution1 solution1; + private _350.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _350.Solution1(); + solution2 = new _350.Solution2(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {2, 2}, solution1.intersect(new int[] {1, 2, 2, 1}, new int[] {2, 2})); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {2, 2}, solution2.intersect(new int[] {1, 2, 2, 1}, new int[] {2, 2})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_351Test.java b/src/test/java/com/fishercoder/firstthousand/_351Test.java new file mode 100644 index 0000000000..60368d0cff --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_351Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._351; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _351Test { + private _351.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _351.Solution1(); + } + + @Test + public void test1() { + assertEquals(9, solution1.numberOfPatterns(1, 1)); + } + + @Test + public void test2() { + assertEquals(65, solution1.numberOfPatterns(1, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_352Test.java b/src/test/java/com/fishercoder/firstthousand/_352Test.java new file mode 100644 index 0000000000..ee23e2b1c1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_352Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.Interval; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._352; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _352Test { + private _352.Solution1.SummaryRanges test; + private static List actual; + + @BeforeEach + public void setUp() { + test = new _352.Solution1.SummaryRanges(); + } + + @Test + public void test1() { + test.addNum(1); + actual = test.getIntervals(); + CommonUtils.printIntervals(actual); + + test.addNum(3); + actual = test.getIntervals(); + CommonUtils.printIntervals(actual); + + test.addNum(7); + actual = test.getIntervals(); + CommonUtils.printIntervals(actual); + + test.addNum(2); + actual = test.getIntervals(); + CommonUtils.printIntervals(actual); + + test.addNum(6); + actual = test.getIntervals(); + CommonUtils.printIntervals(actual); + } +} diff --git a/src/test/java/com/fishercoder/_355Test.java b/src/test/java/com/fishercoder/firstthousand/_355Test.java similarity index 88% rename from src/test/java/com/fishercoder/_355Test.java rename to src/test/java/com/fishercoder/firstthousand/_355Test.java index 02d17ba760..292e69255b 100644 --- a/src/test/java/com/fishercoder/_355Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_355Test.java @@ -1,28 +1,24 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._355; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import com.fishercoder.solutions.firstthousand._355; import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; - -/** - * Created by fishercoder on 5/10/17. - */ +/** Created by fishercoder on 5/10/17. */ public class _355Test { - private static _355.Solution1.Twitter solution1Twitter; - private static _355.Solution2.Twitter solution2Twitter; + private _355.Solution1.Twitter solution1Twitter; + private _355.Solution2.Twitter solution2Twitter; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1Twitter = new _355.Solution1.Twitter(); solution2Twitter = new _355.Solution2.Twitter(); } - @Before + @BeforeEach public void cleanUp() { solution1Twitter = new _355.Solution1.Twitter(); solution2Twitter = new _355.Solution2.Twitter(); diff --git a/src/test/java/com/fishercoder/firstthousand/_356Test.java b/src/test/java/com/fishercoder/firstthousand/_356Test.java new file mode 100644 index 0000000000..af1285df33 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_356Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._356; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _356Test { + private _356.Solution1 solution1; + private static int[][] points; + + @BeforeEach + public void setUp() { + solution1 = new _356.Solution1(); + } + + @Test + public void test1() { + points = + new int[][] { + {1, 1}, + {-1, 1}, + }; + assertEquals(true, solution1.isReflected(points)); + } + + @Test + public void test2() { + points = + new int[][] { + {1, 1}, + {-1, -1}, + }; + assertEquals(false, solution1.isReflected(points)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_358Test.java b/src/test/java/com/fishercoder/firstthousand/_358Test.java new file mode 100644 index 0000000000..8d3725ab92 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_358Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._358; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _358Test { + + private _358.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _358.Solution1(); + } + + @Test + public void test1() { + System.out.println(solution1.rearrangeString("aabbcc", 3)); + } + + @Test + public void test2() { + System.out.println(solution1.rearrangeString("aaabc", 3)); + } + + @Test + public void test3() { + System.out.println(solution1.rearrangeString("aaadbbcc", 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_35Test.java b/src/test/java/com/fishercoder/firstthousand/_35Test.java new file mode 100644 index 0000000000..141636e168 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_35Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._35; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _35Test { + private _35.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _35.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 3, 5, 6}; + assertEquals(2, solution1.searchInsert(nums, 5)); + } + + @Test + public void test2() { + nums = new int[] {1}; + assertEquals(0, solution1.searchInsert(nums, 1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_362Test.java b/src/test/java/com/fishercoder/firstthousand/_362Test.java new file mode 100644 index 0000000000..ed1a2d849f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_362Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._362; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _362Test { + private _362.Solution1.HitCounter hitCounter; + + @BeforeEach + public void setup() { + hitCounter = new _362.Solution1.HitCounter(); + } + + @Test + public void test1() { + hitCounter.hit(1); + hitCounter.hit(2); + hitCounter.hit(3); + assertEquals(3, hitCounter.getHits(4)); + + hitCounter.hit(300); + assertEquals(4, hitCounter.getHits(300)); + assertEquals(3, hitCounter.getHits(301)); + + hitCounter.hit(301); + assertEquals(4, hitCounter.getHits(300)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_368Test.java b/src/test/java/com/fishercoder/firstthousand/_368Test.java new file mode 100644 index 0000000000..79aba9d320 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_368Test.java @@ -0,0 +1,59 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._368; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _368Test { + private _368.Solution1 solution1; + private _368.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _368.Solution1(); + solution2 = new _368.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 4, 8}; + assertEquals(Arrays.asList(1, 2, 4, 8), solution1.largestDivisibleSubset(nums)); + assertEquals(Arrays.asList(8, 4, 2, 1), solution2.largestDivisibleSubset(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 3}; + assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList(1, 2)); + assertEquals(solution2.largestDivisibleSubset(nums), Arrays.asList(2, 1)); + } + + @Test + public void test3() { + nums = new int[] {1}; + assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList(1)); + } + + @Test + public void test4() { + nums = new int[] {546, 669}; + assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList(546)); + } + + @Test + public void test5() { + nums = new int[] {}; + assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList()); + } + + @Test + public void test6() { + nums = new int[] {4, 8, 10, 240}; + assertEquals(solution1.largestDivisibleSubset(nums), Arrays.asList(4, 8, 240)); + assertEquals(solution2.largestDivisibleSubset(nums), Arrays.asList(240, 8, 4)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_369Test.java b/src/test/java/com/fishercoder/firstthousand/_369Test.java new file mode 100644 index 0000000000..b79ae7faaa --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_369Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._369; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _369Test { + + private _369.Solution2 solution2; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution2 = new _369.Solution2(); + } + + @Test + public void test1() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 9}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 3, 0}); + assertEquals(expected, solution2.plusOne(head)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_36Test.java b/src/test/java/com/fishercoder/firstthousand/_36Test.java new file mode 100644 index 0000000000..22f82442f1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_36Test.java @@ -0,0 +1,86 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._36; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _36Test { + private _36.Solution1 solution1; + private static char[][] board; + + @BeforeEach + public void setup() { + solution1 = new _36.Solution1(); + } + + @Test + public void test1() { + board = + new char[][] { + {'4', '3', '5', '2', '6', '9', '7', '8', '1'}, + {'6', '8', '2', '5', '7', '1', '4', '9', '3'}, + {'1', '9', '7', '8', '3', '4', '5', '6', '2'}, + {'8', '2', '6', '1', '9', '5', '3', '4', '7'}, + {'3', '7', '4', '6', '8', '2', '9', '1', '5'}, + {'9', '5', '1', '7', '4', '3', '6', '2', '8'}, + {'5', '1', '9', '3', '2', '6', '8', '7', '4'}, + {'2', '4', '8', '9', '5', '7', '1', '3', '6'}, + {'7', '6', '3', '4', '1', '8', '2', '5', '9'}, + }; + assertEquals(true, solution1.isValidSudoku(board)); + } + + @Test + public void test2() { + board = + new char[][] { + {'.', '8', '7', '6', '5', '4', '3', '2', '1'}, + {'2', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'3', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'4', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'5', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'6', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'7', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'8', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'9', '.', '.', '.', '.', '.', '.', '.', '.'}, + }; + assertEquals(true, solution1.isValidSudoku(board)); + } + + @Test + public void test3() { + board = + new char[][] { + {'.', '.', '.', '.', '5', '.', '.', '1', '.'}, + // this upper right corner 3*3 square is invalid, '1' appears twice + {'.', '4', '.', '3', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '3', '.', '.', '1'}, + {'8', '.', '.', '.', '.', '.', '.', '2', '.'}, + {'.', '.', '2', '.', '7', '.', '.', '.', '.'}, + {'.', '1', '5', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '2', '.', '.', '.'}, + {'.', '2', '.', '9', '.', '.', '.', '.', '.'}, + {'.', '.', '4', '.', '.', '.', '.', '.', '.'}, + }; + assertEquals(false, solution1.isValidSudoku(board)); + } + + @Test + public void test4() { + board = + new char[][] { + {'.', '.', '4', '.', '.', '.', '6', '3', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'5', '.', '.', '.', '.', '.', '.', '9', '.'}, + {'.', '.', '.', '5', '6', '.', '.', '.', '.'}, + {'4', '.', '3', '.', '.', '.', '.', '.', '1'}, + {'.', '.', '.', '7', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '5', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.', '.'} + }; + assertEquals(false, solution1.isValidSudoku(board)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_370Test.java b/src/test/java/com/fishercoder/firstthousand/_370Test.java new file mode 100644 index 0000000000..5b8b999f53 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_370Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._370; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _370Test { + private _370.Solution1 solution1; + private static int[][] updates; + private static int length; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _370.Solution1(); + } + + @Test + public void test1() { + updates = + new int[][] { + {1, 3, 2}, + {2, 4, 3}, + {0, 2, -2}, + }; + length = 5; + expected = new int[] {-2, 0, 3, 5, 3}; + assertArrayEquals(expected, solution1.getModifiedArray(length, updates)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_372Test.java b/src/test/java/com/fishercoder/firstthousand/_372Test.java new file mode 100644 index 0000000000..c5d0ec0be9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_372Test.java @@ -0,0 +1,78 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._372; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _372Test { + private _372.Solution1 solution1; + private static int expected; + private static int actual; + private static int a; + private static int[] b; + + @BeforeEach + public void setUp() { + solution1 = new _372.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = 0; + actual = 0; + a = 0; + b = new int[10000]; + } + + @Test + public void test1() { + + a = 78267; + b = + new int[] { + 1, 7, 7, 4, 3, 1, 7, 0, 1, 4, 4, 9, 2, 8, 5, 0, 0, 9, 3, 1, 2, 5, 9, 6, 0, 9, 9, + 0, 9, 6, 0, 5, 3, 7, 9, 8, 8, 9, 8, 2, 5, 4, 1, 9, 3, 8, 0, 5, 9, 5, 6, 1, 1, 8, + 9, 3, 7, 8, 5, 8, 5, 5, 3, 0, 4, 3, 1, 5, 4, 1, 7, 9, 6, 8, 8, 9, 8, 0, 6, 7, 8, + 3, 1, 1, 1, 0, 6, 8, 1, 1, 6, 6, 9, 1, 8, 5, 6, 9, 0, 0, 1, 7, 1, 7, 7, 2, 8, 5, + 4, 4, 5, 2, 9, 6, 5, 0, 8, 1, 0, 9, 5, 8, 7, 6, 0, 6, 1, 8, 7, 2, 9, 8, 1, 0, 7, + 9, 4, 7, 6, 9, 2, 3, 1, 3, 9, 9, 6, 8, 0, 8, 9, 7, 7, 7, 3, 9, 5, 5, 7, 4, 9, 8, + 3, 0, 1, 2, 1, 5, 0, 8, 4, 4, 3, 8, 9, 3, 7, 5, 3, 9, 4, 4, 9, 3, 3, 2, 4, 8, 9, + 3, 3, 8, 2, 8, 1, 3, 2, 2, 8, 4, 2, 5, 0, 6, 3, 0, 9, 0, 5, 4, 1, 1, 8, 0, 4, 2, + 5, 8, 2, 4, 2, 7, 5, 4, 7, 6, 9, 0, 8, 9, 6, 1, 4, 7, 7, 9, 7, 8, 1, 4, 4, 3, 6, + 4, 5, 2, 6, 0, 1, 1, 5, 3, 8, 0, 9, 8, 8, 0, 0, 6, 1, 6, 9, 6, 5, 8, 7, 4, 8, 9, + 9, 2, 4, 7, 7, 9, 9, 5, 2, 2, 6, 9, 7, 7, 9, 8, 5, 9, 8, 5, 5, 0, 3, 5, 8, 9, 5, + 7, 3, 4, 6, 4, 6, 2, 3, 5, 2, 3, 1, 4, 5, 9, 3, 3, 6, 4, 1, 3, 3, 2, 0, 0, 4, 4, + 7, 2, 3, 3, 9, 8, 7, 8, 5, 5, 0, 8, 3, 4, 1, 4, 0, 9, 5, 5, 4, 4, 9, 7, 7, 4, 1, + 8, 7, 5, 2, 4, 9, 7, 9, 1, 7, 8, 9, 2, 4, 1, 1, 7, 6, 4, 3, 6, 5, 0, 2, 1, 4, 3, + 9, 2, 0, 0, 2, 9, 8, 4, 5, 7, 3, 5, 8, 2, 3, 9, 5, 9, 1, 8, 8, 9, 2, 3, 7, 0, 4, + 1, 1, 8, 7, 0, 2, 7, 3, 4, 6, 1, 0, 3, 8, 5, 8, 9, 8, 4, 8, 3, 5, 1, 1, 4, 2, 5, + 9, 0, 5, 3, 1, 7, 4, 8, 9, 6, 7, 2, 3, 5, 5, 3, 9, 6, 9, 9, 5, 7, 3, 5, 2, 9, 9, + 5, 5, 1, 0, 6, 3, 8, 0, 5, 5, 6, 5, 6, 4, 5, 1, 7, 0, 6, 3, 9, 4, 4, 9, 1, 3, 4, + 7, 7, 5, 8, 2, 0, 9, 2, 7, 3, 0, 9, 0, 7, 7, 7, 4, 1, 2, 5, 1, 3, 3, 6, 4, 8, 2, + 5, 9, 5, 0, 8, 2, 5, 6, 4, 8, 8, 8, 7, 3, 1, 8, 5, 0, 5, 2, 4, 8, 5, 1, 1, 0, 7, + 9, 6, 5, 1, 2, 6, 6, 4, 7, 0, 9, 5, 6, 9, 3, 7, 8, 8, 8, 6, 5, 8, 3, 8, 5, 4, 5, + 8, 5, 7, 5, 7, 3, 2, 8, 7, 1, 7, 1, 8, 7, 3, 3, 6, 2, 9, 3, 3, 9, 3, 1, 5, 1, 5, + 5, 8, 1, 2, 7, 8, 9, 2, 5, 4, 5, 4, 2, 6, 1, 3, 6, 0, 6, 9, 6, 1, 0, 1, 4, 0, 4, + 5, 5, 8, 2, 2, 6, 3, 4, 3, 4, 3, 8, 9, 7, 5, 5, 9, 1, 8, 5, 9, 9, 1, 8, 7, 2, 1, + 1, 8, 1, 5, 6, 8, 5, 8, 0, 2, 4, 4, 7, 8, 9, 5, 9, 8, 0, 5, 0, 3, 5, 5, 2, 6, 8, + 3, 4, 1, 4, 7, 1, 7, 2, 7, 5, 8, 8, 7, 2, 2, 3, 9, 2, 2, 7, 3, 2, 9, 0, 2, 3, 6, + 9, 7, 2, 8, 0, 8, 1, 6, 5, 2, 3, 0, 2, 0, 0, 0, 9, 2, 2, 2, 3, 6, 6, 0, 9, 1, 0, + 0, 3, 5, 8, 3, 2, 0, 3, 5, 1, 4, 1, 6, 8, 7, 6, 0, 9, 8, 0, 1, 0, 4, 5, 6, 0, 2, + 8, 2, 5, 0, 2, 8, 5, 2, 3, 0, 2, 6, 7, 3, 0, 0, 2, 1, 9, 0, 1, 9, 9, 2, 0, 1, 6, + 7, 7, 9, 9, 6, 1, 4, 8, 5, 5, 6, 7, 0, 6, 1, 7, 3, 5, 9, 3, 9, 0, 5, 9, 2, 4, 8, + 6, 6, 2, 2, 3, 9, 3, 5, 7, 4, 1, 6, 9, 8, 2, 6, 9, 0, 0, 8, 5, 7, 7, 0, 6, 0, 5, + 7, 4, 9, 6, 0, 7, 8, 4, 3, 9, 8, 8, 7, 4, 1, 5, 6, 0, 9, 4, 1, 9, 4, 9, 4, 1, 8, + 6, 7, 8, 2, 5, 2, 3, 3, 4, 3, 3, 1, 6, 4, 1, 6, 1, 5, 7, 8, 1, 9, 7, 6, 0, 8, 0, + 1, 4, 4, 0, 1, 1, 8, 3, 8, 3, 8, 3, 9, 1, 6, 0, 7, 1, 3, 3, 4, 9, 3, 5, 2, 4, 2, + 0, 7, 3, 3, 8, 7, 7, 8, 8, 0, 9, 3, 1, 2, 2, 4, 3, 3, 3, 6, 1, 6, 9, 6, 2, 0, 1, + 7, 5, 6, 2, 5, 3, 5, 0, 3, 2, 7, 2, 3, 0, 3, 6, 1, 7, 8, 7, 0, 4, 0, 6, 7, 6, 6, + 3, 9, 8, 5, 8, 3, 3, 0, 9, 6, 7, 1, 9, 2, 1, 3, 5, 1, 6, 3, 4, 3, 4, 1, 6, 8, 4, + 2, 5 + }; + expected = 70; + actual = solution1.superPow(a, b); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_376Test.java b/src/test/java/com/fishercoder/firstthousand/_376Test.java new file mode 100644 index 0000000000..b7479c82b9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_376Test.java @@ -0,0 +1,93 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._376; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _376Test { + private _376.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _376.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 7, 4, 9, 2, 5}; + assertEquals(6, solution1.wiggleMaxLength(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 17, 5, 10, 13, 15, 10, 5, 16, 8}; + assertEquals(7, solution1.wiggleMaxLength(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; + assertEquals(2, solution1.wiggleMaxLength(nums)); + } + + @Test + public void test4() { + nums = + new int[] { + 33, 53, 12, 64, 50, 41, 45, 21, 97, 35, 47, 92, 39, 0, 93, 55, 40, 46, 69, 42, + 6, 95, 51, 68, 72, 9, 32, 84, 34, 64, 6, 2, 26, 98, 3, 43, 30, 60, 3, 68, 82, 9, + 97, 19, 27, 98, 99, 4, 30, 96, 37, 9, 78, 43, 64, 4, 65, 30, 84, 90, 87, 64, 18, + 50, 60, 1, 40, 32, 48, 50, 76, 100, 57, 29, 63, 53, 46, 57, 93, 98, 42, 80, 82, + 9, 41, 55, 69, 84, 82, 79, 30, 79, 18, 97, 67, 23, 52, 38, 74, 15 + }; + assertEquals(67, solution1.wiggleMaxLength(nums)); + } + + @Test + public void test5() { + nums = new int[] {3, 3, 3, 2, 5}; + assertEquals(3, solution1.wiggleMaxLength(nums)); + } + + @Test + public void test6() { + nums = + new int[] { + 372, 492, 288, 399, 81, 2, 320, 94, 416, 469, 427, 117, 265, 357, 399, 456, 496, + 337, 355, 219, 475, 295, 457, 350, 490, 470, 281, 127, 131, 36, 430, 412, 442, + 174, 128, 253, 1, 56, 306, 295, 340, 73, 253, 130, 259, 223, 14, 79, 409, 384, + 209, 151, 317, 441, 156, 275, 140, 224, 128, 250, 290, 191, 161, 472, 477, 125, + 470, 230, 321, 5, 311, 23, 27, 248, 138, 284, 215, 356, 320, 194, 434, 136, 221, + 273, 450, 440, 28, 179, 36, 386, 482, 203, 24, 8, 391, 21, 500, 484, 135, 348, + 292, 396, 145, 443, 406, 61, 212, 480, 455, 78, 309, 318, 84, 474, 209, 225, + 177, 356, 227, 263, 181, 476, 478, 151, 494, 395, 23, 114, 395, 429, 450, 247, + 245, 150, 354, 230, 100, 172, 454, 155, 189, 33, 290, 187, 443, 123, 59, 358, + 241, 141, 39, 196, 491, 381, 157, 157, 134, 431, 295, 20, 123, 118, 207, 199, + 317, 188, 267, 335, 315, 308, 115, 321, 56, 52, 253, 492, 97, 374, 398, 272, 74, + 206, 109, 172, 471, 55, 452, 452, 329, 367, 372, 252, 99, 62, 122, 287, 320, + 325, 307, 481, 316, 378, 87, 97, 457, 21, 312, 249, 354, 286, 196, 43, 170, 500, + 265, 253, 19, 480, 438, 113, 473, 247, 257, 33, 395, 456, 246, 310, 469, 408, + 112, 385, 53, 449, 117, 122, 210, 286, 149, 20, 364, 372, 71, 26, 155, 292, 16, + 72, 384, 160, 79, 241, 346, 230, 15, 427, 96, 95, 59, 151, 325, 490, 223, 131, + 81, 294, 18, 70, 171, 339, 14, 40, 463, 421, 355, 123, 408, 357, 202, 235, 390, + 344, 198, 98, 361, 434, 174, 216, 197, 274, 231, 85, 494, 57, 136, 258, 134, + 441, 477, 456, 318, 155, 138, 461, 65, 426, 162, 90, 342, 284, 374, 204, 464, 9, + 280, 391, 491, 231, 298, 284, 82, 417, 355, 356, 207, 367, 262, 244, 283, 489, + 477, 143, 495, 472, 372, 447, 322, 399, 239, 450, 168, 202, 89, 333, 276, 199, + 416, 490, 494, 488, 137, 327, 113, 189, 430, 320, 197, 120, 71, 262, 31, 295, + 218, 74, 238, 169, 489, 308, 300, 260, 397, 308, 328, 267, 419, 84, 357, 486, + 289, 312, 230, 64, 468, 227, 268, 28, 243, 267, 254, 153, 407, 399, 346, 385, + 77, 297, 273, 484, 366, 482, 491, 368, 221, 423, 107, 272, 98, 309, 426, 181, + 320, 77, 185, 382, 478, 398, 476, 22, 328, 450, 299, 211, 285, 62, 344, 484, + 395, 466, 291, 487, 301, 407, 28, 295, 36, 429, 99, 462, 240, 124, 261, 387, 30, + 362, 161, 156, 184, 188, 99, 377, 392, 442, 300, 98, 285, 312, 312, 365, 415, + 367, 105, 81, 378, 413, 43, 326, 490, 320, 266, 390, 53, 327, 75, 332, 454, 29, + 370, 392, 360, 1, 335, 355, 344, 120, 417, 455, 93, 60, 256, 451, 188, 161, 388, + 338, 238, 26, 275, 340, 109, 185 + }; + assertEquals(334, solution1.wiggleMaxLength(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_377Test.java b/src/test/java/com/fishercoder/firstthousand/_377Test.java new file mode 100644 index 0000000000..edbe1c9d56 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_377Test.java @@ -0,0 +1,68 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._377; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _377Test { + private _377.Solution1 solution1; + private _377.Solution2 solution2; + private _377.Solution3 solution3; + private _377.Solution4 solution4; + private static int[] nums; + private static int target; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _377.Solution1(); + solution2 = new _377.Solution2(); + solution3 = new _377.Solution3(); + } + + @BeforeEach + public void setUp() throws Exception { + // always have to reset these global variables before using it again + solution2.count = 0; + solution4 = new _377.Solution4(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3}; + target = 4; + expected = 7; + assertEquals(expected, solution1.combinationSum4(nums, target)); + assertEquals(expected, solution2.combinationSum4(nums, target)); + assertEquals(expected, solution3.combinationSum4(nums, target)); + assertEquals(expected, solution4.combinationSum4(nums, target)); + } + + @Test + public void test2() { + nums = new int[] {4, 2, 1}; + target = 32; + expected = 39882198; + // assertEquals(39882198, solution1.combinationSum4(nums, target));//this results in + // MLE, so comment out + + assertEquals(expected, solution2.combinationSum4(nums, target)); + + assertEquals(expected, solution3.combinationSum4(nums, target)); + + assertEquals(expected, solution4.combinationSum4(nums, target)); + } + + @Test + public void test3() { + nums = new int[] {9}; + target = 3; + expected = 0; + assertEquals(expected, solution1.combinationSum4(nums, target)); + assertEquals(expected, solution2.combinationSum4(nums, target)); + assertEquals(expected, solution3.combinationSum4(nums, target)); + assertEquals(expected, solution4.combinationSum4(nums, target)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_378Test.java b/src/test/java/com/fishercoder/firstthousand/_378Test.java new file mode 100644 index 0000000000..92a89fcd26 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_378Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._378; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _378Test { + private _378.Solution1 solution1; + private _378.Solution2 solution2; + private _378.Solution3 solution3; + private static int[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _378.Solution1(); + solution2 = new _378.Solution2(); + solution3 = new _378.Solution3(); + } + + @Test + public void test1() { + matrix = new int[][] {new int[] {-5}}; + assertEquals(-5, solution1.kthSmallest(matrix, 1)); + assertEquals(-5, solution2.kthSmallest(matrix, 1)); + assertEquals(-5, solution3.kthSmallest(matrix, 1)); + } + + @Test + public void test2() { + matrix = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2],[1,3]"); + assertEquals(1, solution1.kthSmallest(matrix, 2)); + assertEquals(1, solution2.kthSmallest(matrix, 2)); + assertEquals(1, solution3.kthSmallest(matrix, 2)); + } + + @Test + public void test3() { + matrix = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,5,9],[10,11,13],[12,13,15]"); + assertEquals(13, solution1.kthSmallest(matrix, 8)); + assertEquals(13, solution2.kthSmallest(matrix, 8)); + assertEquals(13, solution3.kthSmallest(matrix, 8)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_37Test.java b/src/test/java/com/fishercoder/firstthousand/_37Test.java new file mode 100644 index 0000000000..16132d627e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_37Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._37; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _37Test { + private _37.Solution1 solution1; + private static char[][] board; + + @BeforeEach + public void setup() { + solution1 = new _37.Solution1(); + } + + @Test + public void test1() { + board = + new char[][] { + {'5', '3', '.', '.', '7', '.', '.', '.', '.'}, + {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, + {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, + {'8', '3', '.', '.', '6', '.', '.', '.', '3'}, + {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, + {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, + {'.', '6', '.', '.', '7', '.', '2', '8', '.'}, + {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, + {'.', '.', '.', '.', '8', '.', '.', '7', '9'} + }; + CommonUtils.print2DCharArray(board); + solution1.solveSudoku(board); + CommonUtils.print2DCharArray(board); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_381Test.java b/src/test/java/com/fishercoder/firstthousand/_381Test.java new file mode 100644 index 0000000000..dbb8bbb8e8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_381Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.firstthousand._381; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _381Test { + private _381.Solution1.RandomizedCollection randomizedCollection; + + @BeforeEach + public void setup() { + randomizedCollection = new _381.Solution1.RandomizedCollection(); + } + + @Test + public void test1() { + assertTrue(randomizedCollection.insert(1)); + assertFalse(randomizedCollection.insert(1)); + assertTrue(randomizedCollection.insert(2)); + randomizedCollection.getRandom(); + assertTrue(randomizedCollection.remove(2)); + randomizedCollection.getRandom(); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_384Test.java b/src/test/java/com/fishercoder/firstthousand/_384Test.java new file mode 100644 index 0000000000..1ea924c463 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_384Test.java @@ -0,0 +1,15 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._384; +import org.junit.jupiter.api.Test; + +public class _384Test { + private _384.Solution2 solution2; + + @Test + public void test1() { + solution2 = new _384.Solution2(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); + CommonUtils.printArray(solution2.shuffle()); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_385Test.java b/src/test/java/com/fishercoder/firstthousand/_385Test.java new file mode 100644 index 0000000000..981039e06e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_385Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._385; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _385Test { + private _385.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _385.Solution1(); + } + + @Test + public void test1() { + solution1.deserialize("324"); + } + + @Test + public void test2() { + solution1.deserialize("[-1]"); + } + + @Test + public void test3() { + solution1.deserialize("[]"); + } + + @Test + public void test4() { + solution1.deserialize("[-1,-2]"); + } + + @Test + public void test5() { + solution1.deserialize("[-1,-2,[-3,-4,[5,[6,[7,8]]]]]"); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_388Test.java b/src/test/java/com/fishercoder/firstthousand/_388Test.java new file mode 100644 index 0000000000..4c269f0917 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_388Test.java @@ -0,0 +1,62 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._388; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _388Test { + private _388.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _388.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 10, + solution1.lengthLongestPath( + "dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext")); + } + + @Test + public void test2() { + assertEquals( + 9, solution1.lengthLongestPath("dir\\n\\tsubdir1\\n\\tsubdir2\\n\\t\\tfile.ext")); + } + + @Test + public void test3() { + assertEquals(7, solution1.lengthLongestPath("aaaaaaaaaaaaaaaaaaaaa/sth.png")); + } + + @Test + public void test4() { + assertEquals(9, solution1.lengthLongestPath("a/aa/aaa/file1.txt")); + } + + @Test + public void test5() { + assertEquals(25, solution1.lengthLongestPath("file name with space.txt")); + } + + @Test + public void test6() { + assertEquals(13, solution1.lengthLongestPath("dir\\n file.txt")); + } + + @Test + public void test7() { + assertEquals(12, solution1.lengthLongestPath("dir\n file.txt")); + } + + @Test + public void test8() { + assertEquals( + 7, + solution1.lengthLongestPath("a\\n\\tb1\\n\\t\\tf1.txt\\n\\taaaaa\\n\\t\\tf2.txt")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_38Test.java b/src/test/java/com/fishercoder/firstthousand/_38Test.java new file mode 100644 index 0000000000..fa65f70732 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_38Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._38; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _38Test { + private _38.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _38.Solution1(); + } + + @Test + public void test1() { + assertEquals("21", solution1.countAndSay(3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_392Test.java b/src/test/java/com/fishercoder/firstthousand/_392Test.java new file mode 100644 index 0000000000..434a2e279f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_392Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._392; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _392Test { + private _392.Solution1 solution1; + private static String s; + private static String t; + private static boolean expected; + private static boolean actual; + + @BeforeEach + public void setUp() { + solution1 = new _392.Solution1(); + } + + @Test + public void test1() { + s = "abc"; + t = "ahbgdc"; + expected = true; + actual = solution1.isSubsequence(s, t); + assertEquals(expected, actual); + } + + @Test + public void test2() { + s = "axc"; + t = "ahbgdc"; + expected = false; + actual = solution1.isSubsequence(s, t); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_393Test.java b/src/test/java/com/fishercoder/firstthousand/_393Test.java new file mode 100644 index 0000000000..59843a778d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_393Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._393; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +public class _393Test { + private _393.Solution1 solution1; + private static boolean expected; + private static boolean actual; + private static int[] data; + + @BeforeEach + public void setUp() { + solution1 = new _393.Solution1(); + } + + @Test + @Disabled + public void test1() { + data = new int[] {197, 130, 1}; + expected = true; + actual = solution1.validUtf8(data); + assertEquals(expected, actual); + } + + @Test + public void test2() { + data = new int[] {5}; + expected = true; + actual = solution1.validUtf8(data); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_394Test.java b/src/test/java/com/fishercoder/firstthousand/_394Test.java new file mode 100644 index 0000000000..b119b04f3b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_394Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._394; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by varunu28 on 1/08/19. */ +public class _394Test { + private _394.Solution1 test; + + @BeforeEach + public void setUp() { + test = new _394.Solution1(); + } + + @Test + public void test1() { + assertEquals("aaabcbc", test.decodeString("3[a]2[bc]")); + } + + @Test + public void test2() { + assertEquals("accaccacc", test.decodeString("3[a2[c]]")); + } + + @Test + public void test3() { + assertEquals("abcabccdcdcdef", test.decodeString("2[abc]3[cd]ef")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_395Test.java b/src/test/java/com/fishercoder/firstthousand/_395Test.java new file mode 100644 index 0000000000..6ac366c8bb --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_395Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._395; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _395Test { + + private _395.Solution1 solution1; + private _395.Solution2 solution2; + private static String s; + private static int k; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _395.Solution1(); + solution2 = new _395.Solution2(); + } + + @Test + public void test1() { + s = "ababbc"; + expected = 5; + k = 2; + assertEquals(expected, solution1.longestSubstring(s, k)); + assertEquals(expected, solution2.longestSubstring(s, k)); + } + + @Test + public void test2() { + s = "aaabb"; + k = 3; + expected = 3; + assertEquals(expected, solution1.longestSubstring(s, k)); + assertEquals(expected, solution2.longestSubstring(s, k)); + } + + @Test + public void test3() { + s = "bbaaacbd"; + k = 3; + expected = 3; + assertEquals(expected, solution1.longestSubstring(s, k)); + assertEquals(expected, solution2.longestSubstring(s, k)); + } + + @Test + public void test4() { + s = "weitong"; + k = 2; + expected = 0; + assertEquals(expected, solution1.longestSubstring(s, k)); + assertEquals(expected, solution2.longestSubstring(s, k)); + } + + @Test + public void test5() { + s = "a"; + k = 2; + expected = 0; + assertEquals(expected, solution1.longestSubstring(s, k)); + assertEquals(expected, solution2.longestSubstring(s, k)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_396Test.java b/src/test/java/com/fishercoder/firstthousand/_396Test.java new file mode 100644 index 0000000000..55c69fc2fd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_396Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._396; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _396Test { + private _396.Solution1 solution1; + private _396.Solution2 solution2; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _396.Solution1(); + solution2 = new _396.Solution2(); + } + + @Test + public void test1() { + A = new int[] {4, 3, 2, 6}; + assertEquals(26, solution1.maxRotateFunction(A)); + assertEquals(26, solution2.maxRotateFunction(A)); + } + + @Test + public void test2() { + A = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + assertEquals(330, solution1.maxRotateFunction(A)); + assertEquals(330, solution2.maxRotateFunction(A)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_397Test.java b/src/test/java/com/fishercoder/firstthousand/_397Test.java new file mode 100644 index 0000000000..f90f3a007c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_397Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._397; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _397Test { + private _397.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _397.Solution1(); + } + + @Test + public void test1() { + assertEquals(17, solution1.integerReplacement(65535)); + } + + @Test + public void test2() { + assertEquals(14, solution1.integerReplacement(1234)); + } + + @Test + public void test3() { + assertEquals(3, solution1.integerReplacement(5)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_39Test.java b/src/test/java/com/fishercoder/firstthousand/_39Test.java new file mode 100644 index 0000000000..75620c59bb --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_39Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._39; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _39Test { + private _39.Solution1 solution1; + private static int[] candidates; + private static List> expected; + + @BeforeEach + public void setup() { + solution1 = new _39.Solution1(); + } + + @Test + public void test1() { + candidates = new int[] {2, 3, 6, 7}; + expected = new ArrayList<>(); + expected.add(Arrays.asList(2, 2, 3)); + expected.add(Arrays.asList(7)); + assertEquals(expected, solution1.combinationSum(candidates, 7)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_3Test.java b/src/test/java/com/fishercoder/firstthousand/_3Test.java new file mode 100644 index 0000000000..adfaed89fb --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_3Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._3; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3Test { + private _3.Solution1 solution1; + private _3.Solution2 solution2; + private _3.Solution3 solution3; + private _3.Solution4 solution4; + private _3.Solution5 solution5; + private _3.Solution6 solution6; + private static int expected; + private static String s; + + @BeforeEach + public void setup() { + solution1 = new _3.Solution1(); + solution2 = new _3.Solution2(); + solution3 = new _3.Solution3(); + solution4 = new _3.Solution4(); + solution5 = new _3.Solution5(); + solution6 = new _3.Solution6(); + } + + @Test + public void test1() { + expected = 3; + s = "abcabcbb"; + assertEquals(expected, solution1.lengthOfLongestSubstring(s)); + assertEquals(expected, solution2.lengthOfLongestSubstring(s)); + assertEquals(expected, solution3.lengthOfLongestSubstring(s)); + assertEquals(expected, solution4.lengthOfLongestSubstring(s)); + assertEquals(expected, solution5.lengthOfLongestSubstring(s)); + assertEquals(expected, solution6.lengthOfLongestSubstring(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_400Test.java b/src/test/java/com/fishercoder/firstthousand/_400Test.java new file mode 100644 index 0000000000..5dc6e10a2a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_400Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._400; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _400Test { + private _400.Solution1 solution1; + private static int expected; + private static int actual; + private static int n; + + @BeforeEach + public void setUp() { + solution1 = new _400.Solution1(); + } + + @Test + public void test1() { + n = 11; + expected = 0; + actual = solution1.findNthDigit(n); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_401Test.java b/src/test/java/com/fishercoder/firstthousand/_401Test.java new file mode 100644 index 0000000000..cc22597b4e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_401Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._401; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _401Test { + private _401.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _401.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList( + "0:01", "0:02", "0:04", "0:08", "0:16", "0:32", "1:00", "2:00", "4:00", + "8:00"), + solution1.readBinaryWatch(1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_402Test.java b/src/test/java/com/fishercoder/firstthousand/_402Test.java new file mode 100644 index 0000000000..192c8d7083 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_402Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._402; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _402Test { + private _402.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _402.Solution1(); + } + + @Test + public void test1() { + assertEquals("1219", solution1.removeKdigits("1432219", 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_404Test.java b/src/test/java/com/fishercoder/firstthousand/_404Test.java new file mode 100644 index 0000000000..2f12c7c4af --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_404Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._404; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _404Test { + private _404.Solution1 solution1; + private _404.Solution2 solution2; + private _404.Solution3 solution3; + private static TreeNode root; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _404.Solution1(); + solution2 = new _404.Solution2(); + solution3 = new _404.Solution3(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); + expected = 24; + assertEquals(expected, solution1.sumOfLeftLeaves(root)); + assertEquals(expected, solution2.sumOfLeftLeaves(root)); + assertEquals(expected, solution3.sumOfLeftLeaves(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2)); + expected = 0; + assertEquals(expected, solution1.sumOfLeftLeaves(root)); + assertEquals(expected, solution2.sumOfLeftLeaves(root)); + assertEquals(expected, solution3.sumOfLeftLeaves(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_406Test.java b/src/test/java/com/fishercoder/firstthousand/_406Test.java new file mode 100644 index 0000000000..e56f5d5cfe --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_406Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._406; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _406Test { + private _406.Solution1 solution1; + private static int[][] people; + private static int[][] actual; + + @BeforeEach + public void setUp() { + solution1 = new _406.Solution1(); + } + + @Test + public void test1() { + people = + new int[][] { + {7, 0}, + {4, 4}, + {7, 1}, + {5, 0}, + {6, 1}, + {5, 2} + }; + actual = solution1.reconstructQueue(people); + CommonUtils.printArrayArray(actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_408Test.java b/src/test/java/com/fishercoder/firstthousand/_408Test.java new file mode 100644 index 0000000000..6462eb480e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_408Test.java @@ -0,0 +1,125 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._408; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _408Test { + private _408.Solution1 solution1; + private _408.Solution2 solution2; + private static Boolean expected; + private static Boolean actual; + private static String word; + private static String abbr; + + @BeforeEach + public void setup() { + solution1 = new _408.Solution1(); + solution2 = new _408.Solution2(); + word = ""; + abbr = ""; + } + + @Test + public void test1() { + word = "internationalization"; + abbr = "i12iz4n"; + expected = true; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + actual = solution2.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test2() { + word = "apple"; + abbr = "a2e"; + expected = false; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test3() { + word = "internationalization"; + abbr = "i5a11o1"; + expected = true; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test4() { + word = "hi"; + abbr = "1"; + expected = false; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test5() { + word = "a"; + abbr = "1"; + expected = true; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test6() { + word = "a"; + abbr = "2"; + expected = false; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test7() { + word = "hi"; + abbr = "1i"; + expected = true; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test8() { + word = "hi"; + abbr = "3"; + expected = false; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test9() { + word = "hi"; + abbr = "11"; + expected = false; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test10() { + word = "word"; + abbr = "1o1d"; + expected = true; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } + + @Test + public void test11() { + word = "abbreviation"; + abbr = "a010n"; + expected = false; + actual = solution1.validWordAbbreviation(word, abbr); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_409Test.java b/src/test/java/com/fishercoder/firstthousand/_409Test.java new file mode 100644 index 0000000000..79fb529d0d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_409Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._409; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _409Test { + private _409.Solution1 solution1; + private _409.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _409.Solution1(); + solution2 = new _409.Solution2(); + } + + @Test + public void test1() { + assertEquals(7, solution1.longestPalindrome("abccccdd")); + assertEquals(7, solution2.longestPalindrome("abccccdd")); + } + + @Test + public void test2() { + assertEquals(7, solution1.longestPalindrome("abccAccdd")); + assertEquals(7, solution2.longestPalindrome("abccAccdd")); + } + + @Test + public void test3() { + assertEquals( + 983, + solution1.longestPalindrome( + "civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth")); + assertEquals( + 983, + solution2.longestPalindrome( + "civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth")); + } + + @Test + public void test4() { + assertEquals(3, solution1.longestPalindrome("ccc")); + assertEquals(3, solution2.longestPalindrome("ccc")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_40Test.java b/src/test/java/com/fishercoder/firstthousand/_40Test.java new file mode 100644 index 0000000000..b3d2f399c5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_40Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._40; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _40Test { + private _40.Solution1 solution1; + private static int[] candidates; + private static int target; + private static List> expected; + + @BeforeEach + public void setup() { + solution1 = new _40.Solution1(); + } + + @Test + public void test1() { + candidates = new int[] {10, 1, 2, 7, 6, 1, 5}; + expected = + Arrays.asList( + (Arrays.asList(1, 1, 6)), + Arrays.asList(1, 2, 5), + Arrays.asList(1, 7), + Arrays.asList(2, 6)); + target = 8; + assertEquals(expected, solution1.combinationSum2(candidates, target)); + } + + @Test + public void test2() { + candidates = + new int[] { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }; + expected = + Arrays.asList( + Arrays.asList( + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1)); + target = 30; + assertEquals(expected, solution1.combinationSum2(candidates, target)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_410Test.java b/src/test/java/com/fishercoder/firstthousand/_410Test.java new file mode 100644 index 0000000000..5acdae8d2e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_410Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._410; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _410Test { + private _410.Solution1 test; + private static int[] nums; + + @BeforeEach + public void setUp() { + test = new _410.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {7, 2, 5, 10, 8}; + assertEquals(18, test.splitArray(nums, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_415Test.java b/src/test/java/com/fishercoder/firstthousand/_415Test.java new file mode 100644 index 0000000000..5b30300506 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_415Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._415; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _415Test { + private _415.Solution1 solution1; + private _415.Solution2 solution2; + private static String expected; + private static String actual; + private static String num1; + private static String num2; + + @BeforeEach + public void setup() { + solution1 = new _415.Solution1(); + solution2 = new _415.Solution2(); + } + + @Test + public void test1() { + num1 = "123"; + num2 = "34567"; + expected = "34690"; + actual = solution1.addStrings(num1, num2); + assertEquals(expected, actual); + actual = solution2.addStrings(num1, num2); + assertEquals(expected, actual); + } + + @Test + public void test2() { + num1 = "1"; + num2 = "9"; + expected = "10"; + actual = solution1.addStrings(num1, num2); + assertEquals(expected, actual); + } + + @Test + public void test3() { + num1 = "9"; + num2 = "99"; + expected = "108"; + actual = solution1.addStrings(num1, num2); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_416Test.java b/src/test/java/com/fishercoder/firstthousand/_416Test.java new file mode 100644 index 0000000000..5f2b480484 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_416Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._416; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _416Test { + private _416.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _416.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 5, 11, 5}; + assertEquals(true, solution1.canPartition(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 3, 5}; + assertEquals(false, solution1.canPartition(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 3, 4, 5, 6, 7}; + assertEquals(true, solution1.canPartition(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_417Test.java b/src/test/java/com/fishercoder/firstthousand/_417Test.java new file mode 100644 index 0000000000..5a0922198c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_417Test.java @@ -0,0 +1,68 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._417; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _417Test { + private _417.Solution1 solution1; + private static int[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _417.Solution1(); + } + + @Test + public void test1() { + matrix = + new int[][] { + {2, 3, 5}, + {3, 4, 4}, + {5, 3, 1}, + }; + for (int[] arr : solution1.pacificAtlantic(matrix)) { + CommonUtils.printArray(arr); + } + } + + @Test + public void test2() { + matrix = + new int[][] { + {3, 5}, + {4, 4}, + }; + for (int[] arr : solution1.pacificAtlantic(matrix)) { + CommonUtils.printArray(arr); + } + } + + @Test + public void test3() { + matrix = + new int[][] { + {1, 2, 2, 3, 5}, + {3, 2, 3, 4, 4}, + {2, 4, 5, 3, 1}, + {6, 7, 1, 4, 5}, + {5, 1, 1, 2, 4}, + }; + for (int[] arr : solution1.pacificAtlantic(matrix)) { + CommonUtils.printArray(arr); + } + } + + @Test + public void test4() { + matrix = + new int[][] { + {2, 3, 5}, + {3, 4, 4}, + }; + for (int[] arr : solution1.pacificAtlantic(matrix)) { + CommonUtils.printArray(arr); + } + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_418Test.java b/src/test/java/com/fishercoder/firstthousand/_418Test.java new file mode 100644 index 0000000000..e0184d1fca --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_418Test.java @@ -0,0 +1,53 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._418; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _418Test { + private _418.Solution1 test; + private static String[] sentence; + + @BeforeEach + public void setup() { + test = new _418.Solution1(); + } + + @Test + public void test1() { + sentence = new String[] {"hello", "world"}; + assertEquals(1, test.wordsTyping(sentence, 2, 8)); + } + + @Test + public void test2() { + sentence = new String[] {"a", "bcd", "e"}; + assertEquals(2, test.wordsTyping(sentence, 3, 6)); + } + + @Test + public void test3() { + sentence = new String[] {"I", "had", "apple", "pie"}; + assertEquals(1, test.wordsTyping(sentence, 4, 5)); + } + + @Test + public void test4() { + sentence = new String[] {"f", "p", "a"}; + assertEquals(10, test.wordsTyping(sentence, 8, 7)); + } + + @Test + public void test5() { + sentence = new String[] {"hello", "leetcode"}; + assertEquals(1, test.wordsTyping(sentence, 1, 20)); + } + + @Test + public void test6() { + sentence = new String[] {"h"}; + assertEquals(4, test.wordsTyping(sentence, 2, 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_41Test.java b/src/test/java/com/fishercoder/firstthousand/_41Test.java new file mode 100644 index 0000000000..6e87dd0f61 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_41Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._41; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _41Test { + private _41.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _41.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 0}; + assertEquals(3, solution1.firstMissingPositive(nums)); + } + + @Test + public void test2() { + nums = new int[] {}; + assertEquals(1, solution1.firstMissingPositive(nums)); + } + + @Test + public void test3() { + nums = new int[] {3, 4, -1, 1}; + assertEquals(2, solution1.firstMissingPositive(nums)); + } + + @Test + public void test4() { + nums = new int[] {2}; + assertEquals(1, solution1.firstMissingPositive(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_421Test.java b/src/test/java/com/fishercoder/firstthousand/_421Test.java new file mode 100644 index 0000000000..83fc3c43c1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_421Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._421; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _421Test { + private _421.Solution1 solution1; + private static int expected; + private static int actual; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _421.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {3, 10, 5, 25, 2, 8}; + expected = 28; + actual = solution1.findMaximumXOR(nums); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_422Test.java b/src/test/java/com/fishercoder/firstthousand/_422Test.java new file mode 100644 index 0000000000..a775b17c11 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_422Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._422; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _422Test { + private _422.Solution1 test; + private static boolean expected; + private static boolean actual; + private static List words; + + @BeforeEach + public void setUp() { + test = new _422.Solution1(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + words = new ArrayList<>(Arrays.asList("abcd", "bnrt", "crmy", "dtye")); + expected = true; + actual = test.validWordSquare(words); + assertEquals(expected, actual); + } + + @Test + public void test2() { + words = new ArrayList<>(Arrays.asList("abcd", "bnrt", "crm", "dt")); + expected = true; + actual = test.validWordSquare(words); + assertEquals(expected, actual); + } + + @Test + public void test3() { + words = new ArrayList<>(Arrays.asList("ball", "asee", "let", "lep")); + expected = false; + actual = test.validWordSquare(words); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_423Test.java b/src/test/java/com/fishercoder/firstthousand/_423Test.java new file mode 100644 index 0000000000..d6a69f52a0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_423Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._423; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _423Test { + private _423.Solution1 solution1; + private static String expected; + private static String actual; + private static String s; + + @BeforeEach + public void setup() { + solution1 = new _423.Solution1(); + } + + @Test + public void test1() { + s = "fviefuro"; + expected = "45"; + actual = solution1.originalDigits(s); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_424Test.java b/src/test/java/com/fishercoder/firstthousand/_424Test.java new file mode 100644 index 0000000000..7f13ab1f86 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_424Test.java @@ -0,0 +1,70 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._424; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _424Test { + private _424.Solution1 solution1; + private _424.Solution2 solution2; + private static String s; + private static int k; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _424.Solution1(); + solution2 = new _424.Solution2(); + } + + @Test + public void test1() { + s = "ABAB"; + k = 2; + expected = 4; + assertEquals(expected, solution1.characterReplacement(s, k)); + assertEquals(expected, solution2.characterReplacement(s, k)); + } + + @Test + public void test2() { + s = "AABABBA"; + k = 1; + expected = 4; + assertEquals(expected, solution1.characterReplacement(s, k)); + } + + @Test + public void test3() { + s = "AAAA"; + k = 2; + expected = 4; + assertEquals(expected, solution1.characterReplacement(s, k)); + } + + @Test + public void test4() { + s = "AAAB"; + k = 0; + expected = 3; + assertEquals(expected, solution1.characterReplacement(s, k)); + } + + @Test + public void test5() { + s = "AABA"; + k = 0; + expected = 2; + assertEquals(expected, solution1.characterReplacement(s, k)); + } + + @Test + public void test6() { + s = "ABBB"; + k = 2; + expected = 4; + assertEquals(expected, solution1.characterReplacement(s, k)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_425Test.java b/src/test/java/com/fishercoder/firstthousand/_425Test.java new file mode 100644 index 0000000000..04ba5f9fd1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_425Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._425; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _425Test { + private _425.Solution1 solution1; + private static String[] words; + + @BeforeEach + public void setup() { + solution1 = new _425.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"area", "lead", "wall", "lady", "ball"}; + List> result = solution1.wordSquares(words); + CommonUtils.printListList(result); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_426Test.java b/src/test/java/com/fishercoder/firstthousand/_426Test.java new file mode 100644 index 0000000000..9f6c6ae4ec --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_426Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._426; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _426Test { + private _426.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _426.Solution1(); + } + + @Test + public void test1() { + _426.Node node1 = new _426.Node(1); + _426.Node node3 = new _426.Node(3); + _426.Node node5 = new _426.Node(5); + _426.Node node2 = new _426.Node(2); + node2.left = node1; + node2.right = node3; + _426.Node node4 = new _426.Node(4); + node4.left = node2; + node4.right = node5; + _426.Node actual = solution1.treeToDoublyList(node4); + System.out.println("Finished."); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_429Test.java b/src/test/java/com/fishercoder/firstthousand/_429Test.java new file mode 100644 index 0000000000..9f3a1f315c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_429Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.Node; +import com.fishercoder.solutions.firstthousand._429; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _429Test { + private _429.Solution1 solution1; + private static Node root; + private static List> expected; + + @BeforeEach + public void setUp() { + solution1 = new _429.Solution1(); + } + + @Test + public void test1() { + root = new Node(1); + Node node3 = new Node(3); + Node node2 = new Node(2); + Node node4 = new Node(4); + root.children = Arrays.asList(node3, node2, node4); + Node node5 = new Node(5); + Node node6 = new Node(6); + node3.children = Arrays.asList(node5, node6); + expected = new ArrayList<>(); + expected.add(Arrays.asList(1)); + expected.add(Arrays.asList(3, 2, 4)); + expected.add(Arrays.asList(5, 6)); + assertEquals(expected, solution1.levelOrder(root)); + } + + @Test + public void test2() { + root = null; + expected = new ArrayList<>(); + assertEquals(expected, solution1.levelOrder(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_42Test.java b/src/test/java/com/fishercoder/firstthousand/_42Test.java new file mode 100644 index 0000000000..899cf42f30 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_42Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._42; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/13/17. */ +public class _42Test { + private _42.Solution1 solution1; + private static int[] height; + + @BeforeEach + public void setup() { + solution1 = new _42.Solution1(); + } + + @Test + public void test1() { + height = new int[] {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; + assertEquals(6, solution1.trap(height)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_433Test.java b/src/test/java/com/fishercoder/firstthousand/_433Test.java new file mode 100644 index 0000000000..cdbf47e1aa --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_433Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._433; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _433Test { + private _433.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _433.Solution1(); + } + + @Test + public void test1() { + assertEquals(-1, solution1.minMutation("AACCGGTT", "AACCGGTA", new String[] {})); + } + + @Test + public void test2() { + assertEquals( + -1, + solution1.minMutation( + "AAAAAAAA", + "CCCCCCCC", + new String[] { + "AAAAAAAA", + "AAAAAAAC", + "AAAAAACC", + "AAAAACCC", + "AAAACCCC", + "AACACCCC", + "ACCACCCC", + "ACCCCCCC", + "CCCCCCCA" + })); + } + + @Test + public void test3() { + assertEquals( + -1, + solution1.minMutation( + "AAAAAAAT", "CCCCCCCC", new String[] {"AAAAAAAC", "AAAAAAAA", "CCCCCCCC"})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_434Test.java b/src/test/java/com/fishercoder/firstthousand/_434Test.java new file mode 100644 index 0000000000..9e4fdeea01 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_434Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._434; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _434Test { + private _434.Solution1 solution1; + private static int expected; + private static int actual; + private static String s; + + @BeforeEach + public void setUp() { + solution1 = new _434.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = 0; + actual = 0; + } + + @Test + public void test1() { + s = "Hello, my name is John"; + expected = 5; + actual = solution1.countSegments(s); + assertEquals(expected, actual); + } + + @Test + public void test2() { + s = ", , , , a, eaefa"; + expected = 6; + actual = solution1.countSegments(s); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_435Test.java b/src/test/java/com/fishercoder/firstthousand/_435Test.java new file mode 100644 index 0000000000..f582707184 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_435Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._435; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _435Test { + private _435.Solution1 solution1; + private _435.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _435.Solution1(); + solution2 = new _435.Solution2(); + } + + @Test + public void test1() { + int[][] intervals = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2],[2,3],[3,4],[1,3]"); + assertEquals(1, solution1.eraseOverlapIntervals(intervals)); + assertEquals(1, solution2.eraseOverlapIntervals(intervals)); + } + + @Test + public void test2() { + int[][] intervals = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[-52,31],[-73,-26],[82,97],[-65,-11],[-62,-49],[95,99],[58,95],[-31,49],[66,98],[-63,2],[30,47],[-40,-26]"); + assertEquals(7, solution1.eraseOverlapIntervals(intervals)); + assertEquals(7, solution2.eraseOverlapIntervals(intervals)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_436Test.java b/src/test/java/com/fishercoder/firstthousand/_436Test.java new file mode 100644 index 0000000000..261b183472 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_436Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._436; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _436Test { + private _436.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _436.Solution1(); + } + + @Test + public void test1() { + int[][] intervals = + new int[][] { + {3, 4}, + {2, 3}, + {1, 2} + }; + solution1.findRightInterval(intervals); + } + + @Test + public void test2() { + int[][] intervals = + new int[][] { + {1, 4}, + {2, 3}, + {3, 4} + }; + solution1.findRightInterval(intervals); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_439Test.java b/src/test/java/com/fishercoder/firstthousand/_439Test.java new file mode 100644 index 0000000000..6739456566 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_439Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._439; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/18/17. */ +public class _439Test { + private _439.Solution1 solution1; + private static String expression; + private static String expected; + + @BeforeEach + public void setUp() { + solution1 = new _439.Solution1(); + } + + @Test + public void test1() { + expression = "T?2:3"; + expected = "2"; + assertEquals(expected, solution1.parseTernary(expression)); + } + + @Test + public void test2() { + expression = "F?1:T?4:5"; + expected = "4"; + assertEquals(expected, solution1.parseTernary(expression)); + } + + @Test + public void test3() { + expression = "T?T?F:5:3"; + expected = "F"; + assertEquals(expected, solution1.parseTernary(expression)); + } + + @Test + public void test4() { + expression = "T?T:F?T?1:2:F?3:4"; + expected = "T"; + assertEquals(expected, solution1.parseTernary(expression)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_43Test.java b/src/test/java/com/fishercoder/firstthousand/_43Test.java new file mode 100644 index 0000000000..088fa7da81 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_43Test.java @@ -0,0 +1,57 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._43; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _43Test { + private _43.Solution1 solution1; + private _43.Solution2 solution2; + private static String expected; + private static String num1; + private static String num2; + + @BeforeEach + public void setup() { + solution1 = new _43.Solution1(); + solution2 = new _43.Solution2(); + } + + @Test + public void test1() { + num1 = "123"; + num2 = "45"; + expected = "5535"; + assertEquals(expected, solution1.multiply(num1, num2)); + assertEquals(expected, solution2.multiply(num1, num2)); + } + + @Test + public void test2() { + num1 = "9133"; + num2 = "0"; + expected = "0"; + assertEquals(expected, solution1.multiply(num1, num2)); + assertEquals(expected, solution2.multiply(num1, num2)); + } + + @Test + public void test3() { + num1 = "6913259244"; + num2 = "71103343"; + expected = "491555843274052692"; + assertEquals(expected, solution1.multiply(num1, num2)); + assertEquals(expected, solution2.multiply(num1, num2)); + } + + @Test + public void test4() { + num1 = "401716832807512840963"; + num2 = "167141802233061013023557397451289113296441069"; + expected = "67143675422804947379429215144664313370120390398055713625298709447"; + assertEquals(expected, solution1.multiply(num1, num2)); + assertEquals(expected, solution2.multiply(num1, num2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_441Test.java b/src/test/java/com/fishercoder/firstthousand/_441Test.java new file mode 100644 index 0000000000..7e52e61d5d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_441Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._441; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _441Test { + private _441.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _441.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.arrangeCoins(3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_442Test.java b/src/test/java/com/fishercoder/firstthousand/_442Test.java new file mode 100644 index 0000000000..58c790eb7b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_442Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._442; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _442Test { + private _442.Solution1 solution1; + private _442.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _442.Solution1(); + solution2 = new _442.Solution2(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList(2, 3), solution1.findDuplicates(new int[] {4, 3, 2, 7, 8, 2, 3, 1})); + assertEquals( + Arrays.asList(2, 3), solution2.findDuplicates(new int[] {4, 3, 2, 7, 8, 2, 3, 1})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_443Test.java b/src/test/java/com/fishercoder/firstthousand/_443Test.java new file mode 100644 index 0000000000..d56753855d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_443Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._443; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _443Test { + private _443.Solution1 solution1; + private static char[] chars; + + @BeforeEach + public void setup() { + solution1 = new _443.Solution1(); + } + + @Test + public void test1() { + chars = new char[] {'a', 'a', 'b', 'b', 'c', 'c', 'c'}; + assertEquals(6, solution1.compress(chars)); + } + + @Test + public void test2() { + chars = new char[] {'a'}; + assertEquals(1, solution1.compress(chars)); + } + + @Test + public void test3() { + chars = new char[] {'a', 'b'}; + assertEquals(2, solution1.compress(chars)); + } + + @Test + public void test4() { + chars = new char[] {'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'}; + assertEquals(4, solution1.compress(chars)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_444Test.java b/src/test/java/com/fishercoder/firstthousand/_444Test.java new file mode 100644 index 0000000000..a7f1a27c57 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_444Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._444; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _444Test { + private _444.Solution1 solution1; + private static int[] org; + private static List> seqs; + + @BeforeEach + public void setup() { + solution1 = new _444.Solution1(); + } + + @Test + public void test1() { + org = new int[] {1, 2, 3}; + seqs = new ArrayList<>(); + seqs.add(Arrays.asList(1, 2)); + seqs.add(Arrays.asList(1, 3)); + assertEquals(false, solution1.sequenceReconstruction(org, seqs)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_445Test.java b/src/test/java/com/fishercoder/firstthousand/_445Test.java new file mode 100644 index 0000000000..c926890eb9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_445Test.java @@ -0,0 +1,54 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._445; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/13/17. */ +public class _445Test { + private _445.Solution1 solution1; + private _445.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _445.Solution1(); + solution2 = new _445.Solution2(); + } + + @Test + public void test1() { + ListNode l1 = LinkedListUtils.contructLinkedList(new int[] {7, 2, 4, 3}); + + ListNode l2 = LinkedListUtils.contructLinkedList(new int[] {5, 6, 4}); + + ListNode expected = LinkedListUtils.contructLinkedList(new int[] {7, 8, 0, 7}); + + assertEquals(expected, solution1.addTwoNumbers(l1, l2)); + } + + @Test + public void test2() { + ListNode l1 = LinkedListUtils.contructLinkedList(new int[] {7, 2, 4, 3}); + + ListNode l2 = LinkedListUtils.contructLinkedList(new int[] {5, 6, 4}); + + ListNode expected = LinkedListUtils.contructLinkedList(new int[] {7, 8, 0, 7}); + + assertEquals(expected, solution2.addTwoNumbers(l1, l2)); + } + + @Test + public void test3() { + ListNode l1 = LinkedListUtils.contructLinkedList(new int[] {5}); + + ListNode l2 = LinkedListUtils.contructLinkedList(new int[] {5}); + + ListNode expected = LinkedListUtils.contructLinkedList(new int[] {1, 0}); + + assertEquals(expected, solution2.addTwoNumbers(l1, l2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_447Test.java b/src/test/java/com/fishercoder/firstthousand/_447Test.java new file mode 100644 index 0000000000..8d29aa59ac --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_447Test.java @@ -0,0 +1,45 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._447; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _447Test { + private _447.Solution1 solution1; + private static int[][] points; + + @BeforeEach + public void setup() { + solution1 = new _447.Solution1(); + } + + @Test + public void test1() { + points = + new int[][] { + {0, 0}, + {1, 0}, + {2, 0}, + }; + assertEquals(2, solution1.numberOfBoomerangs(points)); + } + + @Test + public void test2() { + points = + new int[][] { + {3, 6}, + {7, 5}, + {3, 5}, + {6, 2}, + {9, 1}, + {2, 7}, + {0, 9}, + {0, 6}, + {2, 6} + }; + assertEquals(10, solution1.numberOfBoomerangs(points)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_449Test.java b/src/test/java/com/fishercoder/firstthousand/_449Test.java new file mode 100644 index 0000000000..e3ea11dbff --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_449Test.java @@ -0,0 +1,66 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.solutions.firstthousand._449; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _449Test { + private _449.Solution1 solution1; + private _449.Solution2 solution2; + private _449.Solution3 solution3; + private _449.Solution4 solution4; + private static TreeNode expectedRoot; + + @BeforeEach + public void setup() { + solution1 = new _449.Solution1(); + solution2 = new _449.Solution2(); + solution3 = new _449.Solution3(); + solution4 = new _449.Solution4(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + expectedRoot = new TreeNode(3); + expectedRoot.left = new TreeNode(1); + expectedRoot.right = new TreeNode(4); + expectedRoot.left.right = new TreeNode(2); + assertEquals( + expectedRoot.toString(), + solution1.deserialize(solution1.serialize(expectedRoot)).toString()); + assertEquals( + expectedRoot.toString(), + solution2.deserialize(solution2.serialize(expectedRoot)).toString()); + assertEquals( + expectedRoot.toString(), + solution3.deserialize(solution3.serialize(expectedRoot)).toString()); + assertEquals( + expectedRoot.toString(), + solution4.deserialize(solution4.serialize(expectedRoot)).toString()); + } + + @Test + public void test2() { + expectedRoot = new TreeNode(2); + expectedRoot.left = new TreeNode(1); + expectedRoot.right = new TreeNode(3); + assertEquals( + expectedRoot.toString(), + solution1.deserialize(solution1.serialize(expectedRoot)).toString()); + assertEquals( + expectedRoot.toString(), + solution2.deserialize(solution2.serialize(expectedRoot)).toString()); + assertEquals( + expectedRoot.toString(), + solution3.deserialize(solution3.serialize(expectedRoot)).toString()); + assertEquals( + expectedRoot.toString(), + solution4.deserialize(solution4.serialize(expectedRoot)).toString()); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_44Test.java b/src/test/java/com/fishercoder/firstthousand/_44Test.java new file mode 100644 index 0000000000..152448d90d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_44Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._44; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _44Test { + private _44.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _44.Solution1(); + } + + @Test + public void test1() { + assertEquals(false, solution1.isMatch("aa", "a")); + } + + @Test + public void test2() { + assertEquals(true, solution1.isMatch("aa", "aa")); + } + + @Test + public void test3() { + assertEquals(false, solution1.isMatch("aaa", "aa")); + } + + @Test + public void test4() { + assertEquals(true, solution1.isMatch("aa", "*")); + } + + @Test + public void test5() { + assertEquals(true, solution1.isMatch("aa", "a*")); + } + + @Test + public void test6() { + assertEquals(true, solution1.isMatch("ab", "?*")); + } + + @Test + public void test7() { + assertEquals(false, solution1.isMatch("aab", "c*a*b")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_450Test.java b/src/test/java/com/fishercoder/firstthousand/_450Test.java new file mode 100644 index 0000000000..db09c365b5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_450Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._450; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _450Test { + private _450.Solution1 solution1; + private _450.Solution2 solution2; + private _450.Solution3 solution3; + private static TreeNode expected; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _450.Solution1(); + solution2 = new _450.Solution2(); + solution3 = new _450.Solution3(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(5, 3, 6, 2, 4, null, 7)); + TreeUtils.printBinaryTree(root); + + expected = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 6, 2, null, null, 7)); + TreeUtils.printBinaryTree(expected); + assertEquals(expected, solution1.deleteNode(root, 3)); + + expected = TreeUtils.constructBinaryTree(Arrays.asList(5, 2, 6, null, 4, null, 7)); + TreeUtils.printBinaryTree(expected); + assertEquals(expected, solution2.deleteNode(root, 3)); + + expected = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 6, 2, null, null, 7)); + assertEquals(expected, solution3.deleteNode(root, 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_451Test.java b/src/test/java/com/fishercoder/firstthousand/_451Test.java new file mode 100644 index 0000000000..5c49ba669d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_451Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._451; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _451Test { + private _451.Solution1 solution1; + private _451.Solution2 solution2; + private static String expected; + private static String input; + + @BeforeEach + public void setup() { + solution1 = new _451.Solution1(); + solution2 = new _451.Solution2(); + } + + @Test + public void test1() { + input = "tree"; + expected = "eert"; + assertEquals(expected, solution1.frequencySort(input)); + assertEquals(expected, solution2.frequencySort(input)); + } + + @Test + public void test2() { + input = "cccaaa"; + expected = "aaaccc"; + assertEquals(expected, solution1.frequencySort(input)); + assertEquals(expected, solution2.frequencySort(input)); + } + + @Test + public void test3() { + input = "Aabb"; + expected = "bbAa"; + assertEquals(expected, solution1.frequencySort(input)); + assertEquals(expected, solution2.frequencySort(input)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_452Test.java b/src/test/java/com/fishercoder/firstthousand/_452Test.java new file mode 100644 index 0000000000..a7b30ea48f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_452Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._452; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _452Test { + private _452.Solution1 solution1; + private _452.Solution2 solution2; + private _452.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _452.Solution1(); + solution2 = new _452.Solution2(); + solution3 = new _452.Solution3(); + } + + @Test + public void test1() { + int[][] points = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[3,9],[7,12],[3,8],[6,8],[9,10],[2,9],[0,9],[3,9],[0,6],[2,8]"); + assertEquals(2, solution1.findMinArrowShots(points)); + assertEquals(2, solution2.findMinArrowShots(points)); + assertEquals(2, solution3.findMinArrowShots(points)); + } + + @Test + public void test2() { + int[][] points = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[-2147483646,-2147483645],[2147483646,2147483647]"); + assertEquals(2, solution1.findMinArrowShots(points)); + assertEquals(2, solution2.findMinArrowShots(points)); + assertEquals(2, solution3.findMinArrowShots(points)); + } + + @Test + public void test3() { + int[][] points = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,6],[0,9],[7,8]"); + assertEquals(2, solution1.findMinArrowShots(points)); + assertEquals(2, solution2.findMinArrowShots(points)); + assertEquals(2, solution3.findMinArrowShots(points)); + } + + @Test + public void test4() { + int[][] points = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[9,12],[1,10],[4,11],[8,12],[3,9],[6,9],[6,7]"); + assertEquals(2, solution1.findMinArrowShots(points)); + assertEquals(2, solution2.findMinArrowShots(points)); + assertEquals(2, solution3.findMinArrowShots(points)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_453Test.java b/src/test/java/com/fishercoder/firstthousand/_453Test.java new file mode 100644 index 0000000000..b725346569 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_453Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._453; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _453Test { + private _453.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _453.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.minMoves(new int[] {1, 2, 3})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_454Test.java b/src/test/java/com/fishercoder/firstthousand/_454Test.java new file mode 100644 index 0000000000..c1a12e375d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_454Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._454; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _454Test { + private _454.Solution1 solution1; + private static int expected; + private static int actual; + private static int[] A; + private static int[] B; + private static int[] C; + private static int[] D; + + @BeforeEach + public void setup() { + solution1 = new _454.Solution1(); + } + + @Test + public void test1() { + A = new int[] {1, 2}; + B = new int[] {-2, -1}; + C = new int[] {-1, 2}; + D = new int[] {0, 2}; + expected = 2; + actual = solution1.fourSumCount(A, B, C, D); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_455Test.java b/src/test/java/com/fishercoder/firstthousand/_455Test.java new file mode 100644 index 0000000000..4b6a58ced0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_455Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._455; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _455Test { + private _455.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _455.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.findContentChildren(new int[] {1, 2, 3}, new int[] {1, 1})); + } + + @Test + public void test2() { + assertEquals(2, solution1.findContentChildren(new int[] {1, 2}, new int[] {1, 2, 3})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_456Test.java b/src/test/java/com/fishercoder/firstthousand/_456Test.java new file mode 100644 index 0000000000..8d1a8a3a85 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_456Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._456; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _456Test { + private _456.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _456.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.find132pattern(new int[] {-1, 3, 2, 0})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_457Test.java b/src/test/java/com/fishercoder/firstthousand/_457Test.java new file mode 100644 index 0000000000..01e549836e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_457Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._457; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _457Test { + private _457.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _457.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, -1, 1, 2, 2}; + assertEquals(true, solution1.circularArrayLoop(nums)); + } + + @Test + public void test2() { + nums = new int[] {-1, 2}; + assertEquals(false, solution1.circularArrayLoop(nums)); + } + + @Test + public void test3() { + nums = new int[] {-1, 2, 3}; + assertEquals(false, solution1.circularArrayLoop(nums)); + } + + @Test + public void test4() { + nums = new int[] {2, 1, 9}; + assertEquals(false, solution1.circularArrayLoop(nums)); + } +} diff --git a/src/test/java/com/fishercoder/_458Test.java b/src/test/java/com/fishercoder/firstthousand/_458Test.java similarity index 77% rename from src/test/java/com/fishercoder/_458Test.java rename to src/test/java/com/fishercoder/firstthousand/_458Test.java index 6f521c305f..48ca6d8fe8 100644 --- a/src/test/java/com/fishercoder/_458Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_458Test.java @@ -1,26 +1,25 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._458; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static junit.framework.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._458; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _458Test { - private static _458.Solution1 solution1; + private _458.Solution1 solution1; private static int expected; private static int actual; private static int buckets; private static int minutesToDie; private static int minutesToTest; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _458.Solution1(); } - @Before + @BeforeEach public void setupForEachTest() { expected = 0; actual = 0; diff --git a/src/test/java/com/fishercoder/firstthousand/_459Test.java b/src/test/java/com/fishercoder/firstthousand/_459Test.java new file mode 100644 index 0000000000..c6a6dfb8dd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_459Test.java @@ -0,0 +1,78 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._459; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _459Test { + private _459.Solution1 solution1; + private _459.Solution2 solution2; + private _459.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _459.Solution1(); + solution2 = new _459.Solution2(); + solution3 = new _459.Solution3(); + } + + @Test + public void test1() { + assertEquals(true, solution1.repeatedSubstringPattern("abab")); + assertEquals(true, solution2.repeatedSubstringPattern("abab")); + assertEquals(true, solution3.repeatedSubstringPattern("abab")); + } + + @Test + public void test2() { + assertEquals(false, solution1.repeatedSubstringPattern("aba")); + assertEquals(false, solution2.repeatedSubstringPattern("aba")); + assertEquals(false, solution3.repeatedSubstringPattern("aba")); + } + + @Test + public void test3() { + assertEquals(false, solution1.repeatedSubstringPattern("a")); + assertEquals(false, solution2.repeatedSubstringPattern("a")); + assertEquals(false, solution3.repeatedSubstringPattern("a")); + } + + @Test + public void test4() { + assertEquals( + false, + solution1.repeatedSubstringPattern( + "qtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvbus")); + assertEquals( + false, + solution2.repeatedSubstringPattern( + "qtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvbus")); + assertEquals( + false, + solution3.repeatedSubstringPattern( + "qtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvburqtpddbuotbbqcwivrfxjujjddntgeiqvdgaijvwcyaubwewpjvygehljxepbpiwuqzdzubdubzvafspqpqwuzifwovyddwyvvbus")); + } + + @Test + public void test5() { + assertEquals(true, solution1.repeatedSubstringPattern("ababab")); + assertEquals(true, solution2.repeatedSubstringPattern("ababab")); + assertEquals(true, solution3.repeatedSubstringPattern("ababab")); + } + + @Test + public void test6() { + assertEquals(false, solution1.repeatedSubstringPattern("abababc")); + assertEquals(false, solution2.repeatedSubstringPattern("abababc")); + assertEquals(false, solution3.repeatedSubstringPattern("abababc")); + } + + @Test + public void test7() { + assertEquals(false, solution1.repeatedSubstringPattern("abababaaba")); + assertEquals(false, solution2.repeatedSubstringPattern("abababaaba")); + assertEquals(false, solution3.repeatedSubstringPattern("abababaaba")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_45Test.java b/src/test/java/com/fishercoder/firstthousand/_45Test.java new file mode 100644 index 0000000000..da234fcdfb --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_45Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._45; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _45Test { + private _45.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _45.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 3, 1, 1, 4}; + assertEquals(2, solution1.jump(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_460Test.java b/src/test/java/com/fishercoder/firstthousand/_460Test.java new file mode 100644 index 0000000000..2fcc36309e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_460Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._460; +import org.junit.jupiter.api.Test; + +public class _460Test { + + private _460.Solution1.LFUCache lfuCache; + + @Test + public void test1() { + lfuCache = new _460.Solution1.LFUCache(2); + lfuCache.put(1, 1); + lfuCache.put(2, 2); + assertEquals(1, lfuCache.get(1)); + lfuCache.put(3, 3); + assertEquals(-1, lfuCache.get(2)); + assertEquals(3, lfuCache.get(3)); + lfuCache.put(4, 4); + assertEquals(-1, lfuCache.get(1)); + assertEquals(3, lfuCache.get(3)); + assertEquals(4, lfuCache.get(4)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_461Test.java b/src/test/java/com/fishercoder/firstthousand/_461Test.java new file mode 100644 index 0000000000..65a77696c0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_461Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._461; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _461Test { + private _461.Solution1 solution1; + private _461.Solution2 solution2; + private _461.Solution3 solution3; + private static int x; + private static int y; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _461.Solution1(); + solution2 = new _461.Solution2(); + solution3 = new _461.Solution3(); + } + + @Test + public void test1() { + x = 1; + y = 4; + expected = 2; + assertEquals(expected, solution1.hammingDistance(x, y)); + assertEquals(expected, solution2.hammingDistance(x, y)); + assertEquals(expected, solution3.hammingDistance(x, y)); + } + + @Test + public void test2() { + x = 3; + y = 1; + expected = 1; + assertEquals(expected, solution1.hammingDistance(x, y)); + assertEquals(expected, solution2.hammingDistance(x, y)); + assertEquals(expected, solution3.hammingDistance(x, y)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_462Test.java b/src/test/java/com/fishercoder/firstthousand/_462Test.java new file mode 100644 index 0000000000..d4725baaf5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_462Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._462; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _462Test { + private _462.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _462.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minMoves2(new int[] {1, 2, 3})); + } + + @Test + public void test2() { + assertEquals(0, solution1.minMoves2(new int[] {1})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_467Test.java b/src/test/java/com/fishercoder/firstthousand/_467Test.java new file mode 100644 index 0000000000..80679c1c90 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_467Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._467; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _467Test { + private _467.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _467.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.findSubstringInWraproundString("a")); + } +} diff --git a/src/test/java/com/fishercoder/_468Test.java b/src/test/java/com/fishercoder/firstthousand/_468Test.java similarity index 79% rename from src/test/java/com/fishercoder/_468Test.java rename to src/test/java/com/fishercoder/firstthousand/_468Test.java index 676eb72cbb..49f609d838 100644 --- a/src/test/java/com/fishercoder/_468Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_468Test.java @@ -1,16 +1,16 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._468; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._468; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _468Test { - private static _468.Solution1 solution1; + private _468.Solution1 solution1; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _468.Solution1(); } @@ -31,7 +31,8 @@ public void test3() { @Test public void test4() { - assertEquals("Neither", solution1.validIPAddress("02001:0db8:85a3:0000:0000:8a2e:0370:7334")); + assertEquals( + "Neither", solution1.validIPAddress("02001:0db8:85a3:0000:0000:8a2e:0370:7334")); } @Test @@ -78,5 +79,4 @@ public void test12() { public void test13() { assertEquals("Neither", solution1.validIPAddress("1081:db8:85a3:01:z:8A2E:0370:7334")); } - } diff --git a/src/test/java/com/fishercoder/firstthousand/_46Test.java b/src/test/java/com/fishercoder/firstthousand/_46Test.java new file mode 100644 index 0000000000..7148c2aa71 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_46Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._46; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _46Test { + private _46.Solution1 solution1; + private _46.Solution2 solution2; + private _46.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _46.Solution1(); + solution2 = new _46.Solution2(); + solution3 = new _46.Solution3(); + } + + @Test + public void test1() { + CommonUtils.printListList(solution1.permute(new int[] {1, 2, 3})); + CommonUtils.printListList(solution2.permute(new int[] {1, 2, 3})); + CommonUtils.printListList(solution3.permute(new int[] {1, 2, 3})); + } + + @Test + public void test2() { + CommonUtils.printListList(solution1.permute(new int[] {1, 2, 3, 4, 5, 6})); + CommonUtils.printListList(solution2.permute(new int[] {1, 2, 3, 4, 5, 6})); + CommonUtils.printListList(solution3.permute(new int[] {1, 2, 3, 4, 5, 6})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_473Test.java b/src/test/java/com/fishercoder/firstthousand/_473Test.java new file mode 100644 index 0000000000..b7df76bc45 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_473Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._473; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _473Test { + private _473.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _473.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 1, 2, 2, 2}; + assertEquals(true, solution1.makesquare(nums)); + } + + @Test + public void test2() { + nums = new int[] {3, 3, 3, 3, 4}; + assertEquals(false, solution1.makesquare(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_474Test.java b/src/test/java/com/fishercoder/firstthousand/_474Test.java new file mode 100644 index 0000000000..fdcad6f4d3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_474Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._474; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _474Test { + private _474.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _474.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 4, solution1.findMaxForm(new String[] {"10", "0001", "111001", "1", "0"}, 5, 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_475Test.java b/src/test/java/com/fishercoder/firstthousand/_475Test.java new file mode 100644 index 0000000000..42d0ea415a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_475Test.java @@ -0,0 +1,57 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._475; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 4/23/17. */ +public class _475Test { + private _475.Solution1 test; + private static int expected; + private static int actual; + private static int[] houses; + private static int[] heaters; + + @BeforeEach + public void setup() { + test = new _475.Solution1(); + } + + @Test + public void test1() { + houses = new int[] {1, 2, 3}; + heaters = new int[] {2}; + expected = 1; + actual = test.findRadius(houses, heaters); + assertEquals(expected, actual); + } + + @Test + public void test2() { + houses = new int[] {1, 2, 3, 4}; + heaters = new int[] {1, 4}; + expected = 1; + actual = test.findRadius(houses, heaters); + assertEquals(expected, actual); + } + + @Test + public void test3() { + houses = new int[] {1}; + heaters = new int[] {1, 2, 3, 4}; + expected = 0; + actual = test.findRadius(houses, heaters); + assertEquals(expected, actual); + } + + @Test + public void test4() { + houses = new int[] {1, 2, 3, 5, 15}; + heaters = new int[] {2, 30}; + expected = 13; + actual = test.findRadius(houses, heaters); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_476Test.java b/src/test/java/com/fishercoder/firstthousand/_476Test.java new file mode 100644 index 0000000000..78c31ad38a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_476Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._476; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/14/17. */ +public class _476Test { + private _476.Solution1 solution1; + private _476.Solution2 solution2; + private static int expected; + private static int actual; + private static int input; + + @BeforeEach + public void setup() { + solution1 = new _476.Solution1(); + solution2 = new _476.Solution2(); + } + + @BeforeEach + public void setupForEachTest() { + expected = 0; + actual = 0; + input = 0; + } + + @Test + public void test1() { + + input = 5; + expected = 2; + actual = solution1.findComplement(input); + actual = solution2.findComplement(input); + assertEquals(expected, actual); + } + + @Test + public void test2() { + + input = 5; + expected = 2; + actual = solution1.findComplement(input); + actual = solution2.findComplement(input); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_478Test.java b/src/test/java/com/fishercoder/firstthousand/_478Test.java new file mode 100644 index 0000000000..b925782524 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_478Test.java @@ -0,0 +1,15 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._478; +import org.junit.jupiter.api.Test; + +public class _478Test { + private _478.Solution1 solution1; + + @Test + public void test1() { + solution1 = new _478.Solution1(10.0, 5.0, -7.5); + CommonUtils.printArray(solution1.randPoint()); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_479Test.java b/src/test/java/com/fishercoder/firstthousand/_479Test.java new file mode 100644 index 0000000000..cbed893142 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_479Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._479; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _479Test { + private _479.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _479.Solution1(); + } + + @Test + public void test1() { + assertEquals(123, solution1.largestPalindrome(3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_47Test.java b/src/test/java/com/fishercoder/firstthousand/_47Test.java new file mode 100644 index 0000000000..02da863165 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_47Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._47; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _47Test { + private _47.Solution1 solution1; + private _47.Solution2 solution2; + private static List> actual; + + @BeforeEach + public void setup() { + solution1 = new _47.Solution1(); + solution2 = new _47.Solution2(); + } + + @Test + public void test1() { + actual = solution1.permuteUnique(new int[] {1, 1, 2}); + CommonUtils.printListList(actual); + actual = solution2.permuteUnique(new int[] {1, 1, 2}); + CommonUtils.printListList(actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_480Test.java b/src/test/java/com/fishercoder/firstthousand/_480Test.java new file mode 100644 index 0000000000..4cc3d1235c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_480Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._480; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/27/17. */ +public class _480Test { + private _480.Solution1 solution1; + private static int[] nums; + private static double[] expected; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _480.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 3, -1, -3, 5, 3, 6, 7}; + expected = new double[] {1, -1, -1, 3, 5, 6}; + k = 3; + assertArrayEquals(expected, solution1.medianSlidingWindow(nums, k), 0); + } +} diff --git a/src/test/java/com/fishercoder/_482Test.java b/src/test/java/com/fishercoder/firstthousand/_482Test.java similarity index 77% rename from src/test/java/com/fishercoder/_482Test.java rename to src/test/java/com/fishercoder/firstthousand/_482Test.java index a5fd6d7c69..c25838e0bf 100644 --- a/src/test/java/com/fishercoder/_482Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_482Test.java @@ -1,25 +1,24 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._482; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static junit.framework.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._482; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _482Test { - private static _482.Solution1 solution1; + private _482.Solution1 solution1; private static String expected; private static String actual; private static String S; private static int k; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _482.Solution1(); } - @Before + @BeforeEach public void setupForEachTest() { expected = ""; actual = ""; diff --git a/src/test/java/com/fishercoder/firstthousand/_485Test.java b/src/test/java/com/fishercoder/firstthousand/_485Test.java new file mode 100644 index 0000000000..0efab86396 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_485Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._485; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _485Test { + private _485.Solution1 solution1; + private _485.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _485.Solution1(); + solution2 = new _485.Solution2(); + } + + @Test + public void test1() { + assertEquals(3, solution1.findMaxConsecutiveOnes(new int[] {1, 1, 0, 1, 1, 1})); + assertEquals(3, solution2.findMaxConsecutiveOnes(new int[] {1, 1, 0, 1, 1, 1})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_487Test.java b/src/test/java/com/fishercoder/firstthousand/_487Test.java new file mode 100644 index 0000000000..80711ab672 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_487Test.java @@ -0,0 +1,432 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._487; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _487Test { + private _487.Solution1 soution1; + private _487.Solution2 soution2; + private static int[] nums; + private static int expected; + private static int actual; + + @BeforeEach + public void setup() { + soution1 = new _487.Solution1(); + soution2 = new _487.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 1, 0, 1, 1, 1}; + expected = 6; + actual = soution1.findMaxConsecutiveOnes(nums); + assertEquals(expected, actual); + actual = soution2.findMaxConsecutiveOnes(nums); + assertEquals(expected, actual); + } + + @Test + public void test2() { + nums = new int[] {1, 1, 1, 1, 1, 1}; + expected = 6; + assertEquals(expected, soution1.findMaxConsecutiveOnes(nums)); + } + + @Test + public void test3() { + nums = new int[] {}; + expected = 0; + assertEquals(expected, soution1.findMaxConsecutiveOnes(nums)); + } + + @Test + public void test4() { + nums = new int[] {1, 0, 0, 0, 1, 1, 0}; + expected = 3; + assertEquals(expected, soution1.findMaxConsecutiveOnes(nums)); + } + + @Test + public void test5() { + nums = + new int[] { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }; + expected = 9621; + assertEquals(expected, soution1.findMaxConsecutiveOnes(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_48Test.java b/src/test/java/com/fishercoder/firstthousand/_48Test.java new file mode 100644 index 0000000000..dce9612f49 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_48Test.java @@ -0,0 +1,95 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._48; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _48Test { + private _48.Solution1 solution1; + private _48.Solution2 solution2; + private _48.Solution3 solution3; + private static int[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _48.Solution1(); + solution2 = new _48.Solution2(); + solution3 = new _48.Solution3(); + } + + @Test + public void test1() { + matrix = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + }; + solution1.rotate(matrix); + CommonUtils.print2DIntArray(matrix); + } + + @Test + public void test2() { + matrix = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + }; + solution2.rotate(matrix); + CommonUtils.print2DIntArray(matrix); + } + + @Test + public void test3() { + matrix = + new int[][] { + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12}, + {13, 14, 15, 16} + }; + solution2.rotate(matrix); + CommonUtils.print2DIntArray(matrix); + } + + @Test + public void test4() { + matrix = + new int[][] { + {1, 2}, + {3, 4} + }; + solution1.rotate(matrix); + CommonUtils.print2DIntArray(matrix); + } + + @Test + public void test5() { + matrix = + new int[][] { + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12}, + {13, 14, 15, 16} + }; + CommonUtils.print2DIntArray(matrix); + solution3.rotate(matrix); + CommonUtils.print2DIntArray(matrix); + } + + @Test + public void test6() { + matrix = + new int[][] { + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12}, + {13, 14, 15, 16} + }; + solution1.rotate(matrix); + CommonUtils.print2DIntArray(matrix); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_490Test.java b/src/test/java/com/fishercoder/firstthousand/_490Test.java new file mode 100644 index 0000000000..edac25fedf --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_490Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._490; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _490Test { + private _490 test; + private static boolean expected; + private static boolean actual; + private static int[][] maze; + private static int[] start; + private static int[] destination; + + @BeforeEach + public void setup() { + test = new _490(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + maze = + new int[][] { + {0, 0, 0, 0, 0}, + {1, 1, 0, 0, 1}, + {0, 0, 0, 0, 0}, + {0, 1, 0, 0, 1}, + {0, 1, 0, 0, 0} + }; + start = new int[] {4, 3}; + destination = new int[] {0, 1}; + actual = test.hasPath(maze, start, destination); + expected = false; + assertEquals(expected, actual); + } + + @Test + public void test2() { + maze = + new int[][] { + {0, 0, 1, 0, 0}, + {0, 0, 0, 0, 0}, + {0, 0, 0, 1, 0}, + {1, 1, 0, 1, 1}, + {0, 0, 0, 0, 0} + }; + start = new int[] {0, 4}; + destination = new int[] {4, 4}; + actual = test.hasPath(maze, start, destination); + expected = true; + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_491Test.java b/src/test/java/com/fishercoder/firstthousand/_491Test.java new file mode 100644 index 0000000000..3fdd5d782a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_491Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._491; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _491Test { + private _491.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setUp() { + solution1 = new _491.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {4, 6, 7, 7}; + List> actual = solution1.findSubsequences(nums); + CommonUtils.printListList(actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_492Test.java b/src/test/java/com/fishercoder/firstthousand/_492Test.java new file mode 100644 index 0000000000..4b5a1f9d24 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_492Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._492; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/25/17. */ +public class _492Test { + private _492.Solution1 solution1; + private static int[] expected; + private static int[] actual; + private static int area; + + @BeforeEach + public void setup() { + solution1 = new _492.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = new int[] {0, 0}; + actual = new int[] {0, 0}; + area = 0; + } + + @Test + public void test1() { + area = 4; + expected = new int[] {2, 2}; + actual = solution1.constructRectangle(area); + assertArrayEquals(expected, actual); + } + + @Test + public void test2() { + area = 3; + expected = new int[] {3, 1}; + actual = solution1.constructRectangle(area); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_493Test.java b/src/test/java/com/fishercoder/firstthousand/_493Test.java new file mode 100644 index 0000000000..cefd291db9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_493Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._493; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _493Test { + private _493.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _493.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 3, 2, 3, 1}; + assertEquals(2, solution1.reversePairs(nums)); + } + + @Test + public void test2() { + nums = new int[] {2, 4, 3, 5, 1}; + assertEquals(3, solution1.reversePairs(nums)); + } + + @Test + public void test3() { + nums = new int[] {2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647}; + assertEquals(0, solution1.reversePairs(nums)); + } + + @Test + public void test4() { + nums = new int[] {1, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647}; + assertEquals(0, solution1.reversePairs(nums)); + } + + @Test + public void test5() { + nums = new int[] {2147483647, 2147483646, 2147483647, 2147483647, 2147483647}; + assertEquals(0, solution1.reversePairs(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_494Test.java b/src/test/java/com/fishercoder/firstthousand/_494Test.java new file mode 100644 index 0000000000..e0895d8257 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_494Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._494; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _494Test { + private _494.Solution1 solution1; + private static int expected; + private static int actual; + private static int S; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _494.Solution1(); + } + + @Test + public void test1() { + S = 3; + nums = new int[] {1, 1, 1, 1, 1}; + expected = 5; + actual = solution1.findTargetSumWays(nums, S); + assertEquals(expected, actual); + } + + @Test + public void test2() { + S = 3; + nums = new int[] {1, 1, 1, 1, 5}; + expected = 4; + actual = solution1.findTargetSumWays(nums, S); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_495Test.java b/src/test/java/com/fishercoder/firstthousand/_495Test.java new file mode 100644 index 0000000000..dc281c8163 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_495Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._495; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/8/17. */ +public class _495Test { + _495.Solution1 solution1 = new _495.Solution1(); + private static int actual = 0; + private static int expected = 0; + private static int[] timeSeries; + private static int duration = 0; + + @BeforeEach + public void setup() { + timeSeries = new int[] {}; + duration = 0; + expected = 0; + actual = 0; + } + + @Test + public void test1() { + timeSeries = new int[] {1, 4}; + duration = 2; + actual = solution1.findPoisonedDuration(timeSeries, duration); + expected = 4; + assertEquals(expected, actual); + } + + @Test + public void test2() { + timeSeries = new int[] {1, 2}; + duration = 2; + actual = solution1.findPoisonedDuration(timeSeries, duration); + expected = 3; + assertEquals(expected, actual); + } + + @Test + public void test3() { + timeSeries = new int[] {}; + duration = 100000; + actual = solution1.findPoisonedDuration(timeSeries, duration); + expected = 0; + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_496Test.java b/src/test/java/com/fishercoder/firstthousand/_496Test.java new file mode 100644 index 0000000000..ef46577443 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_496Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._496; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _496Test { + private _496.Solution1 solution1; + private _496.Solution2 solution2; + private static int[] findNums; + private static int[] nums; + private static int[] expected; + private static int[] actual; + + @BeforeEach + public void setup() { + solution1 = new _496.Solution1(); + solution2 = new _496.Solution2(); + } + + @BeforeEach + public void setupForEachTest() { + expected = new int[] {}; + actual = new int[] {}; + findNums = new int[] {}; + nums = new int[] {}; + } + + @Test + public void test1() { + findNums = new int[] {4, 1, 2}; + nums = new int[] {1, 3, 4, 2}; + expected = new int[] {-1, 3, -1}; + actual = solution1.nextGreaterElement(findNums, nums); + assertArrayEquals(expected, actual); + actual = solution2.nextGreaterElement(findNums, nums); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_498Test.java b/src/test/java/com/fishercoder/firstthousand/_498Test.java new file mode 100644 index 0000000000..764224c17d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_498Test.java @@ -0,0 +1,80 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._498; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/26/17. */ +public class _498Test { + private _498.Solutoin1 solutoin1; + private _498.Solutoin2 solutoin2; + private static int[][] matrix; + private static int[] expected; + + @BeforeEach + public void setup() { + solutoin1 = new _498.Solutoin1(); + solutoin2 = new _498.Solutoin2(); + } + + @Test + public void test1() { + matrix = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + }; + expected = new int[] {1, 2, 4, 7, 5, 3, 6, 8, 9}; + assertArrayEquals(expected, solutoin1.findDiagonalOrder(matrix)); + } + + @Test + public void test2() { + matrix = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + {10, 11, 12}, + {13, 14, 15}, + }; + expected = new int[] {1, 2, 4, 7, 5, 3, 6, 8, 10, 13, 11, 9, 12, 14, 15}; + assertArrayEquals(expected, solutoin1.findDiagonalOrder(matrix)); + } + + @Test + public void test3() { + matrix = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + }; + expected = new int[] {1, 2, 4, 7, 5, 3, 6, 8, 9}; + assertArrayEquals(expected, solutoin2.findDiagonalOrder(matrix)); + } + + @Test + public void test4() { + matrix = new int[][] {{2, 5}, {8, 4}, {0, -1}}; + expected = new int[] {2, 5, 8, 0, 4, -1}; + assertArrayEquals(expected, solutoin2.findDiagonalOrder(matrix)); + } + + @Test + public void test5() { + matrix = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + {10, 11, 12}, + {13, 14, 15}, + }; + expected = new int[] {1, 2, 4, 7, 5, 3, 6, 8, 10, 13, 11, 9, 12, 14, 15}; + assertArrayEquals(expected, solutoin2.findDiagonalOrder(matrix)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_49Test.java b/src/test/java/com/fishercoder/firstthousand/_49Test.java new file mode 100644 index 0000000000..e983d6566d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_49Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.firstthousand._49; +import java.util.Arrays; +import java.util.List; +import org.apache.commons.collections4.CollectionUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _49Test { + private _49.Solution1 solution1; + private static String[] words; + private static List> expected; + private static List> actual; + + @BeforeEach + public void setup() { + solution1 = new _49.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"eat", "tea", "tan", "ate", "nat", "bat"}; + List e1 = Arrays.asList("bat"); + List e2 = Arrays.asList("tan", "nat"); + List e3 = Arrays.asList("ate", "eat", "tea"); + expected = Arrays.asList(e1, e2, e3); + actual = solution1.groupAnagrams(words); + assertEquals(expected.size(), actual.size()); + assertEquals(expected.containsAll(actual), actual.containsAll(expected)); + for (List a : actual) { + switch (a.size()) { + case 1: + assertTrue(CollectionUtils.isEqualCollection(e1, a)); + break; + case 2: + assertTrue(CollectionUtils.isEqualCollection(e2, a)); + break; + case 3: + assertTrue(CollectionUtils.isEqualCollection(e3, a)); + break; + default: + // Should not have come into this branch ever. + assertTrue(false); + } + } + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_4Test.java b/src/test/java/com/fishercoder/firstthousand/_4Test.java new file mode 100644 index 0000000000..5e7b3321cf --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_4Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._4; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _4Test { + private _4.Solution1 solution1; + private _4.Solution2 solution2; + private static int[] A; + private static int[] B; + + @BeforeEach + public void setup() { + solution1 = new _4.Solution1(); + solution2 = new _4.Solution2(); + } + + @Test + public void test1() { + A = new int[] {1, 3}; + B = new int[] {2}; + assertEquals(2.0, solution1.findMedianSortedArrays(A, B), 0.0); + assertEquals(2.0, solution2.findMedianSortedArrays(A, B), 0.0); + } + + @Test + public void test2() { + A = new int[] {1, 2}; + B = new int[] {3, 4}; + assertEquals(2.5, solution1.findMedianSortedArrays(A, B), 0.0); + assertEquals(2.5, solution2.findMedianSortedArrays(A, B), 0.0); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_500Test.java b/src/test/java/com/fishercoder/firstthousand/_500Test.java new file mode 100644 index 0000000000..08ee029a0a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_500Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._500; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/15/17. */ +public class _500Test { + private _500 test; + private static String[] expected; + private static String[] actual; + private String[] words; + + @BeforeEach + public void setup() { + test = new _500(); + } + + @Test + public void test1() { + words = new String[] {"Alaska", "Hello", "Dad", "Peace"}; + expected = new String[] {"Alaska", "Dad"}; + actual = test.findWords(words); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_501Test.java b/src/test/java/com/fishercoder/firstthousand/_501Test.java new file mode 100644 index 0000000000..6c70f2320e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_501Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._501; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/28/17. */ +public class _501Test { + private _501.Solution1 solution1; + private _501.Solution2 solution2; + private static int[] expected; + private static int[] actual; + private static TreeNode treeNode; + + @BeforeEach + public void setup() { + solution1 = new _501.Solution1(); + solution2 = new _501.Solution2(); + } + + @BeforeEach + public void setupForEachTest() { + expected = new int[] {}; + actual = new int[] {}; + treeNode = new TreeNode(0); + } + + @Test + public void test1() { + treeNode = new TreeNode(1); + treeNode.right = new TreeNode(2); + treeNode.right.left = new TreeNode(2); + expected = new int[] {2}; + CommonUtils.printArray(expected); + CommonUtils.printArray(actual); + actual = solution1.findMode(treeNode); + assertArrayEquals(expected, actual); + + actual = solution2.findMode(treeNode); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_502Test.java b/src/test/java/com/fishercoder/firstthousand/_502Test.java new file mode 100644 index 0000000000..61431cb639 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_502Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._502; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _502Test { + private _502.Solution1 solution1; + private static int[] Profits; + private static int[] Capital; + + @BeforeEach + public void setup() { + solution1 = new _502.Solution1(); + } + + @Test + public void test1() { + Profits = new int[] {1, 2, 3}; + Capital = new int[] {0, 1, 1}; + assertEquals(4, solution1.findMaximizedCapital(2, 0, Profits, Capital)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_503Test.java b/src/test/java/com/fishercoder/firstthousand/_503Test.java new file mode 100644 index 0000000000..099e5f2cca --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_503Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._503; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _503Test { + private _503.Solution1 solution1; + private _503.Solution2 solution2; + private static int[] nums; + private static int[] expected; + private static int[] actual; + + @BeforeEach + public void setup() { + solution1 = new _503.Solution1(); + solution2 = new _503.Solution2(); + } + + @BeforeEach + public void setupForEachTest() { + expected = new int[] {}; + nums = new int[] {}; + } + + @Test + public void test1() { + nums = new int[] {1, 2, 1}; + expected = new int[] {2, -1, 2}; + actual = solution1.nextGreaterElements(nums); + assertArrayEquals(expected, actual); + + actual = solution2.nextGreaterElements(nums); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_504Test.java b/src/test/java/com/fishercoder/firstthousand/_504Test.java new file mode 100644 index 0000000000..8920f94133 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_504Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._504; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/15/17. */ +public class _504Test { + private _504.Solution1 solution1; + private static String expected; + private static String actual; + private static int num; + + @BeforeEach + public void setup() { + solution1 = new _504.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = ""; + actual = ""; + num = 0; + } + + @Test + public void test1() { + num = 100; + expected = "202"; + actual = solution1.convertToBase7(num); + assertEquals(expected, actual); + } + + @Test + public void test2() { + num = -7; + expected = "-10"; + actual = solution1.convertToBase7(num); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_505Test.java b/src/test/java/com/fishercoder/firstthousand/_505Test.java new file mode 100644 index 0000000000..49f1edc9f3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_505Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._505; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _505Test { + private _505.Solution1 solution1; + private static int expected; + private static int actual; + private static int[][] maze; + private static int[] start; + private static int[] destination; + + @BeforeEach + public void setup() { + solution1 = new _505.Solution1(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + maze = + new int[][] { + {0, 0, 0, 0, 0}, + {1, 1, 0, 0, 1}, + {0, 0, 0, 0, 0}, + {0, 1, 0, 0, 1}, + {0, 1, 0, 0, 0} + }; + start = new int[] {4, 3}; + destination = new int[] {0, 1}; + actual = solution1.shortestDistance(maze, start, destination); + expected = -1; + assertEquals(expected, actual); + } + + @Test + public void test2() { + maze = + new int[][] { + {0, 0, 1, 0, 0}, + {0, 0, 0, 0, 0}, + {0, 0, 0, 1, 0}, + {1, 1, 0, 1, 1}, + {0, 0, 0, 0, 0} + }; + start = new int[] {0, 4}; + destination = new int[] {4, 4}; + actual = solution1.shortestDistance(maze, start, destination); + expected = 12; + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_506Test.java b/src/test/java/com/fishercoder/firstthousand/_506Test.java new file mode 100644 index 0000000000..63be585537 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_506Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._506; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/15/17. */ +public class _506Test { + private _506.Solution1 solution1; + private static String[] expected; + private static String[] actual; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _506.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = new String[] {}; + actual = new String[] {}; + } + + @Test + public void test1() { + nums = new int[] {2, 4, 1}; + expected = new String[] {"Silver Medal", "Gold Medal", "Bronze Medal"}; + actual = solution1.findRelativeRanks(nums); + assertArrayEquals(expected, actual); + } + + @Test + public void test2() { + nums = new int[] {5, 4, 3, 2, 1}; + expected = new String[] {"Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"}; + actual = solution1.findRelativeRanks(nums); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_507Test.java b/src/test/java/com/fishercoder/firstthousand/_507Test.java new file mode 100644 index 0000000000..1636c9b59f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_507Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._507; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/25/17. */ +public class _507Test { + private _507.Solution1 solution1; + private static boolean expected; + private static boolean actual; + private static int num; + + @BeforeEach + public void setup() { + solution1 = new _507.Solution1(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + expected = true; + num = 28; + actual = solution1.checkPerfectNumber(num); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_508Test.java b/src/test/java/com/fishercoder/firstthousand/_508Test.java new file mode 100644 index 0000000000..b841774220 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_508Test.java @@ -0,0 +1,76 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._508; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _508Test { + private _508.Solution1 solution1; + private _508.Solution2 solution2; + private _508.Solution3 solution3; + private static int[] expected; + private static int[] actual; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _508.Solution1(); + solution2 = new _508.Solution2(); + solution3 = new _508.Solution3(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(5, 2, -3)); + expected = new int[] {2, -3, 4}; + /** Since order does NOT matter, so I'll sort them and then compare */ + Arrays.sort(expected); + actual = solution1.findFrequentTreeSum(root); + Arrays.sort(actual); + assertArrayEquals(expected, actual); + + actual = solution2.findFrequentTreeSum(root); + Arrays.sort(actual); + assertArrayEquals(expected, actual); + + actual = solution3.findFrequentTreeSum(root); + Arrays.sort(actual); + assertArrayEquals(expected, actual); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(5, 2, -5)); + expected = new int[] {2}; + actual = solution1.findFrequentTreeSum(root); + assertArrayEquals(expected, actual); + + actual = solution2.findFrequentTreeSum(root); + assertArrayEquals(expected, actual); + + actual = solution3.findFrequentTreeSum(root); + assertArrayEquals(expected, actual); + } + + @Test + public void test3() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList(3, 1, 5, 0, 2, 4, 6, null, null, null, 3)); + TreeUtils.printBinaryTree(root); + expected = new int[] {6}; + actual = solution1.findFrequentTreeSum(root); + assertArrayEquals(expected, actual); + + actual = solution2.findFrequentTreeSum(root); + assertArrayEquals(expected, actual); + + actual = solution3.findFrequentTreeSum(root); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_509Test.java b/src/test/java/com/fishercoder/firstthousand/_509Test.java new file mode 100644 index 0000000000..3d54cc06cd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_509Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._509; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _509Test { + private _509.Solution1 solution1; + private _509.Solution2 solution2; + private _509.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _509.Solution1(); + solution2 = new _509.Solution2(); + solution3 = new _509.Solution3(); + } + + @Test + public void test1() { + assertEquals(1, solution1.fib(2)); + assertEquals(1, solution2.fib(2)); + assertEquals(1, solution3.fib(2)); + } + + @Test + public void test2() { + assertEquals(2, solution1.fib(3)); + assertEquals(2, solution2.fib(3)); + assertEquals(2, solution3.fib(3)); + } + + @Test + public void test3() { + assertEquals(3, solution1.fib(4)); + assertEquals(3, solution2.fib(4)); + assertEquals(3, solution3.fib(4)); + } + + @Test + public void test4() { + assertEquals(0, solution1.fib(0)); + assertEquals(0, solution2.fib(0)); + assertEquals(0, solution3.fib(0)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_50Test.java b/src/test/java/com/fishercoder/firstthousand/_50Test.java new file mode 100644 index 0000000000..e644c5f4c0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_50Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._50; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _50Test { + private _50.Solution1 solution1; + private _50.Solution2 solution2; + private _50.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _50.Solution1(); + solution2 = new _50.Solution2(); + solution3 = new _50.Solution3(); + } + + @Test + public void test1() { + assertEquals(1024.00000, solution1.myPow(2.00000, 10), 0.00001); + assertEquals(1024.00000, solution2.myPow(2.00000, 10), 0.00001); + assertEquals(1024.00000, solution3.myPow(2.00000, 10), 0.00001); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_513Test.java b/src/test/java/com/fishercoder/firstthousand/_513Test.java new file mode 100644 index 0000000000..0080b85ed3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_513Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.solutions.firstthousand._513; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/15/17. */ +public class _513Test { + private _513.Solution1 solution1; + private static int expected; + private static int actual; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _513.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = 0; + actual = 0; + root = new TreeNode(0); + } + + @Test + public void test1() { + TreeNode root = new TreeNode(2); + root.left = new TreeNode(1); + root.right = new TreeNode(3); + expected = 1; + actual = solution1.findBottomLeftValue(root); + assertEquals(expected, actual); + } + + @Test + public void test2() { + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + root.left.left = new TreeNode(4); + root.right.left = new TreeNode(5); + root.right.right = new TreeNode(6); + root.right.left.left = new TreeNode(7); + expected = 7; + actual = solution1.findBottomLeftValue(root); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_515Test.java b/src/test/java/com/fishercoder/firstthousand/_515Test.java new file mode 100644 index 0000000000..a770ecbfa6 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_515Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.solutions.firstthousand._515; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _515Test { + private _515.Solution1 solution1; + private _515.Solution2 solution2; + private static List expected; + private static List actual; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _515.Solution1(); + solution2 = new _515.Solution2(); + } + + @Test + public void test1() { + TreeNode root = new TreeNode(1); + root.left = new TreeNode(3); + root.right = new TreeNode(2); + expected = Arrays.asList(1, 3); + actual = solution1.largestValues(root); + assertEquals(expected, actual); + + actual = solution2.largestValues(root); + assertEquals(expected, actual); + } + + @Test + public void test2() { + expected = new ArrayList<>(); + actual = solution1.largestValues(null); + assertEquals(expected, actual); + + actual = solution2.largestValues(null); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_516Test.java b/src/test/java/com/fishercoder/firstthousand/_516Test.java new file mode 100644 index 0000000000..3e8077a123 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_516Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._516; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _516Test { + private _516.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _516.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.longestPalindromeSubseq("bbbab")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_518Test.java b/src/test/java/com/fishercoder/firstthousand/_518Test.java new file mode 100644 index 0000000000..a135a9b146 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_518Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._518; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _518Test { + private _518.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _518.Solution1(); + } + + @Test + public void test1() { + int amount = 5; + int[] coins = new int[] {1, 2, 5}; + int expected = 4; + int actual = solution1.change(amount, coins); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_519Test.java b/src/test/java/com/fishercoder/firstthousand/_519Test.java new file mode 100644 index 0000000000..66397207fa --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_519Test.java @@ -0,0 +1,20 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._519; +import org.junit.jupiter.api.Test; + +public class _519Test { + + private _519.Solution solution1; + + @Test + public void test1() { + solution1 = new _519.Solution(3, 1); + CommonUtils.printArray(solution1.flip()); + CommonUtils.printArray(solution1.flip()); + CommonUtils.printArray(solution1.flip()); + solution1.reset(); + CommonUtils.printArray(solution1.flip()); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_51Test.java b/src/test/java/com/fishercoder/firstthousand/_51Test.java new file mode 100644 index 0000000000..0db8087fff --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_51Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._51; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _51Test { + private _51.Solution1 solution1; + private static List> expected; + private static List> actual; + private static int n; + + @BeforeEach + public void setup() { + solution1 = new _51.Solution1(); + expected = new ArrayList<>(); + actual = new ArrayList<>(); + } + + @Test + public void test1() { + n = 4; + expected = new ArrayList<>(); + expected.add(Arrays.asList("..Q.", "Q...", "...Q", ".Q..")); + expected.add(Arrays.asList(".Q..", "...Q", "Q...", "..Q.")); + actual = solution1.solveNQueens(n); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_522Test.java b/src/test/java/com/fishercoder/firstthousand/_522Test.java new file mode 100644 index 0000000000..fa8bb6f6a2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_522Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._522; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 12/31/16. */ +public class _522Test { + + private _522.Solution1 solution1; + private static int expected; + private static int actual; + private static String[] strs; + + @BeforeEach + public void setup() { + solution1 = new _522.Solution1(); + } + + @Test + public void test1() { + strs = new String[] {"aaa", "aaa", "aa"}; + expected = -1; + actual = solution1.findLUSlength(strs); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_523Test.java b/src/test/java/com/fishercoder/firstthousand/_523Test.java new file mode 100644 index 0000000000..115baf9bb9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_523Test.java @@ -0,0 +1,120 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._523; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _523Test { + private _523.Solution1 solution1; + private _523.Solution2 solution2; + private static boolean expected; + private static int[] nums; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _523.Solution1(); + solution2 = new _523.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {23, 2, 4, 6, 7}; + expected = true; + k = 6; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test2() { + nums = new int[] {23, 2, 6, 4, 7}; + expected = true; + k = 6; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test3() { + nums = new int[] {23, 2, 6, 4, 7}; + expected = false; + k = 0; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test4() { + nums = new int[] {0, 1, 0}; + expected = false; + k = 0; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test5() { + nums = new int[] {0, 0}; + expected = true; + k = 0; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test6() { + nums = new int[] {1, 1}; + expected = true; + k = 2; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test7() { + nums = new int[] {0}; + expected = false; + k = -1; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test8() { + nums = new int[] {23, 2, 4, 6, 7}; + expected = true; + k = -6; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test9() { + nums = new int[] {1, 2, 3}; + expected = false; + k = 4; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test10() { + nums = new int[] {5, 2, 4}; + expected = false; + k = 5; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } + + @Test + public void test11() { + nums = new int[] {23, 2, 4, 6, 6}; + expected = true; + k = 7; + assertEquals(expected, solution1.checkSubarraySum(nums, k)); + assertEquals(expected, solution2.checkSubarraySum(nums, k)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_524Test.java b/src/test/java/com/fishercoder/firstthousand/_524Test.java new file mode 100644 index 0000000000..79e4370024 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_524Test.java @@ -0,0 +1,53 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._524; +import java.util.ArrayList; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 4/30/17. */ +public class _524Test { + private _524.Solution1 solution1; + private static String expected; + private static String actual; + private static String s; + private static ArrayList d; + + @BeforeEach + public void setup() { + solution1 = new _524.Solution1(); + } + + @Test + public void test1() { + d = new ArrayList(Arrays.asList("ale", "apple", "monkey", "plea")); + s = "abpcplea"; + expected = "apple"; + actual = solution1.findLongestWord(expected, d); + assertEquals(expected, actual); + } + + @Test + public void test2() { + d = new ArrayList(Arrays.asList("a", "b", "c")); + s = "abpcplea"; + expected = "a"; + actual = solution1.findLongestWord(expected, d); + assertEquals(expected, actual); + } + + @Test + public void test3() { + d = + new ArrayList( + Arrays.asList( + "apple", "ewaf", "awefawfwaf", "awef", "awefe", "ewafeffewafewf")); + s = "aewfafwafjlwajflwajflwafj"; + expected = "ewaf"; + actual = solution1.findLongestWord(expected, d); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_525Test.java b/src/test/java/com/fishercoder/firstthousand/_525Test.java new file mode 100644 index 0000000000..271950d072 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_525Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._525; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _525Test { + private _525.Solution1 solution1; + private static int expected; + private static int actual; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _525.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {0, 1}; + expected = 2; + actual = solution1.findMaxLength(nums); + assertEquals(expected, actual); + } + + @Test + public void test2() { + nums = new int[] {0, 1, 0}; + expected = 2; + actual = solution1.findMaxLength(nums); + assertEquals(expected, actual); + } + + @Test + public void test3() { + nums = new int[] {0, 0, 1, 0, 0, 0, 1, 1}; + expected = 6; + actual = solution1.findMaxLength(nums); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_526Test.java b/src/test/java/com/fishercoder/firstthousand/_526Test.java new file mode 100644 index 0000000000..c2acb0a663 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_526Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._526; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _526Test { + private _526.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _526.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.countArrangement(2)); + } + + @Test + public void test2() { + assertEquals(3, solution1.countArrangement(3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_527Test.java b/src/test/java/com/fishercoder/firstthousand/_527Test.java new file mode 100644 index 0000000000..4bd68b9764 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_527Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._527; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _527Test { + + private _527.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _527.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList( + "l2e", + "god", + "internal", + "me", + "i6t", + "interval", + "inte4n", + "f2e", + "intr4n"), + solution1.wordsAbbreviation( + Arrays.asList( + "like", + "god", + "internal", + "me", + "internet", + "interval", + "intension", + "face", + "intrusion"))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_528Test.java b/src/test/java/com/fishercoder/firstthousand/_528Test.java new file mode 100644 index 0000000000..c5e5b1a035 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_528Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._528; +import org.junit.jupiter.api.Test; + +public class _528Test { + private _528.Solution1 solution1; + private static int expected; + + @Test + public void test1() { + solution1 = new _528.Solution1(new int[] {1}); + expected = 0; + assertEquals(expected, solution1.pickIndex()); + } + + @Test + public void test2() { + solution1 = new _528.Solution1(new int[] {1, 3}); + System.out.println(solution1.pickIndex()); + System.out.println(solution1.pickIndex()); + System.out.println(solution1.pickIndex()); + System.out.println(solution1.pickIndex()); + System.out.println(solution1.pickIndex()); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_529Test.java b/src/test/java/com/fishercoder/firstthousand/_529Test.java new file mode 100644 index 0000000000..0914ba6db3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_529Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._529; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _529Test { + private _529.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _529.Solution1(); + } + + @Test + public void test1() { + char[][] actual = + solution1.updateBoard( + new char[][] { + {'E', 'E', 'E', 'E', 'E'}, + {'E', 'E', 'M', 'E', 'E'}, + {'E', 'E', 'E', 'E', 'E'}, + {'E', 'E', 'E', 'E', 'E'}, + }, + new int[] {3, 0}); + char[][] expected = + new char[][] { + {'B', '1', 'E', '1', 'B'}, + {'B', '1', 'M', '1', 'B'}, + {'B', '1', '1', '1', 'B'}, + {'B', 'B', 'B', 'B', 'B'}, + }; + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_52Test.java b/src/test/java/com/fishercoder/firstthousand/_52Test.java new file mode 100644 index 0000000000..40f8dfd2c1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_52Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._52; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _52Test { + private _52.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _52.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.totalNQueens(1)); + } + + @Test + public void test2() { + assertEquals(92, solution1.totalNQueens(8)); + } + + @Test + public void test3() { + assertEquals(0, solution1.totalNQueens(2)); + } +} diff --git a/src/test/java/com/fishercoder/_530Test.java b/src/test/java/com/fishercoder/firstthousand/_530Test.java similarity index 79% rename from src/test/java/com/fishercoder/_530Test.java rename to src/test/java/com/fishercoder/firstthousand/_530Test.java index 53131bf942..d96f6bf327 100644 --- a/src/test/java/com/fishercoder/_530Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_530Test.java @@ -1,25 +1,24 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._530; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static junit.framework.Assert.assertEquals; +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.solutions.firstthousand._530; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _530Test { - private static _530.Solution1 solution1; + private _530.Solution1 solution1; private static int expected; private static int actual; private static TreeNode root; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _530.Solution1(); } - @Before + @BeforeEach public void setupForEachTest() { expected = 0; actual = 0; diff --git a/src/test/java/com/fishercoder/firstthousand/_532Test.java b/src/test/java/com/fishercoder/firstthousand/_532Test.java new file mode 100644 index 0000000000..0446d5d7c2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_532Test.java @@ -0,0 +1,84 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._532; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _532Test { + private _532.Solution1 test; + private static int expected; + private static int actual; + private static int k; + private static int[] nums; + + @BeforeEach + public void setup() throws IOException { + test = new _532.Solution1(); + Properties properties = new Properties(); + InputStream inputStream = + _532.class.getClassLoader().getResourceAsStream("fishercoder.properties"); + properties.load(inputStream); + } + + @BeforeEach + public void setupForEachTest() { + expected = 0; + actual = 0; + k = 0; + nums = new int[10000]; + } + + @Test + public void test1() { + k = 2; + nums = new int[] {3, 1, 4, 1, 5}; + expected = 2; + actual = test.findPairs(nums, k); + assertEquals(expected, actual); + } + + @Test + public void test2() { + k = 1; + nums = new int[] {1, 2, 3, 4, 5}; + expected = 4; + actual = test.findPairs(nums, k); + assertEquals(expected, actual); + } + + @Test + public void test3() { + k = 0; + nums = new int[] {1, 3, 1, 5, 4}; + expected = 1; + actual = test.findPairs(nums, k); + assertEquals(expected, actual); + } + + // This test case will throw TLE error if your algorithm is O(n^2) + // And it doesn't compile in IntelliJ, it throws "java: code too large" error + // so I'll comment it out for build + // @Test + // public void test4() { + // k = -139; + // nums = new + // int[]{32196,25662,20113,8991,14524,22650,29922,23567,24083,35838,49415,21584,31906,7236,672,28196,40565,17915,31989,43287,2911,44179,35938,7755,31891,34685,54614,46501,32365,1269,49348,23164,22259,34750,29889,24471,52189,54257,20514,27263,46438,54041,42826,211,15954,29874,31160,3511,1091,39059,51850,18957,23086,112,38817,32389,17860,8,29479,774,33497,55493,25741,15363,25706,7951,31961,23162,32613,34616,14693,48726,17341,53668,458,36597,28752,32215,14322,39975,31848,24846,42980,30957,35787,16670,3952,2886,38448,47644,55049,26426,33899,38998,36414,2388,5121,47599,8774,9148,15117,21696,17737,51500,11336,20896,4766,53999,27252,22109,24015,54663,28571,43978,1740,14785,10141,20019,13503,6515,27590,947,50841,5779,24068,11730,40286,3234,18279,2765,6052,24902,27020,20278,36152,36247,19831,20235,8798,13809,13906,37247,37702,35412,36194,32718,35458,47620,14501,25623,41152,42175,48674,15585,42236,21872,39004,28638,54912,36515,24355,53765,32928,36332,43966,33281,53220,17,38581,25567,38285,31125,55445,16154,33575,475,48688,17202,41092,38465,54816,2020,9194,862,31441,39638,13576,16073,9480,21395,19668,31980,50527,50619,17278,16688,26088,4953,51389,55146,19908,55071,30693,34312,6419,39717,30346,43734,43827,13584,41010,935,38147,26518,46409,52594,23276,13459,29334,12386,151,55154,53765,30122,53357,7159,25227,50383,37460,1116,2529,37133,51391,12681,22955,41006,50924,23239,32863,11161,31422,7450,52849,49240,38933,10038,6355,21937,18966,54561,51486,53702,51151,36965,10531,14662,13755,19194,14584,8562,32798,28241,23009,12170,41928,25733,42394,35933,38724,32579,13536,31032,21204,27260,38737,9947,31527,38198,48146,32034,26733,9844,19620,7068,32453,50978,5571,9372,46512,45623,24719,32443,25175,31428,14185,46196,50140,2890,15140,1558,32166,26632,48420,25872,399,25075,53136,5001,12307,6619,45377,48536,39123,2576,6394,45275,15308,44765,21206,49367,31185,19190,45590,9909,13168,39395,19533,36877,41393,6962,34706,10107,7467,38734,31008,14216,49241,3671,26884,30386,43388,21102,14572,21356,22450,43365,26645,50549,15276,6863,53622,4344,49819,46650,47388,45294,35030,1447,36017,46167,17212,2623,32634,49557,53708,14446,38588,5649,44805,42448,31599,55183,997,8311,34660,52148,14272,26911,26754,26723,44568,14490,22241,39669,12113,14450,16460,12823,49804,33914,24661,32178,50501,12689,12085,22257,54841,42364,6447,5470,36103,46496,6750,26462,3802,25507,31784,33498,3357,42658,44574,22348,38148,1624,46864,38928,18494,15408,24885,23971,40930,22850,35088,37741,45600,41620,52377,49812,9234,20215,4519,30221,55387,26976,40308,10630,44462,52025,50184,16952,13074,19695,8052,8040,19847,52618,43223,6115,23590,24160,51992,35131,2218,2149,19684,32472,52398,49558,27301,25550,48465,38878,46962,30043,18604,54909,9522,13893,44743,40643,3835,50385,43864,19480,19828,38178,44482,24616,25354,1837,16217,7419,28653,45204,55042,41556,38993,13251,30387,14423,17588,45230,41108,28967,43136,45918,7516,16669,23874,19471,29914,51379,35706,50297,40996,38997,21957,258,12159,42203,10545,40130,53143,54369,49918,12139,23336,24607,12538,51171,12021,18593,44836,26918,43108,4818,449,18814,52186,17390,11595,19300,49077,52096,52304,29333,18904,14791,10793,24509,55123,28770,20881,22917,25437,20336,7607,17515,43755,4211,25601,55344,42014,36246,4235,31909,46790,6194,47102,22036,13535,14177,43361,42787,49938,40304,39519,23669,29420,2927,48980,22450,19423,11234,22472,9042,12787,28890,43057,51659,20169,33790,41879,6491,54775,36100,44315,13387,8536,51296,49923,23414,18541,45084,23044,54602,17406,16380,10982,52754,44488,19769,39854,42002,35875,23765,7081,27460,8,54137,23841,41029,28713,53870,38165,11791,51826,36146,38678,29076,54120,7665,46165,34117,34153,32284,249,5470,51121,21421,27289,40198,34143,20440,12113,39959,27250,11346,10878,18450,20606,22592,53135,38699,353,29930,39360,15987,47312,28183,13731,53310,26046,45571,55214,20880,29609,5971,49014,2836,35381,17807,31381,20893,1013,31606,13487,40770,52515,51951,36231,16391,20292,1178,2167,48222,47029,40381,37849,51485,8234,22503,28250,41234,10234,46254,6813,38584,24494,20030,30884,35316,23630,17172,4926,39660,46702,47665,9422,7233,29190,55191,47319,30645,25270,36605,47729,13814,43758,20184,2984,5842,45826,44739,49595,22164,12326,36743,38698,51726,13294,38747,16481,54631,9316,10524,28614,45760,13099,9964,47536,27808,30583,6952,49509,20065,53331,48832,8978,52086,29308,51588,2658,30219,18044,14131,20391,42231,29272,2818,28854,31337,52687,46583,40418,7051,6891,37865,4319,45281,26248,2191,19302,17819,50807,21547,17922,15313,45828,53680,55231,38811,11457,20106,11146,6884,27947,55151,20694,3460,25503,20389,1384,28539,48509,19480,40175,50893,33217,18760,24051,29980,674,9345,43571,30850,38533,44570,37513,30613,45909,31018,52880,27871,31604,6064,52668,8299,41884,17599,12466,14761,32862,28615,18111,1875,5040,48669,11338,54528,55347,54142,32462,2716,28033,2200,2027,32579,49920,5073,31526,45654,42888,43834,49605,10786,55065,24606,1285,30528,31279,13858,1049,9981,30474,4928,19415,22397,30149,22395,2289,26727,25314,36378,18493,10727,22257,39483,16460,45801,8379,8406,19114,54240,5783,9941,2968,10543,18267,33981,52572,33026,3178,45500,11869,28000,13452,3017,33341,53949,51582,55454,32612,12395,32410,37072,45792,50050,4022,4253,25851,27246,31334,23721,19262,37319,5102,18615,29534,14606,5302,44639,12031,6394,2738,51552,41989,1450,13680,21446,48938,45576,10216,48924,34699,33377,44846,55430,54623,12811,7956,13237,15237,39565,34224,27286,8081,1277,42916,33101,11791,42238,41645,51653,28609,46333,5569,37704,13907,40143,50603,19628,39940,1944,44681,49874,27177,3063,1005,14636,28522,34561,51396,5606,32192,36364,52769,18829,18198,19570,26751,40292,48546,35981,7579,26662,47893,14688,22893,18127,16212,22399,17975,40739,35181,50185,17120,27963,46959,31658,44160,30914,43402,25734,12087,6490,9480,17650,43168,12475,22584,32015,16918,14704,49062,8401,25506,16029,45088,34167,14151,18610,14563,38544,30908,34608,18774,17411,24695,50245,26753,11702,32096,16257,52755,6947,41806,44487,17147,50839,51290,44840,32043,15995,34959,1761,39296,37647,24503,21421,17005,48697,8170,5289,16261,25246,31541,50997,18289,41941,10305,46091,12892,37212,18033,14443,45177,39876,24746,22870,15886,10187,17429,14175,10991,27391,48344,42205,6789,22755,40200,27148,46178,18978,26480,14615,53478,8087,41812,33170,40743,7223,31647,33231,28002,45961,12174,30725,4089,53330,19748,11002,22466,14489,14115,13617,22197,9467,12319,40261,27741,11613,15980,52958,22676,42599,4652,49637,13732,168,47785,38306,45279,10463,14826,51934,35341,17595,851,4718,34197,45800,30513,20348,28176,13657,45396,49324,6568,3487,1267,8702,20856,26089,48082,52056,3327,20711,32152,33478,45852,55288,8567,4606,40072,12947,52918,35383,23123,39247,27737,43637,4836,38253,19565,49170,12384,51602,4003,49764,9047,26861,41856,33948,29924,16845,24757,22712,39592,6378,15899,17809,47405,42282,7497,15628,38824,46631,6400,19773,32296,50984,24527,24457,49616,32627,1274,9661,51794,47284,28768,44799,29413,52873,31643,52741,29458,37651,40158,2604,18822,39274,47672,53732,25201,12811,8332,31974,42538,483,5484,18764,38612,49046,15774,55273,2333,35698,3858,4676,709,21693,49251,19993,28425,46000,44089,18206,28488,4097,21872,20737,34751,51871,27190,50596,7173,11312,4116,48012,2468,20786,50589,16340,40740,39301,29346,41769,10207,39331,26160,1144,29630,15747,29755,12341,9737,19677,44914,7537,15208,13877,44013,35969,31013,53624,21491,19378,28011,38070,19472,54758,2091,46407,43703,8908,16819,43598,23455,39358,44744,51218,48019,21835,6290,50576,46260,36090,49548,29386,1814,43402,39919,37559,13956,26318,6031,13660,45724,40085,428,43462,986,2524,1369,40818,32556,31015,8227,41226,44790,3745,15149,18170,1751,24759,23632,12711,34328,2216,53792,30424,35949,53880,27005,23411,31599,19064,5698,33822,39662,30083,16755,11654,48410,8768,19635,27877,52417,40712,25938,5458,451,30565,4433,44588,28065,45890,19354,53235,33098,27247,7528,15810,54205,22279,11903,8371,48003,39188,23060,5446,49340,4562,10447,50314,32737,44520,26324,45624,23514,25236,13906,28444,43760,21652,46075,17012,47719,9937,24384,46069,47486,13691,41515,53304,32847,49562,37098,45053,44787,15028,27016,1087,899,54161,42714,25632,18225,16279,34559,25857,55106,18522,10686,39346,48359,12594,6485,18908,2177,29110,54631,9432,10021,24643,52385,36706,3038,49044,43733,46332,30581,6931,29670,36974,27313,8636,3675,18087,55039,47312,32714,4213,28143,32098,31821,47702,49800,50768,54674,50632,8045,10642,14031,44927,1265,49650,45958,8320,47969,47867,51743,39380,50950,33189,27158,49779,5418,55012,20261,22716,21597,4387,6734,17096,30370,42587,19945,29825,44219,5963,45751,13092,41338,20592,43232,3706,48221,28162,16656,20353,9603,52818,30409,42646,38058,42889,37940,40923,15162,8495,8668,24976,38335,17678,409,20833,10979,22271,47517,12480,25210,48375,5424,6322,10633,35421,52720,43277,37436,47471,17407,24607,15195,52700,52503,2760,39354,27562,17245,25880,17840,1994,21409,30887,13320,42246,52701,23200,13907,40518,18189,36984,40213,38744,41266,46333,4705,19895,32475,49844,13814,54538,10218,40091,39242,42104,42571,40564,17149,29184,24843,12976,38797,19921,39739,1031,39387,37882,44180,52184,35437,28526,47250,50628,22837,24570,48803,22840,19601,6962,25033,33341,32463,39275,54194,43365,55034,27672,28105,13615,5067,49227,50669,45712,7757,19104,9316,28694,4709,50669,4472,30254,38375,22197,26531,10700,8609,41273,42354,30257,44728,14353,55417,38651,34696,11324,7754,52388,23857,8080,24231,11953,52748,54000,33518,28327,50309,54885,50724,41379,33851,42556,38424,29811,5470,31417,47493,12301,195,23702,27227,34820,9991,7582,26169,30408,43544,35028,5070,12316,29310,24586,7867,2278,765,49407,18339,10444,29027,37,14833,22857,23175,25356,47901,42145,28963,43935,810,2371,15898,26578,7633,12997,10028,23625,33906,6672,43014,3,33869,36242,48861,39365,31457,21112,1842,50722,41871,14195,48844,3948,20456,44923,5624,50702,42835,18318,46537,28553,27307,30953,3568,11605,1293,2106,33941,25948,1466,28953,29858,50965,22021,52275,32625,26558,34176,9904,22046,4830,14412,53828,17913,33081,21661,22044,1385,30755,19311,28265,16188,52376,29256,1286,16503,37657,40964,16723,28409,32633,14221,53708,43698,19031,43716,17077,10090,29383,34963,6753,7769,51810,37289,28398,25914,24636,7733,53100,42048,16072,26812,19240,38252,53960,30907,43122,6809,10129,13161,50608,16891,19871,22523,6734,27874,10290,2503,27992,15525,19372,23120,15336,52048,3183,50188,46515,12235,47343,25239,8015,20195,55084,49946,4317,19145,9067,21108,19261,10111,21790,537,46356,34969,30784,9686,13354,53725,51704,17730,25408,7209,14657,48129,23060,29592,46784,34435,32501,52597,22972,2068,47880,36875,15393,33329,17803,47232,8142,42245,46810,21230,3194,16364,17827,43204,3438,48057,19491,23160,29529,52762,21240,54967,42228,26817,4433,3602,50396,3556,13040,48025,14722,19405,47962,6333,34006,24875,26189,6011,33765,24655,1914,6065,40579,13762,19441,30495,51928,33070,33007,32296,45893,41799,28369,31741,31579,8351,2231,24965,593,9244,1426,16053,2838,53091,37610,8306,40717,15473,13989,39331,21117,2837,33659,10119,43167,1358,45489,15453,49349,34826,9127,1385,4196,9221,36479,7138,18325,9966,8698,22076,2732,23148,38452,46713,51439,41135,46718,12775,35399,46535,10995,10286,42034,33558,45957,22727,47474,15827,8912,33269,13025,14669,41006,35770,41428,46369,7177,2021,42551,34585,12703,30490,39885,42223,3688,48869,24682,21132,49541,14243,52081,40283,42969,46421,36603,50505,19351,37536,26276,26774,7946,50258,41054,48266,44524,46422,11487,8457,29759,4630,20312,50307,35147,11274,37262,20911,38186,23629,33055,29333,8262,41757,16832,22307,52886,23583,41659,27882,26557,44359,46962,20782,47197,14000,52607,12849,1766,16653,34003,22081,46840,52372,38722,54286,3637,21160,4788,38255,11240,6849,52670,29518,8836,8060,27598,35569,53523,33557,47845,37843,25855,16945,3170,1849,13508,20957,7573,3840,9764,53827,5567,43745,52100,10592,7914,46499,30574,7571,22565,4220,9674,50919,23540,9360,55015,18916,42948,7813,3889,16678,52200,53283,48560,14495,4214,9838,42089,46405,12202,38438,49694,5855,27284,25138,39991,14737,7910,32187,15419,44407,27314,27044,14381,54015,34096,27404,5871,9367,36889,50193,46075,24179,51178,47394,24782,32253,33653,14252,43455,1628,44396,16149,31443,9196,7947,49801,54360,50044,32330,27967,28557,54692,13466,37445,25447,32875,1451,12267,51907,48069,31592,26025,29116,15626,28829,5870,8686,1818,49385,1098,8519,7140,44161,1882,41502,28721,29427,3169,15643,24826,41157,10544,8630,38983,43273,13695,23368,48531,51273,54561,26777,41541,25952,27063,3670,2434,26764,20296,25123,48232,18894,20586,9222,44872,3182,8438,55090,30423,45126,40234,44791,19272,30502,17800,7298,22448,49938,6797,8274,8328,23661,26333,8055,24767,3823,9797,32661,42466,8973,30323,15172,45046,983,53335,25407,9900,50873,29431,6901,46831,36236,33748,29794,409,9454,2943,53387,7412,41945,50168,13785,6931,33788,8674,14865,4383,14258,49404,17655,48264,33318,53890,19307,30046,32961,16938,19441,50044,18401,42502,54287,14056,27803,30085,12705,48589,49718,10783,33793,44436,37455,46251,13658,21765,6199,30815,37711,37122,9286,48039,43361,2805,39535,40663,47242,54181,5530,32452,4819,9241,26793,55301,22882,24751,40839,54612,984,9661,48455,33471,46716,47475,1048,47560,39634,25984,24473,13198,50118,10309,48597,52109,42050,46702,55495,28998,41876,12905,49826,40559,46723,9137,15274,2365,11601,12527,33313,5186,26926,9008,42277,39301,29208,24769,38184,8133,32119,8499,17883,15797,11777,40147,11701,13742,50442,52282,19249,16090,169,21906,33201,2652,6014,46966,33299,50720,35383,5924,27404,51403,17718,10636,34831,1846,34072,31619,25419,28881,4291,25467,27622,43342,13237,184,17051,31653,42058,37976,14826,26955,15189,41984,18844,45734,46140,923,35442,29587,54013,34827,5110,21856,16237,34668,12249,33209,49993,32547,10742,35386,13675,47169,27183,48652,28402,28729,46476,39845,31794,14504,36167,13806,8795,29691,11332,6744,23048,40389,30754,28642,42046,13988,50789,26925,32029,28564,1674,2579,40896,42632,20009,13343,26538,2860,44768,22690,12377,27862,19080,28702,509,28891,50874,39562,38337,48819,19161,53831,20721,17076,32987,4382,1605,48765,24875,6712,52711,20096,31243,44771,47928,51452,17345,29021,7506,28663,11723,9203,38844,16037,55043,18455,42032,44813,43678,4707,26060,2353,26014,3342,44720,16642,33806,40379,30863,4439,23581,12251,50800,51164,9477,5130,51183,46275,45778,15076,44246,22068,21625,10138,3193,41550,45136,50373,549,2623,54103,54746,47304,54605,51666,52766,2020,1104,36436,22827,54712,46368,1796,21028,1654,38688,52529,53634,25518,36888,22218,49350,34848,47243,34277,28350,16891,37593,3370,42330,21853,4097,40653,47094,50938,44704,31099,17224,14418,55348,51705,28340,5063,42086,2609,38223,6592,12976,51059,25270,15834,52921,51412,52244,5161,8459,26407,48679,11110,21236,32351,31935,38886,7586,42316,29825,28762,48674,39439,29714,30360,20319,28791,29056,44101,11014,14900,10076,26505,49872,29438,33830,55100,29530,6668,12209,14041,47184,28253,26034,47223,46209,27508,2017,36128,4295,21931,21664,47194,17995,14471,20057,38066,40719,24858,51237,11618,23954,12222,18979,43100,35323,223,51672,54034,33831,49738,6003,40024,25585,3039,45916,50864,5467,46925,35230,41960,32887,47302,18570,50514,21897,19145,18707,50386,20076,16513,28901,35442,50989,20887,16349,27260,40693,54149,35312,11927,19125,42198,31926,9655,15550,38928,16246,53428,13537,895,37215,3631,23284,38257,24827,5704,32247,50725,22854,7120,31518,45579,40936,52007,31795,17096,25706,9963,1252,42363,54375,35398,47672,20924,37690,42159,4747,33510,19429,8163,53503,5138,37296,3156,43892,22033,37543,18177,54915,23562,11036,25466,19107,8396,45508,16241,16794,35737,4000,35935,49673,46071,47758,9827,30396,11487,35189,30048,35416,39041,27486,30039,50118,47120,9404,38051,11126,31451,7724,39707,53155,28211,38638,41511,16470,46327,38272,6151,55148,13762,40620,34766,16147,825,33838,35986,32324,34242,36740,42934,52260,11935,46291,18330,46294,9614,23307,39985,30669,21295,28908,9703,41414,25087,44552,47540,5950,10221,2054,27312,40598,18702,49413,16914,51903,31618,47676,23091,42618,19605,50762,12254,12659,39737,21857,23425,40689,17127,41590,22667,603,28989,46616,17860,23486,17084,23665,22573,43869,16968,9138,495,22149,16191,1281,12511,24343,24554,37445,34181,54979,35223,45871,47785,26557,26717,35794,17120,9538,24281,7048,30114,43507,54655,7227,45105,2665,10269,51636,47388,43864,36694,48605,16346,19833,3822,6697,53324,928,5004,52890,40446,30934,20437,73,31747,48358,40141,9462,17679,28759,6209,15891,2274,11302,12533,35908,25993,22814,9663,47128,38034,21287,4174,28988,16513,33483,48344,31680,26676,7124,38081,28593,54815,53250,54025,32490,39288,28226,28950,16303,43288,45007,38202,20839,50678,43937,6926,47729,37532,43214,48895,2659,37541,42968,31110,2148,49002,1666,47505,45435,47221,22793,31009,19888,28978,40411,7420,18301,1513,6885,24492,5793,45998,9065,21551,3962,46266,30019,42262,29118,42974,36602,33068,9559,28408,4065,2237,44839,20651,17188,2901,29388,26491,41733,38296,42765,41097,38780,51180,50363,26030,25562,4272,30504,37707,19986,8413,46056,50465,32997,49313,328,2050,3778,23131,13884,15382,8937,41948,1592,34894,15277,43218,38957,2009,22872,18527,11580,25258,4802,45523,5069,24950,16957,38568,40146,18116,3445,16090,34443,29902,5973,37776,31097,47243,52443,4147,49831,3957,38208,46387,24446,37315,3244,30771,25611,53873,44094,40718,37697,35406,39116,25709,1333,30545,16907,14385,30659,42452,30273,32267,44259,47334,31888,33915,21290,24986,53839,16712,42700,35218,51397,24847,48943,3970,30216,45979,34617,6367,52296,2593,52777,7802,40673,37970,26005,5223,1837,39037,11492,50094,21147,19728,54350,10802,3185,16696,32366,27817,151,47784,39566,16411,13381,18351,37256,16510,42436,4119,3401,36799,18733,45307,18408,36159,10577,17690,4185,23345,18213,16558,40120,47703,32245,51457,32847,46872,14757,18335,4921,967,36425,15290,6028,13010,7793,26735,23707,16948,25908,19236,17374,42397,41029,8281,18961,5925,34252,28576,54845,12949,31432,18836,42455,44903,24758,9855,31615,2975,33161,13983,51957,39300,47221,23676,43919,27099,834,41972,6811,218,30418,25321,42686,4623,23795,29489,40884,19755,6343,5919,8365,39383,35850,33850,41020,33130,38161,25869,28283,42005,35029,23937,36041,46806,10942,18809,34301,41895,52583,1281,18758,50443,23459,15235,18622,29083,22337,13924,3382,13056,17090,25241,2519,12840,8601,43974,7238,35428,3708,39352,46083,47466,31907,30156,39019,27238,30012,24583,13348,48677,33532,11038,33693,21733,53431,31756,6735,46744,36487,49834,31667,36354,4880,55414,22728,14120,39129,8729,23318,37117,1379,50499,5381,44518,33711,52317,38119,50718,17535,25713,36990,1873,29912,14500,31803,49388,10024,14498,28888,40772,53945,43846,21950,23849,46623,26978,27350,40703,32639,11675,44993,23491,28345,53087,26829,49991,25379,15898,38515,33397,21162,50835,33799,14848,50203,13565,13715,21515,45418,37138,7738,53529,6730,9360,33528,47646,45565,49313,25936,39680,47967,26018,52824,39744,5756,54079,53458,2174,3465,22783,26302,15805,19411,1398,8704,12632,20722,44647,41612,2019,54435,15323,5960,45680,20235,47140,20035,6199,6313,33321,29494,55363,38577,26755,24202,31675,10921,5063,31019,32993,18946,37767,44944,51704,31472,14893,29051,28363,474,15502,3493,6042,31939,32180,55442,31904,3348,17690,6280,23580,45432,20567,29957,35319,33097,53691,18600,33724,47650,30215,51052,35053,52584,15500,21352,15771,26393,46156,18476,49885,21028,21812,29712,11645,19827,11513,27434,49548,40777,6583,20426,33485,52875,124,34386,40892,24328,33491,41869,32054,39709,12224,50156,32877,30641,36065,50987,47023,5348,40057,19607,17121,35523,18094,48389,51092,30501,30226,9347,36558,17183,29564,50108,50092,17740,13943,17640,8473,26300,53451,31811,6811,22217,45109,28007,30591,2736,31835,44416,3073,38971,41678,11439,29230,35106,21941,20791,2729,27469,5654,33333,42378,51940,31468,54345,19568,22746,30994,20656,51726,21503,2941,54509,40788,107,37146,43178,22056,49535,43658,31336,21922,41714,38106,7710,31366,50165,1489,49765,53132,47035,46435,12336,53187,8018,29359,23937,29980,3263,53525,44662,27755,33815,48152,4287,26486,38100,34400,49542,11007,6092,37785,28058,39907,47270,49801,5628,2777,46214,44964,38359,9902,53395,15459,40842,20630,31588,39010,35814,3009,1374,45949,28799,14696,21045,28949,20147,28216,25398,44370,10996,20861,32893,17055,15499,48137,32452,20228,37635,32988,26432,3803,43084,27695,6657,25101,51119,28516,2951,19596,43091,52545,26184,28824,8473,30917,7292,34225,13921,12507,20066,256,34496,27609,51552,38052,53046,28401,50677,41400,21241,43221,5687,45439,33600,34710,54639,38169,35155,9308,9480,34796,3544,6778,10673,51914,16796,18899,8584,36312,54348,23113,6216,1408,55129,40066,21915,52519,28202,934,43371,34111,55090,9512,5406,16571,29539,5316,37775,30720,1073,53516,1780,15645,4258,31784,31166,51470,11572,54401,24583,52103,21426,54810,14614,19988,25476,44572,53792,40858,4521,46194,51020,17568,27450,16718,53861,6127,43356,53662,21055,40503,26229,21651,1070,17278,32470,33019,20661,28761,7351,50377,7925,34351,31509,23838,20764,18281,27184,14121,55376,32036,29898,34659,27308,46060,31658,51241,18755,39456,33682,49169,18749,27906,17217,10049,50776,34758,5820,14761,20211,35437,22499,17943,21819,2392,17642,11491,42055,43000,39456,2589,26195,46389,9717,46780,17404,26750,6744,4083,1660,10341,1993,39626,32892,34673,37230,54543,44809,53499,52365,6873,11114,41862,21934,19900,5731,43890,43551,24817,4793,50350,21267,49958,26435,10124,46715,22989,10804,6622,1958,39537,45686,47316,2051,55251,3655,35723,21845,45634,34643,35802,51611,14653,445,27850,7097,51045,34213,42479,6347,17692,47199,19688,23386,35096,35825,46814,7939,39611,17482,24364,20984,1502,31118,38409,41372,9475,54117,493,34812,48070,43517,34605,32526,1474,26645,53101,21683,35689,23853,45059,42254,15334,54551,52648,50135,33227,13676,5095,48624,16858,27668,46865,3308,19346,29747,19483,4713,1906,48013,44303,53269,6558,41754,9781,19770,32735,2513,1920,5239,27851,45122,41099,7912,14481,22961,49679,48002,37959,12406,47287,42821,38992,51273,22209,48747,32894,10855,13270,42789,4766,52755,32538,53454,54905,31144,43767,14015,53045,45806,17391,41360,540,38204,1259,12675,45272,4237,46217,49224,2666,5352,54280,32269,44431,21005,20684,20834,34768,53285,51609,42014,43517,9392,27598,12140,6035,20728,16064,53913,51390,44898,10175,10400,51196,35464,21114,6496,17031,23320,11561,18618,9498,3332,14810,53043,47487,28524,9057,45685,21874,28962,11666,19715,28014,46109,30041,38250,24550,25913,18420,50510,3334,6811,38191,39541,9489,6315,39330,30753,2210,33013,13279,32991,19031,27539,14106,24472,5600,45585,35700,38749,12514,49186,30927,27599,21916,27216,44634,13660,21972,45257,13690,18835,40204,43175,48001,8316,15608,9907,49566,22322,42618,32066,25558,34100,22100,39504,16823,31288,30673,238,41663,13943,9400,48112,49570,19859,51289,2835,15100,12707,41390,34630,14077,51412,48514,10068,30479,33251,19306,16815,1737,16726,42101,45022,34708,25790,38844,13771,32593,32977,6652,41392,17944,20377,25735,30388,15227,13862,5955,33959,3234,9667,21764,1872,8356,35182,13131,23740,46130,11109,36033,17511,1475,6910,12497,14077,4723,1166,7962,14229,48575,33339,22618,12247,36643,41263,39822,21486,37251,48213,40127,32010,14635,14067,2616,24573,9966,28944,18902,9081,21376,28281,46393,38959,12194,23222,3829,35347,32140,1983,30644,23943,10333,13920,21582,13770,51818,2546,42667,19896,25363,47586,38964,31422,41336,46510,24487,54250,8296,39208,24595,50652,13259,6321,9386,3160,35248,7365,11536,47364,53336,27534,52599,30124,19880,48807,52915,46586,39913,16099,53476,8327,36838,35770,9521,121,19641,46730,4837,10346,31475,48699,46140,7700,15518,21985,35075,4303,51189,51085,27752,20241,42107,15316,6651,46533,46852,34862,16130,8454,30809,24219,36627,39665,22509,13630,977,19784,39663,17552,54045,43189,20311,7278,18082,37309,40409,53389,22006,47200,36237,26546,51549,12187,40002,19649,27993,3129,33462,48819,8232,13429,54862,6834,38301,3183,9323,19935,19205,3479,23584,31699,51083,1645,7977,35362,45965,48631,20681,45706,18849,2792,29799,9883,23438,25191,53361,28616,8540,31769,41349,39657,25872,17870,35093,1309,42610,28959,21096,27914,52663,4585,31689,8366,23990,54024,53091,12360,38929,20042,15234,52557,21040,7811,9795,3681,26710,5323,26428,14459,5978,43484,23437,2542,17564,31397,30209,2041,36223,20539,25091,8559,49566,3321,17267,33736,12738,42767,49082,43773,13204,659,49343,42515,38614,33245,27300,30828,45423,43085,35168,6478,46010,9501,33489,20974,26931,34877,30314,11195,40483,2286,16594,46797,49434,39517,764,6327,14381,6527,2608,48097,7702,12082,39465,47839,32340,13826,2429,22176,25913,53020,48247,50973,12171,50013,33713,1118,3465,42810,19759,34359,53752,49315,36816,45738,6984,34066,22366,42102,10933,30921,4776,9227,14706,42458,34283,30074,38184,8541,2719,44,11213,6703,55360,41777,34836,43788,25583,3017,47115,16362,35893,21841,45120,8452,619,31252,50231,44567,28501,9016,53881,41957,27383,51619,714,49198,27771,23716,29421,53039,24071,54063,33980,10819,2467,17638,50169,31986,48729,22488,10118,16792,3026,793,14941,20727,1079,28063,15477,6057,37489,32015,5322,50075,16818,5557,21908,28011,28773,40378,37707,43281,336,17365,22023,29033,12404,2584,25593,33781,20908,23269,51556,45360,14172,9895,40988,5713,44906,4887,13651,23120,16593,36919,17805,21743,39364,44512,7244,41289,31731,8034,38333,4867,46956,3099,10678,35522,18485,29055,5481,1055,13820,9333,11175,13385,10250,6650,5631,21814,36763,15769,50415,54350,4144,41741,24293,21214,17075,14907,45790,20505,41092,11494,1639,32660,20242,40881,2894,43393,36639,5622,8864,30532,41176,16963,11516,30010,4490,18103,21224,18174,45001,11752,53591,34582,37188,52453,47066,2672,10939,19554,37104,1130,37194,52014,34184,4240,43222,3204,10418,27838,4851,46068,9563,16287,34040,37990,15675,32370,50584,36465,55480,54332,11603,51437,7076,9371,37012,23981,14207,33109,54521,14290,52167,1537,6039,21500,35784,3248,689,1921,12901,9434,195,27432,41184,14575,27352,51964,29025,51030,39542,35186,43785,19208,28885,42473,5132,19550,39672,25309,10508,13766,54995,34613,31463,51718,45311,52177,31686,3662,31338,49183,17179,134,55361,38422,44605,43070,49370,52778,29840,45072,10089,42678,10815,39735,6493,15681,25888,14700,54746,30465,31142,23281,30643,49703,17871,14848,16319,7887,1057,54264,52052,25439,978,30971,20883,22272,25621,2275,15900,3081,54082,36895,25817,34770,48932,23079,21729,41933,11367,222,39807,37562,33641,11648,43549,21037,38269,31771,4216,31842,9600,10131,37682,11398,13589,37101,15988,20036,48821,46202,13639,26475,51983,47521,53469,20137,5422,41703,51734,49704,31427,22390,53809,27631,48447,38484,25020,6304,39320,29434,9784,12010,47053,21471,49509,42642,44147,48678,45215,39783,21665,18674,22854,8939,35481,45582,15362,3922,35646,27894,4383,4740,22366,20563,4886,19748,40341,31950,20496,42053,41310,27186,27540,18891,27663,21456,5051,37190,33334,47793,31572,973,3944,34886,25896,13481,30787,33630,10921,54546,8017,49706,1577,15330,43379,19799,50916,13742,47105,8654,3336,4190,37908,23675,15004,1295,34457,33171,27870,42213,32062,44961,49184,22173,36807,54927,39291,54072,24762,11323,51602,32962,5752,29606,4738,42302,27784,8950,32545,43346,5175,33062,102,23129,24250,28644,15941,36753,32220,24323,43191,45636,43586,13824,6515,6324,4297,5855,13679,19300,7871,54368,53934,11640,1350,54112,31185,45804,1807,36823,31240,22906,36902,12558,13956,7077,26556,5960,29421,55269,19970,21695,43260,49659,36279,22353,36570,46263,6415,2420,35977,37203,51822,7965,51231,53158,37241,25250,7669,17835,16004,21039,9567,12855,29780,15671,982,4788,41049,51942,21456,19051,38002,21304,37484,41955,54335,55142,8648,53082,53589,14113,5408,41178,8148,55464,54571,38724,17952,38755,8196,3173,28533,25750,2705,49833,49407,22597,42653,37911,19522,8752,12447,48962,35386,39334,16166,5869,9982,36163,28445,6518,38868,42793,16300,23104,3146,55170,33420,47820,4595,16714,4900,49851,51910,31622,12602,41312,54774,41696,53684,7814,50852,29733,47645,27791,13145,54560,13123,41647,49733,4517,53342,45727,31303,8075,28225,53555,45016,39297,17761,29001,9647,8049,47630,9114,34456,26156,47644,44903,31995,19640,29348,54425,48393,25402,23462,39658,38798,48971,23479,46517,5049,4614,6927,36560,39218,45501,21884,43276,5351,17754,9122,17384,16581,35223,18524,15800,14299,21321,49027,52621,38288,39504,23739,22923,6576,27177,29997,53071,9956,22121,9369,20306,26970,24449,27322,20257,10796,8164,47647,12056,27432,47841,18122,53289,55426,39870,42906,40904,278,52532,17778,47239,20596,1924,46978,50138,50703,29387,25195,29426,10762,49558,10751,6314,9855,15970,756,20997,50146,27586,43288,37479,32940,53370,54391,31570,7202,50405,12962,43397,28825,10124,35681,861,5645,30854,43558,31654,25865,20859,6992,31399,18393,33112,52700,31048,38879,46144,6019,3412,45461,24635,45550,27335,9105,14984,9841,31006,6144,17408,5896,43635,43653,17705,26079,50269,23171,47658,25485,16505,30661,34415,55347,38561,45021,14495,33565,32718,14770,44893,49845,49042,20282,21108,37001,52494,5647,23909,5038,3000,41378,2656,45182,28882,53861,48783,27945,28551,33434,44835,15420,16892,32792,22174,37300,6205,48307,24627,47724,38514,54168,36421,9852,15759,37377,7242,34459,22226,40657,16057,21164,40449,26544,25871,49062,28767,2128,30543,20789,53162,21069,26385,28015,26530,26978,32323,37086,36902,36009,6758,37366,25892,10752,24565,42921,24677,31192,17356,44847,14395,20931,3729,53901,36004,17585,24062,25974,25144,8087,32631,51372,47603,47922,14869,25824,20539,50443,41709,36465,15774,18070,11296,40333,29895,23247,23151,27034,2520,3235,5395,54445,35916,35335,3318,34258,48010,6927,38997,22889,46539,17385,4167,46570,42211,19105,41411,25031,15304,11697,17467,23631,12251,28337,50797,13652,7183,1755,3205,45496,53173,47625,49838,49430,45657,12310,45329,11486,12457,52228,42226,30053,34211,44419,40029,48182,13377,51382,35779,50001,55084,773,38962,11769,39677,14775,10539,3310,42114,28198,4247,47863,42613,52955,37843,2596,30508,51482,40667,12659,2823,1652,45717,43228,42950,39744,19123,20452,42347,10,30062,3043,43673,53061,83,40730,16270,20675,17622,109,32058,43085,20869,28285,22476,2783,1473,16000,44721,46478,13730,13549,7447,39958,35507,1477,43863,38621,18354,704,40165,13065,23175,7866,20651,40311,16913,16896,21630,18602,23902,27471,20640,15493,49917,21178,27151,1022,41335,31113,11076,40763,45079,29540,36953,16718,29763,10902,41244,11042,43143,46280,7300,46357,368,38036,9238,48295,29402,51635,29305,46138,51866,47818,8087,14310,49256,22929,38630,16199,1903,12219,31149,10448,40899,29049,26982,5288,53905,25945,44781,1793,23359,17068,37627,6350,18494,32386,5447,49921,54896,54649,45788,49622,11247,22520,5058,21837,47839,4261,31614,950,54235,956,2599,21168,40335,12018,47437,40507,54788,34682,55373,51749,37240,21585,24566,28654,14558,29038,28399,49891,10593,47096,38888,3213,19793,27567,5245,25356,21099,54394,39676,53885,49521,30717,7233,54201,26140,28640,27975,54328,42349,13545,11938,52000,10535,6543,35932,30635,44300,11482,52723,11559,25916,30361,16285,7683,7326,40729,27492,34860,13201,50718,1446,10746,55364,3009,10125,54106,887,5996,31741,13548,43815,20756,8346,14758,51383,2696,38125,28842,9054,9976,28713,2002,22286,21652,39827,50813,38869,15976,49957,37143,26300,30923,18840,4980,31614,12018,53937,47550,11800,9792,11950,16679,3537,30727,2338,20274,20652,25857,49677,32657,28583,25159,10693,35758,18529,50727,23997,569,48889,53478,9381,27794,21832,23248,25174,12509,7942,28732,33644,33141,12870,18659,27383,1882,46082,34776,30157,7810,42043,21529,32036,21709,3464,28554,34918,25643,24144,23898,25815,26846,19583,41341,7759,23105,2719,29089,19481,11364,41411,31842,49545,3826,47686,48251,29098,1354,27179,39588,26940,5704,10605,37539,33737,5692,9875,8788,30976,38666,7318,30456,15196,16759,4011,35198,9441,21335,5930,48900,706,41270,13045,27142,28446,4614,49987,30314,4026,9112,42169,39253,15792,42113,43058,45222,29626,50682,52234,46099,44213,38414,7815,52420,42416,3539,39804,35925,1101,17020,33200,54653,9030,21816,31386,27046,3927,33885,21325,23404,6240,31912,54151,40304,36899,42115,20279,53915,54644,34478,21290,50680,34898,17121,5351,42494,15249,24764,11276,21904,49560,13187,30049,5943,45729,52692,38279,36348,4156,3290,14764,50844,6212,9951,17246,54268,36614,20310,44257,34680,19413,29580,49341,36979,51753,52755,34447,34470,26814,40118,15192,52903,32408,1828,34546,43381,10607,9379,40986,48724,47257,10164,40661,13454,40449,45412,7945,5545,54553,651,5901,4312,18831,6127,26189,51409,21012,14183,6060,28880,32062,41132,8387,12349,33159,11667,8018,27045,19267,906,18278,45555,11131,37926,18945,35607,54757,2809,32369,3284,31495,12842,4228,40315,46684,51713,50925,53905,31957,37155,4674,24614,19168,28945,44579,18097,33103,51398,46354,27150,54284,11722,36033,54137,9811,18610,42638,41523,3117,24458,3341,16963,27661,6728,15354,25198,15876,39613,1870,55380,14111,49830,1367,37718,18144,41618,50230,15773,48175,6760,45967,21544,5897,41833,9694,39023,48732,30033,35535,12590,41197,21057,6585,2075,29854,35091,18605,20959,53956,14538,21201,45427,53714,46227,33810,51297,47215,35606,45172,32297,5996,12074,6270,16689,23024,7998,32692,32251,16964,27626,31942,9063,40547,3169,15983,53674,8908,16612,300,29978,53137,51570,43811,28277,53779,52066,55338,30514,13250,12766,13324,2459,4927,18648,42831,43464,40494,37527,24378,24349,26702,6370,16950,27493,34183,8649,13052,586,45384,17554,44038,38279,22434,98,39351,33247,14483,44846,35220,9958,53727,11623,12909,54444,14940,41190,13743,25277,47986,31642,48602,15267,42749,20831,45460,5616,19043,37430,12864,10756,13930,29716,806,21322,39597,48662,48573,46196,12183,26325,11319,2167,47799,5035,36730,41053,25145,21148,21668,47744,49672,13749,50792,37604,37009,4945,19283,32320,43039,30951,25188,9327,37303,32468,25402,38211,38810,53310,38507,21433,51191,7481,50071,35355,51511,20659,33867,10187,53576,34363,45328,52505,50253,23662,7838,37392,53538,42242,5070,21730,1325,19967,37883,52031,40927,9857,44302,34061,11685,34515,49596,32858,752,22010,17952,10950,12142,7245,33628,3805,40019,54593,21329,51115,35086,3735,24124,8197,9031,27489,22345,33802,40563,11786,14272,43259,30875,43498,12875,32461,24376,8887,5541,54615,51523,41668,6932,33415,11887,20784,14812,45379,33300,16687,8944,54077,21615,11606,33102,33350,2963,36401,7773,48593,36100,6308,5625,40234,38510,15598,5759,6577,48159,29086,22458,37978,1990,42605,48555,3095,17693,23614,44124,53967,49098,10125,8184,26880,14834,50480,50980,52380,11211,23624,27260,23549,29497,20970,40583,19127,912,40349,24037,10743,47016,30882,53110,1891,3845,2041,28354,29667,10340,28470,50445,38817,52496,39789,36674,44345,12262,47164,30634,14004,7317,32514,35837,25087,4605,28134,29807,43470,47090,451,15666,29276,37866,31157,54729,36766,43492,38718,3284,37557,52010,27116,15036,43216,39089,33902,10088,51378,271,4593,38404,24545,46137,22272,12202,34520,9671,6426,38072,37898,49957,15534,16128,398,10469,48495,41821,47158,30677,51884,24357,10264,5794,55259,33345,6769,33073,34118,53431,46879,51172,25188,32838,9058,27773,38856,21210,28545,19939,23004,54118,5364,18094,33968,49594,44948,13566,38728,18789,50014,5241,9262,55351,39160,1218,23835,21595,21105,27822,46708,55211,5773,21356,16866,52246,11104,24755,54295,17199,3290,54117,8242,32492,11177,53249,14471,19461,39199,17453,12632,12107,6714,21657,40769,2282,49180,36947,47992,32161,12016,27087,45157,15626,28316,5687,3710,10276,18544,11868,40242,45259,9351,13815,48187,37494,54907,49163,25128,19989,7170,22276,44609,28989,18182,43191,446,55333,27718,29093,6598,15114,47896,7504,3222,52509,31254,38808,11506,22642,781,8132,35677,9470,10452,47185,20166,43629,16303,32630,22752,18640,37310,48529,18971,16513,33142,5007,12280,35758,10459,3703,39060,46184,26528,38726,40636,10637,14926,47905,34673,16901,44571,2560,9017,43463,26699,17026,17120,18785,12107,44021,31055,39620,29409,9435,20785,15155,2923,6389,1218,12906,50115,43199,26061,42111,39940,7002,24395,16247,12066,50005,2456,16895,9744,52441,16197,37997,42907,50578,9945,12984,50078,847,46633,10830,10056,29976,41791,31924,42346,17647,12433,9121,13382,4901,624,21213,32105,5881,17571,28050,23452,16010,9382,19912,17085,4069,12392,5946,40487,35915,4937,6503,32228,20145,37850,53972,20449,19051,53816,14559,43543,36015,1266,19628,44776,12479,47175,13755,20591,46568,1037,35207,29934,12034,17399,50784,35227,47050,8019,11243,93,34254,35643,14936,25659,7186,5369,12973,6170,27072,35098,25083,51477,42982,4157,585,10564,41274,26468,3378,30694,17576,20837,16755,13476,37047,42156,4264,35294,35610,4991,40700,39942,12250,53847,41001,36107,39008,54318,5047,17109,37927,25498,342,25067,18187,1356,50698,43226,42539,49847,18855,39105,46655,618,2645,30289,10574,48882,2099,34766,26840,14077,5775,45041,9068,16308,12625,1204,30921,9813,18187,30865,29045,25429,38632,20104,28665,24427,12946,23257,16978,37482,16599,46192,49575,7604,1981,14275,37711,38349,52408,9030,7857,10918,26789,18874,33468,22943,51058,190,35771,36251,25371,16550,49660,19458,35642,55048,6196,15692,54627,24191,39799,15878,5920,44822,17368,15336,19117,52268,52046,32414,45253,32143,38220,5395,10684,10445,47652,5064,30951,52992,10822,37346,28291,38127,493,50138,7792,53741,30605,49900,39313,6933,6989,43487,4882,10958,27238,42120,35900,37073,34008,16773,23062,6299,41871,48082,1468,6512,5026,8247,10737,33029,29680,54418,21328,29111,10462,13975,48000,42740,4463,27030,42579,48118,23252,20122,50324,45193,44082,26880,47324,47711,3768,50265,13359,46748,18832,21165,45779,1780,35041,6785,45040,10666,49370,43262,9649,27941,23385,9080,11384,9470,41970,47877,31664,44165,32981,26479,4705,16661,13784,39519,6094,35502,13114,53535,26384,31559,41719,3564,45095,46230,29212,36672,23434,14510,6459,13363,35755,45159,27531,24653,2627,55418,40730,39649,2505,48598,49816,6547,12931,47045,28801,16091,18676,20589,33400,9417,45608,11835,23895,25679,23586,7094,55244,36903,53258,27146,25045,12118,21896,47459,21701,32457,34602,21406,26147,30779,17784,6413,31384,45453,7037,53263,3959,41714,40149,25427,43020,39631,16922,11608,41092,47662,30309,5164,20945,17552,3101,2766,28646,20183,18768,22273,10466,22186,34893,15193,52697,17609,9634,11408,22776,45839,93,52624,14538,7968,23567,35222,53088,10704,26885,55161,38925,25946,13026,30064,25439,41414,26210,30450,50299,5494,4677,41778,51985,3983,25224,9498,23780,5686,11879,29066,6071,24209,49165,22798,37314,44448,21038,26813,13946,24333,13880,22937,16347,9639,23383,37410,9660,11982,4064,45179,18239,19906,50319,52033,18917,47545,20534,24165,34122,19602,2114,53975,24079,43579,38316,1106,15919,38075,53310,40535,4418,9035,24802,15061,20802,39143,12419,5972,7943,53645,8453,22055,49638,31319,21492,49361,53657,43201,18795,7572,17338,30817,9312,12548,917,19870,46926,41397,273,15290,45513,16925,12462,8797,33389,31458,39297,49945,29418,6462,16210,27708,10486,21967,29947,38866,32884,33287,23314,40283,15675,27040,36033,28905,27580,15544,47587,14360,32795,1521,43009,54280,53299,46191,30603,44283,28355,53912,33037,38046,21422,47481,46072,34529,36736,15140,30198,20047,15832,11542,9998,13429,31296,12361,9643,20509,51944,40682,30677,36816,20436,37291,41305,40397,40829,8323,38163,41996,53544,40255,47436,38702,35993,49052,3162,41736,15636,32976,55435,40477,6295,23392,7857,28643,51103,18507,6329,7403,21585,45019,32116,40491,37692,26411,10013,34575,37707,19869,34356,38307,3464,4395,44050,3554,30966,21679,35072,11588,30157,626,31466,7602,42621,35716,44079,16170,30580,21576,9847,46390,33862,10350,13073,35071,1624,38436,12572,22090,41159,6050,32817,55232,50190,16840,20019,52324,19753,54547,54771,31480,40956,26674,9898,15706,44185,12841,16622,34975,41024,26475,54046,3705,7167,342,46148,10107,42681,17016,50174,45598,9542,43680,27899,27669,14535,42329,29769,23080,43966,37910,1593,22856,7633,10798,10296,10292,16535,53018,42082,27770,48048,27672,53442,48695,47433,25401,25949,53912,18304,16992,16914,54869,48030,34037,45495,29164,35415,20124,46954,34240,3449,16826,14216,28964,31743,200,16044,48819,11175,33376,53747,5583,45193,19018,40060,12611,26783,20231,13595,38262,40362,7705,11912,9249,50604,37321,17550,54540,10408,16505,30050,18614,30247,5780,5288,31670,37429,20529,36779,52919,31349,55340,45399,32846,50204,10724,19292,20121,21392,12271,30112,29062,28645,21033,49115,11981,35237,6024,20923,48995,31762,8344,10840,33398,10319,5600,41605,6610,37042,23760,17635,39220,20081,24944,9934,33352,17712,46910,11683,17435,43962,21867,3874,11467,30062,5683,35883,18306,43643,25528,45221,832,52558,22334,31069,29486,2856,31469,37743,37292,27482,43026,48066,14464,54220,18173,5428,1421,40008,35136,46187,55410,20614,39594,45489,9152,33884,466,39978,22639,30130,12194,21792,26558,25204,43790,16166,8693,51226,35053,26090,54676,33797,15101,925,36025,31856,8663,16798,19381,31626,6801,47392,53436,50085,49438,1087,16918,30542,18810,51810,43795,43652,44371,42992,45884,13419,7393,45213,28694,39852,32468,51642,41331,4562,24765,45739,54406,25679,13201,55230,31288,1504,32941,55093,32399,20902,46419,36429,12260,38547,42353,22915,25811,15401,51727,46516,215,3579,8891,1631,23371,20082,53843,39842,33204,22473,431,6960,38040,36639,26704,29804,53920,55113,23808,7943,5934,12161,28995,45484,46046,15703,26305,24627,4828,7613,561,44764,2714,12092,32820,17383,46528,22961,814,49646,8221,33761,33673,7407,10064,50893,52832,25722,23565,45982,16707,7092,27725,31247,52204,34831,40397,19320,37279,52977,37575,35495,21470,6635,51896,21987,39227,25928,27870,35366,47861,33211,8589,17963,50671,5717,53015,35792,18105,19338,48247,51741,5612,46808,55044,8987,4675,1765,47454,48598,39019,46057,53105,40352,2108,12148,54690,27236,6001,44841,41992,44107,24729,14660,50751,26699,27074,16998,11145,27436,2109,54017,30286,38462,25046,13500,33158,31213,5866,31139,6558,10303,10742,22121,18887,10682,10240,36506,375,26770,54802,13684,33282,16938,38104,53394,44538,50402,49519,50126,26288,40856,6066,31577,26655,17066,52577,25965,53723,41244,16733,22835,9959,15365,46479,26431,33070,42048,6910,5654,25772,36303,26874,49240,42257,42444,2408,71,42503,13880,5484,47935,49418,29399,45384,8734,40777,4808,20585,45795,24393,1584,53837,30197,1859,38581,15058,35204,11438,17564,12413,11976,51304,12420,13418,9694,24651,8416,12838,23088,15811,47226,43299,39209,13583,27777,15123,16270,8228,46406,6293,32752,12545,11218,6518,2835,9787,24138,27672,19952,45633,37981,51,18558,17573,31321,39014,20967,38552,18432,44782,1252,46758,39037,18407,9442,28809,54701,12181,8072,26318,11889,30866,11290,54512,44279,49738,12034,46363,494,10056,27797,13921,12611,38427,43671,16537,6435,17903,26153,32603,21613,32972,33191,31960,4110,40820,42878,18279,24955,31483,25121,21311,16751,10712,38762,13889,15334,16650,53431,20026,2138,44103,44543,2816,32275,50248,26042,46401,27338,8188,52068,23326,21566,17278,54166,16706,3529,37272,8290,7446,26675,2901,15119,27775,44186,31521,25503,27145,16174,29019,22661,29203,44871,3134,7806,53363,32827,13252,13772,19859,43930,46336,13644,53759,17020,1855,25296,34757,6754,53879,15835,51510,30993,3692,40735,9926,22242,20138,12335,10415,9922,13352,42297,50064,6145,25989,28006,53422,45031,22217,51904,10826,40598,44512,46632,27412,38268,27670,23423,19150,33118,37469,30516,23329,22369,32551,51645,12494,28324,43953,2530,4288,4590,27895,25090,4248,19736,36665,13040,27407,36119,27072,34094,90,36666,21829,14050,23221,46566,46686,19654,34317,41654,53076,40983,50065,46726,36144,30382,53477,31110,19349,31030,15262,43251,34616,17796,54253,31578,34081,25890,15741,36681,24311,2622,48398,54197,24328,18370,44208,33571,4577,32972,28239,42993,19409,46109,50777,4333,46624,38946,28830,46481,19383,44003,25178,42448,43638,273,18865,54216,3171,33813,50006,39014,45230,28917,26635,11393,22814,50817,25676,7017,17448,38001,27981,55475,46480,54579,3331,25125,43364,52256,3317,47761,26123,29509,33124,40455,30402,18695,27337,28499,25288,34309,28466,24657,54426,992,19167,45137,12746,28033,35201,12593,34476,52431,22386,2380,37057,20269,6194,15517,22958,45489,51158,48039,39733,362,27779,4195,1714,3558,320,26510,30295,26943,29901,25777,50218,18691,34724,39432,50495,9362,22187,50970,51839,39840,55477,27562,3673,44121,13660,10477,22127,48777,27631,46062,35130,11903,33827,27325,39845,16062,35503,14359,25624,20416,2316,42032,19222,34231,6336,19179,19336,21835,1626,39017,25792,24423,29098,10331,42541,15335,41544,29872,52509,33144,21456,11387,25854,47951,35352,46125,13522,19883,25125,53118,16457,8153,52524,27029,13701,4561,29140,53592,25950,42809,43816,35230,38659,13609,37188,25654,27326,1516,53932,7617,25971,22435,33134,39327,32608,36350,44257,33553,17620,54852,24788,32739,9650,36286,52806,26720,51377,26646,26029,8757,54359,24334,38098,14169,19499,55010,7946,27867,14997,22335,20411,26709,42205,1968,12491,49812,51136,17075,30028,25653,46870,13582,36361,25081,40387,7817,35888,21683,27462,16987,44948,50909,19151,35486,47715,41129,40548,42058,39443,48734,11138,14489,52903,4977,17664,46029,8246,35476,8456,23730,23388,25529,27768,30833,26374,6583,46546,43413,46841,16819,22363,40770,16244,36072,30495,19989,34959,41298,44593,44059,5384,20516,34655,36172,20859,13939,19201,44050,20766,15168,31933,16391,20733,11454,21652,10404,32558,26946,12333,13205,1241,23113,11479,17573,35018,49957,26533,7330,53182,16237,32300,52675,10384,42328,45281,41336,36767,43596,19965,20427,12063,18232,52061,6973,54693,15816,10487,32273,5104,16336,33752,41907,33630,11261,27868,31294,248,48773,21922,37646,2148,236,49014,49611,6159,31897,48689,25206,9378,24658,1320,12620,15926,45578,37445,53662,6376,8814,1650,4801,12930,1151,11213,53748,35264,19863,14276,5037,29360,41478,2607,280,17848,8096,12739,51523,51575,42864,8320,34225,30470,51715,48731,22590,19593,1833,41464,35063,47657,14113,51620,36102,12852,12309,49208,46204,2524,52601,37584,5826,25359,49727,4188,54985,35317,10502,6645,55226,45806,10410,20458,20168,17117,8567,48058,27394,28882,45621,11831,52990,34761,12395,33823,25023,20389,973,55467,27129,8831,23785,13896,40423,46935,54804,11610,17359,28098,34416,12523,19246,47863,13342,18618,27748,24562,11962,26433,36446,15004,35570,350,27935,30755,51596,25707,24808,1054,43122,38157,39757,20819,20347,9971,10424,15141,5782,29688,39170,28302,12667,1181,25686,31409,10487,38552,38593,19414,19622,6065,19910,9403,11216,44079,10839,39268,5846,5578,32625,27988,27044,4450,10396,6244,36363,26545,48719,5314,44093,2017,38102,32376,41550,31902,32888,12041,48839,1082,51038,36775,12015,20888,42793,15228,21898,18292,22550,13471,24904,32111,967,49282,615,40698,38115,34131,5668,3073,13326,37398,34914,55195,1670,41958,25882,1888,21960,34500,9140,1810,49035,17363,41125,40052,17544,32141,16474,31413,1039,9360,11767,16894,14987,39538,10728,31998,29738,16868,33468,1843,5244,42363,8165,49522,50573,16817,32936,7343,54879,48419,23691,23861,10369,15802,48648,6261,26285,48104,37248,34960,12303,24940,34908,9469,42885,51965,1509,23155,36774,49050,15900,12669,29719,37742,17676,27548,2538,14820,45969,25619,3723,2355,37885,4307,28107,27801,24125,43751,54503,43898,53625,48802,6562,22258,34036,28466,7770,45476,53885,23234,17736,53084,39752,9443,25916,21168,36688,47869,6352,29598,14470,19356,9002,2412,29071,10284,32142,17734,38234,8952,7788,6997,24545,20048,8511,12464,49663,51354,39428,23112,51494,51441,45392,25737,13159,803,14761,6317,4307,42308,11441,41357,40551,22689,6787,31185,22117,54044,42269,7877,20867,1517,18740,46386,25875,39412,39986,8993,44545,3516,24198,12647,55014,9416,2442,11123,35876,31015,42098,19019,23528,31193,49096,12065,5204,43548,5217,1728,811,12882,26440,27836,32011,14124,19144,38287,12412,37723,28756,22907,5710,34481,53066,19144,34726,29420,51863,41618,12137,26854,24648,25729,37620,22945,28518,22908,44617,7359,37853,33678,16712,48609,39317,6986,13314,37283,1197,20084,5587,8472,10121,6357,50800,5139,10533,23977,10216,29430,53216,13819,15499,18032,20777,12984,45040,32924,34351,39393,53844,33561,9079,1115,54288,24718,26638,23226,6592,38007,34865,30268,3776,26826,1155,37767,5869,6372,36463,18701,30764,38661,40139,1459,49872,24794,10796,18380,48404,42117,48205,34546,21701,19549,6858,52239,27002,4426,7971,8298,8185,19304,43563,1096,34911,52878,17018,2551,12005,27244,28027,37134,51985,37575,2072,54522,52736,21842,54452,13618,13359,25142,47794,49894,14925,40173,30101,16369,45623,54929,20376,2371,13754,48498,53522,3283,25656,48916,49009,46301,5129,12583,11445,24896,51990,23603,54704,4301,9199,49396,34960,29192,6276,15727,37871,19652,32781,41214,2515,51828,30105,19349,4876,9962,51329,48495,45210,36642,32736,47006,47154,27232,53603,54193,1508,3913,26599,28020,44995,5444,48076,38959,1073,48644,4623,35234,54206,33860,11336,16297,17006,55372,49535,36623,34465,18380,37749,41198,4727,21418,4519,36487,24329,53435,39457,30378,5902,47983,10136,51396,39251,42709,35775,1387,54901,50243,51086,10432,54656,31901,43617,51230,37012,10959,14878,6170,41291,40345,30505,37741,15972,7979,831,36891,55323,53586,33014,51849,32818,13926,46208,35592,814,7794,40357,16212,44520,445,5444,17431,26830,53552,43446,5143,50804,36661,37504,43541,49772,50788,48581,51448,30279,12164,46570,11444,2535,6997,34096,52301,47365,489,27250,14921,38445,6471,10636,23284,51724,13665,10275,1347,50589,10827,9442,53057,8133,10053,11019,43052,9883,19423,22902,274,21452,12305,41508,54848,11607,17474,36662,27955,55411,46240,25055,53371,42505,24507,46196,8916,4620,26805,1390,19131,16459,5074,38114,41315,21099,29212,48713,15300,12339,53512,32969,31538,22388,13538,10707,26154,3679,38372,51343,2055,48443,18932,43490,38680,50717,32980,45583,2120,5147,54043,42795,39733,26721,32868,51337,52639,4355,19781,9267,16227,42578,21569,21298,25486,32443,18020,41365,14525,31945,18380,6741,22620,48228,50631,48724,38454,19489,13079,47540,55349,35040,42508,51327,29797,36239,11431,35728,12333,34538,15141,10825,30196,47188,41046,9166,26236,12020,3764,1001,48210,47769,10282,21224,52668,51991,37790,13247,7160,43855,4557,15602,160,25571,16680,29434,36536,24090,16333,53652,6366,2641,12609,48878,23601,11520,40161,1961,24266,46263,19749,42370,24570,17777,38181,3789,6782,8908,49901,20534,41362,15852,45632,43974,33372,47801,27717,36226,14071,37408,22396,47191,38186,48797,508,8456,33944,14031,33925,23188,6960,22871,54557,43701,37951,20279,51256,46268,14035,20378,36261,20136,30366,34717,1999,35068,18224,36969,9058,38212,38878,11273,8385,49570,27912,33468,39184,13281,25348,28923,9395,45108,7626,24091,26482,50103,38010,37799,16801,46320,52723,35894,8389,36102,19372,13345,28122,43247,39920,24092,6868,37179,38004,604,53783,2793,6779,19221,15484,12465,27220,3464,15139,9734,34862,45382,7761,55054,5099,34875,14816,3412,55285,10759,10662,43221,54656,8831,23345,18109,32658,38409,34341,12902,38141,21537,47688,31869,25365,47144,13226,46916,17957,25377,29582,25914,13163,22105,8417,49720,52115,46482,37553,55173,17875,43375,40825,28598,27321,38800,21406,35036,21475,31243,20303,21112,29733,25732,49783,10158,34900,4629,14551,15818,8802,5245,9912,17305,40298,27422,27246,18562,2863,453,44019,33335,49891,13828,5886,19229,27104,28815,3967,25873,52929,43196,30351,16413,18510,30237,20549,16607,37874,53896,38810,8426,9340,3347,46277,55333,41186,48017,22889,24590,12826,23263,810,13610,15684,43478,55321,9519,33833,33850,42948,33579,51446,803,20006,19739,22113,47011,26264,3725,19636,45211,55070,773,11156,41155,32029,10688,23528,12838,11679,44961,26486,20340,19089,42008,12620,7108,14703,53283,44353,1439,15010,37008,37686,11061,15395,27542,32769,43060,24520,2416,33334,12138,5286,29895,1899,43441,13394,4542,39915,48828,41628,19286,8330,20717,23184,18280,53033,37548,4443,13829,28619,8271,30239,53974,2825,46315,41564,16920,37930,49310,4717,19855,42074,49742,29611,38648,35784,12052,8054,53270,26307,9440,52425,25818,29744,630,8738,38877,7131,28384,7461,22587,35831,24003,40692,14920,54127,49116,50756,52651,12660,40389,53077,39623,16539,43723,45758,670,24666,36915,7466,10536,11752,39014,32205,8252,23612,12165,48305,42777,14067,16764,14473,1688,10627,7101,22427,23095,9606,586,3121,21092,5109,38477,54608,14007,28692,35420,17540,6428,26663,7282,18615,43004,5916,55305,46655,37379,43307,51750,21781,54796,13796,5272,26013,50608,37493,42507,32734,32159,39891,29305,12201,52448,25247,10122,1028,48085,40328,24082,32518,14492,23772,46387,37281,40664,6632,17071,21764,45020,8978,5757,41463,13028,45730,31900,52266,16065,39491,36017,9306,9611,36603,50163,54779,49895,3463,55132,42762,27776,53607,20316,27848,6599,13243,39898,36787,46647,2534,4698,50148,7616,40695,35459,50717,10230,7907,52007,9133,22950,55253,54465,19608,34286,47140,39786,3384,53177,38079,23892,44184,41700,29389,38242,31094,36240,11244,433,45918,32886,54763,24832,44228,14826,19857,20230,53863,37476,8843,20734,41797,33477,6963,28561,11732,33697,27186,22906,5673,19360,33937,29961,2121,50385,6266,32403,23359,17804,41477,50948,14529,30520,8553,4337,35427,6649,19578,49337,27032,20718,8459,9631,25505,50474,39158,3279,14074,4159,36580,8133,37074,36600,6312,3622,54282,14614,20002,30122,46733,41087,15183,24254,11327,49202,23058,42495,50585,42562,49597,32980,5807,54987,28967,47413,20969,43954,41916,53100,36326,9019,53308,34863,8470,10614,3391,9732,2558,16084,49762,1775,11455,29452,48916,15180,44477,55243,5944,32976,9751,50409,2838,4664,28194,21969,38256,42350,34288,32529,53394,37013,49813,28477,22748,49316,33021,37589,50477,16514,6049,40191,11388,22213,33834,46812,39606,25989,35907,13955,36073,41834,40653,41507,38483,37482,34904,15750,38376,52658,5837,52835,29474,50333,49802,37401,28535,51141,5112,49479,4727,7776,16434,16505,21689,49437,47692,18219,11300,42685,25022,18414,16582,24293,30968,140,5921,19232,36420,18248,55177,26223,50577,1225,34841,1260,24715,23263,47482,53840,10126,55114,40188,37865,610,36539,38411,47266,45725,16417,11718,22546,24260,13277,37771,53191,25219,6174,18104,39737,42866,38834,28950,2495,8912,8989,15542,54328,29852,14399,39819,7978,16169,49324,30611,50384,42924,50781,39397,23758,46235,25557,28293,28901,34707,23900,56,52209,4103,8322,45962,49194,21226,6783,19329,25663,22242,24076,27487,5639,36791,50805,45928,35534,24168,42398,18485,18892,38393,36433,23761,42544,52594,22966,19614,47641,6261,23235,23987,19102,14011,21796,28232,19540,14068,7217,17593,33643,1639,47389,27602,29941,33139,41562,48824,27153,241,34633,7168,53453,42866,29914,24289,22690,11594,35216,55433,36591,49024,23141,16225,51141,46763,34515,8611,29749,10067,40475,17737,20360,55092,18435,46027,3562,31963,44499,3708,13352,14885,7676,15525,50650,11961,5417,2056,32064,38122,35872,32492,51106,51048,2424,52274,29288,3381,30475,43976,11765,40236,53271,23906,47466,17092,10445,50260,28748,17892,31454,10805,35300,2786,29929,5681,48347,3081,52175,26064,20047,26817,16962,46799,581,15856,12043,8859,29948,50175,16344,1801,13440,8507,17023,190,8472,23148,54934,32009,52433,45460,53171,33320,28811,29042,17879,20559,28570,29045,34999,48188,32761,46657,12401,52268,7909,28135,14672,1848,8127,18899,23977,32148,24670,14409,7920,34380,9831,40327,52094,15730,26619,3952,44092,33975,18370,50029,49666,51629,52046,46988,45158,15647,49742,14812,11566,42969,33326,30677,29905,40321,43302,20,11904,38902,49232,1670,55048,3625,10917,13844,48198,19303,51704,28392,45760,42219,16079,10222,20736,45241,40373,17713,41840,13719,16650,42551,28592,18697,55163,44392,4307,39635,35081,32575,43736,43268,50451,22648,32999,13332,17789,4579,34881,44944,19897,7689,27442,5095,35039,39474,19225,45914,49705,53850,35385,31772,53781,24774,41831,1078,27503,25884,24896,48925,14812,24452,35412,54381,53659,20387,36491,24689,15537,25278,16864,2164,35941,15744,30127,55471,16760,6655,3988,3791,19727,17926,53629,9935,31602,40661,14445,33155,42138,3940,16411,1566,12625,44861,16966,51311,3079,14829,10747,10224,41099,47411,44107,43613,50357,17000,8115,45331,40279,50031,44374,10710,7359,1310,760,13030,715,40855,3158,3079,8192,35654,23561,12652,21342,11827,6432,47920,22256,13653,48966,51720,30329,25173,47810,17918,406,8567,35491,24123,53008,13189,22660,29065,9459,1857,24072,31997,52711,37598,37428,42698,21920,2309,9176,4111,27810,55358,18700,437,3868,39524,40817,4374,44317,13372,4025,44185,41360,53414,30397,41696,46975,40613,26722,20336,37476,3106,31208,12708,12821,7184,258,22694,11067,842,4535,642,23157,44509,35643,9723,38490,50854,1757,48402,48942,35806,8244,4383,24183,32553,50098,20264,17202,27710,52957,52046,47636,43206,28609,53671,49041,5538,37195,34225,49031,25753,23699,42481,6060,31669,42380,16524,51629,30386,31059,54029,24628,38304,41104,11237,52444,12677,10658,53140,1412,11543,26427,2332,37809,18239,38514,32017,18948,3563,17969,45978,50911,3846,42278,48226,22737,2158,7997,19785,37246,44085,52187,39373,31004,1545,33888,2274,55418,48259,1673,9576,22619,17338,35582,38961,43046,39948,19170,52990,4096,27338,47028,44810,20536,17018,18215,12592,11436,53152,41286,19736,16232,40233,52795,9596,23747,32043,17901,32367,25468,39559,14194,23012,26340,44639,41520,11813,16790,17104,27470,11735,50418,40679,26380,275,50946,25456,14990,17100,10047,50144,36846,25511,4372,1494,8905,10207,23425,44603,43456,41168,49512,27899,46313,36180,15043,48996,43629,47260,6588,32707,8271,54302,1610,22905,46320,39783,19625,21227,13153,3979,55443,35699,25055,46765,25851,44119,16774,38015,3695,4263,7593,25043,21211,1066,14240,8098,54727,43098,38299,48489,33573,26732,25438,37599,13777,5580,48597,30197,4141,53660,52006,50419,49602,13062,13300,19611,1610,5999,40068,54850,35019,19143,20715,25820,34431,25479,19902,29300,2134,25406,51034,38081,1009,35582,52960,32636,33570,8287,17861,22636,4535,6075,4918,14416,45010,52948,45087,26363,51932,22315,3344,26367,2897,49883,33507,22455,50350,50,14490,20930,6640,15787,42928,14297,18814,7744,30786,52358,8463,1909,16108,1276,52299,48264,11334,55067,28727,30010,14756,7927,19316,45226,16227,12914,37968,45586,17786,27819,10979,17999,10662,1435,18430,9008,51627,30999,2421,38498,8128,32504,27162,24174,1709,10719,24499,29076,28016,30295,32546,37186,25393,33364,24890,10199,40836,16278,13683,17515,11865,5010,14535,49141,4109,25154,34830,52516,53262,29945,14327,48286,20755,40698,16506,2894,32444,18907,16324,24972,31248,12308,16328,28243,44771,34307,31920,46582,1970,49393,15719,37746,47497,19172,41330,54910,33447,53994,26837,43209,15516,50966,15007,7908,10326,21968,24346,35775,13064,43627,55018,19432,7516,7525,24323,14193,41761,29097,39446,995,34188,53369,31571,47948,37902,49311,256,48643,42949,31554,34885,36250,13961,17201,27637,9084,42953,25237,51706,43580,21970,2245,24194,49149,22814,27442,23079,29643,32552,48376,37881,53412,4664,1393,32044,15435,4464,11773,14314,45361,7553,19295,16243,54650,50966,40940,2992,50089,43054,19522,12174,4945,25210,39162,16237,9009,3323,54523,33137,24786,11026,19818,14652,36170,40984,6189,54659,22283,27150,3357,18346,20838,16130,10306,1124,24518,28315,16221,50725,30722,5634,38099,33702,25151,30179,34740,32227,542,46937,5700,13853,50492,10126,2293,23937,50820,45434,20009,6923,26902,50138,18862,44693,41563,4723,7909,48192,24268}; + // expected = 0; + // actual = test.findPairs(nums, k); + // assertEquals(expected, actual); + // } + + @Test + public void test5() { + k = -1; + nums = new int[] {1, 2, 3, 4, 5}; + expected = 0; + actual = test.findPairs(nums, k); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_533Test.java b/src/test/java/com/fishercoder/firstthousand/_533Test.java new file mode 100644 index 0000000000..86fbc6db6c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_533Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._533; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _533Test { + private _533.Solution1 solution1; + private static char[][] picture; + + @BeforeEach + public void setup() { + solution1 = new _533.Solution1(); + } + + @Test + public void test1() { + picture = + new char[][] { + {'W', 'B', 'W', 'B', 'B', 'W'}, + {'W', 'B', 'W', 'B', 'B', 'W'}, + {'W', 'B', 'W', 'B', 'B', 'W'}, + {'W', 'W', 'B', 'W', 'B', 'W'} + }; + assertEquals(6, solution1.findBlackPixel(picture, 3)); + } + + @Test + public void test2() { + picture = new char[][] {{'B'}}; + assertEquals(1, solution1.findBlackPixel(picture, 1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_536Test.java b/src/test/java/com/fishercoder/firstthousand/_536Test.java new file mode 100644 index 0000000000..257548b147 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_536Test.java @@ -0,0 +1,55 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._536; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _536Test { + private _536.Solution1 solution1; + private _536.Solution2 solution2; + private static TreeNode expected; + private static String s; + + @BeforeEach + public void setup() { + solution1 = new _536.Solution1(); + solution2 = new _536.Solution2(); + } + + @Test + public void test1() { + s = "4(2(3)(1))(6(5))"; + expected = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 3, 1, 5)); + assertEquals(expected, solution1.str2tree(s)); + assertEquals(expected, solution2.str2tree(s)); + } + + @Test + public void test2() { + s = "51(232)(434)"; + expected = TreeUtils.constructBinaryTree(Arrays.asList(51, 232, 434)); + assertEquals(expected, solution1.str2tree(s)); + assertEquals(expected, solution2.str2tree(s)); + } + + @Test + public void test3() { + s = "-4(2(3)(1))(6(5)(7))"; + expected = TreeUtils.constructBinaryTree(Arrays.asList(-4, 2, 6, 3, 1, 5, 7)); + assertEquals(expected, solution1.str2tree(s)); + assertEquals(expected, solution2.str2tree(s)); + } + + @Test + public void test4() { + s = "4"; + expected = TreeUtils.constructBinaryTree(Arrays.asList(4)); + assertEquals(expected, solution1.str2tree(s)); + assertEquals(expected, solution2.str2tree(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_537Test.java b/src/test/java/com/fishercoder/firstthousand/_537Test.java new file mode 100644 index 0000000000..a9e6067e07 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_537Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._537; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/25/17. */ +public class _537Test { + private _537.Solution1 solution1; + private _537.Solution2 solution2; + private static String expected; + private static String a; + private static String b; + + @BeforeEach + public void setup() { + solution1 = new _537.Solution1(); + solution2 = new _537.Solution2(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + expected = "0+2i"; + a = "1+1i"; + b = "1+1i"; + assertEquals(expected, solution1.complexNumberMultiply(a, b)); + assertEquals(expected, solution2.complexNumberMultiply(a, b)); + } + + @Test + public void test2() { + expected = "0+-2i"; + a = "1+-1i"; + b = "1+-1i"; + assertEquals(expected, solution2.complexNumberMultiply(a, b)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_538Test.java b/src/test/java/com/fishercoder/firstthousand/_538Test.java new file mode 100644 index 0000000000..b7a4c95822 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_538Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.solutions.firstthousand._538; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _538Test { + private _538.Solution1 solution1; + private _538.Solution2 solution2; + private static TreeNode expectedRoot; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _538.Solution1(); + solution2 = new _538.Solution2(); + } + + @Test + public void test1() { + root = new TreeNode(5); + root.left = new TreeNode(2); + root.right = new TreeNode(13); + expectedRoot = new TreeNode(18); + expectedRoot.left = new TreeNode(20); + expectedRoot.right = new TreeNode(13); + assertEquals(expectedRoot.toString(), solution1.convertBST(root).toString()); + } + + @Test + public void test2() { + root = new TreeNode(5); + root.left = new TreeNode(2); + root.right = new TreeNode(13); + expectedRoot = new TreeNode(18); + expectedRoot.left = new TreeNode(20); + expectedRoot.right = new TreeNode(13); + assertEquals(expectedRoot.toString(), solution2.convertBST(root).toString()); + } + + @Test + public void test3() { + root = null; + expectedRoot = null; + assertEquals(expectedRoot, solution1.convertBST(root)); + assertEquals(expectedRoot, solution2.convertBST(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_539Test.java b/src/test/java/com/fishercoder/firstthousand/_539Test.java new file mode 100644 index 0000000000..79bd4efce0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_539Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._539; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _539Test { + private _539.Soluiton1 soluiton1; + private static int expected; + private static int actual; + private static List timePoints; + + @BeforeEach + public void setup() { + soluiton1 = new _539.Soluiton1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = 0; + actual = 0; + } + + @Test + public void test1() { + timePoints = new ArrayList<>(Arrays.asList("23:59", "00:00")); + expected = 1; + actual = soluiton1.findMinDifference(timePoints); + assertEquals(expected, actual); + } + + @Test + public void test2() { + timePoints = new ArrayList<>(Arrays.asList("23:59", "00:00", "01:20")); + expected = 1; + actual = soluiton1.findMinDifference(timePoints); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_53Test.java b/src/test/java/com/fishercoder/firstthousand/_53Test.java new file mode 100644 index 0000000000..719477196f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_53Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._53; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _53Test { + private _53.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _53.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {-2, 1, -3, 4, -1, 2, 1, -5, 4}; + assertEquals(6, solution1.maxSubArray(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_540Test.java b/src/test/java/com/fishercoder/firstthousand/_540Test.java new file mode 100644 index 0000000000..a75a374bf2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_540Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._540; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _540Test { + private _540.Solution1 solution1; + private _540.Solution2 solution2; + private _540.Solution3 solution3; + private static int[] nums; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _540.Solution1(); + solution2 = new _540.Solution2(); + solution3 = new _540.Solution3(); + } + + @Test + public void test1() { + nums = new int[] {1, 1, 2, 3, 3, 4, 4, 8, 8}; + expected = 2; + assertEquals(expected, solution1.singleNonDuplicate(nums)); + assertEquals(expected, solution2.singleNonDuplicate(nums)); + assertEquals(expected, solution3.singleNonDuplicate(nums)); + } + + @Test + public void test2() { + nums = new int[] {3, 3, 7, 7, 10, 11, 11}; + expected = 10; + assertEquals(expected, solution1.singleNonDuplicate(nums)); + assertEquals(expected, solution2.singleNonDuplicate(nums)); + assertEquals(expected, solution3.singleNonDuplicate(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 1, 2}; + expected = 2; + assertEquals(expected, solution1.singleNonDuplicate(nums)); + assertEquals(expected, solution2.singleNonDuplicate(nums)); + assertEquals(expected, solution3.singleNonDuplicate(nums)); + } + + @Test + public void test4() { + nums = new int[] {1, 1, 2, 2, 3}; + expected = 3; + assertEquals(expected, solution1.singleNonDuplicate(nums)); + assertEquals(expected, solution2.singleNonDuplicate(nums)); + assertEquals(expected, solution3.singleNonDuplicate(nums)); + } + + @Test + public void test5() { + nums = new int[] {1, 2, 2, 3, 3}; + expected = 1; + assertEquals(expected, solution1.singleNonDuplicate(nums)); + assertEquals(expected, solution2.singleNonDuplicate(nums)); + assertEquals(expected, solution3.singleNonDuplicate(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_541Test.java b/src/test/java/com/fishercoder/firstthousand/_541Test.java new file mode 100644 index 0000000000..252c6de4b7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_541Test.java @@ -0,0 +1,80 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._541; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _541Test { + private _541.Solution1 solution1; + private static String expected; + private static String actual; + private static String s; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _541.Solution1(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + s = "abcd"; + k = 5; + expected = "dcba"; + actual = solution1.reverseStr(s, k); + assertEquals(expected, actual); + } + + @Test + public void test2() { + s = "abcdefg"; + k = 2; + expected = "bacdfeg"; + actual = solution1.reverseStr(s, k); + assertEquals(expected, actual); + } + + @Test + public void test3() { + s = "abcd"; + k = 4; + expected = "dcba"; + actual = solution1.reverseStr(s, k); + assertEquals(expected, actual); + } + + @Test + public void test4() { + s = "abcdefg"; + k = 3; + expected = "cbadefg"; + actual = solution1.reverseStr(s, k); + assertEquals(expected, actual); + } + + @Test + public void test5() { + s = "abcd"; + k = 3; + expected = "cbad"; + actual = solution1.reverseStr(s, k); + assertEquals(expected, actual); + } + + @Test + public void test6() { + s = + "hyzqyljrnigxvdtneasepfahmtyhlohwxmkqcdfehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqlimjkfnqcqnajmebeddqsgl"; + System.out.println("s.length() = " + s.length()); + k = 39; + expected = + "fdcqkmxwholhytmhafpesaentdvxginrjlyqzyhehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqllgsqddebemjanqcqnfkjmi"; + actual = solution1.reverseStr(s, k); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_542Test.java b/src/test/java/com/fishercoder/firstthousand/_542Test.java new file mode 100644 index 0000000000..897be93688 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_542Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._542; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _542Test { + private _542.Solution1 solution1; + private _542.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _542.Solution1(); + solution2 = new _542.Solution2(); + } + + @Test + public void test1() { + int[][] matrix = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,1,0,1,1,1,1,1,1,1],[1,1,0,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,1,1,0],[1,1,1,1,1,1,0,0,1,0],[1,0,0,1,1,1,0,1,0,1],[0,0,1,0,0,1,1,0,0,1],[0,1,0,1,1,1,1,1,1,1],[1,0,0,1,1,0,0,0,0,0],[0,0,1,1,1,1,0,1,1,1],[1,1,0,0,1,0,1,0,1,1]"); + int[][] expected = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[2,1,0,1,2,2,2,3,3,2],[2,1,0,1,1,1,1,2,2,1],[3,2,1,1,0,0,0,1,1,0],[2,1,1,2,1,1,0,0,1,0],[1,0,0,1,1,1,0,1,0,1],[0,0,1,0,0,1,1,0,0,1],[0,1,0,1,1,1,1,1,1,1],[1,0,0,1,1,0,0,0,0,0],[0,0,1,1,2,1,0,1,1,1],[1,1,0,0,1,0,1,0,1,2]"); + assertArrayEquals(expected, solution1.updateMatrix(matrix)); + assertArrayEquals(expected, solution2.updateMatrix(matrix)); + } + + @Test + public void test2() { + int[][] matrix = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,0,0],[0,1,0],[0,0,0]"); + int[][] expected = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,0,0],[0,1,0],[0,0,0]"); + assertArrayEquals(expected, solution1.updateMatrix(matrix)); + assertArrayEquals(expected, solution2.updateMatrix(matrix)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_543Test.java b/src/test/java/com/fishercoder/firstthousand/_543Test.java new file mode 100644 index 0000000000..96b3ee4068 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_543Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._543; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _543Test { + private _543.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _543.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); + TreeUtils.printBinaryTree(root); + assertEquals(3, solution1.diameterOfBinaryTree(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_544Test.java b/src/test/java/com/fishercoder/firstthousand/_544Test.java new file mode 100644 index 0000000000..5f847ed228 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_544Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._544; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _544Test { + private _544 test; + private static int n; + private static String expected; + private static String actual; + + @BeforeEach + public void setup() { + test = new _544(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + n = 2; + expected = "(1,2)"; + actual = test.findContestMatch(n); + assertEquals(expected, actual); + } + + @Test + public void test2() { + n = 4; + expected = "((1,4),(2,3))"; + actual = test.findContestMatch(n); + assertEquals(expected, actual); + } + + @Test + public void test3() { + n = 8; + expected = "(((1,8),(4,5)),((2,7),(3,6)))"; + actual = test.findContestMatch(n); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_545Test.java b/src/test/java/com/fishercoder/firstthousand/_545Test.java new file mode 100644 index 0000000000..2af7bc81ba --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_545Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._545; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _545Test { + private _545.Solution1 test; + private static TreeNode root; + private static List expected; + + @BeforeEach + public void setup() { + test = new _545.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4, 5, null, 6, 7)); + TreeUtils.printBinaryTree(root); + expected = Arrays.asList(1, 2, 4, 6, 7, 5, 3); + assertEquals(expected, test.boundaryOfBinaryTree(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_547Test.java b/src/test/java/com/fishercoder/firstthousand/_547Test.java new file mode 100644 index 0000000000..83f44f2ed5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_547Test.java @@ -0,0 +1,73 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._547; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 1/9/17. */ +public class _547Test { + private _547.Solution1 test; + private static int expected; + private static int actual; + private static int[][] isConnected; + + @BeforeEach + public void setup() { + test = new _547.Solution1(); + } + + @Test + public void test1() { + isConnected = + new int[][] { + {1, 1, 0}, + {1, 1, 0}, + {0, 0, 1}, + }; + expected = 2; + actual = test.findCircleNum(isConnected); + assertEquals(expected, actual); + } + + @Test + public void test2() { + isConnected = + new int[][] { + {1, 1, 0}, + {1, 1, 1}, + {0, 1, 1}, + }; + expected = 1; + actual = test.findCircleNum(isConnected); + assertEquals(expected, actual); + } + + @Test + public void test3() { + isConnected = + new int[][] { + {1, 0, 0, 1}, + {0, 1, 1, 0}, + {0, 1, 1, 1}, + {1, 0, 1, 1}, + }; + expected = 1; + actual = test.findCircleNum(isConnected); + assertEquals(expected, actual); + } + + @Test + public void test4() { + isConnected = + new int[][] { + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1}, + }; + expected = 3; + actual = test.findCircleNum(isConnected); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_548Test.java b/src/test/java/com/fishercoder/firstthousand/_548Test.java new file mode 100644 index 0000000000..5454af53bb --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_548Test.java @@ -0,0 +1,137 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._548; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _548Test { + private _548.Solution1 test; + private static boolean expected; + private static boolean actual; + private static int[] nums; + + @BeforeEach + public void setup() { + test = new _548.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + nums = new int[1000]; + } + + @Test + public void test1() { + nums = new int[] {1, 2, 1, 2, 1, 2, 1}; + expected = true; + actual = test.splitArray(nums); + assertEquals(expected, actual); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 1, 2, 1, 2, 1, 2}; + expected = false; + actual = test.splitArray(nums); + assertEquals(expected, actual); + } + + @Test + public void test3() { + nums = + new int[] { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + }; + expected = false; + long start = System.currentTimeMillis(); + actual = test.splitArray(nums); + System.out.println( + "It took " + (System.currentTimeMillis() - start) + " ms to solve this test case."); + assertEquals(expected, actual); + } + + @Test + public void test4() { + // equalSum is 3, j = 6, k = 9 + nums = new int[] {1, 2, 1, 3, 0, 0, 2, 2, 1, 3, 3}; + expected = true; + actual = test.splitArray(nums); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/_549Test.java b/src/test/java/com/fishercoder/firstthousand/_549Test.java similarity index 80% rename from src/test/java/com/fishercoder/_549Test.java rename to src/test/java/com/fishercoder/firstthousand/_549Test.java index 7bc447e2fc..cdc0555835 100644 --- a/src/test/java/com/fishercoder/_549Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_549Test.java @@ -1,27 +1,24 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._549; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - +import com.fishercoder.solutions.firstthousand._549; import java.util.Arrays; - -import static junit.framework.Assert.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _549Test { - private static _549.Solution1 solution1; + private _549.Solution1 solution1; private static int expected; private static int actual; private static TreeNode root; - @BeforeClass - public static void setup() { - } + @BeforeEach + public void setup() {} - @Before + @BeforeEach public void setupForEachTest() { root = null; actual = 0; diff --git a/src/test/java/com/fishercoder/firstthousand/_54Test.java b/src/test/java/com/fishercoder/firstthousand/_54Test.java new file mode 100644 index 0000000000..e353f45695 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_54Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._54; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _54Test { + private _54.Solution1 solution1; + private static int[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _54.Solution1(); + } + + @Test + public void test1() { + matrix = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + }; + assertEquals(Arrays.asList(1, 2, 3, 6, 9, 8, 7, 4, 5), solution1.spiralOrder(matrix)); + } + + @Test + public void test2() { + matrix = new int[][] {}; + assertEquals(Arrays.asList(), solution1.spiralOrder(matrix)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_551Test.java b/src/test/java/com/fishercoder/firstthousand/_551Test.java new file mode 100644 index 0000000000..10fb164575 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_551Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._551; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _551Test { + private _551.Solution1 test; + private static boolean expected; + private static boolean actual; + private static String s; + + @BeforeEach + public void setup() { + test = new _551.Solution1(); + } + + @Test + public void test1() { + s = "ALLAPPL"; + expected = false; + actual = test.checkRecord(s); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_553Test.java b/src/test/java/com/fishercoder/firstthousand/_553Test.java new file mode 100644 index 0000000000..df45413c09 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_553Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._553; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/25/17. */ +public class _553Test { + private _553.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _553.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1000, 100, 10, 2}; + assertEquals("1000/(100/10/2)", solution1.optimalDivision(nums)); + } + + @Test + public void test2() { + nums = new int[] {1000, 100, 40, 10, 2}; + assertEquals("1000/(100/40/10/2)", solution1.optimalDivision(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_554Test.java b/src/test/java/com/fishercoder/firstthousand/_554Test.java new file mode 100644 index 0000000000..177e2e2a8c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_554Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._554; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _554Test { + private _554.Solution1 test; + private static int expected; + private static int actual; + private static List> wall; + + @BeforeEach + public void setup() { + test = new _554.Solution1(); + } + + @BeforeEach + public void setupForEachTest() {} + + @Test + public void test1() { + wall = new ArrayList<>(); + wall.add(Arrays.asList(1, 2, 2, 1)); + wall.add(Arrays.asList(3, 1, 2)); + wall.add(Arrays.asList(1, 3, 2)); + wall.add(Arrays.asList(2, 4)); + wall.add(Arrays.asList(3, 1, 2)); + wall.add(Arrays.asList(1, 3, 1, 1)); + expected = 2; + actual = test.leastBricks(wall); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_555Test.java b/src/test/java/com/fishercoder/firstthousand/_555Test.java new file mode 100644 index 0000000000..5a57cd49b0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_555Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._555; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 4/29/17. */ +public class _555Test { + private _555.Solution1 solution1; + private static String expected; + private static String actual; + private static String[] strs; + + @BeforeEach + public void setup() { + solution1 = new _555.Solution1(); + } + + @Test + public void test1() { + strs = new String[] {"abc", "xyz"}; + expected = "zyxcba"; + actual = solution1.splitLoopedString(strs); + assertEquals(expected, actual); + } + + @Test + public void test2() { + strs = new String[] {"lc", "evol", "cdy"}; + expected = "ylclovecd"; + actual = solution1.splitLoopedString(strs); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/_556Test.java b/src/test/java/com/fishercoder/firstthousand/_556Test.java similarity index 75% rename from src/test/java/com/fishercoder/_556Test.java rename to src/test/java/com/fishercoder/firstthousand/_556Test.java index 6e8edf4747..5951007176 100644 --- a/src/test/java/com/fishercoder/_556Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_556Test.java @@ -1,20 +1,20 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._556; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import com.fishercoder.solutions.firstthousand._556; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _556Test { - private static _556.Solution1 solution1; + private _556.Solution1 solution1; private static int n; private static int expected; private static int actual; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _556.Solution1(); } @@ -58,5 +58,4 @@ public void test5() { actual = solution1.nextGreaterElement(n); assertEquals(expected, actual); } - } diff --git a/src/test/java/com/fishercoder/firstthousand/_559Test.java b/src/test/java/com/fishercoder/firstthousand/_559Test.java new file mode 100644 index 0000000000..4f170c1f87 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_559Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.Node; +import com.fishercoder.solutions.firstthousand._559; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _559Test { + private _559.Solution1 solution1; + private static Node root; + + @BeforeEach + public void setUp() { + solution1 = new _559.Solution1(); + } + + @Test + public void test1() { + root = new Node(1); + Node node3 = new Node(3); + Node node2 = new Node(2); + Node node4 = new Node(4); + root.children = Arrays.asList(node3, node2, node4); + Node node5 = new Node(5); + Node node6 = new Node(6); + node3.children = Arrays.asList(node5, node6); + + assertEquals(3, solution1.maxDepth(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_55Test.java b/src/test/java/com/fishercoder/firstthousand/_55Test.java new file mode 100644 index 0000000000..409487add8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_55Test.java @@ -0,0 +1,104 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._55; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _55Test { + private _55.Solution1 solution1; + private _55.Solution2 solution2; + private _55.Solution3 solution3; + private _55.Solution4 solution4; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _55.Solution1(); + solution2 = new _55.Solution2(); + solution3 = new _55.Solution3(); + solution4 = new _55.Solution4(); + } + + @Test + public void test1() { + nums = new int[] {0, 2, 3}; + assertEquals(false, solution1.canJump(nums)); + assertEquals(false, solution2.canJump(nums)); + assertEquals(false, solution3.canJump(nums)); + assertEquals(false, solution4.canJump(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2}; + assertEquals(true, solution1.canJump(nums)); + assertEquals(true, solution2.canJump(nums)); + assertEquals(true, solution3.canJump(nums)); + assertEquals(true, solution4.canJump(nums)); + } + + @Test + public void test3() { + nums = new int[] {2, 3, 1, 1, 4}; + assertEquals(true, solution1.canJump(nums)); + assertEquals(true, solution2.canJump(nums)); + assertEquals(true, solution3.canJump(nums)); + assertEquals(true, solution4.canJump(nums)); + } + + @Test + public void test4() { + nums = new int[] {5, 9, 3, 2, 1, 0, 2, 3, 3, 1, 0, 0}; + assertEquals(true, solution1.canJump(nums)); + assertEquals(true, solution2.canJump(nums)); + assertEquals(true, solution3.canJump(nums)); + assertEquals(true, solution4.canJump(nums)); + } + + @Test + public void test5() { + nums = new int[] {2, 0, 0}; + assertEquals(true, solution1.canJump(nums)); + assertEquals(true, solution2.canJump(nums)); + assertEquals(true, solution3.canJump(nums)); + assertEquals(true, solution4.canJump(nums)); + } + + @Test + public void test6() { + nums = new int[] {2, 0}; + assertEquals(true, solution1.canJump(nums)); + assertEquals(true, solution2.canJump(nums)); + assertEquals(true, solution3.canJump(nums)); + assertEquals(true, solution4.canJump(nums)); + } + + @Test + public void test7() { + nums = new int[] {1, 1, 0, 1}; + assertEquals(false, solution1.canJump(nums)); + assertEquals(false, solution2.canJump(nums)); + assertEquals(false, solution3.canJump(nums)); + assertEquals(false, solution4.canJump(nums)); + } + + @Test + public void test8() { + nums = new int[] {0}; + assertEquals(true, solution1.canJump(nums)); + assertEquals(true, solution2.canJump(nums)); + assertEquals(true, solution3.canJump(nums)); + assertEquals(true, solution4.canJump(nums)); + } + + @Test + public void test9() { + nums = new int[] {3, 2, 1, 0, 4}; + assertEquals(false, solution1.canJump(nums)); + assertEquals(false, solution2.canJump(nums)); + assertEquals(false, solution3.canJump(nums)); + assertEquals(false, solution4.canJump(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_560Test.java b/src/test/java/com/fishercoder/firstthousand/_560Test.java new file mode 100644 index 0000000000..0f7c8954cc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_560Test.java @@ -0,0 +1,77 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._560; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _560Test { + private _560.Solution1 solution1; + private _560.Solution2 solution2; + private static int expected; + private static int actual; + private static int[] nums; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _560.Solution1(); + solution2 = new _560.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 1, 1}; + k = 2; + expected = 2; + actual = solution1.subarraySum(nums, k); + assertEquals(expected, actual); + actual = solution2.subarraySum(nums, k); + assertEquals(expected, actual); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 3}; + k = 3; + expected = 2; + actual = solution1.subarraySum(nums, k); + assertEquals(expected, actual); + actual = solution2.subarraySum(nums, k); + assertEquals(expected, actual); + } + + @Test + public void test3() { + nums = new int[] {1, 1}; + k = 1; + expected = 2; + actual = solution1.subarraySum(nums, k); + assertEquals(expected, actual); + actual = solution2.subarraySum(nums, k); + assertEquals(expected, actual); + } + + @Test + public void test4() { + nums = new int[] {0, 0}; + k = 0; + expected = 3; + actual = solution1.subarraySum(nums, k); + assertEquals(expected, actual); + actual = solution2.subarraySum(nums, k); + assertEquals(expected, actual); + } + + @Test + public void test5() { + nums = new int[] {100, 1, 2, 3, 4}; + k = 3; + expected = 2; + actual = solution1.subarraySum(nums, k); + assertEquals(expected, actual); + actual = solution2.subarraySum(nums, k); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_561Test.java b/src/test/java/com/fishercoder/firstthousand/_561Test.java new file mode 100644 index 0000000000..64191faa9b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_561Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._561; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 4/23/17. */ +public class _561Test { + private _561.Solution1 solution1; + private static int expected; + private static int actual; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _561.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 4, 3, 2}; + expected = 4; + actual = solution1.arrayPairSum(nums); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_562Test.java b/src/test/java/com/fishercoder/firstthousand/_562Test.java new file mode 100644 index 0000000000..1443891832 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_562Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._562; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 4/23/17. */ +public class _562Test { + private _562.Solution1 solution1; + private static int expected; + private static int actual; + private static int[][] M; + + @BeforeEach + public void setup() { + solution1 = new _562.Solution1(); + } + + @Test + public void test1() { + M = + new int[][] { + {0, 1, 1, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 1} + }; + expected = 3; + actual = solution1.longestLine(M); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_563Test.java b/src/test/java/com/fishercoder/firstthousand/_563Test.java new file mode 100644 index 0000000000..f3ea932725 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_563Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.solutions.firstthousand._563; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 4/23/17. */ +public class _563Test { + private _563.Solution1 solution1; + private static int expected; + private static int actual; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _563.Solution1(); + actual = 0; + } + + @Test + public void test1() { + root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + expected = 1; + actual = solution1.findTilt(root); + assertEquals(expected, actual); + } + + @Disabled + @Test + public void test2() { + root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + root.left.left = new TreeNode(4); + root.right.left = new TreeNode(5); + expected = 11; + actual = solution1.findTilt(root); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_566Test.java b/src/test/java/com/fishercoder/firstthousand/_566Test.java new file mode 100644 index 0000000000..2ed54921b4 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_566Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._566; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 4/29/17. */ +public class _566Test { + private _566.Solution1 solution1; + private _566.Solution2 solution2; + private static int[][] expected; + private static int[][] actual; + private static int[][] nums; + private static int r; + private static int c; + + @BeforeEach + public void setup() { + solution1 = new _566.Solution1(); + solution2 = new _566.Solution2(); + } + + @Test + public void test1() { + nums = + new int[][] { + {1, 2}, + {3, 4}, + }; + r = 1; + c = 4; + expected = new int[][] {{1, 2, 3, 4}}; + actual = solution1.matrixReshape(nums, r, c); + assertArrayEquals(expected, actual); + actual = solution2.matrixReshape(nums, r, c); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_567Test.java b/src/test/java/com/fishercoder/firstthousand/_567Test.java new file mode 100644 index 0000000000..e1097f856c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_567Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._567; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _567Test { + private _567.Solution1 solution1; + private _567.Solution2 solution2; + private static boolean expected; + private static boolean actual; + private static String s1; + private static String s2; + + @BeforeEach + public void setup() { + solution1 = new _567.Solution1(); + solution2 = new _567.Solution2(); + } + + @Test + public void test1() { + s1 = "ab"; + s2 = "eidbaooo"; + expected = true; + actual = solution1.checkInclusion(s1, s2); + actual = solution2.checkInclusion(s1, s2); + assertEquals(expected, actual); + } + + @Test + public void test2() { + s1 = "adc"; + s2 = "dcda"; + expected = true; + actual = solution1.checkInclusion(s1, s2); + actual = solution2.checkInclusion(s1, s2); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_56Test.java b/src/test/java/com/fishercoder/firstthousand/_56Test.java new file mode 100644 index 0000000000..5e3f64fc9f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_56Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._56; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _56Test { + private _56.Solution1 solution1; + private static int[][] intervals; + private static int[][] expected; + + @BeforeEach + public void setup() { + solution1 = new _56.Solution1(); + } + + @Test + public void test1() { + intervals = + new int[][] { + {2, 3}, + {5, 5}, + {2, 2}, + {3, 4}, + {3, 4} + }; + expected = + new int[][] { + {2, 4}, + {5, 5} + }; + assertArrayEquals(expected, solution1.merge(intervals)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_572Test.java b/src/test/java/com/fishercoder/firstthousand/_572Test.java new file mode 100644 index 0000000000..6b65129c14 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_572Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._572; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _572Test { + private _572.Solution1 solution1; + private static boolean expected; + private static TreeNode root; + private static TreeNode subRoot; + + @BeforeEach + public void setup() { + solution1 = new _572.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 4, 5, 1, 2)); + TreeUtils.printBinaryTree(root); + subRoot = TreeUtils.constructBinaryTree(Arrays.asList(4, 1, 2)); + expected = true; + assertEquals(expected, solution1.isSubtree(root, subRoot)); + } + + @Test + public void test2() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList(3, 4, 5, 1, 2, null, null, null, null, 0)); + TreeUtils.printBinaryTree(root); + subRoot = TreeUtils.constructBinaryTree(Arrays.asList(4, 1, 2)); + expected = false; + assertEquals(expected, solution1.isSubtree(root, subRoot)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_575Test.java b/src/test/java/com/fishercoder/firstthousand/_575Test.java new file mode 100644 index 0000000000..f94ff73108 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_575Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._575; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _575Test { + private _575.Solution1 solution1; + private static int expected; + private static int actual; + private static int[] candyType; + + @BeforeEach + public void setup() { + solution1 = new _575.Solution1(); + } + + @Test + public void test1() { + candyType = new int[] {1, 1, 2, 3}; + expected = 2; + actual = solution1.distributeCandies(candyType); + assertEquals(expected, actual); + } + + @Test + public void test2() { + candyType = new int[] {1, 1, 2, 2, 3, 3}; + expected = 3; + actual = solution1.distributeCandies(candyType); + assertEquals(expected, actual); + } + + @Test + public void test3() { + candyType = new int[] {1000, 1, 1, 1}; + expected = 2; + actual = solution1.distributeCandies(candyType); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_57Test.java b/src/test/java/com/fishercoder/firstthousand/_57Test.java new file mode 100644 index 0000000000..c0dea9517f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_57Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._57; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _57Test { + private _57.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _57.Solution1(); + } + + @Test + public void test1() { + Assertions.assertArrayEquals( + new int[][] { + {1, 5}, + {6, 9} + }, + solution1.insert( + new int[][] { + {1, 3}, + {6, 9} + }, + new int[] {2, 5})); + } + + @Test + public void test2() { + Assertions.assertArrayEquals( + new int[][] { + {1, 2}, + {3, 10}, + {12, 16} + }, + solution1.insert( + new int[][] { + {1, 2}, + {3, 5}, + {6, 7}, + {8, 10}, + {12, 16} + }, + new int[] {4, 9})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_581Test.java b/src/test/java/com/fishercoder/firstthousand/_581Test.java new file mode 100644 index 0000000000..f5da2c1173 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_581Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._581; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _581Test { + private _581.Solution1 solution1; + private _581.Solution2 solution2; + private _581.Solution3 solution3; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _581.Solution1(); + solution2 = new _581.Solution2(); + solution3 = new _581.Solution3(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3, 4}; + assertEquals(0, solution1.findUnsortedSubarray(nums)); + assertEquals(0, solution2.findUnsortedSubarray(nums)); + } + + @Test + public void test2() { + nums = new int[] {2, 6, 4, 8, 10, 9, 15}; + assertEquals(5, solution1.findUnsortedSubarray(nums)); + assertEquals(5, solution2.findUnsortedSubarray(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_582Test.java b/src/test/java/com/fishercoder/firstthousand/_582Test.java new file mode 100644 index 0000000000..62ecb4f54e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_582Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._582; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/18/17. */ +public class _582Test { + private _582.Solution1 solution1; + private static List pid; + private static List ppid; + private static List expected; + private static Integer kill; + + @BeforeEach + public void setup() { + solution1 = new _582.Solution1(); + } + + @Test + public void test1() { + pid = Arrays.asList(1, 3, 10, 5); + ppid = Arrays.asList(3, 0, 5, 3); + kill = 5; + expected = Arrays.asList(5, 10); + assertEquals(expected, solution1.killProcess(pid, ppid, kill)); + } + + @Test + public void test2() { + pid = Arrays.asList(1, 3, 10, 5); + ppid = Arrays.asList(3, 0, 5, 3); + kill = 3; + expected = Arrays.asList(3, 1, 5, 10); + assertEquals(expected, solution1.killProcess(pid, ppid, kill)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_583Test.java b/src/test/java/com/fishercoder/firstthousand/_583Test.java new file mode 100644 index 0000000000..05f97356e5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_583Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._583; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/18/17. */ +public class _583Test { + private _583.Solution1 solution1; + private static String word1; + private static String word2; + + @BeforeEach + public void setup() { + solution1 = new _583.Solution1(); + } + + @Test + public void test1() { + word1 = "sea"; + word2 = "eat"; + assertEquals(2, solution1.minDistance(word1, word2)); + } + + @Test + public void test2() { + word1 = "sea"; + word2 = "ate"; + assertEquals(4, solution1.minDistance(word1, word2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_588Test.java b/src/test/java/com/fishercoder/firstthousand/_588Test.java new file mode 100644 index 0000000000..c0381ef3e6 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_588Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._588; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/23/17. */ +public class _588Test { + + private _588.Solution1.FileSystem fileSystem; + + @BeforeEach + public void setup() {} + + @Test + public void test1() { + fileSystem = new _588.Solution1.FileSystem(); + List list = new ArrayList<>(); + assertEquals(list, fileSystem.ls("/")); + fileSystem.mkdir("/a/b/c"); + fileSystem.addContentToFile("/a/b/c/d", "hello"); + list.add("a"); + assertEquals(list, fileSystem.ls("/")); + assertEquals("hello", fileSystem.readContentFromFile("/a/b/c/d")); + } + + @Test + public void test2() { + fileSystem = new _588.Solution1.FileSystem(); + List list = new ArrayList<>(); + assertEquals(list, fileSystem.ls("/")); + fileSystem.mkdir("/a/b/c"); + list.add("c"); + assertEquals(list, fileSystem.ls("/a/b")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_589Test.java b/src/test/java/com/fishercoder/firstthousand/_589Test.java new file mode 100644 index 0000000000..febfb73ce7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_589Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.Node; +import com.fishercoder.solutions.firstthousand._589; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +public class _589Test { + private _589.Solution1 solution1; + private static Node root; + private static List expectedPreOrder; + + @BeforeEach + public void setUp() { + solution1 = new _589.Solution1(); + } + + @Test + @Disabled // todo: Node.createNaryTree method hasn't been implemented yet + public void test1() { + expectedPreOrder = Arrays.asList(1, 3, 5, 6, 2, 4); + root = Node.createNaryTree(expectedPreOrder); + assertEquals(expectedPreOrder, solution1.preorder(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_58Test.java b/src/test/java/com/fishercoder/firstthousand/_58Test.java new file mode 100644 index 0000000000..944facc135 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_58Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._58; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _58Test { + private _58.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _58.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.lengthOfLastWord("Hello World")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_591Test.java b/src/test/java/com/fishercoder/firstthousand/_591Test.java new file mode 100644 index 0000000000..d5f2ebba54 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_591Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._591; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _591Test { + private _591.Solution1 test; + + @BeforeEach + public void setup() { + test = new _591.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, test.isValid("

This is the first line ]]>
")); + } + + @Test + public void test2() { + assertEquals( + false, + test.isValid( + "This is the first line ]]>")); // tag_name is too long (> 9) + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_592Test.java b/src/test/java/com/fishercoder/firstthousand/_592Test.java new file mode 100644 index 0000000000..7ec319b828 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_592Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._592; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/23/17. */ +public class _592Test { + private _592.Solution1 test; + private static String expression; + + @BeforeEach + public void setup() { + test = new _592.Solution1(); + } + + @Test + public void test1() { + expression = "-1/2+1/2+1/3"; + assertEquals("1/3", test.fractionAddition(expression)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_593Test.java b/src/test/java/com/fishercoder/firstthousand/_593Test.java new file mode 100644 index 0000000000..e66e5c8ce4 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_593Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._593; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/22/17. */ +public class _593Test { + private _593.Solution1 solution1; + private static int[] p1; + private static int[] p2; + private static int[] p3; + private static int[] p4; + + @BeforeEach + public void setup() { + solution1 = new _593.Solution1(); + } + + @Test + public void test1() { + p1 = new int[] {0, 0}; + p2 = new int[] {1, 1}; + p3 = new int[] {1, 0}; + p4 = new int[] {0, 1}; + assertEquals(true, solution1.validSquare(p1, p2, p3, p4)); + } + + @Test + public void test2() { + p1 = new int[] {1, 1}; + p2 = new int[] {5, 3}; + p3 = new int[] {3, 5}; + p4 = new int[] {7, 7}; + assertEquals(false, solution1.validSquare(p1, p2, p3, p4)); + } + + @Test + public void test3() { + p1 = new int[] {0, 0}; + p2 = new int[] {0, 0}; + p3 = new int[] {0, 0}; + p4 = new int[] {0, 0}; + System.out.println(solution1.noDuplicate(p1, p2, p3, p4)); + assertEquals(false, solution1.validSquare(p1, p2, p3, p4)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_594Test.java b/src/test/java/com/fishercoder/firstthousand/_594Test.java new file mode 100644 index 0000000000..8d6a8211d7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_594Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._594; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/20/17. */ +public class _594Test { + private _594.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _594.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 3, 2, 2, 5, 2, 3, 7}; + assertEquals(5, solution1.findLHS(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_598Test.java b/src/test/java/com/fishercoder/firstthousand/_598Test.java new file mode 100644 index 0000000000..7df6b4fbd3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_598Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._598; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _598Test { + private _598.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _598.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 4, + solution1.maxCount( + 3, + 3, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[2,2],[3,3]"))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_59Test.java b/src/test/java/com/fishercoder/firstthousand/_59Test.java new file mode 100644 index 0000000000..c498ba65f5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_59Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._59; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _59Test { + private _59.Solution1 solution1; + private _59.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _59.Solution1(); + solution2 = new _59.Solution2(); + } + + @Test + public void test1() { + CommonUtils.print2DIntArray(solution1.generateMatrix(6)); + CommonUtils.print2DIntArray(solution2.generateMatrix(6)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_5Test.java b/src/test/java/com/fishercoder/firstthousand/_5Test.java new file mode 100644 index 0000000000..8e10e4c84a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_5Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._5; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _5Test { + private _5.Solution1 solution1; + private _5.Solution2 solution2; + private _5.Solution3 solution3; + private static String s; + + @BeforeEach + public void setup() { + solution1 = new _5.Solution1(); + solution2 = new _5.Solution2(); + solution3 = new _5.Solution3(); + } + + @Test + public void test1() { + s = "babad"; + assertEquals("bab", solution1.longestPalindrome(s)); + assertEquals("aba", solution2.longestPalindrome(s)); + assertEquals("bab", solution3.longestPalindrome(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_600Test.java b/src/test/java/com/fishercoder/firstthousand/_600Test.java new file mode 100644 index 0000000000..d135383120 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_600Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._600; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/28/17. */ +public class _600Test { + private _600.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _600.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.findIntegers(5)); + assertEquals(514229, solution1.findIntegers(100000000)); + } +} diff --git a/src/test/java/com/fishercoder/_604Test.java b/src/test/java/com/fishercoder/firstthousand/_604Test.java similarity index 92% rename from src/test/java/com/fishercoder/_604Test.java rename to src/test/java/com/fishercoder/firstthousand/_604Test.java index 6f562b90bd..da83888d84 100644 --- a/src/test/java/com/fishercoder/_604Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_604Test.java @@ -1,12 +1,12 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._604; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._604; +import org.junit.jupiter.api.Test; public class _604Test { - private static _604.Solution1.StringIterator test; + private _604.Solution1.StringIterator test; @Test public void test1() { diff --git a/src/test/java/com/fishercoder/firstthousand/_605Test.java b/src/test/java/com/fishercoder/firstthousand/_605Test.java new file mode 100644 index 0000000000..b06175740e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_605Test.java @@ -0,0 +1,160 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._605; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _605Test { + private _605.Solution1 solution1; + private _605.Solution2 solution2; + private static int[] flowerbed; + private static int n; + + @BeforeEach + public void setup() { + solution1 = new _605.Solution1(); + solution2 = new _605.Solution2(); + } + + @Test + public void test1() { + flowerbed = new int[] {1, 0, 0, 0, 1}; + n = 1; + assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test2() { + flowerbed = new int[] {1, 0, 0, 0, 1}; + n = 2; + assertEquals(false, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test3() { + flowerbed = new int[] {1, 0, 0, 0, 0, 1}; + n = 2; + assertEquals(false, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test4() { + flowerbed = new int[] {1, 0, 1, 0, 1, 0, 1}; + n = 1; + assertEquals(false, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test5() { + flowerbed = new int[] {0, 0, 1, 0, 1}; + n = 1; + assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test6() { + flowerbed = new int[] {1, 0, 0, 0, 1, 0, 0}; + n = 2; + assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test7() { + flowerbed = new int[] {0, 0, 1, 0, 0}; + n = 2; + assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test8() { + flowerbed = new int[] {1}; + n = 0; + assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test9() { + flowerbed = new int[] {0}; + n = 0; + assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test10() { + flowerbed = new int[] {0}; + n = 1; + assertEquals(true, solution1.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test11() { + flowerbed = new int[] {1, 0, 0, 0, 1}; + n = 1; + assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test12() { + flowerbed = new int[] {1, 0, 0, 0, 1}; + n = 2; + assertEquals(false, solution2.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test13() { + flowerbed = new int[] {1, 0, 0, 0, 0, 1}; + n = 2; + assertEquals(false, solution2.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test14() { + flowerbed = new int[] {1, 0, 1, 0, 1, 0, 1}; + n = 1; + assertEquals(false, solution2.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test15() { + flowerbed = new int[] {0, 0, 1, 0, 1}; + n = 1; + assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test16() { + flowerbed = new int[] {1, 0, 0, 0, 1, 0, 0}; + n = 2; + assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test17() { + flowerbed = new int[] {0, 0, 1, 0, 0}; + n = 2; + assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test18() { + flowerbed = new int[] {1}; + n = 0; + assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test19() { + flowerbed = new int[] {0}; + n = 0; + assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); + } + + @Test + public void test20() { + flowerbed = new int[] {0}; + n = 1; + assertEquals(true, solution2.canPlaceFlowers(flowerbed, n)); + } +} diff --git a/src/test/java/com/fishercoder/_606Test.java b/src/test/java/com/fishercoder/firstthousand/_606Test.java similarity index 75% rename from src/test/java/com/fishercoder/_606Test.java rename to src/test/java/com/fishercoder/firstthousand/_606Test.java index 803b053725..996bfe8f29 100644 --- a/src/test/java/com/fishercoder/_606Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_606Test.java @@ -1,22 +1,21 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._606; -import org.junit.BeforeClass; -import org.junit.Test; - +import com.fishercoder.solutions.firstthousand._606; import java.util.Arrays; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _606Test { - private static _606.Solution1 solution1; - private static _606.Solution2 solution2; + private _606.Solution1 solution1; + private _606.Solution2 solution2; private static TreeNode treeNode; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _606.Solution1(); solution2 = new _606.Solution2(); } diff --git a/src/test/java/com/fishercoder/firstthousand/_609Test.java b/src/test/java/com/fishercoder/firstthousand/_609Test.java new file mode 100644 index 0000000000..4097764e9a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_609Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._609; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _609Test { + private _609.Solution1 solution1; + private static String[] paths; + private static List> actual; + + @BeforeEach + public void setup() { + solution1 = new _609.Solution1(); + } + + @Test + public void test1() { + paths = + new String[] { + "root/a 1.txt(abcd) 2.txt(efgh)", + "root/c 3.txt(abcd)", + "root/c/d 4.txt(efgh)", + "root 4.txt(efgh)" + }; + actual = solution1.findDuplicate(paths); + CommonUtils.printListList(actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_60Test.java b/src/test/java/com/fishercoder/firstthousand/_60Test.java new file mode 100644 index 0000000000..f2e3d21044 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_60Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._60; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _60Test { + private _60.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _60.Solution1(); + } + + @Test + public void test1() { + assertEquals("231", solution1.getPermutation(3, 4)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_611Test.java b/src/test/java/com/fishercoder/firstthousand/_611Test.java new file mode 100644 index 0000000000..0fccbd3548 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_611Test.java @@ -0,0 +1,82 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._611; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _611Test { + private _611.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _611.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 2, 3, 4}; + assertEquals(3, solution1.triangleNumber(nums)); + } + + @Test + public void test2() { + nums = + new int[] { + 85, 88, 57, 88, 61, 71, 24, 12, 11, 37, 37, 72, 43, 22, 65, 79, 34, 41, 37, 74, + 28, 25, 73, 44, 11, 84, 29, 33, 21, 87, 87, 21, 14, 47, 45, 31, 40, 33, 85, 99, + 47, 29, 13, 82, 56, 33, 47, 11, 80, 59, 16, 16, 57, 53, 37, 20, 90, 84, 97, 39, + 50, 22, 44, 54, 67, 69, 46, 58, 11, 29, 89, 10, 89, 30, 12, 55, 30, 32, 23, 74, + 69, 19, 48, 72, 42, 12, 19, 88, 39, 32, 98, 89, 99, 19, 78, 35, 45, 16, 22, 73, + 86, 76, 39, 50, 40, 85, 85, 61, 71, 97, 48, 39, 21, 39, 35, 11, 58, 94, 37, 50, + 13, 78, 67, 23, 24, 53, 98, 41, 85, 90, 85, 92, 31, 11, 46, 88, 83, 68, 87, 33, + 13, 43, 95, 39, 33, 40, 47, 95, 28, 52, 91, 60, 19, 76, 80, 75, 51, 64, 67, 33, + 43, 47, 15, 80, 36, 78, 33, 98, 18, 15, 77, 100, 60, 28, 51, 86, 30, 67, 48, 43, + 62, 23, 82, 26, 93, 99, 19, 93, 21, 22, 34, 100, 46, 94, 42, 58, 31, 27, 81, 47, + 90, 53, 85, 69, 41, 29, 73, 26, 23, 54, 59, 83, 99, 19, 28, 96, 15, 56, 40, 32, + 32, 42, 85, 43, 71, 36, 98, 74, 38, 91, 11, 79, 62, 24, 34, 29, 10, 31, 54, 59, + 97, 35, 23, 92, 72, 96, 52, 45, 57, 39, 69, 53, 89, 46, 44, 66, 43, 60, 62, 28, + 53, 72, 82, 63, 92, 39, 20, 56, 72, 25, 36, 33, 60, 92, 29, 40, 21, 53, 45, 53, + 80, 56, 42, 67, 79, 11, 39, 11, 77, 82, 97, 91, 55, 96, 73, 48, 26, 11, 90, 40, + 28, 21, 35, 99, 25, 73, 39, 62, 66, 50, 72, 58, 26, 91, 96, 88, 98, 25, 99, 27, + 26, 26, 40, 43, 80, 21, 99, 25, 94, 61, 32, 49, 40, 67, 91, 99, 17, 99, 76, 34, + 21, 100, 14, 98, 68, 83, 54, 21, 52, 84, 37, 87, 100, 69, 43, 76, 31, 31, 39, + 29, 90, 82, 37, 26, 36, 11, 37, 95, 26, 43, 29, 66, 70, 99, 88, 44, 37, 96, 83, + 38, 44, 50, 48, 10, 98, 67, 58, 66, 74, 91, 24, 19, 82, 94, 94, 58, 89, 73, 92, + 80, 19, 76, 60, 23, 54, 78, 55, 46, 20, 27, 88, 23, 93, 16, 60, 97, 67, 78, 86, + 83, 73, 48, 32, 39, 83, 49, 58, 82, 50, 99, 67, 39, 79, 48, 76, 82, 72, 92, 32, + 98, 97, 82, 44, 37, 11, 50, 94, 51, 89, 100, 46, 95, 10, 99, 39, 68, 81, 34, 61, + 78, 95, 54, 85, 31, 74, 38, 95, 85, 88, 45, 78, 35, 82, 42, 19, 12, 89, 24, 42, + 88, 57, 79, 82, 13, 22, 64, 66, 42, 73, 44, 54, 68, 70, 15, 27, 44, 72, 44, 49, + 42, 40, 26, 50, 33, 24, 56, 79, 68, 100, 41, 42, 92, 89, 27, 50, 100, 35, 54, + 89, 98, 32, 80, 15, 90, 66, 53, 27, 77, 82, 97, 58, 95, 70, 24, 79, 27, 29, 54, + 90, 25, 76, 26, 90, 79, 67, 31, 85, 15, 74, 22, 59, 20, 71, 62, 15, 48, 31, 48, + 91, 89, 19, 51, 17, 60, 13, 65, 44, 89, 19, 45, 72, 84, 94, 98, 21, 89, 24, 10, + 19, 14, 26, 68, 100, 44, 26, 21, 65, 14, 79, 93, 48, 29, 60, 47, 84, 54, 13, 89, + 45, 91, 29, 15, 65, 52, 39, 59, 13, 25, 59, 91, 22, 74, 77, 62, 95, 28, 86, 30, + 24, 61, 53, 12, 16, 89, 79, 64, 36, 66, 47, 40, 13, 94, 28, 51, 64, 79, 38, 28, + 54, 99, 11, 69, 22, 42, 89, 36, 62, 96, 32, 50, 24, 45, 90, 46, 13, 31, 84, 39, + 84, 51, 34, 97, 13, 49, 10, 43, 42, 47, 86, 33, 76, 72, 39, 26, 77, 70, 100, 93, + 64, 20, 80, 76, 48, 56, 32, 38, 56, 67, 15, 14, 14, 11, 61, 44, 31, 57, 39, 65, + 71, 16, 68, 75, 47, 81, 55, 16, 80, 63, 10, 73, 81, 20, 20, 20, 34, 37, 40, 43, + 44, 13, 89, 31, 75, 54, 37, 62, 76, 63, 94, 65, 82, 51, 69, 56, 76, 45, 12, 55, + 25, 29, 65, 49, 16, 43, 48, 65, 20, 39, 81, 81, 61, 85, 73, 51, 81, 30, 18, 62, + 54, 85, 85, 80, 59, 63, 37, 58, 32, 48, 84, 64, 50, 47, 99, 49, 62, 69, 60, 100, + 67, 50, 16, 22, 65, 97, 58, 23, 70, 27, 14, 91, 84, 38, 91, 37, 31, 53, 100, 45, + 29, 64, 75, 94, 70, 89, 78, 81, 70, 18, 79, 70, 83, 70, 61, 15, 42, 70, 77, 45, + 36, 59, 65, 71, 41, 82, 89, 64, 48, 67, 89, 28, 20, 10, 99, 23, 27, 65, 77, 32, + 96, 98, 59, 64, 99, 21, 77, 65, 43, 37, 62, 64, 78, 91, 57, 98, 75, 62, 92, 61, + 27, 40, 51, 66, 55, 60, 44, 83, 71, 38, 34, 32, 72, 22, 45, 89, 26, 96, 87, 59, + 92, 14, 94, 80, 44, 36, 94, 36, 21, 18, 95, 80, 88, 95, 87, 86, 53, 14, 55, 10, + 24, 13, 11, 10, 61, 47, 76, 94, 75, 85, 25, 22, 22, 76, 40, 43, 69, 87, 30, 66, + 43, 74, 70, 30, 94, 32, 47, 88, 95, 67, 25, 59, 64, 12, 20, 44, 48, 84, 21, 34, + 63, 79, 21, 13, 33, 71, 16, 45, 37, 34, 39, 24, 64, 67, 42, 64, 65, 55, 89, 96, + 72, 58, 54, 17, 43, 30, 54, 27, 82, 66, 17, 43, 41, 38, 84, 37, 93, 70, 75, 53, + 81, 35, 87, 70, 77, 52, 92, 90, 19, 45, 33, 58, 20, 77, 88, 37, 49, 82, 22, 24, + 63, 47 + }; + assertEquals(105489315, solution1.triangleNumber(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_617Test.java b/src/test/java/com/fishercoder/firstthousand/_617Test.java new file mode 100644 index 0000000000..9a6b5f7054 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_617Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._617; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _617Test { + private _617.Solution1 solution1; + private _617.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _617.Solution1(); + solution2 = new _617.Solution2(); + } + + @Test + public void test1() { + assertEquals( + TreeUtils.constructBinaryTree(Arrays.asList(3, 4, 5, 5, 4, null, 7)), + solution1.mergeTrees( + TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5)), + TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3, null, 4, null, 7)))); + } + + @Test + public void test2() { + assertEquals( + TreeUtils.constructBinaryTree(Arrays.asList(3, 4, 5, 5, 4, null, 7)), + solution2.mergeTrees( + TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5)), + TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3, null, 4, null, 7)))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_61Test.java b/src/test/java/com/fishercoder/firstthousand/_61Test.java new file mode 100644 index 0000000000..676a3f1b89 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_61Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.solutions.firstthousand._61; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _61Test { + private _61.Solution1 solution1; + private static ListNode expected; + private static ListNode actual; + private static ListNode head; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _61.Solution1(); + } + + @Test + public void test1() { + k = 2; + expected = new ListNode(4); + expected.next = new ListNode(5); + expected.next.next = new ListNode(1); + expected.next.next.next = new ListNode(2); + expected.next.next.next.next = new ListNode(3); + + head = new ListNode(1); + head.next = new ListNode(2); + head.next.next = new ListNode(3); + head.next.next.next = new ListNode(4); + head.next.next.next.next = new ListNode(5); + + actual = solution1.rotateRight(head, k); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_621Test.java b/src/test/java/com/fishercoder/firstthousand/_621Test.java new file mode 100644 index 0000000000..2539752c02 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_621Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._621; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _621Test { + private _621.Solution1 solution1; + private static char[] tasks; + + @BeforeEach + public void setup() { + solution1 = new _621.Solution1(); + } + + @Test + public void test1() { + tasks = new char[] {'A', 'A', 'A', 'B', 'B', 'B'}; + assertEquals(8, solution1.leastInterval(tasks, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_622Test.java b/src/test/java/com/fishercoder/firstthousand/_622Test.java new file mode 100644 index 0000000000..9d359084c1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_622Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._622; +import org.junit.jupiter.api.Test; + +public class _622Test { + private _622.Solution1.MyCircularQueue myCircularQueue; + + @Test + public void test1() { + myCircularQueue = new _622.Solution1.MyCircularQueue(3); + assertEquals(true, myCircularQueue.enQueue(1)); + assertEquals(true, myCircularQueue.enQueue(2)); + assertEquals(true, myCircularQueue.enQueue(3)); + assertEquals(false, myCircularQueue.enQueue(4)); + assertEquals(3, myCircularQueue.Rear()); + assertEquals(true, myCircularQueue.isFull()); + assertEquals(true, myCircularQueue.deQueue()); + assertEquals(true, myCircularQueue.enQueue(4)); + assertEquals(4, myCircularQueue.Rear()); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_623Test.java b/src/test/java/com/fishercoder/firstthousand/_623Test.java new file mode 100644 index 0000000000..68af49e419 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_623Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._623; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _623Test { + private _623.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _623.Solution1(); + } + + @Test + public void test1() { + TreeNode expected = + TreeUtils.constructBinaryTree(Arrays.asList(4, 1, 1, 2, null, null, 6, 3, 1, 5)); + TreeUtils.printBinaryTree(expected); + TreeNode inputTree = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 3, 1, 5)); + TreeUtils.printBinaryTree(inputTree); + assertEquals(expected, solution1.addOneRow(inputTree, 1, 2)); + } + + @Test + public void test2() { + TreeNode expected = + TreeUtils.constructBinaryTree(Arrays.asList(4, 2, null, 1, 1, 3, null, null, 1)); + TreeUtils.printBinaryTree(expected); + TreeNode inputTree = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, null, 3, 1)); + TreeUtils.printBinaryTree(inputTree); + assertEquals(expected, solution1.addOneRow(inputTree, 1, 3)); + } + + @Test + public void test3() { + TreeNode expected = + TreeUtils.constructBinaryTree( + Arrays.asList(4, 2, 5, 1, 1, 1, 1, 3, null, null, 1, 6, null, null, 7)); + TreeUtils.printBinaryTree(expected); + TreeNode inputTree = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 5, 3, 1, 6, 7)); + TreeUtils.printBinaryTree(inputTree); + TreeNode actual = solution1.addOneRow(inputTree, 1, 3); + TreeUtils.printBinaryTree(actual); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_628Test.java b/src/test/java/com/fishercoder/firstthousand/_628Test.java new file mode 100644 index 0000000000..8a793bedd7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_628Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._628; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _628Test { + private _628.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _628.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3}; + assertEquals(6, solution1.maximumProduct(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 3, 4}; + assertEquals(24, solution1.maximumProduct(nums)); + } + + @Test + public void test3() { + nums = new int[] {-4, -3, -2, -1, 60}; + assertEquals(720, solution1.maximumProduct(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_62Test.java b/src/test/java/com/fishercoder/firstthousand/_62Test.java new file mode 100644 index 0000000000..389f391dd8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_62Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._62; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _62Test { + private _62.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _62.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.uniquePaths(1, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_630Test.java b/src/test/java/com/fishercoder/firstthousand/_630Test.java new file mode 100644 index 0000000000..f1c5e0ee03 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_630Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._630; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _630Test { + private _630.Solution1 solution1; + private static int[][] courses; + + @BeforeEach + public void setup() { + solution1 = new _630.Solution1(); + } + + @Test + public void test1() { + courses = + new int[][] { + {100, 200}, + {200, 1300}, + {1000, 1250}, + {2000, 3200}, + {300, 1200} + }; + assertEquals(4, solution1.scheduleCourse(courses)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_631Test.java b/src/test/java/com/fishercoder/firstthousand/_631Test.java new file mode 100644 index 0000000000..0d315a5a21 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_631Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._631; +import org.junit.jupiter.api.Test; + +public class _631Test { + private _631.Solution1.Excel excel; + + @Test + public void test1() { + excel = new _631.Solution1.Excel(3, 'C'); + assertEquals(0, excel.get(1, 'A')); + excel.set(1, 'A', 1); + assertEquals(1, excel.get(1, 'A')); + } + + @Test + public void test2() { + excel = new _631.Solution1.Excel(3, 'C'); + assertEquals(0, excel.sum(1, 'A', new String[] {"A2"})); + excel.set(2, 'A', 1); + assertEquals(1, excel.get(1, 'A')); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_635Test.java b/src/test/java/com/fishercoder/firstthousand/_635Test.java new file mode 100644 index 0000000000..1c7eb820a6 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_635Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._635; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 9/9/17. */ +public class _635Test { + private _635.Solution1.LogSystem logSystem; + private static List expected; + + @BeforeEach + public void setup() { + logSystem = new _635.Solution1.LogSystem(); + } + + @BeforeEach + public void clear() { + logSystem = new _635.Solution1.LogSystem(); + expected = new ArrayList<>(); + } + + @Test + public void test1() { + logSystem.put(1, "2017:01:01:23:59:59"); + logSystem.put(2, "2017:01:01:22:59:59"); + logSystem.put(3, "2016:01:01:00:00:00"); + expected = Arrays.asList(1, 2, 3); + assertEquals( + expected, logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year")); + } + + @Test + public void test2() { + logSystem.put(1, "2017:01:01:23:59:59"); + logSystem.put(2, "2017:01:01:22:59:59"); + logSystem.put(3, "2016:01:01:00:00:00"); + expected = Arrays.asList(1, 2); + assertEquals( + expected, logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_636Test.java b/src/test/java/com/fishercoder/firstthousand/_636Test.java new file mode 100644 index 0000000000..72a64113b2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_636Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._636; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _636Test { + private _636.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _636.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {3, 4}, + solution1.exclusiveTime( + 2, Arrays.asList("0:start:0", "1:start:2", "1:end:5", "0:end:6"))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_63Test.java b/src/test/java/com/fishercoder/firstthousand/_63Test.java new file mode 100644 index 0000000000..dac8d386b5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_63Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._63; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _63Test { + private _63.Solution1 solution1; + private static int[][] obstacleGrid; + + @BeforeEach + public void setup() { + solution1 = new _63.Solution1(); + } + + @Test + public void test1() { + obstacleGrid = + new int[][] { + {0, 0}, + {0, 1}, + }; + assertEquals(0, solution1.uniquePathsWithObstacles(obstacleGrid)); + } + + @Test + public void test2() { + obstacleGrid = + new int[][] { + {0, 0, 0}, + {0, 1, 0}, + {0, 0, 0}, + }; + assertEquals(2, solution1.uniquePathsWithObstacles(obstacleGrid)); + } + + @Test + public void test3() { + int[][] obstacleGrid = new int[][] {{1, 0}}; + assertEquals(0, solution1.uniquePathsWithObstacles(obstacleGrid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_643Test.java b/src/test/java/com/fishercoder/firstthousand/_643Test.java new file mode 100644 index 0000000000..dc5b275f1f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_643Test.java @@ -0,0 +1,45 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._643; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _643Test { + private _643.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _643.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 12, -5, -6, 50, 3}; + assertEquals(12.75, solution1.findMaxAverage(nums, 4), 0.01); + } + + @Test + public void test2() { + nums = new int[] {-1}; + assertEquals(-1.0, solution1.findMaxAverage(nums, 1), 0.01); + } + + @Test + public void test3() { + nums = + new int[] { + -6662, 5432, -8558, -8935, 8731, -3083, 4115, 9931, -4006, -3284, -3024, 1714, + -2825, -2374, -2750, -959, 6516, 9356, 8040, -2169, -9490, -3068, 6299, 7823, + -9767, 5751, -7897, 6680, -1293, -3486, -6785, 6337, -9158, -4183, 6240, -2846, + -2588, -5458, -9576, -1501, -908, -5477, 7596, -8863, -4088, 7922, 8231, -4928, + 7636, -3994, -243, -1327, 8425, -3468, -4218, -364, 4257, 5690, 1035, 6217, + 8880, 4127, -6299, -1831, 2854, -4498, -6983, -677, 2216, -1938, 3348, 4099, + 3591, 9076, 942, 4571, -4200, 7271, -6920, -1886, 662, 7844, 3658, -6562, -2106, + -296, -3280, 8909, -8352, -9413, 3513, 1352, -8825 + }; + assertEquals(37.25555555555555, solution1.findMaxAverage(nums, 90), 0.01); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_645Test.java b/src/test/java/com/fishercoder/firstthousand/_645Test.java new file mode 100644 index 0000000000..9b401f195e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_645Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._645; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _645Test { + private _645.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _645.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 2, 4}; + assertArrayEquals(new int[] {2, 3}, solution1.findErrorNums(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_646Test.java b/src/test/java/com/fishercoder/firstthousand/_646Test.java new file mode 100644 index 0000000000..8bdfb7556f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_646Test.java @@ -0,0 +1,60 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._646; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _646Test { + + private _646.Solution1 solution1; + private _646.Solution2 solution2; + private static int[][] pairs; + + @BeforeEach + public void setup() { + solution1 = new _646.Solution1(); + solution2 = new _646.Solution2(); + } + + @Test + public void test1() { + pairs = + new int[][] { + {1, 2}, + {2, 3}, + {5, 6}, + {3, 4} + }; + assertEquals(3, solution1.findLongestChain(pairs)); + assertEquals(3, solution2.findLongestChain(pairs)); + } + + @Test + public void test2() { + pairs = + new int[][] { + {9, 10}, + {-9, 9}, + {-6, 1}, + {-4, 1}, + {8, 10}, + {7, 10}, + {9, 10}, + {2, 10} + }; + assertEquals(2, solution1.findLongestChain(pairs)); + assertEquals(2, solution2.findLongestChain(pairs)); + } + + @Test + public void test3() { + pairs = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[-6,9],[1,6],[8,10],[-1,4],[-6,-2],[-9,8],[-5,3],[0,3]"); + assertEquals(3, solution1.findLongestChain(pairs)); + assertEquals(3, solution2.findLongestChain(pairs)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_647Test.java b/src/test/java/com/fishercoder/firstthousand/_647Test.java new file mode 100644 index 0000000000..042731ae96 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_647Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._647; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _647Test { + private _647.Solution1 solution1; + private _647.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _647.Solution1(); + solution2 = new _647.Solution2(); + } + + @Test + public void test1() { + assertEquals(3, solution1.countSubstrings("abc")); + assertEquals(3, solution2.countSubstrings("abc")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_648Test.java b/src/test/java/com/fishercoder/firstthousand/_648Test.java new file mode 100644 index 0000000000..624cbba3cc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_648Test.java @@ -0,0 +1,66 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._648; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _648Test { + private _648.Solution1 solution1; + private static List dict; + private static String sentence; + + @BeforeEach + public void setup() { + solution1 = new _648.Solution1(); + } + + @Test + public void test1() { + dict = Arrays.asList("cat", "bat", "rat"); + sentence = "the cattle was rattled by the battery"; + assertEquals("the cat was rat by the bat", solution1.replaceWords(dict, sentence)); + } + + @Test + public void test2() { + dict = + Arrays.asList( + "e", "k", "c", "harqp", "h", "gsafc", "vn", "lqp", "soy", "mr", "x", + "iitgm", "sb", "oo", "spj", "gwmly", "iu", "z", "f", "ha", "vds", "v", + "vpx", "fir", "t", "xo", "apifm", "tlznm", "kkv", "nxyud", "j", "qp", "omn", + "zoxp", "mutu", "i", "nxth", "dwuer", "sadl", "pv", "w", "mding", "mubem", + "xsmwc", "vl", "farov", "twfmq", "ljhmr", "q", "bbzs", "kd", "kwc", "a", + "buq", "sm", "yi", "nypa", "xwz", "si", "amqx", "iy", "eb", "qvgt", "twy", + "rf", "dc", "utt", "mxjfu", "hm", "trz", "lzh", "lref", "qbx", "fmemr", + "gil", "go", "qggh", "uud", "trnhf", "gels", "dfdq", "qzkx", "qxw"); + sentence = + "ikkbp miszkays wqjferqoxjwvbieyk gvcfldkiavww vhokchxz dvypwyb " + + "bxahfzcfanteibiltins ueebf lqhflvwxksi dco kddxmckhvqifbuzkhstp wc " + + "ytzzlm gximjuhzfdjuamhsu gdkbmhpnvy ifvifheoxqlbosfww mengfdydekwttkhbzenk wjhmmyltmeufqvcpcxg " + + "hthcuovils ldipovluo aiprogn nusquzpmnogtjkklfhta klxvvlvyh nxzgnrveghc mpppfhzjkbucv cqcft " + + "uwmahhqradjtf iaaasabqqzmbcig zcpvpyypsmodtoiif qjuiqtfhzcpnmtk yzfragcextvx ivnvgkaqs " + + "iplazv jurtsyh gzixfeugj rnukjgtjpim hscyhgoru aledyrmzwhsz xbahcwfwm hzd ygelddphxnbh " + + "rvjxtlqfnlmwdoezh zawfkko iwhkcddxgpqtdrjrcv bbfj mhs nenrqfkbf spfpazr " + + "wrkjiwyf cw dtd cqibzmuuhukwylrnld dtaxhddidfwqs bgnnoxgyynol hg " + + "dijhrrpnwjlju muzzrrsypzgwvblf zbugltrnyzbg hktdviastoireyiqf qvufxgcixvhrjqtna ipfzhuvgo daee " + + "r nlipyfszvxlwqw yoq dewpgtcrzausqwhh qzsaobsghgm ichlpsjlsrwzhbyfhm ksenb " + + "bqprarpgnyemzwifqzz oai pnqottd nygesjtlpala qmxixtooxtbrzyorn gyvukjpc s mxhlkdaycskj uvwmerplaibeknltuvd ocnn " + + "frotscysdyclrc ckcttaceuuxzcghw pxbd oklwhcppuziixpvihihp"; + assertEquals( + "i miszkays w gvcfldkiavww v dvypwyb bxahfzcfanteibiltins ueebf " + + "lqhflvwxksi dc k w ytzzlm gximjuhzfdjuamhsu gdkbmhpnvy i mengfdydekwttkhbzenk w " + + "h ldipovluo a nusquzpmnogtjkklfhta k nxzgnrveghc mpppfhzjkbucv c uwmahhqradjtf i z " + + "q yzfragcextvx i i j gzixfeugj rnukjgtjpim h a x h " + + "ygelddphxnbh rvjxtlqfnlmwdoezh z i bbfj mhs nenrqfkbf " + + "spfpazr w c dtd c dtaxhddidfwqs bgnnoxgyynol h " + + "dijhrrpnwjlju muzzrrsypzgwvblf z h q i daee r nlipyfszvxlwqw " + + "yoq dewpgtcrzausqwhh q i k bqprarpgnyemzwifqzz " + + "oai pnqottd nygesjtlpala q gyvukjpc s mxhlkdaycskj " + + "uvwmerplaibeknltuvd ocnn f c pxbd oklwhcppuziixpvihihp", + solution1.replaceWords(dict, sentence)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_649Test.java b/src/test/java/com/fishercoder/firstthousand/_649Test.java new file mode 100644 index 0000000000..8f84355791 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_649Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._649; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/8/17. */ +public class _649Test { + private _649.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _649.Solution1(); + } + + @Test + public void test1() { + assertEquals("Dire", solution1.predictPartyVictory("RDDDR")); + } + + @Test + public void test2() { + assertEquals("Radiant", solution1.predictPartyVictory("RD")); + } + + @Test + public void test3() { + assertEquals("Dire", solution1.predictPartyVictory("RDD")); + } + + @Test + public void test4() { + assertEquals("Radiant", solution1.predictPartyVictory("RDDR")); + } + + @Test + public void test5() { + assertEquals("Dire", solution1.predictPartyVictory("RDDRD")); + } + + @Test + public void test6() { + assertEquals("Dire", solution1.predictPartyVictory("RDDDDDRR")); + } + + @Test + public void test7() { + assertEquals("Dire", solution1.predictPartyVictory("RDDDDRR")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_64Test.java b/src/test/java/com/fishercoder/firstthousand/_64Test.java new file mode 100644 index 0000000000..d007079793 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_64Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._64; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _64Test { + private _64.Solution1 solution1; + private static int[][] grid; + + @BeforeEach + public void setup() { + solution1 = new _64.Solution1(); + } + + @Test + public void test1() { + grid = + new int[][] { + {1, 2}, + {1, 1} + }; + assertEquals(3, solution1.minPathSum(grid)); + } + + @Test + public void test2() { + grid = new int[][] {{1}}; + assertEquals(1, solution1.minPathSum(grid)); + } + + @Test + public void test3() { + grid = + new int[][] { + {1, 3, 1}, + {1, 5, 1}, + {4, 2, 1} + }; + assertEquals(7, solution1.minPathSum(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_650Test.java b/src/test/java/com/fishercoder/firstthousand/_650Test.java new file mode 100644 index 0000000000..45f99dc7bf --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_650Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._650; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _650Test { + private _650.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _650.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.minSteps(3)); + } + + @Test + public void test2() { + assertEquals(9, solution1.minSteps(20)); + } + + @Test + public void test3() { + assertEquals(19, solution1.minSteps(19)); + } + + @Test + public void test4() { + assertEquals(0, solution1.minSteps(1)); + } + + @Test + public void test5() { + assertEquals(35, solution1.minSteps(741)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_651Test.java b/src/test/java/com/fishercoder/firstthousand/_651Test.java new file mode 100644 index 0000000000..d5e09c1fb6 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_651Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._651; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _651Test { + private _651.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _651.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.maxA(3)); + } + + @Test + public void test2() { + assertEquals(324, solution1.maxA(20)); + } + + @Test + public void test3() { + assertEquals(256, solution1.maxA(19)); + } + + @Test + public void test4() { + assertEquals(1, solution1.maxA(1)); + } + + @Test + public void test5() { + assertEquals(1327104, solution1.maxA(50)); + } + + @Test + public void test6() { + assertEquals(9, solution1.maxA(7)); + } +} diff --git a/src/test/java/com/fishercoder/_652Test.java b/src/test/java/com/fishercoder/firstthousand/_652Test.java similarity index 81% rename from src/test/java/com/fishercoder/_652Test.java rename to src/test/java/com/fishercoder/firstthousand/_652Test.java index c8ad5c5d37..d739c95f31 100644 --- a/src/test/java/com/fishercoder/_652Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_652Test.java @@ -1,28 +1,26 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.common.classes.TreeNode; -import com.fishercoder.solutions._652; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.solutions.firstthousand._652; import java.util.ArrayList; import java.util.Arrays; import java.util.List; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _652Test { - private static _652.Solution1 solution1; + private _652.Solution1 solution1; private static List expected; private static TreeNode root; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _652.Solution1(); } - @Before + @BeforeEach public void setUp() { root = null; } diff --git a/src/test/java/com/fishercoder/firstthousand/_653Test.java b/src/test/java/com/fishercoder/firstthousand/_653Test.java new file mode 100644 index 0000000000..1678473ce2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_653Test.java @@ -0,0 +1,89 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._653; +import java.util.ArrayList; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _653Test { + private _653.Solution1 solution1; + private static boolean expected; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _653.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + root = null; + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(5, 3, 2, 4, 6, 7))); + expected = true; + assertEquals(expected, solution1.findTarget(root, 9)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 1, 3))); + expected = true; + assertEquals(expected, solution1.findTarget(root, 4)); + } + + @Test + public void test3() { + root = new TreeNode(1); + expected = false; + assertEquals(expected, solution1.findTarget(root, 2)); + } + + @Test + public void test4() { + /** + * 2 / \ 0 3 / \ -4 1 + * + *

target = 1; expected = true; + */ + root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 0, -4, 1, 3))); + expected = true; + assertEquals(expected, solution1.findTarget(root, -1)); + } + + @Test + public void test5() { + root = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(2, 1, 3, -4, 0))); + expected = true; + assertEquals(expected, solution1.findTarget(root, 2)); + } + + @Test + public void test6() { + root = + TreeUtils.constructBinaryTree( + new ArrayList<>( + Arrays.asList( + 3393, 2264, 4972, 1908, 3252, 4128, 5140, 965, 2018, 3082, + null, 3838, 4196, 5085, null, 559, 1187, null, 2143, 2968, + null, 3810, 3957, null, 4825, null, null, 0, 908, 1135, + 1659, null, null, 2764, null, 3581, null, null, 4106, 4498, + null, null, 498, 821, null, null, null, 1262, 1826, 2513, + 2910, 3486, 3708, null, null, 4377, 4673, 231, null, null, + null, null, 1482, null, null, 2386, 2690, null, null, null, + null, null, null, 4349, null, null, null, 170, 376, 1421, + 1613, null, null, 2534, null, null, null, 96, null, null, + null, 1303))); + expected = true; + assertEquals(expected, solution1.findTarget(root, 5831)); + // TreeUtils.printBinaryTree(root); + // assertEquals(expected, mapSolution.findTarget(root, 5831)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_654Test.java b/src/test/java/com/fishercoder/firstthousand/_654Test.java new file mode 100644 index 0000000000..8c8ee834c0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_654Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._654; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _654Test { + private static int[] nums; + private static TreeNode expected; + private _654.Solution1 solution1; + private _654.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _654.Solution1(); + solution2 = new _654.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {3, 2, 1, 6, 0, 5}; + expected = TreeUtils.constructBinaryTree(Arrays.asList(6, 3, 5, null, 2, 0, null, null, 1)); + assertEquals(expected, solution1.constructMaximumBinaryTree(nums)); + assertEquals(expected, solution2.constructMaximumBinaryTree(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_655Test.java b/src/test/java/com/fishercoder/firstthousand/_655Test.java new file mode 100644 index 0000000000..19b40cea49 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_655Test.java @@ -0,0 +1,59 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._655; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _655Test { + private static List> expected; + private static TreeNode root; + private _655.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _655.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2)); + expected = new ArrayList<>(2); + List row1 = new ArrayList<>(3); + row1.add(""); + row1.add("1"); + row1.add(""); + expected.add(row1); + List row2 = new ArrayList<>(3); + row2.add(0, "2"); + row2.add(""); + row2.add(""); + expected.add(row2); + CommonUtils.printListList(expected); + assertEquals(expected, solution1.printTree(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4)); + TreeUtils.printBinaryTree(root); + CommonUtils.printListList(solution1.printTree(root)); + } + + @Test + public void test3() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList(3, null, 30, 10, null, null, 15, null, 45)); + TreeUtils.printBinaryTree(root); + CommonUtils.printListList(solution1.printTree(root)); + System.out.println(solution1.printTree(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_656Test.java b/src/test/java/com/fishercoder/firstthousand/_656Test.java new file mode 100644 index 0000000000..25d3252965 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_656Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._656; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/25/17. */ +public class _656Test { + private _656.Solution1 solution1; + private static int[] A; + private static List expected; + + @BeforeEach + public void setup() { + solution1 = new _656.Solution1(); + } + + @Test + public void test1() { + A = new int[] {1, 2, 4, -1, 2}; + expected = new ArrayList<>(Arrays.asList(1, 3, 5)); + assertEquals(expected, solution1.cheapestJump(A, 2)); + } +} diff --git a/src/test/java/com/fishercoder/_658Test.java b/src/test/java/com/fishercoder/firstthousand/_658Test.java similarity index 78% rename from src/test/java/com/fishercoder/_658Test.java rename to src/test/java/com/fishercoder/firstthousand/_658Test.java index 03ef91b005..618d9e272e 100644 --- a/src/test/java/com/fishercoder/_658Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_658Test.java @@ -1,24 +1,23 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._658; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import com.fishercoder.solutions.firstthousand._658; import java.util.ArrayList; import java.util.Arrays; import java.util.List; - -import static junit.framework.Assert.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _658Test { - private static _658.Solution1 solution1; + private _658.Solution1 solution1; private static List arr; private static List expected; private static int k; private static int x; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _658.Solution1(); } diff --git a/src/test/java/com/fishercoder/firstthousand/_659Test.java b/src/test/java/com/fishercoder/firstthousand/_659Test.java new file mode 100644 index 0000000000..8ac7aabf23 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_659Test.java @@ -0,0 +1,278 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._659; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _659Test { + private _659.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _659.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3, 3, 4, 5}; + assertEquals(true, solution1.isPossible(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 3, 3, 4, 4, 5, 5}; + assertEquals(true, solution1.isPossible(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 3, 4, 4, 5}; + assertEquals(false, solution1.isPossible(nums)); + } + + @Test + public void test4() { + /** This is one cool test case from Leetcode... */ + nums = + new int[] { + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, + 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, + 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, + 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, + 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, + 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, + 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, + 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, + 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, + 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, + 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, + 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, + 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, + 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, + 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, + 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, + 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, + 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, + 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, + 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, + 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 34, 34, + 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, + 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, + 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, + 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, + 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, + 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, + 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, + 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, + 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, + 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, + 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, + 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, + 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, + 40, 40, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, + 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, + 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, + 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, + 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, + 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, + 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, + 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 44, + 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, + 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, + 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, + 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, + 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, + 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, + 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, + 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, + 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, + 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, + 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, + 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, + 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, + 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, + 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, + 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, + 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, + 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, + 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, + 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, + 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, + 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 54, + 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, + 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, + 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, + 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, + 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, + 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, + 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, + 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, + 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, + 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58, 58, 58, + 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, + 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, + 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, + 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, + 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, + 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, + 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, + 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, + 61, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, + 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, + 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, + 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, + 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, + 65, 65, 65, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 67, 67, + 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, + 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, + 67, 67, 67, 67, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, + 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, + 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, + 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, + 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, + 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 70, 70, 70, + 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, + 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, + 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, + 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, + 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, + 72, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, + 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, + 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 74, + 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, + 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, + 74, 74, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, + 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, + 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 76, 76, 76, 76, 76, + 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, + 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, + 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, + 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, + 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, + 78, 78, 78, 78, 78, 78, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, + 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, + 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80, 80, 80, 80, 80, + 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, + 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, + 80, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, + 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, + 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, + 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 83, 83, 83, 83, 83, 83, 83, 83, 83, + 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, + 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, + 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, + 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, + 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, + 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, + 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, + 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 86, + 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, + 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, + 86, 86, 86, 86, 86, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, + 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, + 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 88, 88, 88, + 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, + 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, + 88, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, + 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, + 89, 89, 89, 89, 89, 89, 89, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, + 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, + 90, 90, 90, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, + 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, + 91, 91, 91, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, + 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, + 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, + 92, 92, 92, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, + 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, + 93, 93, 93, 93, 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 96, 96, 96, 96, + 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, + 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, + 96, 96, 96, 96, 96, 96, 96, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, + 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, + 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, + 97, 97, 97, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, + 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, + 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, + 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 100, 100, 100, 100, 100, + 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, + 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, + 100, 100, 100 + }; + assertEquals(true, solution1.isPossible(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_65Test.java b/src/test/java/com/fishercoder/firstthousand/_65Test.java new file mode 100644 index 0000000000..a8abbcb7c3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_65Test.java @@ -0,0 +1,162 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._65; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _65Test { + private _65.Solution1 solution1; + private _65.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _65.Solution1(); + solution2 = new _65.Solution2(); + } + + @Test + public void test1() { + assertEquals(false, solution1.isNumber("1 a")); + assertEquals(false, solution2.isNumber("1 a")); + } + + @Test + public void test2() { + assertEquals(false, solution1.isNumber("4e+")); + assertEquals(false, solution2.isNumber("4e+")); + } + + @Test + public void test3() { + assertEquals(true, solution1.isNumber("005047e+6")); + assertEquals(true, solution2.isNumber("005047e+6")); + } + + @Test + public void test4() { + assertEquals(false, solution1.isNumber(".e10")); + assertEquals(false, solution2.isNumber(".e10")); + } + + @Test + public void test5() { + assertEquals(true, solution1.isNumber("2e10")); + assertEquals(true, solution2.isNumber("2e10")); + } + + @Test + public void test6() { + assertEquals(false, solution1.isNumber("abc")); + assertEquals(false, solution2.isNumber("abc")); + } + + @Test + public void test7() { + assertEquals(false, solution1.isNumber(" -.")); + assertEquals(false, solution2.isNumber(" -.")); + } + + @Test + public void test8() { + assertEquals(true, solution1.isNumber("+.8")); + assertEquals(true, solution2.isNumber("+.8")); + } + + @Test + public void test9() { + assertEquals(false, solution1.isNumber(".")); + assertEquals(false, solution2.isNumber(".")); + } + + @Test + public void test10() { + assertEquals(false, solution1.isNumber(".e1")); + assertEquals(false, solution2.isNumber(".e1")); + } + + @Test + public void test11() { + assertEquals(true, solution1.isNumber("0")); + assertEquals(true, solution2.isNumber("0")); + } + + @Test + public void test12() { + assertEquals(false, solution1.isNumber("0e")); + assertEquals(false, solution2.isNumber("0e")); + } + + @Test + public void test13() { + assertEquals(false, solution1.isNumber("6ee69")); + assertEquals(false, solution2.isNumber("6ee69")); + } + + @Test + public void test14() { + assertEquals(false, solution1.isNumber("+++")); + assertEquals(false, solution2.isNumber("+++")); + } + + @Test + public void test15() { + assertEquals(false, solution1.isNumber("0e")); + assertEquals(false, solution2.isNumber("0e")); + } + + @Test + public void test16() { + assertEquals(false, solution1.isNumber("e9")); + assertEquals(false, solution2.isNumber("e9")); + } + + @Test + public void test17() { + assertEquals(true, solution1.isNumber(" 0.1 ")); + assertEquals(true, solution2.isNumber(" 0.1 ")); + } + + @Test + public void test18() { + assertEquals(true, solution1.isNumber("46.e3")); + assertEquals(true, solution2.isNumber("46.e3")); + } + + @Test + public void test19() { + assertEquals(false, solution1.isNumber("..")); + assertEquals(false, solution2.isNumber("..")); + } + + @Test + public void test20() { + assertEquals(false, solution1.isNumber(".e1")); + assertEquals(false, solution2.isNumber(".e1")); + } + + @Test + public void test21() { + assertEquals(false, solution1.isNumber("..")); + assertEquals(false, solution2.isNumber("..")); + } + + @Test + public void test22() { + assertEquals(false, solution1.isNumber("1e.")); + assertEquals(false, solution2.isNumber("1e.")); + } + + @Test + public void test24() { + assertEquals(true, solution1.isNumber("-1.")); + assertEquals(true, solution2.isNumber("-1.")); + } + + @Test + public void test25() { + assertEquals(false, solution1.isNumber("6e6.5")); + assertEquals(false, solution2.isNumber("6e6.5")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_661Test.java b/src/test/java/com/fishercoder/firstthousand/_661Test.java new file mode 100644 index 0000000000..394e34ca95 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_661Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._661; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _661Test { + private _661.Solution1 solution1; + private static int[][] M; + private static int[][] expected; + + @BeforeEach + public void setup() { + solution1 = new _661.Solution1(); + } + + @Test + public void test1() { + M = + new int[][] { + {1, 1, 1}, + {1, 0, 1}, + {1, 1, 1} + }; + expected = + M = + new int[][] { + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0} + }; + assertArrayEquals(expected, solution1.imageSmoother(M)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_662Test.java b/src/test/java/com/fishercoder/firstthousand/_662Test.java new file mode 100644 index 0000000000..1820132f69 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_662Test.java @@ -0,0 +1,63 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._662; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +public class _662Test { + private _662.Solution1 solution1; + private static TreeNode root; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _662.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5, 3, null, 9)); + expected = 4; + assertEquals(expected, solution1.widthOfBinaryTree(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, null, 5, 3)); + expected = 2; + assertEquals(expected, solution1.widthOfBinaryTree(root)); + } + + @Test + public void test3() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2, 5)); + expected = 2; + assertEquals(expected, solution1.widthOfBinaryTree(root)); + } + + @Test + @Disabled + /** TODO: need to figure out how to pass in the input for the 4th example on Leetcode */ + public void test4() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 3, 2, 5, null, null, 9, 6, null, null, null, null, null, null, + 7)); + expected = 8; + assertEquals(expected, solution1.widthOfBinaryTree(root)); + } + + @Test + public void test5() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1)); + expected = 1; + assertEquals(expected, solution1.widthOfBinaryTree(root)); + } +} diff --git a/src/test/java/com/fishercoder/_663Test.java b/src/test/java/com/fishercoder/firstthousand/_663Test.java similarity index 80% rename from src/test/java/com/fishercoder/_663Test.java rename to src/test/java/com/fishercoder/firstthousand/_663Test.java index 48844fbf75..976bf4fdd7 100644 --- a/src/test/java/com/fishercoder/_663Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_663Test.java @@ -1,22 +1,21 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._663; -import org.junit.BeforeClass; -import org.junit.Test; - +import com.fishercoder.solutions.firstthousand._663; import java.util.Arrays; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _663Test { - private static _663.Solution1 solution1; + private _663.Solution1 solution1; private static TreeNode root; private static boolean expected; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _663.Solution1(); } @@ -51,5 +50,4 @@ public void test4() { expected = false; assertEquals(expected, solution1.checkEqualTree(root)); } - } diff --git a/src/test/java/com/fishercoder/firstthousand/_664Test.java b/src/test/java/com/fishercoder/firstthousand/_664Test.java new file mode 100644 index 0000000000..87748eaa4a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_664Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._664; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _664Test { + private _664.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _664.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.strangePrinter("aaabbb")); + } + + @Test + public void test2() { + assertEquals(2, solution1.strangePrinter("aba")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_665Test.java b/src/test/java/com/fishercoder/firstthousand/_665Test.java new file mode 100644 index 0000000000..458aeeaafd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_665Test.java @@ -0,0 +1,77 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._665; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _665Test { + private _665.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _665.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {4, 2, 3}; + assertEquals(true, solution1.checkPossibility(nums)); + } + + @Test + public void test2() { + nums = new int[] {4, 2, 1}; + assertEquals(false, solution1.checkPossibility(nums)); + } + + @Test + public void test3() { + nums = new int[] {3, 4, 2, 3}; + assertEquals(false, solution1.checkPossibility(nums)); + } + + @Test + public void test4() { + nums = new int[] {2, 3, 3, 2, 4}; + assertEquals(true, solution1.checkPossibility(nums)); + } + + @Test + public void test5() { + nums = new int[] {2, 3, 3, 2, 2, 4}; + assertEquals(false, solution1.checkPossibility(nums)); + } + + @Test + public void test6() { + nums = new int[] {2, 3, 3, 2, 2, 2, 4}; + assertEquals(false, solution1.checkPossibility(nums)); + } + + @Test + public void test7() { + nums = new int[] {3, 3, 2, 2}; + assertEquals(false, solution1.checkPossibility(nums)); + } + + @Test + public void test8() { + nums = new int[] {-1, 4, 2, 3}; + assertEquals(true, solution1.checkPossibility(nums)); + } + + @Test + public void test9() { + nums = new int[] {1, 2, 4, 5, 3}; + assertEquals(true, solution1.checkPossibility(nums)); + } + + @Test + public void test10() { + nums = new int[] {1, 2, 4, 5, 3, 6}; + assertEquals(true, solution1.checkPossibility(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_666Test.java b/src/test/java/com/fishercoder/firstthousand/_666Test.java new file mode 100644 index 0000000000..d2b9d3abc9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_666Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._666; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _666Test { + private _666.Solution1 solution1; + private _666.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _666.Solution1(); + solution2 = new _666.Solution2(); + } + + @BeforeEach + public void cleanUp() { + solution1.totalSum = 0; + solution2.totalSum = 0; + } + + @Test + public void test1() { + nums = new int[] {113, 215, 221}; + assertEquals(12, solution1.pathSum(nums)); + assertEquals(12, solution2.pathSum(nums)); + } + + @Test + public void test2() { + nums = new int[] {113, 221}; + assertEquals(4, solution1.pathSum(nums)); + assertEquals(4, solution2.pathSum(nums)); + } + + @Test + public void test3() { + nums = new int[] {113, 214, 221, 348, 487}; + assertEquals(26, solution1.pathSum(nums)); + assertEquals(26, solution2.pathSum(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_667Test.java b/src/test/java/com/fishercoder/firstthousand/_667Test.java new file mode 100644 index 0000000000..ed890df115 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_667Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._667; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _667Test { + private _667.Solutoin1 solution1; + private _667.Solutoin2 solution2; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _667.Solutoin1(); + solution2 = new _667.Solutoin2(); + } + + @Test + public void test1() { + expected = new int[] {1, 2, 3}; + assertArrayEquals(expected, solution1.constructArray(3, 1)); + assertArrayEquals(expected, solution2.constructArray(3, 1)); + } + + @Test + public void test2() { + CommonUtils.printArray(solution1.constructArray(3, 2)); + CommonUtils.printArray(solution2.constructArray(3, 2)); + } + + @Test + public void test3() { + CommonUtils.printArray(solution1.constructArray(10, 4)); + CommonUtils.printArray(solution2.constructArray(10, 4)); + } + + @Test + public void test4() { + CommonUtils.printArray(solution1.constructArray(5, 3)); + CommonUtils.printArray(solution2.constructArray(5, 3)); + } + + @Test + public void test5() { + CommonUtils.printArray(solution1.constructArray(5, 2)); + CommonUtils.printArray(solution2.constructArray(5, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_668Test.java b/src/test/java/com/fishercoder/firstthousand/_668Test.java new file mode 100644 index 0000000000..a75cdfc302 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_668Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._668; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _668Test { + private _668.Solution1 solution1; + private _668.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _668.Solution1(); + solution2 = new _668.Solution2(); + } + + @Test + public void test1() { + assertEquals(3, solution1.findKthNumber(3, 3, 5)); + assertEquals(3, solution2.findKthNumber(3, 3, 5)); + } + + @Test + public void test2() { + assertEquals(6, solution1.findKthNumber(2, 3, 6)); + assertEquals(6, solution2.findKthNumber(2, 3, 6)); + } + + @Test + public void test3() { + // assertEquals(31666344, solution1.findKthNumber(9895, 28405, 100787757));//this + // will run into OOM error, so comment out + assertEquals(31666344, solution2.findKthNumber(9895, 28405, 100787757)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_669Test.java b/src/test/java/com/fishercoder/firstthousand/_669Test.java new file mode 100644 index 0000000000..81e520419c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_669Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._669; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _669Test { + private _669.Solution1 solution1; + private static TreeNode root; + private static TreeNode expected; + + @BeforeEach + public void setup() { + solution1 = new _669.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 2)); + expected = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2)); + assertEquals(expected, solution1.trimBST(root, 1, 2)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 0, 4, null, 2, null, null, 1)); + expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 2, null, 1)); + assertEquals(expected, solution1.trimBST(root, 1, 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_66Test.java b/src/test/java/com/fishercoder/firstthousand/_66Test.java new file mode 100644 index 0000000000..da972e9b3f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_66Test.java @@ -0,0 +1,55 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._66; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _66Test { + private _66.Solution1 solution1; + private _66.Solution2 solution2; + private static int[] digits; + + @BeforeEach + public void setup() { + solution1 = new _66.Solution1(); + solution2 = new _66.Solution2(); + } + + @Test + public void test1() { + digits = new int[] {9, 9, 9, 9}; + assertArrayEquals(new int[] {1, 0, 0, 0, 0}, solution1.plusOne(digits)); + } + + @Test + public void test2() { + digits = new int[] {8, 9, 9, 9}; + assertArrayEquals(new int[] {9, 0, 0, 0}, solution1.plusOne(digits)); + } + + @Test + public void test3() { + digits = new int[] {2, 4, 9, 3, 9}; + assertArrayEquals(new int[] {2, 4, 9, 4, 0}, solution1.plusOne(digits)); + } + + @Test + public void test4() { + digits = new int[] {9, 9, 9, 9, 9}; + assertArrayEquals(new int[] {1, 0, 0, 0, 0, 0}, solution2.plusOne(digits)); + } + + @Test + public void test5() { + digits = new int[] {8, 9, 9, 9, 9}; + assertArrayEquals(new int[] {9, 0, 0, 0, 0}, solution2.plusOne(digits)); + } + + @Test + public void test6() { + digits = new int[] {2, 4, 9, 4, 9}; + assertArrayEquals(new int[] {2, 4, 9, 5, 0}, solution2.plusOne(digits)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_670Test.java b/src/test/java/com/fishercoder/firstthousand/_670Test.java new file mode 100644 index 0000000000..50d8fbe65e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_670Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._670; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _670Test { + private _670.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _670.Solution1(); + } + + @Test + public void test1() { + assertEquals(7236, solution1.maximumSwap(2736)); + } + + @Test + public void test2() { + assertEquals(9973, solution1.maximumSwap(9973)); + } + + @Test + public void test3() { + assertEquals(73236, solution1.maximumSwap(23736)); + } + + @Test + public void test4() { + assertEquals(98213, solution1.maximumSwap(91283)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_671Test.java b/src/test/java/com/fishercoder/firstthousand/_671Test.java new file mode 100644 index 0000000000..9de1a94417 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_671Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._671; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _671Test { + private _671.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _671.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 5, null, null, 5, 7)); + assertEquals(5, solution1.findSecondMinimumValue(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 2)); + assertEquals(-1, solution1.findSecondMinimumValue(root)); + } +} diff --git a/src/test/java/com/fishercoder/_672Test.java b/src/test/java/com/fishercoder/firstthousand/_672Test.java similarity index 87% rename from src/test/java/com/fishercoder/_672Test.java rename to src/test/java/com/fishercoder/firstthousand/_672Test.java index 24bc24d02f..61694d3973 100644 --- a/src/test/java/com/fishercoder/_672Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_672Test.java @@ -1,17 +1,17 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._672; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._672; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _672Test { - private static _672.Solution1 solution1; - private static _672.Solution2 solution2; + private _672.Solution1 solution1; + private _672.Solution2 solution2; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _672.Solution1(); solution2 = new _672.Solution2(); } @@ -105,5 +105,4 @@ public void test15() { assertEquals(8, solution1.flipLights(7, 5)); assertEquals(8, solution2.flipLights(7, 5)); } - -} \ No newline at end of file +} diff --git a/src/test/java/com/fishercoder/firstthousand/_673Test.java b/src/test/java/com/fishercoder/firstthousand/_673Test.java new file mode 100644 index 0000000000..c26cd319c1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_673Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._673; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +public class _673Test { + private _673.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _673.Solution1(); + } + + @Test + @Disabled + public void test1() { + nums = new int[] {1, 3, 5, 4, 7}; + assertEquals(2, solution1.findNumberOfLIS(nums)); + } + + @Test + public void test2() { + nums = new int[] {2, 2, 2, 2, 2}; + assertEquals(5, solution1.findNumberOfLIS(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_674Test.java b/src/test/java/com/fishercoder/firstthousand/_674Test.java new file mode 100644 index 0000000000..02f68cb6ff --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_674Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._674; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _674Test { + private _674.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _674.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 3, 5, 4, 7}; + assertEquals(3, solution1.findLengthOfLCIS(nums)); + } + + @Test + public void test2() { + nums = new int[] {2, 2, 2, 2, 2}; + assertEquals(1, solution1.findLengthOfLCIS(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_675Test.java b/src/test/java/com/fishercoder/firstthousand/_675Test.java new file mode 100644 index 0000000000..34705f2ecb --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_675Test.java @@ -0,0 +1,92 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.ArrayUtils; +import com.fishercoder.solutions.firstthousand._675; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _675Test { + private _675.Solution1 solution1; + private static List> forest; + + @BeforeEach + public void setup() { + solution1 = new _675.Solution1(); + } + + @Test + public void test1() { + forest = new ArrayList<>(); + forest.add(Arrays.asList(1, 2, 3)); + forest.add(Arrays.asList(0, 0, 4)); + forest.add(Arrays.asList(7, 6, 5)); + assertEquals(6, solution1.cutOffTree(forest)); + } + + @Test + public void test2() { + forest = new ArrayList<>(); + forest.add(Arrays.asList(1, 2, 3)); + forest.add(Arrays.asList(0, 0, 0)); + forest.add(Arrays.asList(7, 6, 5)); + assertEquals(-1, solution1.cutOffTree(forest)); + } + + @Test + public void test3() { + forest = new ArrayList<>(); + forest.add(Arrays.asList(2, 3, 4)); + forest.add(Arrays.asList(0, 0, 5)); + forest.add(Arrays.asList(8, 7, 6)); + assertEquals(6, solution1.cutOffTree(forest)); + } + + @Test + public void test4() { + forest = + ArrayUtils.buildList( + new int[][] { + {2, 3, 4, 9}, + {0, 0, 5, 10}, + {8, 7, 6, 12}, + }); + assertEquals(13, solution1.cutOffTree(forest)); + } + + @Test + public void test5() { + forest = + ArrayUtils.buildList( + new int[][] { + {0, 0, 0, 3528, 2256, 9394, 3153}, + {8740, 1758, 6319, 3400, 4502, 7475, 6812}, + {0, 0, 3079, 6312, 0, 0, 0}, + {6828, 0, 0, 0, 0, 0, 8145}, + {6964, 4631, 0, 0, 0, 4811, 0}, + {0, 0, 0, 0, 9734, 4696, 4246}, + {3413, 8887, 0, 4766, 0, 0, 0}, + {7739, 0, 0, 2920, 0, 5321, 2250}, + {3032, 0, 3015, 0, 3269, 8582, 0} + }); + assertEquals(-1, solution1.cutOffTree(forest)); + } + + @Test + public void test6() { + forest = + ArrayUtils.buildList( + new int[][] { + {54581641, 64080174, 24346381, 69107959}, + {86374198, 61363882, 68783324, 79706116}, + {668150, 92178815, 89819108, 94701471}, + {83920491, 22724204, 46281641, 47531096}, + {89078499, 18904913, 25462145, 60813308} + }); + assertEquals(57, solution1.cutOffTree(forest)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_676Test.java b/src/test/java/com/fishercoder/firstthousand/_676Test.java new file mode 100644 index 0000000000..2f8ff0f223 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_676Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._676; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _676Test { + private _676.Solution1.MagicDictionary magicDictionarySol1; + + @BeforeEach + public void setup() { + magicDictionarySol1 = new _676.Solution1.MagicDictionary(); + } + + @BeforeEach + public void cleanup() { + magicDictionarySol1 = new _676.Solution1.MagicDictionary(); + } + + @Test + public void test1() { + magicDictionarySol1.buildDict(new String[] {"hello", "leetcode"}); + assertEquals(false, magicDictionarySol1.search("hello")); + assertEquals(true, magicDictionarySol1.search("hhllo")); + assertEquals(false, magicDictionarySol1.search("hell")); + assertEquals(false, magicDictionarySol1.search("leetcoded")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_678Test.java b/src/test/java/com/fishercoder/firstthousand/_678Test.java new file mode 100644 index 0000000000..d953377274 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_678Test.java @@ -0,0 +1,64 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._678; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _678Test { + private _678.Solution1 solution1; + private _678.Solution2 solution2; + private _678.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _678.Solution1(); + solution2 = new _678.Solution2(); + solution3 = new _678.Solution3(); + } + + @Test + public void test1() { + assertEquals(true, solution1.checkValidString("()")); + assertEquals(true, solution2.checkValidString("()")); + assertEquals(true, solution3.checkValidString("()")); + } + + @Test + public void test2() { + assertEquals(true, solution1.checkValidString("(*)")); + assertEquals(true, solution2.checkValidString("(*)")); + assertEquals(true, solution3.checkValidString("(*)")); + } + + @Test + public void test3() { + assertEquals(true, solution1.checkValidString("(*))")); + assertEquals(true, solution2.checkValidString("(*))")); + assertEquals(true, solution3.checkValidString("(*))")); + } + + @Test + public void test4() { + assertEquals( + false, + solution1.checkValidString( + "(((()))())))*))())()(**(((())(()(*()((((())))*())(())*(*(()(*)))()*())**((()(()))())(*(*))*))())")); + assertEquals( + false, + solution2.checkValidString( + "(((()))())))*))())()(**(((())(()(*()((((())))*())(())*(*(()(*)))()*())**((()(()))())(*(*))*))())")); + assertEquals( + false, + solution3.checkValidString( + "(((()))())))*))())()(**(((())(()(*()((((())))*())(())*(*(()(*)))()*())**((()(()))())(*(*))*))())")); + } + + @Test + public void test5() { + assertEquals(true, solution1.checkValidString("(((******)))")); + assertEquals(true, solution2.checkValidString("(((******)))")); + assertEquals(true, solution3.checkValidString("(((******)))")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_679Test.java b/src/test/java/com/fishercoder/firstthousand/_679Test.java new file mode 100644 index 0000000000..7636332698 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_679Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._679; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _679Test { + private _679.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _679.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.judgePoint24(new int[] {4, 1, 8, 7})); + } + + @Test + public void test2() { + assertEquals(false, solution1.judgePoint24(new int[] {1, 2, 1, 2})); + } + + @Test + public void test3() { + // 8 / (1 - 2/3) = 24 + assertEquals(true, solution1.judgePoint24(new int[] {1, 2, 3, 8})); + } + + @Test + public void test4() { + assertEquals(true, solution1.judgePoint24(new int[] {1, 3, 4, 6})); + } + + @Test + public void test5() { + assertEquals(true, solution1.judgePoint24(new int[] {1, 9, 1, 2})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_67Test.java b/src/test/java/com/fishercoder/firstthousand/_67Test.java new file mode 100644 index 0000000000..63ee6d337a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_67Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._67; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _67Test { + private _67.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _67.Solution1(); + } + + @Test + public void test1() { + assertEquals("100", solution1.addBinary("11", "1")); + } +} diff --git a/src/test/java/com/fishercoder/_680Test.java b/src/test/java/com/fishercoder/firstthousand/_680Test.java similarity index 79% rename from src/test/java/com/fishercoder/_680Test.java rename to src/test/java/com/fishercoder/firstthousand/_680Test.java index 2b7df6e7d2..687226b591 100644 --- a/src/test/java/com/fishercoder/_680Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_680Test.java @@ -1,17 +1,17 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._680; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._680; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _680Test { - private static _680.Solution1 solution1; - private static _680.Solution2 solution2; + private _680.Solution1 solution1; + private _680.Solution2 solution2; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _680.Solution1(); solution2 = new _680.Solution2(); } @@ -57,4 +57,4 @@ public void test7() { assertEquals(false, solution1.validPalindrome("abc")); assertEquals(false, solution2.validPalindrome("abc")); } -} \ No newline at end of file +} diff --git a/src/test/java/com/fishercoder/firstthousand/_681Test.java b/src/test/java/com/fishercoder/firstthousand/_681Test.java new file mode 100644 index 0000000000..f05f48f6fd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_681Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._681; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _681Test { + private _681.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _681.Solution1(); + } + + @Test + public void test1() { + assertEquals("19:39", solution1.nextClosestTime("19:34")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_682Test.java b/src/test/java/com/fishercoder/firstthousand/_682Test.java new file mode 100644 index 0000000000..7129f5f241 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_682Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._682; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _682Test { + private _682.Solution1 solution1; + private static String[] ops; + + @BeforeEach + public void setup() { + solution1 = new _682.Solution1(); + } + + @Test + public void test1() { + ops = new String[] {"5", "2", "C", "D", "+"}; + assertEquals(30, solution1.calPoints(ops)); + } + + @Test + public void test2() { + ops = new String[] {"5", "-2", "4", "C", "D", "9", "+", "+"}; + assertEquals(27, solution1.calPoints(ops)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_683Test.java b/src/test/java/com/fishercoder/firstthousand/_683Test.java new file mode 100644 index 0000000000..cb7db00b9d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_683Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._683; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _683Test { + private _683.Solution1 solution1; + private static int[] flowers; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _683.Solution1(); + } + + @Test + public void test1() { + flowers = new int[] {1, 3, 2}; + k = 1; + assertEquals(2, solution1.kEmptySlots(flowers, k)); + } + + @Test + public void test2() { + flowers = new int[] {1, 2, 3}; + k = 1; + assertEquals(-1, solution1.kEmptySlots(flowers, k)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_684Test.java b/src/test/java/com/fishercoder/firstthousand/_684Test.java new file mode 100644 index 0000000000..d746367e54 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_684Test.java @@ -0,0 +1,77 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._684; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _684Test { + private _684.Solution1 solution1; + private static int[][] edges; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _684.Solution1(); + } + + @Test + public void test1() { + edges = + new int[][] { + {1, 2}, + {1, 3}, + {2, 3} + }; + expected = new int[] {2, 3}; + assertArrayEquals(expected, solution1.findRedundantConnection(edges)); + } + + @Test + public void test2() { + edges = + new int[][] { + {1, 2}, + {2, 3}, + {3, 4}, + {1, 4}, + {1, 5} + }; + expected = new int[] {1, 4}; + assertArrayEquals(expected, solution1.findRedundantConnection(edges)); + } + + @Test + public void test3() { + edges = + new int[][] { + {9, 10}, + {5, 8}, + {2, 6}, + {1, 5}, + {3, 8}, + {4, 9}, + {8, 10}, + {4, 10}, + {6, 8}, + {7, 9} + }; + expected = new int[] {4, 10}; + assertArrayEquals(expected, solution1.findRedundantConnection(edges)); + } + + @Test + public void test4() { + edges = + new int[][] { + {1, 2}, + {2, 3}, + {1, 5}, + {3, 4}, + {1, 4} + }; + expected = new int[] {1, 4}; + assertArrayEquals(expected, solution1.findRedundantConnection(edges)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_685Test.java b/src/test/java/com/fishercoder/firstthousand/_685Test.java new file mode 100644 index 0000000000..8962f7c38e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_685Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._685; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _685Test { + private _685.Solution1 solution1; + private _685.Solution2 solution2; + private static int[][] edges; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _685.Solution1(); + solution2 = new _685.Solution2(); + } + + @Test + public void test1() { + edges = + new int[][] { + {2, 1}, + {3, 1}, + {4, 2}, + {1, 4} + }; + expected = new int[] {2, 1}; + assertArrayEquals(expected, solution1.findRedundantDirectedConnection(edges)); + assertArrayEquals(expected, solution2.findRedundantDirectedConnection(edges)); + } + + @Test + public void test2() { + edges = + new int[][] { + {2, 1}, + {1, 4}, + {4, 3}, + {3, 2} + }; + expected = new int[] {3, 2}; + assertArrayEquals(expected, solution1.findRedundantDirectedConnection(edges)); + assertArrayEquals(expected, solution2.findRedundantDirectedConnection(edges)); + } + + @Test + public void test3() { + edges = + new int[][] { + {1, 2}, + {1, 3}, + {2, 3}, + }; + expected = new int[] {2, 3}; + // assertArrayEquals(expected, solution1.findRedundantDirectedConnection(edges)); + assertArrayEquals(expected, solution2.findRedundantDirectedConnection(edges)); + } +} diff --git a/src/test/java/com/fishercoder/_686Test.java b/src/test/java/com/fishercoder/firstthousand/_686Test.java similarity index 97% rename from src/test/java/com/fishercoder/_686Test.java rename to src/test/java/com/fishercoder/firstthousand/_686Test.java index f4f2f9674b..1dcf28d042 100644 --- a/src/test/java/com/fishercoder/_686Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_686Test.java @@ -1,17 +1,17 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._686; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._686; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _686Test { - private static _686.Solution1 solution1; - private static _686.Solution2 solution2; + private _686.Solution1 solution1; + private _686.Solution2 solution2; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _686.Solution1(); solution2 = new _686.Solution2(); } @@ -48,10 +48,15 @@ public void test5() { @Test public void test6() { - assertEquals(-1, solution1.repeatedStringMatch("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaba")); - assertEquals(-1, solution2.repeatedStringMatch("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaba")); + assertEquals( + -1, + solution1.repeatedStringMatch( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaba")); + assertEquals( + -1, + solution2.repeatedStringMatch( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaba")); } - } diff --git a/src/test/java/com/fishercoder/firstthousand/_687Test.java b/src/test/java/com/fishercoder/firstthousand/_687Test.java new file mode 100644 index 0000000000..35ed371dce --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_687Test.java @@ -0,0 +1,187 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._687; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _687Test { + private _687.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _687.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 5, 1, 1, null, 5)); + assertEquals(2, solution1.longestUnivaluePath(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 4, 5, 4, 4, null, 5)); + TreeUtils.printBinaryTree(root); + assertEquals(2, solution1.longestUnivaluePath(root)); + } + + @Test + public void test3() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 5, 1, 1, null, 5, null, 1, 1)); + TreeUtils.printBinaryTree(root); + assertEquals(4, solution1.longestUnivaluePath(root)); + } + + @Test + public void test4() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 5, 8, 9, 7, 7, 8, 1, 4, 8, 1, 9, 0, 8, 7, 1, 7, 4, 2, 9, 8, 2, 4, + null, null, 9, null, null, null, 6, 0, 9, 4, 1, 0, 1, 8, 9, 0, 1, 8, + 9, 1, 0, 9, 6, 2, 5, null, 2, 3, 0, 2, 4, 8, 8, 8, 5, 0, 0, 9, 4, 9, + 1, null, 0, 7, 2, 2, 3, null, 6, 1, 0, 8, 9, 9, 9, 4, 8, 4, 3, 4, 4, + 0, null, null, 8, 3, 8, null, null, 0, null, 0, 4, 9, 1, 2, null, 4, + 4, 0, 4, 3, 5, 5, 7, 4, 1, 6, null, 1, 0, null, null, null, 2, 8, 7, + 7, null, null, 0, 2, 5, 5, 9, 3, 3, null, 7, 6, 6, 7, 9, 8, 1, 7, 7, + 7, 2, 6, null, 7, null, 4, 6, 4, 6, null, null, 9, 1, null, null, + null, 5, 5, 5, 4, 2, 2, 8, 5, 1, 1, 3, 1, 3, 7, null, 2, null, 9, 1, + 4, 4, 7, 7, null, 1, 5, 6, 2, 7, 3, null, 9, 1, null, 2, 4, 4, 8, + null, null, 7, null, 6, null, 7, 4, 3, 5, 8, 4, 8, 5, null, null, 8, + null, null, null, 4, 4, null, null, null, null, 8, 3, 5, 5, null, + null, null, 1, 2, 0, null, null, 9, 3, null, 8, 3, 7, 1, 8, 9, 0, 1, + 8, 2, null, 4, null, null, 8, null, null, null, null, 2, null, 4, 8, + 5, 5, 3, 1, null, null, 6, null, 1, null, null, 6, null, null, null, + null, 7, 3, null, null, null, 8, 6, 4, null, 6, 9, 0, 7, 8, null, + null, 0, 6, 7, null, null, 0, 0, 7, 2, 3, 2, null, 0, 2, 3, null, 0, + 1, 7, 9, 0, 7, null, null, null, null, 5, 8, 2, 6, 3, 2, 0, 4, null, + null, 0, 9, 1, 1, 1, null, 1, 3, null, 7, 9, 1, 3, 3, 8, null, null, + null, null, 6, null, null, null, null, 9, 8, 1, 3, 8, 3, 0, 6, null, + null, 8, 5, 6, 5, 2, 1, null, 5, null, 7, 0, 0, null, 9, 3, 9, null, + 3, 0, 0, 9, 1, 7, 0, 2, null, 6, 8, 5, null, null, null, null, null, + 7, null, 2, 5, null, null, 9, null, null, null, null, null, null, + null, null, null, null, null, 4, 1, null, 3, 6, 6, 2, 5, 5, 9, null, + null, 7, 8, null, null, 2, 7, 3, 7, 2, 5, null, 1, 3, 4, null, null, + 8, 3, 6, 9, null, 1, null, null, null, null, 9, 7, 5, 2, null, 5, + null, 6, 4, 5, null, 1, 2, 0, 6, null, 1, 6, null, null, 5, null, 7, + 8, 4, 7, 8, 6, 4, null, 5, 6, 7, 9, 1, 0, 4, null, null, null, 6, 4, + 8, 4, 5, null, 0, 4, 4, 0, 1, 7, 1, null, 1, null, 3, 6, null, null, + null, null, 8, null, 5, 0, 7, 5, null, null, 5, 8, null, null, 3, + null, null, 8, null, 2, 4, null, null, null, null, null, null, null, + 9, null, 9, null, 9, null, null, null, null, 7, 1, null, null, 2, + null, null, 5, 5, 5, 5, 6, 4, null, null, 1, 6, 4, 0, null, 0, 6, 3, + 0, null, 5, 5, null, null, null, null, 2, null, 3, 6, null, 3, 0, 5, + 0, 1, 0, 3, 4, 9, 9, 2, 7, 3, 8, 6, 9, null, 5, 8, null, null, null, + null, 9, 8, 0, 7, null, null, 8, 8, 6, 6, 0, 2, 7, 4, 2, 3, 8, 6, 4, + null, 8, null, null, null, 2, 0, null, 1, 3, 5, 4, 2, 2, 5, 8, 8, + null, 3, 0, null, 1, 6, 0, null, null, 9, null, 2, null, 6, 8, 2, + null, null, 5, null, null, null, 9, 6, 6, 4, 2, 0, null, null, 1, + null, 0, null, null, null, 6, 6, null, null, null, 4, 7, 9, null, 0, + 1, null, null, 9, null, null, null, 4, null, 8, null, null, null, + null, null, null, 4, null, 6, null, 3, null, null, 5, 1, 2, 5, null, + 0, 7, 8, null, 7, null, null, 4, null, 4, 4, null, 2, null, 6, null, + null, null, 7, null, null, null, null, 6, 4, null, 6, null, 6, 9, + null, null, null, 9, 6, null, 9, null, 3, null, 2, null, 7, 7, null, + null, 0, null, 6, 3, null, null, null, null, null, null, 1, null, + null, null, 6, 9, 7, null, 7, null, 9, 3, 3, null, null, null, null, + 4, null, null, 3, null, null, null, 3, 9, null, 0, 3, 1, 9, 6, 7, 9, + 4, 8, null, null, 6, null, 1, 3, 7, null, null, null, 3, null, 2, + null, 8, 1, 1, null, null, 6, null, 7, 3, 5, null, 6, 3, 4, null, + null, 5, 7, 1, null, null, 6, 4, 6, null, null, null, null, 5, 7, 0, + 7, 0, null, 5, 8, 5, 5, 4, 5, null, null, null, null, null, null, 1, + 7, null, null, 7, null, 9, 9, 6, 4, null, null, 3, 2, 1, null, 0, + null, 0, 6, null, null, null, 1, 5, null, null, null, 8, null, null, + null, null, 3, 4, 8, null, null, 9, 6, 4, null, null, null, null, 8, + 9, null, 1, null, null, null, 7, null, null, null, null, null, 9, + null, null, null, 4, 1, 6, 7, 0, null, null, null, 7, null, null, 8, + null, null, null, null, null, null, null, 4, null, 9, null, null, + null, null, 3, 0, 6, null, 5, null, 9, 9, null, null, 4, 3, 4, null, + null, null, null, 8, null, 5, null, null, null, null, 5, 2, null, + null, null, null, null, null, null, 2, null, null, 2, 1, 8, 5, null, + 0, null, 0, 3, 2, 4, 5, null, null, null, null, null, 7, null, null, + 0, null, 0, null, null, null, 0, 3, 9, null, null, null, null, 5, + null, null, 0, 5, 0, 0, null, 9, null, null, null, null, null, null, + null, null, 8, null, 9, 3, 5, 9, 0, 5, 9, null, null, 9, 4, null, 0, + 2, 0, null, null, 7, null, 7, null, 5, 7, 8, 7, null, null, null, 3, + 0, 3, null, null, null, null, null, 4, 5, null, null, 2, 3, null, 2, + null, null, 7, null, null, 9, null, null, 9, 7, 1, null, null, 1, 6, + 1, 8, null, null, 5, null, null, 3, 7, 9, 6, null, null, null, null, + 1, null, null, null, 3, 7, 3, 2, 3, 3, null, 1, null, null, null, 1, + null, null, 4, 3, 4, 8, 7, null, 0, 3, 0, null, 1, 1, null, null, + null, null, null, 5, null, 6, 0, null, 3, 1, null, 6, null, null, 4, + 0, 1, null, 6, 1, null, null, 9, 6, 4, 9, 0, 8, 9, 3, 3, 6, null, + null, null, null, null, null, null, null, null, null, null, null, 2, + null, null, null, null, null, 8, 5, 8, 3, 5, 4, null, 6, null, 0, + null, null, 6, null, 4, 3, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, 7, 3, null, null, 1, null, + 2, 4, null, null, null, 6, null, null, null, 6, null, 5, null, null, + null, null, 1, null, null, 3, null, 1, null, 7, 1, null, null, 7, 1, + 3, 4, 8, null, null, null, null, null, 4, null, null, 4, null, null, + null, 7, null, 6, null, null, 1, null, null, null, 7, 3, 3, null, + null, null, null, 3, 0, null, null, 4, null, null, null, null, null, + null, null, null, null, null, 8, null, null, 9, null, null, 6, 6, 5, + 2, null, 8, 3, 8, null, null, null, null, 6, 7, 0, null, null, null, + null, 1, 1, 5, null, 0, 5, null, 5, null, null, null, 1, 2, null, 2, + 9, 1, null, 2, 4, 1, null, null, null, 1, 8, 4, 4, 5, 2, null, null, + 6, 4, 7, 5, 2, 9, null, 4, null, null, null, null, null, 3, null, + null, 5, 9, null, null, null, null, 9, null, 9, null, null, null, 2, + null, 1, 9, null, null, null, null, null, 1, 9, 3, null, null, 1, 9, + null, 5, 2, 1, 0, null, null, 1, 9, 8, 4, 7, null, null, 5, 7, null, + null, null, null, 1, 2, 8, null, 6, 0, null, null, null, null, 0, + null, null, null, 6, null, 2, 3, 0, 9, null, null, 1, 4, 6, null, 8, + null, null, 5, null, 3, 0, null, 6, null, null, null, null, null, 2, + null, null, null, null, null, null, 2, 5, 8, 6, 9, null, null, null, + 8, null, null, 9, 6, null, null, null, null, 3, null, null, null, + null, 9, null, null, 2, null, null, null, null, null, null, 8, 8, + null, null, null, null, null, 9, null, 6, null, 2, 5, null, null, 1, + 2, null, 4, null, null, 4, null, null, 3, 5, null, 3, 3, null, null, + 1, null, null, null, null, 4, null, 2, 3, null, 4, 5, 3, null, 7, + null, null, null, 7, 6, null, null, 1, 3, null, 4, 9, 8, null, null, + 0, null, 3, 4, null, 8, null, 1, null, null, 2, 2, null, null, 4, + null, null, null, 3, null, null, 2, null, null, null, 4, null, 5, + null, null, null, null, 2, null, 5, null, null, null, null, null, + null, 2, 7, 5, null, 6, null, null, null, null, 2, null, 0, null, 3, + null, 1, null, 9, 4, null, 3, null, null, null, null, null, null, + null, 5, 5, 7, null, null, 1, null, 4, 6, null, null, null, 2, null, + 5, 9, 0, 6, 2, null, null, null, null, null, null, null, null, null, + null, null, null, 5, null, 7, null, 2, 9, null, null, 1, null, null, + null, 1, 6, null, 6, null, null, 0, 8, null, 4, null, null, null, + null, 4, null, null, 0, null, 6, 0, null, null, null, 4, null, null, + null, null, null, 0, null, null, null, null, null, null, null, null, + null, null, null, null, 0, 5, 4, 2, 6, 4, 5, 3, 4, null, null, 5, + null, null, null, null, 4, null, null, 3, 6, 2, 0, null, 6, 6, null, + null, null, null, 0, 6, null, null, null, 3, 9, 4, null, null, null, + null, null, 0, null, null, 6, 7, 0, null, 9, 2, null, 3, 3, null, + null, 8, null, 3, null, null, null, 8, 5, 3, null, 2, 4, null, 9, 6, + 9, null, null, null, null, 6, null, 6, null, 5, 3, null, null, null, + null, 4, null, null, null, 9, 0, 9, 7, 1, 1, null, 1, null, 1, 6, + null, 5, null, 6, null, null, 1, null, null, null, null, null, null, + 5, null, null, null, null, null, 3, null, 6, 1, null, 0, 2, null, + null, 0, null, null, 0, null, null, null, null, null, 3, null, null, + 8, null, null, 5, 3, 3, null, null, null, null, null, null, null, 3, + null, null, 0, 8, 7, null, null, 8, 1, null, null, null, null, null, + null, 7, null, null, null, null, null, null, null, null, null, null, + null, 5, 2, null, 2, 6, null, null, null, null, null, null, null, 1, + 5, 0, null, null, 2, null, 7, null, null, 6, null, null, null, null, + null, null, null, null, null, null, null, null, null, 8, null, null, + null, null, 3, null, null, 4, null, null, 2, null, null, null, null, + 0, 3, null, null, null, null, null, 7, null, 8, null, null, null, + null, 8, 5, null, 3, 4, null, null, null, 8, null, null, null, null, + null, null, null, null, null, 3, 7, null, null, null, 4, 0, 3, null, + null, 6, null, null, null, null, null, null, null, null, null, null, + null, null, 8, null, null, null, null, null, 2, null, null, null, + null, null, null, null, null, null, 0, null, null, null, 2, null, + null, null, 8, 2, null, null, null, null, null, null, null, 8, null, + null, null, null, null, null, null, null, null, null, 2, null, null, + null, 2, 5, null, null, null, null, null, null, null, null, null, + null, null, 2, null, null, null, null, null, 8, null, null, null, + null, null, null, null, null, null, null, 0, 5)); + assertEquals(2, solution1.longestUnivaluePath(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_688Test.java b/src/test/java/com/fishercoder/firstthousand/_688Test.java new file mode 100644 index 0000000000..b98a2e4a04 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_688Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.firstthousand._688; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _688Test { + private _688.Solution1 solution1; + private _688.Solution2 solution2; + + @BeforeEach + public void setupForEachTest() { + solution1 = new _688.Solution1(); + solution2 = new _688.Solution2(); + } + + @Test + public void test1() { + assertEquals(0.0625, solution1.knightProbability(3, 2, 0, 0)); + assertEquals(0.0625, solution2.knightProbability(3, 2, 0, 0)); + } + + @Test + public void test2() { + assertTrue(Math.abs(0.00019 - solution2.knightProbability(8, 30, 6, 4)) < 10e-7); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_689Test.java b/src/test/java/com/fishercoder/firstthousand/_689Test.java new file mode 100644 index 0000000000..7c0a8a4e9d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_689Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._689; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _689Test { + private _689.Solution1 solution1; + private static int[] nums; + private static int[] expected; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _689.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 1, 2, 6, 7, 5, 1}; + expected = new int[] {0, 3, 5}; + k = 2; + assertArrayEquals(expected, solution1.maxSumOfThreeSubarrays(nums, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_68Test.java b/src/test/java/com/fishercoder/firstthousand/_68Test.java new file mode 100644 index 0000000000..8887bb9ecd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_68Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._68; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _68Test { + private _68.Solution1 solution1; + private static String[] words; + + @BeforeEach + public void setup() { + solution1 = new _68.Solution1(); + } + + @Test + public void test1() { + words = + new String[] { + "This", "is", "a", "good", "test!", "\n", "What", "do", "you", "\n", "think?", + "\n", "I", "think", "so", "too!" + }; + assertEquals( + Arrays.asList( + "This is a good", + "test! \n What do", + "you \n think? \n I", + "think so too! "), + solution1.fullJustify(words, 16)); + } + + @Test + public void test2() { + words = new String[] {"This", "is", "an", "example", "of", "text", "justification."}; + assertEquals( + Arrays.asList("This is an", "example of text", "justification. "), + solution1.fullJustify(words, 16)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_690Test.java b/src/test/java/com/fishercoder/firstthousand/_690Test.java new file mode 100644 index 0000000000..5dda2e4834 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_690Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.Employee; +import com.fishercoder.solutions.firstthousand._690; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/18/17. */ +public class _690Test { + private _690.Solution1 solution1; + private static List employees; + private static int id; + + @BeforeEach + public void setupForEachTest() { + solution1 = new _690.Solution1(); + } + + @Test + public void test1() { + employees = + new ArrayList( + Arrays.asList( + new Employee(1, 5, Arrays.asList(2, 3)), + new Employee(2, 3, Arrays.asList()), + new Employee(3, 3, Arrays.asList()))); + id = 1; + assertEquals(11, solution1.getImportance(employees, id)); + } + + @Test + public void test2() { + employees = + new ArrayList( + Arrays.asList( + new Employee(1, 5, Arrays.asList(2, 3)), + new Employee(2, 3, Arrays.asList(4)), + new Employee(3, 4, Arrays.asList()), + new Employee(4, 1, Arrays.asList()))); + id = 1; + assertEquals(13, solution1.getImportance(employees, id)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_692Test.java b/src/test/java/com/fishercoder/firstthousand/_692Test.java new file mode 100644 index 0000000000..d01abb59bc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_692Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._692; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _692Test { + private _692.Solution1 solution1; + private static String[] words; + private static List expected; + private static List actual; + + @BeforeEach + public void setup() { + solution1 = new _692.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"i", "love", "leetcode", "i", "love", "coding"}; + actual = solution1.topKFrequent(words, 2); + expected = new ArrayList<>(Arrays.asList("i", "love")); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_694Test.java b/src/test/java/com/fishercoder/firstthousand/_694Test.java new file mode 100644 index 0000000000..4b6dbd4a4e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_694Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._694; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _694Test { + private _694.Solution1 solution1; + private static int[][] grid; + + @BeforeEach + public void setup() { + solution1 = new _694.Solution1(); + } + + @Test + public void test1() { + grid = + new int[][] { + {1, 1, 0, 1, 1}, + {1, 0, 0, 0, 0}, + {0, 0, 0, 0, 1}, + {1, 1, 0, 1, 1} + }; + assertEquals(3, solution1.numDistinctIslands(grid)); + } + + @Test + public void test2() { + grid = + new int[][] { + {1, 1, 0, 0, 0}, + {1, 1, 0, 0, 0}, + {0, 0, 0, 1, 1}, + {0, 0, 0, 1, 1} + }; + assertEquals(1, solution1.numDistinctIslands(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_695Test.java b/src/test/java/com/fishercoder/firstthousand/_695Test.java new file mode 100644 index 0000000000..ca7baaefd1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_695Test.java @@ -0,0 +1,117 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._695; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _695Test { + private _695.Solution1 solution1; + private _695.Solution2 solution2; + private static int[][] grid; + + @BeforeEach + public void setup() { + solution1 = new _695.Solution1(); + solution2 = new _695.Solution2(); + } + + @Test + public void test1() { + grid = + new int[][] { + {0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, + {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0}, + {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0} + }; + assertEquals(6, solution1.maxAreaOfIsland(grid)); + } + + @Test + public void test2() { + grid = + new int[][] { + {0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, + {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0}, + {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0} + }; + assertEquals(6, solution2.maxAreaOfIsland(grid)); + } + + @Test + public void test3() { + grid = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]"); + assertEquals(4, solution2.maxAreaOfIsland(grid)); + } + + @Test + public void test4() { + grid = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," + + "[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]"); + assertEquals(2500, solution2.maxAreaOfIsland(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_697Test.java b/src/test/java/com/fishercoder/firstthousand/_697Test.java new file mode 100644 index 0000000000..f4dcc43364 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_697Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._697; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _697Test { + private _697.Solution1 solution1; + private _697.Solution2 solution2; + private _697.Solution3 solution3; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _697.Solution1(); + solution2 = new _697.Solution2(); + solution3 = new _697.Solution3(); + } + + @Test + public void test1() { + nums = new int[] {1}; + assertEquals(1, solution1.findShortestSubArray(nums)); + assertEquals(1, solution2.findShortestSubArray(nums)); + assertEquals(1, solution3.findShortestSubArray(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 2, 3, 1}; + assertEquals(2, solution1.findShortestSubArray(nums)); + assertEquals(2, solution2.findShortestSubArray(nums)); + assertEquals(2, solution3.findShortestSubArray(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 2, 3, 1, 1}; + assertEquals(6, solution1.findShortestSubArray(nums)); + assertEquals(6, solution2.findShortestSubArray(nums)); + assertEquals(6, solution3.findShortestSubArray(nums)); + } + + @Test + public void test4() { + nums = new int[] {1, 2, 2, 3, 1, 1, 5}; + assertEquals(6, solution1.findShortestSubArray(nums)); + assertEquals(6, solution2.findShortestSubArray(nums)); + assertEquals(6, solution3.findShortestSubArray(nums)); + } + + @Test + public void test5() { + nums = new int[] {1, 2, 2, 3, 1, 4, 2}; + assertEquals(6, solution1.findShortestSubArray(nums)); + assertEquals(6, solution2.findShortestSubArray(nums)); + assertEquals(6, solution3.findShortestSubArray(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_698Test.java b/src/test/java/com/fishercoder/firstthousand/_698Test.java new file mode 100644 index 0000000000..a0771d8bd7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_698Test.java @@ -0,0 +1,88 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._698; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _698Test { + private _698.Solution1 solution1; + private _698.Solution2 solution2; + private static int[] nums; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _698.Solution1(); + solution2 = new _698.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {4, 3, 2, 3, 5, 2, 1}; + k = 4; + assertEquals(true, solution1.canPartitionKSubsets(nums, k)); + } + + @Test + public void test2() { + nums = new int[] {-1, 1, 0, 0}; + k = 4; + assertEquals(false, solution1.canPartitionKSubsets(nums, k)); + } + + @Test + public void test3() { + nums = new int[] {4, 3, 2, 3, 5, 2, 1}; + k = 4; + assertEquals(true, solution2.canPartitionKSubsets(nums, k)); + } + + @Test + public void test4() { + nums = new int[] {-1, 1, 0, 0}; + k = 4; + assertEquals(false, solution2.canPartitionKSubsets(nums, k)); + } + + @Test + public void test5() { + nums = new int[] {2, 2, 2, 2, 3, 4, 5}; + k = 4; + assertEquals(false, solution2.canPartitionKSubsets(nums, k)); + } + + @Test + public void test6() { + nums = new int[] {1, 2, 3, 4}; + k = 3; + assertEquals(false, solution2.canPartitionKSubsets(nums, k)); + } + + @Test + public void test7() { + nums = new int[] {1, 1, 1, 1, 2, 2, 2, 2}; + k = 3; + assertEquals(true, solution2.canPartitionKSubsets(nums, k)); + } + + @Test + public void test8() { + /** This test case clearly shows how backtracking plays out beautifully! */ + nums = + new int[] { + 3522, 181, 521, 515, 304, 123, 2512, 312, 922, 407, 146, 1932, 4037, 2646, 3871, + 269 + }; + k = 5; + assertEquals(true, solution2.canPartitionKSubsets(nums, k)); + } + + @Test + public void test9() { + nums = new int[] {1, 2, 3, 5}; + k = 2; + assertEquals(false, solution2.canPartitionKSubsets(nums, k)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_699Test.java b/src/test/java/com/fishercoder/firstthousand/_699Test.java new file mode 100644 index 0000000000..d336363d1b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_699Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._699; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _699Test { + private _699.Solution1 solution1; + private static int[][] positions; + + @BeforeEach + public void setup() { + solution1 = new _699.Solution1(); + } + + @Test + public void test1() { + positions = + new int[][] { + {1, 2}, + {2, 3}, + {6, 1} + }; + assertEquals(Arrays.asList(2, 5, 5), solution1.fallingSquares(positions)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_69Test.java b/src/test/java/com/fishercoder/firstthousand/_69Test.java new file mode 100644 index 0000000000..0ea616e813 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_69Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._69; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _69Test { + private _69.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _69.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.mySqrt(16)); + } + + @Test + public void test2() { + assertEquals(2, solution1.mySqrt(8)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_6Test.java b/src/test/java/com/fishercoder/firstthousand/_6Test.java new file mode 100644 index 0000000000..f193da17ee --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_6Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._6; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _6Test { + private _6.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _6.Solution1(); + } + + @Test + public void test1() { + assertEquals("PAHNAPLSIIGYIR", solution1.convert("PAYPALISHIRING", 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_700Test.java b/src/test/java/com/fishercoder/firstthousand/_700Test.java new file mode 100644 index 0000000000..683352bda8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_700Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._700; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _700Test { + private _700.Solution1 solution1; + private static TreeNode root; + private static TreeNode expected; + + @BeforeEach + public void setup() { + solution1 = new _700.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 7, 1, 3)); + expected = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3)); + assertEquals(expected, solution1.searchBST(root, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_701Test.java b/src/test/java/com/fishercoder/firstthousand/_701Test.java new file mode 100644 index 0000000000..a0bbb08ea5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_701Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._701; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _701Test { + private _701.Solution1 solution1; + private _701.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _701.Solution1(); + solution2 = new _701.Solution2(); + } + + @Test + public void test1() { + int val = 88; + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList(61, 46, 66, 43, null, null, null, 39, null, null, null)); + TreeUtils.printBinaryTree(root); + TreeUtils.printBinaryTree(solution1.insertIntoBST(root, val)); + } + + @Test + public void test2() { + int val = 88; + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList(61, 46, 66, 43, null, null, null, 39, null, null, null)); + TreeUtils.printBinaryTree(root); + TreeUtils.printBinaryTree(solution2.insertIntoBST(root, val)); + } + + @Test + public void test3() { + int val = 5; + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 7, 1, 3)); + TreeUtils.printBinaryTree(root); + TreeUtils.printBinaryTree(solution2.insertIntoBST(root, val)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_703Test.java b/src/test/java/com/fishercoder/firstthousand/_703Test.java new file mode 100644 index 0000000000..a7d20078f7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_703Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._703; +import org.junit.jupiter.api.Test; + +public class _703Test { + private _703.Solution1.KthLargest solution1; + private static int[] A; + + @Test + public void test1() { + solution1 = new _703.Solution1.KthLargest(3, new int[] {4, 5, 8, 2}); + assertEquals(4, solution1.add(3)); + assertEquals(5, solution1.add(5)); + assertEquals(5, solution1.add(10)); + assertEquals(8, solution1.add(9)); + assertEquals(8, solution1.add(4)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_704Test.java b/src/test/java/com/fishercoder/firstthousand/_704Test.java new file mode 100644 index 0000000000..c9f71a3f3c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_704Test.java @@ -0,0 +1,56 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._704; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _704Test { + private _704.Solution1 solution1; + private _704.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _704.Solution1(); + solution2 = new _704.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {-1, 0, 3, 5, 9, 12}; + assertEquals(4, solution1.search(nums, 9)); + } + + @Test + public void test2() { + nums = new int[] {-1, 0, 3, 5, 9, 12}; + assertEquals(-1, solution1.search(nums, 2)); + } + + @Test + public void test3() { + nums = new int[] {5}; + assertEquals(0, solution1.search(nums, 5)); + assertEquals(0, solution2.search(nums, 5)); + } + + @Test + public void test4() { + nums = new int[] {-1, 0}; + assertEquals(1, solution1.search(nums, 0)); + } + + @Test + public void test5() { + nums = new int[] {-1, 0, 3, 5, 9, 12}; + assertEquals(1, solution1.search(nums, 0)); + } + + @Test + public void test6() { + nums = new int[] {2, 5}; + assertEquals(-1, solution2.search(nums, 0)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_706Test.java b/src/test/java/com/fishercoder/firstthousand/_706Test.java new file mode 100644 index 0000000000..a0ba9fea20 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_706Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._706; +import org.junit.jupiter.api.Test; + +public class _706Test { + private _706.Solution2.MyHashMap myHashMap; + + @Test + public void test1() { + myHashMap = new _706.Solution2.MyHashMap(); + + myHashMap.put(1, 1); + myHashMap.put(2, 2); + assertEquals(1, myHashMap.get(1)); + assertEquals(-1, myHashMap.get(3)); + myHashMap.put(2, 1); + assertEquals(1, myHashMap.get(2)); + myHashMap.remove(2); + assertEquals(-1, myHashMap.get(2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_709Test.java b/src/test/java/com/fishercoder/firstthousand/_709Test.java new file mode 100644 index 0000000000..27b2a53cda --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_709Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._709; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _709Test { + private _709.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _709.Solution1(); + } + + @Test + public void test1() { + assertEquals("hello", solution1.toLowerCase("Hello")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_70Test.java b/src/test/java/com/fishercoder/firstthousand/_70Test.java new file mode 100644 index 0000000000..14a2093469 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_70Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._70; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _70Test { + private _70.Solution1 solution1; + private _70.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _70.Solution1(); + solution2 = new _70.Solution2(); + } + + @Test + public void test1() { + assertEquals(3, solution1.climbStairs(3)); + } + + @Test + public void test2() { + assertEquals(3, solution2.climbStairs(3)); + } + + @Test + public void test3() { + assertEquals(13, solution1.climbStairs(6)); + } + + @Test + public void test4() { + assertEquals(13, solution2.climbStairs(6)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_712Test.java b/src/test/java/com/fishercoder/firstthousand/_712Test.java new file mode 100644 index 0000000000..76dcc35acc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_712Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._712; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _712Test { + private _712.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _712.Solution1(); + } + + @Test + public void test1() { + assertEquals(231, solution1.minimumDeleteSum("sea", "eat")); + } + + @Test + public void test2() { + assertEquals(403, solution1.minimumDeleteSum("delete", "leet")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_713Test.java b/src/test/java/com/fishercoder/firstthousand/_713Test.java new file mode 100644 index 0000000000..b943808706 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_713Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._713; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _713Test { + private _713.Solution1 solution1; + private static int[] nums; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _713.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3}; + k = 0; + assertEquals(0, solution1.numSubarrayProductLessThanK(nums, k)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_714Test.java b/src/test/java/com/fishercoder/firstthousand/_714Test.java new file mode 100644 index 0000000000..576ccf598d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_714Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._714; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _714Test { + private _714.Solution1 solution1; + private _714.Solution2 solution2; + private static int[] prices; + private static int fee; + + @BeforeEach + public void setup() { + solution1 = new _714.Solution1(); + solution2 = new _714.Solution2(); + } + + @Test + public void test1() { + prices = new int[] {1, 3, 2, 8, 4, 9}; + fee = 2; + assertEquals(8, solution1.maxProfit(prices, fee)); + assertEquals(8, solution2.maxProfit(prices, fee)); + } + + @Test + public void test2() { + prices = new int[] {1, 3, 7, 5, 10, 3}; + fee = 3; + assertEquals(6, solution1.maxProfit(prices, fee)); + assertEquals(6, solution2.maxProfit(prices, fee)); + } + + @Test + public void test3() { + prices = new int[] {1, 4, 6, 2, 8, 3, 10, 14}; + fee = 3; + assertEquals(13, solution1.maxProfit(prices, fee)); + assertEquals(13, solution2.maxProfit(prices, fee)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_716Test.java b/src/test/java/com/fishercoder/firstthousand/_716Test.java new file mode 100644 index 0000000000..6655aa0c44 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_716Test.java @@ -0,0 +1,95 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._716; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _716Test { + private _716.Solution1.MaxStack maxStackSolution1; + private _716.Solution2.MaxStack maxStackSolution2; + private _716.Solution3.MaxStack maxStackSolution3; + + @BeforeEach + public void setup() { + maxStackSolution1 = new _716.Solution1.MaxStack(); + maxStackSolution2 = new _716.Solution2.MaxStack(); + maxStackSolution3 = new _716.Solution3.MaxStack(); + } + + @Test + public void test1() { + maxStackSolution1.push(5); + assertEquals(5, maxStackSolution1.peekMax()); + maxStackSolution2.push(5); + assertEquals(5, maxStackSolution2.popMax()); + maxStackSolution3.push(5); + assertEquals(5, maxStackSolution3.popMax()); + } + + @Test + public void test2() { + maxStackSolution1.push(5); + maxStackSolution1.push(1); + assertEquals(5, maxStackSolution1.popMax()); + assertEquals(1, maxStackSolution1.peekMax()); + } + + @Test + public void test3() { + maxStackSolution1.push(74); + assertEquals(74, maxStackSolution1.popMax()); + maxStackSolution1.push(89); + maxStackSolution1.push(67); + assertEquals(89, maxStackSolution1.popMax()); + assertEquals(67, maxStackSolution1.pop()); + maxStackSolution1.push(61); + maxStackSolution1.push(-77); + assertEquals(61, maxStackSolution1.peekMax()); + assertEquals(61, maxStackSolution1.popMax()); + maxStackSolution1.push(81); + assertEquals(81, maxStackSolution1.peekMax()); + assertEquals(81, maxStackSolution1.popMax()); + maxStackSolution1.push(81); + assertEquals(81, maxStackSolution1.pop()); + maxStackSolution1.push(-71); + maxStackSolution1.push(32); + } + + @Test + public void test4() { + maxStackSolution2.push(5); + assertEquals(5, maxStackSolution2.peekMax()); + assertEquals(5, maxStackSolution2.popMax()); + } + + @Test + public void test5() { + maxStackSolution2.push(5); + maxStackSolution2.push(1); + assertEquals(5, maxStackSolution2.popMax()); + assertEquals(1, maxStackSolution2.peekMax()); + } + + @Test + public void test6() { + maxStackSolution2.push(74); + assertEquals(74, maxStackSolution2.popMax()); + maxStackSolution2.push(89); + maxStackSolution2.push(67); + assertEquals(89, maxStackSolution2.popMax()); + assertEquals(67, maxStackSolution2.pop()); + maxStackSolution2.push(61); + maxStackSolution2.push(-77); + assertEquals(61, maxStackSolution2.peekMax()); + assertEquals(61, maxStackSolution2.popMax()); + maxStackSolution2.push(81); + assertEquals(81, maxStackSolution2.peekMax()); + assertEquals(81, maxStackSolution2.popMax()); + maxStackSolution2.push(81); + assertEquals(81, maxStackSolution2.pop()); + maxStackSolution2.push(-71); + maxStackSolution2.push(32); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_718Test.java b/src/test/java/com/fishercoder/firstthousand/_718Test.java new file mode 100644 index 0000000000..4a67f31e60 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_718Test.java @@ -0,0 +1,216 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._718; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _718Test { + private _718.Solution1 solution1; + private _718.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _718.Solution1(); + solution2 = new _718.Solution2(); + } + + @Test + public void test1() { + assertEquals(3, solution1.findLength(new int[] {1, 2, 3, 2, 1}, new int[] {3, 2, 1, 4, 7})); + assertEquals(3, solution2.findLength(new int[] {1, 2, 3, 2, 1}, new int[] {3, 2, 1, 4, 7})); + } + + @Test + public void test2() { + assertEquals(5, solution1.findLength(new int[] {0, 0, 0, 0, 0}, new int[] {0, 0, 0, 0, 0})); + assertEquals(5, solution2.findLength(new int[] {0, 0, 0, 0, 0}, new int[] {0, 0, 0, 0, 0})); + } + + @Test + public void test3() { + assertEquals( + 681, + solution1.findLength( + new int[] { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, + new int[] { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + })); + assertEquals( + 681, + solution2.findLength( + new int[] { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, + new int[] { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + })); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_719Test.java b/src/test/java/com/fishercoder/firstthousand/_719Test.java new file mode 100644 index 0000000000..66e079d036 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_719Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._719; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _719Test { + private _719.Solution1 solution1; + private _719.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _719.Solution1(); + solution2 = new _719.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 3, 1}; + assertEquals(0, solution1.smallestDistancePair(nums, 1)); + assertEquals(0, solution2.smallestDistancePair(nums, 1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_71Test.java b/src/test/java/com/fishercoder/firstthousand/_71Test.java new file mode 100644 index 0000000000..569d9dc9f0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_71Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._71; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _71Test { + private _71.Solution1 solution1; + private _71.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _71.Solution1(); + solution2 = new _71.Solution2(); + } + + @Test + public void test1() { + assertEquals("/home", solution1.simplifyPath("/home/")); + } + + @Test + public void test2() { + assertEquals("/c", solution1.simplifyPath("/a/./b/../../c/")); + } + + @Test + public void test3() { + assertEquals("/a/b/c", solution1.simplifyPath("/a//b////c/d//././/..")); + } + + @Test + public void test4() { + assertEquals("/", solution1.simplifyPath("/.")); + } + + @Test + public void test5() { + // assertEquals("/home/user/Pictures", + // solution1.simplifyPath("/home/user/Documents/../Pictures")); + assertEquals( + "/home/user/Pictures", solution2.simplifyPath("/home/user/Documents/../Pictures")); + } + + @Test + public void test6() { + assertEquals("/", solution1.simplifyPath("/../")); + assertEquals("/", solution2.simplifyPath("/../")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_720Test.java b/src/test/java/com/fishercoder/firstthousand/_720Test.java new file mode 100644 index 0000000000..cfa748e07a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_720Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._720; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _720Test { + private _720.Solution1 solution1; + private static String[] words; + + @BeforeEach + public void setup() { + solution1 = new _720.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"w", "wo", "wor", "worl", "world"}; + assertEquals("world", solution1.longestWord(words)); + } + + @Test + public void test2() { + words = new String[] {"a", "banana", "app", "appl", "ap", "apply", "apple"}; + assertEquals("apple", solution1.longestWord(words)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_721Test.java b/src/test/java/com/fishercoder/firstthousand/_721Test.java new file mode 100644 index 0000000000..e25ce1e157 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_721Test.java @@ -0,0 +1,141 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.solutions.firstthousand._721; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _721Test { + private _721.Solution1 solution1; + private _721.Solution2 solution2; + private static List> accounts; + private static List> expected; + + @BeforeEach + public void setup() { + solution1 = new _721.Solution1(); + solution2 = new _721.Solution2(); + } + + @Test + public void test1() throws Exception { + accounts = new ArrayList<>(); + List account1 = + new ArrayList<>(Arrays.asList("John", "johnsmith@mail.com", "john00@mail.com")); + List account2 = new ArrayList<>(Arrays.asList("John", "johnnybravo@mail.com")); + List account3 = + new ArrayList<>( + Arrays.asList("John", "johnsmith@mail.com", "john_newyork@mail.com")); + List account4 = new ArrayList<>(Arrays.asList("Mary", "mary@mail.com")); + accounts.add(account1); + accounts.add(account2); + accounts.add(account3); + accounts.add(account4); + + expected = new ArrayList<>(); + List expected1 = new ArrayList<>(Arrays.asList("Mary", "mary@mail.com")); + List expected2 = + new ArrayList<>( + Arrays.asList( + "John", + "john00@mail.com", + "john_newyork@mail.com", + "johnsmith@mail.com")); + List expected3 = new ArrayList<>(Arrays.asList("John", "johnnybravo@mail.com")); + expected.add(expected1); + expected.add(expected2); + expected.add(expected3); + + assertEqualsIgnoreOrdering(expected, solution1.accountsMerge(accounts)); + assertEqualsIgnoreOrdering(expected, solution2.accountsMerge(accounts)); + } + + private void assertEqualsIgnoreOrdering(List> expected, List> actual) + throws Exception { + // TODO: implement this method + if (true) { + return; + } else { + throw new Exception(); + } + } + + @Test + public void test2() throws Exception { + accounts = new ArrayList<>(); + List account1 = + new ArrayList<>(Arrays.asList("Alex", "Alex5@m.co", "Alex4@m.co", "Alex0@m.co")); + List account2 = + new ArrayList<>( + Arrays.asList("Ethan", "Ethan3@m.co", "Ethan3@m.co", "Ethan0@m.co")); + List account3 = + new ArrayList<>( + Arrays.asList("Kevin", "Kevin4@m.co", "Kevin2@m.co", "Kevin2@m.co")); + List account4 = + new ArrayList<>(Arrays.asList("Gabe", "Gabe0@m.co", "Gabe3@m.co", "Gabe2@m.co")); + List account5 = + new ArrayList<>(Arrays.asList("Gabe", "Gabe3@m.co", "Gabe4@m.co", "Gabe2@m.co")); + accounts.add(account1); + accounts.add(account2); + accounts.add(account3); + accounts.add(account4); + accounts.add(account5); + + expected = new ArrayList<>(); + List expected1 = + new ArrayList<>(Arrays.asList("Alex", "Alex0@m.co", "Alex4@m.co", "Alex5@m.co")); + List expected2 = + new ArrayList<>(Arrays.asList("Kevin", "Kevin2@m.co", "Kevin4@m.co")); + List expected3 = + new ArrayList<>(Arrays.asList("Ethan", "Ethan0@m.co", "Ethan3@m.co")); + List expected4 = + new ArrayList<>( + Arrays.asList( + "Gabe", "Gabe0@m.co", "Gabe2@m.co", "Gabe3@m.co", "Gabe4@m.co")); + expected.add(expected1); + expected.add(expected2); + expected.add(expected3); + expected.add(expected4); + + assertEqualsIgnoreOrdering(expected, solution1.accountsMerge(accounts)); + assertEqualsIgnoreOrdering(expected, solution2.accountsMerge(accounts)); + } + + @Test + public void test3() throws Exception { + accounts = new ArrayList<>(); + List account1 = + new ArrayList<>(Arrays.asList("David", "David0@m.co", "David1@m.co")); + List account2 = + new ArrayList<>(Arrays.asList("David", "David3@m.co", "David4@m.co")); + List account3 = + new ArrayList<>(Arrays.asList("David", "David4@m.co", "David5@m.co")); + List account4 = + new ArrayList<>(Arrays.asList("David", "David2@m.co", "David3@m.co")); + List account5 = + new ArrayList<>(Arrays.asList("David", "David1@m.co", "David2@m.co")); + accounts.add(account1); + accounts.add(account2); + accounts.add(account3); + accounts.add(account4); + accounts.add(account5); + + expected = new ArrayList<>(); + List expected1 = + new ArrayList<>( + Arrays.asList( + "David", + "David0@m.co", + "David1@m.co", + "David2@m.co", + "David3@m.co", + "David4@m.co", + "David5@m.co")); + expected.add(expected1); + + assertEqualsIgnoreOrdering(expected, solution1.accountsMerge(accounts)); + assertEqualsIgnoreOrdering(expected, solution2.accountsMerge(accounts)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_722Test.java b/src/test/java/com/fishercoder/firstthousand/_722Test.java new file mode 100644 index 0000000000..2fa65f2681 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_722Test.java @@ -0,0 +1,94 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._722; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _722Test { + private _722.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _722.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("int main()", "{ ", " ", "int a, b, c;", "a = b + c;", "}"), + solution1.removeComments( + new String[] { + "/*Test program */", + "int main()", + "{ ", + " // variable declaration ", + "int a, b, c;", + "/* This is a test", + " multiline ", + " comment for ", + " testing */", + "a = b + c;", + "}" + })); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList("ab"), + solution1.removeComments(new String[] {"a/*comment", "line", "more_comment*/b"})); + } + + @Test + public void test3() { + assertEquals( + Arrays.asList("struct Node{", " ", " int size;", " int val;", "};"), + solution1.removeComments( + new String[] { + "struct Node{", + " /*/ declare members;/**/", + " int size;", + " /**/int val;", + "};" + })); + } + + @Test + public void test4() { + assertEquals( + Arrays.asList("main() {", " double s = 33;", " cout << s;", "}"), + solution1.removeComments( + new String[] { + "main() {", + "/* here is commments", + " // still comments */", + " double s = 33;", + " cout << s;", + "}" + })); + } + + @Test + public void test5() { + assertEquals( + Arrays.asList("void func(int k) {", " k = k*2/4;", " k = k/2;*/", "}"), + solution1.removeComments( + new String[] { + "void func(int k) {", + "// this function does nothing /*", + " k = k*2/4;", + " k = k/2;*/", + "}" + })); + } + + @Test + public void test6() { + assertEquals( + Arrays.asList("a", "blank", "df"), + solution1.removeComments(new String[] {"a//*b/*/c", "blank", "d/*/e/*/f"})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_723Test.java b/src/test/java/com/fishercoder/firstthousand/_723Test.java new file mode 100644 index 0000000000..7d7e66ae55 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_723Test.java @@ -0,0 +1,56 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._723; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _723Test { + private _723.Solution1 solution1; + private static int[][] board; + private static int[][] expected; + + @BeforeEach + public void setup() { + solution1 = new _723.Solution1(); + } + + @Test + public void test1() { + board = + new int[][] { + {110, 5, 112, 113, 114}, + {210, 211, 5, 213, 214}, + {310, 311, 3, 313, 314}, + {410, 411, 412, 5, 414}, + {5, 1, 512, 3, 3}, + {610, 4, 1, 613, 614}, + {710, 1, 2, 713, 714}, + {810, 1, 2, 1, 1}, + {1, 1, 2, 2, 2}, + {4, 1, 4, 4, 1014}, + }; + + expected = + new int[][] { + {0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0}, + {110, 0, 0, 0, 114}, + {210, 0, 0, 0, 214}, + {310, 0, 0, 113, 314}, + {410, 0, 0, 213, 414}, + {610, 211, 112, 313, 614}, + {710, 311, 412, 613, 714}, + {810, 411, 512, 713, 1014} + }; + assert2dArrayEquals(expected, solution1.candyCrush(board)); + } + + private void assert2dArrayEquals(int[][] expected, int[][] actual) { + for (int i = 0; i < expected.length; i++) { + assertArrayEquals(expected[i], actual[i]); + } + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_724Test.java b/src/test/java/com/fishercoder/firstthousand/_724Test.java new file mode 100644 index 0000000000..7439323364 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_724Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._724; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _724Test { + private _724.Solution1 solution1; + private _724.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _724.Solution1(); + solution2 = new _724.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 7, 3, 6, 5, 6}; + assertEquals(3, solution1.pivotIndex(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2, 3}; + assertEquals(-1, solution1.pivotIndex(nums)); + } + + @Test + public void test3() { + nums = new int[] {-1, -1, -1, 0, 1, 1}; + assertEquals(0, solution1.pivotIndex(nums)); + } + + @Test + public void test4() { + nums = new int[] {-1, -1, 0, 1, 1, 0}; + assertEquals(5, solution1.pivotIndex(nums)); + } + + @Test + public void test5() { + nums = new int[] {1, 7, 3, 6, 5, 6}; + assertEquals(3, solution2.pivotIndex(nums)); + } + + @Test + public void test6() { + nums = new int[] {1, 2, 3}; + assertEquals(-1, solution2.pivotIndex(nums)); + } + + @Test + public void test7() { + nums = new int[] {-1, -1, -1, 0, 1, 1}; + assertEquals(0, solution2.pivotIndex(nums)); + } + + @Test + public void test8() { + nums = new int[] {-1, -1, 0, 1, 1, 0}; + assertEquals(5, solution2.pivotIndex(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_725Test.java b/src/test/java/com/fishercoder/firstthousand/_725Test.java new file mode 100644 index 0000000000..cdee40f75e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_725Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._725; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _725Test { + private _725.Solution1 solution1; + private _725.Solution2 solution2; + private static ListNode root; + private static int k; + private static ListNode[] actual; + + @BeforeEach + public void setup() { + solution1 = new _725.Solution1(); + solution2 = new _725.Solution2(); + } + + @Test + public void test1() { + root = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3}); + k = 5; + actual = solution1.splitListToParts(root, k); + for (ListNode head : actual) { + LinkedListUtils.printList(head); + } + } + + @Test + public void test2() { + root = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + k = 3; + actual = solution1.splitListToParts(root, k); + for (ListNode head : actual) { + LinkedListUtils.printList(head); + } + } + + @Test + public void test3() { + root = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3}); + k = 5; + actual = solution2.splitListToParts(root, k); + for (ListNode head : actual) { + LinkedListUtils.printList(head); + } + } + + @Test + public void test4() { + root = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + k = 3; + actual = solution2.splitListToParts(root, k); + for (ListNode head : actual) { + LinkedListUtils.printList(head); + } + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_726Test.java b/src/test/java/com/fishercoder/firstthousand/_726Test.java new file mode 100644 index 0000000000..03918ac893 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_726Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._726; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _726Test { + private _726.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _726.Solution1(); + } + + @Test + public void test1() { + assertEquals("H2O", solution1.countOfAtoms("H2O")); + } + + @Test + public void test2() { + assertEquals("H2MgO2", solution1.countOfAtoms("Mg(OH)2")); + } + + @Test + public void test3() { + assertEquals("K4N2O14S4", solution1.countOfAtoms("K4(ON(SO3)2)2")); + } + + @Test + public void test4() { + assertEquals("H2MgNO", solution1.countOfAtoms("Mg(H2O)N")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_727Test.java b/src/test/java/com/fishercoder/firstthousand/_727Test.java new file mode 100644 index 0000000000..c2ea867eae --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_727Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._727; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _727Test { + private _727.Solution1 solution1; + private _727.Solution2 solution2; + private static String S; + private static String T; + + @BeforeEach + public void setup() { + solution1 = new _727.Solution1(); + solution2 = new _727.Solution2(); + } + + @Test + public void test1() { + S = "abcdebdde"; + T = "bde"; + assertEquals("bcde", solution1.minWindow(S, T)); + assertEquals("bcde", solution2.minWindow(S, T)); + } + + @Test + public void test2() { + String S = "jmeqksfrsdcmsiwvaovztaqenprpvnbstl"; + String T = "l"; + assertEquals("l", solution1.minWindow(S, T)); + assertEquals("l", solution2.minWindow(S, T)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_728Test.java b/src/test/java/com/fishercoder/firstthousand/_728Test.java new file mode 100644 index 0000000000..8a7b729e40 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_728Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._728; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _728Test { + private _728.Solution1 solution1; + private static List expected; + + @BeforeEach + public void setup() { + solution1 = new _728.Solution1(); + } + + @Test + public void test1() { + expected = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22); + assertEquals(expected, solution1.selfDividingNumbers(1, 22)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_72Test.java b/src/test/java/com/fishercoder/firstthousand/_72Test.java new file mode 100644 index 0000000000..b3de516243 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_72Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._72; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _72Test { + private _72.Solution1 solution1; + private _72.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _72.Solution1(); + solution2 = new _72.Solution2(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minDistance("Ada", "Adam")); + } + + @Test + public void test2() { + assertEquals(5, solution1.minDistance("Ashmi", "Chheda")); + } + + @Test + public void test3() { + assertEquals(1, solution2.minDistance("Ada", "Adam")); + } + + @Test + public void test4() { + assertEquals(5, solution2.minDistance("Ashmi", "Chheda")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_733Test.java b/src/test/java/com/fishercoder/firstthousand/_733Test.java new file mode 100644 index 0000000000..147f212503 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_733Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._733; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _733Test { + private _733.Solution1 solution1; + private static int[][] image; + private static int[][] result; + + @BeforeEach + public void setup() { + solution1 = new _733.Solution1(); + } + + @Test + public void test1() { + image = + new int[][] { + {1, 1, 1}, + {1, 1, 0}, + {1, 0, 1} + }; + result = solution1.floodFill(image, 1, 1, 2); + CommonUtils.print2DIntArray(result); + } + + @Test + public void test2() { + image = + new int[][] { + {0, 0, 0}, + {0, 0, 0} + }; + result = solution1.floodFill(image, 0, 0, 2); + CommonUtils.print2DIntArray(result); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_734Test.java b/src/test/java/com/fishercoder/firstthousand/_734Test.java new file mode 100644 index 0000000000..8cf94d22c1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_734Test.java @@ -0,0 +1,79 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._734; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _734Test { + private _734.Solution1 solution1; + private static String[] words1; + private static String[] words2; + private static String[][] pairs; + + @BeforeEach + public void setup() { + solution1 = new _734.Solution1(); + } + + @Test + public void test1() { + words1 = new String[] {"great", "acting", "skills"}; + words2 = new String[] {"fine", "drama", "talent"}; + pairs = + new String[][] { + {"great", "fine"}, + {"acting", "drama"}, + {"skills", "talent"} + }; + assertEquals(true, solution1.areSentencesSimilar(words1, words2, pairs)); + } + + @Test + public void test2() { + String[] words1 = new String[] {"one", "excellent", "meal"}; + String[] words2 = new String[] {"one", "good", "dinner"}; + String[][] pairs = + new String[][] { + {"great", "good"}, + {"extraordinary", "good"}, + {"well", "good"}, + {"wonderful", "good"}, + {"excellent", "good"}, + {"dinner", "meal"}, + {"fine", "good"}, + {"nice", "good"}, + {"any", "one"}, + {"unique", "one"}, + {"some", "one"}, + {"the", "one"}, + {"an", "one"}, + {"single", "one"}, + {"a", "one"}, + {"keep", "own"}, + {"truck", "car"}, + {"super", "very"}, + {"really", "very"}, + {"actually", "very"}, + {"extremely", "very"}, + {"have", "own"}, + {"possess", "own"}, + {"lunch", "meal"}, + {"super", "meal"}, + {"food", "meal"}, + {"breakfast", "meal"}, + {"brunch", "meal"}, + {"wagon", "car"}, + {"automobile", "car"}, + {"auto", "car"}, + {"fruits", "meal"}, + {"vehicle", "car"}, + {"entertain", "have"}, + {"drink", "have"}, + {"eat", "have"}, + {"take", "have"}, + }; + assertEquals(true, solution1.areSentencesSimilar(words1, words2, pairs)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_735Test.java b/src/test/java/com/fishercoder/firstthousand/_735Test.java new file mode 100644 index 0000000000..2b86783d79 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_735Test.java @@ -0,0 +1,91 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._735; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _735Test { + private _735.Solution1 solution1; + private _735.Solution2 solution2; + private _735.Solution3 solution3; + private _735.Solution4 solution4; + private static int[] asteroids; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _735.Solution1(); + solution2 = new _735.Solution2(); + solution3 = new _735.Solution3(); + solution4 = new _735.Solution4(); + } + + @Test + public void test1() { + asteroids = new int[] {5, 10, -5}; + expected = new int[] {5, 10}; + assertArrayEquals(expected, solution1.asteroidCollision(asteroids)); + assertArrayEquals(expected, solution2.asteroidCollision(asteroids)); + assertArrayEquals(expected, solution3.asteroidCollision(asteroids)); + assertArrayEquals(expected, solution4.asteroidCollision(asteroids)); + } + + @Test + public void test2() { + asteroids = new int[] {8, -8}; + asteroids = solution1.asteroidCollision(asteroids); + expected = new int[] {}; + assertArrayEquals(expected, asteroids); + assertArrayEquals(expected, solution4.asteroidCollision(asteroids)); + } + + @Test + public void test3() { + asteroids = new int[] {10, 2, -5}; + asteroids = solution1.asteroidCollision(asteroids); + expected = new int[] {10}; + assertArrayEquals(expected, asteroids); + assertArrayEquals(expected, solution4.asteroidCollision(asteroids)); + } + + @Test + public void test4() { + asteroids = new int[] {-2, 1, 2, -2}; + asteroids = solution1.asteroidCollision(asteroids); + expected = new int[] {-2, 1}; + assertArrayEquals(expected, asteroids); + assertArrayEquals(expected, solution4.asteroidCollision(asteroids)); + } + + @Test + public void test5() { + asteroids = new int[] {-2, -2, -2, 1}; + asteroids = solution1.asteroidCollision(asteroids); + expected = new int[] {-2, -2, -2, 1}; + assertArrayEquals(expected, asteroids); + assertArrayEquals(expected, solution4.asteroidCollision(asteroids)); + } + + @Test + public void test6() { + asteroids = new int[] {-2, -1, 1, 2}; + asteroids = solution1.asteroidCollision(asteroids); + assertArrayEquals(new int[] {-2, -1, 1, 2}, asteroids); + } + + @Test + public void test7() { + asteroids = new int[] {-2, -2, 1, -2}; + asteroids = solution1.asteroidCollision(asteroids); + assertArrayEquals(new int[] {-2, -2, -2}, asteroids); + } + + @Test + public void test8() { + asteroids = new int[] {-4, -1, 10, 2, -1, 8, -9, -6, 5, 2}; + asteroids = solution1.asteroidCollision(asteroids); + assertArrayEquals(new int[] {-4, -1, 10, 5, 2}, asteroids); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_737Test.java b/src/test/java/com/fishercoder/firstthousand/_737Test.java new file mode 100644 index 0000000000..0e7d93d4af --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_737Test.java @@ -0,0 +1,562 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._737; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _737Test { + private _737.Solution1 solution1; + private static String[] words1; + private static String[] words2; + private static String[][] pairs; + + @BeforeEach + public void setup() { + solution1 = new _737.Solution1(); + } + + @Test + public void test1() { + words1 = new String[] {"great", "acting", "skills"}; + words2 = new String[] {"fine", "drama", "talent"}; + pairs = + new String[][] { + {"great", "fine"}, + {"acting", "drama"}, + {"skills", "talent"} + }; + assertEquals(true, solution1.areSentencesSimilarTwo(words1, words2, pairs)); + } + + @Test + public void test2() { + words1 = new String[] {"great", "acting", "skills"}; + words2 = new String[] {"fine", "drama", "talent"}; + pairs = + new String[][] { + {"great", "good"}, + {"fine", "good"}, + {"drama", "acting"}, + {"skills", "talent"} + }; + assertEquals(true, solution1.areSentencesSimilarTwo(words1, words2, pairs)); + } + + @Test + public void test3() { + String[] words1 = new String[] {"one", "excellent", "meal"}; + String[] words2 = new String[] {"one", "good", "dinner"}; + String[][] pairs = + new String[][] { + {"great", "good"}, + {"extraordinary", "good"}, + {"well", "good"}, + {"wonderful", "good"}, + {"excellent", "good"}, + {"dinner", "meal"}, + {"fine", "good"}, + {"nice", "good"}, + {"any", "one"}, + {"unique", "one"}, + {"some", "one"}, + {"the", "one"}, + {"an", "one"}, + {"single", "one"}, + {"a", "one"}, + {"keep", "own"}, + {"truck", "car"}, + {"super", "very"}, + {"really", "very"}, + {"actually", "very"}, + {"extremely", "very"}, + {"have", "own"}, + {"possess", "own"}, + {"lunch", "meal"}, + {"super", "meal"}, + {"food", "meal"}, + {"breakfast", "meal"}, + {"brunch", "meal"}, + {"wagon", "car"}, + {"automobile", "car"}, + {"auto", "car"}, + {"fruits", "meal"}, + {"vehicle", "car"}, + {"entertain", "have"}, + {"drink", "have"}, + {"eat", "have"}, + {"take", "have"}, + }; + assertEquals(true, solution1.areSentencesSimilarTwo(words1, words2, pairs)); + } + + @Test + public void test4() { + words1 = + new String[] { + "jrocadcojmybpxmuj", + "livgsrfvgtovcurzq", + "mnrdscqkycodx", + "wgcjlntupylayse", + "tglnshmqlmkqqfbpf", + "uzlxmaoro", + "narvuaqmmkqhd", + "xozoyaqxtbustrymo", + "jrocadcojmybpxmuj", + "ainlwrwabqcwq", + "qnjidlmwmxxjgntez", + "bbchthovla", + "vaufbmwdrupcxpg", + "zwwgloilddclufwze", + "tyxrlpmcy", + "wtjtdrlm", + "edurtetzseifez", + "yzxogkunvohdmro", + "livgsrfvgtovcurzq", + "wmpvjvzljhnaxvp", + "rqbswlkw", + "umlzibkkpsyvpdol", + "jkcmceinlyhi", + "wlvmfxbleuot", + "aeloeauxmc", + "ooyllkxg", + "wlvmfxbleuot", + "cuewcvuy", + "vaufbmwdrupcxpg", + "bbchthovla", + "arigdtezmyz", + "yzxogkunvohdmro", + "wrszraxxdum", + "dhmiuqhqlsprxy", + "xpmxtfyvjrnujyxjh", + "bfxbncez", + "cjjkmybleu", + "mnrdscqkycodx", + "mzfpofjn", + "livgsrfvgtovcurzq", + "shfzcyboj", + "xozoyaqxtbustrymo", + "xozoyaqxtbustrymo", + "orlzzpytpzazxr", + "filnwifbukdqijgr", + "fllqjtnxwmfoou", + "mkmawbogphdttd", + "rthpxoxyyiy", + "dkhfozltuckwog", + "wmpvjvzljhnaxvp", + "dhmiuqhqlsprxy", + "yltljjairlkrmdq", + "cuewcvuy", + "subzoyxjkfiwmfb", + "mzvbgcizeeth", + "narvuaqmmkqhd", + "tglnshmqlmkqqfbpf", + "rpesfkhfjucj", + "xrgfejybbkezgor", + "vaufbmwdrupcxpg", + "czlgbqzffodsoxng", + "suvvqdiceuogcmv", + "fllqjtnxwmfoou", + "yltljjairlkrmdq", + "bubwouozgs", + "mnrdscqkycodx", + "rqbswlkw", + "ooyllkxg", + "livgsrfvgtovcurzq", + "rthpxoxyyiy", + "pyzcbpjhntpefbq", + "wtjtdrlm", + "rztcppnmud", + "inuzvkgolupxelcal", + "pdxsxjop", + "wmpvjvzljhnaxvp", + "xydwvemqvtgvzl", + "hqpnoczciajvkbdy", + "rvihrzzkt", + "jzquemjzpvfbka", + "gkqrglav", + "qyaxqaqxiwr", + "mzvbgcizeeth", + "umlzibkkpsyvpdol", + "vaufbmwdrupcxpg", + "ooyllkxg", + "arigdtezmyz", + "bubwouozgs", + "wtjtdrlm", + "xozoyaqxtbustrymo", + "jrocadcojmybpxmuj", + "rnlryins", + "fllqjtnxwmfoou", + "livgsrfvgtovcurzq", + "czlgbqzffodsoxng", + "hlcsiukaroscfg", + "bfxbncez", + "ainlwrwabqcwq", + "vaufbmwdrupcxpg", + "vaufbmwdrupcxpg" + }; + words2 = + new String[] { + "jrocadcojmybpxmuj", + "livgsrfvgtovcurzq", + "mnrdscqkycodx", + "wgcjlntupylayse", + "bbchthovla", + "bfxbncez", + "ztisufueqzequ", + "yutahdply", + "suvvqdiceuogcmv", + "ainlwrwabqcwq", + "fquzrlhdsnuwhhu", + "tglnshmqlmkqqfbpf", + "vaufbmwdrupcxpg", + "zwwgloilddclufwze", + "livgsrfvgtovcurzq", + "wtjtdrlm", + "edurtetzseifez", + "ecqfdkebnamkfglk", + "livgsrfvgtovcurzq", + "wmpvjvzljhnaxvp", + "ryubcgbzmxc", + "pzlmeboecybxmetz", + "hqpnoczciajvkbdy", + "xpmxtfyvjrnujyxjh", + "zwwgloilddclufwze", + "khcyhttaaxp", + "wlvmfxbleuot", + "jzquemjzpvfbka", + "vaufbmwdrupcxpg", + "tglnshmqlmkqqfbpf", + "mzvbgcizeeth", + "cjjkmybleu", + "orlzzpytpzazxr", + "dhmiuqhqlsprxy", + "mzfpofjn", + "bfxbncez", + "inuzvkgolupxelcal", + "inhzsspqltvl", + "wlvmfxbleuot", + "livgsrfvgtovcurzq", + "orlzzpytpzazxr", + "yutahdply", + "yutahdply", + "orlzzpytpzazxr", + "gdziaihbagl", + "yltljjairlkrmdq", + "mkmawbogphdttd", + "aotjpvanljxe", + "aeloeauxmc", + "wmpvjvzljhnaxvp", + "dhmiuqhqlsprxy", + "yltljjairlkrmdq", + "dnaaehrekqms", + "khcyhttaaxp", + "mzvbgcizeeth", + "narvuaqmmkqhd", + "rvihrzzkt", + "bfufqsusp", + "xrgfejybbkezgor", + "vaufbmwdrupcxpg", + "czlgbqzffodsoxng", + "jrocadcojmybpxmuj", + "yltljjairlkrmdq", + "yltljjairlkrmdq", + "bubwouozgs", + "inhzsspqltvl", + "bsybvehdny", + "subzoyxjkfiwmfb", + "livgsrfvgtovcurzq", + "stkglpqdjzxmnlito", + "evepphnzuw", + "xrgfejybbkezgor", + "rztcppnmud", + "cjjkmybleu", + "qyaxqaqxiwr", + "ibwfxvxswjbecab", + "xydwvemqvtgvzl", + "hqpnoczciajvkbdy", + "tglnshmqlmkqqfbpf", + "dnaaehrekqms", + "gkqrglav", + "bfxbncez", + "qvwvgzxqihvk", + "umlzibkkpsyvpdol", + "vaufbmwdrupcxpg", + "khcyhttaaxp", + "arigdtezmyz", + "bubwouozgs", + "fllqjtnxwmfoou", + "xozoyaqxtbustrymo", + "jrocadcojmybpxmuj", + "rnlryins", + "wtjtdrlm", + "livgsrfvgtovcurzq", + "gkqrglav", + "orileazg", + "uzlxmaoro", + "ainlwrwabqcwq", + "vaufbmwdrupcxpg", + "vaufbmwdrupcxpg" + }; + pairs = + new String[][] { + {"yutahdply", "yutahdply"}, + {"xozoyaqxtbustrymo", "xozoyaqxtbustrymo"}, + {"xozoyaqxtbustrymo", "xozoyaqxtbustrymo"}, + {"yutahdply", "yutahdply"}, + {"shfzcyboj", "orlzzpytpzazxr"}, + {"suvvqdiceuogcmv", "llrzqdnoxbscnkqy"}, + {"jkcmceinlyhi", "hqpnoczciajvkbdy"}, + {"hqpnoczciajvkbdy", "hqpnoczciajvkbdy"}, + {"rztcppnmud", "vdjccijgqk"}, + {"vdjccijgqk", "vdjccijgqk"}, + {"jkcmceinlyhi", "hqpnoczciajvkbdy"}, + {"rztcppnmud", "rztcppnmud"}, + {"vdjccijgqk", "vdjccijgqk"}, + {"hqpnoczciajvkbdy", "hqpnoczciajvkbdy"}, + {"umlzibkkpsyvpdol", "ryubcgbzmxc"}, + {"ryubcgbzmxc", "ryubcgbzmxc"}, + {"pzlmeboecybxmetz", "bsybvehdny"}, + {"rqbswlkw", "bsybvehdny"}, + {"bsybvehdny", "bsybvehdny"}, + {"umlzibkkpsyvpdol", "umlzibkkpsyvpdol"}, + {"ryubcgbzmxc", "ryubcgbzmxc"}, + {"rqbswlkw", "rqbswlkw"}, + {"pzlmeboecybxmetz", "pzlmeboecybxmetz"}, + {"bsybvehdny", "bsybvehdny"}, + {"dkhfozltuckwog", "zwwgloilddclufwze"}, + {"zfmpxgrevxp", "pyzcbpjhntpefbq"}, + {"gkqrglav", "czlgbqzffodsoxng"}, + {"tyxrlpmcy", "livgsrfvgtovcurzq"}, + {"shsgrqol", "cufxsgbpjgqvk"}, + {"rphnhtvnihyfkrgv", "yykdqtkkdacpbwtbq"}, + {"dhmiuqhqlsprxy", "ztisufueqzequ"}, + {"ibwfxvxswjbecab", "xydwvemqvtgvzl"}, + {"mkmawbogphdttd", "ainlwrwabqcwq"}, + {"pdxsxjop", "uzlxmaoro"}, + {"ooyllkxg", "khcyhttaaxp"}, + {"jrocadcojmybpxmuj", "jrocadcojmybpxmuj"}, + {"lkopigreodypvude", "lkopigreodypvude"}, + {"hqpnoczciajvkbdy", "rztcppnmud"}, + {"llrzqdnoxbscnkqy", "jrocadcojmybpxmuj"}, + {"cuewcvuy", "jzquemjzpvfbka"}, + {"wlvmfxbleuot", "bfufqsusp"}, + {"bfufqsusp", "bfufqsusp"}, + {"xpmxtfyvjrnujyxjh", "rpesfkhfjucj"}, + {"mzfpofjn", "rpesfkhfjucj"}, + {"rpesfkhfjucj", "rpesfkhfjucj"}, + {"xpmxtfyvjrnujyxjh", "mzfpofjn"}, + {"wlvmfxbleuot", "bfufqsusp"}, + {"rpesfkhfjucj", "xpmxtfyvjrnujyxjh"}, + {"cuewcvuy", "dnaaehrekqms"}, + {"dnaaehrekqms", "dnaaehrekqms"}, + {"rpesfkhfjucj", "wlvmfxbleuot"}, + {"lkopigreodypvude", "mzvbgcizeeth"}, + {"tglnshmqlmkqqfbpf", "bbchthovla"}, + {"orileazg", "filnwifbukdqijgr"}, + {"yltljjairlkrmdq", "xrgfejybbkezgor"}, + {"inuzvkgolupxelcal", "hgxrhkanzvzmsjpzl"}, + {"jzquemjzpvfbka", "iziepzqne"}, + {"muaskefecskjghzn", "iziepzqne"}, + {"hhrllhedyy", "wzflhbbgtc"}, + {"cemnayjhlnj", "hgtyntdmrgjh"}, + {"iziepzqne", "iziepzqne"}, + {"cuewcvuy", "dnaaehrekqms"}, + {"muaskefecskjghzn", "iziepzqne"}, + {"jzquemjzpvfbka", "muaskefecskjghzn"}, + {"dnaaehrekqms", "dnaaehrekqms"}, + {"jrocadcojmybpxmuj", "jrocadcojmybpxmuj"}, + {"llrzqdnoxbscnkqy", "suvvqdiceuogcmv"}, + {"suvvqdiceuogcmv", "suvvqdiceuogcmv"}, + {"bbchthovla", "bbchthovla"}, + {"rvihrzzkt", "tglnshmqlmkqqfbpf"}, + {"filnwifbukdqijgr", "pkirimjwvyxs"}, + {"tglnshmqlmkqqfbpf", "tglnshmqlmkqqfbpf"}, + {"rvihrzzkt", "tglnshmqlmkqqfbpf"}, + {"bbchthovla", "bbchthovla"}, + {"tglnshmqlmkqqfbpf", "tglnshmqlmkqqfbpf"}, + {"hjogoueazw", "lkopigreodypvude"}, + {"lkopigreodypvude", "lkopigreodypvude"}, + {"mzvbgcizeeth", "arigdtezmyz"}, + {"qvwvgzxqihvk", "arigdtezmyz"}, + {"arigdtezmyz", "arigdtezmyz"}, + {"mzvbgcizeeth", "arigdtezmyz"}, + {"qvwvgzxqihvk", "qvwvgzxqihvk"}, + {"hjogoueazw", "hjogoueazw"}, + {"subzoyxjkfiwmfb", "khcyhttaaxp"}, + {"subzoyxjkfiwmfb", "subzoyxjkfiwmfb"}, + {"khcyhttaaxp", "subzoyxjkfiwmfb"}, + {"ooyllkxg", "ooyllkxg"}, + {"orlzzpytpzazxr", "orlzzpytpzazxr"}, + {"oufzmjgplt", "oufzmjgplt"}, + {"shfzcyboj", "shfzcyboj"}, + {"oufzmjgplt", "oufzmjgplt"}, + {"orlzzpytpzazxr", "oufzmjgplt"}, + {"wrszraxxdum", "wrszraxxdum"}, + {"wrszraxxdum", "wrszraxxdum"}, + {"shfzcyboj", "wrszraxxdum"}, + {"yutahdply", "xozoyaqxtbustrymo"}, + {"umlzibkkpsyvpdol", "pzlmeboecybxmetz"}, + {"hgxrhkanzvzmsjpzl", "gwkkpxuvgp"}, + {"xrgfejybbkezgor", "wtjtdrlm"}, + {"wtjtdrlm", "wtjtdrlm"}, + {"yltljjairlkrmdq", "fllqjtnxwmfoou"}, + {"xrgfejybbkezgor", "wtjtdrlm"}, + {"filnwifbukdqijgr", "pkirimjwvyxs"}, + {"pkirimjwvyxs", "pkirimjwvyxs"}, + {"gdziaihbagl", "orileazg"}, + {"orileazg", "orileazg"}, + {"gdziaihbagl", "orileazg"}, + {"hlcsiukaroscfg", "orileazg"}, + {"hlcsiukaroscfg", "hlcsiukaroscfg"}, + {"gdziaihbagl", "gdziaihbagl"}, + {"ainlwrwabqcwq", "ainlwrwabqcwq"}, + {"uzlxmaoro", "bfxbncez"}, + {"qyaxqaqxiwr", "qyaxqaqxiwr"}, + {"pdxsxjop", "pdxsxjop"}, + {"pdxsxjop", "pdxsxjop"}, + {"subzoyxjkfiwmfb", "subzoyxjkfiwmfb"}, + {"uzlxmaoro", "bfxbncez"}, + {"bfxbncez", "bfxbncez"}, + {"qyaxqaqxiwr", "pdxsxjop"}, + {"ooyllkxg", "ooyllkxg"}, + {"hgxrhkanzvzmsjpzl", "ecqfdkebnamkfglk"}, + {"gwkkpxuvgp", "ecqfdkebnamkfglk"}, + {"ecqfdkebnamkfglk", "ecqfdkebnamkfglk"}, + {"yzxogkunvohdmro", "yzxogkunvohdmro"}, + {"inuzvkgolupxelcal", "yzxogkunvohdmro"}, + {"yzxogkunvohdmro", "yzxogkunvohdmro"}, + {"cjjkmybleu", "yzxogkunvohdmro"}, + {"inuzvkgolupxelcal", "inuzvkgolupxelcal"}, + {"ecqfdkebnamkfglk", "gwkkpxuvgp"}, + {"dwojnswr", "dkhfozltuckwog"}, + {"yltljjairlkrmdq", "fllqjtnxwmfoou"}, + {"fllqjtnxwmfoou", "fllqjtnxwmfoou"}, + {"wzflhbbgtc", "zzdvolqtndzfjvqqr"}, + {"dkhfozltuckwog", "dkhfozltuckwog"}, + {"zfmpxgrevxp", "stkglpqdjzxmnlito"}, + {"wzflhbbgtc", "wzflhbbgtc"}, + {"cjjkmybleu", "cjjkmybleu"}, + {"wgcjlntupylayse", "wgcjlntupylayse"}, + {"vyrvelteblnqaabc", "vyrvelteblnqaabc"}, + {"bvxiilsnsarhsyl", "zzdvolqtndzfjvqqr"}, + {"stkglpqdjzxmnlito", "stkglpqdjzxmnlito"}, + {"cemnayjhlnj", "cemnayjhlnj"}, + {"cemnayjhlnj", "cemnayjhlnj"}, + {"hgtyntdmrgjh", "hgtyntdmrgjh"}, + {"rnlryins", "vyrvelteblnqaabc"}, + {"hhrllhedyy", "vyrvelteblnqaabc"}, + {"rnlryins", "rnlryins"}, + {"fquzrlhdsnuwhhu", "zzdvolqtndzfjvqqr"}, + {"zzdvolqtndzfjvqqr", "bvxiilsnsarhsyl"}, + {"wmpvjvzljhnaxvp", "wmpvjvzljhnaxvp"}, + {"qnjidlmwmxxjgntez", "vyrvelteblnqaabc"}, + {"fquzrlhdsnuwhhu", "zzdvolqtndzfjvqqr"}, + {"zzdvolqtndzfjvqqr", "zzdvolqtndzfjvqqr"}, + {"edurtetzseifez", "rphnhtvnihyfkrgv"}, + {"wgcjlntupylayse", "wgcjlntupylayse"}, + {"zwwgloilddclufwze", "aeloeauxmc"}, + {"rphnhtvnihyfkrgv", "rphnhtvnihyfkrgv"}, + {"aeloeauxmc", "aeloeauxmc"}, + {"hgtyntdmrgjh", "wgcjlntupylayse"}, + {"rphnhtvnihyfkrgv", "rphnhtvnihyfkrgv"}, + {"cufxsgbpjgqvk", "cufxsgbpjgqvk"}, + {"mnrdscqkycodx", "shsgrqol"}, + {"qnjidlmwmxxjgntez", "hhrllhedyy"}, + {"shsgrqol", "shsgrqol"}, + {"vyrvelteblnqaabc", "qnjidlmwmxxjgntez"}, + {"zwwgloilddclufwze", "aeloeauxmc"}, + {"evepphnzuw", "rthpxoxyyiy"}, + {"rthpxoxyyiy", "rthpxoxyyiy"}, + {"aotjpvanljxe", "aotjpvanljxe"}, + {"aotjpvanljxe", "stkglpqdjzxmnlito"}, + {"dkhfozltuckwog", "dwojnswr"}, + {"rthpxoxyyiy", "pyzcbpjhntpefbq"}, + {"evepphnzuw", "evepphnzuw"}, + {"aeloeauxmc", "aeloeauxmc"}, + {"zfmpxgrevxp", "aotjpvanljxe"}, + {"stkglpqdjzxmnlito", "aotjpvanljxe"}, + {"bubwouozgs", "mkmawbogphdttd"}, + {"pyzcbpjhntpefbq", "rthpxoxyyiy"}, + {"gkqrglav", "gkqrglav"}, + {"czlgbqzffodsoxng", "czlgbqzffodsoxng"}, + {"yykdqtkkdacpbwtbq", "yykdqtkkdacpbwtbq"}, + {"dhmiuqhqlsprxy", "dhmiuqhqlsprxy"}, + {"ztisufueqzequ", "ztisufueqzequ"}, + {"ztisufueqzequ", "narvuaqmmkqhd"}, + {"narvuaqmmkqhd", "narvuaqmmkqhd"}, + {"narvuaqmmkqhd", "narvuaqmmkqhd"}, + {"ibwfxvxswjbecab", "ibwfxvxswjbecab"}, + {"dhmiuqhqlsprxy", "dhmiuqhqlsprxy"}, + {"xydwvemqvtgvzl", "wmpvjvzljhnaxvp"}, + {"wmpvjvzljhnaxvp", "wmpvjvzljhnaxvp"}, + {"xydwvemqvtgvzl", "wmpvjvzljhnaxvp"}, + {"ibwfxvxswjbecab", "ibwfxvxswjbecab"}, + {"bubwouozgs", "mkmawbogphdttd"}, + {"mkmawbogphdttd", "mkmawbogphdttd"}, + {"ainlwrwabqcwq", "ainlwrwabqcwq"}, + {"mkmawbogphdttd", "mkmawbogphdttd"}, + {"edurtetzseifez", "edurtetzseifez"}, + {"inhzsspqltvl", "inhzsspqltvl"}, + {"cufxsgbpjgqvk", "inhzsspqltvl"}, + {"yykdqtkkdacpbwtbq", "yykdqtkkdacpbwtbq"}, + {"mnrdscqkycodx", "mnrdscqkycodx"}, + {"shsgrqol", "shsgrqol"}, + {"cufxsgbpjgqvk", "inhzsspqltvl"}, + {"livgsrfvgtovcurzq", "livgsrfvgtovcurzq"}, + {"tyxrlpmcy", "tyxrlpmcy"}, + {"livgsrfvgtovcurzq", "livgsrfvgtovcurzq"}, + {"tyxrlpmcy", "tyxrlpmcy"}, + {"czlgbqzffodsoxng", "czlgbqzffodsoxng"}, + {"gkqrglav", "gkqrglav"}, + }; + assertEquals(true, solution1.areSentencesSimilarTwo(words1, words2, pairs)); + } + + @Test + public void test5() { + words1 = new String[] {"a", "very", "delicious", "meal"}; + words2 = new String[] {"one", "really", "good", "dinner"}; + pairs = + new String[][] { + {"great", "good"}, + {"extraordinary", "good"}, + {"well", "good"}, + {"wonderful", "good"}, + {"excellent", "good"}, + {"fine", "good"}, + {"nice", "good"}, + {"any", "one"}, + {"some", "one"}, + {"unique", "one"}, + {"the", "one"}, + {"an", "one"}, + {"single", "one"}, + {"a", "one"}, + {"truck", "car"}, + {"wagon", "car"}, + {"automobile", "car"}, + {"auto", "car"}, + {"vehicle", "car"}, + {"entertain", "have"}, + {"drink", "have"}, + {"eat", "have"}, + {"take", "have"}, + {"fruits", "meal"}, + {"brunch", "meal"}, + {"breakfast", "meal"}, + {"food", "meal"}, + {"dinner", "meal"}, + {"super", "meal"}, + {"lunch", "meal"}, + {"possess", "own"}, + {"keep", "own"}, + {"have", "own"}, + {"extremely", "very"}, + {"really", "very"}, + {"super", "very"}, + }; + assertEquals(false, solution1.areSentencesSimilarTwo(words1, words2, pairs)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_738Test.java b/src/test/java/com/fishercoder/firstthousand/_738Test.java new file mode 100644 index 0000000000..e93a64cac9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_738Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._738; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _738Test { + private _738.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _738.Solution1(); + } + + @Test + public void test1() { + assertEquals(9, solution1.monotoneIncreasingDigits(10)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_739Test.java b/src/test/java/com/fishercoder/firstthousand/_739Test.java new file mode 100644 index 0000000000..27ff760e23 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_739Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._739; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _739Test { + private _739.Solution1 solution1; + private static int[] temperatures; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _739.Solution1(); + } + + @Test + public void test1() { + temperatures = new int[] {73, 74, 75, 71, 69, 72, 76, 73}; + expected = new int[] {1, 1, 4, 2, 1, 1, 0, 0}; + assertArrayEquals(expected, solution1.dailyTemperatures(temperatures)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_73Test.java b/src/test/java/com/fishercoder/firstthousand/_73Test.java new file mode 100644 index 0000000000..b25e0fad71 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_73Test.java @@ -0,0 +1,112 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._73; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _73Test { + private _73.Solution1 solution1; + private _73.Solution2 solution2; + private _73.Solution3 solution3; + private _73.Solution4 solution4; + private static int[][] matrix; + private static int[][] expected; + + @BeforeEach + public void setup() { + solution1 = new _73.Solution1(); + solution2 = new _73.Solution2(); + solution3 = new _73.Solution3(); + solution4 = new _73.Solution4(); + } + + @Test + public void test1() { + matrix = + new int[][] { + {0, 0, 0, 5}, + {4, 3, 1, 4}, + {0, 1, 1, 4}, + {1, 2, 1, 3}, + {0, 0, 1, 1} + }; + solution1.setZeroes(matrix); + expected = + new int[][] { + {0, 0, 0, 0}, + {0, 0, 0, 4}, + {0, 0, 0, 0}, + {0, 0, 0, 3}, + {0, 0, 0, 0} + }; + assertArrayEquals(expected, matrix); + } + + @Test + public void test2() { + matrix = + new int[][] { + {0, 0, 0, 5}, + {4, 3, 1, 4}, + {0, 1, 1, 4}, + {1, 2, 1, 3}, + {0, 0, 1, 1} + }; + solution2.setZeroes(matrix); + expected = + new int[][] { + {0, 0, 0, 0}, + {0, 0, 0, 4}, + {0, 0, 0, 0}, + {0, 0, 0, 3}, + {0, 0, 0, 0} + }; + assertArrayEquals(expected, matrix); + } + + @Test + public void test3() { + matrix = + new int[][] { + {0, 0, 0, 5}, + {4, 3, 1, 4}, + {0, 1, 1, 4}, + {1, 2, 1, 3}, + {0, 0, 1, 1} + }; + solution3.setZeroes(matrix); + expected = + new int[][] { + {0, 0, 0, 0}, + {0, 0, 0, 4}, + {0, 0, 0, 0}, + {0, 0, 0, 3}, + {0, 0, 0, 0} + }; + assertArrayEquals(expected, matrix); + } + + @Test + public void test4() { + matrix = + new int[][] { + {0, 0, 0, 5}, + {4, 3, 1, 4}, + {0, 1, 1, 4}, + {1, 2, 1, 3}, + {0, 0, 1, 1} + }; + solution4.setZeroes(matrix); + expected = + new int[][] { + {0, 0, 0, 0}, + {0, 0, 0, 4}, + {0, 0, 0, 0}, + {0, 0, 0, 3}, + {0, 0, 0, 0} + }; + assertArrayEquals(expected, matrix); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_740Test.java b/src/test/java/com/fishercoder/firstthousand/_740Test.java new file mode 100644 index 0000000000..dab9b04803 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_740Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._740; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _740Test { + private _740.Solution1 solution1; + private _740.Solution2 solution2; + private _740.Solution3 solution3; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _740.Solution1(); + solution2 = new _740.Solution2(); + solution3 = new _740.Solution3(); + } + + @Test + public void test1() { + nums = new int[] {3, 4, 2}; + assertEquals(6, solution1.deleteAndEarn(nums)); + assertEquals(6, solution2.deleteAndEarn(nums)); + assertEquals(6, solution3.deleteAndEarn(nums)); + } + + @Test + public void test2() { + nums = new int[] {2, 2, 3, 3, 3, 4}; + assertEquals(9, solution1.deleteAndEarn(nums)); + assertEquals(9, solution2.deleteAndEarn(nums)); + assertEquals(9, solution3.deleteAndEarn(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_742Test.java b/src/test/java/com/fishercoder/firstthousand/_742Test.java new file mode 100644 index 0000000000..a834486999 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_742Test.java @@ -0,0 +1,121 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._742; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _742Test { + private _742.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _742.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 2)); + System.out.println(solution1.findClosestLeaf(root, 1)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1)); + assertEquals(1, solution1.findClosestLeaf(root, 1)); + } + + @Test + public void test3() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList(1, 2, 3, 4, null, null, null, 5, null, 6)); + TreeUtils.printBinaryTree(root); + assertEquals(3, solution1.findClosestLeaf(root, 2)); + } + + @Test + public void test4() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); + TreeUtils.printBinaryTree(root); + assertEquals(5, solution1.findClosestLeaf(root, 5)); + } + + @Test + public void test5() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9, + null, 10, null, 11, null, 12, null, 13, null, 14, null, 15, null, + 16, null, 17, null, 18, null, 19, null, 20, null, 21, null, 22, + null, 23, null, 24, null, 25, null, 26, null, 27, null, 28, null, + 29, null, 30, null, 31, null, 32, null, 33, null, 34, null, 35, + null, 36, null, 37, null, 38, null, 39, null, 40, null, 41, null, + 42, null, 43, null, 44, null, 45, null, 46, null, 47, null, 48, + null, 49, null, 50, null, 51, null, 52, null, 53, null, 54, null, + 55, null, 56, null, 57, null, 58, null, 59, null, 60, null, 61, + null, 62, null, 63, null, 64, null, 65, null, 66, null, 67, null, + 68, null, 69, null, 70, null, 71, null, 72, null, 73, null, 74, + null, 75, null, 76, null, 77, null, 78, null, 79, null, 80, null, + 81, null, 82, null, 83, null, 84, null, 85, null, 86, null, 87, + null, 88, null, 89, null, 90, null, 91, null, 92, null, 93, null, + 94, null, 95, null, 96, null, 97, null, 98, null, 99, null, 100, + null, 101, null, 102, null, 103, null, 104, null, 105, null, 106, + null, 107, null, 108, null, 109, null, 110, null, 111, null, 112, + null, 113, null, 114, null, 115, null, 116, null, 117, null, 118, + null, 119, null, 120, null, 121, null, 122, null, 123, null, 124, + null, 125, null, 126, null, 127, null, 128, null, 129, null, 130, + null, 131, null, 132, null, 133, null, 134, null, 135, null, 136, + null, 137, null, 138, null, 139, null, 140, null, 141, null, 142, + null, 143, null, 144, null, 145, null, 146, null, 147, null, 148, + null, 149, null, 150, null, 151, null, 152, null, 153, null, 154, + null, 155, null, 156, null, 157, null, 158, null, 159, null, 160, + null, 161, null, 162, null, 163, null, 164, null, 165, null, 166, + null, 167, null, 168, null, 169, null, 170, null, 171, null, 172, + null, 173, null, 174, null, 175, null, 176, null, 177, null, 178, + null, 179, null, 180, null, 181, null, 182, null, 183, null, 184, + null, 185, null, 186, null, 187, null, 188, null, 189, null, 190, + null, 191, null, 192, null, 193, null, 194, null, 195, null, 196, + null, 197, null, 198, null, 199, null, 200, null, 201, null, 202, + null, 203, null, 204, null, 205, null, 206, null, 207, null, 208, + null, 209, null, 210, null, 211, null, 212, null, 213, null, 214, + null, 215, null, 216, null, 217, null, 218, null, 219, null, 220, + null, 221, null, 222, null, 223, null, 224, null, 225, null, 226, + null, 227, null, 228, null, 229, null, 230, null, 231, null, 232, + null, 233, null, 234, null, 235, null, 236, null, 237, null, 238, + null, 239, null, 240, null, 241, null, 242, null, 243, null, 244, + null, 245, null, 246, null, 247, null, 248, null, 249, null, 250, + null, 251, null, 252, null, 253, null, 254, null, 255, null, 256, + null, 257, null, 258, null, 259, null, 260, null, 261, null, 262, + null, 263, null, 264, null, 265, null, 266, null, 267, null, 268, + null, 269, null, 270, null, 271, null, 272, null, 273, null, 274, + null, 275, null, 276, null, 277, null, 278, null, 279, null, 280, + null, 281, null, 282, null, 283, null, 284, null, 285, null, 286, + null, 287, null, 288, null, 289, null, 290, null, 291, null, 292, + null, 293, null, 294, null, 295, null, 296, null, 297, null, 298, + null, 299, null, 300, null, 301, null, 302, null, 303, null, 304, + null, 305, null, 306, null, 307, null, 308, null, 309, null, 310, + null, 311, null, 312, null, 313, null, 314, null, 315, null, 316, + null, 317, null, 318, null, 319, null, 320, null, 321, null, 322, + null, 323, null, 324, null, 325, null, 326, null, 327, null, 328, + null, 329, null, 330, null, 331, null, 332, null, 333, null, 334, + null, 335, null, 336, null, 337, null, 338, null, 339, null, 340, + null, 341, null, 342, null, 343, null, 344, null, 345, null, 346, + null, 347, null, 348, null, 349, null, 350, null, 351, null, 352, + null, 353, null, 354, null, 355, null, 356, null, 357, null, 358, + null, 359, null, 360, null, 361, null, 362, null, 363, null, 364, + null, 365, null, 366, null, 367, null, 368, null, 369, null, 370, + null, 371, null, 372, null, 373, null, 374, null, 375, null, 376, + null, 377, null, 378, null, 379, null, 380, null, 381, null, 382, + null, 383, null, 384, null, 385, null, 386, null, 387, null, 388, + null, 389, null, 390, null, 391, null, 392, null, 393, null, 394, + null, 395, null, 396, null, 397, null, 398, null, 399, null)); + assertEquals(399, solution1.findClosestLeaf(root, 100)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_743Test.java b/src/test/java/com/fishercoder/firstthousand/_743Test.java new file mode 100644 index 0000000000..e393b5a291 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_743Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._743; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _743Test { + private _743.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _743.Solution1(); + } + + @Test + public void test1() { + int[][] times = {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}}; + assertEquals(solution1.networkDelayTime(times, 4, 2), 2); + } + + @Test + public void test2() { + int[][] times = {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}}; + assertEquals(solution1.networkDelayTime(times, 4, 3), -1); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_744Test.java b/src/test/java/com/fishercoder/firstthousand/_744Test.java new file mode 100644 index 0000000000..e9f0bd5499 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_744Test.java @@ -0,0 +1,53 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._744; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _744Test { + private _744.Solution1 solution1; + private static char[] letters; + + @BeforeEach + public void setup() { + solution1 = new _744.Solution1(); + } + + @Test + public void test1() { + letters = new char[] {'c', 'f', 'j'}; + assertEquals('c', solution1.nextGreatestLetter(letters, 'a')); + } + + @Test + public void test2() { + letters = new char[] {'c', 'f', 'j'}; + assertEquals('f', solution1.nextGreatestLetter(letters, 'c')); + } + + @Test + public void test3() { + letters = new char[] {'c', 'f', 'j'}; + assertEquals('f', solution1.nextGreatestLetter(letters, 'd')); + } + + @Test + public void test4() { + letters = new char[] {'c', 'f', 'j'}; + assertEquals('j', solution1.nextGreatestLetter(letters, 'g')); + } + + @Test + public void test5() { + letters = new char[] {'c', 'f', 'j'}; + assertEquals('c', solution1.nextGreatestLetter(letters, 'j')); + } + + @Test + public void test6() { + letters = new char[] {'c', 'f', 'j'}; + assertEquals('c', solution1.nextGreatestLetter(letters, 'k')); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_746Test.java b/src/test/java/com/fishercoder/firstthousand/_746Test.java new file mode 100644 index 0000000000..a9ef0014e3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_746Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._746; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _746Test { + private _746.Solution1 solution1; + private static int[] cost; + + @BeforeEach + public void setUp() { + solution1 = new _746.Solution1(); + } + + @Test + public void test1() { + cost = new int[] {10, 15, 20}; + assertEquals(15, solution1.minCostClimbingStairs(cost)); + } + + @Test + public void test2() { + cost = new int[] {1, 100, 1, 1, 1, 100, 1, 1, 100, 1}; + assertEquals(6, solution1.minCostClimbingStairs(cost)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_747Test.java b/src/test/java/com/fishercoder/firstthousand/_747Test.java new file mode 100644 index 0000000000..47ac4ad866 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_747Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._747; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _747Test { + private _747.Solution1 solution1; + private _747.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setUp() { + solution1 = new _747.Solution1(); + solution2 = new _747.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {3, 6, 1, 0}; + assertEquals(1, solution1.dominantIndex(nums)); + } + + @Test + public void test2() { + nums = new int[] {3, 6, 1, 0}; + assertEquals(1, solution2.dominantIndex(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 2, 3, 4}; + assertEquals(-1, solution1.dominantIndex(nums)); + } + + @Test + public void test4() { + nums = new int[] {1, 2, 3, 4}; + assertEquals(-1, solution2.dominantIndex(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_748Test.java b/src/test/java/com/fishercoder/firstthousand/_748Test.java new file mode 100644 index 0000000000..fbcea263c0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_748Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._748; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _748Test { + private _748.Solution1 solution1; + private static String[] words; + private static String licensePlate; + + @BeforeEach + public void setUp() { + solution1 = new _748.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"step", "steps", "stripe", "stepple"}; + licensePlate = "1s3 PSt"; + assertEquals("steps", solution1.shortestCompletingWord(licensePlate, words)); + } + + @Test + public void test2() { + words = new String[] {"looks", "pest", "stew", "show"}; + licensePlate = "1s3 456"; + assertEquals("pest", solution1.shortestCompletingWord(licensePlate, words)); + } + + @Test + public void test3() { + words = + new String[] { + "suggest", + "letter", + "of", + "husband", + "easy", + "education", + "drug", + "prevent", + "writer", + "old" + }; + licensePlate = "Ah71752"; + assertEquals("husband", solution1.shortestCompletingWord(licensePlate, words)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_74Test.java b/src/test/java/com/fishercoder/firstthousand/_74Test.java new file mode 100644 index 0000000000..e9da70c0a8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_74Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._74; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _74Test { + private _74.Solution1 solution1; + private static int target; + private static int[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _74.Solution1(); + } + + @Test + public void test1() { + target = 3; + matrix = + new int[][] { + {1, 3, 5, 7}, + {10, 11, 16, 20}, + {23, 30, 34, 50}, + }; + assertEquals(true, solution1.searchMatrix(matrix, target)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_750Test.java b/src/test/java/com/fishercoder/firstthousand/_750Test.java new file mode 100644 index 0000000000..bd2a41c4b2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_750Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._750; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _750Test { + private _750.Solution1 solution1; + private static int[][] grid; + + @BeforeEach + public void setUp() { + solution1 = new _750.Solution1(); + } + + @Test + public void test1() { + grid = + new int[][] { + {1, 0, 0, 1, 0}, + {0, 0, 1, 0, 1}, + {0, 0, 0, 1, 0}, + {1, 0, 1, 0, 1} + }; + assertEquals(1, solution1.countCornerRectangles(grid)); + } + + @Test + public void test2() { + grid = + new int[][] { + {1, 1, 1}, + {1, 1, 1}, + {1, 1, 1} + }; + assertEquals(9, solution1.countCornerRectangles(grid)); + } + + @Test + public void test3() { + grid = new int[][] {{1, 1, 1, 1}}; + assertEquals(0, solution1.countCornerRectangles(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_752Test.java b/src/test/java/com/fishercoder/firstthousand/_752Test.java new file mode 100644 index 0000000000..374dc2ea35 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_752Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._752; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _752Test { + private _752.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _752.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 6, + solution1.openLock(new String[] {"0201", "0101", "0102", "1212", "2002"}, "0202")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_754Test.java b/src/test/java/com/fishercoder/firstthousand/_754Test.java new file mode 100644 index 0000000000..a558f4d718 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_754Test.java @@ -0,0 +1,96 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._754; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _754Test { + private _754.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _754.Solution1(); + } + + @Test + public void test4() { + assertEquals(1, solution1.reachNumber(1)); + } + + @Test + public void test2() { + assertEquals(3, solution1.reachNumber(2)); + } + + @Test + public void test1() { + assertEquals(2, solution1.reachNumber(3)); + } + + @Test + public void test3() { + assertEquals(3, solution1.reachNumber(4)); + } + + @Test + public void test5() { + assertEquals(5, solution1.reachNumber(5)); + } + + @Test + public void test6() { + assertEquals(3, solution1.reachNumber(6)); + } + + @Test + public void test7() { + assertEquals(5, solution1.reachNumber(7)); + } + + @Test + public void test8() { + assertEquals(4, solution1.reachNumber(8)); + } + + @Test + public void test9() { + assertEquals(5, solution1.reachNumber(9)); + } + + @Test + public void test10() { + assertEquals(4, solution1.reachNumber(10)); + } + + @Test + public void test11() { + assertEquals(15, solution1.reachNumber(100)); + } + + @Test + public void test12() { + assertEquals(47, solution1.reachNumber(1000)); + } + + @Test + public void test13() { + assertEquals(143, solution1.reachNumber(10000)); + } + + @Test + public void test14() { + assertEquals(447, solution1.reachNumber(100000)); + } + + @Test + public void test15() { + assertEquals(1415, solution1.reachNumber(1000000)); + } + + @Test + public void test16() { + assertEquals(4472, solution1.reachNumber(10000000)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_755Test.java b/src/test/java/com/fishercoder/firstthousand/_755Test.java new file mode 100644 index 0000000000..4a95154494 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_755Test.java @@ -0,0 +1,88 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._755; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _755Test { + private _755.Solution1 solution1; + private static int[] heights; + private static int[] expected; + + @BeforeEach + public void setUp() { + solution1 = new _755.Solution1(); + } + + @Test + public void test1() { + heights = new int[] {2, 1, 1, 2, 1, 2, 2}; + expected = new int[] {2, 2, 2, 3, 2, 2, 2}; + assertArrayEquals(expected, solution1.pourWater(heights, 4, 3)); + } + + @Test + public void test2() { + heights = new int[] {1, 2, 3, 4}; + expected = new int[] {2, 3, 3, 4}; + assertArrayEquals(expected, solution1.pourWater(heights, 2, 2)); + } + + @Test + public void test3() { + heights = new int[] {3, 1, 3}; + expected = new int[] {4, 4, 4}; + assertArrayEquals(expected, solution1.pourWater(heights, 5, 1)); + } + + @Test + public void test4() { + heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + expected = new int[] {1, 2, 3, 4, 3, 3, 2, 2, 3, 4, 3, 2, 1}; + assertArrayEquals(expected, solution1.pourWater(heights, 2, 5)); + } + + @Test + public void test5() { + heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + expected = new int[] {3, 4, 4, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + assertArrayEquals(expected, solution1.pourWater(heights, 5, 2)); + } + + @Test + public void test6() { + heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + expected = new int[] {4, 4, 4, 4, 3, 3, 3, 3, 3, 4, 3, 2, 1}; + assertArrayEquals(expected, solution1.pourWater(heights, 10, 2)); + } + + @Test + public void test7() { + heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + expected = new int[] {2, 3, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + assertArrayEquals(expected, solution1.pourWater(heights, 2, 2)); + } + + @Test + public void test8() { + heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + expected = new int[] {3, 3, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + assertArrayEquals(expected, solution1.pourWater(heights, 3, 2)); + } + + @Test + public void test9() { + heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + expected = new int[] {3, 3, 4, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + assertArrayEquals(expected, solution1.pourWater(heights, 4, 2)); + } + + @Test + public void test10() { + heights = new int[] {1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + expected = new int[] {3, 4, 4, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1}; + assertArrayEquals(expected, solution1.pourWater(heights, 5, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_756Test.java b/src/test/java/com/fishercoder/firstthousand/_756Test.java new file mode 100644 index 0000000000..2f7a86d55d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_756Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._756; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _756Test { + private _756.Solution1 solution1; + private static List allowed; + + @BeforeEach + public void setUp() { + solution1 = new _756.Solution1(); + } + + @Test + public void test1() { + allowed = Arrays.asList("XYD", "YZE", "DEA", "FFF"); + assertEquals(true, solution1.pyramidTransition("XYZ", allowed)); + } + + @Test + public void test2() { + allowed = Arrays.asList("XXX", "XXY", "XYX", "XYY", "YXZ"); + assertEquals(false, solution1.pyramidTransition("XXYX", allowed)); + } + + @Test + public void test3() { + allowed = Arrays.asList("BCE", "BCF", "ABA", "CDA", "AEG", "FAG", "GGG"); + assertEquals(false, solution1.pyramidTransition("ABCD", allowed)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_757Test.java b/src/test/java/com/fishercoder/firstthousand/_757Test.java new file mode 100644 index 0000000000..dcf9037a9a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_757Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._757; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _757Test { + private _757.Solution solution; + int[][] intervals; + + @BeforeEach + public void setup() { + solution = new _757.Solution(); + } + + @Test + public void test1() { + intervals = new int[][] {{1, 3}, {1, 4}, {2, 5}, {3, 5}}; + assertEquals(3, solution.intersectionSizeTwo(intervals)); + } + + @Test + public void test2() { + intervals = + new int[][] { + {16, 18}, {11, 18}, {15, 23}, {1, 16}, {10, 16}, {6, 19}, {18, 20}, {7, 19} + }; + assertEquals(4, solution.intersectionSizeTwo(intervals)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_758Test.java b/src/test/java/com/fishercoder/firstthousand/_758Test.java new file mode 100644 index 0000000000..7cc5f82aa3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_758Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._758; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _758Test { + private _758.Solution1 solution1; + private static String[] words; + + @BeforeEach + public void setUp() { + solution1 = new _758.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"ab", "bc"}; + assertEquals("aabcd", solution1.boldWords(words, "aabcd")); + } + + @Test + public void test2() { + words = new String[] {"ccb", "b", "d", "cba", "dc"}; + assertEquals("eeaadadadc", solution1.boldWords(words, "eeaadadadc")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_75Test.java b/src/test/java/com/fishercoder/firstthousand/_75Test.java new file mode 100644 index 0000000000..1b857664ca --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_75Test.java @@ -0,0 +1,108 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._75; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _75Test { + private _75.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _75.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 2, 1}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {1, 2, 2}, nums); + } + + @Test + public void test2() { + nums = new int[] {0, 1, 2, 0, 2, 1}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {0, 0, 1, 1, 2, 2}, nums); + } + + @Test + public void test3() { + nums = new int[] {1, 0, 2}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {0, 1, 2}, nums); + } + + @Test + public void test4() { + nums = new int[] {1, 0}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {0, 1}, nums); + } + + @Test + public void test5() { + nums = new int[] {2}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {2}, nums); + } + + @Test + public void test6() { + nums = new int[] {2, 0, 1}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {0, 1, 2}, nums); + } + + @Test + public void test7() { + nums = new int[] {0}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {0}, nums); + } + + @Test + public void test8() { + nums = new int[] {2, 2}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {2, 2}, nums); + } + + @Test + public void test9() { + nums = new int[] {2}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {2}, nums); + } + + @Test + public void test10() { + nums = new int[] {1}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {1}, nums); + } + + @Test + public void test11() { + nums = new int[] {1, 2, 0}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {0, 1, 2}, nums); + } + + @Test + public void test12() { + nums = new int[] {1, 2, 1}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {1, 1, 2}, nums); + } + + @Test + public void test13() { + nums = new int[] {2, 0, 2, 1, 1, 0}; + solution1.sortColors(nums); + assertArrayEquals(new int[] {0, 0, 1, 1, 2, 2}, nums); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_760Test.java b/src/test/java/com/fishercoder/firstthousand/_760Test.java new file mode 100644 index 0000000000..55a9efd080 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_760Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._760; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _760Test { + private _760.Solution1 solution1; + private static int[] A; + private static int[] B; + private static int[] expected; + + @BeforeEach + public void setUp() { + solution1 = new _760.Solution1(); + } + + @Test + public void test1() { + A = new int[] {12, 28, 46, 32, 50}; + B = new int[] {50, 12, 32, 46, 28}; + expected = new int[] {1, 4, 3, 2, 0}; + assertArrayEquals(expected, solution1.anagramMappings(A, B)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_762Test.java b/src/test/java/com/fishercoder/firstthousand/_762Test.java new file mode 100644 index 0000000000..c7ee7e1980 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_762Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._762; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _762Test { + private _762.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _762.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.countPrimeSetBits(6, 10)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_763Test.java b/src/test/java/com/fishercoder/firstthousand/_763Test.java new file mode 100644 index 0000000000..7b196a142e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_763Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._763; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _763Test { + private _763.Solution1 solution1; + private _763.Solution2 solution2; + private static List expected; + private static String s; + + @BeforeEach + public void setup() { + solution1 = new _763.Solution1(); + solution2 = new _763.Solution2(); + } + + @Test + public void test1() { + expected = Arrays.asList(9, 7, 8); + s = "ababcbacadefegdehijhklij"; + assertEquals(expected, solution1.partitionLabels(s)); + assertEquals(expected, solution2.partitionLabels(s)); + } + + @Test + public void test2() { + expected = Arrays.asList(10); + s = "eccbbbbdec"; + assertEquals(expected, solution1.partitionLabels(s)); + assertEquals(expected, solution2.partitionLabels(s)); + } + + @Test + public void test3() { + expected = Arrays.asList(1, 9); + s = "caedbdedda"; + assertEquals(expected, solution1.partitionLabels(s)); + assertEquals(expected, solution2.partitionLabels(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_764Test.java b/src/test/java/com/fishercoder/firstthousand/_764Test.java new file mode 100644 index 0000000000..520445cf0a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_764Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._764; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _764Test { + private _764.Solution1 solution1; + private _764.Solution2 solution2; + private static int[][] mines; + + @BeforeEach + public void setup() { + solution1 = new _764.Solution1(); + solution2 = new _764.Solution2(); + } + + @Test + public void test1() { + mines = new int[][] {{0, 1}, {1, 0}, {1, 1}}; + assertEquals(1, solution1.orderOfLargestPlusSign(2, mines)); + assertEquals(1, solution2.orderOfLargestPlusSign(2, mines)); + assertEquals(1, solution2.orderOfLargestPlusSign_initialVersion(2, mines)); + } + + @Test + public void test2() { + mines = new int[][] {{4, 2}}; + assertEquals(2, solution1.orderOfLargestPlusSign(5, mines)); + assertEquals(2, solution2.orderOfLargestPlusSign(5, mines)); + assertEquals(2, solution2.orderOfLargestPlusSign_initialVersion(5, mines)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_765Test.java b/src/test/java/com/fishercoder/firstthousand/_765Test.java new file mode 100644 index 0000000000..f4542aa244 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_765Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._765; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _765Test { + private _765.Solution1 solution1; + private static int[] row; + + @BeforeEach + public void setUp() { + solution1 = new _765.Solution1(); + } + + @Test + public void test1() { + row = new int[] {0, 2, 1, 3}; + assertEquals(1, solution1.minSwapsCouples(row)); + } + + @Test + public void test2() { + row = new int[] {3, 2, 0, 1}; + assertEquals(0, solution1.minSwapsCouples(row)); + } + + @Test + public void test3() { + row = new int[] {0, 4, 7, 3, 1, 5, 2, 8, 6, 9}; + assertEquals(3, solution1.minSwapsCouples(row)); + } + + @Test + public void test4() { + row = new int[] {5, 6, 4, 0, 2, 1, 9, 3, 8, 7, 11, 10}; + assertEquals(4, solution1.minSwapsCouples(row)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_766Test.java b/src/test/java/com/fishercoder/firstthousand/_766Test.java new file mode 100644 index 0000000000..d3c78f125c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_766Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._766; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _766Test { + private _766.Solution1 solution1; + private static int[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _766.Solution1(); + } + + @Test + public void test1() { + matrix = + new int[][] { + {1, 2, 3, 4}, + {5, 1, 2, 3}, + {9, 5, 1, 2} + }; + assertEquals(true, solution1.isToeplitzMatrix(matrix)); + } + + @Test + public void test2() { + matrix = + new int[][] { + {1, 2}, + {2, 2}, + }; + assertEquals(false, solution1.isToeplitzMatrix(matrix)); + } + + @Test + public void test3() { + matrix = + new int[][] { + {1, 2, 3, 4, 5, 9}, + {5, 1, 2, 3, 4, 5}, + {9, 5, 1, 2, 3, 4} + }; + assertEquals(true, solution1.isToeplitzMatrix(matrix)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_767Test.java b/src/test/java/com/fishercoder/firstthousand/_767Test.java new file mode 100644 index 0000000000..4959169b3a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_767Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._767; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _767Test { + private _767.Solution1 solution1; + private _767.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _767.Solution1(); + solution2 = new _767.Solution2(); + } + + @Test + public void test1() { + assertEquals("aba", solution1.reorganizeString("aab")); + assertEquals("aba", solution2.reorganizeString("aab")); + } + + @Test + public void test2() { + assertEquals("", solution1.reorganizeString("aaab")); + assertEquals("", solution2.reorganizeString("aaab")); + } + + @Test + public void test3() { + assertEquals("bababab", solution1.reorganizeString("aaabbbb")); + assertEquals("bababab", solution2.reorganizeString("aaabbbb")); + } + + @Test + public void test4() { + assertEquals("vovlv", solution1.reorganizeString("vvvlo")); + assertEquals("vovlv", solution2.reorganizeString("vvvlo")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_769Test.java b/src/test/java/com/fishercoder/firstthousand/_769Test.java new file mode 100644 index 0000000000..e617ba696e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_769Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._769; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _769Test { + private _769.Solution1 solution1; + private _769.Solution2 solution2; + private static int[] arr; + + @BeforeEach + public void setUp() { + solution1 = new _769.Solution1(); + solution2 = new _769.Solution2(); + } + + @Test + public void test1() { + arr = new int[] {4, 3, 2, 1, 0}; + assertEquals(1, solution1.maxChunksToSorted(arr)); + assertEquals(1, solution2.maxChunksToSorted(arr)); + } + + @Test + public void test2() { + arr = new int[] {1, 0, 2, 3, 4}; + assertEquals(4, solution1.maxChunksToSorted(arr)); + assertEquals(4, solution2.maxChunksToSorted(arr)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_76Test.java b/src/test/java/com/fishercoder/firstthousand/_76Test.java new file mode 100644 index 0000000000..d9d1166fab --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_76Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._76; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _76Test { + private _76.Solution1 solution1; + private _76.Solution2 solution2; + private static String expected; + private static String s; + private static String t; + + @BeforeEach + public void setup() { + solution1 = new _76.Solution1(); + solution2 = new _76.Solution2(); + } + + @Test + public void test1() { + expected = "BANC"; + s = "ADOBECODEBANC"; + t = "ABC"; + assertEquals(expected, solution1.minWindow(s, t)); + assertEquals(expected, solution2.minWindow(s, t)); + } + + @Test + public void test2() { + expected = ""; + s = "A"; + t = "B"; + assertEquals(expected, solution1.minWindow(s, t)); + assertEquals(expected, solution2.minWindow(s, t)); + } + + @Test + public void test3() { + expected = "cwae"; + s = "cabwefgewcwaefgcf"; + t = "cae"; + assertEquals(expected, solution1.minWindow(s, t)); + assertEquals(expected, solution2.minWindow(s, t)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_771Test.java b/src/test/java/com/fishercoder/firstthousand/_771Test.java new file mode 100644 index 0000000000..dd3f04ce58 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_771Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._771; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _771Test { + private _771.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _771.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.numJewelsInStones("aA", "aAAbbbb")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_773Test.java b/src/test/java/com/fishercoder/firstthousand/_773Test.java new file mode 100644 index 0000000000..fe295e44ae --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_773Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._773; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _773Test { + private _773.Solution1 solution1; + private static int[][] board; + + @BeforeEach + public void setup() { + solution1 = new _773.Solution1(); + } + + @Test + public void test1() { + board = + new int[][] { + {1, 2, 3}, + {4, 0, 5} + }; + assertEquals(1, solution1.slidingPuzzle(board)); + } + + @Test + public void test2() { + board = + new int[][] { + {4, 1, 2}, + {5, 0, 3} + }; + assertEquals(5, solution1.slidingPuzzle(board)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_775Test.java b/src/test/java/com/fishercoder/firstthousand/_775Test.java new file mode 100644 index 0000000000..707f64b48f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_775Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._775; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _775Test { + private _775.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _775.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isIdealPermutation(new int[] {0, 1})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_776Test.java b/src/test/java/com/fishercoder/firstthousand/_776Test.java new file mode 100644 index 0000000000..cbdf91ebff --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_776Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._776; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _776Test { + private _776.Solution1 solution1; + private _776.Solution2 solution2; + private static TreeNode root; + private static TreeNode small; + private static TreeNode big; + + @BeforeEach + public void setUp() { + solution1 = new _776.Solution1(); + solution2 = new _776.Solution2(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 1, 3, 5, 7)); + small = TreeUtils.constructBinaryTree(Arrays.asList(2, 1)); + big = TreeUtils.constructBinaryTree(Arrays.asList(4, 3, 6, null, null, 5, 7)); + assertArrayEquals(new TreeNode[] {small, big}, solution1.splitBST(root, 2)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 1, 3, 5, 7)); + small = TreeUtils.constructBinaryTree(Arrays.asList(2, 1)); + big = TreeUtils.constructBinaryTree(Arrays.asList(4, 3, 6, null, null, 5, 7)); + assertArrayEquals(new TreeNode[] {small, big}, solution2.splitBST(root, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_777Test.java b/src/test/java/com/fishercoder/firstthousand/_777Test.java new file mode 100644 index 0000000000..25a65f1fae --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_777Test.java @@ -0,0 +1,78 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.firstthousand._777; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _777Test { + + private _777.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _777.Solution1(); + } + + @Test + public void test1() { + assertTrue(solution1.canTransform("RXXLRXRXL", "XRLXXRRLX")); + } + + @Test + public void test2() { + assertFalse(solution1.canTransform("X", "L")); + } + + @Test + public void test3() { + assertTrue(solution1.canTransform("XXXXXLXXXX", "LXXXXXXXXX")); + } + + @Test + public void test4() { + assertFalse(solution1.canTransform("LLR", "RRL")); + } + + @Test + public void test5() { + assertFalse(solution1.canTransform("XXXL", "LXXR")); + } + + @Test + public void test6() { + assertTrue(solution1.canTransform("RXXX", "XXXR")); + } + + @Test + public void test7() { + assertTrue(solution1.canTransform("XXRXLXRXXX", "XXRLXXXXXR")); + } + + @Test + public void test8() { + assertFalse(solution1.canTransform("XLLR", "LXLX")); + } + + @Test + public void test9() { + assertFalse(solution1.canTransform("RXR", "XXR")); + } + + @Test + public void test10() { + assertTrue(solution1.canTransform("XLXRRXXRXX", "LXXXXXXRRR")); + } + + @Test + public void test11() { + assertFalse(solution1.canTransform("LXXLXRLXXL", "XLLXRXLXLX")); + } + + @Test + public void test12() { + assertFalse(solution1.canTransform("XXXXXLXXXLXXXX", "XXLXXXXXXXXLXX")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_779Test.java b/src/test/java/com/fishercoder/firstthousand/_779Test.java new file mode 100644 index 0000000000..6432aafee0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_779Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._779; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _779Test { + private _779.Solution1 solution1; + private _779.Solution2 solution2; + + @BeforeEach + public void setUp() { + solution1 = new _779.Solution1(); + solution2 = new _779.Solution2(); + } + + @Test + public void test1() { + assertEquals(0, solution1.kthGrammar(1, 1)); + assertEquals(0, solution2.kthGrammar(1, 1)); + } + + @Test + public void test2() { + assertEquals(0, solution1.kthGrammar(2, 1)); + assertEquals(0, solution2.kthGrammar(2, 1)); + } + + @Test + public void test3() { + assertEquals(1, solution1.kthGrammar(2, 2)); + assertEquals(1, solution2.kthGrammar(2, 2)); + } + + @Test + public void test4() { + assertEquals(1, solution1.kthGrammar(4, 5)); + assertEquals(1, solution2.kthGrammar(4, 5)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_77Test.java b/src/test/java/com/fishercoder/firstthousand/_77Test.java new file mode 100644 index 0000000000..e88a3ff975 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_77Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._77; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _77Test { + private _77.Solution1 solution1; + private _77.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _77.Solution1(); + solution2 = new _77.Solution2(); + } + + @Test + public void test1() { + CommonUtils.printListList(solution1.combine(4, 2)); + CommonUtils.printListList(solution2.combine(4, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_781Test.java b/src/test/java/com/fishercoder/firstthousand/_781Test.java new file mode 100644 index 0000000000..6321cf90db --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_781Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._781; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _781Test { + private _781.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _781.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.numRabbits(new int[] {1, 1, 2})); + } + + @Test + public void test2() { + assertEquals(11, solution1.numRabbits(new int[] {10, 10, 10})); + } + + @Test + public void test3() { + assertEquals(0, solution1.numRabbits(new int[] {})); + } + + @Test + public void test4() { + assertEquals(5, solution1.numRabbits(new int[] {1, 0, 1, 0, 0})); + } + + @Test + public void test5() { + assertEquals(7, solution1.numRabbits(new int[] {1, 1, 1, 2, 2, 2})); + } + + @Test + public void test6() { + assertEquals(13, solution1.numRabbits(new int[] {2, 1, 2, 2, 2, 2, 2, 2, 1, 1})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_783Test.java b/src/test/java/com/fishercoder/firstthousand/_783Test.java new file mode 100644 index 0000000000..736b7c6fe8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_783Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._783; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _783Test { + private _783.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setUp() { + solution1 = new _783.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 1, 3, null, null)); + TreeUtils.printBinaryTree(root); + assertEquals(1, solution1.minDiffInBST(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_784Test.java b/src/test/java/com/fishercoder/firstthousand/_784Test.java new file mode 100644 index 0000000000..868e2ae106 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_784Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.fishercoder.solutions.firstthousand._784; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _784Test { + private _784.Solution1 solution1; + private _784.Solution2 solution2; + private static List expected; + + @BeforeEach + public void setup() { + solution1 = new _784.Solution1(); + solution2 = new _784.Solution2(); + } + + @Test + public void test1() { + expected = Arrays.asList("a1b2", "a1B2", "A1b2", "A1B2"); + assertThat(expected).hasSameElementsAs(solution1.letterCasePermutation("a1b2")); + assertThat(expected).hasSameElementsAs(solution2.letterCasePermutation("a1b2")); + } + + @Test + public void test2() { + expected = Arrays.asList("3z4", "3Z4"); + assertThat(expected).hasSameElementsAs(solution1.letterCasePermutation("3z4")); + assertThat(expected).hasSameElementsAs(solution2.letterCasePermutation("3z4")); + } + + @Test + public void test3() { + expected = Arrays.asList("12345"); + assertThat(expected).hasSameElementsAs(solution1.letterCasePermutation("12345")); + assertThat(expected).hasSameElementsAs(solution2.letterCasePermutation("12345")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_785Test.java b/src/test/java/com/fishercoder/firstthousand/_785Test.java new file mode 100644 index 0000000000..9fa2a29cdc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_785Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._785; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _785Test { + private _785.Solution1 solution1; + private _785.Solution2 solution2; + private static int[][] graph; + + @BeforeEach + public void setup() { + solution1 = new _785.Solution1(); + solution2 = new _785.Solution2(); + graph = new int[][] {}; + } + + @Test + public void test1() { + graph = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2,3],[0,2],[0,1,3],[0,2]"); + CommonUtils.print2DIntArray(graph); + assertFalse(solution1.isBipartite(graph)); + assertFalse(solution2.isBipartite(graph)); + } + + @Test + public void test2() { + graph = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,3],[0,2],[1,3],[0,2]"); + CommonUtils.print2DIntArray(graph); + assertTrue(solution1.isBipartite(graph)); + assertTrue(solution2.isBipartite(graph)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_788Test.java b/src/test/java/com/fishercoder/firstthousand/_788Test.java new file mode 100644 index 0000000000..f332227f8f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_788Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._788; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _788Test { + private _788.Solution1 solution1; + private _788.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _788.Solution1(); + solution2 = new _788.Solution2(); + } + + @Test + public void test1() { + assertEquals(4, solution1.rotatedDigits(10)); + assertEquals(4, solution2.rotatedDigits(10)); + } + + @Test + public void test2() { + assertEquals(247, solution1.rotatedDigits(857)); + assertEquals(247, solution2.rotatedDigits(857)); + } + + @Test + public void test3() { + assertEquals(6, solution1.rotatedDigits(15)); + assertEquals(6, solution2.rotatedDigits(15)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_789Test.java b/src/test/java/com/fishercoder/firstthousand/_789Test.java new file mode 100644 index 0000000000..8887e6f74e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_789Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._789; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by varunu28 on 1/01/19. */ +public class _789Test { + private _789.Solution test; + + @BeforeEach + public void setup() { + test = new _789.Solution(); + } + + @Test + public void test1() { + assertEquals(true, test.escapeGhosts(new int[][] {{1, 0}, {0, 3}}, new int[] {0, 1})); + } + + @Test + public void test2() { + assertEquals(false, test.escapeGhosts(new int[][] {{1, 0}}, new int[] {2, 0})); + } + + @Test + public void test3() { + assertEquals(false, test.escapeGhosts(new int[][] {{2, 0}}, new int[] {1, 0})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_78Test.java b/src/test/java/com/fishercoder/firstthousand/_78Test.java new file mode 100644 index 0000000000..da21fe9589 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_78Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._78; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _78Test { + private _78.Solution1 solution1; + private _78.Solution2 solution2; + private _78.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _78.Solution1(); + solution2 = new _78.Solution2(); + solution3 = new _78.Solution3(); + } + + @Test + public void test1() { + CommonUtils.printListList(solution1.subsets(new int[] {1, 2, 3})); + } + + @Test + public void test2() { + CommonUtils.printListList(solution2.subsets(new int[] {1, 2, 3})); + } + + @Test + public void test3() { + CommonUtils.printListList(solution3.subsets(new int[] {1, 2, 3})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_791Test.java b/src/test/java/com/fishercoder/firstthousand/_791Test.java new file mode 100644 index 0000000000..448d247731 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_791Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._791; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _791Test { + private _791.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _791.Solution1(); + } + + @Test + public void test1() { + assertEquals("cbad", solution1.customSortString("cba", "abcd")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_792Test.java b/src/test/java/com/fishercoder/firstthousand/_792Test.java new file mode 100644 index 0000000000..0a4aa1309f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_792Test.java @@ -0,0 +1,155 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._792; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _792Test { + private _792.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _792.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 3, solution1.numMatchingSubseq("abcde", new String[] {"a", "bb", "acd", "ace"})); + } + + @Test + public void test2() { + assertEquals( + 2, + solution1.numMatchingSubseq( + "dsahjpjauf", new String[] {"ahjpjau", "ja", "ahbwzgqnuk", "tnmlanowax"})); + } + + @Test + public void test3() { + assertEquals(2, solution1.numMatchingSubseq("vvvvvvvvm", new String[] {"vvm", "vm", "vn"})); + } + + @Test + public void test4() { + assertEquals(1, solution1.numMatchingSubseq("vvvvvvvvm", new String[] {"vm"})); + } + + @Test + public void test5() { + assertEquals(1, solution1.numMatchingSubseq("vvvvvvvvm", new String[] {"vvm"})); + } + + @Test + public void test6() { + assertEquals( + 51, + solution1.numMatchingSubseq( + "ricogwqznwxxcpueelcobbbkuvxxrvgyehsudccpsnuxpcqobtvwkuvsubiidjtccoqvuahijyefbpqhbejuisksutsowhufsygtwteiqyligsnbqglqblhpdzzeurtdohdcbjvzgjwylmmoiundjscnlhbrhookmioxqighkxfugpeekgtdofwzemelpyjsdeeppapjoliqlhbrbghqjezzaxuwyrbczodtrhsvnaxhcjiyiphbglyolnswlvtlbmkrsurrcsgdzutwgjofowhryrubnxkahocqjzwwagqidjhwbunvlchojtbvnzdzqpvrazfcxtvhkruvuturdicnucvndigovkzrqiyastqpmfmuouycodvsyjajekhvyjyrydhxkdhffyytldcdlxqbaszbuxsacqwqnhrewhagldzhryzdmmrwnxhaqfezeeabuacyswollycgiowuuudrgzmwnxaezuqlsfvchjfloczlwbefksxsbanrektvibbwxnokzkhndmdhweyeycamjeplecewpnpbshhidnzwopdjuwbecarkgapyjfgmanuavzrxricbgagblomyseyvoeurekqjyljosvbneofjzxtaizjypbcxnbfeibrfjwyjqrisuybfxpvqywqjdlyznmojdhbeomyjqptltpugzceyzenflfnhrptuugyfsghluythksqhmxlmggtcbdddeoincygycdpehteiugqbptyqbvokpwovbnplshnzafunqglnpjvwddvdlmjjyzmwwxzjckmaptilrbfpjxiarmwalhbdjiwbaknvcqovwcqiekzfskpbhgxpyomekqvzpqyirelpadooxjhsyxjkfqavbaoqqvvknqryhotjritrkvdveyapjfsfzenfpuazdrfdofhudqbfnzxnvpluwicurrtshyvevkriudayyysepzqfgqwhgobwyhxltligahroyshfndydvffd", + new String[] { + "iowuuudrgzmw", + "azfcxtvhkruvuturdicnucvndigovkzrq", + "ylmmo", + "maptilrbfpjxiarmwalhbd", + "oqvuahijyefbpqhbejuisksutsowhufsygtwteiqyligsnbqgl", + "ytldcdlxqbaszbuxsacqwqnhrewhagldzhr", + "zeeab", + "cqie", + "pvrazfcxtvhkruvuturdicnucvndigovkzrqiya", + "zxnvpluwicurrtshyvevkriudayyysepzq", + "wyhxltligahroyshfn", + "nhrewhagldzhryzdmmrwn", + "yqbvokpwovbnplshnzafunqglnpjvwddvdlmjjyzmw", + "nhrptuugyfsghluythksqhmxlmggtcbdd", + "yligsnbqglqblhpdzzeurtdohdcbjvzgjwylmmoiundjsc", + "zdrfdofhudqbfnzxnvpluwicurrtshyvevkriudayyysepzq", + "ncygycdpehteiugqbptyqbvokpwovbnplshnzafun", + "gdzutwgjofowhryrubnxkahocqjzww", + "eppapjoliqlhbrbgh", + "qwhgobwyhxltligahroys", + "dzutwgjofowhryrubnxkah", + "rydhxkdhffyytldcdlxqbaszbuxs", + "tyqbvokpwovbnplshnzafunqglnpjvwddvdlmjjyzmwwxzjc", + "khvyjyrydhxkdhffyytldcdlxqbasz", + "jajekhvyjyrydhxkdhffyytldcdlxqbaszbuxsacqwqn", + "ppapjoliqlhbrbghq", + "zmwwxzjckmaptilrbfpjxiarm", + "nxkahocqjzwwagqidjhwbunvlchoj", + "ybfxpvqywqjdlyznmojdhbeomyjqptltp", + "udrgzmwnxae", + "nqglnpjvwddvdlmjjyzmww", + "swlvtlbmkrsurrcsgdzutwgjofowhryrubn", + "hudqbfnzxnvpluwicurr", + "xaezuqlsfvchjf", + "tvibbwxnokzkhndmdhweyeycamjeplec", + "olnswlvtlbmkrsurrcsgdzu", + "qiyastqpmfmuouycodvsyjajekhvyjyrydhxkdhffyyt", + "eiqyligsnbqglqblhpdzzeurtdohdcbjvzgjwyl", + "cgiowuuudrgzmwnxaezuqlsfvchjflocz", + "rxric", + "cygycdpehteiugqbptyqbvokpwovbnplshnzaf", + "g", + "surrcsgd", + "yzenflfnhrptuugyfsghluythksqh", + "gdzutwgjofowhryrubnxkahocqjzwwagqid", + "ddeoincygycdpeh", + "yznmojdhbeomyjqptltpugzceyzenflfnhrptuug", + "ejuisks", + "teiqyligsnbqglqblhpdzzeurtdohdcbjvzgjwylmmoi", + "mrwnxhaqfezeeabuacyswollycgio", + "qfskkpfakjretogrokmxemjjbvgmmqrfdxlkfvycwalbdeumav", + "wjgjhlrpvhqozvvkifhftnfqcfjmmzhtxsoqbeduqmnpvimagq", + "ibxhtobuolmllbasaxlanjgalgmbjuxmqpadllryaobcucdeqc", + "ydlddogzvzttizzzjohfsenatvbpngarutztgdqczkzoenbxzv", + "rmsakibpprdrttycxglfgtjlifznnnlkgjqseguijfctrcahbb", + "pqquuarnoybphojyoyizhuyjfgwdlzcmkdbdqzatgmabhnpuyh", + "akposmzwykwrenlcrqwrrvsfqxzohrramdajwzlseguupjfzvd", + "vyldyqpvmnoemzeyxslcoysqfpvvotenkmehqvopynllvwhxzr", + "ysyskgrbolixwmffygycvgewxqnxvjsfefpmxrtsqsvpowoctw", + "oqjgumitldivceezxgoiwjgozfqcnkergctffspdxdbnmvjago", + "bpfgqhlkvevfazcmpdqakonkudniuobhqzypqlyocjdngltywn", + "ttucplgotbiceepzfxdebvluioeeitzmesmoxliuwqsftfmvlg", + "xhkklcwblyjmdyhfscmeffmmerxdioseybombzxjatkkltrvzq", + "qkvvbrgbzzfhzizulssaxupyqwniqradvkjivedckjrinrlxgi", + "itjudnlqncbspswkbcwldkwujlshwsgziontsobirsvskmjbrq", + "nmfgxfeqgqefxqivxtdrxeelsucufkhivijmzgioxioosmdpwx", + "ihygxkykuczvyokuveuchermxceexajilpkcxjjnwmdbwnxccl", + "etvcfbmadfxlprevjjnojxwonnnwjnamgrfwohgyhievupsdqd", + "ngskodiaxeswtqvjaqyulpedaqcchcuktfjlzyvddfeblnczmh", + "vnmntdvhaxqltluzwwwwrbpqwahebgtmhivtkadczpzabgcjzx", + "yjqqdvoxxxjbrccoaqqspqlsnxcnderaewsaqpkigtiqoqopth", + "wdytqvztzbdzffllbxexxughdvetajclynypnzaokqizfxqrjl", + "yvvwkphuzosvvntckxkmvuflrubigexkivyzzaimkxvqitpixo", + "lkdgtxmbgsenzmrlccmsunaezbausnsszryztfhjtezssttmsr", + "idyybesughzyzfdiibylnkkdeatqjjqqjbertrcactapbcarzb", + "ujiajnirancrfdvrfardygbcnzkqsvujkhcegdfibtcuxzbpds", + "jjtkmalhmrknaasskjnixzwjgvusbozslrribgazdhaylaxobj", + "nizuzttgartfxiwcsqchizlxvvnebqdtkmghtcyzjmgyzszwgi", + "egtvislckyltpfogtvfbtxbsssuwvjcduxjnjuvnqyiykvmrxl", + "ozvzwalcvaobxbicbwjrububyxlmfcokdxcrkvuehbnokkzala", + "azhukctuheiwghkalboxfnuofwopsrutamthzyzlzkrlsefwcz", + "yhvjjzsxlescylsnvmcxzcrrzgfhbsdsvdfcykwifzjcjjbmmu", + "tspdebnuhrgnmhhuplbzvpkkhfpeilbwkkbgfjiuwrdmkftphk", + "jvnbeqzaxecwxspuxhrngmvnkvulmgobvsnqyxdplrnnwfhfqq", + "bcbkgwpfmmqwmzjgmflichzhrjdjxbcescfijfztpxpxvbzjch", + "bdrkibtxygyicjcfnzigghdekmgoybvfwshxqnjlctcdkiunob", + "koctqrqvfftflwsvssnokdotgtxalgegscyeotcrvyywmzescq", + "boigqjvosgxpsnklxdjaxtrhqlyvanuvnpldmoknmzugnubfoa", + "jjtxbxyazxldpnbxzgslgguvgyevyliywihuqottxuyowrwfar", + "zqsacrwcysmkfbpzxoaszgqqsvqglnblmxhxtjqmnectaxntvb", + "izcakfitdhgujdborjuhtwubqcoppsgkqtqoqyswjfldsbfcct", + "rroiqffqzenlerchkvmjsbmoybisjafcdzgeppyhojoggdlpzq", + "xwjqfobmmqomhczwufwlesolvmbtvpdxejzslxrvnijhvevxmc", + "ccrubahioyaxuwzloyhqyluwoknxnydbedenrccljoydfxwaxy", + "jjoeiuncnvixvhhynaxbkmlurwxcpukredieqlilgkupminjaj", + "pdbsbjnrqzrbmewmdkqqhcpzielskcazuliiatmvhcaksrusae", + "nizbnxpqbzsihakkadsbtgxovyuebgtzvrvbowxllkzevktkuu", + "hklskdbopqjwdrefpgoxaoxzevpdaiubejuaxxbrhzbamdznrr", + "uccnuegvmkqtagudujuildlwefbyoywypakjrhiibrxdmsspjl", + "awinuyoppufjxgqvcddleqdhbkmolxqyvsqprnwcoehpturicf" + })); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_796Test.java b/src/test/java/com/fishercoder/firstthousand/_796Test.java new file mode 100644 index 0000000000..7b28b9c390 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_796Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.firstthousand._796; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _796Test { + private _796.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _796.Solution1(); + } + + @Test + public void test1() { + assertTrue(solution1.rotateString("abcde", "cdeab")); + } + + @Test + public void test2() { + assertFalse(solution1.rotateString("abcde", "abced")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_799Test.java b/src/test/java/com/fishercoder/firstthousand/_799Test.java new file mode 100644 index 0000000000..f07118cd17 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_799Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._799; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _799Test { + private _799.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _799.Solution1(); + } + + @Test + public void test1() { + assertEquals(0.125, solution1.champagneTower(8, 3, 0), 0); + } + + @Test + public void test2() { + assertEquals(0.875, solution1.champagneTower(8, 3, 1), 0); + } + + @Test + public void test3() { + assertEquals(0.875, solution1.champagneTower(8, 3, 2), 0); + } + + @Test + public void test4() { + assertEquals(0.125, solution1.champagneTower(8, 3, 3), 0); + } + + @Test + public void test5() { + assertEquals(0.0, solution1.champagneTower(1, 1, 1), 0); + } + + @Test + public void test6() { + assertEquals(0.5, solution1.champagneTower(2, 1, 1), 0); + } + + @Test + public void test7() { + assertEquals(0.0, solution1.champagneTower(1000000000, 99, 99), 0); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_79Test.java b/src/test/java/com/fishercoder/firstthousand/_79Test.java new file mode 100644 index 0000000000..e31b09e745 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_79Test.java @@ -0,0 +1,76 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._79; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _79Test { + private _79.Solution1 solution1; + private _79.Solution2 solution2; + private _79.Solution3 solution3; + private static char[][] board; + + @BeforeEach + public void setup() { + solution1 = new _79.Solution1(); + solution2 = new _79.Solution2(); + solution3 = new _79.Solution3(); + } + + @Test + public void test1() { + board = + new char[][] { + {'A', 'B', 'C', 'E'}, + {'S', 'F', 'E', 'S'}, + {'A', 'D', 'E', 'E'}, + }; + assertEquals(true, solution1.exist(board, "ABCEFSADEESE")); + } + + @Test + public void test2() { + board = + new char[][] { + {'A', 'B', 'C', 'E'}, + {'S', 'F', 'C', 'S'}, + {'A', 'D', 'E', 'E'}, + }; + assertEquals(true, solution1.exist(board, "ABCCED")); + + assertEquals(true, solution1.exist(board, "SEE")); + + assertEquals(false, solution1.exist(board, "ABCD")); + } + + @Test + public void test3() { + board = + new char[][] { + {'a'}, {'a'}, + }; + assertEquals(false, solution1.exist(board, "aaa")); + } + + @Test + public void test4() { + board = + new char[][] { + {'A', 'B', 'H', 'I'}, + {'K', 'E', 'H', 'S'}, + {'A', 'D', 'E', 'E'}, + }; + assertEquals(true, solution2.exist(board, "ABHISHEK")); + } + + @Test + public void test5() { + board = + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"A\",\"B\",\"C\",\"E\"],[\"S\",\"F\",\"C\",\"S\"],[\"A\",\"D\",\"E\",\"E\"]"); + assertEquals(true, solution3.exist(board, "ABCCED")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_7Test.java b/src/test/java/com/fishercoder/firstthousand/_7Test.java new file mode 100644 index 0000000000..79ce50ebb7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_7Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._7; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _7Test { + private _7.Solution1 solution1; + private _7.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _7.Solution1(); + solution2 = new _7.Solution2(); + } + + @Test + public void test1() { + /** its reversed number is greater than Integer.MAX_VALUE, thus return 0 */ + assertEquals(0, solution1.reverse(1534236469)); + System.out.println(Integer.MAX_VALUE); + System.out.println(1534236469); + System.out.println(Integer.MAX_VALUE - 1534236469); + } + + @Test + public void test2() { + assertEquals(0, solution2.reverse(1534236469)); + } + + @Test + public void test3() { + assertEquals(-123, solution2.reverse(-321)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_800Test.java b/src/test/java/com/fishercoder/firstthousand/_800Test.java new file mode 100644 index 0000000000..719785d311 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_800Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._800; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _800Test { + private _800.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _800.Solution1(); + } + + @Test + public void test1() { + assertEquals("#11ee66", solution1.similarRGB("#09f166")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_802Test.java b/src/test/java/com/fishercoder/firstthousand/_802Test.java new file mode 100644 index 0000000000..5d24ef4a01 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_802Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._802; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _802Test { + private _802.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _802.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList(2, 4, 5, 6), + solution1.eventualSafeNodes(new int[][] {{1, 2}, {2, 3}, {5}, {0}, {5}, {}, {}})); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList(4), + solution1.eventualSafeNodes( + new int[][] {{1, 2, 3, 4}, {1, 2}, {3, 4}, {0, 4}, {}})); + } + + @Test + public void test3() { + assertEquals( + Arrays.asList(0, 1, 2, 3, 4), + solution1.eventualSafeNodes(new int[][] {{}, {0, 2, 3, 4}, {3}, {4}, {}})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_804Test.java b/src/test/java/com/fishercoder/firstthousand/_804Test.java new file mode 100644 index 0000000000..d33109c248 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_804Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._804; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _804Test { + private _804.Solution1 solution1; + private static String[] words; + + @BeforeEach + public void setUp() { + solution1 = new _804.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"gin", "zen", "gig", "msg"}; + assertEquals(2, solution1.uniqueMorseRepresentations(words)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_806Test.java b/src/test/java/com/fishercoder/firstthousand/_806Test.java new file mode 100644 index 0000000000..899db9c61c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_806Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._806; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _806Test { + private _806.Solution1 solution1; + private static int[] widths; + + @BeforeEach + public void setUp() { + solution1 = new _806.Solution1(); + } + + @Test + public void test1() { + widths = + new int[] { + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10 + }; + assertArrayEquals( + new int[] {3, 60}, solution1.numberOfLines(widths, "abcdefghijklmnopqrstuvwxyz")); + } + + @Test + public void test2() { + widths = + new int[] { + 4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10 + }; + assertArrayEquals(new int[] {2, 4}, solution1.numberOfLines(widths, "bbbcccdddaaa")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_807Test.java b/src/test/java/com/fishercoder/firstthousand/_807Test.java new file mode 100644 index 0000000000..85b19c1172 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_807Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._807; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _807Test { + private _807.Solution1 solution1; + private static int[][] grid; + + @BeforeEach + public void setup() { + solution1 = new _807.Solution1(); + } + + @Test + public void test1() { + grid = + new int[][] { + {3, 0, 8, 4}, + {2, 4, 5, 7}, + {9, 2, 6, 3}, + {0, 3, 1, 0} + }; + assertEquals(35, solution1.maxIncreaseKeepingSkyline(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_809Test.java b/src/test/java/com/fishercoder/firstthousand/_809Test.java new file mode 100644 index 0000000000..3f309a91fa --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_809Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._809; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _809Test { + private _809.Solution1 solution1; + private String[] words; + + @BeforeEach + public void setup() { + solution1 = new _809.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"hello", "hi", "helo"}; + assertEquals(1, solution1.expressiveWords("heeellooo", words)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_80Test.java b/src/test/java/com/fishercoder/firstthousand/_80Test.java new file mode 100644 index 0000000000..04f4d7a4e3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_80Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._80; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _80Test { + private _80.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _80.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, (solution1.removeDuplicates(new int[] {1, 1, 1, 2, 2, 3}))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_811Test.java b/src/test/java/com/fishercoder/firstthousand/_811Test.java new file mode 100644 index 0000000000..5a04ae7f17 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_811Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._811; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _811Test { + private _811.Solution1 solution1; + private static String[] cpdomains; + + @BeforeEach + public void setUp() { + solution1 = new _811.Solution1(); + } + + @Test + public void test1() { + cpdomains = new String[] {"9001 discuss.leetcode.com"}; + CommonUtils.print(solution1.subdomainVisits(cpdomains)); + } + + @Test + public void test2() { + cpdomains = + new String[] { + "900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org" + }; + CommonUtils.print(solution1.subdomainVisits(cpdomains)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_812Test.java b/src/test/java/com/fishercoder/firstthousand/_812Test.java new file mode 100644 index 0000000000..4473a8ea16 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_812Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._812; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _812Test { + private _812.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _812.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 2, + solution1.largestTriangleArea( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,0],[0,1],[1,0],[0,2],[2,0]")), + 0.0000001); + } + + @Test + public void test2() { + assertEquals( + 1799.0, + solution1.largestTriangleArea( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[-35,19],[40,19],[27,-20],[35,-3],[44,20],[22,-21],[35,33],[-19,42],[11,47],[11,37]")), + 0.0000001); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_814Test.java b/src/test/java/com/fishercoder/firstthousand/_814Test.java new file mode 100644 index 0000000000..feaf961cb5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_814Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._814; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _814Test { + private _814.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _814.Solution1(); + } + + @Test + public void test1() { + TreeUtils.printBinaryTree( + solution1.pruneTree( + TreeUtils.constructBinaryTree(Arrays.asList(1, null, 0, 0, 1)))); + } + + @Test + public void test2() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 1, 0, 0, 0, 1)); + TreeUtils.printBinaryTree(root); + TreeUtils.printBinaryTree(solution1.pruneTree(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_819Test.java b/src/test/java/com/fishercoder/firstthousand/_819Test.java new file mode 100644 index 0000000000..43d9918e6a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_819Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._819; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _819Test { + private _819.Solution1 solution1; + private static String[] banned; + + @BeforeEach + public void setUp() { + solution1 = new _819.Solution1(); + } + + @Test + public void test1() { + banned = new String[] {"hit"}; + assertEquals( + "ball", + solution1.mostCommonWord( + "Bob hit a ball, the hit BALL flew far after it was hit.", banned)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_81Test.java b/src/test/java/com/fishercoder/firstthousand/_81Test.java new file mode 100644 index 0000000000..ebba202bb6 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_81Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._81; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _81Test { + private _81.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _81.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, (solution1.search(new int[] {2, 5, 6, 0, 0, 1, 2}, 0))); + } + + @Test + public void test2() { + assertEquals(false, (solution1.search(new int[] {2, 5, 6, 0, 0, 1, 2}, 3))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_821Test.java b/src/test/java/com/fishercoder/firstthousand/_821Test.java new file mode 100644 index 0000000000..68093d7123 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_821Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._821; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _821Test { + private _821.Solution1 solution1; + private _821.Solution2 solution2; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _821.Solution1(); + solution2 = new _821.Solution2(); + } + + @Test + public void test1() { + expected = new int[] {3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0}; + assertArrayEquals(expected, solution1.shortestToChar("loveleetcode", 'e')); + } + + @Test + public void test2() { + expected = new int[] {3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0}; + assertArrayEquals(expected, solution2.shortestToChar("loveleetcode", 'e')); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_823Test.java b/src/test/java/com/fishercoder/firstthousand/_823Test.java new file mode 100644 index 0000000000..ba8d7a1f7c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_823Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._823; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _823Test { + private _823.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _823.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.numFactoredBinaryTrees(new int[] {2, 4})); + } + + @Test + public void test2() { + assertEquals(9, solution1.numFactoredBinaryTrees(new int[] {2, 3, 4, 6, 9})); + } + + @Test + public void test3() { + assertEquals(25, solution1.numFactoredBinaryTrees(new int[] {2, 3, 4, 8, 16, 18})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_824Test.java b/src/test/java/com/fishercoder/firstthousand/_824Test.java new file mode 100644 index 0000000000..6f8287fe80 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_824Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._824; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _824Test { + private _824.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _824.Solution1(); + } + + @Test + public void test1() { + assertEquals( + "Imaa peaksmaaa oatGmaaaa atinLmaaaaa", + solution1.toGoatLatin("I speak Goat Latin")); + } + + @Test + public void test2() { + assertEquals( + "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa", + solution1.toGoatLatin("The quick brown fox jumped over the lazy dog")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_826Test.java b/src/test/java/com/fishercoder/firstthousand/_826Test.java new file mode 100644 index 0000000000..218d6d0ff8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_826Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._826; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _826Test { + private _826.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _826.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 100, + solution1.maxProfitAssignment( + new int[] {2, 4, 6, 8, 10}, + new int[] {10, 20, 30, 40, 50}, + new int[] {4, 5, 6, 7})); + } + + @Test + public void test2() { + assertEquals( + 324, + solution1.maxProfitAssignment( + new int[] {68, 35, 52, 47, 86}, + new int[] {67, 17, 1, 81, 3}, + new int[] {92, 10, 85, 84, 82})); + } + + @Test + public void test3() { + assertEquals( + 190, + solution1.maxProfitAssignment( + new int[] {13, 37, 58}, new int[] {4, 90, 96}, new int[] {34, 73, 45})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_82Test.java b/src/test/java/com/fishercoder/firstthousand/_82Test.java new file mode 100644 index 0000000000..ced2454626 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_82Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._82; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _82Test { + + private _82.Solution1 solution1; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _82.Solution1(); + } + + @Test + public void test1() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 3, 4, 4, 5}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 5}); + assertEquals(expected, solution1.deleteDuplicates(head)); + } + + @Test + public void test2() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 1, 1, 2, 3}); + expected = LinkedListUtils.contructLinkedList(new int[] {2, 3}); + assertEquals(expected, solution1.deleteDuplicates(head)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_830Test.java b/src/test/java/com/fishercoder/firstthousand/_830Test.java new file mode 100644 index 0000000000..70fba3a289 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_830Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._830; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _830Test { + private _830.Solution1 solution1; + private static List> expected; + + @BeforeEach + public void setUp() { + solution1 = new _830.Solution1(); + } + + @Test + public void test1() { + expected = new ArrayList<>(); + expected.add(Arrays.asList(3, 6)); + assertEquals(expected, solution1.largeGroupPositions("abbxxxxzzy")); + } + + @Test + public void test2() { + expected = new ArrayList<>(); + assertEquals(expected, solution1.largeGroupPositions("abc")); + } + + @Test + public void test3() { + expected = new ArrayList<>(); + expected.add(Arrays.asList(3, 5)); + expected.add(Arrays.asList(6, 9)); + expected.add(Arrays.asList(12, 14)); + assertEquals(expected, solution1.largeGroupPositions("abcdddeeeeaabbbcd")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_832Test.java b/src/test/java/com/fishercoder/firstthousand/_832Test.java new file mode 100644 index 0000000000..a5c9c175ec --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_832Test.java @@ -0,0 +1,54 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._832; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _832Test { + private _832.Solution1 solution1; + private static int[][] expected; + private static int[][] A; + + @BeforeEach + public void setUp() { + solution1 = new _832.Solution1(); + } + + @Test + public void test1() { + A = + new int[][] { + {1, 1, 0}, + {1, 0, 1}, + {0, 0, 0} + }; + expected = + new int[][] { + {1, 0, 0}, + {0, 1, 0}, + {1, 1, 1} + }; + assertArrayEquals(expected, solution1.flipAndInvertImage(A)); + } + + @Test + public void test2() { + A = + new int[][] { + {1, 1, 0, 0}, + {1, 0, 0, 1}, + {0, 1, 1, 1}, + {1, 0, 1, 0} + }; + expected = + new int[][] { + {1, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 1}, + {1, 0, 1, 0} + }; + assertArrayEquals(expected, solution1.flipAndInvertImage(A)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_836Test.java b/src/test/java/com/fishercoder/firstthousand/_836Test.java new file mode 100644 index 0000000000..8663135bbd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_836Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._836; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _836Test { + private _836.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _836.Solution1(); + } + + @Test + public void test1() { + assertEquals( + true, solution1.isRectangleOverlap(new int[] {0, 0, 2, 2}, new int[] {1, 1, 3, 3})); + } + + @Test + public void test2() { + assertEquals( + false, + solution1.isRectangleOverlap(new int[] {0, 0, 1, 1}, new int[] {1, 0, 2, 1})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_838Test.java b/src/test/java/com/fishercoder/firstthousand/_838Test.java new file mode 100644 index 0000000000..75890b3227 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_838Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._838; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _838Test { + private _838.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _838.Solution1(); + } + + @Test + public void test1() { + assertEquals("LL.RR.LLRRLL..", solution1.pushDominoes(".L.R...LR..L..")); + } + + @Test + public void test2() { + assertEquals("RR.L", solution1.pushDominoes("RR.L")); + } + + @Test + public void test3() { + assertEquals(".", solution1.pushDominoes(".")); + } + + @Test + public void test4() { + assertEquals("..RRR", solution1.pushDominoes("..R..")); + } + + @Test + public void test5() { + assertEquals("RRR.L", solution1.pushDominoes("R.R.L")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_83Test.java b/src/test/java/com/fishercoder/firstthousand/_83Test.java new file mode 100644 index 0000000000..400d139003 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_83Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._83; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 4/18/17. */ +public class _83Test { + + private _83.Solution1 solution1; + private _83.Solution2 solution2; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _83.Solution1(); + solution2 = new _83.Solution2(); + } + + @Test + public void test1() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 1, 2, 3, 3)); + expected = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3)); + assertEquals(expected, solution1.deleteDuplicates(head)); + } + + @Test + public void test2() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 1, 2, 3, 3)); + expected = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3)); + assertEquals(expected, solution2.deleteDuplicates(head)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_840Test.java b/src/test/java/com/fishercoder/firstthousand/_840Test.java new file mode 100644 index 0000000000..7ae5467da7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_840Test.java @@ -0,0 +1,50 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._840; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _840Test { + private _840.Solution1 test; + private static int[][] grid; + + @BeforeEach + public void setUp() { + test = new _840.Solution1(); + } + + @Test + public void test1() { + grid = + new int[][] { + {4, 3, 8, 4}, + {9, 5, 1, 9}, + {2, 7, 6, 2} + }; + assertEquals(1, test.numMagicSquaresInside(grid)); + } + + @Test + public void test2() { + grid = + new int[][] { + {5, 5, 5}, + {5, 5, 5}, + {5, 5, 5} + }; + assertEquals(0, test.numMagicSquaresInside(grid)); + } + + @Test + public void test3() { + grid = + new int[][] { + {10, 3, 5}, + {1, 6, 11}, + {7, 9, 2} + }; + assertEquals(0, test.numMagicSquaresInside(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_841Test.java b/src/test/java/com/fishercoder/firstthousand/_841Test.java new file mode 100644 index 0000000000..c837bedbb6 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_841Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._841; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _841Test { + private _841.Solution1 solution1; + private _841.Solution2 solution2; + private _841.Solution3 solution3; + private static List> rooms; + + @BeforeEach + public void setup() { + solution1 = new _841.Solution1(); + solution2 = new _841.Solution2(); + solution3 = new _841.Solution3(); + } + + @Test + public void test1() { + rooms = new ArrayList<>(); + rooms.add(Arrays.asList(1)); + rooms.add(Arrays.asList(2)); + rooms.add(Arrays.asList(3)); + rooms.add(Arrays.asList()); + assertEquals(true, solution1.canVisitAllRooms(rooms)); + assertEquals(true, solution2.canVisitAllRooms(rooms)); + assertEquals(true, solution3.canVisitAllRooms(rooms)); + } + + @Test + public void test2() { + rooms = new ArrayList<>(); + rooms.add(Arrays.asList(1, 3)); + rooms.add(Arrays.asList(3, 0, 1)); + rooms.add(Arrays.asList(2)); + rooms.add(Arrays.asList(0)); + assertEquals(false, solution1.canVisitAllRooms(rooms)); + assertEquals(false, solution2.canVisitAllRooms(rooms)); + assertEquals(false, solution3.canVisitAllRooms(rooms)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_844Test.java b/src/test/java/com/fishercoder/firstthousand/_844Test.java new file mode 100644 index 0000000000..0809c4d39e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_844Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._844; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _844Test { + private _844.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _844.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.backspaceCompare("ab#c", "ad#c")); + } + + @Test + public void test2() { + assertEquals(true, solution1.backspaceCompare("ab##", "c#d#")); + } + + @Test + public void test3() { + assertEquals(true, solution1.backspaceCompare("a##c", "#a#c")); + } + + @Test + public void test4() { + assertEquals(false, solution1.backspaceCompare("a#c", "b")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_848Test.java b/src/test/java/com/fishercoder/firstthousand/_848Test.java new file mode 100644 index 0000000000..038a4e9d1b --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_848Test.java @@ -0,0 +1,45 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._848; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _848Test { + private _848.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _848.Solution1(); + } + + @Test + public void test1() { + assertEquals("rpl", solution1.shiftingLetters("abc", new int[] {3, 5, 9})); + } + + @Test + public void test2() { + assertEquals("gfd", solution1.shiftingLetters("aaa", new int[] {1, 2, 3})); + } + + @Test + public void test3() { + assertEquals("rul", solution1.shiftingLetters("ruu", new int[] {26, 9, 17})); + } + + @Test + public void test4() { + assertEquals( + "wqqwlcjnkphhsyvrkdod", + solution1.shiftingLetters( + "mkgfzkkuxownxvfvxasy", + new int[] { + 505870226, 437526072, 266740649, 224336793, 532917782, 311122363, + 567754492, 595798950, 81520022, 684110326, 137742843, 275267355, + 856903962, 148291585, 919054234, 467541837, 622939912, 116899933, + 983296461, 536563513 + })); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_849Test.java b/src/test/java/com/fishercoder/firstthousand/_849Test.java new file mode 100644 index 0000000000..1ce1ce3c88 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_849Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._849; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _849Test { + + private _849.Solution1 solution1; + private _849.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _849.Solution1(); + solution2 = new _849.Solution2(); + } + + @Test + public void test1() { + assertEquals(2, solution1.maxDistToClosest(new int[] {1, 0, 0, 0, 1, 0, 1})); + assertEquals(2, solution2.maxDistToClosest(new int[] {1, 0, 0, 0, 1, 0, 1})); + } + + @Test + public void test2() { + assertEquals(3, solution1.maxDistToClosest(new int[] {1, 0, 0, 0})); + assertEquals(3, solution2.maxDistToClosest(new int[] {1, 0, 0, 0})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_84Test.java b/src/test/java/com/fishercoder/firstthousand/_84Test.java new file mode 100644 index 0000000000..6fa5343069 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_84Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._84; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _84Test { + private _84.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _84.Solution1(); + } + + @Test + public void test1() { + assertEquals(10, (solution1.largestRectangleArea(new int[] {2, 1, 5, 6, 2, 3}))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_851Test.java b/src/test/java/com/fishercoder/firstthousand/_851Test.java new file mode 100644 index 0000000000..8bf2d121f1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_851Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._851; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _851Test { + private _851.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _851.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {5, 5, 2, 5, 4, 5, 6, 7}, + solution1.loudAndRich( + new int[][] {{1, 0}, {2, 1}, {3, 1}, {3, 7}, {4, 3}, {5, 3}, {6, 3}}, + new int[] {3, 2, 5, 4, 6, 1, 7, 0})); + } + + @Test + public void test2() { + assertArrayEquals(new int[] {0}, solution1.loudAndRich(new int[][] {{}}, new int[] {0})); + } + + @Test + public void test3() { + assertArrayEquals( + new int[] {0, 1}, solution1.loudAndRich(new int[][] {{}}, new int[] {0, 1})); + } + + @Test + public void test4() { + assertArrayEquals( + new int[] {0, 1}, solution1.loudAndRich(new int[][] {{}}, new int[] {1, 0})); + } + + @Test + public void test5() { + assertArrayEquals( + new int[] {0, 1, 0}, + solution1.loudAndRich(new int[][] {{0, 2}, {1, 2}}, new int[] {0, 1, 2})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_852Test.java b/src/test/java/com/fishercoder/firstthousand/_852Test.java new file mode 100644 index 0000000000..84ac769454 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_852Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._852; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _852Test { + private _852.Solution1 solution1; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _852.Solution1(); + } + + @Test + public void test1() { + A = new int[] {0, 1, 0}; + assertEquals(1, solution1.peakIndexInMountainArray(A)); + } + + @Test + public void test2() { + A = new int[] {0, 2, 1, 0}; + assertEquals(1, solution1.peakIndexInMountainArray(A)); + } +} diff --git a/src/test/java/com/fishercoder/_856Test.java b/src/test/java/com/fishercoder/firstthousand/_856Test.java similarity index 77% rename from src/test/java/com/fishercoder/_856Test.java rename to src/test/java/com/fishercoder/firstthousand/_856Test.java index 46a45dea79..0bc66b4697 100644 --- a/src/test/java/com/fishercoder/_856Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_856Test.java @@ -1,18 +1,16 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._856; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._856; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -@Ignore public class _856Test { - private static _856.Solution1 solution1; + private _856.Solution1 solution1; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _856.Solution1(); } @@ -60,5 +58,4 @@ public void test8() { public void test9() { assertEquals(12, solution1.scoreOfParentheses("(()()(()()))")); } - -} \ No newline at end of file +} diff --git a/src/test/java/com/fishercoder/firstthousand/_859Test.java b/src/test/java/com/fishercoder/firstthousand/_859Test.java new file mode 100644 index 0000000000..578167fd0a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_859Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._859; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _859Test { + private _859.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _859.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.buddyStrings("ab", "ba")); + } + + @Test + public void test2() { + assertEquals(false, solution1.buddyStrings("ab", "ab")); + } + + @Test + public void test3() { + assertEquals(true, solution1.buddyStrings("aa", "aa")); + } + + @Test + public void test4() { + assertEquals(true, solution1.buddyStrings("aaaaaaabc", "aaaaaaacb")); + } + + @Test + public void test5() { + assertEquals(false, solution1.buddyStrings("", "aa")); + } + + @Test + public void test6() { + assertEquals(true, solution1.buddyStrings("aaa", "aaa")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_85Test.java b/src/test/java/com/fishercoder/firstthousand/_85Test.java new file mode 100644 index 0000000000..02fa63d5db --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_85Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._85; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _85Test { + private _85.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _85.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 6, + (solution1.maximalRectangle( + new char[][] { + {'1', '0', '1', '0', '0'}, + {'1', '0', '1', '1', '1'}, + {'1', '1', '1', '1', '1'}, + {'1', '0', '0', '1', '0'} + }))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_860Test.java b/src/test/java/com/fishercoder/firstthousand/_860Test.java new file mode 100644 index 0000000000..b816ccdb32 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_860Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._860; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _860Test { + + private _860.Solution1 test; + private static int[] bills; + + @BeforeEach + public void setUp() { + test = new _860.Solution1(); + } + + @Test + public void test1() { + bills = new int[] {5, 5, 5, 10, 20}; + assertEquals(true, test.lemonadeChange(bills)); + } + + @Test + public void test2() { + bills = new int[] {5, 5, 10}; + assertEquals(true, test.lemonadeChange(bills)); + } + + @Test + public void test3() { + bills = new int[] {10, 10}; + assertEquals(false, test.lemonadeChange(bills)); + } + + @Test + public void test4() { + bills = new int[] {5, 5, 10, 10, 20}; + assertEquals(false, test.lemonadeChange(bills)); + } + + @Test + public void test5() { + bills = new int[] {5, 5, 5, 20, 5, 5, 5, 10, 20, 5, 10, 20, 5, 20, 5, 10, 5, 5, 5, 5}; + assertEquals(false, test.lemonadeChange(bills)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_861Test.java b/src/test/java/com/fishercoder/firstthousand/_861Test.java new file mode 100644 index 0000000000..cbaa8d00b1 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_861Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._861; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _861Test { + private _861.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _861.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 39, + solution1.matrixScore( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,0,1,1],[1,0,1,0],[1,1,0,0]"))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_867Test.java b/src/test/java/com/fishercoder/firstthousand/_867Test.java new file mode 100644 index 0000000000..a11af6e709 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_867Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._867; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _867Test { + private _867.Solution1 solution1; + private static int[][] A; + private static int[][] result; + + @BeforeEach + public void setup() { + solution1 = new _867.Solution1(); + } + + @Test + public void test1() { + A = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + result = + new int[][] { + {1, 4, 7}, + {2, 5, 8}, + {3, 6, 9} + }; + assertArrayEquals(result, solution1.transpose(A)); + } + + @Test + public void test2() { + A = new int[][] {{1, 2, 3}}; + result = new int[][] {{1}, {2}, {3}}; + assertArrayEquals(result, solution1.transpose(A)); + } + + @Test + public void test3() { + A = new int[][] {{1}, {2}, {3}}; + result = new int[][] {{1, 2, 3}}; + assertArrayEquals(result, solution1.transpose(A)); + } + + @Test + public void test4() { + A = + new int[][] { + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12} + }; + result = + new int[][] { + {1, 5, 9}, + {2, 6, 10}, + {3, 7, 11}, + {4, 8, 12} + }; + assertArrayEquals(result, solution1.transpose(A)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_868Test.java b/src/test/java/com/fishercoder/firstthousand/_868Test.java new file mode 100644 index 0000000000..a12729b799 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_868Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._868; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _868Test { + private _868.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _868.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.binaryGap(22)); + } + + @Test + public void test2() { + assertEquals(2, solution1.binaryGap(5)); + } + + @Test + public void test3() { + assertEquals(1, solution1.binaryGap(6)); + } + + @Test + public void test4() { + assertEquals(0, solution1.binaryGap(8)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_86Test.java b/src/test/java/com/fishercoder/firstthousand/_86Test.java new file mode 100644 index 0000000000..d6489efe56 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_86Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._86; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _86Test { + private _86.Solution1 solution1; + private _86.Solution2 solution2; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _86.Solution1(); + solution2 = new _86.Solution2(); + } + + @Test + public void test1() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 4, 3, 2, 5, 2)); + expected = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 2, 4, 3, 5)); + assertEquals(expected, (solution1.partition(head, 3))); + } + + @Test + public void test2() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 4, 3, 2, 5, 2)); + expected = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 2, 4, 3, 5)); + assertEquals(expected, (solution2.partition(head, 3))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_870Test.java b/src/test/java/com/fishercoder/firstthousand/_870Test.java new file mode 100644 index 0000000000..a5f211ca4c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_870Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._870; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _870Test { + private _870.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _870.Solution1(); + } + + @Test + public void test1() { + CommonUtils.printArray( + solution1.advantageCount(new int[] {2, 7, 11, 15}, new int[] {1, 10, 4, 11})); + } + + @Test + public void test2() { + CommonUtils.printArray( + solution1.advantageCount(new int[] {12, 24, 8, 32}, new int[] {13, 25, 32, 11})); + } + + @Test + public void test3() { + CommonUtils.printArray( + solution1.advantageCount( + new int[] {15, 15, 4, 5, 0, 1, 7, 10, 3, 1, 10, 10, 8, 2, 3}, + new int[] {4, 13, 14, 0, 14, 14, 12, 3, 15, 12, 2, 0, 6, 9, 0})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_872Test.java b/src/test/java/com/fishercoder/firstthousand/_872Test.java new file mode 100644 index 0000000000..d215b8e6fc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_872Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._872; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _872Test { + private _872.Solution1 solution1; + private static TreeNode root1; + private static TreeNode root2; + + @BeforeEach + public void setUp() { + solution1 = new _872.Solution1(); + } + + @Test + public void test1() { + root1 = TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 6, 2, 7, 4, 1, 9, 8)); + root2 = TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 6, 2, 7, 4, 1, 9, 8)); + TreeUtils.printBinaryTree(root1); + TreeUtils.printBinaryTree(root2); + assertEquals(true, solution1.leafSimilar(root1, root2)); + } + + @Test + public void test2() { + root1 = + TreeUtils.constructBinaryTree( + Arrays.asList(18, 35, 22, null, 103, 43, 101, 58, null, 97)); + TreeUtils.printBinaryTree(root1); + root2 = + TreeUtils.constructBinaryTree( + Arrays.asList(94, 102, 17, 122, null, null, 54, 58, 101, 97)); + TreeUtils.printBinaryTree(root2); + assertEquals(false, solution1.leafSimilar(root1, root2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_875Test.java b/src/test/java/com/fishercoder/firstthousand/_875Test.java new file mode 100644 index 0000000000..ce8ca216aa --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_875Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._875; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _875Test { + private _875.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _875.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.minEatingSpeed(new int[] {3, 6, 7, 11}, 8)); + } + + @Test + public void test2() { + assertEquals(30, solution1.minEatingSpeed(new int[] {30, 11, 23, 4, 20}, 5)); + } + + @Test + public void test3() { + assertEquals(23, solution1.minEatingSpeed(new int[] {30, 11, 23, 4, 20}, 6)); + } + + @Test + public void test4() { + assertEquals(2, solution1.minEatingSpeed(new int[] {2, 2}, 2)); + } + + @Test + public void test5() { + assertEquals(2, solution1.minEatingSpeed(new int[] {312884470}, 312884469)); + } + + @Test + public void test6() { + assertEquals(500000000, solution1.minEatingSpeed(new int[] {1000000000}, 2)); + } + + @Test + public void test7() { + assertEquals(1, solution1.minEatingSpeed(new int[] {312884470}, 968709470)); + } + + @Test + public void test8() { + assertEquals( + 3, + solution1.minEatingSpeed(new int[] {805306368, 805306368, 805306368}, 1000000000)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_876Test.java b/src/test/java/com/fishercoder/firstthousand/_876Test.java new file mode 100644 index 0000000000..87953bfcb8 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_876Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._876; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _876Test { + private _876.Solution1 solution1; + private static ListNode head; + private static ListNode middle; + + @BeforeEach + public void setup() { + solution1 = new _876.Solution1(); + } + + @Test + public void test1() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5)); + middle = solution1.middleNode(head); + assertEquals(middle, LinkedListUtils.createSinglyLinkedList(Arrays.asList(3, 4, 5))); + } + + @Test + public void test2() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5, 6)); + middle = solution1.middleNode(head); + assertEquals(middle, LinkedListUtils.createSinglyLinkedList(Arrays.asList(4, 5, 6))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_877Test.java b/src/test/java/com/fishercoder/firstthousand/_877Test.java new file mode 100644 index 0000000000..fa208cc02e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_877Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._877; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _877Test { + private _877.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _877.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.stoneGame(new int[] {5, 3, 4, 5})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_87Test.java b/src/test/java/com/fishercoder/firstthousand/_87Test.java new file mode 100644 index 0000000000..2dca0e4478 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_87Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._87; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _87Test { + private _87.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _87.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isScramble("great", "rgeat")); + } + + @Test + public void test2() { + assertEquals(true, solution1.isScramble("great", "rgtae")); + } + + @Test + public void test3() { + assertEquals(true, solution1.isScramble("abc", "bca")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_880Test.java b/src/test/java/com/fishercoder/firstthousand/_880Test.java new file mode 100644 index 0000000000..ac2a11dd37 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_880Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._880; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +@Disabled +public class _880Test { + private _880.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _880.Solution1(); + } + + @Test + public void test1() { + assertEquals("o", solution1.decodeAtIndex("leet2code3", 10)); + } + + @Test + public void test2() { + assertEquals("h", solution1.decodeAtIndex("ha22", 5)); + } + + @Test + public void test3() { + assertEquals("a", solution1.decodeAtIndex("a2345678999999999999999", 1)); + } + + @Test + public void test4() { + assertEquals("a", solution1.decodeAtIndex("abc", 1)); + } + + @Test + public void test5() { + assertEquals("a", solution1.decodeAtIndex("a2b3c4d5e6f7g8h9", 10)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_881Test.java b/src/test/java/com/fishercoder/firstthousand/_881Test.java new file mode 100644 index 0000000000..26a05c97b4 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_881Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._881; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _881Test { + private _881.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _881.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.numRescueBoats(new int[] {1, 2}, 3)); + } + + @Test + public void test2() { + assertEquals(3, solution1.numRescueBoats(new int[] {3, 2, 2, 1}, 3)); + } + + @Test + public void test3() { + assertEquals(4, solution1.numRescueBoats(new int[] {3, 5, 3, 4}, 5)); + } + + @Test + public void test4() { + assertEquals(2, solution1.numRescueBoats(new int[] {2, 4}, 5)); + } + + @Test + public void test5() { + assertEquals( + 29, + solution1.numRescueBoats( + new int[] { + 4, 9, 3, 1, 1, 7, 6, 10, 10, 10, 1, 8, 8, 7, 8, 10, 7, 4, 6, 3, 6, 1, 2, + 4, 8, 8, 4, 7, 1, 2, 10, 3, 4, 6, 3, 5, 3, 1, 2, 6, 1, 5, 4, 5, 1, 10, + 5, 9, 10, 4 + }, + 10)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_883Test.java b/src/test/java/com/fishercoder/firstthousand/_883Test.java new file mode 100644 index 0000000000..7564c45502 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_883Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._883; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _883Test { + private _883.Solution1 solution1; + private static int[][] grid; + + @BeforeEach + public void setup() { + solution1 = new _883.Solution1(); + } + + @Test + public void test1() { + grid = new int[][] {{2}}; + assertEquals(5, solution1.projectionArea(grid)); + } + + @Test + public void test2() { + grid = + new int[][] { + {1, 2}, + {3, 4}, + }; + assertEquals(17, solution1.projectionArea(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_884Test.java b/src/test/java/com/fishercoder/firstthousand/_884Test.java new file mode 100644 index 0000000000..fb8e062e59 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_884Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._884; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _884Test { + private _884.Solution1 solution1; + private static String[] expected; + + @BeforeEach + public void setUp() { + solution1 = new _884.Solution1(); + } + + @Test + public void test1() { + expected = new String[] {"sweet", "sour"}; + assertArrayEquals( + expected, + solution1.uncommonFromSentences("this apple is sweet", "this apple is sour")); + } + + @Test + public void test2() { + expected = new String[] {"banana"}; + assertArrayEquals(expected, solution1.uncommonFromSentences("apple apple", "banana")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_885Test.java b/src/test/java/com/fishercoder/firstthousand/_885Test.java new file mode 100644 index 0000000000..3c0a1801b5 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_885Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._885; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _885Test { + private _885.Solution1 solution1; + private static int[][] expected; + + @BeforeEach + public void setup() { + solution1 = new _885.Solution1(); + } + + @Test + public void test1() { + expected = + new int[][] { + {0, 0}, + {0, 1}, + {0, 2}, + {0, 3} + }; + assertArrayEquals(expected, solution1.spiralMatrixIII(1, 4, 0, 0)); + } + + @Test + public void test2() { + expected = + new int[][] { + {1, 4}, + {1, 5}, + {2, 5}, + {2, 4}, + {2, 3}, + {1, 3}, + {0, 3}, + {0, 4}, + {0, 5}, + {3, 5}, + {3, 4}, + {3, 3}, + {3, 2}, + {2, 2}, + {1, 2}, + {0, 2}, + {4, 5}, + {4, 4}, + {4, 3}, + {4, 2}, + {4, 1}, + {3, 1}, + {2, 1}, + {1, 1}, + {0, 1}, + {4, 0}, + {3, 0}, + {2, 0}, + {1, 0}, + {0, 0} + }; + assertArrayEquals(expected, solution1.spiralMatrixIII(5, 6, 1, 4)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_888Test.java b/src/test/java/com/fishercoder/firstthousand/_888Test.java new file mode 100644 index 0000000000..b02460b555 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_888Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._888; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _888Test { + private _888.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _888.Solution1(); + } + + @Test + public void test1() { + int[] A = {1, 1}; + int[] B = {2, 2}; + int[] ans = {1, 2}; + assertArrayEquals(ans, solution1.fairCandySwap(A, B)); + } + + @Test + public void test2() { + int[] A = {1, 2}; + int[] B = {2, 3}; + int[] ans = {1, 2}; + assertArrayEquals(ans, solution1.fairCandySwap(A, B)); + } + + @Test + public void test3() { + int[] A = {2}; + int[] B = {1, 3}; + int[] ans = {2, 3}; + assertArrayEquals(ans, solution1.fairCandySwap(A, B)); + } + + @Test + public void test4() { + int[] A = {1, 2, 5}; + int[] B = {2, 4}; + int[] ans = {5, 4}; + assertArrayEquals(ans, solution1.fairCandySwap(A, B)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_88Test.java b/src/test/java/com/fishercoder/firstthousand/_88Test.java new file mode 100644 index 0000000000..a281d812a2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_88Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._88; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _88Test { + + private _88.Solution1 solution1; + private int[] nums1; + private int[] nums2; + private int[] expected; + + @BeforeEach + public void setUp() { + solution1 = new _88.Solution1(); + } + + @Test + public void test1() { + nums1 = new int[] {2, 0}; + nums2 = new int[] {1}; + expected = new int[] {1, 2}; + solution1.merge(nums1, 1, nums2, 1); + assertArrayEquals(expected, nums1); + } + + @Test + public void test2() { + nums1 = new int[] {4, 5, 6, 0, 0, 0}; + nums2 = new int[] {1, 2, 3}; + expected = new int[] {1, 2, 3, 4, 5, 6}; + solution1.merge(nums1, 3, nums2, 3); + assertArrayEquals(expected, nums1); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_890Test.java b/src/test/java/com/fishercoder/firstthousand/_890Test.java new file mode 100644 index 0000000000..bd400789fc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_890Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._890; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _890Test { + private _890.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _890.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("mee", "aqq"), + solution1.findAndReplacePattern( + new String[] {"abc", "deq", "mee", "aqq", "dkd", "ccc"}, "abb")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_892Test.java b/src/test/java/com/fishercoder/firstthousand/_892Test.java new file mode 100644 index 0000000000..1607811456 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_892Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._892; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _892Test { + private _892.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _892.Solution1(); + } + + @Test + public void test1() { + assertEquals(10, solution1.surfaceArea(new int[][] {{2}})); + } + + @Test + public void test2() { + assertEquals( + 34, + solution1.surfaceArea( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2],[3,4]"))); + } + + @Test + public void test3() { + assertEquals( + 16, + solution1.surfaceArea( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,0],[0,2]"))); + } + + @Test + public void test4() { + assertEquals( + 32, + solution1.surfaceArea( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,1,1],[1,0,1],[1,1,1]"))); + } + + @Test + public void test5() { + assertEquals( + 46, + solution1.surfaceArea( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[2,2,2],[2,1,2],[2,2,2]"))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_893Test.java b/src/test/java/com/fishercoder/firstthousand/_893Test.java new file mode 100644 index 0000000000..f1acfaa10e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_893Test.java @@ -0,0 +1,50 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._893; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _893Test { + private _893.Solution1 solution1; + private _893.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _893.Solution1(); + solution2 = new _893.Solution2(); + } + + @Test + public void test1() { + assertEquals( + 3, + solution1.numSpecialEquivGroups( + new String[] {"abcd", "cdab", "cbad", "xyzz", "zzxy", "zzyx"})); + assertEquals( + 3, + solution2.numSpecialEquivGroups( + new String[] {"abcd", "cdab", "cbad", "xyzz", "zzxy", "zzyx"})); + } + + @Test + public void test2() { + assertEquals( + 3, + solution1.numSpecialEquivGroups( + new String[] {"abc", "acb", "bac", "bca", "cab", "cba"})); + assertEquals( + 3, + solution2.numSpecialEquivGroups( + new String[] {"abc", "acb", "bac", "bca", "cab", "cba"})); + } + + @Test + public void test3() { + assertEquals( + 1, solution1.numSpecialEquivGroups(new String[] {"abcd", "cdab", "adcb", "cbad"})); + assertEquals( + 1, solution2.numSpecialEquivGroups(new String[] {"abcd", "cdab", "adcb", "cbad"})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_895Test.java b/src/test/java/com/fishercoder/firstthousand/_895Test.java new file mode 100644 index 0000000000..bfde964c6d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_895Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._895; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _895Test { + + private _895.Solution1.FreqStack freqStack; + + @BeforeEach + public void setup() { + freqStack = new _895.Solution1.FreqStack(); + } + + @Test + public void test1() { + freqStack.push(5); + freqStack.push(7); + freqStack.push(5); + freqStack.push(7); + freqStack.push(4); + freqStack.push(5); + assertEquals(5, freqStack.pop()); + assertEquals(7, freqStack.pop()); + assertEquals(5, freqStack.pop()); + assertEquals(4, freqStack.pop()); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_896Test.java b/src/test/java/com/fishercoder/firstthousand/_896Test.java new file mode 100644 index 0000000000..45ca7c766d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_896Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._896; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _896Test { + private _896.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _896.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 3, 2}; + assertEquals(false, solution1.isMonotonic(nums)); + } + + @Test + public void test2() { + nums = new int[] {6, 5, 4, 4}; + assertEquals(true, solution1.isMonotonic(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_897Test.java b/src/test/java/com/fishercoder/firstthousand/_897Test.java new file mode 100644 index 0000000000..769f268cde --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_897Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._897; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _897Test { + private _897.Solution1 solution1; + private static TreeNode root; + private static TreeNode actual; + + @BeforeEach + public void setup() { + solution1 = new _897.Solution1(); + } + + @Test + public void test1() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList(5, 3, 6, 2, 4, null, 8, 1, null, null, null, 7, 9)); + TreeUtils.printBinaryTree(root); + actual = solution1.increasingBST(root); + TreeUtils.printBinaryTree(actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_89Test.java b/src/test/java/com/fishercoder/firstthousand/_89Test.java new file mode 100644 index 0000000000..af5721b649 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_89Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._89; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _89Test { + + private _89.Solution1 solution1; + private _89.Solution2 solution2; + + @BeforeEach + public void setUp() { + solution1 = new _89.Solution1(); + solution2 = new _89.Solution2(); + } + + @Test + public void test1() { + assertEquals(Arrays.asList(0, 1, 3, 2, 6, 7, 5, 4), solution1.grayCode(3)); + assertEquals(Arrays.asList(0, 1, 3, 2, 6, 7, 5, 4), solution2.grayCode(3)); + } +} diff --git a/src/test/java/com/fishercoder/_8Test.java b/src/test/java/com/fishercoder/firstthousand/_8Test.java similarity index 76% rename from src/test/java/com/fishercoder/_8Test.java rename to src/test/java/com/fishercoder/firstthousand/_8Test.java index 176682ff8f..fc9ca51be0 100644 --- a/src/test/java/com/fishercoder/_8Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_8Test.java @@ -1,16 +1,16 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; -import com.fishercoder.solutions._8; -import org.junit.BeforeClass; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; +import com.fishercoder.solutions.firstthousand._8; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _8Test { - private static _8.Solution1 solution1; + private _8.Solution1 solution1; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _8.Solution1(); } @@ -58,5 +58,4 @@ public void test8() { public void test9() { assertEquals(2147483647, solution1.myAtoi("9223372036854775809")); } - -} \ No newline at end of file +} diff --git a/src/test/java/com/fishercoder/firstthousand/_900Test.java b/src/test/java/com/fishercoder/firstthousand/_900Test.java new file mode 100644 index 0000000000..a15f139a19 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_900Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._900; +import org.junit.jupiter.api.Test; + +public class _900Test { + private _900.Solution1.RLEIterator rleIterator; + + @Test + public void test1() { + rleIterator = new _900.Solution1.RLEIterator(new int[] {3, 8, 0, 9, 2, 5}); + assertEquals(8, rleIterator.next(2)); + assertEquals(8, rleIterator.next(1)); + assertEquals(5, rleIterator.next(1)); + assertEquals(-1, rleIterator.next(2)); + } + + @Test + public void test2() { + rleIterator = + new _900.Solution1.RLEIterator( + new int[] {811, 903, 310, 730, 899, 684, 472, 100, 434, 611}); + assertEquals(903, rleIterator.next(358)); + assertEquals(903, rleIterator.next(345)); + assertEquals(730, rleIterator.next(154)); + assertEquals(684, rleIterator.next(265)); + assertEquals(684, rleIterator.next(73)); + assertEquals(684, rleIterator.next(220)); + assertEquals(684, rleIterator.next(138)); + assertEquals(684, rleIterator.next(4)); + assertEquals(684, rleIterator.next(170)); + assertEquals(684, rleIterator.next(88)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_901Test.java b/src/test/java/com/fishercoder/firstthousand/_901Test.java new file mode 100644 index 0000000000..b3b9d4728d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_901Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._901; +import org.junit.jupiter.api.Test; + +public class _901Test { + private _901.Solution1.StockSpanner stockSpanner; + + @Test + public void test1() { + stockSpanner = new _901.Solution1.StockSpanner(); + assertEquals(1, stockSpanner.next(100)); + assertEquals(1, stockSpanner.next(80)); + assertEquals(1, stockSpanner.next(60)); + assertEquals(2, stockSpanner.next(70)); + assertEquals(1, stockSpanner.next(60)); + assertEquals(4, stockSpanner.next(75)); + assertEquals(6, stockSpanner.next(85)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_904Test.java b/src/test/java/com/fishercoder/firstthousand/_904Test.java new file mode 100644 index 0000000000..fcbceb50f7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_904Test.java @@ -0,0 +1,53 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._904; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _904Test { + private _904.Solution1 solution1; + private static int[] fruits; + + @BeforeEach + public void setup() { + solution1 = new _904.Solution1(); + } + + @Test + public void test1() { + fruits = new int[] {1, 2, 1}; + assertEquals(3, solution1.totalFruit(fruits)); + } + + @Test + public void test2() { + fruits = new int[] {0, 1, 2, 2}; + assertEquals(3, solution1.totalFruit(fruits)); + } + + @Test + public void test3() { + fruits = new int[] {1, 2, 3, 2, 2}; + assertEquals(4, solution1.totalFruit(fruits)); + } + + @Test + public void test4() { + fruits = new int[] {3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4}; + assertEquals(5, solution1.totalFruit(fruits)); + } + + @Test + public void test5() { + fruits = new int[] {0, 1, 6, 6, 4, 4, 6}; + assertEquals(5, solution1.totalFruit(fruits)); + } + + @Test + public void test6() { + fruits = new int[] {0, 1, 1, 4, 3}; + assertEquals(3, solution1.totalFruit(fruits)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_905Test.java b/src/test/java/com/fishercoder/firstthousand/_905Test.java new file mode 100644 index 0000000000..31be843cef --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_905Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._905; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _905Test { + private _905.Solution1 solution1; + private static int[] A; + private static int[] actual; + + @BeforeEach + public void setup() { + solution1 = new _905.Solution1(); + } + + @Test + public void test1() { + A = new int[] {3, 1, 2, 4}; + actual = solution1.sortArrayByParity(A); + CommonUtils.printArray(actual); + } + + @Test + public void test2() { + A = new int[] {1, 3}; + actual = solution1.sortArrayByParity(A); + CommonUtils.printArray(actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_908Test.java b/src/test/java/com/fishercoder/firstthousand/_908Test.java new file mode 100644 index 0000000000..5bb552021c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_908Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._908; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _908Test { + private _908.Solution1 solution1; + private _908.Solution2 solution2; + private static int[] A; + + @BeforeEach + public void setUp() { + solution1 = new _908.Solution1(); + solution2 = new _908.Solution2(); + } + + @Test + public void test1() { + A = new int[] {1}; + assertEquals(0, solution1.smallestRangeI(A, 0)); + assertEquals(0, solution2.smallestRangeI(A, 0)); + } + + @Test + public void test2() { + A = new int[] {0, 10}; + assertEquals(6, solution1.smallestRangeI(A, 2)); + assertEquals(6, solution2.smallestRangeI(A, 2)); + } + + @Test + public void test3() { + A = new int[] {1, 3, 6}; + assertEquals(0, solution1.smallestRangeI(A, 3)); + assertEquals(0, solution2.smallestRangeI(A, 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_90Test.java b/src/test/java/com/fishercoder/firstthousand/_90Test.java new file mode 100644 index 0000000000..48f714ad6c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_90Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._90; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _90Test { + private _90.Solution1 solution1; + private _90.Solution2 solution2; + private _90.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _90.Solution1(); + solution2 = new _90.Solution2(); + solution3 = new _90.Solution3(); + } + + @Test + public void test1() { + CommonUtils.printListList(solution1.subsetsWithDup(new int[] {1, 2, 2})); + } + + @Test + public void test2() { + CommonUtils.printListList(solution2.subsetsWithDup(new int[] {1, 2, 2})); + } + + @Test + public void test3() { + CommonUtils.printListList(solution3.subsetsWithDup(new int[] {1, 2, 2})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_914Test.java b/src/test/java/com/fishercoder/firstthousand/_914Test.java new file mode 100644 index 0000000000..7fabf6bb80 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_914Test.java @@ -0,0 +1,53 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._914; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _914Test { + private _914.Solution1 solution1; + private int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _914.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {1}; + assertEquals(false, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test2() { + arr = new int[] {1, 1}; + assertEquals(true, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test3() { + arr = new int[] {1, 1, 1, 1, 2, 2, 2, 2, 2, 2}; + assertEquals(true, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test4() { + arr = new int[] {1, 1, 1, 2, 2, 2, 3, 3}; + assertEquals(false, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test5() { + arr = new int[] {0, 0, 1, 1, 1, 1, 2, 2, 3, 4}; + assertEquals(false, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test6() { + arr = new int[] {1, 2, 3, 4, 4, 3, 2, 1}; + assertEquals(true, solution1.hasGroupsSizeX(arr)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_917Test.java b/src/test/java/com/fishercoder/firstthousand/_917Test.java new file mode 100644 index 0000000000..fe9499c9dd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_917Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._917; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _917Test { + private _917.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _917.Solution1(); + } + + @Test + public void test1() { + assertEquals("dc-ba", solution1.reverseOnlyLetters("ab-cd")); + } + + @Test + public void test2() { + assertEquals("j-Ih-gfE-dCba", solution1.reverseOnlyLetters("a-bC-dEf-ghIj")); + } + + @Test + public void test3() { + assertEquals("Qedo1ct-eeLg=ntse-T!", solution1.reverseOnlyLetters("Test1ng-Leet=code-Q!")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_918Test.java b/src/test/java/com/fishercoder/firstthousand/_918Test.java new file mode 100644 index 0000000000..5b8cbe789e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_918Test.java @@ -0,0 +1,60 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._918; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _918Test { + private _918.Solution1 solution1; + private _918.Solution2 solution2; + private _918.Solution3 solution3; + + @BeforeEach + public void setup() { + solution1 = new _918.Solution1(); + solution2 = new _918.Solution2(); + solution3 = new _918.Solution3(); + } + + @Test + public void test1() { + assertEquals(3, solution1.maxSubarraySumCircular(new int[] {1, -2, 3, -2})); + } + + @Test + public void test2() { + assertEquals(10, solution1.maxSubarraySumCircular(new int[] {5, -3, 5})); + } + + @Test + public void test3() { + assertEquals(4, solution1.maxSubarraySumCircular(new int[] {3, -1, 2, -1})); + } + + @Test + public void test4() { + assertEquals(3, solution1.maxSubarraySumCircular(new int[] {3, -2, 2, -3})); + } + + @Test + public void test5() { + assertEquals(-1, solution1.maxSubarraySumCircular(new int[] {-2, -3, -1})); + } + + @Test + public void test6() { + assertEquals(10, solution1.maxSubarraySumCircular(new int[] {5, -3, 5})); + } + + @Test + public void test7() { + assertEquals(10, solution2.maxSubarraySumCircular(new int[] {5, -3, 5})); + } + + @Test + public void test8() { + assertEquals(10, solution3.maxSubarraySumCircular(new int[] {5, -3, 5})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_919Test.java b/src/test/java/com/fishercoder/firstthousand/_919Test.java new file mode 100644 index 0000000000..a16e4c16a7 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_919Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._919; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _919Test { + private _919.Solution1.CBTInserter cbtInserter; + + @BeforeEach + public void setup() {} + + @Test + public void test1() { + TreeNode root = + TreeUtils.constructBinaryTree(Arrays.asList(5, 14, 4, 5, 14, 16, 16, 20, 7, 13)); + cbtInserter = new _919.Solution1.CBTInserter(root); + TreeUtils.printBinaryTree(cbtInserter.get_root()); + assertEquals(14, cbtInserter.insert(8)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_91Test.java b/src/test/java/com/fishercoder/firstthousand/_91Test.java new file mode 100644 index 0000000000..890492fec2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_91Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._91; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _91Test { + private _91.Solution1 solution1; + private _91.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _91.Solution1(); + solution2 = new _91.Solution2(); + } + + @Test + public void test1() { + assertEquals(2, solution1.numDecodings("12")); + assertEquals(2, solution2.numDecodings("12")); + } + + @Test + public void test2() { + assertEquals(1, solution1.numDecodings("10")); + assertEquals(1, solution2.numDecodings("10")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_921Test.java b/src/test/java/com/fishercoder/firstthousand/_921Test.java new file mode 100644 index 0000000000..ea8214dcdd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_921Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._921; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _921Test { + private _921.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _921.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minAddToMakeValid("())")); + } + + @Test + public void test2() { + assertEquals(3, solution1.minAddToMakeValid("(((")); + } + + @Test + public void test3() { + assertEquals(0, solution1.minAddToMakeValid("()")); + } + + @Test + public void test4() { + assertEquals(4, solution1.minAddToMakeValid("()))((")); + } + + @Test + public void test5() { + assertEquals(1, solution1.minAddToMakeValid(")()")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_922Test.java b/src/test/java/com/fishercoder/firstthousand/_922Test.java new file mode 100644 index 0000000000..64a45eb4ba --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_922Test.java @@ -0,0 +1,76 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._922; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _922Test { + private _922.Solution1 solution1; + private _922.Solution2 solution2; + private _922.Solution3 solution3; + private static int[] nums; + private static int[] result; + + @BeforeEach + public void setup() { + solution1 = new _922.Solution1(); + solution2 = new _922.Solution2(); + solution3 = new _922.Solution3(); + } + + @Test + public void test1() { + nums = new int[] {4, 2, 5, 7}; + result = solution1.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution2.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution3.sortArrayByParityII(nums); + CommonUtils.printArray(result); + } + + @Test + public void test2() { + nums = new int[] {3, 1, 4, 2}; + result = solution1.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution2.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution3.sortArrayByParityII(nums); + CommonUtils.printArray(result); + } + + @Test + public void test3() { + nums = new int[] {648, 831, 560, 986, 192, 424, 997, 829, 897, 843}; + result = solution1.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution2.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution3.sortArrayByParityII(nums); + CommonUtils.printArray(result); + } + + @Test + public void test4() { + nums = new int[] {3, 4}; + result = solution1.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution2.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution3.sortArrayByParityII(nums); + CommonUtils.printArray(result); + } + + @Test + public void test5() { + nums = new int[] {2, 3, 1, 1, 4, 0, 0, 4, 3, 3}; + result = solution1.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution2.sortArrayByParityII(nums); + CommonUtils.printArray(result); + result = solution3.sortArrayByParityII(nums); + CommonUtils.printArray(result); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_923Test.java b/src/test/java/com/fishercoder/firstthousand/_923Test.java new file mode 100644 index 0000000000..ba49b079c9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_923Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._923; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _923Test { + private _923.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _923.Solution1(); + } + + @Test + public void test1() { + assertEquals(20, solution1.threeSumMulti(new int[] {1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, 8)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_925Test.java b/src/test/java/com/fishercoder/firstthousand/_925Test.java new file mode 100644 index 0000000000..c3aca7a2db --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_925Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._925; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _925Test { + private _925.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _925.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isLongPressedName("alex", "aaleex")); + } + + @Test + public void test2() { + assertEquals(false, solution1.isLongPressedName("saeed", "ssaaedd")); + } + + @Test + public void test3() { + assertEquals(true, solution1.isLongPressedName("leelee", "lleeelee")); + } + + @Test + public void test4() { + assertEquals(true, solution1.isLongPressedName("laiden", "laiden")); + } + + @Test + public void test5() { + assertEquals(false, solution1.isLongPressedName("pyplrz", "ppyypllr")); + } + + @Test + public void test6() { + assertEquals(true, solution1.isLongPressedName("leelee", "lleeelee")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_929Test.java b/src/test/java/com/fishercoder/firstthousand/_929Test.java new file mode 100644 index 0000000000..0174391971 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_929Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._929; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _929Test { + private _929.Solution1 solution1; + private static String[] emails; + + @BeforeEach + public void setup() { + solution1 = new _929.Solution1(); + } + + @Test + public void test1() { + emails = + new String[] { + "test.email+alex@leetcode.com", + "test.e.mail+bob.cathy@leetcode.com", + "testemail+david@lee.tcode.com" + }; + assertEquals(2, solution1.numUniqueEmails(emails)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_92Test.java b/src/test/java/com/fishercoder/firstthousand/_92Test.java new file mode 100644 index 0000000000..44caeb9372 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_92Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.firstthousand._92; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _92Test { + private _92.Solution1 solution1; + private _92.Solution2 solution2; + private static ListNode head; + private static ListNode expected; + + @BeforeEach + public void setup() { + solution1 = new _92.Solution1(); + solution2 = new _92.Solution2(); + } + + @Test + public void test1() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5)); + expected = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 4, 3, 2, 5)); + assertEquals(expected, solution1.reverseBetween(head, 2, 4)); + + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5)); + assertEquals(expected, solution2.reverseBetween(head, 2, 4)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_931Test.java b/src/test/java/com/fishercoder/firstthousand/_931Test.java new file mode 100644 index 0000000000..3aae6a079d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_931Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._931; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _931Test { + private _931.Solution1 solution1; + private static int[][] A; + + @BeforeEach + public void setup() { + solution1 = new _931.Solution1(); + } + + @Test + public void test1() { + A = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + assertEquals(12, solution1.minFallingPathSum(A)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_933Test.java b/src/test/java/com/fishercoder/firstthousand/_933Test.java new file mode 100644 index 0000000000..3a4a01fa51 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_933Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._933; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _933Test { + private _933.Solution1.RecentCounter solution1; + + @BeforeEach + public void setup() { + solution1 = new _933.Solution1.RecentCounter(); + } + + @Test + public void test1() { + assertEquals(1, solution1.ping(1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_934Test.java b/src/test/java/com/fishercoder/firstthousand/_934Test.java new file mode 100644 index 0000000000..bf9e03cdae --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_934Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._934; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _934Test { + private _934.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _934.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 1, + solution1.shortestBridge( + new int[][] { + {0, 1}, + {1, 0} + })); + } + + @Test + public void test2() { + assertEquals(2, solution1.shortestBridge(new int[][] {{0, 1, 0}, {0, 0, 0}, {0, 0, 1}})); + } + + @Test + public void test3() { + assertEquals( + 1, + solution1.shortestBridge( + new int[][] { + {1, 1, 1, 1, 1}, + {1, 0, 0, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 0, 0, 1}, + {1, 1, 1, 1, 1} + })); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_935Test.java b/src/test/java/com/fishercoder/firstthousand/_935Test.java new file mode 100644 index 0000000000..80741b3e16 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_935Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._935; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _935Test { + private _935.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _935.Solution1(); + } + + @Test + public void test1() { + assertEquals(solution1.knightDialer(1), 10); + } + + @Test + public void test2() { + assertEquals(solution1.knightDialer(2), 20); + } + + @Test + public void test3() { + assertEquals(solution1.knightDialer(3), 46); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_936Test.java b/src/test/java/com/fishercoder/firstthousand/_936Test.java new file mode 100644 index 0000000000..70ef2e5b0d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_936Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._936; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _936Test { + private _936.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _936.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals(new int[] {0, 2}, solution1.movesToStamp("abc", "ababc")); + } + + @Test + public void test2() { + assertArrayEquals(new int[] {}, solution1.movesToStamp("aye", "eyeye")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_937Test.java b/src/test/java/com/fishercoder/firstthousand/_937Test.java new file mode 100644 index 0000000000..4a5f1e41cd --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_937Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._937; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _937Test { + private _937.Solution1 solution1; + private static String[] logs; + private static String[] expected; + + @BeforeEach + public void setUp() { + solution1 = new _937.Solution1(); + } + + @Test + public void test1() { + logs = + new String[] { + "a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo" + }; + expected = + new String[] { + "g1 act car", "a8 act zoo", "ab1 off key dog", "a1 9 2 3 1", "zo4 4 7" + }; + assertArrayEquals(expected, solution1.reorderLogFiles(logs)); + } + + @Test + public void test2() { + logs = new String[] {"t kvr", "r 3 1", "i 403", "7 so", "t 54"}; + expected = new String[] {"t kvr", "7 so", "r 3 1", "i 403", "t 54"}; + assertArrayEquals(expected, solution1.reorderLogFiles(logs)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_938Test.java b/src/test/java/com/fishercoder/firstthousand/_938Test.java new file mode 100644 index 0000000000..ff4ff949cb --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_938Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._938; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _938Test { + private _938.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setUp() { + solution1 = new _938.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(10, 5, 15, 3, 7, null, 18)); + TreeUtils.printBinaryTree(root); + assertEquals(32, solution1.rangeSumBST(root, 7, 15)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(10, 5, 15, 3, 7, 13, 18, 1, null, 6)); + TreeUtils.printBinaryTree(root); + assertEquals(23, solution1.rangeSumBST(root, 6, 10)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_93Test.java b/src/test/java/com/fishercoder/firstthousand/_93Test.java new file mode 100644 index 0000000000..e710efdcac --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_93Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._93; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _93Test { + private _93.Solution1 solution1; + private static List expected; + private static String s; + + @BeforeEach + public void setUp() { + solution1 = new _93.Solution1(); + } + + @BeforeEach + public void setupForEachTest() { + expected = new ArrayList<>(); + } + + @Test + public void test1() { + s = "25525511135"; + expected.add("255.255.11.135"); + expected.add("255.255.111.35"); + assertEquals(expected, solution1.restoreIpAddresses(s)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_941Test.java b/src/test/java/com/fishercoder/firstthousand/_941Test.java new file mode 100644 index 0000000000..2536a91d4c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_941Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._941; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _941Test { + private _941.Solution1 solution1; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _941.Solution1(); + } + + @Test + public void test1() { + A = new int[] {0, 3, 2, 1}; + assertEquals(true, solution1.validMountainArray(A)); + } + + @Test + public void test2() { + A = new int[] {2, 1}; + assertEquals(false, solution1.validMountainArray(A)); + } + + @Test + public void test3() { + A = new int[] {3, 5, 5}; + assertEquals(false, solution1.validMountainArray(A)); + } + + @Test + public void test4() { + A = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + assertEquals(false, solution1.validMountainArray(A)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_942Test.java b/src/test/java/com/fishercoder/firstthousand/_942Test.java new file mode 100644 index 0000000000..fe55bce9de --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_942Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._942; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _942Test { + private _942.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _942.Solution1(); + } + + @Test + public void test1() { + CommonUtils.printArray(solution1.diStringMatch("IDID")); + } + + @Test + public void test2() { + CommonUtils.printArray(solution1.diStringMatch("III")); + } + + @Test + public void test3() { + CommonUtils.printArray(solution1.diStringMatch("DDI")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_944Test.java b/src/test/java/com/fishercoder/firstthousand/_944Test.java new file mode 100644 index 0000000000..12d485ad51 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_944Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._944; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _944Test { + private _944.Solution1 solution1; + private static String[] A; + + @BeforeEach + public void setUp() { + solution1 = new _944.Solution1(); + } + + @Test + public void test1() { + A = new String[] {"cba", "daf", "ghi"}; + assertEquals(1, solution1.minDeletionSize(A)); + } + + @Test + public void test2() { + A = new String[] {"a", "b"}; + assertEquals(0, solution1.minDeletionSize(A)); + } + + @Test + public void test3() { + A = new String[] {"zyx", "wvu", "tsr"}; + assertEquals(3, solution1.minDeletionSize(A)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_945Test.java b/src/test/java/com/fishercoder/firstthousand/_945Test.java new file mode 100644 index 0000000000..295539559c --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_945Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._945; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _945Test { + private _945.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _945.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {3, 2, 1, 2, 1, 7}; + assertEquals(6, solution1.minIncrementForUnique(nums)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_946Test.java b/src/test/java/com/fishercoder/firstthousand/_946Test.java new file mode 100644 index 0000000000..3181fa52b3 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_946Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._946; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _946Test { + + private _946.Solution1 solution1; + private _946.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _946.Solution1(); + solution2 = new _946.Solution2(); + } + + @Test + public void test1() { + assertEquals( + true, + solution1.validateStackSequences( + new int[] {1, 2, 3, 4, 5}, new int[] {4, 5, 3, 2, 1})); + assertEquals( + true, + solution2.validateStackSequences( + new int[] {1, 2, 3, 4, 5}, new int[] {4, 5, 3, 2, 1})); + } + + @Test + public void test2() { + assertEquals( + false, + solution1.validateStackSequences( + new int[] {1, 2, 3, 4, 5}, new int[] {4, 3, 5, 1, 2})); + assertEquals( + false, + solution2.validateStackSequences( + new int[] {1, 2, 3, 4, 5}, new int[] {4, 3, 5, 1, 2})); + } + + @Test + public void test3() { + assertEquals(true, solution1.validateStackSequences(new int[] {}, new int[] {})); + assertEquals(true, solution2.validateStackSequences(new int[] {}, new int[] {})); + } + + @Test + public void test4() { + assertEquals( + false, + solution1.validateStackSequences( + new int[] {4, 0, 1, 2, 3}, new int[] {4, 2, 3, 0, 1})); + assertEquals( + false, + solution2.validateStackSequences( + new int[] {4, 0, 1, 2, 3}, new int[] {4, 2, 3, 0, 1})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_94Test.java b/src/test/java/com/fishercoder/firstthousand/_94Test.java new file mode 100644 index 0000000000..52707c4769 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_94Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._94; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _94Test { + private _94.Solution1 solution1; + private _94.Solution2 solution2; + private static TreeNode root; + private static List inorder; + + @BeforeEach + public void setup() { + solution1 = new _94.Solution1(); + solution2 = new _94.Solution2(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4)); + inorder = solution1.inorderTraversal(root); + CommonUtils.printList(inorder); + } + + @Test + public void test2() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9)); + TreeUtils.printBinaryTree(root); + inorder = solution1.inorderTraversal(root); + CommonUtils.printList(inorder); + } + + @Test + public void test3() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, null, null, 6)); + TreeUtils.printBinaryTree(root); + inorder = solution2.inorderTraversal(root); + CommonUtils.printList(inorder); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_950Test.java b/src/test/java/com/fishercoder/firstthousand/_950Test.java new file mode 100644 index 0000000000..4247e2de73 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_950Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._950; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _950Test { + private _950.Solution1 solution1; + + @BeforeEach + public void setUp() { + solution1 = new _950.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {2, 13, 3, 11, 5, 17, 7}, + solution1.deckRevealedIncreasing(new int[] {17, 13, 11, 2, 3, 5, 7})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_951Test.java b/src/test/java/com/fishercoder/firstthousand/_951Test.java new file mode 100644 index 0000000000..cd34975e79 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_951Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._951; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _951Test { + private _951.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _951.Solution1(); + } + + @Test + public void test1() { + TreeNode root1 = + TreeUtils.constructBinaryTree( + Arrays.asList(1, 2, 3, 4, 5, 6, null, null, null, 7, 8)); + TreeNode root2 = + TreeUtils.constructBinaryTree( + Arrays.asList(1, 3, 2, null, 6, 4, 5, null, null, null, null, 8, 7)); + assertTrue(solution1.flipEquiv(root1, root2)); + } + + @Test + public void test2() { + TreeNode root1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); + TreeNode root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 4)); + assertFalse(solution1.flipEquiv(root1, root2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_953Test.java b/src/test/java/com/fishercoder/firstthousand/_953Test.java new file mode 100644 index 0000000000..9e5b3c4727 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_953Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._953; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _953Test { + private _953.Solution1 solution1; + private _953.Solution2 solution2; + private static String[] words; + private static String order; + + @BeforeEach + public void setup() { + solution1 = new _953.Solution1(); + solution2 = new _953.Solution2(); + } + + @Test + public void test1() { + words = new String[] {"hello", "leetcode"}; + order = "hlabcdefgijkmnopqrstuvwxyz"; + assertEquals(true, solution1.isAlienSorted(words, order)); + assertEquals(true, solution2.isAlienSorted(words, order)); + } + + @Test + public void test2() { + words = new String[] {"word", "world", "row"}; + order = "worldabcefghijkmnpqstuvxyz"; + assertEquals(false, solution1.isAlienSorted(words, order)); + assertEquals(false, solution2.isAlienSorted(words, order)); + } + + @Test + public void test3() { + words = new String[] {"apple", "app"}; + order = "abcdefghijklmnopqrstuvwxyz"; + assertEquals(false, solution1.isAlienSorted(words, order)); + assertEquals(false, solution2.isAlienSorted(words, order)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_954Test.java b/src/test/java/com/fishercoder/firstthousand/_954Test.java new file mode 100644 index 0000000000..d0ddcc1626 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_954Test.java @@ -0,0 +1,59 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._954; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _954Test { + private _954.Solution1 solution1; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _954.Solution1(); + } + + @Test + public void test1() { + A = new int[] {3, 1, 3, 6}; + assertEquals(false, solution1.canReorderDoubled(A)); + } + + @Test + public void test2() { + A = new int[] {2, 1, 2, 6}; + assertEquals(false, solution1.canReorderDoubled(A)); + } + + @Test + public void test3() { + A = new int[] {4, -2, 2, -4}; + assertEquals(true, solution1.canReorderDoubled(A)); + } + + @Test + public void test4() { + A = new int[] {1, 2, 4, 16, 8, 4}; + assertEquals(false, solution1.canReorderDoubled(A)); + } + + @Test + public void test5() { + A = new int[] {1, 2, 4, 8}; + assertEquals(true, solution1.canReorderDoubled(A)); + } + + @Test + public void test6() { + A = new int[] {10, 20, 40, 80}; + assertEquals(true, solution1.canReorderDoubled(A)); + } + + @Test + public void test7() { + A = new int[] {0, 0}; + assertEquals(true, solution1.canReorderDoubled(A)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_957Test.java b/src/test/java/com/fishercoder/firstthousand/_957Test.java new file mode 100644 index 0000000000..ce33b432a6 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_957Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._957; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _957Test { + private _957.Solution1 solution1; + private static int[] cells; + + @BeforeEach + public void setup() { + solution1 = new _957.Solution1(); + } + + @Test + public void test1() { + cells = new int[] {0, 1, 0, 1, 1, 0, 0, 1}; + assertArrayEquals(new int[] {0, 0, 1, 1, 0, 0, 0, 0}, solution1.prisonAfterNDays(cells, 7)); + } + + @Test + public void test2() { + cells = new int[] {1, 0, 0, 1, 0, 0, 1, 0}; + assertArrayEquals( + new int[] {0, 0, 1, 1, 1, 1, 1, 0}, solution1.prisonAfterNDays(cells, 1000000000)); + } + + @Test + public void test3() { + cells = new int[] {0, 1, 1, 1, 0, 0, 0, 0}; + assertArrayEquals( + new int[] {0, 0, 1, 0, 0, 1, 1, 0}, solution1.prisonAfterNDays(cells, 99)); + } + + @Test + public void test4() { + cells = new int[] {0, 1, 1, 1, 1, 1, 1, 0}; + assertArrayEquals( + new int[] {0, 0, 1, 1, 1, 1, 0, 0}, solution1.prisonAfterNDays(cells, 99)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_958Test.java b/src/test/java/com/fishercoder/firstthousand/_958Test.java new file mode 100644 index 0000000000..95b405189d --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_958Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._958; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _958Test { + private _958.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _958.Solution1(); + } + + @Test + public void test1() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6)); + assertEquals(true, solution1.isCompleteTree(root)); + } + + @Test + public void test2() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, null, 7)); + assertEquals(false, solution1.isCompleteTree(root)); + } + + @Test + public void test3() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 8)); + assertEquals(true, solution1.isCompleteTree(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_95Test.java b/src/test/java/com/fishercoder/firstthousand/_95Test.java new file mode 100644 index 0000000000..6c6e1ca280 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_95Test.java @@ -0,0 +1,20 @@ +package com.fishercoder.firstthousand; + +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._95; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _95Test { + private _95.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _95.Solution1(); + } + + @Test + public void test1() { + solution1.generateTrees(3).forEach(TreeUtils::printBinaryTree); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_961Test.java b/src/test/java/com/fishercoder/firstthousand/_961Test.java new file mode 100644 index 0000000000..f49fb3845f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_961Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._961; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _961Test { + private _961.Solution1 solution1; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _961.Solution1(); + } + + @Test + public void test1() { + A = new int[] {1, 2, 3, 3}; + assertEquals(3, solution1.repeatedNTimes(A)); + } + + @Test + public void test2() { + A = new int[] {2, 1, 2, 5, 3, 2}; + assertEquals(2, solution1.repeatedNTimes(A)); + } + + @Test + public void test3() { + A = new int[] {5, 1, 5, 2, 5, 3, 5, 4}; + assertEquals(5, solution1.repeatedNTimes(A)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_965Test.java b/src/test/java/com/fishercoder/firstthousand/_965Test.java new file mode 100644 index 0000000000..00a73f5394 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_965Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._965; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _965Test { + private _965.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setUp() { + solution1 = new _965.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 1, 1, 1, null, 1)); + assertEquals(true, solution1.isUnivalTree(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 2, 5, 2)); + assertEquals(false, solution1.isUnivalTree(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_966Test.java b/src/test/java/com/fishercoder/firstthousand/_966Test.java new file mode 100644 index 0000000000..2a79f7af81 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_966Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._966; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by varunu28 on 1/01/19. */ +public class _966Test { + private _966.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _966.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.toString( + new String[] { + "kite", "KiTe", "KiTe", "Hare", "hare", "", "", "KiTe", "", "KiTe" + }), + Arrays.toString( + solution1.spellchecker( + new String[] {"KiTe", "kite", "hare", "Hare"}, + new String[] { + "kite", "Kite", "KiTe", "Hare", "HARE", "Hear", "hear", "keti", + "keet", "keto" + }))); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_96Test.java b/src/test/java/com/fishercoder/firstthousand/_96Test.java new file mode 100644 index 0000000000..0cc7bdbccc --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_96Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._96; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _96Test { + private _96.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _96.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.numTrees(3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_970Test.java b/src/test/java/com/fishercoder/firstthousand/_970Test.java new file mode 100644 index 0000000000..718d9e60a4 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_970Test.java @@ -0,0 +1,56 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._970; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _970Test { + private _970.Solution1 solution1; + private _970.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _970.Solution1(); + solution2 = new _970.Solution2(); + } + + @Test + public void test1() { + assertEquals(Arrays.asList(2, 3, 4, 5, 7, 9, 10), solution1.powerfulIntegers(2, 3, 10)); + assertEquals(Arrays.asList(2, 3, 4, 5, 7, 9, 10), solution2.powerfulIntegers(2, 3, 10)); + } + + @Test + public void test2() { + assertEquals(Arrays.asList(2, 4, 6, 8, 10, 14), solution1.powerfulIntegers(3, 5, 15)); + assertEquals(Arrays.asList(2, 4, 6, 8, 10, 14), solution2.powerfulIntegers(3, 5, 15)); + } + + @Test + public void test3() { + assertEquals(Arrays.asList(2, 3, 5, 7, 8, 9, 10), solution1.powerfulIntegers(2, 6, 12)); + assertEquals(Arrays.asList(2, 3, 5, 7, 8, 9, 10), solution2.powerfulIntegers(2, 6, 12)); + } + + @Test + public void test4() { + assertEquals(Arrays.asList(2, 3, 5, 9, 10, 11), solution1.powerfulIntegers(2, 9, 12)); + assertEquals(Arrays.asList(2, 3, 5, 9, 10, 11), solution2.powerfulIntegers(2, 9, 12)); + } + + @Test + public void test5() { + CommonUtils.printList(solution1.powerfulIntegers(90, 90, 1000000)); + CommonUtils.printList(solution2.powerfulIntegers(90, 90, 1000000)); + } + + @Test + public void test6() { + assertEquals(Arrays.asList(2), solution1.powerfulIntegers(1, 1, 40000)); + assertEquals(Arrays.asList(2), solution2.powerfulIntegers(1, 1, 40000)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_973Test.java b/src/test/java/com/fishercoder/firstthousand/_973Test.java new file mode 100644 index 0000000000..4ad35d1c59 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_973Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._973; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _973Test { + private _973.Solution1 solution1; + private _973.Solution2 solution2; + + @BeforeEach + public void setUp() { + solution1 = new _973.Solution1(); + solution2 = new _973.Solution2(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[][] {{-2, 2}}, solution1.kClosest(new int[][] {{1, 3}, {-2, 2}}, 1)); + assertArrayEquals( + new int[][] {{-2, 2}}, solution2.kClosest(new int[][] {{1, 3}, {-2, 2}}, 1)); + } + + @Test + public void test2() { + assertArrayEquals( + new int[][] {{3, 3}, {-2, 4}}, + solution1.kClosest(new int[][] {{3, 3}, {5, -1}, {-2, 4}}, 2)); + assertArrayEquals( + new int[][] {{-2, 4}, {3, 3}}, + solution2.kClosest(new int[][] {{3, 3}, {5, -1}, {-2, 4}}, 2)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_974Test.java b/src/test/java/com/fishercoder/firstthousand/_974Test.java new file mode 100644 index 0000000000..22f7f8a564 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_974Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._974; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _974Test { + + private _974.Solution1 test; + + @BeforeEach + public void setup() { + test = new _974.Solution1(); + } + + @Test + public void test1() { + assertEquals(7, test.subarraysDivByK(new int[] {4, 5, 0, -2, -3, 1}, 5)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_976Test.java b/src/test/java/com/fishercoder/firstthousand/_976Test.java new file mode 100644 index 0000000000..37a91910f0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_976Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._976; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _976Test { + + private _976.Solution1 test; + + @BeforeEach + public void setup() { + test = new _976.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, test.largestPerimeter(new int[] {2, 1, 2})); + } + + @Test + public void test2() { + assertEquals(0, test.largestPerimeter(new int[] {1, 2, 1})); + } + + @Test + public void test3() { + assertEquals(10, test.largestPerimeter(new int[] {3, 2, 3, 4})); + } + + @Test + public void test4() { + assertEquals(8, test.largestPerimeter(new int[] {3, 6, 2, 3})); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_977Test.java b/src/test/java/com/fishercoder/firstthousand/_977Test.java new file mode 100644 index 0000000000..76a7019b7f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_977Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._977; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _977Test { + private _977.Solution1 solution1; + private _977.Solution2 solution2; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _977.Solution1(); + solution2 = new _977.Solution2(); + } + + @Test + public void test1() { + A = new int[] {-4, -1, 0, 3, 10}; + assertArrayEquals(new int[] {0, 1, 9, 16, 100}, solution1.sortedSquares(A)); + assertArrayEquals(new int[] {0, 1, 9, 16, 100}, solution2.sortedSquares(A)); + } + + @Test + public void test2() { + A = new int[] {-7, -3, 2, 3, 11}; + assertArrayEquals(new int[] {4, 9, 9, 49, 121}, solution1.sortedSquares(A)); + assertArrayEquals(new int[] {4, 9, 9, 49, 121}, solution2.sortedSquares(A)); + } +} diff --git a/src/test/java/com/fishercoder/_979Test.java b/src/test/java/com/fishercoder/firstthousand/_979Test.java similarity index 82% rename from src/test/java/com/fishercoder/_979Test.java rename to src/test/java/com/fishercoder/firstthousand/_979Test.java index 25bdf0ed01..60de4b31e1 100644 --- a/src/test/java/com/fishercoder/_979Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_979Test.java @@ -1,16 +1,15 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._979; -import org.junit.Test; - +import com.fishercoder.solutions.firstthousand._979; import java.util.Arrays; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; public class _979Test { - private static _979.Solution1 solution1; + private _979.Solution1 solution1; private static TreeNode root; @Test diff --git a/src/test/java/com/fishercoder/firstthousand/_97Test.java b/src/test/java/com/fishercoder/firstthousand/_97Test.java new file mode 100644 index 0000000000..ab09bb4ab0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_97Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._97; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _97Test { + private _97.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _97.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isInterleave("aabcc", "dbbca", "aadbbcbcac")); + } + + @Test + public void test2() { + assertEquals(false, solution1.isInterleave("aabcc", "dbbca", "aadbbbaccc")); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_980Test.java b/src/test/java/com/fishercoder/firstthousand/_980Test.java new file mode 100644 index 0000000000..173372c92a --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_980Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._980; +import org.junit.jupiter.api.Test; + +public class _980Test { + private _980.Solution1 solution1; + private static int[][] grid; + + @Test + public void test1() { + solution1 = new _980.Solution1(); + grid = + new int[][] { + {1, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 2, -1}, + }; + assertEquals(2, solution1.uniquePathsIII(grid)); + } + + @Test + public void test2() { + solution1 = new _980.Solution1(); + grid = + new int[][] { + {1, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 2}, + }; + assertEquals(4, solution1.uniquePathsIII(grid)); + } + + @Test + public void test3() { + solution1 = new _980.Solution1(); + grid = + new int[][] { + {0, 1}, + {2, 0}, + }; + assertEquals(0, solution1.uniquePathsIII(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_985Test.java b/src/test/java/com/fishercoder/firstthousand/_985Test.java new file mode 100644 index 0000000000..305ca7c8c2 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_985Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.firstthousand._985; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _985Test { + private _985.Solution1 solution1; + private static int[] expected; + private static int[] actual; + private static int[] A; + private static int[][] queries; + + @BeforeEach + public void setup() { + solution1 = new _985.Solution1(); + } + + @Test + public void test1() { + A = new int[] {1, 2, 3, 4}; + queries = new int[][] {{1, 0}, {-3, 1}, {-4, 0}, {2, 3}}; + expected = new int[] {8, 6, 2, 4}; + actual = solution1.sumEvenAfterQueries(A, queries); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_986Test.java b/src/test/java/com/fishercoder/firstthousand/_986Test.java new file mode 100644 index 0000000000..b396e647ae --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_986Test.java @@ -0,0 +1,50 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._986; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _986Test { + private _986.Solution1 solution1; + private static int[][] A; + private static int[][] B; + private static int[][] expected; + + @BeforeEach + public void setup() { + solution1 = new _986.Solution1(); + } + + @Test + public void test1() { + A = + new int[][] { + {0, 2}, + {5, 10}, + {13, 23}, + {24, 25} + }; + B = + new int[][] { + {1, 5}, + {8, 12}, + {15, 24}, + {25, 26} + }; + expected = + new int[][] { + {1, 2}, + {5, 5}, + {8, 10}, + {15, 23}, + {24, 24}, + {25, 25} + }; + int[][] actual = solution1.intervalIntersection(A, B); + CommonUtils.print2DIntArray(actual); + assertArrayEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_987Test.java b/src/test/java/com/fishercoder/firstthousand/_987Test.java new file mode 100644 index 0000000000..1e788b10ef --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_987Test.java @@ -0,0 +1,92 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._987; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _987Test { + private _987.Solution1 solution1; + private _987.Solution2 solution2; + private static TreeNode root; + private static List> expected; + private static List> actual; + + @BeforeEach + public void setup() { + solution1 = new _987.Solution1(); + solution2 = new _987.Solution2(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); + expected = + Arrays.asList( + Arrays.asList(9), + Arrays.asList(3, 15), + Arrays.asList(20), + Arrays.asList(7)); + actual = solution1.verticalTraversal(root); + assertEquals(expected, actual); + actual = solution2.verticalTraversal(root); + assertEquals(expected, actual); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); + expected = + Arrays.asList( + Arrays.asList(4), + Arrays.asList(2), + Arrays.asList(1, 5, 6), + Arrays.asList(3), + Arrays.asList(7)); + actual = solution1.verticalTraversal(root); + assertEquals(expected, actual); + } + + @Test + public void test3() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 0, 5, 1, 9, null, 2, null, null, null, null, 3, 4, 8, 6, null, null, + null, 7)); + TreeUtils.printBinaryTree(root); + expected = + Arrays.asList( + Arrays.asList(9, 7), + Arrays.asList(5, 6), + Arrays.asList(0, 2, 4), + Arrays.asList(1, 3), + Arrays.asList(8)); + actual = solution1.verticalTraversal(root); + assertEquals(expected, actual); + } + + @Test + public void test4() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 0, 2, 1, 3, null, null, null, 4, 5, null, 7, 6, null, 10, 8, 11, + 9)); + TreeUtils.printBinaryTree(root); + expected = + Arrays.asList( + Arrays.asList(4, 10, 11), + Arrays.asList(3, 6, 7), + Arrays.asList(2, 5, 8, 9), + Arrays.asList(0), + Arrays.asList(1)); + actual = solution1.verticalTraversal(root); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_988Test.java b/src/test/java/com/fishercoder/firstthousand/_988Test.java new file mode 100644 index 0000000000..9bd7365720 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_988Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._988; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _988Test { + private _988.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _988.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(0, 1, 2, 3, 4, 3, 4)); + assertEquals("dba", solution1.smallestFromLeaf(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(25, 1, 3, 1, 3, 0, 2)); + assertEquals("adz", solution1.smallestFromLeaf(root)); + } + + @Test + public void test3() { + root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 1, null, 1, 0, null, 0)); + assertEquals("abc", solution1.smallestFromLeaf(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_989Test.java b/src/test/java/com/fishercoder/firstthousand/_989Test.java new file mode 100644 index 0000000000..da48eb447f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_989Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._989; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _989Test { + private _989.Solution1 solution1; + private static int[] A; + + @BeforeEach + public void setUp() { + solution1 = new _989.Solution1(); + } + + @Test + public void test1() { + A = new int[] {1, 2, 0, 0}; + assertEquals(Arrays.asList(1, 2, 3, 4), solution1.addToArrayForm(A, 34)); + } + + @Test + public void test2() { + A = new int[] {2, 7, 4}; + assertEquals(Arrays.asList(4, 5, 5), solution1.addToArrayForm(A, 181)); + } + + @Test + public void test3() { + A = new int[] {2, 1, 5}; + assertEquals(Arrays.asList(1, 0, 2, 1), solution1.addToArrayForm(A, 806)); + } + + @Test + public void test4() { + A = new int[] {9, 9, 9, 9, 9, 9, 9, 9, 9, 9}; + assertEquals( + Arrays.asList(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), solution1.addToArrayForm(A, 1)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_98Test.java b/src/test/java/com/fishercoder/firstthousand/_98Test.java new file mode 100644 index 0000000000..a36126efe0 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_98Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._98; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Created by fishercoder on 5/17/17. */ +public class _98Test { + private _98.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _98.Solution1(); + } + + @Test + public void test1() { + root = new TreeNode(2); + root.left = new TreeNode(1); + root.right = new TreeNode(3); + assertEquals(true, solution1.isValidBST(root)); + } + + @Test + public void test2() { + root = new TreeNode(0); + assertEquals(true, solution1.isValidBST(root)); + } + + @Test + public void test3() { + root = new TreeNode(1); + root.left = new TreeNode(1); + assertEquals(false, solution1.isValidBST(root)); + } + + @Test + public void test4() { + root = TreeUtils.constructBinaryTree(Arrays.asList(5, 1, 7, null, null, 3, 8)); + TreeUtils.printBinaryTree(root); + assertEquals(false, solution1.isValidBST(root)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_993Test.java b/src/test/java/com/fishercoder/firstthousand/_993Test.java new file mode 100644 index 0000000000..0abe91f8d9 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_993Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.firstthousand._993; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _993Test { + private _993.Solution1 solution1; + private _993.Solution2 solution2; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _993.Solution1(); + solution2 = new _993.Solution2(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4)); + TreeUtils.printBinaryTree(root); + assertEquals(false, solution1.isCousins(root, 4, 3)); + assertEquals(false, solution2.isCousins(root, 4, 3)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4, null, 5)); + TreeUtils.printBinaryTree(root); + assertEquals(true, solution1.isCousins(root, 5, 4)); + assertEquals(true, solution2.isCousins(root, 5, 4)); + } + + @Test + public void test3() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4)); + TreeUtils.printBinaryTree(root); + assertEquals(false, solution1.isCousins(root, 2, 3)); + assertEquals(false, solution2.isCousins(root, 2, 3)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_994Test.java b/src/test/java/com/fishercoder/firstthousand/_994Test.java new file mode 100644 index 0000000000..6e3f27e815 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_994Test.java @@ -0,0 +1,76 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.firstthousand._994; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _994Test { + private _994.Solution1 solution1; + private _994.Solution2 solution2; + private _994.Solution3 solution3; + private static int[][] grid; + + @BeforeEach + public void setUp() { + solution1 = new _994.Solution1(); + solution2 = new _994.Solution2(); + solution3 = new _994.Solution3(); + } + + @Test + public void test1() { + grid = + new int[][] { + {2, 1, 1}, + {1, 1, 0}, + {0, 1, 1} + }; + assertEquals(4, solution1.orangesRotting(grid)); + } + + @Test + public void test2() { + grid = + new int[][] { + {2, 1, 1}, + {0, 1, 1}, + {1, 0, 1} + }; + assertEquals(-1, solution1.orangesRotting(grid)); + } + + @Test + public void test3() { + grid = new int[][] {{0, 2}}; + assertEquals(0, solution1.orangesRotting(grid)); + } + + @Test + public void test4() { + grid = + new int[][] { + {2, 1, 1}, + {1, 1, 0}, + {0, 1, 1} + }; + assertEquals(4, solution2.orangesRotting(grid)); + } + + @Test + public void test5() { + grid = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[2,0,0,1,0,1],[2,0,0,1,2,0]"); + assertEquals(-1, solution2.orangesRotting(grid)); + } + + @Test + public void test6() { + grid = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2],[1]"); + assertEquals(1, solution2.orangesRotting(grid)); + assertEquals(1, solution3.orangesRotting(grid)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_997Test.java b/src/test/java/com/fishercoder/firstthousand/_997Test.java new file mode 100644 index 0000000000..a130631617 --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_997Test.java @@ -0,0 +1,56 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._997; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _997Test { + private _997.Solution1 solution1; + private _997.Solution2 solution2; + private static int[][] trust; + + @BeforeEach + public void setup() { + solution1 = new _997.Solution1(); + solution2 = new _997.Solution2(); + } + + @Test + public void test1() { + trust = new int[][] {{1, 2}}; + assertEquals(2, solution1.findJudge(2, trust)); + assertEquals(2, solution2.findJudge(2, trust)); + } + + @Test + public void test2() { + trust = new int[][] {{1, 3}, {2, 3}}; + assertEquals(3, solution1.findJudge(3, trust)); + } + + @Test + public void test3() { + trust = new int[][] {{1, 2}, {2, 3}, {3, 1}}; + assertEquals(-1, solution1.findJudge(3, trust)); + } + + @Test + public void test4() { + trust = new int[][] {{1, 2}, {2, 3}}; + assertEquals(-1, solution1.findJudge(3, trust)); + } + + @Test + public void test5() { + trust = new int[][] {{1, 3}, {1, 4}, {2, 3}, {2, 4}, {4, 3}}; + assertEquals(3, solution1.findJudge(4, trust)); + } + + @Test + public void test6() { + trust = new int[][] {{1, 3}, {2, 3}, {3, 1}}; + assertEquals(-1, solution1.findJudge(3, trust)); + } +} diff --git a/src/test/java/com/fishercoder/firstthousand/_999Test.java b/src/test/java/com/fishercoder/firstthousand/_999Test.java new file mode 100644 index 0000000000..ac9a2a641e --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_999Test.java @@ -0,0 +1,65 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._999; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _999Test { + private _999.Solution1 solution1; + private static char[][] board; + + @BeforeEach + public void setup() { + solution1 = new _999.Solution1(); + } + + @Test + public void test1() { + board = + new char[][] { + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', 'p', '.', '.', '.', '.'}, + {'.', '.', '.', 'R', '.', '.', '.', 'p'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', 'p', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + }; + assertEquals(3, solution1.numRookCaptures(board)); + } + + @Test + public void test2() { + board = + new char[][] { + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', 'p', 'p', 'p', 'p', 'p', '.', '.'}, + {'.', 'p', 'p', 'B', 'p', 'p', '.', '.'}, + {'.', 'p', 'B', 'R', 'B', 'p', '.', '.'}, + {'.', 'p', 'p', 'B', 'p', 'p', '.', '.'}, + {'.', 'p', 'p', 'p', 'p', 'p', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + }; + assertEquals(0, solution1.numRookCaptures(board)); + } + + @Test + public void test3() { + board = + new char[][] { + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', 'p', '.', '.', '.', '.'}, + {'.', '.', '.', 'p', '.', '.', '.', 'p'}, + {'p', 'p', '.', 'R', '.', 'p', 'B', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', 'B', '.', '.', '.', '.'}, + {'.', '.', '.', 'p', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + }; + assertEquals(3, solution1.numRookCaptures(board)); + } +} diff --git a/src/test/java/com/fishercoder/_99Test.java b/src/test/java/com/fishercoder/firstthousand/_99Test.java similarity index 76% rename from src/test/java/com/fishercoder/_99Test.java rename to src/test/java/com/fishercoder/firstthousand/_99Test.java index 2a1f41a89e..d2fc05c2ad 100644 --- a/src/test/java/com/fishercoder/_99Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_99Test.java @@ -1,17 +1,17 @@ -package com.fishercoder; +package com.fishercoder.firstthousand; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._99; -import org.junit.BeforeClass; -import org.junit.Test; +import com.fishercoder.solutions.firstthousand._99; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _99Test { - private static _99.Solution1 solution1; + private _99.Solution1 solution1; private static TreeNode root; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _99.Solution1(); } @@ -35,5 +35,4 @@ public void test2() { solution1.recoverTree(root); TreeUtils.printBinaryTree(root); } - } diff --git a/src/test/java/com/fishercoder/firstthousand/_9Test.java b/src/test/java/com/fishercoder/firstthousand/_9Test.java new file mode 100644 index 0000000000..2868e4279f --- /dev/null +++ b/src/test/java/com/fishercoder/firstthousand/_9Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.firstthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.firstthousand._9; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _9Test { + private _9.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _9.Solution1(); + } + + @Test + public void test1() { + assertEquals(false, solution1.isPalindrome(2147483647)); + } + + @Test + public void test2() { + assertEquals(true, solution1.isPalindrome(0)); + } + + @Test + public void test3() { + assertEquals(true, solution1.isPalindrome(1)); + } + + @Test + public void test4() { + assertEquals(false, solution1.isPalindrome(10)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3004Test.java b/src/test/java/com/fishercoder/fourththousand/_3004Test.java new file mode 100644 index 0000000000..718e9576ef --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3004Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3004; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3004Test { + private _3004.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3004.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 1, + solution1.maximumSubtreeSize( + new int[][] {{0, 1}, {0, 2}, {0, 3}}, new int[] {1, 1, 2, 3})); + } + + @Test + public void test2() { + assertEquals( + 4, + solution1.maximumSubtreeSize( + new int[][] {{0, 1}, {0, 2}, {0, 3}}, new int[] {1, 1, 1, 1})); + } + + @Test + public void test3() { + assertEquals( + 3, + solution1.maximumSubtreeSize( + new int[][] {{0, 1}, {0, 2}, {2, 3}, {2, 4}}, new int[] {1, 2, 3, 3, 3})); + } + + @Test + public void test4() { + assertEquals(1, solution1.maximumSubtreeSize(new int[][] {{}}, new int[] {1})); + } + + @Test + public void test5() { + assertEquals( + 1, solution1.maximumSubtreeSize(new int[][] {{0, 1}, {1, 2}}, new int[] {1, 1, 2})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3006Test.java b/src/test/java/com/fishercoder/fourththousand/_3006Test.java new file mode 100644 index 0000000000..172e39831c --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3006Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3006; +import java.util.ArrayList; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3006Test { + private _3006.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3006.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList(16, 33), + solution1.beautifulIndices( + "isawsquirrelnearmysquirrelhouseohmy", "my", "squirrel", 15)); + } + + @Test + public void test2() { + assertEquals( + new ArrayList<>(Arrays.asList(0)), + solution1.beautifulIndices("bavgoc", "ba", "c", 6)); + } + + @Test + public void test3() { + assertEquals(Arrays.asList(), solution1.beautifulIndices("lrtsi", "lrts", "i", 3)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3016Test.java b/src/test/java/com/fishercoder/fourththousand/_3016Test.java new file mode 100644 index 0000000000..22d13bd3d7 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3016Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3016; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3016Test { + private _3016.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3016.Solution1(); + } + + @Test + public void test1() { + assertEquals(24, solution1.minimumPushes("aabbccddeeffgghhiiiiii")); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3038Test.java b/src/test/java/com/fishercoder/fourththousand/_3038Test.java new file mode 100644 index 0000000000..ee20962723 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3038Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3038; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3038Test { + private _3038.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _3038.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 2, 3, 2, 4, 2, 3, 3, 1, 3}; + assertEquals(1, solution1.maxOperations(nums)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3074Test.java b/src/test/java/com/fishercoder/fourththousand/_3074Test.java new file mode 100644 index 0000000000..2dc7bbfe32 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3074Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3074; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3074Test { + private _3074.Solution1 solution1; + private static int[] apple; + private static int[] capacity; + + @BeforeEach + public void setup() { + solution1 = new _3074.Solution1(); + } + + @Test + public void test1() { + apple = new int[] {1, 3, 2}; + capacity = new int[] {4, 3, 1, 5, 2}; + assertEquals(2, solution1.minimumBoxes(apple, capacity)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3079Test.java b/src/test/java/com/fishercoder/fourththousand/_3079Test.java new file mode 100644 index 0000000000..afd62fb9d0 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3079Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3079; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3079Test { + private _3079.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3079.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.sumOfEncryptedInt(new int[] {1, 2, 3})); + } + + @Test + public void test2() { + assertEquals(66, solution1.sumOfEncryptedInt(new int[] {10, 21, 31})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3083Test.java b/src/test/java/com/fishercoder/fourththousand/_3083Test.java new file mode 100644 index 0000000000..0422d843a6 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3083Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.fourththousand._3083; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3083Test { + private _3083.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3083.Solution1(); + } + + @Test + public void test1() { + assertTrue(solution1.isSubstringPresent("leetcode")); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3090Test.java b/src/test/java/com/fishercoder/fourththousand/_3090Test.java new file mode 100644 index 0000000000..bd4a61f720 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3090Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3090; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3090Test { + private _3090.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3090.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.maximumLengthSubstring("bcbbbcba")); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3095Test.java b/src/test/java/com/fishercoder/fourththousand/_3095Test.java new file mode 100644 index 0000000000..808bb594a3 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3095Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3095; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3095Test { + private _3095.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3095.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.minimumSubarrayLength(new int[] {2, 1, 8}, 10)); + } + + @Test + public void test2() { + assertEquals(-1, solution1.minimumSubarrayLength(new int[] {1, 12, 2, 5}, 43)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3112Test.java b/src/test/java/com/fishercoder/fourththousand/_3112Test.java new file mode 100644 index 0000000000..972f379417 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3112Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.fourththousand._3112; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3112Test { + private _3112.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3112.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {0, -1, 4}, + solution1.minimumTime( + 3, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1,2],[1,2,1],[0,2,4]"), + new int[] {1, 1, 5})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3142Test.java b/src/test/java/com/fishercoder/fourththousand/_3142Test.java new file mode 100644 index 0000000000..a81afa7c77 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3142Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3142; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3142Test { + private _3142.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3142.Solution1(); + } + + @Test + public void test1() { + assertEquals(false, solution1.satisfiesConditions(new int[][] {{1}, {2}, {3}})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3175Test.java b/src/test/java/com/fishercoder/fourththousand/_3175Test.java new file mode 100644 index 0000000000..90d21243d2 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3175Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3175; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3175Test { + private _3175.Solution1 solution1; + private static int[] skills; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _3175.Solution1(); + } + + @Test + public void test1() { + skills = new int[] {16, 4, 7, 17}; + k = 562084119; + assertEquals(3, solution1.findWinningPlayer(skills, k)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3178Test.java b/src/test/java/com/fishercoder/fourththousand/_3178Test.java new file mode 100644 index 0000000000..c394f9ebb0 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3178Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3178; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3178Test { + private _3178.Solution1 solution1; + private _3178.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _3178.Solution1(); + solution2 = new _3178.Solution2(); + } + + @Test + public void test1() { + assertEquals(1, solution1.numberOfChild(3, 5)); + assertEquals(1, solution2.numberOfChild(3, 5)); + } + + @Test + public void test2() { + assertEquals(2, solution1.numberOfChild(5, 6)); + } + + @Test + public void test3() { + assertEquals(2, solution1.numberOfChild(4, 2)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3185Test.java b/src/test/java/com/fishercoder/fourththousand/_3185Test.java new file mode 100644 index 0000000000..8f0954558c --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3185Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3185; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3185Test { + private _3185.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3185.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.countCompleteDayPairs(new int[] {12, 12, 30, 24, 24})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3186Test.java b/src/test/java/com/fishercoder/fourththousand/_3186Test.java new file mode 100644 index 0000000000..649d3c2211 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3186Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3186; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3186Test { + private _3186.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3186.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.maximumTotalDamage(new int[] {1, 1, 3, 4})); + } + + @Test + public void test2() { + assertEquals(31, solution1.maximumTotalDamage(new int[] {5, 9, 2, 10, 2, 7, 10, 9, 3, 8})); + } + + @Test + public void test3() { + assertEquals(10, solution1.maximumTotalDamage(new int[] {7, 1, 6, 3})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3189Test.java b/src/test/java/com/fishercoder/fourththousand/_3189Test.java new file mode 100644 index 0000000000..68cab6b9d9 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3189Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.fourththousand._3189; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3189Test { + private _3189.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3189.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.minMoves(new int[][] {{0, 0}, {1, 0}, {1, 1}})); + } + + @Test + public void test2() { + assertEquals( + 6, + solution1.minMoves( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,0],[0,1],[0,2],[0,3]"))); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3191Test.java b/src/test/java/com/fishercoder/fourththousand/_3191Test.java new file mode 100644 index 0000000000..1f876db9da --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3191Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3191; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3191Test { + private _3191.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3191.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.minOperations(new int[] {0, 1, 1, 1, 0, 0})); + } + + @Test + public void test2() { + assertEquals(-1, solution1.minOperations(new int[] {0, 1, 1, 1})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3192Test.java b/src/test/java/com/fishercoder/fourththousand/_3192Test.java new file mode 100644 index 0000000000..e6d3ac43c2 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3192Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3192; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3192Test { + private _3192.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3192.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.minOperations(new int[] {0, 1, 1, 0, 1})); + } + + @Test + public void test2() { + assertEquals(1, solution1.minOperations(new int[] {1, 0, 0, 0})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3196Test.java b/src/test/java/com/fishercoder/fourththousand/_3196Test.java new file mode 100644 index 0000000000..852ec9f0bb --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3196Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3196; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3196Test { + private _3196.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3196.Solution1(); + } + + @Test + public void test1() { + assertEquals(10, solution1.maximumTotalCost(new int[] {1, -2, 3, 4})); + } + + @Test + public void test2() { + assertEquals(-7, solution1.maximumTotalCost(new int[] {-14, -13, -20})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3199Test.java b/src/test/java/com/fishercoder/fourththousand/_3199Test.java new file mode 100644 index 0000000000..8d5a768215 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3199Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3199; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3199Test { + private _3199.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3199.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.tripletCount(new int[] {1}, new int[] {2}, new int[] {3})); + } + + @Test + public void test2() { + assertEquals( + 4, solution1.tripletCount(new int[] {1, 1}, new int[] {2, 3}, new int[] {1, 5})); + } + + @Test + public void test3() { + assertEquals( + 9, + solution1.tripletCount( + new int[] {0, 6, 0}, new int[] {8, 8, 4}, new int[] {6, 9, 2})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3208Test.java b/src/test/java/com/fishercoder/fourththousand/_3208Test.java new file mode 100644 index 0000000000..db507e665a --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3208Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3208; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3208Test { + private _3208.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3208.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.numberOfAlternatingGroups(new int[] {0, 1, 0, 1, 0}, 3)); + } + + @Test + public void test2() { + assertEquals(2, solution1.numberOfAlternatingGroups(new int[] {0, 1, 0, 0, 1, 0, 1}, 6)); + } + + @Test + public void test3() { + assertEquals(0, solution1.numberOfAlternatingGroups(new int[] {1, 1, 0, 1}, 4)); + } + + @Test + public void test4() { + assertEquals(4, solution1.numberOfAlternatingGroups(new int[] {0, 1, 0, 1}, 3)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3223Test.java b/src/test/java/com/fishercoder/fourththousand/_3223Test.java new file mode 100644 index 0000000000..10998b2da1 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3223Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3223; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3223Test { + private _3223.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3223.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 38, + solution1.minimumLength( + "ucvbutgkohgbcobqeyqwppbxqoynxeuuzouyvmydfhrprdbuzwqebwuiejoxsxdhbmuaiscalnteocghnlisxxawxgcjloevrdcj")); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3224Test.java b/src/test/java/com/fishercoder/fourththousand/_3224Test.java new file mode 100644 index 0000000000..8207daa4da --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3224Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3224; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3224Test { + private _3224.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3224.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minChanges(new int[] {1, 0, 1, 2, 4, 3}, 4)); + } + + @Test + public void test2() { + assertEquals(2, solution1.minChanges(new int[] {18, 10, 14, 18, 17, 2, 11, 5}, 19)); + } + + @Test + public void test3() { + assertEquals( + 4, solution1.minChanges(new int[] {9, 2, 7, 7, 8, 9, 1, 5, 1, 9, 4, 9, 4, 7}, 9)); + } + + @Test + public void test4() { + assertEquals( + 7, + solution1.minChanges( + new int[] { + 1, 1, 1, 1, 0, 0, 0, 5, 4, 3, 19, 17, 16, 15, 15, 15, 19, 19, 19, 19 + }, + 20)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3226Test.java b/src/test/java/com/fishercoder/fourththousand/_3226Test.java new file mode 100644 index 0000000000..c78f793e04 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3226Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3226; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3226Test { + private _3226.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3226.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minChanges(13, 4)); + } + + @Test + public void test2() { + assertEquals(-1, solution1.minChanges(44, 2)); + } + + @Test + public void test3() { + assertEquals(-1, solution1.minChanges(2, 47)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3228Test.java b/src/test/java/com/fishercoder/fourththousand/_3228Test.java new file mode 100644 index 0000000000..f264774dfa --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3228Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3228; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3228Test { + private _3228.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3228.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.maxOperations("1001101")); + } + + @Test + public void test2() { + assertEquals(0, solution1.maxOperations("00111")); + } + + @Test + public void test3() { + assertEquals(2, solution1.maxOperations("110")); + } + + @Test + public void test4() { + assertEquals(1, solution1.maxOperations("0010000111")); + } + + @Test + public void test5() { + assertEquals(5, solution1.maxOperations("11000100001")); + } + + @Test + public void test6() { + assertEquals(10, solution1.maxOperations("111101100")); + } + + @Test + public void test7() { + assertEquals(4, solution1.maxOperations("0101100000")); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3233Test.java b/src/test/java/com/fishercoder/fourththousand/_3233Test.java new file mode 100644 index 0000000000..326b27c05c --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3233Test.java @@ -0,0 +1,56 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3233; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3233Test { + private _3233.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3233.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.nonSpecialCount(5, 7)); + } + + @Test + public void test2() { + assertEquals(11, solution1.nonSpecialCount(4, 16)); + } + + @Test + public void test3() { + assertEquals(14, solution1.nonSpecialCount(1, 16)); + } + + @Test + public void test4() { + assertEquals(2, solution1.nonSpecialCount(1, 2)); + } + + @Test + public void test5() { + assertEquals(3, solution1.nonSpecialCount(1, 3)); + } + + @Test + public void test6() { + assertEquals(3, solution1.nonSpecialCount(1, 4)); + } + + @Test + public void test7() { + assertEquals(77, solution1.nonSpecialCount(1, 81)); + } + + @Test + public void test8() { + assertEquals(433, solution1.nonSpecialCount(1, 441)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3234Test.java b/src/test/java/com/fishercoder/fourththousand/_3234Test.java new file mode 100644 index 0000000000..f01b253965 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3234Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3234; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3234Test { + private _3234.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3234.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.numberOfSubstrings("00011")); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3237Test.java b/src/test/java/com/fishercoder/fourththousand/_3237Test.java new file mode 100644 index 0000000000..c4fdc7c7e1 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3237Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.fourththousand._3237; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3237Test { + private _3237.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3237.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {2, 3, 1}, + solution1.simulationResult(new int[] {1, 2, 3}, new int[] {3, 3, 2})); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {3, 1, 4, 2}, + solution1.simulationResult(new int[] {1, 4, 2, 3}, new int[] {4, 1, 3})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3240Test.java b/src/test/java/com/fishercoder/fourththousand/_3240Test.java new file mode 100644 index 0000000000..4b80ebfa93 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3240Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.fourththousand._3240; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3240Test { + private _3240.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3240.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 3, + solution1.minFlips( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,0,0],[0,1,0],[0,0,1]"))); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3241Test.java b/src/test/java/com/fishercoder/fourththousand/_3241Test.java new file mode 100644 index 0000000000..fa47e54217 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3241Test.java @@ -0,0 +1,75 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.fourththousand._3241; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3241Test { + private _3241.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3241.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {4, 6, 3, 5, 5}, + solution1.timeTaken( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[2,4],[0,1],[2,3],[0,2]"))); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] { + 21, 23, 21, 23, 24, 26, 24, 26, 26, 27, 23, 26, 25, 24, 27, 27, 25, 23, 24, 28, + 23, 24, 25, 27, 23, 28, 26, 28, 26, 24, 27, 29, 28, 26, 28, 29, 26, 30, 29, 25, + 23, 25, 27, 31, 25, 30, 30, 29, 23, 23, 25, 26, 23, 29, 32, 27, 30, 30, 28, 30, + 31, 31, 27, 31, 24, 29, 30, 29, 24, 26, 25, 26, 28, 26, 27, 24, 32, 24, 27, 30, + 27, 30, 23, 33, 25, 29, 27, 29, 27, 29, 27, 25, 34, 30, 27, 23, 24, 26, 26, 25, + 30, 30, 27, 27, 26, 31, 30, 23, 25, 27, 26, 26, 29, 26, 31, 30, 33, 26, 25, 27, + 30, 32, 32, 28, 29, 31, 34, 27, 27, 34, 30, 28, 25, 30, 32, 24, 34, 30, 29, 26, + 31, 27, 28, 28, 26, 26, 25, 32, 29, 27, 25, 27, 28, 27, 31, 32, 34, 33, 26, 25, + 33, 25, 29, 28, 29, 29, 27, 36, 31, 25, 24, 27, 28, 35, 34, 26, 33, 30, 25, 28, + 31, 25, 32, 34, 35, 25, 36, 27, 28, 33, 27, 33, 24, 27, 25, 33, 28, 32, 29, 28, + 35, 24, 33, 31, 27, 26, 25, 28, 33, 27, 37, 28, 32, 31, 31, 29, 28, 25, 29, 28, + 36, 30, 31, 29, 29, 32, 29, 32, 26, 26, 34, 27, 24, 29, 28, 32, 25, 38, 33, 26, + 38, 29, 29, 32, 33, 27, 27, 32, 28, 30, 27, 28, 32, 31, 27, 30, 27, 25, 31, 32, + 31, 30, 31, 35, 26, 27, 27, 25, 31, 27, 30, 26, 28, 30, 26, 39, 33, 33, 26, 26, + 33, 30, 31, 36, 27, 31, 33, 31, 26, 27, 31, 29, 27, 28, 31, 29, 34, 33, 34, 30, + 31, 27, 39, 38, 29, 33, 24, 26, 28, 24, 32, 28, 31, 36, 27, 31, 29, 29, 30, 24, + 31, 27, 29, 31, 30, 28, 34, 35, 31, 31, 37, 31, 32, 28, 27, 25, 24, 23, 28, 36, + 24, 36, 33, 36, 32, 28, 27, 36, 29, 31, 33, 30, 31, 33, 33, 35, 31, 34, 28, 28, + 34, 31, 33, 34, 28, 29, 27, 31, 29, 27, 33, 29, 25, 28, 29, 37, 34, 29, 31, 28, + 30, 38, 35, 27, 26, 34, 37, 27, 33, 39, 29, 40, 30, 33, 29, 28, 29, 29, 30, 29, + 31, 35, 30, 23, 35, 28, 28, 26, 25, 31, 30, 30, 26, 27, 33, 28, 28, 29, 30, 32, + 28, 36, 31, 31, 26, 35, 32, 27, 29, 39, 32, 34, 36, 28, 27, 32, 29, 33, 36, 30, + 25, 25, 27, 31, 31, 32, 34, 32, 36, 27, 26, 30, 28, 31, 27, 31, 35, 32, 33, 26, + 32, 30, 25, 33, 31, 28, 35, 32, 27, 32, 27, 32, 37, 30, 28, 26, 28, 29, 31, 29, + 34, 33, 26, 25, 29, 29, 27, 31, 28, 31, 35, 32, 28, 29, 26, 31, 30, 39, 23, 28, + 30, 33, 30, 24, 32, 34, 34, 34, 27, 26, 31, 31, 25, 26, 30, 30, 36, 30, 34, 26, + 29, 34, 28, 28, 32, 30, 28, 31, 31, 32, 26, 31, 26, 29, 29, 35, 32, 29, 26, 29, + 34, 28, 24, 34, 29, 26, 30, 30, 35, 34, 28, 30, 25, 30, 31, 31, 33, 27, 30, 30, + 33, 30, 36, 31, 31, 29, 29, 34, 32, 33, 28, 35, 32, 28, 28, 30, 29, 34, 36, 31, + 38, 32, 35, 36, 30, 29, 30, 31, 30, 27, 28, 29, 27, 31, 27, 31, 25, 33, 33, 31, + 27, 29, 32, 32, 29, 25, 30, 34, 35, 31, 33, 35, 33, 26, 25, 30, 26, 34, 40, 29, + 25, 27, 32, 32, 36, 37, 32, 31, 31, 27, 37, 35, 35, 31, 30, 29, 34, 27, 31, 37, + 29, 27, 31, 28, 34, 31, 28, 31, 30, 37, 34, 32, 38, 31, 30, 33, 29, 37, 30, 28, + 32, 34, 35, 31, 29, 23, 28, 26, 32, 35, 31, 28, 32, 29, 30, 30, 35, 40, 35, 36, + 32, 26, 36, 32, 30, 24, 30, 31, 27, 38, 30, 27, 29, 35, 36, 34, 32, 30, 28, 32, + 27, 27, 26, 35, 35, 29, 33, 31, 28, 32, 32, 25, 27, 27, 31, 31, 28, 29, 36, 30, + 28, 30, 29, 32, 32, 33, 30, 33, 37, 38, 28, 30, 32, 34, 29, 30, 27, 26, 25, 36, + 27, 31, 29, 30, 28, 32, 30, 34, 37, 25, 28, 30, 27, 37, 27, 29, 24, 33, 31, 27, + 35, 29, 40, 32 + }, + solution1.timeTaken( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[159,24],[446,195],[38,14],[36,6],[222,93],[427,50],[604,292],[667,181],[197,66],[247,43],[175,91],[738,503],[251,15],[639,184],[262,79],[535,176],[162,94],[310,106],[445,398],[55,11],[318,65],[516,174],[223,74],[539,74],[624,326],[151,22],[509,39],[383,117],[363,297],[690,590],[452,28],[694,92],[188,28],[41,24],[579,575],[620,24],[725,140],[474,383],[5,4],[471,61],[365,25],[331,177],[640,508],[368,334],[469,423],[467,213],[68,3],[246,205],[214,112],[585,415],[100,34],[403,2],[633,37],[1,0],[629,50],[221,152],[695,54],[238,60],[512,40],[727,114],[420,289],[317,42],[37,31],[282,221],[686,89],[391,275],[105,37],[379,36],[174,122],[244,180],[502,397],[43,38],[500,172],[266,239],[352,57],[426,315],[572,398],[191,121],[483,319],[411,67],[461,31],[429,303],[423,162],[614,319],[437,214],[649,339],[270,89],[264,96],[361,112],[711,21],[739,703],[581,270],[556,155],[290,221],[612,147],[200,129],[393,114],[346,145],[115,32],[677,240],[71,4],[593,37],[325,26],[636,501],[179,15],[335,20],[601,284],[170,49],[204,69],[268,177],[651,410],[141,69],[717,211],[17,0],[249,89],[372,135],[510,461],[97,91],[599,517],[546,533],[133,53],[64,17],[360,134],[261,165],[652,375],[185,21],[643,321],[80,73],[733,626],[436,128],[202,147],[501,147],[330,184],[410,188],[327,244],[497,330],[656,434],[621,279],[158,6],[448,355],[631,612],[626,105],[257,13],[136,76],[718,174],[728,343],[54,43],[607,54],[395,8],[24,2],[373,209],[466,276],[333,151],[753,343],[178,24],[699,495],[648,585],[580,516],[91,48],[301,205],[550,127],[28,6],[83,60],[171,139],[529,487],[82,0],[465,321],[606,493],[687,124],[58,26],[544,433],[329,218],[72,26],[569,180],[737,68],[441,48],[534,292],[697,479],[413,117],[647,575],[710,584],[573,450],[577,460],[416,321],[732,687],[109,5],[189,121],[10,2],[48,2],[145,91],[504,63],[565,379],[491,418],[487,396],[16,13],[519,336],[683,329],[706,247],[531,93],[594,118],[459,161],[463,258],[618,275],[29,1],[563,261],[554,101],[653,561],[750,424],[207,55],[193,146],[138,74],[762,389],[315,101],[560,378],[381,186],[34,23],[507,54],[700,513],[302,237],[680,318],[722,594],[392,317],[488,28],[405,171],[272,158],[78,33],[67,19],[46,32],[583,446],[713,667],[47,14],[730,109],[576,94],[708,701],[693,617],[275,237],[107,0],[553,234],[157,114],[86,73],[271,267],[324,223],[482,185],[755,465],[460,453],[98,68],[578,298],[305,227],[322,74],[745,367],[172,158],[468,108],[595,525],[477,251],[253,221],[338,228],[194,21],[239,41],[183,83],[337,0],[503,337],[49,2],[528,124],[688,681],[545,68],[674,406],[558,377],[417,395],[490,362],[232,17],[386,313],[525,338],[50,29],[359,144],[350,227],[298,122],[51,4],[340,107],[527,299],[18,17],[664,359],[137,32],[388,260],[645,544],[21,1],[440,10],[740,229],[209,97],[366,229],[377,254],[334,12],[14,7],[723,61],[358,26],[409,351],[513,267],[142,110],[407,306],[349,316],[106,87],[94,73],[470,111],[40,2],[240,220],[353,262],[613,161],[242,25],[705,465],[757,300],[684,416],[715,81],[224,74],[380,65],[227,106],[591,90],[149,12],[744,494],[99,40],[284,7],[27,15],[655,638],[35,14],[586,295],[123,98],[742,256],[85,42],[12,10],[165,143],[456,116],[716,171],[116,60],[139,96],[760,706],[627,390],[367,351],[518,463],[743,492],[203,38],[498,2],[313,136],[536,213],[729,639],[375,283],[387,12],[752,117],[273,47],[419,105],[296,122],[511,436],[720,127],[356,81],[508,459],[147,63],[70,29],[486,84],[540,134],[23,11],[124,25],[492,109],[213,148],[390,190],[679,174],[592,139],[431,252],[182,46],[76,61],[254,44],[754,408],[523,301],[364,321],[153,16],[198,123],[286,214],[551,522],[102,12],[736,614],[120,67],[644,195],[161,135],[45,31],[548,507],[241,131],[278,257],[280,154],[567,481],[332,270],[279,6],[543,344],[682,376],[297,258],[382,276],[160,155],[533,345],[252,46],[168,57],[33,18],[394,62],[600,175],[245,117],[291,123],[678,370],[537,207],[662,202],[630,582],[31,27],[538,18],[205,64],[596,48],[685,107],[92,54],[761,736],[668,511],[231,113],[354,60],[433,15],[587,112],[89,30],[439,87],[698,171],[464,451],[444,133],[84,82],[616,335],[559,477],[378,351],[473,58],[235,105],[265,11],[228,169],[57,47],[721,34],[113,6],[114,93],[605,82],[166,11],[236,20],[692,334],[476,98],[432,156],[362,225],[495,115],[2,0],[568,318],[300,148],[707,224],[263,208],[701,175],[385,212],[564,368],[135,3],[619,256],[347,156],[637,146],[402,377],[530,257],[401,357],[293,269],[132,52],[499,28],[449,97],[526,15],[494,64],[309,107],[140,45],[196,9],[52,0],[260,45],[74,7],[304,246],[422,221],[541,384],[622,586],[326,157],[177,65],[164,88],[515,152],[44,40],[169,52],[125,112],[428,102],[646,9],[756,1],[443,322],[675,492],[371,204],[167,156],[13,3],[20,0],[308,231],[749,24],[597,447],[670,281],[243,130],[478,177],[7,6],[584,215],[234,23],[602,253],[219,104],[62,11],[4,3],[303,220],[735,570],[9,7],[186,156],[126,122],[19,9],[220,174],[634,291],[173,129],[520,94],[30,12],[557,407],[588,479],[299,31],[128,71],[480,252],[598,400],[672,546],[726,223],[22,20],[425,370],[342,243],[574,269],[181,13],[625,167],[481,444],[95,2],[289,51],[215,128],[104,68],[281,272],[127,71],[343,230],[163,55],[505,54],[638,322],[681,161],[712,97],[201,107],[561,142],[575,308],[39,20],[562,174],[724,46],[384,306],[657,343],[190,150],[493,345],[517,420],[719,34],[665,0],[152,151],[661,252],[134,105],[285,162],[288,6],[287,261],[450,96],[748,347],[187,132],[746,295],[348,74],[341,298],[714,664],[77,17],[216,119],[294,112],[611,342],[399,74],[184,116],[110,18],[63,45],[255,223],[571,244],[642,517],[111,99],[60,38],[650,344],[319,3],[430,120],[206,77],[454,146],[659,28],[357,305],[6,1],[320,81],[370,214],[566,90],[704,342],[696,361],[555,461],[458,457],[457,213],[61,59],[462,29],[609,198],[376,122],[259,105],[344,105],[156,54],[90,84],[485,284],[122,105],[666,530],[218,62],[230,195],[119,33],[11,4],[731,666],[603,527],[88,5],[339,230],[691,73],[396,293],[734,600],[484,251],[702,6],[42,5],[237,220],[69,4],[208,121],[336,3],[521,54],[8,6],[129,54],[121,100],[763,66],[398,34],[3,2],[408,29],[703,176],[277,225],[112,30],[146,20],[532,18],[590,109],[87,74],[397,88],[154,124],[195,154],[522,289],[199,119],[226,86],[15,5],[211,110],[496,241],[658,493],[258,242],[758,575],[210,200],[628,553],[615,377],[225,56],[323,304],[73,64],[56,34],[673,334],[314,5],[144,4],[438,296],[248,187],[617,536],[274,39],[267,75],[311,289],[747,310],[96,95],[751,705],[689,186],[180,59],[176,154],[475,232],[316,293],[447,253],[65,30],[623,558],[81,72],[307,306],[75,1],[489,115],[66,31],[229,169],[663,374],[117,39],[547,291],[424,170],[150,77],[256,11],[524,489],[269,22],[741,281],[654,591],[118,77],[283,126],[276,168],[295,211],[131,15],[418,142],[25,9],[669,342],[412,181],[570,269],[155,100],[514,364],[292,16],[451,215],[321,145],[93,87],[355,202],[709,527],[130,47],[506,426],[415,8],[59,32],[389,210],[250,44],[328,148],[143,36],[312,304],[404,238],[552,498],[435,409],[103,44],[759,139],[212,106],[53,25],[148,131],[192,17],[660,558],[400,81],[610,528],[217,40],[635,334],[479,325],[306,107],[582,521],[589,113],[542,49],[608,276],[406,321],[641,118],[108,77],[79,47],[101,31],[26,6],[549,305],[632,607],[421,298],[32,15],[472,456],[453,59],[345,187],[374,284],[233,166],[414,225],[671,629],[351,165],[676,431],[455,218],[434,97],[442,69],[369,175]"))); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3243Test.java b/src/test/java/com/fishercoder/fourththousand/_3243Test.java new file mode 100644 index 0000000000..e579faf676 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3243Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.fourththousand._3243; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3243Test { + private _3243.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3243.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {3, 2, 1}, + solution1.shortestDistanceAfterQueries( + 5, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[2,4],[0,2],[0,4]"))); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3258Test.java b/src/test/java/com/fishercoder/fourththousand/_3258Test.java new file mode 100644 index 0000000000..456bd3a729 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3258Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3258; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3258Test { + private _3258.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3258.Solution1(); + } + + @Test + public void test1() { + assertEquals(12, solution1.countKConstraintSubstrings("10101", 1)); + } + + @Test + public void test2() { + assertEquals(25, solution1.countKConstraintSubstrings("1010101", 2)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3270Test.java b/src/test/java/com/fishercoder/fourththousand/_3270Test.java new file mode 100644 index 0000000000..9885df481b --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3270Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3270; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3270Test { + private _3270.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3270.Solution1(); + } + + @Test + public void test1() { + assertEquals(0, solution1.generateKey(1, 10, 1000)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3280Test.java b/src/test/java/com/fishercoder/fourththousand/_3280Test.java new file mode 100644 index 0000000000..79ec27f88e --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3280Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3280; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3280Test { + private _3280.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3280.Solution1(); + } + + @Test + public void test1() { + assertEquals("100000100000-10-11101", solution1.convertDateToBinary("2080-02-29")); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3304Test.java b/src/test/java/com/fishercoder/fourththousand/_3304Test.java new file mode 100644 index 0000000000..a24076021e --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3304Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3304; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3304Test { + private _3304.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3304.Solution1(); + } + + @Test + public void test1() { + assertEquals('b', solution1.kthCharacter(5)); + } + + @Test + public void test2() { + assertEquals('h', solution1.kthCharacter(128)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3318Test.java b/src/test/java/com/fishercoder/fourththousand/_3318Test.java new file mode 100644 index 0000000000..bbc46d55dd --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3318Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.fourththousand._3318; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3318Test { + + private _3318.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _3318.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 1, 2, 2, 3, 4, 2, 3}; + assertArrayEquals(new int[] {6, 10, 12}, solution1.findXSum(nums, 6, 2)); + } + + @Test + public void test2() { + nums = new int[] {3, 8, 7, 8, 7, 5}; + assertArrayEquals(new int[] {11, 15, 15, 15, 12}, solution1.findXSum(nums, 2, 2)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3324Test.java b/src/test/java/com/fishercoder/fourththousand/_3324Test.java new file mode 100644 index 0000000000..b60c66b09f --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3324Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3324; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3324Test { + private _3324.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3324.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("a", "aa", "ab", "aba", "abb", "abc"), + solution1.stringSequence("abc")); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3345Test.java b/src/test/java/com/fishercoder/fourththousand/_3345Test.java new file mode 100644 index 0000000000..9f4f208ee7 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3345Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3345; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3345Test { + private _3345.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3345.Solution1(); + } + + @Test + public void test1() { + assertEquals(10, solution1.smallestNumber(10, 2)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3349Test.java b/src/test/java/com/fishercoder/fourththousand/_3349Test.java new file mode 100644 index 0000000000..23a1162402 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3349Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.fourththousand._3349; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3349Test { + private _3349.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3349.Solution1(); + } + + @Test + public void test1() { + assertTrue( + solution1.hasIncreasingSubarrays(Arrays.asList(2, 5, 7, 8, 9, 2, 3, 4, 3, 1), 3)); + } + + @Test + public void test2() { + assertFalse( + solution1.hasIncreasingSubarrays(Arrays.asList(1, 2, 3, 4, 4, 4, 4, 5, 6, 7), 5)); + } + + @Test + public void test3() { + assertTrue(solution1.hasIncreasingSubarrays(Arrays.asList(5, 8, -2, -1), 2)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3353Test.java b/src/test/java/com/fishercoder/fourththousand/_3353Test.java new file mode 100644 index 0000000000..adf77361d5 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3353Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3353; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3353Test { + private _3353.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3353.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minOperations(new int[] {1, 4, 2})); + } + + @Test + public void test2() { + assertEquals(0, solution1.minOperations(new int[] {10, 10, 10})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3354Test.java b/src/test/java/com/fishercoder/fourththousand/_3354Test.java new file mode 100644 index 0000000000..406395cd2d --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3354Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3354; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3354Test { + private _3354.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3354.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.countValidSelections(new int[] {1, 0, 2, 0, 3})); + } + + @Test + public void test2() { + assertEquals( + 3, solution1.countValidSelections(new int[] {16, 13, 10, 0, 0, 0, 10, 6, 7, 8, 7})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3364Test.java b/src/test/java/com/fishercoder/fourththousand/_3364Test.java new file mode 100644 index 0000000000..80f4d7d955 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3364Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3364; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3364Test { + private _3364.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3364.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minimumSumSubarray(Arrays.asList(3, -2, 1, 4), 2, 3)); + } + + @Test + public void test2() { + assertEquals(8, solution1.minimumSumSubarray(Arrays.asList(-12, 8), 1, 1)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3375Test.java b/src/test/java/com/fishercoder/fourththousand/_3375Test.java new file mode 100644 index 0000000000..46d3bca17f --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3375Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3375; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3375Test { + private _3375.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3375.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minOperations(new int[] {5, 2, 5, 4, 5}, 2)); + } + + @Test + public void test2() { + assertEquals(-1, solution1.minOperations(new int[] {2, 1, 2}, 2)); + } + + @Test + public void test3() { + assertEquals(4, solution1.minOperations(new int[] {9, 7, 5, 3}, 1)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3379Test.java b/src/test/java/com/fishercoder/fourththousand/_3379Test.java new file mode 100644 index 0000000000..2d2daf2c83 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3379Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.fourththousand._3379; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3379Test { + private _3379.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3379.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {1, 1, 1, 3}, + solution1.constructTransformedArray(new int[] {3, -2, 1, 1})); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {-1, -1, 4}, solution1.constructTransformedArray(new int[] {-1, 4, -1})); + } + + @Test + public void test3() { + assertArrayEquals(new int[] {-10}, solution1.constructTransformedArray(new int[] {-10})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3386Test.java b/src/test/java/com/fishercoder/fourththousand/_3386Test.java new file mode 100644 index 0000000000..703d54bb06 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3386Test.java @@ -0,0 +1,53 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.fourththousand._3386; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3386Test { + private _3386.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3386.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 1, + solution1.buttonWithLongestTime( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2],[2,5],[3,9],[1,15]"))); + } + + @Test + public void test2() { + assertEquals( + 2, + solution1.buttonWithLongestTime( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[9,4],[19,5],[2,8],[3,11],[2,15]"))); + } + + @Test + public void test3() { + assertEquals( + 2, + solution1.buttonWithLongestTime( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[7,1],[19,3],[9,4],[12,5],[2,8],[15,10],[18,12],[7,14],[19,16]"))); + } + + @Test + public void test4() { + assertEquals( + 16, + solution1.buttonWithLongestTime( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[5,5],[16,17],[16,19]"))); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3402Test.java b/src/test/java/com/fishercoder/fourththousand/_3402Test.java new file mode 100644 index 0000000000..37a664802e --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3402Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.fourththousand._3402; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3402Test { + private _3402.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3402.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 15, + solution1.minimumOperations( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[3,2],[1,3],[3,4],[0,1]"))); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3471Test.java b/src/test/java/com/fishercoder/fourththousand/_3471Test.java new file mode 100644 index 0000000000..f14977034d --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3471Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3471; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3471Test { + private _3471.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3471.Solution1(); + } + + @Test + public void test1() { + assertEquals(7, solution1.largestInteger(new int[] {3, 9, 2, 1, 7}, 3)); + } + + @Test + public void test2() { + assertEquals(0, solution1.largestInteger(new int[] {0, 0}, 2)); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3491Test.java b/src/test/java/com/fishercoder/fourththousand/_3491Test.java new file mode 100644 index 0000000000..2e8d934bed --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3491Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.fourththousand._3491; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3491Test { + private _3491.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3491.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.phonePrefix(new String[] {"1", "2", "4", "3"})); + } +} diff --git a/src/test/java/com/fishercoder/fourththousand/_3502Test.java b/src/test/java/com/fishercoder/fourththousand/_3502Test.java new file mode 100644 index 0000000000..7159fef0c3 --- /dev/null +++ b/src/test/java/com/fishercoder/fourththousand/_3502Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.fourththousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.fourththousand._3502; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _3502Test { + private _3502.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _3502.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {5, 3, 3, 1, 1, 1}, solution1.minCosts(new int[] {5, 3, 4, 1, 3, 2})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1002Test.java b/src/test/java/com/fishercoder/secondthousand/_1002Test.java new file mode 100644 index 0000000000..8c97da80e1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1002Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1002; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1002Test { + private _1002.Solution1 solution1; + private static String[] A; + + @BeforeEach + public void setup() { + solution1 = new _1002.Solution1(); + } + + @Test + public void test1() { + A = new String[] {"bella", "label", "roller"}; + CommonUtils.print(solution1.commonChars(A)); + } + + @Test + public void test2() { + A = new String[] {"cool", "lock", "cook"}; + CommonUtils.print(solution1.commonChars(A)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1003Test.java b/src/test/java/com/fishercoder/secondthousand/_1003Test.java new file mode 100644 index 0000000000..38aadeef3c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1003Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.secondthousand._1003; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1003Test { + private _1003.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1003.Solution1(); + } + + @Test + public void test1() { + assertTrue(solution1.isValid("aabcbc")); + } + + @Test + public void test2() { + assertTrue(solution1.isValid("abcabcababcc")); + } + + @Test + public void test3() { + assertFalse(solution1.isValid("abccba")); + } + + @Test + public void test4() { + assertFalse(solution1.isValid("cababc")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1004Test.java b/src/test/java/com/fishercoder/secondthousand/_1004Test.java new file mode 100644 index 0000000000..1f1908ba29 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1004Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1004; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1004Test { + private _1004.Solution1 solution1; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _1004.Solution1(); + } + + @Test + public void test1() { + A = new int[] {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0}; + assertEquals(6, solution1.longestOnes(A, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1005Test.java b/src/test/java/com/fishercoder/secondthousand/_1005Test.java new file mode 100644 index 0000000000..c0a58c7af6 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1005Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1005; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1005Test { + private _1005.Solution1 solution1; + private _1005.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1005.Solution1(); + solution2 = new _1005.Solution2(); + } + + @Test + public void test1() { + assertEquals(5, solution1.largestSumAfterKNegations(new int[] {4, 2, 3}, 1)); + assertEquals(5, solution2.largestSumAfterKNegations(new int[] {4, 2, 3}, 1)); + } + + @Test + public void test2() { + assertEquals(6, solution1.largestSumAfterKNegations(new int[] {3, -1, 0, 2}, 3)); + assertEquals(6, solution2.largestSumAfterKNegations(new int[] {3, -1, 0, 2}, 3)); + } + + @Test + public void test3() { + assertEquals(13, solution1.largestSumAfterKNegations(new int[] {2, -3, -1, 5, -4}, 2)); + assertEquals(13, solution2.largestSumAfterKNegations(new int[] {2, -3, -1, 5, -4}, 2)); + } + + @Test + public void test4() { + assertEquals(22, solution1.largestSumAfterKNegations(new int[] {-8, 3, -5, -3, -5, -2}, 6)); + assertEquals(22, solution2.largestSumAfterKNegations(new int[] {-8, 3, -5, -3, -5, -2}, 6)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1008Test.java b/src/test/java/com/fishercoder/secondthousand/_1008Test.java new file mode 100644 index 0000000000..706b05901e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1008Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1008; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _1008Test { + private _1008.Solution1 solution1; + private _1008.Solution2 solution2; + private static int[] preorder; + private static TreeNode expected; + private static TreeNode actual; + + @Test + public void test1() { + solution1 = new _1008.Solution1(); + solution2 = new _1008.Solution2(); + preorder = new int[] {8, 5, 1, 7, 10, 12}; + expected = TreeUtils.constructBinaryTree(Arrays.asList(8, 5, 10, 1, 7, null, 12)); + TreeUtils.printBinaryTree(expected); + actual = solution1.bstFromPreorder(preorder); + TreeUtils.printBinaryTree(actual); + assertEquals(expected, actual); + actual = solution2.bstFromPreorder(preorder); + TreeUtils.printBinaryTree(actual); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1009Test.java b/src/test/java/com/fishercoder/secondthousand/_1009Test.java new file mode 100644 index 0000000000..31c93ec4db --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1009Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1009; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1009Test { + private _1009.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1009.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.bitwiseComplement(5)); + } + + @Test + public void test2() { + assertEquals(5, solution1.bitwiseComplement(10)); + } + + @Test + public void test3() { + assertEquals(0, solution1.bitwiseComplement(7)); + } + + @Test + public void test4() { + assertEquals(3, solution1.bitwiseComplement(12)); + } + + @Test + public void test5() { + assertEquals(1, solution1.bitwiseComplement(0)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1010Test.java b/src/test/java/com/fishercoder/secondthousand/_1010Test.java new file mode 100644 index 0000000000..bdc6b6201b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1010Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1010; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1010Test { + private _1010.Solution1 solution1; + private _1010.Solution2 solution2; + private static int[] time; + + @BeforeEach + public void setup() { + solution1 = new _1010.Solution1(); + solution2 = new _1010.Solution2(); + } + + @Test + public void test1() { + time = new int[] {30, 20, 150, 100, 40}; + assertEquals(3, solution1.numPairsDivisibleBy60(time)); + assertEquals(3, solution2.numPairsDivisibleBy60(time)); + } + + @Test + public void test2() { + time = new int[] {60, 60, 60}; + assertEquals(3, solution1.numPairsDivisibleBy60(time)); + assertEquals(3, solution2.numPairsDivisibleBy60(time)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1011Test.java b/src/test/java/com/fishercoder/secondthousand/_1011Test.java new file mode 100644 index 0000000000..2bd00253cd --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1011Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1011; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1011Test { + private _1011.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1011.Solution1(); + } + + @Test + public void test1() { + int[] weights = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + assertEquals(solution1.shipWithinDays(weights, 5), 15); + } + + @Test + public void test2() { + int[] weights = {3, 2, 2, 4, 1, 4}; + assertEquals(solution1.shipWithinDays(weights, 3), 6); + } + + @Test + public void test3() { + int[] weights = {1, 2, 3, 1, 1}; + assertEquals(solution1.shipWithinDays(weights, 4), 3); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1013Test.java b/src/test/java/com/fishercoder/secondthousand/_1013Test.java new file mode 100644 index 0000000000..3a85d798db --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1013Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1013; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1013Test { + private _1013.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1013.Solution1(); + } + + @Test + public void test1() { + assertEquals( + true, + solution1.canThreePartsEqualSum(new int[] {0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1})); + } + + @Test + public void test2() { + assertEquals( + false, + solution1.canThreePartsEqualSum(new int[] {0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1})); + } + + @Test + public void test3() { + assertEquals( + true, solution1.canThreePartsEqualSum(new int[] {3, 3, 6, 5, -2, 2, 5, 1, -9, 4})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1014Test.java b/src/test/java/com/fishercoder/secondthousand/_1014Test.java new file mode 100644 index 0000000000..93b27c09b3 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1014Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1014; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1014Test { + private _1014.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1014.Solution1(); + } + + @Test + public void test1() { + assertEquals(solution1.maxScoreSightseeingPair(new int[] {1, 3, 5}), 7); + } + + @Test + public void test2() { + assertEquals(solution1.maxScoreSightseeingPair(new int[] {8, 1, 5, 2, 6}), 11); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1018Test.java b/src/test/java/com/fishercoder/secondthousand/_1018Test.java new file mode 100644 index 0000000000..2b2fe86d82 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1018Test.java @@ -0,0 +1,59 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1018; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1018Test { + private _1018.Solution1 solution1; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _1018.Solution1(); + } + + @Test + public void test1() { + A = new int[] {0, 1, 1}; + assertEquals(Arrays.asList(true, false, false), solution1.prefixesDivBy5(A)); + } + + @Test + public void test2() { + A = new int[] {1, 1, 1}; + assertEquals(Arrays.asList(false, false, false), solution1.prefixesDivBy5(A)); + } + + @Test + public void test3() { + A = new int[] {0, 1, 1, 1, 1, 1}; + assertEquals( + Arrays.asList(true, false, false, false, true, false), solution1.prefixesDivBy5(A)); + } + + @Test + public void test4() { + A = new int[] {1, 1, 1, 0, 1}; + assertEquals(Arrays.asList(false, false, false, false, false), solution1.prefixesDivBy5(A)); + } + + @Test + public void test5() { + A = + new int[] { + 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, + 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1 + }; + assertEquals( + Arrays.asList( + false, false, false, false, false, false, false, false, false, false, false, + false, false, false, false, false, false, false, false, false, false, false, + false, false, false, false, false, false, false, false, false, true, false, + false, true, true, true, true, false), + solution1.prefixesDivBy5(A)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1019Test.java b/src/test/java/com/fishercoder/secondthousand/_1019Test.java new file mode 100644 index 0000000000..1f66955246 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1019Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.secondthousand._1019; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1019Test { + private _1019.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1019.Solution1(); + } + + @Test + public void test1() { + ListNode head = LinkedListUtils.contructLinkedList(new int[] {2, 1, 5}); + assertArrayEquals(new int[] {5, 5, 0}, solution1.nextLargerNodes(head)); + } + + @Test + public void test2() { + ListNode head = LinkedListUtils.contructLinkedList(new int[] {2, 7, 4, 3, 5}); + assertArrayEquals(new int[] {7, 0, 5, 5, 0}, solution1.nextLargerNodes(head)); + } + + @Test + public void test3() { + ListNode head = LinkedListUtils.contructLinkedList(new int[] {1, 7, 5, 1, 9, 2, 5, 1}); + assertArrayEquals(new int[] {7, 9, 9, 9, 0, 5, 0, 0}, solution1.nextLargerNodes(head)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1020Test.java b/src/test/java/com/fishercoder/secondthousand/_1020Test.java new file mode 100644 index 0000000000..d168079e37 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1020Test.java @@ -0,0 +1,54 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1020; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1020Test { + private _1020.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1020.Solution1(); + } + + @Test + public void test1() { + int[][] map = { + {0, 0, 0, 0}, + {1, 0, 1, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 0} + }; + + assertEquals(solution1.numEnclaves(map), 3); + } + + @Test + public void test2() { + int[][] map = { + {0, 1, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 0} + }; + + assertEquals(solution1.numEnclaves(map), 0); + } + + @Test + public void test3() { + int[][] map = { + {0, 1, 1, 0}, + {0, 0, 0, 0}, + {1, 0, 1, 0}, + {1, 0, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 0}, + }; + + assertEquals(solution1.numEnclaves(map), 3); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1021Test.java b/src/test/java/com/fishercoder/secondthousand/_1021Test.java new file mode 100644 index 0000000000..0cdfdb0e45 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1021Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1021; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1021Test { + private _1021.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1021.Solution1(); + } + + @Test + public void test1() { + assertEquals("()()()", solution1.removeOuterParentheses("(()())(())")); + } + + @Test + public void test2() { + assertEquals("()()()()(())", solution1.removeOuterParentheses("(()())(())(()(()))")); + } + + @Test + public void test3() { + assertEquals("", solution1.removeOuterParentheses("()()")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1022Test.java b/src/test/java/com/fishercoder/secondthousand/_1022Test.java new file mode 100644 index 0000000000..2c085fe14a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1022Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1022; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1022Test { + private _1022.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _1022.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 1, 0, 1, 0, 1)); + TreeUtils.printBinaryTree(root); + assertEquals(22, solution1.sumRootToLeaf(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1024Test.java b/src/test/java/com/fishercoder/secondthousand/_1024Test.java new file mode 100644 index 0000000000..e6958ebf01 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1024Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1024; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1024Test { + private _1024.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1024.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 3, + solution1.videoStitching( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]"), + 10)); + } + + @Test + public void test2() { + assertEquals( + -1, + solution1.videoStitching( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1],[1,2]"), + 5)); + } + + @Test + public void test3() { + assertEquals( + -1, + solution1.videoStitching( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,2],[4,8]"), + 5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1025Test.java b/src/test/java/com/fishercoder/secondthousand/_1025Test.java new file mode 100644 index 0000000000..cb88c5d17f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1025Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1025; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +@Disabled +public class _1025Test { + private _1025.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1025.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.divisorGame(2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1026Test.java b/src/test/java/com/fishercoder/secondthousand/_1026Test.java new file mode 100644 index 0000000000..c659bf55d7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1026Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1026; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _1026Test { + private _1026.Solution1 solution1; + private static TreeNode root; + + @Test + public void test1() { + solution1 = new _1026.Solution1(); + root = + TreeUtils.constructBinaryTree( + Arrays.asList(8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13)); + assertEquals(7, solution1.maxAncestorDiff(root)); + } + + @Test + public void test2() { + solution1 = new _1026.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2, null, 0, 3)); + assertEquals(3, solution1.maxAncestorDiff(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1029Test.java b/src/test/java/com/fishercoder/secondthousand/_1029Test.java new file mode 100644 index 0000000000..eb1fbaf719 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1029Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1029; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1029Test { + private _1029.Solution1 solution1; + private static int[][] costs; + + @BeforeEach + public void setup() { + solution1 = new _1029.Solution1(); + } + + @Test + public void test1() { + costs = + new int[][] { + {10, 20}, + {30, 200}, + {400, 50}, + {30, 20} + }; + assertEquals(110, solution1.twoCitySchedCost(costs)); + } + + @Test + public void test2() { + costs = + new int[][] { + {259, 770}, + {448, 54}, + {926, 667}, + {184, 139}, + {840, 118}, + {577, 469} + }; + assertEquals(1859, solution1.twoCitySchedCost(costs)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1030Test.java b/src/test/java/com/fishercoder/secondthousand/_1030Test.java new file mode 100644 index 0000000000..d309a49d44 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1030Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1030; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1030Test { + private _1030.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1030.Solution1(); + } + + @Test + public void test1() { + CommonUtils.print2DIntArray(solution1.allCellsDistOrder(1, 2, 0, 0)); + } + + @Test + public void test2() { + CommonUtils.print2DIntArray(solution1.allCellsDistOrder(2, 2, 0, 1)); + } + + @Test + public void test3() { + CommonUtils.print2DIntArray(solution1.allCellsDistOrder(2, 3, 1, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1033Test.java b/src/test/java/com/fishercoder/secondthousand/_1033Test.java new file mode 100644 index 0000000000..a7c0393962 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1033Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1033; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1033Test { + private _1033.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1033.Solution1(); + } + + @Test + public void test1() { + int[] expected = {1, 2}; + assertArrayEquals(expected, solution1.numMovesStones(1, 2, 5)); + } + + @Test + public void test2() { + int[] expected = {0, 0}; + assertArrayEquals(expected, solution1.numMovesStones(4, 3, 2)); + } + + @Test + public void test3() { + int[] expected = {1, 2}; + assertArrayEquals(expected, solution1.numMovesStones(3, 5, 1)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1034Test.java b/src/test/java/com/fishercoder/secondthousand/_1034Test.java new file mode 100644 index 0000000000..9c59a217c2 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1034Test.java @@ -0,0 +1,55 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1034; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1034Test { + private _1034.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1034.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray("[3,3],[3,2]"), + solution1.colorBorder( + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[1,1],[1,2]"), + 0, + 0, + 3)); + } + + @Test + public void test2() { + assertArrayEquals( + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[1,2,1],[1,2,2],[2,2,1]"), + solution1.colorBorder( + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[1,2,1],[1,2,2],[2,2,1]"), + 1, + 1, + 2)); + } + + @Test + public void test3() { + assertArrayEquals( + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[1,1,1,1,1,2],[1,2,1,1,1,2],[1,1,1,1,1,2]"), + solution1.colorBorder( + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[1,2,1,2,1,2],[2,2,2,2,1,2],[1,2,2,2,1,2]"), + 1, + 3, + 1)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1037Test.java b/src/test/java/com/fishercoder/secondthousand/_1037Test.java new file mode 100644 index 0000000000..90d2bd14a9 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1037Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1037; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1037Test { + private _1037.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1037.Solution1(); + } + + @Test + public void test1() { + assertEquals( + true, + solution1.isBoomerang( + new int[][] {new int[] {1, 1}, new int[] {2, 3}, new int[] {3, 2}})); + } + + @Test + public void test2() { + assertEquals( + false, + solution1.isBoomerang( + new int[][] {new int[] {1, 1}, new int[] {2, 2}, new int[] {3, 3}})); + } + + @Test + public void test3() { + assertEquals( + true, + solution1.isBoomerang( + new int[][] {new int[] {0, 0}, new int[] {0, 2}, new int[] {2, 1}})); + } + + @Test + public void test4() { + assertEquals( + false, + solution1.isBoomerang( + new int[][] {new int[] {0, 0}, new int[] {1, 1}, new int[] {1, 1}})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1038Test.java b/src/test/java/com/fishercoder/secondthousand/_1038Test.java new file mode 100644 index 0000000000..56c9fa9ccd --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1038Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1038; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1038Test { + + private _1038.Solution1 solution1; + private static TreeNode root; + private static TreeNode expected; + private static TreeNode actual; + + @BeforeEach + public void setup() { + solution1 = new _1038.Solution1(); + } + + @Test + public void test1() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 4, 1, 6, 0, 2, 5, 7, null, null, null, 3, null, null, null, 8)); + TreeUtils.printBinaryTree(root); + expected = + TreeUtils.constructBinaryTree( + Arrays.asList( + 30, 36, 21, 36, 35, 26, 15, null, null, null, 33, null, null, null, + 8)); + TreeUtils.printBinaryTree(expected); + actual = solution1.bstToGst(root); + TreeUtils.printBinaryTree(actual); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1043Test.java b/src/test/java/com/fishercoder/secondthousand/_1043Test.java new file mode 100644 index 0000000000..f4f43d15ee --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1043Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1043; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1043Test { + private _1043.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1043.Solution1(); + } + + @Test + public void test1() { + assertEquals(84, solution1.maxSumAfterPartitioning(new int[] {1, 15, 7, 9, 2, 5, 10}, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1046Test.java b/src/test/java/com/fishercoder/secondthousand/_1046Test.java new file mode 100644 index 0000000000..acc6d78161 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1046Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1046; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1046Test { + private _1046.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1046.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.lastStoneWeight(new int[] {2, 7, 4, 1, 8, 1})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1047Test.java b/src/test/java/com/fishercoder/secondthousand/_1047Test.java new file mode 100644 index 0000000000..8b7935c669 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1047Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1047; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1047Test { + private _1047.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1047.Solution1(); + } + + @Test + public void test1() { + assertEquals("ca", solution1.removeDuplicates("abbaca")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1049Test.java b/src/test/java/com/fishercoder/secondthousand/_1049Test.java new file mode 100644 index 0000000000..f327ac1996 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1049Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1049; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1049Test { + private _1049.Solution1 solution1; + private static int[] stones; + + @BeforeEach + public void setup() { + solution1 = new _1049.Solution1(); + } + + @Test + public void test1() { + stones = new int[] {2, 7, 4, 1, 8, 1}; + assertEquals(1, solution1.lastStoneWeightII(stones)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1051Test.java b/src/test/java/com/fishercoder/secondthousand/_1051Test.java new file mode 100644 index 0000000000..7270dbfc5f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1051Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1051; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1051Test { + private _1051.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1051.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.heightChecker(new int[] {1, 1, 4, 2, 1, 3})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1055Test.java b/src/test/java/com/fishercoder/secondthousand/_1055Test.java new file mode 100644 index 0000000000..39432fdd31 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1055Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1055; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1055Test { + private _1055.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1055.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.fixedPoint(new int[] {-10, -5, 0, 3, 7})); + } + + @Test + public void test2() { + assertEquals(0, solution1.fixedPoint(new int[] {0, 2, 5, 8, 17})); + } + + @Test + public void test3() { + assertEquals(-1, solution1.fixedPoint(new int[] {-10, -5, 3, 4, 7, 9})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1056Test.java b/src/test/java/com/fishercoder/secondthousand/_1056Test.java new file mode 100644 index 0000000000..570eed43e5 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1056Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1056; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1056Test { + private _1056.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1056.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.confusingNumber(6)); + } + + @Test + public void test2() { + assertEquals(true, solution1.confusingNumber(89)); + } + + @Test + public void test3() { + assertEquals(false, solution1.confusingNumber(11)); + } + + @Test + public void test4() { + assertEquals(false, solution1.confusingNumber(25)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1057Test.java b/src/test/java/com/fishercoder/secondthousand/_1057Test.java new file mode 100644 index 0000000000..8a627a392e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1057Test.java @@ -0,0 +1,50 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1057; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1057Test { + private _1057.Solution1 solution1; + private static int[][] workers; + private static int[][] bikes; + + @BeforeEach + public void setup() { + solution1 = new _1057.Solution1(); + } + + @Test + public void test1() { + workers = + new int[][] { + {0, 0}, + {2, 1}, + }; + bikes = + new int[][] { + {1, 2}, + {3, 3}, + }; + assertArrayEquals(new int[] {1, 0}, solution1.assignBikes(workers, bikes)); + } + + @Test + public void test2() { + workers = + new int[][] { + {0, 0}, + {1, 1}, + {2, 0}, + }; + bikes = + new int[][] { + {1, 0}, + {2, 2}, + {2, 1}, + }; + assertArrayEquals(new int[] {0, 2, 1}, solution1.assignBikes(workers, bikes)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1059Test.java b/src/test/java/com/fishercoder/secondthousand/_1059Test.java new file mode 100644 index 0000000000..cc71d9caf2 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1059Test.java @@ -0,0 +1,83 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1059; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1059Test { + private _1059.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1059.Solution1(); + } + + @Test + public void test1() { + assertFalse( + solution1.leadsToDestination( + 3, + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[0,1],[0,2]"), + 0, + 2)); + } + + @Test + public void test2() { + assertFalse( + solution1.leadsToDestination( + 4, + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[0,1],[0,3],[1,2],[2,1]"), + 0, + 3)); + } + + @Test + public void test3() { + assertTrue( + solution1.leadsToDestination( + 4, + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[0,1],[0,2],[1,3],[2,3]"), + 0, + 3)); + } + + @Test + public void test4() { + assertFalse( + solution1.leadsToDestination( + 2, + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[0,1],[1,1]"), + 0, + 1)); + } + + @Test + public void test5() { + assertFalse(solution1.leadsToDestination(2, new int[][] {}, 0, 1)); + } + + @Test + public void test6() { + assertTrue(solution1.leadsToDestination(1, new int[][] {}, 0, 0)); + } + + @Test + public void test7() { + assertTrue( + solution1.leadsToDestination( + 100, + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[0,16],[0,17],[0,18],[0,19],[0,20],[0,21],[0,22],[0,23],[0,24],[0,25],[0,26],[0,27],[0,28],[0,29],[0,30],[0,31],[0,32],[0,33],[0,34],[0,35],[0,36],[0,37],[0,38],[0,39],[0,40],[0,41],[0,42],[0,43],[0,44],[0,45],[0,46],[0,47],[0,48],[0,49],[0,50],[0,51],[0,52],[0,53],[0,54],[0,55],[0,56],[0,57],[0,58],[0,59],[0,60],[0,61],[0,62],[0,63],[0,64],[0,65],[0,66],[0,67],[0,68],[0,69],[0,70],[0,71],[0,72],[0,73],[0,74],[0,75],[0,76],[0,77],[0,78],[0,79],[0,80],[0,81],[0,82],[0,83],[0,84],[0,85],[0,86],[0,87],[0,88],[0,89],[0,90],[0,91],[0,92],[0,93],[0,94],[0,95],[0,96],[0,97],[0,98],[0,99],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10],[1,11],[1,12],[1,13],[1,14],[1,15],[1,16],[1,17],[1,18],[1,19],[1,20],[1,21],[1,22],[1,23],[1,24],[1,25],[1,26],[1,27],[1,28],[1,29],[1,30],[1,31],[1,32],[1,33],[1,34],[1,35],[1,36],[1,37],[1,38],[1,39],[1,40],[1,41],[1,42],[1,43],[1,44],[1,45],[1,46],[1,47],[1,48],[1,49],[1,50],[1,51],[1,52],[1,53],[1,54],[1,55],[1,56],[1,57],[1,58],[1,59],[1,60],[1,61],[1,62],[1,63],[1,64],[1,65],[1,66],[1,67],[1,68],[1,69],[1,70],[1,71],[1,72],[1,73],[1,74],[1,75],[1,76],[1,77],[1,78],[1,79],[1,80],[1,81],[1,82],[1,83],[1,84],[1,85],[1,86],[1,87],[1,88],[1,89],[1,90],[1,91],[1,92],[1,93],[1,94],[1,95],[1,96],[1,97],[1,98],[1,99],[2,3],[2,4],[2,5],[2,6],[2,7],[2,8],[2,9],[2,10],[2,11],[2,12],[2,13],[2,14],[2,15],[2,16],[2,17],[2,18],[2,19],[2,20],[2,21],[2,22],[2,23],[2,24],[2,25],[2,26],[2,27],[2,28],[2,29],[2,30],[2,31],[2,32],[2,33],[2,34],[2,35],[2,36],[2,37],[2,38],[2,39],[2,40],[2,41],[2,42],[2,43],[2,44],[2,45],[2,46],[2,47],[2,48],[2,49],[2,50],[2,51],[2,52],[2,53],[2,54],[2,55],[2,56],[2,57],[2,58],[2,59],[2,60],[2,61],[2,62],[2,63],[2,64],[2,65],[2,66],[2,67],[2,68],[2,69],[2,70],[2,71],[2,72],[2,73],[2,74],[2,75],[2,76],[2,77],[2,78],[2,79],[2,80],[2,81],[2,82],[2,83],[2,84],[2,85],[2,86],[2,87],[2,88],[2,89],[2,90],[2,91],[2,92],[2,93],[2,94],[2,95],[2,96],[2,97],[2,98],[2,99],[3,4],[3,5],[3,6],[3,7],[3,8],[3,9],[3,10],[3,11],[3,12],[3,13],[3,14],[3,15],[3,16],[3,17],[3,18],[3,19],[3,20],[3,21],[3,22],[3,23],[3,24],[3,25],[3,26],[3,27],[3,28],[3,29],[3,30],[3,31],[3,32],[3,33],[3,34],[3,35],[3,36],[3,37],[3,38],[3,39],[3,40],[3,41],[3,42],[3,43],[3,44],[3,45],[3,46],[3,47],[3,48],[3,49],[3,50],[3,51],[3,52],[3,53],[3,54],[3,55],[3,56],[3,57],[3,58],[3,59],[3,60],[3,61],[3,62],[3,63],[3,64],[3,65],[3,66],[3,67],[3,68],[3,69],[3,70],[3,71],[3,72],[3,73],[3,74],[3,75],[3,76],[3,77],[3,78],[3,79],[3,80],[3,81],[3,82],[3,83],[3,84],[3,85],[3,86],[3,87],[3,88],[3,89],[3,90],[3,91],[3,92],[3,93],[3,94],[3,95],[3,96],[3,97],[3,98],[3,99],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],[4,11],[4,12],[4,13],[4,14],[4,15],[4,16],[4,17],[4,18],[4,19],[4,20],[4,21],[4,22],[4,23],[4,24],[4,25],[4,26],[4,27],[4,28],[4,29],[4,30],[4,31],[4,32],[4,33],[4,34],[4,35],[4,36],[4,37],[4,38],[4,39],[4,40],[4,41],[4,42],[4,43],[4,44],[4,45],[4,46],[4,47],[4,48],[4,49],[4,50],[4,51],[4,52],[4,53],[4,54],[4,55],[4,56],[4,57],[4,58],[4,59],[4,60],[4,61],[4,62],[4,63],[4,64],[4,65],[4,66],[4,67],[4,68],[4,69],[4,70],[4,71],[4,72],[4,73],[4,74],[4,75],[4,76],[4,77],[4,78],[4,79],[4,80],[4,81],[4,82],[4,83],[4,84],[4,85],[4,86],[4,87],[4,88],[4,89],[4,90],[4,91],[4,92],[4,93],[4,94],[4,95],[4,96],[4,97],[4,98],[4,99],[5,6],[5,7],[5,8],[5,9],[5,10],[5,11],[5,12],[5,13],[5,14],[5,15],[5,16],[5,17],[5,18],[5,19],[5,20],[5,21],[5,22],[5,23],[5,24],[5,25],[5,26],[5,27],[5,28],[5,29],[5,30],[5,31],[5,32],[5,33],[5,34],[5,35],[5,36],[5,37],[5,38],[5,39],[5,40],[5,41],[5,42],[5,43],[5,44],[5,45],[5,46],[5,47],[5,48],[5,49],[5,50],[5,51],[5,52],[5,53],[5,54],[5,55],[5,56],[5,57],[5,58],[5,59],[5,60],[5,61],[5,62],[5,63],[5,64],[5,65],[5,66],[5,67],[5,68],[5,69],[5,70],[5,71],[5,72],[5,73],[5,74],[5,75],[5,76],[5,77],[5,78],[5,79],[5,80],[5,81],[5,82],[5,83],[5,84],[5,85],[5,86],[5,87],[5,88],[5,89],[5,90],[5,91],[5,92],[5,93],[5,94],[5,95],[5,96],[5,97],[5,98],[5,99],[6,7],[6,8],[6,9],[6,10],[6,11],[6,12],[6,13],[6,14],[6,15],[6,16],[6,17],[6,18],[6,19],[6,20],[6,21],[6,22],[6,23],[6,24],[6,25],[6,26],[6,27],[6,28],[6,29],[6,30],[6,31],[6,32],[6,33],[6,34],[6,35],[6,36],[6,37],[6,38],[6,39],[6,40],[6,41],[6,42],[6,43],[6,44],[6,45],[6,46],[6,47],[6,48],[6,49],[6,50],[6,51],[6,52],[6,53],[6,54],[6,55],[6,56],[6,57],[6,58],[6,59],[6,60],[6,61],[6,62],[6,63],[6,64],[6,65],[6,66],[6,67],[6,68],[6,69],[6,70],[6,71],[6,72],[6,73],[6,74],[6,75],[6,76],[6,77],[6,78],[6,79],[6,80],[6,81],[6,82],[6,83],[6,84],[6,85],[6,86],[6,87],[6,88],[6,89],[6,90],[6,91],[6,92],[6,93],[6,94],[6,95],[6,96],[6,97],[6,98],[6,99],[7,8],[7,9],[7,10],[7,11],[7,12],[7,13],[7,14],[7,15],[7,16],[7,17],[7,18],[7,19],[7,20],[7,21],[7,22],[7,23],[7,24],[7,25],[7,26],[7,27],[7,28],[7,29],[7,30],[7,31],[7,32],[7,33],[7,34],[7,35],[7,36],[7,37],[7,38],[7,39],[7,40],[7,41],[7,42],[7,43],[7,44],[7,45],[7,46],[7,47],[7,48],[7,49],[7,50],[7,51],[7,52],[7,53],[7,54],[7,55],[7,56],[7,57],[7,58],[7,59],[7,60],[7,61],[7,62],[7,63],[7,64],[7,65],[7,66],[7,67],[7,68],[7,69],[7,70],[7,71],[7,72],[7,73],[7,74],[7,75],[7,76],[7,77],[7,78],[7,79],[7,80],[7,81],[7,82],[7,83],[7,84],[7,85],[7,86],[7,87],[7,88],[7,89],[7,90],[7,91],[7,92],[7,93],[7,94],[7,95],[7,96],[7,97],[7,98],[7,99],[8,9],[8,10],[8,11],[8,12],[8,13],[8,14],[8,15],[8,16],[8,17],[8,18],[8,19],[8,20],[8,21],[8,22],[8,23],[8,24],[8,25],[8,26],[8,27],[8,28],[8,29],[8,30],[8,31],[8,32],[8,33],[8,34],[8,35],[8,36],[8,37],[8,38],[8,39],[8,40],[8,41],[8,42],[8,43],[8,44],[8,45],[8,46],[8,47],[8,48],[8,49],[8,50],[8,51],[8,52],[8,53],[8,54],[8,55],[8,56],[8,57],[8,58],[8,59],[8,60],[8,61],[8,62],[8,63],[8,64],[8,65],[8,66],[8,67],[8,68],[8,69],[8,70],[8,71],[8,72],[8,73],[8,74],[8,75],[8,76],[8,77],[8,78],[8,79],[8,80],[8,81],[8,82],[8,83],[8,84],[8,85],[8,86],[8,87],[8,88],[8,89],[8,90],[8,91],[8,92],[8,93],[8,94],[8,95],[8,96],[8,97],[8,98],[8,99],[9,10],[9,11],[9,12],[9,13],[9,14],[9,15],[9,16],[9,17],[9,18],[9,19],[9,20],[9,21],[9,22],[9,23],[9,24],[9,25],[9,26],[9,27],[9,28],[9,29],[9,30],[9,31],[9,32],[9,33],[9,34],[9,35],[9,36],[9,37],[9,38],[9,39],[9,40],[9,41],[9,42],[9,43],[9,44],[9,45],[9,46],[9,47],[9,48],[9,49],[9,50],[9,51],[9,52],[9,53],[9,54],[9,55],[9,56],[9,57],[9,58],[9,59],[9,60],[9,61],[9,62],[9,63],[9,64],[9,65],[9,66],[9,67],[9,68],[9,69],[9,70],[9,71],[9,72],[9,73],[9,74],[9,75],[9,76],[9,77],[9,78],[9,79],[9,80],[9,81],[9,82],[9,83],[9,84],[9,85],[9,86],[9,87],[9,88],[9,89],[9,90],[9,91],[9,92],[9,93],[9,94],[9,95],[9,96],[9,97],[9,98],[9,99],[10,11],[10,12],[10,13],[10,14],[10,15],[10,16],[10,17],[10,18],[10,19],[10,20],[10,21],[10,22],[10,23],[10,24],[10,25],[10,26],[10,27],[10,28],[10,29],[10,30],[10,31],[10,32],[10,33],[10,34],[10,35],[10,36],[10,37],[10,38],[10,39],[10,40],[10,41],[10,42],[10,43],[10,44],[10,45],[10,46],[10,47],[10,48],[10,49],[10,50],[10,51],[10,52],[10,53],[10,54],[10,55],[10,56],[10,57],[10,58],[10,59],[10,60],[10,61],[10,62],[10,63],[10,64],[10,65],[10,66],[10,67],[10,68],[10,69],[10,70],[10,71],[10,72],[10,73],[10,74],[10,75],[10,76],[10,77],[10,78],[10,79],[10,80],[10,81],[10,82],[10,83],[10,84],[10,85],[10,86],[10,87],[10,88],[10,89],[10,90],[10,91],[10,92],[10,93],[10,94],[10,95],[10,96],[10,97],[10,98],[10,99],[11,12],[11,13],[11,14],[11,15],[11,16],[11,17],[11,18],[11,19],[11,20],[11,21],[11,22],[11,23],[11,24],[11,25],[11,26],[11,27],[11,28],[11,29],[11,30],[11,31],[11,32],[11,33],[11,34],[11,35],[11,36],[11,37],[11,38],[11,39],[11,40],[11,41],[11,42],[11,43],[11,44],[11,45],[11,46],[11,47],[11,48],[11,49],[11,50],[11,51],[11,52],[11,53],[11,54],[11,55],[11,56],[11,57],[11,58],[11,59],[11,60],[11,61],[11,62],[11,63],[11,64],[11,65],[11,66],[11,67],[11,68],[11,69],[11,70],[11,71],[11,72],[11,73],[11,74],[11,75],[11,76],[11,77],[11,78],[11,79],[11,80],[11,81],[11,82],[11,83],[11,84],[11,85],[11,86],[11,87],[11,88],[11,89],[11,90],[11,91],[11,92],[11,93],[11,94],[11,95],[11,96],[11,97],[11,98],[11,99],[12,13],[12,14],[12,15],[12,16],[12,17],[12,18],[12,19],[12,20],[12,21],[12,22],[12,23],[12,24],[12,25],[12,26],[12,27],[12,28],[12,29],[12,30],[12,31],[12,32],[12,33],[12,34],[12,35],[12,36],[12,37],[12,38],[12,39],[12,40],[12,41],[12,42],[12,43],[12,44],[12,45],[12,46],[12,47],[12,48],[12,49],[12,50],[12,51],[12,52],[12,53],[12,54],[12,55],[12,56],[12,57],[12,58],[12,59],[12,60],[12,61],[12,62],[12,63],[12,64],[12,65],[12,66],[12,67],[12,68],[12,69],[12,70],[12,71],[12,72],[12,73],[12,74],[12,75],[12,76],[12,77],[12,78],[12,79],[12,80],[12,81],[12,82],[12,83],[12,84],[12,85],[12,86],[12,87],[12,88],[12,89],[12,90],[12,91],[12,92],[12,93],[12,94],[12,95],[12,96],[12,97],[12,98],[12,99],[13,14],[13,15],[13,16],[13,17],[13,18],[13,19],[13,20],[13,21],[13,22],[13,23],[13,24],[13,25],[13,26],[13,27],[13,28],[13,29],[13,30],[13,31],[13,32],[13,33],[13,34],[13,35],[13,36],[13,37],[13,38],[13,39],[13,40],[13,41],[13,42],[13,43],[13,44],[13,45],[13,46],[13,47],[13,48],[13,49],[13,50],[13,51],[13,52],[13,53],[13,54],[13,55],[13,56],[13,57],[13,58],[13,59],[13,60],[13,61],[13,62],[13,63],[13,64],[13,65],[13,66],[13,67],[13,68],[13,69],[13,70],[13,71],[13,72],[13,73],[13,74],[13,75],[13,76],[13,77],[13,78],[13,79],[13,80],[13,81],[13,82],[13,83],[13,84],[13,85],[13,86],[13,87],[13,88],[13,89],[13,90],[13,91],[13,92],[13,93],[13,94],[13,95],[13,96],[13,97],[13,98],[13,99],[14,15],[14,16],[14,17],[14,18],[14,19],[14,20],[14,21],[14,22],[14,23],[14,24],[14,25],[14,26],[14,27],[14,28],[14,29],[14,30],[14,31],[14,32],[14,33],[14,34],[14,35],[14,36],[14,37],[14,38],[14,39],[14,40],[14,41],[14,42],[14,43],[14,44],[14,45],[14,46],[14,47],[14,48],[14,49],[14,50],[14,51],[14,52],[14,53],[14,54],[14,55],[14,56],[14,57],[14,58],[14,59],[14,60],[14,61],[14,62],[14,63],[14,64],[14,65],[14,66],[14,67],[14,68],[14,69],[14,70],[14,71],[14,72],[14,73],[14,74],[14,75],[14,76],[14,77],[14,78],[14,79],[14,80],[14,81],[14,82],[14,83],[14,84],[14,85],[14,86],[14,87],[14,88],[14,89],[14,90],[14,91],[14,92],[14,93],[14,94],[14,95],[14,96],[14,97],[14,98],[14,99],[15,16],[15,17],[15,18],[15,19],[15,20],[15,21],[15,22],[15,23],[15,24],[15,25],[15,26],[15,27],[15,28],[15,29],[15,30],[15,31],[15,32],[15,33],[15,34],[15,35],[15,36],[15,37],[15,38],[15,39],[15,40],[15,41],[15,42],[15,43],[15,44],[15,45],[15,46],[15,47],[15,48],[15,49],[15,50],[15,51],[15,52],[15,53],[15,54],[15,55],[15,56],[15,57],[15,58],[15,59],[15,60],[15,61],[15,62],[15,63],[15,64],[15,65],[15,66],[15,67],[15,68],[15,69],[15,70],[15,71],[15,72],[15,73],[15,74],[15,75],[15,76],[15,77],[15,78],[15,79],[15,80],[15,81],[15,82],[15,83],[15,84],[15,85],[15,86],[15,87],[15,88],[15,89],[15,90],[15,91],[15,92],[15,93],[15,94],[15,95],[15,96],[15,97],[15,98],[15,99],[16,17],[16,18],[16,19],[16,20],[16,21],[16,22],[16,23],[16,24],[16,25],[16,26],[16,27],[16,28],[16,29],[16,30],[16,31],[16,32],[16,33],[16,34],[16,35],[16,36],[16,37],[16,38],[16,39],[16,40],[16,41],[16,42],[16,43],[16,44],[16,45],[16,46],[16,47],[16,48],[16,49],[16,50],[16,51],[16,52],[16,53],[16,54],[16,55],[16,56],[16,57],[16,58],[16,59],[16,60],[16,61],[16,62],[16,63],[16,64],[16,65],[16,66],[16,67],[16,68],[16,69],[16,70],[16,71],[16,72],[16,73],[16,74],[16,75],[16,76],[16,77],[16,78],[16,79],[16,80],[16,81],[16,82],[16,83],[16,84],[16,85],[16,86],[16,87],[16,88],[16,89],[16,90],[16,91],[16,92],[16,93],[16,94],[16,95],[16,96],[16,97],[16,98],[16,99],[17,18],[17,19],[17,20],[17,21],[17,22],[17,23],[17,24],[17,25],[17,26],[17,27],[17,28],[17,29],[17,30],[17,31],[17,32],[17,33],[17,34],[17,35],[17,36],[17,37],[17,38],[17,39],[17,40],[17,41],[17,42],[17,43],[17,44],[17,45],[17,46],[17,47],[17,48],[17,49],[17,50],[17,51],[17,52],[17,53],[17,54],[17,55],[17,56],[17,57],[17,58],[17,59],[17,60],[17,61],[17,62],[17,63],[17,64],[17,65],[17,66],[17,67],[17,68],[17,69],[17,70],[17,71],[17,72],[17,73],[17,74],[17,75],[17,76],[17,77],[17,78],[17,79],[17,80],[17,81],[17,82],[17,83],[17,84],[17,85],[17,86],[17,87],[17,88],[17,89],[17,90],[17,91],[17,92],[17,93],[17,94],[17,95],[17,96],[17,97],[17,98],[17,99],[18,19],[18,20],[18,21],[18,22],[18,23],[18,24],[18,25],[18,26],[18,27],[18,28],[18,29],[18,30],[18,31],[18,32],[18,33],[18,34],[18,35],[18,36],[18,37],[18,38],[18,39],[18,40],[18,41],[18,42],[18,43],[18,44],[18,45],[18,46],[18,47],[18,48],[18,49],[18,50],[18,51],[18,52],[18,53],[18,54],[18,55],[18,56],[18,57],[18,58],[18,59],[18,60],[18,61],[18,62],[18,63],[18,64],[18,65],[18,66],[18,67],[18,68],[18,69],[18,70],[18,71],[18,72],[18,73],[18,74],[18,75],[18,76],[18,77],[18,78],[18,79],[18,80],[18,81],[18,82],[18,83],[18,84],[18,85],[18,86],[18,87],[18,88],[18,89],[18,90],[18,91],[18,92],[18,93],[18,94],[18,95],[18,96],[18,97],[18,98],[18,99],[19,20],[19,21],[19,22],[19,23],[19,24],[19,25],[19,26],[19,27],[19,28],[19,29],[19,30],[19,31],[19,32],[19,33],[19,34],[19,35],[19,36],[19,37],[19,38],[19,39],[19,40],[19,41],[19,42],[19,43],[19,44],[19,45],[19,46],[19,47],[19,48],[19,49],[19,50],[19,51],[19,52],[19,53],[19,54],[19,55],[19,56],[19,57],[19,58],[19,59],[19,60],[19,61],[19,62],[19,63],[19,64],[19,65],[19,66],[19,67],[19,68],[19,69],[19,70],[19,71],[19,72],[19,73],[19,74],[19,75],[19,76],[19,77],[19,78],[19,79],[19,80],[19,81],[19,82],[19,83],[19,84],[19,85],[19,86],[19,87],[19,88],[19,89],[19,90],[19,91],[19,92],[19,93],[19,94],[19,95],[19,96],[19,97],[19,98],[19,99],[20,21],[20,22],[20,23],[20,24],[20,25],[20,26],[20,27],[20,28],[20,29],[20,30],[20,31],[20,32],[20,33],[20,34],[20,35],[20,36],[20,37],[20,38],[20,39],[20,40],[20,41],[20,42],[20,43],[20,44],[20,45],[20,46],[20,47],[20,48],[20,49],[20,50],[20,51],[20,52],[20,53],[20,54],[20,55],[20,56],[20,57],[20,58],[20,59],[20,60],[20,61],[20,62],[20,63],[20,64],[20,65],[20,66],[20,67],[20,68],[20,69],[20,70],[20,71],[20,72],[20,73],[20,74],[20,75],[20,76],[20,77],[20,78],[20,79],[20,80],[20,81],[20,82],[20,83],[20,84],[20,85],[20,86],[20,87],[20,88],[20,89],[20,90],[20,91],[20,92],[20,93],[20,94],[20,95],[20,96],[20,97],[20,98],[20,99],[21,22],[21,23],[21,24],[21,25],[21,26],[21,27],[21,28],[21,29],[21,30],[21,31],[21,32],[21,33],[21,34],[21,35],[21,36],[21,37],[21,38],[21,39],[21,40],[21,41],[21,42],[21,43],[21,44],[21,45],[21,46],[21,47],[21,48],[21,49],[21,50],[21,51],[21,52],[21,53],[21,54],[21,55],[21,56],[21,57],[21,58],[21,59],[21,60],[21,61],[21,62],[21,63],[21,64],[21,65],[21,66],[21,67],[21,68],[21,69],[21,70],[21,71],[21,72],[21,73],[21,74],[21,75],[21,76],[21,77],[21,78],[21,79],[21,80],[21,81],[21,82],[21,83],[21,84],[21,85],[21,86],[21,87],[21,88],[21,89],[21,90],[21,91],[21,92],[21,93],[21,94],[21,95],[21,96],[21,97],[21,98],[21,99],[22,23],[22,24],[22,25],[22,26],[22,27],[22,28],[22,29],[22,30],[22,31],[22,32],[22,33],[22,34],[22,35],[22,36],[22,37],[22,38],[22,39],[22,40],[22,41],[22,42],[22,43],[22,44],[22,45],[22,46],[22,47],[22,48],[22,49],[22,50],[22,51],[22,52],[22,53],[22,54],[22,55],[22,56],[22,57],[22,58],[22,59],[22,60],[22,61],[22,62],[22,63],[22,64],[22,65],[22,66],[22,67],[22,68],[22,69],[22,70],[22,71],[22,72],[22,73],[22,74],[22,75],[22,76],[22,77],[22,78],[22,79],[22,80],[22,81],[22,82],[22,83],[22,84],[22,85],[22,86],[22,87],[22,88],[22,89],[22,90],[22,91],[22,92],[22,93],[22,94],[22,95],[22,96],[22,97],[22,98],[22,99],[23,24],[23,25],[23,26],[23,27],[23,28],[23,29],[23,30],[23,31],[23,32],[23,33],[23,34],[23,35],[23,36],[23,37],[23,38],[23,39],[23,40],[23,41],[23,42],[23,43],[23,44],[23,45],[23,46],[23,47],[23,48],[23,49],[23,50],[23,51],[23,52],[23,53],[23,54],[23,55],[23,56],[23,57],[23,58],[23,59],[23,60],[23,61],[23,62],[23,63],[23,64],[23,65],[23,66],[23,67],[23,68],[23,69],[23,70],[23,71],[23,72],[23,73],[23,74],[23,75],[23,76],[23,77],[23,78],[23,79],[23,80],[23,81],[23,82],[23,83],[23,84],[23,85],[23,86],[23,87],[23,88],[23,89],[23,90],[23,91],[23,92],[23,93],[23,94],[23,95],[23,96],[23,97],[23,98],[23,99],[24,25],[24,26],[24,27],[24,28],[24,29],[24,30],[24,31],[24,32],[24,33],[24,34],[24,35],[24,36],[24,37],[24,38],[24,39],[24,40],[24,41],[24,42],[24,43],[24,44],[24,45],[24,46],[24,47],[24,48],[24,49],[24,50],[24,51],[24,52],[24,53],[24,54],[24,55],[24,56],[24,57],[24,58],[24,59],[24,60],[24,61],[24,62],[24,63],[24,64],[24,65],[24,66],[24,67],[24,68],[24,69],[24,70],[24,71],[24,72],[24,73],[24,74],[24,75],[24,76],[24,77],[24,78],[24,79],[24,80],[24,81],[24,82],[24,83],[24,84],[24,85],[24,86],[24,87],[24,88],[24,89],[24,90],[24,91],[24,92],[24,93],[24,94],[24,95],[24,96],[24,97],[24,98],[24,99],[25,26],[25,27],[25,28],[25,29],[25,30],[25,31],[25,32],[25,33],[25,34],[25,35],[25,36],[25,37],[25,38],[25,39],[25,40],[25,41],[25,42],[25,43],[25,44],[25,45],[25,46],[25,47],[25,48],[25,49],[25,50],[25,51],[25,52],[25,53],[25,54],[25,55],[25,56],[25,57],[25,58],[25,59],[25,60],[25,61],[25,62],[25,63],[25,64],[25,65],[25,66],[25,67],[25,68],[25,69],[25,70],[25,71],[25,72],[25,73],[25,74],[25,75],[25,76],[25,77],[25,78],[25,79],[25,80],[25,81],[25,82],[25,83],[25,84],[25,85],[25,86],[25,87],[25,88],[25,89],[25,90],[25,91],[25,92],[25,93],[25,94],[25,95],[25,96],[25,97],[25,98],[25,99],[26,27],[26,28],[26,29],[26,30],[26,31],[26,32],[26,33],[26,34],[26,35],[26,36],[26,37],[26,38],[26,39],[26,40],[26,41],[26,42],[26,43],[26,44],[26,45],[26,46],[26,47],[26,48],[26,49],[26,50],[26,51],[26,52],[26,53],[26,54],[26,55],[26,56],[26,57],[26,58],[26,59],[26,60],[26,61],[26,62],[26,63],[26,64],[26,65],[26,66],[26,67],[26,68],[26,69],[26,70],[26,71],[26,72],[26,73],[26,74],[26,75],[26,76],[26,77],[26,78],[26,79],[26,80],[26,81],[26,82],[26,83],[26,84],[26,85],[26,86],[26,87],[26,88],[26,89],[26,90],[26,91],[26,92],[26,93],[26,94],[26,95],[26,96],[26,97],[26,98],[26,99],[27,28],[27,29],[27,30],[27,31],[27,32],[27,33],[27,34],[27,35],[27,36],[27,37],[27,38],[27,39],[27,40],[27,41],[27,42],[27,43],[27,44],[27,45],[27,46],[27,47],[27,48],[27,49],[27,50],[27,51],[27,52],[27,53],[27,54],[27,55],[27,56],[27,57],[27,58],[27,59],[27,60],[27,61],[27,62],[27,63],[27,64],[27,65],[27,66],[27,67],[27,68],[27,69],[27,70],[27,71],[27,72],[27,73],[27,74],[27,75],[27,76],[27,77],[27,78],[27,79],[27,80],[27,81],[27,82],[27,83],[27,84],[27,85],[27,86],[27,87],[27,88],[27,89],[27,90],[27,91],[27,92],[27,93],[27,94],[27,95],[27,96],[27,97],[27,98],[27,99],[28,29],[28,30],[28,31],[28,32],[28,33],[28,34],[28,35],[28,36],[28,37],[28,38],[28,39],[28,40],[28,41],[28,42],[28,43],[28,44],[28,45],[28,46],[28,47],[28,48],[28,49],[28,50],[28,51],[28,52],[28,53],[28,54],[28,55],[28,56],[28,57],[28,58],[28,59],[28,60],[28,61],[28,62],[28,63],[28,64],[28,65],[28,66],[28,67],[28,68],[28,69],[28,70],[28,71],[28,72],[28,73],[28,74],[28,75],[28,76],[28,77],[28,78],[28,79],[28,80],[28,81],[28,82],[28,83],[28,84],[28,85],[28,86],[28,87],[28,88],[28,89],[28,90],[28,91],[28,92],[28,93],[28,94],[28,95],[28,96],[28,97],[28,98],[28,99],[29,30],[29,31],[29,32],[29,33],[29,34],[29,35],[29,36],[29,37],[29,38],[29,39],[29,40],[29,41],[29,42],[29,43],[29,44],[29,45],[29,46],[29,47],[29,48],[29,49],[29,50],[29,51],[29,52],[29,53],[29,54],[29,55],[29,56],[29,57],[29,58],[29,59],[29,60],[29,61],[29,62],[29,63],[29,64],[29,65],[29,66],[29,67],[29,68],[29,69],[29,70],[29,71],[29,72],[29,73],[29,74],[29,75],[29,76],[29,77],[29,78],[29,79],[29,80],[29,81],[29,82],[29,83],[29,84],[29,85],[29,86],[29,87],[29,88],[29,89],[29,90],[29,91],[29,92],[29,93],[29,94],[29,95],[29,96],[29,97],[29,98],[29,99],[30,31],[30,32],[30,33],[30,34],[30,35],[30,36],[30,37],[30,38],[30,39],[30,40],[30,41],[30,42],[30,43],[30,44],[30,45],[30,46],[30,47],[30,48],[30,49],[30,50],[30,51],[30,52],[30,53],[30,54],[30,55],[30,56],[30,57],[30,58],[30,59],[30,60],[30,61],[30,62],[30,63],[30,64],[30,65],[30,66],[30,67],[30,68],[30,69],[30,70],[30,71],[30,72],[30,73],[30,74],[30,75],[30,76],[30,77],[30,78],[30,79],[30,80],[30,81],[30,82],[30,83],[30,84],[30,85],[30,86],[30,87],[30,88],[30,89],[30,90],[30,91],[30,92],[30,93],[30,94],[30,95],[30,96],[30,97],[30,98],[30,99],[31,32],[31,33],[31,34],[31,35],[31,36],[31,37],[31,38],[31,39],[31,40],[31,41],[31,42],[31,43],[31,44],[31,45],[31,46],[31,47],[31,48],[31,49],[31,50],[31,51],[31,52],[31,53],[31,54],[31,55],[31,56],[31,57],[31,58],[31,59],[31,60],[31,61],[31,62],[31,63],[31,64],[31,65],[31,66],[31,67],[31,68],[31,69],[31,70],[31,71],[31,72],[31,73],[31,74],[31,75],[31,76],[31,77],[31,78],[31,79],[31,80],[31,81],[31,82],[31,83],[31,84],[31,85],[31,86],[31,87],[31,88],[31,89],[31,90],[31,91],[31,92],[31,93],[31,94],[31,95],[31,96],[31,97],[31,98],[31,99],[32,33],[32,34],[32,35],[32,36],[32,37],[32,38],[32,39],[32,40],[32,41],[32,42],[32,43],[32,44],[32,45],[32,46],[32,47],[32,48],[32,49],[32,50],[32,51],[32,52],[32,53],[32,54],[32,55],[32,56],[32,57],[32,58],[32,59],[32,60],[32,61],[32,62],[32,63],[32,64],[32,65],[32,66],[32,67],[32,68],[32,69],[32,70],[32,71],[32,72],[32,73],[32,74],[32,75],[32,76],[32,77],[32,78],[32,79],[32,80],[32,81],[32,82],[32,83],[32,84],[32,85],[32,86],[32,87],[32,88],[32,89],[32,90],[32,91],[32,92],[32,93],[32,94],[32,95],[32,96],[32,97],[32,98],[32,99],[33,34],[33,35],[33,36],[33,37],[33,38],[33,39],[33,40],[33,41],[33,42],[33,43],[33,44],[33,45],[33,46],[33,47],[33,48],[33,49],[33,50],[33,51],[33,52],[33,53],[33,54],[33,55],[33,56],[33,57],[33,58],[33,59],[33,60],[33,61],[33,62],[33,63],[33,64],[33,65],[33,66],[33,67],[33,68],[33,69],[33,70],[33,71],[33,72],[33,73],[33,74],[33,75],[33,76],[33,77],[33,78],[33,79],[33,80],[33,81],[33,82],[33,83],[33,84],[33,85],[33,86],[33,87],[33,88],[33,89],[33,90],[33,91],[33,92],[33,93],[33,94],[33,95],[33,96],[33,97],[33,98],[33,99],[34,35],[34,36],[34,37],[34,38],[34,39],[34,40],[34,41],[34,42],[34,43],[34,44],[34,45],[34,46],[34,47],[34,48],[34,49],[34,50],[34,51],[34,52],[34,53],[34,54],[34,55],[34,56],[34,57],[34,58],[34,59],[34,60],[34,61],[34,62],[34,63],[34,64],[34,65],[34,66],[34,67],[34,68],[34,69],[34,70],[34,71],[34,72],[34,73],[34,74],[34,75],[34,76],[34,77],[34,78],[34,79],[34,80],[34,81],[34,82],[34,83],[34,84],[34,85],[34,86],[34,87],[34,88],[34,89],[34,90],[34,91],[34,92],[34,93],[34,94],[34,95],[34,96],[34,97],[34,98],[34,99],[35,36],[35,37],[35,38],[35,39],[35,40],[35,41],[35,42],[35,43],[35,44],[35,45],[35,46],[35,47],[35,48],[35,49],[35,50],[35,51],[35,52],[35,53],[35,54],[35,55],[35,56],[35,57],[35,58],[35,59],[35,60],[35,61],[35,62],[35,63],[35,64],[35,65],[35,66],[35,67],[35,68],[35,69],[35,70],[35,71],[35,72],[35,73],[35,74],[35,75],[35,76],[35,77],[35,78],[35,79],[35,80],[35,81],[35,82],[35,83],[35,84],[35,85],[35,86],[35,87],[35,88],[35,89],[35,90],[35,91],[35,92],[35,93],[35,94],[35,95],[35,96],[35,97],[35,98],[35,99],[36,37],[36,38],[36,39],[36,40],[36,41],[36,42],[36,43],[36,44],[36,45],[36,46],[36,47],[36,48],[36,49],[36,50],[36,51],[36,52],[36,53],[36,54],[36,55],[36,56],[36,57],[36,58],[36,59],[36,60],[36,61],[36,62],[36,63],[36,64],[36,65],[36,66],[36,67],[36,68],[36,69],[36,70],[36,71],[36,72],[36,73],[36,74],[36,75],[36,76],[36,77],[36,78],[36,79],[36,80],[36,81],[36,82],[36,83],[36,84],[36,85],[36,86],[36,87],[36,88],[36,89],[36,90],[36,91],[36,92],[36,93],[36,94],[36,95],[36,96],[36,97],[36,98],[36,99],[37,38],[37,39],[37,40],[37,41],[37,42],[37,43],[37,44],[37,45],[37,46],[37,47],[37,48],[37,49],[37,50],[37,51],[37,52],[37,53],[37,54],[37,55],[37,56],[37,57],[37,58],[37,59],[37,60],[37,61],[37,62],[37,63],[37,64],[37,65],[37,66],[37,67],[37,68],[37,69],[37,70],[37,71],[37,72],[37,73],[37,74],[37,75],[37,76],[37,77],[37,78],[37,79],[37,80],[37,81],[37,82],[37,83],[37,84],[37,85],[37,86],[37,87],[37,88],[37,89],[37,90],[37,91],[37,92],[37,93],[37,94],[37,95],[37,96],[37,97],[37,98],[37,99],[38,39],[38,40],[38,41],[38,42],[38,43],[38,44],[38,45],[38,46],[38,47],[38,48],[38,49],[38,50],[38,51],[38,52],[38,53],[38,54],[38,55],[38,56],[38,57],[38,58],[38,59],[38,60],[38,61],[38,62],[38,63],[38,64],[38,65],[38,66],[38,67],[38,68],[38,69],[38,70],[38,71],[38,72],[38,73],[38,74],[38,75],[38,76],[38,77],[38,78],[38,79],[38,80],[38,81],[38,82],[38,83],[38,84],[38,85],[38,86],[38,87],[38,88],[38,89],[38,90],[38,91],[38,92],[38,93],[38,94],[38,95],[38,96],[38,97],[38,98],[38,99],[39,40],[39,41],[39,42],[39,43],[39,44],[39,45],[39,46],[39,47],[39,48],[39,49],[39,50],[39,51],[39,52],[39,53],[39,54],[39,55],[39,56],[39,57],[39,58],[39,59],[39,60],[39,61],[39,62],[39,63],[39,64],[39,65],[39,66],[39,67],[39,68],[39,69],[39,70],[39,71],[39,72],[39,73],[39,74],[39,75],[39,76],[39,77],[39,78],[39,79],[39,80],[39,81],[39,82],[39,83],[39,84],[39,85],[39,86],[39,87],[39,88],[39,89],[39,90],[39,91],[39,92],[39,93],[39,94],[39,95],[39,96],[39,97],[39,98],[39,99],[40,41],[40,42],[40,43],[40,44],[40,45],[40,46],[40,47],[40,48],[40,49],[40,50],[40,51],[40,52],[40,53],[40,54],[40,55],[40,56],[40,57],[40,58],[40,59],[40,60],[40,61],[40,62],[40,63],[40,64],[40,65],[40,66],[40,67],[40,68],[40,69],[40,70],[40,71],[40,72],[40,73],[40,74],[40,75],[40,76],[40,77],[40,78],[40,79],[40,80],[40,81],[40,82],[40,83],[40,84],[40,85],[40,86],[40,87],[40,88],[40,89],[40,90],[40,91],[40,92],[40,93],[40,94],[40,95],[40,96],[40,97],[40,98],[40,99],[41,42],[41,43],[41,44],[41,45],[41,46],[41,47],[41,48],[41,49],[41,50],[41,51],[41,52],[41,53],[41,54],[41,55],[41,56],[41,57],[41,58],[41,59],[41,60],[41,61],[41,62],[41,63],[41,64],[41,65],[41,66],[41,67],[41,68],[41,69],[41,70],[41,71],[41,72],[41,73],[41,74],[41,75],[41,76],[41,77],[41,78],[41,79],[41,80],[41,81],[41,82],[41,83],[41,84],[41,85],[41,86],[41,87],[41,88],[41,89],[41,90],[41,91],[41,92],[41,93],[41,94],[41,95],[41,96],[41,97],[41,98],[41,99],[42,43],[42,44],[42,45],[42,46],[42,47],[42,48],[42,49],[42,50],[42,51],[42,52],[42,53],[42,54],[42,55],[42,56],[42,57],[42,58],[42,59],[42,60],[42,61],[42,62],[42,63],[42,64],[42,65],[42,66],[42,67],[42,68],[42,69],[42,70],[42,71],[42,72],[42,73],[42,74],[42,75],[42,76],[42,77],[42,78],[42,79],[42,80],[42,81],[42,82],[42,83],[42,84],[42,85],[42,86],[42,87],[42,88],[42,89],[42,90],[42,91],[42,92],[42,93],[42,94],[42,95],[42,96],[42,97],[42,98],[42,99],[43,44],[43,45],[43,46],[43,47],[43,48],[43,49],[43,50],[43,51],[43,52],[43,53],[43,54],[43,55],[43,56],[43,57],[43,58],[43,59],[43,60],[43,61],[43,62],[43,63],[43,64],[43,65],[43,66],[43,67],[43,68],[43,69],[43,70],[43,71],[43,72],[43,73],[43,74],[43,75],[43,76],[43,77],[43,78],[43,79],[43,80],[43,81],[43,82],[43,83],[43,84],[43,85],[43,86],[43,87],[43,88],[43,89],[43,90],[43,91],[43,92],[43,93],[43,94],[43,95],[43,96],[43,97],[43,98],[43,99],[44,45],[44,46],[44,47],[44,48],[44,49],[44,50],[44,51],[44,52],[44,53],[44,54],[44,55],[44,56],[44,57],[44,58],[44,59],[44,60],[44,61],[44,62],[44,63],[44,64],[44,65],[44,66],[44,67],[44,68],[44,69],[44,70],[44,71],[44,72],[44,73],[44,74],[44,75],[44,76],[44,77],[44,78],[44,79],[44,80],[44,81],[44,82],[44,83],[44,84],[44,85],[44,86],[44,87],[44,88],[44,89],[44,90],[44,91],[44,92],[44,93],[44,94],[44,95],[44,96],[44,97],[44,98],[44,99],[45,46],[45,47],[45,48],[45,49],[45,50],[45,51],[45,52],[45,53],[45,54],[45,55],[45,56],[45,57],[45,58],[45,59],[45,60],[45,61],[45,62],[45,63],[45,64],[45,65],[45,66],[45,67],[45,68],[45,69],[45,70],[45,71],[45,72],[45,73],[45,74],[45,75],[45,76],[45,77],[45,78],[45,79],[45,80],[45,81],[45,82],[45,83],[45,84],[45,85],[45,86],[45,87],[45,88],[45,89],[45,90],[45,91],[45,92],[45,93],[45,94],[45,95],[45,96],[45,97],[45,98],[45,99],[46,47],[46,48],[46,49],[46,50],[46,51],[46,52],[46,53],[46,54],[46,55],[46,56],[46,57],[46,58],[46,59],[46,60],[46,61],[46,62],[46,63],[46,64],[46,65],[46,66],[46,67],[46,68],[46,69],[46,70],[46,71],[46,72],[46,73],[46,74],[46,75],[46,76],[46,77],[46,78],[46,79],[46,80],[46,81],[46,82],[46,83],[46,84],[46,85],[46,86],[46,87],[46,88],[46,89],[46,90],[46,91],[46,92],[46,93],[46,94],[46,95],[46,96],[46,97],[46,98],[46,99],[47,48],[47,49],[47,50],[47,51],[47,52],[47,53],[47,54],[47,55],[47,56],[47,57],[47,58],[47,59],[47,60],[47,61],[47,62],[47,63],[47,64],[47,65],[47,66],[47,67],[47,68],[47,69],[47,70],[47,71],[47,72],[47,73],[47,74],[47,75],[47,76],[47,77],[47,78],[47,79],[47,80],[47,81],[47,82],[47,83],[47,84],[47,85],[47,86],[47,87],[47,88],[47,89],[47,90],[47,91],[47,92],[47,93],[47,94],[47,95],[47,96],[47,97],[47,98],[47,99],[48,49],[48,50],[48,51],[48,52],[48,53],[48,54],[48,55],[48,56],[48,57],[48,58],[48,59],[48,60],[48,61],[48,62],[48,63],[48,64],[48,65],[48,66],[48,67],[48,68],[48,69],[48,70],[48,71],[48,72],[48,73],[48,74],[48,75],[48,76],[48,77],[48,78],[48,79],[48,80],[48,81],[48,82],[48,83],[48,84],[48,85],[48,86],[48,87],[48,88],[48,89],[48,90],[48,91],[48,92],[48,93],[48,94],[48,95],[48,96],[48,97],[48,98],[48,99],[49,50],[49,51],[49,52],[49,53],[49,54],[49,55],[49,56],[49,57],[49,58],[49,59],[49,60],[49,61],[49,62],[49,63],[49,64],[49,65],[49,66],[49,67],[49,68],[49,69],[49,70],[49,71],[49,72],[49,73],[49,74],[49,75],[49,76],[49,77],[49,78],[49,79],[49,80],[49,81],[49,82],[49,83],[49,84],[49,85],[49,86],[49,87],[49,88],[49,89],[49,90],[49,91],[49,92],[49,93],[49,94],[49,95],[49,96],[49,97],[49,98],[49,99],[50,51],[50,52],[50,53],[50,54],[50,55],[50,56],[50,57],[50,58],[50,59],[50,60],[50,61],[50,62],[50,63],[50,64],[50,65],[50,66],[50,67],[50,68],[50,69],[50,70],[50,71],[50,72],[50,73],[50,74],[50,75],[50,76],[50,77],[50,78],[50,79],[50,80],[50,81],[50,82],[50,83],[50,84],[50,85],[50,86],[50,87],[50,88],[50,89],[50,90],[50,91],[50,92],[50,93],[50,94],[50,95],[50,96],[50,97],[50,98],[50,99],[51,52],[51,53],[51,54],[51,55],[51,56],[51,57],[51,58],[51,59],[51,60],[51,61],[51,62],[51,63],[51,64],[51,65],[51,66],[51,67],[51,68],[51,69],[51,70],[51,71],[51,72],[51,73],[51,74],[51,75],[51,76],[51,77],[51,78],[51,79],[51,80],[51,81],[51,82],[51,83],[51,84],[51,85],[51,86],[51,87],[51,88],[51,89],[51,90],[51,91],[51,92],[51,93],[51,94],[51,95],[51,96],[51,97],[51,98],[51,99],[52,53],[52,54],[52,55],[52,56],[52,57],[52,58],[52,59],[52,60],[52,61],[52,62],[52,63],[52,64],[52,65],[52,66],[52,67],[52,68],[52,69],[52,70],[52,71],[52,72],[52,73],[52,74],[52,75],[52,76],[52,77],[52,78],[52,79],[52,80],[52,81],[52,82],[52,83],[52,84],[52,85],[52,86],[52,87],[52,88],[52,89],[52,90],[52,91],[52,92],[52,93],[52,94],[52,95],[52,96],[52,97],[52,98],[52,99],[53,54],[53,55],[53,56],[53,57],[53,58],[53,59],[53,60],[53,61],[53,62],[53,63],[53,64],[53,65],[53,66],[53,67],[53,68],[53,69],[53,70],[53,71],[53,72],[53,73],[53,74],[53,75],[53,76],[53,77],[53,78],[53,79],[53,80],[53,81],[53,82],[53,83],[53,84],[53,85],[53,86],[53,87],[53,88],[53,89],[53,90],[53,91],[53,92],[53,93],[53,94],[53,95],[53,96],[53,97],[53,98],[53,99],[54,55],[54,56],[54,57],[54,58],[54,59],[54,60],[54,61],[54,62],[54,63],[54,64],[54,65],[54,66],[54,67],[54,68],[54,69],[54,70],[54,71],[54,72],[54,73],[54,74],[54,75],[54,76],[54,77],[54,78],[54,79],[54,80],[54,81],[54,82],[54,83],[54,84],[54,85],[54,86],[54,87],[54,88],[54,89],[54,90],[54,91],[54,92],[54,93],[54,94],[54,95],[54,96],[54,97],[54,98],[54,99],[55,56],[55,57],[55,58],[55,59],[55,60],[55,61],[55,62],[55,63],[55,64],[55,65],[55,66],[55,67],[55,68],[55,69],[55,70],[55,71],[55,72],[55,73],[55,74],[55,75],[55,76],[55,77],[55,78],[55,79],[55,80],[55,81],[55,82],[55,83],[55,84],[55,85],[55,86],[55,87],[55,88],[55,89],[55,90],[55,91],[55,92],[55,93],[55,94],[55,95],[55,96],[55,97],[55,98],[55,99],[56,57],[56,58],[56,59],[56,60],[56,61],[56,62],[56,63],[56,64],[56,65],[56,66],[56,67],[56,68],[56,69],[56,70],[56,71],[56,72],[56,73],[56,74],[56,75],[56,76],[56,77],[56,78],[56,79],[56,80],[56,81],[56,82],[56,83],[56,84],[56,85],[56,86],[56,87],[56,88],[56,89],[56,90],[56,91],[56,92],[56,93],[56,94],[56,95],[56,96],[56,97],[56,98],[56,99],[57,58],[57,59],[57,60],[57,61],[57,62],[57,63],[57,64],[57,65],[57,66],[57,67],[57,68],[57,69],[57,70],[57,71],[57,72],[57,73],[57,74],[57,75],[57,76],[57,77],[57,78],[57,79],[57,80],[57,81],[57,82],[57,83],[57,84],[57,85],[57,86],[57,87],[57,88],[57,89],[57,90],[57,91],[57,92],[57,93],[57,94],[57,95],[57,96],[57,97],[57,98],[57,99],[58,59],[58,60],[58,61],[58,62],[58,63],[58,64],[58,65],[58,66],[58,67],[58,68],[58,69],[58,70],[58,71],[58,72],[58,73],[58,74],[58,75],[58,76],[58,77],[58,78],[58,79],[58,80],[58,81],[58,82],[58,83],[58,84],[58,85],[58,86],[58,87],[58,88],[58,89],[58,90],[58,91],[58,92],[58,93],[58,94],[58,95],[58,96],[58,97],[58,98],[58,99],[59,60],[59,61],[59,62],[59,63],[59,64],[59,65],[59,66],[59,67],[59,68],[59,69],[59,70],[59,71],[59,72],[59,73],[59,74],[59,75],[59,76],[59,77],[59,78],[59,79],[59,80],[59,81],[59,82],[59,83],[59,84],[59,85],[59,86],[59,87],[59,88],[59,89],[59,90],[59,91],[59,92],[59,93],[59,94],[59,95],[59,96],[59,97],[59,98],[59,99],[60,61],[60,62],[60,63],[60,64],[60,65],[60,66],[60,67],[60,68],[60,69],[60,70],[60,71],[60,72],[60,73],[60,74],[60,75],[60,76],[60,77],[60,78],[60,79],[60,80],[60,81],[60,82],[60,83],[60,84],[60,85],[60,86],[60,87],[60,88],[60,89],[60,90],[60,91],[60,92],[60,93],[60,94],[60,95],[60,96],[60,97],[60,98],[60,99],[61,62],[61,63],[61,64],[61,65],[61,66],[61,67],[61,68],[61,69],[61,70],[61,71],[61,72],[61,73],[61,74],[61,75],[61,76],[61,77],[61,78],[61,79],[61,80],[61,81],[61,82],[61,83],[61,84],[61,85],[61,86],[61,87],[61,88],[61,89],[61,90],[61,91],[61,92],[61,93],[61,94],[61,95],[61,96],[61,97],[61,98],[61,99],[62,63],[62,64],[62,65],[62,66],[62,67],[62,68],[62,69],[62,70],[62,71],[62,72],[62,73],[62,74],[62,75],[62,76],[62,77],[62,78],[62,79],[62,80],[62,81],[62,82],[62,83],[62,84],[62,85],[62,86],[62,87],[62,88],[62,89],[62,90],[62,91],[62,92],[62,93],[62,94],[62,95],[62,96],[62,97],[62,98],[62,99],[63,64],[63,65],[63,66],[63,67],[63,68],[63,69],[63,70],[63,71],[63,72],[63,73],[63,74],[63,75],[63,76],[63,77],[63,78],[63,79],[63,80],[63,81],[63,82],[63,83],[63,84],[63,85],[63,86],[63,87],[63,88],[63,89],[63,90],[63,91],[63,92],[63,93],[63,94],[63,95],[63,96],[63,97],[63,98],[63,99],[64,65],[64,66],[64,67],[64,68],[64,69],[64,70],[64,71],[64,72],[64,73],[64,74],[64,75],[64,76],[64,77],[64,78],[64,79],[64,80],[64,81],[64,82],[64,83],[64,84],[64,85],[64,86],[64,87],[64,88],[64,89],[64,90],[64,91],[64,92],[64,93],[64,94],[64,95],[64,96],[64,97],[64,98],[64,99],[65,66],[65,67],[65,68],[65,69],[65,70],[65,71],[65,72],[65,73],[65,74],[65,75],[65,76],[65,77],[65,78],[65,79],[65,80],[65,81],[65,82],[65,83],[65,84],[65,85],[65,86],[65,87],[65,88],[65,89],[65,90],[65,91],[65,92],[65,93],[65,94],[65,95],[65,96],[65,97],[65,98],[65,99],[66,67],[66,68],[66,69],[66,70],[66,71],[66,72],[66,73],[66,74],[66,75],[66,76],[66,77],[66,78],[66,79],[66,80],[66,81],[66,82],[66,83],[66,84],[66,85],[66,86],[66,87],[66,88],[66,89],[66,90],[66,91],[66,92],[66,93],[66,94],[66,95],[66,96],[66,97],[66,98],[66,99],[67,68],[67,69],[67,70],[67,71],[67,72],[67,73],[67,74],[67,75],[67,76],[67,77],[67,78],[67,79],[67,80],[67,81],[67,82],[67,83],[67,84],[67,85],[67,86],[67,87],[67,88],[67,89],[67,90],[67,91],[67,92],[67,93],[67,94],[67,95],[67,96],[67,97],[67,98],[67,99],[68,69],[68,70],[68,71],[68,72],[68,73],[68,74],[68,75],[68,76],[68,77],[68,78],[68,79],[68,80],[68,81],[68,82],[68,83],[68,84],[68,85],[68,86],[68,87],[68,88],[68,89],[68,90],[68,91],[68,92],[68,93],[68,94],[68,95],[68,96],[68,97],[68,98],[68,99],[69,70],[69,71],[69,72],[69,73],[69,74],[69,75],[69,76],[69,77],[69,78],[69,79],[69,80],[69,81],[69,82],[69,83],[69,84],[69,85],[69,86],[69,87],[69,88],[69,89],[69,90],[69,91],[69,92],[69,93],[69,94],[69,95],[69,96],[69,97],[69,98],[69,99],[70,71],[70,72],[70,73],[70,74],[70,75],[70,76],[70,77],[70,78],[70,79],[70,80],[70,81],[70,82],[70,83],[70,84],[70,85],[70,86],[70,87],[70,88],[70,89],[70,90],[70,91],[70,92],[70,93],[70,94],[70,95],[70,96],[70,97],[70,98],[70,99],[71,72],[71,73],[71,74],[71,75],[71,76],[71,77],[71,78],[71,79],[71,80],[71,81],[71,82],[71,83],[71,84],[71,85],[71,86],[71,87],[71,88],[71,89],[71,90],[71,91],[71,92],[71,93],[71,94],[71,95],[71,96],[71,97],[71,98],[71,99],[72,73],[72,74],[72,75],[72,76],[72,77],[72,78],[72,79],[72,80],[72,81],[72,82],[72,83],[72,84],[72,85],[72,86],[72,87],[72,88],[72,89],[72,90],[72,91],[72,92],[72,93],[72,94],[72,95],[72,96],[72,97],[72,98],[72,99],[73,74],[73,75],[73,76],[73,77],[73,78],[73,79],[73,80],[73,81],[73,82],[73,83],[73,84],[73,85],[73,86],[73,87],[73,88],[73,89],[73,90],[73,91],[73,92],[73,93],[73,94],[73,95],[73,96],[73,97],[73,98],[73,99],[74,75],[74,76],[74,77],[74,78],[74,79],[74,80],[74,81],[74,82],[74,83],[74,84],[74,85],[74,86],[74,87],[74,88],[74,89],[74,90],[74,91],[74,92],[74,93],[74,94],[74,95],[74,96],[74,97],[74,98],[74,99],[75,76],[75,77],[75,78],[75,79],[75,80],[75,81],[75,82],[75,83],[75,84],[75,85],[75,86],[75,87],[75,88],[75,89],[75,90],[75,91],[75,92],[75,93],[75,94],[75,95],[75,96],[75,97],[75,98],[75,99],[76,77],[76,78],[76,79],[76,80],[76,81],[76,82],[76,83],[76,84],[76,85],[76,86],[76,87],[76,88],[76,89],[76,90],[76,91],[76,92],[76,93],[76,94],[76,95],[76,96],[76,97],[76,98],[76,99],[77,78],[77,79],[77,80],[77,81],[77,82],[77,83],[77,84],[77,85],[77,86],[77,87],[77,88],[77,89],[77,90],[77,91],[77,92],[77,93],[77,94],[77,95],[77,96],[77,97],[77,98],[77,99],[78,79],[78,80],[78,81],[78,82],[78,83],[78,84],[78,85],[78,86],[78,87],[78,88],[78,89],[78,90],[78,91],[78,92],[78,93],[78,94],[78,95],[78,96],[78,97],[78,98],[78,99],[79,80],[79,81],[79,82],[79,83],[79,84],[79,85],[79,86],[79,87],[79,88],[79,89],[79,90],[79,91],[79,92],[79,93],[79,94],[79,95],[79,96],[79,97],[79,98],[79,99],[80,81],[80,82],[80,83],[80,84],[80,85],[80,86],[80,87],[80,88],[80,89],[80,90],[80,91],[80,92],[80,93],[80,94],[80,95],[80,96],[80,97],[80,98],[80,99],[81,82],[81,83],[81,84],[81,85],[81,86],[81,87],[81,88],[81,89],[81,90],[81,91],[81,92],[81,93],[81,94],[81,95],[81,96],[81,97],[81,98],[81,99],[82,83],[82,84],[82,85],[82,86],[82,87],[82,88],[82,89],[82,90],[82,91],[82,92],[82,93],[82,94],[82,95],[82,96],[82,97],[82,98],[82,99],[83,84],[83,85],[83,86],[83,87],[83,88],[83,89],[83,90],[83,91],[83,92],[83,93],[83,94],[83,95],[83,96],[83,97],[83,98],[83,99],[84,85],[84,86],[84,87],[84,88],[84,89],[84,90],[84,91],[84,92],[84,93],[84,94],[84,95],[84,96],[84,97],[84,98],[84,99],[85,86],[85,87],[85,88],[85,89],[85,90],[85,91],[85,92],[85,93],[85,94],[85,95],[85,96],[85,97],[85,98],[85,99],[86,87],[86,88],[86,89],[86,90],[86,91],[86,92],[86,93],[86,94],[86,95],[86,96],[86,97],[86,98],[86,99],[87,88],[87,89],[87,90],[87,91],[87,92],[87,93],[87,94],[87,95],[87,96],[87,97],[87,98],[87,99],[88,89],[88,90],[88,91],[88,92],[88,93],[88,94],[88,95],[88,96],[88,97],[88,98],[88,99],[89,90],[89,91],[89,92],[89,93],[89,94],[89,95],[89,96],[89,97],[89,98],[89,99],[90,91],[90,92],[90,93],[90,94],[90,95],[90,96],[90,97],[90,98],[90,99],[91,92],[91,93],[91,94],[91,95],[91,96],[91,97],[91,98],[91,99],[92,93],[92,94],[92,95],[92,96],[92,97],[92,98],[92,99],[93,94],[93,95],[93,96],[93,97],[93,98],[93,99],[94,95],[94,96],[94,97],[94,98],[94,99],[95,96],[95,97],[95,98],[95,99],[96,97],[96,98],[96,99],[97,98],[97,99],[98,99]"), + 0, + 99)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1060Test.java b/src/test/java/com/fishercoder/secondthousand/_1060Test.java new file mode 100644 index 0000000000..a2f11f8b26 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1060Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1060; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1060Test { + private _1060.Solution1 solution1; + private _1060.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1060.Solution1(); + solution2 = new _1060.Solution2(); + } + + @Test + public void test1() { + assertEquals(5, solution1.missingElement(new int[] {4, 7, 9, 10}, 1)); + assertEquals(5, solution2.missingElement(new int[] {4, 7, 9, 10}, 1)); + } + + @Test + public void test2() { + assertEquals(8, solution1.missingElement(new int[] {4, 7, 9, 10}, 3)); + assertEquals(8, solution2.missingElement(new int[] {4, 7, 9, 10}, 3)); + } + + @Test + public void test3() { + assertEquals(6, solution1.missingElement(new int[] {1, 2, 4}, 3)); + assertEquals(6, solution2.missingElement(new int[] {1, 2, 4}, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1061Test.java b/src/test/java/com/fishercoder/secondthousand/_1061Test.java new file mode 100644 index 0000000000..6a5f598191 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1061Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1061; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1061Test { + private _1061.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1061.Solution1(); + } + + @Test + public void test1() { + assertEquals("makkek", solution1.smallestEquivalentString("parker", "morris", "parser")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1062Test.java b/src/test/java/com/fishercoder/secondthousand/_1062Test.java new file mode 100644 index 0000000000..a4df3491c2 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1062Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1062; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1062Test { + private _1062.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1062.Solution1(); + } + + @Test + public void test1() { + assertEquals(0, (solution1.longestRepeatingSubstring("abcd"))); + } + + @Test + public void test2() { + assertEquals(2, (solution1.longestRepeatingSubstring("abbaba"))); + } + + @Test + public void test3() { + assertEquals(3, (solution1.longestRepeatingSubstring("aabcaabdaab"))); + } + + @Test + public void test4() { + assertEquals(4, (solution1.longestRepeatingSubstring("aaaaa"))); + } + + @Test + public void test5() { + assertEquals( + 10, + (solution1.longestRepeatingSubstring( + "aaabaabbbaaabaabbaabbbabbbaaaabbaaaaaabbbaabbbbbbbbbaaaabbabbaba"))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1065Test.java b/src/test/java/com/fishercoder/secondthousand/_1065Test.java new file mode 100644 index 0000000000..4e70dd8fbd --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1065Test.java @@ -0,0 +1,60 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1065; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1065Test { + private _1065.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1065.Solution1(); + } + + @Test + public void test1() { + CommonUtils.print2DIntArray( + solution1.indexPairs( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + new String[] { + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + })); + } + + @Test + public void test2() { + CommonUtils.print2DIntArray( + solution1.indexPairs( + "thestoryofleetcodeandme", new String[] {"story", "fleet", "leetcode"})); + } + + @Test + public void test3() { + CommonUtils.print2DIntArray(solution1.indexPairs("ababa", new String[] {"aba", "ab"})); + } + + @Test + public void test4() { + CommonUtils.print2DIntArray( + solution1.indexPairs( + "aabaabbaabbaababaaaaaababaabaabaabaababbaabbbbaabbaaababbbbaabbabbabbababbabaabaaaabaabbbb", + new String[] { + "aabaaabbaba", + "bbabbbaaabaaaab", + "ababaabaababb", + "bbbaaabababbba", + "baaaabbaa" + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1066Test.java b/src/test/java/com/fishercoder/secondthousand/_1066Test.java new file mode 100644 index 0000000000..25e6514be1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1066Test.java @@ -0,0 +1,94 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1066; +import org.junit.jupiter.api.Test; + +public class _1066Test { + private _1066.Solution1 solution1; + private static int[][] workers; + private static int[][] bikes; + + @Test + public void test1() { + solution1 = new _1066.Solution1(); + workers = + new int[][] { + {0, 0}, + {2, 1}, + }; + bikes = + new int[][] { + {1, 2}, + {3, 3}, + }; + assertEquals(6, solution1.assignBikes(workers, bikes)); + } + + @Test + public void test2() { + solution1 = new _1066.Solution1(); + workers = + new int[][] { + {0, 0}, + {1, 1}, + {2, 0}, + }; + bikes = + new int[][] { + {1, 0}, + {2, 2}, + {2, 1}, + }; + assertEquals(4, solution1.assignBikes(workers, bikes)); + } + + @Test + public void test3() { + solution1 = new _1066.Solution1(); + workers = + new int[][] { + {0, 0}, + {1, 0}, + {2, 0}, + {3, 0}, + {4, 0}, + {5, 0}, + }; + bikes = + new int[][] { + {0, 999}, + {1, 999}, + {2, 999}, + {3, 999}, + {4, 999}, + {5, 999}, + {6, 999}, + {7, 999}, + }; + assertEquals(5994, solution1.assignBikes(workers, bikes)); + } + + @Test + public void test4() { + solution1 = new _1066.Solution1(); + workers = + new int[][] { + {815, 60}, + {638, 626}, + {6, 44}, + {103, 90}, + {591, 880}, + }; + bikes = + new int[][] { + {709, 161}, + {341, 339}, + {755, 955}, + {172, 27}, + {433, 489}, + }; + assertEquals(1458, solution1.assignBikes(workers, bikes)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1071Test.java b/src/test/java/com/fishercoder/secondthousand/_1071Test.java new file mode 100644 index 0000000000..b6b1591a5b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1071Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1071; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1071Test { + private _1071.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1071.Solution1(); + } + + @Test + public void test1() { + assertEquals("ABC", solution1.gcdOfStrings("ABCABC", "ABC")); + } + + @Test + public void test2() { + assertEquals("AB", solution1.gcdOfStrings("ABABAB", "ABAB")); + } + + @Test + public void test3() { + assertEquals("", solution1.gcdOfStrings("LEET", "CODE")); + } + + @Test + public void test4() { + assertEquals("", solution1.gcdOfStrings("ABCABCD", "ABC")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1078Test.java b/src/test/java/com/fishercoder/secondthousand/_1078Test.java new file mode 100644 index 0000000000..94f519d131 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1078Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1078; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1078Test { + private _1078.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1078.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new String[] {"girl", "student"}, + solution1.findOcurrences( + "alice is a good girl she is a good student", "a", "good")); + } + + @Test + public void test2() { + assertArrayEquals( + new String[] {"we", "rock"}, + solution1.findOcurrences("we will we will rock you", "we", "will")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1079Test.java b/src/test/java/com/fishercoder/secondthousand/_1079Test.java new file mode 100644 index 0000000000..6c956ad471 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1079Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1079; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1079Test { + private _1079.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1079.Solution1(); + } + + @Test + public void test1() { + assertEquals(8, solution1.numTilePossibilities("AAB")); + } + + @Test + public void test2() { + assertEquals(188, solution1.numTilePossibilities("AAABBC")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1080Test.java b/src/test/java/com/fishercoder/secondthousand/_1080Test.java new file mode 100644 index 0000000000..36dd6d7106 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1080Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1080; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1080Test { + private _1080.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1080.Solution1(); + } + + @Test + public void test1() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, -3, -5, null, 4, null)); + TreeUtils.printBinaryTree(root); + TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(1, null, -3, 4)); + TreeUtils.printBinaryTree(expected); + assertEquals(expected, solution1.sufficientSubset(root, -1)); + } + + @Test + public void test2() { + TreeNode root = + TreeUtils.constructBinaryTree(Arrays.asList(1, 2, -3, -5, 3, null, 4, null)); + TreeUtils.printBinaryTree(root); + TreeNode expected = + TreeUtils.constructBinaryTree(Arrays.asList(1, 2, -3, null, 3, null, 4)); + TreeUtils.printBinaryTree(expected); + assertEquals(expected, solution1.sufficientSubset(root, -1)); + } + + @Test + public void test3() { + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList(5, 4, 8, 11, null, 17, 4, 7, 1, null, null, 5, 3)); + TreeUtils.printBinaryTree(root); + TreeNode expected = + TreeUtils.constructBinaryTree( + Arrays.asList(5, 4, 8, 11, null, 17, 4, 7, null, null, null, 5)); + TreeUtils.printBinaryTree(expected); + assertEquals(expected, solution1.sufficientSubset(root, 22)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1085Test.java b/src/test/java/com/fishercoder/secondthousand/_1085Test.java new file mode 100644 index 0000000000..9b35fb17b2 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1085Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1085; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1085Test { + private _1085.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1085.Solution1(); + } + + @Test + public void test1() { + assertEquals(0, solution1.sumOfDigits(new int[] {34, 23, 1, 24, 75, 33, 54, 8})); + } + + @Test + public void test2() { + assertEquals(1, solution1.sumOfDigits(new int[] {99, 77, 33, 66, 55})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1086Test.java b/src/test/java/com/fishercoder/secondthousand/_1086Test.java new file mode 100644 index 0000000000..68b509f557 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1086Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1086; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1086Test { + private _1086.Solution1 solution1; + private _1086.Solution2 solution2; + private static int[][] items; + + @BeforeEach + public void setup() { + solution1 = new _1086.Solution1(); + solution2 = new _1086.Solution2(); + } + + @Test + public void test1() { + items = + new int[][] { + {1, 91}, + {1, 92}, + {2, 93}, + {2, 97}, + {1, 60}, + {2, 77}, + {1, 65}, + {1, 87}, + {1, 100}, + {2, 100}, + {2, 76} + }; + assertArrayEquals( + new int[][] { + {1, 87}, + {2, 88} + }, + solution1.highFive(items)); + } + + @Test + public void test2() { + items = + new int[][] { + {1, 91}, + {1, 92}, + {2, 93}, + {2, 97}, + {1, 60}, + {2, 77}, + {1, 65}, + {1, 87}, + {1, 100}, + {2, 100}, + {2, 76} + }; + assertArrayEquals( + new int[][] { + {1, 87}, + {2, 88} + }, + solution2.highFive(items)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1087Test.java b/src/test/java/com/fishercoder/secondthousand/_1087Test.java new file mode 100644 index 0000000000..8861216e94 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1087Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1087; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1087Test { + private _1087.Solution1 solution1; + private _1087.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1087.Solution1(); + solution2 = new _1087.Solution2(); + } + + @Test + public void test1() { + assertArrayEquals( + new String[] {"ade", "adf", "bde", "bdf", "cde", "cdf"}, + solution1.expand("{a,b,c}d{e,f}")); + assertArrayEquals( + new String[] {"ade", "adf", "bde", "bdf", "cde", "cdf"}, + solution2.expand("{a,b,c}d{e,f}")); + } + + @Test + public void test2() { + assertArrayEquals(new String[] {"abcd"}, solution1.expand("abcd")); + assertArrayEquals(new String[] {"abcd"}, solution2.expand("abcd")); + } + + @Test + public void test3() { + assertArrayEquals( + new String[] {"acdf", "acef", "bcdf", "bcef"}, solution1.expand("{a,b}c{d,e}f")); + assertArrayEquals( + new String[] {"acdf", "acef", "bcdf", "bcef"}, solution2.expand("{a,b}c{d,e}f")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1089Test.java b/src/test/java/com/fishercoder/secondthousand/_1089Test.java new file mode 100644 index 0000000000..9f06603538 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1089Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1089; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1089Test { + private _1089.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1089.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {1, 0, 2, 3, 0, 4, 5, 0}; + solution1.duplicateZeros(arr); + assertArrayEquals(new int[] {1, 0, 0, 2, 3, 0, 0, 4}, arr); + } + + @Test + public void test2() { + arr = new int[] {1, 2, 3}; + solution1.duplicateZeros(arr); + assertArrayEquals(new int[] {1, 2, 3}, arr); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1090Test.java b/src/test/java/com/fishercoder/secondthousand/_1090Test.java new file mode 100644 index 0000000000..e1cf75e677 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1090Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1090; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1090Test { + private _1090.Solution1 solution1; + + @BeforeEach + public void setupForEachTest() { + solution1 = new _1090.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 9, + solution1.largestValsFromLabels( + new int[] {5, 4, 3, 2, 1}, new int[] {1, 1, 2, 2, 3}, 3, 1)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1091Test.java b/src/test/java/com/fishercoder/secondthousand/_1091Test.java new file mode 100644 index 0000000000..32fd31cb94 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1091Test.java @@ -0,0 +1,76 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1091; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1091Test { + private _1091.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1091.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 2, + solution1.shortestPathBinaryMatrix( + new int[][] { + {0, 1}, + {1, 0} + })); + } + + @Test + public void test2() { + assertEquals( + 4, + solution1.shortestPathBinaryMatrix( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,0,0],[1,1,0],[1,1,0]"))); + } + + @Test + public void test3() { + assertEquals( + -1, + solution1.shortestPathBinaryMatrix( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,0,0],[1,1,0],[1,1,0]"))); + } + + @Test + public void test4() { + assertEquals( + -1, + solution1.shortestPathBinaryMatrix( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,0,0],[1,1,0],[1,1,1]"))); + } + + @Test + public void test5() { + assertEquals(1, solution1.shortestPathBinaryMatrix(new int[][] {{0}})); + } + + @Test + public void test6() { + assertEquals( + 7, + solution1.shortestPathBinaryMatrix( + new int[][] { + {0, 1, 0, 0, 1, 1, 0}, + {1, 0, 0, 0, 0, 0, 0}, + {1, 0, 0, 1, 1, 1, 1}, + {0, 1, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0, 1}, + {1, 0, 0, 1, 0, 0, 0}, + {1, 0, 1, 0, 0, 1, 0} + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1094Test.java b/src/test/java/com/fishercoder/secondthousand/_1094Test.java new file mode 100644 index 0000000000..57ccb95c1c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1094Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1094; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1094Test { + private _1094.Solution1 solution1; + private static int[][] trips; + private static int capacity; + + @BeforeEach + public void setup() { + solution1 = new _1094.Solution1(); + } + + @Test + public void test1() { + trips = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[2,1,5],[3,3,7]"); + capacity = 4; + assertEquals(false, solution1.carPooling(trips, capacity)); + } + + @Test + public void test2() { + trips = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[2,1,5],[3,3,7]"); + capacity = 5; + assertEquals(true, solution1.carPooling(trips, capacity)); + } + + @Test + public void test3() { + trips = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[7,5,6],[6,7,8],[10,1,6]"); + capacity = 16; + assertEquals(false, solution1.carPooling(trips, capacity)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1099Test.java b/src/test/java/com/fishercoder/secondthousand/_1099Test.java new file mode 100644 index 0000000000..a35a4155da --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1099Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1099; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1099Test { + private _1099.Solution1 solution1; + private _1099.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1099.Solution1(); + solution2 = new _1099.Solution2(); + } + + @Test + public void test1() { + assertEquals(58, solution1.twoSumLessThanK(new int[] {34, 23, 1, 24, 75, 33, 54, 8}, 60)); + } + + @Test + public void test2() { + assertEquals(-1, solution1.twoSumLessThanK(new int[] {10, 20, 30}, 15)); + } + + @Test + public void test3() { + assertEquals(58, solution2.twoSumLessThanK(new int[] {34, 23, 1, 24, 75, 33, 54, 8}, 60)); + } + + @Test + public void test4() { + assertEquals(-1, solution2.twoSumLessThanK(new int[] {10, 20, 30}, 15)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1100Test.java b/src/test/java/com/fishercoder/secondthousand/_1100Test.java new file mode 100644 index 0000000000..812fb57979 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1100Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1100; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1100Test { + private _1100.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1100.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.numKLenSubstrNoRepeats("havefunonleetcode", 5)); + } + + @Test + public void test2() { + assertEquals(0, solution1.numKLenSubstrNoRepeats("home", 5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1103Test.java b/src/test/java/com/fishercoder/secondthousand/_1103Test.java new file mode 100644 index 0000000000..3b44136dc0 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1103Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1103; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1103Test { + private _1103.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1103.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals(new int[] {1, 2, 3, 1}, solution1.distributeCandies(7, 4)); + } + + @Test + public void test2() { + assertArrayEquals(new int[] {5, 2, 3}, solution1.distributeCandies(10, 3)); + } + + @Test + public void test3() { + assertArrayEquals( + new int[] { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, + 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 5, 0, 0, 0, 0, 0 + }, + solution1.distributeCandies(600, 40)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1104Test.java b/src/test/java/com/fishercoder/secondthousand/_1104Test.java new file mode 100644 index 0000000000..fc51c2b998 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1104Test.java @@ -0,0 +1,114 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1104; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +public class _1104Test { + private _1104.Solution1 solution1; + private _1104.Solution2 solution2; + private static List expected; + + @BeforeEach + public void setup() { + solution1 = new _1104.Solution1(); + solution2 = new _1104.Solution2(); + } + + @Test + public void test1() { + expected = Arrays.asList(1, 3, 4, 14); + assertEquals(expected, solution1.pathInZigZagTree(14)); + } + + @Test + public void test2() { + expected = Arrays.asList(1, 2, 6, 10, 26); + assertEquals(expected, solution1.pathInZigZagTree(26)); + } + + @Test + public void test3() { + expected = Arrays.asList(1, 2, 7, 9, 28, 38); + assertEquals(expected, solution1.pathInZigZagTree(38)); + } + + @Test + public void test4() { + expected = Arrays.asList(1, 3, 5, 13, 20, 54, 83); + assertEquals(expected, solution1.pathInZigZagTree(83)); + } + + @Test + public void test5() { + expected = Arrays.asList(1, 2, 7, 9, 28, 39, 113, 156, 455, 625, 1821, 2500, 7287, 10000); + assertEquals(expected, solution1.pathInZigZagTree(10000)); + } + + @Test + @Disabled + public void test6() { + // takes too long to finish, ignore to let build pass + expected = + Arrays.asList( + 1, 2, 6, 11, 24, 47, 97, 188, 390, 754, 1562, 3018, 6250, 12075, 25000, + 48303, 100000); + assertEquals(expected, solution1.pathInZigZagTree(100000)); + } + + @Test + @Disabled + public void test7() { + // takes too long to finish, ignore to let build pass + expected = + Arrays.asList( + 1, 3, 5, 12, 23, 48, 94, 195, 377, 781, 1509, 3125, 6037, 12500, 24151, + 50000, 96607, 200000); + assertEquals(expected, solution1.pathInZigZagTree(200000)); + } + + @Test + @Disabled + public void test8() { + // takes too long to finish, ignore to let build pass + expected = + Arrays.asList( + 1, 2, 6, 11, 24, 47, 97, 188, 390, 754, 1562, 3018, 6250, 12075, 25000, + 48303, 100000, 193215, 400000); + assertEquals(expected, solution1.pathInZigZagTree(400000)); + } + + @Test + @Disabled + public void test9() { + // takes too long to finish, ignore to let build pass + expected = + Arrays.asList( + 1, 2, 7, 8, 30, 34, 122, 139, 488, 559, 1953, 2237, 7812, 8950, 31250, + 35803, 125000, 143215, 500000); + assertEquals(expected, solution1.pathInZigZagTree(500000)); + } + + @Test + public void test10() { + expected = Arrays.asList(1); + assertEquals(expected, solution1.pathInZigZagTree(1)); + } + + @Test + public void test11() { + expected = Arrays.asList(1, 3, 4, 14); + assertEquals(expected, solution2.pathInZigZagTree(14)); + } + + @Test + public void test12() { + expected = Arrays.asList(1); + assertEquals(expected, solution2.pathInZigZagTree(1)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1105Test.java b/src/test/java/com/fishercoder/secondthousand/_1105Test.java new file mode 100644 index 0000000000..748f80db80 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1105Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1105; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1105Test { + private _1105.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1105.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 6, + solution1.minHeightShelves( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]"), + 4)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1108Test.java b/src/test/java/com/fishercoder/secondthousand/_1108Test.java new file mode 100644 index 0000000000..8af1f717df --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1108Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1108; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1108Test { + private _1108.Solution1 solution1; + private _1108.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1108.Solution1(); + solution2 = new _1108.Solution2(); + } + + @Test + public void test1() { + assertEquals("1[.]1[.]1[.]1", solution1.defangIPaddr("1.1.1.1")); + assertEquals("1[.]1[.]1[.]1", solution2.defangIPaddr("1.1.1.1")); + } + + @Test + public void test2() { + assertEquals("255[.]100[.]50[.]0", solution1.defangIPaddr("255.100.50.0")); + assertEquals("255[.]100[.]50[.]0", solution2.defangIPaddr("255.100.50.0")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1110Test.java b/src/test/java/com/fishercoder/secondthousand/_1110Test.java new file mode 100644 index 0000000000..607fd0d528 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1110Test.java @@ -0,0 +1,63 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1110; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1110Test { + private _1110.Solution1 solution1; + private _1110.Solution2 solution2; + private _1110.Solution3 solution3; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _1110.Solution1(); + solution2 = new _1110.Solution2(); + solution3 = new _1110.Solution3(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); + TreeUtils.printBinaryTree(root); + List actual = solution1.delNodes(root, new int[] {3, 5}); + for (TreeNode node : actual) { + TreeUtils.printBinaryTree(node); + } + + actual.clear(); + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); + actual = solution2.delNodes(root, new int[] {3, 5}); + for (TreeNode node : actual) { + TreeUtils.printBinaryTree(node); + } + + actual.clear(); + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); + actual = solution3.delNodes(root, new int[] {3, 5}); + for (TreeNode node : actual) { + TreeUtils.printBinaryTree(node); + } + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 4, 3)); + TreeUtils.printBinaryTree(root); + List actual = solution1.delNodes(root, new int[] {2, 3}); + for (TreeNode node : actual) { + TreeUtils.printBinaryTree(node); + } + actual.clear(); + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 4, 3)); + actual = solution2.delNodes(root, new int[] {2, 3}); + for (TreeNode node : actual) { + TreeUtils.printBinaryTree(node); + } + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1118Test.java b/src/test/java/com/fishercoder/secondthousand/_1118Test.java new file mode 100644 index 0000000000..4cf7a15d55 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1118Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1118; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1118Test { + private _1118.Solution1 solution1; + + @BeforeEach + public void setupForEachTest() { + solution1 = new _1118.Solution1(); + } + + @Test + public void test1() { + assertEquals(31, solution1.numberOfDays(1992, 7)); + } + + @Test + public void test2() { + assertEquals(29, solution1.numberOfDays(2000, 2)); + } + + @Test + public void test3() { + assertEquals(28, solution1.numberOfDays(1900, 2)); + } + + @Test + public void test4() { + assertEquals(29, solution1.numberOfDays(1836, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1119Test.java b/src/test/java/com/fishercoder/secondthousand/_1119Test.java new file mode 100644 index 0000000000..afa2581e5c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1119Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1119; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1119Test { + private _1119.Solution1 solution1; + private _1119.Solution2 solution2; + private static String S; + + @BeforeEach + public void setup() { + solution1 = new _1119.Solution1(); + solution2 = new _1119.Solution2(); + } + + @Test + public void test1() { + S = "leetcodeisacommunityforcoders"; + assertEquals("ltcdscmmntyfrcdrs", solution1.removeVowels(S)); + assertEquals("ltcdscmmntyfrcdrs", solution2.removeVowels(S)); + } + + @Test + public void test2() { + S = "aeiou"; + assertEquals("", solution1.removeVowels(S)); + assertEquals("", solution2.removeVowels(S)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1122Test.java b/src/test/java/com/fishercoder/secondthousand/_1122Test.java new file mode 100644 index 0000000000..9bbeaa113d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1122Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1122; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1122Test { + private _1122.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1122.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19}, + solution1.relativeSortArray( + new int[] {2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19}, + new int[] {2, 1, 4, 3, 9, 6})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1128Test.java b/src/test/java/com/fishercoder/secondthousand/_1128Test.java new file mode 100644 index 0000000000..a0ab447a90 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1128Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1128; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1128Test { + private _1128.Solution1 solution1; + private static int[][] dominoes; + + @BeforeEach + public void setup() { + solution1 = new _1128.Solution1(); + } + + @Test + public void test1() { + dominoes = + new int[][] { + {1, 2}, + {2, 1}, + {3, 4}, + {5, 6} + }; + assertEquals(1, solution1.numEquivDominoPairs(dominoes)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1133Test.java b/src/test/java/com/fishercoder/secondthousand/_1133Test.java new file mode 100644 index 0000000000..801eaf3c1f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1133Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1133; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1133Test { + private _1133.Solution1 solution1; + private _1133.Solution2 solution2; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _1133.Solution1(); + solution2 = new _1133.Solution2(); + } + + @Test + public void test1() { + A = new int[] {5, 7, 3, 9, 4, 9, 8, 3, 1}; + assertEquals(8, solution1.largestUniqueNumber(A)); + assertEquals(8, solution2.largestUniqueNumber(A)); + } + + @Test + public void test2() { + A = new int[] {9, 9, 8, 8}; + assertEquals(-1, solution1.largestUniqueNumber(A)); + assertEquals(-1, solution2.largestUniqueNumber(A)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1134Test.java b/src/test/java/com/fishercoder/secondthousand/_1134Test.java new file mode 100644 index 0000000000..56c1b3fa88 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1134Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1134; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1134Test { + private _1134.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1134.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isArmstrong(153)); + } + + @Test + public void test2() { + assertEquals(false, solution1.isArmstrong(123)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1136Test.java b/src/test/java/com/fishercoder/secondthousand/_1136Test.java new file mode 100644 index 0000000000..5e932cc19b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1136Test.java @@ -0,0 +1,126 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1136; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1136Test { + private _1136.Solution1 solution1; + private _1136.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1136.Solution1(); + solution2 = new _1136.Solution2(); + } + + @Test + public void test1() { + assertEquals( + 2, + solution1.minimumSemesters( + 3, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,3],[2,3]"))); + assertEquals( + 2, + solution2.minimumSemesters( + 3, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,3],[2,3]"))); + } + + @Test + public void test2() { + assertEquals( + -1, + solution1.minimumSemesters( + 3, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2],[2,3],[3,1]"))); + assertEquals( + -1, + solution2.minimumSemesters( + 3, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2],[2,3],[3,1]"))); + } + + @Test + public void test3() { + assertEquals( + 25, + solution1.minimumSemesters( + 25, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "" + + "[5,10],[11,14],[21,22],[16,19],[21,25],[6,18],[1,9],[4,7]," + + "[10,23],[5,14],[9,18],[18,21],[11,22],[1,15],[1,2],[5,18],[7,20],[2,23]," + + "[12,13],[9,14],[10,16],[11,21],[5,12],[2,24],[8,17],[15,17],[10,13],[11,16]," + + "[20,22],[7,11],[9,15],[16,22],[18,20],[19,22],[10,18],[3,20],[16,25],[10,15]," + + "[1,23],[13,16],[23,25],[1,8],[4,10],[19,24],[11,20],[3,18],[6,25],[11,13]," + + "[13,15],[22,24],[6,24],[17,20],[2,25],[15,24],[8,21],[14,16],[5,16],[19,23]," + + "[1,5],[4,22],[19,20],[12,15],[16,18],[9,13],[13,22],[14,22],[2,8],[3,13]," + + "[9,23],[14,15],[14,17],[8,20],[9,17],[3,19],[8,25],[2,12],[7,24],[19,25]," + + "[1,13],[6,11],[14,21],[7,15],[3,14],[15,23],[10,17],[4,20],[6,14],[10,21]," + + "[2,13],[3,21],[8,11],[5,21],[6,23],[17,25],[16,21],[12,22],[1,16]," + + "[6,19],[7,25],[3,23],[11,25],[3,10],[6,7],[2,3],[5,25],[1,6],[4,17]," + + "[2,16],[13,17],[17,22],[6,13],[5,6],[4,11],[4,23],[4,8],[12,23],[7,21]," + + "[5,20],[3,24],[2,10],[13,14],[11,24],[1,3],[2,7],[7,23],[6,17],[5,17]," + + "[16,17],[8,15],[8,23],[7,17],[14,18],[16,23],[23,24],[4,12],[17,19],[5,9]," + + "[10,11],[5,23],[2,9],[1,19],[2,19],[12,20],[2,14],[11,12],[1,12],[13,23],[4,9]," + + "[7,13],[15,20],[21,24],[8,18],[9,11],[8,19],[6,22],[16,20],[22,25],[20,21],[6,16]," + + "[3,17],[1,22],[9,22],[20,24],[2,6],[9,16],[2,4],[2,20],[20,25],[9,10],[3,11],[15,18]," + + "[1,20],[3,6],[8,14],[10,22],[12,21],[7,8],[8,16],[9,20],[3,8],[15,21],[17,21],[11,18]," + + "[13,24],[17,24],[6,20],[4,15],[6,15],[3,22],[13,21],[2,22],[13,25],[9,12],[4,19],[1,24]," + + "[12,19],[5,8],[1,7],[3,16],[3,5],[12,24],[3,12],[2,17],[18,22],[4,25],[8,24]," + + "[15,19],[18,23],[1,4],[1,21],[10,24],[20,23],[4,14],[16,24],[10,20],[18,24]," + + "[1,14],[12,14],[10,12],[4,16],[5,19],[4,5],[19,21],[15,25],[1,18],[2,21],[4,24]," + + "[7,14],[4,6],[15,16],[3,7],[21,23],[1,17],[12,16],[13,18],[5,7],[9,19],[2,15],[22,23]," + + "[7,19],[17,23],[8,22],[11,17],[7,16],[8,9],[6,21],[4,21],[4,13],[14,24],[3,4],[7,18]," + + "[11,15],[5,11],[12,17],[6,9],[1,25],[12,18],[6,12],[8,10],[6,8],[11,23],[7,10],[14,25]," + + "[14,23],[12,25],[5,24],[10,19],[3,25],[7,9],[8,12],[5,22],[24,25],[13,19],[3,15],[5,15]," + + "[15,22],[10,14],[3,9],[13,20],[1,10],[9,21],[10,25],[9,24],[14,20],[9,25],[8,13],[7,12]," + + "[5,13],[6,10],[2,5],[2,18],[14,19],[1,11],[7,22],[18,25],[11,19]," + + "[18,19],[4,18],[17,18],[2,11]"))); + + assertEquals( + 25, + solution2.minimumSemesters( + 25, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "" + + "[5,10],[11,14],[21,22],[16,19],[21,25],[6,18],[1,9],[4,7]," + + "[10,23],[5,14],[9,18],[18,21],[11,22],[1,15],[1,2],[5,18],[7,20],[2,23]," + + "[12,13],[9,14],[10,16],[11,21],[5,12],[2,24],[8,17],[15,17],[10,13],[11,16]," + + "[20,22],[7,11],[9,15],[16,22],[18,20],[19,22],[10,18],[3,20],[16,25],[10,15]," + + "[1,23],[13,16],[23,25],[1,8],[4,10],[19,24],[11,20],[3,18],[6,25],[11,13]," + + "[13,15],[22,24],[6,24],[17,20],[2,25],[15,24],[8,21],[14,16],[5,16],[19,23]," + + "[1,5],[4,22],[19,20],[12,15],[16,18],[9,13],[13,22],[14,22],[2,8],[3,13]," + + "[9,23],[14,15],[14,17],[8,20],[9,17],[3,19],[8,25],[2,12],[7,24],[19,25]," + + "[1,13],[6,11],[14,21],[7,15],[3,14],[15,23],[10,17],[4,20],[6,14],[10,21]," + + "[2,13],[3,21],[8,11],[5,21],[6,23],[17,25],[16,21],[12,22],[1,16]," + + "[6,19],[7,25],[3,23],[11,25],[3,10],[6,7],[2,3],[5,25],[1,6],[4,17]," + + "[2,16],[13,17],[17,22],[6,13],[5,6],[4,11],[4,23],[4,8],[12,23],[7,21]," + + "[5,20],[3,24],[2,10],[13,14],[11,24],[1,3],[2,7],[7,23],[6,17],[5,17]," + + "[16,17],[8,15],[8,23],[7,17],[14,18],[16,23],[23,24],[4,12],[17,19],[5,9]," + + "[10,11],[5,23],[2,9],[1,19],[2,19],[12,20],[2,14],[11,12],[1,12],[13,23],[4,9]," + + "[7,13],[15,20],[21,24],[8,18],[9,11],[8,19],[6,22],[16,20],[22,25],[20,21],[6,16]," + + "[3,17],[1,22],[9,22],[20,24],[2,6],[9,16],[2,4],[2,20],[20,25],[9,10],[3,11],[15,18]," + + "[1,20],[3,6],[8,14],[10,22],[12,21],[7,8],[8,16],[9,20],[3,8],[15,21],[17,21],[11,18]," + + "[13,24],[17,24],[6,20],[4,15],[6,15],[3,22],[13,21],[2,22],[13,25],[9,12],[4,19],[1,24]," + + "[12,19],[5,8],[1,7],[3,16],[3,5],[12,24],[3,12],[2,17],[18,22],[4,25],[8,24]," + + "[15,19],[18,23],[1,4],[1,21],[10,24],[20,23],[4,14],[16,24],[10,20],[18,24]," + + "[1,14],[12,14],[10,12],[4,16],[5,19],[4,5],[19,21],[15,25],[1,18],[2,21],[4,24]," + + "[7,14],[4,6],[15,16],[3,7],[21,23],[1,17],[12,16],[13,18],[5,7],[9,19],[2,15],[22,23]," + + "[7,19],[17,23],[8,22],[11,17],[7,16],[8,9],[6,21],[4,21],[4,13],[14,24],[3,4],[7,18]," + + "[11,15],[5,11],[12,17],[6,9],[1,25],[12,18],[6,12],[8,10],[6,8],[11,23],[7,10],[14,25]," + + "[14,23],[12,25],[5,24],[10,19],[3,25],[7,9],[8,12],[5,22],[24,25],[13,19],[3,15],[5,15]," + + "[15,22],[10,14],[3,9],[13,20],[1,10],[9,21],[10,25],[9,24],[14,20],[9,25],[8,13],[7,12]," + + "[5,13],[6,10],[2,5],[2,18],[14,19],[1,11],[7,22],[18,25],[11,19]," + + "[18,19],[4,18],[17,18],[2,11]"))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1137Test.java b/src/test/java/com/fishercoder/secondthousand/_1137Test.java new file mode 100644 index 0000000000..576fec0086 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1137Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1137; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1137Test { + private _1137.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1137.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.tribonacci(3)); + } + + @Test + public void test2() { + assertEquals(4, solution1.tribonacci(4)); + } + + @Test + public void test3() { + assertEquals(1389537, solution1.tribonacci(25)); + } + + @Test + public void test4() { + assertEquals(0, solution1.tribonacci(0)); + } + + @Test + public void test5() { + assertEquals(1, solution1.tribonacci(2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1138Test.java b/src/test/java/com/fishercoder/secondthousand/_1138Test.java new file mode 100644 index 0000000000..1b31fc42f8 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1138Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1138; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1138Test { + private _1138.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1138.Solution1(); + } + + @Test + public void test1() { + assertEquals("DDR!UURRR!!DDD!", solution1.alphabetBoardPath("leet")); + } + + @Test + public void test2() { + assertEquals("DDDDD!UUUUURRR!DDDDLLLD!", solution1.alphabetBoardPath("zdz")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1143Test.java b/src/test/java/com/fishercoder/secondthousand/_1143Test.java new file mode 100644 index 0000000000..aeeab23514 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1143Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1143; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1143Test { + private _1143.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1143.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.longestCommonSubsequence("abcde", "ace")); + } + + @Test + public void test2() { + assertEquals(3, solution1.longestCommonSubsequence("abc", "abc")); + } + + @Test + public void test3() { + assertEquals(0, solution1.longestCommonSubsequence("abc", "def")); + } + + @Test + public void test4() { + assertEquals(2, solution1.longestCommonSubsequence("ezupkr", "ubmrapg")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1145Test.java b/src/test/java/com/fishercoder/secondthousand/_1145Test.java new file mode 100644 index 0000000000..7b8cbfebd5 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1145Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1145; +import java.util.Arrays; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1145Test { + + private _1145.Solution1 solution1; + private static TreeNode root; + private static int n; + private static int x; + + @BeforeEach + public void setup() { + solution1 = new _1145.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); + n = 11; + x = 3; + Assertions.assertEquals(true, solution1.btreeGameWinningMove(root, n, x)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1146Test.java b/src/test/java/com/fishercoder/secondthousand/_1146Test.java new file mode 100644 index 0000000000..9a9e942806 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1146Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1146; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1146Test { + private _1146.Solution1.SnapshotArray snapshotArray; + + @BeforeEach + public void setup() {} + + @Test + public void test1() { + snapshotArray = new _1146.Solution1.SnapshotArray(3); + snapshotArray.set(0, 5); + snapshotArray.snap(); + snapshotArray.set(0, 6); + assertEquals(5, snapshotArray.get(0, 0)); + } + + @Test + public void test2() { + snapshotArray = new _1146.Solution1.SnapshotArray(2); + snapshotArray.snap(); + snapshotArray.set(1, 17); + snapshotArray.set(0, 20); + snapshotArray.snap(); + snapshotArray.snap(); + snapshotArray.snap(); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1150Test.java b/src/test/java/com/fishercoder/secondthousand/_1150Test.java new file mode 100644 index 0000000000..31ab1d28be --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1150Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1150; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1150Test { + private _1150.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1150.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 4, 5, 5, 5, 5, 5, 6, 6}; + assertEquals(true, solution1.isMajorityElement(nums, 5)); + } + + @Test + public void test2() { + nums = new int[] {10, 100, 101, 101}; + assertEquals(false, solution1.isMajorityElement(nums, 101)); + } + + @Test + public void test3() { + nums = new int[] {1, 1, 1, 2, 3, 3, 3}; + assertEquals(false, solution1.isMajorityElement(nums, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1151Test.java b/src/test/java/com/fishercoder/secondthousand/_1151Test.java new file mode 100644 index 0000000000..6ff740accd --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1151Test.java @@ -0,0 +1,57 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1151; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1151Test { + private _1151.Solution1 solution1; + private static int[] data; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _1151.Solution1(); + } + + @Test + public void test1() { + data = new int[] {1, 0, 1, 0, 1}; + expected = 1; + assertEquals(expected, solution1.minSwaps(data)); + } + + @Test + public void test2() { + data = new int[] {0, 0, 0, 1, 0}; + expected = 0; + assertEquals(expected, solution1.minSwaps(data)); + } + + @Test + public void test3() { + data = new int[] {1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1}; + expected = 3; + assertEquals(expected, solution1.minSwaps(data)); + } + + @Test + public void test4() { + data = + new int[] { + 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, + 0, 0, 1, 1, 1, 1, 0, 0, 1 + }; + expected = 8; + assertEquals(expected, solution1.minSwaps(data)); + } + + @Test + public void test5() { + data = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + expected = 0; + assertEquals(expected, solution1.minSwaps(data)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1152Test.java b/src/test/java/com/fishercoder/secondthousand/_1152Test.java new file mode 100644 index 0000000000..a8735a8366 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1152Test.java @@ -0,0 +1,80 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1152; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1152Test { + private _1152.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1152.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("home", "about", "career"), + solution1.mostVisitedPattern( + new String[] { + "joe", "joe", "joe", "james", "james", "james", "james", "mary", "mary", + "mary" + }, + new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + new String[] { + "home", "about", "career", "home", "cart", "maps", "home", "home", + "about", "career" + })); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList("oz", "mryxsjc", "wlarkzzqht"), + solution1.mostVisitedPattern( + new String[] {"zkiikgv", "zkiikgv", "zkiikgv", "zkiikgv"}, + new int[] {436363475, 710406388, 386655081, 797150921}, + new String[] {"wnaaxbfhxp", "mryxsjc", "oz", "wlarkzzqht"})); + } + + @Test + public void test3() { + assertEquals( + Arrays.asList("hibympufi", "hibympufi", "yljmntrclw"), + solution1.mostVisitedPattern( + new String[] { + "h", + "eiy", + "cq", + "h", + "cq", + "txldsscx", + "cq", + "txldsscx", + "h", + "cq", + "cq" + }, + new int[] { + 527896567, 334462937, 517687281, 134127993, 859112386, 159548699, + 51100299, 444082139, 926837079, 317455832, 411747930 + }, + new String[] { + "hibympufi", + "hibympufi", + "hibympufi", + "hibympufi", + "hibympufi", + "hibympufi", + "hibympufi", + "hibympufi", + "yljmntrclw", + "hibympufi", + "yljmntrclw" + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1154Test.java b/src/test/java/com/fishercoder/secondthousand/_1154Test.java new file mode 100644 index 0000000000..71b7285b65 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1154Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1154; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1154Test { + private _1154.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1154.Solution1(); + } + + @Test + public void test1() { + assertEquals(9, solution1.dayOfYear("2019-01-09")); + } + + @Test + public void test2() { + assertEquals(271, solution1.dayOfYear("1969-09-28")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1160Test.java b/src/test/java/com/fishercoder/secondthousand/_1160Test.java new file mode 100644 index 0000000000..639bb1b7bc --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1160Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1160; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1160Test { + private _1160.Solution1 solution1; + private static String[] words; + + @BeforeEach + public void setup() { + solution1 = new _1160.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"cat", "bt", "hat", "tree"}; + assertEquals(6, solution1.countCharacters(words, "atach")); + } + + @Test + public void test2() { + words = new String[] {"hello", "world", "leetcode"}; + assertEquals(10, solution1.countCharacters(words, "welldonehoneyr")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1161Test.java b/src/test/java/com/fishercoder/secondthousand/_1161Test.java new file mode 100644 index 0000000000..00e98fef2f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1161Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1161; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1161Test { + private _1161.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1161.Solution1(); + } + + @Test + public void test1() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 7, 0, 7, -8, null, null)); + assertEquals(2, solution1.maxLevelSum(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1165Test.java b/src/test/java/com/fishercoder/secondthousand/_1165Test.java new file mode 100644 index 0000000000..54c5642d88 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1165Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1165; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1165Test { + private _1165.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1165.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.calculateTime("abcdefghijklmnopqrstuvwxyz", "cba")); + } + + @Test + public void test2() { + assertEquals(73, solution1.calculateTime("pqrstuvwxyzabcdefghijklmno", "leetcode")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1170Test.java b/src/test/java/com/fishercoder/secondthousand/_1170Test.java new file mode 100644 index 0000000000..76195ffed9 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1170Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1170; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1170Test { + private _1170.Solution1 solution1; + private _1170.Solution2 solution2; + private static String[] queries; + private static String[] words; + + @BeforeEach + public void setup() { + solution1 = new _1170.Solution1(); + solution2 = new _1170.Solution2(); + } + + @Test + public void test1() { + queries = new String[] {"cbd"}; + words = new String[] {"zaaaz"}; + assertArrayEquals(new int[] {1}, solution1.numSmallerByFrequency(queries, words)); + } + + @Test + public void test2() { + queries = new String[] {"bbb", "cc"}; + words = new String[] {"a", "aa", "aaa", "aaaa"}; + assertArrayEquals(new int[] {1, 2}, solution1.numSmallerByFrequency(queries, words)); + } + + @Test + public void test3() { + queries = new String[] {"cbd"}; + words = new String[] {"zaaaz"}; + assertArrayEquals(new int[] {1}, solution2.numSmallerByFrequency(queries, words)); + } + + @Test + public void test4() { + queries = new String[] {"bbb", "cc"}; + words = new String[] {"a", "aa", "aaa", "aaaa"}; + assertArrayEquals(new int[] {1, 2}, solution2.numSmallerByFrequency(queries, words)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1171Test.java b/src/test/java/com/fishercoder/secondthousand/_1171Test.java new file mode 100644 index 0000000000..95ba86032d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1171Test.java @@ -0,0 +1,83 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.secondthousand._1171; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1171Test { + private _1171.Solution1 solution1; + private _1171.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1171.Solution1(); + solution2 = new _1171.Solution2(); + } + + @Test + public void test1() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {3, 1}), + solution1.removeZeroSumSublists( + LinkedListUtils.contructLinkedList(new int[] {1, 2, -3, 3, 1}))); + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {3, 1}), + solution2.removeZeroSumSublists( + LinkedListUtils.contructLinkedList(new int[] {1, 2, -3, 3, 1}))); + } + + @Test + public void test2() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {1, 2, 4}), + solution1.removeZeroSumSublists( + LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, -3, 4}))); + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {1, 2, 4}), + solution2.removeZeroSumSublists( + LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, -3, 4}))); + } + + @Test + public void test3() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {1}), + solution1.removeZeroSumSublists( + LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, -3, -2}))); + } + + @Test + public void test4() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {5, -2, -5}), + solution1.removeZeroSumSublists( + LinkedListUtils.contructLinkedList(new int[] {5, -3, -4, 1, 6, -2, -5}))); + } + + @Test + public void test5() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {}), + solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(new int[] {0}))); + } + + @Test + public void test6() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {2}), + solution1.removeZeroSumSublists( + LinkedListUtils.contructLinkedList(new int[] {2, 0}))); + } + + @Test + public void test7() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {1, 5, 1}), + solution1.removeZeroSumSublists( + LinkedListUtils.contructLinkedList( + new int[] {1, 3, 2, -3, -2, 5, 100, -100, 1}))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1175Test.java b/src/test/java/com/fishercoder/secondthousand/_1175Test.java new file mode 100644 index 0000000000..e43dc6438f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1175Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1175; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +@Disabled +public class _1175Test { + private _1175.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1175.Solution1(); + } + + @Test + public void test1() { + assertEquals(12, solution1.numPrimeArrangements(5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1176Test.java b/src/test/java/com/fishercoder/secondthousand/_1176Test.java new file mode 100644 index 0000000000..03e75bea68 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1176Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1176; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1176Test { + private _1176.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1176.Solution1(); + } + + @Test + public void test1() { + assertEquals(0, solution1.dietPlanPerformance(new int[] {1, 2, 3, 4, 5}, 1, 3, 3)); + } + + @Test + public void test2() { + assertEquals(1, solution1.dietPlanPerformance(new int[] {3, 2}, 2, 0, 1)); + } + + @Test + public void test3() { + assertEquals(0, solution1.dietPlanPerformance(new int[] {6, 5, 0, 0}, 2, 1, 5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1180Test.java b/src/test/java/com/fishercoder/secondthousand/_1180Test.java new file mode 100644 index 0000000000..570902e857 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1180Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1180; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1180Test { + private _1180.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1180.Solution1(); + } + + @Test + public void test1() { + assertEquals(8, solution1.countLetters("aaaba")); + } + + @Test + public void test2() { + assertEquals(55, solution1.countLetters("aaaaaaaaaa")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1182Test.java b/src/test/java/com/fishercoder/secondthousand/_1182Test.java new file mode 100644 index 0000000000..4c987f8d10 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1182Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1182; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1182Test { + private _1182.Solution1 solution1; + private static int[] colors; + private static int[][] queries; + + @BeforeEach + public void setup() { + solution1 = new _1182.Solution1(); + } + + @Test + public void test1() { + colors = new int[] {1, 1, 2, 1, 3, 2, 2, 3, 3}; + queries = + new int[][] { + {1, 3}, + {2, 2}, + {6, 1} + }; + assertEquals(Arrays.asList(3, 0, 3), solution1.shortestDistanceColor(colors, queries)); + } + + @Test + public void test2() { + colors = new int[] {1, 2}; + queries = new int[][] {{0, 3}}; + assertEquals(Arrays.asList(-1), solution1.shortestDistanceColor(colors, queries)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1184Test.java b/src/test/java/com/fishercoder/secondthousand/_1184Test.java new file mode 100644 index 0000000000..8a53ce3284 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1184Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1184; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1184Test { + private _1184.Solution1 solution1; + private static int[] distance; + + @BeforeEach + public void setup() { + solution1 = new _1184.Solution1(); + } + + @Test + public void test1() { + distance = new int[] {1, 2, 3, 4}; + assertEquals(1, solution1.distanceBetweenBusStops(distance, 0, 1)); + } + + @Test + public void test2() { + distance = new int[] {1, 2, 3, 4}; + assertEquals(4, solution1.distanceBetweenBusStops(distance, 0, 3)); + } + + @Test + public void test3() { + distance = new int[] {1, 2, 3, 4}; + assertEquals(3, solution1.distanceBetweenBusStops(distance, 0, 2)); + } + + @Test + public void test4() { + distance = new int[] {7, 10, 1, 12, 11, 14, 5, 0}; + assertEquals(17, solution1.distanceBetweenBusStops(distance, 7, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1185Test.java b/src/test/java/com/fishercoder/secondthousand/_1185Test.java new file mode 100644 index 0000000000..a8d1436ece --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1185Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1185; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1185Test { + private _1185.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1185.Solution1(); + } + + @Test + public void test1() { + assertEquals("Saturday", solution1.dayOfTheWeek(3, 8, 2019)); + } + + @Test + public void test2() { + assertEquals("Sunday", solution1.dayOfTheWeek(18, 7, 1999)); + } + + @Test + public void test3() { + assertEquals("Sunday", solution1.dayOfTheWeek(15, 8, 1993)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1189Test.java b/src/test/java/com/fishercoder/secondthousand/_1189Test.java new file mode 100644 index 0000000000..31a62c9441 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1189Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1189; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1189Test { + private _1189.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1189.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.maxNumberOfBalloons("nlaebolko")); + } + + @Test + public void test2() { + assertEquals(2, solution1.maxNumberOfBalloons("loonbalxballpoon")); + } + + @Test + public void test3() { + assertEquals(0, solution1.maxNumberOfBalloons("leetcode")); + } + + @Test + public void test4() { + assertEquals( + 10, + solution1.maxNumberOfBalloons( + "krhizmmgmcrecekgyljqkldocicziihtgpqwbticmvuyznragqoyrukzopfmjhjjxemsxmrsxuqmnkrzhgvtgdgtykhcglurvppvcwhrhrjoislonvvglhdciilduvuiebmffaagxerjeewmtcwmhmtwlxtvlbocczlrppmpjbpnifqtlninyzjtmazxdbzwxthpvrfulvrspycqcghuopjirzoeuqhetnbrcdakilzmklxwudxxhwilasbjjhhfgghogqoofsufysmcqeilaivtmfziumjloewbkjvaahsaaggteppqyuoylgpbdwqubaalfwcqrjeycjbbpifjbpigjdnnswocusuprydgrtxuaojeriigwumlovafxnpibjopjfqzrwemoinmptxddgcszmfprdrichjeqcvikynzigleaajcysusqasqadjemgnyvmzmbcfrttrzonwafrnedglhpudovigwvpimttiketopkvqw")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1190Test.java b/src/test/java/com/fishercoder/secondthousand/_1190Test.java new file mode 100644 index 0000000000..9a3efbb715 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1190Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1190; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1190Test { + private _1190.Solution1 solution1; + private _1190.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1190.Solution1(); + solution2 = new _1190.Solution2(); + } + + @Test + public void test1() { + assertEquals("dcba", solution1.reverseParentheses("(abcd)")); + assertEquals("dcba", solution2.reverseParentheses("(abcd)")); + } + + @Test + public void test2() { + assertEquals("iloveu", solution1.reverseParentheses("(u(love)i)")); + } + + @Test + public void test3() { + assertEquals("leetcode", solution1.reverseParentheses("(ed(et(oc))el)")); + } + + @Test + public void test4() { + assertEquals("apmnolkjihgfedcbq", solution1.reverseParentheses("a(bcdefghijkl(mno)p)q")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1196Test.java b/src/test/java/com/fishercoder/secondthousand/_1196Test.java new file mode 100644 index 0000000000..2d2a6b2fc7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1196Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1196; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1196Test { + private _1196.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1196.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.maxNumberOfApples(new int[] {100, 200, 150, 1000})); + } + + @Test + public void test2() { + assertEquals(5, solution1.maxNumberOfApples(new int[] {900, 950, 800, 1000, 700, 800})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1197Test.java b/src/test/java/com/fishercoder/secondthousand/_1197Test.java new file mode 100644 index 0000000000..020e40680a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1197Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1197; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1197Test { + private _1197.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1197.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minKnightMoves(2, 1)); + } + + @Test + public void test2() { + assertEquals(4, solution1.minKnightMoves(5, 5)); + } + + @Test + public void test3() { + assertEquals(56, solution1.minKnightMoves(2, 112)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1198Test.java b/src/test/java/com/fishercoder/secondthousand/_1198Test.java new file mode 100644 index 0000000000..46cf677d35 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1198Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1198; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1198Test { + private _1198.Solution1 solution1; + private static int[][] mat; + + @BeforeEach + public void setup() { + solution1 = new _1198.Solution1(); + } + + @Test + public void test1() { + mat = + new int[][] { + {1, 2, 3, 4, 5}, + {2, 4, 5, 8, 10}, + {3, 5, 7, 9, 11}, + {1, 3, 5, 7, 9} + }; + assertEquals(5, solution1.smallestCommonElement(mat)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1200Test.java b/src/test/java/com/fishercoder/secondthousand/_1200Test.java new file mode 100644 index 0000000000..53839a0851 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1200Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1200; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1200Test { + private _1200.Solution1 solution1; + private static int[] arr; + private static List> expected; + + @BeforeEach + public void setup() { + solution1 = new _1200.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {4, 2, 1, 3}; + expected = new ArrayList<>(); + expected.add(Arrays.asList(1, 2)); + expected.add(Arrays.asList(2, 3)); + expected.add(Arrays.asList(3, 4)); + assertEquals(expected, solution1.minimumAbsDifference(arr)); + } + + @Test + public void test2() { + arr = new int[] {40, 11, 26, 27, -20}; + expected = new ArrayList<>(); + expected.add(Arrays.asList(26, 27)); + assertEquals(expected, solution1.minimumAbsDifference(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1207Test.java b/src/test/java/com/fishercoder/secondthousand/_1207Test.java new file mode 100644 index 0000000000..e61f564d9e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1207Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1207; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1207Test { + private _1207.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1207.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {1, 2, 2, 1, 1, 3}; + assertEquals(true, solution1.uniqueOccurrences(arr)); + } + + @Test + public void test2() { + arr = new int[] {1, 2}; + assertEquals(false, solution1.uniqueOccurrences(arr)); + } + + @Test + public void test3() { + arr = new int[] {-3, 0, 1, -3, 1, 1, 1, -3, 10, 0}; + assertEquals(true, solution1.uniqueOccurrences(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1209Test.java b/src/test/java/com/fishercoder/secondthousand/_1209Test.java new file mode 100644 index 0000000000..815a4e700a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1209Test.java @@ -0,0 +1,64 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1209; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1209Test { + + private _1209.Solution1 solution1; + private _1209.Solution2 solution2; + private _1209.Solution3 solution3; + private _1209.Solution4 solution4; + + @BeforeEach + public void setup() { + solution1 = new _1209.Solution1(); + solution2 = new _1209.Solution2(); + solution3 = new _1209.Solution3(); + solution4 = new _1209.Solution4(); + } + + @Test + public void test1() { + assertEquals("abcd", solution1.removeDuplicates("abcd", 2)); + assertEquals("abcd", solution2.removeDuplicates("abcd", 2)); + assertEquals("abcd", solution3.removeDuplicates("abcd", 2)); + assertEquals("abcd", solution4.removeDuplicates("abcd", 2)); + } + + @Test + public void test2() { + assertEquals("aa", solution1.removeDuplicates("deeedbbcccbdaa", 3)); + assertEquals("aa", solution2.removeDuplicates("deeedbbcccbdaa", 3)); + assertEquals("aa", solution3.removeDuplicates("deeedbbcccbdaa", 3)); + } + + @Test + public void test3() { + assertEquals("ps", solution1.removeDuplicates("pbbcggttciiippooaais", 2)); + assertEquals("ps", solution2.removeDuplicates("pbbcggttciiippooaais", 2)); + assertEquals("ps", solution3.removeDuplicates("pbbcggttciiippooaais", 2)); + } + + @Test + public void test4() { + assertEquals( + "ghayqgq", + solution1.removeDuplicates( + "ghanyhhhhhttttttthhyyyyyynnnnnnyqkkkkkkkrrrrrrjjjjjjjryyyyyyfffffffygq", + 7)); + assertEquals( + "ghayqgq", + solution2.removeDuplicates( + "ghanyhhhhhttttttthhyyyyyynnnnnnyqkkkkkkkrrrrrrjjjjjjjryyyyyyfffffffygq", + 7)); + assertEquals( + "ghayqgq", + solution3.removeDuplicates( + "ghanyhhhhhttttttthhyyyyyynnnnnnyqkkkkkkkrrrrrrjjjjjjjryyyyyyfffffffygq", + 7)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1213Test.java b/src/test/java/com/fishercoder/secondthousand/_1213Test.java new file mode 100644 index 0000000000..f7532abc1f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1213Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1213; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1213Test { + private _1213.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1213.Solution1(); + } + + @Test + public void test1() { + CommonUtils.printList( + solution1.arraysIntersection( + new int[] {1, 2, 3, 4, 5}, + new int[] {1, 2, 5, 7, 9}, + new int[] {1, 3, 4, 5, 8})); + } +} diff --git a/src/test/java/com/fishercoder/_1214Test.java b/src/test/java/com/fishercoder/secondthousand/_1214Test.java similarity index 76% rename from src/test/java/com/fishercoder/_1214Test.java rename to src/test/java/com/fishercoder/secondthousand/_1214Test.java index b892a93284..fdfe8063a5 100644 --- a/src/test/java/com/fishercoder/_1214Test.java +++ b/src/test/java/com/fishercoder/secondthousand/_1214Test.java @@ -1,22 +1,21 @@ -package com.fishercoder; +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1214; -import org.junit.BeforeClass; -import org.junit.Test; - +import com.fishercoder.solutions.secondthousand._1214; import java.util.Arrays; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _1214Test { - private static _1214.Solution1 solution1; + private _1214.Solution1 solution1; private static TreeNode root1; private static TreeNode root2; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _1214.Solution1(); } @@ -40,5 +39,4 @@ public void test3() { root2 = TreeUtils.constructBinaryTree(Arrays.asList(5, 1, 7, 0, 2)); assertEquals(true, solution1.twoSumBSTs(root1, root2, 17)); } - -} \ No newline at end of file +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1217Test.java b/src/test/java/com/fishercoder/secondthousand/_1217Test.java new file mode 100644 index 0000000000..f3d4eca3eb --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1217Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1217; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1217Test { + private _1217.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1217.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minCostToMoveChips(new int[] {1, 2, 3})); + } + + @Test + public void test2() { + assertEquals(2, solution1.minCostToMoveChips(new int[] {2, 2, 2, 3, 3})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1219Test.java b/src/test/java/com/fishercoder/secondthousand/_1219Test.java new file mode 100644 index 0000000000..76df79b16c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1219Test.java @@ -0,0 +1,53 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1219; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1219Test { + private _1219.Solution1 solution1; + private static int[][] grid; + + @BeforeEach + public void setup() { + solution1 = new _1219.Solution1(); + } + + @Test + public void test1() { + grid = + new int[][] { + {0, 6, 0}, + {5, 8, 7}, + {0, 9, 0}, + }; + assertEquals(24, solution1.getMaximumGold(grid)); + } + + @Test + public void test2() { + grid = + new int[][] { + {1, 0, 7}, + {2, 0, 6}, + {3, 4, 5}, + {0, 3, 0}, + {9, 0, 20}, + }; + assertEquals(28, solution1.getMaximumGold(grid)); + } + + @Test + public void test3() { + grid = + new int[][] { + {0, 0, 19, 5, 8}, + {11, 20, 14, 1, 0}, + {0, 0, 1, 1, 1}, + {0, 2, 0, 2, 0}, + }; + assertEquals(77, solution1.getMaximumGold(grid)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1221Test.java b/src/test/java/com/fishercoder/secondthousand/_1221Test.java new file mode 100644 index 0000000000..cd1f46bebb --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1221Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1221; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1221Test { + private _1221.Solution1 solution1; + private _1221.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1221.Solution1(); + solution2 = new _1221.Solution2(); + } + + @Test + public void test1() { + assertEquals(4, solution1.balancedStringSplit("RLRRLLRLRL")); + assertEquals(4, solution2.balancedStringSplit("RLRRLLRLRL")); + } + + @Test + public void test2() { + assertEquals(3, solution1.balancedStringSplit("RLLLLRRRLR")); + assertEquals(3, solution2.balancedStringSplit("RLLLLRRRLR")); + } + + @Test + public void test3() { + assertEquals(1, solution1.balancedStringSplit("LLLLRRRR")); + assertEquals(1, solution2.balancedStringSplit("LLLLRRRR")); + } + + @Test + public void test4() { + assertEquals(2, solution1.balancedStringSplit("RLRRRLLRLL")); + assertEquals(2, solution2.balancedStringSplit("RLRRRLLRLL")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1228Test.java b/src/test/java/com/fishercoder/secondthousand/_1228Test.java new file mode 100644 index 0000000000..a15c1554f4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1228Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1228; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1228Test { + private _1228.Solution1 solution1; + private _1228.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1228.Solution1(); + solution2 = new _1228.Solution2(); + } + + @Test + public void test1() { + assertEquals(9, solution1.missingNumber(new int[] {5, 7, 11, 13})); + assertEquals(9, solution2.missingNumber(new int[] {5, 7, 11, 13})); + } + + @Test + public void test2() { + assertEquals(14, solution1.missingNumber(new int[] {15, 13, 12})); + assertEquals(14, solution2.missingNumber(new int[] {15, 13, 12})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1230Test.java b/src/test/java/com/fishercoder/secondthousand/_1230Test.java new file mode 100644 index 0000000000..2b99db6f6f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1230Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1230; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1230Test { + private _1230.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1230.Solution1(); + } + + @Test + public void test1() { + assertEquals(0.4, solution1.probabilityOfHeads(new double[] {0.4}, 1)); + } + + @Test + public void test2() { + assertEquals( + 0.03125, solution1.probabilityOfHeads(new double[] {0.5, 0.5, 0.5, 0.5, 0.5}, 0)); + } + + @Test + public void test3() { + assertEquals(0.125, solution1.probabilityOfHeads(new double[] {0.5, 0.25}, 2)); + } + + @Test + public void test4() { + assertEquals(0.21875, solution1.probabilityOfHeads(new double[] {0.5, 0.25, 0.25}, 2)); + } + + @Test + public void test5() { + assertEquals(0.375, solution1.probabilityOfHeads(new double[] {0.5, 0.5, 0.5, 0.5}, 2)); + } + + @Test + public void test6() { + assertEquals( + 0.31250, + solution1.probabilityOfHeads(new double[] {0.5, 0.5, 0.5, 0.5, 0.5, 0.5}, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1232Test.java b/src/test/java/com/fishercoder/secondthousand/_1232Test.java new file mode 100644 index 0000000000..04166d01bd --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1232Test.java @@ -0,0 +1,70 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1232; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1232Test { + private _1232.Solution1 solution1; + private static int[][] coordinates; + + @BeforeEach + public void setup() { + solution1 = new _1232.Solution1(); + } + + @Test + public void test1() { + coordinates = + new int[][] { + {1, 2}, + {2, 3}, + {3, 4}, + {4, 5}, + {5, 6}, + {6, 7} + }; + assertEquals(true, solution1.checkStraightLine(coordinates)); + } + + @Test + public void test2() { + coordinates = + new int[][] { + {1, 1}, + {2, 2}, + {3, 4}, + {4, 5}, + {5, 6}, + {7, 7} + }; + assertEquals(false, solution1.checkStraightLine(coordinates)); + } + + @Test + public void test3() { + coordinates = + new int[][] { + {-3, -2}, + {-1, -2}, + {2, -2}, + {-2, -2}, + {0, -2} + }; + assertEquals(true, solution1.checkStraightLine(coordinates)); + } + + @Test + public void test4() { + coordinates = + new int[][] { + {0, 1}, + {1, 3}, + {-4, -7}, + {5, 11} + }; + assertEquals(true, solution1.checkStraightLine(coordinates)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1233Test.java b/src/test/java/com/fishercoder/secondthousand/_1233Test.java new file mode 100644 index 0000000000..bdf32b8894 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1233Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +import com.fishercoder.solutions.secondthousand._1233; +import java.util.ArrayList; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1233Test { + private _1233.Solution1 solution1; + private static String[] folder; + + @BeforeEach + public void setup() { + solution1 = new _1233.Solution1(); + } + + @Test + public void test1() { + folder = new String[] {"/a", "/a/b", "/c/d", "/c/d/e", "/c/f"}; + ArrayList expected = new ArrayList(); + expected.add("/a"); + expected.add("/c/d"); + expected.add("/c/f"); + assertThat(expected).hasSameElementsAs(solution1.removeSubfolders(folder)); + } + + @Test + public void test2() { + folder = new String[] {"/a", "/a/b/c", "/a/b/d"}; + ArrayList expected = new ArrayList(); + expected.add("/a"); + assertThat(expected).hasSameElementsAs(solution1.removeSubfolders(folder)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1243Test.java b/src/test/java/com/fishercoder/secondthousand/_1243Test.java new file mode 100644 index 0000000000..2ed88079f1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1243Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.secondthousand._1243; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1243Test { + private _1243.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1243.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {6, 2, 3, 4}; + assertTrue(solution1.transformArray(arr).equals(Arrays.asList(6, 3, 3, 4))); + } + + @Test + public void test2() { + arr = new int[] {1, 6, 3, 4, 3, 5}; + assertTrue(solution1.transformArray(arr).equals(Arrays.asList(1, 4, 4, 4, 4, 5))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1249Test.java b/src/test/java/com/fishercoder/secondthousand/_1249Test.java new file mode 100644 index 0000000000..c4f9a57aac --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1249Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.solutions.secondthousand._1249; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1249Test { + private _1249.Solution1 solution1; + private _1249.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1249.Solution1(); + solution2 = new _1249.Solution2(); + } + + @Test + public void test1() { + System.out.println(solution1.minRemoveToMakeValid("lee(t(c)o)de)")); + System.out.println(solution2.minRemoveToMakeValid("lee(t(c)o)de)")); + } + + @Test + public void test2() { + System.out.println(solution1.minRemoveToMakeValid("a)b(c)d")); + } + + @Test + public void test3() { + System.out.println(solution1.minRemoveToMakeValid("))((")); + } + + @Test + public void test4() { + System.out.println(solution1.minRemoveToMakeValid("(a(b(c)d)")); + } + + @Test + public void test5() { + System.out.println(solution1.minRemoveToMakeValid("())()(((")); // should be "()()" + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1252Test.java b/src/test/java/com/fishercoder/secondthousand/_1252Test.java new file mode 100644 index 0000000000..8999db5390 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1252Test.java @@ -0,0 +1,59 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1252; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1252Test { + private _1252.Solution1 solution1; + private _1252.Solution2 solution2; + private static int[][] indices; + + @BeforeEach + public void setup() { + solution1 = new _1252.Solution1(); + solution2 = new _1252.Solution2(); + } + + @Test + public void test1() { + indices = + new int[][] { + {0, 1}, + {1, 1} + }; + assertEquals(6, solution1.oddCells(2, 3, indices)); + } + + @Test + public void test2() { + indices = + new int[][] { + {1, 1}, + {0, 0} + }; + assertEquals(0, solution1.oddCells(2, 2, indices)); + } + + @Test + public void test3() { + indices = + new int[][] { + {0, 1}, + {1, 1} + }; + assertEquals(6, solution2.oddCells(2, 3, indices)); + } + + @Test + public void test4() { + indices = + new int[][] { + {1, 1}, + {0, 0} + }; + assertEquals(0, solution2.oddCells(2, 2, indices)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1257Test.java b/src/test/java/com/fishercoder/secondthousand/_1257Test.java new file mode 100644 index 0000000000..3335d49477 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1257Test.java @@ -0,0 +1,117 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1257; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1257Test { + private _1257.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1257.Solution1(); + } + + @Test + public void test1() { + assertEquals( + "North America", + solution1.findSmallestRegion( + Arrays.asList( + Arrays.asList("Earth", "North America", "South America"), + Arrays.asList("North America", "United States", "Canada"), + Arrays.asList("United States", "New York", "Boston"), + Arrays.asList("Canada", "Ontario", "Quebec"), + Arrays.asList("South America", "Brazil")), + "Quebec", + "New York")); + } + + @Test + public void test2() { + assertEquals( + "Canada", + solution1.findSmallestRegion( + Arrays.asList( + Arrays.asList("Earth", "North America", "South America"), + Arrays.asList("North America", "United States", "Canada"), + Arrays.asList("United States", "New York", "Boston"), + Arrays.asList("Canada", "Ontario", "Quebec"), + Arrays.asList("South America", "Brazil")), + "Canada", + "Quebec")); + } + + @Test + public void test3() { + assertEquals( + "Earth", + solution1.findSmallestRegion( + Arrays.asList( + Arrays.asList("Earth", "North America", "South America"), + Arrays.asList("North America", "United States", "Canada"), + Arrays.asList("United States", "New York", "Boston"), + Arrays.asList("Canada", "Ontario", "Quebec"), + Arrays.asList("South America", "Brazil")), + "Canada", + "South America")); + } + + @Test + public void test4() { + assertEquals( + "GfAj", + solution1.findSmallestRegion( + Arrays.asList( + Arrays.asList("zDkA", "GfAj", "lt"), + Arrays.asList("GfAj", "rtupD", "og", "l"), + Arrays.asList("rtupD", "IT", "jGcew", "ZwFqF"), + Arrays.asList("og", "yVobt", "EjA", "piUyQ"), + Arrays.asList("IT", "XFlc", "W", "rB"), + Arrays.asList("l", "GwQg", "shco", "Dub", "KwgZq"), + Arrays.asList("jGcew", "KH", "lbW"), + Arrays.asList("KH", "BZ", "sauG"), + Arrays.asList("sNyV", "WbrP"), + Arrays.asList("oXMG", "uqe"), + Arrays.asList("ALlyw", "jguyA", "Mi"), + Arrays.asList("PnGPY", "Ev", "lI"), + Arrays.asList("wmYF", "xreBK"), + Arrays.asList("x", "dclJ"), + Arrays.asList("JyOSt", "i"), + Arrays.asList("yEH", "UY", "GIwLp"), + Arrays.asList("lbW", "M"), + Arrays.asList("th", "JyOSt", "ALlyw"), + Arrays.asList("ZwFqF", "GDl"), + Arrays.asList("Zqk", "th"), + Arrays.asList("Aa", "wmYF"), + Arrays.asList("nQ", "IOw"), + Arrays.asList("oGg", "x"), + Arrays.asList("pLGYN", "ldb"), + Arrays.asList("XjpeC", "vK", "aaO", "D"), + Arrays.asList("a", "TekG", "zp"), + Arrays.asList("Dub", "PnGPY"), + Arrays.asList("SOvB", "iD", "pLGYN", "Zqk"), + Arrays.asList("bmFhM", "SOvB", "RWsEM", "z"), + Arrays.asList("SAH", "bmFhM"), + Arrays.asList("GEs", "oXMG", "tNJYJ"), + Arrays.asList("zh", "PWeEf"), + Arrays.asList("Mfb", "GEs", "XjpeC", "p"), + Arrays.asList("Sn", "rVIh", "twv", "pYA", "Ywm"), + Arrays.asList("piUyQ", "G", "aTi"), + Arrays.asList("If", "e", "y", "quEA", "sNyV"), + Arrays.asList("XFlc", "Sn", "ftXOZ"), + Arrays.asList("lt", "Q", "fWB", "a", "Wk", "zpqU"), + Arrays.asList("xsUkW", "Cssa", "TgPi", "qx"), + Arrays.asList("sauG", "If", "nK", "HHOr", "yEH", "YWMgF"), + Arrays.asList("shco", "xsUkW"), + Arrays.asList("GwQg", "Mfb", "gr", "S", "nQ"), + Arrays.asList("v", "SAH", "Rjr"), + Arrays.asList("BZ", "v", "zh", "oGg", "WP"), + Arrays.asList("yVobt", "Aa", "lJRmv")), + "RWsEM", + "GfAj")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1258Test.java b/src/test/java/com/fishercoder/secondthousand/_1258Test.java new file mode 100644 index 0000000000..4cffb0163b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1258Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1258; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1258Test { + + private _1258.Solution1 solution1; + private static List> synonyms; + + @BeforeEach + public void setup() { + solution1 = new _1258.Solution1(); + } + + @Test + public void test1() { + synonyms = + Arrays.asList( + Arrays.asList("happy", "joy"), + Arrays.asList("sad", "sorrow"), + Arrays.asList("joy", "cheerful")); + List expected = + Arrays.asList( + "I am cheerful today but was sad yesterday", + "I am cheerful today but was sorrow yesterday", + "I am happy today but was sad yesterday", + "I am happy today but was sorrow yesterday", + "I am joy today but was sad yesterday", + "I am joy today but was sorrow yesterday"); + assertEquals( + expected, + solution1.generateSentences(synonyms, "I am happy today but was sad yesterday")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1260Test.java b/src/test/java/com/fishercoder/secondthousand/_1260Test.java new file mode 100644 index 0000000000..fae81acc36 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1260Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1260; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1260Test { + private _1260.Solution1 solution1; + private static int[][] grid; + private static List> expected; + + @BeforeEach + public void setup() { + solution1 = new _1260.Solution1(); + } + + @Test + public void test1() { + grid = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + expected = + Arrays.asList( + Arrays.asList(9, 1, 2), Arrays.asList(3, 4, 5), Arrays.asList(6, 7, 8)); + assertEquals(expected, solution1.shiftGrid(grid, 1)); + } + + @Test + public void test2() { + grid = new int[][] {{1}, {2}, {3}, {4}, {7}, {6}, {5}}; + expected = + Arrays.asList( + Arrays.asList(6), + Arrays.asList(5), + Arrays.asList(1), + Arrays.asList(2), + Arrays.asList(3), + Arrays.asList(4), + Arrays.asList(7)); + assertEquals(expected, solution1.shiftGrid(grid, 23)); + } + + @Test + public void test3() { + grid = + new int[][] { + {3, 8, 1, 9}, + {19, 7, 2, 5}, + {4, 6, 11, 10}, + {12, 0, 21, 13} + }; + expected = + Arrays.asList( + Arrays.asList(12, 0, 21, 13), + Arrays.asList(3, 8, 1, 9), + Arrays.asList(19, 7, 2, 5), + Arrays.asList(4, 6, 11, 10)); + assertEquals(expected, solution1.shiftGrid(grid, 4)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1266Test.java b/src/test/java/com/fishercoder/secondthousand/_1266Test.java new file mode 100644 index 0000000000..ce34e9b30f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1266Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1266; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1266Test { + private _1266.Solution1 solution1; + private static int[][] points; + + @BeforeEach + public void setup() { + solution1 = new _1266.Solution1(); + } + + @Test + public void test1() { + points = + new int[][] { + {1, 1}, + {3, 4}, + {-1, 0} + }; + assertEquals(7, solution1.minTimeToVisitAllPoints(points)); + } + + @Test + public void test2() { + points = + new int[][] { + {3, 2}, + {-2, 2} + }; + assertEquals(5, solution1.minTimeToVisitAllPoints(points)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1267Test.java b/src/test/java/com/fishercoder/secondthousand/_1267Test.java new file mode 100644 index 0000000000..f5cdafb111 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1267Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1267; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1267Test { + private _1267.Solution1 solution1; + private static int[][] grid; + + @BeforeEach + public void setup() { + solution1 = new _1267.Solution1(); + } + + @Test + public void test1() { + grid = + new int[][] { + {1, 0}, + {0, 1} + }; + assertEquals(0, solution1.countServers(grid)); + } + + @Test + public void test2() { + grid = + new int[][] { + {1, 0}, + {1, 1} + }; + assertEquals(3, solution1.countServers(grid)); + } + + @Test + public void test3() { + grid = + new int[][] { + {1, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1} + }; + assertEquals(4, solution1.countServers(grid)); + } + + @Test + public void test4() { + grid = + new int[][] { + {1, 1, 0, 0}, + {1, 1, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1} + }; + assertEquals(6, solution1.countServers(grid)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1268Test.java b/src/test/java/com/fishercoder/secondthousand/_1268Test.java new file mode 100644 index 0000000000..5cbb1dbdf4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1268Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1268; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1268Test { + private _1268.Solution1 solution1; + private static List> expected; + private static String[] products; + + @BeforeEach + public void setup() { + solution1 = new _1268.Solution1(); + } + + @Test + public void test1() { + products = new String[] {"mobile", "mouse", "moneypot", "monitor", "mousepad"}; + expected = + Arrays.asList( + Arrays.asList("mobile", "moneypot", "monitor"), + Arrays.asList("mobile", "moneypot", "monitor"), + Arrays.asList("mouse", "mousepad"), + Arrays.asList("mouse", "mousepad"), + Arrays.asList("mouse", "mousepad")); + assertEquals(expected, solution1.suggestedProducts(products, "mouse")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1271Test.java b/src/test/java/com/fishercoder/secondthousand/_1271Test.java new file mode 100644 index 0000000000..e379ff34d5 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1271Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1271; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1271Test { + private _1271.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1271.Solution1(); + } + + @Test + public void test1() { + assertEquals("IOI", solution1.toHexspeak("257")); + } + + @Test + public void test2() { + assertEquals("ERROR", solution1.toHexspeak("3")); + } + + @Test + public void test3() { + assertEquals("ERROR", solution1.toHexspeak("619879596177")); + } + + @Test + public void test4() { + assertEquals("AEIDBCDIBC", solution1.toHexspeak("747823223228")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1273Test.java b/src/test/java/com/fishercoder/secondthousand/_1273Test.java new file mode 100644 index 0000000000..32a7c8ba5c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1273Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1273; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1273Test { + private _1273.Solution1 solution1; + private static int[] parent; + private static int[] value; + + @BeforeEach + public void setup() { + solution1 = new _1273.Solution1(); + } + + @Test + public void test1() { + parent = new int[] {-1, 0, 0, 1, 2, 2, 2}; + value = new int[] {1, -2, 4, 0, -2, -1, -1}; + assertEquals(2, solution1.deleteTreeNodes(7, parent, value)); + } + + @Test + public void test2() { + parent = new int[] {-1, 0, 0, 1, 2, 2, 2}; + value = new int[] {1, -2, 3, 0, -2, -1, 0}; + assertEquals(2, solution1.deleteTreeNodes(7, parent, value)); + } + + @Test + public void test3() { + parent = new int[] {-1, 0, 0, 1, 2, 2, 2}; + value = new int[] {1, -2, 4, 0, -2, -1, -2}; + assertEquals(6, solution1.deleteTreeNodes(7, parent, value)); + } + + @Test + public void test4() { + parent = new int[] {-1, 0, 0, 1, 2, 2, 2}; + value = new int[] {3, -2, 4, 0, -2, -1, -2}; + assertEquals(0, solution1.deleteTreeNodes(7, parent, value)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1275Test.java b/src/test/java/com/fishercoder/secondthousand/_1275Test.java new file mode 100644 index 0000000000..0d9de9f4e9 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1275Test.java @@ -0,0 +1,76 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1275; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1275Test { + private _1275.Solution1 solution1; + private static int[][] moves; + + @BeforeEach + public void setup() { + solution1 = new _1275.Solution1(); + } + + @BeforeEach + public void clear() { + solution1 = new _1275.Solution1(); + } + + @Test + public void test1() { + moves = + new int[][] { + {0, 0}, + {2, 0}, + {1, 1}, + {2, 1}, + {2, 2}, + }; + assertEquals("A", solution1.tictactoe(moves)); + } + + @Test + public void test2() { + moves = + new int[][] { + {0, 0}, + {1, 1}, + {0, 1}, + {0, 2}, + {1, 0}, + {2, 0}, + }; + assertEquals("B", solution1.tictactoe(moves)); + } + + @Test + public void test3() { + moves = + new int[][] { + {0, 0}, + {1, 1}, + {2, 0}, + {1, 0}, + {1, 2}, + {2, 1}, + {0, 1}, + {0, 2}, + {2, 2}, + }; + assertEquals("Draw", solution1.tictactoe(moves)); + } + + @Test + public void test4() { + moves = + new int[][] { + {0, 0}, + {1, 1}, + }; + assertEquals("Pending", solution1.tictactoe(moves)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1277Test.java b/src/test/java/com/fishercoder/secondthousand/_1277Test.java new file mode 100644 index 0000000000..38105fe0d8 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1277Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1277; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1277Test { + private _1277.Solution1 solution1; + private _1277.Solution2 solution2; + private static int[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _1277.Solution1(); + solution2 = new _1277.Solution2(); + } + + @Test + public void test1() { + matrix = + new int[][] { + {0, 1, 1, 1}, + {1, 1, 1, 1}, + {0, 1, 1, 1} + }; + assertEquals(15, solution1.countSquares(matrix)); + } + + @Test + public void test2() { + matrix = + new int[][] { + {0, 1, 1, 1}, + {1, 1, 1, 1}, + {0, 1, 1, 1} + }; + assertEquals(15, solution2.countSquares(matrix)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1281Test.java b/src/test/java/com/fishercoder/secondthousand/_1281Test.java new file mode 100644 index 0000000000..297889396d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1281Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1281; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1281Test { + private _1281.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1281.Solution1(); + } + + @Test + public void test1() { + assertEquals(15, solution1.subtractProductAndSum(234)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1282Test.java b/src/test/java/com/fishercoder/secondthousand/_1282Test.java new file mode 100644 index 0000000000..cfa0866113 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1282Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1282; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1282Test { + private _1282.Solution1 solution1; + private static int[] groupSizes; + + @BeforeEach + public void setup() { + solution1 = new _1282.Solution1(); + } + + @Test + public void test1() { + groupSizes = new int[] {3, 3, 3, 3, 3, 1, 3}; + CommonUtils.printListList(solution1.groupThePeople(groupSizes)); + } + + @Test + public void test2() { + groupSizes = new int[] {2, 1, 3, 3, 3, 2}; + CommonUtils.printListList(solution1.groupThePeople(groupSizes)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1283Test.java b/src/test/java/com/fishercoder/secondthousand/_1283Test.java new file mode 100644 index 0000000000..8ae6031468 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1283Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1283; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1283Test { + private _1283.Solution solution; + private static int[] nums; + private static int threshold; + + @BeforeEach + public void setup() { + solution = new _1283.Solution(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 5, 9}; + threshold = 6; + assertEquals(5, solution.smallestDivisor(nums, threshold)); + } + + @Test + public void test2() { + nums = new int[] {2, 3, 5, 7, 11}; + threshold = 11; + assertEquals(3, solution.smallestDivisor(nums, threshold)); + } +} diff --git a/src/test/java/com/fishercoder/_1286Test.java b/src/test/java/com/fishercoder/secondthousand/_1286Test.java similarity index 83% rename from src/test/java/com/fishercoder/_1286Test.java rename to src/test/java/com/fishercoder/secondthousand/_1286Test.java index 33713b09d1..4527838c45 100644 --- a/src/test/java/com/fishercoder/_1286Test.java +++ b/src/test/java/com/fishercoder/secondthousand/_1286Test.java @@ -1,12 +1,12 @@ -package com.fishercoder; +package com.fishercoder.secondthousand; -import com.fishercoder.solutions._1286; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.Assert.assertEquals; +import com.fishercoder.solutions.secondthousand._1286; +import org.junit.jupiter.api.Test; public class _1286Test { - private static _1286.Solution1.CombinationIterator combinationIterator; + private _1286.Solution1.CombinationIterator combinationIterator; @Test public void test1() { @@ -38,5 +38,4 @@ public void test3() { assertEquals("bcd", combinationIterator.next()); assertEquals(false, combinationIterator.hasNext()); } - -} \ No newline at end of file +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1287Test.java b/src/test/java/com/fishercoder/secondthousand/_1287Test.java new file mode 100644 index 0000000000..cf46eb9067 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1287Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1287; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1287Test { + private _1287.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1287.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.findSpecialInteger(new int[] {1, 2, 2, 6, 6, 6, 6, 7, 10})); + } + + @Test + public void test2() { + assertEquals(1, solution1.findSpecialInteger(new int[] {1})); + } + + @Test + public void test3() { + assertEquals(3, solution1.findSpecialInteger(new int[] {1, 2, 3, 3})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1289Test.java b/src/test/java/com/fishercoder/secondthousand/_1289Test.java new file mode 100644 index 0000000000..6f3afecebe --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1289Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1289; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1289Test { + private _1289.Solution1 solution1; + private static int[][] arr; + + @BeforeEach + public void setup() { + solution1 = new _1289.Solution1(); + } + + @Test + public void test1() { + arr = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + assertEquals(13, solution1.minFallingPathSum(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1290Test.java b/src/test/java/com/fishercoder/secondthousand/_1290Test.java new file mode 100644 index 0000000000..9d4fd86f4c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1290Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.secondthousand._1290; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1290Test { + private _1290.Solution1 solution1; + private _1290.Solution2 solution2; + private static ListNode head; + + @BeforeEach + public void setup() { + solution1 = new _1290.Solution1(); + solution2 = new _1290.Solution2(); + } + + @Test + public void test1() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 0, 1)); + assertEquals(5, solution1.getDecimalValue(head)); + } + + @Test + public void test2() { + head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 1, 1)); + assertEquals(7, solution2.getDecimalValue(head)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1291Test.java b/src/test/java/com/fishercoder/secondthousand/_1291Test.java new file mode 100644 index 0000000000..4abc6900e7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1291Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1291; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +@Disabled +public class _1291Test { + private _1291.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1291.Solution1(); + } + + @Test + public void test1() { + assertEquals(Arrays.asList(), solution1.sequentialDigits(100, 300)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1295Test.java b/src/test/java/com/fishercoder/secondthousand/_1295Test.java new file mode 100644 index 0000000000..3c55dfba3d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1295Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1295; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1295Test { + private _1295.Solution1 solution1; + private _1295.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1295.Solution1(); + solution2 = new _1295.Solution2(); + } + + @Test + public void test1() { + assertEquals(2, solution1.findNumbers(new int[] {12, 345, 2, 6, 7896})); + assertEquals(2, solution2.findNumbers(new int[] {12, 345, 2, 6, 7896})); + } + + @Test + public void test2() { + assertEquals(1, solution1.findNumbers(new int[] {555, 901, 482, 1771})); + assertEquals(1, solution2.findNumbers(new int[] {555, 901, 482, 1771})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1296Test.java b/src/test/java/com/fishercoder/secondthousand/_1296Test.java new file mode 100644 index 0000000000..17b1435357 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1296Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1296; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1296Test { + private _1296.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1296.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.isPossibleDivide(new int[] {1, 2, 3, 3, 4, 4, 5, 6}, 4)); + } + + @Test + public void test2() { + assertEquals( + true, + solution1.isPossibleDivide(new int[] {3, 2, 1, 2, 3, 4, 3, 4, 5, 9, 10, 11}, 3)); + } + + @Test + public void test3() { + assertEquals(true, solution1.isPossibleDivide(new int[] {3, 3, 2, 2, 1, 1}, 3)); + } + + @Test + public void test4() { + assertEquals(false, solution1.isPossibleDivide(new int[] {1, 2, 3, 4}, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1297Test.java b/src/test/java/com/fishercoder/secondthousand/_1297Test.java new file mode 100644 index 0000000000..d84ca4e41c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1297Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1297; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1297Test { + private _1297.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1297.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.maxFreq("aababcaab", 2, 3, 4)); + } + + @Test + public void test2() { + assertEquals(2, solution1.maxFreq("aaaa", 1, 3, 3)); + } + + @Test + public void test3() { + assertEquals(3, solution1.maxFreq("aabcabcab", 2, 2, 3)); + } + + @Test + public void test4() { + assertEquals(0, solution1.maxFreq("abcde", 2, 3, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1299Test.java b/src/test/java/com/fishercoder/secondthousand/_1299Test.java new file mode 100644 index 0000000000..edfd4256a6 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1299Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1299; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1299Test { + private _1299.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1299.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {17, 18, 5, 4, 6, 1}; + assertArrayEquals(new int[] {18, 6, 6, 6, 1, -1}, solution1.replaceElements(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1300Test.java b/src/test/java/com/fishercoder/secondthousand/_1300Test.java new file mode 100644 index 0000000000..0602e8294e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1300Test.java @@ -0,0 +1,71 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1300; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1300Test { + private _1300.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1300.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {4, 9, 3}; + assertEquals(3, solution1.findBestValue(arr, 10)); + } + + @Test + public void test2() { + arr = new int[] {2, 3, 5}; + assertEquals(5, solution1.findBestValue(arr, 10)); + } + + @Test + public void test3() { + arr = new int[] {60864, 25176, 27249, 21296, 20204}; + assertEquals(11361, solution1.findBestValue(arr, 56803)); + } + + @Test + public void test4() { + arr = new int[] {2, 3, 5}; + assertEquals(5, solution1.findBestValue(arr, 11)); + } + + @Test + public void test5() { + arr = new int[] {60864, 25176, 27249, 21296, 20204}; + assertEquals(11361, solution1.findBestValue(arr, 56803)); + } + + @Test + public void test6() { + arr = new int[] {48772, 52931, 14253, 32289, 75263}; + assertEquals(8175, solution1.findBestValue(arr, 40876)); + } + + @Test + public void test7() { + arr = new int[] {1547, 83230, 57084, 93444, 70879}; + assertEquals(17422, solution1.findBestValue(arr, 71237)); + } + + @Test + public void test8() { + arr = new int[] {1, 1, 2}; + assertEquals(2, solution1.findBestValue(arr, 10)); + } + + @Test + public void test9() { + arr = new int[] {1, 1, 1}; + assertEquals(1, solution1.findBestValue(arr, 10)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1302Test.java b/src/test/java/com/fishercoder/secondthousand/_1302Test.java new file mode 100644 index 0000000000..e94bde8f0f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1302Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1302; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1302Test { + private _1302.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1302.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 15, + solution1.deepestLeavesSum( + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 2, 3, 4, 5, null, 6, 7, null, null, null, null, 8)))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1304Test.java b/src/test/java/com/fishercoder/secondthousand/_1304Test.java new file mode 100644 index 0000000000..d3db0f4129 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1304Test.java @@ -0,0 +1,20 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1304; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1304Test { + private _1304.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1304.Solution1(); + } + + @Test + public void test1() { + CommonUtils.printArray(solution1.sumZero(5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1305Test.java b/src/test/java/com/fishercoder/secondthousand/_1305Test.java new file mode 100644 index 0000000000..ec76c104fb --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1305Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1305; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1305Test { + private _1305.Solution1 solution1; + private static TreeNode root1; + private static TreeNode root2; + + @BeforeEach + public void setup() { + solution1 = new _1305.Solution1(); + } + + @Test + public void test1() { + root1 = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 4)); + root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 3)); + assertEquals(Arrays.asList(0, 1, 1, 2, 3, 4), solution1.getAllElements(root1, root2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1309Test.java b/src/test/java/com/fishercoder/secondthousand/_1309Test.java new file mode 100644 index 0000000000..da824cbdf4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1309Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1309; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1309Test { + private _1309.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1309.Solution1(); + } + + @Test + public void test1() { + assertEquals("jkab", solution1.freqAlphabets("10#11#12")); + } + + @Test + public void test2() { + assertEquals("acz", solution1.freqAlphabets("1326#")); + } + + @Test + public void test3() { + assertEquals("y", solution1.freqAlphabets("25#")); + } + + @Test + public void test4() { + assertEquals( + "abcdefghijklmnopqrstuvwxyz", + solution1.freqAlphabets( + "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1313Test.java b/src/test/java/com/fishercoder/secondthousand/_1313Test.java new file mode 100644 index 0000000000..0f62f8833d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1313Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1313; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1313Test { + private _1313.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1313.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {2, 4, 4, 4}, solution1.decompressRLElist(new int[] {1, 2, 3, 4})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1314Test.java b/src/test/java/com/fishercoder/secondthousand/_1314Test.java new file mode 100644 index 0000000000..1cbe1b5865 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1314Test.java @@ -0,0 +1,56 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1314; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1314Test { + private _1314.Solution1 solution1; + private _1314.Solution2 solution2; + private static int[][] mat; + private static int[][] expected; + + @BeforeEach + public void setup() { + solution1 = new _1314.Solution1(); + solution2 = new _1314.Solution2(); + } + + @Test + public void test1() { + mat = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + expected = + new int[][] { + {12, 21, 16}, + {27, 45, 33}, + {24, 39, 28} + }; + assertArrayEquals(expected, solution1.matrixBlockSum(mat, 1)); + assertArrayEquals(expected, solution2.matrixBlockSum(mat, 1)); + } + + @Test + public void test2() { + mat = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + expected = + new int[][] { + {45, 45, 45}, + {45, 45, 45}, + {45, 45, 45} + }; + assertArrayEquals(expected, solution1.matrixBlockSum(mat, 2)); + assertArrayEquals(expected, solution2.matrixBlockSum(mat, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1315Test.java b/src/test/java/com/fishercoder/secondthousand/_1315Test.java new file mode 100644 index 0000000000..298e1bc73b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1315Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1315; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1315Test { + private _1315.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _1315.Solution1(); + } + + @Test + public void test1() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList(6, 7, 8, 2, 7, 1, 3, 9, null, 1, 4, null, null, null, 5)); + TreeUtils.printBinaryTree(root); + assertEquals(18, solution1.sumEvenGrandparent(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1317Test.java b/src/test/java/com/fishercoder/secondthousand/_1317Test.java new file mode 100644 index 0000000000..15c8e25182 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1317Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1317; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1317Test { + private _1317.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1317.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals(new int[] {11, 999}, solution1.getNoZeroIntegers(1010)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1323Test.java b/src/test/java/com/fishercoder/secondthousand/_1323Test.java new file mode 100644 index 0000000000..40fad27517 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1323Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1323; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1323Test { + private _1323.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1323.Solution1(); + } + + @Test + public void test1() { + assertEquals(9969, solution1.maximum69Number(9669)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1324Test.java b/src/test/java/com/fishercoder/secondthousand/_1324Test.java new file mode 100644 index 0000000000..c105205baf --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1324Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1324; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1324Test { + private _1324.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1324.Solution1(); + } + + @Test + public void test1() { + assertEquals(Arrays.asList("HAY", "ORO", "WEU"), solution1.printVertically("HOW ARE YOU")); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList("TBONTB", "OEROOE", " T"), + solution1.printVertically("TO BE OR NOT TO BE")); + } + + @Test + public void test3() { + assertEquals( + Arrays.asList("CIC", "OSO", "N M", "T I", "E N", "S G", "T"), + solution1.printVertically("CONTEST IS COMING")); + } +} diff --git a/src/test/java/com/fishercoder/_1325Test.java b/src/test/java/com/fishercoder/secondthousand/_1325Test.java similarity index 91% rename from src/test/java/com/fishercoder/_1325Test.java rename to src/test/java/com/fishercoder/secondthousand/_1325Test.java index 4dc8f3049b..208fa87e67 100644 --- a/src/test/java/com/fishercoder/_1325Test.java +++ b/src/test/java/com/fishercoder/secondthousand/_1325Test.java @@ -1,23 +1,22 @@ -package com.fishercoder; +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; import com.fishercoder.common.classes.TreeNode; import com.fishercoder.common.utils.TreeUtils; -import com.fishercoder.solutions._1325; -import org.junit.BeforeClass; -import org.junit.Test; - +import com.fishercoder.solutions.secondthousand._1325; import java.util.Arrays; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class _1325Test { - private static _1325.Solution1 solution1; - private static _1325.Solution2 solution2; + private _1325.Solution1 solution1; + private _1325.Solution2 solution2; private static TreeNode root; private static TreeNode expected; - @BeforeClass - public static void setup() { + @BeforeEach + public void setup() { solution1 = new _1325.Solution1(); solution2 = new _1325.Solution2(); } @@ -111,5 +110,4 @@ public void test10() { TreeUtils.printBinaryTree(expected); assertEquals(expected, solution2.removeLeafNodes(root, 1)); } - -} \ No newline at end of file +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1331Test.java b/src/test/java/com/fishercoder/secondthousand/_1331Test.java new file mode 100644 index 0000000000..b65ecd0550 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1331Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1331; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1331Test { + private _1331.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1331.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {40, 10, 20, 30}; + assertArrayEquals(new int[] {4, 1, 2, 3}, solution1.arrayRankTransform(arr)); + } + + @Test + public void test2() { + arr = new int[] {100, 100, 100}; + assertArrayEquals(new int[] {1, 1, 1}, solution1.arrayRankTransform(arr)); + } + + @Test + public void test3() { + arr = new int[] {-1, -3, 100}; + assertArrayEquals(new int[] {2, 1, 3}, solution1.arrayRankTransform(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1333Test.java b/src/test/java/com/fishercoder/secondthousand/_1333Test.java new file mode 100644 index 0000000000..f2e5d4bbaf --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1333Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1333; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1333Test { + private _1333.Solution1 solution1; + private static int[][] restaurants; + + @BeforeEach + public void setup() { + solution1 = new _1333.Solution1(); + } + + @Test + public void test1() { + restaurants = + new int[][] { + {1, 4, 1, 40, 10}, + {2, 8, 0, 50, 5}, + {3, 8, 1, 30, 4}, + {4, 10, 0, 10, 3}, + {5, 1, 1, 15, 1} + }; + assertEquals(Arrays.asList(3, 1, 5), solution1.filterRestaurants(restaurants, 1, 50, 10)); + } + + @Test + public void test2() { + restaurants = + new int[][] { + {1, 4, 1, 40, 10}, + {2, 8, 0, 50, 5}, + {3, 8, 1, 30, 4}, + {4, 10, 0, 10, 3}, + {5, 1, 1, 15, 1} + }; + assertEquals( + Arrays.asList(4, 3, 2, 1, 5), solution1.filterRestaurants(restaurants, 0, 50, 10)); + } + + @Test + public void test3() { + restaurants = + new int[][] { + {1, 4, 1, 40, 10}, + {2, 8, 0, 50, 5}, + {3, 8, 1, 30, 4}, + {4, 10, 0, 10, 3}, + {5, 1, 1, 15, 1} + }; + assertEquals(Arrays.asList(4, 5), solution1.filterRestaurants(restaurants, 0, 30, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1334Test.java b/src/test/java/com/fishercoder/secondthousand/_1334Test.java new file mode 100644 index 0000000000..6b890d3bdb --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1334Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1334; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1334Test { + private _1334.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1334.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 3, + solution1.findTheCity( + 4, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1,3],[1,2,1],[1,3,4],[2,3,1]"), + 4)); + } + + @Test + public void test2() { + assertEquals( + 5, + solution1.findTheCity( + 6, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1,10],[0,2,1],[2,3,1],[1,3,1],[1,4,1],[4,5,10]"), + 20)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1337Test.java b/src/test/java/com/fishercoder/secondthousand/_1337Test.java new file mode 100644 index 0000000000..1ad8b83b86 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1337Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1337; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1337Test { + private _1337.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1337.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.removePalindromeSub("ababa")); + } + + @Test + public void test2() { + assertEquals(2, solution1.removePalindromeSub("abb")); + } + + @Test + public void test3() { + assertEquals(2, solution1.removePalindromeSub("baabb")); + } + + @Test + public void test4() { + assertEquals(0, solution1.removePalindromeSub("")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1338Test.java b/src/test/java/com/fishercoder/secondthousand/_1338Test.java new file mode 100644 index 0000000000..ec850f659b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1338Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1338; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1338Test { + private _1338.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1338.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {3, 3, 3, 3, 5, 5, 5, 2, 2, 7}; + assertEquals(2, solution1.minSetSize(arr)); + } + + @Test + public void test2() { + arr = new int[] {7, 7, 7, 7, 7, 7}; + assertEquals(1, solution1.minSetSize(arr)); + } + + @Test + public void test3() { + arr = new int[] {1, 9}; + assertEquals(1, solution1.minSetSize(arr)); + } + + @Test + public void test4() { + arr = new int[] {1000, 1000, 3, 7}; + assertEquals(1, solution1.minSetSize(arr)); + } + + @Test + public void test5() { + arr = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + assertEquals(5, solution1.minSetSize(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1339Test.java b/src/test/java/com/fishercoder/secondthousand/_1339Test.java new file mode 100644 index 0000000000..76808dcf35 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1339Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1339; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1339Test { + private _1339.Solution1 solution1; + private _1339.Solution2 solution2; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _1339.Solution1(); + solution2 = new _1339.Solution2(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6)); + assertEquals(110, solution1.maxProduct(root)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6)); + assertEquals(110, solution2.maxProduct(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1341Test.java b/src/test/java/com/fishercoder/secondthousand/_1341Test.java new file mode 100644 index 0000000000..5722126102 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1341Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1341; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1341Test { + private _1341.Solution1 solution1; + private static int[][] mat; + + @BeforeEach + public void setup() { + solution1 = new _1341.Solution1(); + } + + @Test + public void test1() { + mat = + new int[][] { + {1, 1, 0, 0, 0}, + {1, 1, 1, 1, 0}, + {1, 0, 0, 0, 0}, + {1, 1, 0, 0, 0}, + {1, 1, 1, 1, 1} + }; + assertArrayEquals(new int[] {2, 0}, solution1.kWeakestRows(mat, 2)); + } + + @Test + public void test2() { + mat = + new int[][] { + {1, 0, 0, 0}, + {1, 1, 1, 1}, + {1, 0, 0, 0}, + {1, 0, 0, 0} + }; + assertArrayEquals(new int[] {0, 2}, solution1.kWeakestRows(mat, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1342Test.java b/src/test/java/com/fishercoder/secondthousand/_1342Test.java new file mode 100644 index 0000000000..37c021cf71 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1342Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1342; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1342Test { + private _1342.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1342.Solution1(); + } + + @Test + public void test1() { + assertEquals(12, solution1.numberOfSteps(123)); + } + + @Test + public void test2() { + assertEquals(6, solution1.numberOfSteps(14)); + } + + @Test + public void test3() { + assertEquals(4, solution1.numberOfSteps(8)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1343Test.java b/src/test/java/com/fishercoder/secondthousand/_1343Test.java new file mode 100644 index 0000000000..790bd4a23d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1343Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1343; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1343Test { + private _1343.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1343.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.numOfSubarrays(new int[] {2, 2, 2, 2, 5, 5, 5, 8}, 3, 4)); + } + + @Test + public void test2() { + assertEquals(5, solution1.numOfSubarrays(new int[] {1, 1, 1, 1, 1}, 1, 0)); + } + + @Test + public void test3() { + assertEquals( + 6, solution1.numOfSubarrays(new int[] {11, 13, 17, 23, 29, 31, 7, 5, 2, 3}, 3, 5)); + } + + @Test + public void test4() { + assertEquals(1, solution1.numOfSubarrays(new int[] {7, 7, 7, 7, 7, 7, 7}, 7, 7)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1344Test.java b/src/test/java/com/fishercoder/secondthousand/_1344Test.java new file mode 100644 index 0000000000..9161305cc4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1344Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1344; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1344Test { + private _1344.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1344.Solution1(); + } + + @Test + public void test1() { + assertEquals(165, solution1.angleClock(12, 30), 0); + } + + @Test + public void test2() { + assertEquals(75, solution1.angleClock(3, 30), 0); + } + + @Test + public void test3() { + assertEquals(155, solution1.angleClock(4, 50), 0); + } + + @Test + public void test4() { + assertEquals(0, solution1.angleClock(12, 0), 0); + } + + @Test + public void test5() { + assertEquals(76.5, solution1.angleClock(1, 57), 0); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1345Test.java b/src/test/java/com/fishercoder/secondthousand/_1345Test.java new file mode 100644 index 0000000000..73e9e658a3 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1345Test.java @@ -0,0 +1,57 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1345; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1345Test { + private _1345.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1345.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {100, -23, -23, 404, 100, 23, 23, 23, 3, 404}; + assertEquals(3, solution1.minJumps(arr)); + } + + @Test + public void test2() { + arr = new int[] {7}; + assertEquals(0, solution1.minJumps(arr)); + } + + @Test + public void test3() { + arr = new int[] {7, 6, 9, 6, 9, 6, 9, 7}; + assertEquals(1, solution1.minJumps(arr)); + } + + @Test + public void test4() { + arr = new int[] {6, 1, 9}; + assertEquals(2, solution1.minJumps(arr)); + } + + @Test + public void test5() { + arr = new int[] {11, 22, 7, 7, 7, 7, 7, 7, 7, 22, 13}; + assertEquals(3, solution1.minJumps(arr)); + } + + @Test + public void test6() { + arr = + new int[] { + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 11 + }; + assertEquals(2, solution1.minJumps(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1346Test.java b/src/test/java/com/fishercoder/secondthousand/_1346Test.java new file mode 100644 index 0000000000..73ed14b2e9 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1346Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1346; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1346Test { + private _1346.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1346.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {10, 2, 5, 3}; + assertEquals(true, solution1.checkIfExist(arr)); + } + + @Test + public void test2() { + arr = new int[] {7, 1, 14, 11}; + assertEquals(true, solution1.checkIfExist(arr)); + } + + @Test + public void test3() { + arr = new int[] {3, 1, 7, 11}; + assertEquals(false, solution1.checkIfExist(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1347Test.java b/src/test/java/com/fishercoder/secondthousand/_1347Test.java new file mode 100644 index 0000000000..7b3c1abd94 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1347Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1347; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1347Test { + private _1347.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1347.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minSteps("bab", "aba")); + } + + @Test + public void test2() { + assertEquals(5, solution1.minSteps("leetcode", "practice")); + } + + @Test + public void test3() { + assertEquals(0, solution1.minSteps("anagram", "mangaar")); + } + + @Test + public void test4() { + assertEquals(0, solution1.minSteps("xxyyzz", "xxyyzz")); + } + + @Test + public void test5() { + assertEquals(4, solution1.minSteps("friend", "family")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1348Test.java b/src/test/java/com/fishercoder/secondthousand/_1348Test.java new file mode 100644 index 0000000000..435aee45ba --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1348Test.java @@ -0,0 +1,65 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1348; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _1348Test { + private _1348.Solution1.TweetCounts tweetCounts; + + @Test + public void test1() { + tweetCounts = new _1348.Solution1.TweetCounts(); + + tweetCounts.recordTweet("tweet3", 0); + tweetCounts.recordTweet("tweet3", 60); + tweetCounts.recordTweet("tweet3", 10); + assertEquals( + Arrays.asList(2), + tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59)); + assertEquals( + Arrays.asList(2, 1), + tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60)); + tweetCounts.recordTweet("tweet3", 120); + assertEquals( + Arrays.asList(4), tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210)); + } + + @Test + public void test2() { + tweetCounts = new _1348.Solution1.TweetCounts(); + + tweetCounts.recordTweet("tweet0", 33); + tweetCounts.recordTweet("tweet1", 89); + tweetCounts.recordTweet("tweet2", 99); + tweetCounts.recordTweet("tweet3", 53); + tweetCounts.recordTweet("tweet4", 3); + assertEquals( + Arrays.asList(0), + tweetCounts.getTweetCountsPerFrequency("hour", "tweet0", 89, 3045)); + tweetCounts.recordTweet("tweet0", 28); + tweetCounts.recordTweet("tweet0", 91); + tweetCounts.recordTweet("tweet0", 9); + tweetCounts.recordTweet("tweet1", 6); + } + + @Test + public void test3() { + tweetCounts = new _1348.Solution1.TweetCounts(); + + tweetCounts.recordTweet("tweet0", 13); + tweetCounts.recordTweet("tweet1", 16); + tweetCounts.recordTweet("tweet2", 12); + tweetCounts.recordTweet("tweet3", 18); + tweetCounts.recordTweet("tweet4", 82); + tweetCounts.recordTweet("tweet3", 89); + assertEquals( + Arrays.asList(0), + tweetCounts.getTweetCountsPerFrequency("day", "tweet0", 89, 9471)); + assertEquals( + Arrays.asList(2, 0), + tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 13, 4024)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1349Test.java b/src/test/java/com/fishercoder/secondthousand/_1349Test.java new file mode 100644 index 0000000000..cf3ab2ca04 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1349Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1349; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1349Test { + private _1349.Solution1 solution1; + private static char[][] seats; + + @BeforeEach + public void setup() { + solution1 = new _1349.Solution1(); + } + + @Test + public void test1() { + seats = + new char[][] { + {'#', '.', '#', '#', '.', '#'}, + {'.', '#', '#', '#', '#', '.'}, + {'#', '.', '#', '#', '.', '#'} + }; + assertEquals(4, solution1.maxStudents(seats)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1352Test.java b/src/test/java/com/fishercoder/secondthousand/_1352Test.java new file mode 100644 index 0000000000..a5f156fc32 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1352Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1352; +import org.junit.jupiter.api.Test; + +public class _1352Test { + private _1352.Solution1.ProductOfNumbers productOfNumbers; + + @Test + public void test1() { + productOfNumbers = new _1352.Solution1.ProductOfNumbers(); + productOfNumbers.add(3); + productOfNumbers.add(0); + productOfNumbers.add(2); + productOfNumbers.add(5); + productOfNumbers.add(4); + assertEquals(20, productOfNumbers.getProduct(2)); + assertEquals(40, productOfNumbers.getProduct(3)); + assertEquals(0, productOfNumbers.getProduct(4)); + productOfNumbers.add(8); + assertEquals(32, productOfNumbers.getProduct(2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1353Test.java b/src/test/java/com/fishercoder/secondthousand/_1353Test.java new file mode 100644 index 0000000000..8d330e3f28 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1353Test.java @@ -0,0 +1,74 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1353; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1353Test { + private _1353.Solution1 solution1; + private static int[][] events; + + @BeforeEach + public void setup() { + solution1 = new _1353.Solution1(); + } + + @Test + public void test1() { + events = + new int[][] { + {1, 2}, + {2, 3}, + {3, 4} + }; + assertEquals(3, solution1.maxEvents(events)); + } + + @Test + public void test2() { + events = + new int[][] { + {1, 2}, + {2, 3}, + {3, 4}, + {1, 2} + }; + assertEquals(4, solution1.maxEvents(events)); + } + + @Test + public void test3() { + events = + new int[][] { + {1, 4}, + {4, 4}, + {2, 2}, + {3, 4}, + {1, 1} + }; + assertEquals(4, solution1.maxEvents(events)); + } + + @Test + public void test4() { + events = new int[][] {{1, 100000}}; + assertEquals(1, solution1.maxEvents(events)); + } + + @Test + public void test5() { + events = + new int[][] { + {1, 1}, + {1, 2}, + {1, 3}, + {1, 4}, + {1, 5}, + {1, 6}, + {1, 7}, + }; + assertEquals(7, solution1.maxEvents(events)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1354Test.java b/src/test/java/com/fishercoder/secondthousand/_1354Test.java new file mode 100644 index 0000000000..b1ba27621a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1354Test.java @@ -0,0 +1,174 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1354; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1354Test { + private _1354.Solution1 solution1; + private static int[] target; + + @BeforeEach + public void setup() { + solution1 = new _1354.Solution1(); + } + + @Test + public void test1() { + target = new int[] {9, 3, 5}; + assertEquals(true, solution1.isPossible(target)); + } + + @Test + public void test2() { + target = new int[] {1, 1, 1, 2}; + assertEquals(false, solution1.isPossible(target)); + } + + @Test + public void test3() { + target = new int[] {8, 5}; + assertEquals(true, solution1.isPossible(target)); + } + + @Test + public void test9() { + target = new int[] {9, 9, 9}; + assertEquals(false, solution1.isPossible(target)); + } + + @Test + public void test11() { + target = new int[] {1, 1, 2}; + assertEquals(false, solution1.isPossible(target)); + } + + @Test + public void test12() { + target = new int[] {1, 1000000000}; + assertEquals(true, solution1.isPossible(target)); + } + + @Test + public void test13() { + target = new int[] {5, 50}; + assertEquals(false, solution1.isPossible(target)); + } + + @Test + public void test10() { + target = + new int[] { + 835647834, 503037935, 773002076, 731298404, 903645595, 488189634, 319785391, + 111546683, 609144970, 415205491, 685900245, 878508756, 236413773, 991053691, + 947595808, 913014938, 533038855, 88376603, 436545178, 983496954, 122530918, + 346542007, 55893465, 472984628, 347337093, 322587100, 552866643, 609759356, + 288893937, 471774337, 465491640, 783022406, 699817530, 340584553, 663909719, + 651419106, 846593366, 952609573, 912379694, 661318302, 538633771, 745093202, + 753577352, 60741272, 800245613, 228718955, 314289253, 384902244, 834091366, + 330486268, 832528567, 405339553, 667374764, 477631332, 458512135, 281436435, + 301856749, 331522322, 439316110, 65782135, 313620054, 377064760, 689776101, + 453352404, 739524725, 113039032, 403624252, 864855957, 816177580, 472331359, + 702356269, 634580725, 79566086, 723272803, 484129094, 785382934, 519691527, + 303358848, 673141033, 900376058, 640090231, 332948759, 3578533, 603932450, + 300252459, 455172786, 398327644, 667611961, 579527425, 847780358, 251487657, + 239105566, 949519075, 816672375, 569680210, 522034786, 433488047, 339341869, + 323607606, 695508020, 671840506, 403376732, 437224135, 704526427, 733331510, + 566004060, 190603334, 401309800, 135615700, 480920888, 880495868, 394755529, + 754131300, 980354442, 940475050, 455976643, 26150213, 620704469, 305714399, + 452126616, 331922227, 285017717, 547688077, 571029451, 580099665, 888037179, + 116069830, 492086251, 460673303, 652403652, 529457074, 959765712, 884239314, + 707976976, 421820667, 102910899, 850649035, 332557694, 156833539, 193014693, + 712889809, 65402492, 81873875, 205826928, 906576787, 536580780, 98764481, + 614216242, 724741649, 926963940, 415525579, 707467586, 670685849, 645316339, + 663012408, 451540628, 190074574, 100279961, 373676876, 108645392, 439186589, + 289025528, 317751557, 696247292, 252536329, 524764647, 664019135, 681731166, + 697044386, 721419536, 89117662, 386734310, 933623270, 18288756, 858592177, + 843786785, 267433641, 104266328, 816027769, 103604036, 874219314, 626912072, + 643973051, 138646542, 930635468, 638385362, 666376936, 75446650, 725532251, + 558802301, 898021675, 602462415, 287100257, 103256759, 241469306, 418203100, + 359820338, 439745508, 711532895, 913171524, 828285226, 490697952, 94810193, + 272869408, 26560972, 979834901, 5721504, 185048349, 42545696, 396115122, + 904051513, 220906107, 738993493, 588839845, 742987869, 641939237, 20134580, + 613427664, 760712842, 725934923, 233138479, 694808775, 504699750, 45306699, + 928921991, 348537469, 341584856, 314823473, 323200569, 5906520, 590908812, + 7058731, 612527161, 95929757, 923027538, 504340570, 108267579, 981049825, + 3621484, 268758794, 155138552, 967177696, 154587439, 226488698, 435889878, + 282062129, 837804113, 874696398, 704916352, 672370979, 429701338, 699214790, + 311303195, 947796876, 50395096, 943382310, 264134145, 110060599, 662341190, + 707099896, 778109433, 209892183, 129970328, 614555781, 65561327, 723924768, + 308208692, 907464540, 511861513, 664166961, 278299804, 383189150, 305144703, + 399367644, 745158781, 611505481, 788035592, 958094209, 4691422, 982804694, + 182195783, 644444281, 888532003, 913888496, 781449985, 252740240, 661510236, + 998487245, 284318090, 646237565, 822471400, 371153017, 988783, 234143485, + 906623027, 265667900, 88762375, 654203306, 124205686, 724165887, 715879556, + 778678123, 201533689, 241172717, 220243793, 300732194, 834092268, 546274723, + 978426161, 544966162, 860580567, 833904274, 408002579, 683573852, 414196298, + 567536223, 401758309, 449922489, 264257881, 552000887, 313883439, 121158141, + 816111867, 426696045, 166578559, 869183250, 677763442, 449243505, 446613544, + 618933881, 765618361, 782239559, 139564825, 17361518, 375149549, 752695711, + 170774896, 24223979, 929368291, 714737788, 968883177, 418171397, 28938451, + 530535587, 831491933, 388232832, 375471966, 501547932, 42057135, 105907929, + 319325198, 866969758, 883623220, 636639049, 98250237, 119670840, 810521959, + 994818843, 979182684, 698619283, 306787611, 79779236, 316097105, 20407838, + 246365554, 405157606, 746584313, 338447287, 956653822, 157277229, 382734547, + 919850823, 17698838, 400783732, 846595424, 820236688, 844188244, 592045378, + 664969413, 199985888, 986952695, 924241714, 545487784, 487539172, 147186879, + 945151212, 958877753, 669099388, 403471548, 499546716, 915071435, 897865485, + 892380510, 328284017, 649646782, 564916899, 126507447, 341928577, 916918420, + 335542645, 562151880, 341948589, 153658487, 915169176, 189949851, 956608598, + 92763236, 671268832, 829439855, 198464213, 96704707, 504412819, 126161975, + 935193455, 513168023, 134286447, 385405295, 615567547, 17991890, 790707341, + 991003956, 917307082, 145826213, 725182412, 707571456, 193431873, 694550855, + 988821536, 296476226, 166333863, 595537769, 579249117, 788071113, 445822525, + 312987370, 690589050, 707167073, 734770753, 836025747, 398024391, 614034356, + 19892950, 177961692, 901857208, 570179250, 880745468, 628351955, 471663624, + 537067058, 620422588, 801133337, 443608528, 655426152, 630822673, 323931531, + 616054005, 715419230, 344542774, 569479396, 46182177, 115731968, 615621055, + 927437470, 415304055, 613134814, 593402338, 555887984, 424883671, 737555229, + 580850096, 206291958, 408415618, 568495923, 383046806, 331153888, 630013482, + 639299672, 932192570, 426968629, 665396997, 377560684, 425576288, 499413833, + 304016029, 435936610, 890997684, 37462641, 308478314, 648743689, 968679592, + 6563304, 528867706, 815369648, 699526086, 854594514, 77461257, 912978080, + 437849059, 584065006, 448033159, 851418436, 275202511, 933127854, 57918210, + 936121297, 728315416, 493270959, 635640085, 751603865, 815510463, 322128401, + 817518864, 113319096, 719706704, 151675058, 579563270, 817496583, 331259914, + 304828788, 832758432, 514693374, 628452536, 859015740, 54698207, 502378937, + 925823748, 468799569, 912430738, 503849861, 155092332, 42145830, 16914543, + 954818713, 944191613, 753948564, 101131208, 628620169, 514926770, 674609079, + 477754503, 226746575, 399775590, 317837974, 685000591, 380154246, 862619940, + 156768091, 907663822, 640534228, 509892852, 140342102, 997418526, 552535460, + 430179820, 454222315, 646598713, 14409817, 280310343, 324342456, 625965172, + 814280682, 974660429, 122025967, 697839974, 290110820, 878936393, 874327021, + 252896959, 368474189, 596100537, 759459589, 74112139, 496075772, 660681195, + 476919942, 732314798, 881551587, 600771213, 916355905, 62502174, 722694262, + 846208181, 235701369, 377851030, 444635218, 905821472, 6007590, 14543647, + 806494416, 995077069, 714758118, 844053358, 554781732, 772216007, 970855797, + 411569324, 513182664, 137085083, 204095216, 632442670, 843815967, 289982568, + 631746100, 825138049, 349162102, 5326115, 207201935, 835205999, 161469959, + 312314799, 644367317, 122663749, 602404965, 763996480, 974548439, 732304994, + 808596406, 180421152, 975264158, 122323736, 966123769, 50110342, 744171007, + 561228509, 513803739, 631885254, 848740278, 38362310, 334317889, 407483295, + 704719903, 793514069, 3850010, 47429717, 332085264, 296925009, 179969654, + 285965854, 188329006, 353044452, 907132439, 173498729, 971005353, 886796300, + 374137584, 167007939, 257311427, 69578518, 986207933, 820577598, 762893591, + 855603877, 154159499, 81230719, 988672413, 560039653, 187206037, 124634358, + 756546188, 562543979, 382863463, 791317508, 638677331, 472652962, 704857946, + 755439531, 209470628, 413904724, 236402946, 980178476, 925780627, 624361832, + 986373739, 865597279, 733945791, 40519093, 346073968, 531933636, 452742332, + 388931131, 509971886, 203046081, 86591499, 107265603, 170834917, 222658017, + 931432329, 545903464, 244840320, 750824817, 665373361, 418717528, 723513553, + 572491457, 458224851, 688126547, 872604574, 477404041, 810687226, 902428227, + 809180291, 181691685, 593946617, 443990866, 886931195, 671951824, 117916938, + 88508493, 101987861, 998221579, 302729532, 637782794, 713816070, 909221425, + 478649015, 947515758, 588376423, 102208139, 387839447, 126033549, 266213171, + 286274226, 201637730, 911783168, 261943681, 622500013, 456472374, 708804967, + 489562877, 836248357, 622073857, 432626926, 157337505, 416320771, 436152162, + 592575425, 42633820, 518316330, 29165774, 254298082, 747662083, 309417930, + 635421959, 306846172 + }; + assertEquals(false, solution1.isPossible(target)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1356Test.java b/src/test/java/com/fishercoder/secondthousand/_1356Test.java new file mode 100644 index 0000000000..c7d6e7b468 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1356Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1356; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1356Test { + private _1356.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1356.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {0, 1, 2, 4, 8, 3, 5, 6, 7}, + solution1.sortByBits(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8})); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}, + solution1.sortByBits(new int[] {1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1})); + } + + @Test + public void test3() { + assertArrayEquals(new int[] {10000, 10000}, solution1.sortByBits(new int[] {10000, 10000})); + } + + @Test + public void test4() { + assertArrayEquals( + new int[] {2, 3, 5, 17, 7, 11, 13, 19}, + solution1.sortByBits(new int[] {2, 3, 5, 7, 11, 13, 17, 19})); + } + + @Test + public void test5() { + assertArrayEquals( + new int[] {10, 100, 10000, 1000}, + solution1.sortByBits(new int[] {10, 100, 1000, 10000})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1357Test.java b/src/test/java/com/fishercoder/secondthousand/_1357Test.java new file mode 100644 index 0000000000..d242401a7f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1357Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1357; +import org.junit.jupiter.api.Test; + +public class _1357Test { + private _1357.Solution1.Cashier cashier; + + @Test + public void test1() { + cashier = + new _1357.Solution1.Cashier( + 3, + 50, + new int[] {1, 2, 3, 4, 5, 6, 7}, + new int[] {100, 200, 300, 400, 300, 200, 100}); + assertEquals(500.0, cashier.getBill(new int[] {1, 2}, new int[] {1, 2}), 0); + assertEquals(4000.0, cashier.getBill(new int[] {3, 7}, new int[] {10, 10}), 0); + assertEquals( + 800.0, + cashier.getBill(new int[] {1, 2, 3, 4, 5, 6, 7}, new int[] {1, 1, 1, 1, 1, 1, 1}), + 0); + assertEquals(4000.0, cashier.getBill(new int[] {4}, new int[] {10}), 0); + assertEquals(4000.0, cashier.getBill(new int[] {7, 3}, new int[] {10, 10}), 0); + assertEquals( + 7350.0, + cashier.getBill( + new int[] {7, 5, 3, 1, 6, 4, 2}, new int[] {10, 10, 10, 9, 9, 9, 7}), + 0); + assertEquals(2500.0, cashier.getBill(new int[] {2, 3, 5}, new int[] {5, 3, 2}), 0); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1358Test.java b/src/test/java/com/fishercoder/secondthousand/_1358Test.java new file mode 100644 index 0000000000..22d27c1804 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1358Test.java @@ -0,0 +1,54 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1358; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1358Test { + private _1358.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1358.Solution1(); + } + + @Test + public void test1() { + assertEquals(10, solution1.numberOfSubstrings("abcabc")); + } + + @Test + public void test2() { + assertEquals(3, solution1.numberOfSubstrings("aaacb")); + } + + @Test + public void test3() { + assertEquals(1, solution1.numberOfSubstrings("abc")); + } + + @Test + public void test4() { + assertEquals(19, solution1.numberOfSubstrings("abbbbcabc")); + } + + @Test + public void test5() { + assertEquals(3, solution1.numberOfSubstrings("ababbbc")); + } + + @Test + public void test6() { + assertEquals(0, solution1.numberOfSubstrings("abab")); + } + + @Test + public void test7() { + assertEquals( + 1244858478, + solution1.numberOfSubstrings( + "cccbbbacababcccabcaaaababacaccbccccabcbcbcbabcabbcbbccaabababcabbaabcabbabcbaccbccaaababcbbbbcbbcaccacccabcabaabaaaccbcaccccbabccabacacbbbccbacbbbbbacbcabcabccbaccccccccaacabababababccbbcccbbcbcaabbcbbbbbcaacacaacccbbbbabcabbcacabbcbaabcaabcbcaaaabbcbbbacccbacaabacbbaacbcbacaaabcbaacbcbaccabccbacbacbabbbabbbcbcaaaaccccacbaaaaccbbccaaacbbabbbcabbaccbabbacbaccaaaababbbcbcaaccacbacbbaaabacbcacacacbccaaabbcccbbcaccbccbabcabcaacbaaacbbcababbbcbabbaacbcaaccbcacccabaacabcacaaccacacbcaaccbacccaacccbbbcbbcababbcbcacaccacccabababccbaaaabcaacaacbcbaababaccaaabcccababcbaaccbbabcacbbcbcccbbacacbbbacbbcbccabcbbabbbcbacbabcaacaacbaabcbbbcbcbcbaacaabcaccacccaacccccbcbccabbbacaccaababcbabcbacbccacbcccbaacccbbcabacaacabababcbcaacaacbabaccacccbbbbaabacbcaaaaaaaaaccbacaaabaaccbbacbaaacbcbbcaccbabbabbbabbbbccbaacbbabbbbaaabcabbbbbbbacbcacacaacaccbaccbcabbbbaabbacacbbcabcaaaaabbbcaacbabcbbcacbabbbaaaacbcbaabbcbbaacbacacbcbccbccacaccaacccbccbcbbcabacbbaabcbbcacbabaabacccbaacbbaaccbcababbcbcbbbcaacababaccbcccbbcbababccccaccbccccabaabcabbcacccbcbacaacccabbaacbbbbcbcbacacabaaaaabcbbbabbabcbcccabbaaaccbaaaabbaabbacbccabbbcaacbbbcabbcbcbbcbacbaababaabaaacbcaabcacbccaaacacabbccbcbcbbabcbbccacacbbcacaccaaacbabcababcbcbbcbcaccccabcbccbaacaacbbaaacbbccbbccbbcacbcabbacccaaabaacbabaacbaabaaabcabcbcaaaccaaccbbbccbcbacbacbababbcbcacaaaabcbaccccaccabcaabcabbbcabbbcaaccbcacaccbabbbcbacabbaccabbabcaccbbacabbbabbcaaaabbacaabaacbccacbaabbacbbabcbacabbacbbcbbcbbaabaabcabaaabcccbbcbbccbbccbccabbbabababcabcbbbaaaabbaacaccbaabcaacccbabbacacabcbbbababcbaababcbbaccaaaabbacccbacbcaacacaacacabcaacabbacacbcbacaabaaccaacbaaabbcaaabbbbbcbcbbcccbabcabbbabacaaaaaacacaacacacbaabccbacacabbaaababcaabbabbccabaaababbabbbbabcababbaacbcccbabbccbcaabccabbcbabaaaacbabcbbcbbabcbbaaacccacccbbbcbbccbcbaaaaacaaccabcbaacabcbabaaacbacccacabaacbababbcbbcbababcaaccabbcbbacaacaaccbacbbccbbaaccbbcaccaccbbaacccacaabbbcaabcbcbcaacbabacccbabcaababcaababbbcacbaabccaccabcacaaaaabaacaaaaababbbbbcaabaccbabacbaacbcababcbbaccaccbabcbbcbbbccbbcbbcabbbccaaccccaaccabaabcbcbcacbcbaabccaacabaaabacabacbccccccbbacabbaccacccaaaaabacaababcbababaaaccaabacbbbcacccccbccbaacbbccacacababbaacabbabcaaccccbababcbaacbccbbabbcbcaccbabaacbbcbbcaaccabccaacbbbaccaababbacabbcabcbccaabcbabaabaaababaaacbaacabaccaaacbaabaccbbcaaaabacacbabbaaabcbaaabbacbaabacababaabcccabbacaaaabccacbabbcbabbccaabacccabaccabcccaabcbbbcacbccabcacabaccbcabacbcbbabcbabcaaacbababaccbcccbacbcbbbcccbbcacbbaccbbbccaacaccbbbabacbbabcbaabcbacaabbbbcaacabbbbabcbabaaabacbbcacbaacbbccbabbcbccacaabcabaaaabbccabaabccabbcabccacccccbbacabbbbbbbcbccbbbcccaccccbcaaccbcacaacacbbbaaccbcabcbccccaabbbcccaacbbccacbcbcacabaccbaaaabaaccacbabccccacaccababcacbcbbcbbbaaabacbaccabcabbbbaababbabcaacbcbcbccccaabbccccbaccbabaabbacbacaabbbbbacbcbcbccccbaccbbacabcbabbaabccacaaaaabcbaaabcbcacccaccbabaccacbcaaaabaabcbaacabacbacaaabcaacaaccbcaabbcbcbaabcccacaabcccabccaacbcbbbaccacbacbabbccaaacbabacbcababbcabcbbbbacaaaacacccbaccbbabbbccbccbacabbbcaabaccaccbcbbacbacacaaaaacbcababaaabccbcaabaabccbbbcbccbbacaccbacbcaaabacabcccacbaccbbccccabcaaabcccbacabbbbcaacbbbacacaaacccaaccbbaccbcabacaccbcbabcabbacaacaaaabaacbaccbacccbaccaaabbbbcbaacbcacaaaaaacbccccabcbaaaccccacbbcacccabbccabbcaaccbbccccbbaaacabbcbbbabacaaccbbaabaabaacbbacacbbbbbccbaaaabcbaaccacabacbaaaaabcbabbaabbbcbbbacbbbcbbbcaaacacbcbbcaccaabbcaacaaacbcbbcaaaccccbabababbaabaacacababbabcccccbaccaccabcbccbccbbababbbacbbcaaaaccbaacaabccacaabcbbabcbbccaabbbacacccabbabbaacaccbbabbbabccccbcbbcbbcbbccacccbcacbcacccbccabcccbcacbacaccbacbacaaacabacbbacabababacaabcbaccbbabcaaacbbbabacbaabbccabbbcbbbbccacaccccaabcbcbbcccabbababcbaaaacccaccabbabaababccbaabbbcaacbaacabaababcbacbbabbcbacccaaaaaaaaaacbcaccbacacbbccaabbcbacbbccbabaaabacccbaccbbbbbbcaaababaabbbcbbababaabaaacbbbacaabcbbccbbacccabbbcbcbacbccacbcbbaccacbccbaaaccbcbaaacbaabaccbbaaabaaccabbcaccccacbaacaacabbcccbccbbaaacaacbcaacbbcaccbbabcaaaacccaabacbababcbcbcccbcccaabcaccbacbabcbaaccaacaaaaacababacaccccccbaabcbbacaccbbaabaacaccbccbacaaabcccbcbaabbcabbbabbbcbbbabccbbacccabbccabbccabcacbcaacaccbccccaaacbcbabbabbaacbcbcbcbcbbcbaccbcbcbacbbcaaaaaabbaccbcbabbabbabaacacbcbabcccbbaaccbaaaabcabbccbabbabaababaaababcabcacaccccbaaabcacbababacacaacaaacaabaabbbbbcacabbacbcaacbbccacccbabbcbaaacaabaacaabaacbbaabcaaacbccabbbcbacccabcbacacacbcabbbccacabacbaaababccaababaabacacaaabacacbabcacacacaccacbbcbcbcbcbbbaabaaaabbbacbbaaccabcaabaabcbbbbbabbbababbcbbaacbacbbccbbbbcccabccacbaccaccbbccaaccabbaaaccabccbbabccacaaabbbcacacaabbaabaabcbbaaacacccbccbbbcaabcacbcaabaaccccccabcaabcacbccaacbcaaaccbccbcacbcabaacaaaccabbbcbccbcabaabbcacbcbacccaabaccaabbbabcccbacbbcbcbccbaaccacbcbbacabbababbbbccacbaaabacaacbbacbccbbcbccbaaaaacabbbbbcbacbabbacabbaaaccaaaacccbcbaabbbbcbcaaabcccbbabcacbaccacabbbcaabbacaaabccabacccaccbcbabcbbaabbaccabcaccaaccababcccbbacacaaaacccccbaaaacccbbacbabcbbcabaaccacacaaaccbcbcacabbacccccabcbacbacbcbcbbcaaaccccabccbcacaaaaabacacaacbbbcaaacaabbbabbaaabbabbbcbabaaaaccbbbcccbaacaabacaaacbccbcbabbbcacabaacaabababcbaabcaaaccccbcbccbbaabbacccacccbcaaacccaccaabbbcbaaacccccabcabbbbbbaaacbbccacabacabaabaacbbabaacbcccbabcacaccbacabababbcaaccbacbabababbabbbcabaccbaacbbcacbcacbabbcbbaaabbbcbacbcbcbbbaacbbbbaccabaaaabaaacbbbaccaaabbccabbcbcccabcacbabbbabbbbcbcbabacbcacbcbacaacbaabacbbccacbbaacbaccccaccbacababbbcababbbacaaabacbbabcabbcabbaacaccbbbbcabcccaccacbcaaabaccbaaaacacabcabaabaccaaacbbaabcaccacbbabababcccbccccaaccabbcabaaaacbaacaacabbcbbaacccbabcbcbbbacbbcacbabbbbaccbbbbbaaaabcaabbaabbbabbcbaaccbbabaaabababbabcbcbcabcaccabaacbabcbbcbaaaacbacccbabbbcbbbccabbccaccbabcbbcbbacbcaccbcaaccabbabcbabcabacabbbaaabbacacaaaacbaababbbaaacbcbabacacbcbbacbccaaacbcacaacaccacaabbcbcccbccacbabbcaabcbbcbabababbabbabbcabbbbabbcbaacaacbbbcaaacbbcabcbaabccccabcbaabbabbbbcabcabccbcabcccbbbcbbaacccbbaabaaababccaacbabaaaabacaabbcccabaabaabbcacccabbcbcccbbbaababbabcaabbaaaaccbcbacabacabbbaccbbabbabcaaacbcacabababbbcbccbaaabbcccbbaacaacbababbabbcababcabbcaacbaacaabacbaaacbcbbaccabbbccbbccbbaaacababbabbacbbaccccabaaacbcacabbabaacbbacaacaaacaaaaabbacbaaacbbbacbacbbbbcabbcbbcacbbaaccaaacacbcabacababcbcacabbccccacbbababaccbabcacbcbccacaaacccabaabaabbcabbbacacbabccbcaaaababaabaaaccbbbababcabaababbcababbcacaaacaaaccbcbabcbabcbacbccacabcbcaacbcacbcbcaabcbabaacbacbbcbcbabbbacbaaacccbacabcbcbcbbbabbcbcbbacabcabaaacbbacbbbaabcaaabcbaabcabacacaabaaaaabcccabbacabaccbbbbabbbababaabccabbbacaabcbccacaaaababbcccababaaabaccbaccbaabccbaabcaaaabaccacbabaaaaaccbcbbaabcbaccccbbaaccabbacacaccbabbbbaacabacababcaaaabbacbcbbcbcbababaccbbbaccbbbabcbababcacbbbbcbaabbcbbbccbaccabccababccaccbbacbbaabcaaaacaaccbabbbacaaacabbcacacbcabaacabbbacbaaababaaccccacacacbcbaaabcbbbbcbabcbccbbbaacabcabbcabaacbacbaabbcccaacccbabcaaccaaaaaacabacbacbcbbcccbaccaccacaaabcccbacabcbabaaaababccbaccbbcaaabcccababacccbabcaabcbcabbbbcbccbaccaaccacbaabbbabbcbbaccaaacaaccbbaacbabcbbaaaccabaaabbbaabcacabaaabacbabcaaabbacbbacbaaccaaacabbcaaacbbbacbcbbcbabbabaacbcababbbbbcbbabbacacabbbccbbccbbbcabcbcbccacbaabbcacccbbbbbccbaaccabbcbacbaaaaabababbcccbaabbcababbbbaacbccbbacabbaaacbbccaaaabaabcbacbaccbcccbabaaaccaaccbcccaacabbcbbcbbccaaaabcccbccbaaccabacaaabaaabacacbbaaacbbbcbabbcaabccbcaabbacaaacccbbcaacacbbaccbbabbcbbbaacbccccbabcbbacaccbbcaaaaabcabccccaaccacbcbababcbbaaaabbbcaabbabbcabbaaaabcbbacccbbaabbbacaacacaaaacccbaabacaaccbccacbbcaaacbcaaaaabaaccbcaccabbaaacbbbbabaaaabbcaacabbbccbaacbbbcbaccaabbcabbcbcccacbbbacbbabaaaabacbcbcababcbaaaabcaacbacbbccbaacacccbaacabbcccabbaabaaaabcccbaabbcbcbcacbcaabcccbabaabccccabccbbaaabccaabbacbcccbaacabccacccabbbbcacaaccbccbcccacbaccacaaaababbcabccabcaacacbcacbcccababbababbbccacabbcaccabccacccbababbbcccccacbbacaacaaaacabcacccabbbcabccacaccaabbcaabacbabccccacabcaacabbabccacacbaabaccabaacccbbcabaccaaaaaacbbbbbacacccbaabbaaabaccbaaccaaccaacbaacabbbcaaaaaabccbaccbaaacccaaabcbbbabccbababcbbbcbabcabcccababacabcbabacaacaccbabaaacccacbbcaaaaaccabbcaaccbbbccbabababccccaccbccbcbacaabbabbcbaabbbcabbccaaaabbacababbaaacccaacbcabacacabacabbccabbccccbcbbaabbaacbabbaaaaabaccaaabbcabacabacaccccbcababbacabaabbcbbaccabbbcbccaababbbababacbbcaaccabbabbabbcbaabaacccaaacbabbcbcbcccaababaabbcaaaacbcccaacbccbcccbbbbbcbbcaababccccaaacacaaabbccacabbaabccbcaabbaaccbacabcaccaaababbaabccaaabaababbababcaacbbacccabccbcccccabaacaaccacbcbaaacbbacbbaccabbbbacccaccbaccccbbacacbcccbcaacaccbaccccbcaaaababacbbbaccabccbabaaccaabbbbcaaabcbbbbcbbabbbbaaccabcaacccabacabccaccbbabaacbcaabcbabbbbaabbbbbacbabbbaacaababaabababaaaababcacbccbcbbcacaccaccbababcaacacbcbababacabcbbcbcabbcabaacccaaabbbcabacabbccbccabcccbbaabccaccaacaccaacabbccccbababacbaacacaacaacbbcbcccacccababccabcccaabacbabcbbbcabaaccaaabcbccbcaabaacaccaccacccbaaccbcaabcccabacbbbacbabacaccacccbbbaccacbccbcbbbaabcacccacaabcaaabbbcacacbbbcbccaccbcbabbbccbaacbbcccbbabcbabababcbcccabaabbaabacccbacaaccbbccaacbbbcbaabaaabbcbaccbaccbabaaacbbbcaabacacccabccbbcccabcbbbaaacbccbaaaacbbcbccbacacccccbcbbcccbbaaaccbabaaccabbaabcabccbbbaccaabbbcbbbaaaabccccacbbabbccbccbaccbacbcbccbbcbcabbbcbcabacbabcacaaabccbcbcacbbaacabacaaccaabacbcbaabbaccbcccbbacacbcccabccbaaacacbbabccbcacacccacbaabaaababbabaacacbbcbaccaacabaaaaabcbcccbbabaabcbabbbccabccccbbcbccaaacaabcccacaaacbacabbccbcbaaacbcababaaaaccbcbccacccabbcabbabbccaaababaaaabbabbabcbbaaaccabbcbbccbacbbcaacbabaaaacbaaabaaabaaaaaacabbbbbbabcbacbbbbbbcbccaabccccacaabcaabbabbabacbcccbcbcabacbacabbcbbcbbacbbcabaccabaabababaacbbcbcabbcacacaabbcbcabbcacaaaaacbacacaacbbbbbaaabbbbabcccbaaccccbcccbbccaaccaacaaccccacacacbcabcbccbaaccccabbbbabbacbbbaccccbcccbcabbbbababcaacbbacccbbcbbbcccababacbaacacbaacbbbacacaababccccabcbcbccacbbbaaaccbbcaaacbcaaccbbcbccabbcbcaabacaaacacccaccaaabacbacbabacbbcabaacbccbccaccbcccaaaabbaacbaccbcbbaccccacacaabcccbaacbccaabbcbbbbabacacacbbabaaaabcccccbcbabaaabbbabcabccbbcaababaccacacccbbcabcabbccaaacbabcacbaaaababababcbbbaccccacbacaccbbcccbbbbccaabbccccbabbbccbccbbbbbaaacaaabbcabaabbbcacaaabcbbabaaacbbccabbcbabbccaabacbcaaaccabcbbbaaabacbcaccabbabacccabbcbccbabaccccbbbbabbcbacabcbbbcabbbcccbbbbcccccacaacacabacaaabbcacabaacbbbcaabcbbaccbaaabcaccbaccaacbbbaababaccbcaacacabcacabacabaacbcaacacabacbcbaccacccbaacccaabababbbaabbbaacccbccacbaccbaacabcbbbcaaaabcacbbcbcccbaaacbcabbaaaaabccbcbcccabaabacaccbbbacaaccbcbababcbbbacccbbccbabcccacaabccaaabbcbbbaccbbabcbbabcaabcbaacbbcabcabbbcbaababaacbabbccbcabbabcbbbabcccbbabacbabbaabcaccbccacbabccbaaabbbacccababbaaabbbbabbccabababbcabbabcccccccccbcbccaaacbccabbabbbaccaacacbbcbbbbcbabccbbacbabbababcabbcccbbabaaaaacccacacbaaaaccccbcbabcabbbcbaccbbbbcbbbaaabcbaccbabbaacaaacbcbabaacbcaacbccbcbbacbbcbabcbcaaaaaabbbabcbaacabbaacbcbaaaaacaabbaabbccbcaccacbccabbcaaacbababbcbccbcacbbbcaabaccbcbcbbaaccabbabcabccaccbbaaaaacabbbababccbaccacbcaaaaacccaaacacabacabcbcacaabbcbcacbbcaaccacacacbbbbbbcccbbbccbbabbcccabbccacacbacabababbbaabcbcbbabbaabaababbcaccabbcabacbcacaacbbbbaabbccbaaacccacbbbcbbbababaabaaababcbcbaacccbbccbcbcbaaaacbbbbcaaaabaaabbbccabcbaacabcaacbbcaabacabababbabbbabbbbcccabaccbabbccbbbabcabaabbcacbbcacacbacacababbbccababcbccbabaaaaabccbabbabccacaaabcacacaabbababababaccbbaabcacbbabacbacbbbabaacaabacbbbaaabcaacbcaccbbbabccababcbcbcbcabaaaccbcabcacbbbababaaaababbbbccaacbabaababcbcabccaccbacbabaaaaabbccbcaaaaaabccbbabcaabacbabacccabbcbcbccbacbaabbaacaaacbacbbaaccaabaaabacacccaabccabbbaacaaccacccacacccaccbccabcaaabacacbbccbabbacabbbaaabbabcaacbabbabcbbcaccacccbbccaccbacbcacccbabcbcbcbbabcccccacbabbacacbbabcacabbaababbcccaababaccacaacabacaaaccababacabbcabcabacaccbbcacaaabbaacbbcaabcccbcbabccbabbaccbcabbacabccbcbbcbacabcabbbbcababacaababccbbbccbbaababbcabbcaaaabbbacaabaabccaaccbbcbaabbbccabbccabcbacbcaccabababbbccbbccbbcccbacccccaacbbabccababcbcbcbcaacbbbbaaccacccabbbaabaccbacacbbbcbbabaaaaccabcaabbaccbccbaacabaacaabbaaaacabaaccabacbccabbcccbcccbcaaccaaccbcaabcbabbbaacabbaabcccbaccacbbacbbbcacabbcbaaabcaaaccbbcbaaabbabcaccacbbbbccaccabcaacabcaacbaccbcabbccaaabcbccbaaabbcbaacbccbacbbcbaccbaaabaaaababccbbcbacabcbabcbbacbccaabaaccbabcbcbcaaababbbcbccababbacaccaabcbbaacbabaacabaaaaaabcaabcbbcacbcacabcbbbabacbbbbaabacbcaaaaacaacacbcbcbbcbcbccacbaccacbbcbcbcabcbaaabbcbabacabacbabccbbbcbbcccbaaabcccacabbbcaacaacbaabbcbcbacbbcbbbaaaacbbabcaaacbccbccaacbacaaccaababbacbcaabcbaabcbbcabbabcbccaaaacbabbbcaabccacaaccbbbccbaccbaaccabbcbbbccbcaabbbacbbcacaccacbcbbbbabcccabbccabbcbacaccbaaacbabbcbacccaacaabacbbabcaaacaacbbabcacacaabbbaabaaccccbbcaccbbbacacbbabcaacbbbbbababbaabacbaabbcaabccaaabbacbacacccccbaccbbbccbcaaaabccbbbababbbbbaabccaccaaaccaabbbbacaacbbcaaccccabcaaabcabcaacbcbbcccabaababbbbbabcbcbccababcbccacbcbcacbaabcacccbacaccacabbbababbbabcaabbbcccacccabbcaacababcbbcbbaaccacbbaacbaabcabcababbacbcabaaacccacbbaacbabbcccabcbaaccbbcbcabacbcbbbbcccaccacaacacaacaabbcccaaccbcacbabbacabaabccccaaabccabcabacaabbaaaabcbbbbabbcbbbbabbcbcccbabbccabbaaacbbcabcabbabccccbbacaacbbccbccccababbcacabbacaccbbbcbcaaabcbcbccbbbccaacaaabacacaabacbacbbcaaccaaaaccccbcbbcbaacbcbaccccaccbbcaaccccaabbcbbbabbaaacbcbbaacaccababaaacbbaaccaccbcaacabaccacbbaccaaaacbcbabacabacbbaaababcbbbaacbaccbccbacabacbaaababcbcaccbabbaccbbbcabbabbabcbcaaccaccbbbbacbbaaabcaabacccbbbacbaacabacabbaaabcbcaaaaccccabacaacccacbbbcbababaaabbbaccbcaabbccaabaccbbaababaabbcbbabcbcabbbacabaccbbabcbbbbcacabbacbacacccacbaacabaaaacacbcbaaacbcbbabccbaaabaaabaccccaaabaabcbccaaabcabcacabacccbabccbcacacbbbaaacabaacabbbcaacabcaacbabccacbccbacbcbbcaaaabcbbbbbbcbcbaabbcbcabbbababbabaaacbaaccbacccbbccabacaacbbbbacbbaaccacbcccaacbbbbccaabaccaccbbcaaacabaabcabaacccbabbaacaabaacccabcccccbabaacaaccbcbbabaccbbbcaabacaccbcbacccbccbaacacccacaccabcaababbaaaaccbcabcbbacbccaaaabcacbbacacbaababbccccbabababbaaabbcbabbcaabbacaaaabaaaaaaaaaccbaacbbbaacbbbabccaccacacacbacabbbbabbaacbbbbacbcbbbcacccbabcabbbacabcbbbcbcacbabbacbccbccabcbcbacbbbbcbccacacccaabcbcaabbababababbacabbbbbcaabcacabbbcababaabcbaabaaaacbcbbbcbcbcbacbbcbabccabbcacaaabababacbcaacacabcbccccaacccacbaaaacaccabcabbaccbcbccbabccccaaaaacacabbabcacabcbabccbaccaacbcbcaaaabaacacabaacbaccccabbaccabaabccccaccbccbabcbcccabbbccabaaccbcaccbbabcbabcccbcccaacacaccacbbbacbbbcaacbaabbbcaccccbbccacccacaabbbcabbbacbcacbccbaccbbccabcbbbabcacabbbaacbbbcccaaabacaaabcbcccbccaaabcbbabaabcbacbbbabbccbcacbccaacbcbabbbbababbcacccbabbaaccbbcbcbcacccaabbaabcaabbcacbcbabacbcaabaaabcbaabbbbabacbbcabbaaccbcccbcabcbabccbbcaaccbacabcaacaccbcacbbaccacbbcbbccbaccbcbaccbaabcccbaabacbcaaabcacbbaaaabacaccbbbcbbacabcababbcbbabbccacbabcbcbabcbbaccbacbbbabcbbcaaacbaabcbcbccaccbacacbbabaccccccbccaccbaacabacbccbcaabbaacbcacaabacbacbbaccbabbbbaccccaabbacccababbbbaabccaaccbabbbaabbaacbababcbcbbabcbacbabccccbbcbcabcbbabacacbbccbaabbccacaabbcbccbcbbcccbbababbccccabaacbabaabcaacbbbcacbbabaaabcbcbbbbacaccacacacacacbacabacbcbabcccaaababcaabcbabbcbbacbaacccaaabccacccaaabaaabbccccaccbacbcccaabbacbbccbbbcbaaacabbcbbcacaacbcabcabcbacbababbccabbbbbcbcaaccbcabababbaaacbabcbcaccababbacabaaaaccabaacbbcaaababccaccbaacacacbcabcbabaabcbcbbccbaabaabacaaaaccbbbbabbaabccabbcacacacbcacbccccaacaccbabbbcbbacaccaccbacacbacacbbabaaabaccbcccaacbaaababcbcbbabacacbbcaacbaabbcabbbbccacabaccaacacacccacacacbbcaaaacbccccbcbaaaabccccababcbbcbbbcaaaccbaaccaaacbababccbbbbababcbcabbabbcbbbbbccabaabaabcacaaaababcabbbabbacbaaaaccacbcbacbabcccbaaabbbcbcbcbbbccbabaccabbacacbbcaaacbbaabcacabbbcabcbbccacaacaacbbbbcbbcbbcacccbaacabbaabcbaaabbbaabbcbbaccacbccaacacbbbbbaabbaacabbcacbabbbcabccbabbabcbbbbacabbcaaccacabcbbcaaaccabcbacbaacbacbbacabcaaacaabbbaaccbccabaccbcbcbbbccccbbabcaabbbcabccacacacbbcaccaabbbaaacbaababcaaacbabbbaaaccaaabbaacacabbabcbccbbcaacbbccbccccabccabbabcaccbcbbabaaacaccabcbcbcbbabbaabcaabccbabaabccbabcbcacbcabccbcabaaaaccacbbacbcabaaacacbcababbaacbcaaacccacaaabbbccaaccabbbbcabcbccbcaabbccbacccabcbabbcccacbaabccaabbbbabbcabababaacaaacbaaaacaaabcaccbcabbbcccaabcaaaaabacaaababbacccababbcabbcaacbcaacbabbaccaabbababbabcbaaaaccaaaaaaaccccbbbababbabbbaabbcaabaacbacababcbbcbbbbbbcaaaccbaacbccbabbaabbacabbcbacccaccccacacabcbaabcccabacbcacacabaabcbcbcabacbcaaacbaabccbcaabbaccabcbcbcccbcaaaaaacccbbbccccaaaaccccaccacaabaabbababbcbbaaabacacacbcacccaacbcaaccbaabaccabcacbcbcababbaaacaaacccbbabccabbbbbbccababcaaccabbcbccbaacaabaccbaaacabcbacbccacaaabcbaccacccabacbaabccbccaabacaabbcbccbacbbcabababcccbaccbbacbbcccbbbabccaabcababbcaccaccabbacbacccccabaccaabcbaabbcbababcacbbaacaabcabacccaabcbccabaccccccabbaabcccababbaaacbaacbcbcaabccaccccacaaacaabbccccaacbbccababbbbcbbbaaabaccabcaccacbbbabacbacbbbbccacacbabcbccabcabacbbccbacbccaacbacbcaababccaaababbbbcccbaabbbabbbabaacaabcbacbbccccbcaccbcbccbcccaccbbbbaababacbaccccaaaaccbbaabaaabccbaaaacaaaaccaabaaabaccccacabbabcbbacbacabacabcbbbabccbacbcbacbbbcabcbcccacbababaccbabccbacaabbbccbbcaaaaccbaacbbbaaacbbbccbacccbabccaababababbbbbbcaacccccabbcccccabcbbbabbabbcaacaaabbabbcaacbbcaccabcacbaabcaabbcacccbbaacbbcccccbcccccbaaacbcccaabaaaaaaabbcccbcbacbbaaccaacbabacbccbabaacabbcaabbcbccccacccacbcccbaccbbbcbbccbbcbbabcaacaaabaccacabcbbbacbcabcbccacaaabaabbaccaccbccccabaabcabccbaaaacacacabababcbcbaaabbccaccaaacbbccaaccbbccaaacbbbbaabcbbbccabaacabccbabbcabcababbbccabaaababbbcbbbcbbcbbbacbbbbaacbcacccacccbbcbcbccabbcaabacaaaccabaaacbacbaaabbccccacaabcbbbccababacbaaaccbcacbabaccaccccabcccabccabacacccbccaaabcbacbaacbaabaabbacbbacbbbcccabbccbcccaacababbacaccbabbacbcbabbccaacbabbccbabcaccbbbaaaaacbacbcbaaaccaaacaccbaccaccaaaaabbacaaaabcbbaaabacaccaabcaaaccbaabaccacabccbccccbbbaccaacbaccacaabccbcabacaacccbcbbcacacaaabcbcaaccccccbbcacbacaabccaccabbaaaaacbcccaaacbacaacabbaacbaccababbacbcccbbbbbcabbbacaaccccbaabacbaccacacccbababbbbbbccccbbbbbacabbacbaabbaaacbbcbcaccbabcabcbaaacacbacbababababacbaababacccccacbbcbcacbccbaabcccaccbcabccacbabcbacbbccccbacaccbbbacaaabcbaaaccbbbaaacabababacccbabcaabcbcaaabaabaaacbacbcaabacaaacbcbacccabcabaccaabcbaaabbbcacabbaabacaabbbcaaccbaacbbbcbbbbcccbcaabbacabacaabcabaaabcbbabbaaabbbabcabaacbabbbcbacccabbbabcccaccbbbbbbaacacaabcabcbcbaaabacbaabbacbacabccccaacbcbcabccccaccacabbcacabbccbacacacabccbcaaaabbacccbaacbccbbaccbbbaccaabbacbcaccbababbbccaaccbccccbaabbbacaaababaabacabcbcbaaccbcbcaccccbbcbaabcaaccabaccccbcabbccccbabcbcacbcccacbbacbcabbbbaacbabcbbccbccaaabaccbabbbcbbacacaaabbaacabcbcbccbbacbccaaabacbccaababaccbbcbabccacccabcaccbbcbabbacbbcabbaaaabcacccbcacababccacbbbcbbaacbcbabacbbaaabaababcbaaaabaccbbcccccabbbacaaccbaabbaaaaaababbcaccbbbaacbbacacbaccaaccaccaccaabaabcbabaccccabbacaacacccbacccccacbcbaaabcaaaaccbcacaaccbaaabbcbcacabcacccbbbabbbcaccaaabacbcccbbbacababbacacccbaabcccaccacababcbaccaccabbcaccacccaacacbcccbbabaacbcccacbbaacbbaabbbbcabbcccbbcaaabacabbababccacaccccaabbcbbbbabcaaaabaccccbccabcbabcacacaababccabbbbbaaccaabbcacbacbaabcabbbbccbcbbcbccccbcbabbbbbbabbbacabccbaacbbbabccababccaaabaccbcacbbabacabcaabbcbcacbbccabaaccaabbbccaaabccccbabaacacbbaccabbabababbbacbbbcbaccccbcabacbcbcbaacbcaaababacbcbabcbccabcaccccaaacbaabaccaababaaaabacbccccaccbaaacbcaaabbcbacbbcabcbbbabccccaaaaabbcbacbcbabbabbbbccccaacacccccaaccaabcbbbcaaaccabcaabbaaaccacbbcccbaaccbcbabbbcaccbbaaaabaabbcbcaaacbabbbcacacbacbabacacabbacabcabbabaabcaaaccaabccaccaabbcaccbccbbcacbbaacaababcbcaacbbccabbcccaabcbbccbacabcbcbbbcbbaacaaabbcbbcbbbccbbabccacbbbccbbccbacacaabbbbbaaccbbbaccccbaccccaaaabbabaacbcaaaccbbcbbacbccbabaaacabbabcabbccbbbaaacaccaaccaabbbcbcccaccbcbcccaabbabababcacbaaacabccbacbbcaaabbbbbbbbbccbcccbcbabaaaabbcaacbcbcbbccaabbcbacbabcaaabcbbbcbcbbbccbaacabbcbcbababbacbbbbaaaacbbacbbbbbaacbaccbbbbacbbacaabbcbacccabaccaacbbababbabaaacbaabaccbabcaabbcbaababbacbacacaaacbbbaacbaacccbcabccabacbaaacbccbbcaabcbbcaabaabaaaccacbaacbabccbbbcabbabcbcababcaaabcaabcccabacbbcaacabbbccccacabcaabbaabcbcbacaabcccbbbbabbcbcccabbabaaccaacaccccbacccaacabbbbaabcbcacacbbbbaaaacbcacabaabccaaacbabccbbaaccbbaaaabacccabcabbaabcabccbccbbabacbbcbcccccacbaabacacaccaaaabbccbabccabacaabbbccbcbaabbaaccababccbbbbccaabaaaacccaacbbacacbcaccbbccbcaabbcaccbcacacbcbbacccabcbcaabcabcbbaaccabaaccaaacccccbccbbbcabcbbcbcbababacacabbabaacbcaaaaacbccaccaaabcaaaaaccabccbbbcbbacaacbbbabcbbaaacacbbbcccacbcacabbbcbcabbacacbaaacbbbbcbcaccaaaccbcbcaccaaaccbbcaaaccbaabacbbcaacabbbccaabaacbaabcbcabbabbacbaabcacacbccaccbcabbbcbbcabacbbbcabcabbcbaabbacbcbbcbcbacaacbcabccaacbbcbaabcacbccbbbacabacccbcbbaaaababaaabacaaccabccbcacbabcbaacbcaaacacccbbababcbbbccbcabbcacbabaabbaacabbbbacacbababacbbbaabacbbbaabbaaabccaaaabbaabbaabaabbccabbabaccaccbcaaaacabbcbaabbcbacaacabccbabcbbaacccaaccccabaaaccccbbbaaabbaaabaccccacbbabaacaaacaacbbbbcabbbbabaabaaccbabbbbaaabcaaabaabbcbbaacbcacbcabbcacaaacbaacbbbaaabbbccabbbabbcbacbccbabbbbbbbababaabaaccabcbccccaababbccabcabbccbabaccabcbcacbaabccacaccbbbacbaabacbbcabbcaabbcbbaaacbcbacaacbacabaababaaaaccaaaccccccbcaabcbccaccbaabccccaabbccbbbaaacbbbbabbccbbcacaaacaccaaacababaacabbabcccccbabcbaccaacbccaabbaacaaccbbaaccccbbaaaaacbabbcaacaabbbbcbabacbcacbcabbcacbcabaaabaaabbaacccbcacacaabcbabbaabcacaacabbcacccbacbcbcaaabaccbacacabcbabbcaccabcabcacbabbbbbcbaaacaccaabcccabcbaabcbababbcbacaaccaabcacbbccbaababbcbccaabbbbcacaaabccaaacbbaaccccbababaacccabaababbabccbbcbccccbcbbacbaabaaccabcccacaccabcccaacbcaaccbccccccacbaabbabbabcbaababccbaccccccbbbcaaaccaacacabcabaccabbbacbaccccbccaacabacccbbbcababcbcbaccabacbbbbaaccbbacbabbaaacccbaaabbcbcabcbcaaabcbbccbbbbabcccaaccababccacbcbccaccabbabacbcbbbcacccbaacbcacbbcbcacbcabcbcbabbccacbaacbcbbcbcbabcaacaaabbbaacbbcbcacbaabcbcaaacbccbbaacbbabcabbbbaccbacaccbabbccbbbccabbbcbcccacbcaccbacbacabaacbcacacabbbcbbbabaaccaacacaccbbcbacaacabaaccaacbcccaabacccbcbaaacacacbbccbbccaabacababbcccacccbbcaaacabbbbcbbcbaaacbbacacaaccacabcbaabcbccaabaacacbcacbcbbbacccabbcbbabcbbabaabcaabbbcbaccaabbbcabccacbbcaabcacbcacbbabaaacbcabbcbaaccaaacacccbacbbcccbbbabcbcbacacccbcbaabcaacaaccbaccbbbcccbacabacbbbbbcaaacaabbaabacbbccabbbcaacccccbacabcaababcbccaabbcbaaaacbaccbcccaabaccbaaabbbcacabacaabccbbcaaccacbbcabcabccbbbbcbabacaaaacaacbbbcaacbcbababacbaabaaaabccbbbccbbccaaaaacabaaacbccbabbaaaacaabbccccccbaabcbccbacbcabbcbaacaabbcbccbaaabacccaaccaabbaaaacacabcaaaccabaabccbabbbaabbcbaaaabcbaabaaaacabaccbabcacabbcbcbabccccbcbcaabccacacbcaaacbaccccbcaccbcbcccccccccabccccabbcbbcbbbacacaaabacbcbaacbbacaabccabccabaccbaaabccabbbcaaacbbcaabacbcccbbbcbbcacacaabacbbbacabbabbccaccababcbacaabcacbbcbcbccaabcaccbabcacbbacbaacabcbbabaccccbbccccbccabbbcccabaabbababcabcaccbccabbbababacaccaaaaabbaaccacbabbbcaaacaccaaaccabbabaabccaaabaccccabcabbabcbbbccbacaccbaabbcbaacbcbbcbabcbbccaacaabbcbaaabbbbbaaabaaabcbaccbbbacbcbbcbaaccacbbbbcbcaaabaccbbbabbaabcaccaabacbcbacbabaaacbcbabccabbcacacbbcabccccbbaacaabaccbcbabcbbabaccbbbacbbcbbbbbbbbabccacacabbbccacabcbcbbbcbccacbabccccacacbbccabbbbbbbbacaabbbbcccacacbbaccaaabbaccbacabacaccacbcbbcccacaccaacbaaacabcaccabacbabcacabccaabaaabaabbbaacabaaccbccbbcbcbacccbaccbbbccacbcbccbbacabcabaccaccbaccacaaaaccaacaabaccaaacabaacbcbbaaacccbcaacbaccacbcabcbabccbcaaaaaccbacaaacbbbcaabccbcaabbcbbbacbacbcaacbbcaababacbcbccbcccaacbaabbccbaaccbbbcacbcbabcaacccbabaabbabcbbbacabbbababaabaaaaacbccbaccbabbbcaabcaabcacbaabaababbcacbccbabbbabcabbabcaacbaabaaaccabacacaacaacacabacbabacbbcabaaccbccabbacccccbabaabaacabacbbbcaccabcacabccccccacacbabaccaaaaabcababaabacaabcbcacbbccaabababbcbcccaccccacbbcababbacbabacbbabbbcacbcccbbaabcbaaaccbbbbcbacbabcabaabbccaaaabaccbaaaccbbbaabbbcababbcbacbbabbabbbabbcbabbcbbbcaacbabaabaacbacababcccbacabbacaaccacaabcbbbbaccbabbabacacbcbcaacccaabcbccbabacbbccccabaacabaabcbcbcbcbaaccbaacacbcccaacabaacbabbaabaccbbcccbacbcabccbccccaabaabbbcabbaabbcbcabacacacababcacacbcacbbbbbcabacbcabacaabaaabbacbacbabacacabccabacbabcccbbcbccabbaccacccacbccaaaaaabaabcbbbcacbbbbbcbaaaccccaaabaacbacabbcabaacbbaacacaaccbbaaabcabbacbbbacbbabcaaababccacbbacbccbccbabcccabacacbacbbcaacbaacaabacbcbaacbbcaaabbaccbcbccbcacccabcabbcaaccbaababbccbbaabcabcbbaabcbbcabbaabccbbabccaaaccabacaabcaacbacccaacbaacabccbaacaaccbbbcabbbacbcaaaacbacccaccbaacaaaabccbcbccbbababcbabcaacabaaaaaaccaabcacabbbabbcbcabacbcbcabaabcbccabbcbcaccaccbacaacbbbaabcbccabacaabaaabbbcbbaacabbbccaabccbcbaccacabbabbcbacabcbacbabacbbaaacabaabaaacacacaccabbbccacaccbbbbccaacbccaaaabababacbabaaaaccaaaccbccabcbabcbacaccbccaabbbcacacacabbaacacbcaaacacbbaacbaacacccbabacabbcbbbaabaacaaccbaabcbcbbccabcccbcbbcbcbaaccbaabbccbbacbccbacbcbbbacabaaababbcbccbbaacbbbacabbcbabaccbbaccccbaacabccaccbacccabbbccbcaaccabbbbbbcbabccacbcbacaaaabcabaccbabaabcaaccacbacccbcaccaccaacaaaccbbacaabbccaacbaccccacbaccaababacaacccaabcbcccabcacccbbbbaabaacaabcbabcaccaccaaabacababaacbcccbaaabcacacacacbcaabaccabcabbbcbcbcbaacaaccbcbbccccbbacaccbbbabbacacacaaabbacbbabcbcaaabccacabcbabbcbcbbcaabacbbbbcaababcaabccaaccbbaaccbabccbcbaabbbccacabbaaaccccaacbabbbabcacbabaaccbbccaabbbacccbcacabbbcbcbcbabcccbaababbbbcaabbabbbcbbaaabacbcbcbabcbccbbaabaababbabbbcaccaacacbacbaaccaacacbabbaacaabbaacacccbaabbbbbbaaababacccabccabbacabbbbaabaabcbacbaaacbbaccbaaaabccbacaaabaacbbbaaacbaacbcbcccaacaacbabbccccabbacbaccabccabbbbabaaaacccacbcabacaacbababbcabcbcbccbcaccabbbcbacbabccaccaaabacbacaaacacccbacabaaacabbccaccbbaabbbbaccaccabcaaacbbaabacbacbcabcccacbaccbabacbcacbbcaabcbbbcccaaccccbcaabababbbabaabbccbbbabaaacbbacaaccacbaabcbbabacbabccabccaaccacacabaabcccaabcccccccaabbcacbacabbccbbbbaabaabcaabaaacacbbaabcaacccccacbccbaacbabaabcccabcbabaccccbbbacabcbcbcbaccabbbcbbcabbbbbbcacbcabaabcaabcabaaaaaccccabcacbbccbbaabaccbcabaabababaccacbacbbccabbcbbbaaaaababbbcacacccccaacbbcbabbaaaabcbcbcaacccbccbccacbbbaacabbcbbbcbcabaacabcbabaaaabaacacbbabcabcabccbbacbcbcabccccbcabbcbccabbcbbbaabcccabbccabcbcaaacbbacaccbccbbbaaccccacbcbbacabaaaaaacacbcacabcabcabacccabcabbbcbabbaccbbaacaacbbcbbbbbbbcacaabaccbccacaaabcabaabbaacbbbacbcbcaaabbbbaccbcbacacbbcaacbccccaabaacacacabcbabbcbacababbbbabcaccccccbbcabbabbaabbccbbccaaaabcbabbaacbaabcaaaaabbcccbabcbbabbccaccbccbcaabacbcbcaabcbbbcabacacccaacccbacaabababbccbcbbcaaccbbaacbcaabbbcabbcccbccbbaccaacabababcaaacbbacaaabcabaabcacacacacbaaaabccbcabbccacbccaaccbabacbabababbbabcccbbbbbaccabbbbcacacbaaaababcabcaaaacbabaabbbaabcbcccbcabcaabccbbbccbabbcbbbbcbbcabcababcaacabbaabbccaabcbacccababaacbaacabcbbbabcccbcccbabbabcaacacacaaaabbbbbbabbcbbcacccaaacbcacbbaaccccbbbcabbbabccbcbabcabbacacabbaccabcacacbbacccbcbbccabbaacabbaaacaaabbccaccbacacbbcaacbccabababaabaccbbbaaacccaaccccccabcacabcbabbbbbbbacbbbaccbbcabacbcacabaaacaabaacbbbcabaabbbaabcaccacbababcbcabcaaacbaacaaaccacbbbabaccccabbababbcaaaacaabcbbbcbabacaaabbcaccaabcbbbcaccbaccbabccaaaacbaaaaccacbabbbcbbcaccccabcaaaacbabacccbbaaabbcbbaacbaaaccbccbbabbabbabaaacaaaccaaccacaabcaacaaabcaacaccccabcaaaacbcacaabbabaaababccaababbabccbbccbbbcccaaaaacbccbcbaaccbbcbcbbaacbcabcbacaabcbbaaacaaccaccccbabaaacabcbacaacacabababccabbcbabccccbbccccbccbaabbacbcacbbcacbababbaacbcbcbcbbabbbcbccacaabbbcbabaccabcbacbccbbbaaabacbaababbccacaccaabcbacbaaaccccbcbbcababbacaaacaacabacbcbabccbbcbabaccbccaccaacbacaaacacaacaccbaccbcbbccabbaaabcccccabccaccbbbabbbbbbacacbabcabcababaaaaaccabacaabacbaaabaaaccccabaccbbcccbaaaccbabaacbcbcaccbaabbaccaacaabcbaccaacabcbcaacccccbaabbbccabcabaccacabcababccbaabcacabcbbaacccacbccbbaabbbccaccababbbacbcaccbbaccbcacaabbbcbcbcacbaabbcbaacaabbabbcccbcabbccaabbbcababccccaabcbcbaaaccaacbbacbbcbccbaccaaaccccccccbcbbcbaacbcbaaabbccaccaaaabcbacabcbcaabcbbbcbaaabcabcaaacbaaccaccaccbcacacccccbcabbacbcbcccccabcbcbcabbaaabcacaccbbbcababbaccabcccbcaabbacbbaabaabbabcccbccccccacacccbaaaccabcccaaaaaabbbcbaabcccccaccbccbbbcccaabbacbbababbcbcacabbcabbabacacccabcbbabbcbabbcbccbccaacbbcbbbbabcbcaacbbcacabacbbccbccbcabcabbccbaaacabcccacbccbabcbbcbaccabbcabcabccbbcbcaabccaacbaacbababcbccabcbcaccaccbbcbbcacbaaaccbabbcbabcbaccbaacbcacbbaccbcbaabbcaacbcacabbcbbbcababccbabcbbacaaccbbacbbaabbbcbccbbacbccbcaaaaccbcacbabbabbacacaacaaabcbbabbbcbabcaaaaacacabbcbccaaacabacbaaababacbcacbabacbaaccaaacbbcacbbaccaabbacbcccbccabcacbcbacbabbaaabcbabccbaaaacaaacbbcaabbcaaaababcbaccaabbcaccbacccccaaababaaabcaacabbcbbcbcaabccbbbbcbabccabccbccaccaacccbaaaabbcbbacacccbaabbabbcbbbacaaaabcbbbbacabcaabcbcaaabaacccacbcbccbbcacaacbccbcccbbcaaaccbcbbaabababbaacbaaaccabaacbacbcaacacbbacaacabacabcabccabbcbbaccbcbcbccbbaaabcaabccaacbbcabcccacbccacbbaccabccbaaaaccbbcbacabbcccbbbaaacbbaccbacbbbcbbabbbbccacccacbaabaacbbccaababbacbccbcccccbbbbcbacbabcaababcaabbbabaaaabcacacbbbcababccbcaabacaaacaaaaacbaacabcacaaccabacaabacacccccaccabcbaaaccccabbaaacbabcabbabcaccbaaacaacbbabbcabbbabcaccccbccacabaaccbccacacaacccaabacbaccccabcacaaacaabacabbaaaacaabbacccbbaaaababaaaacbabcbcacbbcbbcbaabaabcbbacacccbbccccbbcabccabaccccccaaabcabacbbabbccbacaaacabccabccacbaabbcbbccbcbaaaabaaacbbbbcccbbbcaccaabcbbbccbacbcbacabbabaabbcababcccabbbcccacbcaaabbabbabbbbaaabbcacccabccaaacbcabbcccabacbbccbabcbbabbcaacbaabaccbbbaabccbacabcccabccaccbcbbccbcbacacaaccacabcaaacbccacbcabcaccbcccbaaababacbbaababaacbaacabcaccbbaccaabaacbccaacaabaabbcabcacabbbababbacaaaabcbbbcccaccbbbaacbabbbbacabbacacbbbbaccabacacbbababaccabcbccbbcacaacacacbbcacaabcaccccacabaabacabbbacbaaacbcacbbccbaacccbcbcacbcaccccabbacbccbabacbaaabbaccabaabaabacaaaabbbabaaabcbcacbaabcbababcabacaaccacacaccaacabaacaacbcbbaccbaacbabacaaccbaacbbaabaaccaccbaabbabbbbbcbccbcbacccacaacabacbbacabbbbbcccbccaaabacccbaacbaccabbccaaaabcbaaacccbbacbaaababbbbbbcabbabacacaaabbaccbcccbaaabbccccbacacbaccbbbcbbcaaaaabaaacaaabacbbaaaaccbaabcaccbabbcbbabcbcbcbccaaaabcaaabccabbaaccacbccaacabbcaaabcacaaabaabacaabcbbbbcaccbacaabaaacacbaaccbbacccaaababbbccbcccbccbabbabaabcbccbccbbacbbbabcbbccbbbccbcbcbcacbcbbcccbbcbaccaccbbabbaaabacbcacacbbacbbcccbbbcbbaacabacbcbcabbacbccbaaabcbbacbbbbbabcbaccabbabcbbcccacccaacbabacbbbcbbacbbabaaaaacacacaaccacaacbbbbaabbacbbaccbbbcccbcabcaaacbbaabacbccaababacccacaaabaccbcabacbaaacaaacabaccbbbabbbbcccabcaaacbcabbaaabbcccaaabbbaacaacbbacbbaaccacbcabcccabcbbbccbbcacbccbbbcabaaaacbbcaccaaaaacaccabcbbabacbcbcaabaaabbccabbbcaaacaaacbbabacccabaacbbbbaccbaabccacbaccbbaabbaaacbbacacacccbacaaaabacabaabaabcbbcbabcaaabacbcbabcccacacbccbccbcbbbbbabccbaaacabbcaacbcabcaabaacabbbaabbacaabbabaacbabccccacccccaaccacabcccacaabaababcbabaccbabaaabcaaaaacaccbcbcaaccbabaacaaabbcccbcababbabbccbcacabacbbcccbcacbbcbaabcababacacbcbaacbbbabbcbacbbaccbcbaabbbbbacabaabcbacaabbbccbbbaabbcbcccaacbcccabacbaaabbccbcbbabaccbbababcbaaacabbaccabbcaabbacaacacaaababbccbabacbabaabbacbbacacaabaabcccacbccbcabaacbccabbbabbbcbbaacbabbbaabbbaccacbacaabccccbacaaacabbaaababcccbabcbbaacababccacbbcbbaabacabccaabbccaaacacacbcaaaccacacacbccbabbbbcbbcbaabababaaccacbaabbccccbbbabbccbbaaacaabacacccbcaacbcbabcbbccbabaacbacabbacabbcbacbaabccbcabbababaaabcacabbabcaaaabbbabcccccaaaccbbacaabacaaabbcbcbabbbcacbacacaabcbcaaacbaabaaabcacbbbbaabbabaacacacbbcaababaabcbccccbaabcbcaaaaacabbbcabbbcaabbcbabcbbccacabcaaabbaccacaccbcaaacbccbcbbbbbccacccacbbcbbacacaabbcbbcccabbbbbaaabbbccbaaccbcaaaabccccacccabbabcaaabcabcccbabbcccbbbacaccbccabcabbcccaacababcbcccccacbccaabcbcacbbaacbcaababbcbabaaacbabacababaabaabaccbcbbcaacabbababccabbbaabcbcbacbacbccccaaccabacbaccbbbccacacaabaaaaacabbaccaccbbaaabaacabacaacbcbbbcaaaabccbcbcccbbcaaaacacbccbacbcbacbbaaabbbaaaacbbbbabbcccaabbaaccbaaaacbacbaacabacacbbcabbaaabaaccbbccbcaacaaabcabacccbbbcaabbcaaccacbbcbccbbcacabcacacbaaacbcbbbaaababbbbabccacabaaabcabaccabbcabcaacbbbabcbcababbaaccacaccbccbccbaacacacacccccababccbcbacbacabbccaacbbaabcbcaabbcacccbbacaaaccabccacaaaacabbacababacacacababbabaabbabcbcabcccabacbcbbabcaccacaacacabbcccbacaaccbbbabaccacbcaaacacacbabccabaaacbacaaacbaabcacbcbcaaaacbbabaacbacaabbaaaabbcbacaaabbbbcccacabacbaccabbabbcaaaacbbccbcbaabbacacabbccbcbaccaaacbabbacccaacaaaacbcacaaabcaacbccbabcaabacaaababccbcacccbbbaaccccbcaaacbbabcabaaaccbcabbcbbaaacacabcbbcccbaabbbccaccbacacbabbbbabccbbbacbaacbaaccbbccccbbbabacbacbabcabcbcabcabacaccbccbbabbbbbbbabaacaacccacbcbaabaabcbcaaababbaaaccabbcacbaaabbbaccbbacbbbacaaaabaabccbbbccabaacbccabccaabbccccbbaaaabbcabacbbccbccaccabcaacbcbcabaabcaabccabbcbbababcacbaacacaaabbccbbbbacccaaaabbacbcbcbaacbcbbcbbcbcacbcbccaccbcaaacccbcccacaccabacbabccaccaaaaabcaaaacccacbcaaabbcabbccaaaabbababacabcccacaaccbacabcbbcacbcacbabbcaccaaacbbacbababaacabbbcbacbaaccabccaacbbbccccabbababbaaabbccbccacbabacbaccabcbabcabacabacbcbbcbbbaaaccaaaacbbacbbaccbbcbabcaacaaababaabcabbbbacacabbacccbcbbabcacbcbabcacbaaccbabacbcbbbcbbaabbacccccccaacaacaababbbaaabacacbaabbcacbccbbaaaccaacacbaaacabcbbabccbbbacabaacbcbaaaabaccacbacbabcaccacbbacbccbcbabcacbccccabaccaabbbbbccbaaacbacbaabaabccbcabbcaacbaabaacbcbbccbcaccccbaacbbbabccbbacacabcbbccaabacabbcbacbaabcccbbabbbaacbcccabcbaabcbaacaabbcbcbbcaabcabcbccbbbbbcacbbbcbaaabbccbbccbaaccbacabbccbccbbaabbabcbbbbabccacbcbbaaabccaabbcacbcacaaaabbbbbbaacbaaabbacacbccccbccacacbcbaaccabbbacaccacbacbabababcbbbcababbaabaacbcbacababcbabacabcabcbcbabacbbbbbbabacacbabaabcccbbaccaacbacbaabcbcaacaabcbabccbcabcbcbcbcbbccabbaaacabcaacbcacccbabbcababccbcbbbbbacbacccbaccccbacccacbccaaaabaaccaabababbbcaabcbcbccbbacbccccccbaaaacaaccacbcbabacabcabaccbaccbcabcabccccaaababcabccabcbaccabcbbbaaccbaaaaaaccacccbbbbccbbbbaababacbacabcacbabcaabbabacababcacbbcbabbaaccabbbaccabbcaacbbacbccacccbcaccbacacccbbccaaacaabaaaccbabcbcccaabacabcabbbcaccbbbbcbccbbccacbbbcaaaccaaaabbbbbabababcbbacaccabaabcbabbaabbcaabaacbcbbabcbbcccbbbacbcabccbbbbcbcababacaaccbcbabcabcbcbbcbccbacbcccbbcaaabacabbabccacababccbbbcacbaababbccbcaccbaccbbbcbbabbbcccbbcabcbbccabbcbcccabcaabcbabcacccaccbabcbbbaccbabcacababcacbcbbbcbabcacbcccabcbbcbabbabcbbbaaaabcbbbaabbbcbaaabccaccabcaacacccaccbaabaccbababbbbcbbccbaacacbcabccbcbacaccaccaccbaccbbaababaacbcbacbcabbbaccaacacbcbabcbabbaabbacbacccaabaaaacbabccbabbcacacbccbcccaabcccbabbabccacbaabbccbaabacbbcbacacaccaacabbbababccbbacccbbaccabcabacaacbbbaccabaaababbbcaaabccaacabcccaabaccbccbbcabacaaacbaabaabababcabcabaaacaccaabacbaccccbccaacabaabaaabaccacccaacbaacaacabcbaabbabaababcccccbcbacaacacbbbaaccaacbcacccccacaabbcbcacbbbabbabbbcbbbbbbcabbbabbccbbcccabcbbbccbaacbacaacbcbaabbbabaabcbbbcccbcbcbaacbabbccbabccaabaccaaabcabaacacccbcbbaccbbaaabbabaaabbaccbbababcbabbbbcbbaccabbbbbbaabaccccacccbabbbaabacccacababcababacbbbbbabcbbbbaacbbcbaaacbabbcabacaaabaaabbbbaabaaaacabccbacaaacaccacaccbaaccbcbbccbabcaccaaacccbacabbabaccabacbcaaaccacbabcbbcaabccaaaababcccabccbaaabaccbbcacccabbcbbaccbcbacabccabacbaaaaaaccbacacabacabcbbcbaccbcaacbaabccabaaabbbbbabbaabbcbbaccccbbaabbaccbbcabbbaacccaaacabcbaabbaaaacccbabababbcbbccbcacaccbcaacbccaccbbaacbabbcbababbaccacbabababbacbccabbccbaacccbaacabcbcbabbbbbacbabaaaaaaabcaccaabacbcabaabbbacabbaccacbacbbbacbaccccbaacbcbaabaabacccacacaacccccacccacbbbccabaccbbbbbcaacbcbbabcacaabcacacacbaababacaaacabbabccacbabacbbaabcaabaaaacbabababcbaababbbcaccaaabaacabbbaacbbacacbcbcaabbcbaccbbacabcacaacbbcaccbcacbcababacaabacbbcbbbcaccbabbcbaacbbcaabcaccbbabaaccccaabaacbcbcbbbbbaacbcbcbaabcaaaaacabccccbcacabbcbcabbabbcaabbbccbbbccabacabaccccbcbabbbbabaccacaabababacababbbabcacbbbaccbabacacaaacacbbabaacbabcbbbccaaccccaabaaaacbbabbaabbaccbbcbbbacccacbccbbbabcacabaaabcabbbcbaabccabbbabcbaccccbabacbcaaaaacbcabbcaabaccaaaacaaabccaccccaabaaabcabaabcacaabccabcabccbaacbccabbaaababacbcbaccaabbbaacbcacabcaaabccbcabacabbccaaaacbcbbbbbcacababcbbbbbcacbbabccccbcbabbbcaacbcbbbcabbbcabcccabcaccbabbbbbccbacabbbaacacbaccbaabcaabcccbcbbababccabbabaacccccbaacbcbabaaabbcbaacabababcccbcababcccbcccabbabccccaccaacaacabacbabbcbbcbcaaaabaccaaccabbbaababbcccaababcbacaccacbccbcbcbcbbaccacacbbcbbcbaabaaacccabcbcacbccbcbbcabaacaaaaccbabbbaaacbcacacccabcbbcaabaacccacbabacaaacbabaaabbbbbcacbacabbaccacbbccaaacaabccbaaacabcbccbbcaaccbcccaacaccbcacaacabcacacbbaaaccbbabbcbbacbacabbacabaccccccbbbcbbaacbcacabbbcbaaccaccbcabbcccabbbbbbcbbbacbcabaaabcbaaccaacbccaaaacbcbbccccabbaaabcaaaabcbbabbaaaabbcbbbaaabbcabcccccccbcacbbcaccccacbbbbcccacabcbbaccbcccccbcacbaacccccbbaabaccbaaabcbbababaaacacaababbaaaacbacbccaabbcbcabababacabcbccbaabcbccbaabbacccbcbbbabacccbcbcacacbcccbbaacbcacbabbbbccaabbcaaacbaabcabbabbcccccbccaacccaccbbaccbcbaaccbaabcccbcbaccacbaababaccbbccaccbcabaabcabbbacbabaaacbcaaaccaaaccacbbaaaccabcbccaabcacbcbcccacbccaabcbaaaaccbabbabacbbbcbacccbaaaacaaacbcbccbaacaabbaccbacbbcbcbaaacbaacbaacaaacccbcacabaccacbbaccbaaabacbbbaabcccaaaacbabccaababaacccccabccaabbababbabaabbccbabacbbbcaccabcaabbbbcbccabaacacbbbccbaacbacbbcabbccbccbcbaaabacaaaabbacbbcccccaabccbaaabbaabccaabaccabbaccabaabbabacbabaacbccbabbaabbacacccacbabbcbbcbcbcbcabccaacbabacbccabaababbcccccbacacccacbacbaacabcacaaacacbcbaccbbccaaccbccbbbcbbcbaacbbbabaabcacaababbccacbbcabcbbcacaccbaaaaabbaaaaabbcacaccbbccbcacacbaaaacbcbabacacbababcabcacaaaabacbcaaacccbbcbbbcbaacacbacaabbcabacabbbbaccaccccbcaabacacabcacbabcbaacabacacbbcacabbbbccaaaccacabcaabcbbbaacacabbaacbabcaacbabcbabacaabcacbacacccacaaabcacccbccbcbbabaaabaaaabbcbbbccabccaabacaccbcbaacbaacbacabccbbaccbccbacbbacbbcbaabcacbcccacabcbbacabbbcacbbbbaabbbccbcbbcbabacccabaabccbcacacaaaccccaabbbcbcbbbacabcacbcbcbbbcbbbbccbacbababacabcabcabbcabcccbbbcaabccaccbcbbacbcbaccaabacacaccbacabbacbaaaabcccabbaabcbccacbcbbaacaaaacbcbbcbbcccbcbcbbcbccccccbacaccaabccaabbccabaaacbbbabbbbacacabaaacbbaccbcaaaaaccbaccbbabcbabbcaaccaccbcccbbaccacbccacbbaabcccccbbcbaaccbbabcbcabccacbbcbccccccccccaabcacbcaabbacbabccccbccbbbbabbbcbccaccaabcabacbbcbaabaaaaaccabaacbccccabccabbbaccccbbcccabacaaccacbccbaaaacaacbccaacacbbaccccacbbccaacabccacbacababbacaccabcbbbaabacaaaaccbcbacbaccbcbbacabaccbcbbbbccacccabcaabccbcbbabbcbabbbcabbccccabaccbacabcbbbbcaaacaabaacabbabccccbaabccbcacacabbbcbccaaccbcbbacacaccbbccbccaacaaacabcabbbaaccbaabaabcacbabccbbaaccbbacbcaacabcabcbbacaaabacbaacbabcacacaaccaabbbbcbcababbbbabbababcbabbbbcacbbbbccaacaacbaaaacbcabbbaabaabcabacccabbcccbcbcbcacbcccabbccccabaaabababccbcbabacaacaccbabcbaaacabbaabacabbbacbbcbbccbabcaaacccbacccabcbbccccbcaacabababacbbcbacbbbaaabacbbbbbcabaabcaacabacbcbcbccaaacbabcbbabbbbccbabacbaaccaaacbaccbbaaaacababcabcbaccbbcabaabcbbbabacccccababcacaacacabbbbcacbacbaabbabcaabaaaaabcbacaaacbcbbbccbcccaacbcabccaccbbbccaaaccacbacabcbcbcbaabccbaccabcbabbbccaacccaaaababccccbaaabacccbcbacbaccbbbabbbacabaacbcaacbaaacbaacabbcbacabcbabbabbcbaaacabcccabcbcababcbaacabccaabacababbabbcbbaabacabbcbbbaccbbbcbcaccacabcccbacaaaaaaacaccbaababaabcaccabbaacbabcbbccbbabaabcbcbaacaccabaccaacbacabbababcbbbcacaacacbcbabcbbbabbbccacbaaabcabaccabbabcaccbcccbabacbbbcccbbbcbcaacbbacaacacacbccccbccaaccbbbccacbccbccaababcaaaaabbccbbccbaababccababbacabbaaaaacbbabbcaacaacbbbcccbbbcabcabbcbbabcbcbcacabaaaaacaaccbcccacbbaccaabacbbbbabacbcaabbbaaaaccaacacccbbcaacbbacacabbaaabacbaccaabbbaacccaacbabbbbaaabccbaaabbaabccaabcababaabacbaaccbbccccccbbacacbaabbcaabcbbbacaaccbaaababcbcbbbabbbbbacaabbaacbcaabbaccbacbbcacbcccaaabaaaabccbbcacaabbabbcabbaccbbabbcaccccbcbcaccacccbcbbcaacbcaacbbbcaacccbbacababaacbaabacaababbcbcbaabccabcbcabbcbbcbaaaaabaaaccbaaabacccaabcabbaacccbcbcbbbaccbcccccbbcbcacaabbcaccaaabcccacbbbbabaccbacccabcacacbaccacbbbbbaaaacacbacbbcbabaaaaccbcbccbbabcaacbaacaaacaabbacacbcaccbccaaccaacacbabacbbbabbcaacbcbbbaaaacccacbaacaaacbcaccaaccbcacbbabacccbccacaaacbbcbcbaabaabbacbbcacbbaabcabcaccbaccbcbbbccccbbbbcaaaabbcbcababcbbacbabcacbcabcaaacbbbcaaabbcaccacbcaacbacccabcbcbcbcaccbcacaabcacacaabaabccbbcabbcccccbcbaccabbbcacbcbbaaabaabbaacbbababaabaaccccacabcbbcababbccabaaaabcccbcaccccaaabbacacbccbaaaccccccbaaacabcccaaaaaabccccccacacabcaabaccaacbcbaabccbacacabaaaabbcbabcbaaacaaabacaacabbaacbbbabababbbcabcbacbabacaccbbbbaaabccbcccbccbbcacabcbacbbcbbaabacbaaaacaaabbacbacbccaacabcbbbaaabaaccbcacccaaacccacbaabcbcacacbacabaacbccbbabcbccaccacaccbcbbaccbacbcccaabcbcabaacbababbcabbacabcaabaabcaaabaccccbcacacccbaaaacccacaabcabbacbbcaaabcaccbabacccaccbaaaaabbbcbaccbaaaabcacacaaaaabbbcbbcacbacacabcabcbbabbacaaacbbcbacbababaababcbbccbacbbbcaccbabaccbcaabbaacbabaccbcababbbccaabaabbbbacaaaccbcbbaabbaabcabacbcbbbbbacacaaacaabcaabacaaaccbaabcacacccccabbbcaaaccaaccbabbacccbbbbcbaacccabcbbbcccabcacaccbbcccacbabcbbaacacaccaabcacbbbaaaabccbbcbacacacbbbbaacacaacbbaaabcbbacacbbbbcaaaaaabbcaacbcbbaabccacbbcbaaaabacacccbcaabaccbacbcabaabacabcacbaabcccccabbcaabbcaacababacbbbcacbcbbcbaccacabaaacabcbbaabbabcacbabcbbacbacabacbcbbcbacbcaaaccabccccbcbcbcccaaccabcabcaccabbbbbaaaacbbcccabccaabaccaccccabccbcaaccacbabbbbcccabbacbbbaababbcbccbccaabbaaabababcaccbcbbcccacacacacccbbaacbababccababbaacbcacbcccccacabaaccaccabcabcaccbaabccaaccabbbabbacccbaaacbbabcbacbacbbacaabccccaacaccbcabcbbcbcbacaaccaacaccacbbcbccccccaacbabbbcccabaabacaaacbbacbaababbbccbcccccbccacbcabbacaacabcabbcccabaccababbaabaacaaacbccbbbccbcaaaaacbaccbaccbcccabaccabbbbacccbbcacbaaaabcaabbbcaaabccabcbabbaaaacbabcbcbcabacbccaabbbcaaacbcacaaabcabbcabccbccaacbccbcbcacaaabaccbcbbbcbcccabbbcbabcaabacacabacccbabbaccbbbabcbababcbbbcbaacaabbaccbaaacacacbaaaaabbcacaacaccaacaabacbabaaabbacbcbacccccbbcbcabaccbcababbcccbcaabbacaaaabcaababcbbcaccbbabcaccbbabcabababbacbcbbcbcacacaaaaacbacbabaababccbbaccccabaaaabcabacbbbaaaabbbccabcabcbccbcaccacbcccbbcbcabccbcbcacacaacbbbccbcbbccacbcbabbbccbcaccbccbaccbcbccbbbacccacbacaacbcababcbaaacccabcbcacbacabaccaaccbabcbbcacbacaccbbababbccbaaccccbcaabbabbccbccaaababacaccccbcbcaababaaabbbbbbbaaccccbaacabacbacaabcbacccbbacacbcbacbcbacaabbbabcbcaccaacabaccabaacaaacccccbcaaabacaccacacccbbabbccccbacbccacaabbcbaaacccbccbabbbbbabbaaaaacbcbbccccccbbbccccbbbcaaabacbbabaabcbcbcbaabbbbcbcbabcabcaaccbabbbabbcbccbacbaaccaacbcccbbcbabbbbbcbacbccabbabacbaaccabbbcbcbbbcccbccbcbcbbbbccacaabbbaaabbaabaabbaabacbbbcabbbbbcacbbccabaaaabbcbacbccccacabcbaccabababaacbbccaacbbcccaccbaaaccccbcbacbbbcbabbcbccbccccbacaaccabbbacccabcbcaccbcbcccbaabcaabbaaacaaabbccbccabbcbaaaabacbcaccabbcbacabaaccacbbaacababaabcacccabbcccababacbbcabcacaababbcbabaacbbbbbbacccbbcbabaccbcbcabcbabaacbbccbcbabaaaabcccbcacbaccbbbbccabaacacabbbcacabaaacabccacabbacaababcbabbbbaabbaccbaaaacaaacacababbbbacbbbbacbcacbabaaaacbacbcbccabccbaccacbabcbbccccbaaccaccbcabbcacaaaacbbbbcabcbccbbcbabaacbbbcabcbabcbbccbcbaabacbbacaabacbbcbbbaccaacbcabccbbaaabbaaaccabbbbabcccbcaaaaabcaaabcbcbcacbbaccabccbbcbabcbbbbaacbbcccbaaabcbccabacaacccacccbacbbaccbacabbcacabbcaabccaacbabaaaaccbabaaaacababbccbaabaabccccbbbcacabaaccacbbccbaccabbacccaaabcabcccbcacbbabbcacacaaaccbaacbccacbabaabaccabbabbabaaacbacccabbcaccababbcbccbbaaaacccbbbcabcacabacbccccacbcbbcbbaaaccbacaacabcbaaaccacabcabcabbbbccacaccabbbccbaccabbacbbacccccbcbbaaababccabaaaacccaabbccacbcbcccbacaaabcaccbaaaaacbaccabcbbcbbcacbbaabbccacbbccccacbcbbabaaacbccaaababcacbbacbccbabacbbacbccaccccbcaabacbcbccaaacbbbbbbaabcbacbaaccbbbaabbccaaabbccbbbbabcacabaacccaacbbbaaccabccbcbcbabbbacabcbabacaabbaacabaaaccbabbabbcabaacaccbabbbbbbbcbcbaaabccacacbacaabbbcababbccbccbcbacaaacbaaaccaccbbbbbaacaaabccaccccbababccbaaaacabbacacaaabbbcbacaabccababaacbbbbbbbabacacabcabcbbcbbcbcabcaacbbcacaaaaabcbacabcbabbaaccbacabbaaaaccbbaabbaacbbcbbcbccbcbabaaaaccabbccabcccaaccbcccbcaccccacbbcbcbaaabbbabbcbccbaccaacbabcaccbaccacccbbcbbaaabaacaaababcabbbcabaabbbabbcbaccaacbaabbccabcbccbbbccccaaaaaabaaccabbcacbcbabcacaabcbcacabbcbacbbbaaaaccbcabbcccbabcbaaaacbccaababccccacaaabbbcbabaaababaababacabaabaacbcaaabcbbcbcabbcbbbababacbcbabaaccaccaabccaaaabbcaaabbaaaccaacacaacaabbbaccbbcbcabbbbbcacacaabbbbcaccabcbabccacabbaabbcabacbacacbcacabacababbaaaaabbcabbabcaccaaacaacbbbcababbbbcbbabbcabababacaaacaaabaccaccabacccabbbababaabbcaaabcbcbbcbbbbaccabccbabacababccaaaabbbbbaabbcaaacacccbbaaabcaaabcacbbcbbccbbccbbcbbaababbcabccbacbbcbcacbcbabcbbacacbbacccbaccccbcbbcaacbcbaacbcaabccbccaaccabbcbaaccbbccaacbcccabcabcbbacccccaaaaaabbabbbbabcacabcbcabbbcabbbaaabccabbaacbbacbabcaacccacabccaabaccaabbabcbccbcaaabcaccaccababcbcacbacbaccbcaacababaaaaacccaabcbccbbacaccbcbabbaaaccbbbbbcbcaccaaaacaccbcaaccabcbacccbbcbabbbbcbcabcaabcccabcaccabacacbbbabaaccabbbbabbccbbbacacaabbcacacccbbcaaacbcccabbacabccaabbabbbabaaabccaabaacacbbcbbbacaabacabbbccccacbcbbacaacaacbbaaacbbbbbaaaaaaababbabccbaccbbacbbcabaacaabcaaaacabaccbbbaacabbbbcbacbbbaabbbacabbbbccbacbaaacbcacccacbbaacbbbbabbcbbccbcbccabbbbcbbabbaaabacaccacacaabaaaaabaccbbaccbaabbbacacbcbcbbbcbcbccbccaaacaaccbacaababbccaaabaabaabbaacbacabbcacbacaabaababbbbcabbabbcaaaccbacaaccbabcbabaacccbbacacccaaabcacaaccbaccbbabbbacaaababaababacccabbbcbaccabacaccbaccbbbbabbcabccacbbcacabaaacbccacabccacbbcbaababababaabcbbbcaaacababbcaaaccbcbacbababcacaabaacacabbaacaabaaccaabbbbcaacaccaaaccabbabbbaacaccbbaacacccbbacaacaaaccabacbcbcacbacbcabcabacbacccbabbbcbccabaaaabaccababbbbbbcacbcbabcabcacacbaacaccaacabaabaabbabbabbccaccaccaaababccaccacbabbaabbcaaaaabccaabbbbaccbcabbacaababccbcacaacbabccacbcabaccacbaaabaccabbabaabccaaaccbbabbccbacabcacabbcaaaabcbccabaaccbbbbbbcbbcbbccaaccbaabcaaccbbccaccbcbcaccaacbcbccabbccaabcbbabacabaabbcbbacacbbbcacabcacbcbbabbabcaacccaacbbaabccbbaabbaacccccbabacccabbcabbccaccaacbcaaabbabbcabbbbbabaaaccaccaaacabcccaccbcabcbabcbccbabcacccbacbaacbcabcbaaabacabcbcababbcbbcccbbbbaccaccabbaabbcbabcbcbaabccbcbabcaabcacbcabcaccaccaccbbbacaccacabccbbcbabcbacbaacacbbbccccabbcbcbabcabaaabbacbacbaabccacacaccccbaaaabccabaaaaacbaabcababccbbbcbbcaaccccacaaaacbacbcbcbabccbbcbbcbabbbaccbabcaabbacccccbaaccabbccbcbbbccbacbccaaacbaababacbbbbcacbcaabacacacbaacbcbbcbcbcaabbbcbbbacbbccbcabaaaaaabbcaabcbcbaccabbbbcabbacbabbbabbcacbbbbbcaccbcbaccbabbcbabcabbccacbbbabcababcbcbacbbacacbccababaabbbaacbacaaccaccbccbabbcbbcaacabcbcbaccccbcbcbcbacaabacbbabcbbacbbcaacbacbcabcaaacaaaccbacaabcabcccaacbaacaabbaabacacbacbababccbabbbcbabcbbabbaabccccbcbbbbbaccbccaaabcbbbbbacabccabbacabaccbbababaabbacaabcbacaacacccabbbabaabcacaccacabbaaacccaacbbcbaaaaacaccabacbcabcabcaabacbaacacbbbccabbacbcbcbabbbcbcccbbcbacacacbcccbcbcbbacacaabbbccacbbacabccbabaccaabcabbcccacacbcbbcacbaabaabaabbcacccaacabccaacabbaaabbccaccabccbaaacacbaaccbacacabbbcbbcaaabcaacccbaccbcbbaabacabcaaabbcbbcbacbccabbabcbbcbccbcbabcaababacbabbaaaaaabbbababbbcbccabcabbabacbbcbbabccaabccacbcaaacbbcababbbccabbabbaaaacbcaababcccbaaccabcbcbbabacbbabccaacccbbaaabcabbcabcacabccabcccaccccccbaacaabcbbcabaabbaccaacaabbbacbcbbbabbacabbcccccacaabcabaabaabbbabcbbabbaaccccacabababccbbbaacacbcacbabccbbabaccbacbcacaabbacaccccbaacabaaacccacbacaaccacbaabccbcaaabaabbcbbaabbcbccbcbccbcbaccabaacaacabacacaccbbabbacbbbcccacacbcabbaacbbbbbcbacababbcacccabacaaaacbcbbcbaabbcbcccbcbccbcbaccbbaabbcacacabaaaaaccabcacbaabaaaacacabcccccabbbcacbabbbbcaabaabbabcabccbcccbabbabacabaaacaacccbbaacbbaaabacaccbcaacbbcaaabcbcbaacabccabcbababbbacacaaaacabbbccbcbaabababbabbbbaabacbccaabbccccccbcbbccaccacbbbaabbcacbccabacbccccaccaabbacabbccccbabaaccccbbabcccbcacaabbacabbbbbbacbabccccccaacbbcbaacbaccbacaccabbabbbcabacabacbbcbabbaabccaaaccccbabbbcbcccaaabacccbccbccacbbbbabbbccabaccaabbabcacababccacabacabbbbbbbbcbbccbcbabbacbbbcbcbbbaacabaacbbcacaaabccbcccbcbcacbbbccacbabccbccacabbaccbaaabbcccbbabcccccbbccacbccccbaaaaababcabacaabacbabccbcbabcaacbababaacbacacbaabcbacabaccababbbbbaccbcaccbacaacaacbbaacccbacabcaacabbaaaaacccabbcaacabcccbcacbbcbabacacacaccaccbcaabcbcbbbbcbccaaacbccbaacaaaaccacbaaaaacaccccccaacbccbcccbccaaabbbbaacbbbaaabaacbccabbbcaccacbbcabcbcaccacccbaacababbaaaabacbccacbcababbbbaacaacaccaccbbaabcbbaaccccbbbcbababacacacbbacbaaaabacacccbcabcaaaabcbbcbaacccbbbbabcaaccbccccaccbcacccbacccbacaabbacbbaaacabaccabcabbcabccacabaaabbbacbbbbccbabbcaaabccaaacbccababacccaaaaccaccaccaccbacbbacabbcabaccbbaacabcbcaaaacbabacabaabccaacbabbabccacabaaccbbcacbabaaccaabbbabccccabbbababbcacabcccabacabacbccacabcbbccaccabaabacbcccaaccabcbabbcacbcbbccacabbaaacbccbcacccbaccaabbaabbabaaaaccacbbccababcaabcbcbbbbcabbccbbcbcbbaaaccccacbbabbaccaacbacbccacccabcbabaaabcbaabccbcbcbaabcbbababbcabcacbbbcabcbcaacbaccbcabaabbccabaaacacabaccacbbcbcbcccbabcacabbbaccbbcacbcbccbbacaacbabbcacbbabacccbbccbabaccbbcccacccaccccabbcbbcacabacbcaacacaccaacacaacacaababcbbbcbacaabcbccaacabcacbcacbbbaaacacbcbccabcaccaaabbcaaabcabccbbacaaabbbabcbabcccccccccbbbaabaccbcabaacabaababcccbcbababcaaacccbcbcccbabacbccbbaaacaaaacabaabaacbbabbbaaccbaaacbccabacbbcccbbcbabaabcacaccacbcbabacbcbcbcaabbacaacaacbbabccbaacacbabaabcbbccaccbbbcaacbbcaccbbcbaaabbcbcbacbbaacabcbaccbccbaacabbbcacbcccaccbbaaccccbbcabcaabccbbbabaacbcabcbbbbcbaacbccbacbcaaabbcaaaaaabbaababaabaaacbcbbbaaababaacbacbabbcbbcbabcaccaacababbbbcaaaabbbaabbabacaaaaacccbaccaabbbabbbcbabaaabcaacbcbbcbbbbbabbccabbabccccbacbbabaaccbbaabbabbcacaaacaccabbaaacabccaacacacbbcacbaabbacaccbbabbccbacbcccabacccbaabbabcbbccacabbcbaabbbabbcaaaabbccbbcccaababbccaabbbabcbabbcbcaabbaacbbcabbaabbcaabcaaccabacaacbaabbbbbacabcaaabbcbcbbbaaccbaacccaccbbaabaabbbbabcbcabbaccbaacccabbbabbcaaaabcbbcaabbcccbcabbbcbbccccbabbcbccacbabbcaabcabccaccccbabbcabacbababbcbbcbbbcccbcaabcbbcabaccbabcbacabcbbabcaacccaaabcabacbccaaabcabaccccbaaabbbccbbabbabccbabccbabbccccbbccacbcabbcabaccaaaabccbbcaaaccacbabbccabbbcacaacaabaabcbbaacacabbabbcbaacaaccaabbccccaccaaacbaaacaccabcbbacbcacaaaacaaaccaacbaaccbbacbaabccbbcbacbccaabbabbacacbcbaacbbcabbbabcccccbcacaabbaabaacbbbbbcccbcacbbabcaabcabbacabbbcacababcbabbbbcbaaccabacaabbaaabcbcccaabaacbbacacbacbacacccbcbacaaccbaababbacaacacbbacbcaaabacbbbcbbcccbacaabaabbabaabbcabbbbcbbcccbabcbaccbaabbcbccaabababbcacaaaccbbbaacaaababbbacbaaaaaabbacbaacbcbacaccacbcccaacbbabacbcabbcabacabbccbbccbaaababbaccbbbbabbacabbacbccaccaabbbabbcbbaccaaabbbcbaabcbbbbcbbbabbbbbabcacacaacabcaaaaaabbaccaaccbcacccaacaabcbaababcbcbbbaabbcbbaaccccaaaccabcabcbbacbaaaacabacbabcccbcaccbbabbabcbbabbccbbcbcababaccbcbbaccaaaabbbbcaaaccabbbcabacaccbaabcabbcaacabbbaaababbcccabacaabbcaaaababacbccbccbcaccbaacbcbbabbaaacbacbbbbbacaaacccbcaaaabbccacbcbabbababcccacacaabacacbbcccaacabcccacccababaabccbcaabcccbacbacbababacaabacaabababbababaabcaacaacacaccaacaaccbbaabccabaaacccacacbbbcabbbcaaaaccbaacaaccbaabbabaccbbcbabbabbcccbcaccaccabbacbbbabcccabcbbbbabaabcbcccbacbacbaabbcbcbabbbccaabbcacacbcbbbaaccaabcbccbbaccabaacacbcacbbababbabcaacbbbbccbabbbbacababbabacaabacabbcaaaccbbbbcaaabcacabacaaacbccccabccbababcbaaccaacbbbccaacabbbbbcacbcaaabbaababacacccbccccbcabccbccbacccacababbccbacbcbcababaaccabbaccacccababccccaccbbcaaaaccaccabcaabbcbcaabbbaacbabaabaaabbacabaccbbccbcabcaacaaacacccbcbccacabbcbaacbccbaaccacacacababacbbbbacbbcccacbcbbcbbcbaacbcabcbaacaacccacabaccbbabacbaaaabbbcccbbabbabaccccabacaababccccaccbabcabccbbaccbaabcacaabacccbcacaacabacbbcccaccacacacabbbbbccbaaabbcbaccbaaaabbcabcbaacbbcccabbabbbbaccacbabcacacbaabcaacbccacacbcccbcbacaabacaacbacccaabaabbabcaaccacababaabacaabaacbccabcaaacbbbacccacaaaaaaaabcccaabcaaccbabacbbaabbcabbbbbcbcabbbbbccacacbabbaabbcbacabbacbaaabcaaccbbbbabacbcaaababacbacbacbccccacbcbccbccabacbaacaabcbacccacbcaccbcaaccbbbabbbbaaccbbabbabcbbccbcbbcbaccbbababbaabbbbbcaaaabaccababccacaccaabbbcbccacbcabcbbcccccacbabcbcbccaabababbcbbabbababcccabacbcccbcaaabacbacbacaaaaacbbabacaacbcabaaaccabcabacccabcbabacaaccbcccaaabbaccbcbbaccccbcccbaaacccbccaccccabccbababcbccccbbcbaacccaabbcbbbbcabaabcbaabcbbaaccacabcbbccccbabcccbacababbacccccaaabbabcbcbabcbbabbbbaaccaccaaacacaababbbcbabcbcbaacbaacbbcccbabbbbaccbacbcabbbbcbcbaaacaacccbbcbbbaababacccccbbccbaaaabbbacbcacabcbbababcbbbaaaaccccababcabbaacbaaccabcbaababcbbcacaabcaaaccbccabcababacccabaccbbacaabbabccccaaaabbbbabcabaabbbbccabcacccccaaacbbbacbcacaccccacbcaabbbcababcaacaaccaacbbacaaccccbababbbbbbcccbccbbacaabcacbccbbabbaababcbbacaccbabacbbbcbbabcabbaabcabcacaacccbcbaabbaccabaccccabacbaccaaababbabaaaabbbaaacbcacbbcacabaabccaacacacabacbbbbbcabbbbbccbbcabcbbcacbcbcccaabccbcaaabaccaabacccccbb")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1360Test.java b/src/test/java/com/fishercoder/secondthousand/_1360Test.java new file mode 100644 index 0000000000..9f4854c784 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1360Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1360; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1360Test { + private _1360.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1360.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.daysBetweenDates("2019-06-29", "2019-06-30")); + } + + @Test + public void test2() { + assertEquals(15, solution1.daysBetweenDates("2020-01-15", "2019-12-31")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1361Test.java b/src/test/java/com/fishercoder/secondthousand/_1361Test.java new file mode 100644 index 0000000000..d065fda7e6 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1361Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1361; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1361Test { + private _1361.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1361.Solution1(); + } + + @Test + public void test1() { + assertEquals( + true, + solution1.validateBinaryTreeNodes( + 4, new int[] {1, -1, 3, -1}, new int[] {2, -1, -1, -1})); + } + + @Test + public void test2() { + assertEquals( + false, + solution1.validateBinaryTreeNodes( + 4, new int[] {1, -1, 3, -1}, new int[] {2, 3, -1, -1})); + } + + @Test + public void test3() { + assertEquals( + false, solution1.validateBinaryTreeNodes(2, new int[] {1, 0}, new int[] {-1, -1})); + } + + @Test + public void test4() { + assertEquals( + false, + solution1.validateBinaryTreeNodes( + 6, new int[] {1, -1, -1, 4, -1, -1}, new int[] {2, -1, -1, 5, -1, -1})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1362Test.java b/src/test/java/com/fishercoder/secondthousand/_1362Test.java new file mode 100644 index 0000000000..176f2cb8c5 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1362Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1362; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1362Test { + private _1362.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1362.Solution1(); + } + + @Test + public void test1() { + CommonUtils.printArray(solution1.closestDivisors(8)); + } + + @Test + public void test2() { + CommonUtils.printArray(solution1.closestDivisors(123)); + } + + @Test + public void test3() { + CommonUtils.printArray(solution1.closestDivisors(999)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1365Test.java b/src/test/java/com/fishercoder/secondthousand/_1365Test.java new file mode 100644 index 0000000000..0c6a009eae --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1365Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1365; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1365Test { + private _1365.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1365.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {8, 1, 2, 2, 3}; + assertArrayEquals(new int[] {4, 0, 1, 1, 3}, solution1.smallerNumbersThanCurrent(nums)); + } + + @Test + public void test2() { + nums = new int[] {6, 5, 4, 8}; + assertArrayEquals(new int[] {2, 1, 0, 3}, solution1.smallerNumbersThanCurrent(nums)); + } + + @Test + public void test3() { + nums = new int[] {7, 7, 7, 7}; + assertArrayEquals(new int[] {0, 0, 0, 0}, solution1.smallerNumbersThanCurrent(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1366Test.java b/src/test/java/com/fishercoder/secondthousand/_1366Test.java new file mode 100644 index 0000000000..2d3ad6d720 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1366Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1366; +import org.junit.jupiter.api.Test; + +public class _1366Test { + private _1366.Solution1 solution1; + private static String[] votes; + + @Test + public void test1() { + solution1 = new _1366.Solution1(); + votes = new String[] {"ABC", "ACB", "ABC", "ACB", "ACB"}; + assertEquals("ACB", solution1.rankTeams(votes)); + } + + @Test + public void test2() { + solution1 = new _1366.Solution1(); + votes = new String[] {"WXYZ", "XYZW"}; + assertEquals("XWYZ", solution1.rankTeams(votes)); + } + + @Test + public void test3() { + solution1 = new _1366.Solution1(); + votes = new String[] {"ZMNAGUEDSJYLBOPHRQICWFXTVK"}; + assertEquals("ZMNAGUEDSJYLBOPHRQICWFXTVK", solution1.rankTeams(votes)); + } + + @Test + public void test4() { + solution1 = new _1366.Solution1(); + votes = new String[] {"BCA", "CAB", "CBA", "ABC", "ACB", "BAC"}; + assertEquals("ABC", solution1.rankTeams(votes)); + } + + @Test + public void test5() { + solution1 = new _1366.Solution1(); + votes = new String[] {"M", "M", "M", "M"}; + assertEquals("M", solution1.rankTeams(votes)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1367Test.java b/src/test/java/com/fishercoder/secondthousand/_1367Test.java new file mode 100644 index 0000000000..c434020f6b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1367Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1367; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _1367Test { + private _1367.Solution1 solution1; + + @Test + public void test1() { + solution1 = new _1367.Solution1(); + ListNode head = LinkedListUtils.contructLinkedList(new int[] {4, 2, 8}); + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, + 3)); + assertEquals(true, solution1.isSubPath(head, root)); + } + + @Test + public void test2() { + solution1 = new _1367.Solution1(); + ListNode head = LinkedListUtils.contructLinkedList(new int[] {1, 4, 2, 6}); + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, + 3)); + assertEquals(true, solution1.isSubPath(head, root)); + } + + @Test + public void test3() { + solution1 = new _1367.Solution1(); + ListNode head = LinkedListUtils.contructLinkedList(new int[] {1, 4, 2, 6, 8}); + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, + 3)); + assertEquals(false, solution1.isSubPath(head, root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1370Test.java b/src/test/java/com/fishercoder/secondthousand/_1370Test.java new file mode 100644 index 0000000000..0f66fc1be4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1370Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1370; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1370Test { + private _1370.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1370.Solution1(); + } + + @Test + public void test1() { + assertEquals("abccbaabccba", solution1.sortString("aaaabbbbcccc")); + } + + @Test + public void test2() { + assertEquals("art", solution1.sortString("rat")); + } + + @Test + public void test3() { + assertEquals("cdelotee", solution1.sortString("leetcode")); + } + + @Test + public void test4() { + assertEquals("ggggggg", solution1.sortString("ggggggg")); + } + + @Test + public void test5() { + assertEquals("ops", solution1.sortString("spo")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1371Test.java b/src/test/java/com/fishercoder/secondthousand/_1371Test.java new file mode 100644 index 0000000000..8dbba215a4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1371Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1371; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1371Test { + private _1371.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1371.Solution1(); + } + + @Test + public void test1() { + assertEquals(13, solution1.findTheLongestSubstring("eleetminicoworoep")); + } + + @Test + public void test2() { + assertEquals(5, solution1.findTheLongestSubstring("leetcodeisgreat")); + } + + @Test + public void test3() { + assertEquals(6, solution1.findTheLongestSubstring("bcbcbc")); + } + + @Test + public void test4() { + assertEquals(1, solution1.findTheLongestSubstring("id")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1372Test.java b/src/test/java/com/fishercoder/secondthousand/_1372Test.java new file mode 100644 index 0000000000..227d57fad0 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1372Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1372; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _1372Test { + private _1372.Solution1 solution1; + private static TreeNode root; + + @Test + public void test1() { + solution1 = new _1372.Solution1(); + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, null, 1, 1, 1, null, null, 1, 1, null, 1, null, null, null, 1, + null, 1)); + TreeUtils.printBinaryTree(root); + assertEquals(3, solution1.longestZigZag(root)); + } + + @Test + public void test2() { + solution1 = new _1372.Solution1(); + root = + TreeUtils.constructBinaryTree( + Arrays.asList(1, 1, 1, null, 1, null, null, 1, 1, null, 1)); + TreeUtils.printBinaryTree(root); + assertEquals(4, solution1.longestZigZag(root)); + } + + @Test + public void test3() { + solution1 = new _1372.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(1)); + TreeUtils.printBinaryTree(root); + assertEquals(0, solution1.longestZigZag(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1373Test.java b/src/test/java/com/fishercoder/secondthousand/_1373Test.java new file mode 100644 index 0000000000..2a0a770a9c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1373Test.java @@ -0,0 +1,68 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1373; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _1373Test { + private _1373.Solution1 solution1; + private static TreeNode root; + + @Test + public void test1() { + solution1 = new _1373.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3)); + TreeUtils.printBinaryTree(root); + assertEquals(6, solution1.maxSumBST(root)); + } + + @Test + public void test2() { + solution1 = new _1373.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 8, 3, null, 6, 3)); + TreeUtils.printBinaryTree(root); + assertEquals(7, solution1.maxSumBST(root)); + } + + @Test + public void test3() { + solution1 = new _1373.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(-4, -2, -5)); + TreeUtils.printBinaryTree(root); + assertEquals(0, solution1.maxSumBST(root)); + } + + @Test + public void test4() { + solution1 = new _1373.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(4, 3, null, 1, 2)); + TreeUtils.printBinaryTree(root); + assertEquals(2, solution1.maxSumBST(root)); + } + + @Test + public void test5() { + solution1 = new _1373.Solution1(); + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 1, 4, 3, 2, 4, 2, 5, null, null, null, null, null, null, 4, 6)); + TreeUtils.printBinaryTree(root); + assertEquals(20, solution1.maxSumBST(root)); + } + + @Test + public void test6() { + solution1 = new _1373.Solution1(); + root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 4, 8, null, 6, 1, 9, null, -5, 4, null, null, null, -3, null, 10)); + TreeUtils.printBinaryTree(root); + assertEquals(14, solution1.maxSumBST(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1374Test.java b/src/test/java/com/fishercoder/secondthousand/_1374Test.java new file mode 100644 index 0000000000..bdc0454d51 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1374Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1374; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1374Test { + private _1374.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1374.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.generateTheString(1).length()); + } + + @Test + public void test2() { + assertEquals(4, solution1.generateTheString(4).length()); + } + + @Test + public void test3() { + assertEquals(2, solution1.generateTheString(2).length()); + } + + @Test + public void test4() { + assertEquals(7, solution1.generateTheString(7).length()); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1375Test.java b/src/test/java/com/fishercoder/secondthousand/_1375Test.java new file mode 100644 index 0000000000..749d41b93d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1375Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1375; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1375Test { + private _1375.Solution1 solution1; + private static int[] light; + + @BeforeEach + public void setup() { + solution1 = new _1375.Solution1(); + } + + @Test + public void test1() { + light = new int[] {2, 1, 3, 5, 4}; + assertEquals(3, solution1.numTimesAllBlue(light)); + } + + @Test + public void test2() { + light = new int[] {3, 2, 4, 1, 5}; + assertEquals(2, solution1.numTimesAllBlue(light)); + } + + @Test + public void test3() { + light = new int[] {1, 2, 3, 4, 5, 6}; + assertEquals(6, solution1.numTimesAllBlue(light)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1376Test.java b/src/test/java/com/fishercoder/secondthousand/_1376Test.java new file mode 100644 index 0000000000..df64e6c252 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1376Test.java @@ -0,0 +1,60 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1376; +import org.junit.jupiter.api.Test; + +public class _1376Test { + private _1376.Solution1 solution1; + private static int[] manager; + private static int[] informTime; + + @Test + public void test1() { + solution1 = new _1376.Solution1(); + manager = new int[] {-1}; + informTime = new int[] {0}; + assertEquals(0, solution1.numOfMinutes(1, 0, manager, informTime)); + } + + @Test + public void test2() { + solution1 = new _1376.Solution1(); + manager = new int[] {2, 2, -1, 2, 2, 2}; + informTime = new int[] {0, 0, 1, 0, 0, 0}; + assertEquals(1, solution1.numOfMinutes(6, 2, manager, informTime)); + } + + @Test + public void test3() { + solution1 = new _1376.Solution1(); + manager = new int[] {1, 2, 3, 4, 5, 6, -1}; + informTime = new int[] {0, 6, 5, 4, 3, 2, 1}; + assertEquals(21, solution1.numOfMinutes(7, 6, manager, informTime)); + } + + @Test + public void test4() { + solution1 = new _1376.Solution1(); + manager = new int[] {-1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6}; + informTime = new int[] {1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}; + assertEquals(3, solution1.numOfMinutes(15, 0, manager, informTime)); + } + + @Test + public void test5() { + solution1 = new _1376.Solution1(); + manager = new int[] {3, 3, -1, 2}; + informTime = new int[] {0, 0, 162, 914}; + assertEquals(1076, solution1.numOfMinutes(4, 2, manager, informTime)); + } + + @Test + public void test6() { + solution1 = new _1376.Solution1(); + manager = new int[] {5, 9, 6, 10, -1, 8, 9, 1, 9, 3, 4}; + informTime = new int[] {0, 213, 0, 253, 686, 170, 975, 0, 261, 309, 337}; + assertEquals(2560, solution1.numOfMinutes(11, 4, manager, informTime)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1377Test.java b/src/test/java/com/fishercoder/secondthousand/_1377Test.java new file mode 100644 index 0000000000..64ed5506cc --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1377Test.java @@ -0,0 +1,84 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1377; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1377Test { + private _1377.Solution1 solution1; + private static int[][] edges; + + @BeforeEach + public void setup() { + solution1 = new _1377.Solution1(); + } + + @Test + public void test1() { + edges = + new int[][] { + {1, 2}, + {1, 3}, + {1, 7}, + {2, 4}, + {2, 6}, + {3, 5}, + }; + assertEquals(0.16666666666666666, solution1.frogPosition(7, edges, 2, 4), 0); + } + + @Test + public void test2() { + edges = + new int[][] { + {1, 2}, + {1, 3}, + {1, 7}, + {2, 4}, + {2, 6}, + {3, 5}, + }; + assertEquals(0.3333333333333333, solution1.frogPosition(7, edges, 1, 7), 0); + } + + @Test + public void test3() { + edges = + new int[][] { + {1, 2}, + {1, 3}, + {1, 7}, + {2, 4}, + {2, 6}, + {3, 5}, + }; + assertEquals(0.16666666666666666, solution1.frogPosition(7, edges, 20, 6), 0); + } + + @Test + public void test4() { + edges = + new int[][] { + {2, 1}, + {3, 2}, + }; + assertEquals(1.0, solution1.frogPosition(3, edges, 1, 2), 0); + } + + @Test + public void test5() { + edges = + new int[][] { + {2, 1}, + {3, 2}, + {4, 1}, + {5, 1}, + {6, 4}, + {7, 1}, + {8, 7}, + }; + assertEquals(0.0, solution1.frogPosition(8, edges, 7, 7), 0); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1379Test.java b/src/test/java/com/fishercoder/secondthousand/_1379Test.java new file mode 100644 index 0000000000..85d3c42465 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1379Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1379; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1379Test { + private _1379.Solution1 solution1; + private _1379.Solution2 solution2; + private static TreeNode original; + private static TreeNode cloned; + private static TreeNode target; + + @BeforeEach + public void setup() { + solution1 = new _1379.Solution1(); + solution2 = new _1379.Solution2(); + } + + @Test + public void test1() { + original = TreeUtils.constructBinaryTree(Arrays.asList(7, 4, 3, null, null, 6, 19)); + cloned = TreeUtils.constructBinaryTree(Arrays.asList(7, 4, 3, null, null, 6, 19)); + target = TreeUtils.constructBinaryTree(Arrays.asList(3, 6, 19)); + TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); + TreeUtils.printBinaryTree(solution2.getTargetCopy(original, cloned, target)); + } + + @Test + public void test2() { + original = TreeUtils.constructBinaryTree(Arrays.asList(7)); + cloned = TreeUtils.constructBinaryTree(Arrays.asList(7)); + target = TreeUtils.constructBinaryTree(Arrays.asList(7)); + TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); + } + + @Test + public void test3() { + original = + TreeUtils.constructBinaryTree( + Arrays.asList(8, null, 6, null, 5, null, 4, null, 3, null, 2, null, 1)); + cloned = + TreeUtils.constructBinaryTree( + Arrays.asList(8, null, 6, null, 5, null, 4, null, 3, null, 2, null, 1)); + target = TreeUtils.constructBinaryTree(Arrays.asList(4, null, 3, null, 2, null, 1)); + TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); + } + + @Test + public void test4() { + original = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + cloned = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + target = TreeUtils.constructBinaryTree(Arrays.asList(5, 10)); + TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); + } + + @Test + public void test5() { + original = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 3)); + cloned = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 3)); + target = TreeUtils.constructBinaryTree(Arrays.asList(2, 3)); + TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1380Test.java b/src/test/java/com/fishercoder/secondthousand/_1380Test.java new file mode 100644 index 0000000000..d1a98b373d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1380Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1380; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1380Test { + private _1380.Solution1 solution1; + private _1380.Solution2 solution2; + private static int[][] matrix; + + @BeforeEach + public void setup() { + solution1 = new _1380.Solution1(); + solution2 = new _1380.Solution2(); + } + + @Test + public void test1() { + matrix = + new int[][] { + {3, 7, 8}, + {9, 11, 13}, + {15, 16, 17} + }; + assertEquals(Arrays.asList(15), solution1.luckyNumbers(matrix)); + assertEquals(Arrays.asList(15), solution2.luckyNumbers(matrix)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1381Test.java b/src/test/java/com/fishercoder/secondthousand/_1381Test.java new file mode 100644 index 0000000000..667f90a74f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1381Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1381; +import org.junit.jupiter.api.Test; + +public class _1381Test { + private _1381.Solution1.CustomStack customStack; + private _1381.Solution2.CustomStack customStack2; + + @Test + public void test1() { + customStack = new _1381.Solution1.CustomStack(3); + customStack.push(1); + customStack.push(2); + assertEquals(2, customStack.pop()); + customStack.push(2); + customStack.push(3); + customStack.push(4); + customStack.increment(5, 100); + customStack.increment(2, 100); + assertEquals(103, customStack.pop()); + assertEquals(202, customStack.pop()); + assertEquals(201, customStack.pop()); + assertEquals(-1, customStack.pop()); + } + + @Test + public void test2() { + customStack2 = new _1381.Solution2.CustomStack(3); + customStack2.push(-1); + customStack2.push(20); + assertEquals(20, customStack2.pop()); + customStack2.push(30); + customStack2.push(40); + customStack2.push(50); + customStack2.increment(5, 100); + assertEquals(140, customStack2.pop()); + assertEquals(130, customStack2.pop()); + assertEquals(99, customStack2.pop()); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1382Test.java b/src/test/java/com/fishercoder/secondthousand/_1382Test.java new file mode 100644 index 0000000000..394636d1cc --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1382Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1382; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1382Test { + private _1382.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1382.Solution1(); + } + + @Test + public void test1() { + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList(1, null, 2, null, 3, null, 4, null, null)); + TreeUtils.printBinaryTree(root); + TreeUtils.printBinaryTree(solution1.balanceBST(root)); + } + + @Test + public void test2() { + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList(1, null, 2, null, 3, null, 4, null, 5, null, null)); + TreeUtils.printBinaryTree(root); + TreeUtils.printBinaryTree(solution1.balanceBST(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1385Test.java b/src/test/java/com/fishercoder/secondthousand/_1385Test.java new file mode 100644 index 0000000000..b6c0ee5463 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1385Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1385; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1385Test { + private _1385.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1385.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 2, solution1.findTheDistanceValue(new int[] {4, 5, 8}, new int[] {10, 9, 1, 8}, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1386Test.java b/src/test/java/com/fishercoder/secondthousand/_1386Test.java new file mode 100644 index 0000000000..06e34bf841 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1386Test.java @@ -0,0 +1,59 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1386; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1386Test { + private _1386.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1386.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 4, + solution1.maxNumberOfFamilies( + 3, + new int[][] { + {1, 2}, + {1, 3}, + {1, 8}, + {2, 6}, + {3, 1}, + {3, 10}, + })); + } + + @Test + public void test2() { + assertEquals( + 2, + solution1.maxNumberOfFamilies( + 2, + new int[][] { + {2, 1}, + {1, 8}, + {2, 6}, + })); + } + + @Test + public void test3() { + assertEquals( + 4, + solution1.maxNumberOfFamilies( + 4, + new int[][] { + {4, 3}, + {1, 4}, + {4, 6}, + {1, 7}, + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1387Test.java b/src/test/java/com/fishercoder/secondthousand/_1387Test.java new file mode 100644 index 0000000000..c882128ccb --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1387Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1387; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1387Test { + private _1387.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1387.Solution1(); + } + + @Test + public void test1() { + assertEquals(13, solution1.getKth(12, 15, 2)); + } + + @Test + public void test2() { + assertEquals(1, solution1.getKth(1, 1, 1)); + } + + @Test + public void test3() { + assertEquals(7, solution1.getKth(7, 11, 4)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1388Test.java b/src/test/java/com/fishercoder/secondthousand/_1388Test.java new file mode 100644 index 0000000000..0653e73c02 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1388Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1388; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1388Test { + private _1388.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1388.Solution1(); + } + + @Test + public void test1() { + assertEquals(10, solution1.maxSizeSlices(new int[] {1, 2, 3, 4, 5, 6})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1389Test.java b/src/test/java/com/fishercoder/secondthousand/_1389Test.java new file mode 100644 index 0000000000..b68c453eb7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1389Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1389; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1389Test { + private _1389.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1389.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {0, 4, 1, 3, 2}, + solution1.createTargetArray(new int[] {0, 1, 2, 3, 4}, new int[] {0, 1, 2, 2, 1})); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {0, 1, 2, 3, 4}, + solution1.createTargetArray(new int[] {1, 2, 3, 4, 0}, new int[] {0, 1, 2, 3, 0})); + } + + @Test + public void test3() { + assertArrayEquals(new int[] {1}, solution1.createTargetArray(new int[] {1}, new int[] {0})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1390Test.java b/src/test/java/com/fishercoder/secondthousand/_1390Test.java new file mode 100644 index 0000000000..0485b6c084 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1390Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1390; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1390Test { + private _1390.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1390.Solution1(); + } + + @Test + public void test1() { + assertEquals(32, solution1.sumFourDivisors(new int[] {21, 4, 7})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1392Test.java b/src/test/java/com/fishercoder/secondthousand/_1392Test.java new file mode 100644 index 0000000000..96bc768abe --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1392Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1392; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1392Test { + + private _1392.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1392.Solution1(); + } + + @Test + public void test1() { + assertEquals("l", solution1.longestPrefix("level")); + } + + @Test + public void test2() { + assertEquals("abab", solution1.longestPrefix("ababab")); + } + + @Test + public void test3() { + assertEquals("leet", solution1.longestPrefix("leetcodeleet")); + } + + @Test + public void test4() { + assertEquals("", solution1.longestPrefix("a")); + } + + @Test + public void test5() { + assertEquals("aaaa", solution1.longestPrefix("aaaaa")); + } + + @Test + public void test6() { + assertEquals( + "", + solution1.longestPrefix( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab")); + } + + @Test + public void test7() { + assertEquals( + "abbbaaabbabbaaaaaabbabaabbabbaabbabaababbbabbaabaabbbabbaabbbbbbabaaaaabbababaabaaaabaaaaabaabbbabbbbaaaababbbbbaabbaabbbaaabaabaababaaababaabbaaaabbbaababbbaabaabaaabbbbabbaaabbbabaabbbabaabbbbaabaaaaabbabbbaabaabaaaaabaabaabbaaaabaabbaabbbabaabaababaabababbbabbaaabbbabababbbbababaaaabbbbababbbabbaaabbabbaaaaaaabbbabbbbbaabababbbbabbaaabababbbaaabbbbbbbbabababbbaabbbabbbaababbbaabbbaababaabbbaaaaaaabaabbabbaaaaaaababababbbbbaaaaabbababaabababbbabbaaaaaaaabbabbaabbbaaaabbabaaababaabaabbaababaababaabbabbabbbaaaabbbaabaabbbbababaabbbaaaabbbbbaaabbaabaaabbbaaaabbbababababbabbbbbaaababbaabbaaabbbabbbabaaaabbbaabbabaababbababbaaababbbaabbaabbaababbababaabbbababaaaabbabaaaaaababababbaabaababbbaaabaabaababbbbaaabbbbbaaabbabaaabbbaabaaabaabaababababaabaaaaaaaabbaabbbaaabababbbbbbabbbabbbbabbbabaabbbabbbaaabaaababaaaaababbabbbbaaaabbbababaabababbabbaaaabaabbabbbbbbbabbabbbbbbababbbabbabbabbabaabaaabbbbaaabbbbaabbabbbaaaaaabbabbbbbbaabbababbaaaabbbabaababaabbbaabbaabbaaaaabaaababaabbabaabaabaabbaabaaaabbabaaabaabababbaabbababbbabbabbbbabbbbababbabbbbbaabbaabababbaabaababaabaaabbaabababaabaaaababbbaabbabbaaaabbabaaaaaababbaaabaaabbabaaabaaaaaabbbbbbaabaabbababaaabababaaabbbbbabaaababbaaabbbbabaaababaaaabaababababbbabbbbabbbabbbbaabbbabbaabbaabaabbbababaaabbaabaaaabaabbbbbbababbbababaabababbbaabbbbbabbaaaaaabbababbbabaaabaabaabbbaabaaabbbabaaaabababbbbaaaabbaaabbbaababaaaaabaabbaababaabbabaabbaaabbbbbaababababbbabbbaaaaaaabaaaabbabaaababbaaaaabababababaaababbbabaaabaaaaabaaaaaaabaabbbaabbbbbabaaaababaaaaaaaabbaabbbbbbabaaabbababbbbbaaaabaaabababaababababbbaabaabaabaaabaabbaababbbbaabbaaabbaaaababaaababababbbaaababbababaaabbabaaabbaaaaaaababaababbaaaabbbbaababbbabaaabababaabbbabbabababbababbaaabaabbbbbbaaaabbbbababbabaaabababbbbbbbbabbbbaaabaaababaaaababaabababbbabaaabaabbbbbbbbbaabaaababbabababbbaabbaababbaaaabbbabababaaabbbaaabbaababbabbaababbbabbbaabbbbaaaaaaaabbabaaabbabbaaaabbbaaaaaaabbbbaaaabbabaabbabbbbbabaabbbbbaaabbbabbbaabbaabbabbabbaaabaabaabbaabbbbaabbaaaaabaaaaabababaaababaababababbbbaabababbbbaabaabbaaaababbbaaababaaaabbbabababaabbbabbbbaababbabaaaabababbaaaabaabaabbbababbbbabbaaaaaaababbabbbabaaabaabbaabaaabbbbaabbaabbababaabaaabaababbaabbbbbbaaababbbbbbababbaabaaabbbaaababaaaaabbabaaaaaaaabbbababbabababbabbbaababaaaabaabaabbbbbbaabbbaaaababaaaabbbaabaaaaabbabaabaaabaaabbabbabaaaaabaaaabbaabaaabaabbbabaaaabbaabababaaaabaaababbbbaabbbbbabaababbababaaaababbbbabbaaababbabbabbaabababbbbaabbaaabaabababbbabbbabbbbabaaabbbabbaaabbabbbababbbbaaaaabbbaaabbaaaabbabbaabaaabbbbbaabbbabbabaabbabbbaaaabbbbaaaabaabbaabababbbbbabbbabbbbaabbabbaaaaaabaababaaaaabbabaabbbbbaaaabbaabbaabbbaabaaaabbabbbbbbaaaabbbbaaaaaaabaaabbaababaaaaaaabaaabababbbbabbababbbbbaaabbabbaaaabbaaabbbabbbaababbaaabbbbbaaaaabaaaabaaaaaaaabaaaabaababbabababbaabababbaabbaabaababbbababbbbbabaaabaabaabbabbbababbbbabaaaaaabbbaaaabaaabbaaabbabbbbaaabbaaaabbaabbbbbbababaabbbabbabababaabaabbaabaaaababbabababbabbaaabbabbaabbbbbaaaaabbbabaabaaabaaababbabaaaabaababbabbaaaaabababbbbaaabaaabbababbabbbbabaaaaaabaaabbabbaabbaabaaabbbbbaaaababaaaababbabaaaabbabaabbabbaababbbaaabaabbabbabbbabbbabbbbbbbbabbabbaababbbabaaaaaaaaaaababbbaabbbabbbaababababaaabbabaababbaabbaaaaabababbbbbaabbabbabaabaaabaaaabbabbbbaabbabbbaababbabbaabbababaaabbaaabbbbaaaabbbbabbbbabbbabbbbaaabbaaabaabaabbaabbabbaaaaababbbaaabbbbbbbbbbaaaabbaaaabbaabaaaaabaabbbbbaabaaabbabbabaababbbbbbbaabaaababbaaabaabbababaaaababbabbabbbbaabbbaaabbbbbaaaabbbababbbabbaabaaabbabaabbabaaaaaaaabababbbababbbbabbbbbabbaaaabbabaabaabbabaaaaabbaababaabbaabaababbaaaaabbaaaabbbbaabaaabbabaaaabbababbaaabababaaaaabaaabaaabbbaababababaabbabbbbbbabbaaaaabbbaabaaaaabbbaabaaabbaaabababbabbbbbbbaaaabbaaabbabbbbbbbbbaabababbbbabbbbabbbbaaaabaababbbabbaabababababbaaababbaaabababaaabaaabaaabaabbbbbbbbbabbbaaabbabbbbabbbababaaaaaabbbbbbbbbbaababbbbaaaababaabbaabbaabaabababbbbabbabbbbbabbabbbabaaabbaababaabababbabaabbbbbaabbbbabbbabaaabbbbaaaaabbbabbbbababbbabaabbaabbabbaabbabbaabbbbabbbbabbaabaaaaaaabaaaabbaaaabbbbbabababbbbaabbbbbbabbbaaaababbbbababbbbabaabaabbaabaabbaaabaabbbbabaaaabbabbabaaaaaababbbbbbabbaabaaababbabbbaaaaabaaaabbaaabbbbaaabaaaabbbaabaababaababababaabaaabbaabaabbaabbbbabbabbbbaaabbabaabbbaabbabaaaaabbaabaabababbbaabbbabaabaaaababaabbbbbabaaaabaabbbbbbbbbabbaaaaabbbbabbbaaabbbbbbaababbaababbbbbbbbbababbabbbabbabaabaaabababbbbababaabbbbabbaabbbabaababbbbbbbabaabaabbbabbbaaabbbabaaaabbbbbbbbabbaababbbbbaabbbababbbbbaaaaabbbbbbbbabbabbbababababaababbbaaabbabbababbabaaaaaabaaabaabbaabaaabaabaabbbabbabaaaaabbbabbabbabbbbababbabbbabbbabaababaaaaaabbaabaaaaaaabbabaabaabbbbaaaaaababaaabbbbababbaabbaaababaaabbaaabaabbbbbbabbbaabaaabbabaabbbbbaaaabaabbbbbabbabaaaaaaaaaabbbbbaabbbbaabbbbbbaabbababbbaabaabbbabbaabaabbbbaaaabbabaabaabbabbabababbbbbbabbbbbbaabbabaababaabaababababbabbaaabbbbaaababbbaaaaaaaaabaabbbbababbbbaaaaabbbbbbbabaaaaaabbabbbababbbbbabbaaaaabbaabbbaabbbaaaaabaaaaaabbaaabbbbbbbbbbbbbababbbaabbbbbabaaababbbbaabaabbbbabaaaaababbbbbbbbbbbaabaaaaabaaabababbbabbaabaabaaaababbababaaaabbabababbbababbabbaababbaabaaabaabaaaababbbbabbbabbbbaaabbbbbbbbaaabaababaabbbbaabababaaaabbaababbaababababbaabababbbbbbaabaabaaaaabbbbaabbaaabaaabbbaababababababbbabababaaaabbaaaaaababaabbaabbaababbbababbaababaaabbbaababbbababbbabbabbbbababbbbbabbabbbbaaabbbbbaaabaaabbabbaabbaababbbaaabbbabababbbbbbabbbaaababaaaaaaaaabaabaaaababbbaababbbbbaabbabaaabbaaaabbabbaababbbaabbbababaababaabbbbbababbaaaaaaabbaaaabaaaaaaaabaaabbbaabbaababaabaabbbaaabbaaaabbbaaabaabbbbaaaabbbbbaaabbaaabbbababaaaaabbbbbaaabbaabbbabbabbbbababaaabbbabbbaaabbbaaabaaabbaaaabaaabbabbabbbaabbaaabbabbbaabbaababbbaababaaabababbaabaabbaabbbabbbaabbabbabbabbababbaabaabbbbaabaabbabbbbbbbbbbaabbababbabaaabaaabbababbabbbbbbaabaabbaababaabababbbbbabaabaabbaaaababaaaabbbbbbaabaabbbbaababbbbabbbaaaaabaababababbabbababaabaabbbbbbaaabbaabbabbabbbaabbbaabbbbaabbbbbbbaabbaabaaaabbabbbaabababbbabaaabbbbbbbbabbbbbbababababaaaaaabaaabaabbbbbabbbbabbbaaabbaaababbaabbbbbaaaaabbbabaaabbaaaabaaababaaaabaabbabbabaabaaaaaaabaababaabaaaaaaabbaaaabaababbbbbbababbbabbaabbbbbbbbabababbbbbbbabbbabababaaabaabbaabbbaababbaabaaabbbaaaabababababbbbbbbbaababbbababbabbbaaaaaabbabbabaabaababbabaabbaabbbaaabbaaababbaaabaabbbbbbbaaabaabbbaabbbabaabaaabaaaabaaabbbbbbbbbaababbaabaaaabbbaabbbbaaababaabaaabaaaaabaababbaabaaaabbbabbababaaaabaaaabbaaaaaababababaaabbaabbabbbaaabaaaaaaaabababbabaaaabbabbabbbbbaaaaaaabbbbbaaabbaabbbbbaabbabbbabbabbbaaaababaaabbbababaaabbbbbbbbbabaabaaabbbaaabababbabbababababbbbbabbbabaaaabbbabbbabbbaaabbaabbaabbabbaabaaabbbbaaaabababbabaaaabbbabaaaaaabbabbaabbaaaaaaaabaaabaaabbbbaabbbbbaabbaabaababaaaabbbbbaabaabbabbbbabbbaabaabaaabbabbbabaabaabbabbabbabaabbababababbababbaaabaaabbbabbaaaaababbbaaaaabbbaaaaabbabaaabaabbaaabbbaaabbabbbaabbabbbabbabbbbbbbbbbaaababaaabbababbbaababbaabbaababbbbababbbabababbabbbabbabababbaaabbaabaaaabbbaaabbababbbaaaaaabaabbabaabaabaaaaabbbabaaabaaaaabbbbabbbbbabbaababbabaabbbabbaababbbbbbaabaabbabbbbbbbaaabbbbabbaaabbababbbabbabbabbabaaaaabbaaaaabbaaaabaaabbbbaabbabaababbbbababbbbbbbaabbbabbaaaabaabbbaabbaaabbabaaaaabbaaabaababbbbbbaabaaabababbabaabaabbbabaabbbbbbbaabaaabbbbaabababbaabaaaababbbabbabbaabbbbaaabaabbbababbbaaaabbaababbababaaabaaaabaaaaaaaaabbbbbabbaaabbabaaabbaaabaabaababaaaabbbaababaabaaaabbbabbbbaaababaaabbbabbabbbababaaababbabaabbbaaabbaabbbabbaabbbabaabbaaababbbabbaaaabbaaababbabbaaabaabbbbbbaaaaaaabababbabaabaaabbabbaaaaabbabababbbbababbbbababbbbaabbababaaaaababbabaaababaaaabbaabbbabbaaaaaabaaabbabbbaabbabbaaabbabbaabbbabbaaaaaabbbabbbaababbbbbaabbaaaaaababbabbbaaaaabababbaaabbbaabbabbaaababbbbbaabbbbbabbbababaaabbabbaabaaabbbbbaabbabbbbabbbbaabbbbbbbbaaabaababaabbaabaaababbaababbbbbabaababbbaabbbabbbbbababaaabbbabbaaaababbabbaaaabababbbbabbbabaabbbbaaabbaaaabaabbabaabbbaabbbabbbbbabbabaabaaabbbaabbaabbbbbbbbababbbabbbbabababbaaababaabbbbbaabbbbbbaaaabababbababaabbaaabaaabbaabbaaabbbbbabbabbabbaaaabababaaababababaaabbabbbaabbaaabababbabbbaaabbbbaabbaabbabbbaaaaabbbbabbbbbabbaaababaabbbbaabbbbaaaabaababbbabbaabbbabbbbbaaaaabbababbbaabbbbaaabbaaabbbaaaaaaabbabbbbbabbbbaababaabbbaaabbabbbbbbaabbabbabbaababbaaababbabbabaabaabbbaaabbabaaaaababbabbabababbabbbbabaaababbaabbaabbaababbabaabbabbbbbabbaabbbbababbbbaaabbbaabaabbababaaaabaababbbbabaaababbbbababbbabbbbbbbbabbaababaabaaaabbbbbbaabbaaabbbababbbbabbbbbabbbbaabababaabbbaababaaaabbabbaaaabaaababbaabbbaababaabaabbabbabaabaabbbbbbaaaababbbbbbbaabbbababababaaabbaabbaabbaaaaaaabbaaabbbaabbababaaabbaaaaabababbabbabbbaabaaaaababbbbbabaaaababababaabaabbbbabbabaababbbabbaababababbaabaabbbbaabbabbbaaabbaaaabbaabbaabbbbabbbbabbbbbabbbaaabaaabbaaabaababbbbbbbbbbbbaabaaabbbbabbabbbbbbaaabbaabbabaaaabaabbbbbabbbababbabbabaabaabaaababbbbbbbbabababaaaabbbaababbaabaaabaababbbaababbbbbabaabaabbbbabaababbabababbaaabaababbababbbabbbbbbabaabababaaabbabbababbbbbabbbbbbbbabaaabbaaaaabaaaaabbaaabaaaaaaabaaabbabbbbbbabbbabbbbbababbbbbabaabaaaabbabbbababbbbbabaabbbaaababaaaaaabbabaabbbbbbbbabbaabaabaabbabbabbbababbbbabaababaababaabbabbbabbbabaaabaaabaababbaabaaababababbbaaabbbabbbbbbbbbabbabbababbbbbbbaaaaabbaabbababbbbabaaabbababaabaaaaaaababbabababbaabaabaaababbbabbbbbabababaabbbabaaabaabaabbaabbbbbbbaabaabbaababbbaaabbbabbaaabaabbbbaaaabbaababbababbbaabaaaabbbabbabaabaababbaababbbbbbababbbbbbaaabaabbbbababaabbbbaabbbabbabaaabbaaaaabbbabbaaaabbabaabbbaaaabbababbabbbaabaabbbbaabbabaaababaabaaaaabaabbabbbabbbaabbababbababaababbbababaaabaabbabaabbbbaaaaaaaababbbbbbbbbbababbbbababbbbbabaaabbbbbabbbabaaaabbbaabaaabaaabbbabbbbaaaabaaabababbbabaaaabbbbbabbbbaabbabaabbabababaabbbababbbabbbabbbbbbbbaaabbabbaabbabbbbbbbbbbbbbbbabaaaababababbbabaaababbabbaaabbabbababbaaaaaaaaabbaabbaababbbabbaabbabaabababaabaaaabbababbbabaabbbaaaaabaababbaabbaabbaaabaaabbbbaaababbbabababababbbbaababbbababaabaaaaaababbbaabbaaaababbabbaabbbbbbbbabbaabbbbbaabaababbaaaaaabaaaabbbabaabbbaaabbbababbbaababbbabbaabaaabbabaaaabaabaaaabbbababaabbaabaabaabaabbaabbbbbbbabababaabbbbababaababaaabbbbaabbbbbbaabbbbaaabbbaabababbaababababbaaaabbbbbabababbbbbbaababaabbbabaabbbabbabbbbbababababbababbbbaaabaaaaaabbbbbbbbbbaaaaaaabbaaabaaaababbaaaaabbbaaaaababbaaabbbbbaaabbaabbababaabbaabaaaaabbaaabbbaaababbabaaabbabbabbabbbabbababbbaabaaaaaaaababbbbbaabbaabbaaaaaaabbbbbbbabbabbabbbababbbababbaabbbabbbaaabbbbaabaabaaaaabaaaaabbbaaababbbababbbabbabaabaabaaaabababaabbbabbbababbaaabaababbbaaabaaaabaaaabbbbaabbabababababababababbabbaabbaaabbbbaabbbabbbbbbaaaaabbbaabbbbababbbabaaaaabaabbabbaabbaabaaababaaaabaababbbaabbabbaaaaabaaabaababbababbababbaaabbbbbababbbbaabbaabaabbabbbbbaabbbaababbbaaabababbaabbabaaabaabbaaaabaaaabababbbbbbbbbaabbaaaaabbabaabbbbaaabbaaaaaaabaabbbabbabbbbbbaaabbaaabbbababbabbbababbbbaaabaabaabbbaabaababbaabbaaababbbabbaabaabaababbabababaabbabbaabbaabaabbbbbaabbbabbabbbabbbbaabbbbaaabaaabbaaaaababbbababbbabbbbabaabbaabababaaabbbbaaaaabbbbabbbabbbbbaabbaabbbbaaaabbbbbbbbabbbaabaaabbbbabaabaababbbbabaabaabaabbbbbbbbaaabbbaabbaaababbabbaaabbabbbbbabbaababaabbaabbaabbabbaabbabbaabaaaabbaaaaaaabbbbaaaabaaabbbbbaabaababbbaabbababbbbabbabbbaaaaabbaabbababbaaaaaababbabbaababaaaaabaabbababbbbbaabbaaaabaaabaabbbbbaabbabbbabaaabaabababbbaaaaabbbbbbbabbaaaaaaaababbaaaaaabababaaabbbbaaabbabaaabbabaaaabbaaaaaabababaaaabbaaabbbabbbbabbbabbaabbbabaabbbbbbaababaabbaabaabbbbbabbbababbabaaabbabbbbababaaaaaaaaaabbbbaaaabbabaabbaaaaaaaaaabbaaabbabbbbbababbbbbbbbbbaabbbbbbabaabbbaaaabbaabbaaaaaaabaabbaaabbabbbababbbbabbabbbbabaaaaaabaabbaaaaaaabbabababbabbabbbbbbabaaabbabbbabbaaabbbaabbbbbabaabbbbababababbabbbbbbabbbbbabbbabbabaaabbababbabbabaaaaaabaabbabbaaababbaabbbaaabbabaabbbabaaaabaabbbabbaaababaaaabbabbbabaaaaaababaabbaabbbbabbaababaababaabaaaaabbaaaaaabbbbabbbaaababbababaaababaababaabbabbbabbabbaabbaabbbbabbaaaaaaababaaaabaabbbbbaabaabbbaababbbaaabbaaaabbabbaababbaabbbaabbabaaaabbabbbbaaabaaabbabbaabaabababaaaabaaaaabbaaababbaaabaaabaaababaabbbabbabaabaabbaaababbbbababbaaababbabbbabbbbabaaabbabbaaabbbababbababababbaabbbbbababababaababbbabaaaabbaabaaaabababaabbabbaabaaaababaaaabaabbbababbaaabaaaabaabbbbababaaaaabaabababbaaaaabbbababbbaababbabbaaaababaaaaabaabaaaaaabaaabbbabababababaabbbabaaabbaabbbbabaabaaabababbbaabbaabbaabbababaaaaabbbbabaabbabbabbbbaabbbaaaabbbaabaabbabbababbaaaaaaabbaabaaaaaabbabbabaabaaabbaaabbababbabbbbbbabbaabbaabababbaabaaabaabbbbbbbabbaaaabaaaaaabbabbbbaaabbaabbbbaaaabababaababbabaaaabbabbbbaabbbbbbbbbbbaabbaabbbaabbbbbaaababbbaabbabaabbaabbbbabbbabaaabbaabababbabaabaabbbaaaaabaabbbaabbaabbbaabbababbababbababbbaaaabaabbbabbaaaabaaabbaaaabbbbbbaabaaaabbababbaabaababbababaaaaaabbabbbbbababbabaababaabababaababbbaabaaaabbabbaaababbaabaaababbbaaabbaabbbbbbabbbbbbbbabaabaabbbbbabababbabaaabaaabaaaabbbababaababbabbaaaabababababaabbbababbabbbbbabaabbabbaabaaababbabbbbaabbabbabbbbababaaabbababbaabaaaabbbbabbbabaaababaabbaaaabbbaaaabababaabbbabababbbbbbaababbababbaabbbbaaaabaabbaaaabbbabbaaabaaaabbababaaaaaaabaabbaaabbaabaabbbaaabbbbbaababbbbbbbababbaababaabbbaababbbabbbbbbbaabaaaabbabaabaababaaaaabbaaaaabaabababaaaaaaabbabaabababbbaaaabababbbbbbbaababbbabaaaaaababbabbabaabaaaaaaaabbaabaabaabbbababbbbababababbaabaaaabbbbabbababaaabbaaabbbababbbbabbbaabbbbbaabaaaaaaabbaaabbbbbaaabbbababbbabbbaabbabaaaababbaaabbbbbababbaaaabbbbaaaaabaabbbbaaabbbababaaabbbbababaaaaaaaaababbaaabaababbabbabbaababababbababbbaaaaaabaabaabaaababaaaababbababbbababbbabbaabbbbbaabbbbaabbbbbabaaaaabbbabababaaabbbaabbbaaaaababbbaaaaaaabbbbaababbbbbababbabbababbabbaaabbabaaaabbbbabbaabbabbaaabbbababbbbaaabbaaaabbbbaaabbbabbbababbababaaaaabaaabaaabbabbbbabababbbbbbbabaabbbbbababbaaaabbabbabaabbabbababbbabbababbaaaaabbaabaaabaabbbabbbabababaabaabbbbbbabbaababbaaabbababbabbaaaabaabbabbbabbaaaabababbbaabbbaaababaabaaaabbabbbaaaabaaabaabbbaaaaabbbbbbbbaaaaabbbababbbaaabbaaabaababbaabbaaaabababbbaabaaabaaabbbbaaaabbbaaabbbbbaabbaabbbaaabbbabababbbbbbaaaaaabababbabaabbbbababbabbbababbbababbbbabbbbabaaaaabbaaaaaababaabbabbaabaababbaaabbaaaabbbabbbbaabbabaabaababaababaaabbbbbaabbabbabaabaabbabbbabbabababbbbbaabbbaabaaaaabbbbabaaaabbabbbaabaabbabaababbbabaaabababbbaabbababbaaabaaaabbaaabaaaabbbbbabbbabbbbbaaabaabbabababababbaaaaaabaaaabaabbbababbabababbbaabbabbabbaababbabbbbaaabbabbababbbaabbabbbbaabbbabaabbababbaababbaaabaababaaaabbaaabbbbabbbbaaabbbbaabaaabaabababbbaabababbaaabbbbbabbbabbabbbabbabbabaaababbbbaabbabaaaabbbaaabbaaaaababaababbaabbbababababbbbaaaaaabbaabaaaabaaaaaaabbabbbbaababbbbababbaaaabbbaabbbbaaaabaababaabbbbbaaabbbbaabaaaababababbbaabbaaaabaabbabbabbababbaababaabbbbbbbbbababbbaaaabbabaaabbaaabbababbbbbbbbbbaababbabbbababbaaabaaaaabbbbbaaaabaaaabbabaababbaabbbabbaabbbaaaabbabaababaaabbbababbababaabaaabaaabaaaabbbaabbbbabaabbaaaaaaabbaaaaabbbabbbaabbbaabababbaaabbaaabbaabbbabbaabbaaabbababababbbbabbaaaabaabaaaabaaabbbaabaaaaabbbabbaaabaaabaababaaaaaababbbabaaabbaababbbabaabaabbabaababbaaabaaabbbbaabbbbbabaabbbbaababaaaabbbbbbaababaababaaabbbaaaaaaaaaababbbaababbababbbabbababababaaabbaaabbaababbbbabbbbbbbababaaabbbbabbabbabbbaaaabbbbbbaabbbbbaaabbbabbaabbaaaabaabbbbbbbabaababbababbbababababbababbbaababbbabbabaababbabbaabababbaabaabaabbaaabbaaababbbbbbababaaababbbabbbababbbbabbaabbaaabbbaabaaaaaaabbaababaababaabbabbbbbaaaabbbabbababbbbbabbababaaaaabbabbababaaabaabaabaaaaabbabbabababbababbbaaaababbaaaaaaabbaaababaababbbbaaaabbbaabaababaaaabbababaaabbaaabaaabbaaaababbaabbbbbbabbbaabaaaaaabbbaaabbaabbabaaaabaaabbababaaababababbbbaabbbaabbbababbaababbbabbbabbabaabbababababbbbbaabaaabbbaababbbbabbbaabbabaaabbbbbabbbabbbbabaabbabbbbaabaaabbbaabaaaabaabbaabbaabababaaabbbbaaaaaabbbababbabaaabbbbababbabaaabbbaaaaaaabbababbbbabaaabababbaaaaabbbaababbaaababbababaabbbbbabaaababbbbbbbaaaaaaabbaaabbababbabbbaaaaababbaaaaabaabbaabaabaaabaaaaababbabbaaabbabbbbaabbabbabbabbbbabaabbabababbabbbbaababbabbbabbabbbabaaaaaababbbabaaaaabbbbbaaaababbbbababbbbbabbbbbbbbabbaaabbabaaaaabbaabbabaabbaabbaababbabaabaaababbbbbaaababababbaabbaabbabbababbabbaaaabababbbbabbbaaabababaaaabbbabbaabbbabaabbaaaabbaababababbbababbbbababaaaabaaabbbabbaabbbaabbabbbabbbaaabaaabaaaabaabababbbabbaaaabbaababababaabaabbaabbaababbaaabaababbbbabaaaaaabbbaabbaaabaabbbbaaabbaabaabbaababaabbbabbbabbbabaaababaababaababaaabbbbbabbbbbaabbbaaabaabbbabaabababbabababaaaabbbaabbbabaaabbabbbaabbbaabaaaaabbbabbabaababaaaababbbbaaaaaabaaaabbabbbbabbbaabaabbbabaabbbabababbbbbbbbaabaabbaaaaabbbbabbbaabbabaaabaaaabbabaabbbbbbaaaabbbababaabaabbbaaaaaaaabbbbabbbabbbaaabbbbabaabbaaabbababaabaabbaabaababbabbbabbbababbbbabbbbabbbaabbbabbaababbabbabbaabaabbaababbababababaaabbaabaabaaabababbabaabbaaababbbaaababbababaabbabaaabbbaaaabaababbbabaabbaaaabbbaababaabbbbaababbbbbaabbaaaaaabababaaabbababbaaabbaabababaabbbbbbabbbbbaabaabbaaabbaabbbbabbbaabbabaaaabbbababaabaabaaaabbabbbabbaabbbbbabaaabbabbabbbbbaabaaabbabababbaaabbbaabaabbbbbaaaaabbaaaabbbabbbaabaabbabbabbabbaabababbbbbbbbabaabaabbbabbbabbababbbaabbabaabbbaabaaaabbaabbaabbbabaabbaabaabbbabaaabbaaabbbaaaabbbbbaaaabbbabbbbaaababbbbbbbbababbaabbabbabbbbbabaaaabaabbaabbbbbabbabaaaaaabaababbabbabbaabbabbbabbbbaaabbbaabbbaabbbaaaababbbbbaaabaababbbbbabbbaabbaabbbbaaabbbbbbaabaaaabaaabaaabbabbaaabbaababaaabbaababbabbaaaabababbbabbaaabaaaaabbaabababbaabaaaabaabbbbaaaaabbaabbbaaabbabbbaaabbabbbabbbbbbababbbbaaabaaababbbabbaababbbbbabbabababbbbbabbaaaaaabbababbbababaaababbaabbabbbbaaabbbababaaaabababaaababbbababaabaaaabbabbbbabababababbbbbbaaabaaabaaabaababaaababbababbababbabbbabaabbabaaaaabaaabaaaabaaaabbabbaababbaababaabaabaabaabbaababbaaaabaaaaaaaaabbbaabaabaababbaaaabbababaababaaabababaabbbbbaaabaabbbbaaaaaaabbaabababaabbbabbaaabbaaabaaababaaababbabbbbabaabbaaaaaababbbaaababbaaabbabababbbaaaabaaaabaabbabbaaaaababaaabbaabbbbabaabbbbbbbaaabbaabbbaabbbbbbbbbbaaaabbabaabaaababbabbbaabababbaaaabaabbabbbaaaabaaaaaaaaabaaabaabbbbbababbbbababbbbaaabbbaabababbbbbbbaabbaaabbbaabbaabaabababaabbababbbabaaaabbabaabbbaabbababbaaabbabbaabaaaaaaabbbbabaaabbbababbabbaababaabababbaaaabbabababbbaabbbaabbaaabaaabbabbbbbbaababaaabbbaaabbabbbaabbbaabbabbaababbbabbaabbbaaaaabbabaaabaaabaaabaaaabbabaabbabbbbbabaababbabbbbbbaaabbababaaaabaabbbaababaabaaaabaabaabbaaababbbbbbaabbbbbbaaabbaaaaabaabbbbababbaaabaaabbabbaaabbababbabaaabbababbbaabaabbbabbbabbbbabbbbbbbbbabababbbababbbababbbabbbabbaabaaababaabbbbaaaabbaabaaabbbbbbbaaabababbbbaabababbababaaabaabbabbaabbabaaabbabaababbababaaabbbbbbaabbbaaaabaaabaababbbbabaaabbaaababbbbabaaaaabbbabaabaabbabaabbaaaabbaabbbbabbababbaaaababbbababbabbbbaaabbaaabaabababbbaaaaaaabbbbbaaababaaaabbbbaabaaaabaaababbbabbaaabaaabbabbabbbabaaaababaaaababaababaaaababbaaaaababbaababaaabbaabbbbaababbbaabaaababaaaabbbaabbbbbbaabbbbabbbabbaababbbbaabbbabaaaabaaabbbbbbaabaababababaaababbabbbbaababaaabaabbaabaaaaabbbbbbabbababaabaaabaabbaaabaaaaabbbaabbababbababaaababaaabbabbababaaaaababaabbbbbababaaaabaabbabababaabbaabaabababbbbaabbabaaaabbbbbbaaababbbbababbbbbabbaabbbbabbaababababbbbaaabbababbbbabaaabbbaaaababaababaaaabbababbabbbbbbaabaaaabaabbbbbbbbbababbbabbabbbbbabaabbababbaabbaabbbbbaabbbbbabaababaabaaaabbabbaabaabaabbababbababaaaabababaaaababababaabbbbaaaabbbabaababbabaaaaaaabbbbaaababbaaaabbbbabbbbaaaaaabaaaabbababaababbabbaabbbaaabbbbaaaaabababaabaaabbbaaabaaaaaaababababababbbaabbaabaaabbabbaabbbbaaaabbbababaabbbbbbbabbabaaabbaaaaaaabbababababbbbababbbababaaabbbabbbbababbbbbbbbabbbbababbabbbbaabaaaaababbabababaabbbaaabaabaabbaabaabbbbabbbbabaabababbabbaaaabaaaababbabababababaabbabbaabaabaaaabbaaabababbaababbabaaaaabbaaabaabaaabbbbbbabaababbbaabbaaabaababaaaaaaabbbaaabaaabbbbbabaaabbabbaabaabbaaababbbaabbbaabaaabaababaabbbbbabaaaaaaaaaaaabbaabbababaababbbbbaabbabaabbaabbaababaabbaaabbabbabbbbabbbabbabaaaababaabbabbabababbaaaaaaababbbabbbbabababbaabbbabababbbabbabbabbbabbbbbbababbaaabbaababaaaaabbbaaabababbabbaaabbabbaaaabaababaabbbaaabaaaabaabbabbaabaaaabbbbbabbabbbabaaabbbbbbababababaaaa", + solution1.longestPrefix( + "abbbaaabbabbaaaaaabbabaabbabbaabbabaababbbabbaabaabbbabbaabbbbbbabaaaaabbababaabaaaabaaaaabaabbbabbbbaaaababbbbbaabbaabbbaaabaabaababaaababaabbaaaabbbaababbbaabaabaaabbbbabbaaabbbabaabbbabaabbbbaabaaaaabbabbbaabaabaaaaabaabaabbaaaabaabbaabbbabaabaababaabababbbabbaaabbbabababbbbababaaaabbbbababbbabbaaabbabbaaaaaaabbbabbbbbaabababbbbabbaaabababbbaaabbbbbbbbabababbbaabbbabbbaababbbaabbbaababaabbbaaaaaaabaabbabbaaaaaaababababbbbbaaaaabbababaabababbbabbaaaaaaaabbabbaabbbaaaabbabaaababaabaabbaababaababaabbabbabbbaaaabbbaabaabbbbababaabbbaaaabbbbbaaabbaabaaabbbaaaabbbababababbabbbbbaaababbaabbaaabbbabbbabaaaabbbaabbabaababbababbaaababbbaabbaabbaababbababaabbbababaaaabbabaaaaaababababbaabaababbbaaabaabaababbbbaaabbbbbaaabbabaaabbbaabaaabaabaababababaabaaaaaaaabbaabbbaaabababbbbbbabbbabbbbabbbabaabbbabbbaaabaaababaaaaababbabbbbaaaabbbababaabababbabbaaaabaabbabbbbbbbabbabbbbbbababbbabbabbabbabaabaaabbbbaaabbbbaabbabbbaaaaaabbabbbbbbaabbababbaaaabbbabaababaabbbaabbaabbaaaaabaaababaabbabaabaabaabbaabaaaabbabaaabaabababbaabbababbbabbabbbbabbbbababbabbbbbaabbaabababbaabaababaabaaabbaabababaabaaaababbbaabbabbaaaabbabaaaaaababbaaabaaabbabaaabaaaaaabbbbbbaabaabbababaaabababaaabbbbbabaaababbaaabbbbabaaababaaaabaababababbbabbbbabbbabbbbaabbbabbaabbaabaabbbababaaabbaabaaaabaabbbbbbababbbababaabababbbaabbbbbabbaaaaaabbababbbabaaabaabaabbbaabaaabbbabaaaabababbbbaaaabbaaabbbaababaaaaabaabbaababaabbabaabbaaabbbbbaababababbbabbbaaaaaaabaaaabbabaaababbaaaaabababababaaababbbabaaabaaaaabaaaaaaabaabbbaabbbbbabaaaababaaaaaaaabbaabbbbbbabaaabbababbbbbaaaabaaabababaababababbbaabaabaabaaabaabbaababbbbaabbaaabbaaaababaaababababbbaaababbababaaabbabaaabbaaaaaaababaababbaaaabbbbaababbbabaaabababaabbbabbabababbababbaaabaabbbbbbaaaabbbbababbabaaabababbbbbbbbabbbbaaabaaababaaaababaabababbbabaaabaabbbbbbbbbaabaaababbabababbbaabbaababbaaaabbbabababaaabbbaaabbaababbabbaababbbabbbaabbbbaaaaaaaabbabaaabbabbaaaabbbaaaaaaabbbbaaaabbabaabbabbbbbabaabbbbbaaabbbabbbaabbaabbabbabbaaabaabaabbaabbbbaabbaaaaabaaaaabababaaababaababababbbbaabababbbbaabaabbaaaababbbaaababaaaabbbabababaabbbabbbbaababbabaaaabababbaaaabaabaabbbababbbbabbaaaaaaababbabbbabaaabaabbaabaaabbbbaabbaabbababaabaaabaababbaabbbbbbaaababbbbbbababbaabaaabbbaaababaaaaabbabaaaaaaaabbbababbabababbabbbaababaaaabaabaabbbbbbaabbbaaaababaaaabbbaabaaaaabbabaabaaabaaabbabbabaaaaabaaaabbaabaaabaabbbabaaaabbaabababaaaabaaababbbbaabbbbbabaababbababaaaababbbbabbaaababbabbabbaabababbbbaabbaaabaabababbbabbbabbbbabaaabbbabbaaabbabbbababbbbaaaaabbbaaabbaaaabbabbaabaaabbbbbaabbbabbabaabbabbbaaaabbbbaaaabaabbaabababbbbbabbbabbbbaabbabbaaaaaabaababaaaaabbabaabbbbbaaaabbaabbaabbbaabaaaabbabbbbbbaaaabbbbaaaaaaabaaabbaababaaaaaaabaaabababbbbabbababbbbbaaabbabbaaaabbaaabbbabbbaababbaaabbbbbaaaaabaaaabaaaaaaaabaaaabaababbabababbaabababbaabbaabaababbbababbbbbabaaabaabaabbabbbababbbbabaaaaaabbbaaaabaaabbaaabbabbbbaaabbaaaabbaabbbbbbababaabbbabbabababaabaabbaabaaaababbabababbabbaaabbabbaabbbbbaaaaabbbabaabaaabaaababbabaaaabaababbabbaaaaabababbbbaaabaaabbababbabbbbabaaaaaabaaabbabbaabbaabaaabbbbbaaaababaaaababbabaaaabbabaabbabbaababbbaaabaabbabbabbbabbbabbbbbbbbabbabbaababbbabaaaaaaaaaaababbbaabbbabbbaababababaaabbabaababbaabbaaaaabababbbbbaabbabbabaabaaabaaaabbabbbbaabbabbbaababbabbaabbababaaabbaaabbbbaaaabbbbabbbbabbbabbbbaaabbaaabaabaabbaabbabbaaaaababbbaaabbbbbbbbbbaaaabbaaaabbaabaaaaabaabbbbbaabaaabbabbabaababbbbbbbaabaaababbaaabaabbababaaaababbabbabbbbaabbbaaabbbbbaaaabbbababbbabbaabaaabbabaabbabaaaaaaaabababbbababbbbabbbbbabbaaaabbabaabaabbabaaaaabbaababaabbaabaababbaaaaabbaaaabbbbaabaaabbabaaaabbababbaaabababaaaaabaaabaaabbbaababababaabbabbbbbbabbaaaaabbbaabaaaaabbbaabaaabbaaabababbabbbbbbbaaaabbaaabbabbbbbbbbbaabababbbbabbbbabbbbaaaabaababbbabbaabababababbaaababbaaabababaaabaaabaaabaabbbbbbbbbabbbaaabbabbbbabbbababaaaaaabbbbbbbbbbaababbbbaaaababaabbaabbaabaabababbbbabbabbbbbabbabbbabaaabbaababaabababbabaabbbbbaabbbbabbbabaaabbbbaaaaabbbabbbbababbbabaabbaabbabbaabbabbaabbbbabbbbabbaabaaaaaaabaaaabbaaaabbbbbabababbbbaabbbbbbabbbaaaababbbbababbbbabaabaabbaabaabbaaabaabbbbabaaaabbabbabaaaaaababbbbbbabbaabaaababbabbbaaaaabaaaabbaaabbbbaaabaaaabbbaabaababaababababaabaaabbaabaabbaabbbbabbabbbbaaabbabaabbbaabbabaaaaabbaabaabababbbaabbbabaabaaaababaabbbbbabaaaabaabbbbbbbbbabbaaaaabbbbabbbaaabbbbbbaababbaababbbbbbbbbababbabbbabbabaabaaabababbbbababaabbbbabbaabbbabaababbbbbbbabaabaabbbabbbaaabbbabaaaabbbbbbbbabbaababbbbbaabbbababbbbbaaaaabbbbbbbbabbabbbababababaababbbaaabbabbababbabaaaaaabaaabaabbaabaaabaabaabbbabbabaaaaabbbabbabbabbbbababbabbbabbbabaababaaaaaabbaabaaaaaaabbabaabaabbbbaaaaaababaaabbbbababbaabbaaababaaabbaaabaabbbbbbabbbaabaaabbabaabbbbbaaaabaabbbbbabbabaaaaaaaaaabbbbbaabbbbaabbbbbbaabbababbbaabaabbbabbaabaabbbbaaaabbabaabaabbabbabababbbbbbabbbbbbaabbabaababaabaababababbabbaaabbbbaaababbbaaaaaaaaabaabbbbababbbbaaaaabbbbbbbabaaaaaabbabbbababbbbbabbaaaaabbaabbbaabbbaaaaabaaaaaabbaaabbbbbbbbbbbbbababbbaabbbbbabaaababbbbaabaabbbbabaaaaababbbbbbbbbbbaabaaaaabaaabababbbabbaabaabaaaababbababaaaabbabababbbababbabbaababbaabaaabaabaaaababbbbabbbabbbbaaabbbbbbbbaaabaababaabbbbaabababaaaabbaababbaababababbaabababbbbbbaabaabaaaaabbbbaabbaaabaaabbbaababababababbbabababaaaabbaaaaaababaabbaabbaababbbababbaababaaabbbaababbbababbbabbabbbbababbbbbabbabbbbaaabbbbbaaabaaabbabbaabbaababbbaaabbbabababbbbbbabbbaaababaaaaaaaaabaabaaaababbbaababbbbbaabbabaaabbaaaabbabbaababbbaabbbababaababaabbbbbababbaaaaaaabbaaaabaaaaaaaabaaabbbaabbaababaabaabbbaaabbaaaabbbaaabaabbbbaaaabbbbbaaabbaaabbbababaaaaabbbbbaaabbaabbbabbabbbbababaaabbbabbbaaabbbaaabaaabbaaaabaaabbabbabbbaabbaaabbabbbaabbaababbbaababaaabababbaabaabbaabbbabbbaabbabbabbabbababbaabaabbbbaabaabbabbbbbbbbbbaabbababbabaaabaaabbababbabbbbbbaabaabbaababaabababbbbbabaabaabbaaaababaaaabbbbbbaabaabbbbaababbbbabbbaaaaabaababababbabbababaabaabbbbbbaaabbaabbabbabbbaabbbaabbbbaabbbbbbbaabbaabaaaabbabbbaabababbbabaaabbbbbbbbabbbbbbababababaaaaaabaaabaabbbbbabbbbabbbaaabbaaababbaabbbbbaaaaabbbabaaabbaaaabaaababaaaabaabbabbabaabaaaaaaabaababaabaaaaaaabbaaaabaababbbbbbababbbabbaabbbbbbbbabababbbbbbbabbbabababaaabaabbaabbbaababbaabaaabbbaaaabababababbbbbbbbaababbbababbabbbaaaaaabbabbabaabaababbabaabbaabbbaaabbaaababbaaabaabbbbbbbaaabaabbbaabbbabaabaaabaaaabaaabbbbbbbbbaababbaabaaaabbbaabbbbaaababaabaaabaaaaabaababbaabaaaabbbabbababaaaabaaaabbaaaaaababababaaabbaabbabbbaaabaaaaaaaabababbabaaaabbabbabbbbbaaaaaaabbbbbaaabbaabbbbbaabbabbbabbabbbaaaababaaabbbababaaabbbbbbbbbabaabaaabbbaaabababbabbababababbbbbabbbabaaaabbbabbbabbbaaabbaabbaabbabbaabaaabbbbaaaabababbabaaaabbbabaaaaaabbabbaabbaaaaaaaabaaabaaabbbbaabbbbbaabbaabaababaaaabbbbbaabaabbabbbbabbbaabaabaaabbabbbabaabaabbabbabbabaabbababababbababbaaabaaabbbabbaaaaababbbaaaaabbbaaaaabbabaaabaabbaaabbbaaabbabbbaabbabbbabbabbbbbbbbbbaaababaaabbababbbaababbaabbaababbbbababbbabababbabbbabbabababbaaabbaabaaaabbbaaabbababbbaaaaaabaabbabaabaabaaaaabbbabaaabaaaaabbbbabbbbbabbaababbabaabbbabbaababbbbbbaabaabbabbbbbbbaaabbbbabbaaabbababbbabbabbabbabaaaaabbaaaaabbaaaabaaabbbbaabbabaababbbbababbbbbbbaabbbabbaaaabaabbbaabbaaabbabaaaaabbaaabaababbbbbbaabaaabababbabaabaabbbabaabbbbbbbaabaaabbbbaabababbaabaaaababbbabbabbaabbbbaaabaabbbababbbaaaabbaababbababaaabaaaabaaaaaaaaabbbbbabbaaabbabaaabbaaabaabaababaaaabbbaababaabaaaabbbabbbbaaababaaabbbabbabbbababaaababbabaabbbaaabbaabbbabbaabbbabaabbaaababbbabbaaaabbaaababbabbaaabaabbbbbbaaaaaaabababbabaabaaabbabbaaaaabbabababbbbababbbbababbbbaabbababaaaaababbabaaababaaaabbaabbbabbaaaaaabaaabbabbbaabbabbaaabbabbaabbbabbaaaaaabbbabbbaababbbbbaabbaaaaaababbabbbaaaaabababbaaabbbaabbabbaaababbbbbaabbbbbabbbababaaabbabbaabaaabbbbbaabbabbbbabbbbaabbbbbbbbaaabaababaabbaabaaababbaababbbbbabaababbbaabbbabbbbbababaaabbbabbaaaababbabbaaaabababbbbabbbabaabbbbaaabbaaaabaabbabaabbbaabbbabbbbbabbabaabaaabbbaabbaabbbbbbbbababbbabbbbabababbaaababaabbbbbaabbbbbbaaaabababbababaabbaaabaaabbaabbaaabbbbbabbabbabbaaaabababaaababababaaabbabbbaabbaaabababbabbbaaabbbbaabbaabbabbbaaaaabbbbabbbbbabbaaababaabbbbaabbbbaaaabaababbbabbaabbbabbbbbaaaaabbababbbaabbbbaaabbaaabbbaaaaaaabbabbbbbabbbbaababaabbbaaabbabbbbbbaabbabbabbaababbaaababbabbabaabaabbbaaabbabaaaaababbabbabababbabbbbabaaababbaabbaabbaababbabaabbabbbbbabbaabbbbababbbbaaabbbaabaabbababaaaabaababbbbabaaababbbbababbbabbbbbbbbabbaababaabaaaabbbbbbaabbaaabbbababbbbabbbbbabbbbaabababaabbbaababaaaabbabbaaaabaaababbaabbbaababaabaabbabbabaabaabbbbbbaaaababbbbbbbaabbbababababaaabbaabbaabbaaaaaaabbaaabbbaabbababaaabbaaaaabababbabbabbbaabaaaaababbbbbabaaaababababaabaabbbbabbabaababbbabbaababababbaabaabbbbaabbabbbaaabbaaaabbaabbaabbbbabbbbabbbbbabbbaaabaaabbaaabaababbbbbbbbbbbbaabaaabbbbabbabbbbbbaaabbaabbabaaaabaabbbbbabbbababbabbabaabaabaaababbbbbbbbabababaaaabbbaababbaabaaabaababbbaababbbbbabaabaabbbbabaababbabababbaaabaababbababbbabbbbbbabaabababaaabbabbababbbbbabbbbbbbbabaaabbaaaaabaaaaabbaaabaaaaaaabaaabbabbbbbbabbbabbbbbababbbbbabaabaaaabbabbbababbbbbabaabbbaaababaaaaaabbabaabbbbbbbbabbaabaabaabbabbabbbababbbbabaababaababaabbabbbabbbabaaabaaabaababbaabaaababababbbaaabbbabbbbbbbbbabbabbababbbbbbbaaaaabbaabbababbbbabaaabbababaabaaaaaaababbabababbaabaabaaababbbabbbbbabababaabbbabaaabaabaabbaabbbbbbbaabaabbaababbbaaabbbabbaaabaabbbbaaaabbaababbababbbaabaaaabbbabbabaabaababbaababbbbbbababbbbbbaaabaabbbbababaabbbbaabbbabbabaaabbaaaaabbbabbaaaabbabaabbbaaaabbababbabbbaabaabbbbaabbabaaababaabaaaaabaabbabbbabbbaabbababbababaababbbababaaabaabbabaabbbbaaaaaaaababbbbbbbbbbababbbbababbbbbabaaabbbbbabbbabaaaabbbaabaaabaaabbbabbbbaaaabaaabababbbabaaaabbbbbabbbbaabbabaabbabababaabbbababbbabbbabbbbbbbbaaabbabbaabbabbbbbbbbbbbbbbbabaaaababababbbabaaababbabbaaabbabbababbaaaaaaaaabbaabbaababbbabbaabbabaabababaabaaaabbababbbabaabbbaaaaabaababbaabbaabbaaabaaabbbbaaababbbabababababbbbaababbbababaabaaaaaababbbaabbaaaababbabbaabbbbbbbbabbaabbbbbaabaababbaaaaaabaaaabbbabaabbbaaabbbababbbaababbbabbaabaaabbabaaaabaabaaaabbbababaabbaabaabaabaabbaabbbbbbbabababaabbbbababaababaaabbbbaabbbbbbaabbbbaaabbbaabababbaababababbaaaabbbbbabababbbbbbaababaabbbabaabbbabbabbbbbababababbababbbbaaabaaaaaabbbbbbbbbbaaaaaaabbaaabaaaababbaaaaabbbaaaaababbaaabbbbbaaabbaabbababaabbaabaaaaabbaaabbbaaababbabaaabbabbabbabbbabbababbbaabaaaaaaaababbbbbaabbaabbaaaaaaabbbbbbbabbabbabbbababbbababbaabbbabbbaaabbbbaabaabaaaaabaaaaabbbaaababbbababbbabbabaabaabaaaabababaabbbabbbababbaaabaababbbaaabaaaabaaaabbbbaabbabababababababababbabbaabbaaabbbbaabbbabbbbbbaaaaabbbaabbbbababbbabaaaaabaabbabbaabbaabaaababaaaabaababbbaabbabbaaaaabaaabaababbababbababbaaabbbbbababbbbaabbaabaabbabbbbbaabbbaababbbaaabababbaabbabaaabaabbaaaabaaaabababbbbbbbbbaabbaaaaabbabaabbbbaaabbaaaaaaabaabbbabbabbbbbbaaabbaaabbbababbabbbababbbbaaabaabaabbbaabaababbaabbaaababbbabbaabaabaababbabababaabbabbaabbaabaabbbbbaabbbabbabbbabbbbaabbbbaaabaaabbaaaaababbbababbbabbbbabaabbaabababaaabbbbaaaaabbbbabbbabbbbbaabbaabbbbaaaabbbbbbbbabbbaabaaabbbbabaabaababbbbabaabaabaabbbbbbbbaaabbbaabbaaababbabbaaabbabbbbbabbaababaabbaabbaabbabbaabbabbaabaaaabbaaaaaaabbbbaaaabaaabbbbbaabaababbbaabbababbbbabbabbbaaaaabbaabbababbaaaaaababbabbaababaaaaabaabbababbbbbaabbaaaabaaabaabbbbbaabbabbbabaaabaabababbbaaaaabbbbbbbabbaaaaaaaababbaaaaaabababaaabbbbaaabbabaaabbabaaaabbaaaaaabababaaaabbaaabbbabbbbabbbabbaabbbabaabbbbbbaababaabbaabaabbbbbabbbababbabaaabbabbbbababaaaaaaaaaabbbbaaaabbabaabbaaaaaaaaaabbaaabbabbbbbababbbbbbbbbbaabbbbbbabaabbbaaaabbaabbaaaaaaabaabbaaabbabbbababbbbabbabbbbabaaaaaabaabbaaaaaaabbabababbabbabbbbbbabaaabbabbbabbaaabbbaabbbbbabaabbbbababababbabbbbbbabbbbbabbbabbabaaabbababbabbabaaaaaabaabbabbaaababbaabbbaaabbabaabbbabaaaabaabbbabbaaababaaaabbabbbabaaaaaababaabbaabbbbabbaababaababaabaaaaabbaaaaaabbbbabbbaaababbababaaababaababaabbabbbabbabbaabbaabbbbabbaaaaaaababaaaabaabbbbbaabaabbbaababbbaaabbaaaabbabbaababbaabbbaabbabaaaabbabbbbaaabaaabbabbaabaabababaaaabaaaaabbaaababbaaabaaabaaababaabbbabbabaabaabbaaababbbbababbaaababbabbbabbbbabaaabbabbaaabbbababbababababbaabbbbbababababaababbbabaaaabbaabaaaabababaabbabbaabaaaababaaaabaabbbababbaaabaaaabaabbbbababaaaaabaabababbaaaaabbbababbbaababbabbaaaababaaaaabaabaaaaaabaaabbbabababababaabbbabaaabbaabbbbabaabaaabababbbaabbaabbaabbababaaaaabbbbabaabbabbabbbbaabbbaaaabbbaabaabbabbababbaaaaaaabbaabaaaaaabbabbabaabaaabbaaabbababbabbbbbbabbaabbaabababbaabaaabaabbbbbbbabbaaaabaaaaaabbabbbbaaabbaabbbbaaaabababaababbabaaaabbabbbbaabbbbbbbbbbbaabbaabbbaabbbbbaaababbbaabbabaabbaabbbbabbbabaaabbaabababbabaabaabbbaaaaabaabbbaabbaabbbaabbababbababbababbbaaaabaabbbabbaaaabaaabbaaaabbbbbbaabaaaabbababbaabaababbababaaaaaabbabbbbbababbabaababaabababaababbbaabaaaabbabbaaababbaabaaababbbaaabbaabbbbbbabbbbbbbbabaabaabbbbbabababbabaaabaaabaaaabbbababaababbabbaaaabababababaabbbababbabbbbbabaabbabbaabaaababbabbbbaabbabbabbbbababaaabbababbaabaaaabbbbabbbabaaababaabbaaaabbbaaaabababaabbbabababbbbbbaababbababbaabbbbaaaabaabbaaaabbbabbaaabaaaabbababaaaaaaabaabbaaabbaabaabbbaaabbbbbaababbbbbbbababbaababaabbbaababbbabbbbbbbaabaaaabbabaabaababaaaaabbaaaaabaabababaaaaaaabbabaabababbbaaaabababbbbbbbaababbbabaaaaaababbabbabaabaaaaaaaabbaabaabaabbbababbbbababababbaabaaaabbbbabbababaaabbaaabbbababbbbabbbaabbbbbaabaaaaaaabbaaabbbbbaaabbbababbbabbbaabbabaaaababbaaabbbbbababbaaaabbbbaaaaabaabbbbaaabbbababaaabbbbababaaaaaaaaababbaaabaababbabbabbaababababbababbbaaaaaabaabaabaaababaaaababbababbbababbbabbaabbbbbaabbbbaabbbbbabaaaaabbbabababaaabbbaabbbaaaaababbbaaaaaaabbbbaababbbbbababbabbababbabbaaabbabaaaabbbbabbaabbabbaaabbbababbbbaaabbaaaabbbbaaabbbabbbababbababaaaaabaaabaaabbabbbbabababbbbbbbabaabbbbbababbaaaabbabbabaabbabbababbbabbababbaaaaabbaabaaabaabbbabbbabababaabaabbbbbbabbaababbaaabbababbabbaaaabaabbabbbabbaaaabababbbaabbbaaababaabaaaabbabbbaaaabaaabaabbbaaaaabbbbbbbbaaaaabbbababbbaaabbaaabaababbaabbaaaabababbbaabaaabaaabbbbaaaabbbaaabbbbbaabbaabbbaaabbbabababbbbbbaaaaaabababbabaabbbbababbabbbababbbababbbbabbbbabaaaaabbaaaaaababaabbabbaabaababbaaabbaaaabbbabbbbaabbabaabaababaababaaabbbbbaabbabbabaabaabbabbbabbabababbbbbaabbbaabaaaaabbbbabaaaabbabbbaabaabbabaababbbabaaabababbbaabbababbaaabaaaabbaaabaaaabbbbbabbbabbbbbaaabaabbabababababbaaaaaabaaaabaabbbababbabababbbaabbabbabbaababbabbbbaaabbabbababbbaabbabbbbaabbbabaabbababbaababbaaabaababaaaabbaaabbbbabbbbaaabbbbaabaaabaabababbbaabababbaaabbbbbabbbabbabbbabbabbabaaababbbbaabbabaaaabbbaaabbaaaaababaababbaabbbababababbbbaaaaaabbaabaaaabaaaaaaabbabbbbaababbbbababbaaaabbbaabbbbaaaabaababaabbbbbaaabbbbaabaaaababababbbaabbaaaabaabbabbabbababbaababaabbbbbbbbbababbbaaaabbabaaabbaaabbababbbbbbbbbbaababbabbbababbaaabaaaaabbbbbaaaabaaaabbabaababbaabbbabbaabbbaaaabbabaababaaabbbababbababaabaaabaaabaaaabbbaabbbbabaabbaaaaaaabbaaaaabbbabbbaabbbaabababbaaabbaaabbaabbbabbaabbaaabbababababbbbabbaaaabaabaaaabaaabbbaabaaaaabbbabbaaabaaabaababaaaaaababbbabaaabbaababbbabaabaabbabaababbaaabaaabbbbaabbbbbabaabbbbaababaaaabbbbbbaababaababaaabbbaaaaaaaaaababbbaababbababbbabbababababaaabbaaabbaababbbbabbbbbbbababaaabbbbabbabbabbbaaaabbbbbbaabbbbbaaabbbabbaabbaaaabaabbbbbbbabaababbababbbababababbababbbaababbbabbabaababbabbaabababbaabaabaabbaaabbaaababbbbbbababaaababbbabbbababbbbabbaabbaaabbbaabaaaaaaabbaababaababaabbabbbbbaaaabbbabbababbbbbabbababaaaaabbabbababaaabaabaabaaaaabbabbabababbababbbaaaababbaaaaaaabbaaababaababbbbaaaabbbaabaababaaaabbababaaabbaaabaaabbaaaababbaabbbbbbabbbaabaaaaaabbbaaabbaabbabaaaabaaabbababaaababababbbbaabbbaabbbababbaababbbabbbabbabaabbababababbbbbaabaaabbbaababbbbabbbaabbabaaabbbbbabbbabbbbabaabbabbbbaabaaabbbaabaaaabaabbaabbaabababaaabbbbaaaaaabbbababbabaaabbbbababbabaaabbbaaaaaaabbababbbbabaaabababbaaaaabbbaababbaaababbababaabbbbbabaaababbbbbbbaaaaaaabbaaabbababbabbbaaaaababbaaaaabaabbaabaabaaabaaaaababbabbaaabbabbbbaabbabbabbabbbbabaabbabababbabbbbaababbabbbabbabbbabaaaaaababbbabaaaaabbbbbaaaababbbbababbbbbabbbbbbbbabbaaabbabaaaaabbaabbabaabbaabbaababbabaabaaababbbbbaaababababbaabbaabbabbababbabbaaaabababbbbabbbaaabababaaaabbbabbaabbbabaabbaaaabbaababababbbababbbbababaaaabaaabbbabbaabbbaabbabbbabbbaaabaaabaaaabaabababbbabbaaaabbaababababaabaabbaabbaababbaaabaababbbbabaaaaaabbbaabbaaabaabbbbaaabbaabaabbaababaabbbabbbabbbabaaababaababaababaaabbbbbabbbbbaabbbaaabaabbbabaabababbabababaaaabbbaabbbabaaabbabbbaabbbaabaaaaabbbabbabaababaaaababbbbaaaaaabaaaabbabbbbabbbaabaabbbabaabbbabababbbbbbbbaabaabbaaaaabbbbabbbaabbabaaabaaaabbabaabbbbbbaaaabbbababaabaabbbaaaaaaaabbbbabbbabbbaaabbbbabaabbaaabbababaabaabbaabaababbabbbabbbababbbbabbbbabbbaabbbabbaababbabbabbaabaabbaababbababababaaabbaabaabaaabababbabaabbaaababbbaaababbababaabbabaaabbbaaaabaababbbabaabbaaaabbbaababaabbbbaababbbbbaabbaaaaaabababaaabbababbaaabbaabababaabbbbbbabbbbbaabaabbaaabbaabbbbabbbaabbabaaaabbbababaabaabaaaabbabbbabbaabbbbbabaaabbabbabbbbbaabaaabbabababbaaabbbaabaabbbbbaaaaabbaaaabbbabbbaabaabbabbabbabbaabababbbbbbbbabaabaabbbabbbabbababbbaabbabaabbbaabaaaabbaabbaabbbabaabbaabaabbbabaaabbaaabbbaaaabbbbbaaaabbbabbbbaaababbbbbbbbababbaabbabbabbbbbabaaaabaabbaabbbbbabbabaaaaaabaababbabbabbaabbabbbabbbbaaabbbaabbbaabbbaaaababbbbbaaabaababbbbbabbbaabbaabbbbaaabbbbbbaabaaaabaaabaaabbabbaaabbaababaaabbaababbabbaaaabababbbabbaaabaaaaabbaabababbaabaaaabaabbbbaaaaabbaabbbaaabbabbbaaabbabbbabbbbbbababbbbaaabaaababbbabbaababbbbbabbabababbbbbabbaaaaaabbababbbababaaababbaabbabbbbaaabbbababaaaabababaaababbbababaabaaaabbabbbbabababababbbbbbaaabaaabaaabaababaaababbababbababbabbbabaabbabaaaaabaaabaaaabaaaabbabbaababbaababaabaabaabaabbaababbaaaabaaaaaaaaabbbaabaabaababbaaaabbababaababaaabababaabbbbbaaabaabbbbaaaaaaabbaabababaabbbabbaaabbaaabaaababaaababbabbbbabaabbaaaaaababbbaaababbaaabbabababbbaaaabaaaabaabbabbaaaaababaaabbaabbbbabaabbbbbbbaaabbaabbbaabbbbbbbbbbaaaabbabaabaaababbabbbaabababbaaaabaabbabbbaaaabaaaaaaaaabaaabaabbbbbababbbbababbbbaaabbbaabababbbbbbbaabbaaabbbaabbaabaabababaabbababbbabaaaabbabaabbbaabbababbaaabbabbaabaaaaaaabbbbabaaabbbababbabbaababaabababbaaaabbabababbbaabbbaabbaaabaaabbabbbbbbaababaaabbbaaabbabbbaabbbaabbabbaababbbabbaabbbaaaaabbabaaabaaabaaabaaaabbabaabbabbbbbabaababbabbbbbbaaabbababaaaabaabbbaababaabaaaabaabaabbaaababbbbbbaabbbbbbaaabbaaaaabaabbbbababbaaabaaabbabbaaabbababbabaaabbababbbaabaabbbabbbabbbbabbbbbbbbbabababbbababbbababbbabbbabbaabaaababaabbbbaaaabbaabaaabbbbbbbaaabababbbbaabababbababaaabaabbabbaabbabaaabbabaababbababaaabbbbbbaabbbaaaabaaabaababbbbabaaabbaaababbbbabaaaaabbbabaabaabbabaabbaaaabbaabbbbabbababbaaaababbbababbabbbbaaabbaaabaabababbbaaaaaaabbbbbaaababaaaabbbbaabaaaabaaababbbabbaaabaaabbabbabbbabaaaababaaaababaababaaaababbaaaaababbaababaaabbaabbbbaababbbaabaaababaaaabbbaabbbbbbaabbbbabbbabbaababbbbaabbbabaaaabaaabbbbbbaabaababababaaababbabbbbaababaaabaabbaabaaaaabbbbbbabbababaabaaabaabbaaabaaaaabbbaabbababbababaaababaaabbabbababaaaaababaabbbbbababaaaabaabbabababaabbaabaabababbbbaabbabaaaabbbbbbaaababbbbababbbbbabbaabbbbabbaababababbbbaaabbababbbbabaaabbbaaaababaababaaaabbababbabbbbbbaabaaaabaabbbbbbbbbababbbabbabbbbbabaabbababbaabbaabbbbbaabbbbbabaababaabaaaabbabbaabaabaabbababbababaaaabababaaaababababaabbbbaaaabbbabaababbabaaaaaaabbbbaaababbaaaabbbbabbbbaaaaaabaaaabbababaababbabbaabbbaaabbbbaaaaabababaabaaabbbaaabaaaaaaababababababbbaabbaabaaabbabbaabbbbaaaabbbababaabbbbbbbabbabaaabbaaaaaaabbababababbbbababbbababaaabbbabbbbababbbbbbbbabbbbababbabbbbaabaaaaababbabababaabbbaaabaabaabbaabaabbbbabbbbabaabababbabbaaaabaaaababbabababababaabbabbaabaabaaaabbaaabababbaababbabaaaaabbaaabaabaaabbbbbbabaababbbaabbaaabaababaaaaaaabbbaaabaaabbbbbabaaabbabbaabaabbaaababbbaabbbaabaaabaababaabbbbbabaaaaaaaaaaaabbaabbababaababbbbbaabbabaabbaabbaababaabbaaabbabbabbbbabbbabbabaaaababaabbabbabababbaaaaaaababbbabbbbabababbaabbbabababbbabbabbabbbabbbbbbababbaaabbaababaaaaabbbaaabababbabbaaabbabbaaaabaababaabbbaaabaaaabaabbabbaabaaaabbbbbabbabbbabaaabbbbbbababababaaaababababbaaabaaaaabbabbababbbbbbbbabbbaabbababaaaaaabaabbbbaaaaabbabbbaababbbaaababababbabbaaabbabbbaabbbababaabbbbbabbbabaaaaabbaaababbaabaaabababaabaaaaabbabbaabbabbaaaaaaabbaabbbaabbbbbbbaaaaabbabbaabbbaabbabbbaaabababaabababbaabbabbbaabababbbabbbbaabbbaabbabbabbababbbaabbaababbbbbaabaaabbbbbababaaaaaaaaaabaabbaabbbbbbbbaababbbabbabababaaaabbbaabbababbbbaabbbaabbababbbabaaaabaaabbbbaababbbaababbbbbaabbbaabbbababbbabbabbbaaaabbbbbaaaaaaabaaabbbbbbbaabbabbaaaaaaaabbbbabbaabaabbbaabababaabbbabababaaaaabbaaabbbaaaaaaabbbbabaababaababbbbbbaaaaabbaabbaabbbaabaaabbbbabbbbbaaaaaaababaaaabbaaaaaaaaaaabbabbaaaabbaabbbbababaabababababbabaaaaaaabbbaabbaaaabaaaaaabbabaaabbbaabbbbbbbabbaabbaababbbbabaabbbababbaabababbbbabbabababbababbbbabbbbaabbabbbbbaabaabbbabbabbbabbbaabaaaabbabbabaaabbbbbbabaaababbababbababaabababaabbbbbbbbbabbabaaaababbbbabbaaabbbaabaabbbaabaaaaaabbbaabbbaabaaaabbaaaabbabababbababbbababbabbaaaabbbaaabaaaabbababbababbabababbbaabaaaabbbaaabbaaaabbbbbbbbbaabaaaabababbbababaaaabaaaaaaabbaabbbbbabbaaaaabbababbbbbabbbabbbbbaaabbaaaaaababaaaaaaaaabbaaaaaababaaabaaabbabaaaaabbbbabbaabbbbabbbbaaaaaaabbaabbbaabbbbabbbbaaabbaabbbbbaaababbbbabaaaaaabaababaabbbaaabbbbbabbbbbbbbbbabaaabbbaabaaabbabaaaabababbaabaaaabaabbabbbaaaababaaaabbaabaaabbabbabbaaabbaabbaabbbaabbabbabbabbabbabbababaabaabaababbbbabbabaababaabbbbbaabaababbabbaaabbbabaaababaababbbabbbabaabbbaaaaabaaabaaaabbababbbaaabaabaabbbbaaaababaaabaababbabababbaaababaaabaaaaabbababbaababbbbabaaaabbabbbaaabbabbbbbbabbaaababaababbbbaababbbbabbabaabbabbabbbbabbbabbaabbbabbabbbbbaabbbabaaabababbabbaaaaabbabbbababbbababbbbaaabbabbabaababbabbaabbababbbbabbaaabbbaaabaaabbbbbabbbbaaabaaabaaaaaaabbbbaabbbaaabaabbbbaaabaaabaaaaabbbbbbbbababbabbaaaaabababaaaaaabbbbbbaaabbbbaaabaabbaababbbbaaaaaabbabbbaaabbbbababaababaabbabbbabbaaabaaabbabbbbbabaabbbaabbaabbbabaabbbbaabbbaabaaaaabbaaabbaaababbaababbabbbbaabbabaababababbaababbbabbababaabaabbbbbbaabbaabbaababaabaabaaaabbaabbbabbbbabababbababaabaaabaabaabbabbababababbbaabababbabbbbbaaabaaabaabbbababbbaaaababbbbaabababbbabaabaaabbaaaababababbababababaaaaaaaaababababaabbbbbbbbabbaabbabababababbaaaaaabbababbabbbbbabbaaababbaaabaabbaababbaabbabbaaabaaaababbababaabbbaaaaaabbabbbaaaaabbabbaababaabaabaabbbaaabbbbabababbbaaabaaaabbbbbbaaababbabaabbbbababbabaabaaabbaaabaababababbbbbaaababbbbaabaabbbaabbbbaabaabbbbbabbaaaaaabaababbbabbaaaaaaaabbbbbbaabbaaabbbbbbabaabbabaaabaaabbbaabbaaaabaaabaabbbaaabaaabbaabbaaaabbababbbbbbbabbaabaaaaabbbababbbbbbbbaababaababbbbbababbabbbaaaabbbabbbaabbaaababbaaaabbbbbbaababaaabbabbbbbbbababbbbbbbabbabbabbbbaaabaaababbbbbaabababababbaabbbaaaabbbbaababaaabaaababbbbaaabbabbbbaaabbaaababaabbbaaaaaababbbaaabbbaabaabaaabbabbbaabaababbaabbaabbaabbaaaaaaaaabaaababbbbabbabbaaabbaabababaabaaaababbbbbbbbabbbaabbabbaabaaababbabbaaaaaabababbabaabaabbbbabaaabbbaaabbaaaaababbaabababbaabaaaaabbbaabbbbaaaaabbbaaabbbabbbaaaaabbabbbbaababaaaabbaaabaababbaaaaabbbaabaabbabaabbabbbaaaaababaabbbababbaaaaabbaabbaababbaabbabbbbababbbabaaaaabaaabbbabaaaaaabbbaaaaaaaababaaaababbbbaabbaababbaaaaabaabbaababbbbaabbbbababbbbbbaabaabbbbbbbbabbaabbaabababaaabaabbabbaaaabbbaaabaababaababbababababbbbbababbaaabaaabbbbbaabbbaabaaababbabbaaababaaaababbaaabaabaabaababbabbbabaabbbbbabaabbbaaabaaaababababbbababaaaaabbbababaaaabbababaaaababbbbabaaaaaabbabaabbbaababbaaabbabbaaababbbaabbbbbbbabbabbabaabbbbbbaaabbbabaabbaabbbbbbbbaabbabaabbbaaaaabaabbbabbbbbbaabaababbbbabbbbbbaabbbaaaaaaabbbababbbbabbbabbabbbabbabbabbabbbaabaaaabbbaaaaababbbaabbbaababbabaabbaababbbabbabaaaaaabaaaababababaababbaabbabbbbabaaabbbbaaababaabaabbababaaaababbbbbbbabaabbbabaabaabbaabbabbbababaabaabaaabaabbaabaabbbaaabbababbbaabbaabbbaaaabbbaabaababbbababbbaababbbababbbababbaabaaabbaaabbbaabbaaaabbaaabbbbbabbbbbababbabaaabbabbababbbaabbaabbbaabbbaababbbabbbaabbabaaabbaaaabbabbbabbbbbbaaabbbbbabbabbbaabaaabaabbaabbbabbbabbababababababbaaabbbbbababbabbbbbbabbabbbabbabababbaaababbaaababaaaaabbbabbbabbaaabaabbbaabbaaababbaabbaababababbbbabbbbaabbbaababbaaaabbababbabaaaabbaabaabaabaabababbbbabbbaababbabaaababaabababbabbbbabbabbabbbbbbbbababbabaaabbbabaabbbbbabaabaaaabbababbbbbaabaabaaabaabbbbbaaababbbbabbabaaaabbabaaabbbaaaabaabaaabbbbbaababbbababbbbbbababaaababbababbbbbbbabbbaabbbabaababbbaaaaaaaaaababbaabaabbbbbaababbabaaaabbbaaabaaababbbbaabaaabababbbabbababbaabbbbababaababbababbaaabbbaaabbbbaaabbabbababbbabababaabbaaaaaaabbbbbbbbbaaaababbbaabbbbabbabbbbbaabbaaabaababbbabbbaabbbabaababbaaaaababbbabbababbabbababaabababaabbabaaaabbbaabbababaababbaabababbbbabbaabbaababbaaaaabaaaaaabbaabababaaaaabaabbbaababababaaabaaababbababbabbbbaabbbaaabaababaaabbbaaabaabbbbaabbabaabaabbabbbbabaaabaabbbaaaaababbbbbbbabbbabbbaaabbabbaaaaabbabaaaaabbaaaaaabbbbbbbaabaabbbbbbbbabaaaabababaaabaaabaaabaababbaabbaababbbbaaabbbbaaabaaabbabbababababaaaaaaaaababaabbaabaaaabbbabbbaababababaabaaababbbaabbaaabbbbaaaabababaabaaabbabaaaababbabbaaaabbbaaaaaaaaaaaaaaaabbaabaabbbbbaabaaaaabaaababbabbbbbbaaaabbaabaaaaabbabbbbbababaabbabbabbabbabbaaabaabaabbbbbabbaabbababbabaabbaaaaabbababbaaabaaaaaaabbaaabbbbbbbbabaaaabbbbbabbbaaaabbaaaaababaaabbaabbbbbaaaabaaabbbaabbaaaababbaabaabbaaaabbaaaabbaabaabbabbababaabaaaaaaabababaabaaababbabbbbbbabbbbabbabbbabbbabaababaaababbbbbbaaaabbbababbbabbbaaaaaabaabbbaabbbababbbbbbbbabaabbabbbbbbaaabbbbbababbabbabbaabbaabaaabbaaaabaaababaaabbbbbbababbaabbaaababbabbbaabababbbbbaaaaaaaaabaabaabaababbaabaaaababaabbabaabbbbaaaabaabbbbbbbabbaaabbbabbaababbabaabbbaabbbbaabaaaaabbbabaabaabbabaabbabababaabbbabbbbbbabbbbbbabaabbbbaabaabbbaaaaababbaaabbbabaaababbbaabbabbaaabaabaabbbbbabbbabaabbabababbbabbbbbbbbabbaaaaabaaaaaaabbabbbaaaababbbbaabaabbbbbbbaabbbaabaabaabababbaaabababaabaaabaaabbbbbabbabaaaaabababababbbaababbabaaabbbbaabababbbbabbabaaaaaabbabbbbaabaaaaabbbbbaaaaababbbbbbaabbbaababaaaabaaaaabbabbabbabbabbbababbbbbbbaaaaaabaabaabbaaaabbbabaaaabbbbaaabaaabbbaababbbabbaabbbbbabbaaababaababbbbabaababaabaaabababbaaabbbaaababaaaabaabaabbbbaabaabaaabbbbababaaaababbaaabbaaabbaabbbbbabbbaaaabbbabbaaaaaabababbbaabaabbabbabbbbabbbbbbabbbbbabbabbbababbabaabbbababaaababbbbbabbbbaabbbaaaabbaabbbaababbbabaaaabbbaaababbbaabbaaabbabbaabbbabbbbbaaaaaaaaabbabbabbabbbbbabbbbaaabbbabbababaabababbaabbbbbbbbbaabaababababaabaaaaabababaaaaaaabbababbabbaabaababaaaabbababbbaaaabaaaabbbaabbbbbbabbbbabbabaabbbaaababababbababbbaabaaaaaabaaabaaaaabbabbbaaabbabbabbbabbbbabbabaabaabbbbbaabbbbabbbaaaaabbbaaabbabbbabbbbababbaabbbaabababbabbbabbbaabbabbbbababbbbbbabbabaabababbbbbababbbbabbbabbbabbabbabbaababbbbaaabaaabbabaaabababababbbaaababbabbabaaabbabbababaaaaabbabaaaabbbbbbbbaaabaaabbbabbaaaabbbbbaababbbbbbabbbbbbbbabaabbbaabaaaabababbbabaaaaaaabaabbbbaaaabbabaaabbaaaaaaabbaaababbbbaaaabaaababbabababababbaaaabaabaaabbabaaababaabaaabaabbabbbbbaabaababaabbbbababaaaaaaaabbaabbbabbaaabbbaababbbbaaabaabbababbaaaaaaaababbbbaaaabbbaababbbaaabaabbbabbbbabbbbaabababaabbbabbabaabaaabaabbaaabaabbaabbbabaabbbbbbbbaaaabbaabbbbbbaabbbababaaaabbbabababbbbbbaaababbbaabaabbbbbbaaaabbbaabbbbbbaaaabbabaaabbbaaabbbbbabbbbabaaabaabbbaaaaaabaabbaababaabaaababbbbbbaaaabababbabbaababbbabbababaaaabbbaabaababbaaaabbaaaaaaabbbabbbabbbbbaaabbbaabababbabbababbabbababbbabbabaaaabbabbbaaaaabbaabbababbabaabbaaaaabbaabbbbbbabbbabaabbbabbbabaaabbbaaabaaabbaababbbbabbbbabbabbababbabaaababaabbbbbbabaabaabaaabbbaabbaaabaabbbbbbbaabbababaabbbbbbababbbbaabaaabbaabaaabbaaaaaabaaababaababaabbabbbbababbaabababbabbabbababaaabbbababbbabbaabaabaaababababbbbababaaabaaabaababaabaaaaaaabbbabaaaaabaabbbbbabbabbbaabaabaabaababbaaaaaabbbabbabababaabbabbaabbaabbaabbbbabbaababbabaaababaabaababaaabbabbbaabaaabaabbaabbbababababaabbbaabbabbbaaaabbbabaabbabbabbabaaabbabababbbbababbaaabaaaabbabbbaaabbbabbabbaabbaabbaaabbbaaabbaaababaaababbaabbaabaaaaabbbbbbababbaabababaaaababaaabbababaaaaaabaaabbabbabaabbbabaababbaaabbabaaabbbaaaaaababbaaaabbaaaaabbabbabbbaaabaabaaabbaabbbaaabbbabbaaabaaababbaaaaaabaaaaaaabbabaababaaaaabaabaaaaaaabbabbbaaabaabbbbbababaaabbabaaababbaabbaabbabbbababbabaabbaaabbbbbabbbbbbaabbbaabaaaabbaaabbbaabaaabaaaabbbabbbbabbbbbbbaabaabbbabababbbababbabbabaabbbbababaaababbbbbbabbbababbaababaabaaaabaabbbbaaababbaaaabaaaaabbabaaababbaabbaaaabaabbbabbbabbaabbabbbabaababbaabababbaabbbbbabbbaaaabbabaaabbaaabaababbabbabababbabbabbbabababaabbbbaababbaabbbabaabaaabbababababbababbbbbabbbabbaabbbbabaabbbbabaabbbbbaabbabaaaabbaaaababbaabaabaabbbbbaaaabbababbbabbbaabbaaaabbabaababbababbbbababaaabaabbbbbbbabbbbabaaaaaababaaabbbaabbbabaaabbbaaabbbaabababbaabbaaababaaaabababbbaaaaaabbbbaabbbabbbbbaaabaaabaaabbaabbbaababaaabaabababaabbaabbababbaaaabbbaaaababaabbbabbbbbbabbbaaaababbbbbbbbbabbbbaaabbabaaabbbabbbaabaaaabbabbbbaaababaabbabaaaababbabaaabbabbabbbbaaabbababbbababbbbbbbabababbbbaabaabbbabbbababbaabbbbbabaabbbabaaaabaaaaaaabbbbbbaaaaaaaaabaaabaaababbaabaaaaababbbbaaabbaabbbabbabababbaabbababbaaababbbbbbaabbbbbaaaaabbbbbbaabbaaaaaabbbababbbaababbbaaabbaabbbaabaaaaaababbaaabbbabaaabbabababbaaabababaaaababaabbbbbaaaaabababbabbaaababbabaabbbabbbbbabbbabbbaaabbabbbaaaaaababbabababbaaaaaaabaaaabbaaaaababbbabaabbbaaaabbbaaabbaabaaaabbabbbababaabbbbbaaaaabbaaaaaaabaababbaabbbabaabbbbabbbaaabaabbaabaabbbaaababbabbbbbabbbbabaabbaabbbabbaaababbbabbbbaababbbabbaabbbaaaaaabbababbbbbabaaaaaaaaabababaabbbaabbaabbbaabbbaaabbaabbaabbabbabaaaaabaaaabbaaabbaabbabbbaaaabbbaaabaababaaabbaaabbbaabaababbaaaababaababaabababbbaaaaabbbbbabababbbaaaabaaaaaabbbababaabbbbaaaabbbbbaabbaaaaaaaabbaabbbbbbbbbaabbbababbaabbabbaabaababbbbbabababbbbbabaabbbaaabbbababbaaaabababaaaababbabbbbbbbbababbaaabbabbaaabababbbababaabaaaaaaaaababbaababbbaaabbabbbaabaaaaaababaaabbaabbbabaabbabbbbababaaaaaababbabbaaabbaabaaaaaaaabbbaaabababbbbabbbababbaabbabaaaaaabababbaaaabbaaaaabaabbbbaabaababababbbabaaabbbbbaababaaaabababbaabaabbaaaaaaabbbaaaaaabbaaabbaaaaabbbbbbbaaababbbababbbaabbaabbbbaaabbabbbbaabaabbaaabbabaaaabaaabaaababaabbabbbabaaaaaaabaababbaabbabbbbbbaaabbbaaaaaaabbaabbbabaaabbaabbabaaabaaaaaaaabbabbbaaaaabababbbbbabbbaaaaabbbbaaaaabababbbabaababababbaaababbaabbbaaaaabaabababaababbbabbbaabbaaaaaabbaaabbbbbbababaaabbaaabaabaaaabbabaaababbbbbbbbbbbababbabbaaabbbabbababbabbbbbbabaaaabbabbbababbbabbbbaaabbaaaaababbbbababbbbbaaabbaaabbbbbbbaaaabbbabaabbbaabababaababbaabaaababbbbbaaaababaaabbbbabaaababbabababbaaaababaaabaaaaaabaaaabaaaabaabbaaaaaabbabaabaabababbbbabbbabbaababbbbabbbabbbabbbbaaabbbbaabbbaababaababbaaabaaababababbbbbbbbaaaabaaaaaaaabaaaaaaabbbababbaabbbaaabbbaaabaababbabbaaaabaaabbabbaaabbaaabbbabbababbaabaaabbbababbabbaabaabbaabbbbaabbbbbabbabbbbbaaabbbaaabbbbbaaabbaaaaabbbbbbaaabbaabbabaaababbabababaaaabbababbaabaaaaaabbabaaabbababaaabbaaabbabbaaaabbbabbbaaaaababbaaaaaaaaaabbabaabbbbaaaababbbbbbabaaabbbbbaabbbbbababbaabbbbaaaabaabbbababbabaaababaaabbabbbabbababababaabababaaaabbbbbaaababaaaaababaabaabbaaaababaaaabbbaaaaaaaabaaaabbbaaababbbaaaaabaaaababababbbabbbaabaaabbaabaaababaabbbaaaaaabaaaaabbaabbbbaabbabbbbabbabaaaaabbabbaabaabbabbaababbaaabbabbbbbbbbbbaabababaaabbbabbaaabbbbabbbbbbabaaaaaaaaaaababababbabbbaaaabbbbabbaabaababbbbaababaaaaaaabaabbbaabbaabaabaaababbaababaabbbbbbabbaaabaabaaaabbbbabbababaaabaabbaabababbbbbaaaaabbaaaabbbaabaaaaaaaababbaaabaaaaabbbabbbbbaaaaaabbababaaaaababbbbbbaaabbaaaabbaabaaaabbbbbabaaabbbaaabaaaaabbbaabbaaabbabbaaabaabbbaababbbbaabbababbbabbaaabbbababbbbbbbbbbbaaababaabababaaaababbbbababaabaabaaabbbababaaaaaaabbaaaababaabaabbaabbabbbabaabbabbbbbbabbabbaabbabbabbbbabbabaabbabaaaabbaaabababbaabbaaaabbbbabbbaabaaaaaaaabbbbbbbbabbbaaabbbaaabbaaabbbbbabbaaabbabbababaaabaaababbbbbaabbaabbaabbbabaaabbbaaaabbabbabbaaababbbbbbaaabbabbbaaaaaaaaaababaabbaabaabbaababaabaaabbbabbababbabbababbbababbababaabbbabbabbbbbbaabbaababbaaaabbbaabbbbaabbbbbbabbbababbbbbbbaaaabbbbbbabaaaaabbbbaaabaabbabaaabaaaaaaaaaaababaabbbbabababbbabaaabbbbbabbbbbbaabaabbbbaaabaabbbabaabaaabbbababaaabaabbaabbbbaaaaababababbabaabbbaabaababbbbbaabaabababbabbaabbbabaaaaaaaabbabbaaaaaaaabbaabbbabbbaabbbabababbbababbbaababaaaaaababbaaaabbaaaaaaaaaaabbbbaabbbbbbbabbbababbaaabbbaaabaaabaabbaabaaaaabbbabbbbbbabbabbabbaaaabaaabbaaaaaaabbbbabababbbbbaababaaaababbabaaaababbbbbaaaabbaaaabbbbaaabbbabaabaaaaaaaabbbbaaabbabaaabaabbbbabbbaaabaabaababaaababaaaaaaabaabbaabaaabaaaabbbbaaabaaaabaaaaaababaaaaabbabbaaababaaababbbbaabbbbbbabaababbbbbaabbbaababaaabaabbaaaabaaaaabaababbabbbabbaabaabbaabbaabbbbababbbbbbbbaabbbaababbabbbaabbabaaaaaaabbbbaaaababbabaaaaababbbabbbabbbabbbaaaaaaaaabaaaababbbaabababbabbaababbaaabbbbbbbabbbbbabaaabbbabbbbbaabbabbaaababbbababaabbaaabbbbbababaabaaabababbbbbaabbbabbbaababbbabaaaababbbaabbaaabbabbababababaabbabaaabaabbabaaabbabbbaaaaaabaaaaabbabbbbabbababbbabbaababbbaaabbbbbaaababaaaaabbbababaaabaabaaaaabbbaaaabbbbbabbabaaabbbabbbbbaaaabaabbbaaabbbbbaaabbaaaaabbbbabaabaaabbaaaababababbbbaaaaababaabaababaaabaabbabbabbaabbaaabaabaaabbbababaabbabbbaababbbbaaaabbabbabbbbbbbbaababbbbabbabaaabbbaabbabbaabaabbaabbbbabbabaabbbabbbbbbbabbbbaaaabbbbabbaabaaaababaaaabaaaaababbaaaabbbaaaabbabbabbababbbbaaabbbbbbbabbbaaabaaaaabaaababbabbaaabbaaaaabaaabbbabbabbabaaababbbbbaaaabbbabaabbabaaaaaaaaaabbabbbababbaaabbbaabbababbaabaaabbbbabbbaababbbabaabbbbbbabaabbaabaabbbabaaaabbababbabbabaabababaabbbbabbbaaaaabaaabbabaabaabaaabaaaabaabbaabbaabbbababaababbababbbbbababaaababbaabbbbbbbbbaaaaabbbababaabaababbaabbabababababbbaaabbbbbabbbbaaababbabaababbbbabbabbaabbaaabbaabbabbababbbbaabaabbbbbbabbaababbaabababbaaaabaabbabbaaaaabbaabbbbbbbabbbbbaaaabbaaaabaaabaaabbbbbbaaabbbaaaabbbaaababbabbbbbbbbabaababaabbabbbaabbabaaaaabababaaaabbbaabaabaaaaabaaabbaabbbaaabababbabbbaabababbbbbbbbbabbbbabaabbaabababaabaaababbaaaabbaabbbabbbabbbaabaabababaabbbbaabbbaaaaaabaababaabbbbabbababaabbabbbbaaabaabaaabaaabaaaabbaabaabbbabaaabbabbaabbabbbbababbbaaababababababbbbabaababaaaaabbbbbabaababbbabaabbaababbabaabbaabbbaaabababababbababaaabaaaaabababbbaabbabbbabbbababaaabbbbbaaaaaabbababbabbbaaabbbbbaabbbbaaabababaababaaaaabaaaaaaaabbabaabbaabababbabaababbabbbaaabbaabbaaaaaaabaaabaaabaaabbababbabbbbbbaababbabaabaaabbbabbbaaabbaababbbaabbbbbbbbbaabbbabaabbbaaaabbabbbaabababbaaabaabbaaababaaabbaabbbbaaaabaaaabbabbaabaabbaababbbbababbabbabbbababaabbabbbaabaabababbbbaaaabaabbaaababababbbabababbbababaabbaabbaaabababbaabbbbbbbabbaabbababbaabbbaababbabbbbbbbbaaabaaaabbaabbaabbabbbaaababaaaaabbaababbbabbbabbabaabababbbaaaaabaabaabababbabababababbbbababbabbbbbbaabaaaabaabbbaabaabaabaababaabababbaababbaaabaabaababbaabbbaabbbbabbaababbabaabbabbbbaaababaaabbbbbbaaaababaabbbaaabbbbbbaababbbbbbabababbabbbbaababbbabbbbaabaabaabaabbbababbaaabbbaabaaabaaaababbabbbaaabbaaaaababababbbbabaaaabaabbaabbbabbbabaaaaabbbbbbbabbabbaabaaaaabbbbbaaababababaaabbabbaabbbabaabbabaaaabbbbbbabaabbbbbbaababbbbbbbabaababaabaabbbaaabbbbaaaaabbbbbaaaabbababbbbaabbabbbaabaabaaaaaabbaaaabbbbabbbbababbababbabbbababbaaabbbbababaaabbaabbbabbabbabaabbaabbbbaabaaababbbbbbbbbbbabaaaabbaaabaaababaaabbbbbaabbbbaaababaaaaababbbbbaaaabababbaaabaaaabbabaabbaabaaabaaaaaaabbbbbbbaaabbbaabbbabbaaababaabbbbbabbbbbabaabbababbbbbababababbababbbbbaaaaaabaaababbbbaabababaaabaabbbaababbbbbabbbbbbaabaabbabbbaaaaaaabbaabbbaabbbaabbbbaabbaaabababbbbbbbbaaabaabbbababaaababaabbaabbbabbaabbbaabbbbbbbbbabababbaaabaabaabbaababaaabbabbbaaaaaababaaaaaaaabbaaaabbabbabaaaabbbabbbaaabbbbababbabbababbaaaaaabaaabaaaaabaabbababababaabaabbbbbbbaababaaaaabababaaaaabaababaababbbbbabbabbbaababbbabbababbbababbbbabaabbaaaabbbabbaabbaababaaaabababbbbabbbabaaabbababbbabbababaaaabbabababbaabbbbabaabaaaaaababbaabbbaaaabbbbababbbbaaaabbaaaabbbaababbbabaabbbaabaabbabbabbabbbbbabbbbabbabaaaabaaaabaabaababbabbbbbbbaaabbbbabbbabababbbbbaaaabbabbbaababaaaaabbbaabababaabbbaabaababbaabbbabaaabbabbabbbbbbbaabbbaaabbababbababbabbbaaaaaaaabaabbbaabbababaaaaaabaabaabbaaabbbbbabaabaaaabbbbababbbbbbbbababbbbaaabbaaababaaabbabbababbababaaaaabbbaabaabababbababbbbaaaaaaaababbbbaaababaaabaabaabaaaabbbbaabbbaabbbbaaabbbabbaabaabbaabaaabbabbaaaaaababaaabababbababbaabbaaaabaaabbabbbaaabbabbaaaaaabaaaaaababbbbbaababaaabbbaabbaaaaabbbabbaabbaaaaaaaabaabaaabbabbbbababbabaaaabbbbabaaababaaabaabbaabbabbbbbaabbaaabbabbbabbbbbbbbabaabababaabbbbbaaababbaababbababbaababbaaaabbabaaababaabbbababbaaaababaaabaaabababbaaaaabaabaabbbaaaaaabbbaaababbabbaabbaaaaaabbbabbbbbabaabbaaaabbabbbbaaaaaaababbababbbbbababbbaabaaabaabaaababababaaabaaaababbbbaaabaababbaaabbbbaaaaaababbabaaaabaaabaabbaaaabbabbbbabababbaabbbabbbbabaaabbbaabbbaabbababaaababaabaabbabbaaaababababaaaabbaabaaaabbbaaabaababababaabbabbaaabbaabaaaaababababbbbbbbababaaabbbbbbbabaabbaabbbbabaaaaaaaabbbbaaababaaabaabaabbbbababaababbbabbaababbbbbbbabbbbabbaabababbaabababaaaabaaaababbbabbabaaaabbaabaaaababbaaabbbbabaabbaaabbbabbababbbaabbabaabaabbbaaabbbbaabbbbbabbaaaabbbbabbababababababbbbbaaababaabbaabbaaaaaaabbabaabbabbaaaaabaabbaababbababaaabbbaababbbbbaabaaaabaabaaaababbbbaababbbbabbaabbbbbaaaaaabbaaabbaaabbbbaabbaababaaababbbabbbbbaabaabbbbbaabbbbaaabbbbbbbbbbaabbbbabbbbbbbbaabaabaababbaaaabbbaabaaabbabbabaaaabbbaaaabbbbbbabbaaabbabbaababaabbbaaabbabbaaaaaabbabaabbabbaabbabaababbbabbaabaabbbabbaabbbbbbabaaaaabbababaabaaaabaaaaabaabbbabbbbaaaababbbbbaabbaabbbaaabaabaababaaababaabbaaaabbbaababbbaabaabaaabbbbabbaaabbbabaabbbabaabbbbaabaaaaabbabbbaabaabaaaaabaabaabbaaaabaabbaabbbabaabaababaabababbbabbaaabbbabababbbbababaaaabbbbababbbabbaaabbabbaaaaaaabbbabbbbbaabababbbbabbaaabababbbaaabbbbbbbbabababbbaabbbabbbaababbbaabbbaababaabbbaaaaaaabaabbabbaaaaaaababababbbbbaaaaabbababaabababbbabbaaaaaaaabbabbaabbbaaaabbabaaababaabaabbaababaababaabbabbabbbaaaabbbaabaabbbbababaabbbaaaabbbbbaaabbaabaaabbbaaaabbbababababbabbbbbaaababbaabbaaabbbabbbabaaaabbbaabbabaababbababbaaababbbaabbaabbaababbababaabbbababaaaabbabaaaaaababababbaabaababbbaaabaabaababbbbaaabbbbbaaabbabaaabbbaabaaabaabaababababaabaaaaaaaabbaabbbaaabababbbbbbabbbabbbbabbbabaabbbabbbaaabaaababaaaaababbabbbbaaaabbbababaabababbabbaaaabaabbabbbbbbbabbabbbbbbababbbabbabbabbabaabaaabbbbaaabbbbaabbabbbaaaaaabbabbbbbbaabbababbaaaabbbabaababaabbbaabbaabbaaaaabaaababaabbabaabaabaabbaabaaaabbabaaabaabababbaabbababbbabbabbbbabbbbababbabbbbbaabbaabababbaabaababaabaaabbaabababaabaaaababbbaabbabbaaaabbabaaaaaababbaaabaaabbabaaabaaaaaabbbbbbaabaabbababaaabababaaabbbbbabaaababbaaabbbbabaaababaaaabaababababbbabbbbabbbabbbbaabbbabbaabbaabaabbbababaaabbaabaaaabaabbbbbbababbbababaabababbbaabbbbbabbaaaaaabbababbbabaaabaabaabbbaabaaabbbabaaaabababbbbaaaabbaaabbbaababaaaaabaabbaababaabbabaabbaaabbbbbaababababbbabbbaaaaaaabaaaabbabaaababbaaaaabababababaaababbbabaaabaaaaabaaaaaaabaabbbaabbbbbabaaaababaaaaaaaabbaabbbbbbabaaabbababbbbbaaaabaaabababaababababbbaabaabaabaaabaabbaababbbbaabbaaabbaaaababaaababababbbaaababbababaaabbabaaabbaaaaaaababaababbaaaabbbbaababbbabaaabababaabbbabbabababbababbaaabaabbbbbbaaaabbbbababbabaaabababbbbbbbbabbbbaaabaaababaaaababaabababbbabaaabaabbbbbbbbbaabaaababbabababbbaabbaababbaaaabbbabababaaabbbaaabbaababbabbaababbbabbbaabbbbaaaaaaaabbabaaabbabbaaaabbbaaaaaaabbbbaaaabbabaabbabbbbbabaabbbbbaaabbbabbbaabbaabbabbabbaaabaabaabbaabbbbaabbaaaaabaaaaabababaaababaababababbbbaabababbbbaabaabbaaaababbbaaababaaaabbbabababaabbbabbbbaababbabaaaabababbaaaabaabaabbbababbbbabbaaaaaaababbabbbabaaabaabbaabaaabbbbaabbaabbababaabaaabaababbaabbbbbbaaababbbbbbababbaabaaabbbaaababaaaaabbabaaaaaaaabbbababbabababbabbbaababaaaabaabaabbbbbbaabbbaaaababaaaabbbaabaaaaabbabaabaaabaaabbabbabaaaaabaaaabbaabaaabaabbbabaaaabbaabababaaaabaaababbbbaabbbbbabaababbababaaaababbbbabbaaababbabbabbaabababbbbaabbaaabaabababbbabbbabbbbabaaabbbabbaaabbabbbababbbbaaaaabbbaaabbaaaabbabbaabaaabbbbbaabbbabbabaabbabbbaaaabbbbaaaabaabbaabababbbbbabbbabbbbaabbabbaaaaaabaababaaaaabbabaabbbbbaaaabbaabbaabbbaabaaaabbabbbbbbaaaabbbbaaaaaaabaaabbaababaaaaaaabaaabababbbbabbababbbbbaaabbabbaaaabbaaabbbabbbaababbaaabbbbbaaaaabaaaabaaaaaaaabaaaabaababbabababbaabababbaabbaabaababbbababbbbbabaaabaabaabbabbbababbbbabaaaaaabbbaaaabaaabbaaabbabbbbaaabbaaaabbaabbbbbbababaabbbabbabababaabaabbaabaaaababbabababbabbaaabbabbaabbbbbaaaaabbbabaabaaabaaababbabaaaabaababbabbaaaaabababbbbaaabaaabbababbabbbbabaaaaaabaaabbabbaabbaabaaabbbbbaaaababaaaababbabaaaabbabaabbabbaababbbaaabaabbabbabbbabbbabbbbbbbbabbabbaababbbabaaaaaaaaaaababbbaabbbabbbaababababaaabbabaababbaabbaaaaabababbbbbaabbabbabaabaaabaaaabbabbbbaabbabbbaababbabbaabbababaaabbaaabbbbaaaabbbbabbbbabbbabbbbaaabbaaabaabaabbaabbabbaaaaababbbaaabbbbbbbbbbaaaabbaaaabbaabaaaaabaabbbbbaabaaabbabbabaababbbbbbbaabaaababbaaabaabbababaaaababbabbabbbbaabbbaaabbbbbaaaabbbababbbabbaabaaabbabaabbabaaaaaaaabababbbababbbbabbbbbabbaaaabbabaabaabbabaaaaabbaababaabbaabaababbaaaaabbaaaabbbbaabaaabbabaaaabbababbaaabababaaaaabaaabaaabbbaababababaabbabbbbbbabbaaaaabbbaabaaaaabbbaabaaabbaaabababbabbbbbbbaaaabbaaabbabbbbbbbbbaabababbbbabbbbabbbbaaaabaababbbabbaabababababbaaababbaaabababaaabaaabaaabaabbbbbbbbbabbbaaabbabbbbabbbababaaaaaabbbbbbbbbbaababbbbaaaababaabbaabbaabaabababbbbabbabbbbbabbabbbabaaabbaababaabababbabaabbbbbaabbbbabbbabaaabbbbaaaaabbbabbbbababbbabaabbaabbabbaabbabbaabbbbabbbbabbaabaaaaaaabaaaabbaaaabbbbbabababbbbaabbbbbbabbbaaaababbbbababbbbabaabaabbaabaabbaaabaabbbbabaaaabbabbabaaaaaababbbbbbabbaabaaababbabbbaaaaabaaaabbaaabbbbaaabaaaabbbaabaababaababababaabaaabbaabaabbaabbbbabbabbbbaaabbabaabbbaabbabaaaaabbaabaabababbbaabbbabaabaaaababaabbbbbabaaaabaabbbbbbbbbabbaaaaabbbbabbbaaabbbbbbaababbaababbbbbbbbbababbabbbabbabaabaaabababbbbababaabbbbabbaabbbabaababbbbbbbabaabaabbbabbbaaabbbabaaaabbbbbbbbabbaababbbbbaabbbababbbbbaaaaabbbbbbbbabbabbbababababaababbbaaabbabbababbabaaaaaabaaabaabbaabaaabaabaabbbabbabaaaaabbbabbabbabbbbababbabbbabbbabaababaaaaaabbaabaaaaaaabbabaabaabbbbaaaaaababaaabbbbababbaabbaaababaaabbaaabaabbbbbbabbbaabaaabbabaabbbbbaaaabaabbbbbabbabaaaaaaaaaabbbbbaabbbbaabbbbbbaabbababbbaabaabbbabbaabaabbbbaaaabbabaabaabbabbabababbbbbbabbbbbbaabbabaababaabaababababbabbaaabbbbaaababbbaaaaaaaaabaabbbbababbbbaaaaabbbbbbbabaaaaaabbabbbababbbbbabbaaaaabbaabbbaabbbaaaaabaaaaaabbaaabbbbbbbbbbbbbababbbaabbbbbabaaababbbbaabaabbbbabaaaaababbbbbbbbbbbaabaaaaabaaabababbbabbaabaabaaaababbababaaaabbabababbbababbabbaababbaabaaabaabaaaababbbbabbbabbbbaaabbbbbbbbaaabaababaabbbbaabababaaaabbaababbaababababbaabababbbbbbaabaabaaaaabbbbaabbaaabaaabbbaababababababbbabababaaaabbaaaaaababaabbaabbaababbbababbaababaaabbbaababbbababbbabbabbbbababbbbbabbabbbbaaabbbbbaaabaaabbabbaabbaababbbaaabbbabababbbbbbabbbaaababaaaaaaaaabaabaaaababbbaababbbbbaabbabaaabbaaaabbabbaababbbaabbbababaababaabbbbbababbaaaaaaabbaaaabaaaaaaaabaaabbbaabbaababaabaabbbaaabbaaaabbbaaabaabbbbaaaabbbbbaaabbaaabbbababaaaaabbbbbaaabbaabbbabbabbbbababaaabbbabbbaaabbbaaabaaabbaaaabaaabbabbabbbaabbaaabbabbbaabbaababbbaababaaabababbaabaabbaabbbabbbaabbabbabbabbababbaabaabbbbaabaabbabbbbbbbbbbaabbababbabaaabaaabbababbabbbbbbaabaabbaababaabababbbbbabaabaabbaaaababaaaabbbbbbaabaabbbbaababbbbabbbaaaaabaababababbabbababaabaabbbbbbaaabbaabbabbabbbaabbbaabbbbaabbbbbbbaabbaabaaaabbabbbaabababbbabaaabbbbbbbbabbbbbbababababaaaaaabaaabaabbbbbabbbbabbbaaabbaaababbaabbbbbaaaaabbbabaaabbaaaabaaababaaaabaabbabbabaabaaaaaaabaababaabaaaaaaabbaaaabaababbbbbbababbbabbaabbbbbbbbabababbbbbbbabbbabababaaabaabbaabbbaababbaabaaabbbaaaabababababbbbbbbbaababbbababbabbbaaaaaabbabbabaabaababbabaabbaabbbaaabbaaababbaaabaabbbbbbbaaabaabbbaabbbabaabaaabaaaabaaabbbbbbbbbaababbaabaaaabbbaabbbbaaababaabaaabaaaaabaababbaabaaaabbbabbababaaaabaaaabbaaaaaababababaaabbaabbabbbaaabaaaaaaaabababbabaaaabbabbabbbbbaaaaaaabbbbbaaabbaabbbbbaabbabbbabbabbbaaaababaaabbbababaaabbbbbbbbbabaabaaabbbaaabababbabbababababbbbbabbbabaaaabbbabbbabbbaaabbaabbaabbabbaabaaabbbbaaaabababbabaaaabbbabaaaaaabbabbaabbaaaaaaaabaaabaaabbbbaabbbbbaabbaabaababaaaabbbbbaabaabbabbbbabbbaabaabaaabbabbbabaabaabbabbabbabaabbababababbababbaaabaaabbbabbaaaaababbbaaaaabbbaaaaabbabaaabaabbaaabbbaaabbabbbaabbabbbabbabbbbbbbbbbaaababaaabbababbbaababbaabbaababbbbababbbabababbabbbabbabababbaaabbaabaaaabbbaaabbababbbaaaaaabaabbabaabaabaaaaabbbabaaabaaaaabbbbabbbbbabbaababbabaabbbabbaababbbbbbaabaabbabbbbbbbaaabbbbabbaaabbababbbabbabbabbabaaaaabbaaaaabbaaaabaaabbbbaabbabaababbbbababbbbbbbaabbbabbaaaabaabbbaabbaaabbabaaaaabbaaabaababbbbbbaabaaabababbabaabaabbbabaabbbbbbbaabaaabbbbaabababbaabaaaababbbabbabbaabbbbaaabaabbbababbbaaaabbaababbababaaabaaaabaaaaaaaaabbbbbabbaaabbabaaabbaaabaabaababaaaabbbaababaabaaaabbbabbbbaaababaaabbbabbabbbababaaababbabaabbbaaabbaabbbabbaabbbabaabbaaababbbabbaaaabbaaababbabbaaabaabbbbbbaaaaaaabababbabaabaaabbabbaaaaabbabababbbbababbbbababbbbaabbababaaaaababbabaaababaaaabbaabbbabbaaaaaabaaabbabbbaabbabbaaabbabbaabbbabbaaaaaabbbabbbaababbbbbaabbaaaaaababbabbbaaaaabababbaaabbbaabbabbaaababbbbbaabbbbbabbbababaaabbabbaabaaabbbbbaabbabbbbabbbbaabbbbbbbbaaabaababaabbaabaaababbaababbbbbabaababbbaabbbabbbbbababaaabbbabbaaaababbabbaaaabababbbbabbbabaabbbbaaabbaaaabaabbabaabbbaabbbabbbbbabbabaabaaabbbaabbaabbbbbbbbababbbabbbbabababbaaababaabbbbbaabbbbbbaaaabababbababaabbaaabaaabbaabbaaabbbbbabbabbabbaaaabababaaababababaaabbabbbaabbaaabababbabbbaaabbbbaabbaabbabbbaaaaabbbbabbbbbabbaaababaabbbbaabbbbaaaabaababbbabbaabbbabbbbbaaaaabbababbbaabbbbaaabbaaabbbaaaaaaabbabbbbbabbbbaababaabbbaaabbabbbbbbaabbabbabbaababbaaababbabbabaabaabbbaaabbabaaaaababbabbabababbabbbbabaaababbaabbaabbaababbabaabbabbbbbabbaabbbbababbbbaaabbbaabaabbababaaaabaababbbbabaaababbbbababbbabbbbbbbbabbaababaabaaaabbbbbbaabbaaabbbababbbbabbbbbabbbbaabababaabbbaababaaaabbabbaaaabaaababbaabbbaababaabaabbabbabaabaabbbbbbaaaababbbbbbbaabbbababababaaabbaabbaabbaaaaaaabbaaabbbaabbababaaabbaaaaabababbabbabbbaabaaaaababbbbbabaaaababababaabaabbbbabbabaababbbabbaababababbaabaabbbbaabbabbbaaabbaaaabbaabbaabbbbabbbbabbbbbabbbaaabaaabbaaabaababbbbbbbbbbbbaabaaabbbbabbabbbbbbaaabbaabbabaaaabaabbbbbabbbababbabbabaabaabaaababbbbbbbbabababaaaabbbaababbaabaaabaababbbaababbbbbabaabaabbbbabaababbabababbaaabaababbababbbabbbbbbabaabababaaabbabbababbbbbabbbbbbbbabaaabbaaaaabaaaaabbaaabaaaaaaabaaabbabbbbbbabbbabbbbbababbbbbabaabaaaabbabbbababbbbbabaabbbaaababaaaaaabbabaabbbbbbbbabbaabaabaabbabbabbbababbbbabaababaababaabbabbbabbbabaaabaaabaababbaabaaababababbbaaabbbabbbbbbbbbabbabbababbbbbbbaaaaabbaabbababbbbabaaabbababaabaaaaaaababbabababbaabaabaaababbbabbbbbabababaabbbabaaabaabaabbaabbbbbbbaabaabbaababbbaaabbbabbaaabaabbbbaaaabbaababbababbbaabaaaabbbabbabaabaababbaababbbbbbababbbbbbaaabaabbbbababaabbbbaabbbabbabaaabbaaaaabbbabbaaaabbabaabbbaaaabbababbabbbaabaabbbbaabbabaaababaabaaaaabaabbabbbabbbaabbababbababaababbbababaaabaabbabaabbbbaaaaaaaababbbbbbbbbbababbbbababbbbbabaaabbbbbabbbabaaaabbbaabaaabaaabbbabbbbaaaabaaabababbbabaaaabbbbbabbbbaabbabaabbabababaabbbababbbabbbabbbbbbbbaaabbabbaabbabbbbbbbbbbbbbbbabaaaababababbbabaaababbabbaaabbabbababbaaaaaaaaabbaabbaababbbabbaabbabaabababaabaaaabbababbbabaabbbaaaaabaababbaabbaabbaaabaaabbbbaaababbbabababababbbbaababbbababaabaaaaaababbbaabbaaaababbabbaabbbbbbbbabbaabbbbbaabaababbaaaaaabaaaabbbabaabbbaaabbbababbbaababbbabbaabaaabbabaaaabaabaaaabbbababaabbaabaabaabaabbaabbbbbbbabababaabbbbababaababaaabbbbaabbbbbbaabbbbaaabbbaabababbaababababbaaaabbbbbabababbbbbbaababaabbbabaabbbabbabbbbbababababbababbbbaaabaaaaaabbbbbbbbbbaaaaaaabbaaabaaaababbaaaaabbbaaaaababbaaabbbbbaaabbaabbababaabbaabaaaaabbaaabbbaaababbabaaabbabbabbabbbabbababbbaabaaaaaaaababbbbbaabbaabbaaaaaaabbbbbbbabbabbabbbababbbababbaabbbabbbaaabbbbaabaabaaaaabaaaaabbbaaababbbababbbabbabaabaabaaaabababaabbbabbbababbaaabaababbbaaabaaaabaaaabbbbaabbabababababababababbabbaabbaaabbbbaabbbabbbbbbaaaaabbbaabbbbababbbabaaaaabaabbabbaabbaabaaababaaaabaababbbaabbabbaaaaabaaabaababbababbababbaaabbbbbababbbbaabbaabaabbabbbbbaabbbaababbbaaabababbaabbabaaabaabbaaaabaaaabababbbbbbbbbaabbaaaaabbabaabbbbaaabbaaaaaaabaabbbabbabbbbbbaaabbaaabbbababbabbbababbbbaaabaabaabbbaabaababbaabbaaababbbabbaabaabaababbabababaabbabbaabbaabaabbbbbaabbbabbabbbabbbbaabbbbaaabaaabbaaaaababbbababbbabbbbabaabbaabababaaabbbbaaaaabbbbabbbabbbbbaabbaabbbbaaaabbbbbbbbabbbaabaaabbbbabaabaababbbbabaabaabaabbbbbbbbaaabbbaabbaaababbabbaaabbabbbbbabbaababaabbaabbaabbabbaabbabbaabaaaabbaaaaaaabbbbaaaabaaabbbbbaabaababbbaabbababbbbabbabbbaaaaabbaabbababbaaaaaababbabbaababaaaaabaabbababbbbbaabbaaaabaaabaabbbbbaabbabbbabaaabaabababbbaaaaabbbbbbbabbaaaaaaaababbaaaaaabababaaabbbbaaabbabaaabbabaaaabbaaaaaabababaaaabbaaabbbabbbbabbbabbaabbbabaabbbbbbaababaabbaabaabbbbbabbbababbabaaabbabbbbababaaaaaaaaaabbbbaaaabbabaabbaaaaaaaaaabbaaabbabbbbbababbbbbbbbbbaabbbbbbabaabbbaaaabbaabbaaaaaaabaabbaaabbabbbababbbbabbabbbbabaaaaaabaabbaaaaaaabbabababbabbabbbbbbabaaabbabbbabbaaabbbaabbbbbabaabbbbababababbabbbbbbabbbbbabbbabbabaaabbababbabbabaaaaaabaabbabbaaababbaabbbaaabbabaabbbabaaaabaabbbabbaaababaaaabbabbbabaaaaaababaabbaabbbbabbaababaababaabaaaaabbaaaaaabbbbabbbaaababbababaaababaababaabbabbbabbabbaabbaabbbbabbaaaaaaababaaaabaabbbbbaabaabbbaababbbaaabbaaaabbabbaababbaabbbaabbabaaaabbabbbbaaabaaabbabbaabaabababaaaabaaaaabbaaababbaaabaaabaaababaabbbabbabaabaabbaaababbbbababbaaababbabbbabbbbabaaabbabbaaabbbababbababababbaabbbbbababababaababbbabaaaabbaabaaaabababaabbabbaabaaaababaaaabaabbbababbaaabaaaabaabbbbababaaaaabaabababbaaaaabbbababbbaababbabbaaaababaaaaabaabaaaaaabaaabbbabababababaabbbabaaabbaabbbbabaabaaabababbbaabbaabbaabbababaaaaabbbbabaabbabbabbbbaabbbaaaabbbaabaabbabbababbaaaaaaabbaabaaaaaabbabbabaabaaabbaaabbababbabbbbbbabbaabbaabababbaabaaabaabbbbbbbabbaaaabaaaaaabbabbbbaaabbaabbbbaaaabababaababbabaaaabbabbbbaabbbbbbbbbbbaabbaabbbaabbbbbaaababbbaabbabaabbaabbbbabbbabaaabbaabababbabaabaabbbaaaaabaabbbaabbaabbbaabbababbababbababbbaaaabaabbbabbaaaabaaabbaaaabbbbbbaabaaaabbababbaabaababbababaaaaaabbabbbbbababbabaababaabababaababbbaabaaaabbabbaaababbaabaaababbbaaabbaabbbbbbabbbbbbbbabaabaabbbbbabababbabaaabaaabaaaabbbababaababbabbaaaabababababaabbbababbabbbbbabaabbabbaabaaababbabbbbaabbabbabbbbababaaabbababbaabaaaabbbbabbbabaaababaabbaaaabbbaaaabababaabbbabababbbbbbaababbababbaabbbbaaaabaabbaaaabbbabbaaabaaaabbababaaaaaaabaabbaaabbaabaabbbaaabbbbbaababbbbbbbababbaababaabbbaababbbabbbbbbbaabaaaabbabaabaababaaaaabbaaaaabaabababaaaaaaabbabaabababbbaaaabababbbbbbbaababbbabaaaaaababbabbabaabaaaaaaaabbaabaabaabbbababbbbababababbaabaaaabbbbabbababaaabbaaabbbababbbbabbbaabbbbbaabaaaaaaabbaaabbbbbaaabbbababbbabbbaabbabaaaababbaaabbbbbababbaaaabbbbaaaaabaabbbbaaabbbababaaabbbbababaaaaaaaaababbaaabaababbabbabbaababababbababbbaaaaaabaabaabaaababaaaababbababbbababbbabbaabbbbbaabbbbaabbbbbabaaaaabbbabababaaabbbaabbbaaaaababbbaaaaaaabbbbaababbbbbababbabbababbabbaaabbabaaaabbbbabbaabbabbaaabbbababbbbaaabbaaaabbbbaaabbbabbbababbababaaaaabaaabaaabbabbbbabababbbbbbbabaabbbbbababbaaaabbabbabaabbabbababbbabbababbaaaaabbaabaaabaabbbabbbabababaabaabbbbbbabbaababbaaabbababbabbaaaabaabbabbbabbaaaabababbbaabbbaaababaabaaaabbabbbaaaabaaabaabbbaaaaabbbbbbbbaaaaabbbababbbaaabbaaabaababbaabbaaaabababbbaabaaabaaabbbbaaaabbbaaabbbbbaabbaabbbaaabbbabababbbbbbaaaaaabababbabaabbbbababbabbbababbbababbbbabbbbabaaaaabbaaaaaababaabbabbaabaababbaaabbaaaabbbabbbbaabbabaabaababaababaaabbbbbaabbabbabaabaabbabbbabbabababbbbbaabbbaabaaaaabbbbabaaaabbabbbaabaabbabaababbbabaaabababbbaabbababbaaabaaaabbaaabaaaabbbbbabbbabbbbbaaabaabbabababababbaaaaaabaaaabaabbbababbabababbbaabbabbabbaababbabbbbaaabbabbababbbaabbabbbbaabbbabaabbababbaababbaaabaababaaaabbaaabbbbabbbbaaabbbbaabaaabaabababbbaabababbaaabbbbbabbbabbabbbabbabbabaaababbbbaabbabaaaabbbaaabbaaaaababaababbaabbbababababbbbaaaaaabbaabaaaabaaaaaaabbabbbbaababbbbababbaaaabbbaabbbbaaaabaababaabbbbbaaabbbbaabaaaababababbbaabbaaaabaabbabbabbababbaababaabbbbbbbbbababbbaaaabbabaaabbaaabbababbbbbbbbbbaababbabbbababbaaabaaaaabbbbbaaaabaaaabbabaababbaabbbabbaabbbaaaabbabaababaaabbbababbababaabaaabaaabaaaabbbaabbbbabaabbaaaaaaabbaaaaabbbabbbaabbbaabababbaaabbaaabbaabbbabbaabbaaabbababababbbbabbaaaabaabaaaabaaabbbaabaaaaabbbabbaaabaaabaababaaaaaababbbabaaabbaababbbabaabaabbabaababbaaabaaabbbbaabbbbbabaabbbbaababaaaabbbbbbaababaababaaabbbaaaaaaaaaababbbaababbababbbabbababababaaabbaaabbaababbbbabbbbbbbababaaabbbbabbabbabbbaaaabbbbbbaabbbbbaaabbbabbaabbaaaabaabbbbbbbabaababbababbbababababbababbbaababbbabbabaababbabbaabababbaabaabaabbaaabbaaababbbbbbababaaababbbabbbababbbbabbaabbaaabbbaabaaaaaaabbaababaababaabbabbbbbaaaabbbabbababbbbbabbababaaaaabbabbababaaabaabaabaaaaabbabbabababbababbbaaaababbaaaaaaabbaaababaababbbbaaaabbbaabaababaaaabbababaaabbaaabaaabbaaaababbaabbbbbbabbbaabaaaaaabbbaaabbaabbabaaaabaaabbababaaababababbbbaabbbaabbbababbaababbbabbbabbabaabbababababbbbbaabaaabbbaababbbbabbbaabbabaaabbbbbabbbabbbbabaabbabbbbaabaaabbbaabaaaabaabbaabbaabababaaabbbbaaaaaabbbababbabaaabbbbababbabaaabbbaaaaaaabbababbbbabaaabababbaaaaabbbaababbaaababbababaabbbbbabaaababbbbbbbaaaaaaabbaaabbababbabbbaaaaababbaaaaabaabbaabaabaaabaaaaababbabbaaabbabbbbaabbabbabbabbbbabaabbabababbabbbbaababbabbbabbabbbabaaaaaababbbabaaaaabbbbbaaaababbbbababbbbbabbbbbbbbabbaaabbabaaaaabbaabbabaabbaabbaababbabaabaaababbbbbaaababababbaabbaabbabbababbabbaaaabababbbbabbbaaabababaaaabbbabbaabbbabaabbaaaabbaababababbbababbbbababaaaabaaabbbabbaabbbaabbabbbabbbaaabaaabaaaabaabababbbabbaaaabbaababababaabaabbaabbaababbaaabaababbbbabaaaaaabbbaabbaaabaabbbbaaabbaabaabbaababaabbbabbbabbbabaaababaababaababaaabbbbbabbbbbaabbbaaabaabbbabaabababbabababaaaabbbaabbbabaaabbabbbaabbbaabaaaaabbbabbabaababaaaababbbbaaaaaabaaaabbabbbbabbbaabaabbbabaabbbabababbbbbbbbaabaabbaaaaabbbbabbbaabbabaaabaaaabbabaabbbbbbaaaabbbababaabaabbbaaaaaaaabbbbabbbabbbaaabbbbabaabbaaabbababaabaabbaabaababbabbbabbbababbbbabbbbabbbaabbbabbaababbabbabbaabaabbaababbababababaaabbaabaabaaabababbabaabbaaababbbaaababbababaabbabaaabbbaaaabaababbbabaabbaaaabbbaababaabbbbaababbbbbaabbaaaaaabababaaabbababbaaabbaabababaabbbbbbabbbbbaabaabbaaabbaabbbbabbbaabbabaaaabbbababaabaabaaaabbabbbabbaabbbbbabaaabbabbabbbbbaabaaabbabababbaaabbbaabaabbbbbaaaaabbaaaabbbabbbaabaabbabbabbabbaabababbbbbbbbabaabaabbbabbbabbababbbaabbabaabbbaabaaaabbaabbaabbbabaabbaabaabbbabaaabbaaabbbaaaabbbbbaaaabbbabbbbaaababbbbbbbbababbaabbabbabbbbbabaaaabaabbaabbbbbabbabaaaaaabaababbabbabbaabbabbbabbbbaaabbbaabbbaabbbaaaababbbbbaaabaababbbbbabbbaabbaabbbbaaabbbbbbaabaaaabaaabaaabbabbaaabbaababaaabbaababbabbaaaabababbbabbaaabaaaaabbaabababbaabaaaabaabbbbaaaaabbaabbbaaabbabbbaaabbabbbabbbbbbababbbbaaabaaababbbabbaababbbbbabbabababbbbbabbaaaaaabbababbbababaaababbaabbabbbbaaabbbababaaaabababaaababbbababaabaaaabbabbbbabababababbbbbbaaabaaabaaabaababaaababbababbababbabbbabaabbabaaaaabaaabaaaabaaaabbabbaababbaababaabaabaabaabbaababbaaaabaaaaaaaaabbbaabaabaababbaaaabbababaababaaabababaabbbbbaaabaabbbbaaaaaaabbaabababaabbbabbaaabbaaabaaababaaababbabbbbabaabbaaaaaababbbaaababbaaabbabababbbaaaabaaaabaabbabbaaaaababaaabbaabbbbabaabbbbbbbaaabbaabbbaabbbbbbbbbbaaaabbabaabaaababbabbbaabababbaaaabaabbabbbaaaabaaaaaaaaabaaabaabbbbbababbbbababbbbaaabbbaabababbbbbbbaabbaaabbbaabbaabaabababaabbababbbabaaaabbabaabbbaabbababbaaabbabbaabaaaaaaabbbbabaaabbbababbabbaababaabababbaaaabbabababbbaabbbaabbaaabaaabbabbbbbbaababaaabbbaaabbabbbaabbbaabbabbaababbbabbaabbbaaaaabbabaaabaaabaaabaaaabbabaabbabbbbbabaababbabbbbbbaaabbababaaaabaabbbaababaabaaaabaabaabbaaababbbbbbaabbbbbbaaabbaaaaabaabbbbababbaaabaaabbabbaaabbababbabaaabbababbbaabaabbbabbbabbbbabbbbbbbbbabababbbababbbababbbabbbabbaabaaababaabbbbaaaabbaabaaabbbbbbbaaabababbbbaabababbababaaabaabbabbaabbabaaabbabaababbababaaabbbbbbaabbbaaaabaaabaababbbbabaaabbaaababbbbabaaaaabbbabaabaabbabaabbaaaabbaabbbbabbababbaaaababbbababbabbbbaaabbaaabaabababbbaaaaaaabbbbbaaababaaaabbbbaabaaaabaaababbbabbaaabaaabbabbabbbabaaaababaaaababaababaaaababbaaaaababbaababaaabbaabbbbaababbbaabaaababaaaabbbaabbbbbbaabbbbabbbabbaababbbbaabbbabaaaabaaabbbbbbaabaababababaaababbabbbbaababaaabaabbaabaaaaabbbbbbabbababaabaaabaabbaaabaaaaabbbaabbababbababaaababaaabbabbababaaaaababaabbbbbababaaaabaabbabababaabbaabaabababbbbaabbabaaaabbbbbbaaababbbbababbbbbabbaabbbbabbaababababbbbaaabbababbbbabaaabbbaaaababaababaaaabbababbabbbbbbaabaaaabaabbbbbbbbbababbbabbabbbbbabaabbababbaabbaabbbbbaabbbbbabaababaabaaaabbabbaabaabaabbababbababaaaabababaaaababababaabbbbaaaabbbabaababbabaaaaaaabbbbaaababbaaaabbbbabbbbaaaaaabaaaabbababaababbabbaabbbaaabbbbaaaaabababaabaaabbbaaabaaaaaaababababababbbaabbaabaaabbabbaabbbbaaaabbbababaabbbbbbbabbabaaabbaaaaaaabbababababbbbababbbababaaabbbabbbbababbbbbbbbabbbbababbabbbbaabaaaaababbabababaabbbaaabaabaabbaabaabbbbabbbbabaabababbabbaaaabaaaababbabababababaabbabbaabaabaaaabbaaabababbaababbabaaaaabbaaabaabaaabbbbbbabaababbbaabbaaabaababaaaaaaabbbaaabaaabbbbbabaaabbabbaabaabbaaababbbaabbbaabaaabaababaabbbbbabaaaaaaaaaaaabbaabbababaababbbbbaabbabaabbaabbaababaabbaaabbabbabbbbabbbabbabaaaababaabbabbabababbaaaaaaababbbabbbbabababbaabbbabababbbabbabbabbbabbbbbbababbaaabbaababaaaaabbbaaabababbabbaaabbabbaaaabaababaabbbaaabaaaabaabbabbaabaaaabbbbbabbabbbabaaabbbbbbababababaaaa")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1394Test.java b/src/test/java/com/fishercoder/secondthousand/_1394Test.java new file mode 100644 index 0000000000..df68892540 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1394Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1394; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1394Test { + + private _1394.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1394.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.findLucky(new int[] {2, 2, 3, 4})); + } + + @Test + public void test2() { + assertEquals(3, solution1.findLucky(new int[] {1, 2, 2, 3, 3, 3})); + } + + @Test + public void test3() { + assertEquals(-1, solution1.findLucky(new int[] {2, 2, 2, 3, 3})); + } + + @Test + public void test4() { + assertEquals(-1, solution1.findLucky(new int[] {5})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1395Test.java b/src/test/java/com/fishercoder/secondthousand/_1395Test.java new file mode 100644 index 0000000000..759ddd2f4b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1395Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1395; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1395Test { + private _1395.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1395.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.numTeams(new int[] {2, 5, 3, 4, 1})); + } + + @Test + public void test2() { + assertEquals(0, solution1.numTeams(new int[] {2, 1, 3})); + } + + @Test + public void test3() { + assertEquals(4, solution1.numTeams(new int[] {1, 2, 3, 4})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1399Test.java b/src/test/java/com/fishercoder/secondthousand/_1399Test.java new file mode 100644 index 0000000000..49a955e2b7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1399Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1399; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1399Test { + private _1399.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1399.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.countLargestGroup(13)); + } + + @Test + public void test2() { + assertEquals(2, solution1.countLargestGroup(2)); + } + + @Test + public void test3() { + assertEquals(6, solution1.countLargestGroup(15)); + } + + @Test + public void test4() { + assertEquals(5, solution1.countLargestGroup(24)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1400Test.java b/src/test/java/com/fishercoder/secondthousand/_1400Test.java new file mode 100644 index 0000000000..079bfc4dd7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1400Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1400; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1400Test { + private _1400.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1400.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.canConstruct("annabelle", 2)); + } + + @Test + public void test2() { + assertEquals(false, solution1.canConstruct("leetcode", 3)); + } + + @Test + public void test3() { + assertEquals(true, solution1.canConstruct("true", 4)); + } + + @Test + public void test4() { + assertEquals(true, solution1.canConstruct("yzyzyzyzyzyzyzy", 2)); + } + + @Test + public void test5() { + assertEquals(false, solution1.canConstruct("cr", 7)); + } + + @Test + public void test6() { + assertEquals(true, solution1.canConstruct("qlkzenwmmnpkopu", 15)); + } + + @Test + public void test7() { + assertEquals(true, solution1.canConstruct("jsautfnlcmwqpzycehdulmdencthhlzsnijd", 35)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1401Test.java b/src/test/java/com/fishercoder/secondthousand/_1401Test.java new file mode 100644 index 0000000000..d809f47579 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1401Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1401; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1401Test { + private _1401.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1401.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.checkOverlap(1, 0, 0, 1, -1, 3, 1)); + } + + @Test + public void test2() { + assertEquals(true, solution1.checkOverlap(1, 0, 0, -1, 0, 0, 1)); + } + + @Test + public void test3() { + assertEquals(true, solution1.checkOverlap(1, 1, 1, -3, -3, 3, 3)); + } + + @Test + public void test4() { + assertEquals(true, solution1.checkOverlap(1, 1, 1, 1, -3, 2, 1)); + } + + @Test + public void test5() { + assertEquals(false, solution1.checkOverlap(1, 1, 1, 1, -3, 2, -1)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1403Test.java b/src/test/java/com/fishercoder/secondthousand/_1403Test.java new file mode 100644 index 0000000000..9d045f5287 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1403Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1403; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1403Test { + private _1403.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1403.Solution1(); + } + + @Test + public void test1() { + assertEquals(Arrays.asList(10, 9), solution1.minSubsequence(new int[] {4, 3, 10, 9, 8})); + } + + @Test + public void test2() { + assertEquals(Arrays.asList(7, 7, 6), solution1.minSubsequence(new int[] {4, 4, 7, 6, 7})); + } + + @Test + public void test3() { + assertEquals(Arrays.asList(6), solution1.minSubsequence(new int[] {6})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1405Test.java b/src/test/java/com/fishercoder/secondthousand/_1405Test.java new file mode 100644 index 0000000000..37cfe3dabf --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1405Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.solutions.secondthousand._1405; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1405Test { + private _1405.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1405.Solution1(); + } + + @Test + public void test1() { + System.out.println(solution1.longestDiverseString(1, 1, 7)); + } + + @Test + public void test2() { + System.out.println(solution1.longestDiverseString(0, 8, 11)); + } + + @Test + public void test3() { + System.out.println(solution1.longestDiverseString(0, 0, 7)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1408Test.java b/src/test/java/com/fishercoder/secondthousand/_1408Test.java new file mode 100644 index 0000000000..60ec328985 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1408Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1408; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1408Test { + private _1408.Solution1 solution1; + private static String[] words; + private static List expected; + private static List actual; + + @BeforeEach + public void setup() { + solution1 = new _1408.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"mass", "as", "hero", "superhero"}; + expected = Arrays.asList("as", "hero"); + actual = solution1.stringMatching(words); + assertEquals(expected.containsAll(actual), actual.containsAll(expected)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1409Test.java b/src/test/java/com/fishercoder/secondthousand/_1409Test.java new file mode 100644 index 0000000000..60d5da1bea --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1409Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1409; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1409Test { + private _1409.Solution1 solution1; + private static int[] queries; + + @BeforeEach + public void setup() { + solution1 = new _1409.Solution1(); + } + + @Test + public void test1() { + queries = new int[] {3, 1, 2, 1}; + assertArrayEquals(new int[] {2, 1, 2, 1}, solution1.processQueries(queries, 5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1410Test.java b/src/test/java/com/fishercoder/secondthousand/_1410Test.java new file mode 100644 index 0000000000..9c13a19a69 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1410Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1410; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1410Test { + private _1410.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1410.Solution1(); + } + + @Test + public void test1() { + assertEquals( + "& is an HTML entity but &ambassador; is not.", + solution1.entityParser("& is an HTML entity but &ambassador; is not.")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1413Test.java b/src/test/java/com/fishercoder/secondthousand/_1413Test.java new file mode 100644 index 0000000000..d5b9e02f44 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1413Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1413; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1413Test { + private _1413.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1413.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {-3, 2, -3, 4, 2}; + assertEquals(5, solution1.minStartValue(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 2}; + assertEquals(1, solution1.minStartValue(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, -2, -3}; + assertEquals(5, solution1.minStartValue(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1414Test.java b/src/test/java/com/fishercoder/secondthousand/_1414Test.java new file mode 100644 index 0000000000..db6846082a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1414Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1414; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1414Test { + private _1414.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1414.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.findMinFibonacciNumbers(7)); + } + + @Test + public void test2() { + assertEquals(3, solution1.findMinFibonacciNumbers(19)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1415Test.java b/src/test/java/com/fishercoder/secondthousand/_1415Test.java new file mode 100644 index 0000000000..f71e4c28ee --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1415Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1415; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1415Test { + + private _1415.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1415.Solution1(); + } + + @Test + public void test1() { + assertEquals("cab", solution1.getHappyString(3, 9)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1417Test.java b/src/test/java/com/fishercoder/secondthousand/_1417Test.java new file mode 100644 index 0000000000..84143d509d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1417Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1417; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1417Test { + private _1417.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1417.Solution1(); + } + + @Test + public void test1() { + assertEquals("0a1b2c", solution1.reformat("a0b1c2")); + } + + @Test + public void test2() { + assertEquals("c2o0v1i9d", solution1.reformat("covid2019")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1418Test.java b/src/test/java/com/fishercoder/secondthousand/_1418Test.java new file mode 100644 index 0000000000..b2724bbc27 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1418Test.java @@ -0,0 +1,70 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1418; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1418Test { + private _1418.Solution1 solution1; + private static List> orders; + private static List> expected; + + @BeforeEach + public void setup() { + solution1 = new _1418.Solution1(); + } + + @Test + public void test1() { + orders = + Arrays.asList( + Arrays.asList("Laura", "2", "Bean Burrito"), + Arrays.asList("Jhon", "2", "Beef Burrito"), + Arrays.asList("Melissa", "2", "Soda")); + expected = + Arrays.asList( + Arrays.asList("Table", "Bean Burrito", "Beef Burrito", "Soda"), + Arrays.asList("2", "1", "1", "1")); + assertEquals(expected, solution1.displayTable(orders)); + } + + @Test + public void test2() { + orders = + Arrays.asList( + Arrays.asList("James", "12", "Fried Chicken"), + Arrays.asList("Ratesh", "12", "Fried Chicken"), + Arrays.asList("Amadeus", "12", "Fried Chicken"), + Arrays.asList("Adam", "1", "Canadian Waffles"), + Arrays.asList("Brianna", "1", "Canadian Waffles")); + expected = + Arrays.asList( + Arrays.asList("Table", "Canadian Waffles", "Fried Chicken"), + Arrays.asList("1", "2", "0"), + Arrays.asList("12", "0", "3")); + assertEquals(expected, solution1.displayTable(orders)); + } + + @Test + public void test3() { + orders = + Arrays.asList( + Arrays.asList("David", "3", "Ceviche"), + Arrays.asList("Corina", "10", "Beef Burrito"), + Arrays.asList("David", "3", "Fried Chicken"), + Arrays.asList("Carla", "5", "Water"), + Arrays.asList("Carla", "5", "Ceviche"), + Arrays.asList("Rous", "3", "Ceviche")); + expected = + Arrays.asList( + Arrays.asList("Table", "Beef Burrito", "Ceviche", "Fried Chicken", "Water"), + Arrays.asList("3", "0", "2", "1", "0"), + Arrays.asList("5", "0", "1", "0", "1"), + Arrays.asList("10", "1", "0", "0", "0")); + assertEquals(expected, solution1.displayTable(orders)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1422Test.java b/src/test/java/com/fishercoder/secondthousand/_1422Test.java new file mode 100644 index 0000000000..2e24796e2a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1422Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1422; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1422Test { + private _1422.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1422.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.maxScore("011101")); + } + + @Test + public void test2() { + assertEquals(5, solution1.maxScore("00111")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1423Test.java b/src/test/java/com/fishercoder/secondthousand/_1423Test.java new file mode 100644 index 0000000000..35993ff149 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1423Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1423; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1423Test { + private _1423.Solution1 solution1; + private _1423.Solution2 solution2; + private static int[] cardPoints; + private static int expected; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _1423.Solution1(); + solution2 = new _1423.Solution2(); + } + + @Test + public void test1() { + cardPoints = new int[] {1, 2, 3, 4, 5, 6, 1}; + expected = 12; + k = 3; + assertEquals(expected, solution1.maxScore(cardPoints, k)); + assertEquals(expected, solution2.maxScore(cardPoints, k)); + } + + @Test + public void test2() { + cardPoints = new int[] {96, 90, 41, 82, 39, 74, 64, 50, 30}; + expected = 536; + k = 8; + assertEquals(expected, solution1.maxScore(cardPoints, k)); + assertEquals(expected, solution2.maxScore(cardPoints, k)); + } + + @Test + public void test3() { + cardPoints = new int[] {100, 40, 17, 9, 73, 75}; + expected = 248; + k = 3; + assertEquals(expected, solution1.maxScore(cardPoints, k)); + assertEquals(expected, solution2.maxScore(cardPoints, k)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1424Test.java b/src/test/java/com/fishercoder/secondthousand/_1424Test.java new file mode 100644 index 0000000000..d0662b8efb --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1424Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1424; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1424Test { + private _1424.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1424.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {1, 4, 2, 7, 5, 3, 8, 6, 9}, + solution1.findDiagonalOrder( + Arrays.asList( + Arrays.asList(1, 2, 3), + Arrays.asList(4, 5, 6), + Arrays.asList(7, 8, 9)))); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {1, 6, 2, 8, 7, 3, 9, 4, 12, 10, 5, 13, 11, 14, 15, 16}, + solution1.findDiagonalOrder( + Arrays.asList( + Arrays.asList(1, 2, 3, 4, 5), + Arrays.asList(6, 7), + Arrays.asList(8), + Arrays.asList(9, 10, 11), + Arrays.asList(12, 13, 14, 15, 16)))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1426Test.java b/src/test/java/com/fishercoder/secondthousand/_1426Test.java new file mode 100644 index 0000000000..18c21d3f57 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1426Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1426; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1426Test { + private _1426.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1426.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {1, 1, 3, 3, 5, 5, 7, 7}; + assertEquals(0, solution1.countElements(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1427Test.java b/src/test/java/com/fishercoder/secondthousand/_1427Test.java new file mode 100644 index 0000000000..98a3fd7305 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1427Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1427; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1427Test { + private _1427.Solution1 solution1; + private static int[][] shift; + + @BeforeEach + public void setup() { + solution1 = new _1427.Solution1(); + } + + @Test + public void test1() { + shift = + new int[][] { + {0, 1}, + {1, 2}, + }; + assertEquals("cab", solution1.stringShift("abc", shift)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1428Test.java b/src/test/java/com/fishercoder/secondthousand/_1428Test.java new file mode 100644 index 0000000000..05f2410374 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1428Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.BinaryMatrix; +import com.fishercoder.common.classes.BinaryMatrixImpl; +import com.fishercoder.solutions.secondthousand._1428; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1428Test { + private _1428.Solution1 solution1; + private static BinaryMatrix binaryMatrix; + + @BeforeEach + public void setup() { + solution1 = new _1428.Solution1(); + } + + @Test + public void test1() { + binaryMatrix = + new BinaryMatrixImpl( + new int[][] { + {0, 0}, + {1, 1} + }); + assertEquals(0, solution1.leftMostColumnWithOne(binaryMatrix)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1431Test.java b/src/test/java/com/fishercoder/secondthousand/_1431Test.java new file mode 100644 index 0000000000..3722420667 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1431Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1431; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1431Test { + private _1431.Solution1 solution1; + private static int[] candies; + + @BeforeEach + public void setup() { + solution1 = new _1431.Solution1(); + } + + @Test + public void test1() { + candies = new int[] {2, 3, 5, 1, 3}; + assertEquals( + Arrays.asList(true, true, true, false, true), + solution1.kidsWithCandies(candies, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1432Test.java b/src/test/java/com/fishercoder/secondthousand/_1432Test.java new file mode 100644 index 0000000000..0fbaf30195 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1432Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1432; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1432Test { + private _1432.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1432.Solution1(); + } + + @Test + public void test1() { + assertEquals(888, solution1.maxDiff(555)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1436Test.java b/src/test/java/com/fishercoder/secondthousand/_1436Test.java new file mode 100644 index 0000000000..4b3623fe6f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1436Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1436; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1436Test { + private _1436.Solution1 solution1; + private static List> paths; + + @BeforeEach + public void setup() { + solution1 = new _1436.Solution1(); + } + + @Test + public void test1() { + paths = + Arrays.asList( + Arrays.asList("Lima", "Sao Paulo"), + Arrays.asList("New York", "Lima"), + Arrays.asList("London", "New York")); + assertEquals("Sao Paulo", solution1.destCity(paths)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1437Test.java b/src/test/java/com/fishercoder/secondthousand/_1437Test.java new file mode 100644 index 0000000000..538d9a7a94 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1437Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1437; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1437Test { + private _1437.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1437.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 0, 0, 0, 1, 0, 0, 1}; + assertEquals(true, solution1.kLengthApart(nums, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1438Test.java b/src/test/java/com/fishercoder/secondthousand/_1438Test.java new file mode 100644 index 0000000000..9452de4fea --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1438Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1438; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1438Test { + private _1438.Solution1 solution1; + private static int[] nums; + private static int limit; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _1438.Solution1(); + } + + @Test + public void test1() { + expected = 2; + nums = new int[] {8, 2, 4, 7}; + limit = 4; + assertEquals(expected, solution1.longestSubarray(nums, limit)); + } + + @Test + public void test2() { + expected = 4; + nums = new int[] {10, 1, 2, 4, 7, 2}; + limit = 5; + assertEquals(expected, solution1.longestSubarray(nums, limit)); + } + + @Test + public void test3() { + expected = 3; + nums = new int[] {4, 2, 2, 2, 4, 4, 2, 2}; + limit = 0; + assertEquals(expected, solution1.longestSubarray(nums, limit)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1439Test.java b/src/test/java/com/fishercoder/secondthousand/_1439Test.java new file mode 100644 index 0000000000..6c468a1410 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1439Test.java @@ -0,0 +1,60 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1439; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1439Test { + private _1439.Solution1 solution1; + private static int[][] mat; + private static int expected; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _1439.Solution1(); + } + + @Test + public void test1() { + mat = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,3,11],[2,4,6]"); + expected = 7; + k = 5; + assertEquals(expected, solution1.kthSmallest(mat, k)); + } + + @Test + public void test2() { + mat = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,3,11],[2,4,6]"); + expected = 17; + k = 9; + assertEquals(expected, solution1.kthSmallest(mat, k)); + } + + @Test + public void test3() { + mat = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,10,10],[1,4,5],[2,3,6]"); + expected = 9; + k = 7; + assertEquals(expected, solution1.kthSmallest(mat, k)); + } + + @Test + public void test4() { + mat = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,1,10],[2,2,9]"); + expected = 12; + k = 7; + assertEquals(expected, solution1.kthSmallest(mat, k)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1441Test.java b/src/test/java/com/fishercoder/secondthousand/_1441Test.java new file mode 100644 index 0000000000..448e1b2a30 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1441Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1441; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1441Test { + private _1441.Solution1 solution1; + private static int[] target; + + @BeforeEach + public void setup() { + solution1 = new _1441.Solution1(); + } + + @Test + public void test1() { + target = new int[] {1, 3}; + assertEquals(Arrays.asList("Push", "Push", "Pop", "Push"), solution1.buildArray(target, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1446Test.java b/src/test/java/com/fishercoder/secondthousand/_1446Test.java new file mode 100644 index 0000000000..5753ac2279 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1446Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1446; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1446Test { + private _1446.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1446.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.maxPower("leetcode")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1447Test.java b/src/test/java/com/fishercoder/secondthousand/_1447Test.java new file mode 100644 index 0000000000..c9e47d63e3 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1447Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1447; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1447Test { + private _1447.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1447.Solution1(); + } + + @Test + public void test1() { + assertEquals(Arrays.asList("1/2"), solution1.simplifiedFractions(2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1448Test.java b/src/test/java/com/fishercoder/secondthousand/_1448Test.java new file mode 100644 index 0000000000..66dca235b1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1448Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1448; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _1448Test { + private _1448.Solution1 solution1; + private static TreeNode root; + + @Test + public void test1() { + solution1 = new _1448.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, 4, 3, null, 1, 5)); + assertEquals(4, solution1.goodNodes(root)); + } + + @Test + public void test2() { + solution1 = new _1448.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(3, 3, null, 4, 2)); + assertEquals(3, solution1.goodNodes(root)); + } + + @Test + public void test3() { + solution1 = new _1448.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(1)); + assertEquals(1, solution1.goodNodes(root)); + } + + @Test + public void test4() { + solution1 = new _1448.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(2, null, 4, 10, 8, null, null, 4)); + assertEquals(4, solution1.goodNodes(root)); + } + + @Test + public void test5() { + solution1 = new _1448.Solution1(); + root = TreeUtils.constructBinaryTree(Arrays.asList(9, null, 3, 6)); + assertEquals(1, solution1.goodNodes(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1450Test.java b/src/test/java/com/fishercoder/secondthousand/_1450Test.java new file mode 100644 index 0000000000..32c84e6b98 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1450Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1450; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1450Test { + private _1450.Solution1 solution1; + private static int[] startTime; + private static int[] endTime; + + @BeforeEach + public void setup() { + solution1 = new _1450.Solution1(); + } + + @Test + public void test1() { + startTime = new int[] {1, 2, 3}; + endTime = new int[] {3, 2, 7}; + assertEquals(1, solution1.busyStudent(startTime, endTime, 4)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1451Test.java b/src/test/java/com/fishercoder/secondthousand/_1451Test.java new file mode 100644 index 0000000000..472b600a06 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1451Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1451; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1451Test { + private _1451.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1451.Solution1(); + } + + @Test + public void test1() { + assertEquals("Is cool leetcode", solution1.arrangeWords("Leetcode is cool")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1452Test.java b/src/test/java/com/fishercoder/secondthousand/_1452Test.java new file mode 100644 index 0000000000..455100f19a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1452Test.java @@ -0,0 +1,59 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1452; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1452Test { + private _1452.Solution1 solution1; + private static List> favoriteCompanies; + + @BeforeEach + public void setup() { + solution1 = new _1452.Solution1(); + } + + @Test + public void test1() { + favoriteCompanies = new ArrayList<>(); + favoriteCompanies.add(Arrays.asList("leetcode", "google", "facebook")); + favoriteCompanies.add(Arrays.asList("google", "microsoft")); + favoriteCompanies.add(Arrays.asList("google", "facebook")); + favoriteCompanies.add(Arrays.asList("google")); + favoriteCompanies.add(Arrays.asList("amazon")); + assertEquals(Arrays.asList(0, 1, 4), solution1.peopleIndexes(favoriteCompanies)); + } + + @Test + public void test2() { + favoriteCompanies = new ArrayList<>(); + favoriteCompanies.add( + Arrays.asList( + "nxaqhyoprhlhvhyojanr", + "ovqdyfqmlpxapbjwtssm", + "qmsbphxzmnvflrwyvxlc", + "udfuxjdxkxwqnqvgjjsp", + "yawoixzhsdkaaauramvg", + "zycidpyopumzgdpamnty")); // 6 + favoriteCompanies.add( + Arrays.asList( + "nxaqhyoprhlhvhyojanr", + "ovqdyfqmlpxapbjwtssm", + "udfuxjdxkxwqnqvgjjsp", + "yawoixzhsdkaaauramvg", + "zycidpyopumzgdpamnty")); // 5 + favoriteCompanies.add( + Arrays.asList( + "ovqdyfqmlpxapbjwtssm", + "qmsbphxzmnvflrwyvxlc", + "udfuxjdxkxwqnqvgjjsp", + "yawoixzhsdkaaauramvg", + "zycidpyopumzgdpamnty")); // 5 + assertEquals(Arrays.asList(0), solution1.peopleIndexes(favoriteCompanies)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1455Test.java b/src/test/java/com/fishercoder/secondthousand/_1455Test.java new file mode 100644 index 0000000000..216b93b41c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1455Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1455; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1455Test { + private _1455.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1455.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.isPrefixOfWord("i love eating burger", "burg")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1456Test.java b/src/test/java/com/fishercoder/secondthousand/_1456Test.java new file mode 100644 index 0000000000..a001e5fc20 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1456Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1456; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1456Test { + private _1456.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1456.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.maxVowels("abciiidef", 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1457Test.java b/src/test/java/com/fishercoder/secondthousand/_1457Test.java new file mode 100644 index 0000000000..35f96690cd --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1457Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1457; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1457Test { + private _1457.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1457.Solution1(); + } + + @Test + public void test1() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(2, 3, 1, 3, 1, null, 1)); + assertEquals(2, solution1.pseudoPalindromicPaths(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1460Test.java b/src/test/java/com/fishercoder/secondthousand/_1460Test.java new file mode 100644 index 0000000000..ec0c6e1ad1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1460Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1460; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1460Test { + private _1460.Solution1 solution1; + private static int[] target; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1460.Solution1(); + } + + @Test + public void test1() { + target = new int[] {1, 2, 3, 4}; + arr = new int[] {2, 4, 1, 3}; + assertEquals(true, solution1.canBeEqual(target, arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1461Test.java b/src/test/java/com/fishercoder/secondthousand/_1461Test.java new file mode 100644 index 0000000000..a724aa9718 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1461Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1461; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1461Test { + private _1461.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1461.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.hasAllCodes("00110110", 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1462Test.java b/src/test/java/com/fishercoder/secondthousand/_1462Test.java new file mode 100644 index 0000000000..28eef6bca4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1462Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1462; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1462Test { + private _1462.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1462.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList(false, true), + solution1.checkIfPrerequisite( + 3, + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[1,0],[2,0]"), + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[0,1],[2,0]"))); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList(true, true), + solution1.checkIfPrerequisite( + 3, + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[1,2],[1,0],[2,0]"), + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[1,0],[1,2]"))); + } + + @Test + public void test3() { + assertEquals( + Arrays.asList( + true, false, true, true, true, true, true, true, false, false, true, true, + false, false, true, true, true, true, false, false, true, false, true, + false, true, false, true, true, false, true, true, false, false, true, + false, false, true, true, true, false), + solution1.checkIfPrerequisite( + 7, + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[2,3],[2,1],[2,0],[3,4],[3,6],[5,1],[5,0],[1,4],[1,0],[4,0],[0,6]"), + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[3,0],[6,4],[5,6],[2,6],[2,3],[5,6],[4,0],[2,6],[3,5],[5,3],[1,6],[1,0],[3,5],[6,5],[2,3],[3,0],[3,4],[3,4],[2,5],[0,3],[4,0],[6,4],[5,0],[6,5],[5,6],[6,5],[1,0],[3,4],[1,5],[1,4],[3,6],[0,1],[1,2],[5,1],[5,3],[5,3],[3,4],[5,4],[5,4],[5,3]"))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1464Test.java b/src/test/java/com/fishercoder/secondthousand/_1464Test.java new file mode 100644 index 0000000000..90ceb195fa --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1464Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1464; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1464Test { + private _1464.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1464.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {3, 4, 5, 2}; + assertEquals(12, solution1.maxProduct(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1466Test.java b/src/test/java/com/fishercoder/secondthousand/_1466Test.java new file mode 100644 index 0000000000..1e77c533c0 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1466Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1466; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1466Test { + private _1466.Solution1 solution1; + private _1466.Solution2 solution2; + private static int[][] connections; + + @BeforeEach + public void setup() { + solution1 = new _1466.Solution1(); + solution2 = new _1466.Solution2(); + } + + @Test + public void test1() { + connections = new int[][] {{0, 1}, {1, 3}, {2, 3}, {4, 0}, {4, 5}}; + assertEquals(3, solution1.minReorder(6, connections)); + } + + @Test + public void test2() { + connections = new int[][] {{0, 1}, {1, 3}, {2, 3}, {4, 0}, {4, 5}}; + assertEquals(3, solution2.minReorder(6, connections)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1469Test.java b/src/test/java/com/fishercoder/secondthousand/_1469Test.java new file mode 100644 index 0000000000..2d18b767c8 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1469Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1469; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1469Test { + private _1469.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1469.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList(4), + solution1.getLonelyNodes( + TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4)))); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList(6, 2), + solution1.getLonelyNodes( + TreeUtils.constructBinaryTree( + Arrays.asList( + 7, 1, 4, 6, null, 5, 3, null, null, null, null, null, 2)))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1470Test.java b/src/test/java/com/fishercoder/secondthousand/_1470Test.java new file mode 100644 index 0000000000..40d0e67851 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1470Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1470; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1470Test { + private _1470.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1470.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 5, 1, 3, 4, 7}; + assertArrayEquals(new int[] {2, 3, 5, 4, 1, 7}, solution1.shuffle(nums, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1471Test.java b/src/test/java/com/fishercoder/secondthousand/_1471Test.java new file mode 100644 index 0000000000..66870118ea --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1471Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1471; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1471Test { + private _1471.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1471.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3, 4, 5}; + assertArrayEquals(new int[] {5, 1}, solution1.getStrongest(nums, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1472Test.java b/src/test/java/com/fishercoder/secondthousand/_1472Test.java new file mode 100644 index 0000000000..da38f127aa --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1472Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1472; +import org.junit.jupiter.api.Test; + +public class _1472Test { + private _1472.Solution1.BrowserHistory browserHistory; + + @Test + public void test1() { + browserHistory = new _1472.Solution1.BrowserHistory("leetcode.com"); + browserHistory.visit("google.com"); + browserHistory.visit("facebook.com"); + browserHistory.visit("youtube.com"); + assertEquals("facebook.com", browserHistory.back(1)); + assertEquals("google.com", browserHistory.back(1)); + assertEquals("facebook.com", browserHistory.forward(1)); + browserHistory.visit("linkedin.com"); + assertEquals("linkedin.com", browserHistory.forward(2)); + assertEquals("google.com", browserHistory.back(2)); + assertEquals("leetcode.com", browserHistory.back(7)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1474Test.java b/src/test/java/com/fishercoder/secondthousand/_1474Test.java new file mode 100644 index 0000000000..e6c37eb7ea --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1474Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.secondthousand._1474; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1474Test { + private _1474.Solution1 solution1; + private static ListNode head; + + @BeforeEach + public void setup() { + solution1 = new _1474.Solution1(); + } + + @Test + public void test1() { + head = + LinkedListUtils.contructLinkedList( + new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}); + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {1, 2, 6, 7, 11, 12}), + solution1.deleteNodes(head, 2, 3)); + } + + @Test + public void test2() { + head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {1, 5, 9}), + solution1.deleteNodes(head, 1, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1475Test.java b/src/test/java/com/fishercoder/secondthousand/_1475Test.java new file mode 100644 index 0000000000..8c920fe8a6 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1475Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1475; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1475Test { + private _1475.Solution1 solution1; + private static int[] prices; + + @BeforeEach + public void setup() { + solution1 = new _1475.Solution1(); + } + + @Test + public void test1() { + prices = new int[] {8, 4, 6, 2, 3}; + assertArrayEquals(new int[] {4, 2, 4, 2, 3}, solution1.finalPrices(prices)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1476Test.java b/src/test/java/com/fishercoder/secondthousand/_1476Test.java new file mode 100644 index 0000000000..80931ad63d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1476Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1476; +import org.junit.jupiter.api.Test; + +public class _1476Test { + private _1476.Solution1.SubrectangleQueries solution1; + private static int[][] rectangle; + + @Test + public void test1() { + rectangle = + new int[][] { + {1, 2, 1}, + {4, 3, 4}, + {3, 2, 1}, + {1, 1, 1} + }; + solution1 = new _1476.Solution1.SubrectangleQueries(rectangle); + assertEquals(1, solution1.getValue(0, 2)); + solution1.updateSubrectangle(0, 0, 3, 2, 5); + assertEquals(5, solution1.getValue(0, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1480Test.java b/src/test/java/com/fishercoder/secondthousand/_1480Test.java new file mode 100644 index 0000000000..1a37e2e62e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1480Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1480; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1480Test { + private _1480.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1480.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 2, 3, 4}; + assertArrayEquals(new int[] {1, 3, 6, 10}, solution1.runningSum(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1481Test.java b/src/test/java/com/fishercoder/secondthousand/_1481Test.java new file mode 100644 index 0000000000..e12ba4457d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1481Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1481; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1481Test { + private _1481.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1481.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {5, 5, 4}; + assertEquals(1, solution1.findLeastNumOfUniqueInts(arr, 1)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1482Test.java b/src/test/java/com/fishercoder/secondthousand/_1482Test.java new file mode 100644 index 0000000000..2128c8d339 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1482Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1482; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1482Test { + private _1482.Solution1 solution1; + private static int expected; + private static int[] bloomDay; + private static int m; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _1482.Solution1(); + } + + @Test + public void test1() { + expected = 3; + bloomDay = new int[] {1, 10, 3, 10, 2}; + m = 3; + k = 1; + assertEquals(expected, solution1.minDays(bloomDay, m, k)); + } + + @Test + public void test2() { + expected = -1; + bloomDay = new int[] {1, 10, 3, 10, 2}; + m = 3; + k = 2; + assertEquals(expected, solution1.minDays(bloomDay, m, k)); + } + + @Test + public void test3() { + expected = 9; + bloomDay = new int[] {1, 10, 2, 9, 3, 8, 4, 7, 5, 6}; + m = 4; + k = 2; + assertEquals(expected, solution1.minDays(bloomDay, m, k)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1485Test.java b/src/test/java/com/fishercoder/secondthousand/_1485Test.java new file mode 100644 index 0000000000..f51ae82a5f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1485Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.solutions.secondthousand._1485; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1485Test { + + private _1485.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1485.Solution1(); + } + + @Test + public void test1() { + _1485.Node root = new _1485.Node(1); + _1485.Node node1 = new _1485.Node(4); + _1485.Node node2 = new _1485.Node(7); + root.right = node1; + node1.left = node2; + node1.random = node2; + node2.random = root; + _1485.NodeCopy actual = solution1.copyRandomBinaryTree(root); + System.out.println("Finished."); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1486Test.java b/src/test/java/com/fishercoder/secondthousand/_1486Test.java new file mode 100644 index 0000000000..a479da36dd --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1486Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1486; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1486Test { + private _1486.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1486.Solution1(); + } + + @Test + public void test1() { + assertEquals(8, solution1.xorOperation(5, 0)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1487Test.java b/src/test/java/com/fishercoder/secondthousand/_1487Test.java new file mode 100644 index 0000000000..249a437690 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1487Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1487; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1487Test { + private _1487.Solution1 solution1; + private static String[] names; + + @BeforeEach + public void setup() { + solution1 = new _1487.Solution1(); + } + + @Test + public void test1() { + names = new String[] {"pes", "fifa", "gta", "pes(2019)"}; + assertArrayEquals( + new String[] {"pes", "fifa", "gta", "pes(2019)"}, solution1.getFolderNames(names)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1490Test.java b/src/test/java/com/fishercoder/secondthousand/_1490Test.java new file mode 100644 index 0000000000..87600a51ef --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1490Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.Node; +import com.fishercoder.solutions.secondthousand._1490; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1490Test { + private _1490.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1490.Solution1(); + } + + @Test + public void test1() { + Node root = new Node(8); + Node child = new Node(1); + root.children.add(child); + Node leftGrandChild = new Node(8); + Node rightGrandChild = new Node(5); + child.children.add(leftGrandChild); + child.children.add(rightGrandChild); + assertEquals(root, solution1.cloneTree(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1491Test.java b/src/test/java/com/fishercoder/secondthousand/_1491Test.java new file mode 100644 index 0000000000..636ec664d2 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1491Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1491; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1491Test { + private _1491.Solution1 solution1; + private static int[] salary; + + @BeforeEach + public void setup() { + solution1 = new _1491.Solution1(); + } + + @Test + public void test1() { + salary = new int[] {4000, 3000, 1000, 2000}; + assertEquals(2500.0000, solution1.average(salary)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1492Test.java b/src/test/java/com/fishercoder/secondthousand/_1492Test.java new file mode 100644 index 0000000000..859435f4ec --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1492Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1492; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1492Test { + private _1492.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1492.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.kthFactor(12, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1493Test.java b/src/test/java/com/fishercoder/secondthousand/_1493Test.java new file mode 100644 index 0000000000..2d3b90cdc1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1493Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1493; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1493Test { + private _1493.Solution1 solution1; + private _1493.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1493.Solution1(); + solution2 = new _1493.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 1, 0, 1}; + assertEquals(3, solution1.longestSubarray(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 1, 0, 1}; + assertEquals(3, solution2.longestSubarray(nums)); + } + + @Test + public void test3() { + nums = new int[] {0, 1, 1, 1, 0, 1, 1, 0, 1}; + assertEquals(5, solution2.longestSubarray(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1496Test.java b/src/test/java/com/fishercoder/secondthousand/_1496Test.java new file mode 100644 index 0000000000..5f7edc4323 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1496Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1496; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1496Test { + private _1496.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1496.Solution1(); + } + + @Test + public void test1() { + assertEquals(false, solution1.isPathCrossing("NES")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1502Test.java b/src/test/java/com/fishercoder/secondthousand/_1502Test.java new file mode 100644 index 0000000000..89a612829a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1502Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1502; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1502Test { + private _1502.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1502.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {3, 5, 1}; + assertEquals(true, solution1.canMakeArithmeticProgression(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1507Test.java b/src/test/java/com/fishercoder/secondthousand/_1507Test.java new file mode 100644 index 0000000000..adc6105e74 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1507Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1507; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1507Test { + private _1507.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1507.Solution1(); + } + + @Test + public void test1() { + assertEquals("2052-10-20", solution1.reformatDate("20th Oct 2052")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1508Test.java b/src/test/java/com/fishercoder/secondthousand/_1508Test.java new file mode 100644 index 0000000000..6baef1bc88 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1508Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1508; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1508Test { + private _1508.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1508.Solution1(); + } + + @Test + public void test1() { + assertEquals(13, solution1.rangeSum(new int[] {1, 2, 3, 4}, 4, 1, 5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1509Test.java b/src/test/java/com/fishercoder/secondthousand/_1509Test.java new file mode 100644 index 0000000000..022fe68cac --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1509Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1509; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1509Test { + private _1509.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1509.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minDifference(new int[] {6, 6, 0, 1, 1, 4, 6})); + } + + @Test + public void test2() { + assertEquals(1, solution1.minDifference(new int[] {82, 81, 95, 75, 20})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1512Test.java b/src/test/java/com/fishercoder/secondthousand/_1512Test.java new file mode 100644 index 0000000000..f1d64261e8 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1512Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1512; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1512Test { + private _1512.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1512.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.numIdenticalPairs(new int[] {1, 2, 3, 1, 1, 3})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1514Test.java b/src/test/java/com/fishercoder/secondthousand/_1514Test.java new file mode 100644 index 0000000000..9c34abb670 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1514Test.java @@ -0,0 +1,255 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1514; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1514Test { + private _1514.Solution1 solution1; + private static int[][] edges; + private static double[] succProb; + + @BeforeEach + public void setup() { + solution1 = new _1514.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 0.25, + solution1.maxProbability( + 3, + new int[][] { + {0, 1}, + {1, 2}, + {0, 2} + }, + new double[] {0.5, 0.5, 0.2}, + 0, + 2)); + } + + @Test + public void test2() { + assertEquals( + 0.3, + solution1.maxProbability( + 3, + new int[][] { + {0, 1}, + {1, 2}, + {0, 2} + }, + new double[] {0.5, 0.5, 0.3}, + 0, + 2)); + } + + @Test + public void test3() { + assertEquals( + 0.0, solution1.maxProbability(3, new int[][] {{0, 1}}, new double[] {0.5}, 0, 2)); + } + + @Test + public void test4() { + assertEquals( + 0.16, + solution1.maxProbability( + 5, + new int[][] { + {2, 3}, + {1, 2}, + {3, 4}, + {1, 3}, + {1, 4}, + {0, 1}, + {2, 4}, + {0, 4}, + {0, 2}, + }, + new double[] {0.06, 0.26, 0.49, 0.25, 0.2, 0.64, 0.23, 0.21, 0.77}, + 0, + 3)); + } + + @Test + public void test5() { + assertEquals( + 0.21390, + solution1.maxProbability( + 5, + new int[][] { + {1, 4}, + {2, 4}, + {0, 4}, + {0, 3}, + {0, 2}, + {2, 3}, + }, + new double[] {0.37, 0.17, 0.93, 0.23, 0.39, 0.04}, + 3, + 4)); + } + + @Test + public void test6() { + edges = + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[448,931],[234,889],[214,962],[576,746],[678,734],[214,928],[602,779],[190,968],[227,858],[714,842],[177,345],[705,994],[365,998],[307,336],[123,914],[398,487],[112,234],[44,357],[318,506],[311,926],[559,735],[28,299],[689,723],[29,566],[355,476],[507,813],[799,841],[166,581],[499,522],[155,508],[80,954],[412,564],[502,618],[59,746],[272,400],[75,312],[510,887],[303,524],[646,845],[786,928],[124,151],[109,858],[96,762],[291,798],[69,303],[27,112],[292,774],[257,384],[59,755],[140,245],[431,769],[60,338],[173,403],[95,666],[165,384],[298,894],[963,980],[325,945],[419,440],[338,424],[344,846],[396,449],[76,242],[620,981],[264,433],[580,686],[196,682],[272,926],[223,593],[644,785],[487,924],[289,511],[714,988],[625,987],[50,362],[88,664],[233,352],[32,754],[206,961],[641,810],[301,570],[77,523],[26,109],[482,580],[528,683],[128,228],[436,452],[253,844],[126,877],[462,994],[204,337],[380,625],[179,807],[635,726],[143,748],[594,798],[972,996],[328,780],[267,831],[176,399],[257,600],[495,735],[844,893],[102,803],[62,942],[354,903],[234,301],[306,854],[63,555],[39,179],[125,749],[414,487],[80,291],[416,835],[77,951],[10,384],[637,798],[248,966],[646,879],[210,839],[675,876],[580,990],[187,245],[18,876],[881,933],[422,747],[422,432],[635,742],[813,976],[719,900],[149,672],[518,999],[342,746],[121,262],[457,876],[534,984],[219,524],[192,228],[636,671],[196,835],[323,658],[360,747],[643,969],[95,414],[199,325],[169,471],[50,235],[307,517],[500,927],[226,886],[131,962],[65,313],[470,514],[851,987],[437,665],[284,620],[468,752],[54,781],[266,885],[362,825],[0,90],[14,619],[259,686],[171,180],[249,520],[240,245],[225,264],[128,372],[198,383],[306,422],[46,376],[107,797],[746,961],[401,474],[346,435],[241,355],[109,919],[497,541],[271,871],[329,953],[376,541],[564,626],[91,514],[8,610],[595,865],[888,971],[852,905],[532,974],[211,653],[288,410],[463,501],[258,987],[99,515],[494,780],[562,891],[392,620],[293,409],[161,250],[460,527],[801,939],[275,929],[76,553],[236,555],[192,257],[497,604],[140,931],[224,845],[159,339],[328,902],[63,658],[231,626],[862,947],[305,469],[109,426],[216,499],[156,162],[297,685],[101,719],[524,978],[794,914],[933,950],[859,982],[626,929],[162,685],[252,904],[95,837],[293,705],[117,120],[334,880],[19,937],[304,989],[391,800],[54,80],[266,970],[99,916],[34,819],[163,348],[507,725],[295,826],[99,308],[378,463],[799,833],[389,975],[699,709],[836,967],[38,990],[586,871],[664,958],[840,990],[333,379],[71,282],[487,778],[766,845],[225,732],[446,703],[672,762],[342,512],[693,862],[80,316],[325,836],[118,738],[278,297],[107,205],[442,743],[715,812],[40,660],[138,272],[234,941],[804,812],[459,631],[45,798],[246,556],[396,797],[817,894],[548,603],[233,613],[386,742],[215,974],[102,628],[44,555],[210,281],[191,270],[119,979],[613,995],[794,987],[151,814],[621,719],[322,986],[144,200],[625,653],[574,632],[123,735],[528,612],[344,351],[203,298],[357,763],[303,357],[55,555],[209,916],[97,979],[602,994],[74,104],[94,665],[561,884],[202,843],[849,876],[630,683],[37,315],[335,705],[63,569],[76,594],[377,984],[246,735],[49,328],[29,380],[394,397],[66,158],[270,648],[581,944],[304,480],[161,459],[626,782],[169,403],[19,904],[289,387],[200,402],[276,608],[45,662],[339,569],[103,673],[328,602],[328,905],[438,910],[675,679],[125,313],[383,656],[179,266],[807,968],[176,946],[250,466],[106,295],[409,627],[399,708],[350,812],[54,363],[482,774],[217,411],[58,73],[865,912],[387,554],[21,876],[263,374],[784,969],[391,997],[170,181],[56,163],[510,575],[159,925],[14,532],[605,699],[834,845],[119,835],[522,931],[341,749],[361,469],[187,437],[78,613],[814,950],[443,996],[542,876],[378,694],[170,183],[560,803],[320,486],[50,530],[817,941],[209,521],[258,322],[235,540],[595,950],[191,497],[16,953],[299,436],[236,568],[160,298],[812,874],[173,916],[731,770],[341,768],[76,956],[788,858],[67,639],[331,674],[693,792],[62,188],[555,626],[313,473],[172,470],[245,250],[10,116],[754,976],[665,694],[530,947],[506,785],[752,854],[437,788],[61,731],[361,926],[318,909],[405,470],[331,919],[577,589],[931,976],[288,746],[151,340],[279,654],[397,523],[113,496],[318,807],[84,955],[290,637],[517,966],[687,858],[342,741],[238,554],[809,924],[76,162],[941,975],[109,452],[21,663],[207,583],[670,838],[150,558],[801,874],[318,483],[286,377],[173,216],[111,431],[463,489],[630,884],[623,782],[193,305],[8,690],[476,937],[35,938],[159,317],[96,977],[198,488],[460,461],[537,607],[426,451],[42,90],[488,794],[56,819],[43,66],[96,200],[383,743],[293,299],[119,218],[531,720],[432,582],[338,888],[560,700],[619,747],[400,488],[569,968],[519,569],[284,628],[32,438],[369,706],[282,283],[645,959],[129,381],[667,725],[313,549],[9,66],[495,619],[393,729],[425,888],[26,390],[145,568],[126,288],[318,418],[115,695],[215,449],[521,645],[228,962],[180,838],[53,318],[41,820],[772,801],[292,729],[138,835],[538,557],[588,698],[85,169],[503,883],[499,603],[542,954],[439,727],[514,923],[291,843],[269,875],[645,672],[535,825],[19,279],[121,962],[60,240],[181,902],[110,907],[649,995],[30,687],[481,678],[147,300],[663,810],[392,742],[345,568],[600,848],[732,815],[320,717],[577,994],[454,790],[427,491],[43,983],[83,172],[308,398],[391,817],[575,629],[393,931],[601,797],[485,685],[41,95],[139,463],[507,549],[843,980],[342,652],[111,972],[167,309],[71,834],[386,418],[57,991],[133,715],[692,835],[376,513],[164,308],[851,877],[581,774],[755,849],[608,900],[360,409],[21,507],[128,680],[252,965],[83,936],[572,871],[309,378],[80,232],[714,855],[489,559],[146,996],[533,549],[189,401],[288,312],[196,202],[268,408],[213,522],[486,817],[231,402],[14,804],[825,897],[408,594],[524,618],[10,487],[262,860],[301,862],[246,634],[582,969],[284,976],[271,286],[397,606],[239,422],[432,443],[359,907],[355,826],[268,468],[173,451],[356,854],[546,992],[170,411],[486,758],[84,771],[868,898],[149,735],[767,833],[12,102],[302,509],[414,711],[970,991],[83,771],[97,715],[389,595],[215,374],[182,381],[313,453],[531,835],[461,666],[496,596],[58,241],[334,996],[526,987],[263,567],[200,883],[73,419],[58,293],[553,785],[502,593],[462,475],[606,662],[84,107],[698,720],[99,672],[528,817],[260,582],[563,773],[187,305],[253,752],[152,981],[379,410],[30,515],[248,439],[217,406],[113,127],[332,498],[142,878],[136,396],[228,388],[11,884],[42,255],[4,175],[660,860],[521,863],[69,328],[796,817],[92,464],[142,217],[214,691],[981,989],[354,895],[268,669],[80,524],[703,723],[129,292],[141,216],[634,807],[350,625],[53,151],[106,708],[2,872],[93,723],[35,984],[778,829],[521,583],[95,607],[342,933],[425,983],[71,89],[3,94],[448,676],[362,822],[233,740],[145,786],[2,784],[47,974],[287,981],[565,711],[34,138],[312,605],[566,879],[335,740],[255,878],[657,987],[207,781],[235,865],[435,808],[292,588],[126,196],[834,988],[530,961],[536,709],[461,824],[394,577],[192,832],[525,752],[297,725],[33,35],[257,838],[65,276],[402,876],[478,747],[692,801],[61,809],[466,550],[261,412],[178,608],[134,266],[611,765],[45,740],[6,719],[154,406],[268,662],[46,233],[761,977],[74,370],[151,581],[21,753],[268,995],[25,573],[772,937],[27,181],[275,556],[11,45],[375,915],[649,991],[515,616],[123,987],[522,544],[320,488],[210,370],[101,702],[216,659],[396,812],[657,911],[672,674],[14,540],[140,580],[403,835],[230,608],[120,315],[275,304],[806,973],[49,796],[398,729],[527,772],[113,674],[154,452],[233,971],[362,480],[467,509],[249,797],[33,666],[9,991],[219,576],[136,857],[911,945],[521,791],[98,949],[337,507],[446,522],[589,891],[578,609],[835,987],[99,464],[192,845],[10,731],[479,506],[286,456],[137,677],[211,239],[116,161],[699,752],[20,251],[692,893],[580,957],[636,837],[180,972],[424,546],[317,331],[175,915],[19,187],[360,862],[43,944],[322,849],[614,665],[85,985],[156,337],[401,751],[202,327],[250,836],[557,788],[470,988],[4,282],[683,932],[491,534],[765,888],[19,235],[127,843],[339,677],[108,190],[122,199],[213,886],[383,742],[526,932],[163,678],[167,271],[643,914],[271,644],[187,572],[122,679],[398,985],[290,905],[487,741],[81,493],[639,713],[311,790],[3,47],[150,844],[585,979],[283,316],[232,271],[59,616],[233,858],[143,398],[308,966],[452,879],[467,845],[87,674],[464,604],[101,141],[144,972],[372,650],[796,982],[39,568],[95,294],[327,633],[890,962],[282,407],[281,326],[352,788],[570,902],[757,921],[531,784],[236,284],[445,865],[360,724],[317,761],[66,328],[194,340],[409,562],[362,688],[569,876],[195,953],[855,918],[416,864],[213,273],[269,947],[63,529],[833,916],[28,914],[830,940],[203,303],[159,974],[551,819],[300,618],[290,553],[518,921],[158,455],[835,947],[252,508],[117,260],[305,376],[335,465],[96,445],[210,513],[556,644],[300,547],[72,928],[253,558],[343,585],[93,515],[535,810],[385,741],[392,965],[99,141],[188,535],[19,921],[241,596],[141,300],[321,732],[697,727],[170,925],[151,745],[616,856],[383,465],[311,697],[306,695],[160,856],[22,596],[258,718],[194,906],[632,749],[427,987],[307,356],[23,888],[375,968],[186,313],[135,431],[27,439],[331,931],[444,991],[477,675],[728,740],[596,868],[307,857],[223,463],[214,470],[244,263],[610,711],[198,773],[241,984],[335,940],[12,677],[358,538],[675,761],[560,825],[355,929],[821,983],[83,571],[513,702],[341,476],[475,868],[334,352],[811,956],[233,295],[43,557],[487,817],[519,829],[470,728],[574,754],[54,857],[144,828],[140,254],[556,859],[165,868],[317,909],[43,263],[323,380],[119,239],[356,554],[44,511],[626,915],[205,389],[166,816],[521,899],[98,773],[338,343],[79,355],[260,798],[209,850],[166,176],[804,820],[296,805],[85,338],[406,608],[97,954],[201,775],[681,890],[33,601],[251,834],[776,956],[138,551],[195,924],[112,137],[862,987],[461,806],[19,228],[354,647],[257,984],[499,971],[33,237],[30,541],[151,727],[337,529],[25,386],[47,300],[548,582],[302,312],[7,868],[66,117],[154,622],[462,594],[622,752],[641,710],[527,760],[152,536],[406,879],[200,331],[98,866],[245,503],[285,894],[73,583],[2,323],[62,419],[137,407],[199,461],[771,865],[515,721],[168,243],[629,655],[298,432],[442,562],[688,784],[492,747],[638,831],[86,284],[177,514],[633,894],[180,343],[253,830],[208,604],[884,967],[531,592],[131,644],[6,185],[174,319],[169,266],[11,272],[236,897],[232,484],[442,796],[108,642],[173,514],[133,418],[305,807],[8,858],[420,811],[219,246],[305,648],[443,791],[356,828],[76,353],[19,156],[263,631],[126,377],[208,726],[449,814],[236,792],[7,207],[144,156],[143,532],[181,775],[61,125],[266,568],[469,569],[293,797],[299,665],[357,437],[732,916],[231,736],[635,915],[378,632],[83,790],[450,731],[722,894],[678,795],[386,710],[325,411],[131,491],[840,886],[730,761],[401,938],[71,660],[278,426],[668,770],[522,556],[585,864],[429,597],[18,933],[335,618],[220,934],[676,944],[217,548],[413,764],[271,479],[657,804],[56,510],[354,366],[738,904],[117,796],[555,674],[214,684],[285,996],[105,309],[395,558],[153,388],[656,756],[143,688],[341,587],[810,827],[310,648],[3,992],[334,943],[367,768],[376,711],[385,864],[93,472],[473,706],[597,924],[694,845],[47,522],[155,184],[270,718],[213,525],[896,948],[276,673],[115,874],[485,887],[760,825],[66,95],[691,874],[62,787],[440,594],[79,356],[640,672],[527,840],[44,596],[431,762],[16,455],[682,975],[353,567],[731,748],[242,820],[55,387],[476,562],[516,906],[247,834],[652,989],[656,742],[35,962],[310,610],[431,992],[660,679],[440,915],[190,505],[87,566],[418,483],[581,881],[328,681],[83,366],[30,900],[64,432],[134,710],[200,452],[256,440],[575,893],[530,756],[71,666],[739,900],[289,566],[489,575],[196,985],[191,646],[427,697],[231,500],[185,953],[29,134],[80,236],[28,582],[330,724],[690,886],[198,898],[473,681],[439,790],[95,573],[100,942],[460,615],[182,283],[264,380],[424,606],[115,534],[352,792],[34,655],[644,902],[35,724],[400,934],[377,390],[123,257],[257,735],[447,453],[194,593],[190,256],[362,889],[192,993],[210,508],[8,437],[229,428],[2,124],[73,448],[618,763],[469,717],[487,830],[90,700],[111,878],[562,989],[233,252],[340,687],[143,536],[82,202],[145,749],[808,962],[43,405],[340,726],[526,742],[194,889],[553,656],[173,541],[158,905],[264,781],[223,418],[130,598],[93,442],[420,631],[178,556],[40,158],[415,700],[174,520],[454,981],[795,980],[687,759],[651,715],[325,598],[292,715],[175,987],[85,165],[437,807],[719,949],[184,977],[403,725],[309,771],[284,797],[6,512],[41,929],[524,660],[165,229],[741,756],[3,536],[663,752],[291,567],[482,591],[367,428],[720,721],[448,604],[459,525],[185,254],[380,918],[752,841],[64,544],[595,869],[469,559],[122,672],[271,776],[489,770],[26,786],[270,807],[740,986],[31,825],[247,754],[295,703],[13,467],[18,538],[342,609],[176,238],[298,887],[97,474],[29,568],[313,589],[196,271],[601,855],[379,648],[215,834],[258,983],[227,635],[899,944],[290,949],[551,585],[267,688],[536,762],[208,822],[260,357],[167,800],[650,866],[275,490],[94,563],[773,908],[247,612],[105,894],[311,715],[363,724],[197,553],[4,580],[757,883],[258,885],[42,732],[635,667],[72,618],[123,574],[629,988],[327,662],[67,567],[802,898],[126,413],[7,881],[144,540],[378,644],[65,445],[314,843],[0,277],[317,849],[41,406],[738,915],[48,581],[84,227],[161,803],[641,844],[738,767],[335,652],[48,486],[76,857],[363,790],[223,589],[211,681],[22,397],[683,916],[378,645],[207,455],[513,592],[475,849],[13,441],[336,880],[803,926],[32,564],[820,960],[288,931],[735,933],[295,572],[235,434],[27,300],[60,640],[347,839],[674,879],[160,305],[418,628],[59,414],[46,374],[489,930],[740,827],[89,766],[10,44],[431,603],[317,484],[307,945],[65,71],[295,873],[951,989],[477,537],[321,526],[144,830],[263,283],[319,728],[631,745],[339,643],[255,809],[402,510],[133,565],[251,257],[153,829],[32,574],[8,285],[340,350],[334,898],[467,959],[95,643],[266,788],[163,498],[270,621],[503,744],[639,672],[51,66],[553,980],[12,353],[60,626],[367,654],[673,895],[605,882],[469,739],[60,832],[170,913],[101,195],[117,304],[149,292],[92,773],[32,737],[13,885],[502,940],[147,653],[92,268],[375,628],[474,638],[310,746],[258,388],[253,705],[352,371],[11,563],[68,369],[287,599],[310,984],[250,893],[558,614],[530,608],[507,709],[375,392],[360,609],[53,304],[804,991],[608,612],[205,826],[299,582],[407,979],[539,893],[756,789],[228,556],[212,933],[122,309],[223,934],[461,919],[187,836],[728,760],[556,962],[809,884],[185,907],[770,858],[411,876],[451,794],[285,387],[326,541],[614,985],[105,440],[611,986],[283,701],[507,855],[168,731],[412,518],[132,970],[825,853],[293,357],[528,682],[534,610],[37,278],[536,662],[55,128],[158,184],[52,488],[576,648],[50,343],[242,288],[387,938],[282,905],[25,31],[568,955],[139,260],[709,976],[459,854],[47,970],[345,944],[493,838],[316,455],[280,753],[418,692],[468,691],[834,942],[381,644],[51,366],[423,744],[232,914],[24,510],[282,318],[854,895],[284,570],[650,957],[3,390],[290,723],[508,876],[234,843],[291,801],[23,395],[179,766],[142,837],[528,572],[635,984],[446,783],[332,854],[675,875],[497,933],[86,756],[679,965],[78,140],[360,869],[847,925],[197,223],[215,737],[557,709],[403,595],[22,339],[289,341],[125,848],[225,676],[350,608],[355,874],[584,868],[108,325],[615,634],[565,807],[804,981],[167,558],[98,784],[111,489],[43,174],[46,939],[180,690],[293,916],[3,291],[14,545],[74,880],[397,639],[700,962],[310,598],[333,385],[406,907],[72,348],[95,699],[224,397],[639,681],[205,331],[556,887],[78,173],[61,467],[284,464],[463,771],[114,592],[49,412],[292,888],[790,885],[694,914],[464,737],[535,551],[284,313],[92,994],[495,612],[42,378],[764,934],[716,936],[578,679],[268,520],[558,725],[66,953],[69,340],[7,61],[234,731],[128,637],[603,959],[225,886],[131,299],[74,848],[130,968],[216,360],[291,731],[150,770],[454,905],[208,733],[251,381],[218,245],[203,778],[80,226],[238,419],[388,918],[307,983],[76,524],[738,793],[825,975],[251,737],[23,440],[420,782],[791,878],[67,517],[537,689],[473,973],[597,963],[615,732],[206,670],[95,718],[495,711],[725,738],[23,240],[735,879],[70,950],[100,759],[445,617],[139,279],[219,857],[578,820],[419,789],[209,401],[465,492],[457,996],[391,490],[541,926],[623,648],[130,422],[447,945],[648,780],[569,652],[157,752],[199,570],[79,792],[952,994],[165,271],[353,802],[616,884],[261,902],[548,971],[190,696],[207,890],[299,677],[545,833],[37,97],[668,893],[249,842],[7,280],[658,915],[728,782],[773,840],[512,847],[82,142],[912,937],[129,251],[623,968],[97,135],[540,658],[198,592],[443,667],[371,664],[130,381],[35,188],[100,404],[157,436],[350,830],[238,678],[265,786],[539,602],[114,838],[479,962],[26,659],[114,305],[108,418],[50,665],[178,601],[176,861],[191,496],[146,689],[31,685],[752,915],[418,654],[230,588],[568,791],[511,643],[369,973],[5,207],[503,712],[544,976],[379,595],[162,664],[410,558],[330,986],[214,694],[203,315],[485,995],[595,773],[213,795],[50,503],[385,473],[408,428],[653,834],[2,267],[675,910],[129,697],[195,750],[772,967],[643,964],[564,658],[448,586],[926,962],[701,820],[45,409],[781,923],[11,933],[475,565],[143,755],[197,524],[0,720],[642,936],[178,988],[100,395],[458,466],[590,611],[99,232],[504,688],[973,994],[11,849],[662,741],[121,533],[934,972],[642,696],[229,616],[91,512],[314,352],[78,697],[626,980],[131,219],[356,407],[207,511],[219,788],[522,965],[540,591],[422,701],[69,857],[552,608],[493,808],[803,947],[73,836],[51,568],[51,112],[561,741],[360,598],[334,795],[419,524],[201,682],[746,832],[122,800],[629,636],[258,835],[216,248],[419,913],[315,729],[82,594],[159,953],[16,595],[670,717],[643,744],[547,749],[724,855],[836,911],[334,890],[513,993],[337,940],[249,655],[241,322],[457,810],[335,805],[549,789],[649,984],[705,783],[493,501],[409,485],[329,862],[25,412],[167,407],[543,694],[401,506],[278,613],[337,608],[490,745],[220,517],[505,883],[661,925],[194,819],[760,919],[247,495],[742,972],[760,916],[433,692],[265,942],[324,597],[387,412],[95,126],[55,880],[759,972],[887,892],[482,749],[778,916],[699,756],[465,731],[263,640],[77,362],[798,824],[175,774],[124,400],[501,797],[473,647],[101,621],[561,938],[77,437],[234,536],[244,843],[347,837],[199,299],[478,665],[849,945],[45,413],[782,820],[686,773],[83,116],[517,519],[329,852],[253,810],[406,711],[608,725],[599,963],[172,887],[465,998],[132,626],[142,767],[189,365],[91,452],[242,944],[474,747],[183,522],[344,652],[98,948],[183,684],[112,746],[401,922],[79,274],[445,842],[857,860],[90,854],[164,278],[669,706],[160,407],[711,937],[217,704],[428,677],[30,407],[384,952],[371,492],[410,519],[363,592],[159,518],[557,687],[307,677],[513,767],[811,904],[272,749],[758,863],[799,906],[169,752],[547,797],[522,572],[342,646],[8,595],[428,442],[254,772],[346,778],[67,935],[234,284],[92,778],[274,316],[452,674],[66,150],[253,477],[703,848],[869,900],[845,987],[308,359],[425,545],[780,829],[4,846],[502,842],[120,697],[86,768],[206,451],[520,939],[498,813],[495,871],[49,488],[608,797],[181,610],[33,41],[139,293],[96,514],[839,883],[229,722],[8,71],[42,326],[102,684],[618,796],[577,905],[284,734],[187,333],[310,745],[341,997],[629,630],[861,965],[617,964],[220,845],[173,481],[261,878],[335,934],[110,879],[222,266],[446,454],[119,516],[147,660],[122,771],[540,609],[13,670],[269,727]"); + succProb = + new double[] { + 0.88, 0.59, 0.67, 0.93, 0.76, 0.88, 0.9, 0.95, 0.7, 0.95, 0.69, 0.87, 0.7, 0.74, + 0.95, 0.89, 0.71, 0.87, 0.83, 0.98, 0.91, 0.75, 0.63, 0.85, 0.9, 0.7, 0.73, + 0.58, 0.56, 0.58, 0.88, 0.78, 0.98, 0.58, 0.94, 0.93, 0.91, 0.81, 0.7, 0.71, + 0.75, 0.74, 0.78, 0.58, 0.89, 0.68, 0.99, 0.93, 0.63, 0.53, 0.64, 0.57, 0.91, + 0.7, 0.99, 0.66, 0.69, 0.89, 0.83, 0.66, 0.77, 0.85, 0.53, 0.96, 0.95, 0.79, + 0.86, 0.54, 0.97, 0.61, 0.66, 0.59, 0.67, 0.55, 0.73, 0.68, 0.96, 0.99, 0.59, + 0.67, 0.81, 0.61, 0.92, 0.69, 0.93, 0.7, 0.99, 0.76, 0.81, 0.85, 1.0, 0.54, 0.8, + 0.55, 0.51, 0.89, 0.83, 0.75, 0.92, 0.75, 0.8, 0.58, 0.88, 0.73, 0.73, 0.93, + 0.52, 0.52, 0.61, 0.54, 0.88, 0.55, 0.91, 0.53, 0.63, 0.56, 0.52, 0.92, 0.54, + 0.86, 0.8, 0.77, 0.85, 0.66, 0.82, 0.94, 0.84, 0.64, 0.8, 0.52, 0.92, 0.59, + 0.97, 0.87, 0.67, 0.71, 0.81, 0.71, 0.93, 0.89, 0.77, 0.59, 0.86, 0.62, 0.64, + 0.51, 0.69, 0.93, 0.59, 0.74, 0.99, 0.8, 0.53, 0.85, 0.69, 0.92, 0.62, 0.9, + 0.83, 0.74, 0.85, 0.93, 0.87, 0.85, 0.59, 0.93, 0.56, 0.98, 0.59, 0.75, 0.89, + 0.64, 0.53, 0.65, 0.72, 0.88, 0.78, 0.76, 0.56, 0.85, 0.71, 0.81, 0.53, 0.77, + 0.91, 0.55, 0.7, 0.65, 0.62, 0.67, 0.82, 0.68, 0.72, 0.92, 0.76, 0.67, 0.62, + 0.95, 0.64, 0.92, 0.77, 0.93, 0.87, 1.0, 0.92, 0.86, 0.59, 0.62, 0.62, 0.54, + 0.65, 0.79, 0.8, 0.93, 0.92, 0.53, 0.88, 0.58, 0.67, 1.0, 0.82, 1.0, 0.7, 0.8, + 0.62, 0.68, 0.86, 0.62, 0.69, 0.52, 0.76, 0.53, 0.57, 0.52, 0.55, 0.92, 0.6, + 0.98, 0.52, 0.88, 0.89, 0.68, 0.78, 0.87, 0.92, 0.96, 0.82, 0.97, 0.54, 0.92, + 0.81, 0.53, 0.92, 0.87, 0.74, 0.68, 0.77, 0.99, 0.89, 0.84, 0.65, 0.88, 0.53, + 0.97, 0.66, 0.72, 0.97, 0.56, 0.57, 0.59, 0.76, 0.81, 0.77, 0.95, 0.82, 0.67, + 0.61, 0.86, 0.58, 0.83, 0.83, 0.51, 0.65, 0.6, 0.53, 0.61, 0.75, 0.63, 0.8, + 0.94, 0.86, 0.75, 0.52, 0.81, 0.91, 0.61, 0.57, 0.78, 0.85, 0.62, 0.56, 0.59, + 0.89, 0.56, 0.94, 0.84, 0.88, 0.7, 0.9, 0.72, 0.94, 0.94, 0.91, 0.94, 0.69, + 0.98, 0.86, 0.51, 0.69, 0.8, 0.69, 0.89, 0.61, 0.85, 0.55, 0.55, 0.92, 0.85, + 0.76, 0.74, 0.91, 0.7, 0.66, 0.54, 0.6, 0.51, 0.55, 0.83, 0.86, 0.66, 0.61, + 0.67, 0.67, 0.84, 0.85, 0.68, 0.81, 0.89, 0.73, 0.98, 0.65, 0.96, 0.53, 0.54, + 0.7, 0.89, 0.91, 0.82, 0.72, 0.65, 0.93, 1.0, 0.87, 0.92, 0.54, 0.9, 0.71, 0.69, + 0.5, 0.75, 0.5, 0.95, 0.98, 0.95, 0.64, 0.84, 0.56, 0.98, 0.9, 0.7, 0.7, 0.51, + 0.52, 0.73, 0.9, 0.86, 0.59, 0.69, 0.57, 0.72, 0.87, 0.9, 0.53, 0.79, 0.74, + 0.98, 0.83, 0.64, 0.7, 0.78, 0.62, 0.51, 0.85, 0.57, 0.95, 0.54, 0.8, 0.95, + 0.97, 0.94, 0.89, 0.53, 0.8, 0.9, 0.81, 0.72, 0.89, 0.69, 0.51, 0.87, 0.54, + 0.91, 0.99, 0.67, 0.82, 0.75, 0.84, 0.57, 0.69, 0.69, 0.89, 0.93, 0.51, 0.82, + 0.57, 0.73, 0.68, 0.8, 0.62, 0.94, 0.64, 0.6, 0.62, 0.81, 0.52, 0.72, 0.52, + 0.92, 0.97, 0.59, 0.86, 0.71, 0.67, 0.75, 0.76, 0.79, 0.88, 0.52, 0.88, 0.88, + 0.79, 0.79, 0.83, 0.71, 0.74, 0.62, 0.68, 0.68, 0.7, 0.69, 0.92, 0.98, 0.67, + 0.94, 0.7, 0.81, 0.97, 0.63, 0.68, 0.78, 0.92, 0.69, 0.64, 0.52, 0.62, 0.55, + 0.51, 0.53, 0.76, 0.71, 0.7, 0.65, 0.61, 0.51, 0.64, 0.85, 0.95, 0.95, 0.61, + 0.59, 0.54, 0.81, 0.54, 0.98, 0.7, 0.57, 0.95, 0.85, 0.72, 0.78, 0.98, 0.88, + 0.95, 0.86, 0.91, 0.52, 0.79, 0.97, 0.59, 0.69, 0.95, 0.94, 0.54, 0.62, 0.56, + 0.51, 0.87, 0.99, 0.52, 0.51, 0.69, 0.9, 0.94, 0.73, 0.79, 1.0, 0.97, 0.5, 0.84, + 0.57, 0.55, 0.88, 0.8, 0.96, 0.57, 0.68, 0.82, 0.62, 0.77, 0.73, 0.79, 0.89, + 0.54, 0.93, 0.96, 0.77, 0.68, 0.62, 0.66, 0.59, 0.98, 0.57, 0.51, 0.92, 0.59, + 0.5, 0.73, 0.62, 0.99, 0.88, 0.68, 0.58, 0.73, 0.59, 0.58, 0.87, 0.93, 0.92, + 0.51, 0.88, 0.92, 0.57, 0.55, 0.88, 0.94, 0.95, 0.84, 0.76, 0.87, 0.85, 0.61, + 0.7, 0.8, 0.69, 0.59, 0.7, 0.77, 0.91, 0.56, 0.52, 0.85, 0.89, 0.88, 0.55, 0.72, + 0.91, 0.7, 0.62, 0.54, 0.94, 0.69, 0.79, 0.64, 0.53, 0.65, 0.73, 0.92, 0.77, + 0.77, 0.55, 0.74, 0.96, 0.6, 0.58, 0.88, 0.94, 0.54, 0.58, 0.95, 0.69, 0.9, + 0.78, 0.56, 0.51, 0.9, 0.55, 0.58, 0.81, 0.67, 0.82, 0.74, 0.55, 0.8, 0.75, + 0.54, 0.7, 0.9, 0.78, 0.8, 0.81, 0.65, 0.93, 0.53, 0.73, 0.6, 0.67, 0.81, 0.62, + 0.7, 0.65, 0.72, 0.61, 0.86, 0.99, 0.87, 0.72, 0.53, 0.83, 0.91, 0.81, 0.86, + 0.86, 0.78, 0.57, 0.98, 0.56, 0.98, 0.97, 0.56, 0.91, 0.9, 0.6, 0.53, 0.51, + 0.87, 0.69, 0.98, 0.52, 0.8, 0.56, 0.57, 0.61, 0.9, 0.73, 0.8, 0.6, 0.9, 0.79, + 0.62, 0.57, 0.73, 0.76, 0.97, 0.87, 0.82, 0.76, 0.91, 0.86, 0.88, 0.51, 0.54, + 0.77, 0.62, 0.72, 0.51, 0.92, 0.52, 0.82, 0.94, 0.81, 0.59, 0.66, 0.58, 0.67, + 0.92, 0.5, 0.91, 0.97, 0.93, 0.81, 0.67, 0.68, 0.9, 0.54, 0.9, 0.84, 0.85, 0.62, + 0.95, 0.81, 0.76, 0.54, 0.62, 0.83, 0.75, 0.66, 0.8, 0.74, 0.96, 0.84, 0.6, + 0.73, 0.81, 0.55, 0.69, 0.81, 0.84, 0.74, 0.77, 0.87, 0.81, 0.82, 0.82, 0.86, + 0.51, 0.64, 0.62, 0.69, 0.53, 0.86, 0.53, 0.56, 0.55, 0.95, 0.59, 0.73, 0.62, + 0.97, 0.58, 0.68, 0.87, 0.74, 0.81, 0.54, 0.98, 0.86, 0.75, 0.87, 0.53, 0.55, + 0.6, 0.79, 0.75, 0.75, 0.55, 0.88, 0.77, 0.75, 0.53, 0.96, 0.84, 0.63, 0.67, + 0.89, 0.63, 0.97, 0.62, 0.56, 0.81, 0.61, 0.69, 0.7, 0.98, 0.65, 0.6, 0.96, + 0.82, 0.75, 0.69, 0.74, 0.82, 0.91, 0.86, 0.85, 0.89, 0.51, 0.51, 0.6, 0.81, + 0.68, 0.9, 0.74, 1.0, 0.85, 0.53, 0.72, 0.5, 0.74, 0.54, 0.69, 0.75, 0.71, 0.95, + 0.77, 0.77, 0.84, 0.55, 0.74, 0.61, 0.54, 0.65, 0.94, 0.67, 0.71, 0.65, 0.91, + 1.0, 0.7, 0.62, 0.65, 0.81, 0.78, 0.76, 0.88, 0.7, 0.88, 0.79, 0.67, 0.94, 0.98, + 0.67, 0.64, 0.63, 0.56, 0.97, 0.68, 0.89, 0.59, 0.7, 0.52, 0.61, 0.84, 0.87, + 0.75, 0.9, 0.61, 0.52, 1.0, 0.88, 0.82, 0.64, 0.72, 0.81, 0.89, 0.98, 0.63, + 0.99, 0.63, 0.8, 0.72, 0.91, 0.56, 0.98, 0.7, 0.93, 0.68, 0.7, 0.58, 0.93, 0.66, + 0.99, 0.81, 0.89, 0.82, 0.94, 0.81, 0.87, 0.57, 0.52, 0.8, 0.84, 0.5, 0.83, + 0.73, 0.84, 0.5, 0.72, 0.74, 0.82, 0.56, 0.74, 0.76, 0.83, 0.74, 0.54, 0.62, + 0.96, 0.61, 0.53, 0.59, 0.87, 0.96, 0.6, 0.67, 0.99, 0.72, 0.94, 0.57, 0.88, + 0.55, 0.77, 0.89, 0.83, 0.68, 0.86, 0.81, 0.6, 0.58, 0.56, 0.79, 0.65, 0.61, + 0.54, 0.66, 0.52, 0.61, 0.64, 0.88, 0.71, 0.52, 0.84, 0.81, 0.92, 0.64, 0.64, + 0.95, 0.53, 0.92, 0.69, 0.8, 0.81, 0.54, 0.7, 0.55, 0.81, 0.95, 0.99, 0.59, 0.9, + 0.97, 0.67, 0.69, 0.88, 0.58, 0.55, 0.58, 0.91, 0.57, 0.8, 0.59, 0.72, 0.64, + 0.95, 0.54, 0.51, 0.63, 0.89, 0.92, 0.78, 0.71, 0.66, 0.73, 0.8, 0.66, 0.95, + 0.54, 0.51, 0.78, 0.7, 0.76, 0.86, 0.59, 0.76, 0.64, 0.81, 0.58, 0.62, 0.86, + 0.89, 0.6, 0.74, 0.78, 0.9, 0.72, 0.91, 0.63, 0.69, 0.76, 0.58, 0.97, 0.9, 0.77, + 0.78, 0.5, 0.78, 0.69, 0.78, 1.0, 0.52, 0.81, 0.9, 0.56, 0.69, 0.58, 0.58, 0.6, + 0.68, 0.82, 0.99, 0.52, 0.92, 0.67, 0.61, 0.71, 0.99, 0.56, 0.6, 0.62, 0.85, + 0.84, 0.99, 0.59, 0.51, 0.78, 0.85, 0.54, 0.7, 0.9, 0.56, 0.89, 0.91, 0.52, 0.5, + 0.63, 0.6, 0.65, 0.94, 0.7, 0.93, 0.92, 0.64, 0.89, 0.74, 0.74, 0.64, 0.86, + 0.91, 0.55, 0.9, 0.51, 0.86, 0.84, 0.56, 0.98, 1.0, 0.78, 0.72, 0.71, 0.86, + 0.99, 0.64, 0.58, 0.51, 0.96, 0.68, 0.91, 0.52, 0.57, 0.79, 0.81, 0.61, 0.57, + 0.86, 0.66, 0.76, 0.61, 0.56, 0.73, 0.75, 0.83, 0.69, 0.57, 0.58, 0.59, 0.53, + 0.84, 0.8, 0.79, 0.8, 0.75, 0.97, 0.58, 0.89, 0.88, 0.54, 0.75, 0.71, 0.62, + 0.76, 0.85, 0.52, 0.94, 0.71, 0.73, 0.8, 0.67, 0.87, 0.54, 0.72, 0.72, 0.64, + 0.71, 0.66, 0.68, 0.53, 0.78, 0.65, 0.77, 0.97, 0.84, 0.57, 0.85, 0.67, 0.87, + 0.59, 0.68, 0.9, 0.79, 0.54, 0.5, 0.53, 0.97, 0.74, 0.89, 0.98, 0.96, 0.9, 0.84, + 0.8, 0.56, 0.67, 0.87, 0.8, 0.77, 0.62, 0.65, 0.74, 0.93, 0.7, 0.81, 0.77, 0.61, + 0.85, 0.9, 0.67, 0.73, 0.87, 0.77, 0.91, 0.87, 0.93, 0.61, 0.85, 0.87, 0.76, + 0.63, 0.52, 0.95, 0.84, 0.87, 0.55, 0.87, 0.76, 0.58, 0.7, 0.53, 0.93, 0.76, + 0.52, 0.79, 0.68, 0.65, 0.66, 0.53, 0.89, 0.5, 0.77, 0.6, 0.52, 0.61, 0.7, 0.63, + 0.88, 0.56, 0.68, 0.85, 0.87, 0.73, 0.84, 0.87, 0.55, 0.99, 0.53, 0.82, 0.91, + 0.91, 0.81, 0.85, 0.57, 0.58, 0.84, 0.92, 0.74, 0.52, 0.9, 0.88, 0.75, 0.61, + 0.62, 0.55, 0.56, 0.92, 0.62, 0.64, 0.56, 0.64, 0.73, 0.88, 0.98, 0.54, 0.75, + 0.8, 0.53, 0.92, 0.75, 0.72, 0.94, 0.93, 0.79, 0.95, 0.61, 0.99, 0.57, 0.74, + 0.56, 0.76, 0.53, 0.9, 0.65, 0.94, 0.89, 0.84, 0.87, 0.82, 0.67, 0.7, 0.87, + 0.92, 0.57, 0.63, 0.87, 0.66, 0.71, 0.61, 0.7, 0.73, 0.92, 0.9, 0.75, 0.84, + 0.96, 0.6, 0.58, 0.57, 0.65, 0.64, 0.63, 0.71, 0.62, 0.83, 0.58, 0.79, 0.68, + 0.59, 0.85, 0.7, 0.54, 0.63, 0.91, 0.64, 0.74, 0.66, 0.76, 0.76, 0.97, 0.96, + 0.95, 0.94, 0.89, 0.67, 0.69, 0.85, 0.82, 0.55, 0.64, 0.89, 0.64, 0.64, 0.87, + 0.53, 0.56, 0.68, 0.55, 0.78, 0.94, 0.63, 0.85, 0.61, 0.83, 0.8, 0.61, 0.84, + 0.83, 0.91, 0.76, 0.55, 0.84, 0.52, 0.96, 1.0, 0.6, 0.71, 0.97, 0.62, 0.88, + 0.52, 0.69, 0.71, 0.82, 0.66, 0.87, 0.66, 0.73, 0.6, 0.58, 0.61, 0.89, 0.84, + 0.53, 0.77, 0.83, 0.8, 0.51, 0.63, 0.75, 0.65, 0.95, 0.51, 0.93, 0.53, 0.51, + 0.54, 0.74, 0.82, 0.54, 0.56, 0.62, 0.69, 0.7, 0.64, 0.92, 0.5, 0.54, 0.87, + 0.91, 0.63, 0.9, 0.59, 0.55, 0.59, 0.6, 0.8, 0.9, 0.54, 0.89, 0.85, 0.65, 0.69, + 0.8, 0.88, 0.83, 0.62, 0.75, 0.71, 0.52, 0.71, 0.89, 0.94, 0.56, 0.93, 0.92, + 0.78, 0.55, 0.98, 0.52, 0.77, 0.83, 0.92, 0.78, 0.58, 0.66, 0.76, 0.53, 0.7, + 0.91, 0.55, 0.55, 0.56, 0.75, 0.75, 0.81, 0.91, 0.55, 0.98, 0.94, 0.64, 0.77, + 0.84, 0.93, 0.75, 0.64, 0.93, 0.87, 0.7, 0.82, 0.93, 0.66, 0.74, 0.51, 0.96, + 0.85, 0.63, 0.99, 0.59, 0.9, 0.53, 0.87, 0.74, 0.68, 0.74, 1.0, 0.54, 1.0, 0.93, + 0.99, 0.65, 0.71, 0.51, 0.99, 0.76, 0.6, 0.61, 0.91, 0.62, 0.93, 0.6, 0.69, + 0.57, 0.82, 0.85, 0.84, 0.77, 0.66, 0.77, 0.66, 0.74, 0.94, 0.72, 0.79, 0.66, + 0.94, 0.84, 0.84, 0.75, 0.52, 0.66, 0.58, 0.64, 0.52, 0.52, 0.87, 0.69, 0.75, + 0.77, 0.68, 0.82, 0.87, 0.95, 0.94, 0.71, 0.53, 0.8, 0.51, 1.0, 0.93, 0.58, + 0.65, 0.66, 0.66, 0.93, 1.0, 0.52, 0.52, 0.56, 0.69, 0.66, 0.52, 0.78, 0.54, + 0.56, 0.58, 0.82, 0.74, 0.85, 0.51, 0.51, 0.76, 0.87, 0.81, 0.81, 0.87, 0.9, + 0.85, 0.92, 0.85, 0.87, 0.97, 0.58, 0.98, 0.54, 0.81, 0.75, 0.72, 0.7, 0.56, + 0.83, 0.81, 0.95, 0.8, 0.88, 0.87, 0.55, 0.95, 0.67, 0.68, 0.93, 0.71, 0.53, + 0.74, 0.72, 0.92, 0.97, 0.84, 0.81, 0.86, 0.92, 0.56, 0.59, 0.59, 0.81, 0.61, + 0.86, 0.89, 0.53, 0.7, 0.61, 0.57, 0.6, 0.95, 0.62, 0.6, 0.94, 0.68, 0.85, 0.72, + 0.64, 0.79, 0.7, 0.82, 0.72, 0.93, 0.59, 0.7, 0.67, 0.86, 0.86, 0.77, 0.95, + 0.83, 0.82, 0.93, 0.92, 0.61, 0.53, 0.94, 0.66, 0.67, 0.78, 0.88, 0.68, 0.93, + 0.9, 0.82, 0.83, 0.73, 0.74, 0.6, 0.95, 0.8, 0.62, 0.99, 0.9, 0.81, 0.58, 0.6, + 0.59, 0.6, 0.74, 0.81, 0.69, 0.76, 0.88, 0.82, 0.5, 0.88, 0.9, 0.86, 0.72, 0.56, + 0.9, 0.84, 0.78, 0.88, 0.52, 0.83, 0.74, 0.6, 0.7, 0.99, 0.54, 0.6, 0.94, 0.79, + 0.96, 0.64, 0.51, 0.64, 0.55, 0.5, 0.92, 0.57, 0.97, 0.62, 0.57, 0.76, 0.57, + 0.81, 0.54, 0.59, 0.75, 0.6, 0.97, 0.68, 0.53, 0.6, 0.64, 0.88, 0.88, 0.97, + 0.91, 0.62, 0.7, 0.91, 0.56, 0.61, 0.82, 0.99, 0.7, 0.93, 0.93, 0.71, 0.81, + 0.64, 0.87, 0.76, 0.75, 0.97, 0.92, 0.91, 0.53, 0.68, 0.78, 0.95, 0.58, 0.72, + 0.88, 0.57, 0.61, 0.86, 0.83, 0.91, 0.6, 0.74, 0.83, 0.59, 0.69, 0.77, 0.73, + 0.76, 0.8, 0.69, 0.74, 0.85, 0.82, 0.98, 0.75, 0.67, 0.52, 0.57, 0.72, 0.73, + 0.71, 0.79, 0.86, 0.55, 0.99, 0.84, 0.97, 0.74, 0.77, 0.71, 0.8, 0.77, 0.85, + 0.73, 0.61, 0.85, 0.56, 0.91, 0.74, 0.54, 0.69, 0.84, 0.91, 0.94, 0.86, 0.53, + 0.58, 0.53, 0.6, 0.8, 0.84, 0.95, 0.96, 0.72, 0.65, 0.64, 0.84, 0.93, 0.53, + 0.63, 0.76, 0.55, 0.9, 0.63, 0.68, 0.93, 0.54, 0.5, 0.55, 0.66, 0.54, 0.81, + 0.57, 0.53, 0.64, 0.69, 0.62, 0.65, 0.51, 0.98, 0.75, 0.59, 0.57, 0.62, 0.63, + 0.86, 0.78, 0.56, 0.84, 0.82, 0.68, 0.93, 0.77, 0.98, 0.51, 0.79, 0.77, 0.64, + 0.85, 0.78, 0.66, 0.54, 0.62, 0.6, 0.93, 0.9, 0.6, 0.96, 0.93, 0.99, 0.52, 0.82, + 0.56, 0.72, 0.87, 0.61, 0.5, 0.94, 0.77, 0.63, 0.8, 0.75, 0.87, 0.56, 0.78, + 0.89, 0.86, 0.75, 0.93, 0.82, 0.78, 0.76, 0.92, 0.75, 0.58, 0.75, 0.79, 0.95, + 0.74, 0.94, 0.69, 0.51, 0.74, 0.68, 0.58, 0.53, 0.94, 0.65, 0.94, 0.72, 0.89, + 0.96, 1.0, 0.67, 0.64, 0.87, 0.89, 0.78, 0.76, 0.51, 0.81, 0.9, 0.63, 0.93 + }; + assertEquals(0.344138400144, solution1.maxProbability(1000, edges, succProb, 112, 493)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1518Test.java b/src/test/java/com/fishercoder/secondthousand/_1518Test.java new file mode 100644 index 0000000000..ae78a54ffc --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1518Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1518; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1518Test { + private _1518.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1518.Solution1(); + } + + @Test + public void test1() { + assertEquals(13, solution1.numWaterBottles(9, 3)); + } + + @Test + public void test2() { + assertEquals(19, solution1.numWaterBottles(15, 4)); + } + + @Test + public void test3() { + assertEquals(6, solution1.numWaterBottles(5, 5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1523Test.java b/src/test/java/com/fishercoder/secondthousand/_1523Test.java new file mode 100644 index 0000000000..e8bfc3bdc6 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1523Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1523; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1523Test { + private _1523.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1523.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.countOdds(3, 7)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1524Test.java b/src/test/java/com/fishercoder/secondthousand/_1524Test.java new file mode 100644 index 0000000000..402ed9bf78 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1524Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1524; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1524Test { + private _1524.Solution1 solution1; + private _1524.Solution2 solution2; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1524.Solution1(); + solution2 = new _1524.Solution2(); + } + + @Test + public void test1() { + arr = new int[] {1, 3, 5}; + assertEquals(4, solution1.numOfSubarrays(arr)); + } + + @Test + public void test2() { + arr = new int[] {2, 4, 6}; + assertEquals(0, solution1.numOfSubarrays(arr)); + } + + @Test + public void test4() { + arr = new int[] {1, 3, 5}; + assertEquals(4, solution2.numOfSubarrays(arr)); + } + + @Test + public void test5() { + arr = new int[] {2, 4, 6}; + assertEquals(0, solution2.numOfSubarrays(arr)); + } + + @Test + public void test6() { + arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + assertEquals(16, solution2.numOfSubarrays(arr)); + } + + @Test + public void test7() { + arr = new int[] {1, 2, 3, 4, 5}; + assertEquals(9, solution2.numOfSubarrays(arr)); + } + + @Test + public void test8() { + arr = new int[] {1, 2, 3, 4}; + assertEquals(6, solution2.numOfSubarrays(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1525Test.java b/src/test/java/com/fishercoder/secondthousand/_1525Test.java new file mode 100644 index 0000000000..52615f43e7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1525Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1525; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1525Test { + private _1525.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1525.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.numSplits("aacaba")); + } + + @Test + public void test2() { + assertEquals(1, solution1.numSplits("abcd")); + } + + @Test + public void test3() { + assertEquals(4, solution1.numSplits("aaaaa")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1526Test.java b/src/test/java/com/fishercoder/secondthousand/_1526Test.java new file mode 100644 index 0000000000..dab1cf14e0 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1526Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1526; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1526Test { + private _1526.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1526.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.minNumberOperations(new int[] {1, 2, 3, 2, 1})); + } + + @Test + public void test2() { + assertEquals(4, solution1.minNumberOperations(new int[] {3, 1, 1, 2})); + } + + @Test + public void test3() { + assertEquals(7, solution1.minNumberOperations(new int[] {3, 1, 5, 4, 2})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1528Test.java b/src/test/java/com/fishercoder/secondthousand/_1528Test.java new file mode 100644 index 0000000000..773a3518b6 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1528Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1528; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1528Test { + private _1528.Solution1 solution1; + private static int[] indices; + + @BeforeEach + public void setup() { + solution1 = new _1528.Solution1(); + } + + @Test + public void test1() { + indices = new int[] {4, 5, 6, 7, 0, 2, 1, 3}; + assertEquals("leetcode", solution1.restoreString("codeleet", indices)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1530Test.java b/src/test/java/com/fishercoder/secondthousand/_1530Test.java new file mode 100644 index 0000000000..89c24b7748 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1530Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1530; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1530Test { + private _1530.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _1530.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4)); + assertEquals(1, solution1.countPairs(root, 3)); + } + + @Test + public void test2() { + root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); + assertEquals(2, solution1.countPairs(root, 3)); + } + + @Test + public void test3() { + root = + TreeUtils.constructBinaryTree( + Arrays.asList(7, 1, 4, 6, null, 5, 3, null, null, null, null, null, 2)); + assertEquals(1, solution1.countPairs(root, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1534Test.java b/src/test/java/com/fishercoder/secondthousand/_1534Test.java new file mode 100644 index 0000000000..b3b1188556 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1534Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1534; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1534Test { + private _1534.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1534.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {3, 0, 1, 1, 9, 7}; + assertEquals(4, solution1.countGoodTriplets(arr, 7, 2, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1535Test.java b/src/test/java/com/fishercoder/secondthousand/_1535Test.java new file mode 100644 index 0000000000..934482edc2 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1535Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1535; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1535Test { + private _1535.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1535.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {2, 1, 3, 5, 4, 6, 7}; + assertEquals(5, solution1.getWinner(arr, 2)); + } + + @Test + public void test2() { + arr = new int[] {1, 11, 22, 33, 44, 55, 66, 77, 88, 99}; + assertEquals(99, solution1.getWinner(arr, 100)); + } + + @Test + public void test3() { + arr = new int[] {1, 9, 8, 2, 3, 7, 6, 4, 5}; + assertEquals(9, solution1.getWinner(arr, 7)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1539Test.java b/src/test/java/com/fishercoder/secondthousand/_1539Test.java new file mode 100644 index 0000000000..3b0c738552 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1539Test.java @@ -0,0 +1,57 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1539; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1539Test { + private _1539.Solution1 solution1; + private _1539.Solution2 solution2; + private _1539.Solution3 solution3; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1539.Solution1(); + solution2 = new _1539.Solution2(); + solution3 = new _1539.Solution3(); + } + + @Test + public void test1() { + arr = new int[] {2, 3, 4, 7, 11}; + assertEquals(9, solution1.findKthPositive(arr, 5)); + } + + @Test + public void test2() { + arr = new int[] {1, 2, 3, 4}; + assertEquals(6, solution1.findKthPositive(arr, 2)); + } + + @Test + public void test3() { + arr = new int[] {2, 3, 4, 7, 11}; + assertEquals(9, solution2.findKthPositive(arr, 5)); + } + + @Test + public void test4() { + arr = new int[] {1, 2, 3, 4}; + assertEquals(6, solution2.findKthPositive(arr, 2)); + } + + @Test + public void test5() { + arr = new int[] {2, 3, 4, 7, 11}; + assertEquals(9, solution3.findKthPositive(arr, 5)); + } + + @Test + public void test6() { + arr = new int[] {1, 2, 3, 4}; + assertEquals(6, solution3.findKthPositive(arr, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1541Test.java b/src/test/java/com/fishercoder/secondthousand/_1541Test.java new file mode 100644 index 0000000000..e73ebac519 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1541Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1541; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1541Test { + private _1541.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1541.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minInsertions("(()))")); + } + + @Test + public void test2() { + assertEquals(0, solution1.minInsertions("())")); + } + + @Test + public void test3() { + assertEquals(3, solution1.minInsertions("))())(")); + } + + @Test + public void test4() { + assertEquals(12, solution1.minInsertions("((((((")); + } + + @Test + public void test5() { + assertEquals(5, solution1.minInsertions(")))))))")); + } + + @Test + public void test6() { + assertEquals(4, solution1.minInsertions("(()))(()))()())))")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1544Test.java b/src/test/java/com/fishercoder/secondthousand/_1544Test.java new file mode 100644 index 0000000000..2dc6c7d6d3 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1544Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1544; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1544Test { + private _1544.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1544.Solution1(); + } + + @Test + public void test1() { + assertEquals("leetcode", solution1.makeGood("leEeetcode")); + } + + @Test + public void test2() { + assertEquals("", solution1.makeGood("abBAcC")); + } + + @Test + public void test3() { + assertEquals("s", solution1.makeGood("s")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1545Test.java b/src/test/java/com/fishercoder/secondthousand/_1545Test.java new file mode 100644 index 0000000000..332dc74f5a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1545Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1545; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1545Test { + private _1545.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1545.Solution1(); + } + + @Test + public void test1() { + assertEquals('0', solution1.findKthBit(3, 1)); + } + + @Test + public void test2() { + assertEquals('1', solution1.findKthBit(4, 11)); + } + + @Test + public void test3() { + assertEquals('0', solution1.findKthBit(1, 1)); + } + + @Test + public void test4() { + assertEquals('1', solution1.findKthBit(2, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1550Test.java b/src/test/java/com/fishercoder/secondthousand/_1550Test.java new file mode 100644 index 0000000000..5ec03af89f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1550Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1550; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1550Test { + private _1550.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1550.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {2, 6, 4, 1}; + assertEquals(false, solution1.threeConsecutiveOdds(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1551Test.java b/src/test/java/com/fishercoder/secondthousand/_1551Test.java new file mode 100644 index 0000000000..b600442f5a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1551Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1551; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1551Test { + private _1551.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1551.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minOperations(3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1556Test.java b/src/test/java/com/fishercoder/secondthousand/_1556Test.java new file mode 100644 index 0000000000..32c25acc8a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1556Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1556; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1556Test { + private _1556.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1556.Solution1(); + } + + @Test + public void test1() { + assertEquals("987", solution1.thousandSeparator(987)); + } + + @Test + public void test2() { + assertEquals("1.234", solution1.thousandSeparator(1234)); + } + + @Test + public void test3() { + assertEquals("0", solution1.thousandSeparator(0)); + } + + @Test + public void test4() { + assertEquals("123.456.789", solution1.thousandSeparator(123456789)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1557Test.java b/src/test/java/com/fishercoder/secondthousand/_1557Test.java new file mode 100644 index 0000000000..c3c20fba6d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1557Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1557; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1557Test { + private _1557.Solution1 solution1; + private static List> edges; + + @BeforeEach + public void setup() { + solution1 = new _1557.Solution1(); + } + + @Test + public void test1() { + edges = + Arrays.asList( + Arrays.asList(0, 1), + Arrays.asList(0, 2), + Arrays.asList(2, 5), + Arrays.asList(3, 4), + Arrays.asList(4, 2)); + assertEquals(Arrays.asList(0, 3), solution1.findSmallestSetOfVertices(6, edges)); + } + + @Test + public void test2() { + edges = + Arrays.asList( + Arrays.asList(0, 1), + Arrays.asList(2, 1), + Arrays.asList(3, 1), + Arrays.asList(1, 4), + Arrays.asList(2, 4)); + assertEquals(Arrays.asList(0, 2, 3), solution1.findSmallestSetOfVertices(5, edges)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1558Test.java b/src/test/java/com/fishercoder/secondthousand/_1558Test.java new file mode 100644 index 0000000000..d1f852130f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1558Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1558; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1558Test { + private _1558.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1558.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 5}; + assertEquals(5, solution1.minOperations(nums)); + } + + @Test + public void test2() { + nums = new int[] {2, 2}; + assertEquals(3, solution1.minOperations(nums)); + } + + @Test + public void test3() { + nums = new int[] {4, 2, 5}; + assertEquals(6, solution1.minOperations(nums)); + } + + @Test + public void test4() { + nums = new int[] {3, 2, 2, 4}; + assertEquals(7, solution1.minOperations(nums)); + } + + @Test + public void test5() { + nums = new int[] {2, 4, 8, 16}; + assertEquals(8, solution1.minOperations(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1560Test.java b/src/test/java/com/fishercoder/secondthousand/_1560Test.java new file mode 100644 index 0000000000..3c93c7e7e9 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1560Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1560; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1560Test { + private _1560.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1560.Solution1(); + } + + @Test + public void test1() { + assertEquals(Arrays.asList(1, 2), solution1.mostVisited(4, new int[] {1, 3, 1, 2})); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList(2), solution1.mostVisited(2, new int[] {2, 1, 2, 1, 2, 1, 2, 1, 2})); + } + + @Test + public void test3() { + assertEquals( + Arrays.asList(1, 2, 3, 4, 5, 6, 7), + solution1.mostVisited(7, new int[] {1, 3, 5, 7})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1561Test.java b/src/test/java/com/fishercoder/secondthousand/_1561Test.java new file mode 100644 index 0000000000..b2edf189bf --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1561Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1561; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1561Test { + private _1561.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1561.Solution1(); + } + + @Test + public void test1() { + assertEquals(9, solution1.maxCoins(new int[] {2, 4, 1, 2, 7, 8})); + } + + @Test + public void test2() { + assertEquals(4, solution1.maxCoins(new int[] {2, 4, 5})); + } + + @Test + public void test3() { + assertEquals(18, solution1.maxCoins(new int[] {9, 8, 7, 6, 5, 1, 2, 3, 4})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1566Test.java b/src/test/java/com/fishercoder/secondthousand/_1566Test.java new file mode 100644 index 0000000000..0fa7d3ca82 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1566Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1566; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1566Test { + private _1566.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1566.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.containsPattern(new int[] {1, 2, 4, 4, 4, 4}, 1, 3)); + } + + @Test + public void test2() { + assertEquals(true, solution1.containsPattern(new int[] {1, 2, 1, 2, 1, 1, 1, 3}, 2, 2)); + } + + @Test + public void test3() { + assertEquals(false, solution1.containsPattern(new int[] {1, 2, 1, 2, 1, 3}, 2, 3)); + } + + @Test + public void test4() { + assertEquals(false, solution1.containsPattern(new int[] {1, 2, 3, 1, 2}, 2, 2)); + } + + @Test + public void test5() { + assertEquals(true, solution1.containsPattern(new int[] {1, 2, 4, 4, 4, 4}, 1, 3)); + } + + @Test + public void test6() { + assertEquals(false, solution1.containsPattern(new int[] {2, 2, 2, 2}, 2, 3)); + } + + @Test + public void test7() { + assertEquals( + false, solution1.containsPattern(new int[] {2, 2, 1, 2, 2, 1, 1, 1, 2, 1}, 2, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1567Test.java b/src/test/java/com/fishercoder/secondthousand/_1567Test.java new file mode 100644 index 0000000000..8d20267219 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1567Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1567; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1567Test { + private _1567.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1567.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.getMaxLen(new int[] {1, -2, -3, 4})); + } + + @Test + public void test2() { + assertEquals(3, solution1.getMaxLen(new int[] {0, 1, -2, -3, -4})); + } + + @Test + public void test3() { + assertEquals(2, solution1.getMaxLen(new int[] {-1, -2, -3, 0, 1})); + } + + @Test + public void test4() { + assertEquals(1, solution1.getMaxLen(new int[] {-1, 2})); + } + + @Test + public void test5() { + assertEquals(4, solution1.getMaxLen(new int[] {1, 2, 3, 5, -6, 4, 0, 10})); + } + + @Test + public void test6() { + assertEquals(0, solution1.getMaxLen(new int[] {-1})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1572Test.java b/src/test/java/com/fishercoder/secondthousand/_1572Test.java new file mode 100644 index 0000000000..f55fe33d89 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1572Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1572; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1572Test { + private _1572.Solution1 solution1; + private static int[][] mat; + + @BeforeEach + public void setup() { + solution1 = new _1572.Solution1(); + } + + @Test + public void test1() { + mat = + new int[][] { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + assertEquals(25, solution1.diagonalSum(mat)); + } + + @Test + public void test2() { + mat = new int[][] {{5}}; + assertEquals(5, solution1.diagonalSum(mat)); + } + + @Test + public void test3() { + mat = + new int[][] { + {1, 1, 1, 1}, + {1, 1, 1, 1}, + {1, 1, 1, 1}, + {1, 1, 1, 1}, + }; + assertEquals(8, solution1.diagonalSum(mat)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1574Test.java b/src/test/java/com/fishercoder/secondthousand/_1574Test.java new file mode 100644 index 0000000000..5b6e247626 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1574Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1574; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1574Test { + private _1574.Solution1 solution1; + private static int[] arr; + + @BeforeEach + public void setup() { + solution1 = new _1574.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {1, 2, 3, 10, 4, 2, 3, 5}; + assertEquals(3, solution1.findLengthOfShortestSubarray(arr)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1576Test.java b/src/test/java/com/fishercoder/secondthousand/_1576Test.java new file mode 100644 index 0000000000..e08dad2e08 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1576Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1576; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1576Test { + private _1576.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1576.Solution1(); + } + + @Test + public void test1() { + assertEquals("azs", solution1.modifyString("?zs")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1577Test.java b/src/test/java/com/fishercoder/secondthousand/_1577Test.java new file mode 100644 index 0000000000..e62cd266ad --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1577Test.java @@ -0,0 +1,63 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1577; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1577Test { + private _1577.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1577.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.numTriplets(new int[] {7, 4}, new int[] {5, 2, 8, 9})); + } + + @Test + public void test2() { + assertEquals(9, solution1.numTriplets(new int[] {1, 1}, new int[] {1, 1, 1})); + } + + @Test + public void test3() { + assertEquals(2, solution1.numTriplets(new int[] {7, 7, 8, 3}, new int[] {1, 2, 9, 7})); + } + + @Test + public void test4() { + assertEquals( + 0, + solution1.numTriplets(new int[] {4, 7, 9, 11, 23}, new int[] {3, 5, 1024, 12, 18})); + } + + @Test + public void test5() { + assertEquals(4, solution1.numTriplets(new int[] {3, 1, 2, 2}, new int[] {1, 3, 4, 4})); + } + + @Test + public void test6() { + assertEquals(5, solution1.numTriplets(new int[] {4, 1, 4, 1, 12}, new int[] {3, 2, 5, 4})); + } + + @Test + public void test7() { + assertEquals( + 234, + solution1.numTriplets( + new int[] { + 14, 1, 1, 12, 7, 12, 10, 4, 11, 10, 5, 2, 5, 14, 7, 9, 10, 13, 15, 6, 9, + 12, 6, 12, 4, 10, 9, 12, 11 + }, + new int[] { + 3, 12, 1, 9, 1, 12, 4, 12, 4, 1, 7, 10, 7, 11, 4, 13, 4, 11, 5, 1, 14, + 12, 15, 4, 2, 3, 13, 10, 3, 4 + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1582Test.java b/src/test/java/com/fishercoder/secondthousand/_1582Test.java new file mode 100644 index 0000000000..8c70c1d67f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1582Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1582; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1582Test { + private _1582.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1582.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 3, + solution1.numSpecial( + new int[][] { + {0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0}, + {0, 1, 0, 0, 0}, + {0, 0, 1, 0, 0}, + {0, 0, 0, 1, 1} + })); + } + + @Test + public void test2() { + assertEquals( + 2, + solution1.numSpecial( + new int[][] { + {0, 0, 0, 1}, + {1, 0, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 0} + })); + } + + @Test + public void test3() { + assertEquals( + 3, + solution1.numSpecial( + new int[][] { + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1} + })); + } + + @Test + public void test4() { + assertEquals( + 1, + solution1.numSpecial( + new int[][] { + {1, 0, 0}, + {0, 0, 1}, + {1, 0, 0} + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1583Test.java b/src/test/java/com/fishercoder/secondthousand/_1583Test.java new file mode 100644 index 0000000000..30e01c6908 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1583Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1583; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1583Test { + private _1583.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1583.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 2, + solution1.unhappyFriends( + 4, + new int[][] { + {1, 2, 3}, + {3, 2, 0}, + {3, 1, 0}, + {1, 2, 0} + }, + new int[][] { + {0, 1}, + {2, 3} + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1588Test.java b/src/test/java/com/fishercoder/secondthousand/_1588Test.java new file mode 100644 index 0000000000..a43106fb0f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1588Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1588; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1588Test { + private _1588.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1588.Solution1(); + } + + @Test + public void test1() { + assertEquals(58, solution1.sumOddLengthSubarrays(new int[] {1, 4, 2, 5, 3})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1592Test.java b/src/test/java/com/fishercoder/secondthousand/_1592Test.java new file mode 100644 index 0000000000..5d8a68fda8 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1592Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1592; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1592Test { + private _1592.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1592.Solution1(); + } + + @Test + public void test1() { + assertEquals("hello ", solution1.reorderSpaces(" hello")); + } + + @Test + public void test2() { + assertEquals( + "this is a sentence", solution1.reorderSpaces(" this is a sentence ")); + } + + @Test + public void test3() { + assertEquals( + "practice makes perfect ", + solution1.reorderSpaces(" practice makes perfect")); + } + + @Test + public void test4() { + assertEquals("hello world", solution1.reorderSpaces("hello world")); + } + + @Test + public void test5() { + assertEquals( + "walks udp package into bar a ", + solution1.reorderSpaces(" walks udp package into bar a")); + } + + @Test + public void test6() { + assertEquals("a", solution1.reorderSpaces("a")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1600Test.java b/src/test/java/com/fishercoder/secondthousand/_1600Test.java new file mode 100644 index 0000000000..61256c12af --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1600Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1600; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _1600Test { + private _1600.Solution1.ThroneInheritance throneInheritance; + + @Test + public void test1() { + throneInheritance = new _1600.Solution1.ThroneInheritance("king"); + throneInheritance.birth("king", "andy"); + throneInheritance.birth("king", "bob"); + throneInheritance.birth("king", "catherine"); + throneInheritance.birth("andy", "matthew"); + throneInheritance.birth("bob", "alex"); + throneInheritance.birth("bob", "asha"); + assertEquals( + Arrays.asList("king", "andy", "matthew", "bob", "alex", "asha", "catherine"), + throneInheritance.getInheritanceOrder()); + throneInheritance.death("bob"); + assertEquals( + Arrays.asList("king", "andy", "matthew", "alex", "asha", "catherine"), + throneInheritance.getInheritanceOrder()); + } + + @Test + public void test2() { + throneInheritance = new _1600.Solution1.ThroneInheritance("king"); + assertEquals(Arrays.asList("king"), throneInheritance.getInheritanceOrder()); + throneInheritance.birth("king", "clyde"); + throneInheritance.birth("clyde", "shannon"); + throneInheritance.birth("shannon", "scott"); + throneInheritance.birth("king", "keith"); + assertEquals( + Arrays.asList("king", "clyde", "shannon", "scott", "keith"), + throneInheritance.getInheritanceOrder()); + throneInheritance.birth("clyde", "joseph"); + assertEquals( + Arrays.asList("king", "clyde", "shannon", "scott", "joseph", "keith"), + throneInheritance.getInheritanceOrder()); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1604Test.java b/src/test/java/com/fishercoder/secondthousand/_1604Test.java new file mode 100644 index 0000000000..0ae28ffb92 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1604Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1604; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1604Test { + private _1604.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1604.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("daniel"), + solution1.alertNames( + new String[] {"daniel", "daniel", "daniel", "luis", "luis", "luis", "luis"}, + new String[] { + "10:00", "10:40", "11:00", "09:00", "11:00", "13:00", "15:00" + })); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList("bob"), + solution1.alertNames( + new String[] {"alice", "alice", "alice", "bob", "bob", "bob", "bob"}, + new String[] { + "12:01", "12:00", "18:00", "21:00", "21:20", "21:30", "23:00" + })); + } + + @Test + public void test3() { + assertEquals( + Arrays.asList(), + solution1.alertNames( + new String[] {"john", "john", "john"}, + new String[] {"23:58", "23:59", "00:01"})); + } + + @Test + public void test4() { + assertEquals( + Arrays.asList("clare", "leslie"), + solution1.alertNames( + new String[] { + "leslie", "leslie", "leslie", "clare", "clare", "clare", "clare" + }, + new String[] { + "13:00", "13:20", "14:00", "18:00", "18:51", "19:30", "19:49" + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1605Test.java b/src/test/java/com/fishercoder/secondthousand/_1605Test.java new file mode 100644 index 0000000000..dd0eeef619 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1605Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1605; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1605Test { + private _1605.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1605.Solution1(); + } + + @Test + public void test1() { + int[][] expected = new int[][] {{0, 5, 0}, {0, 1, 6}, {8, 0, 2}}; + assertArrayEquals( + expected, solution1.restoreMatrix(new int[] {5, 7, 10}, new int[] {8, 6, 8})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1625Test.java b/src/test/java/com/fishercoder/secondthousand/_1625Test.java new file mode 100644 index 0000000000..29de522566 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1625Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1625; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1625Test { + private _1625.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1625.Solution1(); + } + + @Test + public void test1() { + assertEquals("2050", solution1.findLexSmallestString("5525", 9, 2)); + } + + @Test + public void test2() { + assertEquals("24", solution1.findLexSmallestString("74", 5, 1)); + } + + @Test + public void test3() { + assertEquals("0011", solution1.findLexSmallestString("0011", 4, 2)); + } + + @Test + public void test4() { + assertEquals("00553311", solution1.findLexSmallestString("43987654", 7, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1626Test.java b/src/test/java/com/fishercoder/secondthousand/_1626Test.java new file mode 100644 index 0000000000..7e637770c5 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1626Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1626; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1626Test { + private _1626.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1626.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.bestTeamScore(new int[] {1, 2, 3, 5}, new int[] {8, 9, 10, 1})); + } + + @Test + public void test2() { + assertEquals( + 34, + solution1.bestTeamScore(new int[] {1, 3, 5, 10, 15}, new int[] {1, 2, 3, 4, 5})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1628Test.java b/src/test/java/com/fishercoder/secondthousand/_1628Test.java new file mode 100644 index 0000000000..da2dc39471 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1628Test.java @@ -0,0 +1,55 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1628; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1628Test { + private _1628.Solution1.TreeBuilder treeBuilderSolution1; + + @BeforeEach + public void setup() { + treeBuilderSolution1 = new _1628.Solution1.TreeBuilder(); + } + + @Test + public void test1() { + _1628.Solution1.Node node = treeBuilderSolution1.buildTree(new String[] {"3", "4", "+"}); + List list = node.print(node, new ArrayList<>()); + CommonUtils.printList(list); + assertEquals(7, node.evaluate()); + } + + @Test + public void test2() { + _1628.Solution1.Node node = + treeBuilderSolution1.buildTree(new String[] {"3", "4", "+", "2", "*", "7", "/"}); + List list = node.print(node, new ArrayList<>()); + CommonUtils.printList(list); + assertEquals(2, node.evaluate()); + } + + @Test + public void test3() { + _1628.Solution1.Node node = + treeBuilderSolution1.buildTree(new String[] {"4", "5", "2", "7", "+", "-", "*"}); + List list = node.print(node, new ArrayList<>()); + CommonUtils.printList(list); + assertEquals(-16, node.evaluate()); + } + + @Test + public void test4() { + _1628.Solution1.Node node = + treeBuilderSolution1.buildTree( + new String[] {"4", "2", "+", "3", "5", "1", "-", "*", "+"}); + List list = node.print(node, new ArrayList<>()); + CommonUtils.printList(list); + assertEquals(18, node.evaluate()); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1636Test.java b/src/test/java/com/fishercoder/secondthousand/_1636Test.java new file mode 100644 index 0000000000..b9c474ee04 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1636Test.java @@ -0,0 +1,45 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1636; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1636Test { + private _1636.Solution1 solution1; + private _1636.Solution2 solution2; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1636.Solution1(); + solution2 = new _1636.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {1, 1, 2, 2, 2, 3}; + assertArrayEquals(new int[] {3, 1, 1, 2, 2, 2}, solution2.frequencySort(nums)); + assertArrayEquals(new int[] {3, 1, 1, 2, 2, 2}, solution1.frequencySort(nums)); + } + + @Test + public void test2() { + nums = + new int[] { + -53, -53, 52, 52, 52, 52, -53, -53, 52, -53, 52, 52, 52, -53, 52, 52, -53, 52, + -53, 52, -53, 52, 52, 52, 52, 52, 52, 52, 52, 52, -53, 52, -53, 52, -53, 52, 52, + 52, -53, -53, 52, -53, 52, 52, 52, 52, -53, -53, -53, -53, -53, 52, 52, -53, 52, + -53, 52, 52, 52 + }; + assertArrayEquals( + new int[] { + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, + 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, + 52, 52, 52, 52, 52 + }, + solution1.frequencySort(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1640Test.java b/src/test/java/com/fishercoder/secondthousand/_1640Test.java new file mode 100644 index 0000000000..bf368c7c14 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1640Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1640; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1640Test { + private _1640.Solution1 solution1; + private static int[] arr; + private static int[][] pieces; + + @BeforeEach + public void setup() { + solution1 = new _1640.Solution1(); + } + + @Test + public void test1() { + arr = new int[] {85}; + pieces = new int[][] {{85}}; + assertEquals(true, solution1.canFormArray(arr, pieces)); + } + + @Test + public void test2() { + arr = new int[] {91, 4, 64, 78}; + pieces = new int[][] {{78}, {4, 64}, {91}}; + assertEquals(true, solution1.canFormArray(arr, pieces)); + } + + @Test + public void test3() { + arr = new int[] {49, 18, 16}; + pieces = new int[][] {{16, 18, 49}}; + assertEquals(false, solution1.canFormArray(arr, pieces)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1641Test.java b/src/test/java/com/fishercoder/secondthousand/_1641Test.java new file mode 100644 index 0000000000..ff7a97dd46 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1641Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1641; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1641Test { + private _1641.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1641.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.countVowelStrings(1)); + } + + @Test + public void test2() { + assertEquals(15, solution1.countVowelStrings(2)); + } + + @Test + public void test3() { + assertEquals(35, solution1.countVowelStrings(3)); + } + + @Test + public void test4() { + assertEquals(70, solution1.countVowelStrings(4)); + } + + @Test + public void test5() { + assertEquals(66045, solution1.countVowelStrings(33)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1642Test.java b/src/test/java/com/fishercoder/secondthousand/_1642Test.java new file mode 100644 index 0000000000..b85e155906 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1642Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1642; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1642Test { + private _1642.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1642.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.furthestBuilding(new int[] {4, 2, 7, 6, 9, 14, 12}, 5, 1)); + } + + @Test + public void test2() { + assertEquals( + 7, solution1.furthestBuilding(new int[] {4, 12, 2, 7, 3, 18, 20, 3, 19}, 10, 2)); + } + + @Test + public void test3() { + assertEquals(3, solution1.furthestBuilding(new int[] {14, 3, 19, 3}, 17, 0)); + } + + @Test + public void test4() { + assertEquals(6, solution1.furthestBuilding(new int[] {17, 16, 5, 10, 10, 14, 7}, 74, 6)); + } + + @Test + public void test5() { + assertEquals(1, solution1.furthestBuilding(new int[] {7, 5, 13}, 0, 0)); + } + + @Test + public void test6() { + assertEquals(3, solution1.furthestBuilding(new int[] {2, 7, 9, 12}, 5, 1)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1644Test.java b/src/test/java/com/fishercoder/secondthousand/_1644Test.java new file mode 100644 index 0000000000..3bbdb23b36 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1644Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1644; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +public class _1644Test { + private _1644.Solution1 solution1; + + @Test + public void test1() { + solution1 = new _1644.Solution1(); + TreeNode root = + TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4)); + TreeUtils.printBinaryTree(root); + TreeNode p = TreeUtils.constructBinaryTree(Arrays.asList(5, 6, 2, null, null, 7, 4)); + TreeUtils.printBinaryTree(p); + TreeNode q = new TreeNode(10); + TreeNode actual = solution1.lowestCommonAncestor(root, p, q); + System.out.println("actual: " + actual); + assertEquals(null, actual); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1646Test.java b/src/test/java/com/fishercoder/secondthousand/_1646Test.java new file mode 100644 index 0000000000..ff61aa4545 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1646Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1646; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1646Test { + private _1646.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1646.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.getMaximumGenerated(3)); + } + + @Test + public void test2() { + assertEquals(1, solution1.getMaximumGenerated(2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1652Test.java b/src/test/java/com/fishercoder/secondthousand/_1652Test.java new file mode 100644 index 0000000000..951459d470 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1652Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1652; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1652Test { + private _1652.Solution1 solution1; + private static int[] code; + + @BeforeEach + public void setup() { + solution1 = new _1652.Solution1(); + } + + @Test + public void test1() { + code = new int[] {5, 7, 1, 4}; + assertArrayEquals(new int[] {12, 10, 16, 13}, solution1.decrypt(code, 3)); + } + + @Test + public void test2() { + code = new int[] {1, 2, 3, 4}; + assertArrayEquals(new int[] {0, 0, 0, 0}, solution1.decrypt(code, 0)); + } + + @Test + public void test3() { + code = new int[] {2, 4, 9, 3}; + assertArrayEquals(new int[] {12, 5, 6, 13}, solution1.decrypt(code, -2)); + } + + @Test + public void test4() { + code = new int[] {10, 5, 7, 7, 3, 2, 10, 3, 6, 9, 1, 6}; + assertArrayEquals( + new int[] {22, 26, 22, 28, 29, 22, 19, 22, 18, 21, 28, 19}, + solution1.decrypt(code, -4)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1653Test.java b/src/test/java/com/fishercoder/secondthousand/_1653Test.java new file mode 100644 index 0000000000..439f76cbe4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1653Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1653; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1653Test { + private _1653.Solution1 solution1; + private _1653.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1653.Solution1(); + solution2 = new _1653.Solution2(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minimumDeletions("aababbab")); + assertEquals(2, solution2.minimumDeletions("aababbab")); + } + + @Test + public void test2() { + assertEquals(0, solution1.minimumDeletions("aaabbb")); + assertEquals(0, solution2.minimumDeletions("aaabbb")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1656Test.java b/src/test/java/com/fishercoder/secondthousand/_1656Test.java new file mode 100644 index 0000000000..83e143e265 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1656Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1656; +import java.util.Arrays; +import java.util.Collections; +import org.junit.jupiter.api.Test; + +public class _1656Test { + private _1656.Solution1.OrderedStream orderedStream; + + @Test + public void test1() { + orderedStream = new _1656.Solution1.OrderedStream(5); + assertEquals(Collections.emptyList(), orderedStream.insert(3, "ccccc")); + assertEquals(Arrays.asList("aaaaa"), orderedStream.insert(1, "aaaaa")); + assertEquals(Arrays.asList("bbbbb", "ccccc"), orderedStream.insert(2, "bbbbb")); + assertEquals(Collections.emptyList(), orderedStream.insert(5, "eeeee")); + assertEquals(Arrays.asList("ddddd", "eeeee"), orderedStream.insert(4, "ddddd")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1657Test.java b/src/test/java/com/fishercoder/secondthousand/_1657Test.java new file mode 100644 index 0000000000..241b04d2bc --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1657Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1657; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1657Test { + private _1657.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1657.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.closeStrings("abc", "bca")); + } + + @Test + public void test2() { + assertEquals(false, solution1.closeStrings("a", "aa")); + } + + @Test + public void test3() { + assertEquals(true, solution1.closeStrings("cabbba", "abbccc")); + } + + @Test + public void test4() { + assertEquals(false, solution1.closeStrings("cabbba", "aabbss")); + } + + @Test + public void test5() { + assertEquals(false, solution1.closeStrings("abbbzcf", "babzzcz")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1658Test.java b/src/test/java/com/fishercoder/secondthousand/_1658Test.java new file mode 100644 index 0000000000..bbf9adedd1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1658Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1658; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1658Test { + private _1658.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1658.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minOperations(new int[] {1, 1, 4, 2, 3}, 5)); + } + + @Test + public void test2() { + assertEquals(-1, solution1.minOperations(new int[] {5, 6, 7, 8, 9}, 4)); + } + + @Test + public void test3() { + assertEquals(5, solution1.minOperations(new int[] {3, 2, 20, 1, 1, 3}, 10)); + } + + @Test + public void test4() { + assertEquals( + 16, + solution1.minOperations( + new int[] { + 8828, 9581, 49, 9818, 9974, 9869, 9991, 10000, 10000, 10000, 9999, 9993, + 9904, 8819, 1231, 6309 + }, + 134365)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1663Test.java b/src/test/java/com/fishercoder/secondthousand/_1663Test.java new file mode 100644 index 0000000000..bf7c12a6cd --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1663Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1663; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1663Test { + private _1663.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1663.Solution1(); + } + + @Test + public void test1() { + assertEquals("aay", solution1.getSmallestString(3, 27)); + } + + @Test + public void test2() { + assertEquals("aaszz", solution1.getSmallestString(5, 73)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1669Test.java b/src/test/java/com/fishercoder/secondthousand/_1669Test.java new file mode 100644 index 0000000000..e559bae654 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1669Test.java @@ -0,0 +1,52 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.secondthousand._1669; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1669Test { + private _1669.Solution1 solution1; + private _1669.Solution2 solution2; + private static ListNode l1; + private static ListNode l2; + private static int a; + private static int b; + private static ListNode list1; + private static ListNode list2; + private static ListNode expected; + private static ListNode actual; + + @BeforeEach + public void setup() { + solution1 = new _1669.Solution1(); + solution2 = new _1669.Solution2(); + } + + @Test + public void test1() { + list1 = LinkedListUtils.contructLinkedList(new int[] {0, 1, 2, 3, 4, 5}); + list2 = LinkedListUtils.contructLinkedList(new int[] {1000000, 1000001, 1000002}); + expected = + LinkedListUtils.contructLinkedList( + new int[] {0, 1, 2, 1000000, 1000001, 1000002, 5}); + actual = solution1.mergeInBetween(list1, 3, 4, list2); + LinkedListUtils.printList(actual); + assertEquals(expected, actual); + } + + @Test + public void test2() { + l1 = LinkedListUtils.contructLinkedList(new int[] {0, 1, 2, 3, 4, 5}); + l2 = LinkedListUtils.contructLinkedList(new int[] {1000000, 1000001, 1000002}); + a = 3; + b = 4; + assertEquals( + LinkedListUtils.contructLinkedList( + new int[] {0, 1, 2, 1000000, 1000001, 1000002, 5}), + solution2.mergeInBetween(l1, a, b, l2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1670Test.java b/src/test/java/com/fishercoder/secondthousand/_1670Test.java new file mode 100644 index 0000000000..6cded39607 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1670Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1670; +import org.junit.jupiter.api.Test; + +public class _1670Test { + private _1670.Solution1.FrontMiddleBackQueue solution1; + + @Test + public void test1() { + solution1 = new _1670.Solution1.FrontMiddleBackQueue(); + solution1.pushFront(1); + solution1.pushBack(2); + solution1.pushMiddle(3); + solution1.pushMiddle(4); + assertEquals(1, solution1.popFront()); + assertEquals(3, solution1.popMiddle()); + assertEquals(4, solution1.popMiddle()); + assertEquals(2, solution1.popFront()); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1673Test.java b/src/test/java/com/fishercoder/secondthousand/_1673Test.java new file mode 100644 index 0000000000..ec4584770a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1673Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1673; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1673Test { + private _1673.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1673.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals(new int[] {2, 6}, solution1.mostCompetitive(new int[] {3, 5, 2, 6}, 2)); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {2, 3, 3, 4}, + solution1.mostCompetitive(new int[] {2, 4, 3, 3, 5, 4, 9, 6}, 4)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1675Test.java b/src/test/java/com/fishercoder/secondthousand/_1675Test.java new file mode 100644 index 0000000000..129fb7c44e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1675Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1675; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1675Test { + private _1675.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1675.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minimumDeviation(new int[] {1, 2, 3, 4})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1676Test.java b/src/test/java/com/fishercoder/secondthousand/_1676Test.java new file mode 100644 index 0000000000..5436df2355 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1676Test.java @@ -0,0 +1,45 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1676; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1676Test { + private _1676.Solution1 solution1; + private _1676.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1676.Solution1(); + solution2 = new _1676.Solution2(); + } + + @Test + public void test1() { + TreeNode root = + TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4)); + TreeUtils.printBinaryTree(root); + TreeNode node1 = TreeUtils.constructBinaryTree(Arrays.asList(4)); + TreeNode node2 = TreeUtils.constructBinaryTree(Arrays.asList(7)); + TreeNode[] nodes = new TreeNode[] {node1, node2}; + TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(2, 7, 4)); + assertEquals(expected, solution1.lowestCommonAncestor(root, nodes)); + } + + @Test + public void test2() { + TreeNode root = + TreeUtils.constructBinaryTree(Arrays.asList(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4)); + TreeUtils.printBinaryTree(root); + TreeNode node1 = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 8)); + TreeNode[] nodes = new TreeNode[] {node1}; + TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 8)); + // assertEquals(expected, solution1.lowestCommonAncestor(root, nodes)); + // assertEquals(expected, solution2.lowestCommonAncestor(root, nodes)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1679Test.java b/src/test/java/com/fishercoder/secondthousand/_1679Test.java new file mode 100644 index 0000000000..014c9983a7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1679Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1679; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1679Test { + private _1679.Solution1 solution1; + private static int[] nums; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _1679.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 5, 4, 4, 1, 3, 4, 4, 1, 4, 4, 1, 2, 1, 2, 2, 3, 2, 4, 2}; + k = 3; + assertEquals(4, solution1.maxOperations(nums, k)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1685Test.java b/src/test/java/com/fishercoder/secondthousand/_1685Test.java new file mode 100644 index 0000000000..643fa9cbb3 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1685Test.java @@ -0,0 +1,30 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1685; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1685Test { + private _1685.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _1685.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 3, 5}; + assertArrayEquals(new int[] {4, 3, 5}, solution1.getSumAbsoluteDifferences(nums)); + } + + @Test + public void test2() { + nums = new int[] {1, 4, 6, 8, 10}; + assertArrayEquals( + new int[] {24, 15, 13, 15, 21}, solution1.getSumAbsoluteDifferences(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1686Test.java b/src/test/java/com/fishercoder/secondthousand/_1686Test.java new file mode 100644 index 0000000000..a05eb59d78 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1686Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1686; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1686Test { + private _1686.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1686.Solution1(); + } + + @Test + public void test1() { + assertEquals(-1, solution1.stoneGameVI(new int[] {2, 4, 3}, new int[] {1, 6, 7})); + } + + @Test + public void test2() { + assertEquals(1, solution1.stoneGameVI(new int[] {1, 3}, new int[] {2, 1})); + } + + @Test + public void test3() { + /** + * in this case, Alice doesn't want to take the stone with value 2, because that'll result + * in her loss to Bob instead, she could take the stone with value 1, taking away Bob's + * stone with value 3, ending in a tie which is better than a loss. + */ + assertEquals(0, solution1.stoneGameVI(new int[] {1, 2}, new int[] {3, 1})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1688Test.java b/src/test/java/com/fishercoder/secondthousand/_1688Test.java new file mode 100644 index 0000000000..7f1368177b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1688Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1688; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1688Test { + private _1688.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1688.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.numberOfMatches(7)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1690Test.java b/src/test/java/com/fishercoder/secondthousand/_1690Test.java new file mode 100644 index 0000000000..83fffc09d4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1690Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1690; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1690Test { + private _1690.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1690.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.stoneGameVII(new int[] {5, 3, 1, 4, 2})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1694Test.java b/src/test/java/com/fishercoder/secondthousand/_1694Test.java new file mode 100644 index 0000000000..dcd77ef299 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1694Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1694; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1694Test { + private _1694.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1694.Solution1(); + } + + @Test + public void test1() { + assertEquals("123-456", solution1.reformatNumber("1-23-45 6")); + } + + @Test + public void test2() { + assertEquals("123-45-67", solution1.reformatNumber("123 4-567")); + } + + @Test + public void test3() { + assertEquals("123-456-78", solution1.reformatNumber("123 4-5678")); + } + + @Test + public void test4() { + assertEquals("12", solution1.reformatNumber("12")); + } + + @Test + public void test5() { + assertEquals("175-229-353-94-75", solution1.reformatNumber("--17-5 229 35-39475 ")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1695Test.java b/src/test/java/com/fishercoder/secondthousand/_1695Test.java new file mode 100644 index 0000000000..67c7f906e1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1695Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1695; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1695Test { + private _1695.Solution1 solution1; + private _1695.Solution2 solution2; + private static int[] nums; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _1695.Solution1(); + solution2 = new _1695.Solution2(); + } + + @Test + public void test1() { + nums = new int[] {4, 2, 4, 5, 6}; + expected = 17; + assertEquals(expected, solution1.maximumUniqueSubarray(nums)); + assertEquals(expected, solution2.maximumUniqueSubarray(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1700Test.java b/src/test/java/com/fishercoder/secondthousand/_1700Test.java new file mode 100644 index 0000000000..3272f6fa10 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1700Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1700; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1700Test { + private _1700.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1700.Solution1(); + } + + @Test + public void test1() { + assertEquals(0, solution1.countStudents(new int[] {1, 1, 0, 0}, new int[] {0, 1, 0, 1})); + } + + @Test + public void test2() { + assertEquals( + 3, + solution1.countStudents( + new int[] {1, 1, 1, 0, 0, 1}, new int[] {1, 0, 0, 0, 1, 1})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1701Test.java b/src/test/java/com/fishercoder/secondthousand/_1701Test.java new file mode 100644 index 0000000000..72df9dffa7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1701Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1701; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1701Test { + private _1701.Solution1 solution1; + private static int[] A; + + @BeforeEach + public void setup() { + solution1 = new _1701.Solution1(); + } + + @Test + public void test1() { + assertEquals(5.0, solution1.averageWaitingTime(new int[][] {{1, 2}, {2, 5}, {4, 3}})); + } + + @Test + public void test2() { + assertEquals( + 3.25, solution1.averageWaitingTime(new int[][] {{5, 2}, {5, 4}, {10, 3}, {20, 1}})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1705Test.java b/src/test/java/com/fishercoder/secondthousand/_1705Test.java new file mode 100644 index 0000000000..32073599e1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1705Test.java @@ -0,0 +1,40 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1705; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1705Test { + private _1705.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1705.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 7, solution1.eatenApples(new int[] {1, 2, 3, 5, 2}, new int[] {3, 2, 1, 4, 2})); + } + + @Test + public void test2() { + assertEquals( + 5, + solution1.eatenApples(new int[] {3, 0, 0, 0, 0, 2}, new int[] {3, 0, 0, 0, 0, 2})); + } + + @Test + public void test3() { + assertEquals(5, solution1.eatenApples(new int[] {9, 2}, new int[] {3, 5})); + } + + @Test + public void test4() { + assertEquals( + 8, solution1.eatenApples(new int[] {2, 1, 1, 4, 5}, new int[] {10, 10, 6, 4, 2})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1708Test.java b/src/test/java/com/fishercoder/secondthousand/_1708Test.java new file mode 100644 index 0000000000..467ed2f2b1 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1708Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1708; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1708Test { + private _1708.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1708.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {5, 2, 3}, solution1.largestSubarray(new int[] {1, 4, 5, 2, 3}, 3)); + } + + @Test + public void test2() { + assertArrayEquals(new int[] {5}, solution1.largestSubarray(new int[] {1, 4, 5, 2, 3}, 1)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1711Test.java b/src/test/java/com/fishercoder/secondthousand/_1711Test.java new file mode 100644 index 0000000000..6b6e89bfd8 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1711Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1711; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1711Test { + private _1711.Solution1 solution1; + private static int[] deliciousness; + + @BeforeEach + public void setup() { + solution1 = new _1711.Solution1(); + } + + @Test + public void test1() { + deliciousness = new int[] {1, 3, 5, 7, 9}; + assertEquals(4, solution1.countPairs(deliciousness)); + } + + @Test + public void test2() { + deliciousness = new int[] {1, 1, 1, 3, 3, 3, 7}; + assertEquals(15, solution1.countPairs(deliciousness)); + } + + @Test + public void test3() { + deliciousness = + new int[] { + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 + }; + assertEquals(528, solution1.countPairs(deliciousness)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1716Test.java b/src/test/java/com/fishercoder/secondthousand/_1716Test.java new file mode 100644 index 0000000000..b7afb36411 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1716Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1716; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1716Test { + private _1716.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1716.Solution1(); + } + + @Test + public void test1() { + assertEquals(10, solution1.totalMoney(4)); + } + + @Test + public void test2() { + assertEquals(37, solution1.totalMoney(10)); + } + + @Test + public void test3() { + assertEquals(96, solution1.totalMoney(20)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1717Test.java b/src/test/java/com/fishercoder/secondthousand/_1717Test.java new file mode 100644 index 0000000000..5d77e4048f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1717Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1717; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1717Test { + private _1717.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1717.Solution1(); + } + + @Test + public void test1() { + assertEquals(19, solution1.maximumGain("cdbcbbaaabab", 4, 5)); + } + + @Test + public void test2() { + assertEquals(20, solution1.maximumGain("aabbaaxybbaabb", 5, 4)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1718Test.java b/src/test/java/com/fishercoder/secondthousand/_1718Test.java new file mode 100644 index 0000000000..79e7a93671 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1718Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1718; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1718Test { + private _1718.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1718.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals(new int[] {3, 1, 2, 3, 2}, solution1.constructDistancedSequence(3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1721Test.java b/src/test/java/com/fishercoder/secondthousand/_1721Test.java new file mode 100644 index 0000000000..5f6b65e09e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1721Test.java @@ -0,0 +1,67 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.secondthousand._1721; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1721Test { + private _1721.Solution1 solution1; + private _1721.Solution2 solution2; + private _1721.Solution3 solution3; + private static ListNode expected; + private static ListNode node; + private static int k; + + @BeforeEach + public void setup() { + solution1 = new _1721.Solution1(); + solution2 = new _1721.Solution2(); + solution3 = new _1721.Solution3(); + } + + @Test + public void test1() { + node = new ListNode(1); + node.next = new ListNode(2); + node.next.next = new ListNode(3); + node.next.next.next = new ListNode(4); + node.next.next.next.next = new ListNode(5); + + expected = new ListNode(1); + expected.next = new ListNode(4); + expected.next.next = new ListNode(3); + expected.next.next.next = new ListNode(2); + expected.next.next.next.next = new ListNode(5); + + k = 2; + assertEquals(expected, solution2.swapNodes(node, k)); + } + + @Test + public void test2() { + node = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5}); + expected = LinkedListUtils.contructLinkedList(new int[] {1, 4, 3, 2, 5}); + k = 2; + assertEquals(expected, solution2.swapNodes(node, k)); + } + + @Test + public void test3() { + node = LinkedListUtils.contructLinkedList(new int[] {90, 100}); + k = 2; + expected = LinkedListUtils.contructLinkedList(new int[] {100, 90}); + assertEquals(expected, solution1.swapNodes(node, k)); + } + + @Test + public void test4() { + node = LinkedListUtils.contructLinkedList(new int[] {90, 100}); + k = 2; + expected = LinkedListUtils.contructLinkedList(new int[] {100, 90}); + assertEquals(expected, solution3.swapNodes(node, k)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1726Test.java b/src/test/java/com/fishercoder/secondthousand/_1726Test.java new file mode 100644 index 0000000000..1170c4e71c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1726Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1726; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1726Test { + private _1726.Solution1 solution1; + private _1726.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _1726.Solution1(); + solution2 = new _1726.Solution2(); + } + + @Test + public void test1() { + assertEquals(8, solution1.tupleSameProduct(new int[] {2, 3, 4, 6})); + } + + @Test + public void test2() { + assertEquals(16, solution1.tupleSameProduct(new int[] {1, 2, 4, 5, 10})); + } + + @Test + public void test3() { + assertEquals(40, solution1.tupleSameProduct(new int[] {2, 3, 4, 6, 8, 12})); + } + + @Test + public void test4() { + assertEquals(0, solution1.tupleSameProduct(new int[] {2, 3, 5, 7})); + } + + @Test + public void test5() { + assertEquals(128, solution1.tupleSameProduct(new int[] {1, 2, 3, 4, 6, 8, 12, 24})); + } + + @Test + public void test6() { + assertEquals(40, solution2.tupleSameProduct(new int[] {2, 3, 4, 6, 8, 12})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1727Test.java b/src/test/java/com/fishercoder/secondthousand/_1727Test.java new file mode 100644 index 0000000000..682b058299 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1727Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1727; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1727Test { + private _1727.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1727.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 8, + solution1.largestSubmatrix( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1,1,1,1,1,1],[1,1,0,1,1,0,1],[1,0,0,1,0,1,1]"))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1730Test.java b/src/test/java/com/fishercoder/secondthousand/_1730Test.java new file mode 100644 index 0000000000..1401b331a6 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1730Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1730; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1730Test { + private _1730.Solution1 test; + + @BeforeEach + public void setup() { + test = new _1730.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 3, + test.getFood( + new char[][] { + {'X', 'X', 'X', 'X', 'X', 'X'}, + {'X', '*', 'O', 'O', 'O', 'X'}, + {'X', 'O', 'O', '#', 'O', 'X'}, + {'X', 'X', 'X', 'X', 'X', 'X'}, + })); + } + + @Test + public void test2() { + assertEquals( + -1, + test.getFood( + new char[][] { + {'X', 'X', 'X', 'X', 'X'}, + {'X', '*', 'X', 'O', 'X'}, + {'X', 'O', 'X', '#', 'X'}, + {'X', 'X', 'X', 'X', 'X'}, + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1733Test.java b/src/test/java/com/fishercoder/secondthousand/_1733Test.java new file mode 100644 index 0000000000..11c4d61533 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1733Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1733; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1733Test { + private _1733.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1733.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 1, + solution1.minimumTeachings( + 2, new int[][] {{1}, {2}, {1, 2}}, new int[][] {{1, 2}, {1, 3}, {2, 3}})); + } + + @Test + public void test2() { + assertEquals( + 2, + solution1.minimumTeachings( + 3, + new int[][] {{2}, {1, 3}, {1, 2}, {3}}, + new int[][] {{1, 4}, {1, 2}, {3, 4}, {2, 3}})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1745Test.java b/src/test/java/com/fishercoder/secondthousand/_1745Test.java new file mode 100644 index 0000000000..79f8cacd49 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1745Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1745; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1745Test { + private _1745.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1745.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.checkPartitioning("abcbdd")); + } + + @Test + public void test2() { + assertEquals(false, solution1.checkPartitioning("bcbddxy")); + } + + @Test + public void test3() { + assertEquals(true, solution1.checkPartitioning("juchzcedhfesefhdeczhcujzzvbmoeombv")); + } + + @Test + public void test4() { + assertEquals( + true, + solution1.checkPartitioning( + "gbofdldvwelqiizbievfolrujxnwjmjwsjrjeqecwssgtlteltslfzkblzihcgwjnqaiqbxohcnxulxozzkanaofgoddogfoanakzzoxluxnchoxbqiaqnjwgchizlbkzflstletltgsswceqejrjswjmjwnxjurlofveibziiqlewvdldfobgxebrcrbexv")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1746Test.java b/src/test/java/com/fishercoder/secondthousand/_1746Test.java new file mode 100644 index 0000000000..55844e5865 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1746Test.java @@ -0,0 +1,45 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1746; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1746Test { + private _1746.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1746.Solution1(); + } + + @Test + public void test1() { + assertEquals(17, solution1.maxSumAfterOperation(new int[] {2, -1, -4, -3})); + } + + @Test + public void test2() { + assertEquals(4, solution1.maxSumAfterOperation(new int[] {1, -1, 1, 1, -1, -1, 1})); + } + + @Test + public void test3() { + assertEquals(1936, solution1.maxSumAfterOperation(new int[] {-44})); + } + + @Test + public void test4() { + assertEquals( + 10954, + solution1.maxSumAfterOperation( + new int[] { + 29, 71, -52, -23, -28, 50, 27, 29, 0, 50, -92, 22, -38, 90, 3, 6, 70, + -56, -7, 40, 79, 98, 72, 88, -5, -78, 12, 69, 30, -73, 99, -59, 33, 0, + -6, 25, 87, -93, 20, -89, -22, 80, 57, 51, 48, 0, 65, -57, -57, 28, -42, + -97, 97, -49, 38, 40, -41, 3, 31, -12, 47, -10, 17, -32, 68, 40, 55, 86, + -99, -2, 100, 89, 31, -67 + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1752Test.java b/src/test/java/com/fishercoder/secondthousand/_1752Test.java new file mode 100644 index 0000000000..45a5856fc0 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1752Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1752; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1752Test { + private _1752.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1752.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.check(new int[] {3, 4, 5, 1, 2})); + } + + @Test + public void test2() { + assertEquals(false, solution1.check(new int[] {2, 1, 3, 4})); + } + + @Test + public void test3() { + assertEquals(true, solution1.check(new int[] {1, 2, 3})); + } + + @Test + public void test4() { + assertEquals(true, solution1.check(new int[] {1, 1, 1})); + } + + @Test + public void test5() { + assertEquals(true, solution1.check(new int[] {2, 1})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1753Test.java b/src/test/java/com/fishercoder/secondthousand/_1753Test.java new file mode 100644 index 0000000000..b9d3b930e3 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1753Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1753; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1753Test { + private _1753.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1753.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.maximumScore(1, 2, 6)); + } + + @Test + public void test2() { + assertEquals(3, solution1.maximumScore(6, 2, 1)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1754Test.java b/src/test/java/com/fishercoder/secondthousand/_1754Test.java new file mode 100644 index 0000000000..7f9e47bd6e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1754Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1754; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1754Test { + private _1754.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1754.Solution1(); + } + + @Test + public void test1() { + assertEquals("cbcabaaaaa", solution1.largestMerge("cabaa", "bcaaa")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1758Test.java b/src/test/java/com/fishercoder/secondthousand/_1758Test.java new file mode 100644 index 0000000000..a72eaf9d2e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1758Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1758; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1758Test { + private _1758.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1758.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.minOperations("0100")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1759Test.java b/src/test/java/com/fishercoder/secondthousand/_1759Test.java new file mode 100644 index 0000000000..f802fbc3d0 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1759Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1759; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1759Test { + private _1759.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1759.Solution1(); + } + + @Test + public void test1() { + assertEquals(13, solution1.countHomogenous("abbcccaa")); + } + + @Test + public void test2() { + assertEquals(17, solution1.countHomogenous("abbcccaabaa")); + } + + @Test + public void test3() { + assertEquals( + 499500, + solution1.countHomogenous( + "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1762Test.java b/src/test/java/com/fishercoder/secondthousand/_1762Test.java new file mode 100644 index 0000000000..a656806a21 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1762Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1762; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1762Test { + private _1762.Solution1 solution1; + private static int[] heights; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _1762.Solution1(); + } + + @Test + public void test1() { + heights = new int[] {4, 2, 3, 1}; + expected = new int[] {0, 2, 3}; + assertArrayEquals(expected, solution1.findBuildings(heights)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1768Test.java b/src/test/java/com/fishercoder/secondthousand/_1768Test.java new file mode 100644 index 0000000000..1e257ab32f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1768Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.solutions.secondthousand._1768; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1768Test { + private _1768.Solution1 solution1; + private _1768.Solution2 solution2; + private static String word1; + private static String word2; + private static String expected; + private static String actual; + + @BeforeEach + public void setup() { + solution1 = new _1768.Solution1(); + solution2 = new _1768.Solution2(); + } + + @Test + public void test1() { + word1 = "abc"; + word2 = "pqr"; + expected = "apbqcr"; + actual = solution1.mergeAlternately(word1, word2); + Assertions.assertEquals(actual, expected); + actual = solution2.mergeAlternately(word1, word2); + Assertions.assertEquals(actual, expected); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1772Test.java b/src/test/java/com/fishercoder/secondthousand/_1772Test.java new file mode 100644 index 0000000000..d59ca8291c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1772Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1772; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1772Test { + private _1772.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1772.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new String[] {"touch", "cooler", "lock"}, + solution1.sortFeatures( + new String[] {"cooler", "lock", "touch"}, + new String[] { + "i like cooler cooler", "lock touch cool", "locker like touch" + })); + } + + @Test + public void test2() { + + assertArrayEquals( + new String[] {"a", "aa", "b", "c"}, + solution1.sortFeatures( + new String[] {"a", "aa", "b", "c"}, + new String[] {"a", "a aa", "a a a a a", "b a"})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1774Test.java b/src/test/java/com/fishercoder/secondthousand/_1774Test.java new file mode 100644 index 0000000000..86555359f2 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1774Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1774; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1774Test { + private _1774.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1774.Solution1(); + } + + @Test + public void test1() { + assertEquals(10, solution1.closestCost(new int[] {1, 7}, new int[] {3, 4}, 10)); + } + + @Test + public void test2() { + assertEquals(17, solution1.closestCost(new int[] {2, 3}, new int[] {4, 5, 100}, 18)); + } + + @Test + public void test3() { + assertEquals(8, solution1.closestCost(new int[] {3, 10}, new int[] {2, 5}, 9)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1775Test.java b/src/test/java/com/fishercoder/secondthousand/_1775Test.java new file mode 100644 index 0000000000..ea5a5de7d0 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1775Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1775; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1775Test { + private _1775.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1775.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 3, + solution1.minOperations( + new int[] {1, 2, 3, 4, 5, 6}, new int[] {1, 1, 2, 2, 2, 2})); + } + + @Test + public void test2() { + assertEquals(-1, solution1.minOperations(new int[] {1, 1, 1, 1, 1, 1, 1}, new int[] {6})); + } + + @Test + public void test3() { + assertEquals(3, solution1.minOperations(new int[] {6, 6}, new int[] {1})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1781Test.java b/src/test/java/com/fishercoder/secondthousand/_1781Test.java new file mode 100644 index 0000000000..8c584ddbd7 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1781Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1781; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1781Test { + private _1781.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1781.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.beautySum("aabcb")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1790Test.java b/src/test/java/com/fishercoder/secondthousand/_1790Test.java new file mode 100644 index 0000000000..bef24cded9 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1790Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1790; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1790Test { + private _1790.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1790.Solution1(); + } + + @Test + public void test1() { + assertEquals(false, solution1.areAlmostEqual("a", "z")); + } + + @Test + public void test2() { + assertEquals(true, solution1.areAlmostEqual("bank", "kanb")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1792Test.java b/src/test/java/com/fishercoder/secondthousand/_1792Test.java new file mode 100644 index 0000000000..4e57a0cad3 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1792Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1792; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1792Test { + private _1792.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1792.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 0.78333, + solution1.maxAverageRatio( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2],[3,5],[2,2]"), + 2), + 0.00001); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1804Test.java b/src/test/java/com/fishercoder/secondthousand/_1804Test.java new file mode 100644 index 0000000000..8ae76ac8e8 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1804Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1804; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1804Test { + private _1804.Solution1.Trie solution1; + + @BeforeEach + public void setup() {} + + @Test + public void test1() { + solution1 = new _1804.Solution1.Trie(); + solution1.insert("apple"); + solution1.insert("apple"); + assertEquals(2, solution1.countWordsEqualTo("apple")); + assertEquals(2, solution1.countWordsStartingWith("app")); + solution1.erase("apple"); + assertEquals(1, solution1.countWordsEqualTo("apple")); + assertEquals(1, solution1.countWordsStartingWith("app")); + solution1.erase("apple"); + assertEquals(0, solution1.countWordsStartingWith("app")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1813Test.java b/src/test/java/com/fishercoder/secondthousand/_1813Test.java new file mode 100644 index 0000000000..0b5a08ac45 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1813Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1813; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1813Test { + private _1813.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1813.Solution1(); + } + + @Test + public void test1() { + assertEquals(false, solution1.areSentencesSimilar("of", "A lot of words")); + } + + @Test + public void test2() { + assertEquals(true, solution1.areSentencesSimilar("Eating right now", "Eating")); + } + + @Test + public void test3() { + assertEquals(true, solution1.areSentencesSimilar("c h p Ny", "c BDQ r h p Ny")); + } + + @Test + public void test4() { + assertEquals(true, solution1.areSentencesSimilar("A", "a A b A")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1814Test.java b/src/test/java/com/fishercoder/secondthousand/_1814Test.java new file mode 100644 index 0000000000..2d0e84609a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1814Test.java @@ -0,0 +1,62 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1814; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1814Test { + private _1814.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1814.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.countNicePairs(new int[] {42, 11, 1, 97})); + } + + @Test + public void test2() { + assertEquals(4, solution1.countNicePairs(new int[] {13, 10, 35, 24, 76})); + } + + @Test + public void test3() { + assertEquals( + 2, + solution1.countNicePairs( + new int[] { + 352171103, 442454244, 42644624, 152727101, 413370302, 293999243 + })); + } + + @Test + public void test4() { + assertEquals( + 678, + solution1.countNicePairs( + new int[] { + 8047408, 192867140, 497837845, 279787822, 151999002, 168514912, + 193424242, 399636844, 132424231, 476736524, 267958611, 493350382, + 476382727, 232939232, 197000791, 295291645, 126313621, 374645524, + 7956597, 1376731, 496463745, 234481430, 359130803, 287625836, 250572050, + 42311324, 477434624, 493231448, 493231244, 150494051, 184645534, + 365252413, 495764582, 335976531, 384564332, 377151623, 198736741, + 335161533, 245552540, 194897341, 83911938, 220562020, 496645745, + 287151782, 374635526, 372483324, 485101584, 271797172, 244949442, + 254333303, 251635002, 459181805, 472392123, 241350140, 256121502, + 336895621, 354635302, 358909704, 194525491, 3606063, 194150341, + 63477436, 341936141, 60299206, 69811896, 369928813, 229926920, + 435310522, 299542980, 463777364, 164534512, 305885501, 437181734, + 74288247, 487281835, 171161022, 423966312, 496989544, 452633252, + 252433101, 141565141, 315895501, 478897927, 232532230, 472451262, + 160504114, 476666674, 6179716, 251483002, 474777474, 443532332, + 475808424, 457514604, 400936002, 384878483, 172616122, 283292232, + 165645615, 392000144, 378636873 + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1823Test.java b/src/test/java/com/fishercoder/secondthousand/_1823Test.java new file mode 100644 index 0000000000..461c275564 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1823Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1823; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1823Test { + private _1823.Solution1 solution1; + private _1823.Solution2 solution2; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _1823.Solution1(); + solution2 = new _1823.Solution2(); + } + + @Test + public void test1() { + expected = 1; + assertEquals(expected, solution1.findTheWinner(6, 5)); + assertEquals(expected, solution2.findTheWinner(6, 5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1826Test.java b/src/test/java/com/fishercoder/secondthousand/_1826Test.java new file mode 100644 index 0000000000..dde9593a9d --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1826Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1826; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1826Test { + private _1826.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1826.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.badSensor(new int[] {2, 3, 4, 5}, new int[] {2, 1, 3, 4})); + } + + @Test + public void test2() { + assertEquals(-1, solution1.badSensor(new int[] {2, 2, 2, 2, 2}, new int[] {2, 2, 2, 2, 5})); + } + + @Test + public void test3() { + assertEquals( + 2, solution1.badSensor(new int[] {2, 3, 2, 2, 3, 2}, new int[] {2, 3, 2, 3, 2, 7})); + } + + @Test + public void test4() { + assertEquals( + -1, + solution1.badSensor(new int[] {1, 2, 3, 2, 3, 2}, new int[] {1, 2, 3, 3, 2, 3})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1836Test.java b/src/test/java/com/fishercoder/secondthousand/_1836Test.java new file mode 100644 index 0000000000..573133d3ef --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1836Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.secondthousand._1836; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1836Test { + private _1836.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1836.Solution1(); + } + + @Test + public void test1() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {1, 3}), + solution1.deleteDuplicatesUnsorted( + LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 2}))); + } + + @Test + public void test2() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {}), + solution1.deleteDuplicatesUnsorted( + LinkedListUtils.contructLinkedList(new int[] {2, 1, 1, 2}))); + } + + @Test + public void test3() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {1, 4}), + solution1.deleteDuplicatesUnsorted( + LinkedListUtils.contructLinkedList(new int[] {3, 2, 2, 1, 3, 2, 4}))); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1844Test.java b/src/test/java/com/fishercoder/secondthousand/_1844Test.java new file mode 100644 index 0000000000..6f047401ad --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1844Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.solutions.secondthousand._1844; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1844Test { + private _1844.Solution1 solution1; + private _1844.Solution2 solution2; + private static String s; + private static String actual; + + @BeforeEach + public void setup() { + solution1 = new _1844.Solution1(); + solution2 = new _1844.Solution2(); + } + + @Test + public void test1() { + s = "a1c1e1"; + actual = "abcdef"; + String expected = solution1.replaceDigits(s); + Assertions.assertEquals(actual, expected); + } + + @Test + public void test2() { + s = "a1c1e1"; + actual = "abcdef"; + String expected = solution2.replaceDigits(s); + Assertions.assertEquals(actual, expected); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1861Test.java b/src/test/java/com/fishercoder/secondthousand/_1861Test.java new file mode 100644 index 0000000000..817bcc30a4 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1861Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.secondthousand._1861; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1861Test { + private _1861.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1861.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new char[][] {{'.'}, {'#'}, {'#'}}, + solution1.rotateTheBox(new char[][] {{'#', '.', '#'}})); + } + + @Test + public void test2() { + assertArrayEquals( + new char[][] { + {'#', '.'}, + {'#', '#'}, + {'*', '*'}, + {'.', '.'} + }, + solution1.rotateTheBox( + new char[][] { + {'#', '.', '*', '.'}, + {'#', '#', '*', '.'} + })); + } + + @Test + public void test3() { + assertArrayEquals( + new char[][] { + {'.', '#', '#'}, + {'.', '#', '#'}, + {'#', '#', '*'}, + {'#', '*', '.'}, + {'#', '.', '*'}, + {'#', '.', '.'} + }, + solution1.rotateTheBox( + new char[][] { + {'#', '#', '*', '.', '*', '.'}, + {'#', '#', '#', '*', '.', '.'}, + {'#', '#', '#', '.', '#', '.'} + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1862Test.java b/src/test/java/com/fishercoder/secondthousand/_1862Test.java new file mode 100644 index 0000000000..0984797ddb --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1862Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1862; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1862Test { + private _1862.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1862.Solution1(); + } + + @Test + public void test1() { + assertEquals(49, solution1.sumOfFlooredPairs(new int[] {7, 7, 7, 7, 7, 7, 7})); + } + + @Test + public void test2() { + assertEquals(10, solution1.sumOfFlooredPairs(new int[] {2, 5, 9})); + } + + @Test + public void test3() { + assertEquals(17, solution1.sumOfFlooredPairs(new int[] {4, 3, 4, 3, 5})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1868Test.java b/src/test/java/com/fishercoder/secondthousand/_1868Test.java new file mode 100644 index 0000000000..c5efa8dfb3 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1868Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1868; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1868Test { + private _1868.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1868.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList(Arrays.asList(6, 6)), + solution1.findRLEArray(new int[][] {{1, 3}, {2, 3}}, new int[][] {{6, 3}, {3, 3}})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1869Test.java b/src/test/java/com/fishercoder/secondthousand/_1869Test.java new file mode 100644 index 0000000000..52fe193a5b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1869Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1869; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1869Test { + private _1869.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1869.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.checkZeroOnes("1101")); + } + + @Test + public void test2() { + assertEquals(false, solution1.checkZeroOnes("111000")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1886Test.java b/src/test/java/com/fishercoder/secondthousand/_1886Test.java new file mode 100644 index 0000000000..49a8e2b1f9 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1886Test.java @@ -0,0 +1,63 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1886; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1886Test { + private _1886.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1886.Solution1(); + } + + @Test + public void test1() { + assertEquals( + true, + solution1.findRotation( + new int[][] { + {0, 1}, + {1, 0} + }, + new int[][] { + {1, 0}, + {0, 1} + })); + } + + @Test + public void test2() { + assertEquals( + false, + solution1.findRotation( + new int[][] { + {0, 1}, + {1, 1} + }, + new int[][] { + {1, 0}, + {0, 1} + })); + } + + @Test + public void test3() { + assertEquals( + true, + solution1.findRotation( + new int[][] { + {0, 0, 0}, + {0, 1, 0}, + {1, 1, 1} + }, + new int[][] { + {1, 1, 1}, + {0, 1, 0}, + {0, 0, 0} + })); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1891Test.java b/src/test/java/com/fishercoder/secondthousand/_1891Test.java new file mode 100644 index 0000000000..9801415def --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1891Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1891; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1891Test { + private _1891.Solution1 solution1; + private static int[] ribbons; + private static int k; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _1891.Solution1(); + } + + @Test + public void test1() { + ribbons = new int[] {9, 7, 5}; + k = 3; + expected = 5; + assertEquals(expected, solution1.maxLength(ribbons, k)); + } + + @Test + public void test2() { + ribbons = new int[] {7, 5, 9}; + k = 4; + expected = 4; + assertEquals(expected, solution1.maxLength(ribbons, k)); + } + + @Test + public void test3() { + ribbons = new int[] {5, 7, 9}; + k = 22; + expected = 0; + assertEquals(expected, solution1.maxLength(ribbons, k)); + } + + @Test + public void test4() { + ribbons = + new int[] { + 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, + 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, + 1, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, + 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, + 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000 + }; + k = 49; + expected = 100000; + assertEquals(expected, solution1.maxLength(ribbons, k)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1893Test.java b/src/test/java/com/fishercoder/secondthousand/_1893Test.java new file mode 100644 index 0000000000..ee0059582c --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1893Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1893; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1893Test { + private _1893.Solution1 solution1; + private static int[][] ranges; + + @BeforeEach + public void setup() { + solution1 = new _1893.Solution1(); + } + + @Test + public void test1() { + ranges = + new int[][] { + {1, 10}, + {10, 20} + }; + assertEquals(false, solution1.isCovered(ranges, 21, 21)); + } + + @Test + public void test2() { + ranges = new int[][] {{50, 50}}; + assertEquals(false, solution1.isCovered(ranges, 1, 50)); + } + + @Test + public void test3() { + ranges = + new int[][] { + {1, 10}, + {10, 20} + }; + assertEquals(false, solution1.isCovered(ranges, 21, 25)); + } + + @Test + public void test4() { + ranges = new int[][] {{1, 50}}; + assertEquals(true, solution1.isCovered(ranges, 1, 50)); + } + + @Test + public void test5() { + ranges = new int[][] {{1, 2}, {3, 4}, {5, 6}}; + assertEquals(true, solution1.isCovered(ranges, 2, 5)); + } + + @Test + public void test6() { + ranges = new int[][] {{50, 50}}; + assertEquals(false, solution1.isCovered(ranges, 49, 49)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1894Test.java b/src/test/java/com/fishercoder/secondthousand/_1894Test.java new file mode 100644 index 0000000000..6f7f108c04 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1894Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1894; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1894Test { + private _1894.Solution1 solution1; + private static int[] chalk; + + @BeforeEach + public void setup() { + solution1 = new _1894.Solution1(); + } + + @Test + public void test1() { + chalk = new int[] {3, 4, 1, 2}; + assertEquals(1, solution1.chalkReplacer(chalk, 25)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1903Test.java b/src/test/java/com/fishercoder/secondthousand/_1903Test.java new file mode 100644 index 0000000000..6787885eda --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1903Test.java @@ -0,0 +1,34 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1903; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1903Test { + private _1903.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1903.Solution1(); + } + + @Test + public void test1() { + assertEquals("5", solution1.largestOddNumber("52")); + } + + @Test + public void test2() { + assertEquals("35427", solution1.largestOddNumber("35427")); + } + + @Test + public void test3() { + assertEquals( + "", + solution1.largestOddNumber( + "682240884028086868404428842828664608862668422848464222044808062248028626808622648468064684002662206226066026484442422688042080684686220644488204022062860680482080686642464640464808662064884824260028648606444680200422064006864880486640800648828268684608460686600424266848424006442022080400840864222486042862488644828486026242662426040828662088280044400400424020064640620248088446626800004044064806462400400808484282028844606842644222262268080240226642826480202242048684644022206008424648828488048060828220426484664062040088682460884424244242680060828686004084220646404684460668220446600022404464840226860682284864260460062842664002264620886406204262808446844460642604220688262062206042420262486060840840200204428206042808202226686442802082260602448060888208640660040042842868040226042806882022200082868802846462486282022688286468886460484864682266006842422680488062820228682402042462442020680806828680682860066248862048260424084866468262220822282466082622820044066420086022044644488868064226204280402044466444662008040804488442842204888822242848628666060006844088448804684608462068840266664428842804224226488266880408840028880666482806262486642804886804020802602826800026262682488440426248848408228828402648406066826028286866602226282202064046026606802880680206048008826400288046824246002402848264680620268826046040682460022406248462448266202008446048806644840666482206486460260068046426846804462440420428626624282804686840266280868428264024428400242244466882240660662866626886866086022082226662266622222806468088084466446686080068200882082440048686088260622260026066842422840646000440660628886482088266406406646628464060442240480080428040044408066824082600648688222682062806686862800642842822862806288886042668262448668042444244488284228664648240824048482826604426828048406062684464280468848428424866268608022804082240424842868240080248264628846842082446428266822680204242042482866848480006022286888204466242824064642008284404664042466288424202066280466668280082844028000248022448060886222420226688228806602680082482282244644446824824400226688686600200444400486880626660448806482682002004606884280846866484648620864406460224484266020486608004402240220402266646466288600022066206602280442406606288884424464682482266882484222262488000080828660462222062288424002444400442062002280066266444044622026888464280288802028020226400066288268208044406860208484000668208806268080842484620886468624240248882444848428820662224028684626264884224668866602606268644024686428028204200200204400068224664086428488064068480688424848684822246004626620806848244280066602806402208624868202848060288888804204246408228042460808868648262462226000260204424000008004260886062828822288264602206062086064662826866466266262244624008804880084688604442402482066668888868426484662244862268660860460800802424286642020884082064200080886868628088468000024204846686286064688086422240280282022828048662200600862048480486808462828640604868028064828200248060264604848282884682400682822604806266606246266468064644802824020008226026680804626420264242842468400648604842480480882426648006622660200206422004680060644440024062840640226820068422482822080224006202086844260208444646288042206820668426422060240826022864402044200486486866428022604440824408244028084860842626264224842862402888244608646642868284224660042428224068864446860648084428806068642206080066864864648884820464846040882642680248442406028440068448888242602400428248622002640626046680860200480286046822002426262260846606826020824284026200628040804862660260020004808882626646804284028842660664246282420408046088202000026460440884660620444646866084642866240284408084280242820040008468284088664204826204402868000682086406482206244620228484402088848460606202046002884664884466200420680068602862424600022466046280228802826288808662220080440820684826204846246266042446220626202468660468460426604228202200888882860464424288008462882000622860064886888226220242428644026864606406880024688408844288206400846048800828424828602442804826642484644642620228686020404482008064620202822008068284024808208800080224200280462600208402240420246484440242604000264086004402664868442660082484442868048802404680828026846066000068840820804288682606682420642202426024868662462462220682408642228408284224022264640806220826068688000862824288806286662822240026822064200608800640644808864064826204482084260208028846848800066806284622802668400688862648460266262862448662088200206848002602422484048804608264064822686024682608228400068668880848406804040822662866244200460208406444224842466084404262602468800046484208246600688024422080264040684664668020024824228826860842444862802622666826804402206846228262680406204402046462842466220602808062640022264486400482088086800426246400022622000828880828820044084680864846260222068280806206200260246242844266420400268022066840086222840688004202228808826424826244862886604640000802466204240828242646060286866266040246248680882462462600824022808626226428466622622424288660646422446282406826428888844048206226066800428446886220628220880244888604208226466604488442284246884604404860440244204422466462624626484862460800846880044888824088606644228066482448886648844840668044648448824848006042422666028800680084824068464224662842204204022608624466222088604882406022284864224686482048060664640826024200086024622640628626046460666020004000266602866282824864480284844046242066862840006242846400602828620848248288280228060822228408460068408046640400804068202682228668628262068680888822800422288240246264662660840608626006064024260086802240860628246448002804662608860060622288608842088004800086008026660808282002082880024640220466062200828628020020620884086208264488468244068848608820828208826202488400226066022286804886046420224046640044068820660222026408244862622660044800426602080666208086482606628680260842824822066846622066088846602282628808268842648244424240606628622840644084802266224406826882644842686022020220022622608024206806228882244000842866628242066600226842004806046682226604260040402480628244046806042288602206242286080222224846006020086286488826824288808460808844206824642808204664046266880442208668848080220608024064446422880602620024006242666024820844444840408006442860882682208800826280228260226802222420480008804482208606426624846204026022846286448026660684808488062864440880420222828006006204444028806046420286424488822606242226222028086662444806822802404486246464086068626646424682062400424684468600426488860226088040440662622420266604820064662846662802406426064608228442826848682064826680004882022406884640840662424442840604286200468202422082062880222686822822002080026442046660646284282842640088680642064246262802006202088662280624600262426664264828060228642246260040844068442400868284840288064000484882886608462488248846662624280480266640682424828248202602484442026420282800884282202202080064268862884680488684842062608202480282200684668460608222628826864028620660664604804848442002844244006206488080860660446400628462086846080440822682842624224648882062080080246800268044426026660408082200424646480206280288604880824888444648888462880244440868808664446668420480808484444046444086060822020068042668602268440266224880262080888240668642862644884486606088064624220204408022064048068426040888642264888642224800682624460408062828228266462028884440684802864806602042002820668622802664842088240402688868044464262806646426684404602446480868206024264248044228648086600680842440682868662808844266626628640822224826802224008822442646284088664648464848200688488602800084482226440804240200662406282082640462286060226822484628640088404244424426228082640400486244280642288264622082882004040204068488204840284226680886264048282644082488260846606262864400206006064488884068422080240048608802262442660064086280088288244628882224264286002224648268426064060284400440664642404646082228006226622280222462608000060680606842686028886668666404664284264402624224284460062602844822482266808200262828404626602804206628066806020440880686686602246282882260060284824828080622088806048846606024046866084642206446286086842442042684024800600862840626280422428004606460868606660428022466648068848884444868242040024688648486622680040004084062408686868688424446048228208266606826828402682468062620820208660442622608400226248244260624226422280286404620282620684844628022482644240046246268426068404260882280662484666466008866826404048824082860046666844426622044428646406244602882288888048428246086040668404060006862684408440006622206644868646480486684826046224460648824246880662484820420866824064268682408862686280480464462644264020000000606202002466660022204286028662226620424622864042862806048068660284402840628882404240486462486424264842424608846060066226608040484628402848442000882626600488444060800684828222860484446064846008688000808042260608686282064668226660068606666604608608200688264864448608022682466488408840262620268682006624446268246844022266026026600242220806286000228848026260008008622608084884086666648402488488262226668226886208460006408246268666446488860224624826842226886064402246066266806462204604888026422028200026420684824222880084002420642464808684246268862880428408646864088024622228866682888068202244060804820648268084068008840462208682800200240848226808484668426206882406422080482624002246464264204280404480460828882048864484020286882024420020406800460640208480228204266800846842406820084646422244444080244442262608228488248884000660046626468262086624840848264668600640866622246286804042080668860082806068600648444068228028042040800648282602802264806204626464242466044004024284228408484626806440488646206888404662022866406066008206260204820022688808826646806022088042048464608228442282286440408284668408408426064280822642408240020820040888462488444442080448880884244484800626484806860840004406404224624802000264288444826422622602640888268484062486684604008000228486820648266680022460624648608822240660824662464880608622608224280886680648606426860242202820006048006684426620864004000644806868228404060220424020288662026400644062022444008626206262064408486286262026028662428208604402640246622464848222068640282042600086664268488248408000688000482068200628884664482064484848220286042404860868622248804082640082044204602280046446688644640202626662864888422068846808404866264480202008048068646482220084406864464666280444862882466264668846408824620646008200822622206440248266668840606086286640682882666024084026408622886868604820642220808006680864860282222486268082460804288602024824222046820002200280868082648022640220824680002622026642624868484602864880660662444200062888204600808828024220420600842220200486244462486400406042462824044082462004406488666468468806240448208084882420006028826084622644484082420480864406240040226640440220008000820420008086468446824468860266662600668842668260688462206682664442088622648684668680240822400802002600468828402640840862046648088824066086466826802684064442824488042264046280226468648662284246482882686200022424424844826846846822860200680248806648808460600844440642486680828624062662266880066088820064004228260862266886080008062600424828240802820486240002204462268060604482222602808408060004844808848602620408284846064082264420440266026860884840240206226882828620446044626026602428042886608464602640402608260220066400280662020226642402426628040202246064686606024686604462288404028264646008848680022406686422480246686604268620604642686824442600402800044062404440224228642882064060406404062288442864828204206002042006428028048486028444604488240664004460664862422482288824000046266044688622066604040626822244288204006662020426284626468468624020802404062422608482280804660286802204884066464602206282288202248420800868640648084886248824406628802840424622464226648044204086864660000842808464840206042608024684248620660664048204680426064480028800684466848604668046802220420684822206664820246026480064862868428806682480866842822802224620864486806280606228008822248226882028828822044082446448688682828266024880484422422288266002680426040462406844620468246064622002644208046808048828208226062442222248268060042200446202284088280004866880624280866242202660228406066664846604822046222628026626806262006804444444226646820886024064642444082200226088200604244660660448648244602608204820224608464824808864646482660228088840460824240004888828862480646666840828002660082440822404402080442420060040268086280080620226206244262880284800028664062040484220440844688020000820868880262602606222482662828886408020842444684688062680262408864622466242042666486000466208482420448086282442648640064626822684208220260468682602262842422204662844462826086686682624888488600664462206046640880404828804620884068464800862680666882606226044828086224044460606648026808844886060444820244864882428486024448220462286800624882802408222224806600646442882060004864682846866440608002440842224486264446606080606084608446648084820022848064400822400246406248880408024008024402606626866206822482460060622860808286822268662662060260682860466226428820840864880480886068080488244244466642008208042802088660808060802688642842226086482602266684084808886480804866804648862640600682808648822046488628202486086206880468842026462868808026080680066484000640666824268024800004042028828264606202044048864686004880226666260468644204482646806466404804408226820426064406220246842884466060480462488444480246046088262808828020880244800280888022442864448466604248468246086402406842440820200842284024604266266860888044082846206684268822288868226860266488024284806648448008428848604602404284080448828608206808626620202060684204604688624268080048864484008846462286822224688006006264462820220068426862604648682686248648226206266688282880288462200646840226686880680646848600246628626642466482862082482480606026680822824466482282066828462484042886444006006288448402024826260288226422626020468066466622444846620848200220422244264020802244022224466822682608628288842486620002208044268400086846862646068042644640040424422624082626802680666202260842822640608604428660246006286244620448204086006662808404826002806204680428424664028244428244462664644006208440202044842040066842220200064222002220424404806068222828824248060820264840064288006260060222220688840062260448266406024008686446246422028424408642440688006082006048082286042008006844628406064808888642046622842022800668846482666462048620046606626222482620048682082600444004428420824864668624062080486820086862640840864262602026044808222688484066020468008400866648660086040044680464266042822642682266400804066246220080080844866648286660486286684464840240620606842462208800264008088044884862442862046600828628000266464682022686642006600824464642220802460222264660040486040282240868664244804640640460426482224442800684666604046088668826488406242022660802286688826420226206026226462224228282882484286602288048802422842886688844802242264606620226220684082604848020202888864488666800620264600886662464484868888488602868064446608648464404660086642628824046864800626862868000468648244004226666880662242866228682000402280844664224044000284068680466048284606880402268004244408444448842488648244882822826466486402248404046488448640202800400428426028466464082464840408022228084640226086460822662286468628602840444422262860268648022060442864604488824402642608640442622686020")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1904Test.java b/src/test/java/com/fishercoder/secondthousand/_1904Test.java new file mode 100644 index 0000000000..073a6fdfba --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1904Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1904; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1904Test { + private _1904.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1904.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.numberOfRounds("12:01", "12:44")); + } + + @Test + public void test2() { + assertEquals(95, solution1.numberOfRounds("00:01", "00:00")); + } + + @Test + public void test3() { + assertEquals(40, solution1.numberOfRounds("20:00", "06:00")); + } + + @Test + public void test4() { + assertEquals(55, solution1.numberOfRounds("04:54", "18:51")); + } + + @Test + public void test5() { + assertEquals(0, solution1.numberOfRounds("23:46", "00:01")); + } + + @Test + public void test6() { + assertEquals(39, solution1.numberOfRounds("18:51", "04:54")); + } + + @Test + public void test9() { + assertEquals(39, solution1.numberOfRounds("18:51", "04:50")); + } + + @Test + public void test7() { + assertEquals(2, solution1.numberOfRounds("00:01", "00:57")); + } + + @Test + public void test8() { + assertEquals(6, solution1.numberOfRounds("00:01", "01:57")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1933Test.java b/src/test/java/com/fishercoder/secondthousand/_1933Test.java new file mode 100644 index 0000000000..261261ca8b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1933Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1933; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1933Test { + private _1933.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1933.Solution1(); + } + + @Test + public void test1() { + assertEquals(false, solution1.isDecomposable("000111000")); + } + + @Test + public void test2() { + assertEquals(true, solution1.isDecomposable("00011111222")); + } + + @Test + public void test3() { + assertEquals(false, solution1.isDecomposable("011100022233")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1936Test.java b/src/test/java/com/fishercoder/secondthousand/_1936Test.java new file mode 100644 index 0000000000..089ff0f698 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1936Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1936; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1936Test { + private _1936.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1936.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.addRungs(new int[] {3}, 1)); + } + + @Test + public void test2() { + assertEquals(2, solution1.addRungs(new int[] {1, 3, 5, 10}, 2)); + } + + @Test + public void test3() { + assertEquals(0, solution1.addRungs(new int[] {3, 6, 8, 10}, 3)); + } + + @Test + public void test4() { + assertEquals(1, solution1.addRungs(new int[] {3, 4, 6, 7}, 2)); + } + + @Test + public void test5() { + assertEquals(0, solution1.addRungs(new int[] {5}, 10)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1945Test.java b/src/test/java/com/fishercoder/secondthousand/_1945Test.java new file mode 100644 index 0000000000..12f7b3be5a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1945Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1945; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1945Test { + private _1945.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1945.Solution1(); + } + + @Test + public void test1() { + assertEquals(8, solution1.getLucky("zbax", 2)); + } + + @Test + public void test2() { + assertEquals(36, solution1.getLucky("iiii", 1)); + } + + @Test + public void test3() { + assertEquals(6, solution1.getLucky("leetcode", 2)); + } + + @Test + public void test4() { + assertEquals(8, solution1.getLucky("zbax", 2)); + } + + @Test + public void test5() { + assertEquals(8, solution1.getLucky("fleyctuuajsr", 5)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1966Test.java b/src/test/java/com/fishercoder/secondthousand/_1966Test.java new file mode 100644 index 0000000000..f72734be2f --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1966Test.java @@ -0,0 +1,60 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1966; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1966Test { + private _1966.Solution1 solution1; + private _1966.Solution2 solution2; + private _1966.Solution3 solution3; + private static int[] nums; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _1966.Solution1(); + solution2 = new _1966.Solution2(); + solution3 = new _1966.Solution3(); + } + + @Test + public void test1() { + nums = new int[] {7}; + expected = 1; + assertEquals(expected, solution1.binarySearchableNumbers(nums)); + assertEquals(expected, solution2.binarySearchableNumbers(nums)); + assertEquals(expected, solution3.binarySearchableNumbers(nums)); + } + + @Test + public void test2() { + nums = new int[] {-1, 5, 2}; + expected = 1; + assertEquals(expected, solution1.binarySearchableNumbers(nums)); + assertEquals(expected, solution2.binarySearchableNumbers(nums)); + assertEquals(expected, solution3.binarySearchableNumbers(nums)); + } + + @Test + public void test3() { + /** This is to answer the follow-up question, what if duplicates are allowed in the input */ + nums = new int[] {-1, -1, 5, 2}; + expected = 2; + assertEquals(expected, solution1.binarySearchableNumbers(nums)); + assertEquals(expected, solution2.binarySearchableNumbers(nums)); + assertEquals(expected, solution3.binarySearchableNumbers(nums)); + } + + @Test + public void test4() { + /** This is to answer the follow-up question, what if duplicates are allowed in the input */ + nums = new int[] {-1, -1, 5, 2, 2, 5}; + expected = 3; + assertEquals(expected, solution1.binarySearchableNumbers(nums)); + assertEquals(expected, solution2.binarySearchableNumbers(nums)); + assertEquals(expected, solution3.binarySearchableNumbers(nums)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1968Test.java b/src/test/java/com/fishercoder/secondthousand/_1968Test.java new file mode 100644 index 0000000000..019601761e --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1968Test.java @@ -0,0 +1,20 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1968; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1968Test { + private _1968.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1968.Solution1(); + } + + @Test + public void test1() { + CommonUtils.printArray(solution1.rearrangeArray(new int[] {1, 2, 3, 4, 5})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1971Test.java b/src/test/java/com/fishercoder/secondthousand/_1971Test.java new file mode 100644 index 0000000000..ba7e6ee670 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1971Test.java @@ -0,0 +1,62 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1971; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1971Test { + private _1971.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1971.Solution1(); + } + + @Test + public void test1() { + int[][] edges = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1],[1,2],[2,0]"); + assertEquals(true, solution1.validPath(3, edges, 0, 2)); + } + + @Test + public void test2() { + int[][] edges = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1],[0,2],[3,5],[5,4],[4,3]"); + assertEquals(false, solution1.validPath(6, edges, 0, 5)); + } + + @Test + public void test3() { + int[][] edges = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[4,3],[1,4],[4,8],[1,7],[6,4],[4,2],[7,4],[4,0],[0,9],[5,4]"); + assertEquals(true, solution1.validPath(10, edges, 5, 9)); + } + + @Test + public void test4() { + int[][] edges = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,7],[0,8],[6,1],[2,0],[0,4],[5,8],[4,7],[1,3],[3,5],[6,5]"); + assertEquals(true, solution1.validPath(10, edges, 7, 5)); + } + + @Test + public void test5() { + assertEquals(true, solution1.validPath(1, new int[][] {}, 0, 0)); + } + + @Test + public void test6() { + int[][] edges = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[3,12],[26,84],[10,43],[68,47],[33,10],[87,35],[41,96],[70,92],[38,31],[88,59],[7,30],[89,26],[95,25],[66,28],[14,24],[86,11],[83,65],[14,4],[67,7],[89,45],[52,73],[47,85],[86,53],[68,81],[43,68],[87,78],[94,49],[70,21],[11,82],[60,93],[22,32],[69,99],[7,1],[41,46],[73,94],[98,52],[68,0],[69,89],[37,72],[25,50],[72,78],[96,60],[73,95],[7,69],[97,19],[46,75],[8,38],[19,36],[64,41],[61,78],[97,14],[54,28],[6,18],[25,32],[34,77],[58,60],[17,63],[98,87],[13,76],[58,53],[81,74],[29,6],[37,5],[65,63],[89,56],[61,18],[23,34],[76,29],[73,76],[11,63],[98,0],[54,14],[63,7],[87,32],[79,57],[72,0],[94,16],[85,16],[12,91],[14,17],[30,45],[42,41],[82,69],[24,28],[31,59],[11,88],[41,89],[48,12],[92,76],[84,64],[19,64],[21,32],[30,19],[47,43],[45,27],[31,17],[53,36],[88,3],[83,7],[27,48],[13,6],[14,40],[90,28],[80,85],[29,79],[10,50],[56,86],[82,88],[11,99],[37,55],[62,2],[55,92],[51,53],[9,40],[65,97],[25,57],[7,96],[86,1],[39,93],[45,86],[40,90],[58,75],[99,86],[82,45],[5,81],[89,91],[15,83],[93,38],[3,93],[71,28],[11,97],[74,47],[64,96],[88,96],[4,99],[88,26],[0,55],[36,75],[26,24],[84,88],[58,40],[77,72],[58,48],[50,92],[62,68],[70,49],[41,71],[68,6],[64,91],[50,81],[35,44],[91,48],[21,37],[62,98],[64,26],[63,51],[77,55],[25,13],[60,41],[87,79],[75,17],[61,95],[30,82],[47,79],[28,7],[92,95],[91,59],[94,85],[24,65],[91,31],[3,9],[59,58],[70,43],[95,13],[30,96],[51,9],[16,70],[29,94],[37,22],[35,79],[14,90],[75,9],[2,57],[81,80],[61,87],[69,88],[98,79],[18,70],[82,19],[36,27],[49,62],[67,75],[62,77],[83,96],[92,37],[95,22],[46,97],[35,0],[44,79],[82,89],[68,94],[96,31],[92,34],[25,0],[46,36],[38,84],[21,0],[0,80],[72,44],[56,97],[86,26],[94,57],[25,6],[81,13],[66,63],[57,5],[72,49],[46,86],[95,16],[95,37],[14,89],[44,22],[60,39],[37,47],[58,86],[89,96],[38,83],[51,91],[72,70],[14,82],[60,30],[58,39],[57,22],[95,70],[44,76],[5,68],[15,69],[33,61],[81,32],[21,68],[73,20],[22,72],[83,8],[15,54],[93,42],[68,95],[55,72],[33,92],[5,49],[17,96],[44,77],[24,53],[2,98],[33,81],[32,43],[20,16],[67,84],[98,35],[58,11],[72,5],[3,59],[78,79],[6,0],[26,71],[96,97],[18,92],[1,36],[78,0],[63,15],[20,43],[32,73],[37,76],[73,16],[76,23],[50,44],[68,2],[14,86],[69,65],[95,98],[53,64],[6,76],[7,11],[14,84],[62,50],[83,58],[78,92],[37,0],[13,55],[12,86],[11,59],[41,86],[27,26],[94,43],[20,78],[0,73],[58,90],[69,36],[62,34],[65,26],[32,85]"); + assertEquals(false, solution1.validPath(100, edges, 20, 53)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1973Test.java b/src/test/java/com/fishercoder/secondthousand/_1973Test.java new file mode 100644 index 0000000000..075bcd5b58 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1973Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.secondthousand._1973; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1973Test { + private _1973.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1973.Solution1(); + } + + @Test + public void test1() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(10, 3, 4, 2, 1)); + TreeUtils.printBinaryTree(root); + assertEquals(2, solution1.equalToDescendants(root)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1974Test.java b/src/test/java/com/fishercoder/secondthousand/_1974Test.java new file mode 100644 index 0000000000..a02724bd2a --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1974Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1974; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1974Test { + private _1974.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1974.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.minTimeToType("abc")); + } + + @Test + public void test2() { + assertEquals(7, solution1.minTimeToType("bza")); + } + + @Test + public void test3() { + assertEquals(34, solution1.minTimeToType("zjpc")); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1981Test.java b/src/test/java/com/fishercoder/secondthousand/_1981Test.java new file mode 100644 index 0000000000..f1dbcdf763 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1981Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1981; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1981Test { + private _1981.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1981.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 0, + solution1.minimizeTheDifference( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2,3],[4,5,6],[7,8,9]"), + 13)); + } + + @Test + public void test2() { + assertEquals( + 94, + solution1.minimizeTheDifference( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1],[2],[3]"), + 100)); + } + + @Test + public void test3() { + assertEquals( + 1, + solution1.minimizeTheDifference( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2,9,8,7]"), + 6)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1984Test.java b/src/test/java/com/fishercoder/secondthousand/_1984Test.java new file mode 100644 index 0000000000..7a8631275b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1984Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1984; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1984Test { + private _1984.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1984.Solution1(); + } + + @Test + public void test1() { + assertEquals(0, solution1.minimumDifference(new int[] {90}, 1)); + } + + @Test + public void test2() { + assertEquals(2, solution1.minimumDifference(new int[] {9, 4, 1, 7}, 2)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1985Test.java b/src/test/java/com/fishercoder/secondthousand/_1985Test.java new file mode 100644 index 0000000000..6df02ed866 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1985Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.secondthousand; + +import com.fishercoder.solutions.secondthousand._1985; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1985Test { + private _1985.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1985.Solution1(); + } + + @org.junit.jupiter.api.Test + public void test1() { + Assertions.assertEquals( + "3", solution1.kthLargestNumber(new String[] {"3", "6", "7", "10"}, 4)); + } + + @Test + public void test2() { + Assertions.assertEquals( + "2", solution1.kthLargestNumber(new String[] {"2", "21", "12", "1"}, 3)); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1991Test.java b/src/test/java/com/fishercoder/secondthousand/_1991Test.java new file mode 100644 index 0000000000..43ffcb4802 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1991Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.secondthousand._1991; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1991Test { + private _1991.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1991.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.findMiddleIndex(new int[] {2, 3, -1, 8, 4})); + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1992Test.java b/src/test/java/com/fishercoder/secondthousand/_1992Test.java new file mode 100644 index 0000000000..5e553cd18b --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1992Test.java @@ -0,0 +1,57 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.secondthousand._1992; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1992Test { + private _1992.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _1992.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[][] {{13, 1, 28, 1}, {22, 4, 24, 39}}, + solution1.findFarmland( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "" + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 0 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 1 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 2 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 3 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 4 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 5 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 6 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 7 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 8 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 9 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 10 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 11 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 12 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 13 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 14 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 15 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 16 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 17 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 18 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 19 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 20 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 21 + + "[0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," // 22 + + "[0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," // 23 + + "[0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]," // 24 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 25 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 26 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 27 + + "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]," // 28 + + "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]"))); // 29 + // 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9 + } +} diff --git a/src/test/java/com/fishercoder/secondthousand/_1993Test.java b/src/test/java/com/fishercoder/secondthousand/_1993Test.java new file mode 100644 index 0000000000..ebd408bd14 --- /dev/null +++ b/src/test/java/com/fishercoder/secondthousand/_1993Test.java @@ -0,0 +1,61 @@ +package com.fishercoder.secondthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.secondthousand._1993; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _1993Test { + private _1993.Solution1.LockingTree lockingTree; + + @BeforeEach + public void setup() {} + + @Test + public void test1() { + lockingTree = new _1993.Solution1.LockingTree(new int[] {-1, 0, 0, 1, 1, 2, 2}); + assertTrue(lockingTree.lock(2, 2)); + assertFalse(lockingTree.unlock(2, 3)); + assertTrue(lockingTree.unlock(2, 2)); + assertTrue(lockingTree.lock(4, 5)); + assertTrue(lockingTree.upgrade(0, 1)); + assertFalse(lockingTree.lock(0, 1)); + } + + @Test + public void test2() { + lockingTree = new _1993.Solution1.LockingTree(new int[] {-1, 0, 3, 1, 0}); + assertFalse(lockingTree.upgrade(4, 5)); + assertFalse(lockingTree.upgrade(3, 8)); + assertFalse(lockingTree.unlock(0, 7)); + assertTrue(lockingTree.lock(2, 7)); + assertFalse(lockingTree.upgrade(4, 6)); + } + + @Test + public void test3() { + lockingTree = new _1993.Solution1.LockingTree(new int[] {-1, 4, 9, 0, 6, 1, 0, 6, 3, 1}); + assertFalse(lockingTree.upgrade(9, 43)); + assertFalse(lockingTree.upgrade(4, 27)); + assertFalse(lockingTree.upgrade(5, 34)); + assertFalse(lockingTree.upgrade(7, 31)); + assertFalse(lockingTree.upgrade(4, 27)); + assertTrue(lockingTree.lock(2, 47)); + assertFalse(lockingTree.unlock(7, 21)); + assertTrue(lockingTree.upgrade(4, 12)); + assertFalse(lockingTree.upgrade(1, 1)); + assertFalse(lockingTree.upgrade(8, 20)); + assertTrue(lockingTree.lock(5, 50)); + assertFalse(lockingTree.upgrade(8, 28)); + assertTrue(lockingTree.upgrade(0, 11)); + assertFalse(lockingTree.upgrade(6, 19)); + assertTrue(lockingTree.lock(9, 27)); + assertFalse(lockingTree.unlock(5, 6)); + assertFalse(lockingTree.upgrade(0, 5)); + assertFalse(lockingTree.unlock(4, 49)); + assertFalse(lockingTree.unlock(4, 42)); + assertFalse(lockingTree.upgrade(5, 27)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2001Test.java b/src/test/java/com/fishercoder/thirdthousand/_2001Test.java new file mode 100644 index 0000000000..f1dd229998 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2001Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2001; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2001Test { + private _2001.Solution1 solution1; + private _2001.Solution2 solution2; + + @BeforeEach + public void setup() { + solution1 = new _2001.Solution1(); + solution2 = new _2001.Solution2(); + } + + @Test + public void test1() { + assertEquals( + 6, + solution1.interchangeableRectangles( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[4,8],[3,6],[10,20],[15,30]"))); + assertEquals( + 6, + solution2.interchangeableRectangles( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[4,8],[3,6],[10,20],[15,30]"))); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2007Test.java b/src/test/java/com/fishercoder/thirdthousand/_2007Test.java new file mode 100644 index 0000000000..81187db592 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2007Test.java @@ -0,0 +1,20 @@ +package com.fishercoder.thirdthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2007; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2007Test { + private _2007.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2007.Solution1(); + } + + @Test + public void test1() { + CommonUtils.printArray(solution1.findOriginalArray(new int[] {1, 3, 4, 2, 6, 8})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2012Test.java b/src/test/java/com/fishercoder/thirdthousand/_2012Test.java new file mode 100644 index 0000000000..b4ebcfab1d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2012Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2012; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2012Test { + private _2012.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2012.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.sumOfBeauties(new int[] {2, 4, 6, 4})); + } + + @Test + public void test2() { + assertEquals(14, solution1.sumOfBeauties(new int[] {1, 2, 3, 4, 5, 7, 8, 9, 10})); + } + + @Test + public void test3() { + assertEquals(0, solution1.sumOfBeauties(new int[] {9, 9, 3, 8, 7, 9, 6, 10})); + } + + @Test + public void test4() { + assertEquals(0, solution1.sumOfBeauties(new int[] {8, 4, 6, 3, 10, 5, 8, 5, 5, 9})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2017Test.java b/src/test/java/com/fishercoder/thirdthousand/_2017Test.java new file mode 100644 index 0000000000..acbaafdf89 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2017Test.java @@ -0,0 +1,50 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2017; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2017Test { + private _2017.Solution1 solution1; + private static int[][] grid; + + @BeforeEach + public void setup() { + solution1 = new _2017.Solution1(); + } + + @Test + public void test1() { + grid = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[2,5,4],[1,5,1]"); + assertEquals(4, solution1.gridGame(grid)); + } + + @Test + public void test2() { + grid = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[3,3,1],[8,5,2]"); + assertEquals(4, solution1.gridGame(grid)); + } + + @Test + public void test3() { + grid = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,3,1,15],[1,3,3,1]"); + assertEquals(7, solution1.gridGame(grid)); + } + + @Test + public void test4() { + grid = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[20,3,20,17,2,12,15,17,4,15],[20,10,13,14,15,5,2,3,14,3]"); + assertEquals(63, solution1.gridGame(grid)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2018Test.java b/src/test/java/com/fishercoder/thirdthousand/_2018Test.java new file mode 100644 index 0000000000..18df30c855 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2018Test.java @@ -0,0 +1,118 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2018; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2018Test { + private _2018.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2018.Solution1(); + } + + @Test + public void test1() { + assertEquals( + true, + solution1.placeWordInCrossword( + new char[][] { + {'#', ' ', '#'}, + {' ', ' ', '#'}, + {'#', 'c', ' '} + }, + "abc")); + } + + @Test + public void test2() { + assertEquals( + false, + solution1.placeWordInCrossword( + new char[][] { + {' ', '#', 'a'}, + {' ', '#', 'c'}, + {' ', '#', 'a'} + }, + "ac")); + } + + @Test + public void test3() { + assertEquals( + true, + solution1.placeWordInCrossword( + new char[][] { + {'#', ' ', '#'}, + {' ', ' ', '#'}, + {'#', ' ', 'c'} + }, + "ca")); + } + + @Test + public void test4() { + assertEquals( + true, + solution1.placeWordInCrossword( + new char[][] { + {'#', ' ', '#'}, + {' ', ' ', '#'}, + {'#', ' ', 'c'} + }, + "cd")); + } + + @Test + public void test5() { + assertEquals( + true, + solution1.placeWordInCrossword( + new char[][] { + {'#', ' ', '#'}, + {' ', '#', '#'}, + {'#', ' ', 'c'} + }, + "ca")); + } + + @Test + public void test6() { + assertEquals( + true, + solution1.placeWordInCrossword( + new char[][] { + {'#', ' ', '#'}, + {'#', 'c', '#'}, + {'#', '#', 'c'} + }, + "ca")); + } + + @Test + public void test7() { + assertEquals( + false, + solution1.placeWordInCrossword( + new char[][] { + {' ', 'b', '#'}, + {' ', '#', '#'}, + {' ', ' ', 'c'} + }, + "ca")); + } + + @Test + public void test8() { + assertEquals( + true, + solution1.placeWordInCrossword( + CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray( + "[\"#\",\" \",\"#\"],[\" \",\" \",\"#\"],[\"#\",\"c\",\" \"]"), + "abc")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2024Test.java b/src/test/java/com/fishercoder/thirdthousand/_2024Test.java new file mode 100644 index 0000000000..00b5ba760d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2024Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2024; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2024Test { + private _2024.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2024.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.maxConsecutiveAnswers("TTFF", 2)); + } + + @Test + public void test2() { + assertEquals(3, solution1.maxConsecutiveAnswers("TFFT", 1)); + } + + @Test + public void test3() { + assertEquals(5, solution1.maxConsecutiveAnswers("TTFTTFTT", 1)); + } + + @Test + public void test4() { + assertEquals(1, solution1.maxConsecutiveAnswers("F", 1)); + } + + @Test + public void test5() { + assertEquals(8, solution1.maxConsecutiveAnswers("FFFTTFTTFT", 3)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2028Test.java b/src/test/java/com/fishercoder/thirdthousand/_2028Test.java new file mode 100644 index 0000000000..1730477b5b --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2028Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2028; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2028Test { + private _2028.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2028.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {}, + solution1.missingRolls( + new int[] { + 4, 2, 2, 5, 4, 5, 4, 5, 3, 3, 6, 1, 2, 4, 2, 1, 6, 5, 4, 2, 3, 4, 2, 3, + 3, 5, 4, 1, 4, 4, 5, 3, 6, 1, 5, 2, 3, 3, 6, 1, 6, 4, 1, 3 + }, + 2, + 53)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2038Test.java b/src/test/java/com/fishercoder/thirdthousand/_2038Test.java new file mode 100644 index 0000000000..19b2c87d80 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2038Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2038; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2038Test { + private _2038.Solution1 solution1; + private static String color; + + @BeforeEach + public void setup() { + solution1 = new _2038.Solution1(); + } + + @Test + public void test1() { + color = "AAABABB"; + assertEquals(true, solution1.winnerOfGame(color)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2039Test.java b/src/test/java/com/fishercoder/thirdthousand/_2039Test.java new file mode 100644 index 0000000000..3e70a9069f --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2039Test.java @@ -0,0 +1,58 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2039; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2039Test { + private _2039.Solution1 solution1; + private static int[][] edges; + private static int[] patience; + + @BeforeEach + public void setup() { + solution1 = new _2039.Solution1(); + } + + @Test + public void test1() { + edges = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1],[1,2]"); + patience = new int[] {0, 2, 1}; + assertEquals(8, solution1.networkBecomesIdle(edges, patience)); + } + + @Test + public void test2() { + edges = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1],[0,2],[1,2]"); + patience = new int[] {0, 10, 10}; + assertEquals(3, solution1.networkBecomesIdle(edges, patience)); + } + + @Test + public void test3() { + edges = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[3,8],[4,13],[0,7],[0,4],[1,8],[14,1],[7,2],[13,10],[9,11],[12,14],[0,6],[2,12],[11,5],[6,9],[10,3]"); + patience = new int[] {0, 3, 2, 1, 5, 1, 5, 5, 3, 1, 2, 2, 2, 2, 1}; + assertEquals(20, solution1.networkBecomesIdle(edges, patience)); + } + + @Test + public void test4() { + edges = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1],[1,2]"); + patience = new int[] {0, 2, 2}; + assertEquals(7, solution1.networkBecomesIdle(edges, patience)); + } + + @Test + public void test5() { + edges = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1],[1,2]"); + patience = new int[] {0, 2, 3}; + assertEquals(8, solution1.networkBecomesIdle(edges, patience)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2049Test.java b/src/test/java/com/fishercoder/thirdthousand/_2049Test.java new file mode 100644 index 0000000000..5b54505362 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2049Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2049; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2049Test { + private _2049.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2049.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.countHighestScoreNodes(new int[] {-1, 2, 0, 2, 0})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2050Test.java b/src/test/java/com/fishercoder/thirdthousand/_2050Test.java new file mode 100644 index 0000000000..c0366a439f --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2050Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2050; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2050Test { + private _2050.Solution1 solution1; + private static int[][] relation; + private static int[] time; + private static int n; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _2050.Solution1(); + } + + @Test + public void test1() { + n = 3; + relation = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3],[2,3]"); + time = new int[] {3, 2, 5}; + expected = 8; + assertEquals(expected, solution1.minimumTime(n, relation, time)); + } + + @Test + public void test2() { + n = 5; + relation = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,5],[2,5],[3,5],[3,4],[4,5]"); + time = new int[] {1, 2, 3, 4, 5}; + expected = 12; + assertEquals(expected, solution1.minimumTime(n, relation, time)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2054Test.java b/src/test/java/com/fishercoder/thirdthousand/_2054Test.java new file mode 100644 index 0000000000..637018e129 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2054Test.java @@ -0,0 +1,46 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2054; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2054Test { + private _2054.Solution1 solution1; + private static int[][] events; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _2054.Solution1(); + } + + @Test + public void test1() { + events = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,3,2],[4,5,2],[2,4,3]"); + expected = 4; + assertEquals(expected, solution1.maxTwoEvents(events)); + } + + @Test + public void test2() { + events = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,3,2],[4,5,2],[1,5,5]"); + expected = 5; + assertEquals(expected, solution1.maxTwoEvents(events)); + } + + @Test + public void test3() { + events = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,5,3],[1,5,1],[6,6,5]"); + expected = 8; + assertEquals(expected, solution1.maxTwoEvents(events)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2063Test.java b/src/test/java/com/fishercoder/thirdthousand/_2063Test.java new file mode 100644 index 0000000000..110b060c3a --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2063Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2063; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2063Test { + private _2063.Solution1 solution1; + private static String word; + private static long expected; + + @BeforeEach + public void setup() { + solution1 = new _2063.Solution1(); + } + + @Test + public void test1() { + word = "aba"; + expected = 6L; + assertEquals(expected, solution1.countVowels(word)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2070Test.java b/src/test/java/com/fishercoder/thirdthousand/_2070Test.java new file mode 100644 index 0000000000..1b6a8d3465 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2070Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2070; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2070Test { + private _2070.Solution1 solution1; + private static int[][] items; + private static int[] queries; + private static int[] expected; + + @BeforeEach + public void setup() { + solution1 = new _2070.Solution1(); + } + + @Test + public void test1() { + items = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2],[3,2],[2,4],[5,6],[3,5]"); + queries = new int[] {1, 2, 3, 4, 5, 6}; + expected = new int[] {2, 4, 5, 5, 6, 6}; + assertArrayEquals(expected, solution1.maximumBeauty(items, queries)); + } + + @Test + public void test2() { + items = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2],[1,2],[1,3],[1,4]"); + queries = new int[] {1}; + expected = new int[] {4}; + assertArrayEquals(expected, solution1.maximumBeauty(items, queries)); + } + + @Test + public void test3() { + items = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[10,1000]"); + queries = new int[] {5}; + expected = new int[] {0}; + assertArrayEquals(expected, solution1.maximumBeauty(items, queries)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2075Test.java b/src/test/java/com/fishercoder/thirdthousand/_2075Test.java new file mode 100644 index 0000000000..dbcf37769f --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2075Test.java @@ -0,0 +1,51 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2075; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2075Test { + private _2075.Solution1 solution1; + private static String encodedText; + private static int rows; + private static String expected; + + @BeforeEach + public void setup() { + solution1 = new _2075.Solution1(); + } + + @Test + public void test1() { + encodedText = "ch ie pr"; + rows = 3; + expected = "cipher"; + assertEquals(expected, solution1.decodeCiphertext(encodedText, rows)); + } + + @Test + public void test2() { + encodedText = "iveo eed l te olc"; + rows = 4; + expected = "i love leetcode"; + assertEquals(expected, solution1.decodeCiphertext(encodedText, rows)); + } + + @Test + public void test3() { + encodedText = "coding"; + rows = 1; + expected = "coding"; + assertEquals(expected, solution1.decodeCiphertext(encodedText, rows)); + } + + @Test + public void test4() { + encodedText = " b ac"; + rows = 2; + expected = " abc"; + assertEquals(expected, solution1.decodeCiphertext(encodedText, rows)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2076Test.java b/src/test/java/com/fishercoder/thirdthousand/_2076Test.java new file mode 100644 index 0000000000..b8f74e2972 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2076Test.java @@ -0,0 +1,106 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2076; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2076Test { + private _2076.Solution1 solution1; + private static int[][] restrictions; + private static int[][] requests; + private static int n; + private static boolean[] expected; + + @BeforeEach + public void setup() { + solution1 = new _2076.Solution1(); + } + + @Test + public void test1() { + restrictions = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1]"); + requests = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,2],[2,1]"); + expected = new boolean[] {true, false}; + n = 3; + assertArrayEquals(expected, solution1.friendRequests(n, restrictions, requests)); + } + + @Test + public void test2() { + restrictions = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1]"); + requests = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2],[0,2]"); + expected = new boolean[] {true, false}; + n = 3; + assertArrayEquals(expected, solution1.friendRequests(n, restrictions, requests)); + } + + @Test + public void test3() { + restrictions = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1],[1,2],[2,3]"); + requests = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,4],[1,2],[3,1],[3,4]"); + expected = new boolean[] {true, false, true, false}; + n = 5; + assertArrayEquals(expected, solution1.friendRequests(n, restrictions, requests)); + } + + @Test + public void test4() { + restrictions = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,6],[6,2]"); + requests = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,2],[2,3],[0,2],[6,4],[6,4]"); + expected = new boolean[] {true, true, true, true, true}; + n = 7; + assertArrayEquals(expected, solution1.friendRequests(n, restrictions, requests)); + } + + @Test + public void test5() { + restrictions = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[14,2],[1,8],[4,5],[16,6],[10,8],[10,3],[17,14],[13,2],[5,1],[0,4]," + + "[8,12],[6,5],[7,9],[12,16],[17,16],[15,11],[5,7],[9,16],[14,7],[7,8],[2,7],[3,5],[9,13],[10,13],[2,3],[2,17],[12,3],[9,10],[15,4],[11,13]," + + "[13,7],[7,1],[13,6],[10,11],[10,17],[11,2],[7,17],[0,10],[15,1],[9,3],[1,11],[11,0],[7,6],[8,0],[6,15],[0,13],[9,15],[5,11],[6,12],[17,15]," + + "[2,12],[15,0],[4,7],[16,5],[9,5],[4,3],[12,5],[1,2],[13,5],[10,7],[12,15],[11,17],[12,0],[9,14],[17,12],[4,6],[13,15],[4,10],[11,7]," + + "[8,5],[5,17],[8,3],[15,7],[13,12],[9,0],[17,3],[11,8],[8,16],[2,16],[4,12],[3,1],[8,14],[15,3],[14,11],[6,0],[12,7],[0,2],[0,7]," + + "[5,14],[8,2],[13,17],[17,8],[4,13],[1,0],[7,16],[5,2],[9,11],[12,9],[16,3],[5,15],[2,15],[3,6],[17,9],[4,16],[4,2]"); + requests = + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[12,0],[4,7],[9,0],[4,5],[4,6],[0,16],[2,15],[1,2],[12,15]," + + "[16,6],[13,3],[2,12],[12,15],[9,15],[2,16],[1,8],[12,5],[2,16],[14,13],[9,13],[3,1],[13,16],[8,13],[9,16],[5,2],[4,14]," + + "[9,10],[6,5],[5,7],[12,3],[8,2],[12,0],[0,17],[12,16],[9,15],[4,3],[11,7],[4,13],[4,6],[10,13],[14,12],[15,0],[9,6]," + + "[4,10],[7,8],[4,3],[10,17],[4,10],[1,2],[11,12],[6,5],[5,2],[9,10],[14,7],[17,15],[2,17],[11,0],[14,0],[14,11]," + + "[15,7],[13,6],[4,14],[0,4],[17,3],[11,17],[8,12],[6,11],[3,11],[17,15],[17,16],[4,5],[12,7],[0,17],[15,11],[0,4]," + + "[10,16],[15,7],[14,12],[1,6],[11,13],[10,13],[0,5],[1,0],[10,11],[2,17],[1,11],[13,2],[0,5],[12,7],[17,14],[12,9]," + + "[0,17],[15,10],[5,2],[16,6],[0,13],[17,6],[1,11],[13,17],[11,8],[0,16],[13,17],[6,11],[0,7],[13,12],[11,16],[8,13]," + + "[17,6],[8,13],[9,8],[9,0],[17,16],[4,13]"); + expected = + new boolean[] { + false, false, false, false, false, true, false, false, false, false, true, + false, false, false, false, false, false, false, true, false, false, false, + false, false, false, false, false, false, false, false, false, false, false, + false, false, false, false, false, false, false, false, false, true, false, + false, false, false, false, false, true, false, false, false, false, false, + false, false, false, false, false, false, false, false, false, false, false, + false, false, false, false, false, false, false, false, false, false, false, + false, true, false, false, false, false, false, false, false, false, false, + false, false, false, false, true, false, false, false, false, false, false, + false, true, false, false, false, false, false, false, false, false, false, + false, false, false + }; + n = 18; + assertArrayEquals(expected, solution1.friendRequests(n, restrictions, requests)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2080Test.java b/src/test/java/com/fishercoder/thirdthousand/_2080Test.java new file mode 100644 index 0000000000..91922268b0 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2080Test.java @@ -0,0 +1,60 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2080; +import org.junit.jupiter.api.Test; + +public class _2080Test { + private _2080.Solution1.RangeFreqQuery rangeFreqQuery1; + private _2080.Solution2.RangeFreqQuery rangeFreqQuery2; + + @Test + public void test1() { + rangeFreqQuery1 = + new _2080.Solution1.RangeFreqQuery( + new int[] {12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56}); + assertEquals(1, rangeFreqQuery1.query(1, 2, 4)); + assertEquals(2, rangeFreqQuery1.query(0, 11, 33)); + + rangeFreqQuery2 = + new _2080.Solution2.RangeFreqQuery( + new int[] {12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56}); + assertEquals(1, rangeFreqQuery2.query(1, 2, 4)); + assertEquals(2, rangeFreqQuery2.query(0, 11, 33)); + } + + @Test + public void test2() { + rangeFreqQuery1 = new _2080.Solution1.RangeFreqQuery(new int[] {1, 1, 1, 2, 2}); + assertEquals(0, rangeFreqQuery1.query(0, 1, 2)); + assertEquals(3, rangeFreqQuery1.query(0, 2, 1)); + assertEquals(1, rangeFreqQuery1.query(3, 3, 2)); + assertEquals(1, rangeFreqQuery1.query(2, 2, 1)); + + rangeFreqQuery2 = new _2080.Solution2.RangeFreqQuery(new int[] {1, 1, 1, 2, 2}); + assertEquals(0, rangeFreqQuery2.query(0, 1, 2)); + assertEquals(3, rangeFreqQuery2.query(0, 2, 1)); + assertEquals(1, rangeFreqQuery2.query(3, 3, 2)); + assertEquals(1, rangeFreqQuery2.query(2, 2, 1)); + } + + @Test + public void test3() { + rangeFreqQuery1 = + new _2080.Solution1.RangeFreqQuery(new int[] {3, 4, 5, 3, 3, 2, 2, 2, 5, 4}); + assertEquals(2, rangeFreqQuery1.query(2, 6, 3)); + assertEquals(0, rangeFreqQuery1.query(5, 6, 5)); + assertEquals(2, rangeFreqQuery1.query(1, 6, 2)); + assertEquals(1, rangeFreqQuery1.query(0, 2, 3)); + assertEquals(0, rangeFreqQuery1.query(5, 6, 4)); + + rangeFreqQuery2 = + new _2080.Solution2.RangeFreqQuery(new int[] {3, 4, 5, 3, 3, 2, 2, 2, 5, 4}); + assertEquals(2, rangeFreqQuery2.query(2, 6, 3)); + assertEquals(0, rangeFreqQuery2.query(5, 6, 5)); + assertEquals(2, rangeFreqQuery2.query(1, 6, 2)); + assertEquals(1, rangeFreqQuery2.query(0, 2, 3)); + assertEquals(0, rangeFreqQuery2.query(5, 6, 4)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2096Test.java b/src/test/java/com/fishercoder/thirdthousand/_2096Test.java new file mode 100644 index 0000000000..77483dd305 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2096Test.java @@ -0,0 +1,50 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.thirdthousand._2096; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2096Test { + private _2096.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2096.Solution1(); + } + + @Test + public void test1() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(5, 1, 2, 3, null, 6, 4)); + TreeUtils.printBinaryTree(root); + assertEquals("UURL", solution1.getDirections(root, 3, 6)); + } + + @Test + public void test2() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(2, 1)); + TreeUtils.printBinaryTree(root); + assertEquals("L", solution1.getDirections(root, 2, 1)); + } + + @Test + public void test3() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2)); + TreeUtils.printBinaryTree(root); + assertEquals("U", solution1.getDirections(root, 2, 1)); + } + + @Test + public void test4() { + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 5, 8, 3, 1, null, 4, 7, 6, null, null, null, null, null, null, 2)); + TreeUtils.printBinaryTree(root); + assertEquals("U", solution1.getDirections(root, 4, 3)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2103Test.java b/src/test/java/com/fishercoder/thirdthousand/_2103Test.java new file mode 100644 index 0000000000..6c920e68bc --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2103Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2103; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2103Test { + private _2103.Solution1 solution1; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _2103.Solution1(); + } + + @Test + public void test1() { + expected = 1; + assertEquals(expected, solution1.countPoints("B0B6G0R6R0R6G9")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2115Test.java b/src/test/java/com/fishercoder/thirdthousand/_2115Test.java new file mode 100644 index 0000000000..fb418e7938 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2115Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2115; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2115Test { + private _2115.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2115.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("bread"), + solution1.findAllRecipes( + new String[] {"bread"}, + Arrays.asList(Arrays.asList("yeast", "flour")), + new String[] {"yeast", "flour", "corn"})); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList("bread", "sandwich"), + solution1.findAllRecipes( + new String[] {"bread", "sandwich"}, + Arrays.asList( + Arrays.asList("yeast", "flour"), Arrays.asList("bread", "meat")), + new String[] {"yeast", "flour", "corn"})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2116Test.java b/src/test/java/com/fishercoder/thirdthousand/_2116Test.java new file mode 100644 index 0000000000..91be3c8e0c --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2116Test.java @@ -0,0 +1,45 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2116; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2116Test { + private _2116.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2116.Solution1(); + } + + @Test + public void test1() { + assertEquals(true, solution1.canBeValid("))()))", "010100")); + } + + @Test + public void test2() { + assertEquals(true, solution1.canBeValid("()()", "0000")); + } + + @Test + public void test3() { + assertEquals(false, solution1.canBeValid(")", "0")); + } + + @Test + public void test4() { + assertEquals(true, solution1.canBeValid(")(", "00")); + } + + @Test + public void test5() { + assertEquals( + false, + solution1.canBeValid( + "())(()(()(())()())(())((())(()())((())))))(((((((())(()))))(", + "100011110110011011010111100111011101111110000101001101001111")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2126Test.java b/src/test/java/com/fishercoder/thirdthousand/_2126Test.java new file mode 100644 index 0000000000..736c22cd27 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2126Test.java @@ -0,0 +1,43 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2126; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2126Test { + private _2126.Solution1 solution1; + private static int[] asteroids; + private static int mass; + private boolean expected; + + @BeforeEach + public void setup() { + solution1 = new _2126.Solution1(); + } + + @Test + public void test1() { + asteroids = new int[] {3, 9, 19, 5, 21}; + mass = 10; + expected = true; + assertEquals(expected, solution1.asteroidsDestroyed(mass, asteroids)); + } + + @Test + public void test2() { + asteroids = new int[] {4, 9, 23, 4}; + mass = 5; + expected = false; + assertEquals(expected, solution1.asteroidsDestroyed(mass, asteroids)); + } + + @Test + public void test3() { + asteroids = new int[] {156, 197, 192, 14, 97, 160, 14, 5}; + mass = 86; + expected = true; + assertEquals(expected, solution1.asteroidsDestroyed(mass, asteroids)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2134Test.java b/src/test/java/com/fishercoder/thirdthousand/_2134Test.java new file mode 100644 index 0000000000..97c7abc85e --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2134Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2134; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2134Test { + private _2134.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _2134.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {0, 1, 0, 1, 1, 0, 0}; + assertEquals(1, solution1.minSwaps(nums)); + } + + @Test + public void test2() { + nums = new int[] {0, 1, 1, 1, 0, 0, 1, 1, 0}; + assertEquals(2, solution1.minSwaps(nums)); + } + + @Test + public void test3() { + nums = new int[] {1, 1, 0, 0, 1}; + assertEquals(0, solution1.minSwaps(nums)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2135Test.java b/src/test/java/com/fishercoder/thirdthousand/_2135Test.java new file mode 100644 index 0000000000..52d220ad87 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2135Test.java @@ -0,0 +1,44 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2135; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2135Test { + private _2135.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2135.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 2, + solution1.wordCount( + new String[] {"ant", "act", "tack"}, new String[] {"tack", "act", "acti"})); + } + + @Test + public void test2() { + assertEquals( + 1, + solution1.wordCount( + new String[] {"mox", "bj", "rsy", "jqsh"}, + new String[] {"trk", "vjb", "jkr"})); + } + + @Test + public void test3() { + assertEquals( + 1, + solution1.wordCount( + new String[] {"uh"}, + new String[] { + "u", "hur", "k", "b", "u", "yse", "giqoy", "lni", "olqb", "nemc" + })); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2144Test.java b/src/test/java/com/fishercoder/thirdthousand/_2144Test.java new file mode 100644 index 0000000000..94119c4848 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2144Test.java @@ -0,0 +1,39 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2144; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2144Test { + private _2144.Solution1 solution1; + private static int[] cost; + private static int expected; + + @BeforeEach + public void setup() { + solution1 = new _2144.Solution1(); + } + + @Test + public void test1() { + expected = 5; + cost = new int[] {1, 2, 3}; + assertEquals(expected, solution1.minimumCost(cost)); + } + + @Test + public void test2() { + expected = 23; + cost = new int[] {6, 5, 7, 9, 2, 2}; + assertEquals(expected, solution1.minimumCost(cost)); + } + + @Test + public void test3() { + expected = 10; + cost = new int[] {5, 5}; + assertEquals(expected, solution1.minimumCost(cost)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2156Test.java b/src/test/java/com/fishercoder/thirdthousand/_2156Test.java new file mode 100644 index 0000000000..98760fa272 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2156Test.java @@ -0,0 +1,57 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2156; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2156Test { + private _2156.Solution1 solution1; + private static String s; + private int power; + private int modulo; + private static int k; + private static int hashValue; + private static String expected; + + @BeforeEach + public void setup() { + solution1 = new _2156.Solution1(); + } + + @Test + public void test1() { + s = "leetcode"; + power = 7; + modulo = 20; + k = 2; + hashValue = 0; + expected = "ee"; + assertEquals(expected, solution1.subStrHash(s, power, modulo, k, hashValue)); + } + + @Test + public void test2() { + s = "fbxzaad"; + power = 31; + modulo = 100; + k = 3; + hashValue = 32; + expected = "fbx"; + assertEquals(expected, solution1.subStrHash(s, power, modulo, k, hashValue)); + } + + @Test + public void test3() { + s = "xmmhdakfursinye"; + power = 96; + modulo = 45; + k = 15; + hashValue = 21; + expected = "xmmhdakfursinye"; + System.out.println(Math.pow(power, k - 1)); + System.out.println(Math.pow(power, k - 1) % modulo); + assertEquals(expected, solution1.subStrHash(s, power, modulo, k, hashValue)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2190Test.java b/src/test/java/com/fishercoder/thirdthousand/_2190Test.java new file mode 100644 index 0000000000..5c1c6a3ccc --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2190Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2190; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2190Test { + private _2190.Solution1 solution1; + private static int[] nums; + private static int key; + + @BeforeEach + public void setup() { + solution1 = new _2190.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {2, 2, 2, 2, 3}; + key = 2; + assertEquals(2, solution1.mostFrequent(nums, key)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2191Test.java b/src/test/java/com/fishercoder/thirdthousand/_2191Test.java new file mode 100644 index 0000000000..4454acfcf0 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2191Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2191; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2191Test { + private _2191.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2191.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {338, 38, 991}, + solution1.sortJumbled( + new int[] {8, 9, 4, 0, 2, 1, 3, 5, 7, 6}, new int[] {991, 338, 38})); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {0, 999999999}, + solution1.sortJumbled( + new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, new int[] {0, 999999999})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2192Test.java b/src/test/java/com/fishercoder/thirdthousand/_2192Test.java new file mode 100644 index 0000000000..e7756bb44c --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2192Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2192; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2192Test { + private _2192.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2192.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList( + Arrays.asList(), + Arrays.asList(), + Arrays.asList(), + Arrays.asList(0, 1), + Arrays.asList(0, 2), + Arrays.asList(0, 1, 3), + Arrays.asList(0, 1, 2, 3, 4), + Arrays.asList(0, 1, 2, 3)), + solution1.getAncestors( + 8, + CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray( + "[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]"))); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2196Test.java b/src/test/java/com/fishercoder/thirdthousand/_2196Test.java new file mode 100644 index 0000000000..f19c264b1c --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2196Test.java @@ -0,0 +1,32 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.thirdthousand._2196; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2196Test { + private _2196.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2196.Solution1(); + } + + @Test + public void test1() { + TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(50, 20, 80, 15, 17, 19)); + TreeUtils.printBinaryTree(expected); + TreeNode actual = + solution1.createBinaryTree( + new int[][] { + {20, 15, 1}, {20, 17, 0}, {50, 20, 1}, {50, 80, 0}, {80, 19, 1} + }); + TreeUtils.printBinaryTree(actual); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2220Test.java b/src/test/java/com/fishercoder/thirdthousand/_2220Test.java new file mode 100644 index 0000000000..95cbbd2ebd --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2220Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2220; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2220Test { + private _2220.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2220.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.minBitFlips(10, 7)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2224Test.java b/src/test/java/com/fishercoder/thirdthousand/_2224Test.java new file mode 100644 index 0000000000..c1f1d336cd --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2224Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2224; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2224Test { + private _2224.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2224.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.convertTime("02:30", "04:35")); + } + + @Test + public void test2() { + assertEquals(1, solution1.convertTime("11:00", "11:01")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2231Test.java b/src/test/java/com/fishercoder/thirdthousand/_2231Test.java new file mode 100644 index 0000000000..6bdfb15fdf --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2231Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2231; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2231Test { + private _2231.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2231.Solution1(); + } + + @Test + public void test1() { + assertEquals(3412, solution1.largestInteger(1234)); + } + + @Test + public void test2() { + assertEquals(87655, solution1.largestInteger(65875)); + } + + @Test + public void test3() { + assertEquals(427, solution1.largestInteger(247)); + } + + @Test + public void test4() { + assertEquals(472, solution1.largestInteger(274)); + } + + @Test + public void test5() { + assertEquals(75856, solution1.largestInteger(55678)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2265Test.java b/src/test/java/com/fishercoder/thirdthousand/_2265Test.java new file mode 100644 index 0000000000..f13f123895 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2265Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.thirdthousand._2265; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2265Test { + private _2265.Solution1 solution1; + private static TreeNode root; + + @BeforeEach + public void setup() { + solution1 = new _2265.Solution1(); + } + + @Test + public void test1() { + root = TreeUtils.constructBinaryTree(Arrays.asList(4, 8, 5, 0, 1, null, 6)); + TreeUtils.printBinaryTree(root); + assertEquals(5, solution1.averageOfSubtree(root)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2273Test.java b/src/test/java/com/fishercoder/thirdthousand/_2273Test.java new file mode 100644 index 0000000000..57847bb345 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2273Test.java @@ -0,0 +1,24 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2273; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2273Test { + private _2273.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2273.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("abba", "cd"), + solution1.removeAnagrams(new String[] {"abba", "baba", "bbaa", "cd", "cd"})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2284Test.java b/src/test/java/com/fishercoder/thirdthousand/_2284Test.java new file mode 100644 index 0000000000..dc9143a5ad --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2284Test.java @@ -0,0 +1,208 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2284; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2284Test { + private _2284.Solution1 solution1; + private static String[] messages; + private static String[] senders; + + @BeforeEach + public void setup() { + solution1 = new _2284.Solution1(); + } + + @Test + public void test1() { + messages = + new String[] { + "Hello userTwooo", "Hi userThree", "Wonderful day Alice", "Nice day userThree" + }; + senders = new String[] {"Alice", "userTwo", "userThree", "Alice"}; + assertEquals("Alice", solution1.largestWordCount(messages, senders)); + } + + @Test + public void test2() { + messages = new String[] {"How is leetcode for everyone", "Leetcode is useful for practice"}; + senders = new String[] {"Bob", "Charlie"}; + assertEquals("Charlie", solution1.largestWordCount(messages, senders)); + } + + @Test + public void test3() { + messages = + new String[] { + "Ux i E XMm", + "G Mo f q Qa q", + "v qZ J m R", + "z pt T yG W xq Xq G", + "GS F Ug", + "QDv", + "I iY k pd M", + "aOi", + "f xV xa", + "c Zu Fa ofO", + "x c E R H", + "pw sfU", + "i aE G Aqw", + "Yu S di sV sx mc AlB", + "D lx g cF k", + "U fw rh Ne", + "I aN o Sv aE s", + "ZF c Jo IA", + "Y S f Ld D M fbb", + "OI Mn e Q A gT", + "xV f Li v h vy I S", + "Q gI G vj Qd c y r W", + "Q R BK VI", + "K Am NZ", + "wk CT", + "p sQ b Se l BI We fv", + "x WF fW l n px WY rz", + "S rW mh", + "a T og TA b Gg h", + "t v WO", + "Ai bO mY", + "e AMh", + "t nfH", + "q F G ch N", + "sf W iH yx M Pf YjA", + "uE D", + "hA F q NX", + "Fm", + "lI C Vl Em md d L", + "az kz i bx g v dD", + "Fq UR qf hh", + "C r Nq u Ve i", + "x tT BR Bj d a yu G", + "Nm M DM h Wu", + "IZ y Lo ZN Yv", + "l Kh ia Rt", + "VR cg C fM mL MH", + "a P e Gb", + "Xq UO", + "U qM", + "h bM mn e a", + "WD w VT Tf dK G YPE", + "cT T wc O VLT", + "e q K e Ao V kw", + "Ie dt JB a C y O rq", + "ih Wu", + "QP T G Zl Yx Q pSz", + "Rs", + "xA y D e e g", + "Gik", + "D o Y wyD", + "mG z N a j fz P", + "U q W", + "Ei xr Zf", + "wT X EI vz BI", + "nj Fr g J P qH h gZa", + "e wB XX s", + "wL Md wt", + "RE yd U rY J qx", + "DO Q a U N", + "p F gh fv", + "xn LT vg rZ pF z xrf", + "k", + "DD r sh B", + "Z Eg iJ Hq r VX h", + "Xy N k Hd Lk ea", + "teU", + "n kp U k KZ aw", + "UG uO ax S y", + "q D SD", + "r ns E Wv XR wv tP g" + }; + senders = + new String[] { + "K", + "kFIbpoFxn", + "yErgn", + "N", + "wtJesr", + "rusffeL", + "KlpoodEd", + "qGcQqIVdFr", + "ztmCdK", + "HFILjKln", + "rusffeL", + "TmmQZ", + "R", + "CNh", + "YMQDBkOWy", + "kjiSc", + "cGMsZxxx", + "YMQDBkOWy", + "PPqsmNBewN", + "gbtn", + "nQNcL", + "rK", + "ppr", + "LhSVp", + "Ub", + "QGRFMLY", + "YMQDBkOWy", + "Ub", + "PPqsmNBewN", + "SdDObYkD", + "q", + "suAakSCuHz", + "QGRFMLY", + "dnzhjdwrEt", + "ubIEXAO", + "EsBuLal", + "kFIbpoFxn", + "yErgn", + "ubIEXAO", + "TmmQZ", + "TmmQZ", + "xlQqQRrdTv", + "mWxCG", + "TmmQZ", + "DmwIEmS", + "gbtn", + "nBQLLS", + "QhF", + "Ub", + "ppr", + "bmtYQKYv", + "ppr", + "EsBuLal", + "PRiNk", + "rusffeL", + "ztmCdK", + "PPqsmNBewN", + "rK", + "xlQqQRrdTv", + "QGRFMLY", + "EsBuLal", + "QyYJw", + "QIFauTN", + "dnzhjdwrEt", + "zJLcUq", + "ubIEXAO", + "HFILjKln", + "ppr", + "wtJesr", + "ztmCdK", + "suAakSCuHz", + "zJLcUq", + "TU", + "HFILjKln", + "lCkGjDY", + "A", + "zJLcUq", + "SdDObYkD", + "YMQDBkOWy", + "R", + "LhSVp" + }; + assertEquals("ubIEXAO", solution1.largestWordCount(messages, senders)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2293Test.java b/src/test/java/com/fishercoder/thirdthousand/_2293Test.java new file mode 100644 index 0000000000..e75a37356c --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2293Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2293; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2293Test { + private _2293.Solution1 solution1; + private static int expected; + private static int[] nums; + private static String s; + + @BeforeEach + public void setup() { + solution1 = new _2293.Solution1(); + } + + @Test + public void test1() { + expected = 22; + nums = new int[] {70, 38, 21, 22}; + assertEquals(expected, solution1.minMaxGame(nums)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2300Test.java b/src/test/java/com/fishercoder/thirdthousand/_2300Test.java new file mode 100644 index 0000000000..6b72e3591d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2300Test.java @@ -0,0 +1,38 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2300; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2300Test { + private _2300.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2300.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {4, 0, 3}, + solution1.successfulPairs(new int[] {5, 1, 3}, new int[] {1, 2, 3, 4, 5}, 7)); + } + + @Test + public void test2() { + assertArrayEquals( + new int[] {2, 0, 2}, + solution1.successfulPairs(new int[] {3, 1, 2}, new int[] {8, 5, 8}, 16)); + } + + @Test + public void test3() { + assertArrayEquals( + new int[] {0, 0, 0, 1, 3, 3, 4}, + solution1.successfulPairs( + new int[] {1, 2, 3, 4, 5, 6, 7}, new int[] {1, 2, 3, 4, 5, 6, 7}, 25)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2309Test.java b/src/test/java/com/fishercoder/thirdthousand/_2309Test.java new file mode 100644 index 0000000000..90d42b4d83 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2309Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2309; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2309Test { + private _2309.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2309.Solution1(); + } + + @Test + public void test1() { + assertEquals("E", solution1.greatestLetter("lEeTcOdE")); + } + + @Test + public void test2() { + assertEquals("R", solution1.greatestLetter("arRAzFif")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2315Test.java b/src/test/java/com/fishercoder/thirdthousand/_2315Test.java new file mode 100644 index 0000000000..7494165156 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2315Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2315; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2315Test { + private _2315.Solution1 solution1; + private static String s; + + @BeforeEach + public void setup() { + solution1 = new _2315.Solution1(); + } + + @Test + public void test1() { + s = "l|*e*et|c**o|*de|"; + assertEquals(2, solution1.countAsterisks(s)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2316Test.java b/src/test/java/com/fishercoder/thirdthousand/_2316Test.java new file mode 100644 index 0000000000..74b7c4abf4 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2316Test.java @@ -0,0 +1,27 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2316; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2316Test { + private _2316.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2316.Solution1(); + } + + @Test + public void test1() { + assertEquals(0, solution1.countPairs(3, new int[][] {{0, 1}, {0, 2}, {1, 2}})); + } + + @Test + public void test2() { + assertEquals( + 14, solution1.countPairs(7, new int[][] {{0, 2}, {0, 5}, {2, 4}, {1, 6}, {5, 4}})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2325Test.java b/src/test/java/com/fishercoder/thirdthousand/_2325Test.java new file mode 100644 index 0000000000..4618ccdca0 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2325Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.thirdthousand; + +import com.fishercoder.solutions.thirdthousand._2325; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2325Test { + private _2325.Solution1 solution1; + private _2325.Solution2 solution2; + private String key; + private String message; + + @BeforeEach + public void setup() { + solution1 = new _2325.Solution1(); + solution2 = new _2325.Solution2(); + } + + @Test + public void test1() { + key = "the quick brown fox jumps over the lazy dog"; + message = "vkbs bs t suepuv"; + String actual = solution1.decodeMessage(key, message); + String expected = "this is a secret"; + Assertions.assertEquals(actual, expected); + } + + @Test + public void test2() { + key = "the quick brown fox jumps over the lazy dog"; + message = "vkbs bs t suepuv"; + String actual = solution2.decodeMessage(key, message); + String expected = "this is a secret"; + Assertions.assertEquals(actual, expected); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2335Test.java b/src/test/java/com/fishercoder/thirdthousand/_2335Test.java new file mode 100644 index 0000000000..23cfea0731 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2335Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2335; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2335Test { + private _2335.Solution1 solution1; + private static int[] amount; + + @BeforeEach + public void setup() { + solution1 = new _2335.Solution1(); + } + + @Test + public void test1() { + amount = new int[] {5, 4, 4}; + assertEquals(7, solution1.fillCups(amount)); + } + + @Test + public void test2() { + amount = new int[] {0, 0, 0}; + assertEquals(0, solution1.fillCups(amount)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2357Test.java b/src/test/java/com/fishercoder/thirdthousand/_2357Test.java new file mode 100644 index 0000000000..dea8b9577f --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2357Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2357; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2357Test { + private _2357.Solution1 solution1; + private static int[] nums; + + @BeforeEach + public void setup() { + solution1 = new _2357.Solution1(); + } + + @Test + public void test1() { + nums = new int[] {1, 5, 0, 3, 5}; + assertEquals(3, solution1.minimumOperations(nums)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2373Test.java b/src/test/java/com/fishercoder/thirdthousand/_2373Test.java new file mode 100644 index 0000000000..e4c4002e05 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2373Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2373; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2373Test { + + private _2373.Solution1 solution1; + private static int[][] grid; + private static int[][] expected; + + @BeforeEach + public void setup() { + solution1 = new _2373.Solution1(); + } + + @Test + public void test1() { + grid = + new int[][] { + {9, 9, 8, 1}, + {5, 6, 2, 6}, + {8, 2, 6, 4}, + {6, 2, 2, 2} + }; + expected = + new int[][] { + {9, 9}, + {8, 6} + }; + assertArrayEquals(expected, solution1.largestLocal(grid)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2385Test.java b/src/test/java/com/fishercoder/thirdthousand/_2385Test.java new file mode 100644 index 0000000000..8871a74a88 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2385Test.java @@ -0,0 +1,33 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.thirdthousand._2385; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2385Test { + private _2385.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2385.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 4, + solution1.amountOfTime( + TreeUtils.constructBinaryTree(Arrays.asList(1, 5, 3, null, 4, 10, 6, 9, 2)), + 3)); + } + + @Test + public void test2() { + assertEquals( + 1, solution1.amountOfTime(TreeUtils.constructBinaryTree(Arrays.asList(2, 5)), 5)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2389Test.java b/src/test/java/com/fishercoder/thirdthousand/_2389Test.java new file mode 100644 index 0000000000..7d0606db8d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2389Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2389; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2389Test { + private _2389.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2389.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {2, 3, 4}, + solution1.answerQueries(new int[] {4, 5, 2, 1}, new int[] {3, 10, 21})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2392Test.java b/src/test/java/com/fishercoder/thirdthousand/_2392Test.java new file mode 100644 index 0000000000..281318e254 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2392Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.thirdthousand; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2392; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2392Test { + private _2392.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2392.Solution1(); + } + + @Test + public void test1() { + CommonUtils.print2DIntArray( + solution1.buildMatrix( + 3, new int[][] {{1, 2}, {3, 2}}, new int[][] {{2, 1}, {3, 2}})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2409Test.java b/src/test/java/com/fishercoder/thirdthousand/_2409Test.java new file mode 100644 index 0000000000..373822078f --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2409Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2409; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2409Test { + private _2409.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2409.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.countDaysTogether("08-15", "08-18", "08-16", "08-19")); + } + + @Test + public void test2() { + assertEquals(0, solution1.countDaysTogether("10-01", "10-31", "11-01", "12-31")); + } + + @Test + public void test3() { + assertEquals(49, solution1.countDaysTogether("09-01", "10-19", "06-19", "10-20")); + } + + @Test + public void test4() { + assertEquals(27, solution1.countDaysTogether("08-06", "12-08", "08-06", "09-01")); + } + + @Test + public void test5() { + assertEquals(27, solution1.countDaysTogether("08-06", "12-08", "02-04", "09-01")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2423Test.java b/src/test/java/com/fishercoder/thirdthousand/_2423Test.java new file mode 100644 index 0000000000..42166fc7ea --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2423Test.java @@ -0,0 +1,47 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.thirdthousand._2423; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2423Test { + private _2423.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2423.Solution1(); + } + + @Test + public void test1() { + assertTrue(solution1.equalFrequency("abcc")); + } + + @Test + public void test2() { + assertTrue(solution1.equalFrequency("abc")); + } + + @Test + public void test3() { + assertFalse(solution1.equalFrequency("ddaccb")); + } + + @Test + public void test4() { + assertTrue(solution1.equalFrequency("abbcc")); + } + + @Test + public void test5() { + assertFalse(solution1.equalFrequency("aazz")); + } + + @Test + public void test6() { + assertTrue(solution1.equalFrequency("ab")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2433Test.java b/src/test/java/com/fishercoder/thirdthousand/_2433Test.java new file mode 100644 index 0000000000..3af647a895 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2433Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2433; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2433Test { + private _2433.Solution1 solution1; + private static int[] pref; + + @BeforeEach + public void setup() { + solution1 = new _2433.Solution1(); + } + + @Test + public void test1() { + pref = new int[] {5, 2, 0, 3, 1}; + assertArrayEquals(new int[] {5, 7, 2, 3, 2}, solution1.findArray(pref)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2437Test.java b/src/test/java/com/fishercoder/thirdthousand/_2437Test.java new file mode 100644 index 0000000000..43c6d2bdbc --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2437Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2437; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2437Test { + private _2437.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2437.Solution1(); + } + + @Test + public void test1() { + assertEquals(1440, solution1.countTime("??:??")); + } + + @Test + public void test2() { + assertEquals(3, solution1.countTime("?2:16")); + } + + @Test + public void test3() { + assertEquals(2, solution1.countTime("?5:00")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2441Test.java b/src/test/java/com/fishercoder/thirdthousand/_2441Test.java new file mode 100644 index 0000000000..78f76e4e76 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2441Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2441; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2441Test { + private _2441.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2441.Solution1(); + } + + @Test + public void test1() { + assertEquals(7, solution1.findMaxK(new int[] {-1, 10, 6, 7, -7, 1})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2446Test.java b/src/test/java/com/fishercoder/thirdthousand/_2446Test.java new file mode 100644 index 0000000000..13cd344692 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2446Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.thirdthousand._2446; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2446Test { + private _2446.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2446.Solution1(); + } + + @Test + public void test1() { + assertTrue( + solution1.haveConflict( + new String[] {"01:15", "02:00"}, new String[] {"02:00", "03:00"})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2451Test.java b/src/test/java/com/fishercoder/thirdthousand/_2451Test.java new file mode 100644 index 0000000000..b037889edf --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2451Test.java @@ -0,0 +1,62 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2451; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2451Test { + private _2451.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2451.Solution1(); + } + + @Test + public void test1() { + assertEquals("abc", solution1.oddString(new String[] {"adc", "wzy", "abc"})); + } + + @Test + public void test2() { + assertEquals( + "aaaabbbbbbaaabaaaabb", + solution1.oddString( + new String[] { + "nnnmmmnnmmmmmmmmmmnm", + "iiihhhiihhhhhhhhhhih", + "aaaabbbbbbaaabaaaabb", + "qqqpppqqppppppppppqp", + "eeedddeedddddddddded", + "eeedddeedddddddddded", + "iiihhhiihhhhhhhhhhih", + "lllkkkllkkkkkkkkkklk", + "sssrrrssrrrrrrrrrrsr", + "sssrrrssrrrrrrrrrrsr", + "jjjiiijjiiiiiiiiiiji", + "nnnmmmnnmmmmmmmmmmnm", + "xxxwwwxxwwwwwwwwwwxw", + "eeedddeedddddddddded", + "zzzyyyzzyyyyyyyyyyzy", + "wwwvvvwwvvvvvvvvvvwv", + "cccbbbccbbbbbbbbbbcb", + "xxxwwwxxwwwwwwwwwwxw", + "cccbbbccbbbbbbbbbbcb", + "yyyxxxyyxxxxxxxxxxyx", + "hhhggghhgggggggggghg" + })); + } + + @Test + public void test3() { + assertEquals( + "abb", + solution1.oddString( + new String[] { + "mll", "abb", "edd", "jii", "tss", "fee", "dcc", "nmm", "utt", "zyy", + "xww", "tss", "wvv", "xww", "utt" + })); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2460Test.java b/src/test/java/com/fishercoder/thirdthousand/_2460Test.java new file mode 100644 index 0000000000..6e36d15713 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2460Test.java @@ -0,0 +1,28 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2460; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2460Test { + private _2460.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2460.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {1, 4, 2, 0, 0, 0}, + solution1.applyOperations(new int[] {1, 2, 2, 1, 1, 0})); + } + + @Test + public void test2() { + assertArrayEquals(new int[] {1, 0}, solution1.applyOperations(new int[] {0, 1})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2473Test.java b/src/test/java/com/fishercoder/thirdthousand/_2473Test.java new file mode 100644 index 0000000000..c37eb5c6ea --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2473Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2473; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2473Test { + private _2473.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2473.Solution1(); + } + + @Test + public void test1() { + long[] actual = + solution1.minCost( + 4, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2,4],[2,3,2],[2,4,5],[3,4,1],[1,3,4]"), + new int[] {56, 42, 102, 301}, + 2); + assertArrayEquals(new long[] {54, 42, 48, 51}, actual); + } + + @Test + public void test2() { + assertArrayEquals( + new long[] {49117, 67662, 34318, 89780, 2747, 39709, 38302, 21966}, + solution1.minCost( + 8, + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[8,3,193],[4,1,890],[8,2,714],[7,2,654],[6,1,147]"), + new int[] {87310, 86029, 37141, 89780, 2747, 39709, 38302, 21966}, + 63)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2485Test.java b/src/test/java/com/fishercoder/thirdthousand/_2485Test.java new file mode 100644 index 0000000000..0de5a17b8c --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2485Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2485; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2485Test { + private _2485.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2485.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.pivotInteger(1)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2487Test.java b/src/test/java/com/fishercoder/thirdthousand/_2487Test.java new file mode 100644 index 0000000000..697396918d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2487Test.java @@ -0,0 +1,54 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.LinkedListUtils; +import com.fishercoder.solutions.thirdthousand._2487; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2487Test { + private _2487.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2487.Solution1(); + } + + @Test + public void test1() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {13, 8}), + solution1.removeNodes( + LinkedListUtils.contructLinkedList(new int[] {5, 2, 13, 3, 8}))); + } + + @Test + public void test2() { + assertEquals( + LinkedListUtils.contructLinkedList(new int[] {998, 961, 943, 920, 698}), + solution1.removeNodes( + LinkedListUtils.contructLinkedList( + new int[] { + 138, 466, 216, 67, 642, 978, 264, 136, 463, 331, 60, 600, 223, + 275, 856, 809, 167, 101, 846, 165, 575, 276, 409, 590, 733, 200, + 839, 515, 852, 615, 8, 584, 250, 337, 537, 63, 797, 900, 670, + 636, 112, 701, 334, 422, 780, 552, 912, 506, 313, 474, 183, 792, + 822, 661, 37, 164, 601, 271, 902, 792, 501, 184, 559, 140, 506, + 94, 161, 167, 622, 288, 457, 953, 700, 464, 785, 203, 729, 725, + 422, 76, 191, 195, 157, 854, 730, 577, 503, 401, 517, 692, 42, + 135, 823, 883, 255, 111, 334, 365, 513, 338, 65, 600, 926, 607, + 193, 763, 366, 674, 145, 229, 700, 11, 984, 36, 185, 475, 204, + 604, 191, 898, 876, 762, 654, 770, 774, 575, 276, 165, 610, 649, + 235, 749, 440, 607, 962, 747, 891, 943, 839, 403, 655, 22, 705, + 416, 904, 765, 905, 574, 214, 471, 451, 774, 41, 365, 703, 895, + 327, 879, 414, 821, 363, 30, 130, 14, 754, 41, 494, 548, 76, + 825, 899, 499, 188, 982, 8, 890, 563, 438, 363, 32, 482, 623, + 864, 161, 962, 678, 414, 659, 612, 332, 164, 580, 14, 633, 842, + 969, 792, 777, 705, 436, 750, 501, 395, 342, 838, 493, 998, 112, + 660, 961, 943, 721, 480, 522, 133, 129, 276, 362, 616, 52, 117, + 300, 274, 862, 487, 715, 272, 232, 543, 275, 68, 144, 656, 623, + 317, 63, 908, 565, 880, 12, 920, 467, 559, 91, 698 + }))); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2492Test.java b/src/test/java/com/fishercoder/thirdthousand/_2492Test.java new file mode 100644 index 0000000000..96c14c0c22 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2492Test.java @@ -0,0 +1,42 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2492; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2492Test { + private _2492.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2492.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 5, solution1.minScore(4, new int[][] {{1, 2, 9}, {2, 3, 6}, {2, 4, 5}, {1, 4, 7}})); + } + + @Test + public void test2() { + assertEquals( + 1885, + solution1.minScore( + 6, + new int[][] { + {4, 5, 7468}, + {6, 2, 7173}, + {6, 3, 8365}, + {2, 3, 7674}, + {5, 6, 7852}, + {1, 2, 8547}, + {2, 4, 1885}, + {2, 5, 5192}, + {1, 3, 4065}, + {1, 4, 7357} + })); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2500Test.java b/src/test/java/com/fishercoder/thirdthousand/_2500Test.java new file mode 100644 index 0000000000..b8d5865f79 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2500Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2500; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2500Test { + private _2500.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2500.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 8, + solution1.deleteGreatestValue( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2,4],[3,3,1]"))); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2501Test.java b/src/test/java/com/fishercoder/thirdthousand/_2501Test.java new file mode 100644 index 0000000000..a37cda5544 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2501Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2501; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2501Test { + private _2501.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2501.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.longestSquareStreak(new int[] {4, 3, 6, 16, 8, 2})); + } + + @Test + public void test2() { + assertEquals(-1, solution1.longestSquareStreak(new int[] {2, 3, 5, 6, 7})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2511Test.java b/src/test/java/com/fishercoder/thirdthousand/_2511Test.java new file mode 100644 index 0000000000..e2ecc3f0f5 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2511Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2511; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2511Test { + private _2511.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2511.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.captureForts(new int[] {1, 0, 0, -1, 0, 0, 0, 0, 1})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2515Test.java b/src/test/java/com/fishercoder/thirdthousand/_2515Test.java new file mode 100644 index 0000000000..cf8e826575 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2515Test.java @@ -0,0 +1,35 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2515; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2515Test { + private _2515.Solution1 solution1; + private static String[] words; + + @BeforeEach + public void setup() { + solution1 = new _2515.Solution1(); + } + + @Test + public void test1() { + words = new String[] {"hello", "i", "am", "leetcode", "hello"}; + assertEquals(1, solution1.closetTarget(words, "hello", 1)); + } + + @Test + public void test2() { + words = new String[] {"a", "b", "leetcode"}; + assertEquals(1, solution1.closetTarget(words, "leetcode", 0)); + } + + @Test + public void test3() { + words = new String[] {"i", "eat", "leetcode"}; + assertEquals(-1, solution1.closetTarget(words, "ate", 0)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2525Test.java b/src/test/java/com/fishercoder/thirdthousand/_2525Test.java new file mode 100644 index 0000000000..486c227127 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2525Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2525; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2525Test { + private _2525.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2525.Solution1(); + } + + @Test + public void test1() { + assertEquals("Both", solution1.categorizeBox(2909, 3968, 3272, 727)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2544Test.java b/src/test/java/com/fishercoder/thirdthousand/_2544Test.java new file mode 100644 index 0000000000..0aaf3475b4 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2544Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2544; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2544Test { + private _2544.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2544.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.alternateDigitSum(521)); + } + + @Test + public void test2() { + assertEquals(1, solution1.alternateDigitSum(10)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2566Test.java b/src/test/java/com/fishercoder/thirdthousand/_2566Test.java new file mode 100644 index 0000000000..3a55bd2836 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2566Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2566; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2566Test { + private _2566.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2566.Solution1(); + } + + @Test + public void test1() { + assertEquals(99009, solution1.minMaxDifference(11891)); + } + + @Test + public void test2() { + assertEquals(900, solution1.minMaxDifference(456)); + } + + @Test + public void test3() { + assertEquals(99, solution1.minMaxDifference(90)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2574Test.java b/src/test/java/com/fishercoder/thirdthousand/_2574Test.java new file mode 100644 index 0000000000..5c5f32337d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2574Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2574; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2574Test { + private _2574.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2574.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {15, 1, 11, 22}, solution1.leftRightDifference(new int[] {10, 4, 8, 3})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2578Test.java b/src/test/java/com/fishercoder/thirdthousand/_2578Test.java new file mode 100644 index 0000000000..e63afb9f5f --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2578Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2578; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2578Test { + private _2578.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2578.Solution1(); + } + + @Test + public void test1() { + assertEquals(59, solution1.splitNum(4325)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2591Test.java b/src/test/java/com/fishercoder/thirdthousand/_2591Test.java new file mode 100644 index 0000000000..a5e97bf79d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2591Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2591; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2591Test { + private _2591.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2591.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.distMoney(13, 3)); + } + + @Test + public void test2() { + assertEquals(1, solution1.distMoney(17, 2)); + } + + @Test + public void test3() { + assertEquals(1, solution1.distMoney(20, 3)); + } + + @Test + public void test4() { + assertEquals(2, solution1.distMoney(16, 2)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2600Test.java b/src/test/java/com/fishercoder/thirdthousand/_2600Test.java new file mode 100644 index 0000000000..3721066458 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2600Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2600; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2600Test { + private _2600.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2600.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.kItemsWithMaximumSum(4, 2, 3, 7)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2609Test.java b/src/test/java/com/fishercoder/thirdthousand/_2609Test.java new file mode 100644 index 0000000000..6bbfddd3b0 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2609Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2609; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2609Test { + private _2609.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2609.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.findTheLongestBalancedSubstring("01000111")); + } + + @Test + public void test2() { + assertEquals(2, solution1.findTheLongestBalancedSubstring("001")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2641Test.java b/src/test/java/com/fishercoder/thirdthousand/_2641Test.java new file mode 100644 index 0000000000..e1d03407ac --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2641Test.java @@ -0,0 +1,49 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.common.classes.TreeNode; +import com.fishercoder.common.utils.TreeUtils; +import com.fishercoder.solutions.thirdthousand._2641; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2641Test { + private _2641.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2641.Solution1(); + } + + @Test + public void test1() { + TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 9, 1, 10, null, 7)); + TreeUtils.printBinaryTree(root); + TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(0, 0, 0, 7, 7, null, 11)); + TreeUtils.printBinaryTree(expected); + TreeNode actual = solution1.replaceValueInTree(root); + TreeUtils.printBinaryTree(actual); + assertEquals(expected, actual); + } + + @Test + public void test2() { + TreeNode root = + TreeUtils.constructBinaryTree( + Arrays.asList( + 49, 40, 35, 42, 7, null, null, 50, null, null, 44, null, null, null, + 27, 21)); + TreeUtils.printBinaryTree(root); + TreeNode expected = + TreeUtils.constructBinaryTree( + Arrays.asList( + 0, 0, 0, 0, 0, null, null, 44, null, null, 50, null, null, null, 0, + 0)); + TreeUtils.printBinaryTree(expected); + TreeNode actual = solution1.replaceValueInTree(root); + TreeUtils.printBinaryTree(actual); + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2644Test.java b/src/test/java/com/fishercoder/thirdthousand/_2644Test.java new file mode 100644 index 0000000000..5a63634d43 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2644Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2644; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2644Test { + private _2644.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2644.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.maxDivScore(new int[] {2, 9, 15, 50}, new int[] {5, 3, 7, 2})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2670Test.java b/src/test/java/com/fishercoder/thirdthousand/_2670Test.java new file mode 100644 index 0000000000..b49d115896 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2670Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2670; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2670Test { + private _2670.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2670.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals( + new int[] {-2, -1, 0, 2, 3}, + solution1.distinctDifferenceArray(new int[] {3, 2, 3, 4, 2})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2673Test.java b/src/test/java/com/fishercoder/thirdthousand/_2673Test.java new file mode 100644 index 0000000000..ed5139e451 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2673Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2673; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2673Test { + private _2673.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2673.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.minIncrements(7, new int[] {1, 5, 2, 2, 3, 3, 1})); + } + + @Test + public void test2() { + assertEquals(0, solution1.minIncrements(3, new int[] {5, 3, 3})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2682Test.java b/src/test/java/com/fishercoder/thirdthousand/_2682Test.java new file mode 100644 index 0000000000..7c6dc30e5d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2682Test.java @@ -0,0 +1,41 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import com.fishercoder.solutions.thirdthousand._2682; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2682Test { + private _2682.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2682.Solution1(); + } + + @Test + public void test1() { + assertArrayEquals(new int[] {4, 5}, solution1.circularGameLosers(5, 2)); + } + + @Test + public void test2() { + assertArrayEquals(new int[] {}, solution1.circularGameLosers(2, 1)); + } + + @Test + public void test3() { + assertArrayEquals(new int[] {3}, solution1.circularGameLosers(3, 1)); + } + + @Test + public void test4() { + assertArrayEquals(new int[] {2}, solution1.circularGameLosers(3, 2)); + } + + @Test + public void test5() { + assertArrayEquals(new int[] {2, 3}, solution1.circularGameLosers(5, 3)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2689Test.java b/src/test/java/com/fishercoder/thirdthousand/_2689Test.java new file mode 100644 index 0000000000..323ea1772d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2689Test.java @@ -0,0 +1,55 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2689; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2689Test { + private _2689.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2689.Solution1(); + } + + @Test + public void test1() { + _2689.RopeTreeNode root = new _2689.RopeTreeNode(); + root.len = 12; + root.val = ""; + + _2689.RopeTreeNode rootLeft = new _2689.RopeTreeNode(); + rootLeft.len = 6; + rootLeft.val = ""; + root.left = rootLeft; + + _2689.RopeTreeNode rootLeftLeft = new _2689.RopeTreeNode(); + rootLeftLeft.len = 3; + rootLeftLeft.val = "abc"; + rootLeft.left = rootLeftLeft; + + _2689.RopeTreeNode rootLeftRight = new _2689.RopeTreeNode(); + rootLeftRight.len = 3; + rootLeftRight.val = "efg"; + rootLeft.right = rootLeftRight; + + _2689.RopeTreeNode rootRight = new _2689.RopeTreeNode(); + rootRight.len = 6; + rootRight.val = ""; + root.right = rootRight; + + _2689.RopeTreeNode rootRightLeft = new _2689.RopeTreeNode(); + rootRightLeft.len = 3; + rootRightLeft.val = "hij"; + rootRight.left = rootRightLeft; + + _2689.RopeTreeNode rootRightRight = new _2689.RopeTreeNode(); + rootRightRight.len = 3; + rootRightRight.val = "klm"; + rootRight.right = rootRightRight; + + assertEquals('c', solution1.getKthCharacter(root, 3)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2696Test.java b/src/test/java/com/fishercoder/thirdthousand/_2696Test.java new file mode 100644 index 0000000000..4d6c115b14 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2696Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2696; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2696Test { + private _2696.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2696.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minLength("ABFCACDB")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2710Test.java b/src/test/java/com/fishercoder/thirdthousand/_2710Test.java new file mode 100644 index 0000000000..029b9a66f5 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2710Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2710; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2710Test { + private _2710.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2710.Solution1(); + } + + @Test + public void test1() { + assertEquals("512301", solution1.removeTrailingZeros("51230100")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2716Test.java b/src/test/java/com/fishercoder/thirdthousand/_2716Test.java new file mode 100644 index 0000000000..154c2267b5 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2716Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2716; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2716Test { + private _2716.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2716.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minimizedStringLength("ipi")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2717Test.java b/src/test/java/com/fishercoder/thirdthousand/_2717Test.java new file mode 100644 index 0000000000..d83a03e194 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2717Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2717; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2717Test { + private _2717.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2717.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.semiOrderedPermutation(new int[] {2, 1, 4, 3})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2729Test.java b/src/test/java/com/fishercoder/thirdthousand/_2729Test.java new file mode 100644 index 0000000000..eeb63724a5 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2729Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.thirdthousand._2729; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2729Test { + private _2729.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2729.Solution1(); + } + + @Test + public void test1() { + assertTrue(solution1.isFascinating(192)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2739Test.java b/src/test/java/com/fishercoder/thirdthousand/_2739Test.java new file mode 100644 index 0000000000..e38ed1de51 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2739Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2739; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2739Test { + private _2739.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2739.Solution1(); + } + + @Test + public void test1() { + assertEquals(20, solution1.distanceTraveled(2, 1)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2744Test.java b/src/test/java/com/fishercoder/thirdthousand/_2744Test.java new file mode 100644 index 0000000000..8395a7855b --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2744Test.java @@ -0,0 +1,23 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2744; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2744Test { + private _2744.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2744.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 2, + solution1.maximumNumberOfStringPairs(new String[] {"cd", "ac", "dc", "ca", "zz"})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2748Test.java b/src/test/java/com/fishercoder/thirdthousand/_2748Test.java new file mode 100644 index 0000000000..d7f46fa5b9 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2748Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2748; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2748Test { + private _2748.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2748.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.countBeautifulPairs(new int[] {11, 21, 12})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2751Test.java b/src/test/java/com/fishercoder/thirdthousand/_2751Test.java new file mode 100644 index 0000000000..b71da8bf34 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2751Test.java @@ -0,0 +1,84 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2751; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2751Test { + private _2751.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2751.Solution1(); + } + + @Test + public void test2() { + assertEquals( + Arrays.asList(2, 17, 9, 15, 10), + solution1.survivedRobotsHealths( + new int[] {5, 4, 3, 2, 1}, new int[] {2, 17, 9, 15, 10}, "RRRRR")); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList(10), + solution1.survivedRobotsHealths(new int[] {1, 40}, new int[] {10, 11}, "RL")); + } + + @Test + public void test3() { + assertEquals( + Arrays.asList(1, 38), + solution1.survivedRobotsHealths( + new int[] {17, 24, 18}, new int[] {1, 39, 30}, "LLR")); + } + + @Test + public void test4() { + assertEquals( + Arrays.asList(36), + solution1.survivedRobotsHealths( + new int[] {34, 50, 42, 2}, new int[] {6, 27, 17, 38}, "LLRR")); + } + + @Test + public void test5() { + assertEquals( + Arrays.asList(18), + solution1.survivedRobotsHealths( + new int[] {11, 44, 16}, new int[] {1, 20, 17}, "RLR")); + } + + @Test + public void test6() { + assertEquals( + Arrays.asList(20, 16, 50), + solution1.survivedRobotsHealths( + new int[] {31, 24, 30, 19, 33}, new int[] {22, 6, 18, 16, 50}, "LRRLR")); + } + + @Test + public void test7() { + assertEquals( + Arrays.asList(1, 37, 24), + solution1.survivedRobotsHealths( + new int[] {31, 27, 15, 28, 14, 8, 9, 49, 25}, + new int[] {8, 19, 1, 6, 38, 24, 13, 38, 37}, + "LRLRLLRLR")); + } + + @Test + public void test8() { + assertEquals( + Arrays.asList(35), + solution1.survivedRobotsHealths( + new int[] {22, 19, 2, 43, 15, 34, 42, 1, 23, 31, 37, 35, 16, 36, 10}, + new int[] {8, 26, 44, 35, 6, 33, 46, 42, 21, 34, 13, 31, 30, 12, 39}, + "RRRRLLLRLRRLLLL")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2760Test.java b/src/test/java/com/fishercoder/thirdthousand/_2760Test.java new file mode 100644 index 0000000000..a0527dca1c --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2760Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2760; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2760Test { + private _2760.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2760.Solution1(); + } + + @Test + public void test1() { + assertEquals(0, solution1.longestAlternatingSubarray(new int[] {4}, 1)); + } + + @Test + public void test2() { + assertEquals(1, solution1.longestAlternatingSubarray(new int[] {1, 2}, 2)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2765Test.java b/src/test/java/com/fishercoder/thirdthousand/_2765Test.java new file mode 100644 index 0000000000..d043f75c36 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2765Test.java @@ -0,0 +1,36 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2765; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2765Test { + private _2765.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2765.Solution1(); + } + + @Test + public void test1() { + assertEquals(4, solution1.alternatingSubarray(new int[] {2, 3, 4, 3, 4})); + } + + @Test + public void test2() { + assertEquals(2, solution1.alternatingSubarray(new int[] {4, 5, 6})); + } + + @Test + public void test3() { + assertEquals(4, solution1.alternatingSubarray(new int[] {31, 32, 31, 32, 33})); + } + + @Test + public void test4() { + assertEquals(3, solution1.alternatingSubarray(new int[] {13, 14, 15, 14})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2788Test.java b/src/test/java/com/fishercoder/thirdthousand/_2788Test.java new file mode 100644 index 0000000000..c0bd547ff7 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2788Test.java @@ -0,0 +1,25 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2788; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2788Test { + private _2788.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2788.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("one", "two", "three", "four", "five", "six"), + solution1.splitWordsBySeparator( + Arrays.asList("one.two.three", "four.five", "six"), '.')); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2839Test.java b/src/test/java/com/fishercoder/thirdthousand/_2839Test.java new file mode 100644 index 0000000000..e7e8d4d68d --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2839Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.solutions.thirdthousand._2839; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2839Test { + private _2839.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2839.Solution1(); + } + + @Test + public void test1() { + assertTrue(solution1.canBeEqual("bnxw", "bwxn")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2843Test.java b/src/test/java/com/fishercoder/thirdthousand/_2843Test.java new file mode 100644 index 0000000000..65332de126 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2843Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2843; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2843Test { + private _2843.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2843.Solution1(); + } + + @Test + public void test1() { + assertEquals(9, solution1.countSymmetricIntegers(1, 100)); + } + + @Test + public void test2() { + assertEquals(9, solution1.countSymmetricIntegers(10, 100)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2848Test.java b/src/test/java/com/fishercoder/thirdthousand/_2848Test.java new file mode 100644 index 0000000000..50b8ece8b5 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2848Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2848; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2848Test { + private _2848.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2848.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 7, + solution1.numberOfPoints( + Arrays.asList( + Arrays.asList(3, 6), Arrays.asList(1, 5), Arrays.asList(4, 7)))); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2855Test.java b/src/test/java/com/fishercoder/thirdthousand/_2855Test.java new file mode 100644 index 0000000000..cc7dda34dd --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2855Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2855; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2855Test { + private _2855.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2855.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.minimumRightShifts(Arrays.asList(3, 4, 5, 1, 2))); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2900Test.java b/src/test/java/com/fishercoder/thirdthousand/_2900Test.java new file mode 100644 index 0000000000..5b9553bf79 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2900Test.java @@ -0,0 +1,29 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2900; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2900Test { + private _2900.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2900.Solution1(); + } + + @Test + public void test1() { + assertEquals( + Arrays.asList("s", "l", "r", "ypp", "ev", "fv", "qzk", "xlr", "w", "v"), + solution1.getLongestSubsequence( + new String[] { + "s", "l", "djl", "euy", "r", "lur", "u", "ypp", "ev", "fv", "we", "qzk", + "q", "xlr", "w", "wc", "a", "sd", "o", "x", "v" + }, + new int[] {0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2913Test.java b/src/test/java/com/fishercoder/thirdthousand/_2913Test.java new file mode 100644 index 0000000000..5fb8f6fd46 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2913Test.java @@ -0,0 +1,22 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2913; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2913Test { + private _2913.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2913.Solution1(); + } + + @Test + public void test1() { + assertEquals(15, solution1.sumCounts(Arrays.asList(1, 2, 1))); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2917Test.java b/src/test/java/com/fishercoder/thirdthousand/_2917Test.java new file mode 100644 index 0000000000..8d2047091e --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2917Test.java @@ -0,0 +1,21 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2917; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2917Test { + private _2917.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2917.Solution1(); + } + + @Test + public void test1() { + assertEquals(9, solution1.findKOr(new int[] {7, 12, 9, 8, 9, 15}, 4)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2928Test.java b/src/test/java/com/fishercoder/thirdthousand/_2928Test.java new file mode 100644 index 0000000000..93f24d4e8e --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2928Test.java @@ -0,0 +1,26 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2928; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2928Test { + private _2928.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2928.Solution1(); + } + + @Test + public void test1() { + assertEquals(3, solution1.distributeCandies(5, 2)); + } + + @Test + public void test2() { + assertEquals(10, solution1.distributeCandies(3, 3)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2937Test.java b/src/test/java/com/fishercoder/thirdthousand/_2937Test.java new file mode 100644 index 0000000000..67f671c8b9 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2937Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2937; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2937Test { + private _2937.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2937.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.findMinimumOperations("abc", "abb", "ab")); + } + + @Test + public void test2() { + assertEquals(3, solution1.findMinimumOperations("a", "aabc", "a")); + } + + @Test + public void test3() { + assertEquals(7, solution1.findMinimumOperations("ca", "cccabb", "cb")); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2946Test.java b/src/test/java/com/fishercoder/thirdthousand/_2946Test.java new file mode 100644 index 0000000000..99121af38e --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2946Test.java @@ -0,0 +1,53 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions.thirdthousand._2946; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2946Test { + private _2946.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2946.Solution1(); + } + + @Test + public void test1() { + assertTrue( + solution1.areSimilar( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2,1,2],[5,5,5,5],[6,3,6,3]"), + 2)); + } + + @Test + public void test2() { + assertTrue( + solution1.areSimilar( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[3,10,3,10,3,10,3,10],[5,8,5,8,5,8,5,8],[3,9,3,9,3,9,3,9],[3,8,3,8,3,8,3,8],[2,3,2,3,2,3,2,3]"), + 2)); + } + + @Test + public void test3() { + assertTrue( + solution1.areSimilar( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[9,5,3,10],[4,7,10,7],[1,7,9,4],[8,8,1,6],[6,7,6,1],[3,1,1,8],[9,2,8,3],[1,9,7,6]"), + 4)); + } + + @Test + public void test4() { + assertTrue( + solution1.areSimilar( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[9,1,8,9,2,9,1,8,9,2],[10,2,7,8,9,10,2,7,8,9],[7,6,6,9,5,7,6,6,9,5]"), + 5)); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2970Test.java b/src/test/java/com/fishercoder/thirdthousand/_2970Test.java new file mode 100644 index 0000000000..c4090e68a1 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2970Test.java @@ -0,0 +1,31 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2970; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2970Test { + private _2970.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2970.Solution1(); + } + + @Test + public void test1() { + assertEquals(7, solution1.incremovableSubarrayCount(new int[] {6, 5, 7, 8})); + } + + @Test + public void test2() { + assertEquals(3, solution1.incremovableSubarrayCount(new int[] {8, 7, 6, 6})); + } + + @Test + public void test3() { + assertEquals(3, solution1.incremovableSubarrayCount(new int[] {8, 7, 6, 6})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2976Test.java b/src/test/java/com/fishercoder/thirdthousand/_2976Test.java new file mode 100644 index 0000000000..db693cb5ce --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2976Test.java @@ -0,0 +1,48 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2976; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2976Test { + private _2976.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2976.Solution1(); + } + + @Test + public void test1() { + assertEquals( + 28, + solution1.minimumCost( + "abcd", + "acbe", + new char[] {'a', 'b', 'c', 'c', 'e', 'd'}, + new char[] {'b', 'c', 'b', 'e', 'b', 'e'}, + new int[] {2, 5, 5, 1, 2, 20})); + } + + @Test + public void test2() { + assertEquals( + 12, + solution1.minimumCost( + "aaaa", + "bbbb", + new char[] {'a', 'c'}, + new char[] {'c', 'b'}, + new int[] {1, 2})); + } + + @Test + public void test3() { + assertEquals( + -1, + solution1.minimumCost( + "abcd", "abce", new char[] {'a'}, new char[] {'e'}, new int[] {10000})); + } +} diff --git a/src/test/java/com/fishercoder/thirdthousand/_2996Test.java b/src/test/java/com/fishercoder/thirdthousand/_2996Test.java new file mode 100644 index 0000000000..f453bb34b4 --- /dev/null +++ b/src/test/java/com/fishercoder/thirdthousand/_2996Test.java @@ -0,0 +1,37 @@ +package com.fishercoder.thirdthousand; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fishercoder.solutions.thirdthousand._2996; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class _2996Test { + private _2996.Solution1 solution1; + + @BeforeEach + public void setup() { + solution1 = new _2996.Solution1(); + } + + @Test + public void test1() { + assertEquals(6, solution1.missingInteger(new int[] {1, 2, 3, 2, 5})); + } + + @Test + public void test2() { + assertEquals(15, solution1.missingInteger(new int[] {3, 4, 5, 1, 12, 14, 13})); + } + + @Test + public void test3() { + assertEquals(38, solution1.missingInteger(new int[] {37, 1, 2, 9, 5, 8, 5, 2, 9, 4})); + } + + @Test + public void test4() { + assertEquals( + 95, solution1.missingInteger(new int[] {47, 48, 2, 6, 9, 5, 10, 5, 6, 7, 6, 9, 8})); + } +}