In today's session, we'd chill and vibe as usual over a couple of questions. Some of the questions will be in this Gist and we might choose some randomly from LeetCode. The questions are listed below, answers will be posted here subsequently.
Write a program that takes an array A and an index i rnto A, and rearranges the elements such
that all elements less than A[r] (the "pivot") appear first, followed by elements equal to the pivot,
followed by elements greater than the pivot. You are expected to have a function solution(array, index)
Example:
input: array = [0,1,2,0,2,1,1], index = 3
output: [0,0,1,2,2,1,1]
input: [0,1,0,1,1,2,2], index = 2
output: [0,0,1,1,1,2,2]
input: array = [-3, 0, -1, 1, 1, 4, 2], index = 2
output: [-3,-1,0,1,1,2,4]
Given an array A of n objects with Boolean-valued keys, reorder the array so that objects that have tje key false
appear first. The relative ordering of objects with key true
should not change. Use O(1) additional space and O(n) time.
Example:
input: array = [{'first': True}, {'second': False}, {'third': False}, {'fourth': True}, {'fifth': True}]
output: [{'second': False}, {'Third': False}, {'first': True}, {'fourth': True}, {'fifth': True}]
Question 1:
That does it in O(n) time and O(1) space.