The TransCompilator

HTML chunk · 50 solved programs

Chunk 09 · #0540–#0636

Full original English source and expected stdout for fifty solved programs. Prefer plain text? Read the Markdown equivalent.

#0540 · Single Element in a Sorted Array

Medium Array

The entries is a list of 1, 1, 2, 3, 3, 4, 4, 8, and 8. The low is 0. The high is the last position of entries.
Repeat while low is less than high.
The middle is (low plus high) divided by 2. If middle modulo 2 is 1, subtract 1 from middle. The here is item middle of entries. The next is item (middle plus 1) of entries. If here is next, set low to middle plus 2, otherwise set high to middle.

The answer is item low of entries. Show answer.

Expected stdout

2

Open in Playground Standalone Page

#0541 · Reverse String II

Easy String

The source is "abcdefg". The span is 2. Keep a shared letters as an empty list of text. The last is the last position of source.
For every spot from 0 to last.
The piece is the slice of source from spot to spot plus 1. Place piece into letters.

To mirror from left (a whole number) with right (a whole number).
Repeat while left is less than right.
swap item left of letters and item right of letters. Add 1 to left. Subtract 1 from right.

The start is 0.
Repeat while start is at most last.
The finish is start plus span minus 1. If finish is more than last, set finish to last. Mirror from start with finish. Increase start by span plus span.

The result is join letters with "". Show result.

Expected stdout

bacdfeg

Open in Playground Standalone Page

#0542 · 01 Matrix

Medium Array

The grid is a list of 0, 0, 0, 0, 1, 0, 1, 1, and 1. Keep a shared distance as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. Keep a shared count of 0.
For every cell from 0 to 8.
If item cell of grid is 0, Place 0 into distance, Place cell into queue, add 1 to count. If item cell of grid is 1, Place 1000000 into distance.

To relax with spot (a whole number) and here (a whole number). The offered is here plus 1. If item spot of distance is more than offered, set item spot of distance to offered, Place spot into queue, add 1 to count.

The head is 0.
Repeat while head is less than count.
The cell is item head of queue. Add 1 to head. The row is cell divided by 3. The column is cell modulo 3. The here is item cell of distance. If row is more than 0, relax with cell minus 3 and here. If row is less than 2, relax with cell plus 3 and here. If column is more than 0, relax with cell minus 1 and here. If column is less than 2, relax with cell plus 1 and here.

For each value in distance, show value.

Expected stdout

0
0
0
0
1
0
1
2
1

Open in Playground Standalone Page

#0543 · Diameter of Binary Tree

Easy Depth-First Search

Keep a shared values holding 1, 2, 3, 4, and 5. Keep a shared lefts holding 1, 3, -1, -1, and -1. Keep a shared rights holding 2, 4, -1, -1, and -1. Keep a shared heights as an empty list of whole numbers.
For every node from 0 to 4.
Place 0 into heights.

Keep a shared longest of 0.

To deepen from node (a whole number). If node is less than 0, return. The leftnode is item node of lefts. The rightnode is item node of rights. The leftheight is 0. The rightheight is 0. If leftnode is at least 0, deepen from leftnode. If leftnode is at least 0, set leftheight to item leftnode of heights. If rightnode is at least 0, deepen from rightnode. If rightnode is at least 0, set rightheight to item rightnode of heights. The through is leftheight plus rightheight. If through is more than longest, set longest to through. The taller is leftheight. If rightheight is more than taller, set taller to rightheight. Set item node of heights to taller plus 1.

Deepen from 0. Show longest.

Expected stdout

3

Open in Playground Standalone Page

#0546 · Remove Boxes

Hard Array

Keep a shared boxes holding 1, 3, 2, 2, 2, 3, 4, 3, and 1. The size is 9. Keep a shared span of size plus 1. Keep a shared blocksize of size times span. The capacity is (size plus 1) times blocksize. Keep a shared table as an empty list of whole numbers.
For every slot from 0 to capacity minus 1.
Place 0 into table.

Keep a shared best of 0. Keep a shared extra of 0.

To improve with start (a whole number) and finish (a whole number).
For every middle from start plus 1 to finish.
The firstbox is item start of boxes. The otherbox is item middle of boxes. If firstbox is otherbox, The gap is start plus 1, The prior is middle minus 1, The emptyspot is gap times blocksize plus prior times span, The rightspot is middle times blocksize plus finish times span plus extra plus 1, The leftpart is item emptyspot of table, The rightpart is item rightspot of table, The candidate is leftpart plus rightpart, If candidate is more than best, set best to candidate.

The states is size times blocksize. The spot is 0.
Repeat while spot is less than states.
The block is spot divided by blocksize. The start is size minus 1 minus block. The rest is spot modulo blocksize. The finish is rest divided by span. The carryindex is rest modulo span. The extra is span minus 1 minus carryindex. Set best to 0. The key is start times blocksize plus finish times span plus extra. Set item key of table to 0. If start is at most finish, The basekey is (start plus 1) times blocksize plus finish times span, The bonus is extra plus 1, The square is bonus times bonus, Set best to item basekey of table plus square, Improve with start and finish, Set item key of table to best. Add 1 to spot.

The answerkey is (size minus 1) times span. The answer is item answerkey of table. Show answer.

Expected stdout

23

Open in Playground Standalone Page

#0547 · Number of Provinces

Medium Depth-First Search

Keep a shared links holding 1, 1, 0, 1, 1, 0, 0, 0, and 1. Keep a shared visited as an empty list of whole numbers.
For every city from 0 to 2.
Place 0 into visited.

Keep a shared size of 3.

To walk from city (a whole number). If item city of visited is 1, return. Set item city of visited to 1.
For every next from 0 to 2.
The spot is city times size plus next. If item spot of links is 1, walk from next.

The provinces is 0.
For every city from 0 to 2.
If item city of visited is 0, add 1 to provinces. If item city of visited is 0, walk from city.

Show provinces.

Expected stdout

2

Open in Playground Standalone Page

#0551 · Student Attendance Record I

Easy String

The record is "PPALLP". The last is the last position of record. Start with absences of 0, a run of 0, and a longest of 0.
For every spot from 0 to last.
The mark is character spot of record. If mark is "A", add 1 to absences, set run to 0. If mark is "L", add 1 to run. If mark is not "L", set run to 0. If run is more than longest, set longest to run.

If absences is less than 2 and longest is less than 3, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0552 · Student Attendance Record II

Hard Dynamic Programming

The length is 2. Keep a shared ways holding 0, 0, 0, 0, 0, and 0. Keep a shared fresh holding 0, 0, 0, 0, 0, and 0. Set item 0 of ways to 1.
To spread from state (a whole number). The count is item state of ways. The absences is state divided by 3. The lates is state modulo 3. The base is absences times 3. The basecount is item base of fresh. Set item base of fresh to basecount plus count. If lates is less than 2, set item (base plus lates plus 1) of fresh to item (base plus lates plus 1) of fresh plus count. If absences is 0, set item 3 of fresh to item 3 of fresh plus count.

For every step from 1 to length.
For every spot from 0 to 5, set item spot of fresh to 0. For every state from 0 to 5, spread from state. For every state from 0 to 5, set item state of ways to item state of fresh.

The total is 0.
For each count in ways.
Add count to total.

Show total.

Expected stdout

8

Open in Playground Standalone Page

#0553 · Optimal Division

Medium Array

The numbers is a list of 1000, 100, 10, and 2. The last is the last position of numbers. The result is empty text. Place format whole number item 0 of numbers as text into result. If last is 1, Place "/" into result, Place format whole number item 1 of numbers as text into result. If last is more than 1, Place "/(" into result, Place format whole number item 1 of numbers as text into result. For every spot from 2 to last, Place "/" into result, Place format whole number item spot of numbers as text into result. If last is more than 1, Place ")" into result.

Show result.

Expected stdout

1000/(100/10/2)

Open in Playground Standalone Page

#0554 · Brick Wall

Medium Array

The bricks is a list of 1, 2, 2, 1, 3, 1, 2, 1, 3, 2, 2, 4, 3, 1, 2, 1, 3, 1, and 1. The offsets is a list of 0, 4, 7, 10, 12, and 15. The sizes is a list of 4, 3, 3, 2, 3, and 4. The joints is a list of 6 zeros.
For every edge from 1 to 5.
For every row from 0 to 5.
The base is item row of offsets. The size is item row of sizes. The total is 0.
For every step from 0 to size minus 1.
The brick is item (base plus step) of bricks. Add brick to total. If total is edge, set item edge of joints to item edge of joints plus 1.

The best is 0.
For every spot from 1 to 5.
If item spot of joints is more than best, set best to item spot of joints.

The answer is 6 minus best. Show answer.

Expected stdout

2

Open in Playground Standalone Page

#0556 · Next Greater Element III

Medium String

The value is 12. The digits is an empty list of whole numbers.
Repeat while value is more than 0.
The digit is value modulo 10. Place digit into digits. Set value to value divided by 10.

The final is the last position of digits. The middle is (final plus 1) divided by 2.
For every left from 0 to middle minus 1.
The right is final minus left. swap item left of digits and item right of digits.

The pivot is -1.
For every spot from 0 to final minus 1.
If item spot of digits is less than item (spot plus 1) of digits, set pivot to spot.

The valid is 0. If pivot is at least 0, set valid to 1. If pivot is less than 0, set pivot to 0. The chosen is pivot plus 1.
For every spot from pivot plus 1 to final.
If item spot of digits is more than item pivot of digits, set chosen to spot.

swap item pivot of digits and item chosen of digits. The middle is (pivot plus 1 plus final) divided by 2.
For every left from pivot plus 1 to middle.
The right is final minus (left minus pivot minus 1). If left is less than right, swap item left of digits and item right of digits.

The answer is 0.
For each digit in digits.
Set answer to answer times 10 plus digit.

If valid is 1, show answer, otherwise show -1.

Expected stdout

21

Open in Playground Standalone Page

#0557 · Reverse Words in a String III

Easy String

The sentence is "Let's take LeetCode contest". The last is the last position of sentence. Keep a shared word as empty text. Keep a shared flipped as empty text. The result is empty text.
To mirror from spot (a whole number). If spot is less than 0, return. Place character spot of word into flipped. Mirror from spot minus 1.

To settle. Set flipped to empty text. Mirror from the last position of word.

For every spot from 0 to last.
The piece is character spot of sentence. If piece is " ", settle, Place flipped into result, Place " " into result, set word to empty text. If piece is not " ", Place piece into word. If spot is last, settle, Place flipped into result.

Show result.

Expected stdout

s'teL ekat edoCteeL tsetnoc

Open in Playground Standalone Page

#0558 · Logical OR of Two Binary Grids Represented as Quad-Trees

Medium Tree

The firstgrid is a list of 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, and 0. The secondgrid is a list of 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, and 1. Keep a shared merged as an empty list of whole numbers.
For every spot from 0 to 15.
The firstbit is item spot of firstgrid. The secondbit is item spot of secondgrid. If firstbit is 1 or secondbit is 1, Place 1 into merged. If firstbit is 0 and secondbit is 0, Place 0 into merged.

Keep a shared isleaf as an empty list of whole numbers. Keep a shared leafvalue as an empty list of whole numbers. Keep a shared kids as an empty list of whole numbers. Keep a shared top of 0. Keep a shared leftedge of 0. Keep a shared width of 0. Keep a shared seed of 0. Keep a shared allsame of 0. Keep a shared made of 0.
To probe from down (a whole number) with across (a whole number). If down is at least top plus width, return. If across is at least leftedge plus width, probe from down plus 1 with leftedge, return. The spot is down times 4 plus across. If item spot of merged is not seed, set allsame to 0. Probe from down with across plus 1.

To mark from value (a whole number). The lastspot is the last position of isleaf. The spot is lastspot plus 1. Place 1 into isleaf. Place value into leafvalue. Place -1 into kids. Place -1 into kids. Place -1 into kids. Place -1 into kids. Set made to spot.

To square from down (a whole number) with across (a whole number). Set top to down. Set leftedge to across. Set width to 2. Set seed to item (down times 4 plus across) of merged. Set allsame to 1. Probe from down with across. If allsame is 1, mark from seed, return. The lastspot is the last position of isleaf. The node is lastspot plus 1. Place 0 into isleaf. Place 0 into leafvalue. Place -1 into kids. Place -1 into kids. Place -1 into kids. Place -1 into kids. Set seed to item (down times 4 plus across) of merged. Mark from seed. Set item (node times 4) of kids to made. Set seed to item (down times 4 plus across plus 1) of merged. Mark from seed. Set item (node times 4 plus 1) of kids to made. Set seed to item ((down plus 1) times 4 plus across) of merged. Mark from seed. Set item (node times 4 plus 2) of kids to made. Set seed to item ((down plus 1) times 4 plus across plus 1) of merged. Mark from seed. Set item (node times 4 plus 3) of kids to made. Set made to node.

To board from down (a whole number) with across (a whole number). Set top to down. Set leftedge to across. Set width to 4. Set seed to item (down times 4 plus across) of merged. Set allsame to 1. Probe from down with across. If allsame is 1, mark from seed, return. The lastspot is the last position of isleaf. The node is lastspot plus 1. Place 0 into isleaf. Place 0 into leafvalue. Place -1 into kids. Place -1 into kids. Place -1 into kids. Place -1 into kids. The middle is 2. Square from down with across. Set item (node times 4) of kids to made. Square from down with across plus middle. Set item (node times 4 plus 1) of kids to made. Square from down plus middle with across. Set item (node times 4 plus 2) of kids to made. Square from down plus middle with across plus middle. Set item (node times 4 plus 3) of kids to made. Set made to node.

Board from 0 with 0. The queue is an empty list of whole numbers. Place 0 into queue. The head is 0. The count is 1. The line is empty text. Place "[" into line.
Repeat while head is less than count.
The node is item head of queue. If head is more than 0, Place "," into line. Place "[" into line. Place format whole number item node of isleaf as text into line. Place "," into line. Place format whole number item node of leafvalue as text into line. Place "]" into line. If item node of isleaf is 0, Place item (node times 4) of kids into queue, add 1 to count. If item node of isleaf is 0, Place item (node times 4 plus 1) of kids into queue, add 1 to count. If item node of isleaf is 0, Place item (node times 4 plus 2) of kids into queue, add 1 to count. If item node of isleaf is 0, Place item (node times 4 plus 3) of kids into queue, add 1 to count. Add 1 to head.

Place "]" into line. Show line.

Expected stdout

[[0,0],[1,1],[1,1],[1,0],[1,1]]

Open in Playground Standalone Page

#0559 · Maximum Depth of N-ary Tree

Easy Depth-First Search

Keep a shared values holding 1, 3, 2, 4, 5, and 6. Keep a shared parents holding -1, 0, 0, 0, 1, and 1. Keep a shared bestdepth of 0.
To descend from node (a whole number) with level (a whole number). If level is more than bestdepth, set bestdepth to level. For every candidate from 0 to 5, if item candidate of parents is node, descend from candidate with level plus 1.

Descend from 0 with 1. Show bestdepth.

Expected stdout

3

Open in Playground Standalone Page

#0560 · Subarray Sum Equals K

Medium Array

The numbers is a list of 1, 1, and 1. The target is 2. The last is the last position of numbers. The total is 0.
For every start from 0 to last.
The sum is 0.
For every ending from start to last.
The value is item ending of numbers. Add value to sum. If sum is target, add 1 to total.

Show total.

Expected stdout

2

Open in Playground Standalone Page

#0561 · Array Partition

Easy Array

The numbers is a list of 1, 4, 3, and 2. Sort numbers. The last is the last position of numbers. The limit is last divided by 2. The total is 0.
For every pair from 0 to limit.
The spot is pair times 2. The value is item spot of numbers. Add value to total.

Show total.

Expected stdout

4

Open in Playground Standalone Page

#0563 · Binary Tree Tilt

Easy Depth-First Search

Keep a shared values holding 1, 2, and 3. Keep a shared lefts holding 1, -1, and -1. Keep a shared rights holding 2, -1, and -1. Keep a shared sums holding 0, 0, and 0. Keep a shared totaltilt of 0. Keep a shared carry of 0.
To weigh from node (a whole number). If node is less than 0, set carry to 0, return. Weigh from item node of lefts. Set item node of sums to carry. Weigh from item node of rights. The gap is item node of sums minus carry. If gap is less than 0, set gap to 0 minus gap. Add gap to totaltilt. Increase item node of sums by carry plus item node of values. Set carry to item node of sums.

Weigh from 0. Show totaltilt.

Expected stdout

1

Open in Playground Standalone Page

#0564 · Find the Closest Palindrome

Hard String

Keep a shared source as empty text. Place "123" into source. Keep a shared sourcevalue of 0. Keep a shared length of 0. Keep a shared prefix of 0. Keep a shared made of 0. Keep a shared back of 0. Keep a shared best of 0. Keep a shared bestgap of 0. Keep a shared chosen of 0.
To peel from value (a whole number). If value is 0, return. The digit is value modulo 10. Set back to back times 10 plus digit. Peel from value divided by 10.

To reflect from seed (a whole number) with size (a whole number). The local is seed. If size modulo 2 is 1, set local to local divided by 10. Set back to 0. Peel from local. The scale is 1. The limit is size divided by 2. For every step from 1 to limit, set scale to scale times 10. Set made to seed times scale plus back.

To score from value (a whole number). If value is sourcevalue, return. The diff is value minus sourcevalue. If diff is less than 0, set diff to 0 minus diff. If chosen is 0, set best to value, set bestgap to diff, set chosen to 1, return. If diff is less than bestgap, set best to value, set bestgap to diff, return. If diff is bestgap and value is less than best, set best to value.

The last is the last position of source. Set length to last plus 1. The middle is (length plus 1) divided by 2.
For every spot from 0 to middle minus 1.
The digit is the digit value of character spot of source. Set prefix to prefix times 10 plus digit.

For every spot from 0 to last.
The digit is the digit value of character spot of source. Set sourcevalue to sourcevalue times 10 plus digit.

Reflect from prefix minus 1 with length. Score from made. Reflect from prefix with length. Score from made. Reflect from prefix plus 1 with length. Score from made. The lower is 1. For every step from 1 to length minus 1, set lower to lower times 10. Subtract 1 from lower. Score from lower. The upper is 1. For every step from 1 to length, set upper to upper times 10. Add 1 to upper. Score from upper.

Show best.

Expected stdout

121

Open in Playground Standalone Page

#0565 · Array Nesting

Medium Array

Keep a shared links holding 5, 4, 0, 3, 1, 6, and 2. The last is the last position of links. Keep a shared seen holding 0, 0, 0, 0, 0, 0, and 0. Keep a shared best of 0.
To walk from node (a whole number) with depth (a whole number). If item node of seen is 1, return. Set item node of seen to 1. If depth is more than best, set best to depth. Walk from item node of links with depth plus 1.

For every start from 0 to last.
Walk from start with 1.

Show best.

Expected stdout

4

Open in Playground Standalone Page

#0566 · Reshape the Matrix

Easy Array

The matrix is a list of 1, 2, 3, and 4. The rows is 2. The columns is 2. The targetrows is 1. The targetcolumns is 4. The last is the last position of matrix. The total is last plus 1. If targetrows times targetcolumns is not total, set targetrows to rows, set targetcolumns to columns.

The line is empty text. Place "[" into line.
For every spot from 0 to last.
If spot is more than 0 and spot modulo targetcolumns is 0, Place "," into line. If spot modulo targetcolumns is 0, Place "[" into line. The value is item spot of matrix. Place format whole number value as text into line. If spot modulo targetcolumns is less than targetcolumns minus 1, Place "," into line. If spot modulo targetcolumns is targetcolumns minus 1, Place "]" into line.

Place "]" into line. Show line.

Expected stdout

[[1,2,3,4]]

Open in Playground Standalone Page

#0567 · Permutation in String

Medium String

The pattern is "ab". The source is "eidbaooo". The target is sort characters of pattern. The width is the number of characters in pattern. The last is the last position of source. The final is last minus width plus 1. The found is 0.
For every start from 0 to final.
The finish is start plus width. The piece is the slice of source from start to finish. The ordered is sort characters of piece. If ordered matches target, set found to 1. If found is 1, stop.

If found is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0572 · Subtree of Another Tree

Easy Depth-First Search

Keep a shared values holding 3, 4, 5, 1, and 2. Keep a shared lefts holding 1, 3, -1, -1, and -1. Keep a shared rights holding 2, 4, -1, -1, and -1. Keep a shared targetvalues holding 4, 1, and 2. Keep a shared targetlefts holding 1, -1, and -1. Keep a shared targetrights holding 2, -1, and -1. Keep a shared alike of 0. Keep a shared found of 0.
To compare from one (a whole number) with two (a whole number). If alike is 0, return. If one is less than 0 and two is less than 0, return. If one is less than 0, set alike to 0, return. If two is less than 0, set alike to 0, return. If item one of values is not item two of targetvalues, set alike to 0, return. Compare from item one of lefts with item two of targetlefts. Compare from item one of rights with item two of targetrights.

To seek from node (a whole number). If node is less than 0, return. Set alike to 1. Compare from node with 0. If alike is 1, set found to 1, return. Seek from item node of lefts. Seek from item node of rights.

Seek from 0. If found is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0575 · Distribute Candies

Easy Array

The candies is a list of 1, 1, 2, 2, 3, and 3. Sort candies. The last is the last position of candies. The kinds is 1.
For every spot from 1 to last.
If item spot of candies is not item (spot minus 1) of candies, add 1 to kinds.

The middle is (last plus 1) divided by 2. The answer is kinds. If middle is less than kinds, set answer to middle.

Show answer.

Expected stdout

3

Open in Playground Standalone Page

#0576 · Out of Boundary Paths

Medium Dynamic Programming

Keep a shared height of 0. Keep a shared width of 0. Keep a shared budget of 0. Keep a shared startrow of 0. Keep a shared startcolumn of 0. Keep a shared modulus of 0. Keep a shared cells of 0. Keep a shared counts as an empty list of whole numbers. Keep a shared next as an empty list of whole numbers. Keep a shared escapes of 0. Set height to 2. Set width to 2. Set budget to 2. Set startrow to 0. Set startcolumn to 0. Set modulus to 1000000007. Set cells to height times width.
For every cell from 0 to cells minus 1.
Place 0 into counts. Place 0 into next.

The start is startrow times width plus startcolumn. Set item start of counts to 1.

To wipe from spot (a whole number). If spot is at least cells, return. Set item spot of next to 0. Wipe from spot plus 1.

To spill from spot (a whole number). The row is spot divided by width. The column is spot modulo width. The weight is item spot of counts. If row is 0, set escapes to escapes plus weight. If row is 0, set escapes to escapes modulo modulus. If row is more than 0, set item (spot minus width) of next to item (spot minus width) of next plus weight. If row is height minus 1, set escapes to escapes plus weight. If row is height minus 1, set escapes to escapes modulo modulus. If row is less than height minus 1, set item (spot plus width) of next to item (spot plus width) of next plus weight. If column is 0, set escapes to escapes plus weight. If column is 0, set escapes to escapes modulo modulus. If column is more than 0, set item (spot minus 1) of next to item (spot minus 1) of next plus weight. If column is width minus 1, set escapes to escapes plus weight. If column is width minus 1, set escapes to escapes modulo modulus. If column is less than width minus 1, set item (spot plus 1) of next to item (spot plus 1) of next plus weight.

To sweep from spot (a whole number). If spot is at least cells, return. If item spot of counts is more than 0, spill from spot. Sweep from spot plus 1.

To save from spot (a whole number). If spot is at least cells, return. Set item spot of counts to item spot of next. Save from spot plus 1.

To travel from move (a whole number). If move is more than budget, return. Wipe from 0. Sweep from 0. Save from 0. Travel from move plus 1.

Travel from 1. Show escapes.

Expected stdout

6

Open in Playground Standalone Page

#0581 · Shortest Unsorted Continuous Subarray

Medium Array

The numbers is a list of 2, 6, 4, 8, 10, 9, and 15. The ordered is an empty list of whole numbers.
For each value in numbers.
Place value into ordered.

Sort ordered. The first is -1. The farthest is -1.
For every spot from 0 to the last position of numbers.
If item spot of numbers is not item spot of ordered.
If first is less than 0, set first to spot. Set farthest to spot.

If first is less than 0, show 0, otherwise show farthest minus first plus 1.

Expected stdout

5

Open in Playground Standalone Page

#0583 · Delete Operation for Two Strings

Medium String

The first is "sea". The second is "eat". The firstlength is the number of characters in first. The secondlength is the number of characters in second. The width is secondlength plus 1. The height is firstlength plus 1. The cells is height times width. The chart is a list of 16 zeros.
For every row from 1 to firstlength.
For every column from 1 to secondlength.
The above is row minus 1. The before is column minus 1. The letter is the slice of first from above to above plus 1. The other is the slice of second from before to before plus 1. The spot is row times width plus column. The corner is above times width plus before. If letter matches other, set item spot of chart to item corner of chart plus 1.
If letter does not match other.
The up is above times width plus column. The left is row times width plus before. The best is item up of chart. If item left of chart is more than best, set best to item left of chart. Set item spot of chart to best.

The common is firstlength times width plus secondlength. The shared is item common of chart. The answer is firstlength plus secondlength minus 2 times shared. Show answer.

Expected stdout

2

Open in Playground Standalone Page

#0587 · Erect the Fence

Hard Array

Keep a shared xvalues as an empty list of whole numbers. Keep a shared yvalues as an empty list of whole numbers. The posts is a list of 1, 1, 2, 2, 2, 0, 2, 4, 3, 3, 4, and 2.
For every spot from 0 to the last position of posts.
If spot modulo 2 is 0, Place item spot of posts into xvalues. If spot modulo 2 is 1, Place item spot of posts into yvalues.

Keep a shared chosen of 0. Keep a shared lowerx as an empty list of whole numbers. Keep a shared lowery as an empty list of whole numbers. Keep a shared upperx as an empty list of whole numbers. Keep a shared uppery as an empty list of whole numbers. Keep a shared resultx as an empty list of whole numbers. Keep a shared resulty as an empty list of whole numbers. Keep a shared startx of 0. Keep a shared starty of 0. Keep a shared endx of 0. Keep a shared endy of 0. Keep a shared repeated of 0. Keep a shared checkx of 0. Keep a shared checky of 0.

To pick from cursor (a whole number) with best (a whole number). If cursor is more than the last position of xvalues, set chosen to best, return. The herex is item cursor of xvalues. The herey is item cursor of yvalues. The bestx is item best of xvalues. The besty is item best of yvalues. If herex is less than bestx, set best to cursor. If herex is bestx and herey is less than besty, set best to cursor. Pick from cursor plus 1 with best.

To trimleft. The size is the number of items in lowerx. If size is less than 3, return. The last is size minus 1. The middle is size minus 2. The first is size minus 3. The run is item middle of lowerx minus item first of lowerx. The rise is item last of lowery minus item first of lowery. The drop is item middle of lowery minus item first of lowery. The span is item last of lowerx minus item first of lowerx. The turn is run times rise minus drop times span.
If turn is at most 0.
Remove item middle of lowerx. Remove item middle of lowery. Trimleft. Return.

To leftscan from cursor (a whole number). If cursor is more than the last position of xvalues, return. Place item cursor of xvalues into lowerx. Place item cursor of yvalues into lowery. Trimleft. Leftscan from cursor plus 1.

To trimright. The size is the number of items in upperx. If size is less than 3, return. The last is size minus 1. The middle is size minus 2. The first is size minus 3. The run is item middle of upperx minus item first of upperx. The rise is item last of uppery minus item first of uppery. The drop is item middle of uppery minus item first of uppery. The span is item last of upperx minus item first of upperx. The turn is run times rise minus drop times span.
If turn is at most 0.
Remove item middle of upperx. Remove item middle of uppery. Trimright. Return.

To rightscan from cursor (a whole number). If cursor is less than 0, return. Place item cursor of xvalues into upperx. Place item cursor of yvalues into uppery. Trimright. Rightscan from cursor minus 1.

To verify from spot (a whole number). If spot is more than the last position of resultx, return. If item spot of resultx is checkx and item spot of resulty is checky, set repeated to 1, return. Verify from spot plus 1.

To collect from cursor (a whole number). If cursor is more than the last position of xvalues, return. The pointx is item cursor of xvalues. The pointy is item cursor of yvalues. The along is (pointx minus startx) times (endy minus starty) minus (pointy minus starty) times (endx minus startx). The lowx is startx. If endx is less than lowx, set lowx to endx. The highx is startx. If endx is more than highx, set highx to endx. The lowy is starty. If endy is less than lowy, set lowy to endy. The highy is starty. If endy is more than highy, set highy to endy. The outset is 0. If pointx is startx and pointy is starty, set outset to 1. If pointx is endx and pointy is endy, set outset to 1. Set repeated to 0. Set checkx to pointx. Set checky to pointy. Verify from 0. If along is 0 and outset is 0 and repeated is 0 and pointx is at least lowx and pointx is at most highx and pointy is at least lowy and pointy is at most highy, Place pointx into resultx, Place pointy into resulty. Collect from cursor plus 1.

The lastpost is the last position of xvalues.
For every outer from 0 to lastpost minus 1.
Set chosen to outer. Pick from outer plus 1 with outer. The pickindex is chosen. The swapx is item outer of xvalues. The swapy is item outer of yvalues. Set item outer of xvalues to item pickindex of xvalues. Set item outer of yvalues to item pickindex of yvalues. Set item pickindex of xvalues to swapx. Set item pickindex of yvalues to swapy.

Leftscan from 0. Rightscan from the last position of xvalues. The fencex is an empty list of whole numbers. The fencey is an empty list of whole numbers. The lowersize is the number of items in lowerx.
For every spot from 0 to lowersize minus 2.
Place item spot of lowerx into fencex. Place item spot of lowery into fencey.

The uppersize is the number of items in upperx.
For every spot from 0 to uppersize minus 2.
Place item spot of upperx into fencex. Place item spot of uppery into fencey.

The hullsize is the number of items in fencex.
For every edge from 0 to hullsize minus 1.
The nextedge is edge plus 1. If nextedge is hullsize, set nextedge to 0. Set startx to item edge of fencex. Set starty to item edge of fencey. Set endx to item nextedge of fencex. Set endy to item nextedge of fencey. Place startx into resultx. Place starty into resulty. Collect from 0.

The line is empty text. Place "[" into line.
For every spot from 0 to the last position of resultx.
If spot is more than 0, Place "," into line. Place "[" into line. The pointx is format whole number item spot of resultx as text. Place pointx into line. Place "," into line. The pointy is format whole number item spot of resulty as text. Place pointy into line. Place "]" into line.

Place "]" into line. Show line.

Expected stdout

[[1,1],[2,0],[4,2],[3,3],[2,4]]

Open in Playground Standalone Page

#0589 · N-ary Tree Preorder Traversal

Easy Depth-First Search

Keep a shared values holding 1, 3, 2, 4, 5, and 6. Keep a shared firstchild holding 1, 4, -1, -1, -1, and -1. Keep a shared nextsibling holding -1, 2, 3, -1, 5, and -1. Keep a shared order as an empty list of whole numbers.

To gather from node (a whole number). If node is less than 0, return. Place item node of values into order. The child is item node of firstchild.
Repeat while child is at least 0.
Gather from child. Set child to item child of nextsibling.

Gather from 0. For each value in order, show value.

Expected stdout

1
3
5
6
2
4

Open in Playground Standalone Page

#0590 · N-ary Tree Postorder Traversal

Easy Depth-First Search

Keep a shared values holding 1, 3, 2, 4, 5, and 6. Keep a shared firstchild holding 1, 4, -1, -1, -1, and -1. Keep a shared nextsibling holding -1, 2, 3, -1, 5, and -1. Keep a shared order as an empty list of whole numbers.

To gather from node (a whole number). If node is less than 0, return. Visit from item node of firstchild with node.

To visit from child (a whole number) with parent (a whole number). If child is less than 0, Place item parent of values into order, return. Gather from child. Visit from item child of nextsibling with parent.

Gather from 0. For each value in order, show value.

Expected stdout

5
6
3
2
4
1

Open in Playground Standalone Page

#0591 · Tag Validator

Hard String

Keep a shared code as empty text. Place "<DIV>This is the first line <![CDATA[<div>]]></DIV>" into code. Keep a shared spot of 0. Keep a shared ok of 0. Keep a shared name as empty text. Keep a shared found of 0. Keep a shared tags as an empty list of text. Set ok to 1.

To findmark from cursor (a whole number). Set found to 0 minus 1.
Repeat while cursor plus 2 is at most the last position of code.
The piece is the slice of code from cursor to cursor plus 3. If piece matches "]]>", set found to cursor, stop. Add 1 to cursor.

To takecdata. If found is less than 0, set ok to 0, return. Set spot to found plus 3.

To scan from cursor (a whole number). If cursor is more than the last position of code, set spot to cursor, return. The here is cursor. If character here of code is ">", set spot to cursor, return. Place character here of code into name. Scan from cursor plus 1.

To approve. The size is the number of characters in name. If size is less than 1, set ok to 0. If size is more than 9, set ok to 0.
For every index from 0 to size minus 1.
The letter is the slice of name from index to index plus 1. If letter comes before "A", set ok to 0. If "Z" comes before letter, set ok to 0.

To closetag. Add 2 to spot. Set name to empty text. Scan from spot. If spot is more than the last position of code, set ok to 0, return. Add 1 to spot. Approve. If ok is 0, return. The depth is the number of items in tags. If depth is 0, set ok to 0, return. The top is item (depth minus 1) of tags. If name does not match top, set ok to 0, return. Remove the last item from tags.

To opentag. The start is the slice of code from spot to spot plus 1. The angle is 0. If start matches "<", set angle to 1. If angle is 0, set ok to 0, return. Add 1 to spot. Set name to empty text. Scan from spot. If spot is more than the last position of code, set ok to 0, return. Approve. If ok is 0, return. Add 1 to spot. The copy is a copy of name. Place copy into tags. Content.

To content.
Repeat while spot is at most the last position of code and ok is 1.
The here is spot. The finish is the last position of code. The marker is the slice of code from here to here plus 1. The next is here plus 1. If next is more than finish, set next to finish. The near is next plus 1. If near is more than finish plus 1, set near to finish plus 1. The follower is the slice of code from next to near. The wide is here plus 9. If wide is more than finish, set wide to finish plus 1. The head is the slice of code from here to wide. The cdata is 0. If head matches "<![CDATA[", set cdata to 1. If cdata is 1, findmark from here plus 9. If cdata is 1, takecdata. The isangle is 0. If marker matches "<", set isangle to 1. The isclose is 0. If follower matches "/", set isclose to 1. If cdata is 0 and isangle is 0, add 1 to spot. If cdata is 0 and isangle is 1 and isclose is 1, closetag. If cdata is 0 and isangle is 1 and isclose is 1, stop. If cdata is 0 and isangle is 1 and isclose is 0, opentag.

The size is the number of characters in code. If size is less than 1, set ok to 0. If size is more than 0 and ok is 1, opentag. If spot is not size, set ok to 0. The depth is the number of items in tags. If ok is 1 and depth is 0, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0592 · Fraction Addition and Subtraction

Medium String

Keep a shared source as empty text. Place "-1/2+1/2+1/3" into source. Keep a shared spot of 0. Keep a shared sign of 0. Keep a shared top of 0. Keep a shared bottom of 0. Keep a shared number of 0. Keep a shared numerator of 0. Keep a shared denominator of 1.

To readnumber from cursor (a whole number). If cursor is more than the last position of source, set spot to cursor, return. If character cursor of source is not between "0" and "9", set spot to cursor, return. The digit is the digit value of character cursor of source. Set number to number times 10 plus digit. Add 1 to cursor. Readnumber from cursor.

To reduce from left (a whole number) with right (a whole number). If left is less than 0, set left to 0 minus left. If right is less than 0, set right to 0 minus right. If right is 0, set numerator to numerator divided by left, set denominator to denominator divided by left, return. The rest is left modulo right. Reduce from right with rest.

Repeat while spot is at most the last position of source.
The here is spot. Set sign to 1. If character here of source is "-", set sign to 0 minus 1, add 1 to spot. If character here of source is "+", add 1 to spot. Set number to 0. Readnumber from spot. Set top to number. Add 1 to spot. Set number to 0. Readnumber from spot. Set bottom to number. Set numerator to numerator times bottom plus sign times top times denominator. Set denominator to denominator times bottom. Reduce from numerator with denominator.

The line is format whole number numerator as text. Place "/" into line. Place format whole number denominator as text into line. Show line.

Expected stdout

1/3

Open in Playground Standalone Page

#0593 · Valid Square

Medium Math

The points is a list of 0, 0, 1, 1, 1, 0, 0, 1. The gaps is an empty list of whole numbers.
For every first from 0 to 3.
For every second from first plus 1 to 3.
The firstspot is first times 2. The secondspot is second times 2. The gapx is item firstspot of points minus item secondspot of points. The gapy is item (firstspot plus 1) of points minus item (secondspot plus 1) of points. The distance is gapx times gapx plus gapy times gapy. Place distance into gaps.

Sort gaps. The side is item 0 of gaps. The diagonal is item 4 of gaps. If side is more than 0 and item 1 of gaps is side and item 2 of gaps is side and item 3 of gaps is side and item 5 of gaps is diagonal and diagonal is side times 2, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0594 · Longest Harmonious Subsequence

Easy Array

The values is a list of 1, 3, 2, 2, 5, 2, 3, and 7. Sort values. The distinct is an empty list of whole numbers. The counts is an empty list of whole numbers.
For each value in values.
The size is the number of items in distinct. If size is 0, Place value into distinct, Place 1 into counts.
If size is more than 0.
The lastspot is size minus 1. If item lastspot of distinct is value, set item lastspot of counts to item lastspot of counts plus 1. If item lastspot of distinct is not value, Place value into distinct, Place 1 into counts.

The best is 0. The final is the last position of distinct.
For every spot from 0 to final minus 1.
The nextspot is spot plus 1.
If item nextspot of distinct is item spot of distinct plus 1.
The total is item spot of counts plus item nextspot of counts. If total is more than best, set best to total.

Show best.

Expected stdout

5

Open in Playground Standalone Page

#0598 · Range Addition II

Easy Array

The height is 3. The width is 3. The operations is a list of 2, 2, 3, 3. The pairs is the number of items in operations divided by 2. The rowlimit is height. The columnlimit is width.
For every pair from 0 to pairs minus 1.
The row is item (pair times 2) of operations. The column is item (pair times 2 plus 1) of operations. If row is less than rowlimit, set rowlimit to row. If column is less than columnlimit, set columnlimit to column.

The cells is rowlimit times columnlimit. Show cells.

Expected stdout

4

Open in Playground Standalone Page

#0599 · Minimum Index Sum of Two Lists

Easy Array

The names is a list of "Shogun", "Tapioca Express", "Burger King", and "KFC". The favorites is a list of "Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", and "Shogun". The best is 1000000.
For every first from 0 to the last position of names.
For every second from 0 to the last position of favorites.
If item first of names matches item second of favorites.
The total is first plus second. If total is less than best, set best to total.

The answers is an empty list of text.
For every first from 0 to the last position of names.
For every second from 0 to the last position of favorites.
If item first of names matches item second of favorites.
The total is first plus second. If total is best, Place item first of names into answers.

For each answer in answers, show answer.

Expected stdout

Shogun

Open in Playground Standalone Page

#0600 · Non-negative Integers without Consecutive Ones

Hard Dynamic Programming

The limit is 5. The ways is a list of 33 zeros. Set item 0 of ways to 1. Set item 1 of ways to 2.
For every spot from 2 to 32.
The prior is item (spot minus 1) of ways. The earlier is item (spot minus 2) of ways. Set item spot of ways to prior plus earlier.

The top is 0. The probe is limit.
Repeat while probe is more than 0.
Set probe to probe divided by 2. Add 1 to top.

The answer is 0. The before is 0. The blocked is 0.
For every step from 1 to top.
The position is top minus step. The raised is shift 1 left by position. The bit is bitwise and of limit and raised. If bit is more than 0, set answer to answer plus item position of ways. If bit is more than 0 and before is 1, set blocked to 1. If bit is more than 0, set before to 1. If bit is 0, set before to 0. If blocked is 1, stop.

If blocked is 0, add 1 to answer. Show answer.

Expected stdout

5

Open in Playground Standalone Page

#0605 · Can Place Flowers

Easy Array

The flowerbed is a list of 1, 0, 0, 0, and 1. The wanted is 1. The last is the last position of flowerbed. The planted is 0.
For every spot from 0 to last.
The here is item spot of flowerbed. The leftfree is 1. If spot is more than 0 and item (spot minus 1) of flowerbed is 1, set leftfree to 0. The rightfree is 1. If spot is less than last and item (spot plus 1) of flowerbed is 1, set rightfree to 0. If here is 0 and leftfree is 1 and rightfree is 1, set item spot of flowerbed to 1, add 1 to planted.

If planted is at least wanted, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0606 · Construct String from Binary Tree

Medium String

Keep a shared values holding 1, 2, 3, and 4. Keep a shared lefts holding 1, -1, -1, and -1. Keep a shared rights holding 2, 3, -1, and -1. Keep a shared built as empty text.
To cover from node (a whole number) with side (a whole number). Place "(" into built. Shape from node. Place ")" into built.

To shape from node (a whole number). If node is less than 0, return. The piece is format whole number item node of values as text. Place piece into built. The left is item node of lefts. The right is item node of rights. If left is less than 0 and right is less than 0, return. If left is at least 0 and right is at least 0, cover from left with 0, cover from right with 1, return. If left is at least 0, cover from left with 0, return. Place "()" into built. Cover from right with 1.

Shape from 0. Show built.

Expected stdout

1(2()(4))(3)

Open in Playground Standalone Page

#0609 · Find Duplicate File in System

Medium Array

Keep a shared listings holding "root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", and "root 4.txt(efgh)". Keep a shared parts as an empty list of text. Keep a shared folder as empty text. Keep a shared wanted as empty text. Keep a shared symbol as empty text. Keep a shared bracket of 0. Keep a shared contents as an empty list of text. Keep a shared groups as an empty list of text. Keep a shared atspot of 0. Keep a shared fullpath as empty text. Keep a shared content as empty text. Keep a shared line as empty text.
To seek from spot (a whole number) with finish (a whole number). If spot is more than finish, return. The piece is the slice of wanted from spot to spot plus 1. If piece matches symbol, set bracket to spot, return. Seek from spot plus 1 with finish.

To look from place (a whole number). If place is more than the last position of contents, return. If item place of contents matches wanted, set atspot to place, return. Look from place plus 1.

To attach from spot (a whole number). Set line to item spot of groups. Place "," into line. Place fullpath into line. Set item spot of groups to line.

To collect from place (a whole number) with finish (a whole number). If place is more than finish, return. The part is item place of parts. The final is the last position of part. Set wanted to part. Set symbol to empty text. Place "(" into symbol. Set bracket to -1. Seek from 0 with final. The nameend is bracket. The name is the slice of part from 0 to nameend. Set content to the slice of part from bracket plus 1 to final. Set fullpath to empty text. Place folder into fullpath. Place "/" into fullpath. Place name into fullpath. Set wanted to content. Set atspot to -1. Look from 0. If atspot is less than 0, Place content into contents. If atspot is less than 0, Place fullpath into groups. If atspot is at least 0, Attach from atspot. Collect from place plus 1 with finish.

For each listing in listings.
Set parts to split listing by " ". Set folder to item 0 of parts. Collect from 1 with the last position of parts.

The spot is the last position of groups.
Repeat while spot is at least 0.
Show item spot of groups. Subtract 1 from spot.

Expected stdout

root/a/2.txt,root/c/d/4.txt,root/4.txt
root/a/1.txt,root/c/3.txt

Open in Playground Standalone Page

#0611 · Valid Triangle Number

Medium Array

Keep a shared sides holding 2, 2, 3, and 4. Sort sides. The final is the last position of sides. The count is 0.
For every first from 0 to final.
For every second from first plus 1 to final.
For every third from second plus 1 to final.
The left is item first of sides. The right is item second of sides. The longest is item third of sides. If left plus right is more than longest, add 1 to count.

Show count.

Expected stdout

3

Open in Playground Standalone Page

#0617 · Merge Two Binary Trees

Easy Depth-First Search

Keep a shared values holding 1, 3, 2, 5, 2, 1, 3, 0, 4, 0, and 7. Keep a shared lefts holding 1, 3, -1, -1, 5, -1, -1, -1, -1, -1, and -1. Keep a shared rights holding 2, -1, -1, -1, 6, 8, 10, -1, -1, -1, and -1. Keep a shared pieces as an empty list of text.
To merge from first (a whole number) with second (a whole number). If first is less than 0 and second is less than 0, return. If first is less than 0, return. If second is less than 0, return. Increase item first of values by item second of values. The leftfirst is item first of lefts. The leftsecond is item second of lefts. If leftfirst is less than 0, set item first of lefts to leftsecond. If leftfirst is at least 0 and leftsecond is at least 0, merge from leftfirst with leftsecond. The rightfirst is item first of rights. The rightsecond is item second of rights. If rightfirst is less than 0, set item first of rights to rightsecond. If rightfirst is at least 0 and rightsecond is at least 0, merge from rightfirst with rightsecond.

To record from node (a whole number). The piece is format whole number item node of values as text. Place piece into pieces.

Merge from 0 with 4. The queue is an empty list of whole numbers. Place 0 into queue. The head is 0. The count is 1.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. If node is less than 0, Place "null" into pieces. If node is at least 0, record from node, Place item node of lefts into queue, Place item node of rights into queue, add 2 to count.

Repeat while the last position of pieces is at least 0.
The tail is the last position of pieces. If item tail of pieces matches "null", remove the last item from pieces, otherwise stop.

The line is join pieces with ",". Show line.

Expected stdout

3,4,5,5,4,null,7

Open in Playground Standalone Page

#0621 · Task Scheduler

Medium Array

The tasks is a list of "A", "A", "A", "B", "B", and "B". The gap is 2. Keep a shared run of 0. Keep a shared biggest of 0. Keep a shared leaders of 0. The joined is join tasks with "". The order is sort characters of joined. The last is the last position of order. The symbol is the slice of order from 0 to 1. Set run to 1.
To finish. If run is more than biggest, set biggest to run, set leaders to 1, return. If run is biggest, add 1 to leaders.

For every spot from 1 to last.
The piece is the slice of order from spot to spot plus 1. If piece matches symbol, add 1 to run, otherwise Finish, set symbol to piece, set run to 1.

Finish. The spread is (biggest minus 1) times (gap plus 1) plus leaders. The final is the last position of tasks. The total is final plus 1. If spread is less than total, show total, otherwise show spread.

Expected stdout

8

Open in Playground Standalone Page

#0623 · Add One Row to Tree

Medium Depth-First Search

Keep a shared values holding 4, 2, 6, 3, 1, and 5. Keep a shared lefts holding 1, 3, 5, -1, -1, and -1. Keep a shared rights holding 2, 4, -1, -1, -1, and -1. Keep a shared inserted of 1. Keep a shared depth of 2. Keep a shared made of 0. Keep a shared root of 0. Keep a shared pieces as an empty list of text.
To sprout. Place inserted into values. Place -1 into lefts. Place -1 into rights. Set made to the last position of values.

To fill from node (a whole number). The oldleft is item node of lefts. Sprout. Set item made of lefts to oldleft. Set item node of lefts to made. The oldright is item node of rights. Sprout. Set item made of rights to oldright. Set item node of rights to made.

To graft from node (a whole number) with level (a whole number). If node is less than 0, return. If level is depth minus 1, fill from node, return. If item node of lefts is at least 0, graft from item node of lefts with level plus 1. If item node of rights is at least 0, graft from item node of rights with level plus 1.

To record from node (a whole number). The piece is format whole number item node of values as text. Place piece into pieces.

If depth is 1, The oldroot is root, Sprout, Set item made of lefts to oldroot, Set root to made. If depth is more than 1, Graft from root with 1. The queue is an empty list of whole numbers. Place root into queue. The head is 0. The count is 1.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. If node is less than 0, Place "null" into pieces. If node is at least 0, record from node, Place item node of lefts into queue, Place item node of rights into queue, add 2 to count.

Repeat while the last position of pieces is at least 0.
The tail is the last position of pieces. If item tail of pieces matches "null", remove the last item from pieces, otherwise stop.

The line is join pieces with ",". Show line.

Expected stdout

4,1,1,2,null,null,6,3,1,5

Open in Playground Standalone Page

#0624 · Maximum Distance in Arrays

Medium Array

Keep a shared numbers holding 1, 2, 3, 4, 5, 1, 2, and 3. The starts is a list of 0, 3, and 5. The sizes is a list of 3, 2, and 3. Keep a shared smallest of 0. Keep a shared largest of 0.
To scan from spot (a whole number) with finish (a whole number). If spot is more than finish, return. The amount is item spot of numbers. If amount is less than smallest, set smallest to amount. If amount is more than largest, set largest to amount. Scan from spot plus 1 with finish.

Set smallest to item 0 of numbers. Set largest to item 0 of numbers. Scan from 0 with 2. The low is smallest. The high is largest. The best is 0.
For every band from 1 to 2.
The start is item band of starts. The finish is start plus item band of sizes minus 1. Set smallest to item start of numbers. Set largest to item start of numbers. Scan from start with finish. The down is largest minus low. The up is high minus smallest. If down is more than best, set best to down. If up is more than best, set best to up. If smallest is less than low, set low to smallest. If largest is more than high, set high to largest.

Show best.

Expected stdout

4

Open in Playground Standalone Page

#0628 · Maximum Product of Three Numbers

Easy Array

The numbers is a list of 1, 2, and 3. Sort numbers. The last is the last position of numbers. The high is 1.
For every spot from last minus 2 to last.
Set high to high times item spot of numbers.

The low is 1.
For every spot from 0 to 1.
Set low to low times item spot of numbers.

Set low to low times item last of numbers. If low is more than high, set high to low.

Show high.

Expected stdout

6

Open in Playground Standalone Page

#0629 · K Inverse Pairs Array

Hard Dynamic Programming

The size is 3. The pairs is 1. The rows is size plus 1. The columns is pairs plus 1. The total is rows times columns. The table is a list of total zeros. Set item 0 of table to 1.
For every length from 1 to size.
For every inversions from 0 to pairs.
The cell is length times columns plus inversions. The reach is inversions. If length minus 1 is less than reach, set reach to length minus 1. The base is (length minus 1) times columns.
For every used from 0 to reach.
The source is base plus inversions minus used. Set item cell of table to (item cell of table plus item source of table) modulo 1000000007.

The answer is item (size times columns plus pairs) of table. Show answer.

Expected stdout

2

Open in Playground Standalone Page

#0630 · Course Schedule III

Hard Array

Keep a shared durations holding 100, 200, 1000, and 2000. Keep a shared deadlines holding 200, 1300, 1250, and 3200. Keep a shared last of 3. Keep a shared picked of 0. Keep a shared best of 0. Keep a shared taken of 0. Keep a shared route as an empty list of whole numbers.
To seek from candidate (a whole number) with chosen (a whole number). If candidate is more than last, set picked to chosen, return. If item candidate of deadlines is less than item chosen of deadlines, set chosen to candidate. Seek from candidate plus 1 with chosen.

To place from spot (a whole number). The pick is spot. Seek from spot plus 1 with pick. If picked is not spot, swap item spot of deadlines and item picked of deadlines, swap item spot of durations and item picked of durations.

To record. If taken is more than best, set best to taken.

To explore from spot (a whole number) with total (a whole number). If spot is more than last, record, return. Explore from spot plus 1 with total. The finish is total plus item spot of durations.
If finish is at most item spot of deadlines.
Place spot into route. Add 1 to taken. Explore from spot plus 1 with finish. Subtract 1 from taken. Remove the last item from route.

For every spot from 0 to last minus 1.
Place from spot.

Explore from 0 with 0. Show best.

Expected stdout

3

Open in Playground Standalone Page

#0632 · Smallest Range Covering Elements from K Lists

Hard Array

Keep a shared values holding 4, 10, 15, 24, 26, 0, 9, 12, 20, 5, 18, 22, and 30. Keep a shared begins holding 0, 5, and 9. Keep a shared sizes holding 5, 4, and 4. Keep a shared target of 0. Keep a shared smallest of 0. Keep a shared foundone of 0. Keep a shared complete of 0. Keep a shared highest of 0. Keep a shared bestlow of 0. Keep a shared besthigh of 0. Keep a shared bestwidth of 1000000000.
To find from group (a whole number) with spot (a whole number). The base is item group of begins. The size is item group of sizes. If spot is at least size, set foundone to 0, return. The value is item (base plus spot) of values. If value is at least target, set smallest to value, set foundone to 1, return. Find from group with spot plus 1.

To test from spot (a whole number). Set target to item spot of values. Set highest to -1000000000. Set complete to 1.
For every group from 0 to 2.
Set foundone to 0. Find from group with 0. If foundone is 0, set complete to 0. If smallest is more than highest, set highest to smallest. If group is 2 and complete is 1 and highest minus target is less than bestwidth, set bestwidth to highest minus target, set bestlow to target, set besthigh to highest.

For every spot from 0 to 12.
Test from spot.

The report is empty text. Place format whole number bestlow as text into report. Place "," into report. Place format whole number besthigh as text into report. Show report.

Expected stdout

20,24

Open in Playground Standalone Page

#0633 · Sum of Square Numbers

Medium Math

The target is 5. The right is 0.
Repeat while right times right is at most target.
Add 1 to right.

Decrease right by 1. The left is 0. The found is 0.
Repeat while left is at most right.
The total is left times left plus right times right. If total is target, set found to 1, stop. If total is less than target, add 1 to left. If total is more than target, subtract 1 from right.

If found is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0636 · Exclusive Time of Functions

Medium Array

Keep a shared logs holding "0:start:0", "1:start:2", "1:end:5", and "0:end:6". Keep a shared stack as an empty list of whole numbers. Keep a shared elapsed holding 0 and 0. Keep a shared field as empty text. Keep a shared tally of 0. Keep a shared parsed of 0. Keep a shared prior of 0.
To read from spot (a whole number). If spot is more than the last position of field, set parsed to tally, return. The symbol is character spot of field.
If symbol is between "0" and "9".
The digit is the digit value of symbol. Set tally to tally times 10 plus digit. Read from spot plus 1.

To save from spot (a whole number) with amount (a whole number). Increase item spot of elapsed by amount.

To enter from worker (a whole number) with moment (a whole number). The top is the last position of stack. If top is at least 0, save from top with moment minus prior. Place worker into stack. Set prior to moment.

To exit from worker (a whole number) with moment (a whole number). Increase item worker of elapsed by moment minus prior plus 1. Remove the last item from stack. Set prior to moment plus 1.

For each entry in logs.
The fields is split entry by ":". The event is item 1 of fields. Set field to item 0 of fields. Set tally to 0. Read from 0. The worker is parsed. Set field to item 2 of fields. Set tally to 0. Read from 0. The moment is parsed. If event matches "start", enter from worker with moment. If event matches "end", exit from worker with moment.

The firsttime is item 0 of elapsed. The secondtime is item 1 of elapsed. The report is empty text. Place format whole number firsttime as text into report. Place "," into report. Place format whole number secondtime as text into report. Show report.

Expected stdout

3,4

Open in Playground Standalone Page