본문 바로가기

Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.Example 1:Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't ma.. 더보기
LRU Cache Design and implement a data structure for LRU (Least Recently Used) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least recently.. 더보기
Maximum Absolute Difference You are given an array of N integers, A1, A2 ,…, AN. Return maximum value of f(i, j) for all 1 ≤ i, j ≤ N. f(i, j) is defined as |A[i] - A[j]| + |i - j|, where |x| denotes absolute value of x.For example,A=[1, 3, -1] f(1, 1) = f(2, 2) = f(3, 3) = 0 f(1, 2) = f(2, 1) = |1 - 3| + |1 - 2| = 3 f(1, 3) = f(3, 1) = |1 - (-1)| + |1 - 3| = 4 f(2, 3) = f(3, 2) = |3 - (-1)| + |2 - 3| = 5 So, we return 5. .. 더보기
Max Sum Contiguous Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example:Given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6.For this problem, return the maximum sum. My Solutions:import sys def maxSubArray(A): size = len(A) max_sum = max(A[0], -sys.maxsize - 1) for i in range(size): result = A[i] for j in .. 더보기
Add One To Number Given a non-negative number represented as an array of digits,add 1 to the number ( increment the number represented by the digits ).The digits are stored such that the most significant digit is at the head of the list.Example:If the vector has [1, 2, 3]the returned vector should be [1, 2, 4]as 123 + 1 = 124. NOTE: Certain things are intentionally left unclear in this question which you should p.. 더보기
Min Steps in Infinite Grid You are in an infinite 2D grid where you can move in any of the 8 directions : (x,y) to (x+1, y), (x - 1, y), (x, y+1), (x, y-1), (x-1, y-1), (x+1,y+1), (x-1,y+1), (x+1,y-1) You are given a sequence of points and the order in which you need to cover the points. Give the minimum number of steps in which you can achieve it. You start from the first point.Input :Given two integer arrays A and B, wh.. 더보기
Print the Elements of a Linked List If you're new to linked lists, this is a great exercise for learning about them. Given a pointer to the head node of a linked list, print its elements in order, one element per line. If the head pointer is null (indicating the list is empty), don’t print anything.Input FormatThe void Print(Node* head) method takes the head node of a linked list as a parameter. Each struct Node has a data field (.. 더보기
Left Rotation A left rotation operation on an array of size shifts each of the array's elements unit to the left. For example, if left rotations are performed on array , then the array would become .Given an array of integers and a number, , perform left rotations on the array. Then print the updated array as a single line of space-separated integers.Input FormatThe first line contains two space-separated int.. 더보기
Dynamic Array Create a list, , of empty sequences, where each sequence is indexed from to . The elements within each of the sequences also use -indexing.Create an integer, , and initialize it to .The types of queries that can be performed on your list of sequences () are described below:Query: 1 x yFind the sequence, , at index in .Append integer to sequence .Query: 2 x yFind the sequence, , at index in .Find.. 더보기
2D Array - DS Context Given a 2D Array, :1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 We define an hourglass in to be a subset of values with indices falling in this pattern in 's graphical representation:a b c d e f g There are hourglasses in , and an hourglass sum is the sum of an hourglass' values.Task Calculate the hourglass sum for every hourglass in , then print the maximum ho.. 더보기