Skip to content

Instantly share code, notes, and snippets.

@jbetz34
Created May 12, 2023 03:48
Show Gist options
  • Save jbetz34/8fcc428a98ab7d7f65af5446133e718b to your computer and use it in GitHub Desktop.
Save jbetz34/8fcc428a98ab7d7f65af5446133e718b to your computer and use it in GitHub Desktop.
Q solution to LeetCode question 59. can also be used for 54
// Define our helper functions
// rotates the box 90 degrees ccw
turn:{[box] flip reverse each box}
// returns indices of first available nulls in box (top to bottom)
fni:{[box] 1 (where i@)\ first where any each i:null box}
// fills first nulls in the box with first numbers in the line
ffn:{[box;line;indices] .[box; indices; :; count[indices]#line]}
// Solution - only one i have found so far
// can accept different inputs
// atomic - n by n matrix (n is the single number passed)
// list - n by m matrix (n,m are the first,second items of the list respectively)
func:{[x]
line:1+til (*/)2#x;
box:(2#x)#0N;
while[count line;
box:turn ffn[box;line] i:fni box;
line:(count i 1)_line
];
while[1<>.[box;0 0]; box:turn box];
:box
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment