Skip to content

Latest commit

 

History

History
121 lines (77 loc) · 4.15 KB

array.md

File metadata and controls

121 lines (77 loc) · 4.15 KB

Array

  1. Chunk Array
  2. Two Sum
  3. Snail Traversal

Chunk Array

Description

Leetcode-2677

[Easy] Given an array and a chunk size, divide the array into multiple subarrays where each subarray is of length size. The length of the last subarray may be less than size if the number of elements is not evenly divisible by size.

Example

array = [1,2,3,4,5], size = 1 => [[1], [2], [3], [4], [5]]

array = [1,2,3,4,5], size = 2 => [[1,2], [3,4], [5]]

array = [1,2,3,4,5], size = 3 => [[1,2,3], [4,5]]

array = [1,2,3,4,5], size = 6 => [[1,2,3,4,5]]

array = [], size = 1 => []

Solution

  1. Create an empty array to hold chunks called 'chunked'
  2. Create 'index' start at 0
  3. While index is less than array.length
  4. Push a slice of length 'size' from 'array' into 'chunked' using slice() built-in method of array
  5. Add 'size' to 'index'

Implementation

Back to index

Two Sum

Description

Leetcode-1

[Easy] Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. Assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Example

array = [2, 7, 11, 15], target = 18 => [1, 2]
Because array[1], which has number 7 + array[2], which has number 11 = 9, 7 + 11 = 18, we return [0, 1].

Solution

One simple and straightforward solution is test all the combinations of any two numbers in the array and find out the indics. This solution involves two nested iterations through the array. It has the time complexity O(n2)

A more efficient solution is to use hash table with the key being the number and value being the index. Iterate through the array, for the current number array[i], we will check if its complementary number (target - array[i]) is in hash table. If it is, then the target is find and the result is i and the index for target -array[i].

Implementation

Back to index

Snail Traversal

Description

Leetcode-2624

[Medium] Given an array and rows and cols, transform the 1-D array into 2-D array organised in the pattern known as snail traversal order. Snail traversal starts at the top left cell with the first value of the given array, then moves through the entire first column from top to bottom, followed by moving to the next column on the right and traversing it from bottom to top. This pattern continues, alternating the direction of traversal with each column until the entire current array is covered.

If the rows * cols !== arr.length, the input is considered invalid.

Example

arr = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15];
rows = 5;
cols = 4;

The snail traversal should work like this:

img

Correspondingly, the 2-D array is represented as:

[
  [19, 17, 16, 15],
  [10, 1, 14, 4],
  [3, 2, 12, 20],
  [7, 5, 18, 11],
  [9, 8, 6, 13],
];

Solution

Iterate each element of the array, follow the rules of snail traversal to put each element in its corresponding position in the 2-D array.

Implementation