EPI - Arrays

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Compute rows in Pascal's Triangle

- 1 4 6 4 1 - Divide 4 part 1st: 1 2nd: 4 6 (created from below rows [j - 1] + [j]) 3rd: 4 (copied from same rows from 2nd) 4th: 1

Move Zeros

- Always move zero to the end of current examined list.

- Sample online data - Packet sniffer provide a uniform packets for network session. - Input 1: Array of Integer - Input 2: already read k elements

- Brute force: Store all the packets read so far. Get random int to query packet. - Each time run there is a probability of k / (n + 1) to select (n + 1)th item - Generate a random num [0, numSeenSoFar]. If this num is in [0, k-1] repalce it.

Buy and sell a stock twice

- Find max profit from left -> right - Find max profit from right -> left - Sum of two profit

Merge Two Sorted Array (A and B) A has enough size to contains A & B

- Go from right to left (Big to Small)

- Sample offline data - k random subset

- Knuth Shuffle array - Iterate through all items i [0, n) - Pick a random integer in [0, i] and swap it - Only need to do it until k

Compute spiral ordering

- Need to find a clean solution.

Increment an arbitrary-precision Integer <1, 2, 9> + 1

- Plus from right to left - Insert 1 at 0 if overflow

Multiply two arbitrary precision integer

- Pretty similar to "Increment arbitrary-precision integer" - Just do like basic mutiply

Rotate a 2D array

- Rotate by layer - column index -> row index

Sudoku checker problems

- This is just a trivial problem to check our clean code

Contains Duplicate II Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.

- Use set of recent items in k range

Generate nonuniform random number

- With a specified probability array [p1, p2, ..., pN) - Convert to [0, p1 + p2, p1 + p2 + p1, ..., 1) - Generate a random number in [0, 1] - Select a number if it fall into a range [0, p1+p2]

Enumerate all primes to n

- array N [T, T, F, F]

Contains Duplicate entries in array

- check not in set could slow down the performance. - just add to the set and check if len(nums) != len(set(nums))

- Permute the elements of an array - Input is 2 array - 1st array: which one to swap - 2nd input array

- find all left most digit to swap

- Compute the next permutation - (0, 1, 2, 3): first - (3, 2, 1, 0): last - (0, 1, 2, 3) -> (0, 1, 3, 2)

- from right to left: find a decrease - find a smallest number for right sub array. This smallest number must be larger than (i - 1)th

Find All Numbers Disappeared in an Array

- negate all numbers - find any item that not negate

Delete duplicates from a sorted array [0, 0, 0, 1, 1, 3, 3, 3, 5]

- two pointers. - 1st pointer where we insert - 2nd find the difference BF: have an auxiliary array

zigzag conversion

zig zag

Dutch National Flag Problem

1. BF1: Have 3 arrays: smaller, equal, larger O(3xn) memory 2. BF2: 1 aux array: iterate the array 3 times and append to aux array. Memory O(n), time = 4n 3. Optimize: Do it in place with 3 index smaller, equal and larger.

Buy and sell stock once

BF: we calculate every single difference and find the biggest. - save only the biggest while calculate

Shuffling

Generate a random number [left, i] i go from [0, n)

Compute a random permutation

This is exactly Knuth Shuffle

Compute a random subset - takes a input positive integer size N - K <= N - return a size K subset of N

c

Advancing through an array: Write a program which takes an array of n integers, where A[i] denotes the maximum you can advance from index. Return T/F whether it's possible to go to the last index from 1st index [3,3,1,0,2,0,1] => 3->3->2->1

def test(arr): if len(arr) == 0: return False furthest = arr[-1] for i in xrange(len(arr) - 2, -1, -1): if arr[i] >= furthest - i: furthest = i return furthest == 0

Repeated Substring

repeated substring


Ensembles d'études connexes

Business Law - Chapter 20: Banking

View Set

Adult Health Exam #5 (CH Review Qs, W9 Quiz, Misc. Quizlets)

View Set

Test 3: Mobility, Tissue Integrity, and Intracranial Regulation

View Set

Merrill's atlas chapter 3 self-test

View Set

Ch 48 PrepU: Nursing Care of a Family when a Child has an Endocrine or a Metabolic Disorder

View Set

clinical psychology PSYC 4010 EXAM 1 quiz

View Set

Smartbook- Topic 11A Products Liability

View Set

Loss and Grieving - Adaptive Quizzing

View Set

Chapter 7: The Flow of Food: Storage

View Set