├── .gitattributes ├── .github ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE │ └── solve-new-leetcode-problem.md └── pull_request_template.md ├── .gitignore ├── LICENSE ├── P0001.apln ├── P0002.apln ├── P0006.apln ├── P0079.apln ├── P0136.apln ├── P0315.apln ├── P0566.apln ├── P0771.apln ├── P0938.apln ├── P1108.apln ├── P1221.apln ├── P1281.apln ├── P1313.apln ├── P1342.apln ├── P1365.apln ├── P1389.apln ├── P1431.apln ├── P1470.apln ├── P1480.apln ├── P1486.apln ├── P1512.apln ├── P1528.apln ├── P1581.apln ├── P1603.apln ├── P1672.apln ├── P1678.apln ├── P1693.apln ├── P1720.apln ├── P1741.apln ├── P1773.apln ├── P1791.apln ├── P1795.apln ├── P1832.apln ├── P1880.apln ├── P1920.apln ├── P1929.apln ├── P2011.apln ├── P2114.apln ├── P2160.apln └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | *.apl? linguist-language=APL 2 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to APL LeetCode 2 | 3 | Everyone is welcome to contribute their own solutions to the LeetCode problems that are solved in this repository. 4 | 5 | If you wish to add some of your own solutions to the problems that have already been solved: 6 | 1. fork the repository; 7 | 2. edit the relevant `.apln` files; 8 | 3. make a pull request with your added solutions. 9 | 10 | If you have solutions to problems that haven't been solved yet, please open an issue to let us know what problem(s) you would like to see solved. 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/solve-new-leetcode-problem.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Solve new LeetCode problem 3 | about: Suggests we solve a new LeetCode problem 4 | title: Solve LeetCode problem [PXXXX] 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | I would like to request to see the following problem solved: 11 | 12 | - **Name**: _problem name here_ 13 | - **ID**: _problem ID here_ 14 | - **Link**: _url to the problem here_ 15 | 16 | My proposed solution(s): 17 | 18 | ```apl 19 | Sol ← {} ⍝ your proposed solution here, if you have any. 20 | ``` 21 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | Before opening a pull request to add your solutions to this repository, 2 | please **go over the following checklist**: 3 | 4 | - [ ] I tested my solutions with the respective test cases from the LeetCode.com website; 5 | - [ ] I added a couple of comments with an overview of how my solutions work; 6 | - [ ] I included my GitHub nickname (optional); 7 | - [ ] My solutions were added at the _bottom_ of the solutions that already existed; 8 | - [ ] I added my solutions to the runtime comparison tradfns, when such tradfns were present. 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | thumbnails/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Rodrigo Girão Serrão 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /P0001.apln: -------------------------------------------------------------------------------- 1 | :Namespace P0001 2 | ⍝ LeetCode problem #1 3 | ⍝ Two Sum, https://leetcode.com/problems/two-sum/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Use the outer product to brute-force all possible pairs. 7 | Sol ← {⊃⍸(∘.≠⍨⍳≢⍵)∧⍺=∘.+⍨⍵} 8 | 9 | SolEfficient ← { 10 | ⍝ O(n log n) solution 11 | ⍝ Sorts the original vector, then look at the first and last items of the vector. 12 | ⍝ Keep trimming the vector while the sum doesn't match the target. 13 | FT ← { 14 | s ← (⊃⍵)+⊃⌽⍵ 15 | s=⍺:(⊃⌽⍵)(⊃⍵) 16 | s>⍺:⍺ ∇ ¯1↓⍵ 17 | s<⍺:⍺ ∇ 1↓⍵ 18 | } 19 | nums ← ⍺ FT{⍵[⍋⍵]}⍵ 20 | if ← ⊃⍸⍵=⊃nums 21 | if,⊃if~⍨⍸⍵=⊃⌽nums 22 | } 23 | :EndNamespace 24 | -------------------------------------------------------------------------------- /P0002.apln: -------------------------------------------------------------------------------- 1 | :Namespace P0002 2 | ⍝ LeetCode problem #2 3 | ⍝ Add Two Numbers, https://leetcode.com/problems/add-two-numbers/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← { 7 | n ← ⍺⌈⍥≢⍵ 8 | a ← n↑⍺ ⋄ w←n↑⍵ ⍝ Make sure both numbers have same number of digits. 9 | CU ← { ⍝ Update the carry of a temporary result 10 | ⍝ e.g. 10 9 8 → 0 10 8 0 → 0 0 9 0 0 11 | cs←0,⌊10÷⍨⍵ 12 | cs+10|⍵,0 13 | } 14 | ⍝ Do the carry updates and remove trailing 0s 15 | v⌿⍨~⌽∧⍀⌽0=v ← CU⍣n⊢a+w 16 | } 17 | 18 | ⍝ "Obvious" solution uses decode but is probably against spirit of the problem. 19 | SolDec ← ⌽⍤(10⊥⍣¯1+⍥(10∘⊥⌽)) 20 | :EndNamespace 21 | -------------------------------------------------------------------------------- /P0006.apln: -------------------------------------------------------------------------------- 1 | :Namespace P0006 2 | ⍝ LeetCode problem #6 3 | ⍝ Zigzag Conversion, https://leetcode.com/problems/zigzag-conversion/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Smarter solution. 7 | ⍝ ⍵ is the string, ⍺ is number of rows. 8 | Sol ← {⍵[⍋(≢s)⍴(⍳⍺),⌽1+⍳⍺-2]} 9 | 10 | S ← { 11 | ⍝ (s n) ← ⍵ 12 | ⍝ s is string, n is rows 13 | (s n) ← ⍵ 14 | g ← n-1 15 | mg ← ⌈(2×g)÷⍨¯1+≢s ⍝ number of groups that is enough 16 | bg ← (1+⍳g)∘.(,⍨)⍨(g×⍳mg) 17 | cs ← (≢s)↑(⊂0 0),,bg,(⊢/bg)∘.+(⊂¯1 1)×1+⍳g 18 | p ← ⍋⊣/↑cs 19 | s[p] 20 | } 21 | :EndNamespace 22 | -------------------------------------------------------------------------------- /P0079.apln: -------------------------------------------------------------------------------- 1 | :Namespace P0079 2 | ⍝ LeetCode problem #79 3 | ⍝ Word Search, https://leetcode.com/problems/word-search/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | SolRec ← { 7 | (brd wrd) ← ⍵ 8 | ''≡wrd: 1 9 | pos ← ⍸brd=⊃wrd 10 | ⍺ ← pos+⊂1 0 ⍝ "cheat" in the 1st call 11 | lgl ← pos⌿⍨1=+/↑|pos-⍺ 12 | ∨⌿{(⊂⍵)SolRec ((⎕UCS 0)@(⊂⍵)⊢brd)(1↓wrd)}¨lgl 13 | } 14 | 15 | Sol ← { 16 | (brd wrd) ← ⍵ 17 | paths ← ↑↑⊃(,[⍳2]∘.(,⍥⊆))⌿(⊂⍸)⍤2⊢wrd∘.=brd 18 | adj ← paths⌿⍨∧/1=+/|2-⌿[1]paths 19 | ∨⌿(∪≡⊢)⍤2⊢adj 20 | } 21 | :EndNamespace 22 | -------------------------------------------------------------------------------- /P0136.apln: -------------------------------------------------------------------------------- 1 | :Namespace P0136 2 | ⍝ LeetCode problem #136 3 | ⍝ Single Number, https://leetcode.com/problems/single-number/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ A more APL-y solution 7 | Sol ← {⍵~⍵⌿⍨~≠⍵} 8 | 9 | ⍝ An APL rendition of the intended solution 10 | SolXOR ← {2⊥≠/2⊥⍣¯1⊢⍺,⍵}/ 11 | :EndNamespace 12 | -------------------------------------------------------------------------------- /P0315.apln: -------------------------------------------------------------------------------- 1 | :Namespace P0315 2 | ⍝ LeetCode problem #315 3 | ⍝ Count of Smaller Numbers After Self, https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 4 | (⎕IO ⎕ML) ← 0 1 5 | ⍝ Follow-up to P#1365, https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/ 6 | 7 | ⍝ _Very_ similar to the solution to P1365, 8 | ⍝ except we use (∘.<⍨⍳≢⍵) to mask out the comparisons 9 | ⍝ that we do not care about. 10 | Sol ← {+/(∘.<⍨⍳≢⍵)∧∘.>⍨⍵} 11 | Sol2 ← (+/ ∘.<⍨∘⍳∘≢∧∘.>⍨) ⍝ Tacit 12 | :EndNamespace 13 | 14 | -------------------------------------------------------------------------------- /P0566.apln: -------------------------------------------------------------------------------- 1 | :Namespace P0566 2 | ⍝ LeetCode problem #566 3 | ⍝ Reshape the Matrix, https://leetcode.com/problems/reshape-the-matrix/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Check if the new shape holds the same number of elements, 7 | ⍝ and use the standard reshape (⍴) if so. 8 | Sol ← { 9 | (×/⍺)≠×/⍴⍵: ⍵ 10 | ⍺⍴⍵ 11 | } 12 | 13 | ⍝ == A couple of contributions by @abrudz == 14 | ⍝ A branch-less solution 15 | Sol2 ← {⍺⍴⍣((×/⍺)=×/⍴⍵)⊢⍵} 16 | ⍝ Try the reshape, then see if the ravels match 17 | Sol3 ← { 18 | ⍵≢⍥,r←⍺⍴⍵:⍵ 19 | r 20 | } 21 | ⍝ Based on if the ravels match, pick the result 22 | Sol4 ← ⍴(≢⍥,⊃,⍥⊂)⊢ 23 | :EndNamespace 24 | -------------------------------------------------------------------------------- /P0771.apln: -------------------------------------------------------------------------------- 1 | :Namespace P0771 2 | ⍝ LeetCode problem #771 3 | ⍝ Jewels and Stones, https://leetcode.com/problems/jewels-and-stones 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Sum after (atop) the membership, 7 | ⍝ expects the stones on the left and the jewels on the right. 8 | Sol ← (+/∊) 9 | :EndNamespace 10 | -------------------------------------------------------------------------------- /P0938.apln: -------------------------------------------------------------------------------- 1 | :Namespace P0938 2 | ⍝ LeetCode problem #938 3 | ⍝ Range Sum of BST, https://leetcode.com/problems/range-sum-of-bst/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ The low and high values are ⍺ and the BST is ⍵. 7 | ⍝ e.g. 7 15 Sol 10 5 15 3 7 ⍬ 18 8 | Sol ← {(l h) ← ⍺ ⋄ +⌿(⊢×l∘≤∧≤∘h=)∊⍵} 9 | :EndNamespace 10 | -------------------------------------------------------------------------------- /P1108.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1108 2 | ⍝ LeetCode problem #1108 3 | ⍝ Defanging an IP Address, https://leetcode.com/problems/defanging-an-ip-address 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ (Tacit) regex solution 7 | Sol ← '\.'⎕R'[.]' 8 | 9 | ⍝ (Tacit) split on '.', rejoin with '[.]' 10 | Sol2 ← ⊃((⊣,'[.]',⊢)/'.'∘≠⊆⊢) 11 | 12 | ⍝ (Tacit) put '[.]' "at" the positions with '.' and flatten 13 | Sol3 ← ∊(⊂'[.]')@('.'∘=) 14 | 15 | ⍝ (Dfn) trying to only work with flat arrays 16 | Sol4 ← { 17 | exp ← ⍵/⍨1+¯4×d←'.'=⍵ 18 | exp[∊(⍳3)∘.+(⍸d)+2×⍳3] ← 3/'[.]' 19 | exp 20 | } 21 | 22 | ⍝ Similar to Sol, the variant switches off PCRE 23 | ⍝ and activates a faster array-based system. By @abrudz. 24 | Sol5 ← '.'⎕R'[.]'⍠'Regex'0 25 | 26 | ⍝ From Vadim Tukaev on YouTube 27 | Sol6 ← { 28 | SPLIT ← ≠⊆⊢ 29 | JOIN ← 1↓∘,,⍤0 30 | ∊(⊂'[.]')∘JOIN '.'∘SPLIT ⍵ 31 | } 32 | 33 | ⍝ And a rework of Vadim's solution 34 | Sol7 ← {3↓∊(⊂'[.]'),¨'.'(≠⊆⊢)⍵} 35 | 36 | ∇ r ← RTC 37 | ⍝ Runtime comparison of the solutions. 38 | addr ← '127.0.158.1' 39 | :If 0=⎕NC'cmpx' 40 | 'cmpx'⎕CY'dfns' 41 | :EndIf 42 | r ← cmpx 'Sol addr' 'Sol2 addr' 'Sol3 addr' 'Sol4 addr' 'Sol5 addr' 'Sol6 addr' 'Sol7 addr' 43 | ∇ 44 | :EndNamespace 45 | 46 | -------------------------------------------------------------------------------- /P1221.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1221 2 | ⍝ LeetCode problem #1221 3 | ⍝ Split a String in Balanced Strings, https://leetcode.com/problems/split-a-string-in-balanced-strings/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Inspired by Iverson's APL code to check if a string has balanced parens. 7 | Sol1 ← {+⌿0=+⍀-⌿'RL'∘.=⍵} 8 | 9 | ⍝ Same method, different way 10 | Sol2 ← {+/0=+\(~-⊢)'R'=⍵} 11 | :EndNamespace 12 | -------------------------------------------------------------------------------- /P1281.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1281 2 | ⍝ LeetCode problem #1281 3 | ⍝ Subtract the Product and Sum of Digits of an Integer, https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← (×/-+/)⍤(10∘⊥⍣¯1) 7 | :EndNamespace 8 | -------------------------------------------------------------------------------- /P1313.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1313 2 | ⍝ LeetCode problem #1313 3 | ⍝ Decompress Run-Length Encoded List, https://leetcode.com/problems/decompress-run-length-encoded-list/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← {∊⍴/⍵⍴⍨2,⍨2÷⍨≢⍵} 7 | 8 | ⍝ By @abrudz 9 | Sol2 ← (∊⊢⍴/¨⍤⊂⍨≢⍴1 0⍨) 10 | 11 | ⍝ Also by @abrudz, avoiding explicit reshaping with stencil: 12 | Sol3 ← ∊(⍴/⍤⊢⌺(⍪2 2)) 13 | :EndNamespace 14 | -------------------------------------------------------------------------------- /P1342.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1342 2 | ⍝ LeetCode problem #1342 3 | ⍝ Number of Steps to Reduce a Number to Zero, https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | SolR ← { 7 | ⍝ Recursive solution. 8 | 0=⍵: 0 9 | 2|⍵: 1+∇⍵-1 10 | 1+∇⍵÷2 11 | } 12 | 13 | SolTR ← { 14 | ⍝ Tail-recursive solution. 15 | ⍝ Tail recursion is more memory-efficient. 16 | ⍺ ← 0 17 | 0=⍵: ⍺ 18 | 2|⍵: (⍺+1)∇⍵-1 19 | (⍺+1)∇⍵÷2 20 | } 21 | 22 | ⍝ Figure out the binary expansion of the argument: 23 | ⍝ The result is the length of the binary expansion 24 | ⍝ plus the number of 1s in there, minus 1. 25 | Sol ← (¯1++/+≢)∘(2∘⊥⍣¯1) 26 | 27 | ⍝ By Vadim Tukaev on YouTube, also recursive 28 | Sol2 ← { 29 | ⍵<5: 3⌊⍵ 30 | (1+2|⍵)+∇⌊⍵÷2 31 | } 32 | :EndNamespace 33 | -------------------------------------------------------------------------------- /P1365.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1365 2 | ⍝ LeetCode problem #1365 3 | ⍝ How Many Numbers Are Smaller Than the Current Number, https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Uses ∘.>⍨ to compare all numbers with themselves, 7 | ⍝ then uses +/ to sum along rows, i.e., to do the counting. 8 | Sol ← +/∘.>⍨ 9 | :EndNamespace 10 | -------------------------------------------------------------------------------- /P1389.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1389 2 | ⍝ LeetCode problem #1389 3 | ⍝ Create Target Array in the Given Order, https://leetcode.com/problems/create-target-array-in-the-given-order/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← { 7 | t ← ⍬ 8 | ⊃⌽⍺{t ⊢← (⍺↑t),⍵,⍺↓t}¨⍵ 9 | } 10 | 11 | Sol2 ← { 12 | ns←⍵ 13 | ns[⊃{⍺,⍨⍺(≤+⊢)⍵}/⍬,⍨⌽⍺]←⍵ 14 | ns 15 | } 16 | 17 | ⍝ The same idea as above, but eliminates an extra variable ns 18 | Sol3 ← { 19 | ⍵⌷⍨⊂⍋↓⍵,[0.5]⍨⊃{⍺,⍨⍺(≤+⊢)⍵}/⍬,⍨⌽⍺ 20 | } 21 | :EndNamespace 22 | -------------------------------------------------------------------------------- /P1431.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1431 2 | ⍝ LeetCode problem #1431 3 | ⍝ Kids With the Greatest Number of Candies, https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/ 4 | 5 | ⍝ Tacit solution that reads 6 | ⍝ “When is the maximum of the right less than or equal to the sum of the two arguments?” 7 | Sol ← (⌈/⍤⊢≤+) 8 | 9 | ⍝ Tacit solution, equivalent to Sol, but rearranging the maths makes it faster for large inputs. 10 | ⍝ max(kids)≤kids+extra ←→ max(kids)-extra≤kids ←→ kids≥max(kids)-extra. 11 | ⍝ Proposed by @abrudz. 12 | Sol2 ← (⊢≥⌈/⍤⊢-⊣) 13 | 14 | ∇ r ← RTC 15 | ⍝ Runtime comparison of the solutions 16 | :If 0=⎕NC'cmpx' 17 | 'cmpx'⎕CY'dfns' 18 | :EndIf 19 | extra ← 1 20 | kids ← 4 2 1 1 2 21 | r ← cmpx 'extra Sol kids' 'extra Sol2 kids' 22 | 23 | r ⍪← '-' 24 | 25 | extraL ← 500 26 | kidsL ← ?1000⍴1000 27 | r ← ↑r,⍥↓cmpx 'extraL Sol kidsL' 'extraL Sol2 kidsL' 28 | ∇ 29 | :EndNamespace 30 | -------------------------------------------------------------------------------- /P1470.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1470 2 | ⍝ LeetCode problem #1470 3 | ⍝ Shuffle the Array, https://leetcode.com/problems/shuffle-the-array/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← { 7 | ⍝ Reshape into two rows, then ravel the transpose. 8 | ,⍉2(2÷⍨≢⍵)⍴⍵ 9 | } 10 | 11 | ⍝ Tacit solution implementing same algorithm as above, only taking right argument. 12 | Sol2 ← (,∘⍉⊢⍴⍨2,(÷∘2)∘≢) 13 | ⍝ ... which can (and should) be simplified to... (@abrudz) 14 | Sol2_ ← (,∘⍉(2,2÷⍨≢)⍴⊢) 15 | 16 | ⍝ HOWEVER, for some reason I completely ignored the fact that we also got a left argument! 17 | 18 | ⍝ Amazing inner product by "aral", my jaw dropped with this one. 19 | Sol4 ← ⊃(↑,.,↓) 20 | 21 | ⍝ Another interesting solution with grade up (by Leandro). 22 | Sol5 ← {⍵[⍋⍺|⍳≢⍵]} 23 | 24 | ⍝⍝⍝ Some dyadic solutions by @abrudz: 25 | abrudzSol1 ← {,⍉2 ⍺⍴⍵} 26 | abrudzSol2 ← (,∘⍉⊢⍴⍨2,⊣) 27 | abrudzSol3 ← (,∘⍉(2,⊣)⍴⊢) 28 | ⍝ If we pack both arguments on the right as `(nums n)`, we can write a short tacit solution: 29 | abrudzSol4 ← ,∘⍉2@0⍴⊃ 30 | ⍝ `2@0` replaces `nums` with `2` giving `2 n` and this is then used to reshape `⊃(nums n)` i.e. `nums` 31 | :EndNamespace 32 | -------------------------------------------------------------------------------- /P1480.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1480 2 | ⍝ LeetCode problem #1480 3 | ⍝ Running Sum of 1d Array, https://leetcode.com/problems/running-sum-of-1d-array/ 4 | 5 | ⍝ In APL, generally there's more than one obvious way to do it... 6 | ⍝ Not this time. 7 | Solution ← +\ 8 | :EndNamespace 9 | -------------------------------------------------------------------------------- /P1486.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1486 2 | ⍝ LeetCode problem #1486 3 | ⍝ XOR Operation in an Array, https://leetcode.com/problems/xor-operation-in-an-array/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | _U_ ← {⍵⍵⍣¯1 ⍺⍺ ⍵⍵ ⍵} ⍝ Under operator; surgery _U_ anaesthesia. 7 | 8 | ⍝ Build the sequence, then do not-equals-reduction under conversion to binary. 9 | Sol ← {≠⌿_U_(2∘⊥⍣¯1) ⍺+2×⍳⍵} 10 | :EndNamespace 11 | -------------------------------------------------------------------------------- /P1512.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1512 2 | ⍝ LeetCode problem #1512 3 | ⍝ Number of Good Pairs, https://leetcode.com/problems/number-of-good-pairs/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← { 7 | ⍝ Use the outer product to compare all elements with themselves, 8 | ⍝ then sum all of the matches, subtract the irrelevant matches 9 | ⍝ that each element makes with itself and divide by two to account 10 | ⍝ for the duplication of matches with indices i,j and j,i 11 | 2÷⍨(≢⍵)-⍨+/,∘.=⍨⍵ 12 | } 13 | 14 | ⍝ Similar to `Sol`, but we actually use another outer product 15 | ⍝ to create a Boolean mask that tells me which matches I care about. 16 | Sol2 ← (+/∘,∘.=⍨×∘.<⍨∘⍳∘≢) 17 | 18 | ⍝ Group by unique element and using ! to count possible combinations (by Leandro): 19 | Sol3 ← +/{2!≢⍵}⌸ 20 | 21 | ⍝ Straightforward recursion, but not exactly array-oriented (by Simão). 22 | Sol4 ← { 23 | ⍝ If the input vector has 0 or 1 elements, there's no good pairs and we return 0. 24 | ⍝ Otherwise, we count how many good pairs we can do with the head of the input vector 25 | ⍝ and then recurse (∇) after dropping the first element of the vector. 26 | 1≥≢⍵: 0 27 | (∇1↓⍵) + +/(1↑⍵)=1↓⍵ 28 | } 29 | 30 | ⍝ By @abrudz as a comment on the YT video: https://www.youtube.com/watch?v=pa_06_1E3VU&list=PLgTqamKi1MS2b-aKabbnAsnTiQgJAbmnr 31 | Sol ← +/¯1(+/↓=↑)¨,\ 32 | ⍝ ,\ builds all the prefixes of the input vector, 33 | ⍝ ¯1(+/↓=↑)¨ goes through those prefixes, and compares (=) 34 | ⍝ the last item (¯1↑) with all other elements (¯1↓), adding up (+/) the matches. 35 | ⍝ We then sum everything with the outer +/. 36 | :EndNamespace 37 | -------------------------------------------------------------------------------- /P1528.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1528 2 | ⍝ LeetCode problem #1528 3 | ⍝ Shuffle String, https://leetcode.com/problems/shuffle-string/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ ⍺ is the character vector and ⍵ is the indices vector. 7 | Sol ← {⍺[⍋⍵]} 8 | :EndNamespace 9 | -------------------------------------------------------------------------------- /P1581.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1581 2 | ⍝ LeetCode problem #1581 3 | ⍝ Customer Who Visited but Did Not Make Any Transactions, https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← { 7 | ⍝ ⍺ is the visits matrix 8 | ⍝ ⍵ is the transations matrix 9 | {⍺,≢⍵}⌸(⊢/⍺)/⍨~(⊣/⍺)∊⍵[;1] 10 | } 11 | :EndNamespace 12 | -------------------------------------------------------------------------------- /P1603.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1603 2 | ⍝ LeetCode problem #1603 3 | ⍝ Design Parking System, https://leetcode.com/problems/design-parking-system/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | 7 | ⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ 8 | ⍝⍝⍝ --- Simple solution keeping track of state in a variable that is passed around. --- ⍝⍝⍝ 9 | ⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ 10 | 11 | ParkingSystem ← ⊢ 12 | 13 | AddCar ← { 14 | newps ← ⍺-⍵=1+⍳3 15 | (newps⌈0) (~¯1∊newps) 16 | } 17 | 18 | 19 | ⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ 20 | ⍝⍝⍝ --- Solution using a namespace as a container for the state and the `AddCar` function. --- ⍝⍝⍝ 21 | ⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ 22 | 23 | NewParkingSystem ← { 24 | ⍝ Creates a new parking system with the given parking spots. 25 | cPS ← ⎕NS PSNamespace 26 | cPS.ps ← ⍵ 27 | cPS 28 | } 29 | 30 | :Namespace PSNamespace 31 | ⍝ Namespace containing the parking system state and the function to add a car. 32 | 33 | ps ← 0 0 0 34 | 35 | AddCar ← { 36 | newps ← ps-⍵=1+⍳3 37 | ps ⊢← newps⌈0 38 | ~¯1∊newps 39 | } 40 | :EndNamespace 41 | 42 | 43 | ⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ 44 | ⍝⍝⍝ --- Solution using an actual class to represent the parking system. --- ⍝⍝⍝ 45 | ⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ 46 | 47 | :Class PSClass 48 | ⍝ Class containing the state, a constructor to create new parking systems and a function 49 | ⍝ to check if a car can be parked. 50 | 51 | :Field Public ps ← 0 0 0 52 | 53 | ∇ NewParkingSystem spots 54 | :Implements Constructor 55 | :Access Public 56 | ps ← spots 57 | ∇ 58 | 59 | ∇ bool ← AddCar carType ;newps 60 | :Access Public 61 | newps ← ps-carType=1+⍳3 62 | ps ← newps⌈0 63 | bool ← ~¯1∊newps 64 | ∇ 65 | :EndClass 66 | :EndNamespace 67 | -------------------------------------------------------------------------------- /P1672.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1672 2 | ⍝ LeetCode problem #1672 3 | ⍝ Richest Customer Wealth, https://leetcode.com/problems/richest-customer-wealth 4 | 5 | ⍝ Another straightforward solution 6 | Solution ← ⌈/+/ 7 | 8 | ⍝ With explicit mixing, if input is nested: 9 | Sol2 ← {⌈/+/↑} 10 | 11 | ⍝ Using each instead of mixing, by Vadim Tukaev: 12 | Sol3 ← ⌈/+/¨ 13 | :EndNamespace 14 | 15 | -------------------------------------------------------------------------------- /P1678.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1678 2 | ⍝ LeetCode problem #1678 3 | ⍝ Goal Parser Interpretation, https://leetcode.com/problems/goal-parser-interpretation/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Look for '()' to replace with 'o', 7 | ⍝ then remove remaining parens. 8 | Sol ← {'()'~⍨'○'@('()'∘⍷)⍵} 9 | 10 | ⍝ Regex-based approach: 11 | Sol2 ← ('\(\)' '\(al\)' ⎕R 'o' 'al') 12 | :EndNamespace 13 | -------------------------------------------------------------------------------- /P1693.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1693 2 | ⍝ LeetCode problem #1693 3 | ⍝ Daily Leads and Partners, https://leetcode.com/problems/daily-leads-and-partners/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Expression from the video: 7 | ⍝ (dates ,⍥⊂¨ makes) {(⊃⍺),⎕←≢∘∪¨⎕←↓⍉↑⍵}⌸ leads ,¨ partners 8 | Sol ← { 9 | ⍝ Assumes the data comes as an inverted table. 10 | (dates makes leads partners) ← ⍵ 11 | (dates,⍥⊂¨makes){(⊃⍺),≢∘∪¨↓⍉↑⍵}⌸leads,¨partners 12 | } 13 | :EndNamespace 14 | -------------------------------------------------------------------------------- /P1720.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1720 2 | ⍝ LeetCode problem #1720 3 | ⍝ Decode XORed Array, https://leetcode.com/problems/decode-xored-array/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ ⍺ is the first element of the decoded array 7 | ⍝ and ⍵ is the encoded result. 8 | Sol ← {2⊥≠\2⊥⍣¯1⍺,⍵} 9 | 10 | ⍝ Tacit translation, by @abrudz 11 | Sol2 ← 2⊥(≠\2⊥⍣¯1,) 12 | :EndNamespace 13 | -------------------------------------------------------------------------------- /P1741.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1741 2 | ⍝ LeetCode problem #1741 3 | ⍝ Find Total Time Spent by Each Employee, https://leetcode.com/problems/find-total-time-spent-by-each-employee 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← {⍵[;0 1]{(⌽⍺),|+⌿-/⍵}⌸⍵[;2 3]} 7 | ⍝ Assumes the data is in a matrix format: 8 | ⍝┌─┬─────┬──┬───┐ 9 | ⍝│1│11-28│4 │32 │ 10 | ⍝├─┼─────┼──┼───┤ 11 | ⍝│1│11-28│55│200│ 12 | ⍝├─┼─────┼──┼───┤ 13 | ⍝│1│12-03│1 │42 │ 14 | ⍝├─┼─────┼──┼───┤ 15 | ⍝│2│11-28│3 │33 │ 16 | ⍝├─┼─────┼──┼───┤ 17 | ⍝│2│12-09│47│74 │ 18 | ⍝└─┴─────┴──┴───┘ 19 | :EndNamespace 20 | -------------------------------------------------------------------------------- /P1773.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1773 2 | ⍝ LeetCode problem #1773 3 | ⍝ Count Items Matching a Rule, https://leetcode.com/problems/count-items-matching-a-rule/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | fields ← 'type' 'color' 'name' 7 | 8 | Sol ← { 9 | ⍝ ⍵ is the nested vector with items. 10 | ⍝ (key value) ← ⍺ is the query 11 | +⌿(⊂⊃⌽⍺)≡⍤0⊢(fields⍳⊂⊃⍺)⌷⍉↑⍵ 12 | } 13 | :EndNamespace 14 | -------------------------------------------------------------------------------- /P1791.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1791 2 | ⍝ LeetCode problem #1791 3 | ⍝ Find Center of Star Graph, https://leetcode.com/problems/find-center-of-star-graph/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Take the first two edges and find their intersection. 7 | Sol ← {⊃∩⌿2↑⍵} 8 | 9 | ⍝ Corresponding tacit solution. 10 | TSol ← ⊃(∩⌿2↑⊢) 11 | 12 | ⍝ Flatten the structure and use unique mask to filter the centre. 13 | ⍝ Works on Dyalog APL 18.0+ 14 | Sol2 ← {⊃w⌿⍨~≠w←∊⍵} 15 | :EndNamespace 16 | -------------------------------------------------------------------------------- /P1795.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1795 2 | ⍝ LeetCode problem #1795 3 | ⍝ Rearrange Products Table, https://leetcode.com/problems/rearrange-products-table/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← { 7 | ⍝ Takes the data as a matrix. 8 | ft ← ⊃⍪/⍵[;0]{⍺,'st1' 'st2' 'st3',⍪⍵}¨↓0 1↓⍵ 9 | (¯1≠⊢/ft)⌿ft 10 | } 11 | :EndNamespace 12 | -------------------------------------------------------------------------------- /P1832.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1832 2 | ⍝ LeetCode problem #1832 3 | ⍝ Check if the Sentence Is Pangram, https://leetcode.com/problems/check-if-the-sentence-is-pangram 4 | 5 | ⍝ Tacit solution that reads 6 | ⍝ “Are all the lowercase letters in the argument?” 7 | Sol ← (∧/(⎕C⎕A)∊⊢) 8 | 9 | ⍝ By @abrudz: This one is both shorter and handles mixed-case input 10 | Sol2 ← ∧/⎕A∊⍥⎕C⊢ 11 | 12 | ⍝ By Vadim Tukaev on YouTube 13 | Sol3 ← 26=≢∘∪ 14 | :EndNamespace 15 | -------------------------------------------------------------------------------- /P1880.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1880 2 | ⍝ LeetCode problem #1880 3 | ⍝ Check if Word Equals Summation of Two Words, https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← { 7 | nv ← (10⊥⎕A⍳1⎕C⊢) 8 | (nv ⍺)=+/nv¨⍵ 9 | } 10 | :EndNamespace 11 | -------------------------------------------------------------------------------- /P1920.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1920 2 | ⍝ LeetCode problem #1920 3 | ⍝ Build Array from Permutation, https://leetcode.com/problems/build-array-from-permutation/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Immediate solution 7 | Sol ← {⍵[⍵]} 8 | 9 | ⍝ code_report's solution from https://www.youtube.com/watch?v=ctbGMuakpHk 10 | Sol_cr ← ⊂⌷⊢ 11 | 12 | ⍝ Another point-free solution building off of "the chipmunk" ⊃¨⊂ 13 | Sol_chip ← ⊃¨∘⊂⍨ 14 | :EndNamespace 15 | -------------------------------------------------------------------------------- /P1929.apln: -------------------------------------------------------------------------------- 1 | :Namespace P1929 2 | ⍝ LeetCode problem #1929 3 | ⍝ Concatenation of Array, https://leetcode.com/problems/concatenation-of-array 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← ,⍨ 7 | :EndNamespace 8 | -------------------------------------------------------------------------------- /P2011.apln: -------------------------------------------------------------------------------- 1 | :Namespace P2011 2 | ⍝ LeetCode problem #2011 3 | ⍝ Final Value of Variable After Performing Operations, https://leetcode.com/problems/final-value-of-variable-after-performing-operations/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← {(≢⍵)-⍨2×+⌿'+'=1⌷⍉↑⍵} 7 | :EndNamespace 8 | -------------------------------------------------------------------------------- /P2114.apln: -------------------------------------------------------------------------------- 1 | :Namespace P2114 2 | ⍝ LeetCode problem #2114 3 | ⍝ Maximum Number of Words Found in Sentences, https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | ⍝ Use the "slice" idiom from the tooltip of "Partition" 7 | Sol ← ' '∘(⌈⌿(≢≠⊆⊢)¨) 8 | ⍝ Count the words by counting the spaces between the words 9 | Sol1 ← {⌈⌿1+(+/-{+/∧\⌽⍵})' '=↑⍵} 10 | ⍝ Same as above, but checking for spaces before mixing avoids creating trailing spaces 11 | Sol2 ← {1+⌈/+/↑' '=⍵} 12 | :EndNamespace 13 | -------------------------------------------------------------------------------- /P2160.apln: -------------------------------------------------------------------------------- 1 | :Namespace P2160 2 | ⍝ LeetCode problem #2160 3 | ⍝ Minimum Sum of Four Digit Number After Splitting Digits, https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/ 4 | (⎕IO ⎕ML) ← 0 1 5 | 6 | Sol ← {+⌿(2⌿10 1)×{⍵[⍋⍵]}10⊥⍣¯1⊢⍵} 7 | :EndNamespace 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # APL LeetCode 2 | 3 | ## Solving LeetCode problems in APL 4 | 5 | [LeetCode] is an online platform where you can solve many programming problems. 6 | This repository hosts a collection of solutions to those problems, written in [APL](https://apl.wiki). 7 | 8 | You can find these solutions explained in YouTube videos in [this][yt] “Solving LeetCode with APL” playlist. 9 | 10 | [leetcode]: https://leetcode.com 11 | [yt]: https://www.youtube.com/playlist?list=PLgTqamKi1MS2b-aKabbnAsnTiQgJAbmnr 12 | --------------------------------------------------------------------------------