arrays

package
v0.0.0-...-f0e9c47 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package arrays implements the solutions for the problem contained in chapter 5 in the book Elements of Programming Interviews.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Advance

func Advance(x []uint) bool

Advance advances through the array and returns if it is possible to advance to the last position. Time complexity O(N). Space Complexity O(1).

func Alternation

func Alternation(x []int)

Alternation rearranges the slice such that the elements are in the order: x[0] <= x[1] >= x[2] <= x[3] >= x[4] ... Takes O(nlogn) time and O(1) space

func ApplyPermutation

func ApplyPermutation(elems, permutation []int)

ApplyPermutation will apply the permutation in O(N) time and with additional O(N) space complexity.

func BuyAndSellStockOnce

func BuyAndSellStockOnce(prices []int) int

BuyAndSellStockOnce finds the maximum profit possible from the given stock prices in the slice returns the maximum profit. The time complexity is O(N) and the space complexity is O(1).

func DeleteDuplicates

func DeleteDuplicates(x []int) int

DeleteDuplicates deletes duplicate elements and returns the number of valid elements left in the slice. The time complexity is O(N), and the space complexity is O(1).

func GeneratePrimes

func GeneratePrimes(upperBound int) []uint

GeneratePrimes generates all prime numbers from 2 all the way up but no including upperBound. Time complexity is O(n log(log(n))), space complexity O(n)

func Increment

func Increment(digits []int) []int

Increment increments the number passed in as a slice of digits by one, and returns the new number. Time complexity O(n).

func LinearAlternation

func LinearAlternation(x []int)

LinearAlternation rearranges the slice such that the elements are in the order: x[0] <= x[1] >= x[2] <= x[3] >= x[4] ... Takes O(n) time and O(1) space

func Partition

func Partition(elems []int, i int) (int, int)

Partitions the slice in linear time, such that all elements less than the pivot appears before all elements equal to the pivot and elements which are greater than the pivot appears as last. The pivot is selected as elems[i].

func Rotate2DArray

func Rotate2DArray(matrix [][]int)

Rotate2DArray takes as input an nxn 2D array, and rotates the array by 90 degrees clockwise. Takes O(N^2) time and O(1) space complexity.

func SpiralOrdering

func SpiralOrdering(matrix [][]int) []int

SpiralOrdering takes an nxn 2D array and returns the spral ordering of the array. Takes O(N^2) time and O(1) space complexity.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL